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

Last change on this file since 7310 was 7310, checked in by grum, 14 years ago

fix bug on the rbuilder and migrate ajax call on the gpc ajax manager
fix bug:1945
add some functionnalities

File size: 13.0 KB
Line 
1<?php
2
3/* -----------------------------------------------------------------------------
4  class name     : GPCCore
5  class version  : 1.3.1
6  plugin version : 3.3.2
7  date           : 2010-10-20
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|         |            |
33
34  ------------------------------------------------------------------------------
35    no constructor, only static function are provided
36    - static function loadConfig
37    - static function loadConfigFromFile
38    - static function saveConfig
39    - static function deleteConfig
40    - static function getRegistered
41    - static function getModulesInfos
42    - static function register
43    - static function unregister
44    - static function BBtoHTML
45    - static function addHeaderCSS
46    - static function addHeaderJS
47    - static function getUserLanguageDesc
48    - static function getPiwigoSystemPath
49    - static function formatOctet
50   ---------------------------------------------------------------------- */
51
52
53
54class GPCCore
55{
56  static private $piwigoSystemPath;
57
58  static public $pluginName = "GPCCore";
59  static protected $headerItems = array(
60    'css' => array(),
61    'js'  => array()
62  );
63
64  static public function init()
65  {
66    self::$piwigoSystemPath=dirname(dirname(dirname(dirname(__FILE__))));
67  }
68
69  /* ---------------------------------------------------------------------------
70   * grum plugin classes informations functions
71   * -------------------------------------------------------------------------*/
72  static public function getModulesInfos()
73  {
74    return(
75      Array(
76        Array('name' => "CommonPlugin", 'version' => "2.2.0"),
77        Array('name' => "GPCAjax", 'version' => "3.0.0"),
78        Array('name' => "GPCCore", 'version' => "1.2.0"),
79        Array('name' => "GPCCss", 'version' => "3.0.0"),
80        Array('name' => "GPCPagesNavigations", 'version' => "2.0.0"),
81        Array('name' => "GPCPublicIntegration", 'version' => "2.0.0"),
82        Array('name' => "GPCRequestBuilder", 'version' => "1.1.0"),
83        Array('name' => "GPCTables", 'version' => "1.5.0"),
84        Array('name' => "GPCTabSheet", 'version' => "1.1.0"),
85        Array('name' => "GPCTranslate", 'version' => "2.1.0"),
86        Array('name' => "GPCUsersGroups", 'version' => "2.0.0"),
87      )
88    );
89  }
90
91
92  /* ---------------------------------------------------------------------------
93   * register oriented functions
94   * -------------------------------------------------------------------------*/
95
96  /**
97   * register a plugin using GPC
98   *
99   * @param String $pluginName : the plugin name
100   * @param String $release : the plugin version like "2.0.0"
101   * @param String $GPCNeed : the minimal version of GPC needed by the plugin to
102   *                          work
103   * @return Boolean : true if registering is Ok, otherwise false
104   */
105  static public function register($plugin, $release, $GPCneeded)
106  {
107    $config=Array();
108    if(self::loadConfig(self::$pluginName, $config))
109    {
110      $config['registered'][$plugin]=Array(
111        'name' => $plugin,
112        'release' => $release,
113        'needed' => $GPCneeded,
114        'date' => date("Y-m-d"),
115      );
116      return(self::saveConfig(self::$pluginName, $config));
117    }
118    return(false);
119  }
120
121  /**
122   * unregister a plugin using GPC
123   *
124   * assume that if the plugin was not registerd before, unregistering returns
125   * a true value
126   *
127   * @param String $pluginName : the plugin name
128   * @return Boolean : true if registering is Ok, otherwise false
129   */
130  static public function unregister($plugin)
131  {
132    $config=Array();
133    if(self::loadConfig(self::$pluginName, $config))
134    {
135      if(array_key_exists('registered', $config))
136      {
137        if(array_key_exists($plugin, $config['registered']))
138        {
139          unset($config['registered'][$plugin]);
140          return(self::saveConfig(self::$pluginName, $config));
141        }
142      }
143    }
144    // assume if the plugin was not registered before, unregistering it is OK
145    return(true);
146  }
147
148  /**
149   * @return Array : list of registered plugins
150   */
151  static public function getRegistered()
152  {
153    $config=Array();
154    if(self::loadConfig(self::$pluginName, $config))
155    {
156      if(array_key_exists('registered', $config))
157      {
158        return($config['registered']);
159      }
160    }
161    return(Array());
162  }
163
164
165
166  /* ---------------------------------------------------------------------------
167   * config oriented functions
168   * -------------------------------------------------------------------------*/
169
170  /**
171   *  load config from CONFIG_TABLE into an array
172   *
173   * @param String $pluginName : the plugin name, must contain only alphanumerical
174   *                             character
175   * @param Array $config : array, initialized or not with default values ; the
176   *                        config values are loaded in this value
177   * @return Boolean : true if config is loaded, otherwise false
178   */
179  static public function loadConfig($pluginName, &$config=Array())
180  {
181    if(!is_array($config))
182    {
183      return(false);
184    }
185
186    $sql="SELECT value FROM ".CONFIG_TABLE."
187          WHERE param = '".$pluginName."_config'";
188    $result=pwg_query($sql);
189    if($result)
190    {
191      $row=pwg_db_fetch_row($result);
192      if(is_string($row[0]))
193      {
194        $configValues = unserialize($row[0]);
195        reset($configValues);
196        while (list($key, $val) = each($configValues))
197        {
198          if(is_array($val))
199          {
200            foreach($val as $key2 => $val2)
201            {
202              $config[$key][$key2]=$val2;
203            }
204          }
205          else
206          {
207            $config[$key] =$val;
208          }
209        }
210      }
211      return(true);
212    }
213    return(false);
214  }
215
216  /**
217   *  load config from a file into an array
218   *
219   *  note : the config file is a PHP file one var $conf used as an array,
220   *  like the piwigo $conf var
221   *
222   * @param String $fileName : the file name
223   * @param Array $config : array, initialized or not with default values ; the
224   *                        config values are loaded in this value
225   * @return Boolean : true if config is loaded, otherwise false
226   */
227  static public function loadConfigFromFile($fileName, &$config=Array())
228  {
229    $conf=array();
230
231    if(!is_array($config) or !file_exists($fileName))
232    {
233      return(false);
234    }
235
236    include_once($fileName);
237
238    foreach($conf as $key=>$val)
239    {
240      $config[$key]=$val;
241    }
242    return(true);
243  }
244
245
246  /**
247   * save var $my_config into CONFIG_TABLE
248   *
249   * @param String $pluginName : the plugin name, must contain only alphanumerical
250   *                             character
251   * @param Array $config : array of configuration values
252   * @return Boolean : true if config is saved, otherwise false
253   */
254  static public function saveConfig($pluginName, $config)
255  {
256    $sql="REPLACE INTO ".CONFIG_TABLE."
257           VALUES('".$pluginName."_config', '"
258           .serialize($config)."', '')";
259    $result=pwg_query($sql);
260    if($result)
261    { return true; }
262    else
263    { return false; }
264  }
265
266  /**
267   * delete config from CONFIG_TABLE
268   *
269   * @param String $pluginName : the plugin name, must contain only alphanumerical
270   *                             character
271   * @return Boolean : true if config is deleted, otherwise false
272   */
273  static public function deleteConfig($pluginName)
274  {
275    $sql="DELETE FROM ".CONFIG_TABLE."
276          WHERE param='".$pluginName."_config'";
277    $result=pwg_query($sql);
278    if($result)
279    { return true; }
280    else
281    { return false; }
282  }
283
284
285  /**
286   * convert (light) BB tag to HTML tag
287   *
288   * all BB codes are not recognized, only :
289   *  - [ul] [/ul]
290   *  - [li] [/li]
291   *  - [b] [/b]
292   *  - [i] [/i]
293   *  - [url] [/url]
294   *  - carriage return is replaced by a <br>
295   *
296   * @param String $text : text to convert
297   * @return String : BB to HTML text
298   */
299  static public function BBtoHTML($text)
300  {
301    $patterns = Array(
302      '/\[li\](.*?)\[\/li\]\n*/im',
303      '/\[b\](.*?)\[\/b\]/ism',
304      '/\[i\](.*?)\[\/i\]/ism',
305      '/\[p\](.*?)\[\/p\]/ism',
306      '/\[url\]([\w]+?:\/\/[^ \"\n\r\t<]*?)\[\/url\]/ism',
307      '/\[url=([\w]+?:\/\/[^ \"\n\r\t<]*?)\](.*?)\[\/url\]/ism',
308      '/\n{0,1}\[ul\]\n{0,1}/im',
309      '/\n{0,1}\[\/ul\]\n{0,1}/im',
310      '/\n{0,1}\[ol\]\n{0,1}/im',
311      '/\n{0,1}\[\/ol\]\n{0,1}/im',
312      '/\n/im',
313    );
314    $replacements = Array(
315      '<li>\1</li>',
316      '<b>\1</b>',
317      '<i>\1</i>',
318      '<p>\1</p>',
319      '<a href="\1">\1</a>',
320      '<a href="\1">\2</a>',
321      '<ul>',
322      '</ul>',
323      '<ol>',
324      '</ol>',
325      '<br>',
326    );
327
328    return(preg_replace($patterns, $replacements, $text));
329  }
330
331  /**
332   * used to add a css file in the header
333   *
334   * @param String $id : a unique id for the file
335   * @param String $file : the css file
336   */
337  static public function addHeaderCSS($id, $file)
338  {
339    if(!array_key_exists($file, self::$headerItems['css']))
340    {
341      self::$headerItems['css'][$id]=$file;
342    }
343  }
344  static public function addHeaderJS($id, $file)
345  {
346    global $template;
347
348    if(!isset($template->known_scripts)) $template->known_scripts=array();
349
350    if(!array_key_exists($id,  $template->known_scripts) and !array_key_exists($file, self::$headerItems['js']))
351    {
352     $template->known_scripts[$id]=$file;
353     self::$headerItems['js'][$id]=$file;
354    }
355  }
356
357  /**
358   * declared as public to be accessible by the event manager, but this funcion
359   * is not aimed to be used directly
360   */
361  static public function applyHeaderItems()
362  {
363    global $template;
364
365    foreach(self::$headerItems['css'] as $file)
366    {
367      $template->append('head_elements', '<link rel="stylesheet" type="text/css" href="'.$file.'"/>');
368    }
369
370    foreach(self::$headerItems['js'] as $file)
371    {
372      $template->append('head_elements', '<script type="text/javascript" src="'.$file.'"></script>');
373    }
374  }
375
376  /**
377   * use the extended description get_user_language_desc() function if exist
378   * otherwise returns the value
379   *
380   * @param String $value : value to translate
381   * @return String : translated value
382   */
383  static public function getUserLanguageDesc($value)
384  {
385    if(function_exists('get_user_language_desc'))
386    {
387      return(get_user_language_desc($value));
388    }
389    else
390    {
391      return($value);
392    }
393  }
394
395  /**
396   * returns the piwigo system path
397   * @return String
398   */
399  static public function getPiwigoSystemPath()
400  {
401    return(self::$piwigoSystemPath);
402  }
403
404
405 /**
406  * formats a file size into a human readable size
407  *
408  * @param String $format : "A"  : auto
409  *                         "Ai" : auto (io)
410  *                         "O"  : o
411  *                         "K"  : Ko
412  *                         "M"  : Mo
413  *                         "G"  : Go
414  *                         "Ki" : Kio
415  *                         "Mi" : Mio
416  *                         "Gi" : Gio
417  * @param String $thsep : thousand separator
418  * @param Integer $prec : number of decimals
419  * @param Bool $visible : display or not the unit
420  * @return String : a formatted file size
421  */
422 static public function formatOctet($octets, $format="Ai", $thsep="", $prec=2, $visible=true)
423 {
424  if($format=="Ai")
425  {
426   if($octets<1024)
427   { $format="O"; }
428   elseif($octets<1024000)
429   { $format="Ki"; }
430   elseif($octets<1024000000)
431   { $format="Mi"; }
432   else
433   { $format="Gi"; }
434  }
435  elseif($format=="A")
436  {
437   if($octets<1000)
438   { $format="O"; }
439   elseif($octets<1000000)
440   { $format="Ki"; }
441   elseif($octets<1000000000)
442   { $format="Mi"; }
443   else
444   { $format="Gi"; }
445  }
446
447  switch($format)
448  {
449   case "O":
450    $unit="o"; $div=1;
451    break;
452   case "K":
453    $unit="Ko"; $div=1000;
454    break;
455   case "M":
456    $unit="Mo"; $div=1000000;
457    break;
458   case "G":
459    $unit="Go"; $div=1000000000;
460    break;
461   case "Ki":
462    $unit="Kio"; $div=1024;
463    break;
464   case "Mi":
465    $unit="Mio"; $div=1024000;
466    break;
467   case "Gi":
468    $unit="Gio"; $div=1024000000;
469    break;
470  }
471
472  $returned=number_format($octets/$div, $prec, '.', $thsep);
473  if($visible) $returned.=' '.$unit;
474  return($returned);
475 } //function formatOctet
476
477
478} //class
479
480add_event_handler('loc_begin_page_header', array('GPCCore', 'applyHeaderItems'), 10);
481
482GPCCore::init();
483
484?>
Note: See TracBrowser for help on using the repository browser.