public function getPageViews($json = false) { $result = parent::getPageViews(); if ($json) { $result = json_encode($result); } return $result; }
function log_api($module) { $ua = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/MIT[\s%20]+Mobile[\s\/]+([\w\.]+)\s*(.+)/', $ua, $matches)) { if (strpos($matches[2], 'Android')) { $platform = 'android'; } else { $platform = 'iphone'; } } elseif ($ua == 'Apache-HttpClient/UNAVAILABLE') { $platform = 'android'; } else { $platform = reset(explode(" ", $ua)); } PageViews::log_api($module, $platform); }
function parse_line($line) { // ignore nagios checks and localhost calls if (strpos($line, '127.' === 0) || strpos($line, 'check_http') !== FALSE) return; if (preg_match('/GET ' . str_replace('/', '\/', HTTPROOT) . '([\w\-]+)\/?( HTTP|[\w\-]+\.php)/', $line, $matches)) { $module = $matches[1]; if ($module == 'home' || array_key_exists($module, Modules::full_list())) { preg_match('/\[(\d+\/\w+\/\d{4})/', $line, $matches); $day = strtotime(str_replace('/', '-', $matches[1])); $ua_end = strrpos($line, '"'); $ua_start = strrpos(substr($line, 0, $ua_end), '"') + 1; $ua = substr($line, $ua_start, $ua_end - $ua_start); $request = Array('ua' => $ua, 'action' => 'classify'); $result = json_decode(file_get_contents(MOBI_SERVICE_URL . '?' . http_build_query($request)), TRUE); $platform = $result['platform']; PageViews::increment($module, $platform, $day); //echo time() . ' ' . date('Y-m-d', $day) . ' ' . $module . ' ' . $platform . "\n"; } } return; }
/** * Copyright (c) 2008 Massachusetts Institute of Technology * * Licensed under the MIT License * Redistributions of files must retain the above copyright notice. * */ require "Page.php"; require "page_tools.php"; require "counter.php"; $phone = Page::classify_phone(); $page = Page::factory($phone); $prefix = $page->requirePrefix(); preg_match('/\\/((\\w|\\-)+)\\/[^\\/]*?$/', $_SERVER['REQUEST_URI'], $match); $content = $match[1]; PageViews::increment($content); class DataServerException extends Exception { } // use php default error handler for the dev version of the web site // unccmment the line below to use the custom exception handler // set_exception_handler("exception_handler"); function exception_handler($exception) { if (is_a($exception, "DataServerException")) { $error_query = "code=data&url=" . urlencode($_SERVER['REQUEST_URI']); } else { $error_query = "code=internal"; } $error_url = "../error-page/?{$error_query}"; $recipients = array("*****@*****.**");
public static function init() { self::$time = time(); self::$today = date("Y-m-d", self::$time); }
<?php require_once "../../config/mobi_web_constants.php"; require_once WEBROOT . "api/api_header.php"; PageViews::log_api('newsoffice', 'iphone'); define("FEED_STATE_NULL", 0); define("FEED_STATE_FRESH", 1); $channels = array(0 => 'All News', 1 => 'Engineering', 2 => 'Business', 3 => 'Science', 5 => 'Architecture and Planning', 6 => 'Humanities, Arts, and Social Sciences', 99 => 'Campus'); $channel_id = $_REQUEST['channel'] ? $_REQUEST['channel'] : 0; $cache_file = $cache_dir . $channel_id . '.xml'; $news_url = NEWSOFFICE_FEED_URL; $query = array('type' => 'dev'); if ($channel_id) { $query['category'] = $channel_id; } $xmldoc = new DOMDocument(); $is_fresh = FALSE; $loaded = load_from_cache($xmldoc, $channel_id); //$query['lastModified'] = $loaded; switch ($loaded) { case FEED_STATE_FRESH: break; case FEED_STATE_NULL: unset($query['lastModified']); default: // the HTTP extension is not currently installed in our build of PHP // so we just suppress warnings for 304 codes $error_reporting = intval(ini_get('error_reporting')); error_reporting($error_reporting & ~E_WARNING); $body = file_get_contents(NEWSOFFICE_FEED_URL . '?' . http_build_query($query)); error_reporting($error_reporting);
$url = $url_redirects[$id]; } else { $parts[0] = $url_redirects[$id]; $url = URL_PREFIX . implode("/", $parts); } header("Location: " . $url); exit; } } // find the page part if (isset($parts[1])) { if (strlen($parts[1])) { $page = basename($parts[1], '.php'); } } else { // redirect with trailing slash for completeness header("Location: ./$id/"); exit; } $module = WebModule::factory($id, $page, $args); /* log this page view */ PageViews::increment($id, $GLOBALS['deviceClassifier']->getPlatform()); $module->displayPage(); } exit;
$parts[0] = $url_redirects[$id]; $url = URL_PREFIX . implode("/", $parts); } header("Location: " . $url); exit; } } // find the page part if (isset($parts[1])) { if (strlen($parts[1])) { $page = basename($parts[1], '.php'); } } else { // redirect with trailing slash for completeness header("Location: ./$id/"); exit; } if ($module = WebModule::factory($id, $page, $args)) { /* log this page view */ PageViews::increment($id, Kurogo::deviceClassifier()->getPlatform()); $module->displayPage(); } else { throw new Exception("Module $id cannot be loaded"); } } exit;
<?php require_once "../config/mobi_web_constants.php"; require_once "Home.php"; require_once "Modules.php"; require_once WEBROOT . "page_builder/Page.php"; //require WEBROOT . "page_builder/counter.php"; require WEBROOT . "page_builder/page_tools.php"; require WEBROOT . "customize/customize_lib.php"; $page = Page::factory(); $page->module('home'); PageViews::increment('home', $page->platform); $whats_new_count = Home::$whats_new_count; $top_item = Home::$whats_new->getTopItemName(); Modules::init($page->branch, $page->certs, $page->platform); $old_modules = getModuleOrder(); $moduleorder = Modules::refreshAll($old_modules, $page->branch); setModuleOrder($moduleorder); $modules = getActiveModules($page->branch); $modules = Modules::refreshActive($old_modules, $modules, $page->branch); $modules = Modules::add_required($modules, $page->branch); setActiveModules($modules); $all_modules = Modules::$default_order; //$fh = fopen('/tmp/headers-' . time() . '.txt', 'w'); //fwrite($fh, str_replace('",', "\",\n", json_encode($_SERVER)) . '\n'); //fclose($fh); $page->prevent_caching('Basic'); $page->prevent_caching('Touch'); $page->cache(); /* function url($module) {
<? require_once "../../config/mobi_web_constants.php"; require_once WEBROOT . "shuttleschedule/shuttle_lib.php"; $APIROOT = WEBROOT . "/api"; require_once $APIROOT . "/api_header.php"; PageViews::log_api('shuttles', 'iphone'); $data = Array(); $command = $_REQUEST['command']; switch ($command) { case 'stops': $stops = NextBusReader::get_all_stops(); $data = array_values($stops); break; case 'stopInfo': $stops = NextBusReader::get_all_stops(); $time = time(); $stopId = $_REQUEST['id']; $stopInfo = $stops[$stopId]; if (!$stopInfo) { error_log("failed to get stop info for $stopId"); $data = Array('error' => 'could not get stop info'); } $data['stops'] = Array(); foreach ($stopInfo['routes'] as $route) { $gpsActive = ShuttleSchedule::is_running($route) && NextBusReader::gps_active($route); $stopTimes = list_stop_times($route, $time, $gpsActive, $stopId);
function _outputAPICall() { require_once realpath(LIB_DIR.'/PageViews.php'); if (isset($_REQUEST['module']) && $_REQUEST['module']) { $id = $_REQUEST['module']; $api = realpath_exists(LIB_DIR."/api/$id.php"); if ($id == 'shuttles') { $id = 'transit'; $api = realpath_exists(LIB_DIR."/api/$id.php"); } if ($api) { PageViews::log_api($id, $GLOBALS['deviceClassifier']->getPlatform()); $GLOBALS['siteConfig']->loadAPIFile($id, true, true); require_once($api); exit; } } _404(); }
$duration = $interval_types[$url_params['interval']]['duration']; $all_data = PageViews::view_past($url_params['service'], $url_params['interval'], $duration); if ($url_params['service'] == 'web') { $graphs = array( summary_total($all_data, "total", "total page views"), trend($all_data, "total", 'Page Views by ' . ucfirst($url_params['interval']), $url_params['interval']), bar_percentage( platform_data($all_data), "Traffic by Platform"), list_items(generate_popular_content('web', $all_data), "Most Popular Content", "page views"), ); } else { // api $graphs = array( summary_total(PageViews::count_iphone_tokens(), "total", "active iPhone users"), summary_total($all_data, "total", "total API requests"), trend($all_data, "total", 'API Requests by ' . ucfirst($url_params['interval']), $url_params['interval']), bar_percentage( native_platform_data($all_data), "Requests by Platform"), list_items(generate_popular_content('api', $all_data), "Most Popular Modules", "requests"), ); } $name = $service_types[$url_params['service']]; // set states of fake segmented control $statclasses = Array(); foreach ($interval_types as $type => $attrs) { $stclass = Array();
<? require_once dirname(__FILE__) . "/../config/mobi_web_constants.php"; require_once WEBROOT . "api/api_header.php"; PageViews::log_api('map', 'iphone'); switch ($_REQUEST['command']) { case 'categories': require_once LIBDIR . "campus_map.php"; $categories = Buildings::category_titles(); $result = array(); foreach ($categories as $category => $category_text) { $result[] = array( 'categoryName' => $category_text, 'categoryId' => $category, 'categoryItems' => get_category($category), ); } echo json_encode($result); break; case 'category': require_once LIBDIR . "campus_map.php"; if ($category = $_REQUEST['id']) { $places = get_category($category); echo json_encode($places); } break; case 'categorytitles': require_once LIBDIR . "campus_map.php"; $categories = category_titles_wrapper();
public function output() { if (!$this->module) { // here is a stricter version of the overly permissive regex // that used to be in page_header $module_list = implode('|', array_keys(Modules::$module_data)); preg_match('/\\/(' . $module_list . ')\\/[^\\/]*?$/', $_SERVER['REQUEST_URI'], $match); $this->module = $match[1]; } PageViews::increment($this->module, $this->platform); foreach ($this->varnames as $varname) { ${$varname} = $this->{$varname}; } ob_start(); require "../{$this->branch}/base.html"; $uncompressed_html = ob_get_clean(); // replace large chunks of spaces with a single space $compressed_html = self::compress_whitespace($uncompressed_html); if ($this->compressed_mode) { echo $compressed_html; } else { echo $uncompressed_html; } }
<? require_once "../config/mobi_web_constants.php"; require_once("api_header.php"); $module = $_REQUEST['module']; PageViews::log_api($module, 'iphone'); switch ($module) { case 'emergency': require('emergency.php'); break; case 'people': require('people.php'); break; case 'stellar': require('stellar.php'); break; case 'calendar': require('calendar.php'); break; case 'push': require('apns_push.php'); break; default: $data = Array('error' => 'not a valid query'); echo json_encode($data);
require_once "Page.php"; require_once "detection.php"; require_once "adapter.php"; require_once "page_tools.php"; require_once "counter.php"; $prefix = Device::templates(); $page = Page::factory($prefix); // check to see if this is a deep link, if so redirect // must be a webkit classified browser and missing the interal request [ir] var // ir var is defined in jqtouch for GETs and as a hidden var in forms if ($prefix == 'webkit' && (!$_REQUEST['ir'] || $_REQUEST['ir'] == '')) { header("location:/?redirect=" . $_SERVER['REQUEST_URI']); } // record stats for the section in the database $section = Page::getSection($_SERVER['REQUEST_URI']); PageViews::increment($section); class DataServerException extends Exception { } // use php default error handler for the dev version of the web site // unccmment the line below to use the custom exception handler // set_exception_handler("exception_handler"); function exception_handler($exception) { if (is_a($exception, "DataServerException")) { $error_query = "code=data&url=" . urlencode($_SERVER['REQUEST_URI']); } else { $error_query = "code=internal"; } $error_url = "../error-page/?{$error_query}"; $recipients = array("*****@*****.**");
protected function initializeForPage() { switch ($this->page) { case 'index': $service = $this->getArg('service', 'web'); $interval = $this->getArg('interval', 'day'); $duration = $this->getDurationForInterval($interval); $statData = PageViews::view_past($service, $interval, $duration); if ($service=='web') { $statItems = array( $this->summary_total($statData, "total", "total page views"), $this->trend($statData, "total", 'Page Views by ' . ucfirst($interval), $interval), $this->bar_percentage( $this->platform_data($statData), "Traffic by Platform"), $this->list_items($this->generate_popular_content('web', $statData), "Most Popular Content", "page views"), ); } else { // api $statItems = array( //summary_total(PageViews::count_iphone_tokens(), "total", "active users"), $this->summary_total($statData, "total", "total API requests"), $this->trend($statData, "total", 'API Requests by ' . ucfirst($interval), $interval), $this->list_items($this->generate_popular_content('api', $statData), "Most Popular Modules", "requests"), ); } $serviceTypes = Array('web' => 'Website', 'api' => 'Native App'); $interval_types = Array( 'day' => Array('duration' => 7, 'title' => 'Week', 'numdays' => 7), 'week' => Array('duration' => 12, 'title' => '12 Weeks', 'numdays' => 84), 'month' => Array('duration' => 12, 'title' => 'Year', 'numdays' => 365), 'quarter' => Array('duration' => 12, 'title' => '3 Years', 'numdays' => 1095), ); $statclasses = Array(); foreach ($interval_types as $type => $attrs) { $stclass = Array(); $stclass['interval'] = $type; if ($interval == $type) { $stclass['active'] = ' class="active"'; } else { $stclass['active'] = ''; } $stclass['title'] = $attrs['title']; $statclasses[$type] = $stclass; } $this->assign('statsItems', $statItems); $this->assign('statsName', $this->nameForService($service)); //not really $this->assign('statsService', $service); $this->assign('statsInterval', $interval); $this->assign('statsDuration', $duration); $this->assign('statclasses', $statclasses); $this->assign('serviceTypes', $serviceTypes); } }
<?php /** * Copyright (c) 2008 Massachusetts Institute of Technology * * Licensed under the MIT License * Redistributions of files must retain the above copyright notice. * */ /* NOTE THAT THIS FILE ONLY SERVES TOUCH & BASIC DEVICES NOW THOUGH WEBKIT WILL WORK */ /* WEBKIT IS NOW HANDLED BY THE ROOT INDEX FILE TO MAKE LIFE WITH MANIFEST-CACHE EASIER */ require "../../config.gen.inc.php"; require "../page_builder/Page.php"; require "../page_builder/counter.php"; require "data/data.inc.php"; $phone = Page::classify_phone(); $prefix = Page::$requireTable[$phone]; $page = Page::factory($phone); PageViews::increment('home'); if ($phone == 'basic') { $width = 48; $height = 19; } if ($_REQUEST['more'] != true) { require "templates/{$prefix}/index.html"; } else { require "templates/{$prefix}/more.html"; }
function aggregate_days($days, $interval_type, $duration) { $intervals = array(); $counter = array(); // get all intervals, fill in 0 if missing $last_day = $days[count($days) - 1]['date']; $year = substr($last_day, 0, 4); $month = substr($last_day, 5, 7); switch ($interval_type) { case 'quarter': $month = (int) $month - 1; $month = $month - $month % 3 + 1; $utime = mktime(0, 0, 0, $month, 1, $year); break; case 'month': $utime = mktime(0, 0, 0, $month, 1, $year); break; case 'day': case 'week': default: $utime = strtotime($last_day); } $dayofweek = date('w', $utime); for ($i = 1; $i <= $duration; $i++) { $counter[$utime] = 0; switch ($interval_type) { case 'day': $utime -= 86400; break; case 'week': $is_dst = date('I', $utime); $utime -= 86400 * 7; $was_dst = date('I', $utime); // this is further in the past $utime -= ($was_dst - $is_dst) * 3600; break; case 'month': if ($month <= $i) { $utime = mktime(0, 0, 0, $month - $i + 12, 1, $year - 1); } else { $utime = mktime(0, 0, 0, $month - $i, 1, $year); } break; case 'quarter': if ($month - $month % 3 <= $i) { $utime = mktime(0, 0, 0, $month - 3 * $i + 12, 1, $year - 1); } else { $utime = mktime(0, 0, 0, $month - 3 * $i, 1, $year); } break; } } foreach ($days as $day) { $utime = strtotime($day['date']); switch ($interval_type) { case 'day': $interval = $utime; break; case 'week': // week starting on specified date $interval = $utime; while (date('w', $interval) != $dayofweek) { $was_dst = date('I', $interval); $interval += 86400; $is_dst = date('I', $interval); $interval += ($was_dst - $is_dst) * 3600; } break; case 'month': $interval = mktime(0, 0, 0, date('n', $utime), 1, date('Y', $utime)); break; case 'quarter': $interval = PageViews::quarter_of($utime); break; } $counter[$interval] += $day['count']; } ksort($counter); foreach ($counter as $interval => $count) { $intervals[] = array('date' => $interval, 'count' => $count); } return $intervals; }
public static function view_past($system, $time_unit, $duration) { $increments = Array(); // figure out value to use for $begin in sql query // and number of seconds for each $increment $time = time(); switch ($time_unit) { case 'day': $begin = $time - $duration * 86400; $increments = array_pad($increments, $duration, 86400); break; case 'week': $begin = $time - $duration * 86400 * 7; $increments = array_pad($increments, $duration, 86400 * 7); break; case 'month': $month = date('n', $time) + 1; $year = date('Y', $time) - 1; if ($month > 12) { $month = 1; $year += 1; } $begin = mktime(0, 0, 0, $month, 1, $year); $last_begin = $begin; for ($i = 0; $i < $duration; $i++) { $month += 1; if ($month > 12) { $month = 1; $year += 1; } $next_begin = mktime(0, 0, 0, $month, 1, $year); $increments[] = $next_begin - $last_begin; $last_begin = $next_begin; } break; case 'quarter': $current_quarter = PageViews::quarter_of($time); $month = date('n', $current_quarter) + 3; $year = date('Y', $current_quarter) - 3; if ($month > 12) { $month -= 12; $year += 1; } $begin = mktime(0, 0, 0, $month, 1, $year); $last_begin = $begin; for ($i = 0; $i <= $duration; $i++) { $month += 3; if ($month > 12) { $month -= 12; $year += 1; } $next_begin = mktime(0, 0, 0, $month, 1, $year); $increments[] = $next_begin - $last_begin; $last_begin = $next_begin; } break; } $views = Array(); for ($i = 0; $i < $duration; $i++) { $sql_start_date = date('Y-m-d', $begin); $end = $begin + $increments[$i]; $sql_end_date = date('Y-m-d', $end); $new_view = Array('date' => $begin, 'total' => 0); // array below has index for each module, bucket // and the index 'day' for the day or first day of week/month $results = self::getTimeSeries($system, $sql_start_date, NULL, NULL, $sql_end_date); foreach ($results as $row) { if (array_key_exists('platform', $row)) { if (!array_key_exists($row['platform'], $new_view)) $new_view[$row['platform']] = 0; $new_view[$row['platform']] += $row['viewcount']; } if (array_key_exists('module', $row)) { if (!array_key_exists($row['platform'], $new_view)) $new_view[$row['platform']] = 0; $new_view[$row['module']] += $row['viewcount']; } $new_view['total'] += $row['viewcount']; } $views[] = $new_view; $begin = $end; } return $views; }
} usort($popular_pages, 'compare_content'); $popular_pages = array_slice($popular_pages, 0, 10); $start_time = PageViews::$time - 7 * 24 * 60 * 60; $end_time = PageViews::$time - 1 * 24 * 60 * 60; $start_month = date('M', $start_time); $start_day = date('j', $start_time); $end_month = date('M', $end_time); $end_day = date('j', $end_time); $year = date('Y', $end_time); if ($start_month == $end_month) { $days_text = "{$start_month} {$start_day}-{$end_day}, {$year}"; } else { $days_text = "{$start_month} {$start_day}-{$end_month} {$end_day}, {$year}"; } $today_total = PageViews::getToday(); require "templates/{$prefix}/statistics.html"; $page->output(); function f($count) { $count_str = (string) $count; if (strlen($count_str) <= 3) { return $count_str; } else { return f(substr($count_str, 0, -3)) . ',' . substr($count_str, -3); } } function per_cent($part, $total) { return round(100 * $part / $total); }