Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| tao_install_services_SyncService | |
0.00% |
0 / 53 |
|
0.00% |
0 / 7 |
462 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| getRootUrl | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getAvailableDrivers | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getAvailableLanguages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getAvailableTimezones | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| checkData | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 | |||
| 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 | /** |
| 28 | * A Service implementation aiming at checking if the server side can talk 'JSON' and |
| 29 | * receive information from the server to be 'synchronized' with it. |
| 30 | * Information received are the TAO root URL, ... |
| 31 | * |
| 32 | * Please refer to tao/install/api.php for more information about how to call this service. |
| 33 | * |
| 34 | * @access public |
| 35 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 36 | * @package tao |
| 37 | |
| 38 | */ |
| 39 | class tao_install_services_SyncService extends tao_install_services_Service |
| 40 | { |
| 41 | /** |
| 42 | * Creates a new instance of the service. |
| 43 | * @param tao_install_services_Data $data The input data to be handled by the service. |
| 44 | * @throws InvalidArgumentException If the input data structured is malformed or is missing data. |
| 45 | */ |
| 46 | public function __construct(tao_install_services_Data $data) |
| 47 | { |
| 48 | parent::__construct($data); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Executes the main logic of the service. |
| 53 | * @return tao_install_services_Data The result of the service execution. |
| 54 | */ |
| 55 | public function execute() |
| 56 | { |
| 57 | $ext = common_configuration_ComponentFactory::buildPHPExtension('json'); |
| 58 | $report = $ext->check(); |
| 59 | |
| 60 | // We fake JSON encoding for a gracefull response in any case. |
| 61 | $json = $report->getStatus() == common_configuration_Report::VALID; |
| 62 | if (!$json) { |
| 63 | $data = '{"type": "SyncReport", "value": { "json": ' . (($json) ? 'true' : 'false') . '}}'; |
| 64 | } else { |
| 65 | $localesDir = dirname(__FILE__) . '/../../locales'; |
| 66 | $data = json_encode(['type' => 'SyncReport', 'value' => [ |
| 67 | 'json' => true, |
| 68 | 'rootURL' => self::getRootUrl(), |
| 69 | 'availableDrivers' => self::getAvailableDrivers(), |
| 70 | 'availableLanguages' => self::getAvailableLanguages($localesDir), |
| 71 | 'availableTimezones' => self::getAvailableTimezones() |
| 72 | ]]); |
| 73 | } |
| 74 | |
| 75 | $this->setResult(new tao_install_services_Data($data)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Computes the root URL of the platform based on the current |
| 80 | * request. |
| 81 | * |
| 82 | * @return mixed |
| 83 | */ |
| 84 | private static function getRootUrl() |
| 85 | { |
| 86 | // Returns TAO ROOT url based on a call to the API. |
| 87 | $isHTTPS = common_http_Request::isHttps(); |
| 88 | $host = $_SERVER['HTTP_HOST']; |
| 89 | $uri = $_SERVER['REQUEST_URI']; |
| 90 | $currentUrl = ($isHTTPS ? 'https' : 'http') . '://' . $host . $uri; |
| 91 | $parsed = parse_url($currentUrl); |
| 92 | $port = (empty($parsed['port'])) ? '' : ':' . $parsed['port']; |
| 93 | $rootUrl = $parsed['scheme'] . '://' . $parsed['host'] . $port . $parsed['path']; |
| 94 | return str_replace('/tao/install/api.php', '', $rootUrl); |
| 95 | } |
| 96 | |
| 97 | private static function getAvailableDrivers() |
| 98 | { |
| 99 | $compatibleDrivers = ['pdo_mysql', 'pdo_pgsql', 'pdo_sqlsrv','pdo_oci']; |
| 100 | $availableDrivers = []; |
| 101 | |
| 102 | foreach ($compatibleDrivers as $cD) { |
| 103 | $check = common_configuration_ComponentFactory::buildPHPDatabaseDriver($cD); |
| 104 | $report = $check->check(); |
| 105 | |
| 106 | if ($report->getStatus() == common_configuration_Report::VALID) { |
| 107 | $availableDrivers[] = $cD; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return $availableDrivers; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the list of available languages in terms of locales in the /tao meta-extension folder. |
| 116 | * |
| 117 | * @param string $localesPath The path to the /locales directory to scan into. |
| 118 | * @param boolean $sort Sort by alphetical order. |
| 119 | * @return array an array of languages where keys are language tags and values are language labels in english (EN). |
| 120 | */ |
| 121 | private static function getAvailableLanguages($localesPath, $sort = true) |
| 122 | { |
| 123 | $languages = []; |
| 124 | |
| 125 | try { |
| 126 | $languages = tao_install_utils_System::getAvailableLocales($localesPath); |
| 127 | if (true == $sort) { |
| 128 | asort($languages); |
| 129 | } |
| 130 | } catch (Exception $e) { |
| 131 | // Do nothing and return gracefully. |
| 132 | } |
| 133 | |
| 134 | return $languages; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get available timezones on the server side. The returned value |
| 139 | * corresponds to the value returned by DateTimeZone::listIdentifiers()'s PHP |
| 140 | * method. |
| 141 | * |
| 142 | * @return array An array where keys are integers and values are PHP timezone identifiers. |
| 143 | * @see http://www.php.net/manual/en/datetimezone.listidentifiers.php PHP's DateTimeZone::listIdentifiers method. |
| 144 | * @see http://php.net/manual/en/timezones.php For the list of PHP's timezone identifiers. |
| 145 | */ |
| 146 | private static function getAvailableTimezones() |
| 147 | { |
| 148 | return DateTimeZone::listIdentifiers(); |
| 149 | |
| 150 | // get full list of timezone identifiers and add UTC value with the display |
| 151 | $timezone_identifiers = DateTimeZone::listIdentifiers(); |
| 152 | $timezones = []; |
| 153 | foreach ($timezone_identifiers as $timezone_identifier) { |
| 154 | $now = new DateTime(null, new DateTimeZone($timezone_identifier)); |
| 155 | str_replace('_', ' ', $timezone_identifier); |
| 156 | $utcValue = $now->getOffset() / 3600; |
| 157 | $utcHours = floor($utcValue); |
| 158 | $utcMinutes = ($utcValue - $utcHours) * 60; |
| 159 | $utcPrint = sprintf('%d:%02d', $utcHours, $utcMinutes); |
| 160 | $utcValue = ($utcHours > 0) ? (' +' . $utcPrint) : (($utcHours == 0) ? '' : ' ' . $utcPrint); |
| 161 | array_push($timezones, $timezone_identifier . " (UTC" . $utcValue . ")"); |
| 162 | } |
| 163 | |
| 164 | return $timezones; |
| 165 | } |
| 166 | |
| 167 | protected function checkData() |
| 168 | { |
| 169 | // Check data integrity. |
| 170 | $content = $this->getData()->getContent(); |
| 171 | if (!isset($content['type']) || empty($content['type']) || $content['type'] !== 'Sync') { |
| 172 | throw new InvalidArgumentException("Unexpected type: 'type' must be equal to 'Sync'."); |
| 173 | } |
| 174 | } |
| 175 | } |