BxDolAccountQuery.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * All queries related to account
  11. */
  12. class BxDolAccountQuery extends BxDolDb implements iBxDolSingleton
  13. {
  14. public 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. /**
  38. * Get account by specified field name and value.
  39. * It is for internal usage only.
  40. * Use other funtions to get account info, like getInfoById, etc.
  41. * @param string $sField database field name
  42. * @param mixed $sValue database field value
  43. * @return array with porfile info
  44. */
  45. protected function _getDataByField ($sField, $sValue)
  46. {
  47. $sSql = $this->prepare("SELECT * FROM `sys_accounts` WHERE `$sField` = ? LIMIT 1", $sValue);
  48. return $this->getRow($sSql);
  49. }
  50. /**
  51. * Get account info by id
  52. * @param int $iID account id
  53. * @return array with account info
  54. */
  55. public function getInfoById( $iID )
  56. {
  57. return $this->_getDataByField('id', (int)$iID);
  58. }
  59. /**
  60. * get account id by emial
  61. */
  62. public function getIdByEmail($sEmail)
  63. {
  64. return (int)$this->_getFieldByField('id', 'email', $sEmail);
  65. }
  66. /**
  67. * get account id by id
  68. */
  69. public function getIdById($iId)
  70. {
  71. return (int)$this->_getFieldByField('id', 'id', $iId);
  72. }
  73. /**
  74. * get first studio operator id
  75. */
  76. public function getStudioOperatorId()
  77. {
  78. return (int)$this->_getFieldByField('id', 'role', 3);
  79. }
  80. /**
  81. * Get account email by id
  82. * @param string $s search account by this id
  83. * @return account email
  84. */
  85. public function getEmail($iID)
  86. {
  87. return $this->_getFieldByField('email', 'id', (int)$iID);
  88. }
  89. /**
  90. * Get account password by id
  91. * @param string $s search account by this id
  92. * @return account password
  93. */
  94. public function getPassword($iID)
  95. {
  96. return $this->_getFieldByField('password', 'id', (int)$iID);
  97. }
  98. /**
  99. * Update last logged in time
  100. * @param $sPasswordHash - password hash
  101. * @param $sSalt - pasword salt
  102. * @param $iAccountId - account id to update password for
  103. * @return number of affected rows
  104. */
  105. public function updatePassword($sPasswordHash, $sSalt, $iAccountId)
  106. {
  107. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `password` = ?, `salt` = ? WHERE `id`= ?", $sPasswordHash, $sSalt, $iAccountId);
  108. return $this->query($sQuery);
  109. }
  110. /**
  111. * Update last logged in time
  112. * @param int $iID account id
  113. * @return number of affected rows
  114. */
  115. public function updateLoggedIn($iID)
  116. {
  117. return $this->_updateField ($iID, 'logged', time());
  118. }
  119. /**
  120. * Update language
  121. * @param int $iID account id
  122. * @return number of affected rows
  123. */
  124. public function updateLanguage($iID, $iLangId)
  125. {
  126. return $this->_updateField ($iID, 'lang_id', $iLangId);
  127. }
  128. /**
  129. * Update current profile id associated with account
  130. * @param int $iID account id
  131. * @param int $iProfileId set current profile id to this value
  132. * @return number of affected rows
  133. */
  134. public function updateCurrentProfile($iID, $iProfileId)
  135. {
  136. return $this->_updateField ($iID, 'profile_id', $iProfileId);
  137. }
  138. /**
  139. * Update 'email_confirmed' field.
  140. * @param int $isConfirmed - 0: mark email as unconfirmed, 1: as confirmed
  141. * @param int $iID - account id
  142. * @return number of affected rows
  143. */
  144. public function updateEmailConfirmed($isConfirmed, $iID)
  145. {
  146. return $this->_updateField ($iID, 'email_confirmed', $isConfirmed ? 1 : 0);
  147. }
  148. /**
  149. * Get account field by specified field name and value.
  150. * In most cases it is for internal usage only.
  151. * Use other funtions to get account info, like getIdByEmail, etc.
  152. * @param string $sFieldRequested database field name to return
  153. * @param string $sFieldSearch database field name to search for
  154. * @param mixed $sValue database field value
  155. * @return specified account field value
  156. */
  157. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  158. {
  159. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_accounts` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  160. return $this->getOne($sSql);
  161. }
  162. /**
  163. * Update some field by account id
  164. * In most cases it is for internal usage only.
  165. * Use other funtions to get account info, like updateLogged, etc.
  166. * @param string $sFieldRequested database field name to return
  167. * @param string $sFieldSearch database field name to search for
  168. * @param mixed $sValue database field value
  169. * @return specified account field value
  170. */
  171. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  172. {
  173. $sSql = $this->prepare("UPDATE `sys_accounts` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  174. return $this->query($sSql);
  175. }
  176. /**
  177. * Delete account info by id
  178. * @param int $iID profile id
  179. * @return affected rows
  180. */
  181. public function delete($iID)
  182. {
  183. $sSql = $this->prepare("DELETE FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iID);
  184. return $this->query($sSql);
  185. }
  186. /**
  187. * Search account profile by keyword
  188. */
  189. public function searchByTerm($sTerm, $iLimit)
  190. {
  191. $aFieldsQuickSearch = array('name', 'email');
  192. $sWhere = '';
  193. foreach ($aFieldsQuickSearch as $sField)
  194. $sWhere .= $this->prepare(" OR `c`.`$sField` LIKE ? ", '%' . $sTerm . '%');
  195. $sOrderBy = $this->prepare(" ORDER BY `added` DESC LIMIT ?", (int)$iLimit);
  196. $sQuery = $this->prepare("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` = ?) WHERE `p`.`status` = ?", 'system', BX_PROFILE_STATUS_ACTIVE);
  197. return $this->getAll($sQuery . " AND (0 $sWhere) " . $sOrderBy);
  198. }
  199. }
  200. /** @} */