Changeset - 4d7bb7e8bc2e
[Not reviewed]
default
0 4 0
Nathan Brink (binki) - 14 years ago 2012-02-09 20:59:26
ohnobinki@ohnopublishing.net
Add support for <meta/> tags and enforce canonization of URIs for index.php and input.php pages through redirects.
4 files changed with 157 insertions and 28 deletions:
0 comments (0 inline, 0 general)
inc/class.page.php
Show inline comments
 
@@ -79,12 +79,13 @@ class page
 
  /* Scripts and styles */
 
  private $headCode = array();
 

	
 
  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;
 

	
 
  private $semester;
 

	
 
@@ -136,12 +137,17 @@ class page
 
    $this->headCode['gliderHeadcode'] = '<link rel="stylesheet" href="styles/glider.css" type="text/css" media="screen" charset="utf-8" />'; 
 
    $this->headCode['uiTabsKeyboard'] = '<script type="text/javascript" src="scripts/uiTabsKeyboard.js"></script>';
 
    $this->headCode['displayTables'] = '<script type="text/javascript" src="scripts/displayTables.js"></script>';
 

	
 
    $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 :-) */
 
      || strpos($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml') !== FALSE)
 
       $this->xhtml = TRUE;
 

	
 
@@ -235,12 +241,66 @@ class page
 
      $this->scripts[] = $key;
 
    }
 
  }
 

	
 
  /**
 
   * \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 <link
 
   * rel="canonical"/> 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 <link rel="canonical"/> */
 
    $this->headcode_add('link_rel_canonical', '<link rel="canonical" href="'
 
			. htmlentities(self::uri_resolve($uri_full), ENT_QUOTES) . '"'
 
			. ($this->xhtml ? '/>' : '></link>'),
 
			TRUE);
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Output the HTML header for a page, including <!DOCTYPE>, <head />, and opening structure
 
   */
 
  public function head()
 
  {
 
    if ($this->xhtml) {
 
       header('Content-Type: application/xhtml+xml; charset=utf-8');
 
@@ -259,12 +319,18 @@ class page
 
          '      <link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="styles/ie.css" />'. PHP_EOL .
 
          '    <![endif]-->'. PHP_EOL .
 
          '    <link rel="shortcut icon" href="images/favicon.png" />'. PHP_EOL
 
      . '    <style type="text/css">' . PHP_EOL
 
      . $this->cdata_wrap(school_page_css($this->school))
 
      . '    </style>' . PHP_EOL;
 

	
 
    foreach ($this->meta as $key => $value)
 
      echo '    <meta name="' . htmlentities($key, ENT_QUOTES)
 
      . '" content="' . htmlentities($value, ENT_QUOTES)
 
      . '" ' . ($this->xhtml ? '/' : '') .  '>' . PHP_EOL;
 

	
 
    // Write out all passed scripts
 
    foreach ($this->scripts as $i)
 
      echo '    ' . $this->headCode["$i"] . "\n";
 

	
 
    /*
 
     * Perhaps we should have a separate array for javascript library
 
@@ -275,13 +341,13 @@ class page
 
      $javascript_init .= 'jQuery.extend(jQuery.fn.cuteTime.settings, {refresh: 10000, use_html_attribute: false});' . PHP_EOL
 
	. 'jQuery.fn.cuteTime.settings.time_ranges[0].cuteness = \'in the future\';' . PHP_EOL;
 

	
 
    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 = '&amp;school=' . $this->school['id'];
 
    /* kludge */
 
    if (!empty($_REQUEST['s']))
 
      $selectschool_query .= '&amp;s=' . (int)$_REQUEST['s'];
 

	
 
@@ -538,43 +604,67 @@ class page
 
   *   The redirection code to use, if any. For example, this can be
 
   *   used to implement ``permanent'' redirects if necessary.
 
   */
 
  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;
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Get the current school profile handle.
 
   */
inc/class.schedule.php
Show inline comments
 
@@ -450,12 +450,24 @@ class Schedule
 
      array_push($headcode, 'outputPrintStyle');
 
    else
 
      array_push($headcode, 'jAddress');
 

	
 
    $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();
 

	
 

	
 

	
 
    if(!empty($_REQUEST['print']))
 
      {
 
@@ -500,16 +512,16 @@ class Schedule
 
	. '        <p>' . PHP_EOL
 
	. '          <a href="input.php?s='.$this->id.'" class="button">Edit</a>' . PHP_EOL
 
	. '          <span id="printItems"><a href="#" class="button">Print</a></span>' . PHP_EOL
 
	. '          <span id="share"><a href="#" class="button">Share</a></span>' . PHP_EOL;
 

	
 

	
 
      if ($schedule_store !== NULL
 
	  && $this->parent_get() !== NULL
 
	  && ($parent_schedule = schedule_store_retrieve($schedule_store, $this->parent_get())) !== NULL)
 
	echo '          <a class="button" href="' . htmlentities($parent_schedule->my_url()) . '" title="Parent schedule: ' . htmlentities($parent_schedule->getName()) . '">Parent</a>' . PHP_EOL;
 
      if (!empty($parent_schedule))
 
        {
 
          echo '          <a class="button" href="' . htmlentities($parent_uri, ENT_QUOTES) . '" title="Parent schedule: ' . htmlentities($parent_schedule->getName()) . '">Parent</a>' . PHP_EOL;
 
        }
 

	
 
      echo '          <a class="button" href="input.php">Home</a>' . PHP_EOL
 
	. '        </p>'. PHP_EOL
 
	. '        <p class="centeredtext">Having problems? <a href="feedback.php">Let us know</a>.</p>' . PHP_EOL
 
	. '        <p class="centeredtext graytext"><em>Keyboard Shortcut: Left and right arrow keys switch between schedules</em></p>' . PHP_EOL;
 
    }		
index.php
Show inline comments
 
@@ -18,12 +18,23 @@
 
 * along with SlatePermutate.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
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();
 
?>
 

	
 
<h3>Find the schedule that works for you!</h3>
 
<p>Plan your next semester with SlatePermutate! SlatePermutate generates every possible schedule with the courses you enter to let you pick the schedule that fits your life.</p>
 

	
input.php
Show inline comments
 
@@ -172,12 +172,28 @@ if ($qtips_always || !isset($_SESSION['s
 
  }
 
$my_hc .= '  });
 
';
 

	
 
$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();
 

	
 
/*
 
 * Force a student to choose a school or declare he's a generic
 
 * student before displaying the input form. To do this, we need
 
 * another variable in $_SESSION: $_SESSION['school_chosen'].
0 comments (0 inline, 0 general)