/beacon/events/index.php

http://showslow.googlecode.com/ · PHP · 105 lines · 92 code · 12 blank · 1 comment · 18 complexity · a20d57566e3ba97b2d75bfd7a1d0a02a MD5 · raw file

  1. <?php
  2. require_once(dirname(dirname(dirname(__FILE__))).'/global.php');
  3. if (array_key_exists('title', $_GET) && $_GET['title'] != ''
  4. && array_key_exists('url_prefix', $_GET) && filter_var($_GET['url_prefix'], FILTER_VALIDATE_URL) !== false
  5. )
  6. {
  7. $url = validateURL($_GET['url_prefix'], $outputerror);
  8. $type = array_key_exists('type', $_GET) && $_GET['type'] != '' ? $_GET['type'] : FALSE;
  9. $start = array_key_exists('start', $_GET) && $_GET['start'] != '' ? $_GET['start'] : FALSE;
  10. $end = array_key_exists('end', $_GET) && $_GET['type'] != '' ? $_GET['end'] : FALSE;
  11. $resource_url = filter_var($_GET['resource_url'], FILTER_VALIDATE_URL);
  12. $query = sprintf('INSERT INTO event (
  13. url_prefix,
  14. title,
  15. start'
  16. .($type !== FALSE ? ', type' : '')
  17. .($end !== FALSE ? ', end' : '')
  18. .($resource_url !== FALSE ? ', resource_url' : '')
  19. .") VALUES (
  20. '%s',
  21. '%s',
  22. '%s'"
  23. .($type !== FALSE ? ", '%s'" : '')
  24. .($end !== FALSE ? ", '%s'" : '')
  25. .($resource_url !== FALSE ? ", '%s'" : '')
  26. .')',
  27. mysql_real_escape_string($url),
  28. mysql_real_escape_string($_GET['title']),
  29. mysql_real_escape_string($start),
  30. mysql_real_escape_string($type),
  31. mysql_real_escape_string($end),
  32. mysql_real_escape_string($resource_url)
  33. );
  34. if (!mysql_query($query))
  35. {
  36. beaconError(mysql_error());
  37. }
  38. # updating last_event_update for the matching URLs
  39. $query = sprintf("UPDATE urls SET last_event_update = NOW() WHERE INSTR(url, '%s') = 1",
  40. mysql_real_escape_string($url)
  41. );
  42. $result = mysql_query($query);
  43. if (!$result) {
  44. beaconError(mysql_error());
  45. }
  46. if (array_key_exists('manual', $_GET))
  47. {
  48. ?><html>
  49. <head>
  50. <title>Event added</title>
  51. </head>
  52. <body>
  53. <h1>Event added</h1>
  54. <p>Event successfully added.</p>
  55. <p>Add <a href="./">one more</a>.</p>
  56. </body></html>
  57. <?php
  58. exit;
  59. }
  60. } else {
  61. header('HTTP/1.0 400 Bad Request');
  62. ?><html>
  63. <head>
  64. <title>Bad Request: Event beacon</title>
  65. <style>
  66. i {
  67. color: red;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <h1>Bad Request: Event beacon</h1>
  73. <p>This is an event beacon for ShowSlow.</p>
  74. <p>You can use automated script to publish events using GET call to this URL:</p>
  75. <b><pre><?php echo $showslow_base?>beacon/events/?type=<i>sometype</i>&amp;url_prefix=<i>url_prefix</i>&amp;title=<i>some+title</i>&amp;start=<i><?php echo urlencode(date("Y-m-d"))?></i>&amp;end=<i><?php echo urlencode(date("Y-m-d"))?></i>&amp;resource_url=<i>link+to+page</i></pre></b>
  76. or use form below to manually enter events.
  77. <h2>Add an event</h2>
  78. <form action="" method="GET">
  79. <table>
  80. <tr><td>Type:</td><td><input type="text" name="type" size="25"/> (25 characters max)</td></tr>
  81. <tr><td>URL prefix:</td><td><input type="text" name="url_prefix" value="http://www.example.com/" size="80"/></td></tr>
  82. <tr><td>Event title:</td><td><input type="text" name="title" size="80"/></td></tr>
  83. <tr valign="top"><td>Start time:</td><td><input type="text" name="start" value="<?php echo date("Y-m-d H:i:s");?>"/> (leave blank for current time)<br/>Time in MySQL <a href="http://dev.mysql.com/doc/refman/5.1/en/datetime.html">timestamp format</a></td></tr>
  84. <tr valign="top"><td>End time:</td><td><input type="text" name="end"/> (or blank for momentary event)<br/>Time in MySQL <a href="http://dev.mysql.com/doc/refman/5.1/en/datetime.html">timestamp format</a></td></tr>
  85. <tr><td>Resource URL:</td><td><input type="text" name="resource_url" size="80"/></td></tr>
  86. <tr><td></td><td><input type="submit" name="manual" value="add"/></td></tr>
  87. </table>
  88. </form>
  89. </body></html>
  90. <?php
  91. exit;
  92. }
  93. header('HTTP/1.0 204 Data accepted');