<?php

function days_to_project($date_reg) {
	return round((time() - $date_reg) / 86400);
}

function setLogs($infolog, $pricelog = 0)
{
	global $db, $member_id;
	$time = time();
	if($infolog) {
		$db->query("UPDATE `dle_users` SET `lastdate`='{$time}' WHERE `name`='{$member_id['name']}'");
		$db->query("INSERT INTO `cabinet_logs` VALUES (NULL, '{$member_id['name']}', '{$infolog}', '{$pricelog}', '{$time}')");
		$db->close();
	} else return false;
}

function ajax_html($type, $error, $add = array())
{
	$errors['type'] = $type;
	$errors['text'] = $error;

	foreach ($add as $key => $value) {
		$errors[$key] = $value;
	}

	die(html_entity_decode(json_encode( $errors )));
}

function safedArray($source)
{
	global $db;
	$safedArray = [];
	if(count($source) != 0) {
		foreach($source as $key => $value) {
			$safedArray[$key] = $db->safesql($value);
		}
	}
	return $safedArray;
}

function checkArray($array)
{
	if(!$array) return false;

	foreach($array as $key => $value) {
		$array['P_PHONE'] = true;
		$array['Fee'] = true;
		$array['g-recaptcha-response'] = true;
		if(empty($array[$key])) return false;
	}
	
	return true;
}

function strip_data($text)
{
	$quotes = array ( "\x60", "\t", "\n", "\r", ".", ",", ";", ":", "&", "(", ")", "[", "]", "{", "}", "=", "*", "^", "%", "$", "<", ">", "+", "-" );
	$goodquotes = array ("#", "'", '"' );
	$repquotes = array ("\#", "\'", '\"' );
	$text = stripslashes( $text );
	$text = trim( strip_tags( $text ) );
	$text = str_replace( $quotes, ' ', $text );
	$text = str_replace( $goodquotes, $repquotes, $text );
	return $text;
}

function pre_ending($n, $n1, $n2, $n5)
{
    if($n >= 11 and $n <= 19) return $n5;
    $n = $n % 10;
    if($n == 1) return $n1;
    if($n >= 2 and $n <= 4) return $n2;
    return $n5;
}

function create_gift($temp)
{
	$template = $temp;
	$k = strlen($template);
	$sernum = '';
	for ($i=0; $i < $k; $i++)
	{
		switch($template[$i])
		{
			case 'X': $sernum .= chr(rand(65,90)); break;
			case '9': $sernum .= rand(0,9); break;
			case '-': $sernum .= '-';  break;
		}
	}
	return $sernum;
}

?>
