PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_users/admin.users.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 816 lines | 521 code | 114 blank | 181 comment | 144 complexity | 754af9a4ce83480f29f91abe0055a7f2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: admin.users.php 6080 2006-12-21 06:48:26Z pasamio $
  4. * @package Joomla
  5. * @subpackage Users
  6. * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // no direct access
  15. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  16. if (!$acl->acl_check( 'administration', 'manage', 'users', $my->usertype, 'components', 'com_users' )) {
  17. mosRedirect( 'index2.php', _NOT_AUTH );
  18. }
  19. require_once( $mainframe->getPath( 'admin_html' ) );
  20. require_once( $mainframe->getPath( 'class' ) );
  21. $cid = josGetArrayInts( 'cid' );
  22. switch ($task) {
  23. case 'new':
  24. editUser( 0, $option);
  25. break;
  26. case 'edit':
  27. editUser( intval( $cid[0] ), $option );
  28. break;
  29. case 'editA':
  30. editUser( $id, $option );
  31. break;
  32. case 'save':
  33. case 'apply':
  34. // check to see if functionality restricted for use as demo site
  35. if ( $_VERSION->RESTRICT == 1 ) {
  36. mosRedirect( 'index2.php?mosmsg=Functionality Restricted' );
  37. } else {
  38. saveUser( $task );
  39. }
  40. break;
  41. case 'remove':
  42. removeUsers( $cid, $option );
  43. break;
  44. case 'block':
  45. // check to see if functionality restricted for use as demo site
  46. if ( $_VERSION->RESTRICT == 1 ) {
  47. mosRedirect( 'index2.php?mosmsg=Functionality Restricted' );
  48. } else {
  49. changeUserBlock( $cid, 1, $option );
  50. }
  51. break;
  52. case 'unblock':
  53. changeUserBlock( $cid, 0, $option );
  54. break;
  55. case 'logout':
  56. logoutUser( $cid, $option, $task );
  57. break;
  58. case 'flogout':
  59. logoutUser( $id, $option, $task );
  60. break;
  61. case 'cancel':
  62. cancelUser( $option );
  63. break;
  64. case 'contact':
  65. $contact_id = mosGetParam( $_POST, 'contact_id', '' );
  66. mosRedirect( 'index2.php?option=com_contact&task=editA&id='. $contact_id );
  67. break;
  68. default:
  69. showUsers( $option );
  70. break;
  71. }
  72. function showUsers( $option ) {
  73. global $database, $mainframe, $my, $acl, $mosConfig_list_limit;
  74. $filter_type = $mainframe->getUserStateFromRequest( "filter_type{$option}", 'filter_type', 0 );
  75. $filter_logged = intval( $mainframe->getUserStateFromRequest( "filter_logged{$option}", 'filter_logged', 0 ) );
  76. $limit = intval( $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', $mosConfig_list_limit ) );
  77. $limitstart = intval( $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 ) );
  78. $search = $mainframe->getUserStateFromRequest( "search{$option}", 'search', '' );
  79. if (get_magic_quotes_gpc()) {
  80. $filter_type = stripslashes( $filter_type );
  81. $search = stripslashes( $search );
  82. }
  83. $where = array();
  84. if (isset( $search ) && $search!= "") {
  85. $searchEscaped = $database->getEscaped( trim( strtolower( $search ) ) );
  86. $where[] = "(a.username LIKE '%$searchEscaped%' OR a.email LIKE '%$searchEscaped%' OR a.name LIKE '%$searchEscaped%')";
  87. }
  88. if ( $filter_type ) {
  89. if ( $filter_type == 'Public Frontend' ) {
  90. $where[] = "(a.usertype = 'Registered' OR a.usertype = 'Author' OR a.usertype = 'Editor'OR a.usertype = 'Publisher')";
  91. } else if ( $filter_type == 'Public Backend' ) {
  92. $where[] = "(a.usertype = 'Manager' OR a.usertype = 'Administrator' OR a.usertype = 'Super Administrator')";
  93. } else {
  94. $where[] = "a.usertype = LOWER( " . $database->Quote( $filter_type ) . " )";
  95. }
  96. }
  97. if ( $filter_logged == 1 ) {
  98. $where[] = "s.userid = a.id";
  99. } else if ($filter_logged == 2) {
  100. $where[] = "s.userid IS NULL";
  101. }
  102. // exclude any child group id's for this user
  103. $pgids = $acl->get_group_children( $my->gid, 'ARO', 'RECURSE' );
  104. mosArrayToInts( $pgids );
  105. if (is_array( $pgids ) && count( $pgids ) > 0) {
  106. $where[] = '( a.gid != ' . implode( ' OR a.gid != ', $pgids ) . ' )';
  107. }
  108. $query = "SELECT COUNT(a.id)"
  109. . "\n FROM #__users AS a";
  110. if ($filter_logged == 1 || $filter_logged == 2) {
  111. $query .= "\n INNER JOIN #__session AS s ON s.userid = a.id";
  112. }
  113. $query .= ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : '' )
  114. ;
  115. $database->setQuery( $query );
  116. $total = $database->loadResult();
  117. require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
  118. $pageNav = new mosPageNav( $total, $limitstart, $limit );
  119. $query = "SELECT a.*, g.name AS groupname"
  120. . "\n FROM #__users AS a"
  121. . "\n INNER JOIN #__core_acl_aro AS aro ON aro.value = a.id" // map user to aro
  122. . "\n INNER JOIN #__core_acl_groups_aro_map AS gm ON gm.aro_id = aro.aro_id" // map aro to group
  123. . "\n INNER JOIN #__core_acl_aro_groups AS g ON g.group_id = gm.group_id";
  124. if ($filter_logged == 1 || $filter_logged == 2) {
  125. $query .= "\n INNER JOIN #__session AS s ON s.userid = a.id";
  126. }
  127. $query .= (count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "")
  128. . "\n GROUP BY a.id"
  129. ;
  130. $database->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
  131. $rows = $database->loadObjectList();
  132. if ($database->getErrorNum()) {
  133. echo $database->stderr();
  134. return false;
  135. }
  136. $template = 'SELECT COUNT(s.userid) FROM #__session AS s WHERE s.userid = ';
  137. $n = count( $rows );
  138. for ($i = 0; $i < $n; $i++) {
  139. $row = &$rows[$i];
  140. $query = $template . (int) $row->id;
  141. $database->setQuery( $query );
  142. $row->loggedin = $database->loadResult();
  143. }
  144. // get list of Groups for dropdown filter
  145. $query = "SELECT name AS value, name AS text"
  146. . "\n FROM #__core_acl_aro_groups"
  147. . "\n WHERE name != 'ROOT'"
  148. . "\n AND name != 'USERS'"
  149. ;
  150. $types[] = mosHTML::makeOption( '0', '- Select Group -' );
  151. $database->setQuery( $query );
  152. $types = array_merge( $types, $database->loadObjectList() );
  153. $lists['type'] = mosHTML::selectList( $types, 'filter_type', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', "$filter_type" );
  154. // get list of Log Status for dropdown filter
  155. $logged[] = mosHTML::makeOption( 0, '- Select Log Status - ');
  156. $logged[] = mosHTML::makeOption( 1, 'Logged In');
  157. $lists['logged'] = mosHTML::selectList( $logged, 'filter_logged', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', "$filter_logged" );
  158. HTML_users::showUsers( $rows, $pageNav, $search, $option, $lists );
  159. }
  160. /**
  161. * Edit the user
  162. * @param int The user ID
  163. * @param string The URL option
  164. */
  165. function editUser( $uid='0', $option='users' ) {
  166. global $database, $my, $acl, $mainframe;
  167. $msg = checkUserPermissions( array($uid), "edit", true );
  168. if ($msg) {
  169. echo "<script type=\"text/javascript\"> alert('".$msg."'); window.history.go(-1);</script>\n";
  170. exit;
  171. }
  172. $row = new mosUser( $database );
  173. // load the row from the db table
  174. $row->load( (int)$uid );
  175. if ( $uid ) {
  176. $query = "SELECT *"
  177. . "\n FROM #__contact_details"
  178. . "\n WHERE user_id = " . (int) $row->id
  179. ;
  180. $database->setQuery( $query );
  181. $contact = $database->loadObjectList();
  182. $row->name = trim( $row->name );
  183. $row->email = trim( $row->email );
  184. $row->username = trim( $row->username );
  185. $row->password = trim( $row->password );
  186. } else {
  187. $contact = NULL;
  188. $row->block = 0;
  189. }
  190. // check to ensure only super admins can edit super admin info
  191. if ( ( $my->gid < 25 ) && ( $row->gid == 25 ) ) {
  192. mosRedirect( 'index2.php?option=com_users', _NOT_AUTH );
  193. }
  194. $my_group = strtolower( $acl->get_group_name( $row->gid, 'ARO' ) );
  195. if ( $my_group == 'super administrator' && $my->gid != 25 ) {
  196. $lists['gid'] = '<input type="hidden" name="gid" value="'. $my->gid .'" /><strong>Super Administrator</strong>';
  197. } else if ( $my->gid == 24 && $row->gid == 24 ) {
  198. $lists['gid'] = '<input type="hidden" name="gid" value="'. $my->gid .'" /><strong>Administrator</strong>';
  199. } else {
  200. // ensure user can't add group higher than themselves
  201. $my_groups = $acl->get_object_groups( 'users', $my->id, 'ARO' );
  202. if (is_array( $my_groups ) && count( $my_groups ) > 0) {
  203. $ex_groups = $acl->get_group_children( $my_groups[0], 'ARO', 'RECURSE' );
  204. } else {
  205. $ex_groups = array();
  206. }
  207. $gtree = $acl->get_group_children_tree( null, 'USERS', false );
  208. // remove users 'above' me
  209. $i = 0;
  210. while ($i < count( $gtree )) {
  211. if (in_array( $gtree[$i]->value, $ex_groups )) {
  212. array_splice( $gtree, $i, 1 );
  213. } else {
  214. $i++;
  215. }
  216. }
  217. $lists['gid'] = mosHTML::selectList( $gtree, 'gid', 'size="10"', 'value', 'text', $row->gid );
  218. }
  219. // build the html select list
  220. $lists['block'] = mosHTML::yesnoRadioList( 'block', 'class="inputbox" size="1"', $row->block );
  221. // build the html select list
  222. $lists['sendEmail'] = mosHTML::yesnoRadioList( 'sendEmail', 'class="inputbox" size="1"', $row->sendEmail );
  223. $file = $mainframe->getPath( 'com_xml', 'com_users' );
  224. $params =& new mosUserParameters( $row->params, $file, 'component' );
  225. HTML_users::edituser( $row, $contact, $lists, $option, $uid, $params );
  226. }
  227. function saveUser( $task ) {
  228. global $database, $my, $acl;
  229. global $mosConfig_live_site, $mosConfig_mailfrom, $mosConfig_fromname, $mosConfig_sitename;
  230. josSpoofCheck();
  231. $userIdPosted = mosGetParam($_POST, 'id');
  232. if ($userIdPosted) {
  233. $msg = checkUserPermissions( array($userIdPosted), 'save', in_array($my->gid, array(24, 25)) );
  234. if ($msg) {
  235. echo "<script type=\"text/javascript\"> alert('".$msg."'); window.history.go(-1);</script>\n";
  236. exit;
  237. }
  238. }
  239. $row = new mosUser( $database );
  240. if (!$row->bind( $_POST )) {
  241. echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
  242. exit();
  243. }
  244. $row->name = trim( $row->name );
  245. $row->email = trim( $row->email );
  246. $row->username = trim( $row->username );
  247. // sanitise fields
  248. $row->id = (int) $row->id;
  249. // sanitise gid field
  250. $row->gid = (int) $row->gid;
  251. $isNew = !$row->id;
  252. $pwd = '';
  253. // MD5 hash convert passwords
  254. if ($isNew) {
  255. // new user stuff
  256. if ($row->password == '') {
  257. $pwd = mosMakePassword();
  258. $salt = mosMakePassword(16);
  259. $crypt = md5($pwd.$salt);
  260. $row->password = $crypt.':'.$salt;
  261. } else {
  262. $pwd = trim( $row->password );
  263. $salt = mosMakePassword(16);
  264. $crypt = md5($pwd.$salt);
  265. $row->password = $crypt.':'.$salt;
  266. }
  267. $row->registerDate = date( 'Y-m-d H:i:s' );
  268. } else {
  269. $original = new mosUser( $database );
  270. $original->load( (int)$row->id );
  271. // existing user stuff
  272. if ($row->password == '') {
  273. // password set to null if empty
  274. $row->password = null;
  275. } else {
  276. $row->password = trim($row->password);
  277. $salt = mosMakePassword(16);
  278. $crypt = md5($row->password.$salt);
  279. $row->password = $crypt.':'.$salt;
  280. }
  281. // if group has been changed and where original group was a Super Admin
  282. if ( $row->gid != $original->gid ) {
  283. if ( $original->gid == 25 ) {
  284. // count number of active super admins
  285. $query = "SELECT COUNT( id )"
  286. . "\n FROM #__users"
  287. . "\n WHERE gid = 25"
  288. . "\n AND block = 0"
  289. ;
  290. $database->setQuery( $query );
  291. $count = $database->loadResult();
  292. if ( $count <= 1 ) {
  293. // disallow change if only one Super Admin exists
  294. echo "<script> alert('You cannot change this users Group as it is the only active Super Administrator for your site'); window.history.go(-1); </script>\n";
  295. exit();
  296. }
  297. }
  298. $user_group = strtolower( $acl->get_group_name( $original->gid, 'ARO' ) );
  299. if (( $user_group == 'super administrator' && $my->gid != 25) ) {
  300. // disallow change of super-Admin by non-super admin
  301. echo "<script> alert('You cannot change this users Group as you are not a Super Administrator for your site'); window.history.go(-1); </script>\n";
  302. exit();
  303. } else if ( $my->gid == 24 && $original->gid == 24 ) {
  304. // disallow change of super-Admin by non-super admin
  305. echo "<script> alert('You cannot change the Group of another Administrator as you are not a Super Administrator for your site'); window.history.go(-1); </script>\n";
  306. exit();
  307. } // ensure user can't add group higher than themselves done below
  308. }
  309. }
  310. /*
  311. // if user is made a Super Admin group and user is NOT a Super Admin
  312. if ( $row->gid == 25 && $my->gid != 25 ) {
  313. // disallow creation of Super Admin by non Super Admin users
  314. echo "<script> alert('You cannot create a user with this user Group level, only Super Administrators have this ability'); window.history.go(-1); </script>\n";
  315. exit();
  316. }
  317. */
  318. // Security check to avoid creating/editing user to higher level than himself: response to artf4529.
  319. if (!in_array($row->gid,getGIDSChildren($my->gid))) {
  320. // disallow creation of Super Admin by non Super Admin users
  321. echo "<script> alert('You cannot create a user with this user Group level, only Super Administrators have this ability'); window.history.go(-1); </script>\n";
  322. exit();
  323. }
  324. // save usertype to usertype column
  325. $query = "SELECT name"
  326. . "\n FROM #__core_acl_aro_groups"
  327. . "\n WHERE group_id = " . (int) $row->gid
  328. ;
  329. $database->setQuery( $query );
  330. $usertype = $database->loadResult();
  331. $row->usertype = $usertype;
  332. // save params
  333. $params = mosGetParam( $_POST, 'params', '' );
  334. if (is_array( $params )) {
  335. $txt = array();
  336. foreach ( $params as $k=>$v) {
  337. $txt[] = "$k=$v";
  338. }
  339. $row->params = implode( "\n", $txt );
  340. }
  341. if (!$row->check()) {
  342. echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
  343. exit();
  344. }
  345. if (!$row->store()) {
  346. echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
  347. exit();
  348. }
  349. $row->checkin();
  350. // updates the current users param settings
  351. if ( $my->id == $row->id ) {
  352. //session_start();
  353. $_SESSION['session_user_params']= $row->params;
  354. session_write_close();
  355. }
  356. // update the ACL
  357. if (!$isNew) {
  358. $query = "SELECT aro_id"
  359. . "\n FROM #__core_acl_aro"
  360. . "\n WHERE value = " . (int) $row->id
  361. ;
  362. $database->setQuery( $query );
  363. $aro_id = $database->loadResult();
  364. $query = "UPDATE #__core_acl_groups_aro_map"
  365. . "\n SET group_id = " . (int) $row->gid
  366. . "\n WHERE aro_id = " . (int) $aro_id
  367. ;
  368. $database->setQuery( $query );
  369. $database->query() or die( $database->stderr() );
  370. }
  371. // for new users, email username and password
  372. if ($isNew) {
  373. $query = "SELECT email"
  374. . "\n FROM #__users"
  375. . "\n WHERE id = " . (int) $my->id
  376. ;
  377. $database->setQuery( $query );
  378. $adminEmail = $database->loadResult();
  379. $subject = _NEW_USER_MESSAGE_SUBJECT;
  380. $message = sprintf ( _NEW_USER_MESSAGE, $row->name, $mosConfig_sitename, $mosConfig_live_site, $row->username, $pwd );
  381. if ($mosConfig_mailfrom != "" && $mosConfig_fromname != "") {
  382. $adminName = $mosConfig_fromname;
  383. $adminEmail = $mosConfig_mailfrom;
  384. } else {
  385. $query = "SELECT name, email"
  386. . "\n FROM #__users"
  387. // administrator
  388. . "\n WHERE gid = 25"
  389. ;
  390. $database->setQuery( $query );
  391. $admins = $database->loadObjectList();
  392. $admin = $admins[0];
  393. $adminName = $admin->name;
  394. $adminEmail = $admin->email;
  395. }
  396. mosMail( $adminEmail, $adminName, $row->email, $subject, $message );
  397. }
  398. if (!$isNew) {
  399. // if group has been changed
  400. if ( $original->gid != $row->gid ) {
  401. // delete user acounts active sessions
  402. logoutUser( $row->id, 'com_users', 'change' );
  403. }
  404. }
  405. switch ( $task ) {
  406. case 'apply':
  407. $msg = 'Successfully Saved changes to User: '. $row->name;
  408. mosRedirect( 'index2.php?option=com_users&task=editA&hidemainmenu=1&id='. $row->id, $msg );
  409. break;
  410. case 'save':
  411. default:
  412. $msg = 'Successfully Saved User: '. $row->name;
  413. mosRedirect( 'index2.php?option=com_users', $msg );
  414. break;
  415. }
  416. }
  417. /**
  418. * Cancels an edit operation
  419. * @param option component option to call
  420. */
  421. function cancelUser( $option ) {
  422. mosRedirect( 'index2.php?option='. $option .'&task=view' );
  423. }
  424. function removeUsers( $cid, $option ) {
  425. global $database, $acl, $my;
  426. josSpoofCheck();
  427. if (!is_array( $cid ) || count( $cid ) < 1) {
  428. echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>\n";
  429. exit;
  430. }
  431. $msg = checkUserPermissions( $cid, 'delete' );
  432. if ( !$msg && count( $cid ) ) {
  433. $obj = new mosUser( $database );
  434. foreach ($cid as $id) {
  435. $obj->load( $id );
  436. $count = 2;
  437. if ( $obj->gid == 25 ) {
  438. // count number of active super admins
  439. $query = "SELECT COUNT( id )"
  440. . "\n FROM #__users"
  441. . "\n WHERE gid = 25"
  442. . "\n AND block = 0"
  443. ;
  444. $database->setQuery( $query );
  445. $count = $database->loadResult();
  446. }
  447. if ( $count <= 1 && $obj->gid == 25 ) {
  448. // cannot delete Super Admin where it is the only one that exists
  449. $msg = "You cannot delete this Super Administrator as it is the only active Super Administrator for your site";
  450. } else {
  451. // delete user
  452. $obj->delete( $id );
  453. $msg = $obj->getError();
  454. // delete user acounts active sessions
  455. logoutUser( $id, 'com_users', 'remove' );
  456. }
  457. }
  458. }
  459. mosRedirect( 'index2.php?option='. $option, $msg );
  460. }
  461. /*
  462. function removeUsers( $cid, $option ) {
  463. global $database, $acl, $my;
  464. if (!is_array( $cid ) || count( $cid ) < 1) {
  465. echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>\n";
  466. exit;
  467. }
  468. if ( count( $cid ) ) {
  469. $obj = new mosUser( $database );
  470. foreach ($cid as $id) {
  471. // check for a super admin ... can't delete them
  472. $groups = $acl->get_object_groups( 'users', $id, 'ARO' );
  473. $this_group = strtolower( $acl->get_group_name( $groups[0], 'ARO' ) );
  474. if ( $this_group == 'super administrator' && $my->gid != 25 ) {
  475. $msg = "You cannot delete a Super Administrator";
  476. } else if ( $id == $my->id ){
  477. $msg = "You cannot delete Yourself!";
  478. } else if ( ( $this_group == 'administrator' ) && ( $my->gid == 24 ) ){
  479. $msg = "You cannot delete another `Administrator` only `Super Administrators` have this power";
  480. } else {
  481. $obj->load( $id );
  482. $count = 2;
  483. if ( $obj->gid == 25 ) {
  484. // count number of active super admins
  485. $query = "SELECT COUNT( id )"
  486. . "\n FROM #__users"
  487. . "\n WHERE gid = 25"
  488. . "\n AND block = 0"
  489. ;
  490. $database->setQuery( $query );
  491. $count = $database->loadResult();
  492. }
  493. if ( $count <= 1 && $obj->gid == 25 ) {
  494. // cannot delete Super Admin where it is the only one that exists
  495. $msg = "You cannot delete this Super Administrator as it is the only active Super Administrator for your site";
  496. } else {
  497. // delete user
  498. $obj->delete( $id );
  499. $msg = $obj->getError();
  500. // delete user acounts active sessions
  501. logoutUser( $id, 'com_users', 'remove' );
  502. }
  503. }
  504. }
  505. }
  506. mosRedirect( 'index2.php?option='. $option, $msg );
  507. }
  508. */
  509. /**
  510. * Blocks or Unblocks one or more user records
  511. * @param array An array of unique category id numbers
  512. * @param integer 0 if unblock, 1 if blocking
  513. * @param string The current url option
  514. */
  515. function changeUserBlock( $cid=null, $block=1, $option ) {
  516. global $database;
  517. josSpoofCheck();
  518. $action = $block ? 'block' : 'unblock';
  519. if (count( $cid ) < 1) {
  520. echo "<script type=\"text/javascript\"> alert('Select an item to $action'); window.history.go(-1);</script>\n";
  521. exit;
  522. }
  523. $msg = checkUserPermissions( $cid, $action );
  524. if ($msg) {
  525. echo "<script type=\"text/javascript\"> alert('".$msg."'); window.history.go(-1);</script>\n";
  526. exit;
  527. }
  528. mosArrayToInts( $cid );
  529. $cids = 'id=' . implode( ' OR id=', $cid );
  530. $query = "UPDATE #__users"
  531. . "\n SET block = " . (int) $block
  532. . "\n WHERE ( $cids )"
  533. ;
  534. $database->setQuery( $query );
  535. if (!$database->query()) {
  536. echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
  537. exit();
  538. }
  539. // if action is to block a user
  540. if ( $block == 1 ) {
  541. foreach( $cid as $id ) {
  542. // delete user acounts active sessions
  543. logoutUser( $id, 'com_users', 'block' );
  544. }
  545. }
  546. mosRedirect( 'index2.php?option='. $option );
  547. }
  548. /*
  549. function changeUserBlock( $cid=null, $block=1, $option ) {
  550. global $database;
  551. if (count( $cid ) < 1) {
  552. $action = $block ? 'block' : 'unblock';
  553. echo "<script> alert('Select an item to $action'); window.history.go(-1);</script>\n";
  554. exit;
  555. }
  556. $cids = implode( ',', $cid );
  557. $query = "UPDATE #__users"
  558. . "\n SET block = $block"
  559. . "\n WHERE id IN ( $cids )"
  560. ;
  561. $database->setQuery( $query );
  562. if (!$database->query()) {
  563. echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
  564. exit();
  565. }
  566. // if action is to block a user
  567. if ( $block == 1 ) {
  568. foreach( $cid as $id ) {
  569. // delete user acounts active sessions
  570. logoutUser( $id, 'com_users', 'block' );
  571. }
  572. }
  573. mosRedirect( 'index2.php?option='. $option );
  574. }
  575. */
  576. /**
  577. * @param array An array of unique user id numbers
  578. * @param string The current url option
  579. */
  580. function logoutUser( $cid=null, $option, $task ) {
  581. global $database, $my;
  582. josSpoofCheck(null, null, 'request');
  583. if ( is_array( $cid ) ) {
  584. if (count( $cid ) < 1) {
  585. mosRedirect( 'index2.php?option='. $option, 'Please select a user' );
  586. }
  587. foreach( $cid as $cidA ) {
  588. $temp = new mosUser( $database );
  589. $temp->load( $cidA );
  590. // check to see whether a Administrator is attempting to log out a Super Admin
  591. if ( !( $my->gid == 24 && $temp->gid == 25 ) ) {
  592. $id[] = $cidA;
  593. }
  594. }
  595. mosArrayToInts( $cid );
  596. $ids = 'userid=' . implode( ' OR userid=', $cid );
  597. } else {
  598. $temp = new mosUser( $database );
  599. $temp->load( $cid );
  600. // check to see whether a Administrator is attempting to log out a Super Admin
  601. if ( $my->gid == 24 && $temp->gid == 25 ) {
  602. echo "<script> alert('You cannot log out a Super Administrator'); window.history.go(-1); </script>\n";
  603. exit();
  604. }
  605. $ids = 'userid=' . (int) $cid;
  606. }
  607. $query = "DELETE FROM #__session"
  608. . "\n WHERE ( $ids )"
  609. ;
  610. $database->setQuery( $query );
  611. $database->query();
  612. switch ( $task ) {
  613. case 'flogout':
  614. mosRedirect( 'index2.php', $database->getErrorMsg() );
  615. break;
  616. case 'remove':
  617. case 'block':
  618. case 'change':
  619. return;
  620. break;
  621. default:
  622. mosRedirect( 'index2.php?option='. $option, $database->getErrorMsg() );
  623. break;
  624. }
  625. }
  626. /**
  627. * Check if users are of lower permissions than current user (if not super-admin) and if the user himself is not included
  628. *
  629. * @param array of userId $cid
  630. * @param string $actionName to insert in message.
  631. * @return string of error if error, otherwise null
  632. * Added 1.0.11
  633. */
  634. function checkUserPermissions( $cid, $actionName, $allowActionToMyself = false ) {
  635. global $database, $acl, $my;
  636. $msg = null;
  637. if (is_array( $cid ) && count( $cid )) {
  638. $obj = new mosUser( $database );
  639. foreach ($cid as $id) {
  640. if ( $id != 0 ) {
  641. $obj->load( $id );
  642. $groups = $acl->get_object_groups( 'users', $id, 'ARO' );
  643. $this_group = strtolower( $acl->get_group_name( $groups[0], 'ARO' ) );
  644. } else {
  645. $this_group = 'Registered'; // minimal user group
  646. $obj->gid = $acl->get_group_id( $this_group, 'ARO' );
  647. }
  648. if ( !$allowActionToMyself && $id == $my->id ){
  649. $msg .= 'You cannot '. $actionName .' Yourself!';
  650. } else if (($obj->gid == $my->gid && !in_array($my->gid, array(24, 25))) || ($obj->gid && !in_array($obj->gid,getGIDSChildren($my->gid)))) {
  651. $msg .= 'You cannot '. $actionName .' a `'. $this_group .'`. Only higher-level users have this power. ';
  652. }
  653. }
  654. }
  655. return $msg;
  656. }
  657. /**
  658. * Added 1.0.11
  659. */
  660. function getGIDSChildren($gid) {
  661. global $database;
  662. $standardlist = array(-2,);
  663. $query = "SELECT g1.group_id, g1.name"
  664. ."\n FROM #__core_acl_aro_groups g1"
  665. ."\n LEFT JOIN #__core_acl_aro_groups g2 ON g2.lft >= g1.lft"
  666. ."\n WHERE g2.group_id = " . (int) $gid
  667. ."\n ORDER BY g1.name"
  668. ;
  669. $database->setQuery( $query );
  670. $array = $database->loadResultArray();
  671. if( $gid > 0 ) {
  672. $standardlist[]=-1;
  673. }
  674. $array = array_merge($array,$standardlist);
  675. return $array;
  676. }
  677. /**
  678. * Added 1.0.11
  679. */
  680. function getGIDSParents($gid) {
  681. global $database;
  682. $query = "SELECT g1.group_id, g1.name"
  683. ."\n FROM #__core_acl_aro_groups g1"
  684. ."\n LEFT JOIN #__core_acl_aro_groups g2 ON g2.lft <= g1.lft"
  685. ."\n WHERE g2.group_id = " . (int) $gid
  686. ."\n ORDER BY g1.name"
  687. ;
  688. $database->setQuery( $query );
  689. $array = $database->loadResultArray();
  690. return $array;
  691. }
  692. ?>