<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<?php
function check_is_login()
{
	$my_obj=& get_instance();
	$user_name = $my_obj->session->userdata("shop_sys_user_name");
	$md5_encode_str = $my_obj->session->userdata("shop_sys_md5_encode_str");
	$md5_encode_key = $my_obj->config->item('md5_encode_key');
	if( (! $user_name) ||  (! $md5_encode_str) ||  ($md5_encode_str != md5($user_name.$md5_encode_key)))
	{
		echo_msg('<li>登陆超时，或者您还没登陆</li>',site_url(CTL_FOLDER.'login'),'no','_top');
	}
}

function send_email($to,$sb,$mg)
{
	$my_obj=& get_instance();
	$my_obj->config->load('shop_email_config', FALSE, TRUE);
	$my_obj->config->load('shop_site_config', FALSE, TRUE);
	$config['smtp_host'] = $my_obj->config->item('smtp_host');
	$config['smtp_user'] = $my_obj->config->item('smtp_user');
	$config['smtp_pass'] = $my_obj->config->item('smtp_pass');
	$config['protocol'] = 'smtp';
	$config['charset'] = 'utf-8';
	$config['mailtype'] = 'html';
	$config['wordwrap'] = TRUE;
	
	$my_obj->load->library('email',$config);
	$my_obj->email->from($my_obj->config->item('smtp_email'), $my_obj->config->item('sys_site_name'));
	$my_obj->email->to($to); 
	
	$my_obj->email->subject($sb);
	$my_obj->email->message($mg); 
	if($my_obj->email->send())
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function replace_email_template($document)
{
$document = trim($document);
if (strlen($document) <= 0)
{
  return $document;
}  
$my_obj=& get_instance();
$my_obj->config->load('shop_site_config',FALSE,TRUE);
$search = array ("/\{tpl_site_name\}/i",
                  "/\{tpl_send_time\}/i",
                  "/\{tpl_admin_login_url\}/i", 
				  "/\{tpl_site_url\}/i",
				  "/\{tpl_user_login_url\}/i"
        );

$replace = array (
	$my_obj->config->item('sys_site_name'), 
	date("Y-m-d H:i:s",time()),
	site_url('shop_admin/login'),
	base_url(),
	site_url(CTL_FOLDER.'user/login')
);
return @preg_replace ($search, $replace, $document);
}

function get_market_price($p)
{
	if( ! is_numeric($p)) return '';
	$my_obj=& get_instance();
	$my_obj->load->config('top_config');
	$mp = $my_obj->config->item('market_price');
	foreach($mp as $k=>$v)
	{
		$tp = explode('-',$k);
		if(isset($tp[0]) && $tp[0] < $p && isset($tp[1]) && ($tp[1] >= $p || $tp[1] == 0))
		{
			return round($p * $v,2);
		}
	}
}

function get_level_img($l)
{
	if( ! is_numeric($l)) return '';
	$my_obj=& get_instance();
	$my_obj->load->config('top_config');
	$crd = $my_obj->config->item('credit');
	foreach($crd as $v)
	{
		if($v['level'] == $l)
		{
			return '<img src="'.$v['img'].'" alt="'.$v['name'].'" align="absmiddle" />';
		}
	}
	return '';
}
?>
