<?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';

///////////////////////////////////
//Check if super admin is logged
//////////////////////////////////

if(!empty($_SESSION['valid_admin']))
$valid_admin = $_SESSION['valid_admin'];

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

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

if(!empty($_GET['act']))
{
	if($_GET['act'] >= 1 && $_GET['act'] <= 5 && $_GET['act'] != 4  && $_GET['act'] != 3)
	$act = (int) $_GET['act'];
	
	else redirect('index.php');
}
else redirect('index.php');

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

$error = '';

///////////////////////////////////
//Delete logo invoice
//////////////////////////////////

if($act == 4 && isset($_GET['img']))
unlink('../template/images/logo_inv.'. $param_invoice['extension']);

///////////////////////////////////
//Check the data
//////////////////////////////////

if(isset($_POST['name']) || isset($_POST['gg_map_key']) || isset($_POST['key_fb']) || isset($_POST['name_comp']))
{
	if($act == 1)
	{
		if(empty($_POST['name']))
		$error = 1;
		
		elseif(empty($_POST['mail_rep']))
		$error = 2;
		
		elseif(!valid_email($_POST['mail_rep']))
		$error = 3;

		elseif(empty($_POST['currency']))
		$error = 4;
	
		elseif(empty($_POST['nb_ads']) || $_POST['nb_ads'] == 0)
		$error = 5;
		
		elseif(empty($_POST['ads_val']) || $_POST['ads_val'] == 0)
		$error = 7;
		
		elseif(!valid_email($_POST['email_notif']))
		$error = 8;
		
		elseif(empty($_POST['nb_flux_gl']) || $_POST['nb_flux_gl'] == 0)
		$error = 9;
		
		elseif(!empty($_POST['facebook_url']) && preg_match('#^https?://#', $_POST['facebook_url']) == false)
		$error = 22;
		
		elseif(!empty($_POST['twitter_url']) && preg_match('#^https?://#', $_POST['twitter_url']) == false)
		$error = 23;
		
		elseif(!empty($_POST['linkedin_url']) && preg_match('#^https?://#', $_POST['linkedin_url']) == false)
		$error = 24;
		
		elseif(!empty($_POST['instagram_url']) && preg_match('#^https?://#', $_POST['instagram_url']) == false)
		$error = 25;
		
		elseif(!empty($_POST['pinterest_url']) && preg_match('#^https?://#', $_POST['pinterest_url']) == false)
		$error = 26;
	}
	
	if($act == 2)
	{
		if(!empty($_POST['activ_gg_map']) && empty($_POST['gg_map_key']))
		$error = 11;
		
		elseif(!empty($_POST['activ_gg_recaptcha']) && (empty($_POST['gg_recaptcha_site_key']) || empty($_POST['gg_recaptcha_secret_key'])))
		$error = 27;
	}
	
	if($act == 3)
	{
		if(empty($_POST['id_fb']) || empty($_POST['key_fb']))
		$error = 12;
	}
	
	if($act == 4)
	{
		if(!empty($_POST['auto_invoice']) && empty($_POST['name_comp']))
		$error = 13;
		
		elseif(!empty($_POST['auto_invoice']) && empty($_POST['add_comp']))
		$error = 14;
		
		elseif(!empty($_POST['auto_invoice']) && empty($_POST['cod_comp']))
		$error = 15;
		
		elseif(!empty($_POST['auto_invoice']) && empty($_POST['city_comp']))
		$error = 16;
		
		elseif(!empty($_POST['auto_invoice']) && empty($_POST['num_comp']))
		$error = 17;
		
		if(file_exists('../template/images/logo_inv.'. $param_invoice['extension']))
		$extension_downloaded = $param_invoice['extension'];
		
		else $extension_downloaded = '';
		
		if(empty($error))
		{
			if(isset($_FILES['logo_inv']) && $_FILES['logo_inv']['error'] == 0)
			{
				if($_FILES['logo_inv']['size'] <= 1073741824*3)
				{
					$extensions_allowed = array('jpg', 'jpeg', 'gif', 'png');
					$extension_downloaded = strtolower(substr(strrchr($_FILES['logo_inv']['name'], '.'), 1) );
					
					if(in_array($extension_downloaded, $extensions_allowed))
					{
						$picture_name = 'logo_inv.'. $extension_downloaded;
						
						move_uploaded_file($_FILES['logo_inv']['tmp_name'], '../template/images/' . basename($picture_name));
					}
					else $error = 19;
				}
				else $error = 20;
			}
		}
	}
}
elseif(count($_POST) > 0 && $act == 5 && !isset($_POST['lang']))
{
	foreach($cache_lang as $v)
	{
		$code_lang = stripslashes(htmlspecialchars($v['code_lang'], ENT_QUOTES));
		$active = (int) $v['active'];
		
		if($active == 1 && (empty($_POST['title_'. $code_lang]) || empty($_POST['desc_'. $code_lang])))
		$error = 18;
	}
}

///////////////////////////////////
//Creating caches and including html functions
//////////////////////////////////

if((isset($_POST['name']) || isset($_POST['gg_recaptcha_site_key']) || isset($_POST['key_fb']) || isset($_POST['name_comp']) || isset($_POST['server'])) && empty($error))
{
	if($act == 1)
	create_cache_settings($_POST);
	
	if($act == 2)
	create_cache_api_google($_POST);
	
	$error = 'succes';
}
elseif(count($_POST) > 0 && $act == 5 && empty($error) && !isset($_POST['lang']))
{
	create_cache_meta($_POST);
	$error = 'succes';
}

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

$conn = null;

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

$view = array();
$view[] = 'template_adm/tpl_settings_adm.php';

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