XOOPS  2.6.0
member.php
Go to the documentation of this file.
1 <?php
24 
35 {
36 
42  private $_gHandler;
43 
49  private $_uHandler;
50 
56  private $_mHandler;
57 
61  private $_members = array();
62 
70  public function __construct(Connection $db = null)
71  {
72  $this->_gHandler = Xoops::getInstance()->getHandlerGroup();
73  $this->_uHandler = Xoops::getInstance()->getHandlerUser();
74  $this->_mHandler = Xoops::getInstance()->getHandlerMembership();
75  }
76 
82  public function createGroup()
83  {
84  $inst = $this->_gHandler->create();
85  return $inst;
86  }
87 
93  public function createUser()
94  {
95  $inst = $this->_uHandler->create();
96  return $inst;
97  }
98 
106  public function getGroup($id)
107  {
108  return $this->_gHandler->get($id);
109  }
110 
118  public function getUser($id)
119  {
120  if (!isset($this->_members[$id])) {
121  $this->_members[$id] = $this->_uHandler->get($id);
122  }
123  return $this->_members[$id];
124  }
125 
133  public function deleteGroup(XoopsGroup &$group)
134  {
135  $this->_gHandler->delete($group);
136  $this->_mHandler->deleteAll(new Criteria('groupid', $group->getVar('groupid')));
137  return true;
138  }
139 
147  public function deleteUser(XoopsUser &$user)
148  {
149  $this->_uHandler->delete($user);
150  $this->_mHandler->deleteAll(new Criteria('uid', $user->getVar('uid')));
151  return true;
152  }
153 
162  public function insertGroup(XoopsGroup &$group)
163  {
164  return $this->_gHandler->insert($group);
165  }
166 
176  public function insertUser(XoopsUser &$user, $force = false)
177  {
178  return $this->_uHandler->insert($user, $force);
179  }
180 
189  public function getGroups(CriteriaElement $criteria = null, $id_as_key = false)
190  {
191  return $this->_gHandler->getObjects($criteria, $id_as_key);
192  }
193 
202  public function getUsers(CriteriaElement $criteria = null, $id_as_key = false)
203  {
204  return $this->_uHandler->getObjects($criteria, $id_as_key);
205  }
206 
214  public function getGroupList(CriteriaElement $criteria = null)
215  {
216  $groups = $this->_gHandler->getObjects($criteria, true);
217  $ret = array();
218  foreach (array_keys($groups) as $i) {
219  $ret[$i] = $groups[$i]->getVar('name');
220  }
221  return $ret;
222  }
223 
231  public function getUserList(CriteriaElement $criteria = null)
232  {
233  $users = $this->_uHandler->getObjects($criteria, true);
234  $ret = array();
235  foreach (array_keys($users) as $i) {
236  $ret[$i] = $users[$i]->getVar('uname');
237  }
238  return $ret;
239  }
240 
249  public function addUserToGroup($group_id, $user_id)
250  {
251  $mship = $this->_mHandler->create();
252  $mship->setVar('groupid', $group_id);
253  $mship->setVar('uid', $user_id);
254  return $this->_mHandler->insert($mship);
255  }
256 
265  public function removeUsersFromGroup($group_id, $user_ids = array())
266  {
267  $criteria = new CriteriaCompo();
268  $criteria->add(new Criteria('groupid', $group_id));
269  $criteria2 = new CriteriaCompo();
270  foreach ($user_ids as $uid) {
271  $criteria2->add(new Criteria('uid', $uid), 'OR');
272  }
273  $criteria->add($criteria2);
274  return $this->_mHandler->deleteAll($criteria);
275  }
276 
288  public function getUsersByGroup($group_id, $asobject = false, $limit = 0, $start = 0)
289  {
290  $user_ids = $this->_mHandler->getUsersByGroup($group_id, $limit, $start);
291  if (!$asobject) {
292  return $user_ids;
293  } else {
294  $ret = array();
295  foreach ($user_ids as $u_id) {
296  $user = $this->getUser($u_id);
297  if (is_object($user)) {
298  $ret[] = $user;
299  }
300  unset($user);
301  }
302  return $ret;
303  }
304  }
305 
314  public function getGroupsByUser($user_id, $asobject = false)
315  {
316  $ret = array();
317  $group_ids = $this->_mHandler->getGroupsByUser($user_id);
318  if (!$asobject) {
319  return $group_ids;
320  } else {
321  foreach ($group_ids as $g_id) {
322  $ret[] = $this->getGroup($g_id);
323  }
324  return $ret;
325  }
326  }
327 
339  public function loginUser($uname, $pwd)
340  {
341  $criteria = new Criteria('uname', $uname);
342  //$criteria->add(new Criteria('pass', md5($pwd)));
343  $user = $this->_uHandler->getObjects($criteria, false);
344  if (!$user || count($user) != 1) {
345  return false;
346  }
347  $rehash = false;
348  $hash = $user[0]->pass();
349  $type = substr($user[0]->pass(), 0, 1);
350  // see if we have a crypt like signature, old md5 hash is just hex digits
351  if ($type=='$') {
352  if (!password_verify($pwd, $hash)) {
353  return false;
354  }
355  // check if hash uses the best algorithm (i.e. after a PHP upgrade)
356  $rehash = password_needs_rehash($hash, PASSWORD_DEFAULT);
357  } else {
358  if ($hash!=md5($pwd)) {
359  return false;
360  }
361  $rehash = true; // automatically update old style
362  }
363  // hash used an old algorithm, so make it stronger
364  if ($rehash) {
365  $user[0]->setVar('pass', password_hash($pwd, PASSWORD_DEFAULT));
366  $this->_uHandler->insert($user[0]);
367  }
368  return $user[0];
369  }
370 
381  public function loginUserMd5($uname, $md5pwd)
382  {
383  $criteria = new CriteriaCompo(new Criteria('uname', $uname));
384  $criteria->add(new Criteria('pass', $md5pwd));
385  $user = $this->_uHandler->getObjects($criteria, false);
386  if (!$user || count($user) != 1) {
387  $user = false;
388  return $user;
389  }
390  return $user[0];
391  }
392 
400  public function getUserCount(CriteriaElement $criteria = null)
401  {
402  return $this->_uHandler->getCount($criteria);
403  }
404 
412  public function getUserCountByGroup($group_id)
413  {
414  return $this->_mHandler->getCount(new Criteria('groupid', $group_id));
415  }
416 
426  public function updateUserByField(XoopsUser &$user, $fieldName, $fieldValue)
427  {
428  $user->setVar($fieldName, $fieldValue);
429  return $this->insertUser($user);
430  }
431 
441  public function updateUsersByField($fieldName, $fieldValue, CriteriaElement $criteria = null)
442  {
443  if (is_null($criteria)) {
444  $criteria = new Criteria(''); // empty criteria resolves to 'WHERE (1)'
445  }
446  return $this->_uHandler->updateAll($fieldName, $fieldValue, $criteria);
447  }
448 
456  public function activateUser(XoopsUser &$user)
457  {
458  if ($user->getVar('level') != 0) {
459  return true;
460  }
461  $user->setVar('level', 1);
462  return $this->_uHandler->insert($user, true);
463  }
464 
477  public function getUsersByGroupLink(
478  $groups,
479  CriteriaElement $criteria = null,
480  $asobject = false,
481  $id_as_key = false
482  ) {
483 
484  $qb = $this->_uHandler->db2->createXoopsQueryBuilder();
485  $eb = $qb->expr();
486 
487  $qb ->select('DISTINCT ' . ($asobject ? 'u.*' : 'u.uid'))
488  ->fromPrefix('users', 'u')
489  ->leftJoinPrefix('u', 'groups_users_link', 'm', 'm.uid = u.uid');
490 
491  $where = false;
492  if (!empty($groups)) {
493  $qb->where($eb->in('m.groupid', $groups));
494  $where = true;
495  }
496  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
497  $whereMode = $where ? 'AND' : '';
498  $sql[] = $criteria->renderQb($qb, $whereMode);
499  }
500 
501  $ret = array();
502 
503  if (!$result = $qb->execute()) {
504  return $ret;
505  }
506 
507  while ($myrow = $result->fetch(PDO::FETCH_ASSOC)) {
508  if ($asobject) {
509  $user = new XoopsUser();
510  $user->assignVars($myrow);
511  if (!$id_as_key) {
512  $ret[] = $user;
513  } else {
514  $ret[$myrow['uid']] = $user;
515  }
516  unset($user);
517  } else {
518  $ret[] = $myrow['uid'];
519  }
520  }
521  return $ret;
522  }
523 
533  public function getUserCountByGroupLink($groups, $criteria = null)
534  {
535  $qb = $this->_uHandler->db2->createXoopsQueryBuilder();
536  $eb = $qb->expr();
537 
538  $qb ->select('COUNT(DISTINCT u.uid)')
539  ->fromPrefix('users', 'u')
540  ->leftJoinPrefix('u', 'groups_users_link', 'm', 'm.uid = u.uid');
541 
542  $where = false;
543  if (!empty($groups)) {
544  $qb->where($eb->in('m.groupid', $groups));
545  $where = true;
546  }
547  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
548  $whereMode = $where ? 'AND' : '';
549  $criteria->renderQb($qb, $whereMode);
550  }
551 
552  $result = $qb->execute();
553  $ret = $result->fetchColumn(0);
554 
555  return $ret;
556  }
557 }
getUserList(CriteriaElement $criteria=null)
Definition: member.php:231
getUserCountByGroupLink($groups, $criteria=null)
Definition: member.php:533
getGroupsByUser($user_id, $asobject=false)
Definition: member.php:314
deleteGroup(XoopsGroup &$group)
Definition: member.php:133
getGroups(CriteriaElement $criteria=null, $id_as_key=false)
Definition: member.php:189
$i
Definition: dialog.php:68
static getInstance()
Definition: Xoops.php:160
insertUser(XoopsUser &$user, $force=false)
Definition: member.php:176
loginUser($uname, $pwd)
Definition: member.php:339
removeUsersFromGroup($group_id, $user_ids=array())
Definition: member.php:265
activateUser(XoopsUser &$user)
Definition: member.php:456
$result
Definition: pda.php:33
$user
Definition: checklogin.php:47
loginUserMd5($uname, $md5pwd)
Definition: member.php:381
addUserToGroup($group_id, $user_id)
Definition: member.php:249
insertGroup(XoopsGroup &$group)
Definition: member.php:162
getVar($key, $format= 's')
updateUsersByField($fieldName, $fieldValue, CriteriaElement $criteria=null)
Definition: member.php:441
getUserCount(CriteriaElement $criteria=null)
Definition: member.php:400
getUsersByGroupLink($groups, CriteriaElement $criteria=null, $asobject=false, $id_as_key=false)
Definition: member.php:477
$id
Definition: admin_menu.php:36
getUsersByGroup($group_id, $asobject=false, $limit=0, $start=0)
Definition: member.php:288
getUsers(CriteriaElement $criteria=null, $id_as_key=false)
Definition: member.php:202
if(!$xoops->isUser()) $uid
Definition: index.php:31
deleteUser(XoopsUser &$user)
Definition: member.php:147
$criteria2
$sql
Definition: pda.php:32
$type
Definition: misc.php:33
$groups
$limit
Definition: findusers.php:202
__construct(Connection $db=null)
Definition: member.php:70
$uname
Definition: checklogin.php:37
$criteria
getGroupList(CriteriaElement $criteria=null)
Definition: member.php:214
updateUserByField(XoopsUser &$user, $fieldName, $fieldValue)
Definition: member.php:426
$start
setVar($key, $value, $not_gpc=false)
getUserCountByGroup($group_id)
Definition: member.php:412
$user_id
Definition: update.php:57