PageRenderTime 67ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 1ms

/www/api/_functions.php

https://gitlab.com/bradanlane/AirPRS
PHP | 227 lines | 167 code | 48 blank | 12 comment | 24 complexity | 84dce3a4151d2dbcb5d162e952767d4c MD5 | raw file
  1. <?php
  2. define("MAX_ROWS", 200);
  3. $conf = parse_ini_file("/appdata/airprs/php_db_settings.ini");
  4. $default_key = "af14a856-ae0e-44f1-878b-d4f0a747d762";
  5. $dev_key = "12345678-abcd-4567-abcd-1234567890ab";
  6. $tracking_label = "";
  7. $ignore_analytics = false;
  8. $regex_date = '/(20)\d\d\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/';
  9. $regex_call = '/([A-Z0-9]{4,6})(-[0-9]{1,2})|()/';
  10. $regex_integer = '/[-+]?\d+/';
  11. $regex_float = '/[-+]?\d*\.?\d*/';
  12. $regex_uuid4 = '/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}/';
  13. date_default_timezone_set ('UTC');
  14. function enableAnalytics()
  15. {
  16. global $ignore_analytics;
  17. $ignore_analytics = false;
  18. }
  19. function disableAnalytics()
  20. {
  21. global $ignore_analytics;
  22. $ignore_analytics = true;
  23. }
  24. function doAnalytics()
  25. {
  26. global $ignore_analytics;
  27. return(!($ignore_analytics));
  28. }
  29. function isDeveloperKey($key)
  30. {
  31. global $dev_key;
  32. return (strcmp($key, $dev_key) == 0);
  33. }
  34. function getAPIKey()
  35. {
  36. global $default_key;
  37. global $regex_uuid4;
  38. $key = ($_GET['key']);
  39. if (isset($key) && (preg_match($regex_uuid4, $key)))
  40. return $key;
  41. $temp_key = $_SERVER['REMOTE_ADDR'];
  42. $parts = explode('.', $temp_key);
  43. $temp_key = sprintf('%02x%02x%02x%02x', $parts[0], $parts[1], $parts[2], $parts[3]);
  44. $temp_key = $temp_key . substr($default_key, 8);
  45. printf ("key = %s\n", $temp_key);
  46. return $temp_key;
  47. }
  48. function getTrackingLabel()
  49. {
  50. global $tracking_label;
  51. return $tracking_label;
  52. }
  53. function addToTrackingLabel ($arg, $val)
  54. {
  55. global $tracking_label;
  56. if (strlen($tracking_label) > 0)
  57. $tracking_label = $tracking_label . ":";
  58. $tracking_label = $tracking_label . $arg . "=" . strval($val);
  59. }
  60. function getArgIntegerOrValue($arg, $def)
  61. {
  62. global $regex_integer;
  63. if (!isset($_GET[$arg])) $val = $def;
  64. else if (!preg_match($regex_integer, $_GET[$arg])) $val = $def;
  65. else /* return good value */ $val = intval($_GET[$arg]);
  66. addToTrackingLabel ($arg, $val);
  67. return $val;
  68. }
  69. function getArgFloatOrValue($arg, $def)
  70. {
  71. global $regex_float;
  72. if (!isset($_GET[$arg])) $val = $def;
  73. else if (!preg_match($regex_integer, $_GET[$arg])) $val = $def;
  74. else /* return good value */ $val = floatval($_GET[$arg]);
  75. addToTrackingLabel ($arg, $val);
  76. return $val;
  77. }
  78. function getArgCallsignOrValue($arg, $def)
  79. {
  80. global $regex_call;
  81. if (!isset($_GET[$arg])) $val = $def;
  82. else if (!preg_match($regex_call, $_GET[$arg])) $val = $def;
  83. else /* return good value */ $val = $_GET[$arg];
  84. addToTrackingLabel ($arg, $val);
  85. return $val;
  86. }
  87. function getQueryResults ($query)
  88. {
  89. global $conf;
  90. $count = 0;
  91. $data = array();
  92. $output = array();
  93. $names = array();
  94. // NOTE: we return errors via the array but we deliberately do not expose anything from the MySQL system
  95. // $output['sql'] = $query; // THIS IS BAD - we only do this is test
  96. //open connection to mysql db
  97. $connection = mysqli_connect( $conf['host'], $conf['user'], $conf['secret'], $conf['db'] );
  98. if (! $connection) {
  99. $output['success'] = false;
  100. $output['error'] = "unable to access data";
  101. // $output['mysql'] = mysqli_error($connection); // THIS IS BAD - we only do this is test
  102. } else {
  103. //fetch table rows from mysql db
  104. $result = mysqli_query($connection, $query);
  105. if (! $result) {
  106. $output['success'] = false;
  107. $output['error'] = "unable to find data";
  108. // $output['mysql'] = mysqli_error($connection); // THIS IS BAD - we only do this is test
  109. } else {
  110. // get all of the field names
  111. $info = mysqli_fetch_fields($result);
  112. foreach ($info as $field)
  113. $names[] = $field->name;
  114. //create an array
  115. while($row = mysqli_fetch_assoc($result)) {
  116. $count = $count + 1;
  117. $data[] = $row;
  118. }
  119. if (! $count) {
  120. $output['success'] = false;
  121. $output['error'] = "no data available";
  122. // $output['mysql'] = mysqli_error($connection); // THIS IS BAD - we only do this is test
  123. } else {
  124. $output['success'] = true;
  125. $output['series'] = $names;
  126. $output['count'] = $count;
  127. $output['data'] = $data;
  128. }
  129. mysqli_free_result($result);
  130. }
  131. // clean up db connection
  132. mysqli_close ($connection);
  133. }
  134. $reply = sendAnalytics($count);
  135. // $output['analytics'] = $reply;
  136. return $output;
  137. }
  138. function sendAnalytics ($num)
  139. {
  140. global $dev_key;
  141. global $ignore_analytics;
  142. $key = getAPIKey();
  143. if (isDeveloperKey($key))
  144. $tid = 'UA-82222002-3';
  145. else
  146. $tid = 'UA-82222002-4';
  147. if (!doAnalytics())
  148. return "ignoring analytics";
  149. $file_parts = pathinfo($_SERVER['PHP_SELF']);
  150. $data = array (
  151. 'v' => 1,
  152. 'tid' => $tid,
  153. 't' => 'event',
  154. 'ec' => "api",
  155. 'ea' => $file_parts['filename'],
  156. 'el' => getTrackingLabel(),
  157. 'ev' => $num,
  158. 'cid' => $key,
  159. 'cn' => 'weeklyusage',
  160. 'ck' => gmdate('Y-W')
  161. );
  162. //$url = 'https://www.google-analytics.com/debug/collect';
  163. $url = 'https://www.google-analytics.com/collect';
  164. $content = http_build_query($data);
  165. $content = utf8_encode($content);
  166. $user_agent = 'Example/1.0 (http://example.com/)';
  167. $ch = curl_init();
  168. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  169. curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
  170. curl_setopt($ch, CURLOPT_URL, $url);
  171. curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
  172. curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
  173. curl_setopt($ch,CURLOPT_POST, TRUE);
  174. curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
  175. $reply = curl_exec($ch);
  176. curl_close($ch);
  177. return $reply;
  178. }
  179. ?>