Example #1
0
 protected function verify()
 {
     if (empty($this->id)) {
         $user = apiDB::getUserByEmail($_SERVER['PHP_AUTH_USER']);
         $this->id = $user->id;
     }
     if (empty($this->id)) {
         return $this->_response("User id not set. Cannot run \"verify\" without a valid user id.", 404);
     }
     if (empty($_GET["token"])) {
         return $this->_response("No token specified. Cannot run \"verify\" without a valid \"token\" parameter.", 404);
     }
     $message = "";
     $code = apiDB::verifyUser($this->id, $_GET["token"], $message);
     return $this->_response($message, $code);
 }
Example #2
0
 static function graphData($type, $locationid, $period, $graph, $timezone)
 {
     if (empty($locationid)) {
         return "ERROR, no measurement id specified for deleting measurement";
     }
     $conxn = apiDB::getConnection();
     $sql = "SELECT CASE WHEN r." . $type . " IS NULL THEN 0 ELSE r." . $type . " END, d.dt as fdate, CASE WHEN r.days IS NULL THEN 1 ELSE r.days END, CASE WHEN r.d_ave IS NULL THEN 0 ELSE r.d_ave END\n\t\t\tFROM getAllDays(CURRENT_DATE, " . $period . ") d left join (\n\t\t\t\tSELECT " . $type . ", cast(todate - interval '8 hours' - interval '1 second' as date) as todate, CEIL(CAST ((EXTRACT(EPOCH FROM todate)-EXTRACT(EPOCH FROM fromdate)) / 86400.0 AS NUMERIC)) AS days,\n\t\t\t\t\tROUND( CAST((" . $type . "/((EXTRACT(EPOCH FROM todate)-EXTRACT(EPOCH FROM fromdate)) / 86400.0)) AS NUMERIC), 1) AS d_ave\n\t\t\t\tFROM " . $type . "measurement\n\t\t\t\tWHERE locationid = " . $locationid . "\n\t\t\t\t) r ON d.dt = r.todate\n\t\t\tORDER BY d.dt DESC ";
     //error_log( $sql2);
     $result = pg_query($conxn, $sql);
     $results_array = array();
     $data = array();
     if ($result) {
         while ($row = pg_fetch_array($result)) {
             for ($i = 0; $i < $row["days"]; $i++) {
                 $date = new DateTime($row["fdate"], new DateTimeZone($timezone));
                 $date->sub(new DateInterval('P' . $i . 'D'));
                 $item = array();
                 array_push($item, date_format($date, 'm/d'));
                 array_push($item, floatval($row["d_ave"]));
                 array_push($data, $item);
                 if ($i > 0) {
                     pg_fetch_array($result);
                 }
                 //otherwise the range shows up in duplicate
             }
         }
         //$datasets = Array();
         //$datasets["type"] = $graph;
         //$datasets["data"] = array_reverse($data);
         //$results_array["JSChart"]["datasets"][0] = $datasets;
     }
     //return $results_array;
     return array_reverse($data);
 }
 function apiLink()
 {
     $useridString = empty($this->userid) ? "" : "/users/" . $this->userid;
     $locationidString = empty($this->locationid) ? "" : "/locations/" . $this->locationid;
     $linkString = "https://" . apiDB::getServerName() . "/" . apiDB::dirname() . $useridString . $locationidString . "/" . $this->columnName() . "/" . $this->id;
     return "<a href=\"" . $linkString . "\">" . $this->fromdate . "->" . $this->todate . "</a>";
 }
Example #4
0
<?php

include_once 'User.class.php';
// Requests from the same server don't have a HTTP_ORIGIN header
if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
    $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
}
//echo "_REQUEST : " . var_dump($_REQUEST)."<br/><br/>";  // requests starts with everything AFTER 10.0.0.10/api/
//excho "_SERVER[REQUEST_URI] : " . var_dump($_SERVER['REQUEST_URI'])."<br/><br/>";
try {
    $user = array_key_exists('PHP_AUTH_USER', $_SERVER) ? $_SERVER['PHP_AUTH_USER'] : "";
    $pass = array_key_exists('PHP_AUTH_PW', $_SERVER) ? $_SERVER['PHP_AUTH_PW'] : "";
    $pass_hash = substr(crypt($pass, '$2y$09$' . apiDB::getPWSalt() . '$'), 29);
    $message = "";
    $validated = apiDB::validate($user, $pass_hash, $message);
    if ($validated < -1) {
        header('WWW-Authenticate: Basic realm="SASSCAL Weather"');
        header('HTTP/1.0 401 Unauthorized');
        die($message . $validated);
    } else {
        if ($validated < 0) {
            echo $message;
        } else {
            error_log("AUTHORIZED AS -" . $user . "- -" . $pass_hash . "-\r\n", 3, "/var/tmp/auth.log");
            $args = explode('/', rtrim($_REQUEST['request'], '/'));
            $firstclass = strtolower(array_shift($args));
            //pop first object off the URL
            $extension = "html";
            //ignore the extension if there is one.
            if (sizeof($args) == 0 && strrpos($firstclass, ".") !== false) {
                $extension = substr($firstclass, strrpos($firstclass, ".") + 1);
Example #5
0
<?php

include_once 'User.class.php';
// Requests from the same server don't have a HTTP_ORIGIN header
if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
    $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
}
//echo "_REQUEST : " . var_dump($_REQUEST)."<br/><br/>";  // requests starts with everything AFTER 10.0.0.10/api/
//excho "_SERVER[REQUEST_URI] : " . var_dump($_SERVER['REQUEST_URI'])."<br/><br/>";
try {
    $user = array_key_exists('PHP_AUTH_USER', $_SERVER) ? $_SERVER['PHP_AUTH_USER'] : "";
    $pass = array_key_exists('PHP_AUTH_PW', $_SERVER) ? $_SERVER['PHP_AUTH_PW'] : "";
    $validated = apiDB::validate($user, $pass);
    if ($validated < 0) {
        header('WWW-Authenticate: Basic realm="SASSCAL Weather"');
        header('HTTP/1.0 401 Unauthorized');
        die("Not authorized " . $validated);
    } else {
        error_log("AUTHORIZED AS -" . $user . "- -" . $pass . "-");
    }
    $args = explode('/', rtrim($_REQUEST['request'], '/'));
    $firstclass = strtolower(array_shift($args));
    //pop first object off the URL
    $extension = "html";
    //ignore the extension if there is one.
    if (sizeof($args) == 0 && strrpos($firstclass, ".") !== false) {
        $extension = substr($firstclass, strrpos($firstclass, ".") + 1);
        $firstclass = substr($firstclass, 0, strrpos($firstclass, "."));
    }
    $firstclass = $firstclass[strlen($firstclass) - 1] == 's' ? ucfirst(substr($firstclass, 0, strlen($firstclass) - 1)) : ucfirst($firstclass);
    $reflector = new ReflectionClass($firstclass);
Example #6
0
 static function graphData($type, $locationid, $period, $graph, $timezone)
 {
     if (empty($locationid)) {
         return "ERROR, no measurement id specified for deleting measurement";
     }
     $conxn = apiDB::getConnection();
     $sql_old = "SELECT " . $type . ", todate::date, ROUND(CAST ((EXTRACT(EPOCH FROM todate)-EXTRACT(EPOCH FROM fromdate)) / 86400.0 AS NUMERIC), 0) AS days, \n                               ROUND( CAST((rain/((EXTRACT(EPOCH FROM todate)-EXTRACT(EPOCH FROM fromdate)) / 86400.0)) AS NUMERIC), 1) AS d_ave \n                        FROM " . $type . "measurement \n                        WHERE locationid = " . $locationid . "\n                           AND fromdate >= CURRENT_TIMESTAMP - INTERVAL '" . $period . " days'\n                        ORDER BY fromdate ";
     $sql = "SELECT CASE WHEN r." . $type . " IS NULL THEN 0 ELSE r." . $type . " END, d.dt as fdate, CASE WHEN r.days IS NULL THEN 1 ELSE r.days END, CASE WHEN r.d_ave IS NULL THEN 0 ELSE r.d_ave END\n\t\t\tFROM getAllDays(CURRENT_DATE, " . $period . ") d left join (\n\t\t\t\tSELECT " . $type . ", fromdate::date, ROUND(CAST ((EXTRACT(EPOCH FROM todate)-EXTRACT(EPOCH FROM fromdate)) / 86400.0 AS NUMERIC), 0) AS days,\n\t\t\t\t\tROUND( CAST((" . $type . "/((EXTRACT(EPOCH FROM todate)-EXTRACT(EPOCH FROM fromdate)) / 86400.0)) AS NUMERIC), 1) AS d_ave\n\t\t\t\tFROM " . $type . "measurement\n\t\t\t\tWHERE locationid = " . $locationid . "\n\t\t\t\t) r ON d.dt = r.fromdate\n\t\t\tORDER BY d.dt ";
     //error_log( $sql2);
     $result = pg_query($conxn, $sql);
     $results_array = array();
     $data = array();
     if ($result) {
         while ($row = pg_fetch_array($result)) {
             for ($i = 0; $i < $row["days"]; $i++) {
                 $date = new DateTime($row["fdate"], new DateTimeZone($timezone));
                 $date->add(new DateInterval('P' . $i . 'D'));
                 $item = array();
                 array_push($item, date_format($date, 'm/d'));
                 array_push($item, floatval($row["d_ave"]));
                 array_push($data, $item);
             }
         }
         $datasets = array();
         $datasets["type"] = $graph;
         $datasets["data"] = $data;
         $results_array["JSChart"]["datasets"][0] = $datasets;
     }
     //return $results_array;
     return $data;
 }
 protected function graph()
 {
     $type = empty($_GET["type"]) ? "rain" : $_GET["type"];
     $period = empty($_GET["period"]) ? 30 : $_GET["period"];
     $graph = empty($_GET["graph"]) ? "bar" : $_GET["graph"];
     if (empty($this->id)) {
         return $this->_response("Location id not set. Cannot run \"graph\" without a valid location id.", 404);
     }
     return $this->display(apiDB::graphData($type, $this->id, $period, $graph, 'Africa/Windhoek'));
 }