[5357] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[5357] | 4 | // +-----------------------------------------------------------------------+ |
---|
[26461] | 5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
[5357] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
| 23 | |
---|
| 24 | class languages |
---|
| 25 | { |
---|
| 26 | var $fs_languages = array(); |
---|
| 27 | var $db_languages = array(); |
---|
| 28 | var $server_languages = array(); |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Initialize $fs_languages and $db_languages |
---|
| 32 | */ |
---|
| 33 | function languages($target_charset = null) |
---|
| 34 | { |
---|
[9520] | 35 | $this->get_fs_languages($target_charset); |
---|
[5357] | 36 | } |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Perform requested actions |
---|
| 40 | * @param string - action |
---|
| 41 | * @param string - language id |
---|
| 42 | * @param array - errors |
---|
| 43 | */ |
---|
| 44 | function perform_action($action, $language_id) |
---|
| 45 | { |
---|
| 46 | global $conf; |
---|
| 47 | |
---|
| 48 | if (isset($this->db_languages[$language_id])) |
---|
| 49 | { |
---|
| 50 | $crt_db_language = $this->db_languages[$language_id]; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | $errors = array(); |
---|
| 54 | |
---|
| 55 | switch ($action) |
---|
| 56 | { |
---|
| 57 | case 'activate': |
---|
| 58 | if (isset($crt_db_language)) |
---|
| 59 | { |
---|
[25018] | 60 | $errors[] = 'CANNOT ACTIVATE - LANGUAGE IS ALREADY ACTIVATED'; |
---|
[5357] | 61 | break; |
---|
| 62 | } |
---|
| 63 | |
---|
[5452] | 64 | $query = ' |
---|
| 65 | INSERT INTO '.LANGUAGES_TABLE.' |
---|
[9518] | 66 | (id, version, name) |
---|
| 67 | VALUES(\''.$language_id.'\', |
---|
| 68 | \''.$this->fs_languages[$language_id]['version'].'\', |
---|
| 69 | \''.$this->fs_languages[$language_id]['name'].'\') |
---|
[5452] | 70 | ;'; |
---|
[5357] | 71 | pwg_query($query); |
---|
| 72 | break; |
---|
| 73 | |
---|
| 74 | case 'deactivate': |
---|
| 75 | if (!isset($crt_db_language)) |
---|
| 76 | { |
---|
[25018] | 77 | $errors[] = 'CANNOT DEACTIVATE - LANGUAGE IS ALREADY DEACTIVATED'; |
---|
[5357] | 78 | break; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | if ($language_id == get_default_language()) |
---|
| 82 | { |
---|
[25018] | 83 | $errors[] = 'CANNOT DEACTIVATE - LANGUAGE IS DEFAULT LANGUAGE'; |
---|
[5357] | 84 | break; |
---|
| 85 | } |
---|
[6355] | 86 | |
---|
[6550] | 87 | $query = ' |
---|
[5357] | 88 | DELETE |
---|
[6550] | 89 | FROM '.LANGUAGES_TABLE.' |
---|
| 90 | WHERE id= \''.$language_id.'\' |
---|
| 91 | ;'; |
---|
[5357] | 92 | pwg_query($query); |
---|
| 93 | break; |
---|
| 94 | |
---|
| 95 | case 'delete': |
---|
| 96 | if (!empty($crt_db_language)) |
---|
| 97 | { |
---|
[25018] | 98 | $errors[] = 'CANNOT DELETE - LANGUAGE IS ACTIVATED'; |
---|
[5357] | 99 | break; |
---|
| 100 | } |
---|
| 101 | if (!isset($this->fs_languages[$language_id])) |
---|
| 102 | { |
---|
[25018] | 103 | $errors[] = 'CANNOT DELETE - LANGUAGE DOES NOT EXIST'; |
---|
[5357] | 104 | break; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // Set default language to user who are using this language |
---|
| 108 | $query = ' |
---|
| 109 | UPDATE '.USER_INFOS_TABLE.' |
---|
[6550] | 110 | SET language = \''.get_default_language().'\' |
---|
| 111 | WHERE language = \''.$language_id.'\' |
---|
[5357] | 112 | ;'; |
---|
| 113 | pwg_query($query); |
---|
| 114 | |
---|
[23821] | 115 | deltree(PHPWG_ROOT_PATH.'language/'.$language_id, PHPWG_ROOT_PATH.'language/trash'); |
---|
[5357] | 116 | break; |
---|
| 117 | |
---|
| 118 | case 'set_default': |
---|
| 119 | $query = ' |
---|
| 120 | UPDATE '.USER_INFOS_TABLE.' |
---|
[6550] | 121 | SET language = \''.$language_id.'\' |
---|
[9549] | 122 | WHERE user_id IN ('.$conf['default_user_id'].', '.$conf['guest_id'].') |
---|
[5357] | 123 | ;'; |
---|
| 124 | pwg_query($query); |
---|
| 125 | break; |
---|
| 126 | } |
---|
| 127 | return $errors; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Get languages defined in the language directory |
---|
[6355] | 132 | */ |
---|
[5357] | 133 | function get_fs_languages($target_charset = null) |
---|
| 134 | { |
---|
| 135 | if ( empty($target_charset) ) |
---|
| 136 | { |
---|
| 137 | $target_charset = get_pwg_charset(); |
---|
| 138 | } |
---|
| 139 | $target_charset = strtolower($target_charset); |
---|
| 140 | |
---|
| 141 | $dir = opendir(PHPWG_ROOT_PATH.'language'); |
---|
| 142 | while ($file = readdir($dir)) |
---|
| 143 | { |
---|
[9518] | 144 | if ($file!='.' and $file!='..') |
---|
[5357] | 145 | { |
---|
[9518] | 146 | $path = PHPWG_ROOT_PATH.'language/'.$file; |
---|
| 147 | if (is_dir($path) and !is_link($path) |
---|
| 148 | and preg_match('/^[a-zA-Z0-9-_]+$/', $file ) |
---|
| 149 | and file_exists($path.'/common.lang.php') |
---|
| 150 | ) |
---|
| 151 | { |
---|
| 152 | $language = array( |
---|
[9520] | 153 | 'name'=>$file, |
---|
[9518] | 154 | 'code'=>$file, |
---|
| 155 | 'version'=>'0', |
---|
| 156 | 'uri'=>'', |
---|
| 157 | 'author'=>'', |
---|
| 158 | ); |
---|
| 159 | $plg_data = implode( '', file($path.'/common.lang.php') ); |
---|
[5357] | 160 | |
---|
[9518] | 161 | if ( preg_match("|Language Name: (.*)|", $plg_data, $val) ) |
---|
| 162 | { |
---|
| 163 | $language['name'] = trim( $val[1] ); |
---|
| 164 | $language['name'] = convert_charset($language['name'], 'utf-8', $target_charset); |
---|
| 165 | } |
---|
| 166 | if (preg_match("|Version: (.*)|", $plg_data, $val)) |
---|
| 167 | { |
---|
| 168 | $language['version'] = trim($val[1]); |
---|
| 169 | } |
---|
| 170 | if ( preg_match("|Language URI: (.*)|", $plg_data, $val) ) |
---|
| 171 | { |
---|
| 172 | $language['uri'] = trim($val[1]); |
---|
| 173 | } |
---|
| 174 | if ( preg_match("|Author: (.*)|", $plg_data, $val) ) |
---|
| 175 | { |
---|
| 176 | $language['author'] = trim($val[1]); |
---|
| 177 | } |
---|
| 178 | if ( preg_match("|Author URI: (.*)|", $plg_data, $val) ) |
---|
| 179 | { |
---|
| 180 | $language['author uri'] = trim($val[1]); |
---|
| 181 | } |
---|
| 182 | if (!empty($language['uri']) and strpos($language['uri'] , 'extension_view.php?eid=')) |
---|
| 183 | { |
---|
| 184 | list( , $extension) = explode('extension_view.php?eid=', $language['uri']); |
---|
| 185 | if (is_numeric($extension)) $language['extension'] = $extension; |
---|
| 186 | } |
---|
| 187 | // IMPORTANT SECURITY ! |
---|
| 188 | $language = array_map('htmlspecialchars', $language); |
---|
| 189 | $this->fs_languages[$file] = $language; |
---|
| 190 | } |
---|
[5357] | 191 | } |
---|
| 192 | } |
---|
| 193 | closedir($dir); |
---|
[9518] | 194 | @uasort($this->fs_languages, 'name_compare'); |
---|
[5357] | 195 | } |
---|
| 196 | |
---|
| 197 | function get_db_languages() |
---|
| 198 | { |
---|
| 199 | $query = ' |
---|
| 200 | SELECT id, name |
---|
| 201 | FROM '.LANGUAGES_TABLE.' |
---|
| 202 | ORDER BY name ASC |
---|
| 203 | ;'; |
---|
| 204 | $result = pwg_query($query); |
---|
| 205 | |
---|
| 206 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 207 | { |
---|
| 208 | $this->db_languages[ $row['id'] ] = $row['name']; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
[5371] | 213 | * Retrieve PEM server datas to $server_languages |
---|
| 214 | */ |
---|
[9518] | 215 | function get_server_languages($new=false) |
---|
[5371] | 216 | { |
---|
[20449] | 217 | global $user, $conf; |
---|
[5371] | 218 | |
---|
[8083] | 219 | $get_data = array( |
---|
[20449] | 220 | 'category_id' => $conf['pem_languages_category'], |
---|
[8083] | 221 | 'format' => 'php', |
---|
| 222 | ); |
---|
[5371] | 223 | |
---|
| 224 | // Retrieve PEM versions |
---|
| 225 | $version = PHPWG_VERSION; |
---|
| 226 | $versions_to_check = array(); |
---|
[8083] | 227 | $url = PEM_URL . '/api/get_version_list.php'; |
---|
[8238] | 228 | if (fetchRemote($url, $result, $get_data) and $pem_versions = @unserialize($result)) |
---|
[5371] | 229 | { |
---|
[11185] | 230 | if (!preg_match('/^\d+\.\d+\.\d+$/', $version)) |
---|
[5371] | 231 | { |
---|
| 232 | $version = $pem_versions[0]['name']; |
---|
| 233 | } |
---|
[16179] | 234 | $branch = get_branch_from_version($version); |
---|
[5371] | 235 | foreach ($pem_versions as $pem_version) |
---|
| 236 | { |
---|
| 237 | if (strpos($pem_version['name'], $branch) === 0) |
---|
| 238 | { |
---|
| 239 | $versions_to_check[] = $pem_version['id']; |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | if (empty($versions_to_check)) |
---|
| 244 | { |
---|
| 245 | return false; |
---|
| 246 | } |
---|
| 247 | |
---|
[9518] | 248 | // Languages to check |
---|
| 249 | $languages_to_check = array(); |
---|
| 250 | foreach($this->fs_languages as $fs_language) |
---|
| 251 | { |
---|
| 252 | if (isset($fs_language['extension'])) |
---|
| 253 | { |
---|
| 254 | $languages_to_check[] = $fs_language['extension']; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
[5371] | 258 | // Retrieve PEM languages infos |
---|
[8083] | 259 | $url = PEM_URL . '/api/get_revision_list.php'; |
---|
| 260 | $get_data = array_merge($get_data, array( |
---|
| 261 | 'last_revision_only' => 'true', |
---|
| 262 | 'version' => implode(',', $versions_to_check), |
---|
| 263 | 'lang' => $user['language'], |
---|
[9520] | 264 | 'get_nb_downloads' => 'true', |
---|
[8083] | 265 | ) |
---|
| 266 | ); |
---|
[9518] | 267 | if (!empty($languages_to_check)) |
---|
| 268 | { |
---|
| 269 | if ($new) |
---|
| 270 | { |
---|
| 271 | $get_data['extension_exclude'] = implode(',', $languages_to_check); |
---|
| 272 | } |
---|
| 273 | else |
---|
| 274 | { |
---|
| 275 | $get_data['extension_include'] = implode(',', $languages_to_check); |
---|
| 276 | } |
---|
| 277 | } |
---|
[5371] | 278 | |
---|
[8083] | 279 | if (fetchRemote($url, $result, $get_data)) |
---|
[5371] | 280 | { |
---|
| 281 | $pem_languages = @unserialize($result); |
---|
| 282 | if (!is_array($pem_languages)) |
---|
| 283 | { |
---|
| 284 | return false; |
---|
| 285 | } |
---|
| 286 | foreach ($pem_languages as $language) |
---|
| 287 | { |
---|
[9518] | 288 | if (preg_match('/^.*? \[[A-Z]{2}\]$/', $language['extension_name'])) |
---|
[5371] | 289 | { |
---|
[9520] | 290 | $this->server_languages[$language['extension_id']] = $language; |
---|
[5371] | 291 | } |
---|
| 292 | } |
---|
[9520] | 293 | @uasort($this->server_languages, array($this, 'extension_name_compare')); |
---|
[5371] | 294 | return true; |
---|
| 295 | } |
---|
| 296 | return false; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * Extract language files from archive |
---|
| 301 | * |
---|
| 302 | * @param string - install or upgrade |
---|
| 303 | * @param string - remote revision identifier (numeric) |
---|
| 304 | * @param string - language id or extension id |
---|
| 305 | */ |
---|
| 306 | function extract_language_files($action, $revision, $dest='') |
---|
| 307 | { |
---|
| 308 | if ($archive = tempnam( PHPWG_ROOT_PATH.'language', 'zip')) |
---|
| 309 | { |
---|
[8083] | 310 | $url = PEM_URL . '/download.php'; |
---|
| 311 | $get_data = array( |
---|
| 312 | 'rid' => $revision, |
---|
| 313 | 'origin' => 'piwigo_'.$action, |
---|
| 314 | ); |
---|
[5371] | 315 | |
---|
[8083] | 316 | if ($handle = @fopen($archive, 'wb') and fetchRemote($url, $handle, $get_data)) |
---|
[5371] | 317 | { |
---|
| 318 | fclose($handle); |
---|
[19167] | 319 | include_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); |
---|
[5371] | 320 | $zip = new PclZip($archive); |
---|
| 321 | if ($list = $zip->listContent()) |
---|
| 322 | { |
---|
| 323 | foreach ($list as $file) |
---|
| 324 | { |
---|
[9518] | 325 | // we search common.lang.php in archive |
---|
| 326 | if (basename($file['filename']) == 'common.lang.php' |
---|
[5371] | 327 | and (!isset($main_filepath) |
---|
| 328 | or strlen($file['filename']) < strlen($main_filepath))) |
---|
| 329 | { |
---|
| 330 | $main_filepath = $file['filename']; |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | if (isset($main_filepath)) |
---|
| 334 | { |
---|
[9518] | 335 | $root = basename(dirname($main_filepath)); // common.lang.php path in archive |
---|
[5371] | 336 | if (preg_match('/^[a-z]{2}_[A-Z]{2}$/', $root)) |
---|
| 337 | { |
---|
| 338 | if ($action == 'install') |
---|
| 339 | { |
---|
| 340 | $dest = $root; |
---|
| 341 | } |
---|
| 342 | $extract_path = PHPWG_ROOT_PATH.'language/'.$dest; |
---|
| 343 | if ( |
---|
| 344 | $result = $zip->extract( |
---|
| 345 | PCLZIP_OPT_PATH, $extract_path, |
---|
| 346 | PCLZIP_OPT_REMOVE_PATH, $root, |
---|
| 347 | PCLZIP_OPT_REPLACE_NEWER |
---|
| 348 | ) |
---|
| 349 | ) |
---|
| 350 | { |
---|
| 351 | foreach ($result as $file) |
---|
| 352 | { |
---|
| 353 | if ($file['stored_filename'] == $main_filepath) |
---|
| 354 | { |
---|
| 355 | $status = $file['status']; |
---|
| 356 | break; |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | if ($status == 'ok') |
---|
| 360 | { |
---|
[9520] | 361 | $this->get_fs_languages(); |
---|
| 362 | if ($action == 'install') |
---|
| 363 | { |
---|
| 364 | $this->perform_action('activate', $dest); |
---|
| 365 | } |
---|
[5371] | 366 | } |
---|
| 367 | if (file_exists($extract_path.'/obsolete.list') |
---|
| 368 | and $old_files = file($extract_path.'/obsolete.list', FILE_IGNORE_NEW_LINES) |
---|
| 369 | and !empty($old_files)) |
---|
| 370 | { |
---|
[25018] | 371 | $old_files[] = 'obsolete.list'; |
---|
[5371] | 372 | foreach($old_files as $old_file) |
---|
| 373 | { |
---|
| 374 | $path = $extract_path.'/'.$old_file; |
---|
| 375 | if (is_file($path)) |
---|
| 376 | { |
---|
| 377 | @unlink($path); |
---|
| 378 | } |
---|
| 379 | elseif (is_dir($path)) |
---|
| 380 | { |
---|
[23821] | 381 | deltree($path, PHPWG_ROOT_PATH.'language/trash'); |
---|
[5371] | 382 | } |
---|
| 383 | } |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | else $status = 'extract_error'; |
---|
| 387 | } |
---|
| 388 | else $status = 'archive_error'; |
---|
| 389 | } |
---|
| 390 | else $status = 'archive_error'; |
---|
| 391 | } |
---|
| 392 | else $status = 'archive_error'; |
---|
| 393 | } |
---|
| 394 | else $status = 'dl_archive_error'; |
---|
| 395 | } |
---|
| 396 | else $status = 'temp_path_error'; |
---|
| 397 | |
---|
| 398 | @unlink($archive); |
---|
| 399 | return $status; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | /** |
---|
[9520] | 403 | * Sort functions |
---|
| 404 | */ |
---|
| 405 | function language_version_compare($a, $b) |
---|
| 406 | { |
---|
| 407 | $pattern = array('/([a-z])/ei', '/\.+/', '/\.\Z|\A\./'); |
---|
| 408 | $replacement = array( "'.'.intval('\\1', 36).'.'", '.', ''); |
---|
| 409 | |
---|
| 410 | $array = preg_replace($pattern, $replacement, array($a, $b)); |
---|
| 411 | |
---|
| 412 | return version_compare($array[0], $array[1], '>='); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | function extension_name_compare($a, $b) |
---|
| 416 | { |
---|
| 417 | return strcmp(strtolower($a['extension_name']), strtolower($b['extension_name'])); |
---|
| 418 | } |
---|
[5357] | 419 | } |
---|
| 420 | ?> |
---|