Dotclear

Changeset 2149:04a490b510cc


Ignore:
Timestamp:
09/29/13 14:14:48 (11 years ago)
Author:
Denis Jean-Chirstian <contact@…>
Branch:
dcRepo
Message:

Put actions on modules lists helper (and some stuff)

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • admin/style/default.css

    r2123 r2149  
    19681968     vertical-align: middle; 
    19691969     } 
     1970.icon img { 
     1971     width: 16px; 
     1972     height: 16px; 
     1973     } 
    19701974.distrib img { 
    19711975     display: block; 
    19721976     float: right; 
    1973      margin-top: -1em; 
    19741977     } 
    19751978/* ---------------------------------------------------------- post.php, page.php */ 
  • inc/admin/lib.moduleslist.php

    r2148 r2149  
    66     public $modules; 
    77 
    8      protected $page_url = ''; 
     8     public static  $allow_multi_install; 
     9 
     10     protected $path = false; 
     11     protected $path_writable = false; 
     12     protected $path_pattern = false; 
     13 
     14     protected $page_url = 'plugins.php'; 
    915     protected $page_qs = '?'; 
    1016     protected $page_tab = ''; 
     
    1723     protected $sort_asc = true; 
    1824 
    19      public function __construct($core, $modules, $page_url='', $page_tab='') 
     25     public function __construct($core, $root, $allow_multi_install=false) 
    2026     { 
    2127          $this->core = $core; 
    22           $this->setModules($modules); 
    23           $this->setPageURL($page_url); 
    24           $this->setPageTab($page_tab); 
     28          self::$allow_multi_install = (boolean) $allow_multi_install; 
     29          $this->setPathInfo($root); 
    2530          $this->setNavSpecial(__('other')); 
    2631 
     
    3136     { 
    3237          return null; 
     38     } 
     39 
     40     protected function setPathInfo($root) 
     41     { 
     42          $paths = explode(PATH_SEPARATOR, $root); 
     43          $path = array_pop($paths); 
     44          unset($paths); 
     45 
     46          $this->path = $path; 
     47          if (is_dir($path) && is_writeable($path)) { 
     48               $this->path_writable = true; 
     49               $this->path_pattern = preg_quote($path,'!'); 
     50          } 
     51 
     52          return $this; 
     53     } 
     54 
     55     public function getPath() 
     56     { 
     57          return $this->path; 
     58     } 
     59 
     60     public function isPathWritable() 
     61     { 
     62          return $this->path_writable; 
     63     } 
     64 
     65     public function isPathDeletable($root) 
     66     { 
     67          return $this->path_writable  
     68               && preg_match('!^'.$this->path_pattern.'!', $root)  
     69               && $this->core->auth->isSuperAdmin(); 
    3370     } 
    3471 
     
    4481     { 
    4582          return $this->page_url. 
    46                ($with_tab && !empty($this->page_tab) || !empty($queries) ? $this->page_qs : ''). 
     83               (!empty($queries) ? $this->page_qs : ''). 
    4784               (is_array($queries) ? http_build_query($queries) : $queries). 
    48                ($with_tab && !empty($this->page_tab) && !empty($queries) ? '&' : ''). 
    49                ($with_tab && !empty($this->page_tab) ? /*'tab='$this->page_tab.*/'#'.$this->page_tab : ''); 
     85               ($with_tab && !empty($this->page_tab) ? '#'.$this->page_tab : ''); 
    5086     } 
    5187 
     
    173209     { 
    174210          //not yet implemented 
     211     } 
     212 
     213     public function displayMessage($action) 
     214     { 
     215          switch($action) { 
     216               case 'activate':  
     217                    $str = __('Module successfully activated.'); break; 
     218               case 'deactivate':  
     219                    $str = __('Module successfully deactivated.'); break; 
     220               case 'delete':  
     221                    $str = __('Module successfully deleted.'); break; 
     222               case 'install':  
     223                    $str = __('Module successfully installed.'); break; 
     224               case 'update':  
     225                    $str = __('Module successfully updated.'); break; 
     226               default: 
     227                    $str = ''; break; 
     228          } 
     229          if (!empty($str)) { 
     230               dcPage::success($str); 
     231          } 
    175232     } 
    176233 
     
    261318          '<div class="table-outer">'. 
    262319          '<table class="modules"><caption class="hidden">'.html::escapeHTML(__('Modules list')).'</caption><tr>'; 
    263  
     320/* 
     321          if ($this->getSearchQuery() !== null) { 
     322               echo  
     323               '<th class="nowrap">'.__('Accuracy').'</th>'; 
     324          } 
     325//*/ 
    264326          if (in_array('name', $cols)) { 
    265327               echo  
    266                '<th class="first nowrap">'.__('Name').'</th>'; 
     328               '<th class="first nowrap"'.(in_array('icon', $cols) ? ' colspan="2"' : '').'>'.__('Name').'</th>'; 
    267329          } 
    268330 
     
    280342               echo  
    281343               '<th class="nowrap" scope="col">'.__('Details').'</th>'; 
     344          } 
     345 
     346          if (in_array('distrib', $cols)) { 
     347               echo '<th'.(in_array('desc', $cols) ? '' : ' class="maximal"').'></th>'; 
    282348          } 
    283349 
     
    314380               '<tr class="line" id="'.$this->getPageTab().'_p_'.html::escapeHTML($id).'">'; 
    315381 
     382               if (in_array('icon', $cols)) { 
     383                    echo  
     384                    '<td class="nowrap icon">'.sprintf( 
     385                         '<img alt="%1$s" title="%1$s" src="%2$s" />',  
     386                         html::escapeHTML($id), file_exists($module['root'].'/icon.png') ? 'index.php?pf='.$id.'/icon.png' : 'images/favicon.png' 
     387                    ).'</td>'; 
     388               } 
     389/* 
     390               if ($this->getSearchQuery() !== null) { 
     391                    echo  
     392                    '<td class="nowrap count">'.$module['accuracy'].'</td>'; 
     393               } 
     394//*/ 
    316395               # Link to config file 
    317396               $config = in_array('config', $cols) && !empty($module['root']) && file_exists(path::real($module['root'].'/_config.php')); 
     
    319398               echo  
    320399               '<td class="nowrap" scope="row">'.($config ?  
    321                     '<a href="'.$this->getPageURL('module='.$id.'&conf=1').'">'.html::escapeHTML(__($module['name'])).'</a>' :  
    322                     html::escapeHTML(__($module['name'])) 
     400                    '<a href="'.$this->getPageURL('module='.$id.'&conf=1').'">'.html::escapeHTML($module['name']).'</a>' :  
     401                    html::escapeHTML($module['name']) 
    323402               ).'</td>'; 
    324403 
     
    335414               if (in_array('desc', $cols)) { 
    336415                    echo  
    337                     '<td class="maximal">'.html::escapeHTML(__($module['desc'])).'</td>'; 
     416                    '<td class="maximal">'.html::escapeHTML($module['desc']).'</td>'; 
     417               } 
     418 
     419               if (in_array('distrib', $cols)) { 
     420                    echo  
     421                    '<td class="distrib">'.(self::isDistributedModule($id) ?  
     422                         '<img src="images/dotclear_pw.png" alt="'. 
     423                         __('Module from official distribution').'" title="'. 
     424                         __('module from official distribution').'" />'  
     425                    : '').'</td>'; 
    338426               } 
    339427 
     
    375463               $submits[] = '<input type="submit" name="activate" value="'.__('Activate').'" />'; 
    376464          } 
    377 /* 
     465 
    378466          # Delete 
    379           if (in_array('delete', $actions) && $this->isPathWritable() && preg_match('!^'.$this->path_pattern.'!', $module['root'])) { 
     467          if (in_array('delete', $actions) && $this->isPathDeletable($module['root'])) { 
    380468               $submits[] = '<input type="submit" class="delete" name="delete" value="'.__('Delete').'" />'; 
    381469          } 
    382470 
    383471          # Install (form repository) 
    384           if (in_array('install', $actions) && $this->isPathWritable()) { 
     472          if (in_array('install', $actions) && $this->path_writable) { 
    385473               $submits[] = '<input type="submit" name="install" value="'.__('Install').'" />'; 
    386474          } 
    387475 
    388476          # Update (from repository) 
    389           if (in_array('update', $actions) && $this->isPathWritable()) { 
     477          if (in_array('update', $actions) && $this->path_writable) { 
    390478               $submits[] = '<input type="submit" name="update" value="'.__('Update').'" />'; 
    391479          } 
    392 */ 
     480 
    393481          # Parse form 
    394482          if (!empty($submits)) { 
     
    398486               $this->core->formNonce(). 
    399487               form::hidden(array('module'), html::escapeHTML($id)). 
    400                form::hidden(array('tab'), $this->getPageTab()). 
     488               form::hidden(array('tab'), $this->page_tab). 
    401489               implode(' ', $submits). 
    402490               '</div>'. 
     
    405493     } 
    406494 
     495     public function executeAction($prefix, dcModules $modules, dcRepository $repository) 
     496     { 
     497          if (empty($_POST['module'])   || !$this->core->auth->isSuperAdmin() || !$this->isPathWritable()) { 
     498               return null; 
     499          } 
     500 
     501          $id = $_POST['module']; 
     502 
     503          if (!empty($_POST['activate'])) { 
     504 
     505               $enabled = $modules->getDisabledModules(); 
     506               if (!isset($enabled[$id])) { 
     507                    throw new Exception(__('No such module.')); 
     508               } 
     509 
     510               # --BEHAVIOR-- moduleBeforeActivate 
     511               $this->core->callBehavior($type.'BeforeActivate', $id); 
     512 
     513               $modules->activateModule($id); 
     514 
     515               # --BEHAVIOR-- moduleAfterActivate 
     516               $this->core->callBehavior($type.'AfterActivate', $id); 
     517 
     518               http::redirect($this->getPageURL('msg=activate')); 
     519          } 
     520 
     521          if (!empty($_POST['deactivate'])) { 
     522 
     523               if (!$modules->moduleExists($id)) { 
     524                    throw new Exception(__('No such module.')); 
     525               } 
     526 
     527               $module = $modules->getModules($id); 
     528               $module['id'] = $id; 
     529 
     530               if (!$module['root_writable']) { 
     531                    throw new Exception(__('You don\'t have permissions to deactivate this module.')); 
     532               } 
     533 
     534               # --BEHAVIOR-- moduleBeforeDeactivate 
     535               $this->core->callBehavior($prefix.'BeforeDeactivate', $module); 
     536 
     537               $modules->deactivateModule($id); 
     538 
     539               # --BEHAVIOR-- moduleAfterDeactivate 
     540               $this->core->callBehavior($prefix.'AfterDeactivate', $module); 
     541 
     542               http::redirect($this->getPageURL('msg=deactivate')); 
     543          } 
     544 
     545          if (!empty($_POST['delete'])) { 
     546 
     547               $disabled = $modules->getDisabledModules(); 
     548               if (!isset($disabled[$id])) { 
     549 
     550                    if (!$modules->moduleExists($id)) { 
     551                         throw new Exception(__('No such module.')); 
     552                    } 
     553 
     554                    $module = $modules->getModules($id); 
     555                    $module['id'] = $id; 
     556 
     557                    if (!$this->isPathDeletable($module['root'])) { 
     558                         throw new Exception(__("You don't have permissions to delete this module.")); 
     559                    } 
     560 
     561                    # --BEHAVIOR-- moduleBeforeDelete 
     562                    $this->core->callBehavior($prefix.'BeforeDelete', $module); 
     563 
     564                    $modules->deleteModule($id); 
     565 
     566                    # --BEHAVIOR-- moduleAfterDelete 
     567                    $this->core->callBehavior($prefix.'AfterDelete', $module); 
     568               } 
     569               else { 
     570                    $modules->deleteModule($id, true); 
     571               } 
     572 
     573               http::redirect($this->getPageURL('msg=delete')); 
     574          } 
     575 
     576          if (!empty($_POST['update'])) { 
     577 
     578               $updated = $repository->get(); 
     579               if (!isset($updated[$id])) { 
     580                    throw new Exception(__('No such module.')); 
     581               } 
     582 
     583               if (!$modules->moduleExists($id)) { 
     584                    throw new Exception(__('No such module.')); 
     585               } 
     586 
     587               $module = $updated[$id]; 
     588               $module['id'] = $id; 
     589           
     590               if (!self::$allow_multi_install) { 
     591                    $dest = $module['root'].'/../'.basename($module['file']); 
     592               } 
     593               else { 
     594                    $dest = $this->getPath().'/'.basename($module['file']); 
     595                    if ($module['root'] != $dest) { 
     596                         @file_put_contents($module['root'].'/_disabled', ''); 
     597                    } 
     598               } 
     599 
     600               # --BEHAVIOR-- moduleBeforeUpdate 
     601               $this->core->callBehavior($type.'BeforeUpdate', $module); 
     602 
     603               $repository->process($module['file'], $dest); 
     604 
     605               # --BEHAVIOR-- moduleAfterUpdate 
     606               $this->core->callBehavior($type.'AfterUpdate', $module); 
     607 
     608               http::redirect($this->getPageURL('msg=upadte')); 
     609          } 
     610 
     611          if (!empty($_POST['install'])) { 
     612 
     613               $updated = $repository->get(); 
     614               if (!isset($updated[$id])) { 
     615                    throw new Exception(__('No such module.')); 
     616               } 
     617 
     618               $module = $updated[$id]; 
     619               $module['id'] = $id; 
     620 
     621               $dest = $this->getPath().'/'.basename($module['file']); 
     622 
     623               # --BEHAVIOR-- moduleBeforeAdd 
     624               $this->core->callBehavior($type.'BeforeAdd', $module); 
     625 
     626               $ret_code = $repository->process($module['file'], $dest); 
     627 
     628               # --BEHAVIOR-- moduleAfterAdd 
     629               $this->core->callBehavior($type.'AfterAdd', $module); 
     630 
     631               http::redirect($this->getPageURL('msg='.($ret_code == 2 ? 'update' : 'install'))); 
     632          } 
     633     } 
     634 
    407635     public static function sanitizeString($str) 
    408636     { 
  • inc/core/class.dc.repository.php

    r2148 r2149  
    9191               # Increment matches count 
    9292               $sorter[$id] += $nb; 
     93               $result[$id]['accuracy'] = $sorter[$id]; 
    9394          } 
    9495          # Sort response by matches count 
     
    101102     public function process($url, $dest) 
    102103     { 
     104          $this->download($url, $dest); 
     105          return $this->install($dest); 
     106     } 
     107 
     108     public function download($url, $dest) 
     109     { 
    103110          try { 
    104111               $client = netHttp::initClient($url, $path); 
     
    108115               $client->setOutput($dest); 
    109116               $client->get($path); 
     117               unset($client); 
    110118          } 
    111119          catch (Exception $e) { 
     120               unset($client); 
    112121               throw new Exception(__('An error occurred while downloading the file.')); 
    113122          } 
     123     } 
    114124 
    115           unset($client); 
    116           $ret_code = dcModules::installPackage($dest, $this->modules); 
    117  
    118           return $ret_code; 
     125     public function install($path) 
     126     { 
     127          return dcModules::installPackage($path, $this->modules); 
    119128     } 
    120129 
Note: See TracChangeset for help on using the changeset viewer.

Sites map