BxDolAccountQuery.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * All queries related to account
  11. */
  12. class BxDolAccountQuery extends BxDolDb implements iBxDolSingleton
  13. {
  14. protected function __construct()
  15. {
  16. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  17. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  18. parent::__construct();
  19. }
  20. /**
  21. * Prevent cloning the instance
  22. */
  23. public function __clone()
  24. {
  25. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  26. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  27. }
  28. /**
  29. * Get singleton instance of the class
  30. */
  31. public static function getInstance()
  32. {
  33. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  34. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolAccountQuery();
  35. return $GLOBALS['bxDolClasses'][__CLASS__];
  36. }
  37. public function getOperators ()
  38. {
  39. $sSql = $this->prepare("SELECT `id` FROM `sys_accounts` WHERE `role`&" . BX_DOL_ROLE_ADMIN);
  40. return $this->getColumn($sSql);
  41. }
  42. /**
  43. * Get account by specified field name and value.
  44. * It is for internal usage only.
  45. * Use other funtions to get account info, like getInfoById, etc.
  46. * @param string $sField database field name
  47. * @param mixed $sValue database field value
  48. * @return array with porfile info
  49. */
  50. protected function _getDataByField ($sField, $sValue)
  51. {
  52. $sSql = $this->prepare("SELECT * FROM `sys_accounts` WHERE `$sField` = ? LIMIT 1", $sValue);
  53. return $this->getRow($sSql);
  54. }
  55. /**
  56. * Get account info by id
  57. * @param int $iID account id
  58. * @return array with account info
  59. */
  60. public function getInfoById( $iID )
  61. {
  62. return $this->_getDataByField('id', (int)$iID);
  63. }
  64. /**
  65. * get account id by emial
  66. */
  67. public function getIdByEmail($sEmail)
  68. {
  69. return (int)$this->_getFieldByField('id', 'email', $sEmail);
  70. }
  71. /**
  72. * get account id by phone
  73. */
  74. public function getIdByPhone($sPhone)
  75. {
  76. return (int)$this->_getFieldByField('id', 'phone', $sPhone);
  77. }
  78. /**
  79. * get account id by id
  80. */
  81. public function getIdById($iId, $bClearCache = false)
  82. {
  83. if (!$iId)
  84. return false;
  85. $sKey = 'BxDolAccountQuery::getIdById' . $iId;
  86. if ($bClearCache)
  87. $this->cleanMemory($sKey);
  88. $sSql = $this->prepare("SELECT `id` FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iId);
  89. $bResult = $this->fromMemory($sKey, 'getOne', $sSql);
  90. if (!$bResult)
  91. $this->cleanMemory($sKey);
  92. return $bResult;
  93. }
  94. /**
  95. * get first studio operator id
  96. */
  97. public function getStudioOperatorId()
  98. {
  99. return (int)$this->_getFieldByField('id', 'role', 3);
  100. }
  101. /**
  102. * Get account email by id
  103. * @param string $s search account by this id
  104. * @return account email
  105. */
  106. public function getEmail($iID)
  107. {
  108. return $this->_getFieldByField('email', 'id', (int)$iID);
  109. }
  110. /**
  111. * Get account password by id
  112. * @param string $s search account by this id
  113. * @return account password
  114. */
  115. public function getPassword($iID)
  116. {
  117. return $this->_getFieldByField('password', 'id', (int)$iID);
  118. }
  119. /**
  120. * Is account online by id
  121. * @param int $iId account id
  122. * @return account online status
  123. */
  124. public function isOnline($iId)
  125. {
  126. $sSql = $this->prepare("SELECT
  127. `ta`.`id`
  128. FROM `sys_accounts` AS `ta`
  129. INNER JOIN `sys_sessions` AS `ts` ON `ta`.`id`=`ts`.`user_id`
  130. WHERE
  131. `ta`.`id` = ? AND
  132. `ts`.`date` > (UNIX_TIMESTAMP() - 60 * ?)
  133. LIMIT 1", $iId, (int)getParam('sys_account_online_time'));
  134. return (int)$this->getOne($sSql) > 0;
  135. }
  136. /**
  137. * Update password
  138. * @param $sPasswordHash - password hash
  139. * @param $sSalt - pasword salt
  140. * @param $iAccountId - account id to update password for
  141. * @return number of affected rows
  142. */
  143. public function updatePassword($sPasswordHash, $sSalt, $iAccountId)
  144. {
  145. return $this->query("UPDATE `sys_accounts` SET `password` = :password, `salt` = :salt, `password_changed` = UNIX_TIMESTAMP() WHERE `id` = :id", [
  146. 'id' => $iAccountId,
  147. 'password' => $sPasswordHash,
  148. 'salt' => $sSalt
  149. ]);
  150. }
  151. /**
  152. * Save password log
  153. * @param $sPasswordHash - password hash
  154. * @param $sSalt - pasword salt
  155. * @param $iAccountId - account id to update password for
  156. * @return number of affected rows
  157. */
  158. public function logPassword($iAccountId)
  159. {
  160. $iCount = (int)getParam('sys_account_accounts_password_log_count');
  161. if($iCount <= 0)
  162. return;
  163. $aAccount = $this->getRow("SELECT `password`, `password_changed`, `salt` FROM `sys_accounts` WHERE `id` = :id", [
  164. 'id' => $iAccountId
  165. ]);
  166. $this->query("INSERT INTO `sys_accounts_password` SET " . $this->arrayToSQL([
  167. 'account_id' => $iAccountId,
  168. 'password' => $aAccount['password'],
  169. 'password_changed' => $aAccount['password_changed'],
  170. 'salt' => $aAccount['salt'],
  171. ]));
  172. $this->query("DELETE FROM `sys_accounts_password` WHERE `id` NOT IN (SELECT `id` FROM (SELECT `id` FROM `sys_accounts_password` WHERE `account_id` = :account_id ORDER BY `password_changed` DESC LIMIT 0, " . $iCount . ") AS `tap`)", [
  173. 'account_id' => $iAccountId
  174. ]);
  175. }
  176. public function getLastPasswordChanged($iAccountId)
  177. {
  178. $sSql = $this->prepare("SELECT `password_changed` FROM `sys_accounts_password` WHERE `account_id` = ? ORDER BY password_changed DESC limit 0, 1", $iAccountId);
  179. return (int)$this->getOne($sSql);
  180. }
  181. public function getLastPasswordLog($iAccountId)
  182. {
  183. $sSql = $this->prepare("SELECT * FROM `sys_accounts_password` WHERE `account_id` = ? ", $iAccountId);
  184. return $this->getAll($sSql);
  185. }
  186. /**
  187. * Update last logged in time
  188. * @param int $iID account id
  189. * @return number of affected rows
  190. */
  191. public function updateLoggedIn($iID)
  192. {
  193. return $this->query("UPDATE `sys_accounts` SET `logged` = :ts, `ip` = :ip, `login_attempts` = 0, `lang_id` = :lang WHERE `id`= :id", ['ts' => time(), 'ip' => getVisitorIP(), 'lang' => BxDolLanguages::getInstance()->getCurrentLangId(), 'id' => $iID]);
  194. }
  195. /**
  196. * Update login attempts counter
  197. * @param int $iID account id
  198. * @return number of affected rows
  199. */
  200. public function updateAttemptsCounter($iID)
  201. {
  202. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `login_attempts` = `login_attempts` + 1 WHERE `id`= ?", $iID);
  203. return $this->query($sQuery);
  204. }
  205. /**
  206. * Lock account
  207. * @param int $iID account id
  208. * @return number of affected rows
  209. */
  210. public function lockAccount($iID)
  211. {
  212. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `locked` = 1 WHERE `id`= ?", $iID);
  213. return $this->query($sQuery);
  214. }
  215. /**
  216. * Unlock account
  217. * @param int $iID account id
  218. * @return number of affected rows
  219. */
  220. public function unlockAccount($iID)
  221. {
  222. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `locked` = 0, `login_attempts` = 0 WHERE `id`= ?", $iID);
  223. return $this->query($sQuery);
  224. }
  225. /**
  226. * Update language
  227. * @param int $iID account id
  228. * @return number of affected rows
  229. */
  230. public function updateLanguage($iID, $iLangId)
  231. {
  232. return $this->_updateField ($iID, 'lang_id', $iLangId);
  233. }
  234. /**
  235. * Update current profile id associated with account
  236. * @param int $iID account id
  237. * @param int $iProfileId set current profile id to this value
  238. * @return number of affected rows
  239. */
  240. public function updateCurrentProfile($iID, $iProfileId)
  241. {
  242. if ($bResult = $this->_updateField ($iID, 'profile_id', $iProfileId))
  243. $this->cleanMemory('BxDolProfileQuery::getCurrentProfileByAccount' . $iID);
  244. // clean acount swither cache
  245. bx_content_cache_del("profile_switcher_{$iID}_0");
  246. bx_content_cache_del("profile_switcher_{$iID}_1");
  247. return $bResult;
  248. }
  249. /**
  250. * Update 'email_confirmed' field.
  251. * @param int $isConfirmed - 0: mark email as unconfirmed, 1: as confirmed
  252. * @param int $iID - account id
  253. * @return number of affected rows
  254. */
  255. public function updateEmailConfirmed($isConfirmed, $iID)
  256. {
  257. return $this->_updateField ($iID, 'email_confirmed', $isConfirmed ? 1 : 0);
  258. }
  259. /**
  260. * Update 'phone_confirmed' field.
  261. * @param int $isConfirmed - 0: mark phone as unconfirmed, 1: as confirmed
  262. * @param int $iID - account id
  263. * @return number of affected rows
  264. */
  265. public function updatePhoneConfirmed($isConfirmed, $iID)
  266. {
  267. return $this->_updateField ($iID, 'phone_confirmed', $isConfirmed ? 1 : 0);
  268. }
  269. /**
  270. * Update 'welcome_sent' field.
  271. * @param int $isSent - 0: mark as sent, 1: as not sent
  272. * @param int $iID - account id
  273. * @return number of affected rows
  274. */
  275. public function updateWelcomeSent($isSent, $iID)
  276. {
  277. return $this->_updateField ($iID, 'welcome_sent', $isSent ? 1 : 0);
  278. }
  279. /**
  280. * Update 'phone' field.
  281. * @param int $sPhone - phone number
  282. * @param int $iID - account id
  283. * @return number of affected rows
  284. */
  285. public function updatePhone($sPhone, $iID)
  286. {
  287. $sSql = $this->prepare("UPDATE `sys_accounts` SET `phone` = ?, `phone_confirmed` = 0 WHERE `id` = ? LIMIT 1", $sPhone, $iID);
  288. return $this->query($sSql);
  289. }
  290. /**
  291. * Get account field by specified field name and value.
  292. * In most cases it is for internal usage only.
  293. * Use other funtions to get account info, like getIdByEmail, etc.
  294. * @param string $sFieldRequested database field name to return
  295. * @param string $sFieldSearch database field name to search for
  296. * @param mixed $sValue database field value
  297. * @return specified account field value
  298. */
  299. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  300. {
  301. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_accounts` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  302. return $this->getOne($sSql);
  303. }
  304. /**
  305. * Update some field by account id
  306. * In most cases it is for internal usage only.
  307. * Use other funtions to get account info, like updateLogged, etc.
  308. * @param string $sFieldRequested database field name to return
  309. * @param string $sFieldSearch database field name to search for
  310. * @param mixed $sValue database field value
  311. * @return specified account field value
  312. */
  313. public function _updateField ($iId, $sFieldForUpdate, $sValue)
  314. {
  315. $sSql = $this->prepare("UPDATE `sys_accounts` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  316. return $this->query($sSql);
  317. }
  318. /**
  319. * Delete account info by id
  320. * @param int $iID profile id
  321. * @return affected rows
  322. */
  323. public function delete($iID)
  324. {
  325. $sSql = $this->prepare("DELETE FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iID);
  326. if ($res = $this->query($sSql)) {
  327. $this->getIdById($iID, true);
  328. }
  329. return $res;
  330. }
  331. /**
  332. * Get account(s) by params
  333. * @param array $aParams browse params
  334. * @return array with account(s)
  335. */
  336. public function getAccounts($aParams)
  337. {
  338. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  339. $sFieldsClause = "`ta`.*";
  340. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = "";
  341. switch($aParams['type']) {
  342. case 'by_join_date':
  343. $aMethod['params'][1] = array(
  344. 'date' => $aParams['date']
  345. );
  346. $sWhereClause = " AND `ta`.`added` > :date AND `ta`.`added` < UNIX_TIMESTAMP()";
  347. break;
  348. case 'confirmed':
  349. $sWhereClause = " AND (`ta`.`email_confirmed` = 1 OR `ta`.`phone_confirmed` = 1)";
  350. break;
  351. case 'unconfirmed':
  352. $sWhereClause = " AND (`ta`.`email_confirmed` = 0 AND `ta`.`phone_confirmed` = 0)";
  353. break;
  354. }
  355. $sGroupClause = $sGroupClause ? "GROUP BY " . $sGroupClause : "";
  356. $sOrderClause = $sOrderClause ? "ORDER BY " . $sOrderClause : "";
  357. $aMethod['params'][0] = "SELECT
  358. " . $sFieldsClause . "
  359. FROM `sys_accounts` AS `ta`" . $sJoinClause . "
  360. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause;
  361. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  362. }
  363. /**
  364. * Search account profile by keyword
  365. */
  366. public function searchByTerm($sTerm, $iLimit)
  367. {
  368. $aBindings = array(
  369. 'system' => 'system',
  370. 'status' => BX_PROFILE_STATUS_ACTIVE,
  371. 'limit' => (int)$iLimit
  372. );
  373. $sWhere = '';
  374. $aFieldsQuickSearch = array('name', 'email');
  375. foreach ($aFieldsQuickSearch as $sField) {
  376. $aBindings[$sField] = '%' . $sTerm . '%';
  377. $sWhere .= " OR `c`.`$sField` LIKE :$sField ";
  378. }
  379. $sQuery = "SELECT `c`.`id` AS `content_id`, `p`.`account_id`, `p`.`id` AS `profile_id`, `p`.`status` AS `profile_status` FROM `sys_accounts` AS `c` INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = :system) WHERE `p`.`status` = :status AND (0 $sWhere) ORDER BY `added` DESC LIMIT :limit";
  380. return $this->getAll($sQuery, $aBindings);
  381. }
  382. public function getAccountsForPruning($sMode, $iAdded)
  383. {
  384. $aBindings = array(
  385. 'date_limit' => $iAdded,
  386. );
  387. $sQuery = '';
  388. switch($sMode) {
  389. case 'no_profile':
  390. $sQuery = "SELECT `sys_accounts`.`id`, COUNT(`sys_profiles`.`id`) AS `profiles_count` FROM `sys_accounts`
  391. INNER JOIN `sys_profiles` ON `sys_accounts`.`id` = `sys_profiles`.`account_id`
  392. WHERE `added` < :date_limit AND name <> 'Robot'
  393. GROUP BY `sys_profiles`.`account_id`
  394. HAVING COUNT(`sys_profiles`.`id`) <= 1 ";
  395. break;
  396. case 'no_login':
  397. $sQuery = "SELECT `sys_accounts`.`id` FROM `sys_accounts` WHERE `logged` < :date_limit AND `added` < :date_limit AND name <> 'Robot' AND `sys_accounts`.`id` NOT IN (SELECT `user_id` FROM `sys_sessions`)";
  398. break;
  399. case 'no_confirm':
  400. $sWhereClause = "SELECT `sys_accounts`.`id` FROM `sys_accounts` WHERE `added` < :date_limit AND (phone_confirmed = 0 OR email_confirmed = 0) AND name <> 'Robot'";
  401. break;
  402. }
  403. return $this->getAll($sQuery, $aBindings);
  404. }
  405. }
  406. /** @} */