XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
users.php
Go to the documentation of this file.
1 <?php
2 // $Id: rss.php 825 2011-12-09 00:06:11Z 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 
16 // Constant to specify the internal location
17 // Could be useful for themes, plugins and modules
18 define('RMCLOCATION', 'users');
19 
20 include '../../include/cp_header.php';
21 
25 function formatSQL(){
26  global $op;
27 
28  $keyw = '';
29  $email = '';
30  $url = '';
31  $srhmethod = '';
32  $from = '';
33  $login1 = ''; $login2 = ''; $register1 = ''; $register2 = '';
34  $posts1 = ''; $posts2 = ''; $mailok = -1; $actives = -1;
35  $show = '';
36 
38  $sql = '';
39  $tcleaner = TextCleaner::getInstance();
40 
41  foreach ($_REQUEST as $k => $v){
42  $$k = $tcleaner->addslashes($v);
43  }
44 
45  $tpl->assign('srhkeyw', $keyw);
46  $tpl->assign('srhemail', $email);
47  $tpl->assign('srhurl', $url);
48  $tpl->assign('srhsrhmethod', $srhmethod);
49  $tpl->assign('srhfrom', $from);
50 
51  if ($show=='inactives'){
52  $sql = "level<=0 AND ";
53  } elseif ($show=='actives'){
54  $sql = "level>0 AND ";
55  }
56 
57  if ($keyw == '' && $email == '' && $url == '' && $from == ''
58  && $login1 == '' && $login2 == '' && $register1 == '' && $register2 == '' && $posts1 == ''
59  && $posts2 == '' && $mailok == -1 && $actives == -1){
60 
61  if ($show=='inactives'){
62  $sql = " level<=0";
63  } elseif ($show=='actives'){
64  $sql = " level>0";
65  }
66 
67  $tpl->assign('display_adv', 'display: none;');
68  // Extend SQL with plugins
69  // API:
70  $sql = RMEvents::get()->run_event('rmcommon.users.getsql', $sql);
71 
72  return $sql!='' ? "WHERE $sql": '';
73 
74  }
75 
76  $or = false;
77  $ao = $srhmethod;
78  $show = false;
79 
80  if ($keyw!=''){
81  $sql .= "uname LIKE '%$keyw%' $ao name LIKE '%$keyw%'";
82  $or = true;
83  }
84 
85  if ($email!=''){
86  $sql .= ($or ? " $ao " : '')."email LIKE '%$email%'";
87  $or = true;
88  $show = true;
89  }
90 
91  if ($url!=''){
92  $sql .= ($or ? " $ao " : '')."url LIKE '%$url%'";
93  $or = true;
94  $show = true;
95  }
96 
97  if ($from!=''){
98  $sql .= ($or ? " $ao " : '')."user_from LIKE '%$from%'";
99  $or = true;
100  $show = true;
101  }
102 
103  if ($login1!=''){
104  $sql .= ($or ? " $ao " : '').($login2!='' ? '(' : '')."last_login>='$login1'";
105  $or = true;
106  $show = true;
107  }
108 
109  if ($login2!=''){
110  $sql .= ($or ? ($login1!='' ? ' AND ' : " $ao ") : '')."last_login<='$login2'".($login1!='' ? ')' : '');
111  $or = true;
112  $show = true;
113  }
114 
115  if ($register1!=''){
116  list($year, $month, $day) = explode("-", $register1);
117  $time = mktime(0,0,0,$month,$day,$year);
118  $sql .= ($or ? " $ao " : '').($register2!='' ? '(' : '')."last_login>='$time'";
119  $or = true;
120  $show = true;
121  }
122 
123  if ($register2!=''){
124  list($year, $month, $day) = explode("-", $register2);
125  $time = mktime(0,0,0,$month,$day,$year);
126  $sql .= ($or ? ($register1!='' ? ' AND ' : " $ao ") : '')."last_login<='$time'".($register1!='' ? ')' : '');
127  $or = true;
128  $show = true;
129  }
130 
131  if ($posts1>0){
132  $sql .= ($or ? " $ao " : '').($posts2!='' ? '(' : '')."posts>='$posts1'";
133  $or = true;
134  $show = true;
135  }
136 
137  if ($posts2>0){
138  $sql .= ($or ? ($posts1!='' ? ' AND ' : " $ao ") : '')."posts<='$posts2'".($posts1!='' ? ')' : '');
139  $or = true;
140  $show = true;
141  }
142 
143  if ($mailok>-1){
144  $sql .= ($or ? " $ao " : '')."user_mailok='$mailok'";
145  $or = true;
146  }
147 
148  if ($actives>-1){
149  $sql .= ($or ? " $ao " : '')."level".($actives>0 ? ">'0'" : "<='0'");
150  $or = true;
151  }
152 
153  if ($show){ $tpl->assign('display_adv', ''); } else { $tpl->assign('display_adv', 'display: none;'); }
154 
155  $rtsql = $sql!='' ? "WHERE $sql" : '';
156  // ** API **
157  // Event to modify, if it is neccesary, the sql string to query de database
158  $rtsql = RMEvents::get()->run_event('rmcommon.users.getsql', $rtsql);
159  return $rtsql;
160 
161 }
162 
166 function show_users(){
167  global $xoopsSecurity;
168 
169  define('RMCSUBLOCATION','allusers');
170  RMTemplate::get()->add_style('users.css','rmcommon');
171  RMTemplate::get()->add_style('js-widgets.css');
172 
173  //Scripts
174  RMTemplate::get()->add_local_script('users.js','rmcommon','include');
175  RMTemplate::get()->add_local_script('jquery.checkboxes.js','rmcommon','include');
176 
177  RMTemplate::get()->add_head('<script type="text/javascript">var rmcu_select_message = "'.__('You have not selected any user!','rmcommon').'";
178  var rmcu_message = "'.__('Dou you really wish to delete selected users?','rmcommon').'";</script>');
179 
180  $form = new RMForm('', '', '');
181  // Date Field
182  $login1 = new RMFormDate('','login1', '');
183  $login2 = new RMFormDate('','login2', '') ;
184 
185  // Registered Field
186  $register1 = new RMFormDate('','registered1', '');
187  $register2 = new RMFormDate('','registered2', '');
188 
189  xoops_cp_location(__('Users Management','rmcommon'));
190 
192 
193  // Show the theme
194  xoops_cp_header();
195 
196  $db = XoopsDatabaseFactory::getDatabaseConnection();
197 
198  $sql = "SELECT COUNT(*) FROM ".$db->prefix("users")." ".formatSQL();
199 
200  $page = rmc_server_var($_REQUEST, 'pag', 1);
201  $limit = rmc_server_var($_REQUEST, 'limit', 15);
202  $order = rmc_server_var($_GET,'order','uid');
203  list($num) = $db->fetchRow($db->query($sql));
204 
205  $tpages = ceil($num / $limit);
206  $page = $page > $tpages ? $tpages : $page;
207 
208  $start = $num<=0 ? 0 : ($page - 1) * $limit;
209 
210  $nav = new RMPageNav($num, $limit, $page, 5);
211  $nav->target_url('users.php?limit='.$limit.'&order='.$order.'&pag={PAGE_NUM}');
212 
213  $sql = str_replace("COUNT(*)",'*', $sql);
214  $sql .= "ORDER BY $order LIMIT $start, $limit";
215  $result = $db->query($sql);
216 
217  $users = array();
218  $user = new XoopsUser();
219  $t = array(); // Temporary
220  while ($row=$db->fetchArray($result)){
221  $user->assignVars($row);
222  $t = $user->getValues();
223  $t['groups'] = $user->groups();
224  $t = RMEvents::get()->run_event('rmcommon.loading.users.list', $t);
225  $users[] = $t;
226  $t = array();
227  }
228 
229  $xgh = new XoopsGroupHandler($db);
230  $users = RMEvents::get()->run_event('rmcommon.users.list.loaded', $users);
231 
232  // Users template
233  include RMTemplate::get()->get_template('rmc_users.php','module','rmcommon');
234 
235  xoops_cp_footer();
236 }
237 
238 /*
239 * Show the form to create or edit a user
240 */
241 function user_form($edit = false){
242  define('RMCSUBLOCATION','newuser');
243  $query = rmc_server_var($_GET, 'query', '');
244  $query = $query=='' ? '' : base64_decode($query);
245 
246  $db = XoopsDatabaseFactory::getDatabaseConnection();
247 
248  if ($edit){
249  $uid = rmc_server_var($_GET, 'uid', 0);
250  if ($uid<=0)
251  redirectMsg('users.php?'.$query, __('The specified user is not valid!','rmcommon'), 1);
252 
253  $uh = new XoopsUserHandler($db);
254  $user = $uh->get($uid);
255  if ($user->isNew())
256  redirectMsg('users.php?'.$query, __('The specified user does not exists!','rmcommon'), 1);
257  }
258 
260 
261  xoops_cp_location("<a href='users.php'>".__('Users Management','rmcommon')."</a> &raquo; ".__($edit ? 'Editing User' : 'Adding new user','rmcommon'));
262  xoops_cp_header();
263 
264  $form = new RMForm(__($edit ? 'Editing User' : 'Add new user','rmcommon'), 'user_form', 'users.php');
265 
266  // Uname
267  $form->addElement(new RMFormText(__('Username','rmcommon'), 'uname', 50, 50, $edit ? $user->uname() : ''), true);
268  $form->element('uname')->setDescription(__("This field also will be the user login name.",'rmcommon'));
269 
270  // Full Name
271  $form->addElement(new RMFormText(__('Full name','rmcommon'), 'name', 50, 150, $edit ? $user->name() : ''));
272  $form->element('name')->setDescription(__("This field must contain firstname and lastname.",'rmcommon'));
273 
274  // Email
275  $form->addElement(new RMFormText(__('Email address','rmcommon'), 'email', 50, 150, $edit ? $user->email() : ''), true, 'email');
276 
277  // Password
278  $form->addElement(new RMFormText(__($edit ? 'New password' : 'Password','rmcommon'), 'password', 50, 50, '', true), $edit ? false : true);
279  $form->element('password')->setDescription(__('The password should be at least eight characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ &','rmcommon'));
280  $form->addElement(new RMFormText(__('Repeat Password','rmcommon'), 'passwordc', 50, 50, '', true), $edit ? false : true);
281  $form->element('passwordc')->setDescription(__('Type password again.','rmcommon'));
282 
283  // Web
284  $form->addElement(new RMFormText(__('URL (Blog or personal website)','rmcommon'), 'url', 50, 250, $edit ? $user->getVar('url') : ''));
285 
286  // Timezone
287  $form->addElement(new RMFormTimeZoneField(__('Time zone','rmcommon'), 'timezone', 0, 0, $edit ? $user->getVar('timezone_offset') : ''));
288 
289  // Groups
290  $form->addElement(new RMFormGroups(__('Assign to groups','rmcommon'), 'groups', 1, 1, 3, $edit ? $user->groups() : ''));
291 
292  // Other options by API
293  $form = RMEvents::get()->run_event('rmcommon.user.form', $form, $edit, isset($user) ? $user : null);
294 
295  // Action
296  $form->addElement(new RMFormHidden('action',$edit ? 'saveedit' : 'save'));
297  if ($edit)
298  $form->addElement(new RMFormHidden('uid',$user->uid()));
299 
300  // Submit and cancel buttons
301  $ele = new RMFormButtonGroup('');
302  $ele->addButton('sbt', __($edit ? 'Edit User' : 'Add user','rmcommon'), 'submit');
303  $ele->addButton('cancel', __('Cancel','global'), 'button', 'onclick="history.go(-1);"');
304 
305  $form->addElement($ele);
306 
307  $form->display();
308 
309  xoops_cp_footer();
310 }
311 
317 function save_data($edit = false){
318  global $xoopsSecurity;
319 
320  $q = ''; // Query String
321  foreach ($_POST as $k => $v){
322  $$k = $v;
323  if ($k=='XOOPS_TOKEN_REQUEST' || $k=='sbt' || $k=='action' || $k=='password' || $k=='passwordc') continue;
324  $q .= $q=='' ? "$k=".urlencode($v) : "&$k=".urlencode($v);
325  }
326 
327  if (!$xoopsSecurity->check()){
328  redirectMsg('users.php?action='.($edit ? 'edit' : 'new').'&'.$q, __('Sorry, you don\'t have permission to add users.','rmcommon'), 1);
329  die();
330  }
331 
332  if ($edit){
333  if ($uid<=0){
334  redirectMsg('users.php', __('The specified user is not valid!','rmcommon'), 1);
335  die();
336  }
337 
338  $user = new RMUser($uid);
339  if ($user->isNew()){
340  redirectMsg('users.php', __('The specified user does not exists!','rmcommon'), 1);
341  die();
342  }
343  } else {
344  $user = new RMUser();
345  }
346 
347  // Check uname, password and passwordc
348  if ($uname=='' || $email=='' || (!$edit && ($password=='' || $passwordc==''))){
349  redirectMsg('users.php?action='.($edit ? 'edit' : 'new').'&'.$q, __('Please fill all required fields and try again!','rmcommon'), 1);
350  die();
351  }
352 
353  // Check passwords
354  if ($password!=$passwordc){
355  redirectMsg('users.php?action='.($edit ? 'edit' : 'new').'&'.$q, __('Passwords doesn\'t match. Please chek them.','rmcommon'), 1);
356  die();
357  }
358 
359  // Save user data
360  $user->setVar('name', $name);
361  $user->setVar('uname', $uname);
362  $user->setVar('display_name', $display_name);
363  $user->setVar('email', $email);
364  if (!$edit) $user->assignVar('user_regdate', time());
365  if ($password!='') $user->assignVar('pass', md5($password));
366  $user->setVar('level', 1);
367  $user->setVar('timezone_offset', $timezone);
368  $user->setVar('url', $url);
369  $user->setGroups($groups);
370 
371  // Plugins and modules can save metadata.
372  // Metadata are generated by other dynamical fields
373  $user = RMEvents::get()->run_event('rmcommon.add.usermeta.4save', $user);
374 
375  if ($user->save()){
376  $user = RMEvents::get()->run_event($edit ? 'rmcommon.user.edited' : 'rmcommon.user.created', $user);
377  redirectMsg('users.php', __('Database updated successfully!','rmcommon'), 0);
378  } else {
379  redirectMsg('users.php?action='.($edit ? 'edit' : 'new').'&'.$q, __('The users could not be saved. Please try again!','rmcommon').'<br />'.$user->errors(), 1);
380  }
381 
382 }
383 
387 function show_mailer(){
388  global $xoopsConfig, $rmc_config;
389 
390  $uid = rmc_server_var($_GET, 'uid', array());
391  $query = rmc_server_var($_GET, 'query', '');
392 
393  if (!is_array($uid) && $uid<=0 || empty($uid)){
394  // In admin control panel (side) add_message always must to be called before
395  // ExmGUI::show_header()
396  RMTemplate::get()->add_message(__('You must select one user at least. Please click on "Add Users" and select as many users as you wish.'), 0);
397  }
398 
399  $uid = !is_array($uid) ? array($uid) : $uid;
400 
401  xoops_cp_location(__('Sending email to users','rmcommon'));
402  xoops_cp_header();
403 
404  $form = new RMForm(__('Send Email to Users','rmcommon'), 'frm_mailer', 'users.php');
405 
406  $form->addElement(new RMFormUser(__('Users','global'), 'mailer_users', 1, $uid, 30, 600, 400));
407  $form->element('mailer_users')->setDescription(__('Please note that the maximun users number that you can select depends of the limit of emails that you can send accourding to your email server policies (or hosting account policies).','rmcommon'));
408 
409  $form->addElement(new RMFormText(__('Message subject','rmcommon'), 'subject', 50, 255), true);
410  $form->element('subject')->setDescription(__('Subject must be descriptive.','rmcommon'));
411  $form->addElement(new RMFormRadio(__('Message type','rmcommon'), 'type', ' ', 1, 2));
412  $form->element('type')->addOption(__('HTML','global'), 'html', 1, $rmc_config['editor_type']=='tiny' ? 'onclick="switchEditors.go(\'message\', \'tinymce\');"' : '');
413  $form->element('type')->addOption(__('Plain Text','global'), 'text', 0, $rmc_config['editor_type']=='tiny' ? 'onclick="switchEditors.go(\'message\', \'html\');"': '');
414  $form->addElement(new RMFormEditor(__('Message content','rmcommon'), 'message', '99%', '300px', ''), true);
415 
416  $ele = new RMFormButtonGroup();
417  $ele->addButton('sbt', __('Send E-Mail','rmcommon'), 'submit');
418  $ele->addButton('cancel', __('Cancel','rmcommon'), 'button', 'onclick="history.go(-1);"');
419  $form->addElement($ele);
420 
421  $form->addElement(new RMFormHidden('action','sendmail'));
422  $form->addElement(new RMFormHidden('query',$query));
423 
424  $form->display();
425 
426  xoops_cp_footer();
427 }
428 
432 function send_mail(){
433  global $rmc_config, $xoopsConfig;
434 
435  extract($_POST);
436  // Creating a message
437  $mailer = new RMMailer($type=='html' ? 'text/html' : 'text/plain');
438 
439  $mailer->add_xoops_users($mailer_users);
440  $mailer->set_subject($subject);
441 
442  $message = $type=='html' ? TextCleaner::getInstance()->to_display($message) : $message;
443 
444  $mailer->set_body($message);
445 
446  if (!$mailer->batchSend()){
447  xoops_cp_header();
448  echo "<h3>".__('There was errors while sending this emails','rmcommon')."</h3>";
449  foreach ($mailer->errors() as $error){
450  echo "<div class='even'>".$error."</div>";
451  }
452  xoops_cp_footer();
453  }
454 
455  redirectMsg('users.php?'.base64_decode($query), __('Message sent successfully!','rmcommon'), 0);
456 
457 }
458 
462 function activate_users($activate){
463  global $xoopsSecurity;
464 
465  foreach($_GET as $k => $v){
466  if ($k=='XOOPS_TOKEN_REQUEST' || $k=='action') continue;
467  $q .= $q=='' ? "$k=".urlencode($v) : "&$k=".urlencode($v);
468  }
469 
470  $uid = rmc_server_var($_POST, 'ids', array());
471 
472  if (empty($uid))
473  redirectMsg('users.php?'.$q, __('No users has been selected','rmcommon'), 1);
474 
475  $in = '';
476  foreach($uid as $id){
477  $in .= $in=='' ? $id : ','.$id;
478  }
479 
480  $db = XoopsDatabaseFactory::getDatabaseConnection();
481  $sql = "UPDATE ".$db->prefix("users")." SET level='$activate' WHERE uid IN($in)";
482 
483  if ($db->queryF($sql)){
484  redirectMsg('users.php?'.$q, __('Users '.($activate ? 'activated' : 'deactivated').' successfully!','rmcommon'), 0);
485  } else {
486  redirectMsg('users.php?'.$q, __('Users could not be '.($activate ? 'activated' : 'deactivated').'!','rmcommon'), 1);
487  }
488 
489 }
490 
491 function delete_users(){
492  global $xoopsSecurity;
493 
494  if(!$xoopsSecurity->check()){
495  redirectMsg("users.php", implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()), 1);
496  die();
497  }
498 
499  foreach($_GET as $k => $v){
500  if ($k=='XOOPS_TOKEN_REQUEST' || $k=='action') continue;
501  $q .= $q=='' ? "$k=".urlencode($v) : "&$k=".urlencode($v);
502  }
503 
504  $uid = rmc_server_var($_POST, 'ids', array());
505  $member_handler =& xoops_gethandler('member', 'system');
506 
507  foreach($uid as $id){
508 
509  $user =& $member_handler->getUser($id);
510  $groups = $user->getGroups();
511 
512  if (in_array(XOOPS_GROUP_ADMIN, $groups)) {
513  xoops_error( sprintf( __('Admin user cannot be deleted: %s','rmcommon'), $user->getVar("uname").'<br />') );
514  } elseif (!$member_handler->deleteUser($user)) {
515  xoops_error( sprintf( __('User cannot be deleted: %s','rmcommon'), $user->getVar("uname").'<br />') );
516  } else {
517  $online_handler =& xoops_gethandler('online');
518  $online_handler->destroy($uid);
519  // RMV-NOTIFY
520  xoops_notification_deletebyuser($uid);
521  }
522 
523  }
524 
525  redirectMsg("users.php?".$q,__('Users deleted successfully!','rmcommon'),0);
526 
527 
528 }
529 
530 
531 // get the action
532 $action = rmc_server_var($_REQUEST, 'action', '');
533 
534 switch($action){
535  case 'new':
536  user_form();
537  break;
538  case 'edit':
539  user_form(true);
540  break;
541  case 'save':
542  save_data();
543  break;
544  case 'saveedit':
545  save_data(true);
546  break;
547  case 'mailer':
548  show_mailer();
549  break;
550  case 'sendmail':
551  send_mail();
552  break;
553  case 'deactivate':
554  activate_users(0);
555  break;
556  case 'activate':
557  activate_users(1);
558  break;
559  case 'delete':
560  delete_users();
561  break;
562  default:
563  show_users();
564  break;
565 }