BxDolService.php 3.1 KB

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