BxDolLiveUpdates.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. /**
  110. *
  111. * Add transient live update for current user on this current page.
  112. * @param string $sName - unique name.
  113. * @param integer $iFrequency - call frequency.
  114. * @param string $sServiceCall - serialized service call.
  115. * @param boolean $bActive - add active/not active live update.
  116. */
  117. public function add($sName, $iFrequency, $sServiceCall, $bActive = true)
  118. {
  119. if(!$this->_iProfileId)
  120. return false;
  121. if(empty($this->_aSystemsTransient))
  122. $this->_sCacheKey .= $this->_getPageId();
  123. if(!in_array($sName, $this->_aSystemsTransient))
  124. $this->_aSystemsTransient[] = $sName;
  125. if(!in_array($sName, $this->_aSystemsActive))
  126. $this->_aSystemsActive[] = $sName;
  127. if(empty($this->_aSystems[$sName])) {
  128. $this->_aSystems[$sName] = array(
  129. 'name' => $sName,
  130. 'frequency' => $iFrequency,
  131. 'service_call' => $sServiceCall,
  132. 'active' => $bActive ? 1 : 0
  133. );
  134. $this->_updateCached('systems', $this->_aSystems);
  135. }
  136. $mixedResponce = $this->_getRequestedDataBySystem($this->_aSystems[$sName]);
  137. $iCount = 0;
  138. if($mixedResponce !== false && isset($mixedResponce['count']))
  139. $iCount = (int)$mixedResponce['count'];
  140. $aCachedData = $this->_getCachedData();
  141. $aCachedData[$sName] = $iCount;
  142. $this->_updateCached('data', $aCachedData);
  143. return $iCount;
  144. }
  145. protected function _getCacheInfo()
  146. {
  147. return array(
  148. $this->_oCacheObject,
  149. $this->_oQuery->genDbCacheKey($this->_sCacheKey),
  150. $this->_iCacheTTL
  151. );
  152. }
  153. protected function _getCached($sType)
  154. {
  155. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo();
  156. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  157. if(empty($aCached[$sType])) {
  158. switch($sType) {
  159. case 'systems':
  160. $aCached[$sType] = $this->_oQuery->getSystems();
  161. break;
  162. case 'data':
  163. if(!isset($aCached[$sType]))
  164. $aCached[$sType] = array();
  165. $aRequested = $this->_getRequestedData();
  166. foreach($this->_aSystems as $sName => $aSystem)
  167. $aCached[$sType][$sName] = !empty($aRequested[$sName]['count']) ? (int)$aRequested[$sName]['count'] : 0;
  168. break;
  169. }
  170. if(!empty($aCached[$sType]))
  171. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  172. }
  173. return $aCached[$sType];
  174. }
  175. protected function _getCachedSystems()
  176. {
  177. $aSystems = $this->_getCached('systems');
  178. if(!empty($aSystems) && !empty($this->_aSystemsActive))
  179. $aSystems = array_intersect_key($aSystems, array_flip($this->_aSystemsActive));
  180. return $aSystems;
  181. }
  182. protected function _getCachedData()
  183. {
  184. return $this->_getCached('data');
  185. }
  186. protected function _clearCached()
  187. {
  188. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo();
  189. $oCache->delData($sCacheKey);
  190. }
  191. protected function _updateCached($sKey, $aData)
  192. {
  193. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo();
  194. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  195. $aCached[$sKey] = $aData;
  196. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  197. }
  198. protected function _getRequestedData($iIndex = 0, $bIndexCheck = false, $aCachedData = array())
  199. {
  200. $aResult = array();
  201. foreach($this->_aSystems as $sName => $aSystem) {
  202. if(empty($aSystem) || !is_array($aSystem) || (int)$aSystem['active'] != 1)
  203. continue;
  204. if($bIndexCheck && $iIndex % (int)$aSystem['frequency'] != 0)
  205. continue;
  206. $bCachedDataBySystem = !empty($aCachedData) && isset($aCachedData[$sName]);
  207. $mixedResponce = $this->_getRequestedDataBySystem($aSystem, ($bCachedDataBySystem ? (int)$aCachedData[$sName] : 0), !$bCachedDataBySystem);
  208. if($mixedResponce === false)
  209. continue;
  210. $aResult[$sName] = $mixedResponce;
  211. }
  212. return $aResult;
  213. }
  214. protected function _getRequestedDataBySystem($aSystem, $iCachedData = 0, $bInit = true)
  215. {
  216. if(!BxDolService::isSerializedService($aSystem['service_call']))
  217. return false;
  218. $aResponce = BxDolService::callSerialized($aSystem['service_call'], array('count' => (int)$iCachedData, 'init' => ($bInit ? 1 : 0)));
  219. if(empty($aResponce) || !is_array($aResponce) || !isset($aResponce['count']) || (!$bInit && !isset($aResponce['method'])))
  220. return false;
  221. return $aResponce;
  222. }
  223. protected function _getPageId()
  224. {
  225. $aPageParams = array();
  226. parse_str($_SERVER['QUERY_STRING'], $aPageParams);
  227. $aPageParams = array_diff_assoc($aPageParams, array('start', 'per_page', 'order', 'filter'));
  228. return '_' . md5(bx_append_url_params($_SERVER['PHP_SELF'], $aPageParams));
  229. }
  230. }
  231. /** @} */