SwVitaliy

Untitled

Apr 13th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. <?php header('content-type: application/json; charset=utf-8');
  2.  
  3. function is_valid_callback($subject)
  4. {
  5.     $identifier_syntax
  6.       = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';
  7.  
  8.     $reserved_words = array('break', 'do', 'instanceof', 'typeof', 'case',
  9.       'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue',
  10.       'for', 'switch', 'while', 'debugger', 'function', 'this', 'with',
  11.       'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum',
  12.       'extends', 'super', 'const', 'export', 'import', 'implements', 'let',
  13.       'private', 'public', 'yield', 'interface', 'package', 'protected',
  14.       'static', 'null', 'true', 'false');
  15.  
  16.     return preg_match($identifier_syntax, $subject)
  17.         && ! in_array(mb_strtolower($subject, 'UTF-8'), $reserved_words);
  18. }
  19.  
  20. $data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
  21. $json = json_encode($data);
  22.  
  23. # JSON if no callback
  24. if( ! isset($_GET['callback']))
  25.     exit($json);
  26.  
  27. # JSONP if valid callback
  28. if(is_valid_callback($_GET['callback']))
  29.     exit("{$_GET['callback']}($json)");
  30.  
  31. # Otherwise, bad request
  32. header('status: 400 Bad Request', true, 400);
Advertisement
Add Comment
Please, Sign In to add comment