BxDolService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /**
  10. * Service calls to modules' methods.
  11. *
  12. * The class has one static method to make service calls to the module's methods
  13. *
  14. *
  15. * Example of usage:
  16. * @code
  17. * $isSpam = BxDolService::call('bx_antispam', 'is_spam', array($sText));
  18. * @endcode
  19. *
  20. *
  21. * Memberships/ACL:
  22. * Doesn't depend on user's membership.
  23. *
  24. *
  25. * Alerts:
  26. * no alerts available
  27. *
  28. */
  29. class BxDolService extends BxDol
  30. {
  31. static protected $_aMemoryCache = array();
  32. /**
  33. * Perform serice call
  34. * @param $mixed module name or module id
  35. * @param $sMethod service method name in format 'method_name', corresponding class metod is serviceMethodName
  36. * @param $aParams params to pass to service method
  37. * @param $sClass class to search for service method, by default it is main module class
  38. * @return service call result
  39. */
  40. public static function call($mixed, $sMethod, $aParams = array(), $sClass = 'Module', $bIgnoreCache = false, $bIgnoreInactive = false)
  41. {
  42. $aModule = self::getModule($mixed);
  43. if (empty($aModule) || ((int)$aModule['enabled'] == 0 && !$bIgnoreInactive))
  44. return '';
  45. $sKey = md5($mixed . $sMethod . print_r($aParams, true) . $sClass . bx_get_logged_profile_id());
  46. if (!$bIgnoreCache && isset(self::$_aMemoryCache[$sKey]))
  47. return self::$_aMemoryCache[$sKey];
  48. self::$_aMemoryCache[$sKey] = BxDolRequest::processAsService($aModule, $sMethod, $aParams, $sClass);
  49. return self::$_aMemoryCache[$sKey];
  50. }
  51. /**
  52. * Perform serice call by accepting serialized array of service call parameters:
  53. * @code
  54. * array (
  55. * 'module' => 'system', // module name
  56. * 'method' => 'test', // service method name
  57. * 'params' => array(), // array of parameters to pass to service method
  58. * 'class' => 'Module', // class to search service method in
  59. * )
  60. * @endcode
  61. *
  62. * @param $s serialized array of serice call
  63. * @param $aMarkers service method name in format 'method_name', corresponding class metod is serviceMethodName
  64. * @param $sReplaceIn params to pass to service method
  65. * @return service call result
  66. */
  67. public static function callSerialized($s, $aMarkers = array(), $sReplaceIn = 'params')
  68. {
  69. $a = @unserialize($s);
  70. if (false === $a || !is_array($a))
  71. return '';
  72. if (isset($a[$sReplaceIn]) && $aMarkers)
  73. $a[$sReplaceIn] = bx_replace_markers($a[$sReplaceIn], $aMarkers);
  74. return self::call($a['module'], $a['method'], isset($a['params']) ? $a['params'] : array(), isset($a['class']) ? $a['class'] : 'Module', isset($a['ignore_cache']) ? $a['ignore_cache'] : false);
  75. }
  76. public static function callMacro($s, $aMarkers = array())
  77. {
  78. // replace markers
  79. if ($aMarkers)
  80. $s = bx_replace_markers($s, $aMarkers);
  81. // check for correct macros
  82. if (!preg_match("/^([a-zA-Z0-9_\:]+)(.*)$/", $s, $aMatches))
  83. return _t('_sys_macros_malformed');
  84. $a = explode(":", $aMatches[1]);
  85. if (!isset($a[0]) || !isset($a[1]))
  86. return _t('_sys_macros_malformed');
  87. $aParams = array();
  88. if (!empty($aMatches[2]))
  89. $aParams = json_decode($aMatches[2], true);
  90. if (null === $aParams)
  91. return _t('_sys_macros_malformed');
  92. // check if macros is safe
  93. if (!self::call($a[0], 'is_safe_service', array($a[1])))
  94. return _t('_sys_macros_unsafe');
  95. // check module, method and number of arguments
  96. $aModule = self::getModule($a[0]);
  97. if (empty($aModule))
  98. return _t('_sys_macros_method_or_class_not_found');
  99. $iCheck = BxDolRequest::checkCall($aModule, $a[1], $aParams, isset($a[2]) ? $a[2] : 'Module');
  100. switch ($iCheck) {
  101. case 1:
  102. return _t('_sys_macros_method_or_class_not_found');
  103. case 2:
  104. return _t('_sys_macros_required_args_missing');
  105. case 3:
  106. return _t('_sys_macros_too_many_args');
  107. }
  108. // perform call
  109. $mixed = self::call($a[0], $a[1], $aParams, isset($a[2]) ? $a[2] : 'Module', isset($a[3]) && 'ignore_cache' == $a[3] ? true : false);
  110. // check result
  111. if (is_object($mixed))
  112. return _t('_sys_macros_output_not_displayable');
  113. if (is_array($mixed) && !isset($mixed['content']))
  114. return _t('_sys_macros_output_not_displayable');
  115. if (is_array($mixed) && isset($mixed['content']))
  116. return $mixed['content'];
  117. return $mixed;
  118. }
  119. /**
  120. * Check if string is serialized array
  121. */
  122. public static function isSerializedService($s)
  123. {
  124. return preg_match('/^a:[\d+]:\{/', $s);
  125. }
  126. /**
  127. * Serialized service call array
  128. */
  129. public static function getSerializedService($mixedModule, $sMethod, $aParams = array(), $sClass = '')
  130. {
  131. $aService = array(
  132. 'module' => $mixedModule,
  133. 'method' => $sMethod,
  134. );
  135. if(!empty($aParams))
  136. $aService['params'] = $aParams;
  137. if(!empty($sClass))
  138. $aService['class'] = $sClass;
  139. return serialize($aService);
  140. }
  141. protected static function getModule($mixed)
  142. {
  143. $oDb = BxDolModuleQuery::getInstance();
  144. if (is_string($mixed))
  145. $aModule = $oDb->getModuleByName($mixed);
  146. else
  147. $aModule = $oDb->getModuleById($mixed);
  148. return $aModule;
  149. }
  150. }
  151. /** @} */