Action.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Execute an action (request which no need html view response: ajax,json etc...) and manage automatically
  4. * access rights, exceptions and json response.
  5. *
  6. * @author Valentin CARRUESCO
  7. *
  8. * @category Core
  9. *
  10. * @license cc by nc sa
  11. */
  12. class Action {
  13. /**
  14. * Execute an action
  15. * #### Example
  16. * ```php
  17. * Action::write(function(&$response){
  18. * $response['custom'] = 'hello world!';
  19. * },array('user'=>'u','plugin'=>'d')); //User must have user update right and delete plugin right to perform this action
  20. * ```.
  21. *
  22. * @param function action/code to execute
  23. * @param array Array wich contain right to execute action
  24. *
  25. * @return print json response
  26. */
  27. public static function write($f, $p=array()) {
  28. global $myUser;
  29. header('content-type:application/json');
  30. $response = array();
  31. set_error_handler(function ($level, $error, $file, $line) { throw new Exception($error." \r\n\r\n ".$file.' - L '.$line.'');});
  32. try {
  33. foreach ($p as $scope => $right) {
  34. if (!$myUser->can($scope, $right)) throw new Exception('Vous ne disposez pas des droits suffisants pour effectuer cette action');
  35. }
  36. $f($response);
  37. } catch (Exception $e) {
  38. $response['error'] = $e->getMessage();
  39. $response['errorCode'] = $e->getCode();
  40. $response['trace'] = exception_trace($e);
  41. if($myUser->superadmin || $_SERVER['HTTP_HOST'] == '127.0.0.1'){
  42. $traces = $e->getTrace();
  43. $traceIndex = 0;
  44. //Si c'est une erreur de bound sql / data ou une erreur de syntaxe sql
  45. if($e->getCode() == 'HY093' || $e->getCode() == '42000'){
  46. //On recupere la premiere trace qui n'est pas Entity ou Action (trace significative)
  47. foreach($traces as $i => $currentTrace){
  48. if(isset($currentTrace['file']) && in_array(basename($currentTrace['file']), array('Entity.class.php','Action.class.php'))) continue;
  49. $traceIndex = $i;
  50. break;
  51. }
  52. //On affiche la requete et les datas
  53. if(isset($traces[1]) && $traces[1]['function']=='customQuery'){
  54. if(isset($traces[1]['args'])) $response['error'] .= '<code class="d-block">'.(is_string($traces[1]['args'][0]) ? $traces[1]['args'][0]:json_encode($traces[1]['args'][0])).'</code>';
  55. if(isset($traces[1]['args']) && isset($traces[1]['args'][1])){
  56. $plainTrace = $traces[1]['args'][1];
  57. if(is_array($plainTrace)) $plainTrace = implode(',',$plainTrace);
  58. if(is_object($plainTrace)) $plainTrace = implode(',',(array)$plainTrace);
  59. $response['error'] .= '<hr><code>['.$plainTrace.']</code>';
  60. }
  61. }
  62. }
  63. $response['line'] = isset($traces[$traceIndex]['line']) ? $traces[$traceIndex]['line'] : 0;
  64. $response['file'] = isset( $traces[$traceIndex]['file'])? $traces[$traceIndex]['file'] : '';
  65. $response['trace'] = '<pre class="text-light" style="overflow:initial;">'.exception_trace($e).'</pre>';
  66. }
  67. }
  68. echo json_encode($response);
  69. restore_error_handler();
  70. }
  71. public static function register($name,$f) {
  72. $GLOBALS['actions'][$name] = $f;
  73. }
  74. public static function run($name, &$response) {
  75. if(!isset($GLOBALS['actions'][$name])) return;
  76. $functionName = $GLOBALS['actions'][$name];
  77. call_user_func_array($functionName, array(&$response));
  78. }
  79. }