1
0

BxDolAccountQuery.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 first studio operator id
  76. */
  77. public function getStudioOperatorId()
  78. {
  79. return (int)$this->_getFieldByField('id', 'role', 3);
  80. }
  81. /**
  82. * Get account email by id
  83. * @param string $s search account by this id
  84. * @return account email
  85. */
  86. public function getEmail($iID)
  87. {
  88. return $this->_getFieldByField('email', 'id', (int)$iID);
  89. }
  90. /**
  91. * Get account password by id
  92. * @param string $s search account by this id
  93. * @return account password
  94. */
  95. public function getPassword($iID)
  96. {
  97. return $this->_getFieldByField('password', 'id', (int)$iID);
  98. }
  99. /**
  100. * Update last logged in time
  101. * @param $sPasswordHash - password hash
  102. * @param $sSalt - pasword salt
  103. * @param $iAccountId - account id to update password for
  104. * @return number of affected rows
  105. */
  106. public function updatePassword($sPasswordHash, $sSalt, $iAccountId)
  107. {
  108. $sQuery = $this->prepare("UPDATE `sys_accounts` SET `password` = ?, `salt` = ? WHERE `id`= ?", $sPasswordHash, $sSalt, $iAccountId);
  109. return $this->query($sQuery);
  110. }
  111. /**
  112. * Update last logged in time
  113. * @param int $iID account id
  114. * @return number of affected rows
  115. */
  116. public function updateLoggedIn($iID)
  117. {
  118. return $this->_updateField ($iID, 'logged', time());
  119. }
  120. /**
  121. * Update language
  122. * @param int $iID account id
  123. * @return number of affected rows
  124. */
  125. public function updateLanguage($iID, $iLangId)
  126. {
  127. return $this->_updateField ($iID, 'lang_id', $iLangId);
  128. }
  129. /**
  130. * Update current profile id associated with account
  131. * @param int $iID account id
  132. * @param int $iProfileId set current profile id to this value
  133. * @return number of affected rows
  134. */
  135. public function updateCurrentProfile($iID, $iProfileId)
  136. {
  137. return $this->_updateField ($iID, 'profile_id', $iProfileId);
  138. }
  139. /**
  140. * Update 'email_confirmed' field.
  141. * @param int $isConfirmed - 0: mark email as unconfirmed, 1: as confirmed
  142. * @param int $iID - account id
  143. * @return number of affected rows
  144. */
  145. public function updateEmailConfirmed($isConfirmed, $iID)
  146. {
  147. return $this->_updateField ($iID, 'email_confirmed', $isConfirmed ? 1 : 0);
  148. }
  149. /**
  150. * Get account field by specified field name and value.
  151. * In most cases it is for internal usage only.
  152. * Use other funtions to get account info, like getIdByEmail, etc.
  153. * @param string $sFieldRequested database field name to return
  154. * @param string $sFieldSearch database field name to search for
  155. * @param mixed $sValue database field value
  156. * @return specified account field value
  157. */
  158. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  159. {
  160. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_accounts` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  161. return $this->getOne($sSql);
  162. }
  163. /**
  164. * Update some field by account id
  165. * In most cases it is for internal usage only.
  166. * Use other funtions to get account info, like updateLogged, etc.
  167. * @param string $sFieldRequested database field name to return
  168. * @param string $sFieldSearch database field name to search for
  169. * @param mixed $sValue database field value
  170. * @return specified account field value
  171. */
  172. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  173. {
  174. $sSql = $this->prepare("UPDATE `sys_accounts` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  175. return $this->query($sSql);
  176. }
  177. /**
  178. * Delete account info by id
  179. * @param int $iID profile id
  180. * @return affected rows
  181. */
  182. public function delete($iID)
  183. {
  184. $sSql = $this->prepare("DELETE FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iID);
  185. return $this->query($sSql);
  186. }
  187. /**
  188. * Search account profile by keyword
  189. */
  190. public function searchByTerm($sTerm, $iLimit)
  191. {
  192. $aFieldsQuickSearch = array('name', 'email');
  193. $sWhere = '';
  194. foreach ($aFieldsQuickSearch as $sField)
  195. $sWhere .= $this->prepare(" OR `c`.`$sField` LIKE ? ", '%' . $sTerm . '%');
  196. $sOrderBy = $this->prepare(" ORDER BY `added` DESC LIMIT ?", (int)$iLimit);
  197. $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);
  198. return $this->getAll($sQuery . " AND (0 $sWhere) " . $sOrderBy);
  199. }
  200. }
  201. /** @} */