app-pg.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. require 'dbraw.php';
  3. DbRaw::init();
  4. function db()
  5. {
  6. ngx_header_set('Content-Type', 'application/json');
  7. DbRaw::$random->execute([mt_rand(1, 10000)]);
  8. echo json_encode(DbRaw::$random->fetch(), JSON_NUMERIC_CHECK);
  9. }
  10. function query()
  11. {
  12. ngx_header_set('Content-Type', 'application/json');
  13. $query_count = 1;
  14. $params = (int) ngx::query_args()['q'];
  15. if ($params > 1) {
  16. $query_count = min($params, 500);
  17. }
  18. while ($query_count--) {
  19. DbRaw::$random->execute([mt_rand(1, 10000)]);
  20. $arr[] = DbRaw::$random->fetch();
  21. }
  22. echo json_encode($arr, JSON_NUMERIC_CHECK);
  23. }
  24. function update()
  25. {
  26. ngx_header_set('Content-Type', 'application/json');
  27. $query_count = 1;
  28. $params = (int) ngx::query_args()['q'];
  29. if ($params > 1) {
  30. $query_count = min($params, 500);
  31. }
  32. while ($query_count--) {
  33. DbRaw::$random->execute([mt_rand(1, 10000)]);
  34. $row = DbRaw::$random->fetch();
  35. $row['randomNumber'] = mt_rand(1, 10000);
  36. $worlds[] = $row;
  37. }
  38. DbRaw::update($worlds);
  39. echo json_encode($worlds, JSON_NUMERIC_CHECK);
  40. }
  41. function fortune()
  42. {
  43. ngx_header_set('Content-Type', 'text/html;charset=UTF-8');
  44. DbRaw::$fortune->execute();
  45. $arr = DbRaw::$fortune->fetchAll(PDO::FETCH_KEY_PAIR);
  46. $arr[0] = 'Additional fortune added at request time.';
  47. asort($arr);
  48. $html = '';
  49. foreach ($arr as $id => $message) {
  50. $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  51. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  52. }
  53. echo "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>";
  54. }