Example #1
0
 public static function create($username, $password, $email, $isAdmin)
 {
     $db = Config::getDb();
     // Check not exists
     $user = array('username' => $username, 'password' => md5($password), 'email' => $email, 'isAdmin' => $isAdmin);
     if ($db->users->insert($user)) {
         return $user;
     }
     return false;
 }
 public function service($name)
 {
     $start = AetherTimer::getMicroTime();
     $memcache = new Memcache();
     $memcache->connect('localhost', 11211);
     $db = Config::getDb();
     $routeSearch = new RouteSearch();
     $cacheKey = "stops_";
     $filters = array();
     if (isset($_GET['term'])) {
         $query = toLower($_GET['term']);
         $filters['search'] = new MongoRegex("/^{$query}/i");
         $cacheKey .= $query;
     } elseif (isset($_GET['lat']) && isset($_GET['long'])) {
         $lat = $_GET['lat'];
         $long = $_GET['long'];
         $regex = '/^[0-9]+\\.[0-9]*$/';
         $cacheKey .= $lat . "," . $long;
         if (preg_match($regex, $lat) && preg_match($regex, $long)) {
             $filters['location'] = array('$near' => array((double) $lat, (double) $long));
         }
     }
     if (isset($_GET['from']) && !empty($_GET['from'])) {
         $from = toLower($_GET['from']);
         $filters['connectsFrom'] = $from;
     }
     $cacheKey .= r_implode(":", $filters);
     $result = $memcache->get($cacheKey);
     if ($result) {
         $result = unserialize($result);
     } else {
         // Find stops near me!!
         $db->routes->ensureIndex(array('search' => 1));
         $filters['active'] = true;
         $stops = $db->stops->find($filters);
         $result = array('stops' => array());
         while ($stop = $stops->getNext()) {
             $result['stops'][] = array('name' => $stop['name']);
         }
         $result['generated'] = time();
         $result['length'] = count($result['stops']);
         $memcache->set($cacheKey, serialize($result), false, 7200);
     }
     $time = AetherTimer::getMicroTime() - $start;
     $result['time'] = $time;
     if (isset($_GET['callback'])) {
         return new AetherJSONPResponse($result, $_GET['callback']);
     } else {
         return new AetherJSONResponse($result);
     }
 }
 public function service($name)
 {
     if (!$this->auth()) {
         return new AetherJSONResponse(array('ok' => false, 'msg' => 'noaccess'));
     }
     if ($name == "searches") {
         // Find last 30 days
         $time = mktime(0, 0, 0) - 3600 * 24 * 30;
         $tplLogs = array();
         $db = Config::getDb();
         $logs = $db->log->find(array('time' => array('$gte' => $time)))->sort(array('time' => 1));
         $days = array(1 => 'Mandag', 2 => 'Tirsdag', 3 => 'Onsdag', 4 => 'Torsdag', 5 => 'Fredag', 6 => 'Lørdag', 7 => 'Søndag');
         // One row pr distance
         $distribution = array();
         foreach ($logs as $l) {
             $weekday = date("w", $l['time']);
             if ($weekday == 0) {
                 $weekday = 7;
             }
             $day = $days[$weekday];
             $to = $l['to'];
             $from = $l['from'];
             $cur = array('day' => $day, 'from' => $from, 'to' => $to, 'timeUsed' => $l['timeused'], 'time' => (int) $l['time'], 'hits' => (int) $l['hits']);
             $date = date("Y-m-d", $l['time']);
             if (!array_key_exists($date, $tplLogs)) {
                 $tplLogs[$date] = array('hits' => 0, 'noHits' => 0, 'day' => $day);
             } else {
                 if ($l['hits'] > 0) {
                     $tplLogs[$date]['hits']++;
                 } else {
                     $tplLogs[$date]['noHits']++;
                 }
             }
         }
         if (isset($_GET['callback'])) {
             return new AetherJSONPResponse($tplLogs, $_GET['callback']);
         } else {
             return new AetherJSONResponse($tplLogs);
         }
     }
 }
 public function run()
 {
     $tpl = $this->sl->getTemplate();
     if ($this->sl->hasObject('timer')) {
         $timer = $this->sl->get('timer');
         $timer->start('search');
     } else {
         $timer = false;
     }
     $config = $this->sl->get('aetherConfig');
     $search = new RouteSearch();
     if ($timer) {
         $timer->tick('search', 'Create search object');
     }
     $db = Config::getDb();
     if ($config->hasUrlVar("from") && $config->hasUrlVar("to")) {
         $from = urldecode($config->getUrlVar('from'));
         $to = urldecode($config->getUrlVar('to'));
         if (strlen($from) > 0 && strlen($to) > 0) {
             if ($from == $to) {
                 $tpl->set('easteregg', "samestop");
             } else {
                 $hits = $this->search($search, $from, $to, $timer);
                 if ($timer) {
                     $timer->tick('search', 'Search done');
                 }
                 $tpl->set('routes', $hits);
                 $tpl->set('from', $from);
                 $tpl->set('to', $to);
             }
         }
     }
     $activeRoutes = $search->getActiveBusNumbers();
     sort($activeRoutes);
     $tpl->set('activeRoutes', $activeRoutes);
     $tpl->set('departures', $search->getDepartureCount());
     $tpl->set('import', $db->progress->findOne(array('name' => 'import')));
     return $tpl->fetch('search.tpl');
 }
Example #5
0
<?php

require_once "Config.php";
require_once "View.php";
$db = Config::getDb();
$routes = $db->routes->find();
while ($route = $routes->getNext()) {
    echo '<pre>';
    print_r($route);
    echo '</pre>';
}
echo "foo";
Example #6
0
 /**
  * Search for routes going from from to to
  * @return array
  * @param string $from
  * @param string $to
  * @param mixed $time
  */
 public function search($from, $to, $time = false, $weekday = false, $limit = 5, $offset = 0, $timer = false)
 {
     if ($timer) {
         $timer->tick("search", "Entering RouteSearch::search");
     }
     if (!$weekday) {
         $weekday = (int) date("w");
     }
     if ($weekday == 0) {
         // sunday, fix it
         $weekday = 7;
     }
     $cacheKey = $from . $to . $time . $weekday;
     $memcache = new Memcache();
     $memcache->connect('localhost', 11211);
     $hits = $memcache->get($cacheKey);
     if ($timer) {
         $timer->tick("search", "Fetched from memcache");
     }
     if ($hits) {
         $hits = unserialize($hits);
     }
     if (!is_array($hits) || count($hits) == 0) {
         $hits = array();
         /**
          * First find all buses having both stops in their path, kinda nice
          */
         if ($timer) {
             $timer->tick("search", "No cache");
         }
         $db = Config::getDb();
         $start = toLower($from);
         $end = toLower($to);
         if ($timer) {
             $timer->tick("search", "Lowercased input");
         }
         $buses = $db->routes->find(array('search' => array('$all' => array($start, $end))));
         if ($timer) {
             $timer->tick("search", "Search routes");
         }
         // Find some necessary data
         if (!$time) {
             $time = date("Hi");
         }
         //"0700";
         $minutes = (int) substr($time, -2);
         $hours = (int) substr($time, 0, -2);
         $searchFilters = array();
         $hitCount = 0;
         $timeSort = array();
         while ($route = $buses->getNext()) {
             // Iterate over stops and find the
             $startStop = null;
             $endStop = null;
             foreach ($route['stops'] as $stop) {
                 if (!$startStop && toLower($stop['name']) == $start) {
                     $startStop = $stop;
                 } elseif ($startStop && !$endStop && toLower($stop['name']) == $end) {
                     $endStop = $stop;
                 }
             }
             if ($startStop && $endStop) {
                 /**
                  * By now we have tested that we have both stops
                  * and in the correct order for this route
                  * Its time to generate the query to find departures
                  */
                 $minuteMark = $minutes - (int) $startStop['timeOffset'];
                 $latestStartTime = date("Hi", mktime($hours, $minuteMark));
                 $startOffset = (int) $startStop['timeOffset'];
                 $endOffset = (int) $endStop['timeOffset'];
                 $runningTime = $endOffset - $startOffset;
                 $filters = array('route' => $route['_id'], 'time' => array('$gte' => (int) $latestStartTime), 'days' => $weekday);
                 $departuresForRoute = $db->departures->find($filters)->sort(array('time' => 1))->limit(15);
                 /**
                  * Loop over each departure and calculate some times
                  */
                 while ($dep = $departuresForRoute->getNext()) {
                     $arrivalTime = (int) $dep['time'];
                     $startTime = Config::timeAdd($arrivalTime, $startOffset);
                     $startM = substr($startTime, -2);
                     $startH = substr($startTime, 0, -2);
                     $waitTime = ($startH - $hours) * 60 + ($startM - $minutes);
                     $arrivalTime = Config::timeAdd(str_replace(":", "", $startTime), $runningTime, "H:i");
                     while ($waitTime < 0) {
                         $waitTime += 1440;
                     }
                     $timeSort[] = $waitTime;
                     // Wait time should be arrival time minus specified search time
                     $hits[] = array('id' => $route['num'], 'name' => $route['dest'], 'runningTime' => $runningTime, 'startTime' => $startTime, 'wait' => $waitTime, 'arrivalTime' => $arrivalTime, 'arrivalSpan' => $runningTime + $waitTime);
                 }
             }
         }
         if ($timer) {
             $timer->tick("search", "Searched departures");
         }
         array_multisort($timeSort, SORT_ASC, $hits);
         $memcache->set($cacheKey, serialize($hits), false, 120);
     }
     $this->count = count($hits);
     return array_slice($hits, $offset, $limit);
 }
Example #7
0
 /**
  * Import bus stops from a csv file fmor eiendomsprofil data
  *
  * @return int
  * @param string $file
  */
 public function import($file)
 {
     $db = Config::getDb();
     // Lat/long limits, should be converted
     $top = 60.60108;
     //lat
     $bottom = 59.90108;
     //lat
     $left = 5.005268;
     //long
     $right = 5.725268;
     //long
     $gPoint = new gPoint();
     $gPoint->setLongLat($left, $top);
     $gPoint->convertLLtoTM();
     $utmTop = (int) $gPoint->N();
     $utmLeft = (int) $gPoint->E();
     $gPoint->setLongLat($right, $bottom);
     $gPoint->convertLLtoTM();
     $utmBottom = (int) $gPoint->N();
     $utmRight = (int) $gPoint->E();
     $utmTop = 6800000;
     $utmBottom = 6600000;
     $utmRight = -15000;
     $utmLeft = -46000;
     $handle = fopen($file, "r");
     $i = 0;
     $db->stops->ensureIndex(array('location' => '2d'));
     if ($handle) {
         echo "Top: {$utmTop} Bottom: {$utmBottom} Left: {$utmLeft} Right: {$utmRight}<br>\n";
         fgets($handle, 4096);
         while (!feof($handle)) {
             $line = fgets($handle);
             if (strlen($line) > 10) {
                 $fields = explode(",", $line);
                 $name = substr($fields[3], 1, -1);
                 $x = (int) substr($fields[8], 1, -1);
                 $y = (int) substr($fields[9], 1, -1);
                 if ($x < $utmRight && $x > $utmLeft && $y < $utmTop && $y > $utmBottom) {
                     $i++;
                     $gPoint->setUTM($x, $y, "33V");
                     $gPoint->convertTMtoLL();
                     $lat = $gPoint->Lat();
                     $long = $gPoint->Long();
                     $location = array((double) $lat, (double) $long);
                     $stop = $db->stops->findOne(array('location' => $location));
                     if ($stop === null) {
                         $db->stops->insert(array('name' => $name, 'location' => $location, 'aliases' => array($name), 'search' => array(toLower($name)), 'connectsFrom' => array(), 'connectsTo' => array()));
                     } else {
                         if (array_key_exists('aliases', $stop)) {
                             $aliases = $stop['aliases'];
                         } else {
                             $aliases = array();
                         }
                         $aliases[] = $name;
                         $res = $db->stops->update(array("_id" => $stop['_id']), array('$set' => array('aliases' => $aliases)));
                     }
                     unset($stop);
                     //echo "$name lies as $lat,$long\n";
                 }
             }
         }
         fclose($handle);
         return $i;
     }
     return false;
 }