1
0

BxDolLiveUpdates.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. protected function _addSystem($sName, $iFrequency, $sServiceCall, $mixedActive = true)
  121. {
  122. if(!$this->_iProfileId)
  123. return false;
  124. if(empty($this->_aSystemsTransient))
  125. $this->_sCacheKeySystems .= $this->_getPageId();
  126. if(!in_array($sName, $this->_aSystemsTransient))
  127. $this->_aSystemsTransient[] = $sName;
  128. if(!in_array($sName, $this->_aSystemsActive))
  129. $this->_aSystemsActive[] = $sName;
  130. if(empty($this->_aSystems[$sName])) {
  131. list($bActive, $bInit) = is_array($mixedActive) ? $mixedActive : [$mixedActive, true];
  132. $this->_aSystems[$sName] = [
  133. 'name' => $sName,
  134. 'init' => $bInit ? 1 : 0,
  135. 'frequency' => $iFrequency,
  136. 'service_call' => $sServiceCall,
  137. 'active' => $bActive ? 1 : 0
  138. ];
  139. $this->_updateCached('systems', $this->_aSystems);
  140. }
  141. return true;
  142. }
  143. protected function _addData($sName, $iValue)
  144. {
  145. $aCachedData = $this->_getCachedData();
  146. $aCachedData[$sName] = $iValue;
  147. $this->_updateCached('data', $aCachedData);
  148. return true;
  149. }
  150. protected function _getCacheInfo($sType)
  151. {
  152. return array(
  153. $this->_oCacheObject,
  154. $this->_getCacheKey($sType),
  155. $this->_iCacheTTL
  156. );
  157. }
  158. protected function _getCacheKey($sType)
  159. {
  160. return $this->_oQuery->genDbCacheKey($this->{'_sCacheKey' . ucfirst($sType)});
  161. }
  162. protected function _getCached($sType)
  163. {
  164. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo($sType);
  165. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  166. if(empty($aCached)) {
  167. switch($sType) {
  168. case 'systems':
  169. $aCached = $this->_oQuery->getSystems();
  170. break;
  171. case 'data':
  172. if(empty($aCached))
  173. $aCached = array();
  174. $aRequested = $this->_getRequestedData();
  175. foreach($this->_aSystems as $sName => $aSystem)
  176. $aCached[$sName] = !empty($aRequested[$sName]['count']) ? (int)$aRequested[$sName]['count'] : 0;
  177. break;
  178. }
  179. if(!empty($aCached))
  180. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  181. }
  182. return $aCached;
  183. }
  184. protected function _getCachedSystems()
  185. {
  186. $aSystems = $this->_getCached('systems');
  187. if(!empty($aSystems) && !empty($this->_aSystemsActive))
  188. $aSystems = array_intersect_key($aSystems, array_flip($this->_aSystemsActive));
  189. return $aSystems;
  190. }
  191. protected function _getCachedData($bInit = false)
  192. {
  193. $aData = $this->_getCached('data');
  194. if($bInit)
  195. foreach($this->_aSystems as $aSystem)
  196. if(isset($aData[$aSystem['name']]) && (int)$aSystem['init'] == 0)
  197. $aData[$aSystem['name']] = 0;
  198. return $aData;
  199. }
  200. protected function _clearCached()
  201. {
  202. $this->_oCacheObject->delData($this->_getCacheKey('systems'));
  203. $this->_oCacheObject->delData($this->_getCacheKey('data'));
  204. }
  205. protected function _updateCached($sType, $aData)
  206. {
  207. list($oCache, $sCacheKey, $iCacheTtl) = $this->_getCacheInfo($sType);
  208. $aCached = $oCache->getData($sCacheKey, $iCacheTtl);
  209. $aCached = array_merge($aCached ? $aCached : array(), $aData);
  210. $oCache->setData($sCacheKey, $aCached, $iCacheTtl);
  211. }
  212. protected function _getRequestedData($iIndex = 0, $bIndexCheck = false, $aCachedData = array())
  213. {
  214. $bInit = !$bIndexCheck && empty($aCachedData);
  215. $aResult = array();
  216. foreach($this->_aSystems as $sName => $aSystem) {
  217. if(empty($aSystem) || !is_array($aSystem) || (int)$aSystem['active'] != 1 || ($bInit && (int)$aSystem['init'] == 0))
  218. continue;
  219. if($bIndexCheck && $iIndex % (int)$aSystem['frequency'] != 0)
  220. continue;
  221. $bCachedDataBySystem = !empty($aCachedData) && isset($aCachedData[$sName]);
  222. $mixedResponce = $this->_getRequestedDataBySystem($aSystem, ($bCachedDataBySystem ? (int)$aCachedData[$sName] : 0), !$bCachedDataBySystem);
  223. if($mixedResponce === false)
  224. continue;
  225. $aResult[$sName] = $mixedResponce;
  226. }
  227. return $aResult;
  228. }
  229. protected function _getRequestedDataBySystem($aSystem, $iCachedData = 0, $bInit = true)
  230. {
  231. if(!BxDolService::isSerializedService($aSystem['service_call']))
  232. return false;
  233. $aResponce = BxDolService::callSerialized($aSystem['service_call'], array('count' => (int)$iCachedData, 'init' => ($bInit ? 1 : 0)));
  234. if(empty($aResponce) || !is_array($aResponce) || !isset($aResponce['count']) || (!$bInit && !isset($aResponce['method'])))
  235. return false;
  236. return $aResponce;
  237. }
  238. protected function _getPageId()
  239. {
  240. $aPageParams = array();
  241. parse_str($_SERVER['QUERY_STRING'], $aPageParams);
  242. $aPageParams = array_diff_assoc($aPageParams, array('start', 'per_page', 'order', 'filter'));
  243. return '_' . $this->_iProfileId . '_' . md5(bx_append_url_params($_SERVER['PHP_SELF'], $aPageParams));
  244. }
  245. protected function _encodeHash()
  246. {
  247. return base64_encode($this->_sCacheKeySystems);
  248. }
  249. protected function _decodeHash($sHash)
  250. {
  251. return base64_decode($sHash);
  252. }
  253. }
  254. /** @} */