Dotclear

source: plugins/lePluginDuJour/inc/class.dc.leplugindujour.php @ 3053

Revision 3053, 11.8 KB checked in by lipki, 12 years ago (diff)

compatible 2.3.0

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of lePluginDuJour, a plugin for Dotclear 2.
5#
6# Copyright (c) 2010 lipki and contributors
7# kevin@lepeltier.info
8#
9# Licensed under the GPL version 2.0 license.
10# A copy of this license is available in LICENSE file or at
11# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
12#
13# -- END LICENSE BLOCK ------------------------------------
14
15class dclePluginDuJour {
16     
17     public static function lePluginDuJourDashboard($core,$icons) {
18
19          if ($core->auth->isSuperAdmin()) {
20         
21               self::refresh($core);
22         
23               $plugin = $core->blog->settings->leplugindujour->plugin;
24               $label = $core->blog->settings->leplugindujour->label;
25               $desc = $core->blog->settings->leplugindujour->desc;
26               $author = $core->blog->settings->leplugindujour->author;
27               $details = $core->blog->settings->leplugindujour->details;
28               
29               $doc_links = $icons->offsetGet(0);
30               $txt_plugin = clone $icons->offsetGet(0);
31               $news = $icons->offsetGet(1);
32               
33               $txt_plugin[0] ='<div class="message" style="background:url(http://media.dotaddict.org/pda/dc2/'.$plugin.'/icon.png) 8px 6px no-repeat;">'.
34                         '<h3 style="color:#cccccc;">'.$label.'</h3>'.
35                         '<p><em>'.$desc.'</em></p>'.
36                         '<p>'.__('by').' '.$author.'<br />'.
37                         '( <a href="'.$details.'" class="learnmore modal">'.__('More details').'</a> )</p></div>';
38               
39               $icons->offsetSet(0, $txt_plugin);
40               $icons->offsetSet(1, $doc_links);
41               $icons->offsetSet(2, $news);
42               
43          }
44     }
45     
46     public static function refresh($core) {
47     
48          $day = $core->blog->settings->leplugindujour->day;
49         
50          if( $day != date("j, n, Y") ) {
51               
52               $lePluginDuJour = new dcLePluginDuJour($core);
53               $lePluginDuJour->check();
54               $avail_plugins = $lePluginDuJour->getModules('plugins');
55               
56               $day = date("j, n, Y");
57               $plugin = array_rand($avail_plugins);
58               $avail_plugin = $avail_plugins[$plugin];
59               
60               $plugin = html::escapeHTML($avail_plugin['id']);
61               $label = html::escapeHTML($avail_plugin['label']);
62               $desc = html::escapeHTML($avail_plugin['desc']);
63               $author = html::escapeHTML($avail_plugin['author']);
64               $details = $avail_plugin['details'];
65               
66               $core->blog->settings->leplugindujour->put('day', $day);
67               $core->blog->settings->leplugindujour->put('plugin', $plugin);
68               $core->blog->settings->leplugindujour->put('label', $label);
69               $core->blog->settings->leplugindujour->put('desc', $desc);
70               $core->blog->settings->leplugindujour->put('author', $author);
71               $core->blog->settings->leplugindujour->put('details', $details);
72               
73          }
74     }
75     
76     public static function adminEnabledPlugin($core, $settings) {
77          echo '<p><label class="classic">'.
78          form::checkbox('leplugindujour_enabled','1',$settings->leplugindujour->enabled).
79          __('Enable Le Plugin Du Jour').'</label></p>'.
80          '<p class="form-note">'.$core->plugins->moduleInfo('lePluginDuJour','desc').'</p>';
81     }
82     
83     public static function adminBeforeBlogSettingsUpdate($settings) {
84          $settings->addNameSpace('leplugindujour');
85          $settings->leplugindujour->put('enabled',!empty($_POST['leplugindujour_enabled']),'boolean');
86     }
87
88     public static function initWidgets($widgets) {
89          $widgets->create('lePluginDuJour',__('Le Plugin Du Jour'), array('dcLePluginDuJour','widget'));
90
91          $widgets->lePluginDuJour->setting('title',__('Title:'), 'Le Plugin Du Jour','text');
92          $widgets->lePluginDuJour->setting('icon',__('icon'), true,'check');
93
94     }
95     
96     public static function widget($widget) {
97     
98          global $core;
99         
100          self::refresh($core);
101         
102          $plugin = $core->blog->settings->leplugindujour->plugin;
103          $label = $core->blog->settings->leplugindujour->label;
104          $desc = $core->blog->settings->leplugindujour->desc;
105          $author = $core->blog->settings->leplugindujour->author;
106          $details = $core->blog->settings->leplugindujour->details;
107
108          $res =  '<div class="lePluginDuJour">';
109          if( $widget->title ) $res .= 
110                   '<h2>'.$widget->title.'</h2>';
111          $res .= '<h3 ';
112          if( $widget->icon ) $res .= 
113                   'style="background:url(http://media.dotaddict.org/pda/dc2/'.$plugin.'/icon.png) 8px 6px no-repeat;padding-left: 27px;"';
114          $res .= '>'.$label.'</h3>'.
115                   '<p><em>'.$desc.'</em></p>'.
116                   '<p>'.__('by').' '.$author.'<br />'.
117                   '( <a href="'.$details.'" class="learnmore modal">'.__('More details').'</a> )</p></div>';
118
119          return $res;
120     }
121     
122     
123     protected $core;
124     
125     # Set via plugin's settings
126     protected $themes_xml;
127     protected $plugins_xml;
128
129     # Raw datas
130     protected $modules;
131     
132     /**
133      * "Look, Ma ! That's what we call a constructor."
134      * Nothing more to say about it for now. :-)
135      *
136      */
137     public function __construct($core)
138     {
139          $this->core = $core;
140         
141          # Settings compatibility test
142          $s = $core->blog->settings->leplugindujour;
143          $this->themes_xml   =  $s->themes_xml;
144          $this->plugins_xml  =  $s->plugins_xml;
145          $this->modules = array(
146               'plugins' => array(
147                    'new'     => array(),
148                    'update'  => array()
149               ),
150               'themes'  => array(
151                    'new'     => array(),
152                    'update'  => array()
153               )
154          );
155     }
156     
157     /**
158      * Check new/updated plugins availability
159      * Plugins already installed but marked as disabled will be ignored.
160      * Results of this method are stored as two subarrays 'new' and 'update' in $modules['plugins'].
161      *
162      * @param      boolean   $force    Forces datas refresh if true (default to false)
163      *         
164      * @return     boolean   Reports operatio status :
165      *                       - true if operation was successful,
166      *                       - false otherwise, you may check internal error trace.
167      *
168      */
169     protected function checkPlugins($force = false)
170     {
171          if (!$this->plugins_xml) {
172               return false;
173          }
174          try {
175               if (($parser = daModulesReader::quickParse($this->plugins_xml,DC_TPL_CACHE,$force)) === false) {
176                    return false;
177               }
178               
179               $raw_datas = $parser->getModules();
180               
181               uasort($raw_datas,array('self','sort'));
182               
183               # On se débarasse des plugins désactivés.
184               $skipped = array_keys($this->core->plugins->getDisabledModules());
185               foreach ($skipped as $p_id) {
186                    if (isset($raw_datas[$p_id])) {
187                         unset($raw_datas[$p_id]);
188                    }
189               }
190               
191               # On vérifie les mises à jour
192               $updates = array();
193               $current = $this->core->plugins->getModules();
194               foreach ($current as $p_id => $p_infos) {
195                    if (isset($raw_datas[$p_id])) {
196                         if (self::da_version_compare($raw_datas[$p_id]['version'],$p_infos['version'],'>')) {
197                              $updates[$p_id] = $raw_datas[$p_id];
198                              $updates[$p_id]['root'] = $p_infos['root'];
199                              $updates[$p_id]['root_writable'] = $p_infos['root_writable'];
200                              $updates[$p_id]['current_version'] = $p_infos['version'];
201                         }
202                         unset($raw_datas[$p_id]);
203                    }
204               }
205               
206               $this->modules['plugins'] = array(
207                    'new'     => $raw_datas,
208                    'update'  => $updates
209               );
210
211               return true;
212          }
213          catch (Exception $e) {
214               # Probablement à compléter.
215               return false;
216          }
217     }
218     
219     /**
220      * Check new/updated themes availability
221      * Results of this method are stored as two subarrays 'new' and 'update' in $modules['themes'].
222      *
223      * @param      boolean   $force    Forces datas refresh if true (default to false)
224      *         
225      * @return     boolean   Reports operatio status :
226      *                       - true if operation was successful,
227      *                       - false otherwise, you may check internal error trace.
228      *
229      */
230     protected function checkThemes($force = false)
231     {
232          if (!$this->themes_xml) {
233               return false;
234          }
235          try {
236               if (($parser = daModulesReader::quickParse($this->themes_xml,DC_TPL_CACHE,$force)) === false) {
237                    return false;
238               }
239               
240               $raw_datas = $parser->getModules();
241               
242               uasort($raw_datas,array('self','sort'));
243               
244               # On vérifie les mises à jour
245               $updates = array();
246               $core_themes = new dcModules($this->core);
247               $core_themes->loadModules($this->core->blog->themes_path,null);
248               $current = $core_themes->getModules();
249               foreach ($current as $p_id => $p_infos) {
250                    if (isset($raw_datas[$p_id])) {
251                         if (self::da_version_compare($raw_datas[$p_id]['version'],$p_infos['version'],'>')) {
252                              $updates[$p_id] = $raw_datas[$p_id];
253                              $updates[$p_id]['root'] = $p_infos['root'];
254                              $updates[$p_id]['root_writable'] = $p_infos['root_writable'];
255                              $updates[$p_id]['current_version'] = $p_infos['version'];
256                         }
257                         unset($raw_datas[$p_id]);
258                    }
259               }
260               
261               $this->modules['themes'] = array(
262                    'new'     => $raw_datas,
263                    'update'  => $updates
264               );
265               
266               return true;
267          }
268          catch (Exception $e) {
269               # Probablement à compléter.
270               return false;
271          }
272     }
273     
274     /**
275      * Get informations about available new//updated themes/plugins.
276      * Probably the first method to invoke after instanciation.
277      *
278      * @param      boolean   $force    Forces datas refresh if true (default to false)
279      *
280      */
281     public function check($force = false)
282     {
283          if (!$this->checkPlugins($force)) {
284               return false;
285          }
286          if (!$this->checkThemes($force)) {
287               return false;
288          }
289          return true;
290     }
291     
292     /**
293      * Retrieve a specific module list
294      *
295      * @param      string    $type     The type of modules wanted ('plugins' or 'themes')
296      * @param      boolean   $update   Flag to choose between new or updated modules (default to false - new modules list)
297      *
298      * @return     mixed     The matching modules entries as an array, if any. Or a boolean set to false.
299      *
300      */
301     public function getModules($type, $update = false)
302     {
303          $type = ($type == 'themes') ? 'themes' : 'plugins';
304          $what = ($update) ? 'update' : 'new';
305          if (isset($this->modules[$type][$what])) {
306               return $this->modules[$type][$what];
307          }
308          return false;
309     }
310     
311     /**
312      * Search a string in module id, label and description.
313      * Search is case-insensitive and can be apply to available themes or plugins
314      *
315      * @param      string    $search   The search string
316      * @param      string    $type     The type of targeted modules ('plugins' or 'themes')
317      *
318      * @return     array     An array of matching modules entries
319      *
320      */
321     public function search($search,$type = 'plugins')
322     {
323          $type = ($type == 'themes') ? 'themes' : 'plugins';
324          $result = array();
325         
326          foreach ($this->modules[$type]['new'] as $module)
327          {
328               if ( preg_match('/'.$search.'/i',$module['id']) ||
329                    preg_match('/'.$search.'/i',$module['label']) ||
330                    preg_match('/'.$search.'/i',$module['desc']))
331               {
332                    $result[] = $module;
333               }
334          }
335          return $result;
336     }
337     
338     /**
339      * Helper method to fetch and install a ZIP package.
340      *
341      * @param      string    $url           Source file URL
342      * @param      string    $dest          Target file destination
343      * @param      dcModules $coreModules   Target modules stack
344      *
345      * @return     integer   Basic operation status code :
346      *                       - 1 : everything's all right,
347      *                       - 2 : tempfile couldn't be deleted.
348      *
349      */
350     public function processPackage($url,$dest,dcModules $coreModules)
351     {
352          try {
353               $client = netHttp::initClient($url,$path);
354               $client->setUserAgent(self::getUserAgent());
355               $client->useGzip(false);
356               $client->setPersistReferers(false);
357               $client->setOutput($dest);
358               $client->get($path);
359          }
360          catch (Exception $e) {
361               throw new Exception(__('An error occurred while downloading the file.'));
362          }
363         
364          unset($client);
365          $ret_code = dcModules::installPackage($dest,$coreModules);
366         
367          return $ret_code;
368     }
369     
370     /**
371      * Helper method to get user agent according to DC and lePluginDuJour version
372      *
373      * @return     string    lePluginDuJour user agent
374      *
375      */
376     public static function getUserAgent()
377     {
378          $m_version = $GLOBALS['core']->plugins->moduleInfo('lePluginDuJour','version');
379          return sprintf('lePluginDuJour/%s (Dotclear/%s)',$m_version,DC_VERSION);
380     }
381     
382     /**
383      * Helper method to compare correctly version.
384      *
385      * @param      string    $v1            Version of first module
386      * @param      string    $v2            Version of second module
387      * @param      string    $op            Operator
388      *
389      * @return     boolean   True if test of version is correct according to operator
390      *
391      */
392     private static function da_version_compare($v1,$v2,$op)
393     {
394          $v1 = preg_replace('!-r(\d+)$!','-p$1',$v1);
395          $v2 = preg_replace('!-r(\d+)$!','-p$1',$v2);
396          return version_compare($v1,$v2,$op);
397     }
398     
399     /**
400      * Helper method to sort module list.
401      *
402      * @param      array     $a             First module
403      * @param      array     $b             Second module
404      *
405      * @return     integer
406      *
407      */
408     private static function sort($a,$b)
409     {
410          $c = strtolower($a['id']); 
411          $d = strtolower($b['id']); 
412          if ($c == $d) { 
413               return 0; 
414          } 
415          return ($c < $d) ? -1 : 1; 
416     }
417}
418
419?>
Note: See TracBrowser for help on using the repository browser.

Sites map