Location: PHPKode > projects > Liki - The LaTeX Wiki > liki/lib/likiconfig.php
<?php
/*
    Copyright (C) 2004 The Liki Programming Team.
 
    This file is part of Liki.

    Liki is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Liki is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Liki; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
class likiconfig{
	
	function likiconfig(){
	}
	function initial(){
/*
	Global 
*/
$likiconfig = array(

'liki' => array(
	'baseurl' => "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) ,
	'lib' => 'lib',
	'lock' => '10',//Time, in minutes, a page is edited for.
	'count' => '20', //Number of recent changes to display
	'global_read' => '0',
	'global_edit' => '0',
	'public_read' => '0',
	'public_edit' => '0',
	'public_compiled' => '1',
	'su' => '21232f297a57a5a743894a0e4a801fc3'),
/*
	HTML Options
*/
'html' => array(
	'title' =>"Liki - The LaTeX Wiki",
	'style' => "lib/style/liki.css",
	'name' => 'liki.sf.net',
	'description' => 'A Wiki for LaTeX Documents',
	'language' => 'en-gb',
	'date' => 'r'),
/*
	User Details 

	User info is stored in an array, with the ID a hash of the username. [change : could have collisions]
*/

'users' => array(
	'MD521232f297a57a5a743894a0e4a801fc3' => array(
		'pass' => '807ff71b3301262e222ad05e5b7c4325', //	admin:ChangeMe
		'name' => 'Administrator',
		'addusers' => '1',
		'editconfig' => '1')
	),
	
/*
	Database Options
*/
'dbparms' => array(
	'dbtype' => 'file',
	'prefix' => 'liki_',
	'directory' => './likiDB'),

/*
LaTeX Paramaters, for use in checkout.php
*/
//maybe add a boolean for each one so index won't throw up the option if not there.
'latex' => array(
	'basedir' => '.',
	'latex' => "/usr/bin/latex",
	'bibtex' => "/usr/bin/bibtex",
	'bibtex_params' => " -terse ",
	'latex_params'  => ' --interaction=nonstopmode ',
	'dvips' =>"/usr/bin/dvips",
	'ps2pdf' => "/usr/bin/ps2pdf")
);
require_once 'Config.php';
$c = new Config();
$root =& $c->parseConfig($likiconfig, "PHPArray");
$c->writeConfig($likiconfig['dbparms']['directory']."/config.xml", "XML");

return $likiconfig;
}

function getConfig(){
	require_once 'Config.php';
	if(file_exists("config.loc")){
		$fp = fopen("config.loc",'r');
		$path = fgets($fp,1024);
		if(file_exists($path)){
	//		echo 'found path';
			$c = new Config();
			$root =& $c->parseConfig($path, "XML");
			if (PEAR::isError($root)) {
      			  die($root->getMessage());
			}
			$root = $root->toArray();
			$temp = $root['root']['conf'];
			return $temp;
		}
		else
			return likiconfig::initial();
	}
	else {
		touch('config.loc');
		$fp = fopen('config.loc','w');
		fwrite($fp,'./likiDB/config.xml');
		fclose($fp);
		return likiconfig::initial();
	}
}
function doConfigHTML(&$auth,&$db,$new = ''){
	$likiparms = $auth->likiparms;
	$user = $auth->user;
	require_once "HTML/QuickForm.php";
	if($new == '' && $auth->authCfg()){
		$form = new HTML_QuickForm('configForm', 'post','index.php?action=config&update=true','','class="content"','style ="content",colspan=2');
		$form->addElement('html','<td width="50%"><table>'); 
		$form->addElement('html','<tr><td><b><u>Liki Options</u></b></td></tr>','Liki Options');
		$form->addElement('text','baseurl', 'Base Address','align="left"');
		$form->addElement('text','lib','Liki Library Source');
		$form->addElement('text','lock','Minutes to lock pages');
		$form->addElement('text','count','Recent changes displayed');
		$form->addElement('checkbox','global_read', 'Any user can read.');
		$form->addElement('checkbox','global_edit', 'Any user can edit.');
		$form->addElement('checkbox','public_compiled','Public access to compiled document');
		$form->addElement('checkbox','public_edit', 'Public can edit.');
		$form->addElement('checkbox','public_read','Public can read files.');
		if($likiparms['liki']['su'] == $user){
			$userlist = array(' '=>' ');
			foreach($likiparms['users'] as $hash => $stuff){
				$name = $stuff['name'];
				$hash = str_replace('MD5','',$hash);
				if($user != $hash)
					$userlist[$hash] = $name;
			}
			$form->addElement('select','new_admin', 'Change Admin',$userlist);
		}
		$form->addElement('html','<tr><td><b><u>HTML Options</u></b></td></tr>','HTML Options');
		$form->addElement('text','title',"Title start");
		$form->addElement('text','style','CSS Stylesheet');
		$form->addElement('text','name', 'Name of the Liki');
		$form->addElement('text','description','Description of the Liki');
		$form->addElement('text','language','Language of the Liki');
		$form->addElement('text','date','Date Format');

		$form->addElement('html','</table></td><td width="50%"><table>'); 
		$form->addElement('html','<tr><td><b><u>Database Options</u></b></td></tr>','Database Options');
		$form->addElement('select', 'dbtype','Database Type',array('file'=>'File Based'));
		$form->addElement('text','prefix','Prefix for files');
		$form->addElement('text','directory','Directory to store files');
	
		$form->addElement('html','<tr><td><b><u>LaTeX Options</u></b></td></tr>','LaTeX Options');
		$form->addElement('text','basedir','Project Source Directory');
		$form->addElement('text','latex','Path to LaTeX');
		$form->addElement('text','latex_params','LaTeX parameters');	
		$form->addElement('text','bibtex','Path to BibTeX');
		$form->addElement('text','bibtex_params','BibTeX parameters');
		$form->addElement('text','dvips','Path to dvips');
		$form->addElement('text','ps2pdf','Path to ps2pdf');
		$form->addElement('html','</td></table>');
		$form->setDefaults(array_merge($likiparms['liki'],$likiparms['html']));
		$form->setDefaults($likiparms['html']);
		$form->setDefaults($likiparms['dbparms']);
		$form->setDefaults($likiparms['latex']);
		$form->addElement('submit','Submit','Submit Changes');
		$renderer =& $form->defaultRenderer();
		// no bold, left aligned
		$renderer->setElementTemplate(
		"\n\t<tr>\n\t\t<td align=\"left\"  class=\"config\">
		<!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required -->
			{label}</td>\n\t\t<td valign=\"top\" align=\"left\">
		<!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->
			\t{element}</td>\n\t</tr>");
		$form->display();
	}
	elseif ($new == 'newuser' && $likiparms['users']['MD5'.$user]['addusers'] ==1){
		$form = new HTML_QuickForm('userForm', 'post','index.php?action=config&update=true','','class="content"','style ="content"');
		$form->addElement('html','<tr><td><b><u>Add New User</u></b></td></tr>','Add New User');
		$form->addElement('text','new_username', 'Username','align="left"');
		$form->addElement('password','new_password','Password');
		$form->addElement('text','new_name','Name');
		$form->addElement('checkbox','new_addusers','Can add users');
		if($likiparms['users']['MD5'.$user]['editconfig'] ==1)
			$form->addElement('checkbox','editconfig','Allow to edit Config.');
		$form->addElement('submit','Submit','Add User');
		$renderer =& $form->defaultRenderer();
		// no bold, left aligned
		$renderer->setElementTemplate(
		"\n\t<tr>\n\t\t<td align=\"left\"  class=\"config\">
		<!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required -->
			{label}</td>\n\t\t<td valign=\"top\" align=\"left\">
		<!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->
			\t{element}</td>\n\t</tr>");
		$form->display();
	}
	elseif($new == 'changeDetails'){
		$form = new HTML_QuickForm('changedetailsForm', 'post','index.php?action=config&update=true','','class="content"','style ="content"');
		$form->addElement('html','<tr><td><b><u>Change <i>'.$likiparms['users']['MD5'.$user]['name'].'</i> Details</u></b></td></tr>','Change Details');
		$form->addElement('text','new_name', 'New Name (optional)');		
		$form->addElement('password','old_password','Old Password (required)');
		$form->addElement('password','new_password','New Password (optional)');
		$form->addElement('submit','Submit','Update Details');
		$form->setDefaults(array('new_username' => $likiparms['users']['MD5'.$user]['name']));
		$renderer =& $form->defaultRenderer();
		// no bold, left aligned
		$renderer->setElementTemplate(
		"\n\t<tr>\n\t\t<td align=\"left\"  class=\"config\">
		<!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required -->
			{label}</td>\n\t\t<td valign=\"top\" align=\"left\">
		<!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->
			\t{element}</td>\n\t</tr>");
		$form->display();
	}
}
function doUpdate($new,&$auth,&$db){

	if(!$auth->loggedin)
		return 'Not logged on';
	$user = $auth->user;
	$liki = $auth->likiparms['liki'];
	$html = $auth->likiparms['html'];
	$dbparms = $auth->likiparms['dbparms'];
	$latex = $auth->likiparms['latex'];
	$users = $auth->likiparms['users'];
	//print_r($new);	
	//echo $new['editconfig'];
	//echo $new['new_addusers'];
	$ret = 'New configuration saved.';
	if(isset($new['old_password'])){
			if(md5($new['old_password']) == $users['MD5'.$user]['pass']){
					if(isset($new['new_password']) && $new['new_password'] != '')
						$users['MD5'.$user]['pass'] = md5($new['new_password']);
					if(isset($new['new_name']) && $new['new_name'] != '')
						$users['MD5'.$user]['name'] = $new['new_name'];
			}
			else
				return 'Invalid Password';
	}
	else if(isset($new['new_username']) && $users['MD5'.$user]['addusers'] ==1){
		if(!isset($new['editconfig'])) $new['editconfig'] = '0';
		if(!isset($new['new_addusers'])) $new['new_addusers'] = '0';
		if(isset($new['new_username']) && isset($new['new_password']) && isset($new['new_name'])){
			$newusername = md5($new['new_username']);
			$newpassword = md5($new['new_password']);
			if(isset($users['MD5'.$newusername]))
				$ret =  'Username in use';
			else {
				if(!$new['editconfig'] || $users['MD5'.$user]['editconfig'] == 0)
					$new['editconfig'] = 0;
				$users['MD5'.$newusername] = array(
				'pass' => $newpassword,
				'name' => $new['new_name'],
				'editconfig' => $new['editconfig'],
				'addusers' => $new['new_addusers']);
				$ret = 'User Added';
			}
		}
	}
	else if (isset($new['file_config']) && isset($new['config_filename'])){
		$page = $db->getPage($new['config_filename']);
		print_r($new);
		if($page->get('owner') != $user)
			return 'No authorisation';
		if (isset($new['delete_reader']) && $new['delete_reader'] != ' '){
			$s = $page->get('read');
			$s = str_replace($new['delete_reader'],"", $s);
			$s = str_replace("--","-",  $s);
			$page->set('read',$s);	
			//echo ".............".$s;	
		}
		if (isset($new['delete_editer']) && $new['delete_editer'] != ' '){
			$s = $page->get('edit');
			str_replace($new['delete_editer'], "", $s);
			str_replace("--", "-", $s);
			$page->set('edit',$s);
		}	
		if (isset($new['delete_owner']) && $new['delete_owner'] != ' '){
			$s = $page->get('owner');
			str_replace($new['delete_owner'], "", $s);
			str_replace("--", "-", $s);
			$page->set('owner',$s);
		}
		if(isset($new['new_reader']) && $new['new_reader'] != '' && $new['new_reader'] != ' '){
			$s = $page->get('read');
			$s .= '-'.$new['new_reader'];
			$page->set('read',$s);
		}
		if(isset($new['new_editer']) && $new['new_editer'] != '' && $new['new_editer'] != ' '){
			echo 'new editor';
			$s = $page->get('edit');
			$s .= '-'.$new['new_editer'];
			$page->set('edit',$s);
		}
		if(isset($new['new_owner']) && $new['new_owner'] != '' && $new['new_owner'] != ' '){
			$s = $page->get('owner');
			$s .= '-'.$new['new_owner'];
			$page->set('owner',$s);
		}
	}
	else if ($users['MD5'.$user]['editconfig'] ==1){
		if(!isset($new['global_read'])) $new['global_read'] = '0';
		if(!isset($new['public_read'])) $new['public_read'] = '0';
		if(!isset($new['public_edit'])) $new['public_edit'] = '0';
		if(!isset($new['global_edit'])) $new['global_edit'] = '0';
		if(!isset($new['public_compiled'])) $new['public_compiled'] = '0';
		if(isset($new['new_admin']) && $new['new_admin'] != '' && $new['new_admin'] != ' ' && $user == $liki['su']){

			$new['su'] = $new['new_admin'];
		}
		else {

			$new['su'] = $liki['su'];
		}
	foreach($liki as $key => $value)
		if (!isset($new[$key]))
			$ret =  "Error : Invalid configuration";
	foreach($html as $key => $value)
			if (!isset($new[$key]))
				$ret =  "Error : Invalid configuration";
	foreach($dbparms as $key => $value)
			if (!isset($new[$key]))
				$ret =  "Error : Invalid configuration";
	foreach($latex as $key => $value)
			if (!isset($new[$key]))
				$ret =  "Error : Invalid configuration";
	foreach($new as $key => $value){
		if( isset($liki[$key]))
			$liki[$key] = $value;
		else if( isset($html[$key]))
			$html[$key] = $value;
		else if( isset($dbparms[$key])){
				if($dbparms[$key] != $value && $key =='directory'){
					if(!file_exists('config.loc')) touch('config.loc');
					$fp = fopen('config.loc','w');
					fwrite($fp,$value.'/config.xml');
					fclose($fp);
				}
				$dbparms[$key] = $value;
		}
		else if( isset($latex[$key]))
				$latex[$key] = $value;
		}
		
	}
	else{
		return 'No authorisation';
	}
	$likiconfig = array('liki' => $liki,'html'=>$html,'dbparms'=>$dbparms,'latex'=>$latex,'users'=>$users);
	require_once 'Config.php';
	$c = new Config();
	$root =& $c->parseConfig($likiconfig, "PHPArray");
	//if(!file_exists($likiconfig['dbparms']['directory']."/config.xml")) touch($likiconfig['dbparms']['directory']."/config.xml");
	$c->writeConfig($likiconfig['dbparms']['directory']."/config.xml", "XML");
//echo $likiconfig['dbparms']['directory']."/config.xml";
	return $ret;
	
}
function doFileConfig($filename,&$auth,&$db){
	$page = $db->getPage($filename);
	$likiconfig = $auth->likiparms;
	$user = $auth->user;
	if(!$auth->authTex('owner',$page)){
		echo "No Configuration authorization"; return;
	}
	require_once "HTML/QuickForm.php";
	$form = new HTML_QuickForm('fileForm', 'post','index.php?action=config&update=true','','class="content"','style ="content"');
	$form->addElement('html','<tr><td><b><u>Change Access to <i>'.$filename.'</i></u></b></td></tr>','change owner');
	$form->addElement('hidden','file_config','file_config');
	$form->addElement('hidden','config_filename',$filename);

	$userlist = array(' '=>' ');
	foreach($likiconfig['users'] as $hash => $stuff){
		$name = $stuff['name'];
		$hash = str_replace('MD5','',$hash);
		if($user != $hash)
			$userlist[$hash] = $name;
	}
	$form->addElement('select','new_reader', 'New Reader',$userlist);	
	$form->addElement('select','new_editer', 'New Editer',$userlist);
	$form->addElement('select','new_owner', 'New Owner',$userlist);
	if( ($read = $page->get('read')) ){
		$deletes = array(' ' => ' ');
		foreach(explode("-",$read) as $r)
			if(strlen($r) == 32)
				$deletes[$r] = $likiconfig['users']['MD5'.$r]['name'];
		$form->addElement('select','delete_reader','Delete Reader',$deletes);
	}
	if( ($read = $page->get('edit')) ){
		$deletes = array(' ' => ' ');
		foreach(explode("-",$read) as $r)
			if(isset($likiconfig['users']['MD5'.$r]['name'])) $deletes[$r] = $likiconfig['users']['MD5'.$r]['name'];
		$form->addElement('select','delete_editer','Delete Writer',$deletes);
	}
	if( ($read = $page->get('owner')) ){
		$deletes = array(' ' => ' ');
		foreach(explode("-",$read) as $r)
			$deletes[$r] = $likiconfig['users']['MD5'.$r]['name'];
		$form->addElement('select','delete_owner','Delete Owner',$deletes);
	}
	$form->addElement('submit','Submit','Make Changes');
	$renderer =& $form->defaultRenderer();
	// no bold, left aligned
	$renderer->setElementTemplate(
	"\n\t<tr>\n\t\t<td align=\"left\"  class=\"config\">
	<!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required -->
		{label}</td>\n\t\t<td valign=\"top\" align=\"left\">
	<!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->
		\t{element}</td>\n\t</tr>");
	$form->display();

}
}
?>
Return current item: Liki - The LaTeX Wiki