BxDolProfileQuery.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. if (!$iContentId)
  69. return false;
  70. $sKey = 'BxDolProfileQuery::getProfileByContentAndType' . $iContentId . $sType;
  71. if ($bClearCache)
  72. $this->cleanMemory($sKey);
  73. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `content_id` = ? AND `type` = ?", $iContentId, $sType);
  74. $mixedResult = $this->fromMemory($sKey, 'getRow', $sSql);
  75. if (!$mixedResult)
  76. $this->cleanMemory($sKey);
  77. return $mixedResult;
  78. }
  79. public function getConnectedProfilesByType ($aSqlParts, $sType, $iStart, $iLimit)
  80. {
  81. if(empty($aSqlParts['join']))
  82. return array();
  83. $aBindings = array();
  84. $sSelectClause = '';
  85. if(!empty($aSqlParts['fields']))
  86. foreach($aSqlParts['fields'] as $sName => $sField)
  87. $sSelectClause .= ', ' . $sField;
  88. $sWhereClause = '';
  89. if(!empty($sType)) {
  90. $sWhereClause = ' AND `sys_profiles`.`type`=:type';
  91. $aBindings['type'] = $sType;
  92. }
  93. $sOrderClause = '';
  94. if(!empty($aSqlParts['fields']['added']))
  95. $sOrderClause = ' ORDER BY ' . $aSqlParts['fields']['added'] . ' DESC';
  96. $sLimitClause = '';
  97. if(!empty($iLimit))
  98. $sLimitClause = $this->prepareAsString(' LIMIT ?, ?', $iStart, $iLimit);
  99. return $this->getAllWithKey("SELECT `sys_profiles`.*" . $sSelectClause . " FROM `sys_profiles` " . $aSqlParts['join'] . " WHERE 1" . $sWhereClause . $sOrderClause . $sLimitClause, 'id', $aBindings);
  100. }
  101. /**
  102. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  103. * @param $iAccountId account id
  104. * @param $iContentId content id
  105. * @param $sStatus profile status
  106. * @param $sType profile content type
  107. * @return inserted profile's id
  108. */
  109. public function insertProfile ($iAccountId, $iContentId, $sStatus, $sType = 'system')
  110. {
  111. $sSql = $this->prepare("INSERT INTO `sys_profiles` SET `account_id` = ?, `type` = ?, `content_id` = ?, `status` = ?", $iAccountId, $sType, $iContentId, $sStatus);
  112. if (!$this->query($sSql))
  113. return false;
  114. $iProfileId = $this->lastId();
  115. $this->getCurrentProfileByAccount($iAccountId); // it updates current profile id automatically if it is not defined
  116. return $iProfileId;
  117. }
  118. /**
  119. * Update profile's status
  120. * @param $iProfileId profile id
  121. * @param $sStatus profile status
  122. * @return true on success or false otherwise
  123. */
  124. public function changeStatus ($iProfileId, $sStatus)
  125. {
  126. return $this->_updateField ($iProfileId, 'status', $sStatus);
  127. }
  128. /**
  129. * Get current account profile.
  130. * @param string $iAccountId account id
  131. * @return current account's profile id
  132. */
  133. public function getCurrentProfileByAccount ($iAccountId, $bClearCache = false)
  134. {
  135. if (!$iAccountId)
  136. return false;
  137. $sKey = 'BxDolProfileQuery::getCurrentProfileByAccount' . $iAccountId;
  138. if ($bClearCache)
  139. $this->cleanMemory($sKey);
  140. $sSql = $this->prepare("SELECT `profile_id` FROM `sys_accounts` AS `a` INNER JOIN `sys_profiles` AS `p` ON (`a`.`profile_id` = `p`.`id`) WHERE `a`.`id` = ? LIMIT 1", $iAccountId);
  141. $iProfileId = $this->fromMemory($sKey, 'getOne', $sSql);
  142. if (!$iProfileId) {
  143. $this->cleanMemory($sKey);
  144. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `account_id` = ? ORDER BY FIELD(`type`, 'system', 'bx_organizations', 'bx_persons') DESC LIMIT 1", $iAccountId);
  145. $iProfileId = $this->getOne($sSql);
  146. if (!$iProfileId)
  147. return false;
  148. if (!BxDolAccountQuery::getInstance()->updateCurrentProfile($iAccountId, $iProfileId))
  149. return false;
  150. }
  151. return $iProfileId;
  152. }
  153. /**
  154. * Get profile by specified field name and value.
  155. * It is for internal usage only.
  156. * Use other funtions to get profile info, like getInfoById, etc.
  157. * @param string $sField database field name
  158. * @param mixed $sValue database field value
  159. * @return array with porfile info
  160. */
  161. protected function _getDataByField ($sField, $sValue)
  162. {
  163. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `$sField` = ? LIMIT 1", $sValue);
  164. return $this->fromMemory('sys_profiles_' . $sField . '_' . $sValue, 'getRow', $sSql);
  165. }
  166. /**
  167. * get profile id by id
  168. */
  169. public function getIdById($iId, $bClearCache = false)
  170. {
  171. if (!$iId)
  172. return false;
  173. $sKey = 'BxDolProfileQuery::getIdById' . $iId;
  174. if ($bClearCache)
  175. $this->cleanMemory($sKey);
  176. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iId);
  177. $mixedResult = $this->fromMemory($sKey, 'getOne', $sSql);
  178. if (!$mixedResult)
  179. $this->cleanMemory($sKey);
  180. return $mixedResult;
  181. }
  182. /**
  183. * get account info by profile id
  184. */
  185. public function getAccountInfoByProfileId($iId)
  186. {
  187. $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);
  188. return $this->getRow($sSql);
  189. }
  190. /**
  191. * get account email by profile id
  192. */
  193. public function getEmailById($iId)
  194. {
  195. $a = $this->getAccountInfoByProfileId($iId);
  196. if (!$a || empty($a['email']))
  197. return false;
  198. return $a['email'];
  199. }
  200. /**
  201. * Get profile info by id
  202. * @param int $iID profile id
  203. * @return array with profile info
  204. */
  205. public function getInfoById( $iID )
  206. {
  207. return $this->_getDataByField('id', (int)$iID);
  208. }
  209. /**
  210. * Is profile online by id
  211. * @param int $iId profile id
  212. * @return profile online status
  213. */
  214. public function isOnline($iId)
  215. {
  216. $sSql = $this->prepare("SELECT
  217. `tp`.`id`
  218. FROM `sys_profiles` AS `tp`
  219. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
  220. INNER JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
  221. WHERE
  222. `tp`.`id` = ? AND
  223. `ta`.`profile_id`=`tp`.`id` AND
  224. `ts`.`date` > (UNIX_TIMESTAMP() - 60 * ?)
  225. LIMIT 1", $iId, (int)getParam('sys_account_online_time'));
  226. return (int)$this->getOne($sSql) > 0;
  227. }
  228. /**
  229. * Get online profiles count
  230. * @return online profiles count
  231. */
  232. public function getOnlineCount()
  233. {
  234. $sSql = $this->prepare("SELECT
  235. COUNT(`tp`.`id`)
  236. FROM `sys_profiles` AS `tp`
  237. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
  238. INNER JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
  239. WHERE
  240. `ta`.`profile_id`=`tp`.`id` AND
  241. `ts`.`date` > (UNIX_TIMESTAMP() - 60 * ?)", (int)getParam('sys_account_online_time'));
  242. return (int)$this->getOne($sSql);
  243. }
  244. public function getProfileQuota($iProfileId)
  245. {
  246. if (!$iProfileId) // for guests and storages without owner don't count per-user quota
  247. return array('current_size' => 0, 'current_number' => 0, 'quota_size' => 0, 'quota_number' => 0, 'max_file_size' => 0);
  248. $sQuery = $this->prepare("SELECT `current_size`, `current_number`, 0 as `quota_size`, 0 as `quota_number`, 0 as `max_file_size` FROM `sys_storage_user_quotas` WHERE `profile_id` = ?", $iProfileId);
  249. $a = $this->getRow($sQuery);
  250. if (!is_array($a) || !$a)
  251. $a = array ('current_size' => 0, 'current_number' => 0, 'quota_size' => 0, 'quota_number' => 0, 'max_file_size' => 0);
  252. // get quota_number and quota_size from user's acl/membership
  253. $aMembershipInfo = BxDolAcl::getInstance()->getMemberMembershipInfo($iProfileId);
  254. if ($aMembershipInfo) {
  255. if (isset($aMembershipInfo['quota_size']))
  256. $a['quota_size'] = $aMembershipInfo['quota_size'];
  257. if (isset($aMembershipInfo['quota_number']))
  258. $a['quota_number'] = $aMembershipInfo['quota_number'];
  259. if (isset($aMembershipInfo['quota_max_file_size']))
  260. $a['max_file_size'] = $aMembershipInfo['quota_max_file_size'];
  261. }
  262. return $a;
  263. }
  264. public function updateProfileQuota($iProfileId, $iSize, $iNumber = 1)
  265. {
  266. if (!$iProfileId) // for guests and storages without owner don't update per-user quota
  267. return true;
  268. $iTime = time();
  269. $sQuery = $this->prepare("
  270. INSERT INTO `sys_storage_user_quotas`
  271. SET `profile_id` = ?, `current_size` = `current_size` + ?, `current_number` = `current_number` + ?, `ts` = ?
  272. ON DUPLICATE KEY UPDATE `current_size` = `current_size` + ?, `current_number` = `current_number` + ?, `ts` = ?",
  273. $iProfileId, $iSize, $iNumber, $iTime, $iSize, $iNumber, $iTime
  274. );
  275. if ($this->query($sQuery))
  276. return true;
  277. else
  278. return false;
  279. }
  280. /**
  281. * Get profile field by specified field name and value.
  282. * In most cases it is for internal usage only.
  283. * Use other funtions to get profile info, like getIdByEmail, etc.
  284. * @param string $sFieldRequested database field name to return
  285. * @param string $sFieldSearch database field name to search for
  286. * @param mixed $sValue database field value
  287. * @return specified profile field value
  288. */
  289. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  290. {
  291. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_profiles` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  292. return $this->getOne($sSql);
  293. }
  294. /**
  295. * Update some field by profile id
  296. * In most cases it is for internal usage only.
  297. * Use other funtions to get profile info, like updateLogged, etc.
  298. * @param string $sFieldRequested database field name to return
  299. * @param string $sFieldSearch database field name to search for
  300. * @param mixed $sValue database field value
  301. * @return specified profile field value
  302. */
  303. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  304. {
  305. $sSql = $this->prepare("UPDATE `sys_profiles` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  306. return $this->query($sSql);
  307. }
  308. /**
  309. * Delete profile info by id
  310. * @param int $iID profile id
  311. * @return affected rows
  312. */
  313. public function delete($iID)
  314. {
  315. $aInfo = $this->getInfoById($iID);
  316. $sSql = $this->prepare("DELETE FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iID);
  317. if ($res = $this->query($sSql)) {
  318. $this->getProfileByContentAndType($aInfo['content_id'], $aInfo['type'], true);
  319. $this->getCurrentProfileByAccount($aInfo['account_id'], true);
  320. $this->getIdById($iID, true);
  321. }
  322. return $res;
  323. }
  324. /**
  325. * Reset deleted profile ids and assign system profile ids.
  326. * Should be called after profiles module deletion.
  327. * It can be called automatically if
  328. * @code
  329. * 'process_deleted_profiles' => 1
  330. * @endcode
  331. * is specified in module config.php file in 'uninstall' section.
  332. */
  333. public function processDeletedProfiles ()
  334. {
  335. $bResult = true;
  336. // reset deleted profiles
  337. $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;
  338. // try to assign another non-system profile to reset accounts
  339. $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;
  340. // assign system profile to reset accounts
  341. $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;
  342. return $bResult;
  343. }
  344. }
  345. /** @} */