BxDolProfileQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. public function getConnectedProfilesByType ($aSqlParts, $sType, $iStart, $iLimit)
  78. {
  79. if(empty($aSqlParts['join']))
  80. return array();
  81. $aBindings = array();
  82. $sSelectClause = '';
  83. if(!empty($aSqlParts['fields']))
  84. foreach($aSqlParts['fields'] as $sName => $sField)
  85. $sSelectClause .= ', ' . $sField;
  86. $sWhereClause = '';
  87. if(!empty($sType)) {
  88. $sWhereClause = ' AND `sys_profiles`.`type`=:type';
  89. $aBindings['type'] = $sType;
  90. }
  91. $sOrderClause = '';
  92. if(!empty($aSqlParts['fields']['added']))
  93. $sOrderClause = ' ORDER BY ' . $aSqlParts['fields']['added'] . ' DESC';
  94. $sLimitClause = '';
  95. if(!empty($iLimit))
  96. $sLimitClause = $this->prepareAsString(' LIMIT ?, ?', $iStart, $iLimit);
  97. return $this->getAllWithKey("SELECT `sys_profiles`.*" . $sSelectClause . " FROM `sys_profiles` " . $aSqlParts['join'] . " WHERE 1" . $sWhereClause . $sOrderClause . $sLimitClause, 'id', $aBindings);
  98. }
  99. /**
  100. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  101. * @param $iAccountId account id
  102. * @param $iContentId content id
  103. * @param $sStatus profile status
  104. * @param $sType profile content type
  105. * @return inserted profile's id
  106. */
  107. public function insertProfile ($iAccountId, $iContentId, $sStatus, $sType = 'system')
  108. {
  109. $sSql = $this->prepare("INSERT INTO `sys_profiles` SET `account_id` = ?, `type` = ?, `content_id` = ?, `status` = ?", $iAccountId, $sType, $iContentId, $sStatus);
  110. if (!$this->query($sSql))
  111. return false;
  112. $iProfileId = $this->lastId();
  113. $this->getCurrentProfileByAccount($iAccountId); // it updates current profile id automatically if it is not defined
  114. return $iProfileId;
  115. }
  116. /**
  117. * Update profile's status
  118. * @param $iProfileId profile id
  119. * @param $sStatus profile status
  120. * @return true on success or false otherwise
  121. */
  122. public function changeStatus ($iProfileId, $sStatus)
  123. {
  124. return $this->_updateField ($iProfileId, 'status', $sStatus);
  125. }
  126. /**
  127. * Get current account profile.
  128. * @param string $iAccountId account id
  129. * @return current account's profile id
  130. */
  131. public function getCurrentProfileByAccount ($iAccountId, $bClearCache = false)
  132. {
  133. $sKey = 'BxDolProfileQuery::getCurrentProfileByAccount' . $iAccountId;
  134. if ($bClearCache)
  135. $this->cleanMemory($sKey);
  136. $sSql = $this->prepare("SELECT `profile_id` FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iAccountId);
  137. $iProfileId = $this->fromMemory($sKey, 'getOne', $sSql);
  138. if (!$iProfileId) {
  139. $this->cleanMemory($sKey);
  140. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `account_id` = ? LIMIT 1", $iAccountId);
  141. $iProfileId = $this->getOne($sSql);
  142. if (!$iProfileId)
  143. return false;
  144. if (!BxDolAccountQuery::getInstance()->updateCurrentProfile($iAccountId, $iProfileId))
  145. return false;
  146. }
  147. return $iProfileId;
  148. }
  149. /**
  150. * Get profile by specified field name and value.
  151. * It is for internal usage only.
  152. * Use other funtions to get profile info, like getInfoById, etc.
  153. * @param string $sField database field name
  154. * @param mixed $sValue database field value
  155. * @return array with porfile info
  156. */
  157. protected function _getDataByField ($sField, $sValue)
  158. {
  159. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `$sField` = ? LIMIT 1", $sValue);
  160. return $this->getRow($sSql);
  161. }
  162. /**
  163. * get profile id by id
  164. */
  165. public function getIdById($iId, $bClearCache = false)
  166. {
  167. $sKey = 'BxDolProfileQuery::getIdById' . $iId;
  168. if ($bClearCache)
  169. $this->cleanMemory($sKey);
  170. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iId);
  171. $mixedResult = $this->fromMemory($sKey, 'getOne', $sSql);
  172. if (!$mixedResult)
  173. $this->cleanMemory($sKey);
  174. return $mixedResult;
  175. }
  176. /**
  177. * get account info by profile id
  178. */
  179. public function getAccountInfoByProfileId($iId)
  180. {
  181. $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);
  182. return $this->getRow($sSql);
  183. }
  184. /**
  185. * get account email by profile id
  186. */
  187. public function getEmailById($iId)
  188. {
  189. $a = $this->getAccountInfoByProfileId($iId);
  190. if (!$a || empty($a['email']))
  191. return false;
  192. return $a['email'];
  193. }
  194. /**
  195. * Get profile info by id
  196. * @param int $iID profile id
  197. * @return array with profile info
  198. */
  199. public function getInfoById( $iID )
  200. {
  201. return $this->_getDataByField('id', (int)$iID);
  202. }
  203. /**
  204. * Is profile online by id
  205. * @param int $iId profile id
  206. * @return profile online status
  207. */
  208. public function isOnline($iId)
  209. {
  210. $sSql = $this->prepare("SELECT
  211. `tp`.`id`
  212. FROM `sys_profiles` AS `tp`
  213. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
  214. INNER JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
  215. WHERE
  216. `tp`.`id` = ? AND
  217. `ta`.`profile_id`=`tp`.`id` AND
  218. `ts`.`date` > (UNIX_TIMESTAMP() - 60 * ?)
  219. LIMIT 1", $iId, (int)getParam('sys_account_online_time'));
  220. return (int)$this->getOne($sSql) > 0;
  221. }
  222. /**
  223. * Get profile field by specified field name and value.
  224. * In most cases it is for internal usage only.
  225. * Use other funtions to get profile info, like getIdByEmail, etc.
  226. * @param string $sFieldRequested database field name to return
  227. * @param string $sFieldSearch database field name to search for
  228. * @param mixed $sValue database field value
  229. * @return specified profile field value
  230. */
  231. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  232. {
  233. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_profiles` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  234. return $this->getOne($sSql);
  235. }
  236. /**
  237. * Update some field by profile id
  238. * In most cases it is for internal usage only.
  239. * Use other funtions to get profile info, like updateLogged, etc.
  240. * @param string $sFieldRequested database field name to return
  241. * @param string $sFieldSearch database field name to search for
  242. * @param mixed $sValue database field value
  243. * @return specified profile field value
  244. */
  245. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  246. {
  247. $sSql = $this->prepare("UPDATE `sys_profiles` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  248. return $this->query($sSql);
  249. }
  250. /**
  251. * Delete profile info by id
  252. * @param int $iID profile id
  253. * @return affected rows
  254. */
  255. public function delete($iID)
  256. {
  257. $aInfo = $this->getInfoById($iID);
  258. $sSql = $this->prepare("DELETE FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iID);
  259. if ($res = $this->query($sSql)) {
  260. $this->getProfileByContentAndType($aInfo['content_id'], $aInfo['type'], true);
  261. $this->getCurrentProfileByAccount($aInfo['account_id'], true);
  262. $this->getIdById($iID, true);
  263. }
  264. return $res;
  265. }
  266. /**
  267. * Reset deleted profile ids and assign system profile ids.
  268. * Should be called after profiles module deletion.
  269. * It can be called automatically if
  270. * @code
  271. * 'process_deleted_profiles' => 1
  272. * @endcode
  273. * is specified in module config.php file in 'uninstall' section.
  274. */
  275. public function processDeletedProfiles ()
  276. {
  277. $bResult = true;
  278. // reset deleted profiles
  279. $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;
  280. // try to assign another non-system profile to reset accounts
  281. $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;
  282. // assign system profile to reset accounts
  283. $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;
  284. return $bResult;
  285. }
  286. }
  287. /** @} */