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

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

release 3.4.0
optimise config load

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