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

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

release 3.4.3
bug:2167

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