Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_Export | |
0.00% |
0 / 27 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
| getExportPath | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getRelativPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExportFile | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| outputFile | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Utility class that focuses on export. |
| 25 | * |
| 26 | * @author Joel Bout, <joel@taotesting.com> |
| 27 | * @package tao |
| 28 | |
| 29 | */ |
| 30 | class tao_helpers_Export |
| 31 | { |
| 32 | /** |
| 33 | * get the path to save and retrieve the exported files regarding the current extension |
| 34 | * @return string the path |
| 35 | */ |
| 36 | public static function getExportPath() |
| 37 | { |
| 38 | $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tao_export'; |
| 39 | if (!file_exists($path)) { |
| 40 | mkdir($path); |
| 41 | } |
| 42 | return $path; |
| 43 | } |
| 44 | |
| 45 | public static function getRelativPath($file) |
| 46 | { |
| 47 | return ltrim(substr($file, strlen(self::getExportPath())), DIRECTORY_SEPARATOR); |
| 48 | } |
| 49 | |
| 50 | public static function getExportFile($filename = null) |
| 51 | { |
| 52 | if (is_null($filename)) { |
| 53 | $path = tempnam(self::getExportPath(), 'tao_export_'); |
| 54 | } else { |
| 55 | $path = self::getExportPath() . DIRECTORY_SEPARATOR . $filename; |
| 56 | } |
| 57 | return $path; |
| 58 | } |
| 59 | |
| 60 | public static function outputFile($relPath, $filename = null) |
| 61 | { |
| 62 | |
| 63 | $fullpath = self::getExportPath() . DIRECTORY_SEPARATOR . $relPath; |
| 64 | if (tao_helpers_File::securityCheck($fullpath, true) && file_exists($fullpath)) { |
| 65 | Context::getInstance()->getResponse()->setContentHeader(tao_helpers_File::getMimeType($fullpath)); |
| 66 | $fileName = empty($filename) ? basename($fullpath) : $filename; |
| 67 | header('Content-Disposition: attachment; fileName="' . $fileName . '"'); |
| 68 | header("Content-Length: " . filesize($fullpath)); |
| 69 | |
| 70 | //Clean all levels of output buffering |
| 71 | while (ob_get_level() > 0) { |
| 72 | ob_end_clean(); |
| 73 | } |
| 74 | |
| 75 | flush(); |
| 76 | $fp = fopen($fullpath, "r"); |
| 77 | if ($fp !== false) { |
| 78 | while (!feof($fp)) { |
| 79 | echo fread($fp, 65536); |
| 80 | flush(); |
| 81 | } |
| 82 | fclose($fp); |
| 83 | @unlink($fullpath); |
| 84 | } else { |
| 85 | common_Logger::e('Unable to open File to export' . $fullpath); |
| 86 | } |
| 87 | } else { |
| 88 | common_Logger::e('Could not find File to export: ' . $fullpath); |
| 89 | } |
| 90 | } |
| 91 | } |