<?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['mode']) && $_REQUEST['mode'] == 'confirmed') {
    $tpl->assign('confirmed', 1);
}

if (defined('REQUIRE_REGISTERED_USER') && (REQUIRE_REGISTERED_USER == 1) && (EMAIL_CONFIRMATION == 1)) {
	if (isset($_SESSION['phpld']['user']['id'])) {
		$uid = $_SESSION['phpld']['user']['id'];
		if (!has_confirmed_email($uid)) {
			$reason = _L('You have not confirmed your email address yet').'! '._L('Please check your email for the confirmation link').'.';
			gotoUnauthorized($reason);
		}
	}
} 

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


define ('FORCE_SUBMIT_SESSION', "0");// Remove submit session for now

if (defined ('FORCE_SUBMIT_SESSION') && FORCE_SUBMIT_SESSION == 1)
{
   require_once 'include/submit_session.php';
}

//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);
}

$path   = array ();
$path[] = array ('ID' => '0', 'TITLE' => _L(DIRECTORY_TITLE)    , 'TITLE_URL' => DOC_ROOT, 'DESCRIPTION' => SITE_DESC);
$path[] = array ('ID' => '0', 'TITLE' => _L('Submit Article'), 'TITLE_URL' => ''      , 'DESCRIPTION' => _L('Submit a new article to the directory '));
$tpl->assign('path', $path);

//Check if using RTE (Rich Text Editor)
$useRTE = (defined ('ARTICLE_SUBMIT_USE_RTE') && ARTICLE_SUBMIT_USE_RTE == 1 ? 1 : 0);
$tpl->assign('useRTE', $useRTE);

//If current user is banned, show a custom error message
//and stop the rest of the script
check_if_banned();

//Make an additional check if client is allowed to post/submit
//[Spam] protection
require_once 'include/check_post_rules.php';
$post_rules_unauthorized = check_post_rules($_POST);

// Evaluate payment options

if (ARTICLE_PAY_ENABLE == 1 && PAYPAL_ACCOUNT != '')
{
   $price = array ();
   if (FTR_ENABLE == 1 && ARTICLE_PAY_FEATURED > 0)
      $price['featured'] = ARTICLE_PAY_FEATURED;

   if (ARTICLE_PAY_NORMAL > 0)
   {
      $price['normal'] = ARTICLE_PAY_NORMAL;
      if (ARTICLE_PAY_ENABLE_FREE)
         $price['free'] = 0;
   }

   $tpl->assign('price', $price);

   if (isset ($_POST['ARTICLE_TYPE']))
   {
      $article_type = $_POST['ARTICLE_TYPE'];
      switch (strtolower ($article_type))
      {
         case 'free' :
            break;
         default :
            $recpr_required = 0;
            break;
      }
   }
   else
      $recpr_required = 0;

   $_SESSION['SmartyValidate']['submit_article']['validators'][6]['empty'] = ($recpr_required ? false : true);
   $_SESSION['SmartyValidate']['submit_article']['validators'][7]['empty'] = ($recpr_required ? false : true);
   $_SESSION['SmartyValidate']['submit_article']['validators'][8]['empty'] = ($recpr_required ? false : true);
   $_SESSION['SmartyValidate']['submit_article']['validators'][9]['empty'] = ($recpr_required ? false : true);
}

if (isset ($price) && !empty ($price) && is_array ($price))
{
   foreach ($price as $value)
   {
      if ($value > 0)
         $ShowReciprField = 1;
   }
}





$article_id = (!empty ($_REQUEST['articleid']) ? clean_string_paranoia($_REQUEST['articleid']) : 0);

$remove_article  = 0;
$review_article  = 0;
$EditRedirect = 0;

//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 = '';

if (!empty($CategoryID))
    check_if_closed_to_articles($CategoryID);


if (!empty ($article_id))
{
   $article_id = intval ($article_id);
   if (REQUIRE_REGISTERED_USER_ARTICLE == 1 && !empty ($_SESSION['phpld']['user']['id']))
   {
      $check_user = $db->GetOne("SELECT COUNT(*) FROM `{$tables['article']['name']}` WHERE `ID` = ".$db->qstr($article_id)." AND `OWNER_ID` = ".$db->qstr($_SESSION['phpld']['user']['id']));

      $check_double_review = $db->GetOne("SELECT COUNT(*) FROM `{$tables['article_review']['name']}` WHERE `ARTICLE_ID` = ".$db->qstr($article_id));
      if ($check_double_review > 0)
         $tpl->assign('double_review', _L('This entry is already marked for review and was not approved yet. New modifications will overwrite older reviews.'));

   }
   if ($check_user < 1)
   {
      http_custom_redirect(DOC_ROOT.'/unauthorized.php');
   }

   $remove_article = 1;
   $review_article = 1;
}

$EditUnique    = '';
$MoveToPayment = true;
$action        = 'submit'; //Default action
if (REQUIRE_REGISTERED_USER_ARTICLE == 1 && !empty ($_SESSION['phpld']['user']['id']) && $review_article == 1)
   $action = 'edit';

if (!empty ($_SESSION['phpld']['user']['id']))
   $owner_details = $db->GetRow("SELECT `NAME` AS `OWNER_NAME`, `EMAIL` AS `OWNER_EMAIL` FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($_SESSION['phpld']['user']['id']));

if ((empty ($_POST['submit']) && empty ($_POST['edit']))  || (!SmartyValidate :: is_registered_form('submit_article')))
{

   if (defined ('FORCE_SUBMIT_SESSION') && FORCE_SUBMIT_SESSION == 1)
   {
      generateSubmitSession();
   }

   if (!empty ($_SERVER['HTTP_REFERER']))
      $_SESSION['return'] = $_SERVER['HTTP_REFERER'];

   if (REQUIRE_REGISTERED_USER_ARTICLE == 1 && !empty ($_SESSION['phpld']['user']['id']) && !empty ($article_id))
   {
      $sql = "SELECT * FROM `{$tables['article']['name']}` WHERE `ID` = ".$db->qstr($article_id)." AND `OWNER_ID` = ".$db->qstr($_SESSION['phpld']['user']['id']);
      $data = $db->GetRow($sql);
      $EditUnique = ':TITLE:'.$data['TITLE'];
   }
   else
   {
      $data = array ();
      $data['CATEGORY_ID'] = $CategoryID;
   }

   $CategoryTitle = getCategoryTitleByID($data['CATEGORY_ID']);

   if (PAY_ENABLE == 1 && PAYPAL_ACCOUNT != '')
      $article_type = determine_article_type($data['ARTICLE_TYPE']);

   // Clear some varialbles
   if (isset($_SESSION['SmartyPaginate']))
      unset ($_SESSION['SmartyPaginate']);
   if (isset($_SESSION['SmartyValidate']))
      unset ($_SESSION['SmartyValidate']);
   if (isset($_SESSION['values']))
      unset ($_SESSION['values']);

   SmartyValidate :: disconnect();
   SmartyValidate :: connect($tpl, true);
   
    //for login
    if (!SmartyValidate :: is_registered_form('login')) {
	    SmartyValidate :: register_form('login', true);
	    SmartyValidate :: register_validator('v_user', 'user', 'notEmpty', false, false, 'trim', 'login');
	    SmartyValidate :: register_validator('v_pass', 'pass', 'notEmpty', false, false, 'trim', 'login');
    }
    //end for login

   if ((isset($data['CATEGORY_ID']) && $rights['addArt'] == 1)
    || (has_rights_on_all_cats($_SESSION['phpld']['user']['level']))) {
         $dont_show_captch = 1;
         if (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)) {
             $dont_show_pay = 1;
         }
         $tpl->assign('dont_show_captch', $dont_show_captch);
         $tpl->assign('dont_show_pay', $dont_show_pay);
    }
    
    
   SmartyValidate :: register_form('submit_article', true);

   SmartyValidate :: register_criteria('isValueUnique'    , 'validate_unique'         , 'submit_article');
   SmartyValidate :: register_criteria('isUrlUnique'      , 'validateUrlUnique'       , 'submit_article');
   SmartyValidate :: register_criteria('isNotEqual'       , 'validate_not_equal'      , 'submit_article');
   SmartyValidate :: register_criteria('isDomainBanned'   , 'validate_isBannedDomain' , 'submit_article');
   SmartyValidate :: register_criteria('isCaptchaValid'   , 'validate_captcha'        , 'submit_article');
   SmartyValidate :: register_criteria('isBannedEmail'    , 'validate_is_banned_email', 'submit_article');

   SmartyValidate :: register_validator('v_TITLE'         , 'TITLE', 'notEmpty'  , false, false, 'trim', 'submit_article');
   SmartyValidate :: register_validator('v_TITLE_U'       , 'TITLE:article::CATEGORY_ID'.$EditUnique, 'isValueUnique', false, false, null, 'submit_article');
   SmartyValidate :: register_validator('v_TITLE_LENGTH'  , 'TITLE:'.ARTICLE_TITLE_MIN_LENGTH.':'.ARTICLE_TITLE_MAX_LENGTH, 'isLength', false, false, 'trim', 'submit_article');

   SmartyValidate :: register_validator('v_DESCRIPTION_LENGTH', 'DESCRIPTION:'.ARTICLE_DESCRIPTION_MIN_LENGTH.':'.ARTICLE_DESCRIPTION_MAX_LENGTH, 'isLength'  , true, false, 'trim', 'submit_article');

   SmartyValidate :: register_validator('v_ARTICLE_LENGTH', 'ARTICLE:'.ARTICLE_MIN_LENGTH.':'.ARTICLE_MAX_LENGTH, 'isLength'  , true, false, 'trim', 'submit_article');

   SmartyValidate :: register_validator('v_CATEGORY_ID'   , 'CATEGORY_ID:0'      , 'isNotEqual', false, false, null, 'submit_article');
   //Rules acceptance //
   SmartyValidate :: register_validator('v_AGREERULES'     , 'AGREERULES:0'        , 'isEqual'    , false, false, null, 'submit_article');

   if (REQUIRE_REGISTERED_USER_ARTICLE == 0 || empty ($_SESSION['phpld']['user']['id']))
   {
      SmartyValidate :: register_validator('v_OWNER_NAME' , 'OWNER_NAME'         , 'notEmpty'      , false, false, 'trim', 'submit_article');
      SmartyValidate :: register_validator('v_OWNER_NAME_LENGTH'  , 'OWNER_NAME:'.USER_NAME_MIN_LENGTH.':'.USER_NAME_MAX_LENGTH, 'isLength', false, false, 'trim', 'submit_article');
      SmartyValidate :: register_validator('v_OWNER_EMAIL', 'OWNER_EMAIL'        , 'isEmail'       , false, false, 'trim', 'submit_article');
   }
   SmartyValidate :: register_validator('v_OWNER_EMAIL_BANNED', 'OWNER_EMAIL', 'isBannedEmail', false, false, 'trim', 'submit_article');

   if (ARTICLE_PAY_ENABLE)
      SmartyValidate :: register_validator('v_ARTICLE_TYPE'  , 'ARTICLE_TYPE'          , 'notEmpty'      , false, false, 'trim', 'submit_article');
   if (count ($price) > 0 && $dont_show_pay != 1)
      SmartyValidate :: register_validator('v_ARTICLE_TYPE'  , 'ARTICLE_TYPE'          , 'notEmpty'      , false, false, 'trim', 'submit_article');

   if (VISUAL_CONFIRM == 1 && $dont_show_captch != 1)
      SmartyValidate :: register_validator('v_CAPTCHA'    , 'CAPTCHA:IMAGEHASH'  , 'isCaptchaValid', false, false, null, 'submit_article');

   SmartyValidate :: register_validator('v_META_DESCRIPTION_LENGTH', 'META_DESCRIPTION:'.META_DESCRIPTION_MIN_LENGTH.':'.META_DESCRIPTION_MAX_LENGTH, 'isLength'  , true, false, 'trim', 'submit_article');

   //Not usually neccessary, keywords are trimmed to max allowed length automatically
   SmartyValidate :: register_validator('v_META_KEYWORDS_LENGTH' , 'META_KEYWORDS:'.META_KEYWORDS_MIN_LENGTH.':'.META_KEYWORDS_MAX_LENGTH, 'isLength', true , false, 'trim', 'submit_article');
}
else
{
   if (isset($_SESSION['article_submitted'])) {
        unset($_SESSION['article_submitted']);
   }
    
   SmartyValidate :: connect($tpl);

   $CategoryTitle = getCategoryTitleByID($data['CATEGORY_ID']);

   if (REQUIRE_REGISTERED_USER_ARTICLE == 1 && !empty ($_SESSION['phpld']['user']['id']) && !empty ($_POST['edit']))
      $action = 'edit';

   if ($action == 'edit')
      $data = get_table_data('article_review');
   else
      $data = get_table_data('article');
      
   if (!empty ($owner_details))
      $data = array_merge ($data, $owner_details);

   //Take care of white-space chars
   $data = filter_white_space($data);

   //check if user needs approval
   $rights = array();
   $rights = user_needs_approval($_SESSION['phpld']['user']['id'], $data['CATEGORY_ID']);
   if ($action == 'submit' && $rights['addArt'] == 1) {
       $data['STATUS']         = '2';
   } else {
       $data['STATUS']         = '1';
       $tpl->assign('waiting_approval', 1);
   }
   
   $data['IPADDRESS']      = $client_info['IP'];
   if (!empty ($client_info['HOSTNAME']))
      $data['DOMAIN']      = $client_info['HOSTNAME'];

   $data['VALID']          = '1';
    if (!isset ($_POST['ARTICLE_TYPE']))
   {
$article_type = 'normal';
   }
   $data['ARTICLE_TYPE']      = $article_type;

   $data['DATE_ADDED']     = gmdate ('Y-m-d H:i:s');
   $data['DATE_MODIFIED']  = gmdate ('Y-m-d H:i:s');

   if (VISUAL_CONFIRM == 1 && !empty ($_POST['CAPTCHA']))
   {
      $data = array_merge ($data, array ('CAPTCHA' => $_POST['CAPTCHA'], 'IMAGEHASH' => $_POST['IMAGEHASH']));
   }

   $alreadyFeatured = 0;
   $SecondValidation = 1;

   //On review, check if article is already a featured article
   if ($action == 'edit')
   {
      $alreadyFeatured = $db->GetOne("SELECT `FEATURED` FROM {$tables['article']['name']} WHERE `ID` = ".$db->qstr($article_id));
   }

   if (ARTICLE_PAY_ENABLE == '1' && FTR_ENABLE == 1 && $_POST['ARTICLE_TYPE'] == 'featured' && !empty ($data['CATEGORY_ID']) && $alreadyFeatured == 0)
   {
      $SecondValidation = $AllowedFeat = check_allowed_feat($data['CATEGORY_ID']);
      $tpl->assign('AllowedFeat', $AllowedFeat);
   }



   if (!isset ($_SESSION['SmartyValidate']['submit_article']) || empty ($_SESSION['SmartyValidate']['submit_article']))
   {
      //Invalid or expired submission,
      //Block access
      unset ($data);
      $reason = _L('Invalid or expired submit session').'! '._L('Please reload submit page and try again').'.';
      gotoUnauthorized($reason);
   }
   //Rules check
   $data['AGREERULES'] = (isset ($_POST['AGREERULES']) && $_POST['AGREERULES'] == 'on' ? 0 : 1);
   
   if (SmartyValidate :: is_valid($data, 'submit_article'))
   {
      if (defined ('FORCE_SUBMIT_SESSION') && FORCE_SUBMIT_SESSION == 1)
      {
         //Validate unique submit session
         $submitSessionValidation = validate_submit_session();

         if ($submitSessionValidation == 0)
         {
            //Invalid or expired submission,
            //Block access
            unset ($data);
            $reason = _L('Invalid or expired submit session').'! '._L('Please reload submit page and try again').'.';
            gotoUnauthorized($reason);
         }
      }

      //Validate for banned words
      //Pass all submition data as parameter
      $hasBannedWords = if_word_is_banned($data);
      if ($hasBannedWords == 1)
      {
         //Invalid or expired submission,
         //Block access
         unset ($data);
         $reason = _L('The administrator of this article directory, has banned words from your submission.');
         gotoUnauthorized($reason);
      }

      if (trim ($data['RELEASE_DATE']) == '')
      		$data['RELEASE_DATE'] = date ('Y-m-d H:i:s');
      
      //Remove variable as we don't need to write it to DB
      if (isset ($data['CAPTCHA']))
         unset ($data['CAPTCHA']);
      if (isset ($data['IMAGEHASH']))
         unset ($data['IMAGEHASH']);
      if (isset ($data['AGREERULES']))
         unset ($data['AGREERULES']);

      if ($action == 'edit')
      {
         $id = $db->GetOne("SELECT `ID` FROM `{$tables['article_review']['name']}` WHERE `ARTICLE_ID` = ".$db->qstr($article_id));
         $id = (!empty ($id) ? intval ($id) : '');
      }
      else
         $id = $db->GenID($tables['article']['name'].'_SEQ');
         
      //email confirmed related
   
   //if email confirmation not required
   if(EMAIL_CONFIRMATION == 0) {
       $data['OWNER_EMAIL_CONFIRMED'] = '1';
   } else {
       //if has rights no longer requiring email confirmation
       if ($rights['addLink'] == 1) {
           $data['OWNER_EMAIL_CONFIRMED'] = '1';
       }
       //send confirmation email
       elseif (!email_is_confirmed($data['OWNER_EMAIL'])) {
           $data['OWNER_EMAIL_CONFIRMED'] = '0';
           $mail = get_emailer_admin();
    
           //Add email subject
           $emailSubject = "Email Confirmation";
           $mail->Subject = trim ($emailSubject);

           //Add owner email address
           $mail->AddAddress($data['OWNER_EMAIL'], $data['OWNER_NAME']);

           //Add email body
           $emailBody = "Please confirm email address here: ".SITE_URL."confirm_email.php?aid=".$id;
           $mail->Body = trim ($emailBody);

           //Send email
           if (!$mail->Send())
           {
              $error++;
              $tpl->assign('error', $error);
              $errorMsg = $mail->ErrorInfo;
              $tpl->assign('errorMsg', $errorMsg);
           }

           //Clear all addresses (and attachments) for next loop
           $mail->ClearAddresses();
           $mail->ClearAttachments();

           //Free memory
           unset ($unhashedPassword, $mail, $emailBody, $emailSubject);
           $tpl->assign("needs_confirmation", 1);
          //end of confirmation email
        } else {
            $data['OWNER_EMAIL_CONFIRMED'] = '1';
        }
      
   }
   //end of email confirmed
         
      
      $data['ID']        = (!empty ($id) ? intval ($id) : '');
      $data['ARTICLE_TYPE'] = $link_type_int[$article_type];

      switch (strtolower ($article_type))
      {
         case 'featured' :
            $data['FEATURED'] = 1;
            break;
         default :
            break;
      }
     
      if (!empty ($_SESSION['phpld']['user']['id']))
         $data['OWNER_ID'] = $_SESSION['phpld']['user']['id'];

//      if (!empty ($owner_details))
//         $data = array_merge ($data, $owner_details);

      //raluca
         
      if (isset($_SESSION['phpld']['user']['id'])) {
          $rights = user_needs_approval($_SESSION['phpld']['user']['id'], $data['CATEGORY_ID']);
          if ((isset($data['CATEGORY_ID']) && $rights['addArt'] == 1)
            || (has_rights_on_all_cats($_SESSION['phpld']['user']['level']))) {
                $data['PAYED'] = 1;
                $dont_redir_topaypal = 1;
            }
      }
         
      //raluca
      
      $data['OWNER_NOTIF'] = ($price[$article_type] > 0         ? 0 : 1 );
      $data['MARK_REMOVE'] = (!empty ($_POST['MARK_REMOVE']) ? 1 : 0 );

      //Check again fields and truncate to maximum length,
      //auto-submitters can no more force longer text than allowed
      $data['TITLE'] = (!empty ($data['TITLE']) ? (strlen ($data['TITLE']) > ARTICLE_TITLE_MAX_LENGTH ? substr ($data['TITLE'], 0, ARTICLE_TITLE_MAX_LENGTH - 4).' ...' : trim ($data['TITLE'])) : '');

      if ($action != 'edit')
      {
         $data['OWNER_NAME'] = (!empty ($data['OWNER_NAME']) ? (strlen ($data['OWNER_NAME']) > USER_NAME_MAX_LENGTH ? substr ($data['OWNER_NAME'], 0, USER_NAME_MAX_LENGTH - 4).' ...' : trim ($data['OWNER_NAME'])) : '');
      }

      $data['DESCRIPTION'] = (!empty ($data['DESCRIPTION']) ? (strlen ($data['DESCRIPTION']) > ARTICLE_DESCRIPTION_MAX_LENGTH ? substr ($data['DESCRIPTION'], 0, ARTICLE_DESCRIPTION_MAX_LENGTH - 4).' ...' : trim ($data['DESCRIPTION'])) : '');
      $data['ARTICLE'] = (!empty ($data['ARTICLE']) ? (strlen ($data['ARTICLE']) > ARTICLE_MAX_LENGTH ? substr ($data['ARTICLE'], 0, ARTICLE_MAX_LENGTH - 4).' ...' : trim ($data['ARTICLE'])) : '');
      $data['META_DESCRIPTION'] = (!empty ($data['META_DESCRIPTION']) ? (strlen ($data['META_DESCRIPTION']) > META_DESCRIPTION_MAX_LENGTH ? substr ($data['META_DESCRIPTION'], 0, META_DESCRIPTION_MAX_LENGTH - 4).' ...' : trim ($data['META_DESCRIPTION'])) : '');
      $data['META_KEYWORDS'] = (!empty ($data['META_KEYWORDS']) ? clean_meta_keywords($data['META_KEYWORDS']) : '');

      $RegularArticle_notif = true;

         if ($action == 'edit')
         {

            $data['ARTICLE_ID']   = $article_id;
            $submit_notification = db_replace('article_review', $data, 'ID');
            $RegularArticle_notif   = false;
         }
         else
         {
            $payment_id = $data['ID'];
            unset ($data['MARK_REMOVE']);
            $submit_notification = db_replace('article', $data, 'ID');
         }

         if (!empty ($submit_notification))
         {
            $tpl->assign('posted', true);
            
            
            send_submit_notificationsa($data, $RegularArticle_notif, "article");

            //Remind some fields
            $reminder = array ('CATEGORY_ID', 'OWNER_NAME', 'OWNER_EMAIL');

            //Loop throught each article field
            foreach ($data as $field => $value)
            {
               //Check if it's not in the reminder fields
               if (!in_array ($field, $reminder))
               {
                  //Remove data
                  unset ($data[$field]);
               }
            }

            $payment_id = (!empty ($payment_id) && preg_match('`^[\d]+$`', $payment_id) ? intval ($payment_id) : '');

            if($article_type == 'featured' && ARTICLE_PAY_FEATURED > 0 && $dont_redir_topaypal!=1)
               $payment_redirect = 1;
            elseif($article_type == 'normal' && ARTICLE_PAY_NORMAL > 0 && $dont_redir_topaypal!=1)
               $payment_redirect = 1;

            if ($payment_redirect == 1 && !empty ($payment_id) && $MoveToPayment == true)
            {
               //Move to payment page
               $_SESSION['PRIVACY'] = '1';
               http_custom_redirect(DOC_ROOT."/article_payment.php?id=".$payment_id.($action == 'edit' ? '&mode=review' : ''));
            }
            else
            {
               if (defined ('FORCE_SUBMIT_SESSION') && FORCE_SUBMIT_SESSION == 1)
               {
                  //Submission successfull, generate new submit session
                   
                   generateSubmitSession();
                   
                   //Raluca Nicula: need redirect for right& left side articles to be updated
                   $_SESSION['article_submitted'] = 1;
                   http_custom_redirect(DOC_ROOT."/submit_article.php");
               }
               unset ($_POST, $_GET, $_REQUEST);
            }

            
               if (defined ('FORCE_SUBMIT_SESSION') && FORCE_SUBMIT_SESSION == 1)
               {
                  //Submission successfull, generate new submit session
                  generateSubmitSession();
                  
                  //Raluca Nicula: need redirect for right& left side articles to be updated
                  $_SESSION['article_submitted'] = 1;
               }
               unset ($_POST, $_GET, $_REQUEST);

            $EditRedirect = 1;
         }
         else
            $tpl->assign('error', true);
		// echo $db->ErrorMsg();

   }
   else
      $tpl->assign($_POST);
}

if (defined ('ALLOW_HTML') && ALLOW_HTML == 1) {
	//Set allowed tags for template to display
	//Clean up tag list
	$allowedTags = clean_string_paranoia(ALLOWED_HTML_TAGS);
	//Remove multiple commas, commast at begin and end of sting, multiple spaces
	$allowedTags = preg_replace (array ('#^[,]*#i', '#[,]*$#i', '#[,]+#i', '#[\s]#'), array ('', '', ',', ''), $allowedTags);
	$allowedTags = preg_replace ("/,/",', ', $allowedTags);
	$tpl->assign('allowedTags', $allowedTags);
}
if (defined ('ALLOW_ATTR') && ALLOW_ATTR == 1) {
	//Set allowed tags for template to display
	//Clean up tag list
	$allowedAttr = clean_string_paranoia(ALLOWED_ATTR_TAGS);
	//Remove multiple commas, commast at begin and end of sting, multiple spaces
	$allowedAttr = preg_replace (array ('#^[,]*#i', '#[,]*$#i', '#[,]+#i', '#[\s]#'), array ('', '', ',', ''), $allowedAttr);
	$allowedAttr = preg_replace ("/,/",', ', $allowedAttr);
	$tpl->assign('allowedAttr', $allowedAttr);
}
//Determine length of description field
$DescriptionLimit = (isset ($data['DESCRIPTION']) && strlen (trim ($data['DESCRIPTION'])) > 0 ? ARTICLE_DESCRIPTION_MAX_LENGTH - strlen (trim ($data['DESCRIPTION'])) : ARTICLE_DESCRIPTION_MAX_LENGTH);
$tpl->assign('DescriptionLimit', $DescriptionLimit);

//Determine length of ARTICLE field
$ArticleLimit = (isset ($data['ARTICLE']) && strlen (trim ($data['ARTICLE'])) > 0 ? ARTICLE_MAX_LENGTH - strlen (trim ($data['ARTICLE'])) : ARTICLE_MAX_LENGTH);
$tpl->assign('ArticleLimit', $ArticleLimit);

//Determine length of meta description field
$MetaDescriptionLimit = (isset ($data['META_DESCRIPTION']) && strlen (trim ($data['META_DESCRIPTION'])) > 0 ? META_DESCRIPTION_MAX_LENGTH - strlen (trim ($data['META_DESCRIPTION'])) : META_DESCRIPTION_MAX_LENGTH);
$tpl->assign('MetaDescriptionLimit', $MetaDescriptionLimit);
$disable_article_reason = DISABLE_ARTICLE_REASON;
$tpl->assign('disablearticlereason', $disable_article_reason);
if (VISUAL_CONFIRM == 1)
   $tpl->assign('captcha_length', CAPTCHA_PHRASE_LENGTH);

$tpl->assign('remove_article', $remove_article);
$tpl->assign('review_article', $review_article);

if (CAT_SELECTION_METHOD == 0)
{
//   $categs = get_regular_categs_tree(0);
   $categs = get_regular_categs_tree_opentoarticles(0);
   $tpl->assign('categs', $categs);
}

//Load Javascript libraries
$load_Javascript = 1;
$tpl->assign('load_Javascript', $load_Javascript);
// custom //
$prurl = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$tpl->assign('prurl', $prurl);
$CategoryTitle = getCategoryTitleByID($data['CATEGORY_ID']);
$tpl->assign('CategoryID', $CategoryID);
// end custom //
$tpl->assign('CategoryTitle', $CategoryTitle);

$tpl->assign($data);

$tpl->assign('SITE_NAME'      , SITE_NAME);

//Clean whitespace
$tpl->load_filter('output', 'trimwhitespace');

if ($_SESSION['article_submitted'] == 1) {
    $tpl->assign('posted', true);
    unset($_SESSION['article_submitted']);
}

if ((isset($data['CATEGORY_ID']) && $rights['addArt'] == 1)
|| (has_rights_on_all_cats($_SESSION['phpld']['user']['level']))) {
     $dont_show_captch = 1;
     if (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)) {
         $dont_show_pay = 1;
     }
     $tpl->assign('dont_show_captch', $dont_show_captch);
     $tpl->assign('dont_show_pay', $dont_show_pay);
}


//Make output
echo $tpl->fetch('submit_article.tpl', $id);

if ($action == 'edit' && $EditRedirect == 1)
   http_custom_redirect('submit_article.php', 'html', 5, '');
?>