/www/api/_functions.php
PHP | 227 lines | 167 code | 48 blank | 12 comment | 24 complexity | 84dce3a4151d2dbcb5d162e952767d4c MD5 | raw file
- <?php
- define("MAX_ROWS", 200);
- $conf = parse_ini_file("/appdata/airprs/php_db_settings.ini");
- $default_key = "af14a856-ae0e-44f1-878b-d4f0a747d762";
- $dev_key = "12345678-abcd-4567-abcd-1234567890ab";
- $tracking_label = "";
- $ignore_analytics = false;
- $regex_date = '/(20)\d\d\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/';
- $regex_call = '/([A-Z0-9]{4,6})(-[0-9]{1,2})|()/';
- $regex_integer = '/[-+]?\d+/';
- $regex_float = '/[-+]?\d*\.?\d*/';
- $regex_uuid4 = '/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}/';
- date_default_timezone_set ('UTC');
- function enableAnalytics()
- {
- global $ignore_analytics;
- $ignore_analytics = false;
- }
- function disableAnalytics()
- {
- global $ignore_analytics;
- $ignore_analytics = true;
- }
- function doAnalytics()
- {
- global $ignore_analytics;
- return(!($ignore_analytics));
- }
- function isDeveloperKey($key)
- {
- global $dev_key;
- return (strcmp($key, $dev_key) == 0);
- }
- function getAPIKey()
- {
- global $default_key;
- global $regex_uuid4;
-
- $key = ($_GET['key']);
- if (isset($key) && (preg_match($regex_uuid4, $key)))
- return $key;
- $temp_key = $_SERVER['REMOTE_ADDR'];
- $parts = explode('.', $temp_key);
- $temp_key = sprintf('%02x%02x%02x%02x', $parts[0], $parts[1], $parts[2], $parts[3]);
- $temp_key = $temp_key . substr($default_key, 8);
- printf ("key = %s\n", $temp_key);
- return $temp_key;
- }
- function getTrackingLabel()
- {
- global $tracking_label;
- return $tracking_label;
- }
- function addToTrackingLabel ($arg, $val)
- {
- global $tracking_label;
- if (strlen($tracking_label) > 0)
- $tracking_label = $tracking_label . ":";
- $tracking_label = $tracking_label . $arg . "=" . strval($val);
- }
- function getArgIntegerOrValue($arg, $def)
- {
- global $regex_integer;
- if (!isset($_GET[$arg])) $val = $def;
- else if (!preg_match($regex_integer, $_GET[$arg])) $val = $def;
- else /* return good value */ $val = intval($_GET[$arg]);
- addToTrackingLabel ($arg, $val);
- return $val;
- }
- function getArgFloatOrValue($arg, $def)
- {
- global $regex_float;
- if (!isset($_GET[$arg])) $val = $def;
- else if (!preg_match($regex_integer, $_GET[$arg])) $val = $def;
- else /* return good value */ $val = floatval($_GET[$arg]);
- addToTrackingLabel ($arg, $val);
- return $val;
- }
- function getArgCallsignOrValue($arg, $def)
- {
- global $regex_call;
- if (!isset($_GET[$arg])) $val = $def;
- else if (!preg_match($regex_call, $_GET[$arg])) $val = $def;
- else /* return good value */ $val = $_GET[$arg];
- addToTrackingLabel ($arg, $val);
- return $val;
- }
- function getQueryResults ($query)
- {
- global $conf;
- $count = 0;
- $data = array();
- $output = array();
- $names = array();
- // NOTE: we return errors via the array but we deliberately do not expose anything from the MySQL system
- // $output['sql'] = $query; // THIS IS BAD - we only do this is test
-
- //open connection to mysql db
- $connection = mysqli_connect( $conf['host'], $conf['user'], $conf['secret'], $conf['db'] );
- if (! $connection) {
- $output['success'] = false;
- $output['error'] = "unable to access data";
- // $output['mysql'] = mysqli_error($connection); // THIS IS BAD - we only do this is test
- } else {
- //fetch table rows from mysql db
- $result = mysqli_query($connection, $query);
- if (! $result) {
- $output['success'] = false;
- $output['error'] = "unable to find data";
- // $output['mysql'] = mysqli_error($connection); // THIS IS BAD - we only do this is test
- } else {
- // get all of the field names
- $info = mysqli_fetch_fields($result);
- foreach ($info as $field)
- $names[] = $field->name;
- //create an array
- while($row = mysqli_fetch_assoc($result)) {
- $count = $count + 1;
- $data[] = $row;
- }
- if (! $count) {
- $output['success'] = false;
- $output['error'] = "no data available";
- // $output['mysql'] = mysqli_error($connection); // THIS IS BAD - we only do this is test
- } else {
- $output['success'] = true;
- $output['series'] = $names;
- $output['count'] = $count;
- $output['data'] = $data;
- }
- mysqli_free_result($result);
- }
- // clean up db connection
- mysqli_close ($connection);
- }
-
- $reply = sendAnalytics($count);
- // $output['analytics'] = $reply;
- return $output;
- }
-
- function sendAnalytics ($num)
- {
- global $dev_key;
- global $ignore_analytics;
-
- $key = getAPIKey();
- if (isDeveloperKey($key))
- $tid = 'UA-82222002-3';
- else
- $tid = 'UA-82222002-4';
- if (!doAnalytics())
- return "ignoring analytics";
- $file_parts = pathinfo($_SERVER['PHP_SELF']);
- $data = array (
- 'v' => 1,
- 'tid' => $tid,
- 't' => 'event',
- 'ec' => "api",
- 'ea' => $file_parts['filename'],
- 'el' => getTrackingLabel(),
- 'ev' => $num,
- 'cid' => $key,
- 'cn' => 'weeklyusage',
- 'ck' => gmdate('Y-W')
- );
- //$url = 'https://www.google-analytics.com/debug/collect';
- $url = 'https://www.google-analytics.com/collect';
- $content = http_build_query($data);
- $content = utf8_encode($content);
- $user_agent = 'Example/1.0 (http://example.com/)';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
- curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
- curl_setopt($ch,CURLOPT_POST, TRUE);
- curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
- $reply = curl_exec($ch);
- curl_close($ch);
- return $reply;
- }
- ?>