BxDolAccountQuery.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * All queries related to account
  12. */
  13. class BxDolAccountQuery extends BxDolDb implements iBxDolSingleton
  14. {
  15. public function __construct()
  16. {
  17. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  18. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  19. parent::__construct();
  20. }
  21. /**
  22. * Prevent cloning the instance
  23. */
  24. public function __clone()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. }
  29. /**
  30. * Get singleton instance of the class
  31. */
  32. public static function getInstance()
  33. {
  34. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  35. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolAccountQuery();
  36. return $GLOBALS['bxDolClasses'][__CLASS__];
  37. }
  38. /**
  39. * Get account by specified field name and value.
  40. * It is for internal usage only.
  41. * Use other funtions to get account info, like getInfoById, etc.
  42. * @param string $sField database field name
  43. * @param mixed $sValue database field value
  44. * @return array with porfile info
  45. */
  46. protected function _getDataByField ($sField, $sValue)
  47. {
  48. $sSql = $this->prepare("SELECT * FROM `sys_accounts` WHERE `$sField` = ? LIMIT 1", $sValue);
  49. return $this->getRow($sSql);
  50. }
  51. /**
  52. * Get account info by id
  53. * @param int $iID account id
  54. * @return array with account info
  55. */
  56. public function getInfoById( $iID )
  57. {
  58. return $this->_getDataByField('id', (int)$iID);
  59. }
  60. /**
  61. * get account id by emial
  62. */
  63. public function getIdByEmail($sEmail)
  64. {
  65. return (int)$this->_getFieldByField('id', 'email', $sEmail);
  66. }
  67. /**
  68. * get account id by id
  69. */
  70. public function getIdById($iId)
  71. {
  72. return (int)$this->_getFieldByField('id', 'id', $iId);
  73. }
  74. /**
  75. * Get account email by id
  76. * @param string $s search account by this id
  77. * @return account email
  78. */
  79. public function getEmail($iID)
  80. {
  81. return $this->_getFieldByField('email', 'id', (int)$iID);
  82. }
  83. /**
  84. * Get account password by id
  85. * @param string $s search account by this id
  86. * @return account password
  87. */
  88. public function getPassword($iID)
  89. {
  90. return $this->_getFieldByField('password', 'id', (int)$iID);
  91. }
  92. /**
  93. * Update last logged in time
  94. * @param $sPasswordHash - password hash
  95. * @param $sSalt - pasword salt
  96. * @param $iAccountId - account id to update password for
  97. * @return number of affected rows
  98. */
  99. public function updatePassword($sPasswordHash, $sSalt, $iAccountId)
  100. {
  101. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `password` = ?, `salt` = ? WHERE `id`= ?", $sPasswordHash, $sSalt, $iAccountId);
  102. return $this->query($sQuery);
  103. }
  104. /**
  105. * Update last logged in time
  106. * @param int $iID account id
  107. * @return number of affected rows
  108. */
  109. public function updateLoggedIn($iID)
  110. {
  111. return $this->_updateField ($iID, 'logged', time());
  112. }
  113. /**
  114. * Update language
  115. * @param int $iID account id
  116. * @return number of affected rows
  117. */
  118. public function updateLanguage($iID, $iLangId)
  119. {
  120. return $this->_updateField ($iID, 'lang_id', $iLangId);
  121. }
  122. /**
  123. * Update current profile id associated with account
  124. * @param int $iID account id
  125. * @param int $iProfileId set current profile id to this value
  126. * @return number of affected rows
  127. */
  128. public function updateCurrentProfile($iID, $iProfileId)
  129. {
  130. return $this->_updateField ($iID, 'profile_id', $iProfileId);
  131. }
  132. /**
  133. * Update 'email_confirmed' field.
  134. * @param int $isConfirmed - 0: mark email as unconfirmed, 1: as confirmed
  135. * @param int $iID - account id
  136. * @return number of affected rows
  137. */
  138. public function updateEmailConfirmed($isConfirmed, $iID)
  139. {
  140. return $this->_updateField ($iID, 'email_confirmed', $isConfirmed ? 1 : 0);
  141. }
  142. /**
  143. * Get account field by specified field name and value.
  144. * In most cases it is for internal usage only.
  145. * Use other funtions to get account info, like getIdByEmail, etc.
  146. * @param string $sFieldRequested database field name to return
  147. * @param string $sFieldSearch database field name to search for
  148. * @param mixed $sValue database field value
  149. * @return specified account field value
  150. */
  151. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  152. {
  153. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_accounts` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  154. return $this->getOne($sSql);
  155. }
  156. /**
  157. * Update some field by account id
  158. * In most cases it is for internal usage only.
  159. * Use other funtions to get account info, like updateLogged, etc.
  160. * @param string $sFieldRequested database field name to return
  161. * @param string $sFieldSearch database field name to search for
  162. * @param mixed $sValue database field value
  163. * @return specified account field value
  164. */
  165. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  166. {
  167. $sSql = $this->prepare("UPDATE `sys_accounts` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  168. return $this->query($sSql);
  169. }
  170. /**
  171. * Delete account info by id
  172. * @param int $iID profile id
  173. * @return affected rows
  174. */
  175. public function delete($iID)
  176. {
  177. $sSql = $this->prepare("DELETE FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iID);
  178. return $this->query($sSql);
  179. }
  180. /**
  181. * Search account profile by keyword
  182. */
  183. public function searchByTerm($sTerm, $iLimit)
  184. {
  185. $aFieldsQuickSearch = array('name', 'email');
  186. $sWhere = '';
  187. foreach ($aFieldsQuickSearch as $sField)
  188. $sWhere .= $this->prepare(" OR `c`.`$sField` LIKE ? ", '%' . $sTerm . '%');
  189. $sOrderBy = $this->prepare(" ORDER BY `added` DESC LIMIT ?", (int)$iLimit);
  190. $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);
  191. return $this->getAll($sQuery . " AND (0 $sWhere) " . $sOrderBy);
  192. }
  193. }
  194. /** @} */