<?php
function file_check($path)
{
    if (file_exists($path))
    {
        return $path;
    }
    else
    {
        return "modules/Error/404.php";
    }
    return "modules/Welcome/Index.php";
}
//
function page_timer()
{
    $mtime = explode(" ", microtime());
    $msec = (double) $mtime[0];
    $sec = (double) $mtime[1];
    return $sec + $msec;
}
//
function rand_letters($len = 6)
{
    $randstr = '';
    $chars = explode(",", "Q,W,E,R,T,Y,U,P,K,J,H,G,F,D,A,Z,X,V,B,N,M");
    for ($rand = 1; $rand <= $len; $rand++)
    {
        $random = rand(0, count($chars) - 1);
        $randstr .= $chars[$random];
    }
    return $randstr;
}
//
function add_codes($name)
{
    $replace = ['.', '\'', '"', '@', '_'];
    $name = strtoupper($name);
    $name = str_replace($replace, '', $name);
    $chars = explode(",", "Q,1,2,3,A,4,M,K,Y,B,C,W,5,6,L,X,U,T,H,D,7,8,9,S");
    $randstr = substr($name, 0, 6);
    for ($rand = 1; $rand <= 10; $rand++)
    {
        $random = rand(0, count($chars) - 1);
        $randstr .= $chars[$random];
    }
    return $randstr;
}
function settings($name)
{
    global $mysql;
    $names = explode(",", $name);
    for ($x = 0; $x < sizeof($names); $x++)
    {
        $data = $mysql->get_array($mysql->query("value", $mysql->perfix."settings", "`name` = '$names[$x]' limit 1"));
        $return[$names[$x]] = stripslashes($data['value']);
    }
    return $return;
}
//
function arraytostr($array)
{
    $str = '';
    if (is_array($array))
    {
        foreach ($array as $key => $value)
        {
            $str .= $value.",";
        }
    }
    return $str;
}
//
function read_folder($path)
{
    $bad = ['.', '..', 'Thumbs.db'];
    $open = opendir($path);
    while ($fn = readdir($open))
    {
        if (!in_array($fn, $bad))
        {
            $files[] = $fn;
        }
    }
    return $files;
}
//
function timezone($zone, $type, $time)
{
    if ($type == "")
    {
        $type = "+";
    }
    $server_gmt = 10;
    $zone = ($zone == "") ? $server_gmt : $zone;
    $zone = ($type == "-") ? $zone + $server_gmt : $zone - $server_gmt;
    $timeadjust = ($zone * 60 * 60);
    if ($type == "+")
    {
        $t = $time + $timeadjust;
    }
    if ($type == "-")
    {
        $t = $time - $timeadjust;
    }
    return $t;
}
//
function seo_str($str)
{
    $str = ucwords(trim($str));
    $str = strtolower(stripslashes($str));
    $str = preg_replace("([^A-Za-z0-9])", "-", $str);
    $str = preg_replace("(-{2,})", "-", $str);
    $str = str_replace("8218", "", $str);
    $str = preg_replace("(-{2,})", "-", $str);
    return trim($str);
}
//
function current_page()
{
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on")
    {
        $pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80")
    {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    }
    else
    {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}
//get country based on IP address
function ip2Country($ip)
{
    $data = file('https://www.cloudtools.com.au/ip2country/?api_key=38a32d6b40dd8ce7c0c5d23594d8897d&ip='.$ip);
    return trim($data[0]);
}
//spam checker, no longer used
function spamCheck($text)
{
    return false;
}
//checks to see if passed file path is also in customCode folder, if so it includes the customCode version
function fileInclude($file)
{
    $path = $_SERVER['DOCUMENT_ROOT'].'/';
    $i = $path.$file;
    if (file_exists($path.'customCode/'.$file))
    {
        $i = $path.'customCode/'.$file;
    }
    return $i;
}
//send email using php mailer
function emailWithPhpMailer($to, $subject, $message, $replyTo, $html = true, $bcc = '', $fromName = '', $toName = '', $attachPath = [], $useMail = false)
{
    //log the email in the database
    global $mysql;
    $time = time();
    $rt = str_replace(",", "&#8218;", $replyTo);
    $t = str_replace(",", "&#8218;", $to);
    $s = str_replace(",", "&#8218;", $subject);
    $m = str_replace(",", "&#8218;", $message);
    $mysql->insert("fs_email_log", "from,to,subject,message,stamp", addslashes("$rt,$t,$s,$m,$time"));
    //clean up log
    $oneMonth = strtotime("-30 days");
    $mysql->Delete("fs_email_log", "stamp <= $oneMonth");
    $host = trim(str_replace(['demo.', 'www.'], '', $_SERVER['HTTP_HOST']));
    $username = 'mailer@'.$host;
    require_once $_SERVER['DOCUMENT_ROOT'].'/tools/phpmailer/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    if ($html)
    {
        $mail->isHTML(true);
        $mail->AltBody = strip_tags($message);
    }
    else
    {
        $mail->isHTML(false);
        $mail->ContentType = 'text/plain';
    }
    if ($useMail)
    {
        $mail->isMail();
    }
    else
    {
        $mail->SMTPDebug = 0; //set this to 2 for debugging and 0 to disable
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = 'mail.'.$host;
        $mail->Username = $username;
        $mail->Password = 'o9lsyqo6rbpk';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
    }
    $mail->setFrom($username, $fromName);
    if (strstr($to, ','))
    {
        $tos = explode(",", $to);
        for ($x = 0; $x <= sizeof($tos); $x++)
        {
            if (!empty($tos[$x]))
            {
                $mail->addAddress(trim($tos[$x]), $toName);
            }
        }
    }
    else
    {
        $mail->addAddress($to, $toName);
    }
    $mail->addReplyTo($replyTo, $fromName);
    $mail->Subject = $subject;
    $mail->Body = $message;
    if (!empty($bcc))
    {
        $mail->AddBCC($bcc);}
    if (sizeof($attachPath) > 0)
    {
        foreach ($attachPath as $name => $path)
        {
            $mail->addAttachment($path, $name);
        }
    }
    $mail->XMailer = 'freesites.com.au';
    $mail->send();
}
//returns http or https based on current url SSL status
function httpOrHttps()
{
    return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http';
}
