| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 0.00% | 0 / 58 |  | 0.00% | 0 / 4 | CRAP |  | 0.00% | 0 / 1 | 
| QtiCssAuthoring |  | 0.00% | 0 / 58 |  | 0.00% | 0 / 4 | 380 |  | 0.00% | 0 / 1 | 
| save |  | 0.00% | 0 / 15 |  | 0.00% | 0 / 1 | 42 | |||
| load |  | 0.00% | 0 / 13 |  | 0.00% | 0 / 1 | 30 | |||
| getCssArray |  | 0.00% | 0 / 14 |  | 0.00% | 0 / 1 | 12 | |||
| download |  | 0.00% | 0 / 16 |  | 0.00% | 0 / 1 | 30 | |||
| 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 | namespace oat\taoQtiItem\controller; | 
| 24 | |
| 25 | use tao_actions_CommonModule; | 
| 26 | use oat\taoQtiItem\helpers\CssHelper; | 
| 27 | use tao_helpers_Request; | 
| 28 | use common_exception_IsAjaxAction; | 
| 29 | use common_exception_MissingParameter; | 
| 30 | use common_exception_InvalidArgumentType; | 
| 31 | |
| 32 | /** | 
| 33 | * Class QtiCssAuthoring | 
| 34 | * | 
| 35 | * @package oat\taoQtiItem\controller | 
| 36 | */ | 
| 37 | class QtiCssAuthoring extends tao_actions_CommonModule | 
| 38 | { | 
| 39 | /** | 
| 40 | * Save custom CSS as file | 
| 41 | * | 
| 42 | * @throws \common_exception_IsAjaxAction | 
| 43 | */ | 
| 44 | public function save() | 
| 45 | { | 
| 46 | if (!tao_helpers_Request::isAjax()) { | 
| 47 | throw new common_exception_IsAjaxAction(__METHOD__); | 
| 48 | } | 
| 49 | if (!$this->hasRequestParameter('uri')) { | 
| 50 | throw new common_exception_MissingParameter('uri', __METHOD__); | 
| 51 | } | 
| 52 | if (!$this->hasRequestParameter('stylesheetUri')) { | 
| 53 | throw new common_exception_MissingParameter('stylesheetUri', __METHOD__); | 
| 54 | } | 
| 55 | if (!$this->hasRequestParameter('lang')) { | 
| 56 | throw new common_exception_MissingParameter('lang', __METHOD__); | 
| 57 | } | 
| 58 | |
| 59 | $item = new \core_kernel_classes_Resource($this->getRequestParameter('uri')); | 
| 60 | $lang = $this->getRequestParameter('lang'); | 
| 61 | $styleSheet = $this->getRequestParameter('stylesheetUri'); | 
| 62 | if (!\tao_helpers_File::securityCheck($styleSheet, true)) { | 
| 63 | throw new \common_exception_Error('invalid stylesheet path "' . $styleSheet . '"'); | 
| 64 | } | 
| 65 | |
| 66 | $css = $this->getCssArray(); | 
| 67 | CssHelper::saveCssFile($item, $lang, $styleSheet, $css); | 
| 68 | } | 
| 69 | |
| 70 | /** | 
| 71 | * Load the custom styles as JSON | 
| 72 | * | 
| 73 | * @throws \common_exception_IsAjaxAction | 
| 74 | * @throws \common_exception_MissingParameter | 
| 75 | */ | 
| 76 | public function load() | 
| 77 | { | 
| 78 | |
| 79 | if (!$this->hasRequestParameter('uri')) { | 
| 80 | throw new common_exception_MissingParameter('uri', __METHOD__); | 
| 81 | } | 
| 82 | if (!$this->hasRequestParameter('stylesheetUri')) { | 
| 83 | throw new common_exception_MissingParameter('stylesheetUri', __METHOD__); | 
| 84 | } | 
| 85 | if (!$this->hasRequestParameter('lang')) { | 
| 86 | throw new common_exception_MissingParameter('lang', __METHOD__); | 
| 87 | } | 
| 88 | |
| 89 | $item = new \core_kernel_classes_Resource($this->getRequestParameter('uri')); | 
| 90 | $lang = $this->getRequestParameter('lang'); | 
| 91 | $styleSheet = $this->getRequestParameter('stylesheetUri'); | 
| 92 | if (!\tao_helpers_File::securityCheck($styleSheet, true)) { | 
| 93 | throw new \common_exception_Error('invalid stylesheet path "' . $styleSheet . '"'); | 
| 94 | } | 
| 95 | |
| 96 | $cssArray = CssHelper::loadCssFile($item, $lang, $styleSheet); | 
| 97 | |
| 98 | $this->returnJson($cssArray); | 
| 99 | } | 
| 100 | |
| 101 | /** | 
| 102 | * Convert CSS JSON to array | 
| 103 | * | 
| 104 | * @return mixed | 
| 105 | * @throws \common_exception_MissingParameter | 
| 106 | * @throws \common_exception_InvalidArgumentType | 
| 107 | */ | 
| 108 | private function getCssArray() | 
| 109 | { | 
| 110 | if (!$this->hasRequestParameter('cssJson')) { | 
| 111 | throw new common_exception_MissingParameter( | 
| 112 | 'cssJson', | 
| 113 | __CLASS__ . '::' . \Context::getInstance()->getActionName() | 
| 114 | ); | 
| 115 | } | 
| 116 | $cssArr = json_decode($_POST['cssJson'], true); | 
| 117 | if (!is_array($cssArr)) { | 
| 118 | throw new common_exception_InvalidArgumentType( | 
| 119 | __CLASS__, | 
| 120 | \Context::getInstance()->getActionName(), | 
| 121 | 0, | 
| 122 | 'json encoded array' | 
| 123 | ); | 
| 124 | } | 
| 125 | return $cssArr; | 
| 126 | } | 
| 127 | |
| 128 | /** | 
| 129 | * Download custom styles | 
| 130 | */ | 
| 131 | public function download() | 
| 132 | { | 
| 133 | |
| 134 | if (!$this->hasRequestParameter('uri')) { | 
| 135 | throw new common_exception_MissingParameter('uri', __METHOD__); | 
| 136 | } | 
| 137 | if (!$this->hasRequestParameter('stylesheetUri')) { | 
| 138 | throw new common_exception_MissingParameter('stylesheetUri', __METHOD__); | 
| 139 | } | 
| 140 | if (!$this->hasRequestParameter('lang')) { | 
| 141 | throw new common_exception_MissingParameter('lang', __METHOD__); | 
| 142 | } | 
| 143 | |
| 144 | |
| 145 | $item = new \core_kernel_classes_Resource($this->getRequestParameter('uri')); | 
| 146 | $lang = $this->getRequestParameter('lang'); | 
| 147 | $styleSheet = $this->getRequestParameter('stylesheetUri'); | 
| 148 | |
| 149 | if (!\tao_helpers_File::securityCheck($styleSheet, true)) { | 
| 150 | throw new \common_exception_Error('invalid stylesheet path "' . $styleSheet . '"'); | 
| 151 | } | 
| 152 | |
| 153 | header('Set-Cookie: fileDownload=true'); | 
| 154 | setcookie('fileDownload', 'true', 0, '/'); | 
| 155 | header('Content-type: application/octet-stream'); | 
| 156 | header(sprintf('Content-Disposition: attachment; filename=%s', basename($styleSheet))); | 
| 157 | echo CssHelper::downloadCssFile($item, $lang, $styleSheet); | 
| 158 | } | 
| 159 | } |