BxDolLiveUpdates.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  70. $GLOBALS['bxDolClasses'][__CLASS__] = new BxTemplLiveUpdates();
  71. return $GLOBALS['bxDolClasses'][__CLASS__];
  72. }
  73. /**
  74. *
  75. * Is called via Ajax Requests only.
  76. */
  77. public function perform()
  78. {
  79. $iIndex = (int)bx_get('index');
  80. $mixedCacheKey = bx_get('hash');
  81. if($mixedCacheKey !== false)
  82. $this->_sCacheKeySystems = $this->_decodeHash(bx_process_input($mixedCacheKey));
  83. $mixedSystemsActive = bx_get('systems_active');
  84. if($mixedSystemsActive !== false)
  85. $this->_aSystemsActive = array_keys($mixedSystemsActive);
  86. $mixedSystemsTransient = bx_get('systems_transient');
  87. if($mixedSystemsTransient !== false)
  88. $this->_aSystemsTransient = array_keys($mixedSystemsTransient);
  89. $this->_aSystems = $this->_getCachedSystems();
  90. if(empty($this->_aSystems) || !is_array($this->_aSystems))
  91. return array();
  92. $aCurrent = $mixedSystemsActive;
  93. if(empty($aCurrent) || !is_array($aCurrent))
  94. $aCurrent = $this->_getCachedData();
  95. $aRequested = $this->_getRequestedData($iIndex, true, $aCurrent);
  96. $aResult = array();
  97. $bUpdateCache = false;
  98. foreach($aRequested as $sName => $aData) {
  99. if(isset($aCurrent[$sName])) {
  100. if((int)$aCurrent[$sName] == (int)$aData['count'])
  101. continue;
  102. $aResultData = array('count_new' => $aData['count'], 'count_old' => $aCurrent[$sName]);
  103. if(isset($aData['data']))
  104. $aResultData = array_merge($aResultData, $aData['data']);
  105. $aResult[] = array(
  106. 'system' => $sName,
  107. 'data' => $aResultData,
  108. 'method' => $aData['method']
  109. );
  110. }
  111. $aCurrent[$sName] = $aData['count'];
  112. $bUpdateCache = true;
  113. }
  114. if($bUpdateCache)
  115. $this->_updateCached('data', $aCurrent);
  116. return $aResult;
  117. }
  118. protected function _addSystem($sName, $iFrequency, $sServiceCall, $bActive = true)
  119. {
  120. if(!$this->_iProfileId)
  121. return false;
  122. if(empty($this->_aSystemsTransient))
  123. $this->_sCacheKeySystems .= $this->_getPageId();
  124. if(!in_array($sName, $this->_aSystemsTransient))
  125. $this->_aSystemsTransient[] = $sName;
  126. if(!in_array($sName, $this->_aSystemsActive))
  127. $this->_aSystemsActive[] = $sName;
  128. if(empty($this->_aSystems[$sName])) {
  129. $this->_aSystems[$sName] = array(
  130. 'name' => $sName,
  131. 'init' => 1,
  132. 'frequency' => $iFrequency,
  133. 'service_call' => $sServiceCall,
  134. 'active' => $bActive ? 1 : 0
  135. );
  136. $this->_updateCached('systems', $this->_aSystems);
  137. }
  138. return true;
  139. }
  140. protected function _addData($sName, $iValue)
  141. {
  142. $aCachedData = $this->_getCachedData();
  143. $aCachedData[$sName] = $iValue;
  144. $this->_updateCached('data', $aCachedData);
  145. return true;
  146. }
  147. protected function _getCacheInfo($sType)
  148. {
  149. return array(
  150. $this->_oCacheObject,
  151. $this->_getCacheKey($sType),
  152. $this->_iCacheTTL
  153. );
  154. }
  155. protected function _getCacheKey($sType)
  156. {
  157. return $this->_oQuery->genDbCacheKey($this->{'_sCacheKey' . ucfirst($sType)});
  158. }
  159. protected function _getCached($sType)
  160. {
  161. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo($sType);
  162. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  163. if(empty($aCached)) {
  164. switch($sType) {
  165. case 'systems':
  166. $aCached = $this->_oQuery->getSystems();
  167. break;
  168. case 'data':
  169. if(empty($aCached))
  170. $aCached = array();
  171. $aRequested = $this->_getRequestedData();
  172. foreach($this->_aSystems as $sName => $aSystem)
  173. $aCached[$sName] = !empty($aRequested[$sName]['count']) ? (int)$aRequested[$sName]['count'] : 0;
  174. break;
  175. }
  176. if(!empty($aCached))
  177. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  178. }
  179. return $aCached;
  180. }
  181. protected function _getCachedSystems()
  182. {
  183. $aSystems = $this->_getCached('systems');
  184. if(!empty($aSystems) && !empty($this->_aSystemsActive))
  185. $aSystems = array_intersect_key($aSystems, array_flip($this->_aSystemsActive));
  186. return $aSystems;
  187. }
  188. protected function _getCachedData($bInit = false)
  189. {
  190. $aData = $this->_getCached('data');
  191. if($bInit)
  192. foreach($this->_aSystems as $aSystem)
  193. if(isset($aData[$aSystem['name']]) && (int)$aSystem['init'] == 0)
  194. $aData[$aSystem['name']] = 0;
  195. return $aData;
  196. }
  197. protected function _clearCached()
  198. {
  199. $this->_oCacheObject->delData($this->_getCacheKey('systems'));
  200. $this->_oCacheObject->delData($this->_getCacheKey('data'));
  201. }
  202. protected function _updateCached($sType, $aData)
  203. {
  204. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo($sType);
  205. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  206. $aCached = array_merge($aCached ? $aCached : array(), $aData);
  207. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  208. }
  209. protected function _getRequestedData($iIndex = 0, $bIndexCheck = false, $aCachedData = array())
  210. {
  211. $bInit = !$bIndexCheck && empty($aCachedData);
  212. $aResult = array();
  213. foreach($this->_aSystems as $sName => $aSystem) {
  214. if(empty($aSystem) || !is_array($aSystem) || (int)$aSystem['active'] != 1 || ($bInit && (int)$aSystem['init'] == 0))
  215. continue;
  216. if($bIndexCheck && $iIndex % (int)$aSystem['frequency'] != 0)
  217. continue;
  218. $bCachedDataBySystem = !empty($aCachedData) && isset($aCachedData[$sName]);
  219. $mixedResponce = $this->_getRequestedDataBySystem($aSystem, ($bCachedDataBySystem ? (int)$aCachedData[$sName] : 0), !$bCachedDataBySystem);
  220. if($mixedResponce === false)
  221. continue;
  222. $aResult[$sName] = $mixedResponce;
  223. }
  224. return $aResult;
  225. }
  226. protected function _getRequestedDataBySystem($aSystem, $iCachedData = 0, $bInit = true)
  227. {
  228. if(!BxDolService::isSerializedService($aSystem['service_call']))
  229. return false;
  230. $aResponce = BxDolService::callSerialized($aSystem['service_call'], array('count' => (int)$iCachedData, 'init' => ($bInit ? 1 : 0)));
  231. if(empty($aResponce) || !is_array($aResponce) || !isset($aResponce['count']) || (!$bInit && !isset($aResponce['method'])))
  232. return false;
  233. return $aResponce;
  234. }
  235. protected function _getPageId()
  236. {
  237. $aPageParams = array();
  238. parse_str($_SERVER['QUERY_STRING'], $aPageParams);
  239. $aPageParams = array_diff_assoc($aPageParams, array('start', 'per_page', 'order', 'filter'));
  240. return '_' . $this->_iProfileId . '_' . md5(bx_append_url_params($_SERVER['PHP_SELF'], $aPageParams));
  241. }
  242. protected function _encodeHash()
  243. {
  244. return base64_encode($this->_sCacheKeySystems);
  245. }
  246. protected function _decodeHash($sHash)
  247. {
  248. return base64_decode($sHash);
  249. }
  250. }
  251. /** @} */