1 | <?php |
---|
2 | |
---|
3 | class adminModulesList |
---|
4 | { |
---|
5 | public $core; |
---|
6 | public $modules; |
---|
7 | |
---|
8 | public static $allow_multi_install; |
---|
9 | public static $distributed_modules = array(); |
---|
10 | |
---|
11 | protected $list_id = 'unknow'; |
---|
12 | |
---|
13 | protected $path = false; |
---|
14 | protected $path_writable = false; |
---|
15 | protected $path_pattern = false; |
---|
16 | |
---|
17 | protected $page_url = 'plugins.php'; |
---|
18 | protected $page_qs = '?'; |
---|
19 | protected $page_tab = ''; |
---|
20 | |
---|
21 | public static $nav_indexes = 'abcdefghijklmnopqrstuvwxyz0123456789'; |
---|
22 | protected $nav_list = array(); |
---|
23 | protected $nav_special = 'other'; |
---|
24 | |
---|
25 | protected $sort_field = 'sname'; |
---|
26 | protected $sort_asc = true; |
---|
27 | |
---|
28 | public function __construct($core, $root, $allow_multi_install=false) |
---|
29 | { |
---|
30 | $this->core = $core; |
---|
31 | self::$allow_multi_install = (boolean) $allow_multi_install; |
---|
32 | $this->setPathInfo($root); |
---|
33 | $this->setNavSpecial(__('other')); |
---|
34 | |
---|
35 | $this->init(); |
---|
36 | } |
---|
37 | |
---|
38 | protected function init() |
---|
39 | { |
---|
40 | return null; |
---|
41 | } |
---|
42 | |
---|
43 | public function newList($list_id) |
---|
44 | { |
---|
45 | $this->modules = array(); |
---|
46 | $this->page_tab = ''; |
---|
47 | $this->list_id = $list_id; |
---|
48 | |
---|
49 | return $this; |
---|
50 | } |
---|
51 | |
---|
52 | protected function setPathInfo($root) |
---|
53 | { |
---|
54 | $paths = explode(PATH_SEPARATOR, $root); |
---|
55 | $path = array_pop($paths); |
---|
56 | unset($paths); |
---|
57 | |
---|
58 | $this->path = $path; |
---|
59 | if (is_dir($path) && is_writeable($path)) { |
---|
60 | $this->path_writable = true; |
---|
61 | $this->path_pattern = preg_quote($path,'!'); |
---|
62 | } |
---|
63 | |
---|
64 | return $this; |
---|
65 | } |
---|
66 | |
---|
67 | public function getPath() |
---|
68 | { |
---|
69 | return $this->path; |
---|
70 | } |
---|
71 | |
---|
72 | public function isPathWritable() |
---|
73 | { |
---|
74 | return $this->path_writable; |
---|
75 | } |
---|
76 | |
---|
77 | public function isPathDeletable($root) |
---|
78 | { |
---|
79 | return $this->path_writable |
---|
80 | && preg_match('!^'.$this->path_pattern.'!', $root) |
---|
81 | && $this->core->auth->isSuperAdmin(); |
---|
82 | } |
---|
83 | |
---|
84 | public function setPageURL($url) |
---|
85 | { |
---|
86 | $this->page_qs = strpos('?', $url) ? '&' : '?'; |
---|
87 | $this->page_url = $url; |
---|
88 | |
---|
89 | return $this; |
---|
90 | } |
---|
91 | |
---|
92 | public function getPageURL($queries='', $with_tab=true) |
---|
93 | { |
---|
94 | return $this->page_url. |
---|
95 | (!empty($queries) ? $this->page_qs : ''). |
---|
96 | (is_array($queries) ? http_build_query($queries) : $queries). |
---|
97 | ($with_tab && !empty($this->page_tab) ? '#'.$this->page_tab : ''); |
---|
98 | } |
---|
99 | |
---|
100 | public function setPageTab($tab) |
---|
101 | { |
---|
102 | $this->page_tab = $tab; |
---|
103 | |
---|
104 | return $this; |
---|
105 | } |
---|
106 | |
---|
107 | public function getPageTab() |
---|
108 | { |
---|
109 | return $this->page_tab; |
---|
110 | } |
---|
111 | |
---|
112 | public function getSearchQuery() |
---|
113 | { |
---|
114 | $query = !empty($_REQUEST['m_search']) ? trim($_REQUEST['m_search']) : null; |
---|
115 | return strlen($query) > 1 ? $query : null; |
---|
116 | } |
---|
117 | |
---|
118 | public function displaySearchForm() |
---|
119 | { |
---|
120 | $query = $this->getSearchQuery(); |
---|
121 | |
---|
122 | if (empty($this->modules) && $query === null) { |
---|
123 | return $this; |
---|
124 | } |
---|
125 | |
---|
126 | echo |
---|
127 | '<form action="'.$this->getPageURL().'" method="get" class="fieldset">'. |
---|
128 | '<p><label for="m_search" class="classic">'.__('Search in repository:').' </label><br />'. |
---|
129 | form::field(array('m_search','m_search'), 30, 255, html::escapeHTML($query)). |
---|
130 | '<input type="submit" value="'.__('Search').'" /> '; |
---|
131 | if ($query) { echo ' <a href="'.$this->getPageURL().'" class="button">'.__('Reset search').'</a>'; } |
---|
132 | echo '</p>'. |
---|
133 | '</form>'; |
---|
134 | |
---|
135 | if ($query) { |
---|
136 | echo |
---|
137 | '<p class="message">'.sprintf( |
---|
138 | __('Found %d result for search "%s":', 'Found %d results for search "%s":', count($this->modules)), |
---|
139 | count($this->modules), html::escapeHTML($query) |
---|
140 | ). |
---|
141 | '</p>'; |
---|
142 | } |
---|
143 | return $this; |
---|
144 | } |
---|
145 | |
---|
146 | public function setNavSpecial($str) |
---|
147 | { |
---|
148 | $this->nav_special = (string) $str; |
---|
149 | $this->nav_list = array_merge(str_split(self::$nav_indexes), array($this->nav_special)); |
---|
150 | |
---|
151 | return $this; |
---|
152 | } |
---|
153 | |
---|
154 | public function getNavQuery() |
---|
155 | { |
---|
156 | return isset($_REQUEST['m_nav']) && in_array($_REQUEST['m_nav'], $this->nav_list) ? $_REQUEST['m_nav'] : $this->nav_list[0]; |
---|
157 | } |
---|
158 | |
---|
159 | public function displayNavMenu() |
---|
160 | { |
---|
161 | if (empty($this->modules) || $this->getSearchQuery() !== null) { |
---|
162 | return $this; |
---|
163 | } |
---|
164 | |
---|
165 | # Fetch modules required field |
---|
166 | $indexes = array(); |
---|
167 | foreach ($this->modules as $id => $module) { |
---|
168 | if (!isset($module[$this->sort_field])) { |
---|
169 | continue; |
---|
170 | } |
---|
171 | $char = substr($module[$this->sort_field], 0, 1); |
---|
172 | if (!in_array($char, $this->nav_list)) { |
---|
173 | $char = $this->nav_special; |
---|
174 | } |
---|
175 | if (!isset($indexes[$char])) { |
---|
176 | $indexes[$char] = 0; |
---|
177 | } |
---|
178 | $indexes[$char]++; |
---|
179 | } |
---|
180 | |
---|
181 | $buttons = array(); |
---|
182 | foreach($this->nav_list as $char) { |
---|
183 | # Selected letter |
---|
184 | if ($this->getNavQuery() == $char) { |
---|
185 | $buttons[] = '<li class="active" title="'.__('current selection').'"><strong> '.$char.' </strong></li>'; |
---|
186 | } |
---|
187 | # Letter having modules |
---|
188 | elseif (!empty($indexes[$char])) { |
---|
189 | $title = sprintf(__('%d module', '%d modules', $indexes[$char]), $indexes[$char]); |
---|
190 | $buttons[] = '<li class="btn" title="'.$title.'"><a href="'.$this->getPageURL('m_nav='.$char).'" title="'.$title.'"> '.$char.' </a></li>'; |
---|
191 | } |
---|
192 | # Letter without modules |
---|
193 | else { |
---|
194 | $buttons[] = '<li class="btn no-link" title="'.__('no module').'"> '.$char.' </li>'; |
---|
195 | } |
---|
196 | } |
---|
197 | # Parse navigation menu |
---|
198 | echo '<div class="pager">'.__('Browse index:').' <ul>'.implode('',$buttons).'</ul></div>'; |
---|
199 | |
---|
200 | return $this; |
---|
201 | } |
---|
202 | |
---|
203 | public function setSortField($field, $asc=true) |
---|
204 | { |
---|
205 | $this->sort_field = $field; |
---|
206 | $this->sort_asc = (boolean) $asc; |
---|
207 | |
---|
208 | return $this; |
---|
209 | } |
---|
210 | |
---|
211 | public function getSortQuery() |
---|
212 | { |
---|
213 | return !empty($_REQUEST['m_sort']) ? $_REQUEST['m_sort'] : $this->sort_field; |
---|
214 | } |
---|
215 | |
---|
216 | public function displaySortForm() |
---|
217 | { |
---|
218 | //not yet implemented |
---|
219 | } |
---|
220 | |
---|
221 | public function displayMessage($action) |
---|
222 | { |
---|
223 | switch($action) { |
---|
224 | case 'activate': |
---|
225 | $str = __('Module successfully activated.'); break; |
---|
226 | case 'deactivate': |
---|
227 | $str = __('Module successfully deactivated.'); break; |
---|
228 | case 'delete': |
---|
229 | $str = __('Module successfully deleted.'); break; |
---|
230 | case 'install': |
---|
231 | $str = __('Module successfully installed.'); break; |
---|
232 | case 'update': |
---|
233 | $str = __('Module successfully updated.'); break; |
---|
234 | default: |
---|
235 | $str = ''; break; |
---|
236 | } |
---|
237 | if (!empty($str)) { |
---|
238 | dcPage::success($str); |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | public function setModules($modules) |
---|
243 | { |
---|
244 | $this->modules = array(); |
---|
245 | if (!empty($modules) && is_array($modules)) { |
---|
246 | foreach($modules as $id => $module) { |
---|
247 | $this->modules[$id] = self::parseModuleInfo($id, $module); |
---|
248 | } |
---|
249 | } |
---|
250 | return $this; |
---|
251 | } |
---|
252 | |
---|
253 | public function getModules() |
---|
254 | { |
---|
255 | return $this->modules; |
---|
256 | } |
---|
257 | |
---|
258 | public static function parseModuleInfo($id, $module) |
---|
259 | { |
---|
260 | $label = empty($module['label']) ? $id : $module['label']; |
---|
261 | $name = __(empty($module['name']) ? $label : $module['name']); |
---|
262 | |
---|
263 | return array_merge( |
---|
264 | # Default values |
---|
265 | array( |
---|
266 | 'desc' => '', |
---|
267 | 'author' => '', |
---|
268 | 'version' => 0, |
---|
269 | 'current_version' => 0, |
---|
270 | 'root' => '', |
---|
271 | 'root_writable' => false, |
---|
272 | 'permissions' => null, |
---|
273 | 'parent' => null, |
---|
274 | 'priority' => 1000, |
---|
275 | 'standalone_config' => false, |
---|
276 | 'support' => '', |
---|
277 | 'section' => '', |
---|
278 | 'tags' => '', |
---|
279 | 'details' => '', |
---|
280 | 'sshot' => '' |
---|
281 | ), |
---|
282 | # Module's values |
---|
283 | $module, |
---|
284 | # Clean up values |
---|
285 | array( |
---|
286 | 'id' => $id, |
---|
287 | 'sid' => self::sanitizeString($id), |
---|
288 | 'label' => $label, |
---|
289 | 'name' => $name, |
---|
290 | 'sname' => self::sanitizeString($name) |
---|
291 | ) |
---|
292 | ); |
---|
293 | } |
---|
294 | |
---|
295 | public static function setDistributedModules($modules) |
---|
296 | { |
---|
297 | self::$distributed_modules = $modules; |
---|
298 | } |
---|
299 | |
---|
300 | public static function isDistributedModule($module) |
---|
301 | { |
---|
302 | $distributed_modules = self::$distributed_modules; |
---|
303 | |
---|
304 | return is_array($distributed_modules) && in_array($module, $distributed_modules); |
---|
305 | } |
---|
306 | |
---|
307 | public static function sortModules($modules, $field, $asc=true) |
---|
308 | { |
---|
309 | $sorter = array(); |
---|
310 | foreach($modules as $id => $module) { |
---|
311 | $sorter[$id] = isset($module[$field]) ? $module[$field] : $field; |
---|
312 | } |
---|
313 | array_multisort($sorter, $asc ? SORT_ASC : SORT_DESC, $modules); |
---|
314 | |
---|
315 | return $modules; |
---|
316 | } |
---|
317 | |
---|
318 | public function displayModulesList($cols=array('name', 'config', 'version', 'desc'), $actions=array(), $nav_limit=false) |
---|
319 | { |
---|
320 | echo |
---|
321 | '<div class="table-outer">'. |
---|
322 | '<table id="'.html::escapeHTML($this->list_id).'" class="modules'.(in_array('expander', $cols) ? ' expandable' : '').'">'. |
---|
323 | '<caption class="hidden">'.html::escapeHTML(__('Modules list')).'</caption><tr>'; |
---|
324 | |
---|
325 | if (in_array('name', $cols)) { |
---|
326 | echo |
---|
327 | '<th class="first nowrap"'.(in_array('icon', $cols) ? ' colspan="2"' : '').'>'.__('Name').'</th>'; |
---|
328 | } |
---|
329 | |
---|
330 | if (in_array('version', $cols)) { |
---|
331 | echo |
---|
332 | '<th class="nowrap count" scope="col">'.__('Version').'</th>'; |
---|
333 | } |
---|
334 | |
---|
335 | if (in_array('current_version', $cols)) { |
---|
336 | echo |
---|
337 | '<th class="nowrap count" scope="col">'.__('Current version').'</th>'; |
---|
338 | } |
---|
339 | |
---|
340 | if (in_array('desc', $cols)) { |
---|
341 | echo |
---|
342 | '<th class="nowrap" scope="col">'.__('Details').'</th>'; |
---|
343 | } |
---|
344 | |
---|
345 | if (in_array('distrib', $cols)) { |
---|
346 | echo '<th'.(in_array('desc', $cols) ? '' : ' class="maximal"').'></th>'; |
---|
347 | } |
---|
348 | |
---|
349 | if (!empty($actions) && $this->core->auth->isSuperAdmin()) { |
---|
350 | echo |
---|
351 | '<th class="minimal nowrap">'.__('Action').'</th>'; |
---|
352 | } |
---|
353 | |
---|
354 | echo |
---|
355 | '</tr>'; |
---|
356 | |
---|
357 | $sort_field = $this->getSortQuery(); |
---|
358 | |
---|
359 | # Sort modules by id |
---|
360 | $modules = $this->getSearchQuery() === null ? |
---|
361 | self::sortModules($this->modules, $sort_field, $this->sort_asc) : |
---|
362 | $this->modules; |
---|
363 | |
---|
364 | $count = 0; |
---|
365 | foreach ($modules as $id => $module) |
---|
366 | { |
---|
367 | # Show only requested modules |
---|
368 | if ($nav_limit && $this->getSearchQuery() === null) { |
---|
369 | $char = substr($module[$sort_field], 0, 1); |
---|
370 | if (!in_array($char, $this->nav_list)) { |
---|
371 | $char = $this->nav_special; |
---|
372 | } |
---|
373 | if ($this->getNavQuery() != $char) { |
---|
374 | continue; |
---|
375 | } |
---|
376 | } |
---|
377 | |
---|
378 | echo |
---|
379 | '<tr class="line" id="'.html::escapeHTML($this->list_id).'_m_'.html::escapeHTML($id).'">'; |
---|
380 | |
---|
381 | if (in_array('icon', $cols)) { |
---|
382 | echo |
---|
383 | '<td class="module-icon nowrap">'.sprintf( |
---|
384 | '<img alt="%1$s" title="%1$s" src="%2$s" />', |
---|
385 | html::escapeHTML($id), file_exists($module['root'].'/icon.png') ? 'index.php?pf='.$id.'/icon.png' : 'images/module.png' |
---|
386 | ).'</td>'; |
---|
387 | } |
---|
388 | |
---|
389 | # Link to config file |
---|
390 | $config = in_array('config', $cols) && !empty($module['root']) && file_exists(path::real($module['root'].'/_config.php')); |
---|
391 | |
---|
392 | echo |
---|
393 | '<td class="module-name nowrap" scope="row">'.($config ? |
---|
394 | '<a href="'.$this->getPageURL('module='.$id.'&conf=1').'" title"'.sprintf(__('Configure module "%s"'), html::escapeHTML($module['name'])).'">'.html::escapeHTML($module['name']).'</a>' : |
---|
395 | html::escapeHTML($module['name']) |
---|
396 | ).'</td>'; |
---|
397 | |
---|
398 | if (in_array('version', $cols)) { |
---|
399 | echo |
---|
400 | '<td class="module-version nowrap count">'.html::escapeHTML($module['version']).'</td>'; |
---|
401 | } |
---|
402 | |
---|
403 | if (in_array('current_version', $cols)) { |
---|
404 | echo |
---|
405 | '<td class="module-current-version nowrap count">'.html::escapeHTML($module['current_version']).'</td>'; |
---|
406 | } |
---|
407 | |
---|
408 | if (in_array('desc', $cols)) { |
---|
409 | echo |
---|
410 | '<td class="module-desc maximal">'.html::escapeHTML($module['desc']).'</td>'; |
---|
411 | } |
---|
412 | |
---|
413 | if (in_array('distrib', $cols)) { |
---|
414 | echo |
---|
415 | '<td class="module-distrib">'.(self::isDistributedModule($id) ? |
---|
416 | '<img src="images/dotclear_pw.png" alt="'. |
---|
417 | __('Module from official distribution').'" title="'. |
---|
418 | __('module from official distribution').'" />' |
---|
419 | : '').'</td>'; |
---|
420 | } |
---|
421 | |
---|
422 | if (!empty($actions) && $this->core->auth->isSuperAdmin()) { |
---|
423 | echo |
---|
424 | '<td class="module-actions nowrap">'; |
---|
425 | |
---|
426 | $this->displayLineActions($id, $module, $actions); |
---|
427 | |
---|
428 | echo |
---|
429 | '</td>'; |
---|
430 | } |
---|
431 | |
---|
432 | echo |
---|
433 | '</tr>'; |
---|
434 | |
---|
435 | $count++; |
---|
436 | } |
---|
437 | echo |
---|
438 | '</table></div>'; |
---|
439 | |
---|
440 | if(!$count) { |
---|
441 | echo |
---|
442 | '<p class="message">'.__('No module matches your search.').'</p>'; |
---|
443 | } |
---|
444 | } |
---|
445 | |
---|
446 | protected function displayLineActions($id, $module, $actions, $echo=true) |
---|
447 | { |
---|
448 | $submits = array(); |
---|
449 | |
---|
450 | # Activate |
---|
451 | if (in_array('deactivate', $actions) && $module['root_writable']) { |
---|
452 | $submits[] = '<input type="submit" name="deactivate" value="'.__('Deactivate').'" />'; |
---|
453 | } |
---|
454 | |
---|
455 | # Deactivate |
---|
456 | if (in_array('activate', $actions) && $module['root_writable']) { |
---|
457 | $submits[] = '<input type="submit" name="activate" value="'.__('Activate').'" />'; |
---|
458 | } |
---|
459 | |
---|
460 | # Delete |
---|
461 | if (in_array('delete', $actions) && $this->isPathDeletable($module['root'])) { |
---|
462 | $submits[] = '<input type="submit" class="delete" name="delete" value="'.__('Delete').'" />'; |
---|
463 | } |
---|
464 | |
---|
465 | # Install (form repository) |
---|
466 | if (in_array('install', $actions) && $this->path_writable) { |
---|
467 | $submits[] = '<input type="submit" name="install" value="'.__('Install').'" />'; |
---|
468 | } |
---|
469 | |
---|
470 | # Update (from repository) |
---|
471 | if (in_array('update', $actions) && $this->path_writable) { |
---|
472 | $submits[] = '<input type="submit" name="update" value="'.__('Update').'" />'; |
---|
473 | } |
---|
474 | |
---|
475 | # Parse form |
---|
476 | if (!empty($submits)) { |
---|
477 | $res = |
---|
478 | '<form action="'.$this->getPageURL().'" method="post">'. |
---|
479 | '<div>'. |
---|
480 | $this->core->formNonce(). |
---|
481 | form::hidden(array('module'), html::escapeHTML($id)). |
---|
482 | form::hidden(array('tab'), $this->page_tab). |
---|
483 | implode(' ', $submits). |
---|
484 | '</div>'. |
---|
485 | '</form>'; |
---|
486 | |
---|
487 | if (!$echo) { |
---|
488 | return $res; |
---|
489 | } |
---|
490 | echo $res; |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | public function executeAction($prefix, dcModules $modules, dcRepository $repository) |
---|
495 | { |
---|
496 | if (!$this->core->auth->isSuperAdmin() || !$this->isPathWritable()) { |
---|
497 | return null; |
---|
498 | } |
---|
499 | |
---|
500 | # List actions |
---|
501 | if (!empty($_POST['module'])) { |
---|
502 | |
---|
503 | $id = $_POST['module']; |
---|
504 | |
---|
505 | if (!empty($_POST['activate'])) { |
---|
506 | |
---|
507 | $enabled = $modules->getDisabledModules(); |
---|
508 | if (!isset($enabled[$id])) { |
---|
509 | throw new Exception(__('No such module.')); |
---|
510 | } |
---|
511 | |
---|
512 | # --BEHAVIOR-- moduleBeforeActivate |
---|
513 | $this->core->callBehavior($type.'BeforeActivate', $id); |
---|
514 | |
---|
515 | $modules->activateModule($id); |
---|
516 | |
---|
517 | # --BEHAVIOR-- moduleAfterActivate |
---|
518 | $this->core->callBehavior($type.'AfterActivate', $id); |
---|
519 | |
---|
520 | http::redirect($this->getPageURL('msg=activate')); |
---|
521 | } |
---|
522 | |
---|
523 | if (!empty($_POST['deactivate'])) { |
---|
524 | |
---|
525 | if (!$modules->moduleExists($id)) { |
---|
526 | throw new Exception(__('No such module.')); |
---|
527 | } |
---|
528 | |
---|
529 | $module = $modules->getModules($id); |
---|
530 | $module['id'] = $id; |
---|
531 | |
---|
532 | if (!$module['root_writable']) { |
---|
533 | throw new Exception(__('You don\'t have permissions to deactivate this module.')); |
---|
534 | } |
---|
535 | |
---|
536 | # --BEHAVIOR-- moduleBeforeDeactivate |
---|
537 | $this->core->callBehavior($prefix.'BeforeDeactivate', $module); |
---|
538 | |
---|
539 | $modules->deactivateModule($id); |
---|
540 | |
---|
541 | # --BEHAVIOR-- moduleAfterDeactivate |
---|
542 | $this->core->callBehavior($prefix.'AfterDeactivate', $module); |
---|
543 | |
---|
544 | http::redirect($this->getPageURL('msg=deactivate')); |
---|
545 | } |
---|
546 | |
---|
547 | if (!empty($_POST['delete'])) { |
---|
548 | |
---|
549 | $disabled = $modules->getDisabledModules(); |
---|
550 | if (!isset($disabled[$id])) { |
---|
551 | |
---|
552 | if (!$modules->moduleExists($id)) { |
---|
553 | throw new Exception(__('No such module.')); |
---|
554 | } |
---|
555 | |
---|
556 | $module = $modules->getModules($id); |
---|
557 | $module['id'] = $id; |
---|
558 | |
---|
559 | if (!$this->isPathDeletable($module['root'])) { |
---|
560 | throw new Exception(__("You don't have permissions to delete this module.")); |
---|
561 | } |
---|
562 | |
---|
563 | # --BEHAVIOR-- moduleBeforeDelete |
---|
564 | $this->core->callBehavior($prefix.'BeforeDelete', $module); |
---|
565 | |
---|
566 | $modules->deleteModule($id); |
---|
567 | |
---|
568 | # --BEHAVIOR-- moduleAfterDelete |
---|
569 | $this->core->callBehavior($prefix.'AfterDelete', $module); |
---|
570 | } |
---|
571 | else { |
---|
572 | $modules->deleteModule($id, true); |
---|
573 | } |
---|
574 | |
---|
575 | http::redirect($this->getPageURL('msg=delete')); |
---|
576 | } |
---|
577 | |
---|
578 | if (!empty($_POST['update'])) { |
---|
579 | |
---|
580 | $updated = $repository->get(); |
---|
581 | if (!isset($updated[$id])) { |
---|
582 | throw new Exception(__('No such module.')); |
---|
583 | } |
---|
584 | |
---|
585 | if (!$modules->moduleExists($id)) { |
---|
586 | throw new Exception(__('No such module.')); |
---|
587 | } |
---|
588 | |
---|
589 | $module = $updated[$id]; |
---|
590 | $module['id'] = $id; |
---|
591 | |
---|
592 | if (!self::$allow_multi_install) { |
---|
593 | $dest = $module['root'].'/../'.basename($module['file']); |
---|
594 | } |
---|
595 | else { |
---|
596 | $dest = $this->getPath().'/'.basename($module['file']); |
---|
597 | if ($module['root'] != $dest) { |
---|
598 | @file_put_contents($module['root'].'/_disabled', ''); |
---|
599 | } |
---|
600 | } |
---|
601 | |
---|
602 | # --BEHAVIOR-- moduleBeforeUpdate |
---|
603 | $this->core->callBehavior($type.'BeforeUpdate', $module); |
---|
604 | |
---|
605 | $repository->process($module['file'], $dest); |
---|
606 | |
---|
607 | # --BEHAVIOR-- moduleAfterUpdate |
---|
608 | $this->core->callBehavior($type.'AfterUpdate', $module); |
---|
609 | |
---|
610 | http::redirect($this->getPageURL('msg=upadte')); |
---|
611 | } |
---|
612 | |
---|
613 | if (!empty($_POST['install'])) { |
---|
614 | |
---|
615 | $updated = $repository->get(); |
---|
616 | if (!isset($updated[$id])) { |
---|
617 | throw new Exception(__('No such module.')); |
---|
618 | } |
---|
619 | |
---|
620 | $module = $updated[$id]; |
---|
621 | $module['id'] = $id; |
---|
622 | |
---|
623 | $dest = $this->getPath().'/'.basename($module['file']); |
---|
624 | |
---|
625 | # --BEHAVIOR-- moduleBeforeAdd |
---|
626 | $this->core->callBehavior($type.'BeforeAdd', $module); |
---|
627 | |
---|
628 | $ret_code = $repository->process($module['file'], $dest); |
---|
629 | |
---|
630 | # --BEHAVIOR-- moduleAfterAdd |
---|
631 | $this->core->callBehavior($type.'AfterAdd', $module); |
---|
632 | |
---|
633 | http::redirect($this->getPageURL('msg='.($ret_code == 2 ? 'update' : 'install'))); |
---|
634 | } |
---|
635 | } |
---|
636 | # Manual actions |
---|
637 | elseif (!empty($_POST['upload_pkg']) && !empty($_FILES['pkg_file']) |
---|
638 | || !empty($_POST['fetch_pkg']) && !empty($_POST['pkg_url'])) |
---|
639 | { |
---|
640 | if (empty($_POST['your_pwd']) || !$this->core->auth->checkPassword(crypt::hmac(DC_MASTER_KEY, $_POST['your_pwd']))) { |
---|
641 | throw new Exception(__('Password verification failed')); |
---|
642 | } |
---|
643 | |
---|
644 | if (!empty($_POST['upload_pkg'])) { |
---|
645 | files::uploadStatus($_FILES['pkg_file']); |
---|
646 | |
---|
647 | $dest = $this->getPath().'/'.$_FILES['pkg_file']['name']; |
---|
648 | if (!move_uploaded_file($_FILES['pkg_file']['tmp_name'], $dest)) { |
---|
649 | throw new Exception(__('Unable to move uploaded file.')); |
---|
650 | } |
---|
651 | } |
---|
652 | else { |
---|
653 | $url = urldecode($_POST['pkg_url']); |
---|
654 | $dest = $this->getPath().'/'.basename($url); |
---|
655 | $repository->download($url, $dest); |
---|
656 | } |
---|
657 | |
---|
658 | # --BEHAVIOR-- moduleBeforeAdd |
---|
659 | $this->core->callBehavior($prefix.'BeforeAdd', null); |
---|
660 | |
---|
661 | $ret_code = $repository->install($dest); |
---|
662 | |
---|
663 | # --BEHAVIOR-- moduleAfterAdd |
---|
664 | $this->core->callBehavior($prefix.'AfterAdd', null); |
---|
665 | |
---|
666 | http::redirect($this->getPageURL('msg='.($ret_code == 2 ? 'update' : 'install')).'#'.$prefix); |
---|
667 | } |
---|
668 | return null; |
---|
669 | } |
---|
670 | |
---|
671 | public function displayManualForm() |
---|
672 | { |
---|
673 | if (!$this->core->auth->isSuperAdmin() || !$this->isPathWritable()) { |
---|
674 | return null; |
---|
675 | } |
---|
676 | |
---|
677 | # 'Upload module' form |
---|
678 | echo |
---|
679 | '<form method="post" action="'.$this->getPageURL().'" id="uploadpkg" enctype="multipart/form-data" class="fieldset">'. |
---|
680 | '<h4>'.__('Upload a zip file').'</h4>'. |
---|
681 | '<p class="field"><label for="pkg_file" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.__('Zip file path:').'</label> '. |
---|
682 | '<input type="file" name="pkg_file" id="pkg_file" /></p>'. |
---|
683 | '<p class="field"><label for="your_pwd1" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.__('Your password:').'</label> '. |
---|
684 | form::password(array('your_pwd','your_pwd1'),20,255).'</p>'. |
---|
685 | '<p><input type="submit" name="upload_pkg" value="'.__('Upload').'" />'. |
---|
686 | form::hidden(array('tab'), $this->getPageTab()). |
---|
687 | $this->core->formNonce().'</p>'. |
---|
688 | '</form>'; |
---|
689 | |
---|
690 | # 'Fetch module' form |
---|
691 | echo |
---|
692 | '<form method="post" action="'.$this->getPageURL().'" id="fetchpkg" class="fieldset">'. |
---|
693 | '<h4>'.__('Download a zip file').'</h4>'. |
---|
694 | '<p class="field"><label for="pkg_url" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.__('Zip file URL:').'</label> '. |
---|
695 | form::field(array('pkg_url','pkg_url'),40,255).'</p>'. |
---|
696 | '<p class="field"><label for="your_pwd2" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.__('Your password:').'</label> '. |
---|
697 | form::password(array('your_pwd','your_pwd2'),20,255).'</p>'. |
---|
698 | '<p><input type="submit" name="fetch_pkg" value="'.__('Download').'" />'. |
---|
699 | form::hidden(array('tab'), $this->getPageTab()). |
---|
700 | $this->core->formNonce().'</p>'. |
---|
701 | '</form>'; |
---|
702 | } |
---|
703 | |
---|
704 | public static function sanitizeString($str) |
---|
705 | { |
---|
706 | return preg_replace('/[^A-Za-z0-9\@\#+_-]/', '', strtolower($str)); |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | class adminThemesList extends adminModulesList |
---|
711 | { |
---|
712 | protected $page_url = 'blog_theme.php'; |
---|
713 | |
---|
714 | public function displayModulesList($cols=array('name', 'config', 'version', 'desc'), $actions=array(), $nav_limit=false) |
---|
715 | { |
---|
716 | echo |
---|
717 | '<div id="'.html::escapeHTML($this->list_id).'" class="modules'.(in_array('expander', $cols) ? ' expandable' : '').' one-box">'; |
---|
718 | |
---|
719 | $sort_field = $this->getSortQuery(); |
---|
720 | |
---|
721 | # Sort modules by id |
---|
722 | $modules = $this->getSearchQuery() === null ? |
---|
723 | self::sortModules($this->modules, $sort_field, $this->sort_asc) : |
---|
724 | $this->modules; |
---|
725 | |
---|
726 | $res = ''; |
---|
727 | $count = 0; |
---|
728 | foreach ($modules as $id => $module) |
---|
729 | { |
---|
730 | # Show only requested modules |
---|
731 | if ($nav_limit && $this->getSearchQuery() === null) { |
---|
732 | $char = substr($module[$sort_field], 0, 1); |
---|
733 | if (!in_array($char, $this->nav_list)) { |
---|
734 | $char = $this->nav_special; |
---|
735 | } |
---|
736 | if ($this->getNavQuery() != $char) { |
---|
737 | continue; |
---|
738 | } |
---|
739 | } |
---|
740 | |
---|
741 | $current = $this->core->blog->settings->system->theme == $id; |
---|
742 | |
---|
743 | if (preg_match('#^http(s)?://#', $this->core->blog->settings->system->themes_url)) { |
---|
744 | $theme_url = http::concatURL($this->core->blog->settings->system->themes_url, '/'.$id); |
---|
745 | } else { |
---|
746 | $theme_url = http::concatURL($this->core->blog->url, $this->core->blog->settings->system->themes_url.'/'.$id); |
---|
747 | } |
---|
748 | |
---|
749 | $has_conf = file_exists(path::real($this->core->blog->themes_path.'/'.$id).'/_config.php'); |
---|
750 | $has_css = file_exists(path::real($this->core->blog->themes_path.'/'.$id).'/style.css'); |
---|
751 | $parent = $module['parent']; |
---|
752 | $has_parent = !empty($module['parent']); |
---|
753 | if ($has_parent) { |
---|
754 | $is_parent_present = $this->core->themes->moduleExists($parent); |
---|
755 | } |
---|
756 | |
---|
757 | $line = |
---|
758 | '<div class="box '.($current ? 'current-theme' : 'theme').'">'; |
---|
759 | |
---|
760 | if (in_array('sshot', $cols)) { |
---|
761 | # Screenshot from url |
---|
762 | if (preg_match('#^http(s)?://#', $module['sshot'])) { |
---|
763 | $sshot = $module['sshot']; |
---|
764 | } |
---|
765 | # Screenshot from installed module |
---|
766 | elseif (file_exists($this->core->blog->themes_path.'/'.$id.'/screenshot.jpg')) { |
---|
767 | $sshot = $this->getPageURL('shot='.rawurlencode($id)); |
---|
768 | } |
---|
769 | # Default screenshot |
---|
770 | else { |
---|
771 | $sshot = 'images/noscreenshot.png'; |
---|
772 | } |
---|
773 | |
---|
774 | $line .= |
---|
775 | '<div class="module-sshot"><img src="'.$sshot.'" alt="'.__('screenshot.').'" /></div>'; |
---|
776 | } |
---|
777 | |
---|
778 | if (in_array('name', $cols)) { |
---|
779 | $line .= |
---|
780 | '<h4 class="module-name">'.html::escapeHTML($module['name']).'</h4>'; |
---|
781 | } |
---|
782 | |
---|
783 | $line .= |
---|
784 | '<div class="module-infos">'. |
---|
785 | '<p>'; |
---|
786 | |
---|
787 | if (in_array('desc', $cols)) { |
---|
788 | $line .= |
---|
789 | '<span class="module-desc">'.html::escapeHTML($module['desc']).'</span> '; |
---|
790 | } |
---|
791 | |
---|
792 | if (in_array('author', $cols)) { |
---|
793 | $line .= |
---|
794 | '<span class="module-author">'.sprintf(__('by %s'),html::escapeHTML($module['author'])).'</span> '; |
---|
795 | } |
---|
796 | |
---|
797 | if (in_array('version', $cols)) { |
---|
798 | $line .= |
---|
799 | '<span class="module-version">'.sprintf(__('version %s'),html::escapeHTML($module['version'])).'</span> '; |
---|
800 | } |
---|
801 | |
---|
802 | if (in_array('parent', $cols) && $has_parent) { |
---|
803 | if ($is_parent_present) { |
---|
804 | $line .= |
---|
805 | '<span class="module-parent-ok">'.sprintf(__('(built on "%s")'),html::escapeHTML($parent)).'</span> '; |
---|
806 | } |
---|
807 | else { |
---|
808 | $line .= |
---|
809 | '<span class="module-parent-missing">'.sprintf(__('(requires "%s")'),html::escapeHTML($parent)).'</span> '; |
---|
810 | } |
---|
811 | } |
---|
812 | |
---|
813 | $line .= |
---|
814 | '</p>'. |
---|
815 | '</div>'; |
---|
816 | |
---|
817 | $line .= |
---|
818 | '<div class="modules-actions">'; |
---|
819 | |
---|
820 | # _GET actions |
---|
821 | $line .= |
---|
822 | '<p>'; |
---|
823 | |
---|
824 | if ($current && $has_css) { |
---|
825 | $line .= |
---|
826 | '<a href="'.$theme_url.'/style.css" class="button">'.__('View stylesheet').'</a> '; |
---|
827 | } |
---|
828 | if ($current && $has_conf) { |
---|
829 | $line .= |
---|
830 | '<a href="'.$this->getPageURL('conf=1').'" class="button">'.__('Configure theme').'</a> '; |
---|
831 | } |
---|
832 | $line .= |
---|
833 | '</p>'; |
---|
834 | |
---|
835 | # Plugins actions |
---|
836 | if ($current) { |
---|
837 | # --BEHAVIOR-- adminCurrentThemeDetails |
---|
838 | $line .= |
---|
839 | $this->core->callBehavior('adminCurrentThemeDetails', $this->core, $id, $module); |
---|
840 | } |
---|
841 | |
---|
842 | # _POST actions |
---|
843 | if (!empty($actions) && $this->core->auth->isSuperAdmin()) { |
---|
844 | $line .= |
---|
845 | $this->displayLineActions($id, $module, $actions, false); |
---|
846 | } |
---|
847 | |
---|
848 | $line .= |
---|
849 | '</div>'; |
---|
850 | |
---|
851 | $line .= |
---|
852 | '</div>'; |
---|
853 | |
---|
854 | $count++; |
---|
855 | |
---|
856 | $res = $current ? $line.$res : $res.$line; |
---|
857 | } |
---|
858 | echo |
---|
859 | $res. |
---|
860 | '</div>'; |
---|
861 | |
---|
862 | if(!$count) { |
---|
863 | echo |
---|
864 | '<p class="message">'.__('No module matches your search.').'</p>'; |
---|
865 | } |
---|
866 | } |
---|
867 | } |
---|