BxDolLiveUpdates.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_oQuery;
  12. /*
  13. * List of all systems (active and inactive) with full description.
  14. */
  15. protected $_aSystems;
  16. /*
  17. * List of active systems without any additional info.
  18. */
  19. protected $_aSystemsActive;
  20. /*
  21. * List of dynamically added transient systems without any additional info.
  22. * It depends on the page where a user is.
  23. */
  24. protected $_aSystemsTransient;
  25. protected $_iProfileId;
  26. protected $_iInterval;
  27. protected $_oCacheObject;
  28. protected $_sCacheKeySystems;
  29. protected $_sCacheKeyData;
  30. protected $_iCacheTTL;
  31. protected $_sJsClass;
  32. protected $_sJsObject;
  33. protected function __construct()
  34. {
  35. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  36. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  37. parent::__construct();
  38. /**
  39. * Note. Currently Live Updates are associated with profiles (Profile ID) and therefore they are used for logged in members only.
  40. * If it's needed Session ID can be used instead of Profile ID. In this case Live Updates can be used for visitors too. Don't forget
  41. * to update BxBaseLiveUpdates::init if Session ID will be used.
  42. */
  43. $this->_iProfileId = (int)bx_get_logged_profile_id();
  44. if(!$this->_iProfileId)
  45. return;
  46. $this->_oQuery = new BxDolLiveUpdatesQuery();
  47. $this->_iInterval = (int)$this->_oQuery->getParam('sys_live_updates_interval');
  48. $this->_oCacheObject = $this->_oQuery->getDbCacheObject();
  49. $this->_sCacheKeySystems = 'sys_live_updates_systems';
  50. $this->_sCacheKeyData = 'sys_live_updates_data_' . $this->_iProfileId;
  51. $this->_iCacheTTL = 86400;
  52. $this->_sJsClass = 'BxDolLiveUpdates';
  53. $this->_sJsObject = 'oLiveUpdates';
  54. $this->_aSystems = $this->_getCachedSystems();
  55. $this->_aSystemsActive = array_keys($this->_aSystems);
  56. $this->_aSystemsTransient = array();
  57. }
  58. public function __clone()
  59. {
  60. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  61. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  62. }
  63. /**
  64. * get live updates instanse
  65. * @return ready to use class instance
  66. */
  67. public static function getInstance()
  68. {
  69. if(!isLogged())
  70. return false;
  71. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  72. $GLOBALS['bxDolClasses'][__CLASS__] = new BxTemplLiveUpdates();
  73. return $GLOBALS['bxDolClasses'][__CLASS__];
  74. }
  75. /**
  76. *
  77. * Is called via Ajax Requests only.
  78. */
  79. public function perform()
  80. {
  81. $iIndex = (int)bx_get('index');
  82. $mixedCacheKey = bx_get('hash');
  83. if($mixedCacheKey !== false)
  84. $this->_sCacheKeySystems = $this->_decodeHash(bx_process_input($mixedCacheKey));
  85. $mixedSystemsActive = bx_get('systems_active');
  86. if($mixedSystemsActive !== false)
  87. $this->_aSystemsActive = array_keys($mixedSystemsActive);
  88. $mixedSystemsTransient = bx_get('systems_transient');
  89. if($mixedSystemsTransient !== false)
  90. $this->_aSystemsTransient = array_keys($mixedSystemsTransient);
  91. $this->_aSystems = $this->_getCachedSystems();
  92. if(empty($this->_aSystems) || !is_array($this->_aSystems))
  93. return array();
  94. $aCurrent = $mixedSystemsActive;
  95. if(empty($aCurrent) || !is_array($aCurrent))
  96. $aCurrent = $this->_getCachedData();
  97. $aRequested = $this->_getRequestedData($iIndex, true, $aCurrent);
  98. $aResult = array();
  99. $bUpdateCache = false;
  100. foreach($aRequested as $sName => $aData) {
  101. if(isset($aCurrent[$sName])) {
  102. if((int)$aCurrent[$sName] == (int)$aData['count'])
  103. continue;
  104. $aResultData = array('count_new' => (int)$aData['count'], 'count_old' => (int)$aCurrent[$sName]);
  105. if(isset($aData['data']))
  106. $aResultData = array_merge($aResultData, $aData['data']);
  107. $aResult[] = array(
  108. 'system' => $sName,
  109. 'data' => $aResultData,
  110. 'method' => $aData['method']
  111. );
  112. }
  113. $aCurrent[$sName] = $aData['count'];
  114. $bUpdateCache = true;
  115. }
  116. if($bUpdateCache)
  117. $this->_updateCached('data', $aCurrent);
  118. return $aResult;
  119. }
  120. public function performApi($bInit = false)
  121. {
  122. $aCurrent = $this->_getCachedData($bInit);
  123. if($bInit)
  124. $aCurrent = array_intersect_key($aCurrent, array_flip($this->_aSystemsActive));
  125. $aRequested = $this->_getRequestedData(0, !$bInit, !$bInit ? $aCurrent : []);
  126. $bUpdateCache = false;
  127. foreach($aCurrent as $sName => &$iCount)
  128. if(($iRequestedCount = (int)($aRequested[$sName]['count'] ?? 0)) && $iRequestedCount != (int)$iCount) {
  129. $iCount = $iRequestedCount;
  130. $bUpdateCache = true;
  131. }
  132. if($bUpdateCache)
  133. $this->_updateCached('data', $aCurrent);
  134. return $aCurrent;
  135. }
  136. protected function _addSystem($sName, $iFrequency, $sServiceCall, $mixedActive = true)
  137. {
  138. if(!$this->_iProfileId)
  139. return false;
  140. if(empty($this->_aSystemsTransient))
  141. $this->_sCacheKeySystems .= $this->_getPageId();
  142. if(!in_array($sName, $this->_aSystemsTransient))
  143. $this->_aSystemsTransient[] = $sName;
  144. if(!in_array($sName, $this->_aSystemsActive))
  145. $this->_aSystemsActive[] = $sName;
  146. if(empty($this->_aSystems[$sName])) {
  147. list($bActive, $bInit) = is_array($mixedActive) ? $mixedActive : [$mixedActive, true];
  148. $this->_aSystems[$sName] = [
  149. 'name' => $sName,
  150. 'init' => $bInit ? 1 : 0,
  151. 'frequency' => $iFrequency,
  152. 'service_call' => $sServiceCall,
  153. 'active' => $bActive ? 1 : 0
  154. ];
  155. $this->_updateCached('systems', $this->_aSystems);
  156. }
  157. return true;
  158. }
  159. protected function _addData($sName, $iValue)
  160. {
  161. $aCachedData = $this->_getCachedData();
  162. $aCachedData[$sName] = $iValue;
  163. $this->_updateCached('data', $aCachedData);
  164. return true;
  165. }
  166. protected function _getCacheInfo($sType)
  167. {
  168. return array(
  169. $this->_oCacheObject,
  170. $this->_getCacheKey($sType),
  171. $this->_iCacheTTL
  172. );
  173. }
  174. protected function _getCacheKey($sType)
  175. {
  176. return $this->_oQuery->genDbCacheKey($this->{'_sCacheKey' . ucfirst($sType)});
  177. }
  178. protected function _getCached($sType)
  179. {
  180. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo($sType);
  181. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  182. if(empty($aCached)) {
  183. switch($sType) {
  184. case 'systems':
  185. $aCached = $this->_oQuery->getSystems();
  186. break;
  187. case 'data':
  188. if(empty($aCached))
  189. $aCached = array();
  190. $aRequested = $this->_getRequestedData();
  191. foreach($this->_aSystems as $sName => $aSystem)
  192. $aCached[$sName] = !empty($aRequested[$sName]['count']) ? (int)$aRequested[$sName]['count'] : 0;
  193. break;
  194. }
  195. if(!empty($aCached))
  196. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  197. }
  198. return $aCached;
  199. }
  200. protected function _getCachedSystems()
  201. {
  202. $aSystems = $this->_getCached('systems');
  203. if(!empty($aSystems) && !empty($this->_aSystemsActive))
  204. $aSystems = array_intersect_key($aSystems, array_flip($this->_aSystemsActive));
  205. return $aSystems;
  206. }
  207. protected function _getCachedData($bInit = false)
  208. {
  209. $aData = $this->_getCached('data');
  210. if($bInit)
  211. foreach($this->_aSystems as $aSystem)
  212. if(isset($aData[$aSystem['name']]) && (int)$aSystem['init'] == 0)
  213. $aData[$aSystem['name']] = 0;
  214. return $aData;
  215. }
  216. protected function _clearCached()
  217. {
  218. $this->_oCacheObject->delData($this->_getCacheKey('systems'));
  219. $this->_oCacheObject->delData($this->_getCacheKey('data'));
  220. }
  221. protected function _updateCached($sType, $aData)
  222. {
  223. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo($sType);
  224. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  225. $aCached = array_merge($aCached ? $aCached : array(), $aData);
  226. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  227. }
  228. protected function _getRequestedData($iIndex = 0, $bIndexCheck = false, $aCachedData = array())
  229. {
  230. $bInit = !$bIndexCheck && empty($aCachedData);
  231. $aResult = array();
  232. foreach($this->_aSystems as $sName => $aSystem) {
  233. if(empty($aSystem) || !is_array($aSystem) || (int)$aSystem['active'] != 1 || ($bInit && (int)$aSystem['init'] == 0))
  234. continue;
  235. if($bIndexCheck && $iIndex % (int)$aSystem['frequency'] != 0)
  236. continue;
  237. $bCachedDataBySystem = !empty($aCachedData) && isset($aCachedData[$sName]);
  238. $mixedResponce = $this->_getRequestedDataBySystem($aSystem, ($bCachedDataBySystem ? (int)$aCachedData[$sName] : 0), !$bCachedDataBySystem);
  239. if($mixedResponce === false)
  240. continue;
  241. $aResult[$sName] = $mixedResponce;
  242. }
  243. return $aResult;
  244. }
  245. protected function _getRequestedDataBySystem($aSystem, $iCachedData = 0, $bInit = true)
  246. {
  247. if(!BxDolService::isSerializedService($aSystem['service_call']))
  248. return false;
  249. $aResponce = BxDolService::callSerialized($aSystem['service_call'], array('count' => (int)$iCachedData, 'init' => ($bInit ? 1 : 0)));
  250. if(empty($aResponce) || !is_array($aResponce) || !isset($aResponce['count']) || (!$bInit && !isset($aResponce['method'])))
  251. return false;
  252. return $aResponce;
  253. }
  254. protected function _getPageId()
  255. {
  256. $aPageParams = array();
  257. parse_str($_SERVER['QUERY_STRING'], $aPageParams);
  258. $aPageParams = array_diff_assoc($aPageParams, array('start', 'per_page', 'order', 'filter'));
  259. return '_' . $this->_iProfileId . '_' . md5(bx_append_url_params($_SERVER['PHP_SELF'], $aPageParams));
  260. }
  261. protected function _encodeHash()
  262. {
  263. return base64_encode($this->_sCacheKeySystems);
  264. }
  265. protected function _decodeHash($sHash)
  266. {
  267. return base64_decode($sHash);
  268. }
  269. }
  270. /** @} */