BxDolProfileQuery.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 profiles
  11. */
  12. class BxDolProfileQuery 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 BxDolProfileQuery();
  35. return $GLOBALS['bxDolClasses'][__CLASS__];
  36. }
  37. /**
  38. * Get all account profiles.
  39. * @param string $iAccountId account id
  40. * @return profile array
  41. */
  42. public function getProfilesByAccount ($iAccountId)
  43. {
  44. if (!$iAccountId)
  45. return array();
  46. $oAccountQuery = BxDolAccountQuery::getInstance();
  47. $aAccountInfo = $oAccountQuery->getInfoById($iAccountId);
  48. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ? ORDER BY `id` = ? DESC", $iAccountId, $aAccountInfo['profile_id']);
  49. return $this->getAllWithKey($sSql, 'id');
  50. }
  51. /**
  52. * Get profile by content id, type and account.
  53. * @param string $iAccountId account id
  54. * @return array if aprofile ids, key is profile id
  55. */
  56. public function getProfileByContentTypeAccount ($iContentId, $sType, $iAccountId)
  57. {
  58. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ? AND `type` = ? AND `content_id` = ?", $iAccountId, $sType, $iContentId);
  59. return $this->getRow($sSql);
  60. }
  61. /**
  62. * Get profile by content id and type.
  63. * @param string $iAccountId account id
  64. * @return array if aprofile ids, key is profile id
  65. */
  66. public function getProfileByContentAndType ($iContentId, $sType, $bClearCache = false)
  67. {
  68. $sKey = 'BxDolProfileQuery::getProfileByContentAndType' . $iContentId . $sType;
  69. if ($bClearCache)
  70. $this->cleanMemory($sKey);
  71. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `content_id` = ? AND `type` = ?", $iContentId, $sType);
  72. $mixedResult = $this->fromMemory($sKey, 'getRow', $sSql);
  73. if (!$mixedResult)
  74. $this->cleanMemory($sKey);
  75. return $mixedResult;
  76. }
  77. /**
  78. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  79. * @param $iAccountId account id
  80. * @param $iContentId content id
  81. * @param $sStatus profile status
  82. * @param $sType profile content type
  83. * @return inserted profile's id
  84. */
  85. public function insertProfile ($iAccountId, $iContentId, $sStatus, $sType = 'system')
  86. {
  87. $sSql = $this->prepare("INSERT INTO `sys_profiles` SET `account_id` = ?, `type` = ?, `content_id` = ?, `status` = ?", $iAccountId, $sType, $iContentId, $sStatus);
  88. if (!$this->query($sSql))
  89. return false;
  90. $iProfileId = $this->lastId();
  91. $this->getCurrentProfileByAccount($iAccountId); // it updates current profile id automatically if it is not defined
  92. return $iProfileId;
  93. }
  94. /**
  95. * Update profile's status
  96. * @param $iProfileId profile id
  97. * @param $sStatus profile status
  98. * @return true on success or false otherwise
  99. */
  100. public function changeStatus ($iProfileId, $sStatus)
  101. {
  102. return $this->_updateField ($iProfileId, 'status', $sStatus);
  103. }
  104. /**
  105. * Get current account profile.
  106. * @param string $iAccountId account id
  107. * @return current account's profile id
  108. */
  109. public function getCurrentProfileByAccount ($iAccountId, $bClearCache = false)
  110. {
  111. $sKey = 'BxDolProfileQuery::getCurrentProfileByAccount' . $iAccountId;
  112. if ($bClearCache)
  113. $this->cleanMemory($sKey);
  114. $sSql = $this->prepare("SELECT `profile_id` FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iAccountId);
  115. $iProfileId = $this->fromMemory($sKey, 'getOne', $sSql);
  116. if (!$iProfileId) {
  117. $this->cleanMemory($sKey);
  118. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `account_id` = ? LIMIT 1", $iAccountId);
  119. $iProfileId = $this->getOne($sSql);
  120. if (!$iProfileId)
  121. return false;
  122. if (!BxDolAccountQuery::getInstance()->updateCurrentProfile($iAccountId, $iProfileId))
  123. return false;
  124. }
  125. return $iProfileId;
  126. }
  127. /**
  128. * Get profile by specified field name and value.
  129. * It is for internal usage only.
  130. * Use other funtions to get profile info, like getInfoById, etc.
  131. * @param string $sField database field name
  132. * @param mixed $sValue database field value
  133. * @return array with porfile info
  134. */
  135. protected function _getDataByField ($sField, $sValue)
  136. {
  137. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `$sField` = ? LIMIT 1", $sValue);
  138. return $this->getRow($sSql);
  139. }
  140. /**
  141. * get profile id by id
  142. */
  143. public function getIdById($iId, $bClearCache = false)
  144. {
  145. $sKey = 'BxDolProfileQuery::getIdById' . $iId;
  146. if ($bClearCache)
  147. $this->cleanMemory($sKey);
  148. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iId);
  149. $mixedResult = $this->fromMemory($sKey, 'getOne', $sSql);
  150. if (!$mixedResult)
  151. $this->cleanMemory($sKey);
  152. return $mixedResult;
  153. }
  154. /**
  155. * get account info by profile id
  156. */
  157. public function getAccountInfoByProfileId($iId)
  158. {
  159. $sSql = $this->prepare("SELECT `a`.* FROM `sys_accounts` AS `a` INNER JOIN `sys_profiles` AS `p` ON (`p`.`account_id` = `a`.`id`) WHERE `p`.`id` = ? LIMIT 1", $iId);
  160. return $this->getRow($sSql);
  161. }
  162. /**
  163. * get account email by profile id
  164. */
  165. public function getEmailById($iId)
  166. {
  167. $a = $this->getAccountInfoByProfileId($iId);
  168. if (!$a || empty($a['email']))
  169. return false;
  170. return $a['email'];
  171. }
  172. /**
  173. * Get profile info by id
  174. * @param int $iID profile id
  175. * @return array with profile info
  176. */
  177. public function getInfoById( $iID )
  178. {
  179. return $this->_getDataByField('id', (int)$iID);
  180. }
  181. /**
  182. * Is profile online by id
  183. * @param int $iId profile id
  184. * @return profile online status
  185. */
  186. public function isOnline($iId)
  187. {
  188. $sSql = $this->prepare("SELECT
  189. `tp`.`id`
  190. FROM `sys_profiles` AS `tp`
  191. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
  192. INNER JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
  193. WHERE
  194. `tp`.`id` = ? AND
  195. `ta`.`profile_id`=`tp`.`id` AND
  196. `ts`.`date` > (UNIX_TIMESTAMP() - 60 * ?)
  197. LIMIT 1", $iId, (int)getParam('sys_account_online_time'));
  198. return (int)$this->getOne($sSql) > 0;
  199. }
  200. /**
  201. * Get profile field by specified field name and value.
  202. * In most cases it is for internal usage only.
  203. * Use other funtions to get profile info, like getIdByEmail, etc.
  204. * @param string $sFieldRequested database field name to return
  205. * @param string $sFieldSearch database field name to search for
  206. * @param mixed $sValue database field value
  207. * @return specified profile field value
  208. */
  209. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  210. {
  211. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_profiles` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  212. return $this->getOne($sSql);
  213. }
  214. /**
  215. * Update some field by profile id
  216. * In most cases it is for internal usage only.
  217. * Use other funtions to get profile info, like updateLogged, etc.
  218. * @param string $sFieldRequested database field name to return
  219. * @param string $sFieldSearch database field name to search for
  220. * @param mixed $sValue database field value
  221. * @return specified profile field value
  222. */
  223. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  224. {
  225. $sSql = $this->prepare("UPDATE `sys_profiles` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  226. return $this->query($sSql);
  227. }
  228. /**
  229. * Delete profile info by id
  230. * @param int $iID profile id
  231. * @return affected rows
  232. */
  233. public function delete($iID)
  234. {
  235. $aInfo = $this->getInfoById($iID);
  236. $sSql = $this->prepare("DELETE FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iID);
  237. if ($res = $this->query($sSql)) {
  238. $this->getProfileByContentAndType($aInfo['content_id'], $aInfo['type'], true);
  239. $this->getCurrentProfileByAccount($aInfo['account_id'], true);
  240. $this->getIdById($iID, true);
  241. }
  242. return $res;
  243. }
  244. /**
  245. * Reset deleted profile ids and assign system profile ids.
  246. * Should be called after profiles module deletion.
  247. * It can be called automatically if
  248. * @code
  249. * 'process_deleted_profiles' => 1
  250. * @endcode
  251. * is specified in module config.php file in 'uninstall' section.
  252. */
  253. public function processDeletedProfiles ()
  254. {
  255. $bResult = true;
  256. // reset deleted profiles
  257. $bResult &= $this->query("UPDATE `sys_accounts` AS `a` LEFT OUTER JOIN `sys_profiles` AS `p` ON `a`.`profile_id`=`p`.`id` SET `a`.`profile_id`='0' WHERE `p`.`id` IS NULL") !== false;
  258. // try to assign another non-system profile to reset accounts
  259. $bResult &= $this->query("UPDATE `sys_accounts` AS `a` INNER JOIN `sys_profiles` AS `p` ON `a`.`id`=`p`.`account_id` AND `p`.`type`<>'system' AND `a`.`profile_id`='0' SET `a`.`profile_id`=`p`.`id`") !== false;
  260. // assign system profile to reset accounts
  261. $bResult &= $this->query("UPDATE `sys_accounts` AS `a` INNER JOIN `sys_profiles` AS `p` ON `a`.`id`=`p`.`account_id` AND `p`.`type`='system' AND `a`.`profile_id`='0' SET `a`.`profile_id`=`p`.`id`") !== false;
  262. return $bResult;
  263. }
  264. }
  265. /** @} */