Changeset - 864e30313107
[Not reviewed]
default
0 2 0
Nathan Brink (binki) - 15 years ago 2010-10-09 23:39:00
ohnobinki@ohnopublishing.net
Consolidate all redirect-ish code into page::redirect().
2 files changed with 55 insertions and 9 deletions:
0 comments (0 inline, 0 general)
inc/class.page.php
Show inline comments
 
@@ -280,31 +280,83 @@ class page
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Start the PHP session by calling session_start().
 
   *
 
   * Used to make sure that different areas of our code don't call
 
   * session_start() multiple times and to make it easier to ensure
 
   * that session_start() is called at least before it's needed.
 
   */
 
  public static function session_start()
 
  {
 
    static $session_started = FALSE;
 

	
 
    if (!$session_started)
 
      {
 
	session_name('slate_permutate');
 
	session_start();
 
	$session_started = TRUE;
 
      }
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Perform a redirect.
 
   *
 
   * By consolidating all redirects here, we're hopefully able to do
 
   * it in a somewhat compliant and portablish way ;-).
 
   *
 
   * This function does not return. It calls exit().
 
   *
 
   * \param $dest
 
   *   A URL relative to the slate_permutate root. For example,
 
   *   'input.php' or '44' (for clean urls, for example).
 
   * \param $http_code
 
   *   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)
 
      header('HTTP/1.1 ' . $http_code);
 

	
 
    $uri = '';
 

	
 
    $host = '';
 
    if (isset($_SERVER['SERVER_NAME']))
 
      $host = $_SERVER['SERVER_NAME'];
 
    if (isset($_SERvER['HTTP_HOST']))
 
      $host = $_SERVER['HTTP_HOST'];
 

	
 
    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'];
 
	  }
 

	
 
	$uri = $proto . '://' . $host;
 
	if ($port !== NULL)
 
	  $uri .= ':' . $port;
 
	$uri .= dirname($_SERVER['REQUEST_URI']) . '/';
 
      }
 

	
 
    header('Location: ' . $uri . $dest);
 

	
 
    exit();
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Get the current school profile handle.
 
   */
 
  public function get_school()
 
  {
 
    return $this->school;
 
  }
 
}
process.php
Show inline comments
 
@@ -63,98 +63,92 @@ if (isset($_GET['debug']))
 
if(!$DEBUG)
 
  {
 
    $s = FALSE;
 
    if (isset($_GET['s']))
 
      $s = $_GET['s'];
 

	
 
    if($s !== FALSE)
 
      {
 
	$savedSched = schedule_store_retrieve($schedule_store, $s);
 
	if ($savedSched)
 
	  $savedSched->writeoutTables();
 
	else
 
	  page::show_404('Unable to find a saved schedule with an ID of ' . $s . '.');
 
      }
 
    elseif(isset($_GET['del']))
 
      {
 
	/* Allow the user to delete schedules that he has stored in his session */
 
	if ($_SESSION['saved'][(int)$_GET['del']])
 
	  {
 
	    /* user owns this schedule ID */
 
	    schedule_store_delete($schedule_store, (int)$_GET['del']);
 
	    unset($_SESSION['saved'][(int)$_GET['del']]);
 
	  }
 

	
 
	header('Location: input.php');
 
	page::redirect('input.php');
 
	exit;
 
      }
 
    elseif (!isset($_POST['postData']))
 
      {
 
	header('Location: input.php');
 
	page::redirect('input.php');
 
	exit;
 
      }
 
    else
 
      {
 
	/*
 
	 * we probably have input from the user and should interpret
 
	 * it as a schedule to permutate. Then we should redirect the
 
	 * user to the canonical URL for that schedule.
 
	 */
 
		$allClasses = new Schedule($_POST['postData']['name']);
 
	
 
		foreach($_POST['postData'] as $class)
 
		{
 
		  /*
 
		   * Only add classes if the user added at least one
 
		   * section to the class. We know that $class['name']
 
		   * is not a section, so count() needs to be > 1 and
 
		   * we need to skip over 'name' in our loop.
 
		   */
 
			if(is_array($class) && count($class) > 1)
 
			{
 
				$allClasses->addClass($class['name']);
 
		
 
				foreach($class as $section)
 
				  /* Skip the section name, which isn't a section */
 
					if(is_array($section))
 
					{
 
				             $allClasses->addSection($class['name'], $section['letter'], $section['start'], $section['end'], arrayToDays($section['days']));
 
					}
 
			}
 
		}
 
		$allClasses->findPossibilities();
 
		if (!isset($_SESSION['saved']))
 
		  $_SESSION['saved'] = array();
 
		$schedule_id = schedule_store_store($schedule_store, $allClasses);
 
		if ($schedule_id != NULL)
 
		  $_SESSION['saved'][$schedule_id] = $allClasses->getName();
 

	
 
		$process_php_s = '';
 
		if (!$clean_urls)
 
		  $process_php_s = 'process.php?s=';
 
		header('Location: ' . $process_php_s . $schedule_id);
 
		page::redirect($process_php_s . $schedule_id);
 
		exit;
 
		/*
 
		 * writeoutTables() needs to know $schedule_id, so it
 
		 * has to be called after we save the schedule. See
 
		 * schedule_store_store().
 
		 */
 
		$allClasses->writeoutTables();
 
      }
 
  }
 
else
 
  {
 
	echo '<pre>DEBUG OUTPUT: <br /><br />';
 
	foreach($_POST['postData'] as $class) {
 
		echo 'Class: ' . $class['name'] . '<br />';
 
		foreach($class as $section)
 
			if(is_array($section))
 
			{
 
				echo '---- Section that starts at ' . prettyTime($section['start']) . ' and ends at ' . prettyTime($section['end']) . '. This class meets on ';
 
				echo arrayToDays($section['days'],'long',true) . '.<br />';
 
			}
 
		echo '<br />';
 
	}
 
	echo '</pre>';
 

	
 

	
 
}
0 comments (0 inline, 0 general)