Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
taoQtiTest_actions_TestContent | |
0.00% |
0 / 47 |
|
0.00% |
0 / 3 |
306 | |
0.00% |
0 / 1 |
files | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
72 | |||
upload | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
42 | |||
download | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 |
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) 2014 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * |
21 | */ |
22 | |
23 | /** |
24 | * Tests Content Controller provide access to the files of an test |
25 | * |
26 | * @package taoQtiTest |
27 | */ |
28 | class taoQtiTest_actions_TestContent extends tao_actions_CommonModule |
29 | { |
30 | /** |
31 | * Returns a json encoded array describign a directory |
32 | * |
33 | * @throws common_exception_MissingParameter |
34 | * @return string |
35 | */ |
36 | public function files() |
37 | { |
38 | if (!$this->hasRequestParameter('uri')) { |
39 | throw new common_exception_MissingParameter('uri', __METHOD__); |
40 | } |
41 | $testUri = $this->getRequestParameter('uri'); |
42 | $test = new core_kernel_classes_Resource($testUri); |
43 | |
44 | if (!$this->hasRequestParameter('lang')) { |
45 | throw new common_exception_MissingParameter('lang', __METHOD__); |
46 | } |
47 | $testLang = $this->getRequestParameter('lang'); |
48 | |
49 | $subPath = $this->hasRequestParameter('path') ? $this->getRequestParameter('path') : '/'; |
50 | $depth = $this->hasRequestParameter('depth') ? $this->getRequestParameter('depth') : 1; |
51 | |
52 | //build filters |
53 | $filters = []; |
54 | if ($this->hasRequestParameter('filters')) { |
55 | $filterParameter = $this->getRequestParameter('filters'); |
56 | if (!empty($filterParameter)) { |
57 | if (preg_match('/\/\*/', $filterParameter)) { |
58 | common_Logger::w( |
59 | 'Stars mime type are not yet supported, filter "' . $filterParameter . '" will fail' |
60 | ); |
61 | } |
62 | $filters = array_map('trim', explode(',', $filterParameter)); |
63 | } |
64 | } |
65 | |
66 | $data = taoQtiTest_helpers_ResourceManager::buildDirectory($test, $testLang, $subPath, $depth, $filters); |
67 | echo json_encode($data); |
68 | } |
69 | |
70 | /** |
71 | * Upload a file to the item directory |
72 | * |
73 | * @throws common_exception_MissingParameter |
74 | */ |
75 | public function upload() |
76 | { |
77 | if (!$this->hasRequestParameter('uri')) { |
78 | throw new common_exception_MissingParameter('uri', __METHOD__); |
79 | } |
80 | $testUri = $this->getRequestParameter('uri'); |
81 | $test = new core_kernel_classes_Resource($testUri); |
82 | |
83 | if (!$this->hasRequestParameter('lang')) { |
84 | throw new common_exception_MissingParameter('lang', __METHOD__); |
85 | } |
86 | $testLang = $this->getRequestParameter('lang'); |
87 | |
88 | if (!$this->hasRequestParameter('path')) { |
89 | throw new common_exception_MissingParameter('path', __METHOD__); |
90 | } |
91 | |
92 | //TODO path traversal and null byte poison check ? |
93 | $baseDir = taoQtiTest_helpers_ResourceManager::getBaseDir($test); |
94 | $relPath = trim($this->getRequestParameter('path'), '/'); |
95 | $relPath = empty($relPath) ? '' : $relPath . '/'; |
96 | |
97 | $file = tao_helpers_Http::getUploadedFile('content'); |
98 | $fileName = $file['name']; |
99 | |
100 | if (!move_uploaded_file($file["tmp_name"], $baseDir . $relPath . $fileName)) { |
101 | throw new common_exception_Error('Unable to move uploaded file'); |
102 | } |
103 | |
104 | $fileData = taoQtiTest_helpers_ResourceManager::buildFile($test, $testLang, $relPath . $fileName); |
105 | echo json_encode($fileData); |
106 | } |
107 | |
108 | /** |
109 | * Download a file to the item directory* |
110 | * @throws common_exception_MissingParameter |
111 | */ |
112 | public function download() |
113 | { |
114 | if (!$this->hasRequestParameter('uri')) { |
115 | throw new common_exception_MissingParameter('uri', __METHOD__); |
116 | } |
117 | $testUri = $this->getRequestParameter('uri'); |
118 | $test = new core_kernel_classes_Resource($testUri); |
119 | |
120 | if (!$this->hasRequestParameter('path')) { |
121 | throw new common_exception_MissingParameter('path', __METHOD__); |
122 | } |
123 | |
124 | $baseDir = taoQtiTest_helpers_ResourceManager::getBaseDir($test); |
125 | $path = $baseDir . ltrim($this->getRequestParameter('path'), '/'); |
126 | |
127 | tao_helpers_Http::returnFile($path); |
128 | } |
129 | } |