BxDolLiveUpdates.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 $_sCacheKey;
  29. protected $_iCacheTTL;
  30. protected $_sJsClass;
  31. protected $_sJsObject;
  32. protected function __construct()
  33. {
  34. parent::__construct();
  35. /**
  36. * Note. Currently Live Updates are associated with profiles (Profile ID) and therefore they are used for logged in members only.
  37. * 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
  38. * to update BxBaseLiveUpdates::init if Session ID will be used.
  39. */
  40. $this->_iProfileId = (int)bx_get_logged_profile_id();
  41. if(!$this->_iProfileId)
  42. return;
  43. $this->_oQuery = new BxDolLiveUpdatesQuery();
  44. $this->_iInterval = (int)$this->_oQuery->getParam('sys_live_updates_interval');
  45. $this->_oCacheObject = $this->_oQuery->getDbCacheObject();
  46. $this->_sCacheKey = 'sys_live_updates_' . $this->_iProfileId;
  47. $this->_iCacheTTL = 86400;
  48. $this->_sJsClass = 'BxDolLiveUpdates';
  49. $this->_sJsObject = 'oLiveUpdates';
  50. $this->_aSystems = $this->_getCachedSystems();
  51. $this->_aSystemsActive = array_keys($this->_aSystems);
  52. $this->_aSystemsTransient = array();
  53. }
  54. /**
  55. * get live updates instanse
  56. * @return ready to use class instance
  57. */
  58. public static function getInstance()
  59. {
  60. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  61. $GLOBALS['bxDolClasses'][__CLASS__] = new BxTemplLiveUpdates();
  62. return $GLOBALS['bxDolClasses'][__CLASS__];
  63. }
  64. /**
  65. *
  66. * Is called via Ajax Requests only.
  67. */
  68. public function perform()
  69. {
  70. $iIndex = (int)bx_get('index');
  71. $mixedCacheKey = bx_get('hash');
  72. if($mixedCacheKey !== false)
  73. $this->_sCacheKey = base64_decode(bx_process_input($mixedCacheKey));
  74. $mixedSystemsActive = bx_get('systems_active');
  75. if($mixedSystemsActive !== false)
  76. $this->_aSystemsActive = array_keys($mixedSystemsActive);
  77. $mixedSystemsTransient = bx_get('systems_transient');
  78. if($mixedSystemsTransient !== false)
  79. $this->_aSystemsTransient = array_keys($mixedSystemsTransient);
  80. $this->_aSystems = $this->_getCachedSystems();
  81. if(empty($this->_aSystems) || !is_array($this->_aSystems))
  82. return array();
  83. $aCurrent = $mixedSystemsActive;
  84. if(empty($aCurrent) || !is_array($aCurrent))
  85. $aCurrent = $this->_getCachedData();
  86. $aRequested = $this->_getRequestedData($iIndex, true, $aCurrent);
  87. $aResult = array();
  88. $bUpdateCache = false;
  89. foreach($aRequested as $sName => $aData) {
  90. if(isset($aCurrent[$sName])) {
  91. if((int)$aCurrent[$sName] == (int)$aData['count'])
  92. continue;
  93. $aResultData = array('count_new' => $aData['count'], 'count_old' => $aCurrent[$sName]);
  94. if(isset($aData['data']))
  95. $aResultData = array_merge($aResultData, $aData['data']);
  96. $aResult[] = array(
  97. 'system' => $sName,
  98. 'data' => $aResultData,
  99. 'method' => $aData['method']
  100. );
  101. }
  102. $aCurrent[$sName] = $aData['count'];
  103. $bUpdateCache = true;
  104. }
  105. if($bUpdateCache)
  106. $this->_updateCached('data', $aCurrent);
  107. return $aResult;
  108. }
  109. protected function _addSystem($sName, $iFrequency, $sServiceCall, $bActive = true)
  110. {
  111. if(!$this->_iProfileId)
  112. return false;
  113. if(empty($this->_aSystemsTransient))
  114. $this->_sCacheKey .= $this->_getPageId();
  115. if(!in_array($sName, $this->_aSystemsTransient))
  116. $this->_aSystemsTransient[] = $sName;
  117. if(!in_array($sName, $this->_aSystemsActive))
  118. $this->_aSystemsActive[] = $sName;
  119. if(empty($this->_aSystems[$sName])) {
  120. $this->_aSystems[$sName] = array(
  121. 'name' => $sName,
  122. 'frequency' => $iFrequency,
  123. 'service_call' => $sServiceCall,
  124. 'active' => $bActive ? 1 : 0
  125. );
  126. $this->_updateCached('systems', $this->_aSystems);
  127. }
  128. return true;
  129. }
  130. protected function _addData($sName, $iValue)
  131. {
  132. $aCachedData = $this->_getCachedData();
  133. $aCachedData[$sName] = $iValue;
  134. $this->_updateCached('data', $aCachedData);
  135. return true;
  136. }
  137. protected function _getCacheInfo()
  138. {
  139. return array(
  140. $this->_oCacheObject,
  141. $this->_oQuery->genDbCacheKey($this->_sCacheKey),
  142. $this->_iCacheTTL
  143. );
  144. }
  145. protected function _getCached($sType)
  146. {
  147. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo();
  148. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  149. if(empty($aCached[$sType])) {
  150. switch($sType) {
  151. case 'systems':
  152. $aCached[$sType] = $this->_oQuery->getSystems();
  153. break;
  154. case 'data':
  155. if(!isset($aCached[$sType]))
  156. $aCached[$sType] = array();
  157. $aRequested = $this->_getRequestedData();
  158. foreach($this->_aSystems as $sName => $aSystem)
  159. $aCached[$sType][$sName] = !empty($aRequested[$sName]['count']) ? (int)$aRequested[$sName]['count'] : 0;
  160. break;
  161. }
  162. if(!empty($aCached[$sType]))
  163. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  164. }
  165. return $aCached[$sType];
  166. }
  167. protected function _getCachedSystems()
  168. {
  169. $aSystems = $this->_getCached('systems');
  170. if(!empty($aSystems) && !empty($this->_aSystemsActive))
  171. $aSystems = array_intersect_key($aSystems, array_flip($this->_aSystemsActive));
  172. return $aSystems;
  173. }
  174. protected function _getCachedData()
  175. {
  176. return $this->_getCached('data');
  177. }
  178. protected function _clearCached()
  179. {
  180. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo();
  181. $oCache->delData($sCacheKey);
  182. }
  183. protected function _updateCached($sKey, $aData)
  184. {
  185. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo();
  186. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  187. $aCached[$sKey] = $aData;
  188. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  189. }
  190. protected function _getRequestedData($iIndex = 0, $bIndexCheck = false, $aCachedData = array())
  191. {
  192. $aResult = array();
  193. foreach($this->_aSystems as $sName => $aSystem) {
  194. if(empty($aSystem) || !is_array($aSystem) || (int)$aSystem['active'] != 1)
  195. continue;
  196. if($bIndexCheck && $iIndex % (int)$aSystem['frequency'] != 0)
  197. continue;
  198. $bCachedDataBySystem = !empty($aCachedData) && isset($aCachedData[$sName]);
  199. $mixedResponce = $this->_getRequestedDataBySystem($aSystem, ($bCachedDataBySystem ? (int)$aCachedData[$sName] : 0), !$bCachedDataBySystem);
  200. if($mixedResponce === false)
  201. continue;
  202. $aResult[$sName] = $mixedResponce;
  203. }
  204. return $aResult;
  205. }
  206. protected function _getRequestedDataBySystem($aSystem, $iCachedData = 0, $bInit = true)
  207. {
  208. if(!BxDolService::isSerializedService($aSystem['service_call']))
  209. return false;
  210. $aResponce = BxDolService::callSerialized($aSystem['service_call'], array('count' => (int)$iCachedData, 'init' => ($bInit ? 1 : 0)));
  211. if(empty($aResponce) || !is_array($aResponce) || !isset($aResponce['count']) || (!$bInit && !isset($aResponce['method'])))
  212. return false;
  213. return $aResponce;
  214. }
  215. protected function _getPageId()
  216. {
  217. $aPageParams = array();
  218. parse_str($_SERVER['QUERY_STRING'], $aPageParams);
  219. $aPageParams = array_diff_assoc($aPageParams, array('start', 'per_page', 'order', 'filter'));
  220. return '_' . md5(bx_append_url_params($_SERVER['PHP_SELF'], $aPageParams));
  221. }
  222. }
  223. /** @} */