<?php
function deslash(&$rValue, $force = false) {
    if (!$force && !(get_magic_quotes_runtime() || get_magic_quotes_gpc())) {
        return;
    }
    if ($value === null) return;

    switch (gettype($rValue)) {
        case 'array': {
            $newArray = array ();
            foreach ($rValue as $k => $v) {
                deslash($k, $force);
                deslash($v, $force);
                $newArray[$k] = $v;
            }
            $rValue = $newArray;
            break;
        }
        case 'object': {
            foreach ((array)$rValue as $k => $v) {
                deslash($rValue->$k, $force);
            }
            break;
        }
        default:
            $rValue = stripslashes($rValue);
            break;
    }
}

function FormGet($value){
    if (isset($_POST[$value])) {
        $get_value=$_POST[$value];
        deslash($get_value);
    }
    elseif (isset($_GET[$value])) {
        $get_value=$_GET[$value];
        deslash($get_value);
    }
    elseif (isset($_SESSION[$value])) {
        $get_value=$_SESSION[$value];
    }
    else {
        $get_value=null;
    }
    return $get_value;
}

function dump($var, $caption = "") {
    ob_start();
    print_r($var);
    $content = ob_get_contents();
    ob_end_clean();
    echo htmlentities($caption)."<pre style='font-size: 8pt;'>".htmlentities($content)."</pre>";
}

function isUrl($str) {
    return preg_match("`^http://((\w+)\.)+\w+`", $str);
}

function isEmpty($str) {
    return preg_match("`^\s*$`", $str);
}

function isEmail($str) {
    return preg_match("`^[^@]+ @ [^.]+ (\. [^.]+)+$`x", $str);
}

function isFloat($str) {
    return preg_match("`^(\+|-)? \d+ (\. \d+)?$`x", $str);
}

function isInt($str) {
    return preg_match("`^(\+|-)? \d+$`x", $str);
}

function getPageTemplate($name){
    $language =& Language::GetInstance();
    $pagetemplate =& PTemplateManager::getInstance();
    $p_template = $pagetemplate->Get($name);
    return $p_template->Parse("",$language->LangID);
}

function getTemplateByName($name,$data,$lang_ID){
    $mailtemplate =& MTemplateManager::getInstance();
    $m_template = $mailtemplate->Get($name);
    return array($m_template->type,$m_template->Parse($data,$lang_ID));
}

function getDefaultLanguage($mem_id){
    $db =& DB::GetInstance();
    $mem_id = $db->escape($mem_id);
    $query="SELECT lang_id FROM members WHERE mem_userid='$mem_id'";
    return $db->get_var($query);
}

function send_mail  ($to_address,  $from_address,  $subject,  $message, $type, $on_switch, $send_type='outside',$charset='ISO 8859-1')  {

GLOBAL $Sess_UserId;
GLOBAL $Sess_UserName;

    if ($send_type == 'inside' || $send_type == 'inside_hidden'){

        $res = mysql_query("SELECT * FROM members WHERE mem_email='$to_address'");
        echo mysql_error();
        if ($receiver = mysql_fetch_assoc($res)){

	        $tempdate=date("Y/m/d");
	        $message = mysql_escape_string($message);
	        $title = mysql_escape_string($title);

	        mysql_query("INSERT INTO messages SET
	                msg_senderid    = $Sess_UserId,
	                msg_receiverid  = $receiver[mem_userid],
	                msg_senderhandle= '$Sess_UserName',
	                msg_title   = '$subject',
	                msg_text    = '$message',
	                msg_dateadded = '$tempdate',
	                msg_read = 'U'".(($send_type == 'inside_hidden')?", msg_senderdel = 'Y'":""));
        }
    }elseif($send_type == 'outside'){
            if ($type == "html" || $type == "text/html") {
                $headers = "MIME-Version: 1.0\n";
                $headers .= "Content-Type: text/html; charset=$charset\n";
                $headers .= "Content-Transfer-Encoding: 8bit\n";
                $headers .= "From: $from_address\n";
                $headers .= "Reply-To: $from_address\n";
            } else {
                $headers = "MIME-Version: 1.0\n";
                $headers = "Content-Type: text/plain; charset=$charset\n";
                $headers .= "From: $from_address\n";
                $headers .= "Reply-To: $from_address\n";
            }

            if ($on_switch=='ON')
            {
                $message = stripslashes($message);
                ini_set("sendmail_from",$CONST_MAIL);
                mail($to_address,$subject,$message,$headers);
            }
    }
}
function redirect($url) {
    header("Location: $url");
    exit;
}

function clearCrop($path){
    $result= array();
	if ($dir = @opendir($path)) {
	  while (($file = readdir($dir)) !== false) {
          if (preg_match("/^crop_|^temp_/",$file))
            $result["$path/$file"] = lstat("$path/$file");
	  }
	  closedir($dir);
	} else {
        die("Can't open crop dir [$path]");
	}
    foreach ($result as $path => $info) {
        if (time()-3600*2 > $info[10] )
            unlink($path);
    }
}

?>