Changeset - 58eeb176287a
[Not reviewed]
default
0 2 0
Nathan Brink (binki) - 15 years ago 2010-10-09 20:56:31
ohnobinki@ohnopublishing.net
Prevent multiple calls to session_start() and use a named session to avoid colliding with other apps' sessions.
2 files changed with 29 insertions and 11 deletions:
0 comments (0 inline, 0 general)
inc/class.page.php
Show inline comments
 
@@ -54,25 +54,25 @@ class page
 
      || !strlen($_SERVER['HTTP_ACCEPT']) /* then the browser doesn't care :-) */)
 
     {
 
       $this->xhtml = TRUE;
 
       header('Content-type: application/xhtml+xml');
 
     }
 

	
 
   $ga_www = 'http://www.';
 
   if ($_SERVER['SERVER_PORT'] != 80)
 
     $ga_www = 'https://ssl.';
 
   $this->trackingcode = '<script type="text/javascript" src="' . $ga_www . 'google-analytics.com/ga.js" />' . "\n"
 
     . $this->trackingcode;
 

	
 
    session_start();
 
   page::session_start();
 
    if($immediate
 
       && $ntitle != "NOHEAD")
 
      $this->head();
 

	
 
    /* everything that needs sessions started to work: */
 

	
 
    $this->school = school_load_guess();
 
 }
 

	
 
  /**
 
   * \brief
 
   *   Adds some headcode to this page.
 
@@ -256,19 +256,39 @@ class page
 
    echo "<h2>404: Content Not Found</h2>\n"
 
      . "<p>\n"
 
      . '  ' . $message . "\n"
 
      . "</p>\n";
 

	
 
    $page_404->foot();
 

	
 
    exit();
 
  }
 

	
 
  /**
 
   * \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
 
   *   Get the current school profile handle.
 
   */
 
  public function get_school()
 
  {
 
    return $this->school;
 
  }
 
}
process.php
Show inline comments
 
<?php
 

	
 
session_start();
 

	
 
require_once('inc/schedule_store.inc');
 
require_once('inc/class.page.php');
 
include_once 'class.schedule.php';
 
include_once 'class.class.php';
 
include_once 'class.section.php';
 

	
 
function sortInputs($post){
 
//	return array_filter($post['postData']); // Remove any null or unset items. Disabled as it kills day stuff, @FIXME and add day unset setting here (==0).
 
	return $post['postData'];
 
}
 

	
 

	
 
// Converts a 5-element array into a nice string.
 
// Supports multiple modes, prettiness, and searching for different indicators
 
function arrayToDays($array, $mode = 'num', $pretty = false, $key = 1) {
 
	$outString = '';
 
	switch($mode){
 
		case 'short':
 
			$days = array('Mon','Tue','Wed','Thur','Fri');
 
			break;
 
		case 'long':
 
			$days = array('Monday','Tuesday','Wednesday','Thursday','Friday');
 
			break;
 
		case 'num':
 
@@ -48,24 +40,29 @@ function arrayToDays($array, $mode = 'nu
 
	else {
 
		for($i=0; $i<=4; $i++)
 
			if(isset($array[$i]))
 
				$outString = $days[$i];
 
	}
 
	return $outString;
 
}
 

	
 
function prettyTime($time){
 
	return substr($time,0,strlen($time)-2) . ":" . substr($time,strlen($time)-2, strlen($time));
 
}
 

	
 
/*
 
 * The below code relies on sessions being started already.
 
 */
 
page::session_start();
 

	
 
$DEBUG = FALSE;
 
if (isset($_GET['debug']))
 
  $DEBUG = $_GET['debug'];
 

	
 
$schedule_store = schedule_store_init();
 

	
 
if(!$DEBUG)
 
  {
 
    if(isset($_GET['s']))
 
      {
 
	$savedSched = schedule_store_retrieve($schedule_store, $_GET['s']);
 
	if ($savedSched)
 
@@ -75,30 +72,31 @@ if(!$DEBUG)
 
      }
 
    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');
 
	exit;
 
      }
 
    else
 
      {
 
		$allClasses = new Schedule($_POST['postData']['name']);
 
	
 
		foreach(sortInputs($_POST) as $class)
 
		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)
 
@@ -118,25 +116,25 @@ if(!$DEBUG)
 

	
 
		/*
 
		 * 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(sortInputs($_POST) as $class) {
 
	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)