<?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.
#
#      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_arterror'])) {
        unset($_SESSION['inplace_arterror']);
    }
} else {
    $tpl->assign('error_message', $_SESSION['inplace_arterror']);
}

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


if(isset($_SESSION['phpld']['user']['level']) && $_SESSION['phpld']['user']['level'] == 1) {
	$tpl->assign('is_admin', 1);
}

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

if ($mode == 'del') {
    //deleting article
    echo '<h4 style="color: #666666;">Deleting article, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $article['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'],$article['CATEGORY_ID']);
        if ($article['rights']['delArt'] == 1) {
             //delete article
             //get category parent first
                $data = $db->GetOne("SELECT `CACHE_URL` FROM `{$tables['category']['name']}`
                                    WHERE `ID`=(SELECT `CATEGORY_ID` FROM `{$tables['article']['name']}`
                                    WHERE `ID`=".$db->qstr($id).")");
            
	     $return = DOC_ROOT."/".$data;
             $data = $db->Execute("DELETE FROM `{$tables['article']['name']}` WHERE `ID`=".$db->qstr($id)."");
             $data = $db->Execute("DELETE FROM `{$tables['article_review']['name']}` WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
// echo '<script> window.top.location="' . $return . '"; </script>';

if (isset($_REQUEST['detail']) && ($_REQUEST['detail'] == '1')) {
            	echo '<script> window.top.location="' . $return . '"; </script>';
            } else {
             	echo '<script> window.top.location = window.top.location; </script>';
            }
        }
//        http_custom_redirect($return);
echo '<script> window.top.location = window.top.location; </script>';
    }
} elseif ($mode == 'active') {
    //making article active
    echo '<h4 style="color: #666666;">Activating article, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $article['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'],$article['CATEGORY_ID']);
        if ($article['rights']['editArt'] == 1) {
             $data = $db->Execute("UPDATE `{$tables['article']['name']}` SET `STATUS` = '2' WHERE `ID`=".$db->qstr($id)."");
             $data = $db->Execute("UPDATE `{$tables['article_review']['name']}` SET `STATUS` = '2' WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
echo '<script> window.top.location = window.top.location; </script>';
        }
//        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'),));
    $tpl->assign('types', array (0 => _L('None'), 1 => _L('Free'), 2 => _L('Normal'), 3 => _L('Reciprocal'), 4 => _L('Featured'),));

    
    // 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'])) {
        $article['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $article['CATEGORY_ID']);
    
        if ($article['rights']['editArt'] == 1) {
            if (isset($_REQUEST['action']) && $_REQUEST['action']=="submit") {
                $title = $_REQUEST['TITLE'];
                $description = $_REQUEST['DESCRIPTION'];
                $text = $_REQUEST['ARTICLE'];
                $owner_id = $_REQUEST['OWNER_ID'];
                $owner_name = $_REQUEST['OWNER_NAME'];
                $owner_email = $_REQUEST['OWNER_EMAIL'];
                $article_type = $_REQUEST['ARTICLE_TYPE'];
                $status = $_REQUEST['STATUS'];
                $meta_description = $_REQUEST['META_DESCRIPTION'];
                $meta_keywords = $_REQUEST['META_KEYWORDS'];
                $category_id = $_REQUEST['CATEGORY_ID'];
                if (trim ($_REQUEST['RELEASE_DATE']) == '')
            		$release_date = date ('Y-m-d H:i:s');
            	else 
            		$release_date = $_REQUEST['RELEASE_DATE'];
                
                $id = $_REQUEST['ID'];
                
                $_SESSION['editart'] = $_POST;
				foreach ($_SESSION['editart'] as $key=>$value) {
					$_SESSION['editart'][$key] = stripslashes($value);
				}
                
                //validations now
                if ($title == '') {
                    $error = 1;
                    $_SESSION['inplace_arterror'] = "Please fill in a title.";
                //title length
                } elseif (!((strlen($title)>=ARTICLE_TITLE_MIN_LENGTH) && (strlen($title)<=ARTICLE_TITLE_MAX_LENGTH))){
                    $error = 1;
                    $_SESSION['inplace_arterror'] = "The title must have ".ARTICLE_TITLE_MIN_LENGTH." to ".ARTICLE_TITLE_MAX_LENGTH." characters.";
                //short description length
                } elseif (!((strlen($description)>=ARTICLE_DESCRIPTION_MIN_LENGTH) && (strlen($description)<=ARTICLE_DESCRIPTION_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_arterror'] = "The short description must have ".ARTICLE_DESCRIPTION_MIN_LENGTH." to ".ARTICLE_DESCRIPTION_MAX_LENGTH." characters.";
                //article length
                } elseif (!((strlen($text)>=ARTICLE_MIN_LENGTH) && (strlen($text)<=ARTICLE_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_arterror'] = "The article text must have ".ARTICLE_MIN_LENGTH." to ".ARTICLE_MAX_LENGTH." characters.";
                } elseif (REQUIRE_REGISTERED_USER_ARTICLE == 1 && $owner_id == 0) {
                    $error = 1;
                    $_SESSION['inplace_arterror'] = "Please select an article owner.";
                } elseif (is_banned_email($owner_email)) {
                    $error = 1;
                    $_SESSION['inplace_arterror'] = "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_arterror'] = "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_arterror'] = "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_arterror'] = "Your security code does not match the image.";
                } else {
                    if ($db->Execute("UPDATE `{$tables['article']['name']}`
                            SET `TITLE` = ".$db->qstr($title).",
                                `DESCRIPTION` = ".$db->qstr($description).",
                                `ARTICLE` = ".$db->qstr($text).",
                                `OWNER_ID` = ".$db->qstr($owner_id).",
                                `OWNER_NAME` = ".$db->qstr($owner_name).",
                                `OWNER_EMAIL` = ".$db->qstr($owner_email).",
                                `ARTICLE_TYPE` = ".$db->qstr($article_type).",
                                `STATUS` = ".$db->qstr($status).",
                                `META_DESCRIPTION` = ".$db->qstr($meta_description).",
                                `META_KEYWORDS` = ".$db->qstr($meta_keywords).",
                                `CATEGORY_ID` = ".$db->qstr($category_id).",
                                `RELEASE_DATE` = ".$db->qstr($release_date)."
                                 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`= ".$db->qstr($category_id)."");
                     
                     $return = DOC_ROOT."/".$data;
//                     echo '<script> window.top.location="' . $return . '"; </script>';
                    echo '<script> window.top.location = window.top.location; </script>';
                    } else {
                        echo '<script> window.top.location = window.top.location; </script>';
                    }
 
                    } else {
                         $error = 1;
                         $_SESSION['inplace_arterror'] = "Sorry, some database error occured. Please try again.";
                    }
                }
            } else {
                if ($mode == "edit") {
                    $tpl->assign('article', $_SESSION['editart']);
                    unset($_SESSION['editart']);
                    unset($_SESSION['inplace_arterror']);
                } else {
                    $tpl->assign('article', $article);
                }
            }
        } else {
            $error = 1;
            $_SESSION['inplace_arterror'] = "Sorry, you have no editing rights for this article.";
        }
    } else {
        $error = 1;
        $_SESSION['inplace_arterror'] = "Sorry, you have no editing rights for this article.";
    }
    $tpl->assign('error', $error);
    if ($error == 1) {
//        http_custom_redirect($return);
echo '<script> window.top.location = window.top.location; </script>';
    }
    $useRTE = (defined ('ARTICLE_SUBMIT_USE_RTE') && ARTICLE_SUBMIT_USE_RTE == 1 ? 1 : 0);
    $tpl->assign('useRTE', $useRTE);
    echo $tpl->fetch('inplace_editart.tpl');
}
?>
