BxDolAccountQuery.php 5.9 KB

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