source: extensions/GrumPluginClasses/classes/GPCCore.class.inc.php @ 8961

Last change on this file since 8961 was 8961, checked in by grum, 13 years ago

release 3.4.0
fix bug:1984, bug:2109
js file are minified, remove packed files

File size: 21.5 KB
Line 
1<?php
2
3/* -----------------------------------------------------------------------------
4  class name     : GPCCore
5  class version  : 1.3.2
6  plugin version : 3.4.0
7  date           : 2011-01-28
8  ------------------------------------------------------------------------------
9  author: grum at piwigo.org
10  << May the Little SpaceFrog be with you >>
11  ------------------------------------------------------------------------------
12
13  :: HISTORY
14
15| release | date       |
16| 1.0.0   | 2010/03/30 | * Update class & function names
17|         |            |
18| 1.1.0   | 2010/03/30 | * add the BBtoHTML function
19|         |            |
20| 1.2.0   | 2010/07/28 | * add the loadConfigFromFile function
21|         |            |
22| 1.3.0   | 2010/10/13 | * add the addHeaderCSS, addHeaderJS functions
23|         |            |
24| 1.3.1   | 2010/10/20 | * applyHeaderItems functions implemented with an
25|         |            |   higher priority on the 'loc_begin_page_header' event
26|         |            |
27|         |            | * implement the getUserLanguageDesc() function, using
28|         |            |   extended description function if present
29|         |            |
30|         |            | * implement the getPiwigoSystemPath() function
31|         |            |
32|         |            | * implement the rmDir() function
33|         |            |
34| 1.3.2   | 2011/01/28 | * implement the addUI() function
35|         |            |
36|         |            | * implement getMinified() & setMinifiedState() functions
37|         |            |
38|         |            |
39|         |            |
40
41  ------------------------------------------------------------------------------
42    no constructor, only static function are provided
43    - static function loadConfig
44    - static function loadConfigFromFile
45    - static function saveConfig
46    - static function deleteConfig
47    - static function getRegistered
48    - static function getModulesInfos
49    - static function register
50    - static function unregister
51    - static function BBtoHTML
52    - static function addHeaderCSS
53    - static function addHeaderJS
54    - static function addUI
55    - static function getMinified
56    - static function setMinifiedState
57    - static function getUserLanguageDesc
58    - static function getPiwigoSystemPath
59    - static function formatOctet
60    - static function rmDir
61   ---------------------------------------------------------------------- */
62
63
64
65class GPCCore
66{
67  static private $piwigoSystemPath;
68  static private $minified='.min';
69
70  static public $pluginName = "GPCCore";
71  static protected $headerItems = array(
72    'css' => array(),
73    'js'  => array()
74  );
75
76  static public function init()
77  {
78    self::$piwigoSystemPath=dirname(dirname(dirname(dirname(__FILE__))));
79  }
80
81  /* ---------------------------------------------------------------------------
82   * grum plugin classes informations functions
83   * -------------------------------------------------------------------------*/
84  static public function getModulesInfos()
85  {
86    return(
87      Array(
88        Array('name' => "CommonPlugin", 'version' => "2.2.0"),
89        Array('name' => "GPCAjax", 'version' => "3.0.0"),
90        Array('name' => "GPCCategorySelector", 'version' => "1.0.1"),
91        Array('name' => "GPCCore", 'version' => "1.3.2"),
92        Array('name' => "GPCCss", 'version' => "3.0.0"),
93        Array('name' => "GPCPagesNavigation", 'version' => "2.0.0"),
94        Array('name' => "GPCPublicIntegration", 'version' => "2.0.0"),
95        Array('name' => "GPCRequestBuilder", 'version' => "1.1.2"),
96        Array('name' => "GPCTables", 'version' => "1.5.0"),
97        Array('name' => "GPCTabSheet", 'version' => "1.1.1"),
98        Array('name' => "GPCTranslate", 'version' => "2.1.1"),
99        Array('name' => "GPCUsersGroups", 'version' => "2.1.0")
100      )
101    );
102  }
103
104
105  /* ---------------------------------------------------------------------------
106   * register oriented functions
107   * -------------------------------------------------------------------------*/
108
109  /**
110   * register a plugin using GPC
111   *
112   * @param String $pluginName : the plugin name
113   * @param String $release : the plugin version like "2.0.0"
114   * @param String $GPCNeed : the minimal version of GPC needed by the plugin to
115   *                          work
116   * @return Boolean : true if registering is Ok, otherwise false
117   */
118  static public function register($plugin, $release, $GPCneeded)
119  {
120    $config=Array();
121    if(self::loadConfig(self::$pluginName, $config))
122    {
123      $config['registered'][$plugin]=Array(
124        'name' => $plugin,
125        'release' => $release,
126        'needed' => $GPCneeded,
127        'date' => date("Y-m-d"),
128      );
129      return(self::saveConfig(self::$pluginName, $config));
130    }
131    return(false);
132  }
133
134  /**
135   * unregister a plugin using GPC
136   *
137   * assume that if the plugin was not registerd before, unregistering returns
138   * a true value
139   *
140   * @param String $pluginName : the plugin name
141   * @return Boolean : true if registering is Ok, otherwise false
142   */
143  static public function unregister($plugin)
144  {
145    $config=Array();
146    if(self::loadConfig(self::$pluginName, $config))
147    {
148      if(array_key_exists('registered', $config))
149      {
150        if(array_key_exists($plugin, $config['registered']))
151        {
152          unset($config['registered'][$plugin]);
153          return(self::saveConfig(self::$pluginName, $config));
154        }
155      }
156    }
157    // assume if the plugin was not registered before, unregistering it is OK
158    return(true);
159  }
160
161  /**
162   * @return Array : list of registered plugins
163   */
164  static public function getRegistered()
165  {
166    $config=Array();
167    if(self::loadConfig(self::$pluginName, $config))
168    {
169      if(array_key_exists('registered', $config))
170      {
171        return($config['registered']);
172      }
173    }
174    return(Array());
175  }
176
177
178
179  /* ---------------------------------------------------------------------------
180   * config oriented functions
181   * -------------------------------------------------------------------------*/
182
183  /**
184   *  load config from CONFIG_TABLE into an array
185   *
186   * @param String $pluginName : the plugin name, must contain only alphanumerical
187   *                             character
188   * @param Array $config : array, initialized or not with default values ; the
189   *                        config values are loaded in this value
190   * @return Boolean : true if config is loaded, otherwise false
191   */
192  static public function loadConfig($pluginName, &$config=Array())
193  {
194    if(!is_array($config))
195    {
196      return(false);
197    }
198
199    $sql="SELECT value FROM ".CONFIG_TABLE."
200          WHERE param = '".$pluginName."_config'";
201    $result=pwg_query($sql);
202    if($result)
203    {
204      $row=pwg_db_fetch_row($result);
205      if(is_string($row[0]))
206      {
207        $configValues = unserialize($row[0]);
208        reset($configValues);
209        while (list($key, $val) = each($configValues))
210        {
211          if(is_array($val))
212          {
213            foreach($val as $key2 => $val2)
214            {
215              $config[$key][$key2]=$val2;
216            }
217          }
218          else
219          {
220            $config[$key] =$val;
221          }
222        }
223      }
224      return(true);
225    }
226    return(false);
227  }
228
229  /**
230   *  load config from a file into an array
231   *
232   *  note : the config file is a PHP file one var $conf used as an array,
233   *  like the piwigo $conf var
234   *
235   * @param String $fileName : the file name
236   * @param Array $config : array, initialized or not with default values ; the
237   *                        config values are loaded in this value
238   * @return Boolean : true if config is loaded, otherwise false
239   */
240  static public function loadConfigFromFile($fileName, &$config=Array())
241  {
242    $conf=array();
243
244    if(!is_array($config) or !file_exists($fileName))
245    {
246      return(false);
247    }
248
249    include_once($fileName);
250
251    foreach($conf as $key=>$val)
252    {
253      $config[$key]=$val;
254    }
255    return(true);
256  }
257
258
259  /**
260   * save var $my_config into CONFIG_TABLE
261   *
262   * @param String $pluginName : the plugin name, must contain only alphanumerical
263   *                             character
264   * @param Array $config : array of configuration values
265   * @return Boolean : true if config is saved, otherwise false
266   */
267  static public function saveConfig($pluginName, $config)
268  {
269    $sql="REPLACE INTO ".CONFIG_TABLE."
270           VALUES('".$pluginName."_config', '"
271           .serialize($config)."', '')";
272    $result=pwg_query($sql);
273    if($result)
274    { return true; }
275    else
276    { return false; }
277  }
278
279  /**
280   * delete config from CONFIG_TABLE
281   *
282   * @param String $pluginName : the plugin name, must contain only alphanumerical
283   *                             character
284   * @return Boolean : true if config is deleted, otherwise false
285   */
286  static public function deleteConfig($pluginName)
287  {
288    $sql="DELETE FROM ".CONFIG_TABLE."
289          WHERE param='".$pluginName."_config'";
290    $result=pwg_query($sql);
291    if($result)
292    { return true; }
293    else
294    { return false; }
295  }
296
297
298  /**
299   * convert (light) BB tag to HTML tag
300   *
301   * all BB codes are not recognized, only :
302   *  - [ul] [/ul]
303   *  - [li] [/li]
304   *  - [b] [/b]
305   *  - [i] [/i]
306   *  - [url] [/url]
307   *  - carriage return is replaced by a <br>
308   *
309   * @param String $text : text to convert
310   * @return String : BB to HTML text
311   */
312  static public function BBtoHTML($text)
313  {
314    $patterns = Array(
315      '/\[li\](.*?)\[\/li\]\n*/im',
316      '/\[b\](.*?)\[\/b\]/ism',
317      '/\[i\](.*?)\[\/i\]/ism',
318      '/\[p\](.*?)\[\/p\]/ism',
319      '/\[url\]([\w]+?:\/\/[^ \"\n\r\t<]*?)\[\/url\]/ism',
320      '/\[url=([\w]+?:\/\/[^ \"\n\r\t<]*?)\](.*?)\[\/url\]/ism',
321      '/\n{0,1}\[ul\]\n{0,1}/im',
322      '/\n{0,1}\[\/ul\]\n{0,1}/im',
323      '/\n{0,1}\[ol\]\n{0,1}/im',
324      '/\n{0,1}\[\/ol\]\n{0,1}/im',
325      '/\n/im',
326    );
327    $replacements = Array(
328      '<li>\1</li>',
329      '<b>\1</b>',
330      '<i>\1</i>',
331      '<p>\1</p>',
332      '<a href="\1">\1</a>',
333      '<a href="\1">\2</a>',
334      '<ul>',
335      '</ul>',
336      '<ol>',
337      '</ol>',
338      '<br>',
339    );
340
341    return(preg_replace($patterns, $replacements, $text));
342  }
343
344  /**
345   * used to add a css file in the header
346   *
347   * @param String $id : a unique id for the file
348   * @param String $file : the css file
349   */
350  static public function addHeaderCSS($id, $file)
351  {
352    if(!array_key_exists($file, self::$headerItems['css']))
353    {
354      self::$headerItems['css'][$id]=$file;
355    }
356  }
357  static public function addHeaderJS($id, $file)
358  {
359    global $template;
360
361    if(!isset($template->known_scripts)) $template->known_scripts=array();
362
363    if(!array_key_exists($id,  $template->known_scripts) and !array_key_exists($file, self::$headerItems['js']))
364    {
365     $template->known_scripts[$id]=$file;
366     self::$headerItems['js'][$id]=$file;
367    }
368  }
369
370  /**
371   * declared as public to be accessible by the event manager, but this funcion
372   * is not aimed to be used directly
373   */
374  static public function applyHeaderItems()
375  {
376    global $template;
377    $dummy1=null;
378    $dummy2=null;
379
380    foreach(self::$headerItems['css'] as $file)
381    {
382      $template->append('head_elements', '<link rel="stylesheet" type="text/css" href="'.$file.'"/>');
383    }
384
385    foreach(self::$headerItems['js'] as $file)
386    {
387      //$template->append('head_elements', '<script type="text/javascript" src="'.$file.'"></script>');
388      $template->block_html_head(null, '<script type="text/javascript" src="'.$file.'"></script>', $dummy1, $dummy2);
389    }
390  }
391
392  /**
393   * add a ui component ; css & js dependencies are managed
394   *
395   * @param Array $list : possibles values are
396   *                        - inputCheckbox
397   *                        - inputColorPicker
398   *                        - inputColorsFB
399   *                        - inputConsole
400   *                        - inputDotArea
401   *                        - inputList
402   *                        - inputNum
403   *                        - inputPosition
404   *                        - inputRadio
405   *                        - inputStatusBar
406   *                        - inputText
407   *                        - categorySelector
408   */
409  static public function addUI($list)
410  {
411    global $template;
412
413    if(is_string($list)) $list=explode(',', $list);
414    if(!is_array($list)) return(false);
415
416    if(defined('IN_ADMIN'))
417    {
418      $themeFile=GPC_PATH.'css/%s_'.$template->get_themeconf('name').'.css';
419    }
420    else
421    {
422      $themeFile='themes/'.$template->get_themeconf('name').'/css/GPC%s.css';
423    }
424
425    foreach($list as $ui)
426    {
427      switch($ui)
428      {
429        case 'googleTranslate':
430          self::addHeaderJS('google.jsapi', 'http://www.google.com/jsapi');
431          self::addHeaderJS('gpc.googleTranslate', 'plugins/GrumPluginClasses/js/google_translate'.self::$minified.'.js');
432        case 'categorySelector':
433          self::addHeaderCSS('gpc.categorySelector', GPC_PATH.'css/categorySelector.css');
434          self::addHeaderCSS('gpc.categorySelectorT', sprintf($themeFile, 'categorySelector'));
435          self::addHeaderJS('gpc.categorySelector', GPC_PATH.'js/ui.categorySelector'.self::$minified.'.js');
436          break;
437        case 'inputCheckbox':
438          self::addHeaderCSS('gpc.inputCheckbox', GPC_PATH.'css/inputCheckbox.css');
439          self::addHeaderJS('gpc.inputCheckbox', GPC_PATH.'js/ui.inputCheckbox'.self::$minified.'.js');
440          break;
441        case 'inputColorPicker':
442          self::addHeaderCSS('gpc.inputText', GPC_PATH.'css/inputText.css');
443          self::addHeaderCSS('gpc.inputNum', GPC_PATH.'css/inputNum.css');
444          self::addHeaderCSS('gpc.inputColorsFB', GPC_PATH.'css/inputColorsFB.css');
445          self::addHeaderCSS('gpc.inputDotArea', GPC_PATH.'css/inputDotArea.css');
446          self::addHeaderCSS('gpc.inputColorPicker', GPC_PATH.'css/inputColorPicker.css');
447          self::addHeaderCSS('gpc.inputTextT', sprintf($themeFile, 'inputText'));
448          self::addHeaderCSS('gpc.inputNumT', sprintf($themeFile, 'inputNum'));
449          self::addHeaderCSS('gpc.inputColorsFBT', sprintf($themeFile, 'inputColorsFB'));
450          self::addHeaderCSS('gpc.inputDotAreaT', sprintf($themeFile, 'inputDotArea'));
451          self::addHeaderCSS('gpc.inputColorPickerT', sprintf($themeFile, 'inputColorPicker'));
452          self::addHeaderJS('jquery.ui', 'themes/default/js/ui/packed/ui.core.packed.js');
453          self::addHeaderJS('jquery.ui.slider', 'themes/default/js/ui/packed/ui.slider.packed.js');
454          self::addHeaderJS('jquery.ui.draggable', 'themes/default/js/ui/packed/ui.draggable.packed.js');
455          self::addHeaderJS('jquery.ui.dialog', 'themes/default/js/ui/packed/ui.slider.dialog.js');
456          self::addHeaderJS('gpc.inputText', GPC_PATH.'js/ui.inputText'.self::$minified.'.js');
457          self::addHeaderJS('gpc.inputNum', GPC_PATH.'js/ui.inputNum'.self::$minified.'.js');
458          self::addHeaderJS('gpc.inputColorsFB', GPC_PATH.'js/ui.inputColorsFB'.self::$minified.'.js');
459          self::addHeaderJS('gpc.inputDotArea', GPC_PATH.'js/ui.inputDotArea'.self::$minified.'.js');
460          self::addHeaderJS('gpc.inputColorPicker', GPC_PATH.'js/ui.inputColorPicker'.self::$minified.'.js');
461          break;
462        case 'inputColorsFB':
463          self::addHeaderCSS('gpc.inputColorsFB', GPC_PATH.'css/inputColorsFB.css');
464          self::addHeaderCSS('gpc.inputColorsFBT', sprintf($themeFile, 'inputColorsFB'));
465          self::addHeaderJS('gpc.inputColorsFB', GPC_PATH.'js/ui.inputColorsFB'.self::$minified.'.js');
466          break;
467        case 'inputConsole':
468          self::addHeaderCSS('gpc.inputConsole', GPC_PATH.'css/inputConsole.css');
469          self::addHeaderCSS('gpc.inputConsoleT', sprintf($themeFile, 'inputConsole'));
470          self::addHeaderJS('gpc.inputConsole', GPC_PATH.'js/ui.inputConsole'.self::$minified.'.js');
471          break;
472        case 'inputDotArea':
473          self::addHeaderCSS('gpc.inputDotArea', GPC_PATH.'css/inputDotArea.css');
474          self::addHeaderCSS('gpc.inputDotAreaT', sprintf($themeFile, 'inputDotArea'));
475          self::addHeaderJS('gpc.inputDotArea', GPC_PATH.'js/ui.inputDotArea'.self::$minified.'.js');
476          break;
477        case 'inputList':
478          self::addHeaderCSS('gpc.inputList', GPC_PATH.'css/inputList.css');
479          self::addHeaderCSS('gpc.inputListT', sprintf($themeFile, 'inputList'));
480          self::addHeaderJS('gpc.inputList', GPC_PATH.'js/ui.inputList'.self::$minified.'.js');
481          break;
482        case 'inputNum':
483          self::addHeaderCSS('gpc.inputNum', GPC_PATH.'css/inputNum.css');
484          self::addHeaderCSS('gpc.inputNumT', sprintf($themeFile, 'inputNum'));
485          self::addHeaderJS('jquery.ui', 'themes/default/js/ui/packed/ui.core.packed.js');
486          self::addHeaderJS('jquery.ui.slider', 'themes/default/js/ui/packed/ui.slider.packed.js');
487          self::addHeaderJS('gpc.inputNum', GPC_PATH.'js/ui.inputNum'.self::$minified.'.js');
488          break;
489        case 'inputPosition':
490          self::addHeaderCSS('gpc.inputPosition', GPC_PATH.'css/inputPosition.css');
491          self::addHeaderCSS('gpc.inputPositionT', sprintf($themeFile, 'inputPosition'));
492          self::addHeaderJS('gpc.inputPosition', GPC_PATH.'js/ui.inputPosition'.self::$minified.'.js');
493          break;
494        case 'inputRadio':
495          self::addHeaderJS('gpc.inputRadio', GPC_PATH.'js/ui.inputRadio'.self::$minified.'.js');
496          break;
497        case 'inputStatusBar':
498          self::addHeaderCSS('gpc.inputStatusBar', GPC_PATH.'css/inputStatusBar.css');
499          self::addHeaderCSS('gpc.inputStatusBarT', sprintf($themeFile, 'inputStatusBar'));
500          self::addHeaderJS('gpc.inputStatusBar', GPC_PATH.'js/ui.inputStatusBar'.self::$minified.'.js');
501          break;
502        case 'inputText':
503          self::addHeaderCSS('gpc.inputText', GPC_PATH.'css/inputText.css');
504          self::addHeaderCSS('gpc.inputTextT', sprintf($themeFile, 'inputText'));
505          self::addHeaderJS('gpc.inputText', GPC_PATH.'js/ui.inputText'.self::$minified.'.js');
506          break;
507      }
508    }
509  }
510
511  /**
512   * return the minified value
513   *
514   * @return String
515   */
516  static public function getMinified()
517  {
518    return(self::$minified);
519  }
520
521  /**
522   * set the minified state
523   *
524   * @param Bool $state
525   * @return Bool
526   */
527  static public function setMinifiedState($state)
528  {
529    if($state)
530    {
531      self::$minified='.min';
532    }
533    else
534    {
535      self::$minified='';
536    }
537    return(self::$minified!='');
538  }
539
540
541  /**
542   * use the extended description get_user_language_desc() function if exist
543   * otherwise returns the value
544   *
545   * @param String $value : value to translate
546   * @return String : translated value
547   */
548  static public function getUserLanguageDesc($value)
549  {
550    if(function_exists('get_user_language_desc'))
551    {
552      return(get_user_language_desc($value));
553    }
554    else
555    {
556      return($value);
557    }
558  }
559
560
561  /**
562   * remove a path recursively
563   *
564   * @param String $directory : directory to remove
565   * @param Bool $removePath : if set to true, remove the path himself, if set
566   *                           to false, remove only file & sub-directories
567   * @return Bool : true if directory was succesfully removed, otherwise false
568   */
569  static public function rmDir($directory, $removePath=true)
570  {
571    $directory=rtrim($directory, '\/').'/';
572    $returned=true;
573    if(file_exists($directory) and is_dir($directory) and $directory!='./' and $directory!='../')
574    {
575      $dhandle=scandir($directory);
576      foreach($dhandle as $file)
577      {
578        if($file!='.' and $file!='..' )
579        {
580          if(is_dir($directory.$file))
581          {
582            $returned=self::rmDir($directory.$file, true) & $returned;
583          }
584          else
585          {
586            $returned=unlink($directory.$file) & $returned;
587          }
588        }
589      }
590      if($returned and $removePath) $returned=rmdir($directory);
591    }
592    return($returned);
593  }
594
595
596  /**
597   * returns the piwigo system path
598   * @return String
599   */
600  static public function getPiwigoSystemPath()
601  {
602    return(self::$piwigoSystemPath);
603  }
604
605
606 /**
607  * formats a file size into a human readable size
608  *
609  * @param String $format : "A"  : auto
610  *                         "Ai" : auto (io)
611  *                         "O"  : o
612  *                         "K"  : Ko
613  *                         "M"  : Mo
614  *                         "G"  : Go
615  *                         "Ki" : Kio
616  *                         "Mi" : Mio
617  *                         "Gi" : Gio
618  * @param String $thsep : thousand separator
619  * @param Integer $prec : number of decimals
620  * @param Bool $visible : display or not the unit
621  * @return String : a formatted file size
622  */
623 static public function formatOctet($octets, $format="Ai", $thsep="", $prec=2, $visible=true)
624 {
625  if($format=="Ai")
626  {
627   if($octets<1024)
628   { $format="O"; }
629   elseif($octets<1024000)
630   { $format="Ki"; }
631   elseif($octets<1024000000)
632   { $format="Mi"; }
633   else
634   { $format="Gi"; }
635  }
636  elseif($format=="A")
637  {
638   if($octets<1000)
639   { $format="O"; }
640   elseif($octets<1000000)
641   { $format="Ki"; }
642   elseif($octets<1000000000)
643   { $format="Mi"; }
644   else
645   { $format="Gi"; }
646  }
647
648  switch($format)
649  {
650   case "O":
651    $unit="o"; $div=1;
652    break;
653   case "K":
654    $unit="Ko"; $div=1000;
655    break;
656   case "M":
657    $unit="Mo"; $div=1000000;
658    break;
659   case "G":
660    $unit="Go"; $div=1000000000;
661    break;
662   case "Ki":
663    $unit="Kio"; $div=1024;
664    break;
665   case "Mi":
666    $unit="Mio"; $div=1024000;
667    break;
668   case "Gi":
669    $unit="Gio"; $div=1024000000;
670    break;
671  }
672
673  $returned=number_format($octets/$div, $prec, '.', $thsep);
674  if($visible) $returned.=' '.$unit;
675  return($returned);
676 } //function formatOctet
677
678
679} //class
680
681add_event_handler('loc_begin_page_header', array('GPCCore', 'applyHeaderItems'), 10);
682
683GPCCore::init();
684
685?>
Note: See TracBrowser for help on using the repository browser.