<?php
function checkerr($string)
{
$string = htmlentities(stripslashes(trim($string )));
$string = str_replace("'","",$string );
$string = str_replace(";","",$string );
$string = str_replace("$","",$string );
$string = str_replace("=","",$string );
return $string;
}

function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}

function ValideMail($pMail) {
    if (ereg("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@+([_a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]{2,200}\.[a-zA-Z]{2,6}$", $pMail ) ) {
       return true;
    } else {
	return false;
    }
}
function getIP()
{
   
   if( $_SERVER['HTTP_X_FORWARDED_FOR'] != '' )
   {
      $client_ip =
         ( !empty($_SERVER['REMOTE_ADDR']) ) ?
            $_SERVER['REMOTE_ADDR']
            :
            ( ( !empty($_ENV['REMOTE_ADDR']) ) ?
               $_ENV['REMOTE_ADDR']
               :
               "unknown" );
   
       $entries = split('[, ]', $_SERVER['HTTP_X_FORWARDED_FOR']);
   
      reset($entries);
      while (list(, $entry) = each($entries))
      {
         $entry = trim($entry);
         if ( preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $entry, $ip_list) )
         {
            
            $private_ip = array(
                  '/^0\./',
                  '/^127\.0\.0\.1/',
                  '/^192\.168\..*/',
                  '/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/',
                  '/^10\..*/');
   
            $found_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);
   
            if ($client_ip != $found_ip)
            {
               $client_ip = $found_ip;
               break;
            }
         }
      }
   }
   else
   {
      $client_ip =
         ( !empty($_SERVER['REMOTE_ADDR']) ) ?
            $_SERVER['REMOTE_ADDR']
            :
            ( ( !empty($_ENV['REMOTE_ADDR']) ) ?
               $_ENV['REMOTE_ADDR']
               :
               "unknown" );
   }
   
   return $client_ip;
   
}


function user_info($field='') {

if(empty($field))
return false;
$accepted = array('username', 'user_password');
if(!in_array($field, $accepted))
return false;
$result = mysql_query("SELECT ". $field ." FROM Accounts_Classic_Publishers WHERE ID = ". $_SESSION['member_ID'] .";");
if(1 != mysql_num_rows($result)) :
return false;
else :
$row = mysql_fetch_assoc($result);
return $row[$field];
endif;
}

function validLink($link) {

$urlregex = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
if (eregi($urlregex, $link)) { return true; } else {
        return false; }



}


function time_to_sec($time) { 
    $hours = substr($time, 0, -6); 
    $minutes = substr($time, -5, 2); 
    $seconds = substr($time, -2); 

    return $hours * 3600 + $minutes * 60 + $seconds; 
} 

function sec_to_time($seconds) { 
    $hours = floor($seconds / 3600); 
    $minutes = floor($seconds % 3600 / 60); 
    $seconds = $seconds % 60; 

    return sprintf("%d:%02d:%02d", $hours, $minutes, $seconds); 
} 

function random_generator($digits){
srand ((double) microtime() * 10000000);

$input = array ("A", "B", "C", "D", "E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z");

$random_generator="";
for($i=1;$i<$digits+1;$i++){

if(rand(1,2) == 1){
 
$rand_index = array_rand($input);
$random_generator .=$input[$rand_index];

}else{


$random_generator .=rand(1,10);
} 

}

return $random_generator; 
}

function sendemail($emailS, $messageS) {
   													//This should be done in your php.ini, but this is how to do it if you don't have access to that
															date_default_timezone_set('Etc/UTC');
							
														//Create a new PHPMailer instance
															$mail = new PHPMailer();
														//Tell PHPMailer to use SMTP
															//$mail->isSMTP();
														//Enable SMTP debugging
														// 0 = off (for production use)
														// 1 = client messages
														// 2 = client and server messages
															$mail->SMTPDebug = 2;
														//Ask for HTML-friendly debug output
															$mail->Debugoutput = 'html';
														//Set the hostname of the mail server
															$mail->Host = "$smtphost";
														//Set the SMTP port number - likely to be 25, 465 or 587
															$mail->Port = "$smtpport";
														//Whether to use SMTP authentication
															$mail->SMTPAuth = "$smtpauth";
														//Username to use for SMTP authentication
															$mail->Username = "$smtpusername";
														//Password to use for SMTP authentication
															$mail->Password = "$smtppass";
														//Set who the message is to be sent from
															$mail->setFrom("$frommail", "$title");
														//Set an alternative reply-to address
															$mail->addReplyTo("$replytoemail", "$title");
														//Set who the message is to be sent to
															$mail->addAddress("jozefvojtas5198@gmail.com", "$title");
														//Set the subject line
															$mail->Subject = "$title | Shortening service";
														//Set the HTML body
															$mail->isHTML(true);  
														//Read an HTML message body from an external file, convert referenced images to embedded,
															$mail->Body = "Test";
														//Replace the plain text body with one created manually
															$mail->AltBody = "$title";
							
														//send the message, check for errors
															if (!$mail->send()) {
																$success = 0;
																$error = 1;
																$errortext = 'Error: #45679 - Bad connection/authorization - Email not sent';
															} else {
																$success = 1;
																$error = 0;
															}
}
?>
