Location: PHPKode > scripts > AskPeople-Free PHP survey application > AskPeople_2_2_2/index.php
<?php

session_start();

require_once('includes/config.inc.php');
require_once('includes/functions.inc.php');
require_once('login.inc.php');

include('includes/header.php');

switch ($_REQUEST['action']) { // questionnaire to be saved

	
	case "Create" :
	
		$questionnaire['meta']['id'] = getNextId(); // latest ID based on contents of surveys directory
		$questionnaire['meta']['date'] = date("d-M-Y H:i");
		$questionnaire['meta']['template'] = $globalsettings['defaultTemplate'];
		$questionnaire['meta']['title'] = "New Survey";
		$questionnaire['meta']['creator'] = $cookieinfo['name'];

		$mainfields = array("id","date","creator","template","title","introduction");

		foreach ($mainfields as $v) {
			$newquestionnaire[$v] = $questionnaire['meta'][$v];
		}

		$newquestionnaire[] = array("questionid" => "1","type" => "single","validation" => "optional","questiontext" => "First question","options" => "a|b|c");

		$saved = savePoll($questionnaire['meta']['id'],$newquestionnaire);
	
		$dpath = "surveys/data_" . $questionnaire['meta']['id'] . ".dat";
		$saved_datafile =  saveFile($dpath,"");
	
		$cpath = "surveys/config_" . $questionnaire['meta']['id'] . ".php";
		$defaultsettings = @file_get_contents('includes/defaultsettings.php');
		$defaultsettings = str_replace("\$cookieinfo['ID']",$cookieinfo['ID'],$defaultsettings); // establish the owner of this project - the logged in user
		
		$saved_configfile = saveFile($cpath,$defaultsettings);
		
		$_SESSION['allowedaccess'][] = $questionnaire['meta']['id'];
		
		if ($saved) {
			$_SESSION['statusmsg'] = "The questionnaire was created.";
			$_SESSION['admin_sid'] = $questionnaire['meta']['id'];
			$_SESSION['admin_stitle'] = $questionnaire['meta']['title'];
			header("Location: qeditor.php?action=EditQuestionnaire&sid=".$_SESSION['admin_sid']);			
		} else {
			$_SESSION['statusmsg'] = "Sorry, the questionnaire could not be created.";
			header("Location: index.php");			
		}
		
	  break;
	
	case "Duplicate" :
	
		$newid = getNextId(); // latest ID based on contents of surveys directory
		$newname = $_SESSION['admin_stitle'] . " copy";
		
		$oldquest = "surveys/quest_" . $_SESSION['admin_sid'] . ".xml";
		$newquest = "surveys/quest_" . $newid . ".xml";
		$loadedquest = stripslashes(loadFile($oldquest));		
		
		$copiedquest = str_replace("<id>{$_SESSION['admin_sid']}</id>","<id>{$newid}</id>",$loadedquest);
		$copiedquest = str_replace("<title>{$_SESSION['admin_stitle']}</title>","<title>{$newname}</title>",$copiedquest);
		
		$savedquest = saveFile($newquest,$copiedquest);

		// duplicate the config file, with a new ID
		$oldsettings = "surveys/config_" . $_SESSION['admin_sid'] . ".php";
		$loadedsettings = loadFile($oldsettings);		
		$newsettings = "surveys/config_" . $newid . ".php";
		$savedsettings = saveFile($newsettings,$loadedsettings);		

		// create a new blank datafile
		$dpath = "surveys/data_" . $newid . ".dat";
		$saved_datafile =  saveFile($dpath,"");		

		if ($savedsettings || $savedquest) {
			$_SESSION['statusmsg'] = "The project was duplicated.";
			header("Location: index.php");
		} else {
			$_SESSION['statusmsg'] = "Sorry, the project could not be duplicated.";
			header("Location: index.php");
		}

	  break;

	
	default :
	
		$filemenu = makeQuestionnaireMenu("surveys/","","array");
		$templatemenu = makeTemplateMenu("templates/","AskPeopleDefault","templatename","TRUE","array");
		$othermenu = makeTemplateMenu("templates/","","templatename","NOT");
	
		foreach((array)$filemenu as $sk => $stitle) {
			
			// look in config for this survey to check the owner
			// and check for the IDs of users it is shared with			
			// append to menu if it is included in either of these

			$surveysettings = null; // reset all the vars from the previous iteration...
			$settingslabels = null;
			$group_id_array = null;
			$group = null;
			
			$cpath = "surveys/config_{$sk}.php";
			include_once($cpath);
			
			$group = @explode(";",$surveysettings['surveyGroup']);
			foreach((array)$group as $g) {
				$group_id_array[] = substr($g,-2,1); // penultimate character is user ID of group member
			}
			
			if ($surveysettings['surveyOwner'] == $cookieinfo['ID']) { // logged in user is owner of this survey
				$allowedaccess[] = $sk;
				$projectchunk .= "<li class='projlink owner'><a href='projmgr.php?sid={$sk}&stitle=".urlencode($stitle)."' title='Edit project'><img src='images/icons/database_form_16x16.gif' class='icon' alt='Edit project' width='16' height='16' border='0' />{$stitle}</a></li>\n";
			} elseif (@in_array($cookieinfo['ID'],$group_id_array)) {
				$allowedaccess[] = $sk;
				$projectchunk .= "<li class='projlink group'><a href='projmgr.php?sid={$sk}&stitle=".urlencode($stitle)."' title='Edit project that has been shared with you'><img src='images/icons/database_shared_16x16.gif' class='icon' alt='Edit shared project' width='16' height='16' border='0' />{$stitle}</a></li>\n";				
			} elseif (eregi("SAMPLE:",$stitle)) { // one of the sample surveys
				$allowedaccess[] = $sk;
				$projectchunk .= "<li class='projlink sample'><a href='projmgr.php?sid={$sk}&stitle=".urlencode($stitle)."' title='Edit a sample project'><img src='images/icons/database_form_16x16.gif' class='icon' alt='Edit project' width='16' height='16' border='0' />{$stitle}</a></li>\n";							
			} elseif (!isset($surveysettings['surveyOwner'])) { // for backwards compatibility - show if no Owner is set
				$allowedaccess[] = $sk;
				$projectchunk .= "<li class='projlink owner'><a href='projmgr.php?sid={$sk}&stitle=".urlencode($stitle)."' title='Edit project'><img src='images/icons/database_form_16x16.gif' class='icon' alt='Edit project' width='16' height='16' border='0' />{$stitle}</a></li>\n";				
			}
			
		}

		foreach((array)$templatemenu as $tk => $ttitle) {
			$templatechunk .= "<li class='templink'><a href='templatemgr.php?action=EditTemplate&templatename=".urlencode($ttitle)."'><img src='images/icons/file_(edit)_16x16.gif' class='icon' alt='Edit template' width='16' height='16' border='0' />".str_replace(".php","",$ttitle)."</a></li>\n";
		}
		
		if (is_array($allowedaccess)) {
			$_SESSION['allowedaccess'] = $allowedaccess;
		}
		
		$_SESSION['statusmsg'] = null;
		
		$outputform = "
			<div id=\"projects\" class=\"adminlinks\" style=\"width:49%; float:left;\">
				<div class=\"clearbox\">
					<h2>Projects</h2>
					<ul id='newproject'>
						<li class='newprojlink'><a href='index.php?action=Create'><img src='images/icons/database_form_(add)_16x16.gif' class='icon' alt='New project' width='16' height='16' border='0' />Create new project</a></li>
					</ul>
					<ul id='projectlist'>
					{$projectchunk}
					</ul>
				</div>
				
		   		<div class=\"clearbox\">
					<h2>AskPeople Options</h2>
					<ul>
						<li><a href='configmgr.php?filename=apconfig.php'><img src='images/icons/settings1_16x16.gif' class='icon' alt='Manage AskPeople Settings' width='16' height='16' border='0' />Manage AskPeople settings</a></li>
					</ul>
				</div>

		   		<div class=\"clearbox\">
					<h2>File manager</h2>
					<ul>
						<li><a href='filemgr.php'><img src='images/icons/web-server_16x16.gif' class='icon' alt='Upload/download files' width='16' height='16' border='0' />Upload images &amp; files</a></li>
					</ul>
				</div>
			";

		if($cookieinfo['level']>5) {
			$outputform .= "
				<div class=\"clearbox\">
					<h2>Users</h2>
					<ul>
						<li><a href='usermgr.php?action=ManageUsers'><img src='images/icons/administrator1_(edit)_16x16.gif' class='icon' alt='Manage Users' width='16' height='16' border='0' />Manage users</a></li>
					</ul>
				</div>
				";
		}
		
		$outputform .= "</div>
			
			<div id=\"templates\" class=\"adminlinks\" style=\"width:49%; float:right;\">
				<div class=\"clearbox\">
					<h2>Templates</h2>
					<ul id='newtemp'>
						<li class='newtemplink'><a href='templatemgr.php?action=CreateTemplate'><img src='images/icons/file_(add)_16x16.gif' class='icon' alt='New template' width='16' height='16' border='0' />Create new template</a></li>
					</ul>
					<ul id='templist'>
					{$templatechunk}
					</ul>
					<br />
					<h3>Other files used within the templates:</h3>
					<form action='templatemgr.php' method='post'>
						<input type='hidden' name='otherfile' value='true' />
						{$othermenu} <input type='submit' value='Edit' />
					</form>
				</div>
		   		
			</div>	
		";
	
	  break;

}

// output the HTML
echo ($outputform);

echo ("<!-- ");
print_r($cookieinfo);
echo (" -->");

include('includes/footer.php');

?>
Return current item: AskPeople-Free PHP survey application