<?php 
/*#################################################################*\
|# Licence Number 0DT7-0122-0NW1-0210
|# -------------------------------------------------------------   #|
|# Copyright (c)2010 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
# ################################################################################
*/

require_once 'init.php';

if (!isset($_REQUEST['ID'])) {
	die();
}

if (isset($_REQUEST['mode'])) {
    $mode = $_REQUEST['mode'];
}
$tpl->assign('mode', $mode);
if ($mode != 'edit' && $mode != 'del') {
    if (isset($_SESSION['inplace_pageerror'])) {
        unset($_SESSION['inplace_pageerror']);
    }
} else {
    $tpl->assign('error_message', $_SESSION['inplace_pageerror']);
}

$id = $_REQUEST['ID'];

if ($mode == 'del') {
    //deleting page
    echo '<h4 style="color: #666666;">Deleting page, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $page['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], 0);
        if ($page['rights']['delPage'] == 1) {
             $return = DOC_ROOT;
             
             //delete page
             $data = $db->Execute("DELETE FROM `{$tables['page']['name']}` WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
 echo '<script> window.top.location=window.top.location; </script>';
        }
    }
    
} elseif ($mode == 'active') {
    echo '<h4 style="color: #666666;">Activating page, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $page['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], 0);
        if ($page['rights']['editPage'] == 1) {
             
             $data = $db->Execute("UPDATE `{$tables['page']['name']}` SET `STATUS` = '2' WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
echo '<script> window.top.location = window.top.location; </script>';
        }
    }
    
} elseif ($mode == 'inmenu') {
    echo '<h4 style="color: #666666;">Updating page, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $page['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], 0);
        if ($page['rights']['editPage'] == 1) {
             
             $data = $db->Execute("UPDATE `{$tables['page']['name']}` SET `SHOW_IN_MENU` = '1' WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
echo '<script> window.top.location = window.top.location; </script>';
        }
    }
    
} else {
    //Disable any caching by the browser
    disable_browser_cache();
    
//    //Generate unique imagehash for visual confirmation
//    if (VISUAL_CONFIRM == 1)
//    {
//       require_once 'include/functions_imgverif.php';
//       $imagehash = fetch_captcha_hash();
//       $tpl->assign('imagehash', $imagehash);
//       unset ($imagehash);
//    }
    
    
    $page = getPageByID($id);
	foreach ($page as $key=>$value) {
		$page[$key] = stripslashes($value);
	}				
    $tpl->assign('stats', array (0 => _L('Inactive'), 1 => _L('Pending'), 2 => _L('Active'),));
    $tpl->assign('privacy', array (0 => _L('All Users'), 1 => _L('Registered Users'),));
    
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $page['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], 0);
        
        if ($page['rights']['editPage'] == 1) {
            if (isset($_REQUEST['action']) && $_REQUEST['action']=="submit") {
                
                $name 				= $_REQUEST['NAME'];
                $seo_name 			= $_REQUEST['SEO_NAME'];
                $content 			= $_REQUEST['CONTENT'];
                $status 			= $_REQUEST['STATUS'];
                $privacy 			= $_REQUEST['PRIVACY'];
                $show_in_menu 		= $_REQUEST['SHOW_IN_MENU'];
                $meta_keywords 		= $_REQUEST['META_KEYWORDS'];
                $meta_description 	= $_REQUEST['META_DESCRIPTION'];
                $id = $_REQUEST['ID'];
                
               
                
                //keep edited data somewhere, just in case it's not validated at submit from some reason
                $_SESSION['editpage'] = $_POST;
            	foreach ($_SESSION['editpage'] as $key=>$value) {
					$_SESSION['editpage'][$key] = stripslashes($value);
				}
                if ($name == '') {
                    $error = 1;
                    $_SESSION['inplace_pageerror'] = "Page name cannot be empty.";
                } elseif ($seo_name == '') {
                    $error = 1;
                    $_SESSION['inplace_pageerror'] = "Page SEO name cannot be empty.";
//                } elseif (verify_captcha_hash($_POST['IMAGEHASH'],$_POST['CAPTCHA']) == 0) {
//                    $error = 1;
//                    $_SESSION['inplace_pageerror'] = "Your security code does not match the image.";
                } elseif (!((strlen($name)>=TITLE_MIN_LENGTH) && (strlen($name)<=TITLE_MAX_LENGTH))){
                    $error = 1;
                    $_SESSION['inplace_pageerror'] = "The name must have ".TITLE_MIN_LENGTH." to ".TITLE_MAX_LENGTH." characters.";
                }  elseif ($meta_description != '' && !((strlen($meta_description)>=META_DESCRIPTION_MIN_LENGTH) && (strlen($meta_description)<=META_DESCRIPTION_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_pageerror'] = "The meta description must have ".META_DESCRIPTION_MIN_LENGTH." to ".META_DESCRIPTION_MAX_LENGTH." characters.";
                } elseif ($meta_keywords != '' && !((strlen($meta_keywords)>=META_KEYWORDS_MIN_LENGTH) && (strlen($meta_keywords)<=META_KEYWORDS_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_pageerror'] = "The meta keywords must have ".META_KEYWORDS_MIN_LENGTH." to ".META_KEYWORDS_MAX_LENGTH." characters.";
                } else {
                    if ($db->Execute("UPDATE `{$tables['page']['name']}`
                        SET `NAME` = ".$db->qstr($name).",
                        `SEO_NAME` = ".$db->qstr($seo_name).",
                        `CONTENT` = ".$db->qstr($content).",
                        `STATUS` = ".$db->qstr($status).",
                        `PRIVACY` = ".$db->qstr($privacy).",
                        `SHOW_IN_MENU` = ".$db->qstr($show_in_menu).",
                        `META_KEYWORDS` = ".$db->qstr($meta_keywords).",
                        `META_DESCRIPTION` = ".$db->qstr($meta_description)."
                         WHERE `ID`=".$db->qstr($id)."")) {
//                            http_custom_redirect($return);
//                          if ($status < 2) {
//                                 $return = DOC_ROOT;
//                                 echo '<script> window.top.location="' . $return . '"; </script>';
//                          }  else {
                              echo '<script> window.top.location = window.top.location; </script>';
//                          }
 
                         } else {
                             $error = 1;
                             $_SESSION['inplace_pageerror'] = "Sorry, some database error occured. Please try again.";
                         }
                }
            } else {
                if ($mode == "edit") {
                    $tpl->assign('page', $_SESSION['editpage']);
                    unset($_SESSION['editpage']);
                    unset($_SESSION['inplace_pageerror']);
                } else {
                    $tpl->assign('page', $page);
                }
            }
        } else {
            $error = 1;
            $_SESSION['inplace_pageerror'] = "Sorry, you have no editing rights for this page.";
        }
    } else {
        $error = 1;
        $_SESSION['inplace_pageerror'] = "Sorry, you have no editing rights for this page.";
    }
    $tpl->assign('error', $error);
    
    if ($error == 1) {
//        http_custom_redirect($return);
echo '<script> window.top.location = window.top.location; </script>';
    }
    
    $useRTE = (defined ('LINK_SUBMIT_USE_RTE') && LINK_SUBMIT_USE_RTE == 1 ? 1 : 0);
    $tpl->assign('useRTE', $useRTE);
    echo $tpl->fetch('inplace_editpage.tpl');
}
?>
