/** * Constructor * * @param array $server Server info * @param array $config Configuration */ public function __construct(array &$server, array &$config) { $this->config = $config; $this->server = $server; $this->socket = $this->getSocket('ssl://', $this->server['Host'], $this->server['Port'], $this->server['Timeout']); $this->socket->registerResponseParser(250, function ($param) { $params = explode(' ', $param, 64); switch (strtolower($params[0])) { case 'size': if (isset($params[1])) { $this->serverInfo['MailMaxSize'] = (int) $params[1]; } break; case 'auth': if (isset($params[1])) { $this->serverInfo['AuthMethods'] = explode(' ', strtolower($params[1]), 16); } break; } return 250; }, true); $this->socket->registerResponseParser(551, function ($param) { $newAddrs = array(); if (preg_match('/\\<(.*)\\>/ui', $param, $newAddrs)) { if (isset($newAddrs[1]) && Validator::check($newAddrs[1], 'email', 512, 1)) { $this->lastForwardServerRCPT[] = $newAddrs[1]; } } return 551; }, true); }
/** * Constructor * * @param mixed $var The var to be converted in to parameter format * * @throws Exception\InvalidAlphaNumberString */ public function __construct($var) { if (!Validator::check($var, 'alphanumber')) { throw new Exception\InvalidAlphaNumberString($var); } $this->var = $var; }
/** * Check if the input is valid * * @param mixed $value The value to check * @param Error &$error The reference for getting error feedback * * @return bool Return True when it's qualified, false otherwise */ public function qualified(&$value, &$error) { $formatError = ''; if (!is_string($value)) { $error = new Error('INVALID', 'DATATYPE', array(gettype($value))); return false; } if (!Validator::check($value, $this->format, $this->maxlen, $this->minlen, $formatError)) { switch ($formatError) { case 'TOOLONG': $error = new Error('INVALID', 'TOOLONG', array('Max' => $this->maxlen)); break; case 'TOOSHORT': $error = new Error('INVALID', 'TOOSHORT', array('Min' => $this->minlen)); break; default: $error = new Error('INVALID', $formatError); break; } return false; } return true; }
/** * SMTP Constructor * * @param array $config Configuration for initialize the class * * @throws Exception\ServerFromAddressInvalid * @throws Exception\ServerReplyToAddressInvalid * @throws Exception\ServerReturnToAddressInvalid * @throws Exception\ServerErrorToAddressInvalid * @throws Exception\NoServerSpecified */ public function __construct(array &$config) { $typeVerified = array(); $cp = new ConfigParser($config, static::$defaultConfig); $version = Framework::getVersion(); $senderIP = IP::joinIP(Framework::core('request')->getClientInfo('ipArray'), true); $this->config['Handler'] = $version['App'] . ' ' . $version['Ver']; if ($cp->isEmpty('Servers')) { throw new Exception\NoServerSpecified(); } $this->config['Temp'] = PathParser::get($cp->getValid('TempFilesDir', function ($val) { return is_dir($val); }, sys_get_temp_dir())); $servers = $cp->get('Servers'); if ($cp->has('SelectMethod')) { switch ($cp->get('SelectMethod')) { case 'SelectMethod': shuffle($servers); break; } } foreach ($servers as $key => $val) { $serverCP = new ConfigParser($val, static::$defaultServerConfig); $mailUserName = $serverCP->get('Username'); $mailDefaultFrom = strpos($mailUserName, '@') !== false ? $mailUserName : $mailUserName . '@' . $serverCP->get('Host'); $emailFrom = $serverCP->getValid('From', function ($val) { if (empty($val)) { return false; } if (!Validator::check($val, 'email')) { throw new Exception\ServerFromAddressInvalid($val); } return true; }, $mailDefaultFrom); $this->config['Servers'][$key] = array('Host' => $serverCP->get('Host'), 'Port' => $serverCP->get('Port'), 'Type' => $serverCP->getValid('Type', function ($val) use(&$typeVerified) { if (empty($val)) { return false; } if (isset($typeVerified[$val])) { return true; } if (!isset(static::$operators[$val])) { throw new Exception\UnknownServerType($val); } $optClass = static::$operators[$val]; if (!class_exists($optClass)) { throw new Exception\OperatorClassNotFound($optClass, $val); } $parents = class_parents($optClass); if (!isset($parents)) { throw new Exception\OperatorExtendsInvalid($optClass); } $typeVerified[$val] = true; return true; }, 'General'), 'Timeout' => $serverCP->get('Timeout'), 'Retry' => $serverCP->get('Retry'), 'Username' => $mailUserName, 'Password' => $serverCP->get('Password'), 'Handler' => $this->config['Handler'], 'ScreenName' => $serverCP->get('ScreenName', $serverCP->get('Username')), 'From' => $emailFrom, 'ReplyTo' => $serverCP->getValid('ReplyTo', function ($val) { if (empty($val)) { return false; } if (!Validator::check($val, 'email')) { throw new Exception\ServerReplyToAddressInvalid($val); } return true; }, $emailFrom), 'ReturnTo' => $serverCP->getValid('ReturnTo', function ($val) { if (empty($val)) { return false; } if (!Validator::check($val, 'email')) { throw new Exception\ServerReturnToAddressInvalid($val); } return true; }, $emailFrom), 'ErrorTo' => $serverCP->getValid('ReturnTo', function ($val) { if (empty($val)) { return false; } if (!Validator::check($val, 'email')) { throw new Exception\ServerErrorToAddressInvalid($val); } return true; }, $emailFrom), 'SignCert' => PathParser::get($serverCP->getValid('SignatureCert', function ($val) { if (empty($val)) { return false; } if (!is_readable($val)) { throw new Exception\ServerSignCertNotReadable($val); } return true; }, '')), 'SignKey' => PathParser::get($serverCP->getValid('SignatureKey', function ($val) { if (empty($val)) { return false; } if (!is_readable($val)) { throw new Exception\ServerSignKeyNotReadable($val); } return true; }, '')), 'SignPass' => $serverCP->get('SignaturePass'), 'SignChain' => PathParser::get($serverCP->getValid('SignatureChain', function ($val) { if (empty($val)) { return false; } if (!is_readable($val)) { throw new Exception\ServerSignChainNotReadable($val); } return true; }, '')), 'SenderIP' => $senderIP); } }
/** * SMTP initializer * * @param array $cfg Configuration for initialize the class * * @return bool Return true when success, or false when fail */ public static function init(array $cfg = array()) { $version = array(); $senderIP = ''; if (empty(static::$config)) { $version = Framework::getVersion(); $senderIP = IP::joinIP(Framework::core('request')->getClientInfo('ipArray'), true); static::$config['Handler'] = $version['App'] . ' ' . $version['Ver']; if (isset($cfg['Servers']) && is_array($cfg['Servers'])) { if (isset($cfg['SelectMethod']) && $cfg['SelectMethod'] == 'Random') { shuffle($cfg['Servers']); } foreach ($cfg['Servers'] as $key => $val) { static::$config['Servers'][$key] = array('Host' => isset($val['Host']) ? $val['Host'] : 'localhost', 'Port' => isset($val['Port']) ? $val['Port'] : 25, 'Type' => isset($val['Type']) ? $val['Type'] : 'general', 'Timeout' => isset($val['Timeout']) ? $val['Timeout'] : 1, 'Username' => isset($val['Username']) ? $val['Username'] : '', 'Password' => isset($val['Password']) ? $val['Password'] : '', 'Handler' => static::$config['Handler'], 'SenderIP' => $senderIP); // Set poster screen name, this will be display on the receiver's list if (isset($val['Screenname'])) { static::$config['Servers'][$key]['Screenname'] = $val['Screenname']; } else { static::$config['Servers'][$key]['Screenname'] = static::$config['Servers'][$key]['Username']; } // Set MAIL FROM, this one must be set for future use if (isset($val['From'])) { if (Validator::check($val['From'], 'email')) { static::$config['Servers'][$key]['From'] = $val['From']; } else { throw new Exception\ServerFromAddressInvaild($val['From']); } } else { static::$config['Servers'][$key]['From'] = static::$config['Servers'][$key]['Username'] . '@' . static::$config['Servers'][$key]['Host']; } // Set REPLY TO if (isset($val['ReplyTo'])) { if (Validator::check($val['ReplyTo'], 'email')) { static::$config['Servers'][$key]['ReplyTo'] = $val['ReplyTo']; } else { throw new Exception\ServerReplyToAddressInvaild($val['ReplyTo']); } } else { static::$config['Servers'][$key]['ReplyTo'] = static::$config['Servers'][$key]['From']; } // Set RETURN TO if (isset($val['ReturnTo'])) { if (Validator::check($val['ReturnTo'], 'email')) { static::$config['Servers'][$key]['ReturnTo'] = $val['ReturnTo']; } else { throw new Exception\ServerReturnToAddressInvaild($val['ReplyTo']); } } else { static::$config['Servers'][$key]['ReturnTo'] = static::$config['Servers'][$key]['From']; } // Set ERROR TO if (isset($val['ErrorTo'])) { if (Validator::check($val['ErrorTo'], 'email')) { static::$config['Servers'][$key]['ErrorTo'] = $val['ErrorTo']; } else { throw new Exception\ServerErrorToAddressInvaild($val['ErrorTo']); } } else { static::$config['Servers'][$key]['ErrorTo'] = static::$config['Servers'][$key]['From']; } } Framework::registerHook('response_finished', 'SMTP_Sending', function () { static::sendMail(); }); Auther::init(); return true; } else { throw new Exception\NoServerSpecified(); } } return false; }