Changeset - 46201cc47146
[Not reviewed]
default
0 1 0
Nathan Brink (binki) - 14 years ago 2012-02-19 01:01:53
ohnobinki@ohnopublishing.net
Create page::query_string() function for querystring serialization.
1 file changed with 29 insertions and 5 deletions:
0 comments (0 inline, 0 general)
inc/class.page.php
Show inline comments
 
@@ -315,53 +315,49 @@ class page
 
  {
 
    $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;
 
	$uri_full .= self::query_string($query);
 
      }
 

	
 
    /* 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');
 
@@ -719,48 +715,76 @@ class page
 
	  $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;
 
	  list($base_request_uri) = explode('?', $_SERVER['REQUEST_URI'], 2);
 
	  $base_uri .= substr($base_request_uri, 0, strrpos($base_request_uri, '/')) . '/';
 
	}
 

	
 
    if (empty($base_uri) && empty($uri))
 
      return './';
 

	
 
    return $base_uri . $uri;
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Form a query string from a map.
 
   *
 
   * \param $query
 
   *   The map of keys onto values to form into a querystring.
 
   * \param $question
 
   *   Include the question mark which delimits the querystring in a
 
   *   URI.
 
   * \return
 
   *   A querystring suitable for appending to a URI. Includes the `?'
 
   *   by default.
 
   */
 
  public static function query_string(array $query, $question = TRUE)
 
  {
 
    $query_string_parts = array();
 
    foreach ($query as $param => $values)
 
      {
 
	if (!is_array($values))
 
	  $values = array($values);
 
	foreach ($values as $value)
 
	  $query_string_parts[] = rawurlencode($param) . '=' . rawurlencode($value);
 
      }
 
    if (count($query_string_parts))
 
      return ($question ? '?' : '') . implode('&', $query_string_parts);
 
    return '';
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Get the current school profile handle.
 
   */
 
  public function get_school()
 
  {
 
    return $this->school;
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Get the current semester.
 
   */
 
  public function semester_get()
 
  {
 
    return $this->semester;
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Format a chunk of javascript suitable for adding to headcode.
 
   *
 
   * Takes into account whether or not the code should be wrapped in
 
   * CDATA or not.
 
   *
 
   * \param $js
0 comments (0 inline, 0 general)