1
0
mirror of https://e.coding.net/circlecloud/McAccount.git synced 2025-09-05 07:27:21 +00:00

init project...

This commit is contained in:
502647092
2015-10-13 14:26:47 +08:00
commit 69288bd8dc
137 changed files with 43573 additions and 0 deletions

446
Amysql/Amysql.php Normal file
View File

@ -0,0 +1,446 @@
<?php
/************************************************
*
* Amysql PHPMVC - AMP 1.5
* Amysql.com
* @param Object $AmysqlProcess 网站进程
*
*/
class Amysql {
public $AmysqlProcess;
public function Amysql() {
global $Config;
ini_set ( "magic_quotes_runtime", false );
($Config ['DebugPhp'] && error_reporting ( E_ALL )) || error_reporting ( 0 ); // 是否报错
($Config ['SessionStart'] && session_start ()); // SESSION
(! empty ( $Config ['CharSet'] ) && header ( 'Content-type: text/html;charset=' . $Config ['CharSet'] ));
}
static public function AmysqlNotice($notice) {
$AmysqlController = new AmysqlController ();
$AmysqlController->notice = $notice;
$AmysqlController->_view ( 'AmysqlNotice' );
exit ();
}
static public function filter(&$array, $function) {
if (! is_array ( $array ))
Return $array = $function ( $array );
foreach ( $array as $key => $value )
(is_array ( $value ) && $array [$key] = Amysql::filter ( $value, $function )) || $array [$key] = $function ( $value );
Return $array;
}
}
/**
* **********************************************
*
* 总进程对象
*
* @param Object $ControllerName
* @param string $ControllerName
* @param string $ActionName
* @param string $ControllerFile
*
*/
class AmysqlProcess {
public $AmysqlController;
public $ControllerName;
public $ActionName;
public $ControllerFile;
function ProcessStart() {
global $Config;
if ($Config ['HttpPath']) {
$GETParam = (strlen ( $_SERVER ['REQUEST_URI'] ) > strlen ( $_SERVER ['SCRIPT_NAME'] )) ? explode ( '/', trim ( str_ireplace ( $_SERVER ['SCRIPT_NAME'], '', $_SERVER ['REQUEST_URI'] ), '/' ) ) : '';
$GETCount = count ( $GETParam );
if ($GETCount > 1)
for($i = 2; $i < $GETCount; ++ $i)
$_GET [$GETParam [$i]] = isset ( $GETParam [++ $i] ) ? $GETParam [$i] : '';
}
$magic_quotes = function_exists ( 'get_magic_quotes_gpc' ) ? get_magic_quotes_gpc () : false; // 环境是否有过滤
if ($Config ['Filter']) // 开启过滤
{
(! $magic_quotes && (Amysql::filter ( $_GET, 'addslashes' ) && Amysql::filter ( $_REQUEST, 'addslashes' ) && Amysql::filter ( $_COOKIE, 'addslashes' ) && Amysql::filter ( $_FILES, 'addslashes' )));
} else {
($magic_quotes && (Amysql::filter ( $_GET, 'stripslashes' ) && Amysql::filter ( $_REQUEST, 'stripslashes' ) && Amysql::filter ( $_COOKIE, 'stripslashes' ) && Amysql::filter ( $_FILES, 'stripslashes' )));
}
$this->ControllerName = ! empty ( $GETParam [0] ) ? $GETParam [0] : ((isset ( $_GET [$Config ['UrlControllerName']] ) && ! empty ( $_GET [$Config ['UrlControllerName']] )) ? $_GET [$Config ['UrlControllerName']] : 'index');
$this->ControllerName = str_replace ( _PathTag, DIRECTORY_SEPARATOR, $this->ControllerName );
$this->ActionName = ! empty ( $GETParam [1] ) ? $GETParam [1] : ((isset ( $_GET [$Config ['UrlActionName']] ) && ! empty ( $_GET [$Config ['UrlActionName']] )) ? $_GET [$Config ['UrlActionName']] : 'IndexAction');
$this->ControllerFile = _Controller . $this->ControllerName . '.php';
}
function ControllerStart() {
((is_file ( $this->ControllerFile ) && include_once ($this->ControllerFile)) || (is_file ( _Controller . 'index.php' ) && include_once (_Controller . 'index.php')) || Amysql::AmysqlNotice ( $this->ControllerFile . ' 控制器文件不存在' ));
(class_exists ( $this->ControllerName ) || (($this->ControllerName = 'index') && class_exists ( 'index' )) || Amysql::AmysqlNotice ( $this->ControllerName . ' 控制器不存在' ));
$methods = get_class_methods ( $this->ControllerName ); // 获取类中的方法名
(in_array ( $this->ActionName, $methods, true ) || (($this->ActionName = 'IndexAction') && in_array ( $this->ActionName, $methods, true )) || Amysql::AmysqlNotice ( $this->ActionName . ' 方法不存在' ));
$this->AmysqlController = new $this->ControllerName ( $_GET ); // 实例控制器
method_exists ( $this->AmysqlController, 'StartAction' ) ? $this->AmysqlController->StartAction () : '';
$this->AmysqlController->{$this->ActionName} (); // 执行方法
}
}
/**
* **********************************************
*
* 控制器逻辑层 (C)
*
* @param Object $DB
* @param Array $DBConfig
* @param Array $AmysqlModel
* @param Object $AmysqlTemplates
* @param Array $EchoData
*
*/
class AmysqlController {
public $DB;
public $DBConfig; // 控制器调用Mysql的链接配置
public $AmysqlModel; // 控制器关联的数据模型对象集
public $AmysqlClass; // 控制器关联的类对象集
public $AmysqlTemplates; // 控制器关联的模板对象
public $EchoData = array (); // 调试数据
function AmysqlController() {
global $Config;
$this->DBConfig = $Config;
$this->DB = new DB ();
$this->AmysqlTemplates = new AmysqlTemplates ();
}
/**
* 设置数据库配置
*
* @param Array $Config
*/
function _DBSet($_Config = NULL) {
global $Config;
$_Config = ($_Config == NULL) ? $Config : $_Config;
foreach ( $Config as $key => $val )
(empty ( $_Config [$key] ) && $_Config [$key] = $val);
$this->DBConfig = $_Config;
}
/**
* 模板赋值
*
* @param string $name
* @param string $value
*/
public function __set($name, $value = NULL) {
$this->AmysqlTemplates->TemplateValue [$name] = $value;
}
/**
* 数组模板赋值
*
* @param Array $array
*/
public function _array($array) {
if (is_array ( $array )) {
foreach ( $array as $key => $val )
$this->AmysqlTemplates->TemplateValue [$key] = $val;
}
}
/**
* 加载自定义类文件
*
* @param string $file
* @param string $ClassName
* @return Object $ClassName() 类对象
*/
public function _class($file, $ClassName = NULL) {
$file = str_replace ( _PathTag, DIRECTORY_SEPARATOR, $file );
$ClassName = ($ClassName == NULL) ? $file : $ClassName;
$file = _Class . $file . '.php';
if (is_file ( $file )) {
include_once ($file);
if (! class_exists ( $ClassName ))
Amysql::AmysqlNotice ( $ClassName . ' 类对象不存在' );
if (! isset ( $this->AmysqlClass [$ClassName] )) // 不存在类对象
$this->AmysqlClass [$ClassName] = new $ClassName ();
Return $this->AmysqlClass [$ClassName];
}
Amysql::AmysqlNotice ( $file . ' 类文件不存在' );
}
/**
* 加载模型文件
*
* @param string $file
* @param string $ClassName
* @return Object $ClassName() 模型对象
*/
public function _model($file, $ClassName = NULL) {
$file = str_replace ( _PathTag, DIRECTORY_SEPARATOR, $file );
$ClassName = ($ClassName == NULL) ? $file : $ClassName;
$file = _Model . $file . '.php';
if (is_file ( $file )) {
include_once ($file);
if (! class_exists ( $ClassName ))
Amysql::AmysqlNotice ( $ClassName . ' 模型对象不存在' );
$this->DBConfig ['ModelTag'] = $ModelTag = $ClassName . '_' . $this->DBConfig ['ConnectTag']; // 模型标识
if (! isset ( $this->AmysqlModel [$ModelTag] )) // 不存在模型
{
$this->AmysqlModel [$ModelTag] = new $ClassName ();
if (! isset ( $this->DB->DBS [$ModelTag] ))
$this->DB->DBConnect ( $this->DBConfig ); // 没连接就进行数据库连接
$this->AmysqlModel [$ModelTag]->MysqlConnect = $this->DB->DBS [$ModelTag]; // 模型使用的数据库连接
}
Return $this->AmysqlModel [$ModelTag];
}
Amysql::AmysqlNotice ( $file . ' 模型文件不存在' );
}
/**
* 获得相关文件数据(模板目录)
*
* @param string $file
* @return string 数据
*/
public function _file($file) {
$file = str_replace ( _PathTag, DIRECTORY_SEPARATOR, $file );
$file = _View . $file . '.php';
if (is_file ( $file ))
Return file_get_contents ( $file );
Amysql::AmysqlNotice ( $file . ' 数据文件不存在' );
}
/**
* 数据调试
*
* @param $data 数据
*/
public function _echo($data) {
$this->EchoData [] = $data;
}
/**
* 模板显示
*/
public function _view($file = null) {
// 控制器属性共享于模板 2012-4-20
$AmysqlSelfPropertyKey = array_keys ( get_class_vars ( 'AmysqlController' ) );
$ControllerPropertyParam = get_object_vars ( $this );
foreach ( $ControllerPropertyParam as $key => $val ) {
if (! in_array ( $key, $AmysqlSelfPropertyKey )) // 非Amysql控制器属性
$this->AmysqlTemplates->TemplateValue [$key] = $val;
}
$this->AmysqlTemplates->_view ( $file );
// 信息调试输出
if (is_array ( $this->AmysqlModel )) {
foreach ( $this->AmysqlModel as $key => $val ) // Sql调试
echo $this->AmysqlModel [$key]->SqlBug;
}
foreach ( $this->EchoData as $val )
echo "<!--<pre>\n", print_r ( $val, true ), "\n</pre>-->";
}
}
/**
* **********************************************
*
* 视图表现层 (V)
*
* @param Array $TemplateValue
*
*/
class AmysqlTemplates {
public $TemplateValue; // 模板数据
function AmysqlTemplates() {
// 自定义模板常用参数数据
$this->TemplateValue ['_date'] = date ( 'Y-m-d H:i:s', time () );
}
public function _view($file = null) {
global $Config;
// 2012-10-8
($Config ['XSS'] && Amysql::filter ( $this->TemplateValue, 'htmlspecialchars' ) && Amysql::filter ( $_GET, 'htmlspecialchars' ) && Amysql::filter ( $_REQUEST, 'htmlspecialchars' ) && Amysql::filter ( $_COOKIE, 'htmlspecialchars' ));
@extract ( $this->TemplateValue );
$file = str_replace ( _PathTag, DIRECTORY_SEPARATOR, $file );
$file = _View . $file . '.php';
if (is_file ( $file ))
Return include ($file);
Amysql::AmysqlNotice ( $file . ' 模板文件不存在' );
}
}
/**
* **********************************************
*
* 模型数据层 (M)
*
* @param Object $MysqlConnect
* @param String $Affected
* @param String $InsertId
* @param String $ResultSum
* @param String $QueryStatus
*/
class AmysqlModel {
public $MysqlConnect;
public $Affected;
public $InsertId;
public $ResultSum;
public $QueryStatus;
public $SqlBug = '';
/**
* 基本查询
*
* @param string $sql
* @return Object 结果集
*/
public function _query($sql) {
global $Config;
if (! $this->MysqlConnect)
Return false;
$this->QueryStatus = '(0)';
$this->Affected = 0;
if ($Config ['DebugSql'])
$this->SqlBug .= "\n" . '<!--DebugSql: ' . $sql . '-->' . "\n";
$result = mysql_query ( $sql, $this->MysqlConnect );
if (! $result)
Return false;
$this->Affected = mysql_affected_rows ();
$this->QueryStatus = '(ok)';
Return $result;
}
/**
* 插入数据
*
* @param string $table
* @param Array $data
* @return int InsertId 新增ID
*/
public function _insert($table, $data) {
$this->InsertId = 0;
if (! is_array ( $data ) || count ( $data ) == 0)
Return 0;
$field_arr = array ();
foreach ( $data as $key => $val )
$field_arr [] = " `$key` = '$val' ";
$sql = "INSERT INTO `$table` SET " . implode ( ',', $field_arr );
$this->_query ( $sql );
$this->InsertId = mysql_insert_id ();
Return $this->InsertId;
}
/**
* 更新数据
*
* @param string $table
* @param Array $data
* @param string $where
* @return int Affected 影响数
* @param string $content
* 更新数据依据
*/
public function _update($table, $data, $where = '') {
$this->Affected = 0;
if (! is_array ( $data ) || count ( $data ) == 0)
Return 0;
$field_arr = array ();
foreach ( $data as $key => $val )
$field_arr [] = " `$key` = '$val' ";
$sql = "UPDATE `$table` SET " . implode ( ',', $field_arr ) . $where;
$this->_query ( $sql );
Return $this->Affected;
}
/**
* 取得一排数据
*
* @param string $sql
* @param string $result_type
* @return Array $row 数据
*/
public function _row($sql, $result_type = MYSQL_ASSOC) {
$result = $this->_query ( $sql );
if (! $result)
Return false;
$row = mysql_fetch_array ( $result, $result_type );
$this->ResultSum = 1;
$this->_free ( $result );
Return $row;
}
/**
* 取得所有数据
*
* @param string $sql
* @param string $result_type
* @return Array $row 数据
*/
public function _all($sql, $result_type = MYSQL_ASSOC) {
$result = $this->_query ( $sql );
if (! $result)
Return false;
$data = array ();
$this->ResultSum = 0;
while ( $row = mysql_fetch_array ( $result, $result_type ) ) {
$data [] = $row;
++ $this->ResultSum;
}
$this->_free ( $result );
Return $data;
}
/**
* 取得数据总数
*
* @param string $sql
* @return int 数量
*/
public function _sum($sql) {
$result = $this->_query ( $sql );
if (! $result)
Return false;
Return mysql_num_rows ( $result );
}
/**
* 释放结果集
*
* @param Object $rs
*/
public function _free($rs) {
Return mysql_free_result ( $rs );
}
}
/**
* **********************************************
*
* 数据库对象
*
* @param Object $DBS
*/
class DB {
public $DBS;
public function DBConnect($Config) {
$this->DBS [$Config ['ModelTag']] = @mysql_connect ( $Config ['Host'], $Config ['User'], $Config ['Password'] );
(! $this->DBS [$Config ['ModelTag']] && Amysql::AmysqlNotice ( mysql_error () . ' Mysql链接出错请配置/Amysql/config.php文件。' ));
(! empty ( $Config ['DBname'] ) && mysql_select_db ( $Config ['DBname'], $this->DBS [$Config ['ModelTag']] ));
$CharSet = str_replace ( '-', '', $Config ['CharSet'] );
mysql_query ( "SET NAMES '$CharSet'", $this->DBS [$Config ['ModelTag']] );
}
}

31
Amysql/Config.php Normal file
View File

@ -0,0 +1,31 @@
<?php
// 系统基本配置 **********************************************
$Config ['HttpPath'] = false; // 是否开启 index.php/Controller/Action/name/value 模式
$Config ['Filter'] = false; // 是否过滤 $_GET、$_REQUEST、$_COOKIE、$_FILES
$Config ['XSS'] = true; // 是否开启 XSS防范
$Config ['SessionStart'] = true; // 是否开启 SESSION
$Config ['DebugPhp'] = true; // 是否开启PHP运行报错信息
$Config ['DebugSql'] = true; // 是否开启源码调试Sql语句
$Config ['CharSet'] = 'utf-8'; // 设置网页编码
$Config ['UrlControllerName'] = 'c'; // 自定义控制器名称 例如: index.php?c=index
$Config ['UrlActionName'] = 'a'; // 自定义方法名称 例如: index.php?c=index&a=IndexAction
$Config ['index'] = "index.php";
// 默认使用数据库配置 *****************************************
$Config ['ConnectTag'] = 'default'; // Mysql连接标识 可同时进行多连接
$Config ['Host'] = 'localhost:3306'; // Mysql主机地址
$Config ['User'] = 'root'; // Mysql用户
$Config ['Password'] = 'ookk1904'; // Mysql密码
$Config ['DBname'] = 'minecraft'; // 数据库名称
$Config ['CharSet'] = "UTF-8";
// 邮箱信息配置**********************************************
$Config ['smtp'] = "smtp.163.com";
$Config ['port'] = "25";
$Config ['user'] = "jtb1@163.com";
$Config ['sender'] = "jtb1@163.com";
$Config ['pass'] = "jtb2hww";
$Config ['debug'] = false;

201
Amysql/Function.php Normal file
View File

@ -0,0 +1,201 @@
<?php
/**
* 读取配置文件
* @param string $Key KEY值
*/
function Config($Key) {
include (_Amysql . 'Config.php');
if (empty ( $Key ))
return $Config;
if (! array_key_exists ( $Key, $Config ))
return null;
return $Config [$Key];
}
/**
* 缓存操作
*
* @param string $Key
* KEY值
* @param string $Value
* 值
* @param int $Time
* 缓存有效期(秒)
*/
function Cache($Key, $Value = '', $Time = null) {
require_once (_Amysql . 'Cache.php');
$Cahce = new Cache ();
if ($Value === '' && is_null ( $Time )) {
return $Cahce->Get ( md5 ( $Key ) );
} else if (is_null ( $Value )) {
$Cahce->Del ( md5 ( $Key ) );
} else {
if (is_null ( $Time )) {
$Time = Config ( 'CacheDefaultTime' );
}
$Cahce->Set ( md5 ( $Key ), $Value, $Time );
}
}
/**
* 随即生成字符串
*
* @param int $l
* 字符串长度
*/
function RandSting($l = 10) {
$c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
srand ( ( double ) microtime () * 1000000 );
for($i = 0; $i < $l; $i ++) {
$rand .= $c [rand () % strlen ( $c )];
}
return $rand;
}
/**
* json数据输出
*
* @param object $data
* json输出的数据
*/
function Json($data) {
header ( 'Content-Type:application/json; charset=' . Config ( 'Charset' ) );
exit ( json_encode ( $data, 0 ) );
}
/**
* json success数据输出
*
* @param object $info
* json输出的数据
*/
function JsonSuccess($info) {
$json_data ['status'] = 'success';
$json_data ['info'] = $info;
Json ( $json_data );
}
/**
* json error数据输出
*
* @param object $info
* json输出的数据
*/
function JsonError($info) {
$json_data ['status'] = 'error';
$json_data ['info'] = $info;
Json ( $json_data );
}
/**
* 生成url地址
*
* @param string $c
* 控制器
* @param string $a
* 控制器方法
* @param array $p
* get提交参数
*/
function Url($c, $a, $p = null) {
$sp = '';
if (is_array ( $p )) {
foreach ( $p as $key => $value ) {
$sp .= sprintf ( "&%s=%s", urlencode ( $key ), urlencode ( $value ) );
}
}
return sprintf ( "%s%s?%s=%s&%s=%s%s", _Http, Config ( 'index' ), Config ( 'UrlControllerName' ), $c, Config ( 'UrlActionName' ), $a, $sp );
}
/**
* 获取客户端ip地址
*/
function GetUserIP($type = 0, $adv = false) {
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL)
return $ip [$type];
if ($adv) {
if (isset ( $_SERVER ['HTTP_X_FORWARDED_FOR'] )) {
$arr = explode ( ',', $_SERVER ['HTTP_X_FORWARDED_FOR'] );
$pos = array_search ( 'unknown', $arr );
if (false !== $pos)
unset ( $arr [$pos] );
$ip = trim ( $arr [0] );
} elseif (isset ( $_SERVER ['HTTP_CLIENT_IP'] )) {
$ip = $_SERVER ['HTTP_CLIENT_IP'];
} elseif (isset ( $_SERVER ['REMOTE_ADDR'] )) {
$ip = $_SERVER ['REMOTE_ADDR'];
}
} elseif (isset ( $_SERVER ['REMOTE_ADDR'] )) {
$ip = $_SERVER ['REMOTE_ADDR'];
}
// IP地址合法验证
$long = sprintf ( "%u", ip2long ( $ip ) );
$ip = $long ? array (
$ip,
$long
) : array (
'0.0.0.0',
0
);
return $ip [$type];
}
/**
* 设置SESSION有效时间
*/
function start_session($expire = 0) {
if ($expire == 0) {
$expire = ini_get ( 'session.gc_maxlifetime' );
} else {
ini_set ( 'session.gc_maxlifetime', $expire );
}
if (empty ( $_COOKIE ['PHPSESSID'] )) {
session_set_cookie_params ( $expire );
session_start ();
} else {
session_start ();
setcookie ( 'PHPSESSID', session_id (), time () + $expire );
}
}
function mailsender($mailer, $verify, $type) {
include (_Amysql . 'mail.class.php');
// 邮箱线程**********************************************************
$smtp = new smtp ( Config ( 'smtp' ), Config ( 'port' ), true, Config ( 'user' ), Config ( 'pass' ) );
switch ($type) {
case 'status' :
$smtpemailto = $mailer; // 发送给谁
$mailsubject = "minecraft账号验证通知"; // 邮件主题
$mailbody = "你已成功注册 请妥善保管<br>验证码:{$verify}<br>请及时验证邮箱";
$mailtype = "HTML"; // 邮件格式HTML/TXT,TXT为文本邮件
$smtp->debug = Config ( 'debug' ); // 是否显示发送的调试信息
if ($smtp->sendmail ( $smtpemailto, Config ( 'sender' ), $mailsubject, $mailbody, $mailtype ))
return true;
return false;
break;
case 'forget' :
$smtpemailto = $mailer; // 发送给谁
$mailsubject = "minecraft账号验证通知"; // 邮件主题
$mailbody = "你正在操作找回密码<br>验证码:{$verify}<br>请及时验证邮箱";
$mailtype = "HTML"; // 邮件格式HTML/TXT,TXT为文本邮件
$smtp->debug = Config ( 'debug' ); // 是否显示发送的调试信息
if ($smtp->sendmail ( $smtpemailto, Config ( 'sender' ), $mailsubject, $mailbody, $mailtype ))
return true;
return false;
break;
case 'forge' :
$smtpemailto = $mailer; // 发送给谁
$mailsubject = "minecraft账号通知"; // 邮件主题
$mailbody = "你的密码已重置 新密码是:$verify <br>收到密码后请尽快登陆后台修改密码";
$mailtype = "HTML"; // 邮件格式HTML/TXT,TXT为文本邮件
$smtp->debug = Config ( 'debug' ); // 是否显示发送的调试信息
if ($smtp->sendmail ( $smtpemailto, Config ( 'sender' ), $mailsubject, $mailbody, $mailtype ))
return true;
return false;
break;
}
}
function load($tpl) {
include (_View . $tpl . ".php");
}

286
Amysql/mail.class.php Normal file
View File

@ -0,0 +1,286 @@
<?php
error_reporting(0);
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($mailtype=="HTML")
{
$header .= "Content-Type:text/html\r\n";
}
$header .= "To: ".$to."\r\n";
if ($cc != "")
{
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: $from<".$from.">\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
$TO = explode(",", $this->strip_comment($to));
if ($cc != "")
{
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "")
{
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to)
{
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to))
{
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body))
{
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
}
else
{
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo))
{
return $this->smtp_error("sending HELO command");
}
#auth
if($this->auth)
{
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user)))
{
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass)))
{
return $this->smtp_error("sending HELO command");
}
}
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">"))
{
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">"))
{
return $this->smtp_error("sending RCPT TO command");
}
if (!$this->smtp_putcmd("DATA"))
{
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body))
{
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom())
{
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
}
if (!$this->smtp_putcmd("QUIT"))
{
return $this->smtp_error("sending QUIT command");
}
return TRUE;
}
function smtp_sockopen($address)
{
if ($this->relay_host == "")
{
return $this->smtp_sockopen_mx($address);
}
else
{
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok()))
{
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."\n");
return TRUE;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $MXHOSTS))
{
$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
return FALSE;
}
foreach ($MXHOSTS as $host)
{
$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok()))
{
$this->log_write("Warning: Cannot connect to mx host ".$host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
continue;
}
$this->log_write("Connected to mx host ".$host."\n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."\r\n".$body);
$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "\r\n.\r\n");
$this->smtp_debug(". [EOM]\n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("\r\n", "", fgets($this->sock, 512));
$this->smtp_debug($response."\n");
if (!ereg("^[23]", $response))
{
fputs($this->sock, "QUIT\r\n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned \"".$response."\"\n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "")
{
if($cmd=="")
{
$cmd = $arg;
}
else
{
$cmd = $cmd." ".$arg;
}
}
fputs($this->sock, $cmd."\r\n");
$this->smtp_debug("> ".$cmd."\n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".\n");
return FALSE;
}
function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "")
{
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
{
$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
return FALSE;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "\([^()]*\)";
while (ereg($comment, $address))
{
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ \t\r\n])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug)
{
echo $message;
}
}
}
?>