<?php
///////////////////////////////////////////////////////////////////////////////////////////
///Script PHP/MYSQL of management of classifieds ads developed by Script PAG
///Script PAG all rights reserved. Use under license. http://www.script-pag.com
///////////////////////////////////////////////////////////////////////////////////////////

############################################################

require_once __DIR__.'/includes_adm/all_fns_adm.php';
require_once __DIR__.'/../includes/calendar/fns_calendar.php';
require_once __DIR__.'/../includes/functions/uploader/uploadManager.php';

///////////////////////////////////
//Check if an admin is connected
//////////////////////////////////
	
if(!empty($_SESSION['valid_admin']))
$valid_admin = $_SESSION['valid_admin'];

if(!check_valid_all())
redirect('index.php');

modules::init();
pool::execute('beforedispatch');

///////////////////////////////////
//Validation of the action
//////////////////////////////////

if(!empty($_GET['act']))
{
	if($_GET['act'] >= 1 && $_GET['act'] <= 9)
	$act = (int) $_GET['act'];
	
	else $act = 0;
}
else $act = 0;

///////////////////////////////////
//Validation of the state of ads
//////////////////////////////////

if(isset($_GET['state']))
{
	if($_GET['state'] >= 1 || $_GET['state'] <= 8)
	$state = (int) $_GET['state'];
	
	else $state = 0;
}

///////////////////////////////////
//Search engine variables
//////////////////////////////////

$id_ad_search = (!empty($_GET['id_ad'])) ? (int) $_GET['id_ad'] : 0;
$email_search = (!empty($_GET['email'])) ? $_GET['email'] : '';
$cat_search = (!empty($_GET['cat'])) ? (int) $_GET['cat'] : 0;
$reg_search = (!empty($_GET['reg'])) ? (int) $_GET['reg'] : 0;
$result = (!empty($_GET['result'])) ? (int) $_GET['result'] : 0;

///////////////////////////////////
//Get number of ads
//////////////////////////////////	

if(isset($state))
$total = get_ad_num($state, $_GET);

///////////////////////////////////
//Initialize the variable for ads number per page
//////////////////////////////////

if(!empty($_GET['result']))
$limit = (int) $_GET['result'];

else $limit = 5;

///////////////////////////////////
//Pagination ads Page
//////////////////////////////////

if(isset($state))
{
	$page = 1;
			
	if(isset($_GET['page']))
	{
		$page = (int) $_GET['page'];
		
		if ($page <= 0)
		$page = 1;
	}
	
	$max_page = ceil($total/$limit);
	
	if ($max_page > 0 && $page > $max_page)
	$page = $max_page;
	
	$offset = ($page - 1) * $limit;
}

///////////////////////////////////
//Get ads
//////////////////////////////////	

if(isset($state))
$ads = get_ads($state, $_GET, $offset, $limit);

///////////////////////////////////
//Validate ad
//////////////////////////////////

if(isset($_POST['valid']))
{
	$id_ad = (int) $_POST['valid'];
	
	validate_ad($id_ad);
	
	$array_ad = get_ad($id_ad, 1);
	
	if(!isset($_POST['mail_'. $id_ad]))
	send_validate_ad($id_ad, $array_ad);
	
	if($state == 4)
	update_date_ad($id_ad);
	
	get_nb_ads();
	
	redirect('ads_management.php?state='. $state .'&succes=1&menu=6');
}

///////////////////////////////////
//Validate all ads
//////////////////////////////////

if(isset($_POST['valid_all']) && isset($_POST['check_all']))
{
	$array = $_POST['check_all'];
	
	foreach($array as $id_ad)
	{
		validate_ad($id_ad);
		
		$array_ad = get_ad($id_ad, 1);
		
		if(!isset($_POST['mail_'. $id_ad]))
		send_validate_ad($id_ad, $array_ad);
		
		if($state == 4)
		update_date_ad($id_ad);
	}
	
	get_nb_ads();
	
	$succes = (count($_POST['check_all']) > 1) ? '&succes=2' : '&succes=1';
	
	redirect('ads_management.php?state='. $state . $succes .'&menu=6');
}

///////////////////////////////////
//Reject ad
//////////////////////////////////

if(isset($_POST['reject']))
{
	$id_ad = (int) $_POST['reject'];
	
	reject_ad($id_ad);
	
	if(!isset($_POST['mail_'. $id_ad]))
	{
		$modif = (isset($_POST['update_'. $id_ad])) ? $_POST['update_'. $id_ad] : '';
		$msg_rej = (!empty($_POST['msg_'. $id_ad])) ? $_POST['msg_'. $id_ad] : '';
		
		$array_ad = get_ad($id_ad, 1);
		send_reject_ad($id_ad, $modif, $msg_rej, $array_ad);
	}
	
	if(isset($_POST['ban_'. $id_ad]))
	{
		blacklisted_email($_POST['ban_'. $id_ad]);
		create_cache_blacklist();
	}
	
	get_nb_ads();
	
	redirect('ads_management.php?state='. $state .'&succes=3&menu=6');
}

///////////////////////////////////
//Reject all ads
//////////////////////////////////

if(isset($_POST['reject_all']) && isset($_POST['check_all']))
{
	$array = $_POST['check_all'];
	
	foreach($array as $id_ad)
	{
		reject_ad($id_ad);
		
		if(!isset($_POST['mail_'. $id_ad]))
		{
			$modif = (isset($_POST['update_'. $id_ad])) ? $_POST['update_'. $id_ad] : '';
			$msg_rej = (!empty($_POST['msg_'. $id_ad])) ? $_POST['msg_'. $id_ad] : '';
			
			$array_ad = get_ad($id_ad, 1);
			send_reject_ad($id_ad, $modif, $msg_rej, $array_ad);
		}
		
		if(isset($_POST['ban_'. $id_ad]))
		{
			blacklisted_email($_POST['ban_'. $id_ad]);
			create_cache_blacklist();
		}
	}
	
	get_nb_ads();
	
	$succes = (count($_POST['check_all']) > 1) ? '&succes=4' : '&succes=3';
	
	redirect('ads_management.php?state='. $state . $succes .'&menu=6');
}

///////////////////////////////////
//Delete ad
//////////////////////////////////

if(isset($_POST['delete']))
{
	$id_ad = (int) $_POST['delete'];
	
	if(!isset($_POST['mail_'. $id_ad]))
	{
		$modif = (isset($_POST['update_'. $id_ad])) ? $_POST['update_'. $id_ad] : '';
		$msg_rej = (!empty($_POST['msg_'. $id_ad])) ? $_POST['msg_'. $id_ad] : '';
		
		$array_ad = get_ad($id_ad, 1);
		send_reject_ad($id_ad, $modif, $msg_rej, $array_ad);
	}
	
	delete_ad_adm($id_ad);
	
	if(isset($_POST['ban_'. $id_ad]))
	{
		blacklisted_email($_POST['ban_'. $id_ad]);
		create_cache_blacklist();
	}
	
	redirect('ads_management.php?state='. $state .'&succes=5&menu=6');
}

///////////////////////////////////
//Delete all ads
//////////////////////////////////

if(isset($_POST['delete_all']) && isset($_POST['check_all']))
{
	$array = $_POST['check_all'];
	
	foreach($array as $id_ad)
	{
		if(!isset($_POST['mail_'. $id_ad]))
		{
			$modif = (isset($_POST['update_'. $id_ad])) ? $_POST['update_'. $id_ad] : '';
			$msg_rej = (!empty($_POST['msg_'. $id_ad])) ? $_POST['msg_'. $id_ad] : '';
			
			$array_ad = get_ad($id_ad, 1);
			send_reject_ad($id_ad, $modif, $msg_rej, $array_ad);
		}
		
		if(isset($_POST['ban_'. $id_ad]))
		{
			blacklisted_email($_POST['ban_'. $id_ad]);
			create_cache_blacklist();
		}
		
		delete_ad_adm($id_ad);
	}
	
	$succes = (count($_POST['check_all']) > 1) ? '&succes=6' : '&succes=5';
	
	redirect('ads_management.php?state='. $state . $succes .'&menu=6');
}

///////////////////////////////////
//Validating the id of the ad
//////////////////////////////////

if(!isset($state) && !empty($_GET['id_ad']))
$id_ad = (int) $_GET['id_ad'];

elseif(!isset($state) && ($act < 4 || $act > 9)) redirect('index.php');

///////////////////////////////////
//Delete ad's form data in session
//////////////////////////////////

if(!isset($act) || ($act != 1 && $act != 9) || ($act == 9 && !isset($_POST['title']))) {
	unset($_SESSION['cal_period']);
}

///////////////////////////////////
//Check and get the ad
//////////////////////////////////
$images = array();
$files = array();

if(isset($id_ad) && $act == 1)
{
	if(get_ad($id_ad, 1))
	{		
		$ad = get_ad($id_ad, 1);
		$images = get_ad_pictures($id_ad);
		$files = get_ad_files($id_ad);
		$opts_digital = get_opts_ad_digital($id_ad);
		$opts_data = get_opts_ad_data($id_ad);
		$opts_check = get_opts_ad_check($id_ad);
		$fields = get_ad_fields($id_ad);
		$calendar = get_calendar($id_ad);

		//Images
		if(!empty($images)) {
			$new_images = array();
			foreach($images as $img) $new_images[] = isset($img['name']) ? $img['name'] : str_replace('/upload/photos/', '', $img);
			$images = $new_images;
		}
		
		//Files
		if(!empty($files)) {
			$new_files = array();
			foreach($files as $file) $new_files[] = isset($file['name']) ? $file['name'] : str_replace('/upload/files/', '', $file);
			$files = $new_files;
		}
		
		//Digital options
		
		if(is_array($opts_digital))
		{
			foreach($opts_digital as $v)
			{
				if($v['id_ad'] == $_GET['id_ad'])
				$infos['digital_'. $v['id_cat_opt']] = $v['val_digital'];
			}
		}
		
		//Data options
		
		if(is_array($opts_data))
		{
			foreach($opts_data as $v)
			{
				if($v['id_ad'] == $_GET['id_ad'])
				$infos['data_'. $v['id_cat_opt']] = $v['id_val_data'];
			}
		}

		//Checkbox options

		if(is_array($opts_check))
		{
			foreach($opts_check as $v)
			{
				if($v['id_ad'] == $_GET['id_ad'])
				$infos['check_'. $v['id_val_check']] = $v['id_val_check'];
			}
		}
		
		//Fields
		
		if(is_array($fields))
		{
			foreach($fields as $v)
			{
				$id_field = $v['id_field'];			
				$infos['field_'. $id_field] = $v['value'];
			}
		}

		//Calendar
		
		if(count($_POST) <= 0 && isset($_SESSION['cal_period']))
		unset($_SESSION['cal_period']);
		
		if(count($_POST) <= 0)
		{
			if(is_array($calendar) && !isset($_SESSION['cal_period']))
			{
				foreach($calendar as $v)
				{
					$start_end = date('d/m/Y', $v['start_date']);
					$end_date = date('d/m/Y', $v['end_date']);
					$cal_price = $v['price'];
					
					$_SESSION['cal_period'][] = array($start_end, $end_date, $cal_price);
				}
			}
		}
	}
	else redirect('index.php');
} 
else {
	$ad = null;
	$images = null;
}

///////////////////////////////////
//Initializing variable $error
//////////////////////////////////

$error = '';

///////////////////////////////////
//Validation of data
//////////////////////////////////

if(isset($_POST['title']))
{
	if(empty($_POST['name']))
	$error = 9;
	
	elseif(!valid_email($_POST['email']))
	$error = 6;
	
	elseif(isset($_POST['phone']) && empty($_POST['phone']))
	$error = 12;
	
	elseif(isset($_POST['pas']) && (empty($_POST['pas']) OR $_POST['pas'] != $_POST['pas2']))
	$error = 22;
	
	elseif(empty($_POST['cat']))
	$error = 5;
	
	elseif(isset($_POST['comp_name']) && empty($_POST['comp_name']) && $_POST['status'] == 2)
	$error = 7;
	
	elseif(isset($_POST['comp_num']) && empty($_POST['comp_num']) && $_POST['status'] == 2)
	$error = 8;
	
	elseif(empty($_POST['title']))
	$error = 1;
	
	elseif(empty($_POST['text']))
	$error = 2;
	
	elseif(!empty($_POST['video']) && !preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $_POST['video']) && !preg_match('%(?:dailymotion\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|video/|.*[?&]v=)|dai\.ly/)([^"&?/ ]*)%i', $_POST['video'], $matches) && !preg_match('%(?:vimeo\.com/(?:[^/]+/.+/|(?:video/)?|.*[?&]v=))([^"&?/ ]*)%i', $_POST['video'], $matches) && !preg_match("%(?:yahoo\.com/video/)%i", $_POST['video']))
	$error = 23;
	
	elseif($cache_param_fields['act_price'] == 1 && !empty($_POST['price']) && (preg_match("#^[0-9]+?[,.]?([0-9]+)?$#", $_POST['price']) != true))
	$error = 20;
	
	elseif(empty($_POST['reg']))
	$error = 3;
	
	elseif(isset($_POST['county']) && empty($_POST['county']))
	$error = 4;
	
	elseif(isset($_POST['city']) && empty($_POST['city']))
	$error = 11;
	
	elseif(isset($_POST['postcode']) && empty($_POST['postcode']))
	$error = 10;
	
	elseif(isset($_POST['top']) && preg_match("#^[0-9]+$#", $_POST['top_days']) != true)
	$error = 13;
	
	elseif(isset($_POST['premimum']) && preg_match("#^[0-9]+$#", $_POST['premium_days']) != true)
	$error = 14;
	
	elseif(isset($_POST['urgent']) && preg_match("#^[0-9]+$#", $_POST['urgent_days']) != true)
	$error = 15;
	
	elseif(isset($_POST['framed']) && preg_match("#^[0-9]+$#", $_POST['framed_days']) != true)
	$error = 16;
	
	elseif(isset($_POST['top_time']) && preg_match("#^[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]$#", $_POST['top_time']) != true)
	$error = 17;
	
	elseif(isset($_POST['urgent_time']) && preg_match("#^[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]$#", $_POST['urgent_time']) != true)
	$error = 17;
	
	elseif(isset($_POST['framed_time']) && preg_match("#^[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]$#", $_POST['framed_time']) != true)
	$error = 17;
	
	elseif(isset($_POST['premium_time']) && preg_match("#^[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]$#", $_POST['premium_time']) != true)
	$error = 17;
	
	elseif($param_api_gg['activ_gg_map'] == 1 && (empty($_POST['lat']) || empty($_POST['lng'])))
	$error = 21;
	
	else {
		$errors_module = pool::execute('spValidFormAd', $error);
		if(!empty(array_filter($errors_module))) {
			$error = ['module' => $errors_module];
		}
	}
	
	if(empty($error))
	{
		foreach($cache_search_digital as $row)
		{
			$id_cat = (int) $row['id_cat'];
			$id_cat_opt_digital = (int) $row['id_cat_opt'];
			
			foreach($cache_cat_opts as $opt)
			{
				$id_cat_opt = (int) $opt['id_cat_opt'];
				$type_cat_opt = (int) $opt['type_cat_opt'];
				$optional = 0;
				
				if($type_cat_opt != 1)
				continue;
				
				if($id_cat_opt_digital == $id_cat_opt)
				{
					$optional = (int) $opt['optional'];
					break;
				}
			}
		 
			if($optional == 0 && $id_cat == $_POST['cat'])
			{
				if(empty($_POST['digital_'. $id_cat_opt_digital]) || !is_numeric($_POST['digital_'. $id_cat_opt_digital]))
				$error = 24;
			}
			elseif(!empty($_POST['digital_'. $id_cat_opt_digital]) && !is_numeric($_POST['digital_'. $id_cat_opt_digital]))
			$error = 24;
		}
				
		foreach($cache_val_data as $row)
		{
			$id_cat_opt_data = (int) $row['id_cat_opt'];
			
			foreach($cache_cat_opts as $opt)
			{
				$id_cat_opt = (int) $opt['id_cat_opt'];
				$type_cat_opt = (int) $opt['type_cat_opt'];
				$optional = 0;
				
				if($type_cat_opt != 2)
				continue;
				
				if($id_cat_opt_data == $id_cat_opt)
				{
					$optional = (int) $opt['optional'];
					break;
				}
			}
		 
			if($optional == 0)
			{
				if(isset($_POST['data_'. $id_cat_opt_data]) && empty($_POST['data_'. $id_cat_opt_data]))
				$error = 18;
			}
		}
		
		foreach($cache_fields as $row)
		{
			$id_field = (int) $row['id_field'];
			$id_cat = (int) $row['id_cat'];
			$type = (int) $row['type'];
			$optional = $row['optional'];
			
			if($optional == 1)
			{
				if($type == 1 && empty($_POST['field_'. $id_field]) && ($id_cat == 0 || $id_cat == $_POST['cat']))
				$error = 19;
				
				elseif($type == 2 && (empty($_POST['field_'. $id_field]) || preg_match("#^[0-9]+$#", $_POST['field_'. $id_field]) != true) && ($id_cat == 0 || $id_cat == $_POST['cat']))
				$error = 19;
				
				elseif($type == 3 && (empty($_POST['field_'. $id_field]) || preg_match("#^https?://#", $_POST['field_'. $id_field]) != true) && ($id_cat == 0 || $id_cat == $_POST['cat']))
				$error = 19;
			}
			else
			{
				if($type == 2 && !empty($_POST['field_'. $id_field]) && preg_match("#^[0-9]+$#", $_POST['field_'. $id_field]) != true && ($id_cat == 0 || $id_cat == $_POST['cat']))
				$error = 19;
				
				elseif($type == 3 && !empty($_POST['field_'. $id_field]) && preg_match("#^https?://#", $_POST['field_'. $id_field]) != true && ($id_cat == 0 || $id_cat == $_POST['cat']))
				$error = 19;
			}
		}
	}
	if(!empty($error)) {
		if(isset($_POST['photo']) || isset($_POST['remove_photo'])) {
			$images = isset($_POST['photo']) ? $_POST['photo'] : array();
			$new_images = array();
			foreach($images as $key => $img) $new_images[$key] = is_array($img) ? $img : (!preg_match('#^(https|http)?://#', $img) ? str_replace('/upload/photos/', '', $img) : $img);
			$images = $new_images;	
		}
		if(isset($_POST['file']) || isset($_POST['remove_file'])) {
			$files = isset($_POST['file']) ? $_POST['file'] : array();
			$new_files = array();
			$keys = array();
			foreach($files as $key => $file) $new_files[$key] = is_array($file) ? $file : str_replace('/upload/files/', '', $file);
			$files = $new_files;
	
		}
	}
}

///////////////////////////////////
//Update ad or add ad
//////////////////////////////////

if($act == 1 || $act == 9)
{	
	if($act == 1) {
		$id_ad = (int) $ad['id_ad'];
		$id_reg = (isset($_POST['reg'])) ? (int) $_POST['reg'] : (int) $ad['id_reg'];
		$id_county = (isset($_POST['county'])) ? (int) $_POST['county'] : (int) $ad['id_county'];
		$id_cat = (isset($_POST['cat'])) ? (int) $_POST['cat'] : (int) $ad['id_cat'];
		$email = stripslashes(htmlspecialchars($ad['email'], ENT_QUOTES));
		$postcode = stripslashes(htmlspecialchars($ad['postcode'], ENT_QUOTES));
		$city = stripslashes(htmlspecialchars($ad['city'], ENT_QUOTES));
		$state = (int) $ad['state'];
		$type = (int) $ad['type'];
		$status = (int) $ad['status'];
		$name = stripslashes(htmlspecialchars($ad['name'], ENT_QUOTES));
		$phone = stripslashes(htmlspecialchars($ad['phone'], ENT_QUOTES));
		$title = stripslashes(htmlspecialchars($ad['title'], ENT_QUOTES));
		$text = stripslashes(htmlspecialchars($ad['text'], ENT_QUOTES));
		$price = (float) $ad['price'];
		$lat = $ad['lat'];
		$lng = $ad['lng'];
		$phone_hidden = (int) $ad['phone_hidden'];
		$top = (int) $ad['top'];
		$top_days = (int) $ad['top_days'];
		$top_time = (int) $ad['top_time'];
		$top_time = date('d/m/Y', $top_time);
		$urgent = (int) $ad['urgent'];
		$urgent_days = (int) $ad['urgent_days'];
		$urgent_time = (int) $ad['urgent_time'];
		$urgent_time = date('d/m/Y', $urgent_time);
		$framed = (int) $ad['framed'];
		$framed_days = (int) $ad['framed_days'];
		$framed_time = (int) $ad['framed_time'];
		$framed_time = date('d/m/Y', $framed_time);
		$premium = (int) $ad['premium'];
		$premium_days = (int) $ad['premium_days'];
		$premium_time = (int) $ad['premium_time'];
		$premium_time = date('d/m/Y', $premium_time);
		$comp_name = stripslashes(htmlspecialchars($ad['comp_name'], ENT_QUOTES));
		$comp_num = stripslashes(htmlspecialchars($ad['comp_num'], ENT_QUOTES));
		$video = stripslashes(htmlspecialchars($ad['video'], ENT_QUOTES));
		$video_pack = (int) $ad['video_num'];
		$photo_pack = (int) $ad['pictures_pack'];
		$pas = isset($_POST['pas']) ? $_POST['pas'] : htmlspecialchars($ad['password'], ENT_QUOTES);
		$pas2 = isset($_POST['pas2']) ? $_POST['pas2'] : htmlspecialchars($ad['password'], ENT_QUOTES);
	}
	else
	{
		$id_cat = isset($_POST['cat']) ? $_POST['cat'] : 0;
		$id_reg = isset($_POST['reg']) ? $_POST['reg'] : 0;
		$id_county = isset($_POST['county']) ? $_POST['county'] : 0;
		$top_time = date('d/m/Y', 0);
		$urgent_time = date('d/m/Y', 0);
		$framed_time = date('d/m/Y', 0);
		$premium_time = date('d/m/Y', 0);
	}
	
	if(empty($error) && isset($_POST['photo']) || isset($_POST['remove_photo'])) {
		$images = isset($_POST['photo']) ? $_POST['photo'] : array();
		if(isset($_POST['photo']) && !empty($_POST['photo'])) {
			$upload = new uploadManager();
			$images = $upload->moveFile(array('move_file' => $_POST['photo'], 'context' => 'ad_photo'));
		}
		if(isset($_POST['remove_photo']) && !empty($_POST['remove_photo'])) {
			foreach($_POST['remove_photo'] as $rfile) {
				if(strlen($rfile) > 0) @unlink(__DIR__.'/..'.str_replace(URL, '', $rfile));
			}
		}
		$new_images = array();
		foreach($images as $elt => $img) {
			if(strpos($img, '/upload/photos/') > -1) $new_images[] = !preg_match('#^(https|http)?://#', $img) ? str_replace('/upload/photos/', '', $img) : $img;
			else continue;
		}
		$images = $new_images;
	}
	
	if(empty($error) && isset($_POST['file']) || isset($_POST['remove_file'])) {
		$files = isset($_POST['file']) ? $_POST['file'] : array();
		if(isset($_POST['file']) && !empty($_POST['file'])) {
			$upload = new uploadManager();
			$files = $upload->moveFile(array('move_file' => $_POST['file'], 'context' => 'ad_file'));
		}
		if(isset($_POST['remove_file']) && !empty($_POST['remove_file'])) {
			foreach($_POST['remove_file'] as $rfile) {
				if(strlen($rfile) > 0) @unlink(__DIR__.'/..'.str_replace((DIRECTORY_SEPARATOR == '/' ? '\\' : '/'), DIRECTORY_SEPARATOR, str_replace(URL, '', $rfile)));
			}
		}
		$new_files = array();
		foreach($files as $file) $new_files[] = str_replace('/upload/files/', '', $file);
		$files = $new_files;
	}

	$display = 0;
	$upload_doc = 0;
	
	foreach($cache_counties as $row)
	{
		if(isset($id_reg) && $row['par_reg'] == $id_reg)
		$display = 1;
	}
	
	if(isset($_POST['title']) && empty($error))
	{
		$photo_pack = isset($_POST['pictures_pack']) ? 1 : 0;
		
		if(isset($_POST['photo']) && !empty($_POST['photo']))
		{
			if(sizeof($_POST['photo']) > $cache_photo_opt['nb_photo_free'])
			{
				$_POST['pictures_pack'] = 1;
			}
		}
		
		if($act == 1)
		{
			update_ad($_POST, $id_ad, $state, $photo_pack, null, $images, $files, 'back');
			get_nb_ads();
			
			unset($_SESSION['file']);
			unset($_SESSION['cal_period']);
			
			redirect('ads_management.php?id_ad='. $id_ad .'&act=1&succes=1&menu=6');
		}
		else
		{
			$id_ad = register_ad($_POST, $_POST['pas'], 2, $photo_pack, $images, $files, 0, 'back');
			get_nb_ads();
		
			redirect('ads_management.php?id_ad='. $id_ad .'&act=1&succes=2&menu=6');
		}
		
	}
}

///////////////////////////////////
//Remove expired ads
//////////////////////////////////

if($act == 4 && !isset($_GET['actualized']))
{	
	remove_expired_ads();
	get_nb_ads();
	
	redirect('ads_management.php?act=4&actualized=1&menu=6');
}

///////////////////////////////////
//Delete rejected ads
//////////////////////////////////

if($act == 5 && !isset($_GET['actualized']))
{	
	delete_rejected_ads();
	redirect('ads_management.php?act=5&actualized=1&menu=6');
}

///////////////////////////////////
//Delete unconfirmed ads
//////////////////////////////////

if($act == 6 && !isset($_GET['actualized']))
{	
	delete_unconfirmed_ads();
	redirect('ads_management.php?act=6&actualized=1&menu=6');
}

///////////////////////////////////
//Delete deleteted ads
//////////////////////////////////

if($act == 7 && !isset($_GET['actualized']))
{	
	delete_deleted_ads();
	redirect('ads_management.php?act=7&actualized=1&menu=6');
}

///////////////////////////////////
//Delete expired ads
//////////////////////////////////

if($act == 8 && !isset($_GET['actualized']))
{	
	delete_expired_ads();
	redirect('ads_management.php?act=8&actualized=1&menu=6');
}

///////////////////////////////////
//Display view
//////////////////////////////////

pool::execute('beforestart');

$view = array();

if($act == 1 || $act == 9)
$view[] = 'template_adm/tpl_ads_management_mod_adm.php';

elseif(($act >= 4 && $act <= 8) || isset($state))
$view[] = 'template_adm/tpl_ads_management_adm.php';

require_once __DIR__.'/template_adm/tpl_layout_adm.php';

pool::execute('postdispatch');

///////////////////////////////////
//Close database connexion
//////////////////////////////////

$conn = null;