1<?php 2/** 3 * XOOPS Kernel Class 4 * 5 * You may not change or alter any portion of this comment or credits 6 * of supporting developers from this source code or any supporting source code 7 * which is considered copyrighted (c) material of the original comment or credit authors. 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) 13 * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html) 14 * @package kernel 15 * @since 2.0.0 16 * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/ 17 */ 18defined('XOOPS_ROOT_PATH') || exit('Restricted access'); 19 20/** 21 * a group of users 22 * 23 * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) 24 * @author Kazumi Ono <onokazu@xoops.org> 25 * @package kernel 26 */ 27class XoopsGroup extends XoopsObject 28{ 29 /** 30 * constructor 31 */ 32 public function __construct() 33 { 34 parent::__construct(); 35 $this->initVar('groupid', XOBJ_DTYPE_INT, null, false); 36 $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100); 37 $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false); 38 $this->initVar('group_type', XOBJ_DTYPE_OTHER, null, false); 39 } 40 41 /** 42 * Returns Class Base Variable groupid 43 * @param string $format 44 * @return mixed 45 */ 46 public function id($format = 'N') 47 { 48 return $this->getVar('groupid', $format); 49 } 50 51 /** 52 * Returns Class Base Variable groupid 53 * @param string $format 54 * @return mixed 55 */ 56 public function groupid($format = '') 57 { 58 return $this->getVar('groupid', $format); 59 } 60 61 /** 62 * Returns Class Base Variable name 63 * @param string $format 64 * @return mixed 65 */ 66 public function name($format = '') 67 { 68 return $this->getVar('name', $format); 69 } 70 71 /** 72 * Returns Class Base Variable description 73 * @param string $format 74 * @return mixed 75 */ 76 public function description($format = '') 77 { 78 return $this->getVar('description', $format); 79 } 80 81 /** 82 * Returns Class Base Variable group_type 83 * @param string $format 84 * @return mixed 85 */ 86 public function group_type($format = '') 87 { 88 return $this->getVar('group_type', $format); 89 } 90} 91 92/** 93 * XOOPS group handler class. 94 * This class is responsible for providing data access mechanisms to the data source 95 * of XOOPS group class objects. 96 * 97 * @author Kazumi Ono <onokazu@xoops.org> 98 * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) 99 * @package kernel 100 * @subpackage member 101 */ 102class XoopsGroupHandler extends XoopsObjectHandler 103{ 104 /** 105 * This should be here, since this really should be a XoopsPersistableObjectHandler 106 * Here, we fake it for future compatibility 107 * 108 * @var string table name 109 */ 110 public $table; 111 112 public function __construct(XoopsDatabase $db) 113 { 114 parent::__construct($db); 115 $this->table = $this->db->prefix('groups'); 116 } 117 118 /** 119 * create a new {@link XoopsGroup} object 120 * 121 * @param bool $isNew mark the new object as "new"? 122 * @return XoopsGroup XoopsGroup reference to the new object 123 * 124 */ 125 public function create($isNew = true) 126 { 127 $group = new XoopsGroup(); 128 if ($isNew) { 129 $group->setNew(); 130 } 131 132 return $group; 133 } 134 135 /** 136 * retrieve a specific group 137 * 138 * @param int $id ID of the group to get 139 * @return XoopsGroup XoopsGroup reference to the group object, FALSE if failed 140 */ 141 public function get($id) 142 { 143 $id = (int)$id; 144 $group = false; 145 if ($id > 0) { 146 $sql = 'SELECT * FROM ' . $this->db->prefix('groups') . ' WHERE groupid=' . $id; 147 if (!$result = $this->db->query($sql)) { 148 return $group; 149 } 150 $numrows = $this->db->getRowsNum($result); 151 if ($numrows == 1) { 152 $group = new XoopsGroup(); 153 $group->assignVars($this->db->fetchArray($result)); 154 } 155 } 156 157 return $group; 158 } 159 160 /** 161 * insert a group into the database 162 * 163 * @param XoopsObject|XoopsGroup $group a group object 164 * 165 * @return bool true on success, otherwise false 166 */ 167 public function insert(XoopsObject $group) 168 { 169 $className = 'XoopsGroup'; 170 if (!($group instanceof $className)) { 171 return false; 172 } 173 if (!$group->isDirty()) { 174 return true; 175 } 176 if (!$group->cleanVars()) { 177 return false; 178 } 179 foreach ($group->cleanVars as $k => $v) { 180 ${$k} = $v; 181 } 182 if ($group->isNew()) { 183 $groupid = $this->db->genId('group_groupid_seq'); 184 $sql = sprintf('INSERT INTO %s (groupid, name, description, group_type) VALUES (%u, %s, %s, %s)', $this->db->prefix('groups'), $groupid, $this->db->quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type)); 185 } else { 186 $sql = sprintf('UPDATE %s SET name = %s, description = %s, group_type = %s WHERE groupid = %u', $this->db->prefix('groups'), $this->db->quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type), $groupid); 187 } 188 if (!$result = $this->db->query($sql)) { 189 return false; 190 } 191 if (empty($groupid)) { 192 $groupid = $this->db->getInsertId(); 193 } 194 $group->assignVar('groupid', $groupid); 195 196 return true; 197 } 198 199 /** 200 * remove a group from the database 201 * 202 * @param XoopsObject|XoopsGroup $group a group object 203 * 204 * @return bool true on success, otherwise false 205 */ 206 public function delete(XoopsObject $group) 207 { 208 $className = 'XoopsGroup'; 209 if (!($group instanceof $className)) { 210 return false; 211 } 212 $sql = sprintf('DELETE FROM %s WHERE groupid = %u', $this->db->prefix('groups'), $group->getVar('groupid')); 213 if (!$result = $this->db->query($sql)) { 214 return false; 215 } 216 217 return true; 218 } 219 220 /** 221 * retrieve groups from the database 222 * 223 * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} with conditions for the groups 224 * @param bool $id_as_key should the groups' IDs be used as keys for the associative array? 225 * @return mixed Array of groups 226 */ 227 public function getObjects(CriteriaElement $criteria = null, $id_as_key = false) 228 { 229 $ret = array(); 230 $limit = $start = 0; 231 $sql = 'SELECT * FROM ' . $this->db->prefix('groups'); 232 if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { 233 $sql .= ' ' . $criteria->renderWhere(); 234 $limit = $criteria->getLimit(); 235 $start = $criteria->getStart(); 236 } 237 $result = $this->db->query($sql, $limit, $start); 238 if (!$result) { 239 return $ret; 240 } 241 while (false !== ($myrow = $this->db->fetchArray($result))) { 242 $group = new XoopsGroup(); 243 $group->assignVars($myrow); 244 if (!$id_as_key) { 245 $ret[] =& $group; 246 } else { 247 $ret[$myrow['groupid']] = &$group; 248 } 249 unset($group); 250 } 251 252 return $ret; 253 } 254} 255 256/** 257 * membership of a user in a group 258 * 259 * @author Kazumi Ono <onokazu@xoops.org> 260 * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) 261 * @package kernel 262 */ 263class XoopsMembership extends XoopsObject 264{ 265 /** 266 * constructor 267 */ 268 public function __construct() 269 { 270 parent::__construct(); 271 $this->initVar('linkid', XOBJ_DTYPE_INT, null, false); 272 $this->initVar('groupid', XOBJ_DTYPE_INT, null, false); 273 $this->initVar('uid', XOBJ_DTYPE_INT, null, false); 274 } 275} 276 277/** 278 * XOOPS membership handler class. (Singleton) 279 * 280 * This class is responsible for providing data access mechanisms to the data source 281 * of XOOPS group membership class objects. 282 * 283 * @author Kazumi Ono <onokazu@xoops.org> 284 * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) 285 * @package kernel 286 */ 287class XoopsMembershipHandler extends XoopsObjectHandler 288{ 289 /** 290 * This should be here, since this really should be a XoopsPersistableObjectHandler 291 * Here, we fake it for future compatibility 292 * 293 * @var string table name 294 */ 295 public $table; 296 297 public function __construct(XoopsDatabase $db) 298 { 299 parent::__construct($db); 300 $this->table = $this->db->prefix('groups_users_link'); 301 } 302 303 /** 304 * create a new membership 305 * 306 * @param bool $isNew should the new object be set to "new"? 307 * @return XoopsMembership XoopsMembership 308 */ 309 public function create($isNew = true) 310 { 311 $mship = new XoopsMembership(); 312 if ($isNew) { 313 $mship->setNew(); 314 } 315 316 return $mship; 317 } 318 319 /** 320 * retrieve a membership 321 * 322 * @param int $id ID of the membership to get 323 * @return mixed reference to the object if successful, else FALSE 324 */ 325 public function get($id) 326 { 327 $id = (int)$id; 328 $mship = false; 329 if ($id > 0) { 330 $sql = 'SELECT * FROM ' . $this->db->prefix('groups_users_link') . ' WHERE linkid=' . $id; 331 if (!$result = $this->db->query($sql)) { 332 return $mship; 333 } 334 $numrows = $this->db->getRowsNum($result); 335 if ($numrows == 1) { 336 $mship = new XoopsMembership(); 337 $mship->assignVars($this->db->fetchArray($result)); 338 } 339 } 340 341 return $mship; 342 } 343 344 /** 345 * inserts a membership in the database 346 * 347 * @param XoopsObject|XoopsMembership $mship a XoopsMembership object 348 * 349 * @return bool true on success, otherwise false 350 */ 351 public function insert(XoopsObject $mship) 352 { 353 $className = 'XoopsMembership'; 354 if (!($mship instanceof $className)) { 355 return false; 356 } 357 if (!$mship->isDirty()) { 358 return true; 359 } 360 if (!$mship->cleanVars()) { 361 return false; 362 } 363 foreach ($mship->cleanVars as $k => $v) { 364 ${$k} = $v; 365 } 366 if ($mship->isNew()) { 367 $linkid = $this->db->genId('groups_users_link_linkid_seq'); 368 $sql = sprintf('INSERT INTO %s (linkid, groupid, uid) VALUES (%u, %u, %u)', $this->db->prefix('groups_users_link'), $linkid, $groupid, $uid); 369 } else { 370 $sql = sprintf('UPDATE %s SET groupid = %u, uid = %u WHERE linkid = %u', $this->db->prefix('groups_users_link'), $groupid, $uid, $linkid); 371 } 372 if (!$result = $this->db->query($sql)) { 373 return false; 374 } 375 if (empty($linkid)) { 376 $linkid = $this->db->getInsertId(); 377 } 378 $mship->assignVar('linkid', $linkid); 379 380 return true; 381 } 382 383 /** 384 * delete a membership from the database 385 * 386 * @param XoopsObject|XoopsMembership $mship a XoopsMembership object 387 * 388 * @return bool true on success, otherwise false 389 */ 390 public function delete(XoopsObject $mship) 391 { 392 $className = 'XoopsMembership'; 393 if (!($mship instanceof $className)) { 394 return false; 395 } 396 397 $sql = sprintf('DELETE FROM %s WHERE linkid = %u', $this->db->prefix('groups_users_link'), $groupm->getVar('linkid')); 398 if (!$result = $this->db->query($sql)) { 399 return false; 400 } 401 402 return true; 403 } 404 405 /** 406 * retrieve memberships from the database 407 * 408 * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} conditions to meet 409 * @param bool $id_as_key should the ID be used as the array's key? 410 * @return array array of references 411 */ 412 public function getObjects(CriteriaElement $criteria = null, $id_as_key = false) 413 { 414 $ret = array(); 415 $limit = $start = 0; 416 $sql = 'SELECT * FROM ' . $this->db->prefix('groups_users_link'); 417 if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { 418 $sql .= ' ' . $criteria->renderWhere(); 419 $limit = $criteria->getLimit(); 420 $start = $criteria->getStart(); 421 } 422 $result = $this->db->query($sql, $limit, $start); 423 if (!$result) { 424 return $ret; 425 } 426 while (false !== ($myrow = $this->db->fetchArray($result))) { 427 $mship = new XoopsMembership(); 428 $mship->assignVars($myrow); 429 if (!$id_as_key) { 430 $ret[] =& $mship; 431 } else { 432 $ret[$myrow['linkid']] = &$mship; 433 } 434 unset($mship); 435 } 436 437 return $ret; 438 } 439 440 /** 441 * count how many memberships meet the conditions 442 * 443 * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} conditions to meet 444 * @return int 445 */ 446 public function getCount(CriteriaElement $criteria = null) 447 { 448 $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('groups_users_link'); 449 if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { 450 $sql .= ' ' . $criteria->renderWhere(); 451 } 452 $result = $this->db->query($sql); 453 if (!$result) { 454 return 0; 455 } 456 list($count) = $this->db->fetchRow($result); 457 458 return $count; 459 } 460 461 /** 462 * delete all memberships meeting the conditions 463 * 464 * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} with conditions to meet 465 * @return bool 466 */ 467 public function deleteAll(CriteriaElement $criteria = null) 468 { 469 $sql = 'DELETE FROM ' . $this->db->prefix('groups_users_link'); 470 if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { 471 $sql .= ' ' . $criteria->renderWhere(); 472 } 473 if (!$result = $this->db->query($sql)) { 474 return false; 475 } 476 477 return true; 478 } 479 480 /** 481 * retrieve groups for a user 482 * 483 * @param int $uid ID of the user 484 * 485 * @internal param bool $asobject should the groups be returned as {@link XoopsGroup} 486 * objects? FALSE returns associative array. 487 * @return array array of groups the user belongs to 488 */ 489 public function getGroupsByUser($uid) 490 { 491 $ret = array(); 492 $sql = 'SELECT groupid FROM ' . $this->db->prefix('groups_users_link') . ' WHERE uid=' . (int)$uid; 493 $result = $this->db->query($sql); 494 if (!$result) { 495 return $ret; 496 } 497 while (false !== ($myrow = $this->db->fetchArray($result))) { 498 $ret[] = $myrow['groupid']; 499 } 500 501 return $ret; 502 } 503 504 /** 505 * retrieve users belonging to a group 506 * 507 * @param int $groupid ID of the group 508 * @param int $limit number of entries to return 509 * @param int $start offset of first entry to return 510 * @internal param bool $asobject return users as {@link XoopsUser} objects? objects? 511 * FALSE will return arrays 512 * @return array array of users belonging to the group 513 */ 514 public function getUsersByGroup($groupid, $limit = 0, $start = 0) 515 { 516 $ret = array(); 517 $sql = 'SELECT uid FROM ' . $this->db->prefix('groups_users_link') . ' WHERE groupid=' . (int)$groupid; 518 $result = $this->db->query($sql, $limit, $start); 519 if (!$result) { 520 return $ret; 521 } 522 while (false !== ($myrow = $this->db->fetchArray($result))) { 523 $ret[] = $myrow['uid']; 524 } 525 526 return $ret; 527 } 528} 529