<?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') {
    if (isset($_SESSION['inplace_linkerror'])) {
        unset($_SESSION['inplace_linkerror']);
    }
} else {
    $tpl->assign('error_message', $_SESSION['inplace_linkerror']);
}

$id = $_REQUEST['ID'];
$link = getLinkByID($id);
foreach ($link as $key=>$value) {
	$link[$key] = stripslashes($value);
}

$categs = get_categs_tree_frontend_forlinks(0);
$tpl->assign('categs', $categs);

if ($mode == 'del') {
    //deleting link
    echo '<h4 style="color: #666666;">Deleting link, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $link['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $link['CATEGORY_ID']);
        if ($link['rights']['delLink'] == 1) {
             //get category parent first
             $data = $db->GetOne("SELECT `CACHE_URL` FROM `{$tables['category']['name']}`
                                    WHERE `ID`=(SELECT `CATEGORY_ID` FROM `{$tables['link']['name']}`
                                    WHERE `ID`=".$db->qstr($id).")");
             
             $return = DOC_ROOT."/".$data;
             
             //delete links
             $data = $db->Execute("DELETE FROM `{$tables['link']['name']}` WHERE `ID`=".$db->qstr($id)."");
             $data = $db->Execute("DELETE FROM `{$tables['link_review']['name']}` WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
        	if (isset($_REQUEST['detail']) && ($_REQUEST['detail'] == '1')) {
            	echo '<script> window.top.location="' . $return . '"; </script>';
            } else {
             	echo '<script> window.top.location = window.top.location; </script>';
            }
        }
    }
    
} elseif ($mode == 'active') {
    echo '<h4 style="color: #666666;">Activating link, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $link['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $link['CATEGORY_ID']);
        if ($link['rights']['editLink'] == 1) {
             $data = $db->Execute("UPDATE `{$tables['link']['name']}` SET `STATUS` = '2' WHERE `ID`=".$db->qstr($id)."");
             $data = $db->Execute("UPDATE `{$tables['link_review']['name']}` SET `STATUS` = '2' WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
echo '<script> window.top.location = window.top.location; </script>';
        } else {
//             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);
//    }
    
    

    $tpl->assign('stats', array (0 => _L('Inactive'), 1 => _L('Pending'), 2 => _L('Active'),));
    
    // Get list of registered users
    $ActiveUsersList = $db->GetAssoc("SELECT `ID`, CONCAT(`LOGIN`, '  (', `NAME`, ' / ', `EMAIL`, ')') AS `USER` FROM `{$tables['user']['name']}` WHERE `ACTIVE` = '1'");
    $ActiveUsersList[0] = _L('Select username');
    ksort ($ActiveUsersList);
    $tpl->assign('ActiveUsersList', $ActiveUsersList);
    
    
    if (isset($_REQUEST['ID']) & isset($_SESSION['phpld']['user']['id'])) {
    
        $link['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $link['CATEGORY_ID']);
    
        if ($link['rights']['editLink'] == 1) {
            if (isset($_REQUEST['action']) && $_REQUEST['action']=="submit") {
                
                $title 				= $_REQUEST['TITLE'];
                $description 		= $_REQUEST['DESCRIPTION'];
                $url 				= $_REQUEST['URL'];
                $owner_id 			= $_REQUEST['OWNER_ID'];
                $owner_name 		= $_REQUEST['OWNER_NAME'];
                $owner_email 		= $_REQUEST['OWNER_EMAIL'];
                $featured 			= $_REQUEST['FEATURED'];
                $nofollow 			= $_REQUEST['NOFOLLOW'];
                $recpr_required 	= $_REQUEST['RECPR_REQUIRED'];
                $recpr_url 			= $_REQUEST['RECPR_URL'];
                $status 			= $_REQUEST['STATUS'];
                $pagerank 			= $_REQUEST['PAGERANK'];
                $hits 				= $_REQUEST['HITS'];
                $category_id 		= $_REQUEST['CATEGORY_ID'];
                $meta_description 	= $_REQUEST['META_DESCRIPTION'];
                $meta_keywords 		= $_REQUEST['META_KEYWORDS'];
                
                
                $_SESSION['editlink'] = $_POST;
				foreach ($_SESSION['editlink'] as $key=>$value) {
					$_SESSION['editlink'][$key] = stripslashes($value);
				}
                
                $data = $db->GetOne("SELECT COUNT(*) AS c FROM `{$tables['link']['name']}`
                                    WHERE `CATEGORY_ID`=".$db->qstr($category_id)."
                                        AND `TITLE`=".$db->qstr($title)."
                                        AND `ID`!=".$db->qstr($id)."");
               if ($data['c']>0) {
                    $error = 1;
                    $_SESSION['inplace_linkerror'] = "Title is not unique in the parent category. Information not updated.";
               } elseif ($title == '') {
                    $error = 1;
                    $_SESSION['inplace_linkerror'] = "Title cannot be empty.";
               } elseif (!((strlen($title)>=TITLE_MIN_LENGTH) && (strlen($title)<=TITLE_MAX_LENGTH))){
                    $error = 1;
                    $_SESSION['inplace_linkerror'] = "The title must have ".TITLE_MIN_LENGTH." to ".TITLE_MAX_LENGTH." characters.";
               } elseif (!((strlen($description)>= DESCRIPTION_MIN_LENGTH) && (strlen($description)<= DESCRIPTION_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_linkerror'] = "The description must have ".DESCRIPTION_MIN_LENGTH." to ".DESCRIPTION_MAX_LENGTH." characters.";
               }  elseif (REQUIRE_REGISTERED_USER == 1 && $owner_id == '0') {
                    $error = 1;
                    $_SESSION['inplace_linkerror'] = "Please select a link owner.";
               } elseif (is_banned_email($owner_email)) {
                    $error = 1;
                    $_SESSION['inplace_linkerror'] = "The owner email is banned.";
               } elseif ($meta_description != '' && !((strlen($meta_description)>=META_DESCRIPTION_MIN_LENGTH) && (strlen($meta_description)<=META_DESCRIPTION_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_linkerror'] = "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_linkerror'] = "The meta keywords must have ".META_KEYWORDS_MIN_LENGTH." to ".META_KEYWORDS_MAX_LENGTH." characters.";
//               } elseif (verify_captcha_hash($_POST['IMAGEHASH'],$_POST['CAPTCHA']) == 0) {
//                    $error = 1;
//                    $_SESSION['inplace_linkerror'] = "Your security code does not match the image.";
               }  else {
                   
                    if ($db->Execute("UPDATE `{$tables['link']['name']}`
                        SET `TITLE` = ".$db->qstr($title).",
                            `DESCRIPTION` = ".$db->qstr($description).",
                            `URL` = ".$db->qstr($url).",
                            `OWNER_ID` = ".$db->qstr($owner_id).",
                            `OWNER_NAME` = ".$db->qstr($owner_name).",
                            `OWNER_EMAIL` = ".$db->qstr($owner_email).",
                            `FEATURED` = ".$db->qstr($featured).",
                            `NOFOLLOW` = ".$db->qstr($nofollow).",
                            `RECPR_REQUIRED` = ".$db->qstr($recpr_required).",
                            `RECPR_URL` = ".$db->qstr($recpr_url).",
                            `STATUS` = ".$db->qstr($status).",
                            `PAGERANK` = ".$db->qstr($pagerank).",
                            `HITS` = ".$db->qstr($hits).",
                            `CATEGORY_ID` = ".$db->qstr($category_id).",
                            `META_DESCRIPTION` = ".$db->qstr($meta_description).",
                            `META_KEYWORDS` = ".$db->qstr($meta_keywords)."
                             WHERE `ID`=".$db->qstr($id)."")) {
//                        http_custom_redirect($return);

//                     if ($status < 2) {
//                    //get category parent first
//                     $data = $db->GetOne("SELECT `CACHE_URL` FROM `{$tables['category']['name']}`
//                                            WHERE `ID`= '".$category_id."'");
//
//                     $return = DOC_ROOT."/".$data;
//                     echo '<script> window.top.location="' . $return . '"; </script>';
//                    } else {
                        echo '<script> window.top.location = window.top.location; </script>';
//                    }
                    } else {
                         $error = 1;
                         $_SESSION['inplace_linkerror'] = "Sorry, some database error occured. Please try again.";
                    }
               }
           } else {
               if ($mode == "edit") {
                   $tpl->assign('link', $_SESSION['editlink']);
                   unset($_SESSION['editlink']);
                   unset($_SESSION['inplace_linkerror']);
                } else {
                   $tpl->assign('link', $link);
                }
            }
        } else {
            $error = 1;
            $_SESSION['inplace_linkerror'] = "Sorry, you have no editing rights for this link.";
        }
    } else {
        $error = 1;
        $_SESSION['inplace_linkerror'] = "Sorry, you have no editing rights for this link.";
    }
    
    $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_editlink.tpl');
}
?>
