[5153] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[5196] | 5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
[5153] | 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 themes |
---|
| 25 | { |
---|
| 26 | var $fs_themes = array(); |
---|
| 27 | var $db_themes_by_id = array(); |
---|
| 28 | var $server_themes = array(); |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Initialize $fs_themes and $db_themes_by_id |
---|
| 32 | */ |
---|
| 33 | function themes() |
---|
| 34 | { |
---|
| 35 | $this->get_fs_themes(); |
---|
| 36 | |
---|
| 37 | foreach ($this->get_db_themes() as $db_theme) |
---|
| 38 | { |
---|
| 39 | $this->db_themes_by_id[$db_theme['id']] = $db_theme; |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Set tabsheet for themes pages. |
---|
| 45 | * @param string selected page. |
---|
| 46 | */ |
---|
| 47 | function set_tabsheet($selected) |
---|
| 48 | { |
---|
| 49 | include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php'); |
---|
| 50 | |
---|
| 51 | $link = get_root_url().'admin.php?page='; |
---|
| 52 | |
---|
| 53 | $tabsheet = new tabsheet(); |
---|
| 54 | $tabsheet->add('themes_installed', l10n('Installed Themes'), $link.'themes_installed'); |
---|
| 55 | $tabsheet->add('themes_new', l10n('Add New Theme'), $link.'themes_new'); |
---|
| 56 | $tabsheet->select($selected); |
---|
| 57 | $tabsheet->assign(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Perform requested actions |
---|
| 62 | * @param string - action |
---|
| 63 | * @param string - theme id |
---|
| 64 | * @param array - errors |
---|
| 65 | */ |
---|
| 66 | function perform_action($action, $theme_id) |
---|
| 67 | { |
---|
| 68 | if (isset($this->db_themes_by_id[$theme_id])) |
---|
| 69 | { |
---|
| 70 | $crt_db_theme = $this->db_themes_by_id[$theme_id]; |
---|
| 71 | } |
---|
| 72 | |
---|
[5446] | 73 | $file_to_include = PHPWG_THEMES_PATH.'/'.$theme_id.'/admin/maintain.inc.php'; |
---|
| 74 | |
---|
[5153] | 75 | $errors = array(); |
---|
| 76 | |
---|
| 77 | switch ($action) |
---|
| 78 | { |
---|
| 79 | case 'activate': |
---|
| 80 | if (isset($crt_db_theme)) |
---|
| 81 | { |
---|
| 82 | // the theme is already active |
---|
| 83 | break; |
---|
| 84 | } |
---|
[5259] | 85 | |
---|
[5340] | 86 | if ('default' == $theme_id) |
---|
| 87 | { |
---|
| 88 | // you can't activate the "default" theme |
---|
| 89 | break; |
---|
| 90 | } |
---|
| 91 | |
---|
[5259] | 92 | $missing_parent = $this->missing_parent_theme($theme_id); |
---|
| 93 | if (isset($missing_parent)) |
---|
| 94 | { |
---|
| 95 | array_push( |
---|
| 96 | $errors, |
---|
| 97 | sprintf( |
---|
| 98 | l10n('Impossible to activate this theme, the parent theme is missing: %s'), |
---|
| 99 | $missing_parent |
---|
| 100 | ) |
---|
| 101 | ); |
---|
| 102 | |
---|
| 103 | break; |
---|
| 104 | } |
---|
[5446] | 105 | |
---|
| 106 | if (file_exists($file_to_include)) |
---|
| 107 | { |
---|
| 108 | include($file_to_include); |
---|
| 109 | if (function_exists('theme_activate')) |
---|
| 110 | { |
---|
| 111 | theme_activate($theme_id, $this->fs_themes[$theme_id]['version'], $errors); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | if (empty($errors)) |
---|
| 116 | { |
---|
[5452] | 117 | $query = ' |
---|
| 118 | INSERT INTO '.THEMES_TABLE.' |
---|
| 119 | (id, version, name) |
---|
| 120 | VALUES(\''.$theme_id.'\', |
---|
| 121 | \''.$this->fs_themes[$theme_id]['version'].'\', |
---|
| 122 | \''.$this->fs_themes[$theme_id]['name'].'\') |
---|
| 123 | ;'; |
---|
[5446] | 124 | pwg_query($query); |
---|
| 125 | } |
---|
[5153] | 126 | break; |
---|
| 127 | |
---|
| 128 | case 'deactivate': |
---|
| 129 | if (!isset($crt_db_theme)) |
---|
| 130 | { |
---|
| 131 | // the theme is already inactive |
---|
| 132 | break; |
---|
| 133 | } |
---|
| 134 | |
---|
[5382] | 135 | // you can't deactivate the last theme |
---|
| 136 | if (count($this->db_themes_by_id) <= 1) |
---|
| 137 | { |
---|
| 138 | array_push( |
---|
| 139 | $errors, |
---|
| 140 | l10n('Impossible to deactivate this theme, you need at least one theme.') |
---|
| 141 | ); |
---|
| 142 | break; |
---|
| 143 | } |
---|
| 144 | |
---|
[5153] | 145 | if ($theme_id == get_default_theme()) |
---|
| 146 | { |
---|
| 147 | // find a random theme to replace |
---|
| 148 | $new_theme = null; |
---|
| 149 | |
---|
| 150 | $query = ' |
---|
| 151 | SELECT |
---|
| 152 | id |
---|
| 153 | FROM '.THEMES_TABLE.' |
---|
[6542] | 154 | WHERE id != \''.$theme_id.'\' |
---|
[5153] | 155 | ;'; |
---|
| 156 | $result = pwg_query($query); |
---|
| 157 | if (pwg_db_num_rows($result) == 0) |
---|
| 158 | { |
---|
| 159 | $new_theme = 'default'; |
---|
| 160 | } |
---|
| 161 | else |
---|
| 162 | { |
---|
| 163 | list($new_theme) = pwg_db_fetch_row($result); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | $this->set_default_theme($new_theme); |
---|
| 167 | } |
---|
[5446] | 168 | |
---|
| 169 | if (file_exists($file_to_include)) |
---|
| 170 | { |
---|
| 171 | include($file_to_include); |
---|
| 172 | if (function_exists('theme_deactivate')) |
---|
| 173 | { |
---|
| 174 | theme_deactivate($theme_id); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
[6550] | 178 | $query = ' |
---|
[5153] | 179 | DELETE |
---|
[6550] | 180 | FROM '.THEMES_TABLE.' |
---|
| 181 | WHERE id= \''.$theme_id.'\' |
---|
| 182 | ;'; |
---|
[5153] | 183 | pwg_query($query); |
---|
| 184 | break; |
---|
| 185 | |
---|
| 186 | case 'delete': |
---|
| 187 | if (!empty($crt_db_theme)) |
---|
| 188 | { |
---|
| 189 | array_push($errors, 'CANNOT DELETE - THEME IS INSTALLED'); |
---|
| 190 | break; |
---|
| 191 | } |
---|
| 192 | if (!isset($this->fs_themes[$theme_id])) |
---|
| 193 | { |
---|
| 194 | // nothing to do here |
---|
| 195 | break; |
---|
| 196 | } |
---|
[5258] | 197 | |
---|
| 198 | $children = $this->get_children_themes($theme_id); |
---|
| 199 | if (count($children) > 0) |
---|
| 200 | { |
---|
| 201 | array_push( |
---|
| 202 | $errors, |
---|
| 203 | sprintf( |
---|
| 204 | l10n('Impossible to delete this theme. Other themes depends on it: %s'), |
---|
| 205 | implode(', ', $children) |
---|
| 206 | ) |
---|
| 207 | ); |
---|
| 208 | break; |
---|
| 209 | } |
---|
[5153] | 210 | |
---|
| 211 | if (!$this->deltree(PHPWG_THEMES_PATH.$theme_id)) |
---|
| 212 | { |
---|
| 213 | $this->send_to_trash(PHPWG_THEMES_PATH.$theme_id); |
---|
| 214 | } |
---|
| 215 | break; |
---|
| 216 | |
---|
| 217 | case 'set_default': |
---|
| 218 | // first we need to know which users are using the current default theme |
---|
| 219 | $this->set_default_theme($theme_id); |
---|
| 220 | break; |
---|
| 221 | } |
---|
| 222 | return $errors; |
---|
| 223 | } |
---|
| 224 | |
---|
[5259] | 225 | function missing_parent_theme($theme_id) |
---|
| 226 | { |
---|
| 227 | if (!isset($this->fs_themes[$theme_id]['parent'])) |
---|
| 228 | { |
---|
| 229 | return null; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | $parent = $this->fs_themes[$theme_id]['parent']; |
---|
| 233 | |
---|
| 234 | if ('default' == $parent) |
---|
| 235 | { |
---|
| 236 | return null; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | if (!isset($this->fs_themes[$parent])) |
---|
| 240 | { |
---|
| 241 | return $parent; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | return $this->missing_parent_theme($parent); |
---|
| 245 | } |
---|
| 246 | |
---|
[5258] | 247 | function get_children_themes($theme_id) |
---|
| 248 | { |
---|
| 249 | $children = array(); |
---|
| 250 | |
---|
| 251 | foreach ($this->fs_themes as $test_child) |
---|
| 252 | { |
---|
| 253 | if (isset($test_child['parent']) and $test_child['parent'] == $theme_id) |
---|
| 254 | { |
---|
| 255 | array_push($children, $test_child['name']); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | return $children; |
---|
| 260 | } |
---|
| 261 | |
---|
[5153] | 262 | function set_default_theme($theme_id) |
---|
| 263 | { |
---|
[5382] | 264 | global $conf; |
---|
| 265 | |
---|
[5153] | 266 | // first we need to know which users are using the current default theme |
---|
| 267 | $default_theme = get_default_theme(); |
---|
| 268 | |
---|
| 269 | $query = ' |
---|
| 270 | SELECT |
---|
| 271 | user_id |
---|
| 272 | FROM '.USER_INFOS_TABLE.' |
---|
[6542] | 273 | WHERE theme = \''.$default_theme.'\' |
---|
[5153] | 274 | ;'; |
---|
[5382] | 275 | $user_ids = array_unique( |
---|
| 276 | array_merge( |
---|
| 277 | array_from_query($query, 'user_id'), |
---|
| 278 | array($conf['guest_id'], $conf['default_user_id']) |
---|
| 279 | ) |
---|
| 280 | ); |
---|
[5153] | 281 | |
---|
| 282 | // $user_ids can't be empty, at least the default user has the default |
---|
| 283 | // theme |
---|
| 284 | |
---|
| 285 | $query = ' |
---|
| 286 | UPDATE '.USER_INFOS_TABLE.' |
---|
[6544] | 287 | SET theme = \''.$theme_id.'\' |
---|
[5153] | 288 | WHERE user_id IN ('.implode(',', $user_ids).') |
---|
| 289 | ;'; |
---|
| 290 | pwg_query($query); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | function get_db_themes($id='') |
---|
| 294 | { |
---|
| 295 | $query = ' |
---|
| 296 | SELECT |
---|
| 297 | * |
---|
| 298 | FROM '.THEMES_TABLE; |
---|
| 299 | |
---|
| 300 | $clauses = array(); |
---|
| 301 | if (!empty($id)) |
---|
| 302 | { |
---|
[6550] | 303 | $clauses[] = 'id = \''.$id.'\''; |
---|
[5153] | 304 | } |
---|
| 305 | if (count($clauses) > 0) |
---|
| 306 | { |
---|
| 307 | $query .= ' |
---|
| 308 | WHERE '. implode(' AND ', $clauses); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | $result = pwg_query($query); |
---|
| 312 | $themes = array(); |
---|
| 313 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 314 | { |
---|
| 315 | array_push($themes, $row); |
---|
| 316 | } |
---|
| 317 | return $themes; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | /** |
---|
| 322 | * Get themes defined in the theme directory |
---|
| 323 | */ |
---|
| 324 | function get_fs_themes() |
---|
| 325 | { |
---|
| 326 | $dir = opendir(PHPWG_THEMES_PATH); |
---|
| 327 | |
---|
| 328 | while ($file = readdir($dir)) |
---|
| 329 | { |
---|
| 330 | if ($file!='.' and $file!='..') |
---|
| 331 | { |
---|
| 332 | $path = PHPWG_THEMES_PATH.$file; |
---|
| 333 | if (is_dir($path) |
---|
| 334 | and preg_match('/^[a-zA-Z0-9-_]+$/', $file) |
---|
| 335 | and file_exists($path.'/themeconf.inc.php') |
---|
| 336 | ) |
---|
| 337 | { |
---|
| 338 | $theme = array( |
---|
| 339 | 'id' => $file, |
---|
| 340 | 'name' => $file, |
---|
| 341 | 'version' => '0', |
---|
| 342 | 'uri' => '', |
---|
| 343 | 'description' => '', |
---|
| 344 | 'author' => '', |
---|
| 345 | ); |
---|
| 346 | $theme_data = implode( '', file($path.'/themeconf.inc.php') ); |
---|
| 347 | |
---|
| 348 | if ( preg_match("|Theme Name: (.*)|", $theme_data, $val) ) |
---|
| 349 | { |
---|
| 350 | $theme['name'] = trim( $val[1] ); |
---|
| 351 | } |
---|
| 352 | if (preg_match("|Version: (.*)|", $theme_data, $val)) |
---|
| 353 | { |
---|
| 354 | $theme['version'] = trim($val[1]); |
---|
| 355 | } |
---|
| 356 | if ( preg_match("|Theme URI: (.*)|", $theme_data, $val) ) |
---|
| 357 | { |
---|
| 358 | $theme['uri'] = trim($val[1]); |
---|
| 359 | } |
---|
| 360 | if ($desc = load_language('description.txt', $path.'/', array('return' => true))) |
---|
| 361 | { |
---|
| 362 | $theme['description'] = trim($desc); |
---|
| 363 | } |
---|
| 364 | elseif ( preg_match("|Description: (.*)|", $theme_data, $val) ) |
---|
| 365 | { |
---|
| 366 | $theme['description'] = trim($val[1]); |
---|
| 367 | } |
---|
| 368 | if ( preg_match("|Author: (.*)|", $theme_data, $val) ) |
---|
| 369 | { |
---|
| 370 | $theme['author'] = trim($val[1]); |
---|
| 371 | } |
---|
| 372 | if ( preg_match("|Author URI: (.*)|", $theme_data, $val) ) |
---|
| 373 | { |
---|
| 374 | $theme['author uri'] = trim($val[1]); |
---|
| 375 | } |
---|
| 376 | if (!empty($theme['uri']) and strpos($theme['uri'] , 'extension_view.php?eid=')) |
---|
| 377 | { |
---|
| 378 | list( , $extension) = explode('extension_view.php?eid=', $theme['uri']); |
---|
| 379 | if (is_numeric($extension)) $theme['extension'] = $extension; |
---|
| 380 | } |
---|
[5258] | 381 | if (preg_match('/["\']parent["\'][^"\']+["\']([^"\']+)["\']/', $theme_data, $val)) |
---|
| 382 | { |
---|
| 383 | $theme['parent'] = $val[1]; |
---|
| 384 | } |
---|
[5874] | 385 | if (preg_match('/["\']activable["\'].*?(true|false)/', $theme_data, $val)) |
---|
| 386 | { |
---|
| 387 | $theme['activable'] = get_boolean($val[1]); |
---|
| 388 | } |
---|
[5153] | 389 | |
---|
| 390 | // screenshot |
---|
| 391 | $screenshot_path = $path.'/screenshot.png'; |
---|
| 392 | if (file_exists($screenshot_path)) |
---|
| 393 | { |
---|
| 394 | $theme['screenshot'] = $screenshot_path; |
---|
| 395 | } |
---|
| 396 | else |
---|
| 397 | { |
---|
| 398 | global $conf; |
---|
| 399 | $theme['screenshot'] = |
---|
| 400 | PHPWG_ROOT_PATH.'admin/themes/' |
---|
| 401 | .$conf['admin_theme'] |
---|
| 402 | .'/images/missing_screenshot.png' |
---|
| 403 | ; |
---|
| 404 | } |
---|
[5446] | 405 | |
---|
| 406 | $admin_file = $path.'/admin/admin.inc.php'; |
---|
| 407 | if (file_exists($admin_file)) |
---|
| 408 | { |
---|
| 409 | $theme['admin_uri'] = get_root_url().'admin.php?page=theme&theme='.$file; |
---|
| 410 | } |
---|
| 411 | |
---|
[5153] | 412 | // IMPORTANT SECURITY ! |
---|
| 413 | $theme = array_map('htmlspecialchars', $theme); |
---|
| 414 | $this->fs_themes[$file] = $theme; |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | closedir($dir); |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | /** |
---|
| 422 | * Sort fs_themes |
---|
| 423 | */ |
---|
| 424 | function sort_fs_themes($order='name') |
---|
| 425 | { |
---|
| 426 | switch ($order) |
---|
| 427 | { |
---|
| 428 | case 'name': |
---|
| 429 | uasort($this->fs_themes, 'name_compare'); |
---|
| 430 | break; |
---|
| 431 | case 'status': |
---|
| 432 | $this->sort_themes_by_state(); |
---|
| 433 | break; |
---|
| 434 | case 'author': |
---|
| 435 | uasort($this->fs_themes, array($this, 'theme_author_compare')); |
---|
| 436 | break; |
---|
| 437 | case 'id': |
---|
| 438 | uksort($this->fs_themes, 'strcasecmp'); |
---|
| 439 | break; |
---|
| 440 | } |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | /** |
---|
| 444 | * Retrieve PEM server datas to $server_themes |
---|
| 445 | */ |
---|
| 446 | function get_server_themes($new=false) |
---|
| 447 | { |
---|
| 448 | global $user; |
---|
| 449 | |
---|
| 450 | $pem_category_id = 10; |
---|
| 451 | |
---|
| 452 | // Retrieve PEM versions |
---|
| 453 | $version = PHPWG_VERSION; |
---|
| 454 | $versions_to_check = array(); |
---|
| 455 | $url = PEM_URL . '/api/get_version_list.php?category_id='.$pem_category_id.'&format=php'; |
---|
| 456 | if (fetchRemote($url, $result) and $pem_versions = @unserialize($result)) |
---|
| 457 | { |
---|
| 458 | if (!preg_match('/^\d+\.\d+\.\d+/', $version)) |
---|
| 459 | { |
---|
| 460 | $version = $pem_versions[0]['name']; |
---|
| 461 | } |
---|
| 462 | $branch = substr($version, 0, strrpos($version, '.')); |
---|
| 463 | foreach ($pem_versions as $pem_version) |
---|
| 464 | { |
---|
| 465 | if (strpos($pem_version['name'], $branch) === 0) |
---|
| 466 | { |
---|
| 467 | $versions_to_check[] = $pem_version['id']; |
---|
| 468 | } |
---|
| 469 | } |
---|
| 470 | } |
---|
| 471 | if (empty($versions_to_check)) |
---|
| 472 | { |
---|
| 473 | return false; |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | // Themes to check |
---|
| 477 | $themes_to_check = array(); |
---|
| 478 | foreach($this->fs_themes as $fs_theme) |
---|
| 479 | { |
---|
| 480 | if (isset($fs_theme['extension'])) |
---|
| 481 | { |
---|
| 482 | $themes_to_check[] = $fs_theme['extension']; |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | // Retrieve PEM themes infos |
---|
| 487 | $url = PEM_URL . '/api/get_revision_list.php?category_id='.$pem_category_id.'&format=php&last_revision_only=true'; |
---|
| 488 | $url .= '&version=' . implode(',', $versions_to_check); |
---|
| 489 | $url .= '&lang=' . substr($user['language'], 0, 2); |
---|
| 490 | $url .= '&get_nb_downloads=true'; |
---|
| 491 | |
---|
| 492 | if (!empty($themes_to_check)) |
---|
| 493 | { |
---|
| 494 | $url .= $new ? '&extension_exclude=' : '&extension_include='; |
---|
| 495 | $url .= implode(',', $themes_to_check); |
---|
| 496 | } |
---|
| 497 | if (fetchRemote($url, $result)) |
---|
| 498 | { |
---|
| 499 | $pem_themes = @unserialize($result); |
---|
| 500 | if (!is_array($pem_themes)) |
---|
| 501 | { |
---|
| 502 | return false; |
---|
| 503 | } |
---|
| 504 | foreach ($pem_themes as $theme) |
---|
| 505 | { |
---|
| 506 | $this->server_themes[$theme['extension_id']] = $theme; |
---|
| 507 | } |
---|
| 508 | return true; |
---|
| 509 | } |
---|
| 510 | return false; |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | /** |
---|
| 514 | * Sort $server_themes |
---|
| 515 | */ |
---|
| 516 | function sort_server_themes($order='date') |
---|
| 517 | { |
---|
| 518 | switch ($order) |
---|
| 519 | { |
---|
| 520 | case 'date': |
---|
| 521 | krsort($this->server_themes); |
---|
| 522 | break; |
---|
| 523 | case 'revision': |
---|
| 524 | usort($this->server_themes, array($this, 'extension_revision_compare')); |
---|
| 525 | break; |
---|
| 526 | case 'name': |
---|
| 527 | uasort($this->server_themes, array($this, 'extension_name_compare')); |
---|
| 528 | break; |
---|
| 529 | case 'author': |
---|
| 530 | uasort($this->server_themes, array($this, 'extension_author_compare')); |
---|
| 531 | break; |
---|
| 532 | case 'downloads': |
---|
| 533 | usort($this->server_themes, array($this, 'extension_downloads_compare')); |
---|
| 534 | break; |
---|
| 535 | } |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | /** |
---|
| 539 | * Extract theme files from archive |
---|
| 540 | * |
---|
| 541 | * @param string - install or upgrade |
---|
| 542 | * @param string - remote revision identifier (numeric) |
---|
| 543 | * @param string - theme id or extension id |
---|
| 544 | */ |
---|
| 545 | function extract_theme_files($action, $revision, $dest) |
---|
| 546 | { |
---|
| 547 | if ($archive = tempnam( PHPWG_THEMES_PATH, 'zip')) |
---|
| 548 | { |
---|
| 549 | $url = PEM_URL . '/download.php?rid=' . $revision; |
---|
| 550 | $url .= '&origin=piwigo_' . $action; |
---|
| 551 | |
---|
| 552 | if ($handle = @fopen($archive, 'wb') and fetchRemote($url, $handle)) |
---|
| 553 | { |
---|
| 554 | fclose($handle); |
---|
| 555 | include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); |
---|
| 556 | $zip = new PclZip($archive); |
---|
| 557 | if ($list = $zip->listContent()) |
---|
| 558 | { |
---|
| 559 | foreach ($list as $file) |
---|
| 560 | { |
---|
| 561 | // we search main.inc.php in archive |
---|
| 562 | if (basename($file['filename']) == 'themeconf.inc.php' |
---|
| 563 | and (!isset($main_filepath) |
---|
| 564 | or strlen($file['filename']) < strlen($main_filepath))) |
---|
| 565 | { |
---|
| 566 | $main_filepath = $file['filename']; |
---|
| 567 | } |
---|
| 568 | } |
---|
| 569 | if (isset($main_filepath)) |
---|
| 570 | { |
---|
| 571 | $root = dirname($main_filepath); // main.inc.php path in archive |
---|
| 572 | if ($action == 'upgrade') |
---|
| 573 | { |
---|
| 574 | $extract_path = PHPWG_THEMES_PATH . $dest; |
---|
| 575 | } |
---|
| 576 | else |
---|
| 577 | { |
---|
| 578 | $extract_path = PHPWG_THEMES_PATH . ($root == '.' ? 'extension_' . $dest : basename($root)); |
---|
| 579 | } |
---|
| 580 | if ( |
---|
| 581 | $result = $zip->extract( |
---|
| 582 | PCLZIP_OPT_PATH, $extract_path, |
---|
| 583 | PCLZIP_OPT_REMOVE_PATH, $root, |
---|
| 584 | PCLZIP_OPT_REPLACE_NEWER |
---|
| 585 | ) |
---|
| 586 | ) |
---|
| 587 | { |
---|
| 588 | foreach ($result as $file) |
---|
| 589 | { |
---|
| 590 | if ($file['stored_filename'] == $main_filepath) |
---|
| 591 | { |
---|
| 592 | $status = $file['status']; |
---|
| 593 | break; |
---|
| 594 | } |
---|
| 595 | } |
---|
| 596 | if (file_exists($extract_path.'/obsolete.list') |
---|
| 597 | and $old_files = file($extract_path.'/obsolete.list', FILE_IGNORE_NEW_LINES) |
---|
| 598 | and !empty($old_files)) |
---|
| 599 | { |
---|
| 600 | array_push($old_files, 'obsolete.list'); |
---|
| 601 | foreach($old_files as $old_file) |
---|
| 602 | { |
---|
| 603 | $path = $extract_path.'/'.$old_file; |
---|
| 604 | if (is_file($path)) |
---|
| 605 | { |
---|
| 606 | @unlink($path); |
---|
| 607 | } |
---|
| 608 | elseif (is_dir($path)) |
---|
| 609 | { |
---|
| 610 | if (!$this->deltree($path)) |
---|
| 611 | { |
---|
| 612 | $this->send_to_trash($path); |
---|
| 613 | } |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | } |
---|
| 617 | } |
---|
| 618 | else $status = 'extract_error'; |
---|
| 619 | } |
---|
| 620 | else $status = 'archive_error'; |
---|
| 621 | } |
---|
| 622 | else $status = 'archive_error'; |
---|
| 623 | } |
---|
| 624 | else $status = 'dl_archive_error'; |
---|
| 625 | } |
---|
| 626 | else $status = 'temp_path_error'; |
---|
| 627 | |
---|
| 628 | @unlink($archive); |
---|
| 629 | return $status; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | /** |
---|
| 633 | * delete $path directory |
---|
| 634 | * @param string - path |
---|
| 635 | */ |
---|
| 636 | function deltree($path) |
---|
| 637 | { |
---|
| 638 | if (is_dir($path)) |
---|
| 639 | { |
---|
| 640 | $fh = opendir($path); |
---|
| 641 | while ($file = readdir($fh)) |
---|
| 642 | { |
---|
| 643 | if ($file != '.' and $file != '..') |
---|
| 644 | { |
---|
| 645 | $pathfile = $path . '/' . $file; |
---|
| 646 | if (is_dir($pathfile)) |
---|
| 647 | { |
---|
| 648 | $this->deltree($pathfile); |
---|
| 649 | } |
---|
| 650 | else |
---|
| 651 | { |
---|
| 652 | @unlink($pathfile); |
---|
| 653 | } |
---|
| 654 | } |
---|
| 655 | } |
---|
| 656 | closedir($fh); |
---|
| 657 | return @rmdir($path); |
---|
| 658 | } |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | /** |
---|
| 662 | * send $path to trash directory |
---|
| 663 | * @param string - path |
---|
| 664 | */ |
---|
| 665 | function send_to_trash($path) |
---|
| 666 | { |
---|
| 667 | $trash_path = PHPWG_THEMES_PATH . 'trash'; |
---|
| 668 | if (!is_dir($trash_path)) |
---|
| 669 | { |
---|
| 670 | @mkdir($trash_path); |
---|
| 671 | $file = @fopen($trash_path . '/.htaccess', 'w'); |
---|
| 672 | @fwrite($file, 'deny from all'); |
---|
| 673 | @fclose($file); |
---|
| 674 | } |
---|
| 675 | while ($r = $trash_path . '/' . md5(uniqid(rand(), true))) |
---|
| 676 | { |
---|
| 677 | if (!is_dir($r)) |
---|
| 678 | { |
---|
| 679 | @rename($path, $r); |
---|
| 680 | break; |
---|
| 681 | } |
---|
| 682 | } |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | /** |
---|
| 686 | * Sort functions |
---|
| 687 | */ |
---|
| 688 | function theme_version_compare($a, $b) |
---|
| 689 | { |
---|
| 690 | $pattern = array('/([a-z])/ei', '/\.+/', '/\.\Z|\A\./'); |
---|
| 691 | $replacement = array( "'.'.intval('\\1', 36).'.'", '.', ''); |
---|
| 692 | |
---|
| 693 | $array = preg_replace($pattern, $replacement, array($a, $b)); |
---|
| 694 | |
---|
| 695 | return version_compare($array[0], $array[1], '>='); |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | function extension_revision_compare($a, $b) |
---|
| 699 | { |
---|
| 700 | if ($a['revision_date'] < $b['revision_date']) return 1; |
---|
| 701 | else return -1; |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | function extension_name_compare($a, $b) |
---|
| 705 | { |
---|
| 706 | return strcmp(strtolower($a['extension_name']), strtolower($b['extension_name'])); |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | function extension_author_compare($a, $b) |
---|
| 710 | { |
---|
| 711 | $r = strcasecmp($a['author_name'], $b['author_name']); |
---|
| 712 | if ($r == 0) return $this->extension_name_compare($a, $b); |
---|
| 713 | else return $r; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | function theme_author_compare($a, $b) |
---|
| 717 | { |
---|
| 718 | $r = strcasecmp($a['author'], $b['author']); |
---|
| 719 | if ($r == 0) return name_compare($a, $b); |
---|
| 720 | else return $r; |
---|
| 721 | } |
---|
| 722 | |
---|
| 723 | function extension_downloads_compare($a, $b) |
---|
| 724 | { |
---|
| 725 | if ($a['extension_nb_downloads'] < $b['extension_nb_downloads']) return 1; |
---|
| 726 | else return -1; |
---|
| 727 | } |
---|
| 728 | |
---|
| 729 | function sort_themes_by_state() |
---|
| 730 | { |
---|
| 731 | uasort($this->fs_themes, 'name_compare'); |
---|
| 732 | |
---|
| 733 | $active_themes = array(); |
---|
| 734 | $inactive_themes = array(); |
---|
| 735 | $not_installed = array(); |
---|
| 736 | |
---|
| 737 | foreach($this->fs_themes as $theme_id => $theme) |
---|
| 738 | { |
---|
| 739 | if (isset($this->db_themes_by_id[$theme_id])) |
---|
| 740 | { |
---|
| 741 | $this->db_themes_by_id[$theme_id]['state'] == 'active' ? |
---|
| 742 | $active_themes[$theme_id] = $theme : $inactive_themes[$theme_id] = $theme; |
---|
| 743 | } |
---|
| 744 | else |
---|
| 745 | { |
---|
| 746 | $not_installed[$theme_id] = $theme; |
---|
| 747 | } |
---|
| 748 | } |
---|
| 749 | $this->fs_themes = $active_themes + $inactive_themes + $not_installed; |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | // themes specific methods |
---|
| 753 | function get_fs_themes_with_ini() |
---|
| 754 | { |
---|
| 755 | $themes_dir = PHPWG_ROOT_PATH.'themes'; |
---|
| 756 | |
---|
| 757 | $fs_themes = array(); |
---|
| 758 | |
---|
| 759 | foreach (get_dirs($themes_dir) as $theme) |
---|
| 760 | { |
---|
| 761 | $conf_file = $themes_dir.'/'.$theme.'/themeconf.inc.php'; |
---|
| 762 | if (file_exists($conf_file)) |
---|
| 763 | { |
---|
| 764 | $theme_data = array( |
---|
| 765 | 'name' => $theme, |
---|
| 766 | ); |
---|
| 767 | |
---|
| 768 | $ini_file = $themes_dir.'/'.$theme.'/theme.ini'; |
---|
| 769 | if (file_exists($ini_file)) |
---|
| 770 | { |
---|
| 771 | $theme_ini = parse_ini_file($ini_file); |
---|
| 772 | if (isset($theme_ini['extension_id'])) |
---|
| 773 | { |
---|
| 774 | $theme_data['extension_id'] = $theme_ini['extension_id']; |
---|
| 775 | } |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | array_push($fs_themes, $theme_data); |
---|
| 779 | } |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | echo '<pre>'; print_r($fs_themes); echo '</pre>'; |
---|
| 783 | } |
---|
| 784 | |
---|
| 785 | |
---|
| 786 | } |
---|
| 787 | ?> |
---|