possessed.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. <?php
  2. /**
  3. * Bibliotheca
  4. *
  5. * Copyright 2018-2023 Johannes Keßler
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  19. */
  20. /**
  21. * Class Possessed
  22. * User and group management
  23. * Some groups are protected and should not be removed.
  24. *
  25. * passwords used here: password_hash("somePassword", PASSWORD_DEFAULT);
  26. */
  27. class Possessed {
  28. /**
  29. * the global DB object
  30. *
  31. * @var mysqli
  32. */
  33. private mysqli $_DB;
  34. /**
  35. * The user object to query with
  36. *
  37. * @var Doomguy
  38. */
  39. private Doomguy $_User;
  40. /**
  41. * Possessed constructor.
  42. *
  43. * @param mysqli $databaseConnectionObject
  44. * @param Doomguy $userObj
  45. */
  46. public function __construct(mysqli $databaseConnectionObject, Doomguy $userObj) {
  47. $this->_DB = $databaseConnectionObject;
  48. $this->_User = $userObj;
  49. }
  50. /**
  51. * Retrieve the groups for selection
  52. *
  53. * @return array
  54. */
  55. public function getGroups(): array {
  56. $ret = array();
  57. $queryStr = "SELECT `id`, `name`, `description`, `created`, `protected`
  58. FROM `".DB_PREFIX."_group`
  59. WHERE ".$this->_User->getSQLRightsString("delete")."
  60. ORDER BY `name`";
  61. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  62. try {
  63. $query = $this->_DB->query($queryStr);
  64. if($query !== false && $query->num_rows > 0) {
  65. while(($result = $query->fetch_assoc()) != false) {
  66. $ret[$result['id']] = $result;
  67. }
  68. }
  69. }
  70. catch (Exception $e) {
  71. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  72. }
  73. return $ret;
  74. }
  75. /**
  76. * Fetch all available users for management
  77. *
  78. * @return array
  79. */
  80. public function getUsers(): array {
  81. $ret = array();
  82. $queryStr = "SELECT `id`, `login`, `name`, `active`, `baseGroupId`, `protected`, `created`
  83. FROM `".DB_PREFIX."_user`
  84. WHERE ".$this->_User->getSQLRightsString("delete")."";
  85. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  86. try {
  87. $query = $this->_DB->query($queryStr);
  88. if($query !== false && $query->num_rows > 0) {
  89. while(($result = $query->fetch_assoc()) != false) {
  90. $ret[$result['id']] = $result;
  91. $ret[$result['id']]['groups'] = $this->_loadUserGroupInfo($result['id']);
  92. }
  93. }
  94. }
  95. catch (Exception $e) {
  96. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  97. }
  98. return $ret;
  99. }
  100. /**
  101. * Create or update a user and set the required user relations
  102. *
  103. * @param string $username
  104. * @param string $login
  105. * @param string $password
  106. * @param string $group Number
  107. * @param array $groups
  108. * @param bool $active
  109. * @return bool
  110. */
  111. public function createUser(string $username, string $login, string $password, string $group, array $groups, bool $active=false): bool {
  112. $ret = false;
  113. if($this->_validNewLogin($login) && $this->_validUsergroup($group)) {
  114. if ($active === true) {
  115. $active = "1";
  116. } else {
  117. $active = "0";
  118. }
  119. $_password = password_hash($password, PASSWORD_DEFAULT);
  120. $queryStr = "INSERT INTO `".DB_PREFIX . "_user`
  121. SET `name` = '".$this->_DB->real_escape_string($username)."',
  122. `login` = '".$this->_DB->real_escape_string($login)."',
  123. `password` = '".$this->_DB->real_escape_string($_password)."',
  124. `active` = '".$this->_DB->real_escape_string($active)."',
  125. `baseGroupId` = '".$this->_DB->real_escape_string($group)."',
  126. `rights` = 'rwxr--r--',
  127. `owner` = 0,
  128. `group` = '".$this->_DB->real_escape_string($group)."'";
  129. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  130. try {
  131. $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  132. $query = $this->_DB->query($queryStr);
  133. if ($query !== false) {
  134. $_userid = $this->_DB->insert_id;
  135. $queryStrOwner = "UPDATE `".DB_PREFIX . "_user`
  136. SET `owner` = '".$this->_DB->real_escape_string($_userid)."'
  137. WHERE `id` = '".$this->_DB->real_escape_string($_userid)."'";
  138. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStrOwner));
  139. $this->_DB->query($queryStrOwner);
  140. if(!empty($groups)) {
  141. $groups[] = $group;
  142. }
  143. else {
  144. $groups = array($group);
  145. }
  146. $_setGroupRelation = $this->_setGroupReleation($_userid,$groups);
  147. if($_setGroupRelation === false) {
  148. throw new Exception("Failed to insert user relation");
  149. }
  150. } else {
  151. throw new Exception("Failed to insert user");
  152. }
  153. $this->_DB->commit();
  154. $ret = true;
  155. }
  156. catch (Exception $e) {
  157. $this->_DB->rollback();
  158. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  159. }
  160. }
  161. return $ret;
  162. }
  163. /**
  164. * Update given user id with given data
  165. *
  166. * @param string $id Number
  167. * @param string $username
  168. * @param string $login
  169. * @param string $password
  170. * @param string $group
  171. * @param array $groups
  172. * @param bool $active
  173. * @param bool $refreshApiToken
  174. * @return bool
  175. */
  176. public function updateUser(string $id, string $username, string $login, string $password, string $group,
  177. array $groups, bool $active=false, bool $refreshApiToken=false): bool {
  178. $ret = false;
  179. if($this->_validUpdateLogin($login,$id) && $this->_validUsergroup($group)) {
  180. if ($active === true) {
  181. $active = "1";
  182. } else {
  183. $active = "0";
  184. }
  185. $queryStr = "UPDATE `".DB_PREFIX . "_user`
  186. SET `name` = '".$this->_DB->real_escape_string($username)."',
  187. `login` = '".$this->_DB->real_escape_string($login)."',
  188. `active` = '".$this->_DB->real_escape_string($active)."',
  189. `baseGroupId` = '".$this->_DB->real_escape_string($group)."'";
  190. if(Summoner::validate($password)) {
  191. $_password = password_hash($password, PASSWORD_DEFAULT);
  192. $queryStr .= ", `password` = '".$this->_DB->real_escape_string($_password)."'";
  193. }
  194. if($refreshApiToken === true) {
  195. $queryStr .= ", `apiToken` = '".md5(base64_encode(openssl_random_pseudo_bytes(30)))."'";
  196. $queryStr .= ", `apiTokenValidDate` = CURRENT_TIMESTAMP() + INTERVAL 1 DAY";
  197. }
  198. $queryStr .= " WHERE `id` = '".$this->_DB->real_escape_string($id)."'
  199. AND ".$this->_User->getSQLRightsString("delete")."";
  200. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  201. try {
  202. $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  203. $query = $this->_DB->query($queryStr);
  204. if ($query !== false) {
  205. if(!empty($groups)) {
  206. $groups[] = $group;
  207. }
  208. else {
  209. $groups = array($group);
  210. }
  211. $_setGroupRelation = $this->_setGroupReleation($id,$groups,true);
  212. if($_setGroupRelation === false) {
  213. throw new Exception('Failed to insert user relation');
  214. }
  215. } else {
  216. throw new Exception('Failed to insert user');
  217. }
  218. $this->_DB->commit();
  219. $ret = true;
  220. }
  221. catch (Exception $e) {
  222. $this->_DB->rollback();
  223. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  224. }
  225. }
  226. return $ret;
  227. }
  228. /**
  229. * Load the userinformation and groups for given id
  230. *
  231. * @param string $userId Number
  232. * @return array
  233. */
  234. public function getEditData(string $userId): array {
  235. $ret = array();
  236. if(Summoner::validate($userId,'digit')) {
  237. $queryStr = "SELECT `id`, `login`, `name`, `active`, `baseGroupId`,
  238. `created`,`apiToken`,`apiTokenValidDate`, `protected`
  239. FROM `".DB_PREFIX."_user`
  240. WHERE ".$this->_User->getSQLRightsString("delete")."
  241. AND `id` = '".$this->_DB->real_escape_string($userId)."'";
  242. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  243. try {
  244. $query = $this->_DB->query($queryStr);
  245. if($query !== false && $query->num_rows == 1) {
  246. $ret = $query->fetch_assoc();
  247. $ret['groups'] = $this->_loadUserGroupInfo($userId);
  248. }
  249. }
  250. catch (Exception $e) {
  251. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  252. }
  253. }
  254. return $ret;
  255. }
  256. /**
  257. * Delete user by given user id
  258. *
  259. * @param string $id Number
  260. * @return bool
  261. */
  262. public function deleteUser(string $id): bool {
  263. $ret = false;
  264. if(Summoner::validate($id,'digit')) {
  265. if(!$this->_checkIfUserIsInUse($id)) {
  266. try {
  267. $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  268. $d1 = $this->_DB->query("DELETE FROM `".DB_PREFIX."_user`
  269. WHERE `id` = '".$this->_DB->real_escape_string($id)."'
  270. AND ".$this->_User->getSQLRightsString("delete")."
  271. AND `protected` = '0'");
  272. $d2 = $this->_DB->query("DELETE FROM `".DB_PREFIX."_user2group` WHERE `fk_user_id` = '".$this->_DB->real_escape_string($id)."'");
  273. $d3 = $this->_DB->query("DELETE FROM `".DB_PREFIX."_userSession` WHERE `fk_user_id` = '".$this->_DB->real_escape_string($id)."'");
  274. if ($d1 === false || $d2 === false || $d3 === false) {
  275. throw new Exception('Failed to delete the user');
  276. }
  277. $this->_DB->commit();
  278. $ret = true;
  279. } catch (Exception $e) {
  280. $this->_DB->rollback();
  281. Summoner::sysLog("[ERROR] " . __METHOD__ . " mysql catch: " . $e->getMessage());
  282. }
  283. }
  284. }
  285. return $ret;
  286. }
  287. /**
  288. * Create group with given data. Validates duplicates based on name
  289. *
  290. * @param string $name
  291. * @param string $description
  292. * @return bool
  293. */
  294. public function createGroup(string $name, string $description): bool {
  295. $ret = false;
  296. if($this->_validNewGroup($name)) {
  297. $queryStr = "INSERT INTO `".DB_PREFIX."_group` SET
  298. `name` = '".$this->_DB->real_escape_string($name)."',
  299. `description` = '".$this->_DB->real_escape_string($description)."',
  300. `modificationuser` = '".$this->_DB->real_escape_string($this->_User->param('id'))."',
  301. `owner` = '".$this->_DB->real_escape_string($this->_User->param('id'))."',
  302. `group` = '".ADMIN_GROUP_ID."',
  303. `rights` = 'rwxr--r--'";
  304. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  305. try {
  306. $this->_DB->query($queryStr);
  307. $ret = true;
  308. }
  309. catch (Exception $e) {
  310. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  311. }
  312. }
  313. return $ret;
  314. }
  315. /**
  316. * Update given group identified by id with given name and description
  317. * Checks for duplicate
  318. *
  319. * @param string $id Number
  320. * @param string $name
  321. * @param string $description
  322. * @return bool
  323. */
  324. public function updateGroup(string $id, string $name, string $description): bool {
  325. $ret = false;
  326. if($this->_validUpdateGroup($name, $id)) {
  327. $queryStr = "UPDATE `".DB_PREFIX."_group` SET
  328. `name` = '".$this->_DB->real_escape_string($name)."',
  329. `description` = '".$this->_DB->real_escape_string($description)."',
  330. `modificationuser` = '".$this->_DB->real_escape_string($this->_User->param('id'))."'
  331. WHERE `id` = '".$this->_DB->real_escape_string($id)."'
  332. AND ".$this->_User->getSQLRightsString("delete")."";
  333. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  334. try {
  335. $this->_DB->query($queryStr);
  336. $ret = true;
  337. }
  338. catch (Exception $e) {
  339. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  340. }
  341. }
  342. return $ret;
  343. }
  344. /**
  345. * Delete given group identified by id from group table. No relation check yet.
  346. *
  347. * @param string $id Number
  348. * @return bool
  349. */
  350. public function deleteGroup(string $id): bool {
  351. $ret = false;
  352. if(Summoner::validate($id,'digit')) {
  353. if(!$this->_checkIfGroupIsInUse($id)) {
  354. $queryStr = "DELETE FROM `" . DB_PREFIX . "_group`
  355. WHERE " . $this->_User->getSQLRightsString("delete") . "
  356. AND `protected` = '0'
  357. AND `id` = '" . $this->_DB->real_escape_string($id) . "'";
  358. if (QUERY_DEBUG) Summoner::sysLog("[QUERY] " . __METHOD__ . " query: " . Summoner::cleanForLog($queryStr));
  359. try {
  360. $this->_DB->query($queryStr);
  361. $ret = true;
  362. } catch (Exception $e) {
  363. Summoner::sysLog("[ERROR] " . __METHOD__ . " mysql catch: " . $e->getMessage());
  364. }
  365. }
  366. }
  367. return $ret;
  368. }
  369. /**
  370. * Load groupd data from group table fo edit
  371. *
  372. * @param string $id Number
  373. * @return array
  374. */
  375. public function getEditGroupData(string $id): array {
  376. $ret = array();
  377. if(Summoner::validate($id,'digit')) {
  378. $queryStr = "SELECT `id`, `name`, `description`, `created`, `protected`
  379. FROM `".DB_PREFIX."_group`
  380. WHERE ".$this->_User->getSQLRightsString("delete")."
  381. AND `id` = '".$this->_DB->real_escape_string($id)."'";
  382. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  383. try {
  384. $query = $this->_DB->query($queryStr);
  385. if($query !== false && $query->num_rows > 0) {
  386. $ret = $query->fetch_assoc();
  387. }
  388. }
  389. catch (Exception $e) {
  390. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  391. }
  392. }
  393. return $ret;
  394. }
  395. /**
  396. * Check if given group name can be used as a new one
  397. *
  398. * @param string $name
  399. * @return bool
  400. */
  401. private function _validNewGroup(string $name): bool {
  402. $ret = false;
  403. if (Summoner::validate($name, 'nospace')) {
  404. $queryStr = "SELECT `id` FROM `".DB_PREFIX."_group`
  405. WHERE `name` = '".$this->_DB->real_escape_string($name)."'";
  406. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  407. try {
  408. $query = $this->_DB->query($queryStr);
  409. if ($query !== false && $query->num_rows < 1) {
  410. $ret = true;
  411. }
  412. }
  413. catch (Exception $e) {
  414. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  415. }
  416. }
  417. return $ret;
  418. }
  419. /**
  420. * Check if given group name can be used as an update to given group id
  421. *
  422. * @param string $name
  423. * @param string $id Number
  424. * @return bool
  425. */
  426. private function _validUpdateGroup(string $name, string $id): bool {
  427. $ret = false;
  428. if (Summoner::validate($name, 'nospace') && Summoner::validate($id,"digit")) {
  429. $queryStr = "SELECT `id` FROM `" . DB_PREFIX . "_group`
  430. WHERE `name` = '".$this->_DB->real_escape_string($name)."'
  431. AND `id` != '".$this->_DB->real_escape_string($id)."'";
  432. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  433. try {
  434. $query = $this->_DB->query($queryStr);
  435. if ($query !== false && $query->num_rows < 1) {
  436. $ret = true;
  437. }
  438. }
  439. catch (Exception $e) {
  440. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  441. }
  442. }
  443. return $ret;
  444. }
  445. /**
  446. * Check if given login can be used as a new one
  447. *
  448. * @param string $login
  449. * @return bool
  450. */
  451. private function _validNewLogin(string $login): bool {
  452. $ret = false;
  453. if (Summoner::validate($login, 'nospace')) {
  454. $queryStr = "SELECT `id` FROM `".DB_PREFIX."_user`
  455. WHERE `login` = '".$this->_DB->real_escape_string($login)."'";
  456. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  457. try {
  458. $query = $this->_DB->query($queryStr);
  459. if ($query !== false && $query->num_rows < 1) {
  460. $ret = true;
  461. }
  462. }
  463. catch (Exception $e) {
  464. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  465. }
  466. }
  467. return $ret;
  468. }
  469. /**
  470. * Check if given $login can be used as a new login for given id
  471. *
  472. * @param string $login
  473. * @param string $id Number
  474. * @return bool
  475. */
  476. private function _validUpdateLogin(string $login, string $id): bool {
  477. $ret = false;
  478. if (Summoner::validate($login, 'nospace') && Summoner::validate($id,"digit")) {
  479. $queryStr = "SELECT `id` FROM `" . DB_PREFIX . "_user`
  480. WHERE `login` = '".$this->_DB->real_escape_string($login)."'
  481. AND `id` != '".$this->_DB->real_escape_string($id)."'";
  482. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  483. try {
  484. $query = $this->_DB->query($queryStr);
  485. if ($query !== false && $query->num_rows < 1) {
  486. $ret = true;
  487. }
  488. }
  489. catch (Exception $e) {
  490. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  491. }
  492. }
  493. return $ret;
  494. }
  495. /**
  496. * check if given group id is present
  497. *
  498. * @param string $groupId Number
  499. * @return bool
  500. */
  501. private function _validUsergroup(string $groupId): bool {
  502. $ret = false;
  503. if(Summoner::validate($groupId,'digit')) {
  504. $queryStr = "SELECT `id` FROM `".DB_PREFIX."_group`
  505. WHERE `id` = '".$this->_DB->real_escape_string($groupId)."'";
  506. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  507. try {
  508. $query = $this->_DB->query($queryStr);
  509. if($query !== false && $query->num_rows > 0) {
  510. $ret = true;
  511. }
  512. }
  513. catch (Exception $e) {
  514. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  515. }
  516. }
  517. return $ret;
  518. }
  519. /**
  520. * Set user to group relation in database.
  521. * clean will delete all existing ones for given userid first.
  522. *
  523. * @param string $userid Number
  524. * @param array $group Array with group ids
  525. * @param bool $clean
  526. * @return bool
  527. */
  528. private function _setGroupReleation(string $userid, array $group, bool $clean=false): bool {
  529. $ret = false;
  530. if(Summoner::validate($userid,'digit')
  531. && is_array($group) && !empty($group)) {
  532. try {
  533. if($clean === true) {
  534. $queryStrDelete = "DELETE FROM `".DB_PREFIX."_user2group`
  535. WHERE `fk_user_id` = '".$this->_DB->real_escape_string($userid)."'";
  536. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStrDelete));
  537. $this->_DB->query($queryStrDelete);
  538. }
  539. $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_user2group` (`fk_user_id`, `fk_group_id`) VALUES ";
  540. foreach($group as $g) {
  541. $queryStr .= "('".$this->_DB->real_escape_string($userid)."','".$this->_DB->real_escape_string($g)."'),";
  542. }
  543. $queryStr = trim($queryStr, ",");
  544. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  545. $ret = $this->_DB->query($queryStr);
  546. }
  547. catch (Exception $e) {
  548. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  549. }
  550. }
  551. return $ret;
  552. }
  553. /**
  554. * Load all the groups the user is in and the information of them
  555. *
  556. * @param string $userId Number
  557. * @return array
  558. * @todo Not really needed. Can be done in one query. See Doomguy class
  559. *
  560. */
  561. private function _loadUserGroupInfo(string $userId): array{
  562. $ret = array();
  563. $queryStr = "SELECT g.name AS groupName,
  564. g.description AS groupDescription,
  565. g.id AS groupId
  566. FROM `".DB_PREFIX."_user2group` AS u2g,
  567. `".DB_PREFIX."_group` AS g
  568. WHERE u2g.fk_user_id = '".$this->_DB->real_escape_string($userId)."'
  569. AND u2g.fk_group_id = g.id";
  570. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  571. try {
  572. $query = $this->_DB->query($queryStr);
  573. if($query !== false && $query->num_rows > 0) {
  574. while(($result = $query->fetch_assoc()) != false) {
  575. $ret[$result['groupId']] = array(
  576. 'groupName' => $result['groupName'],
  577. 'groupDescription' => $result['groupDescription']
  578. );
  579. }
  580. }
  581. }
  582. catch (Exception $e) {
  583. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  584. }
  585. return $ret;
  586. }
  587. /**
  588. * Check if given userId is used and should not be deleted.
  589. *
  590. * @param string $userId
  591. * @return bool
  592. */
  593. private function _checkIfUserIsInUse(string $userId): bool {
  594. $ret = false;
  595. $queryStr = "SELECT `id` FROM `".DB_PREFIX."_collection`
  596. WHERE `owner` = '".$this->_DB->real_escape_string($userId)."'";
  597. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  598. try {
  599. $query = $this->_DB->query($queryStr);
  600. if($query !== false && $query->num_rows > 0) {
  601. $ret = true;
  602. }
  603. }
  604. catch (Exception $e) {
  605. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  606. }
  607. if(!$ret) {
  608. $queryStr = "SELECT `id` FROM `".DB_PREFIX."_user2group`
  609. WHERE `fk_user_id` = '".$this->_DB->real_escape_string($userId)."'";
  610. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  611. try {
  612. $query = $this->_DB->query($queryStr);
  613. if($query !== false && $query->num_rows > 0) {
  614. $ret = true;
  615. }
  616. }
  617. catch (Exception $e) {
  618. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  619. }
  620. }
  621. return $ret;
  622. }
  623. /**
  624. * Check if given groupId is used and should not be deleted.
  625. *
  626. * @param string $groupId
  627. * @return bool
  628. */
  629. private function _checkIfGroupIsInUse(string $groupId): bool {
  630. $ret = false;
  631. $queryStr = "SELECT `id` FROM `".DB_PREFIX."_collection`
  632. WHERE `group` = '".$this->_DB->real_escape_string($groupId)."'";
  633. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  634. try {
  635. $query = $this->_DB->query($queryStr);
  636. if($query !== false && $query->num_rows > 0) {
  637. $ret = true;
  638. }
  639. }
  640. catch (Exception $e) {
  641. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  642. }
  643. if(!$ret) {
  644. $queryStr = "SELECT `fk_group_id` FROM `".DB_PREFIX."_user2group`
  645. WHERE `fk_group_id` = '".$this->_DB->real_escape_string($groupId)."'";
  646. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  647. try {
  648. $query = $this->_DB->query($queryStr);
  649. if($query !== false && $query->num_rows > 0) {
  650. $ret = true;
  651. }
  652. }
  653. catch (Exception $e) {
  654. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  655. }
  656. }
  657. return $ret;
  658. }
  659. }