Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 75 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| taoItems_helpers_Deployment | |
0.00% |
0 / 75 |
|
0.00% |
0 / 3 |
342 | |
0.00% |
0 / 1 |
| copyResources | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| retrieveExternalResources | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
56 | |||
| retrieveFile | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
72 | |||
| 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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | */ |
| 25 | |
| 26 | /** |
| 27 | * Helper for the deployment of items |
| 28 | * |
| 29 | * @access public |
| 30 | * @author Bout Joel, <joel@taotesting.com> |
| 31 | * @package taoItems |
| 32 | |
| 33 | */ |
| 34 | class taoItems_helpers_Deployment |
| 35 | { |
| 36 | private static $defaultMedia = [ |
| 37 | "jpg", |
| 38 | "jpeg", |
| 39 | "png", |
| 40 | "gif", |
| 41 | "mp3", |
| 42 | 'mp4', |
| 43 | 'webm', |
| 44 | 'swf', |
| 45 | 'wma', |
| 46 | 'wav', |
| 47 | 'css', |
| 48 | 'js', |
| 49 | ]; |
| 50 | |
| 51 | /** |
| 52 | * Copy the resources from one directory to another |
| 53 | * |
| 54 | * @param string $sourceDirectory |
| 55 | * @param string $destinationDirectory |
| 56 | * @param array $excludeFiles |
| 57 | * @return boolean |
| 58 | */ |
| 59 | public static function copyResources($sourceDirectory, $destinationDirectory, $excludeFiles = []) |
| 60 | { |
| 61 | //copy the resources |
| 62 | $exclude = array_merge($excludeFiles, ['.', '..', '.svn']); |
| 63 | $success = true; |
| 64 | foreach (scandir($sourceDirectory) as $file) { |
| 65 | if (!in_array($file, $exclude)) { |
| 66 | $success &= tao_helpers_File::copy( |
| 67 | $sourceDirectory . $file, |
| 68 | $destinationDirectory . $file, |
| 69 | true |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | return $success; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * |
| 78 | * @param unknown $xhtml |
| 79 | * @param unknown $destination |
| 80 | * @return common_report_Report |
| 81 | */ |
| 82 | public static function retrieveExternalResources($xhtml, $destination) |
| 83 | { |
| 84 | |
| 85 | if (!file_exists($destination)) { |
| 86 | if (!mkdir($destination)) { |
| 87 | common_Logger::e('Folder ' . $destination . ' could not be created'); |
| 88 | return new common_report_Report( |
| 89 | common_report_Report::TYPE_ERROR, |
| 90 | __('Unable to create deployement directory'), |
| 91 | $xhtml |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | $authorizedMedia = self::$defaultMedia; |
| 97 | |
| 98 | $mediaList = []; |
| 99 | //take into account json encoded url |
| 100 | $expr = "/http[s]?:(\\\\)?\/(\\\\)?\/[^<'\"&?]+\.(" . implode('|', $authorizedMedia) . ")/mi"; |
| 101 | preg_match_all($expr, $xhtml, $mediaList, PREG_PATTERN_ORDER); |
| 102 | |
| 103 | $uniqueMediaList = array_unique($mediaList[0]); |
| 104 | |
| 105 | $report = new common_report_Report(common_report_Report::TYPE_SUCCESS, __('Retrieving external resources')); |
| 106 | |
| 107 | foreach ($uniqueMediaList as $mediaUrl) { |
| 108 | // This is a file that has to be stored in the item compilation folder itself... |
| 109 | // I do not get why they are all copied. They are all there they were copied from the item module... |
| 110 | // But I agree that remote resources (somewhere on the Internet) should be copied via curl. |
| 111 | // So if the URL does not matches a place where the TAO server is, we curl the resource and store it. |
| 112 | // FileManager files should be considered as remote resources to avoid 404 issues. Indeed, a backoffice |
| 113 | // user might delete an image in the filemanager during a delivery campain. This is dangerous. |
| 114 | |
| 115 | $decodedMediaUrl = str_replace('\/', '/', $mediaUrl); |
| 116 | |
| 117 | $mediaPath = self::retrieveFile($decodedMediaUrl, $destination); |
| 118 | if (!empty($mediaPath) && $mediaPath !== false) { |
| 119 | //replace only when copyFile is successful |
| 120 | $xhtml = str_replace($mediaUrl, basename($mediaPath), $xhtml, $replaced); |
| 121 | } else { |
| 122 | $report->add( |
| 123 | new common_report_Report( |
| 124 | common_report_Report::TYPE_ERROR, |
| 125 | __('Failed retrieving %s', $decodedMediaUrl) |
| 126 | ) |
| 127 | ); |
| 128 | $report->setType(common_report_Report::TYPE_ERROR); |
| 129 | } |
| 130 | } |
| 131 | if ($report->getType() == common_report_Report::TYPE_SUCCESS) { |
| 132 | $report->setData($xhtml); |
| 133 | } |
| 134 | return $report; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Retrieve a file from a given $url and copy it to its final $destination. |
| 139 | * |
| 140 | * @param string $url The URL to be dereferenced. |
| 141 | * @param string $destination The destination of the retrieved file, on the file system. |
| 142 | * @return boolean|string false If an error occurs during the retrieval/copy process, or the final destination name |
| 143 | * if it succeeds. |
| 144 | */ |
| 145 | public static function retrieveFile($url, $destination) |
| 146 | { |
| 147 | |
| 148 | $fileName = basename($url); |
| 149 | // check file name compatibility: |
| 150 | // e.g. if a file with a common name (e.g. car.jpg, house.png, sound.mp3) already exists in the destination |
| 151 | // folder |
| 152 | while (file_exists($destination . $fileName)) { |
| 153 | $lastDot = strrpos($fileName, '.'); |
| 154 | $fileName = substr($fileName, 0, $lastDot) . '_' . substr($fileName, $lastDot); |
| 155 | } |
| 156 | |
| 157 | // Since the file has not been downloaded yet, start downloading it using cUrl |
| 158 | // Only if the resource is external, else we copy it |
| 159 | if (!preg_match('@^' . ROOT_URL . '@', $url)) { |
| 160 | common_Logger::d('Downloading ' . $url); |
| 161 | helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::NO_TIMEOUT); |
| 162 | $fp = fopen($destination . $fileName, 'w+'); |
| 163 | $curlHandler = curl_init(); |
| 164 | curl_setopt($curlHandler, CURLOPT_URL, $url); |
| 165 | curl_setopt($curlHandler, CURLOPT_FILE, $fp); |
| 166 | curl_setopt($curlHandler, CURLOPT_TIMEOUT, 50); |
| 167 | curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, true); |
| 168 | |
| 169 | //if there is an http auth on the local domain, it's mandatory to auth with curl |
| 170 | if (USE_HTTP_AUTH) { |
| 171 | $addAuth = false; |
| 172 | $domains = ['localhost', '127.0.0.1', ROOT_URL]; |
| 173 | |
| 174 | foreach ($domains as $domain) { |
| 175 | if (preg_match("/" . preg_quote($domain, '/') . "/", $url)) { |
| 176 | $addAuth = true; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if ($addAuth) { |
| 181 | curl_setopt($curlHandler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
| 182 | curl_setopt($curlHandler, CURLOPT_USERPWD, USE_HTTP_USER . ":" . USE_HTTP_PASS); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | curl_exec($curlHandler); |
| 187 | $httpCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); |
| 188 | $success = $httpCode == 200; |
| 189 | curl_close($curlHandler); |
| 190 | fclose($fp); |
| 191 | helpers_TimeOutHelper::reset(); |
| 192 | } else { |
| 193 | $path = tao_helpers_File::getPathFromUrl($url); |
| 194 | common_Logger::d('Copying ' . $path); |
| 195 | $success = helpers_File::copy($path, $destination . $fileName); |
| 196 | } |
| 197 | |
| 198 | if ($success == false) { |
| 199 | common_Logger::w('Unable to retrieve ' . $url); |
| 200 | return false; |
| 201 | } else { |
| 202 | return $destination . $fileName; |
| 203 | } |
| 204 | } |
| 205 | } |