<?php 
/*#################################################################*\
|# Licence Number 0D4K-1018-0DWO-0209
|# -------------------------------------------------------------   #|
|# Copyright (c)2009 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
# ################################################################################
# Project:   PHP Link Directory
#
# **********************************************************************
# Copyright (C) 2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
#
# This software is for use only to those who have purchased a license.
# A license must be purchased for EACH installation of the software.
#
# By using the software you agree to the terms:
#
#    - You may not redistribute, sell or otherwise share this software
#      in whole or in part without the consent of the the ownership
#      of PHP Link Directory. Please contact david@david-duval.com
#      if you need more information.
#
#    - You agree to retain a link back to http://www.phplinkdirectory.com/
#      on all pages of your directory in you purchased the $25 version
#      of the software.
#
# License holders are entitled to upgrades to the 3.4 branch of the software
# as they are made available at http://www.phplinkdirectory.com/
#
# In some cases, license holders may be required to agree to changes
# in the software license before receiving updates to the software.
# **********************************************************************
#
# For questions, help, comments, discussion, etc., please join the
# PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
#
# @link           http://www.phplinkdirectory.com/
# @copyright      2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
# @projectManager David DuVal <david@david-duval.com>
# @package        PHPLinkDirectory
# @version        4.0.0
# ################################################################################
*/

//register_globals turned ON is a major security hole
//we'll unset ALL variables requested via URL
//if (ini_get ('register_globals'))
//{
//   //Get request variables
//   $getRequest = array_keys ($_REQUEST);
//   //Loop through each variable
//   foreach ($getRequest as $var)
//   {
//      //Test if variable was declared
//      if ($_REQUEST[$var] === $$var)
//      {
//         //Unset variable
//         unset($$var);
//      }
//   }
//   unset ($getRequest);
//}

//register_globals turned ON is a major security hole
//we'll unset ALL variables requested via URL
$register_globals = trim(ini_get('register_globals'));
 if (!empty($register_globals) && strtolower($register_globals) != 'off') {
   //Get request variables
   $getRequest = array_keys ($_REQUEST);
   //Loop through each variable
   foreach ($getRequest as $var)
   {
      //Test if variable was declared
      if ($_REQUEST[$var] === $var)
      {
         //Set value of the variable to NULL,
         //just in case unset does not work
         $var = null;

         //Unset variable
         unset($var);
      }
   }
   unset ($getRequest);
}

//if (!get_magic_quotes_gpc()){ 
// $_GET = array_map('addslashes', $_GET); 
// $_POST = array_map('addslashes', $_POST); 
// $_COOKIE = array_map('addslashes', $_COOKIE); 
// $_REQUEST = array_map('addslashes', $_REQUEST); 
//}

function custom_stripslashes($arr) {
	foreach ($arr as $k=>$v) {
		if (is_array($v)) {
			$arr[$k] = custom_stripslashes($v);
		} else {
		    $arr[$k] = stripslashes($v);	
		}
	}
	return $arr;
}

if (get_magic_quotes_gpc()){ 
 $_GET = custom_stripslashes($_GET); 
 $_POST = custom_stripslashes($_POST); 
 $_COOKIE = custom_stripslashes($_COOKIE); 
 $_REQUEST = custom_stripslashes($_REQUEST); 
}

/**
 * Define some application wide constants to increase security.
 * By checking them in included files, further execution can be blocked for unauthorized access.
 */
define ('IN_PHPLD'      , true); //For all files
define ('IN_PHPLD_ADMIN', true); //Only for admin files


//Detect web-server software
define ('IS_APACHE', ( strstr ($_SERVER['SERVER_SOFTWARE'], 'Apache') || strstr ($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') ) ? 1 : 0);
define ('IS_IIS'   , strstr ($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') ? 1 : 0);

require_once '../include/version.php';
require_once '../include/config.php';
require_once '../include/client_info.php';
require_once 'include/settings.php';
require_once 'include/tables.php';
require_once 'include/functions.php';
require_once 'include/functions_validate.php';
require_once 'include/dirdb.php';
require_once 'libs/intsmarty/intsmarty.class.php';
require_once 'libs/smarty/SmartyPaginate.class.php';
require_once 'libs/smarty/SmartyValidate.class.php';
require_once 'libs/adodb/adodb.inc.php';
require_once 'admin/dirdb_admin.php';



tweak_time_limit(0);

session_start();

//Define full path on server
define ('SERVER_DOC_ROOT', dirname (__file__));//example:/var/www/html/admin
define ('SERVER_DIRECTORY_ROOT', dirname (dirname (__file__)));//example:/var/www/html

//Connect to database
$db = ADONewConnection(DB_DRIVER);
if ($db->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME))
{
   $db->SetFetchMode(ADODB_FETCH_ASSOC);
   
 //  $setCharset = $db->Execute ("SET NAMES 'utf8'");
  // $setCharset = $db->Execute ("SET CHARACTER SET utf8");
   
   $phpldSettings = read_config($db);
}
else
{
   define ('ERROR', 'ERROR_DB_CONNECT');
   exit ('ERROR :: Could not connect to database server!');
}

if (DEBUG === 1)
{
   set_log('admin_log.txt');
}
//Define current time
if (defined ('SERVER_OFFSET_TIME'))
{
   //Calculate with offset time
   define ('TIMENOW', time() + (SERVER_OFFSET_TIME * 60 * 60));
}
else
{
   //Offset time was not defined, use current time
   define ('TIMENOW', time());
}
$phpldSettings['TIMENOW'] = TIMENOW;
//Add custom database library extender
require_once 'include/adodb_extender.php';
//Path to cache directory
//You might want to set this outside your document root
$db_cache_dir     = SERVER_DIRECTORY_ROOT . '/temp/adodb/';
$db_cache_timeout = (defined ('DB_CACHE_TIMEOUT') ? DB_CACHE_TIMEOUT : 3600);

if (DB_CACHING == '0')
{
	 $db->CacheFlush();
}

//do NOT use DB caching if "register_globals" is ON
if (DB_CACHING && !ini_get('register_globals'))
{
   if (is_dir ($db_cache_dir) && is_writeable ($db_cache_dir))
   {
      //Define database cache directory
      $ADODB_CACHE_DIR = $db_cache_dir;

      //Define cache timeout
      $db->cacheSecs = (int)$db_cache_timeout;
   }
}
//Define session ID
define ('PLD_SESSION_ID', session_id());
//Send character set header
@ header ('Content-type: text/html; charset='.(defined ('CHARSET') ? CHARSET : 'utf-8'));
//Load input filter
require_once 'libs/inputfilter/class.inputfilter_php4.php';

define ('DOC_ROOT', substr ($_SERVER["SCRIPT_NAME"], 0, strrpos ($_SERVER["SCRIPT_NAME"], '/')));

//Determine core templates folder
$core_tpl = (defined ('CORE_TEMPLATES') && CORE_TEMPLATES != '' ? CORE_TEMPLATES : 'Core');
$tpl = get_tpl($core_tpl);
$tpl->assign('date_format', '%D %H:%M:%S');


//widget related//
require_once 'widgets/Widget.php';

$wd = new Widget();

$doc_root = DOC_ROOT;
$tpl->assign('doc_root', $doc_root);

$tpl->assign('requestUri', $_SERVER['REQUEST_URI']);

$directory_root = preg_replace ('`[\/]?admin[\/]?$`', '', DOC_ROOT);
define ('DIRECTORY_ROOT', $directory_root);

define ('TEMPLATE_ROOT', DIRECTORY_ROOT."/templates/Core/".ADMIN_TEMPLATE);

//Check if admin or editor is accessing the page
//Else redirect to login page
if (empty ($_SESSION['phpld']['adminpanel']['id']))
{
   $f = $_SERVER['SCRIPT_NAME'];
   if (($p = strrpos ($f, '/')) !== false)
      $f = substr ($f, $p + 1);

   if ($f != 'login.php')
   {
      if (empty ($_SESSION['return']))
         $_SESSION['return'] = request_uri();

      http_custom_redirect('login.php');
   }
}

//Determine category
$CategoryID = (!empty($_REQUEST['c']) && preg_match ('`^[\d]+$`', $_REQUEST['c']) ? intval ($_REQUEST['c']) :
               (!empty ($_SERVER['HTTP_REFERER']) ? get_category($_SERVER['HTTP_REFERER']) : 0));
$CategoryID = ($CategoryID > 0 ? $CategoryID : 0); //Make sure the category ID is valid
$CategoryTitle = getCategoryTitleByID($CategoryID);
$tpl->assign('CategoryTitle', $CategoryTitle);

//Determine if admin and ajax categories (editors have regular category list)
if ($_SESSION['phpld']['adminpanel']['is_admin'] == 1 && ADMIN_CAT_SELECTION_METHOD == 1)
   define ('AJAX_CAT_SELECTION_METHOD', 1);
else
   define ('AJAX_CAT_SELECTION_METHOD', 0);

require_once 'include/constants.php';

//Valid status values for links, categories and articles
$validLinkStatus     = array (0, 1, 2);
$validCategStatus    = array (0, 1, 2);
$validArticleStatus  = array (0, 1, 2);

$featured_where      = "(`FEATURED` = 1 AND (`EXPIRY_DATE` > ".$db->DBTimeStamp(time())." OR `EXPIRY_DATE` IS NULL))";
$featured_where_join = "({$tables['link']['name']}.FEATURED = '1' AND ({$tables['link']['name']}.EXPIRY_DATE > ".$db->DBTimeStamp(time())." OR {$tables['link']['name']}.EXPIRY_DATE IS NULL))";

//need next one for admin link listing, where both regular and featured are now listed together
$expired_where_join = "NOT({$tables['link']['name']}.EXPIRY_DATE > ".$db->DBTimeStamp(time())." OR {$tables['link']['name']}.EXPIRY_DATE IS NULL)";


//Check if featured, reviewed links, reviewed articles and/or payments are available for menu link
$featLinksItem = $db->GetOne("SELECT COUNT(*) FROM `{$tables['link']['name']}` WHERE `FEATURED` = '1'");
$reviewedLinksItem = $db->GetOne("SELECT COUNT(*) FROM `{$tables['link_review']['name']}`");
$paymentItem = $db->GetOne("SELECT COUNT(*) FROM `{$tables['payment']['name']}`");
$reviewedArticlesItem = $db->GetOne("SELECT COUNT(*) FROM `{$tables['article_review']['name']}`");

if ($_SESSION['phpld']['adminpanel']['is_admin'])
{
   $menu = array (
      'index' => _L('Home'),
      'cat' => array (
            'label' => _L('Categories'),
            'menu'  => array (
               'categs'          => array ('label' => _L('Categories')        , 'url' => DOC_ROOT.'/dir_categs.php'),           
               'approve_categs'  => array ('label' => _L('Approve Categories'), 'url' => DOC_ROOT.'/dir_approve_categs.php'),
				   
               )
            ),
'lnks' => array (
            'label' => _L('Links'),
            'menu'  => array (
               'links'           => array ('label' => _L('Links')             , 'url' => DOC_ROOT.'/dir_links.php'),
              // 'ftr_links'       => array ('label' => _L('Featured Links')    , 'url' => DOC_ROOT.'/dir_links.php?f=1', 'disabled' => (FTR_ENABLE == 1 || $featLinksItem > 0 ? 0 : 1)),
               'approve_links'   => array ('label' => _L('Approve Links')     , 'url' => DOC_ROOT.'/dir_approve_links.php'),
               'reviewed_links'  => array ('label' => _L('Reviewed Links')    , 'url' => DOC_ROOT.'/dir_reviewed_links.php', 'disabled' => ($reviewedLinksItem > 0 ? 0 : 1)),
               'validate'        => array ('label' => _L('Validate Links')    , 'url' => DOC_ROOT.'/dir_validate.php'),		
				 'link_comments'             => array ('label' => _L('Link Comments')        , 'url' => DOC_ROOT.'/dir_link_comments.php'),
                'approve_link_comments'     => array ('label' => _L('Approve Link Comments'), 'url' => DOC_ROOT.'/dir_approve_link_comments.php'),
               )
            ),
				   
      'art' => array (
            'label' => _L('Articles'),
            'menu'  => array (
               'listarticle'       => array ('label' => _L('Articles')        , 'url' => DOC_ROOT.'/article_list.php'),
	   			//'featarticle'       => array ('label' => _L('Featured Articles')        , 'url' => DOC_ROOT.'/article_list.php?f=1', 'disabled' => 1),
               'newarticle'        => array ('label' => _L('New Article')          , 'url' => DOC_ROOT.'/article_edit.php?action=N'),
               'approve_article'   => array ('label' => _L('Approve Articles')     , 'url' => DOC_ROOT.'/article_approve.php'),
               'reviewed_article'  => array ('label' => _L('Reviewed Articles')    , 'url' => DOC_ROOT.'/article_reviewed.php', 'disabled' => (REQUIRE_REGISTERED_USER_ARTICLE == 1 && $reviewedArticlesItem > 0 ? 0 : 1)),
'artimporter'       => array ('label' => _L('Article Importer')     , 'url' => DOC_ROOT.'/artimporter.php')      ,
               
              
				   'article_comments'          => array ('label' => _L('Article Comments')        , 'url' => DOC_ROOT.'/dir_comments.php'),
                'approve_article_comments'  => array ('label' => _L('Approve Article Comments'), 'url' => DOC_ROOT.'/dir_approve_comments.php'),
               )
      ),
        
       'widgets' => array (
            'label' => _L('Widgets'),
            'menu'  => array (
               'list'          => array ('label' => _L('Available Widgets')        , 'url' => DOC_ROOT.'/dir_widgets.php'),
               'list_zones'          => array ('label' => _L('Zones')        , 'url' => DOC_ROOT.'/dir_widget_zones.php'),
       		   'list_howto'          => array ('label' => _L('How To Use Widgets')        , 'url' => DOC_ROOT.'/dir_widget_howto.php')
               )
            ),
	    'pages' => array (
            'label' => _L('Pages'),
            'menu'  => array (
                  array ('label' => _L('New Page')                       , 'url' => DOC_ROOT.'/dir_pages_edit.php?action=N'),
                  array ('label' => _L('List Pages')                     , 'url' => DOC_ROOT.'/dir_pages.php'),
               ),
            ),
        'users' => array(
         	'label' => _L('User management'),
         	'menu' => array(
               'users'          => array ('label' => _L('Edit Users')             , 'url' => DOC_ROOT.'/conf_users.php')       ,
         	)
         ),
    
      'conf' => array (
            'label' => _L('System'),
            'menu'  => array (
               'message'        => array ('label' => _L('Edit Email Templates')   , 'url' => DOC_ROOT.'/email_message.php')    ,
               'templates'      => array ('label' => _L('Template Manager')       , 'url' => DOC_ROOT.'/conf_templates.php')   ,
            // 'admintemplates' => array ('label' => _L('Admin Template Manager') , 'url' => DOC_ROOT.'/conf_admin_templates.php')   ,
               'language'       => array ('label' => _L('Language Editor')        , 'url' => DOC_ROOT.'/conf_language.php')    ,
               'spider'         => array ('label' => _L('Spider')                 , 'url' => DOC_ROOT.'/spider.php')           ,
               'payment'        => array ('label' => _L('Link Payments')               , 'url' => DOC_ROOT.'/conf_payment.php', 'disabled' => (PAY_ENABLE == 1 || $paymentItem > 0 ? 0 : 1)),
			'article_payment'   => array ('label' => _L('Article Payments')             , 'url' => DOC_ROOT.'/article_payment.php', 'disabled' => (ARTICLE_PAY_ENABLE == 1 ? 0 : 1)),
               'profile'        => array ('label' => _L('Profile')                , 'url' => DOC_ROOT.'/conf_profile.php')     ,
               'sitemap'        => array ('label' => _L('Sitemap')                , 'url' => DOC_ROOT.'/conf_sitemap.php')     ,
               'bancontrol'     => array ('label' => _L('Ban Control')            , 'url' => DOC_ROOT.'/conf_bancontrol.php')  ,
               'database'       => array ('label' => _L('Database')               , 'url' => DOC_ROOT.'/conf_database.php')    ,
               'maintenance'    => array ('label' => _L('Maintenance')            , 'url' => DOC_ROOT.'/conf_maintenance.php') ,
			   'cache'               => array ('label' => _L('Clean Cache Now')   , 'url' => DOC_ROOT.'/cache_clean.php','disabled' => (DB_CACHING == 1 ? 0 : 1)),
             'cachesettings'    =>  array ('label' => _L('Cache Settings')        , 'url' => DOC_ROOT.'/conf_settings.php?c=15'),     
               )
            ),
	  
      'settings' => array (
            'label' => _L('Settings'),
            'menu'  => array (
                  array ('label' => _L('Site')                       , 'url' => DOC_ROOT.'/conf_settings.php?c=1'),
                  array ('label' => _L('Directory')                  , 'url' => DOC_ROOT.'/conf_settings.php?c=2'),
				  array ('label' => _L('Admin Area')                 , 'url' => DOC_ROOT.'/conf_settings.php?c=7'),
                  array ('label' => _L('Link Submit')                , 'url' => DOC_ROOT.'/conf_settings.php?c=3'),
				  array ('label' => _L('Link Payment Settings')                    , 'url' => DOC_ROOT.'/conf_settings.php?c=9'),
				  array ('label' => _L('Featured Links')             , 'url' => DOC_ROOT.'/conf_settings.php?c=8'),
				  array ('label' => _L('Reciprocal Link Setup')      , 'url' => DOC_ROOT.'/conf_settings.php?c=12'),
				  array ('label' => _L('Link Comments')             , 'url' => DOC_ROOT.'/conf_settings.php?c=20'),
                  array ('label' => _L('Link Ratings')            , 'url' => DOC_ROOT.'/conf_settings.php?c=21'),
                  array ('label' => _L('Article Submit')             , 'url' => DOC_ROOT.'/conf_settings.php?c=14'),
		          array ('label' => _L('Article Payment Settings')     , 'url' => DOC_ROOT.'/conf_settings.php?c=19'),
                  array ('label' => _L('Article Comments')           , 'url' => DOC_ROOT.'/conf_settings.php?c=16'),
                  array ('label' => _L('Article Ratings')            , 'url' => DOC_ROOT.'/conf_settings.php?c=17'),
                  array ('label' => _L('Author Info')                , 'url' => DOC_ROOT.'/conf_settings.php?c=18'),        
                  array ('label' => _L('Email Config')                    , 'url' => DOC_ROOT.'/conf_settings.php?c=5'),
                  array ('label' => _L('Email Template Setup')              , 'url' => DOC_ROOT.'/conf_settings.php?c=6'), 
				  array ('label' => _L('Search Engine Optimization') , 'url' => DOC_ROOT.'/conf_settings.php?c=4'),
                  array ('label' => _L('Image Verification')         , 'url' => DOC_ROOT.'/conf_settings.php?c=10'),
                  array ('label' => _L('Form Fields')                , 'url' => DOC_ROOT.'/conf_settings.php?c=11'),                
                  array ('label' => _L('Security')                   , 'url' => DOC_ROOT.'/conf_settings.php?c=13'),
	       
                  //array('label' => _L('Paypal Integration')          , 'url' => DOC_ROOT.'/conf_settings.php?c=15'),
               ),
            ),
      'email' => array (
            'label' => _L('Emailer'),
            'menu'  => array (
               'send'               => array ('label' => _L('Send Email')             , 'url' => DOC_ROOT.'/email_send.php'),
               'send_and_add_link'  => array ('label' => _L('Send Email and Add Link'), 'url' => DOC_ROOT.'/email_send_and_add_link.php'),
               'sent_view'          => array ('label' => _L('Sent Emails Report')     , 'url' => DOC_ROOT.'/email_sent_view.php'),
               //'import'             => array ('label' => _L('Import Email Messages')  , 'url' => DOC_ROOT.'/email_import.php'),
               'export'             => array ('label' => _L('Export Email Messages')  , 'url' => DOC_ROOT.'/email_export.php'),
               )
            ),
	
      'logout' => _L('Logout')
   );
}
else
{
      $menu = array (
      'index' => _L('Home'));
	  if ($_SESSION['phpld']['adminpanel']['rights']['addLink'] == 1) { 
	    $menu ['cat'] = array (
            'label' => _L('Categories'),
            'menu'  => array (
               'categs'          => array ('label' => _L('Categories')        , 'url' => DOC_ROOT.'/dir_categs.php'),           
               'approve_categs'  => array ('label' => _L('Approve Categories'), 'url' => DOC_ROOT.'/dir_approve_categs.php'),
				   
               )
            );
$menu['lnks'] = array (
            'label' => _L('Links'),
            'menu'  => array (
               'links'           => array ('label' => _L('Links')             , 'url' => DOC_ROOT.'/dir_links.php'),
              // 'ftr_links'       => array ('label' => _L('Featured Links')    , 'url' => DOC_ROOT.'/dir_links.php?f=1', 'disabled' => (FTR_ENABLE == 1 || $featLinksItem > 0 ? 0 : 1)),
               'approve_links'   => array ('label' => _L('Approve Links')     , 'url' => DOC_ROOT.'/dir_approve_links.php'),
               'reviewed_links'  => array ('label' => _L('Reviewed Links')    , 'url' => DOC_ROOT.'/dir_reviewed_links.php', 'disabled' => ($reviewedLinksItem > 0 ? 0 : 1)),
               'validate'        => array ('label' => _L('Validate Links')    , 'url' => DOC_ROOT.'/dir_validate.php'),				   
               )
          
	               );
	  } else {
	  		 $menu ['cat'] = array (
            'label' => _L('Categories'),
            'menu'  => array (
               'categs'          => array ('label' => _L('Categories')        , 'url' => DOC_ROOT.'/dir_categs.php'),           
               'approve_categs'  => array ('label' => _L('Approve Categories'), 'url' => DOC_ROOT.'/dir_approve_categs.php'),
				   
               )
            );
$menu['lnks'] = array (
            'label' => _L('Links'),
            'menu'  => array (
               'links'           => array ('label' => _L('Links')             , 'url' => DOC_ROOT.'/dir_links.php'),
             		   
               )
          
	               );
	  }
      if ($_SESSION['phpld']['adminpanel']['rights']['addArt'] == 1) {         
	      $menu['art'] = array (
	            'label' => _L('Articles'),
	            'menu'  => array (
	               'listarticle'  => array ('label' => _L('List Articles')  , 'url' => DOC_ROOT.'/article_list.php'),
	               'newarticle'   => array ('label' => _L('New Article')    , 'url' => DOC_ROOT.'/article_edit.php?action=N'),
	               )
	      );
      } else {
      	 $menu['art'] = array (
	            'label' => _L('Articles'),
	            'menu'  => array (
	               'listarticle'  => array ('label' => _L('List Articles')  , 'url' => DOC_ROOT.'/article_list.php'),
	               )
	      );
      }
      if ($_SESSION['phpld']['adminpanel']['rights']['addPage'] == 1) {  
	      $menu['pages'] = array (
	            'label' => _L('Pages'),
	            'menu'  => array (
	                array ('label' => _L('List Pages')                     , 'url' => DOC_ROOT.'/dir_pages.php'),  
	      			array ('label' => _L('New Page')                       , 'url' => DOC_ROOT.'/dir_pages_edit.php?action=N'),
	                  
	               ),
	            );
      } else {
      	$menu['pages'] = array (
	            'label' => _L('Pages'),
	            'menu'  => array (
	                array ('label' => _L('List Pages')                     , 'url' => DOC_ROOT.'/dir_pages.php'),  
	               ),
	            );
      }
      $menu['conf'] = array (
               'label'   => _L('System'),
               'menu'    => array (
                         'profile'      => array ('label' => _L('Profile')             , 'url' => DOC_ROOT.'/conf_profile.php')     ,
                  )
               );
      $menu['logout'] = _L('Logout');
}

$conf_categs = array (
      '1'  => _L('Site')                      ,
      '2'  => _L('Categories')                 ,
      '3'  => _L('Link Submit')               ,
      '4'  => _L('Search Engine Optimization'),
      '5'  => _L('Emailer')                   ,
      '6'  => _L('Notifications')             ,
      '7'  => _L('Admin Area')                ,
      '8'  => _L('Featured Links')            ,
      '9'  => _L('Payment')                   ,
      '10' => _L('Image Verification')        ,
      '11' => _L('Form Fields').' (<em>'._L('Use with caution').'</em>)',
      '12' => _L('Reciprocal Link Setup')     ,
      '13' => _L('Security')                  ,
	  '14' => _L('Article Submit')                  ,
	  '15' => _L('Caching')                   ,
	  '16' => _L('Article Comments')                  ,
      '17' => _L('Article Ratings')                  ,
	  '18' => _L('Author Info')                  ,
      '20' => _L('Link Comments')                  ,
      '21' => _L('Link Ratings')                  ,
      //'15' => _L('Paypal Integration')        ,
);
$tpl->assign('menu', $menu);
$menu_elements = count ($menu);
$tpl->assign('menu_elements', $menu_elements);

$user_details = $db->GetRow("SELECT `ID`, `LOGIN`, `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($_SESSION['phpld']['adminpanel']['id'])." AND `LEVEL` IN ('1', '2') AND `ACTIVE` = '1'");
$tpl->assign('user_details', $user_details);

//Define current version again (backwards compatibility)
$tpl->assign('VERSION', CURRENT_VERSION);

//Array usually for radio buttons with answers yes/no
$tpl->assign('yes_no', array (1 => _L('Yes'), 0 => _L('No')));

$f = $_SERVER['SCRIPT_NAME'];
if (($ptmp = strrpos ($f, '/')) !== false)
   $f = substr ($f, $ptmp + 1);

$current_script = $f;
if(($ptmp = strrpos ($f, '.')) !== false)
   $f = substr ($f, 0, $ptmp);

define ('SCRIPT_NAME', $f);

$ptmp = split ('_', $f, 2);
if (count ($ptmp) > 1)
   $t = $menu[$p[0]]['menu'][$ptmp[1]];
else
   $t = $menu[$ptmp[0]];

if ($t != 'Home')
   $tpl->assign('title', $t);

//Determine current paging item
$current_item = (!empty ($_REQUEST['p']) && preg_match ('`^[\d]+$`', $_REQUEST['p']) ? intval ($_REQUEST['p']) : 1);

//Remove paging from request URI
$_SERVER['REQUEST_URI'] = preg_replace ('`([?]|[&])(p=)+[\d]*`', '', request_uri());

$resetSort = 0;
//if r=1 is passed via URL, all sorting is reset
if (isset ($_REQUEST['r']) && $_REQUEST['r'] == 1)
{
   $resetSort = 0;
}

//Define sort fields
if (isset ($_REQUEST['sort']) && !empty ($_REQUEST['sort']) && $_REQUEST['r'] != 1)
{
   define ('SORT_FIELD', $_REQUEST['sort']);
   $tpl->assign('SORT_FIELD', SORT_FIELD);
}
else
{
   define ('SORT_FIELD', 'ID');
   $tpl->assign('SORT_FIELD', SORT_FIELD);
}

//Define sort order
if (isset ($_REQUEST['sort_order']) && !empty ($_REQUEST['sort_order']))
{
   define ('SORT_ORDER', $_REQUEST['sort_order']);
   $tpl->assign('SORT_ORDER', SORT_ORDER);
   define ('REVERSE_SORT_ORDER', SORT_ORDER == 'ASC' ? 'DESC' : 'ASC');
   $tpl->assign('REVERSE_SORT_ORDER', REVERSE_SORT_ORDER);

   $tpl->assign('requestOrder', 1);
}
else
{
   define ('SORT_ORDER', 'DESC');
   $tpl->assign('SORT_ORDER', SORT_ORDER);
   define ('REVERSE_SORT_ORDER', 'ASC');
   $tpl->assign('REVERSE_SORT_ORDER', REVERSE_SORT_ORDER);

   $tpl->assign('requestOrder', 0);
}


//Disallow access to the page if it's not allowed for editors
if (!$_SESSION['phpld']['adminpanel']['is_admin'])
{
   //List of pages editors are allowed to view
   $editorAllowed = array ('login.php',
                           'index.php',
                           'dir_categs.php',
                           'dir_categs_edit.php',
                           'dir_links.php',
   						   'dir_pages.php',
   						   'dir_pages_edit.php',
                           'dir_reviewed_links.php',
                           'dir_review_links_edit.php',
                           'dir_links_edit.php',
                           'dir_approve_links.php',
                           'conf_profile.php',
                           'link_details.php',
                           'categ_details.php',
                           'categ_link_options.php',
                           'article_list.php',
                           'article_edit.php',
                           'article_details.php',
                           'unauthorized.php'
                     );
   if (!in_array ($current_script, $editorAllowed))
   {
      //Editor is on unallowed page, block access
      http_custom_redirect("unauthorized.php");
      exit();
   }
}

//Check if using RTE (Rich Text Editor)
$useRTE = (defined ('ADMIN_USE_RTE') && ADMIN_USE_RTE == 1 ? 1 : 0);
$tpl->assign('useRTE', $useRTE);
$tpl->assign("rights", $_SESSION['phpld']['adminpanel']['rights']);
?>