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