1
0

BxDolLiveUpdates.php 11 KB

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