XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
functions.php
Go to the documentation of this file.
1 <?php
2 // $Id: functions.php 1071 2012-09-22 23:45:24Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
12 {
13  private $plugin_settings = array();
14 
15  public function get(){
16  static $instance;
17 
18  if (!isset($instance))
19  $instance = new RMFunctions();
20 
21  return $instance;
22 
23  }
24 
28  public function configs($name=''){
29  static $rmc_configs;
30 
31  if (!isset($rmc_configs)){
32  $db = XoopsDatabaseFactory::getDatabaseConnection();
33  $sql = "SELECT mid FROM ".$db->prefix("modules")." WHERE dirname='rmcommon'";
34  list($id) = $db->fetchRow($db->query($sql));
35 
36  include_once XOOPS_ROOT_PATH.'/kernel/object.php';
37  include_once XOOPS_ROOT_PATH.'/kernel/configitem.php';
38  include_once XOOPS_ROOT_PATH.'/class/criteria.php';
39  include_once XOOPS_ROOT_PATH.'/class/module.textsanitizer.php';
40  $ret = array();
41  $result = $db->query("SELECT * FROM ".$db->prefix("config")." WHERE conf_modid='$id'");
42 
43  while($row = $db->fetchArray($result)){
44  $config = new XoopsConfigItem();
45  $config->assignVars($row);
46  $rmc_configs[$config->getVar('conf_name')] = $config->getConfValueForOutput();
47  }
48  }
49 
50  $name = trim($name);
51  if($name!=''){
52  if(isset($rmc_configs[$name])) return $rmc_configs[$name];
53  }
54 
55  return $rmc_configs;
56  }
60  public function get_num_records($table, $filters=''){
61 
62  $db = XoopsDatabaseFactory::getDatabaseConnection();
63 
64  $sql = "SELECT COUNT(*) FROM ".$db->prefix($table);
65  $sql .= $filters!='' ? " WHERE $filters" : '';
66 
67  list($num) = $db->fetchRow($db->query($sql));
68 
69  return $num;
70 
71  }
72 
76  public function create_toolbar(){
77 
78  if(RMCLOCATION=='users'){
79 
80  RMTemplate::get()->add_tool(__('Users List','rmcommon'), 'users.php', 'images/users.png', 'allusers');
81  RMTemplate::get()->add_tool(__('Add User','rmcommon'), 'users.php?action=new', 'images/user_add.png', 'newuser');
82 
83  } elseif(RMCLOCATION=='imgmanager'){
84 
85  RMTemplate::get()->add_tool(__('Categories List','rmcommon'), 'images.php?action=showcats', 'images/category.png', 'showcategories');
86  RMTemplate::get()->add_tool(__('Add Category','rmcommon'), 'images.php?action=newcat', 'images/category_add.png', 'newcategory');
87  $cat = rmc_server_var($_REQUEST,'category',0);
88  if($cat>0){
89  RMTemplate::get()->add_tool(__('Images','rmcommon'), 'images.php?category='.$cat, 'images/image.png', 'showimages');
90  }
91  RMTemplate::get()->add_tool(__('Add Images','rmcommon'), 'images.php?action=new'.($cat>0?"&amp;category=$cat":''), 'images/image_add.png', 'addimages');
92 
93  } else {
94 
95  RMTemplate::get()->add_tool(__('Dashboard','rmcommon'), 'index.php', 'images/dashboard.png', 'dashboard');
96  RMTemplate::get()->add_tool(__('Modules','rmcommon'), 'modules.php', 'images/modules.png', 'modules');
97  RMTemplate::get()->add_tool(__('Blocks','rmcommon'), 'blocks.php', 'images/blocks.png', 'blocks');
98  RMTemplate::get()->add_tool(__('Users','rmcommon'), 'users.php', 'images/users.png', 'users');
99  RMTemplate::get()->add_tool(__('Images','rmcommon'), 'images.php', 'images/images.png', 'imgmanager');
100  RMTemplate::get()->add_tool(__('Comments','rmcommon'), 'comments.php', 'images/comments.png', 'comments');
101  RMTemplate::get()->add_tool(__('Plugins','rmcommon'), 'plugins.php', 'images/plugin.png', 'plugins');
102 
103  }
104 
105  RMTemplate::get()->add_tool(__('Updates','rmcommon'), 'updates.php', 'images/updates.png', 'updates');
106  RMEvents::get()->run_event('rmcommon.create.toolbar');
107 
108  }
109 
117  public function urlencode_array($array, $name, $separator='&'){
118 
119  $toImplode = array();
120  foreach ($array as $key => $value) {
121  if (is_array($value)) {
122  $toImplode[] = self::urlencode_array($value, "{$name}[{$key}]", $separator);
123  } else {
124  $toImplode[] = "{$name}[{$key}]=".urlencode($value);
125  }
126  }
127  return implode($separator, $toImplode);
128 
129  }
130 
137  public function get_groups_names($groups, $list = true){
138 
139  $ret = array();
140  if (count($groups)==1 && $groups[0] == 0){
141  $ret[] = __('All','rmcommon');
142  return $list ? __('All','rmcommon') : $ret;
143  }
144 
145  if(in_array(0, $groups)) $ret[] = __('All','rmcommon');
146 
147 
148  $db = XoopsDatabaseFactory::getDatabaseConnection();
149  $sql = "SELECT name FROM ".$db->prefix("groups")." WHERE groupid IN(".implode(',',$groups).")";
150  $result = $db->query($sql);
151  while($row = $db->fetchArray($result)){
152  $ret[] = $row['name'];
153  }
154 
155  if ($list) return implode(', ',$ret);
156  return $ret;
157  }
158 
164  public function load_images_categories($filters='ORDER BY id_cat DESC', $object = false){
165  $db = XoopsDatabaseFactory::getDatabaseConnection();
166  $sql = "SELECT * FROM ".$db->prefix("rmc_img_cats")." $filters";
167  $result = $db->query($sql);
168  $categories = array();
169  while($row = $db->fetchArray($result)){
170  $tc = new RMImageCategory();
171  $tc->assignVars($row);
172  if (!$object){
173  $categories[] = array(
174  'id' => $tc->id(),
175  'name' => $tc->getVar('name')
176  );
177  } else {
178  $categories[] = $tc;
179  }
180  }
181 
182  return $categories;
183  }
184 
194  public function get_comments($obj,$params,$type='module',$parent=0,$user=null,$assign=true){
195  global $xoopsUser;
196 
197  define('COMMENTS_INCLUDED', 1);
198  $db = XoopsDatabaseFactory::getDatabaseConnection();
199 
201 
202  $params = urlencode($params);
203  $sql = "SELECT * FROM ".$db->prefix("rmc_comments")." WHERE status='approved' AND id_obj='$obj' AND params='$params' AND type='$type' AND parent='$parent'".($user==null?'':" AND user='$user'")." ORDER BY posted";
204  $result = $db->query($sql);
205 
206  $ucache = array();
207  $ecache = array();
208 
209  while($row = $db->fetchArray($result)){
210 
211  $com = new RMComment();
212  $com->assignVars($row);
213 
214  // Editor data
215  if(!isset($ecache[$com->getVar('user')])){
216  $ecache[$com->getVar('user')] = new RMCommentUser($com->getVar('user'));
217  }
218 
219  $editor = $ecache[$com->getVar('user')];
220 
221  if($editor->getVar('xuid')>0){
222 
223  if(!isset($ucache[$editor->getVar('xuid')])){
224  $ucache[$editor->getVar('xuid')] = new XoopsUser($editor->getVar('xuid'));
225  }
226 
227  $user = $ucache[$editor->getVar('xuid')];
228 
229  $poster = array(
230  'id' => $user->getVar('uid'),
231  'name' => $user->getVar('uname'),
232  'email' => $user->getVar('email'),
233  'posts' => $user->getVar('posts'),
234  'avatar'=> XOOPS_UPLOAD_URL.'/'.$user->getVar('user_avatar'),
235  'rank' => $user->rank(),
236  'url' => $user->getVar('url')!='http://'?$user->getVar('url'):''
237  );
238 
239  } else {
240 
241  $poster = array(
242  'id' => 0,
243  'name' => $editor->getVar('name'),
244  'email' => $editor->getVar('email'),
245  'posts' => 0,
246  'avatar'=> '',
247  'rank' => '',
248  'url' => $editor->getVar('url')!='http://'?$editor->getVar('url'):''
249  );
250 
251  }
252 
253  if ($xoopsUser && $xoopsUser->isAdmin()){
254  $editlink = RMCURL.'/comments.php?action=edit&amp;id='.$com->id().'&amp;ret='.urlencode(self::current_url());
255  }elseif($rmc_config['allow_edit']){
256  $time_limit = time() - $com->getVar('posted');
257  if($xoopsUser && $xoopsUser->getVar('uid')==$editor->getVar('xuid') && $time_limit<($rmc_config['edit_limit']*3600)){
258  $editlink = RMCURL.'/post_comment.php?action=edit&amp;id='.$com->id().'&amp;ret='.urlencode(self::current_url());
259  } else {
260  $editlink = '';
261  }
262  }
263 
264  $comms[] = array(
265  'id' => $row['id_com'],
266  'text' => TextCleaner::getInstance()->clean_disabled_tags(TextCleaner::getInstance()->popuplinks(TextCleaner::getInstance()->nofollow($com->getVar('content')))),
267  'poster' => $poster,
268  'posted' => sprintf(__('Posted on %s'), formatTimestamp($com->getVar('posted'), 'l')),
269  'ip' => $com->getVar('ip'),
270  'edit' => $editlink
271  );
272 
273  unset($editor);
274  }
275 
276  $comms = RMEvents::get()->run_event('rmcommon.loading.comments', $comms, $obj, $params, $type, $parent, $user);
277  global $xoopsTpl;
278  $xoopsTpl->assign('lang_edit', __('Edit','rmcommon'));
279 
280  if ($assign){
281  $xoopsTpl->assign('comments', $comms);
282  return true;
283  } else {
284  return $comms;
285  }
286 
287  }
288 
298  public function comments_form($obj, $params, $type='module', $file=array()){
299  global $xoopsTpl, $xoopsRequestUri, $xoopsUser;
300 
301  $config = self::configs();
302 
303  if (!$config['enable_comments']){
304  return;
305  }
306 
307  if (!$xoopsUser && !$config['anonymous_comments']){
308  return;
309  }
310 
311  if (!defined('COMMENTS_INCLUDED')){
312  define('COMMENTS_INCLUDED', 1);
313  }
314 
315  $xoopsTpl->assign('enable_comments_form', 1);
316 
317  $form = array(
318  'show_name' => !($xoopsUser),
319  'lang_name' => __('Name','rmcommon'),
320  'show_email' => !($xoopsUser),
321  'lang_email' => __('Email address','rmcommon'),
322  'show_url' => !($xoopsUser),
323  'lang_url' => __('Web site', 'rmcommon'),
324  'lang_text' => __('Your comment', 'rmcommon'),
325  'lang_submit' => __('Submit Comment', 'rmcommon'),
326  'lang_title' => __('Submit a comment', 'rmcommon'),
327  'uri' => urlencode(RMFunctions::current_url()),
328  'actionurl' => RMCURL.'/post_comment.php',
329  'params' => urlencode($params),
330  'update' => urlencode(str_replace(XOOPS_ROOT_PATH, '', $file)),
331  'type' => $type,
332  'object' => $obj,
333  'action' => 'save'
334  );
335 
336  // You can include new content into Comments form
337  // eg. Captcha checker, etc
338 
339  $form = RMEvents::get()->run_event('rmcommon.comments.form', $form, $obj, $params, $type);
340  RMTemplate::get()->add_jquery();
341  RMTemplate::get()->add_script(RMCURL.'/include/js/jquery.validate.min.js');
342  RMTemplate::get()->add_head('<script type="text/javascript">
343  $(document).ready(function(){
344  $("#rmc-comment-form").validate({
345  messages: {
346  comment_name: "'.__('Please specify your name','rmcommon').'",
347  comment_email: "'.__('Please specify a valid email','rmcommon').'",
348  comment_text: "'.__('Please write a message','rmcommon').'",
349  comment_url: "'.__('Please enter a valid URL','rmcommon').'"
350  }
351  });
352  });</script>');
353 
354  $xoopsTpl->assign('cf', $form);
355 
356  }
357 
363  public function delete_comments($module, $params){
364 
365  if ($module=='' || $params == '') return;
366 
367  $db = XoopsDatabaseFactory::getDatabaseConnection();
368  $sql = "DELETE FROM ".$db->prefix("rmc_comments")." WHERE id_obj='$module' AND params='$params'";
369 
370  // Event
371  RMEvents::get()->run_event('rmcommon.deleting.comments', $module, $params);
372 
373  return $db->queryF($sql);
374 
375  }
376 
377  public function current_url() {
378  $pageURL = 'http';
379  if (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on") {$pageURL .= "s";}
380  $pageURL .= "://";
381  $pageURL .= $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
382  return $pageURL;
383  }
384 
390  public function plugin_settings($dir, $values = false){
391 
392  if ($dir=='') return;
393 
394  if (!isset($this->plugin_settings[$dir])){
395 
396  $db = XoopsDatabaseFactory::getDatabaseConnection();
397  $sql = "SELECT * FROM ".$db->prefix("rmc_settings")." WHERE element='$dir'";
398  $result = $db->query($sql);
399  if($db->getRowsNum($result)<=0) return;
400  $configs = array();
401  while ($row = $db->fetchArray($result)){
402  $configs[$row['name']] = $row;
403  }
404 
405  $configs = $this->getConfValueForOutput($configs);
406  $this->plugin_settings[$dir] = $configs;
407 
408  }
409 
410  if (!$values) return $this->plugin_settings[$dir];
411 
412  $ret = array();
413  foreach($this->plugin_settings[$dir] as $name => $conf){
414  $ret[$name] = $conf['value'];
415  }
416 
417  return $ret;
418 
419  }
420 
421  private function getConfValueForOutput($confs){
422  foreach ($confs as $name => $data){
423 
424  switch ($data['valuetype']) {
425  case 'int':
426  $confs[$name]['value'] = intval($data['value']);
427  break;
428  case 'array':
429  $confs[$name]['value'] = unserialize($data['value']);
430  break;
431  case 'float':
432  $confs[$name]['value'] = floatval($data['value']);
433  break;
434  case 'textarea':
435  $confs[$name]['value'] = stripSlashes($data['value']);
436  break;
437  }
438  }
439 
440  return $confs;
441  }
442 
446  public function plugin_installed($dir){
447 
448  if (isset($GLOBALS['installed_plugins'][$dir]))
449  return true;
450  else
451  return false;
452 
453  }
454 
458  public function load_plugin($name){
459 
460  $name = strtolower($name);
461  if (!file_exists(RMCPATH.'/plugins/'.$name.'/'.$name.'-plugin.php'))
462  return false;
463 
464  include_once RMCPATH.'/plugins/'.$name.'/'.$name.'-plugin.php';
465  $class = ucfirst($name).'CUPlugin';
466 
467  if (!class_exists($class))
468  return false;
469 
470  $plugin = new $class();
471  return $plugin;
472 
473  }
474 
480  public function load_module($mod){
481  $mh = xoops_gethandler('module');
482  if (is_numeric($mod)){
483  $m = $mh->get($mod);
484  } else {
485  $m = $mh->getByDirname($mod);
486  }
487 
488  return $m;
489  }
490 
494  public function get_modules_list($active=-1){
495 
496  $db = XoopsDatabaseFactory::getDatabaseConnection();
497 
498  $sql = "SELECT mid, name, dirname FROM " . $db->prefix("modules");
499  if($active>-1 && $active<2)
500  $sql .= " WHERE isactive=$active";
501 
502  $sql .= " ORDER BY name";
503  $result = $db->query($sql);
504  $modules = array();
505  while ($row = $db->fetchArray($result)) {
506  $modules[] = $row;
507  }
508 
509  return $modules;
510  }
511 
512  public function installed_plugins(){
513 
514  $db = XoopsDatabaseFactory::getDatabaseConnection();
515  $result = $db->query("SELECT dir FROM ".$db->prefix("rmc_plugins").' WHERE status=1');
516  $plugins = array();
517  while($row = $db->fetchArray($result)){
518  $plugins[] = $row['dir'];
519  }
520 
521  $plugins = RMEvents::get()->run_event("rmcommon.installed.plugins", $plugins);
522 
523  return $plugins;
524 
525  }
526 
532  function get_image($id, $size=''){
533 
534  if($id<=0) return false;
535 
536  $img = new RMImage($id);
537 
538  if($img->isNew()) return false;
539 
540  $cat = new RMImageCategory($img->getVar('cat'));
541 
542  $sizes = $cat->getVar('sizes');
543 
544  foreach($sizes as $s){
545  if($s['name'] == $size)
546  break;
547  }
548 
549  $date = explode('-', date('d-m-Y', $img->getVar('date')));
550  $file = XOOPS_UPLOAD_URL.'/'.$date[2].'/'.$date[1].'/';
551  if($size==''){
552  $file .= $img->getVar('file');
553  return $file;
554  }
555 
556  $file .= 'sizes/'.substr($img->getVar('file'), 0, -4).'_'.$s['width'].'x'.$s['height'].substr($img->getVar('file'), -4);
557 
558  if(!is_file(str_replace(XOOPS_URL, XOOPS_ROOT_PATH, $file)))
559  return $img->getOriginal();
560 
561  return $file;
562 
563  }
564 
571  public function add_keywords_description($description, $keywords='', $limit=50){
572 
573  if($description=='') return;
574 
575  $tpl = RMTemplate::get();
576  $tc = TextCleaner::getInstance();
577  $description = strip_tags($description);
578  $tpl->add_meta('description', $tc->truncate($description, 255));
579  if($keywords!=''){
580  $tpl->add_meta('keywords', $keywords);
581  return;
582  }
583 
584  $description = preg_replace("/[^[[:alnum:]]]|[\.,:]/",'', $description);
585  $description = preg_replace("/[[:space:]][[:alnum:]]{0,4}[[:space:]]/",' ',$description);
586 
587  $words = explode(" ", $description);
588  asort($words);
589  $keys = array_rand($words, $limit>count($words) ? count($words) : $limit);
590 
591  foreach($keys as $id){
592  $keywords .= $keywords=='' ? $words[$id] : ', '.$words[$id];
593  }
594 
595  $tpl->add_meta('keywords', $keywords);
596 
597  }
598 
599 }