BxDolService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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)
  41. {
  42. $oDb = BxDolModuleQuery::getInstance();
  43. $aModule = array();
  44. if(is_string($mixed))
  45. $aModule = $oDb->getModuleByName($mixed);
  46. else
  47. $aModule = $oDb->getModuleById($mixed);
  48. if (empty($aModule))
  49. return '';
  50. $sKey = md5($mixed . $sMethod . print_r($aParams, true) . $sClass);
  51. if (!$bIgnoreCache && isset(self::$_aMemoryCache[$sKey]))
  52. return self::$_aMemoryCache[$sKey];
  53. self::$_aMemoryCache[$sKey] = BxDolRequest::processAsService($aModule, $sMethod, $aParams, $sClass);
  54. return self::$_aMemoryCache[$sKey];
  55. }
  56. /**
  57. * Perform serice call by accepting serialized array of service call parameters:
  58. * @code
  59. * array (
  60. * 'module' => 'system', // module name
  61. * 'method' => 'test', // service method name
  62. * 'params' => array(), // array of parameters to pass to service method
  63. * 'class' => 'Module', // class to search service method in
  64. * )
  65. * @endcode
  66. *
  67. * @param $s serialized array of serice call
  68. * @param $aMarkers service method name in format 'method_name', corresponding class metod is serviceMethodName
  69. * @param $sReplaceIn params to pass to service method
  70. * @return service call result
  71. */
  72. public static function callSerialized($s, $aMarkers = array(), $sReplaceIn = 'params')
  73. {
  74. $a = @unserialize($s);
  75. if (false === $a || !is_array($a))
  76. return '';
  77. if (isset($a[$sReplaceIn]) && $aMarkers)
  78. $a[$sReplaceIn] = bx_replace_markers($a[$sReplaceIn], $aMarkers);
  79. 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);
  80. }
  81. /**
  82. * Check if string is serialized array
  83. */
  84. public static function isSerializedService($s)
  85. {
  86. return preg_match('/^a:[\d+]:\{/', $s);
  87. }
  88. /**
  89. * Serialized service call array
  90. */
  91. public static function getSerializedService($mixedModule, $sMethod, $aParams = array(), $sClass = '')
  92. {
  93. $aService = array(
  94. 'module' => $mixedModule,
  95. 'method' => $sMethod,
  96. );
  97. if(!empty($aParams))
  98. $aService['params'] = $aParams;
  99. if(!empty($sClass))
  100. $aService['class'] = $sClass;
  101. return serialize($aService);
  102. }
  103. }
  104. /** @} */