BxDolAccountQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. $sKey = 'BxDolAccountQuery::getIdById' . $iId;
  84. if ($bClearCache)
  85. $this->cleanMemory($sKey);
  86. $sSql = $this->prepare("SELECT `id` FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iId);
  87. $bResult = $this->fromMemory($sKey, 'getOne', $sSql);
  88. if (!$bResult)
  89. $this->cleanMemory($sKey);
  90. return $bResult;
  91. }
  92. /**
  93. * get first studio operator id
  94. */
  95. public function getStudioOperatorId()
  96. {
  97. return (int)$this->_getFieldByField('id', 'role', 3);
  98. }
  99. /**
  100. * Get account email by id
  101. * @param string $s search account by this id
  102. * @return account email
  103. */
  104. public function getEmail($iID)
  105. {
  106. return $this->_getFieldByField('email', 'id', (int)$iID);
  107. }
  108. /**
  109. * Get account password by id
  110. * @param string $s search account by this id
  111. * @return account password
  112. */
  113. public function getPassword($iID)
  114. {
  115. return $this->_getFieldByField('password', 'id', (int)$iID);
  116. }
  117. /**
  118. * Is account online by id
  119. * @param int $iId account id
  120. * @return account online status
  121. */
  122. public function isOnline($iId)
  123. {
  124. $sSql = $this->prepare("SELECT
  125. `ta`.`id`
  126. FROM `sys_accounts` AS `ta`
  127. INNER JOIN `sys_sessions` AS `ts` ON `ta`.`id`=`ts`.`user_id`
  128. WHERE
  129. `ta`.`id` = ? AND
  130. `ts`.`date` > (UNIX_TIMESTAMP() - 60 * ?)
  131. LIMIT 1", $iId, (int)getParam('sys_account_online_time'));
  132. return (int)$this->getOne($sSql) > 0;
  133. }
  134. /**
  135. * Update last logged in time
  136. * @param $sPasswordHash - password hash
  137. * @param $sSalt - pasword salt
  138. * @param $iAccountId - account id to update password for
  139. * @return number of affected rows
  140. */
  141. public function updatePassword($sPasswordHash, $sSalt, $iAccountId)
  142. {
  143. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `password` = ?, `salt` = ? WHERE `id`= ?", $sPasswordHash, $sSalt, $iAccountId);
  144. return $this->query($sQuery);
  145. }
  146. /**
  147. * Update last logged in time
  148. * @param int $iID account id
  149. * @return number of affected rows
  150. */
  151. public function updateLoggedIn($iID)
  152. {
  153. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `logged` = ?, `ip` = ?, `login_attempts` = 0 WHERE `id`= ?", time(), getVisitorIP(), $iID);
  154. return $this->query($sQuery);
  155. }
  156. /**
  157. * Update login attempts counter
  158. * @param int $iID account id
  159. * @return number of affected rows
  160. */
  161. public function updateAttemptsCounter($iID)
  162. {
  163. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `login_attempts` = `login_attempts` + 1 WHERE `id`= ?", $iID);
  164. return $this->query($sQuery);
  165. }
  166. /**
  167. * Lock account
  168. * @param int $iID account id
  169. * @return number of affected rows
  170. */
  171. public function lockAccount($iID)
  172. {
  173. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `locked` = 1 WHERE `id`= ?", $iID);
  174. return $this->query($sQuery);
  175. }
  176. /**
  177. * Unlock account
  178. * @param int $iID account id
  179. * @return number of affected rows
  180. */
  181. public function unlockAccount($iID)
  182. {
  183. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `locked` = 0, `login_attempts` = 0 WHERE `id`= ?", $iID);
  184. return $this->query($sQuery);
  185. }
  186. /**
  187. * Update language
  188. * @param int $iID account id
  189. * @return number of affected rows
  190. */
  191. public function updateLanguage($iID, $iLangId)
  192. {
  193. return $this->_updateField ($iID, 'lang_id', $iLangId);
  194. }
  195. /**
  196. * Update current profile id associated with account
  197. * @param int $iID account id
  198. * @param int $iProfileId set current profile id to this value
  199. * @return number of affected rows
  200. */
  201. public function updateCurrentProfile($iID, $iProfileId)
  202. {
  203. if ($bResult = $this->_updateField ($iID, 'profile_id', $iProfileId))
  204. $this->cleanMemory('BxDolProfileQuery::getCurrentProfileByAccount');
  205. return $bResult;
  206. }
  207. /**
  208. * Update 'email_confirmed' field.
  209. * @param int $isConfirmed - 0: mark email as unconfirmed, 1: as confirmed
  210. * @param int $iID - account id
  211. * @return number of affected rows
  212. */
  213. public function updateEmailConfirmed($isConfirmed, $iID)
  214. {
  215. return $this->_updateField ($iID, 'email_confirmed', $isConfirmed ? 1 : 0);
  216. }
  217. /**
  218. * Update 'phone_confirmed' field.
  219. * @param int $isConfirmed - 0: mark phone as unconfirmed, 1: as confirmed
  220. * @param int $iID - account id
  221. * @return number of affected rows
  222. */
  223. public function updatePhoneConfirmed($isConfirmed, $iID)
  224. {
  225. return $this->_updateField ($iID, 'phone_confirmed', $isConfirmed ? 1 : 0);
  226. }
  227. /**
  228. * Update 'phone' field.
  229. * @param int $sPhone - phone number
  230. * @param int $iID - account id
  231. * @return number of affected rows
  232. */
  233. public function updatePhone($sPhone, $iID)
  234. {
  235. $sSql = $this->prepare("UPDATE `sys_accounts` SET `phone` = ?, `phone_confirmed` = 0 WHERE `id` = ? LIMIT 1", $sPhone, $iID);
  236. return $this->query($sSql);
  237. }
  238. /**
  239. * Get account field by specified field name and value.
  240. * In most cases it is for internal usage only.
  241. * Use other funtions to get account info, like getIdByEmail, etc.
  242. * @param string $sFieldRequested database field name to return
  243. * @param string $sFieldSearch database field name to search for
  244. * @param mixed $sValue database field value
  245. * @return specified account field value
  246. */
  247. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  248. {
  249. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_accounts` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  250. return $this->getOne($sSql);
  251. }
  252. /**
  253. * Update some field by account id
  254. * In most cases it is for internal usage only.
  255. * Use other funtions to get account info, like updateLogged, etc.
  256. * @param string $sFieldRequested database field name to return
  257. * @param string $sFieldSearch database field name to search for
  258. * @param mixed $sValue database field value
  259. * @return specified account field value
  260. */
  261. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  262. {
  263. $sSql = $this->prepare("UPDATE `sys_accounts` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  264. return $this->query($sSql);
  265. }
  266. /**
  267. * Delete account info by id
  268. * @param int $iID profile id
  269. * @return affected rows
  270. */
  271. public function delete($iID)
  272. {
  273. $sSql = $this->prepare("DELETE FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iID);
  274. if ($res = $this->query($sSql)) {
  275. $this->getIdById($iID, true);
  276. }
  277. return $res;
  278. }
  279. /**
  280. * Get account(s) by params
  281. * @param array $aParams browse params
  282. * @return array with account(s)
  283. */
  284. public function getAccounts($aParams)
  285. {
  286. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  287. $sFieldsClause = "`ta`.*";
  288. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = "";
  289. switch($aParams['type']) {
  290. case 'by_join_date':
  291. $aMethod['params'][1] = array(
  292. 'date' => $aParams['date']
  293. );
  294. $sWhereClause = " AND `ta`.`added` > :date AND `ta`.`added` < UNIX_TIMESTAMP()";
  295. break;
  296. case 'confirmed':
  297. $sWhereClause = " AND (`ta`.`email_confirmed` = 1 OR `ta`.`phone_confirmed` = 1)";
  298. break;
  299. case 'unconfirmed':
  300. $sWhereClause = " AND (`ta`.`email_confirmed` = 0 AND `ta`.`phone_confirmed` = 0)";
  301. break;
  302. }
  303. $sGroupClause = $sGroupClause ? "GROUP BY " . $sGroupClause : "";
  304. $sOrderClause = $sOrderClause ? "ORDER BY " . $sOrderClause : "";
  305. $aMethod['params'][0] = "SELECT
  306. " . $sFieldsClause . "
  307. FROM `sys_accounts` AS `ta`" . $sJoinClause . "
  308. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause;
  309. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  310. }
  311. /**
  312. * Search account profile by keyword
  313. */
  314. public function searchByTerm($sTerm, $iLimit)
  315. {
  316. $aBindings = array(
  317. 'system' => 'system',
  318. 'status' => BX_PROFILE_STATUS_ACTIVE,
  319. 'limit' => (int)$iLimit
  320. );
  321. $sWhere = '';
  322. $aFieldsQuickSearch = array('name', 'email');
  323. foreach ($aFieldsQuickSearch as $sField) {
  324. $aBindings[$sField] = '%' . $sTerm . '%';
  325. $sWhere .= " OR `c`.`$sField` LIKE :$sField ";
  326. }
  327. $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";
  328. return $this->getAll($sQuery, $aBindings);
  329. }
  330. }
  331. /** @} */