PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/rokcommon/RokCommon/Ajax.php

https://gitlab.com/che234/adn
PHP | 183 lines | 106 code | 16 blank | 61 comment | 8 complexity | c831c99557e75f771a1d6c6271541835 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: Ajax.php 10831 2013-05-29 19:32:17Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2016 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. defined('ROKCOMMON') or die;
  9. /**
  10. *
  11. */
  12. class RokCommon_Ajax
  13. {
  14. const JSON_ENCODING = 'json';
  15. const FORM_ENCODING = 'form';
  16. /**
  17. *
  18. */
  19. const DEFAULT_MODEL_PREFIX = 'RokCommon_Ajax_Model_';
  20. /**
  21. * @param $path
  22. * @param string $prefix
  23. * @param int $priority
  24. *
  25. * @throws RokCommon_Ajax_Exception
  26. */
  27. public static function addModelPath($path, $prefix = self::DEFAULT_MODEL_PREFIX, $priority = 10)
  28. {
  29. try {
  30. $container = RokCommon_Service::getContainer();
  31. /** @var $ajaxModelLoader RokCommon_ClassLoader_PrefixedLoader */
  32. $ajaxModelLoader = $container->getService('ajax.model.loader');
  33. $ajaxModelLoader->addPath($path, $prefix, $priority);
  34. } catch (RokCommon_ClassLoader_Exception $e) {
  35. throw new RokCommon_Ajax_Exception('Error adding model path.', 0, $e);
  36. }
  37. }
  38. /**
  39. * @param string $model
  40. *
  41. * @return RokCommon_Ajax_Model
  42. */
  43. protected static function &getModel($model)
  44. {
  45. $container = RokCommon_Service::getContainer();
  46. /** @var $ajaxModelLoader RokCommon_ClassLoader_PrefixedLoader */
  47. $ajaxModelLoader = $container->getService('ajax.model.loader');
  48. $model = $ajaxModelLoader->getItem($model, null, 'RokCommon_Ajax_Model');
  49. return $model;
  50. }
  51. /**
  52. * @param string $model
  53. * @param string $action
  54. * @param array $params
  55. *
  56. * @throws RokCommon_Ajax_Exception
  57. * @return string
  58. */
  59. public static function run($model, $action, $params, $encoding = self::JSON_ENCODING)
  60. {
  61. // Set up an independent AJAX error handler
  62. set_error_handler(array('RokCommon_Ajax', 'error_handler'));
  63. set_exception_handler(array('RokCommon_Ajax', 'exception_handler'));
  64. while (@ob_end_clean()) ; // clean any pending output buffers
  65. ob_start(); // start a fresh one
  66. $result = null;
  67. try {
  68. // get a model class instance
  69. $modelInstance = self::getModel($model);
  70. if ($encoding == self::JSON_ENCODING) {
  71. $decoded_params = json_decode($params);
  72. if (null == $decoded_params && strlen($params) > 0) {
  73. throw new RokCommon_Ajax_Exception('Invalid JSON for params');
  74. }
  75. $params = $decoded_params;
  76. }
  77. // set the result to the run
  78. $result = $modelInstance->run($action, $params);
  79. } catch (Exception $ae) {
  80. $result = new RokCommon_Ajax_Result();
  81. $result->setAsError();
  82. $result->setMessage($ae->getMessage());
  83. }
  84. $encoded_result = json_encode($result);
  85. // restore normal error handling;
  86. restore_error_handler();
  87. restore_exception_handler();
  88. return $encoded_result;
  89. }
  90. /**
  91. * @static
  92. *
  93. * @param Exception $exception
  94. */
  95. public static function exception_handler(Exception $exception)
  96. {
  97. echo "Uncaught Exception: " . $exception->getMessage() . "\n";
  98. echo '[' . $exception->getCode() . '] File: ' . $exception->getFile() . ' Line: ' . $exception->getLine();
  99. }
  100. /**
  101. * @static
  102. *
  103. * @param $errno
  104. * @param $errstr
  105. * @param $errfile
  106. * @param $errline
  107. *
  108. * @return bool
  109. * @throws RokCommon_Ajax_Exception
  110. */
  111. public static function error_handler($errno, $errstr, $errfile, $errline)
  112. {
  113. if (!(error_reporting() & $errno)) {
  114. // This error code is not included in error_reporting
  115. return;
  116. }
  117. switch ($errno) {
  118. case E_USER_ERROR:
  119. echo "ERROR [$errno] $errstr\n";
  120. echo " Fatal error on line $errline in file $errfile";
  121. echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")\n";
  122. echo "Aborting...\n";
  123. exit(1);
  124. break;
  125. case E_USER_WARNING:
  126. echo "WARNING [$errno] $errstr\n";
  127. break;
  128. case E_USER_NOTICE:
  129. echo "NOTICE [$errno] $errstr\n";
  130. break;
  131. case E_STRICT:
  132. return false;
  133. break;
  134. default:
  135. throw new RokCommon_Ajax_Exception("UNHANDLED ERROR [$errno] $errstr $errfile:$errline");
  136. break;
  137. }
  138. /* Don't execute PHP internal error handler */
  139. return true;
  140. }
  141. /**
  142. * @param $str
  143. *
  144. * @return string
  145. */
  146. public static function smartStripSlashes($str)
  147. {
  148. $cd1 = substr_count($str, "\"");
  149. $cd2 = substr_count($str, "\\\"");
  150. $cs1 = substr_count($str, "'");
  151. $cs2 = substr_count($str, "\\'");
  152. $tmp = strtr($str, array(
  153. "\\\"" => "",
  154. "\\'" => ""
  155. ));
  156. $cb1 = substr_count($tmp, "\\");
  157. $cb2 = substr_count($tmp, "\\\\");
  158. if ($cd1 == $cd2 && $cs1 == $cs2 && $cb1 == 2 * $cb2) {
  159. return strtr($str, array(
  160. "\\\"" => "\"",
  161. "\\'" => "'",
  162. "\\\\" => "\\"
  163. ));
  164. }
  165. return $str;
  166. }
  167. }