You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users_local.plugin.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. <?php
  2. /*
  3. * Poweradmin, a friendly web-based admin tool for PowerDNS. See <http://www.poweradmin.org> for more details. Copyright 2007-2009 Rejo Zenger <rejo@zenger.nl> Copyright 2010-2017 Poweradmin Development Team <http://www.poweradmin.org/credits.html> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  4. */
  5. /**
  6. * User profile functions
  7. *
  8. * @package Poweradmin
  9. * @copyright 2007-2010 Rejo Zenger <rejo@zenger.nl>
  10. * @copyright 2010-2017 Poweradmin Development Team
  11. * @license http://opensource.org/licenses/GPL-3.0 GPL
  12. *
  13. */
  14. require_once 'inc/toolkit.inc.php';
  15. require_once dirname(dirname(dirname(__DIR__))) . '/vendor/poweradmin/Password.php';
  16. /**
  17. * Verify User has Permission Name
  18. *
  19. * Function to see if user has right to do something. It will check if
  20. * user has "ueberuser" bit set. If it isn't, it will check if the user has
  21. * the specific permission. It returns "false" if the user doesn't have the
  22. * right, and "true" if the user has.
  23. *
  24. * @param array arg[0] Permission name
  25. *
  26. * @return boolean true if user has permission, false otherwise
  27. */
  28. function verify_permission_local($arg) {
  29. if (is_array($arg)) {
  30. $permission = $arg [0];
  31. } else {
  32. $permission = $arg;
  33. }
  34. static $cache = false;
  35. if ($cache !== false) {
  36. return array_key_exists('user_is_ueberuser', $cache) || array_key_exists($permission, $cache);
  37. }
  38. global $db;
  39. if ((!isset($_SESSION['userid'])) || (!is_object($db))) {
  40. return 0;
  41. }
  42. // Set current user ID.
  43. $userid = $_SESSION['userid'];
  44. $query = $db->prepare("SELECT
  45. perm_items.name AS permission
  46. FROM perm_templ_items
  47. LEFT JOIN perm_items ON perm_items.id = perm_templ_items.perm_id
  48. LEFT JOIN perm_templ ON perm_templ.id = perm_templ_items.templ_id
  49. LEFT JOIN users ON perm_templ.id = users.perm_templ
  50. WHERE users.id = ?");
  51. $query->execute(array($userid));
  52. $cache = $query->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC);
  53. return array_key_exists('user_is_ueberuser', $cache) || array_key_exists($permission, $cache);
  54. }
  55. /**
  56. * Get a list of all available permission templates
  57. *
  58. * @return mixed[] array of templates [id, name, descr]
  59. */
  60. function list_permission_templates_local() {
  61. global $db;
  62. $query = "SELECT * FROM perm_templ ORDER BY name";
  63. $response = $db->query($query);
  64. if (PEAR::isError($response)) {
  65. error($response->getMessage());
  66. return false;
  67. }
  68. $template_list = array();
  69. while ($template = $response->fetchRow()) {
  70. $template_list [] = array(
  71. "id" => $template ['id'],
  72. "name" => $template ['name'],
  73. "descr" => $template ['descr']
  74. );
  75. }
  76. return $template_list;
  77. }
  78. /**
  79. * Retrieve all users
  80. *
  81. * Its to show_users therefore the odd name. Has to be changed.
  82. *
  83. * @param int $id Exclude User ID
  84. * @param int $rowstart Startring row number
  85. * @param int $rowamount Number of rows to return this query
  86. *
  87. * @return mixed[] array with all users [id,username,fullname,email,description,active,numdomains]
  88. */
  89. function show_users_local($id = '', $rowstart = 0, $rowamount = 9999999) {
  90. global $db;
  91. $add = '';
  92. if (is_numeric($id)) {
  93. // When a user id is given, it is excluded from the userlist returned.
  94. $add = " WHERE users.id!=" . $db->quote($id, 'integer');
  95. }
  96. // Make a huge query.
  97. $query = "SELECT users.id AS id,
  98. users.username AS username,
  99. users.fullname AS fullname,
  100. users.email AS email,
  101. users.description AS description,
  102. users.active AS active,
  103. users.perm_templ AS perm_templ,
  104. count(zones.owner) AS aantal FROM users
  105. LEFT JOIN zones ON users.id=zones.owner$add
  106. GROUP BY
  107. users.id,
  108. users.username,
  109. users.fullname,
  110. users.email,
  111. users.description,
  112. users.perm_templ,
  113. users.active
  114. ORDER BY
  115. users.fullname";
  116. // Execute the huge query.
  117. $db->setLimit($rowamount, $rowstart);
  118. $response = $db->query($query);
  119. if (PEAR::isError($response)) {
  120. error($response->getMessage());
  121. return false;
  122. }
  123. $ret = array();
  124. while ($r = $response->fetchRow()) {
  125. $ret [] = array(
  126. "id" => $r ["id"],
  127. "username" => $r ["username"],
  128. "fullname" => $r ["fullname"],
  129. "email" => $r ["email"],
  130. "description" => $r ["description"],
  131. "active" => $r ["active"],
  132. "numdomains" => $r ["aantal"]
  133. );
  134. }
  135. return $ret;
  136. }
  137. /**
  138. * Check if Valid User
  139. *
  140. * Check if the given $userid is connected to a valid user.
  141. *
  142. * @param int $id User ID
  143. *
  144. * @return boolean true if user exists, false if users doesnt exist
  145. */
  146. function is_valid_user_local($id) {
  147. global $db;
  148. if (is_numeric($id)) {
  149. $response = $db->queryOne("SELECT id FROM users WHERE id=" . $db->quote($id, 'integer'));
  150. return ($response ? true : false);
  151. }
  152. }
  153. /**
  154. * Check if Username Exists
  155. *
  156. * Checks if a given username exists in the database.
  157. *
  158. * @param string $user Username
  159. *
  160. * @return boolean true if exists, false if not
  161. */
  162. function user_exists($user) {
  163. global $db;
  164. $response = $db->queryOne("SELECT id FROM users WHERE username=" . $db->quote($user, 'text'));
  165. return ($response ? true : false);
  166. }
  167. /**
  168. * Delete User ID
  169. *
  170. * Delete a user from the system. Will also delete zones owned by user or
  171. * re-assign those zones to a new specified owner.
  172. * $zones is an array of zone 'zid's to delete or re-assign depending on
  173. * 'target' value [delete,new_owner] and 'newowner' value
  174. *
  175. * @param int $uid User ID to delete
  176. * @param mixed[] $zones Array of zones
  177. *
  178. * @return boolean true on success, false otherwise
  179. */
  180. function delete_user_local($uid, $zones) {
  181. global $db;
  182. if (($uid != $_SESSION ['userid'] && !do_hook('verify_permission', 'user_edit_others')) || ($uid == $_SESSION ['userid'] && !do_hook('verify_permission', 'user_edit_own'))) {
  183. error(ERR_PERM_DEL_USER);
  184. return false;
  185. } else {
  186. if (is_array($zones)) {
  187. foreach ($zones as $zone) {
  188. if ($zone ['target'] == "delete") {
  189. delete_domain($zone ['zid']);
  190. } elseif ($zone ['target'] == "new_owner") {
  191. add_owner_to_zone($zone ['zid'], $zone ['newowner']);
  192. }
  193. }
  194. }
  195. $query = "DELETE FROM zones WHERE owner = " . $db->quote($uid, 'integer');
  196. $response = $db->query($query);
  197. if (PEAR::isError($response)) {
  198. error($response->getMessage());
  199. return false;
  200. }
  201. $query = "DELETE FROM users WHERE id = " . $db->quote($uid, 'integer');
  202. $response = $db->query($query);
  203. if (PEAR::isError($response)) {
  204. error($response->getMessage());
  205. return false;
  206. }
  207. delete_zone_templ_userid($uid);
  208. }
  209. return true;
  210. }
  211. /**
  212. * Delete Permission Template ID
  213. *
  214. * @param int $ptid Permission template ID
  215. *
  216. * @return boolean true on success, false otherwise
  217. */
  218. function delete_perm_templ_local($ptid) {
  219. global $db;
  220. if (!(do_hook('verify_permission', 'user_edit_templ_perm'))) {
  221. error(ERR_PERM_DEL_PERM_TEMPL);
  222. } else {
  223. $query = "SELECT id FROM users WHERE perm_templ = " . $ptid;
  224. $response = $db->queryOne($query);
  225. if (PEAR::isError($response)) {
  226. error($response->getMessage());
  227. return false;
  228. }
  229. if ($response) {
  230. error(ERR_PERM_TEMPL_ASSIGNED);
  231. return false;
  232. } else {
  233. $query = "DELETE FROM perm_templ_items WHERE templ_id = " . $ptid;
  234. $response = $db->query($query);
  235. if (PEAR::isError($response)) {
  236. error($response->getMessage());
  237. return false;
  238. }
  239. $query = "DELETE FROM perm_templ WHERE id = " . $ptid;
  240. $response = $db->query($query);
  241. if (PEAR::isError($response)) {
  242. error($response->getMessage());
  243. return false;
  244. }
  245. return true;
  246. }
  247. }
  248. }
  249. /**
  250. * Modify User Details
  251. *
  252. * Edit the information of an user.. sloppy implementation with too many queries.. (2) :)
  253. *
  254. * @param int $id User ID
  255. * @param string $user Username
  256. * @param string $fullname Full Name
  257. * @param string $email Email address
  258. * @param string $perm_templ Permission Template Name
  259. * @param string $description Description
  260. * @param int $active Active User
  261. * @param string $password Password
  262. *
  263. * @return boolean true if succesful, false otherwise
  264. */
  265. function edit_user_local($id, $user, $fullname, $email, $perm_templ, $description, $active, $password) {
  266. global $db;
  267. do_hook('verify_permission', 'user_edit_own') ? $perm_edit_own = "1" : $perm_edit_own = "0";
  268. do_hook('verify_permission', 'user_edit_others') ? $perm_edit_others = "1" : $perm_edit_others = "0";
  269. if (($id == $_SESSION ["userid"] && $perm_edit_own == "1") || ($id != $_SESSION ["userid"] && $perm_edit_others == "1")) {
  270. if (!is_valid_email($email)) {
  271. error(ERR_INV_EMAIL);
  272. return false;
  273. }
  274. if ($active != 1) {
  275. $active = 0;
  276. }
  277. // Before updating the database we need to check whether the user wants to
  278. // change the username. If the user wants to change the username, we need
  279. // to make sure it doesn't already exists.
  280. //
  281. // First find the current username of the user ID we want to change. If the
  282. // current username is not the same as the username that was given by the
  283. // user, the username should apparantly changed. If so, check if the "new"
  284. // username already exists.
  285. $query = "SELECT username FROM users WHERE id = " . $db->quote($id, 'integer');
  286. $response = $db->query($query);
  287. if (PEAR::isError($response)) {
  288. error($response->getMessage());
  289. return false;
  290. }
  291. $usercheck = array();
  292. $usercheck = $response->fetchRow();
  293. if ($usercheck ['username'] != $user) {
  294. // Username of user ID in the database is different from the name
  295. // we have been given. User wants a change of username. Now, make
  296. // sure it doesn't already exist.
  297. $query = "SELECT id FROM users WHERE username = " . $db->quote($user, 'text');
  298. $response = $db->queryOne($query);
  299. if ($response) {
  300. error(ERR_USER_EXIST);
  301. return false;
  302. }
  303. }
  304. // So, user doesn't want to change username or, if he wants, there is not
  305. // another user that goes by the wanted username. So, go ahead!
  306. $query = "UPDATE users SET username = " . $db->quote($user, 'text') . ",
  307. fullname = " . $db->quote($fullname, 'text') . ",
  308. email = " . $db->quote($email, 'text') . ",";
  309. if (do_hook('verify_permission', 'user_edit_templ_perm')) {
  310. $query .= "perm_templ = " . $db->quote($perm_templ, 'integer') . ",";
  311. }
  312. $query .= "description = " . $db->quote($description, 'text') . ",
  313. active = " . $db->quote($active, 'integer');
  314. if ($password != "") {
  315. $query .= ", password = " . $db->quote(Poweradmin\Password::hash($password), 'text');
  316. }
  317. $query .= " WHERE id = " . $db->quote($id, 'integer');
  318. $response = $db->query($query);
  319. if (PEAR::isError($response)) {
  320. error($response->getMessage());
  321. return false;
  322. }
  323. } else {
  324. error(ERR_PERM_EDIT_USER);
  325. return false;
  326. }
  327. return true;
  328. }
  329. /**
  330. * Change User Password
  331. *
  332. * Change the pass of the user.
  333. * The user is automatically logged out after the pass change.
  334. *
  335. * @param mixed[] $details User Details
  336. *
  337. * @return null
  338. */
  339. function change_user_pass_local($details) {
  340. global $db;
  341. if ($details ['newpass'] != $details ['newpass2']) {
  342. error(ERR_USER_MATCH_NEW_PASS);
  343. return false;
  344. }
  345. $query = "SELECT id, password FROM users WHERE username = " . $db->quote($_SESSION ["userlogin"], 'text');
  346. $response = $db->query($query);
  347. if (PEAR::isError($response)) {
  348. error($response->getMessage());
  349. return false;
  350. }
  351. $rinfo = $response->fetchRow();
  352. if (Poweradmin\Password::verify($details['currentpass'], $rinfo['password'])) {
  353. $query = "UPDATE users SET password = " . $db->quote(Poweradmin\Password::hash($details['newpass']), 'text') . " WHERE id = " . $db->quote($rinfo ['id'], 'integer');
  354. $response = $db->query($query);
  355. if (PEAR::isError($response)) {
  356. error($response->getMessage());
  357. return false;
  358. }
  359. logout(_('Password has been changed, please login.'), 'success');
  360. } else {
  361. error(ERR_USER_WRONG_CURRENT_PASS);
  362. return false;
  363. }
  364. }
  365. /**
  366. * Get User FullName from User ID
  367. *
  368. * Get a fullname when you have a userid.
  369. *
  370. * @param int $id User ID
  371. *
  372. * @return string Full Name
  373. */
  374. function get_fullname_from_userid_local($id) {
  375. global $db;
  376. if (is_numeric($id)) {
  377. $response = $db->query("SELECT fullname FROM users WHERE id=" . $db->quote($id, 'integer'));
  378. if (PEAR::isError($response)) {
  379. error($response->getMessage());
  380. return false;
  381. }
  382. $r = $response->fetchRow();
  383. return $r ["fullname"];
  384. } else {
  385. error(ERR_INV_ARG);
  386. return false;
  387. }
  388. }
  389. /**
  390. * Get User FullName from User ID
  391. * fixme: Duplicate function
  392. *
  393. * Get a fullname when you have a userid.
  394. *
  395. * @param int $id User ID
  396. *
  397. * @return string Full Name
  398. */
  399. function get_owner_from_id_local($id) {
  400. global $db;
  401. if (is_numeric($id)) {
  402. $response = $db->queryRow("SELECT fullname FROM users WHERE id=" . $db->quote($id, 'integer'));
  403. if ($response) {
  404. return $response ["fullname"];
  405. } else {
  406. error(ERR_USER_NOT_EXIST);
  407. }
  408. }
  409. error(ERR_INV_ARG);
  410. }
  411. /**
  412. * Get Full Names of owners for a Domain ID
  413. *
  414. * @todo also fetch the subowners
  415. *
  416. * @param int $id Domain ID
  417. *
  418. * @return string[] array of owners for domain
  419. */
  420. function get_fullnames_owners_from_domainid_local($id) {
  421. global $db;
  422. if (is_numeric($id)) {
  423. $response = $db->query("SELECT users.id, users.fullname FROM users, zones WHERE zones.domain_id=" . $db->quote($id, 'integer') . " AND zones.owner=users.id ORDER by fullname");
  424. if ($response) {
  425. $names = array();
  426. while ($r = $response->fetchRow()) {
  427. $names [] = $r ['fullname'];
  428. }
  429. return implode(', ', $names);
  430. }
  431. return "";
  432. }
  433. error(ERR_INV_ARG);
  434. }
  435. /**
  436. * Verify User is Zone ID owner
  437. *
  438. * @param int $zoneid Zone ID
  439. *
  440. * @return int 1 if owner, 0 if not owner
  441. */
  442. function verify_user_is_owner_zoneid_local($zoneid) {
  443. global $db;
  444. $userid = $_SESSION ["userid"];
  445. if (is_numeric($zoneid)) {
  446. $response = $db->queryOne("SELECT zones.id FROM zones
  447. WHERE zones.owner = " . $db->quote($userid, 'integer') . "
  448. AND zones.domain_id = " . $db->quote($zoneid, 'integer'));
  449. return ($response ? "1" : "0");
  450. }
  451. error(ERR_INV_ARG);
  452. }
  453. /**
  454. * Get User Details
  455. *
  456. * Gets an array of all users and their details
  457. *
  458. * @param int $specific User ID (optional)
  459. *
  460. * @return mixed[] array of user details
  461. */
  462. function get_user_detail_list_local($specific) {
  463. global $db;
  464. global $ldap_use;
  465. $userid = $_SESSION ['userid'];
  466. // fixme: does this actually verify the permission?
  467. if (v_num($specific)) {
  468. $sql_add = "AND users.id = " . $db->quote($specific, 'integer');
  469. } else {
  470. if (do_hook('verify_permission', 'user_view_others')) {
  471. $sql_add = "";
  472. } else {
  473. $sql_add = "AND users.id = " . $db->quote($userid, 'integer');
  474. }
  475. }
  476. $query = "SELECT users.id AS uid,
  477. username,
  478. fullname,
  479. email,
  480. description AS descr,
  481. active,";
  482. if ($ldap_use) {
  483. $query .= "use_ldap,";
  484. }
  485. $query .= "perm_templ.id AS tpl_id,
  486. perm_templ.name AS tpl_name,
  487. perm_templ.descr AS tpl_descr
  488. FROM users, perm_templ
  489. WHERE users.perm_templ = perm_templ.id " . $sql_add . "
  490. ORDER BY username";
  491. $response = $db->query($query);
  492. if (PEAR::isError($response)) {
  493. error($response->getMessage());
  494. return false;
  495. }
  496. while ($user = $response->fetchRow()) {
  497. $userlist [] = array(
  498. "uid" => $user ['uid'],
  499. "username" => $user ['username'],
  500. "fullname" => $user ['fullname'],
  501. "email" => $user ['email'],
  502. "descr" => $user ['descr'],
  503. "active" => $user ['active'],
  504. "use_ldap" => isset($user['use_ldap']) ? $user ['use_ldap'] : 0,
  505. "tpl_id" => $user ['tpl_id'],
  506. "tpl_name" => $user ['tpl_name'],
  507. "tpl_descr" => $user ['tpl_descr']
  508. );
  509. }
  510. return $userlist;
  511. }
  512. /**
  513. * Get List of Permissions
  514. *
  515. * Get a list of permissions that are available. If first argument is "0", it
  516. * should return all available permissions. If the first argument is > "0", it
  517. * should return the permissions assigned to that particular template only. If
  518. * second argument is true, only the permission names are returned.
  519. *
  520. * @param int $templ_id Template ID (optional) [default=0]
  521. * @param boolean $return_name_only Return name only or all details (optional) [default=false]
  522. *
  523. * @return mixed[] array of permissions [id,name,descr] or permission names [name]
  524. */
  525. function get_permissions_by_template_id_local($templ_id = 0, $return_name_only = false) {
  526. global $db;
  527. $limit = '';
  528. if ($templ_id > 0) {
  529. $limit = ", perm_templ_items
  530. WHERE perm_templ_items.templ_id = " . $db->quote($templ_id, 'integer') . "
  531. AND perm_templ_items.perm_id = perm_items.id";
  532. }
  533. $query = "SELECT perm_items.id AS id,
  534. perm_items.name AS name,
  535. perm_items.descr AS descr
  536. FROM perm_items" . $limit . "
  537. ORDER BY name";
  538. $response = $db->query($query);
  539. if (PEAR::isError($response)) {
  540. error($response->getMessage());
  541. return false;
  542. }
  543. $permission_list = array();
  544. while ($permission = $response->fetchRow()) {
  545. if ($return_name_only == false) {
  546. $permission_list [] = array(
  547. "id" => $permission ['id'],
  548. "name" => $permission ['name'],
  549. "descr" => $permission ['descr']
  550. );
  551. } else {
  552. $permission_list [] = $permission ['name'];
  553. }
  554. }
  555. return $permission_list;
  556. }
  557. /**
  558. * Get name and description of template from Template ID
  559. *
  560. * @param int $templ_id Template ID
  561. *
  562. * @return mixed[] Template details
  563. */
  564. function get_permission_template_details_local($templ_id) {
  565. global $db;
  566. $query = "SELECT *
  567. FROM perm_templ
  568. WHERE perm_templ.id = " . $db->quote($templ_id, 'integer');
  569. $response = $db->query($query);
  570. if (PEAR::isError($response)) {
  571. error($response->getMessage());
  572. return false;
  573. }
  574. $details = $response->fetchRow();
  575. return $details;
  576. }
  577. /**
  578. * Add a Permission Template
  579. *
  580. * @param mixed[] $details Permission template details [templ_name,templ_descr,perm_id]
  581. *
  582. * @return boolean true on success, false otherwise
  583. */
  584. function add_perm_templ_local($details) {
  585. global $db;
  586. global $db_type;
  587. // Fix permission template name and description first.
  588. $query = "INSERT INTO perm_templ (name, descr)
  589. VALUES (" . $db->quote($details ['templ_name'], 'text') . ", " . $db->quote($details ['templ_descr'], 'text') . ")";
  590. $response = $db->query($query);
  591. if (PEAR::isError($response)) {
  592. error($response->getMessage());
  593. return false;
  594. }
  595. if ($db_type == 'pgsql') {
  596. $perm_templ_id = $db->lastInsertId('perm_templ_id_seq');
  597. } else {
  598. $perm_templ_id = $db->lastInsertId();
  599. }
  600. if (isset($details ['perm_id'])) {
  601. foreach ($details ['perm_id'] as $perm_id) {
  602. $query = "INSERT INTO perm_templ_items (templ_id, perm_id) VALUES (" . $db->quote($perm_templ_id, 'integer') . "," . $db->quote($perm_id, 'integer') . ")";
  603. $response = $db->query($query);
  604. if (PEAR::isError($response)) {
  605. error($response->getMessage());
  606. return false;
  607. }
  608. }
  609. }
  610. return true;
  611. }
  612. /**
  613. * Update permission template details
  614. *
  615. * @param mixed[] $details Permission Template Details
  616. *
  617. * @return boolean true on success, false otherwise
  618. */
  619. function update_perm_templ_details_local($details) {
  620. global $db;
  621. // Fix permission template name and description first.
  622. $query = "UPDATE perm_templ
  623. SET name = " . $db->quote($details ['templ_name'], 'text') . ",
  624. descr = " . $db->quote($details ['templ_descr'], 'text') . "
  625. WHERE id = " . $db->quote($details ['templ_id'], 'integer');
  626. $response = $db->query($query);
  627. if (PEAR::isError($response)) {
  628. error($response->getMessage());
  629. return false;
  630. }
  631. // Now, update list of permissions assigned to this template. We could do
  632. // this The Correct Way [tm] by comparing the list of permissions that are
  633. // currently assigned with a list of permissions that should be assigned and
  634. // apply the difference between these two lists to the database. That sounds
  635. // like too much work. Just delete all the permissions currently assigned to
  636. // the template, than assign all the permessions the template should have.
  637. $query = "DELETE FROM perm_templ_items WHERE templ_id = " . $details ['templ_id'];
  638. $response = $db->query($query);
  639. if (PEAR::isError($response)) {
  640. error($response->getMessage());
  641. return false;
  642. }
  643. if (isset($details ['perm_id'])) {
  644. foreach ($details ['perm_id'] as $perm_id) {
  645. $query = "INSERT INTO perm_templ_items (templ_id, perm_id) VALUES (" . $db->quote($details ['templ_id'], 'integer') . "," . $db->quote($perm_id, 'integer') . ")";
  646. $response = $db->query($query);
  647. if (PEAR::isError($response)) {
  648. error($response->getMessage());
  649. return false;
  650. }
  651. }
  652. }
  653. return true;
  654. }
  655. /**
  656. * Update User Details
  657. *
  658. * @param mixed[] $details User details
  659. *
  660. * @return boolean true on success, false otherise
  661. */
  662. function update_user_details_local($details) {
  663. global $db;
  664. do_hook('verify_permission', 'user_edit_own') ? $perm_edit_own = "1" : $perm_edit_own = "0";
  665. do_hook('verify_permission', 'user_edit_others') ? $perm_edit_others = "1" : $perm_edit_others = "0";
  666. do_hook('verify_permission', 'templ_perm_edit') ? $perm_templ_perm_edit = "1" : $perm_templ_perm_edit = "0";
  667. do_hook('verify_permission', 'user_is_ueberuser') ? $perm_is_godlike = "1" : $perm_is_godlike = "0";
  668. if (($details ['uid'] == $_SESSION ["userid"] && $perm_edit_own == "1") || ($details ['uid'] != $_SESSION ["userid"] && $perm_edit_others == "1")) {
  669. if (!is_valid_email($details ['email'])) {
  670. error(ERR_INV_EMAIL);
  671. return false;
  672. }
  673. if (!isset($details ['active']) || $details ['active'] != "on") {
  674. $active = 0;
  675. } else {
  676. $active = 1;
  677. }
  678. if (isset($details ['use_ldap'])) {
  679. $use_ldap = 1;
  680. } else {
  681. $use_ldap = 0;
  682. }
  683. // Before updating the database we need to check whether the user wants to
  684. // change the username. If the user wants to change the username, we need
  685. // to make sure it doesn't already exists.
  686. //
  687. // First find the current username of the user ID we want to change. If the
  688. // current username is not the same as the username that was given by the
  689. // user, the username should apparantly changed. If so, check if the "new"
  690. // username already exists.
  691. $query = "SELECT username FROM users WHERE id = " . $db->quote($details ['uid'], 'integer');
  692. $response = $db->query($query);
  693. if (PEAR::isError($response)) {
  694. error($response->getMessage());
  695. return false;
  696. }
  697. $usercheck = array();
  698. $usercheck = $response->fetchRow();
  699. if ($usercheck ['username'] != $details ['username']) {
  700. // Username of user ID in the database is different from the name
  701. // we have been given. User wants a change of username. Now, make
  702. // sure it doesn't already exist.
  703. $query = "SELECT id FROM users WHERE username = " . $db->quote($details ['username'], 'text');
  704. $response = $db->queryOne($query);
  705. if ($response) {
  706. error(ERR_USER_EXIST);
  707. return false;
  708. }
  709. }
  710. // So, user doesn't want to change username or, if he wants, there is not
  711. // another user that goes by the wanted username. So, go ahead!
  712. $query = "UPDATE users SET username = " . $db->quote($details ['username'], 'text') . ",
  713. fullname = " . $db->quote($details ['fullname'], 'text') . ",
  714. email = " . $db->quote($details ['email'], 'text') . ",
  715. description = " . $db->quote($details ['descr'], 'text') . ",
  716. active = " . $db->quote($active, 'integer');
  717. // If the user is alllowed to change the permission template, set it.
  718. if ($perm_templ_perm_edit == "1") {
  719. $query .= ", perm_templ = " . $db->quote($details ['templ_id'], 'integer');
  720. }
  721. // If the user is allowed to change the use_ldap flag, set it.
  722. if ($perm_is_godlike == "1") {
  723. $query .= ", use_ldap = " . $db->quote($use_ldap, 'integer');
  724. }
  725. if (isset($details ['password']) && $details ['password'] != "") {
  726. $query .= ", password = " . $db->quote(Poweradmin\Password::hash($details['password'], 'text'));
  727. }
  728. $query .= " WHERE id = " . $db->quote($details ['uid'], 'integer');
  729. $response = $db->query($query);
  730. if (PEAR::isError($response)) {
  731. error($response->getMessage());
  732. return false;
  733. }
  734. } else {
  735. error(ERR_PERM_EDIT_USER);
  736. return false;
  737. }
  738. return true;
  739. }
  740. /**
  741. * Add a new user
  742. *
  743. * @param mixed[] $details Array of User details
  744. *
  745. * @return boolean true on success, false otherwise
  746. */
  747. function add_new_user_local($details) {
  748. global $db;
  749. if (!do_hook('verify_permission', 'user_add_new')) {
  750. error(ERR_PERM_ADD_USER);
  751. return false;
  752. } elseif (user_exists($details ['username'])) {
  753. error(ERR_USER_EXIST);
  754. return false;
  755. } elseif (!is_valid_email($details ['email'])) {
  756. error(ERR_INV_EMAIL);
  757. return false;
  758. } elseif ($details ['active'] == 1) {
  759. $active = 1;
  760. } else {
  761. $active = 0;
  762. }
  763. $query = "INSERT INTO users (username, password, fullname, email, description,";
  764. if (do_hook('verify_permission', 'user_edit_templ_perm')) {
  765. $query .= ' perm_templ,';
  766. }
  767. $password_hash = Poweradmin\Password::hash($details['password']);
  768. $query .= " active) VALUES (" . $db->quote($details ['username'], 'text') . ", " . $db->quote($password_hash, 'text') . ", " . $db->quote($details ['fullname'], 'text') . ", " . $db->quote($details ['email'], 'text') . ", " . $db->quote($details ['descr'], 'text') . ", ";
  769. if (do_hook('verify_permission', 'user_edit_templ_perm')) {
  770. $query .= $db->quote($details ['perm_templ'], 'integer') . ", ";
  771. }
  772. $query .= $db->quote($active, 'integer') . ")";
  773. $response = $db->query($query);
  774. if (PEAR::isError($response)) {
  775. error($response->getMessage());
  776. return false;
  777. }
  778. return true;
  779. }