diff --git a/inc/class.page.php b/inc/class.page.php --- a/inc/class.page.php +++ b/inc/class.page.php @@ -82,6 +82,7 @@ class page private $trackingcode = ''; // Tracking code private $pagetitle = ''; // Title of page private $scripts = array(); // Scripts to include on page + private $meta; /* the current school. See get_school(). */ private $school; @@ -139,6 +140,11 @@ class page $this->pagetitle = $ntitle; $this->scripts = $nscripts; + $this->meta = array( + 'msapplication-starturl' => self::uri_resolve(''), + 'msapplication-task' => 'name=Create Schedule; action-uri=' . self::uri_resolve('input.php') . '; icon-uri=' . self::uri_resolve('images/favicon_96.png'), + 'msapplication-tooltip' => 'Find the semester schedule which works for you!', + ); /* Compliant browsers which care, such as gecko, explicitly request xhtml: */ if(empty($_SERVER['HTTP_ACCEPT']) /* then the browser doesn't care :-) */ @@ -238,6 +244,60 @@ class page /** * \brief + * Set a meta element value. + * \param $name + * The name of the meta attribute. + * \param $value + * The value. + */ + public function meta($name, $value = '') + { + $this->meta[$name] = $value; + } + + /** + * \brief + * Set the information necessary to create a canonical URI + * description. + * + * For declaring a page's canonical URI, we use both and soft redirects. + * + * \param $uri + * The base URI for the current page. + * \param $query + * The querystring to canonicalize on. + */ + public function canonize($uri, array $query = array()) + { + $query_string = ''; + $uri_full = $uri; + if (!empty($query)) + { + ksort($query); + $query_members = array(); + foreach ($query as $key => $value) + $query_members[] = rawurlencode($key) . '=' . rawurlencode($value); + $query_string = implode('&', $query_members); + $uri_full .= '?' . $query_string; + } + + /* Detect if we are at the canonical location or not... */ + list($base_request_uri) = explode('?', $_SERVER['REQUEST_URI'], 2); + $base_request_uri = substr($_SERVER['REQUEST_URI'], strrpos($base_request_uri, '/') + 1); + if ($base_request_uri != $uri_full) + /* We are not canonical, redirect. */ + $this->redirect($uri_full); + + /* Mention that this is a canonical URI with */ + $this->headcode_add('link_rel_canonical', 'xhtml ? '/>' : '>'), + TRUE); + } + + /** + * \brief * Output the HTML header for a page, including , , and opening structure */ public function head() @@ -262,6 +322,12 @@ class page . ' ' . PHP_EOL; + + foreach ($this->meta as $key => $value) + echo ' xhtml ? '/' : '') . '>' . PHP_EOL; + // Write out all passed scripts foreach ($this->scripts as $i) echo ' ' . $this->headCode["$i"] . "\n"; @@ -278,7 +344,7 @@ class page echo $this->script_wrap('' . 'var slate_permutate_school = ' . json_encode($this->school['id']) . ';' . PHP_EOL . 'var slate_permutate_semester = ' . json_encode($this->semester['id']) . ';' . PHP_EOL - . $javascript_init); + . $javascript_init) . PHP_EOL; $selectschool_query = '&school=' . $this->school['id']; /* kludge */ @@ -541,37 +607,61 @@ class page public static function redirect($dest, $http_code = NULL) { if ($http_code) + /** + * \todo + * See http://drupal.org/node/208793 + */ header('HTTP/1.1 ' . $http_code); - $uri = ''; + header('Location: ' . self::uri_resolve($dest)); + exit(); + } - $host = ''; - if (isset($_SERVER['SERVER_NAME'])) - $host = $_SERVER['SERVER_NAME']; - if (isset($_SERvER['HTTP_HOST'])) - $host = $_SERVER['HTTP_HOST']; + /** + * \brief + * Calculate the absolute URI on a best-effort basis. + * \param $uri + * The relative URI. An empty string will get the URI to the + * index/default page. + * \return + * An absolute URI referring to the specified page. + */ + public static function uri_resolve($uri = '') + { + static $base_uri = ''; - if (strlen($host)) + static $host = ''; + if (empty($host)) { - $proto = 'http'; - $port = NULL; - if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) - { - if ($_SERVER['SERVER_PORT'] == 443 || !empty($_SERVER['HTTPS'])) - $proto .= 's'; - if ($_SERVER['SERVER_PORT'] != 433) - $port = $_SERVER['SERVER_PORT']; - } - - $uri = $proto . '://' . $host; - if ($port !== NULL) - $uri .= ':' . $port; - $uri .= dirname($_SERVER['REQUEST_URI']) . '/'; + if (isset($_SERVER['SERVER_NAME'])) + $host = $_SERVER['SERVER_NAME']; + if (isset($_SERvER['HTTP_HOST'])) + $host = $_SERVER['HTTP_HOST']; } - header('Location: ' . $uri . $dest); + if (empty($base_uri)) + if (strlen($host)) + { + $proto = 'http'; + $port = NULL; + if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) + { + if ($_SERVER['SERVER_PORT'] == 443 || !empty($_SERVER['HTTPS'])) + $proto .= 's'; + if ($_SERVER['SERVER_PORT'] != 433) + $port = $_SERVER['SERVER_PORT']; + } + + $base_uri = $proto . '://' . $host; + if ($port !== NULL) + $base_uri .= ':' . $port; + $base_uri .= dirname($_SERVER['REQUEST_URI']) . '/'; + } - exit(); + if (empty($base_uri) && empty($uri)) + return './'; + + return $base_uri . $uri; } /** diff --git a/inc/class.schedule.php b/inc/class.schedule.php --- a/inc/class.schedule.php +++ b/inc/class.schedule.php @@ -453,6 +453,18 @@ class Schedule $outputPage = page::page_create(htmlentities($this->getName()), $headcode, array('school' => $this->school_get(), 'semester' => $this->semester_get())); + if (!empty($this->created)) + $outputPage->meta('dcterms.created', gmdate(DATE_W3C, $this->created)); + + if ($schedule_store !== NULL + && $this->parent_get() !== NULL + && ($parent_schedule = schedule_store_retrieve($schedule_store, $this->parent_get())) !== NULL) + { + $parent_uri = $parent_schedule->my_url(); + $outputPage->meta('dcterms.relation', $parent_uri); + $outputPage->meta('dcterms.replaces', $parent_uri); + } + $outputPage->head(); @@ -503,10 +515,10 @@ class Schedule . ' Share' . PHP_EOL; - if ($schedule_store !== NULL - && $this->parent_get() !== NULL - && ($parent_schedule = schedule_store_retrieve($schedule_store, $this->parent_get())) !== NULL) - echo ' Parent' . PHP_EOL; + if (!empty($parent_schedule)) + { + echo ' Parent' . PHP_EOL; + } echo ' Home' . PHP_EOL . '

'. PHP_EOL diff --git a/index.php b/index.php --- a/index.php +++ b/index.php @@ -21,6 +21,17 @@ require_once 'inc/class.page.php'; $welcomepage = page::page_create('Welcome'); + +/* + * If we have chosen a school, set the canonical URL so that it + * contains the school. + */ +$query = array(); +$school = $welcomepage->get_school(); +if ($school['id'] != 'default') + $query['school'] = $school['id']; +$welcomepage->canonize('', $query); + $welcomepage->head(); ?> diff --git a/input.php b/input.php --- a/input.php +++ b/input.php @@ -175,6 +175,22 @@ if ($qtips_always || !isset($_SESSION['s $inputPage->headcode_add('scheduleInput', $inputPage->script_wrap($my_hc), TRUE); +if ($school['id'] != 'default' + && empty($_REQUEST['selectschool']) + && empty($_REQUEST['selectsemester'])) + { + /* + * If we have chosen a school, set the canonical URL so that it + * contains the school and semester. + */ + $query = array('school' => $school['id']); + if ($sch) + $query['s'] = $sch->id_get(); + if (!empty($semester)) + $query['semester'] = $semester['id']; + $inputPage->canonize('input.php', $query); + } + $inputPage->head(); /*