Location: PHPKode > scripts > Yet Another Link Directory > yald2/admin/editconfig.php
<?php
/******************************************************************************
* This file is part of Yet Another Link Directory.                            *
*                                                                             *
* Yet Another Link Directory 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.                                         *
*                                                                             *
* Yet Another Link Directory 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 Yet Another Link Directory; if not, write to the Free Software   *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
******************************************************************************/

require('../inc/config.php');
require('../inc/functions.php');
require('../inc/version.php');
mysql_connect($mysql['host'],$mysql['username'],$mysql['password']);
mysql_select_db($mysql['db']);

$settings = getSettings();

session_start();

if(!isset($_SESSION['yald_admin_logged_in'])){
	header('Location: login.php');
	exit;
}

if(isset($_GET['htaccess'])){
	$begin_url = trim($settings['mod_rewrite_url'],'/\\');
	$end_dir = dirname($settings['actual_path']).'/';
	print "Copy and paste the following .htaccess file and place it in the document root of your website.<br /><br />";
	print "<textarea cols=\"40\" rows=\"5\">RewriteEngine on
RewriteRule ^{$begin_url}$ {$settings['mod_rewrite_url']} [R]
RewriteRule ^{$begin_url}/([a-zA-Z1-9\-/\?=]*(\.html)?)?$ {$settings['actual_path']}?viewcat=$1&catname=0 [L,QSA]
RewriteRule ^{$begin_url}/([a-zA-Z1-9\-/\?=\.]*)?$ {$settings['actual_path']}?viewcat=$1&catname=0 [L,QSA]
RewriteRule ^{$begin_url}/((\d+)_(.*))?$ {$settings['actual_path']}?viewcat=$2&catname=$3
RewriteRule ^{$begin_url}/(.*)$ {$end_dir}$1 [L,QSA]</textarea>";
	exit;
}

$template = file_get_contents('template.html');
$yald_body = '';

foreach($settings as $key=>$value){
	$sticky[$key] = $value;
}

if(isset($_POST['submitted'])){
	foreach($_POST as $key=>$value){
		$sticky[$key] = $value;
	}

	if(empty($_POST['max_url_length']) || !is_numeric($_POST['max_url_length']) || $_POST['max_url_length'] > 255){
		$error = 'You must enter a value 255 or less for max URL length.';
	}
	if(empty($_POST['max_title_length']) || !is_numeric($_POST['max_title_length']) || $_POST['max_title_length'] > 255){
		$error = 'You must enter a value 255 or less for max title length.';
	}
	if(empty($_POST['max_description_length']) || !is_numeric($_POST['max_description_length'])){
		$error = 'You must enter a max description length.';
	}
	if(empty($_POST['actual_path'])){
		$error = 'You must specify the index file.';
	}
	if(empty($_POST['show_num_subs']) || !is_numeric($_POST['show_num_subs'])){
		$error = 'Enter a numeric value for number of subcategories to show.';
	}
	if($_POST['list_type'] == '1' && $_POST['jump_links'] != '1'){
		$error = 'To order links by number of clicks, jump linking must be enabled.';
	}
	if(($_POST['url_type'] == '2' || $_POST['url_type'] == '3') && empty($_POST['mod_rewrite_url'])){
		$error = 'Since you have enabled search engine friendly URLs, you must specify a rewrite URL.';
	}
	if(empty($_POST['admin_user'])){
		$error = 'You must specify a username for the administrator.';
	}
	if($_POST['use_pagination'] == '1' && (empty($_POST['links_per_page']) || !is_numeric($_POST['links_per_page']))){
		$error = 'You must enter a numeric value for links per page.';
	}
	if($_POST['disp_latest'] == '1' && (empty($_POST['num_latest_links']) || !is_numeric($_POST['num_latest_links']))){
		$error = 'You must enter a numeric value for number of latest links to display.';
	}
	if((empty($_POST['count_refresh_time']) && $_POST['count_refresh_time'] != '0') || !is_numeric($_POST['count_refresh_time'])){
		$error = 'You must enter a numeric link count cache expiry.';
	}


	if(!isset($error)){
		// update all settings in database
		$newsettings['site_title'] = manual_serialize($_POST['site_title'],'str');
		$newsettings['jump_links'] = manual_serialize($_POST['jump_links'],'bool');
		$newsettings['max_url_length'] = manual_serialize($_POST['max_url_length'],'str');
		$newsettings['max_title_length'] = manual_serialize($_POST['max_title_length'],'str');
		$newsettings['max_description_length'] = manual_serialize($_POST['max_description_length'],'str');
		$newsettings['index_file'] = manual_serialize($_POST['actual_path'],'str');
		$newsettings['display_admin_link'] = manual_serialize($_POST['display_admin_link'],'bool');
		$newsettings['home_link'] = manual_serialize($_POST['home_link'],'str');
		$newsettings['show_num_subs'] = manual_serialize($_POST['show_num_subs'],'int');
		$newsettings['use_captcha'] = manual_serialize($_POST['use_captcha'],'bool');
		$newsettings['check_links'] = manual_serialize($_POST['check_links'],'bool');
		$newsettings['list_type'] = manual_serialize($_POST['list_type'],'str');
		$newsettings['url_type'] = manual_serialize($_POST['url_type'],'str');
		$newsettings['mod_rewrite_url'] = manual_serialize($_POST['mod_rewrite_url'],'str');
		$newsettings['admin_user'] = $_POST['admin_user'];
		if(!empty($_POST['admin_pass'])){
			$newsettings['admin_pass'] = sha1($_POST['admin_pass']);
		}
		$newsettings['default_template'] = manual_serialize($_POST['default_template'],'str');
		$newsettings['recursive_counts'] = manual_serialize($_POST['recursive_counts'],'bool');
		$newsettings['count_refresh_time'] = manual_serialize($_POST['count_refresh_time'],'int');
		$newsettings['use_pagination'] = manual_serialize($_POST['use_pagination'],'bool');
		$newsettings['links_per_page'] = manual_serialize($_POST['links_per_page'],'int');
		$newsettings['disp_latest'] = manual_serialize($_POST['disp_latest'],'bool');
		$newsettings['num_latest_links'] = manual_serialize($_POST['num_latest_links'],'int');


		foreach($newsettings as $key=>$value){
			if($value === false) die('Internal error with serializing on '.$key);
			if(!mysql_query('UPDATE `'.$settings['settings_table'].'` SET `value`="'.mysql_real_escape_string($value).'" WHERE `setting`="'.$key.'"')){
				$error = 'One or more of these settings was not saved due to an error that occurred while executing the query.';
			}
		}

		if(!isset($error)){
			logEvent('upd_config','0',null);
			$error = 'These settings were saved.';
		}
	}

}


function radio_selected($key,$value){
	global $sticky;
	if(is_bool($sticky[$key])){
		if($sticky[$key]==true){
			$sticky[$key] = '1';
		} else {
			$sticky[$key] = '0';
		}
	}
	if($sticky[$key] == $value){
		return 'checked="checked" ';
	}
}

function menu_item_selected($key,$value){
	global $sticky;
	if($sticky[$key] == $value){
		return ' selected="selected" ';
	}

}

function theme_menu(){
	global $settings;
	$dir = '../'.$settings['template_dir'];
	$result = '';
	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				if(filetype($dir.'/'.$file) == 'dir' && $file != '.' && $file != '..'){
					$result .= '<option value="'.$file.'"'.menu_item_selected('default_template',$file).'>'.ucwords($file).'</option>';
				}
			}
			closedir($dh);
		}
	}

	if(empty($result)){
		$result = '<option value="classic">Classic</option>';
	}

	return $result;

}

$yald_head = <<<EOF
<style type="text/css">
<!--
.style1 {font-size: x-small}
-->
</style>

<script>
function hide_div(divid,field){
	if(document.getElementById(field).checked == true){
	document.getElementById(divid).style.visibility='hidden';
	} else {
	document.getElementById(divid).style.visibility='visible';
	}
}

function mod_rewrite_hide(){
	if(document.getElementById('url_type').value == '2' || document.getElementById('url_type').value == '3'){
		document.getElementById('mod_rewrite_url_div').style.display='table-row';
	} else {
		document.getElementById('mod_rewrite_url_div').style.display='none';
	}
}

window.onload = function(){
hide_div('num_latest_links_div','disp_latest_1');
hide_div('links_per_page_div','use_pagination_1');
mod_rewrite_hide();
}
</script>
EOF;

if(isset($error)){
	$yald_body .= '<div align="center"><div class="errorbox"><b>'.$error.'</b></div></div>';
}
$yald_body .= '<b>Update Configuration:</b><br /><small>Use this page to configure just about every setting on YALD.</small><br />
<form name="settings" method="post" action="'.$_SERVER['PHP_SELF'].'">
<table width="95%" border="0" cellpadding="10">
  <tr>
    <td width="34%"><b>Site Title</b><br />
    <span class="style1">What would you like to name the directory?</span></td>
    <td width="66%" valign="top"><input type="text" name="site_title" id="site_title" value="'.htmlentities($sticky['site_title']).'" /></td>
  </tr>
  <tr>
    <td><b>Link Type</b><br />
      <span class="style1">This affects the way the directory links to other pages. Jump links will look like http://example.com/yald/directory.php?jump=<em>linkid</em></span></td>
    <td valign="top"><p>
      <label>
        <input type="radio" name="jump_links" value="0" id="jump_links_0" '.radio_selected('jump_links','0').'/>
        Direct Linking</label>
      <label>
        <input type="radio" name="jump_links" value="1" id="jump_links_1" '.radio_selected('jump_links','1').'/>
        Jump Linking</label>
      <br />
    </p></td>
  </tr>
  <tr>
    <td><b>Maximum URL Length</b><br />
      <span class="style1">Maximum number of characters accepted for a link URL. This must be no more than 255.</span></td>
    <td valign="top"><input name="max_url_length" type="text" id="max_url_length" size="4" maxlength="4" value="'.htmlentities($sticky['max_url_length']).'" /> 
      characters</td>
  </tr>
  <tr>
    <td><b>Maximum Title Length</b><br />
    <span class="style1">Maximum number of characters accepted for a link title. This must be no more than 255.</span></td>
    <td valign="top"><input name="max_title_length" type="text" id="max_title_length" size="4" maxlength="4" value="'.htmlentities($sticky['max_title_length']).'" />
characters</td>
  </tr>
  <tr>
    <td><b>Maximum Description Length</b><br />
        <span class="style1">Maximum number of characters accepted for a link description.</span></td>
    <td valign="top"><input name="max_description_length" type="text" id="max_description_length" size="4" maxlength="4" value="'.htmlentities($sticky['max_description_length']).'" />
      characters</td>
  </tr>
  <tr>
    <td><b>Index File Location</b><br />
      <span class="style1">This should correspond to the path to your index file (originally directory.php). If the file is called directory.php and the script is in a folder called yald, you would probably enter /yald/directory.php.</span></td>
    <td valign="top"><input name="actual_path" type="text" id="index_file" value="'.htmlentities($sticky['actual_path']).'" /></td>
  </tr>
  <tr>
    <td><b>Display Admin Link?</b><br />
      <span class="style1">Specify whether of not you would like to display a link to the admin panel on your main directory view.</span></td>
    <td valign="top"><p>
      <label>
        <input type="radio" name="display_admin_link" value="1" id="display_admin_link_0" '.radio_selected('display_admin_link','1').'/>
        Yes</label>
      <label>
        <input type="radio" name="display_admin_link" value="0" id="display_admin_link_1" '.radio_selected('display_admin_link','0').'/>
        No</label>
      <br />
    </p></td>
  </tr>
  <tr>
    <td><b>Home Link</b><br />
      <span class="style1">URL to your site\'s home page. This will be displayed at the beggining of the breadcrumb navigation.</span></td>
    <td valign="top"><input type="text" name="home_link" id="home_link" value="'.htmlentities($sticky['home_link']).'" /></td>
  </tr>
  <tr>
    <td><b>Number of Subcategories to Show</b><br />
      <span class="style1">This setting only pertains to categories which have the display subcategories attribute enabled.</span></td>
    <td valign="top"><input name="show_num_subs" type="text" id="show_num_subs" size="2" maxlength="3" value="'.htmlentities($sticky['show_num_subs']).'" /></td>
  </tr>
  <tr>
    <td><b>Use Captcha?</b><br />
      <span class="style1">Would you like to use image verification to prevent robots from submitting links? Your server must have GD installed for this to work.</span></td>
    <td valign="top"><label>
      <input type="radio" name="use_captcha" value="1" id="use_captcha_0" '.radio_selected('use_captcha','1').'/>
Yes</label>
      <label>
      <input type="radio" name="use_captcha" value="0" id="use_captcha_1" '.radio_selected('use_captcha','0').'/>
No</label></td>
  </tr>
  <tr>
    <td><b>Check Links?</b><br />
      This setting will make sure user submitted links are accessible before adding them to the pending links list.</td>
    <td valign="top"><label>
      <input type="radio" name="check_links" value="1" id="check_links_0" '.radio_selected('check_links','1').'/>
Yes</label>
      <label>
      <input type="radio" name="check_links" value="0" id="check_links_1" '.radio_selected('check_links','0').'/>
No</label></td>
  </tr>
  <tr>
    <td><b>Link List Type</b><br />
      <span class="style1">How would you like to order links in the directory? Number of clicks requires jump linking to be enabled above.</span></td>
    <td valign="top"><select name="list_type" id="list_type">
      <option value="1"'.menu_item_selected('list_type','1').'>Number of clicks (requires jump links)</option>
      <option value="2"'.menu_item_selected('list_type','2').'>Alphabetically</option>
      <option value="3"'.menu_item_selected('list_type','3').'>Date added (ascending)</option>
    </select>    </td>
  </tr>
  <tr>
    <td><b>Category URL Format</b><br />
      <span class="style1">Search engine friendly URLs require an Apache server with mod_rewrite and .htaccess support. Standard URLs will look like http://example.com/yald/directory.php?viewcat=<em>catid. </em>Basic search engine friendly will look like http://example.com/yald/<em>catid</em>_<em>catname</em>. Advanced search engine friendly URLs will include the names of all subcategories in the URL, for example http://example.com/yald/<em>category1</em>/<em>subcategory1</em>/.</span><br /></td>
    <td valign="top"><select name="url_type" id="url_type" onchange="mod_rewrite_hide()">
      <option value="1"'.menu_item_selected('url_type','1').'>Standard</option>
      <option value="2"'.menu_item_selected('url_type','2').'>Basic search engine friendly</option>
      <option value="3"'.menu_item_selected('url_type','3').'>Advanced search engine friendly</option>
    </select>    </td>
  </tr>

  <tr id="mod_rewrite_url_div">
    <td><b>Rewrite URL</b><br />
      <span class="style1">This is only necessary if the above setting is set to something other than standard. This MUST be set to something other than the real location of YALD. If YALD is really installed in a folder called /yald/, entering /directory/ here will make it appear as though YALD is installed in /directory/ but cannot be set to /yald/. Be sure to enter a trailing forward slash.</span><br /></td>
    <td valign="top"><input type="text" name="mod_rewrite_url" id="mod_rewrite_url" value="'.htmlentities($sticky['mod_rewrite_url']).'" /><br /><a href="#" onClick="MyWindow=window.open(\''.$_SERVER['PHP_SELF'].'?htaccess=1\',\'.htaccess\',\'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=300\'); return false;">View your .htaccess</a></td>
  </tr>

  <tr>
    <td><b>Admin Username</b><br />
      <span class="style1">Set the administrator\'s username.</span></td>
    <td valign="top"><input type="text" name="admin_user" id="admin_user" value="'.htmlentities($sticky['admin_user']).'" /></td>
  </tr>
  <tr>
    <td><b>Admin Password</b><br />
      <span class="style1">Set the administrator\'s password. If left blank, nothing will be changed.</span></td>
    <td valign="top"><input type="password" name="admin_pass" id="admin_pass" /></td>
  </tr>
  <tr>
    <td><b>Directory Theme</b><br />
      <span class="style1">Select a theme to use for your directory.</span><br /></td>
    <td valign="top"><select name="default_template" id="default_template">
    '.theme_menu().'
    </select>    </td>
  </tr>
  <tr>
    <td><b>Recursive Link Counts</b><br />
      <span class="style1">Recursive link counting will count all links in a category including its subcategories. Disabling this will cause only the links in that category to be counted.</span></td>
    <td valign="top"><label>
      <input type="radio" name="recursive_counts" value="1" id="recursive_counts_0" '.radio_selected('recursive_counts','1').'/>
Enabled</label>
      <label>
      <input type="radio" name="recursive_counts" value="0" id="recursive_counts_1" '.radio_selected('recursive_counts','0').'/>
Disabled</label></td>
  </tr>
  <tr>
    <td><b>Link Count Cache Expiry</b><br />
      <span class="style1">Specify how often you would like to update category link counts in minutes. To update the counts every time a page is loaded, set this to 0. The higher you set this value, the faster your directory will run. The larger your directory, the more this impacts performance.</span></td>
    <td valign="top"><input name="count_refresh_time" type="text" id="count_refresh_time" size="5" maxlength="5" value="'.htmlentities($sticky['count_refresh_time']).'" />
      minutes</td>
  </tr>
  <tr>
    <td><b>Pagination</b><br />
      <span class="style1">Pagination will organize your links into pages. It is  suggested that you use more categories instead of pages.</span></td>
    <td valign="top"><label>
      <input type="radio" name="use_pagination" value="1" id="use_pagination_0" onclick="hide_div(\'links_per_page_div\',\'use_pagination_1\')" '.radio_selected('use_pagination','1').'/>
Enabled</label>
      <label>
      <input type="radio" name="use_pagination" value="0" id="use_pagination_1" onclick="hide_div(\'links_per_page_div\',\'use_pagination_1\')" '.radio_selected('use_pagination','0').'/>
Disabled<br />
      </label>
<div id="links_per_page_div">
    How many links per page?
    <input name="links_per_page" type="text" id="links_per_page" size="4" maxlength="4" value="'.htmlentities($sticky['links_per_page']).'" />
    </div></td>
  </tr>
  <tr>
    <td><b>Display Latest Links?</b><br />
      <span class="style1">If enabled, this will display the latest links on the main directory page.</span></td>
    <td valign="top"><label>
      <input type="radio" name="disp_latest" value="1" id="disp_latest_0" onclick="hide_div(\'num_latest_links_div\',\'disp_latest_1\')" '.radio_selected('disp_latest','1').'/>
Yes</label>
      <label>
      <input type="radio" name="disp_latest" value="0" id="disp_latest_1" onclick="hide_div(\'num_latest_links_div\',\'disp_latest_1\')" '.radio_selected('disp_latest','0').'/>
No<br />
    </label><div id="num_latest_links_div" >
      How many? 
      <input name="num_latest_links" type="text" id="num_latest_links" size="4" maxlength="4" value="'.htmlentities($sticky['num_latest_links']).'" />
</div></td>
  </tr>
  <tr>
  <td><input type="submit" value="Save Settings" /></td>
  <td>&nbsp;</td>
  </tr>
</table>
<input type="hidden" name="submitted" value="1" />
</form>';

$template = admin_output('config');
print $template;

?>
Return current item: Yet Another Link Directory