Commit f04ab681 authored by 顾文旭's avatar 顾文旭

email

parent 6f430c6c
......@@ -192,8 +192,8 @@ class Daemon
'host name: ' . gethostname() . "\r\n";
// $to = 'api@yidian-inc.com';
$to = 'zhangyu1@yidian-inc.com';
Email::sendMail('bp-noreply@yidian-inc.com', $to, $subject, $mail_body);
$to = 'wenxu.gu@linkingcities.com';
Email::sendMail('noreply@linkingcities.com', $to, $subject, $mail_body);
}
}
}
......
......@@ -35,7 +35,7 @@ class InterDynamic
$this->fields = array_keys($this->key_map);
$this->reverse_map = array_flip($this->key_map);
// //文章动态计数哨兵集群 Sentinel apiConnect
$this->redis = RedisUtil::getInstance('count', ['serializer' => 'none', 'master' => $useMaster]);
$this->redis = RedisUtil::getInstance('cache', ['serializer' => 'none', 'master' => $useMaster]);
}
public function get($docid, array $fields = array())
......
......@@ -5,6 +5,12 @@ namespace Api\PhpServices\Ksy;
use Api\PhpUtils\Http\HttpUtil;
use Api\PhpUtils\Mon\MonUtil;
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
/**
* 文档: http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=68515904
* Class Ks3Api
......@@ -144,31 +150,60 @@ class Ks3Api
*/
public static function upload($type)
{
$url = self::BASE_URL . self::PATH_PIC_UPLOAD;
$timestamp = self::msectime();
$handle = fopen($_FILES['file']['tmp_name'], 'r');
$content = fread($handle, filesize($_FILES['file']['tmp_name']));
$extend = ["type" => $type];
$extendJsonStr = json_encode($extend);
$md5Str = $extendJsonStr . "&" . md5($content) . "&" . $timestamp . "&" . self::KSYUN_SK;
$signature = self::KSYUN_AK . ":" . md5($md5Str);
$params = [
"pic" => new \CURLFile($_FILES['file']['tmp_name'], $_FILES['file']['type']),
"timestamp" => $timestamp,
"signature" => $signature,
"extend" => $extendJsonStr,
// 控制台获取密钥:https://portal.qiniu.com/user/key
$accessKey = "v8h5axBiWk5aoyNV-xlWcnmAZdKOHPKeqXp2yJRc";
$secretKey = "kbTIQ2mfpzsWnV9o6iWm7gSHds3lKOWaV5jYzTB9";
$bucket = "pinyipin";
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
// 生成上传 Token
$token = $auth->uploadToken($bucket);
// 初始化 UploadManager 对象并进行文件的上传。
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传,该方法会判断文件大小,进而决定使用表单上传还是分片上传,无需手动配置。
list($ret, $err) = $uploadMgr->putFile($token, $_FILES["file"]["name"], $_FILES['file']['tmp_name']);
// $url = self::BASE_URL . self::PATH_PIC_UPLOAD;
// $timestamp = self::msectime();
//
// $handle = fopen($_FILES['file']['tmp_name'], 'r');
// $content = fread($handle, filesize($_FILES['file']['tmp_name']));
//
// $extend = ["type" => $type];
// $extendJsonStr = json_encode($extend);
// $md5Str = $extendJsonStr . "&" . md5($content) . "&" . $timestamp . "&" . self::KSYUN_SK;
// $signature = self::KSYUN_AK . ":" . md5($md5Str);
//
// $params = [
// "pic" => new \CURLFile($_FILES['file']['tmp_name'], $_FILES['file']['type']),
// "timestamp" => $timestamp,
// "signature" => $signature,
// "extend" => $extendJsonStr,
// ];
//
// $begin = microtime(true);
// $uploadRes = HttpUtil::post($url, $params);
// $end = microtime(true);
// $totalTime = round(($end - $begin), 4) * 1000;
// $resCode = $uploadRes["response"]['code'] === 0 ? 200 : $uploadRes["response"]['code'];
// MonUtil::proxyMon($url, $resCode, 'ks3api', $totalTime);
return [
"response" => [
"data" => [
"animated" => false,
"format" => $_FILES['file']['type'],
"image_id" => $ret['key'],
"size" => [],
"url" => "https://pingouimg.jwshq.cn/" . $ret['key'] . "?url=" . $ret['key'],
]
],
];
$begin = microtime(true);
$uploadRes = HttpUtil::post($url, $params);
$end = microtime(true);
$totalTime = round(($end - $begin), 4) * 1000;
$resCode = $uploadRes["response"]['code'] === 0 ? 200 : $uploadRes["response"]['code'];
MonUtil::proxyMon($url, $resCode, 'ks3api', $totalTime);
return $uploadRes;
}
/**
......@@ -180,61 +215,4 @@ class Ks3Api
list($msec, $sec) = explode(' ', microtime());
return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
}
/**
* 批量获取图片
* @param $imageIds image_id 数组
* @param int $widthSize
* @param int $highSize
* @param float|int $expirationTime
* @return array
*/
public static function concurrencyPicEncryptUrl($imageIds, $widthSize = 0, $highSize = 0, $expirationTime = 60 * 60 * 24 * 365)
{
$url = self::BASE_GEN_URL . self::PATH_PIC_ENCRYPT_URL;
$timestamp = self::msectime();
$extend = [];
$extend["expiration_time"] = $expirationTime;
if (!empty($widthSize) && !empty($highSize)) {
$extend["query"] = "type=thumbnail_" . $widthSize . "x" . $highSize;
}
$extendJsonStr = json_encode($extend);
$params = [];
$urls = [];
foreach ($imageIds as $imageId) {
$md5Str = $extendJsonStr . "&" . $imageId . "&" . self::AUDIT_ID . "&" . $timestamp . "&" . self::KSYUN_SK;
$signature = self::KSYUN_AK . ":" . md5($md5Str);
$contentType = 'multipart/form-data';
$param = [
"image_id" => $imageId,
"auth_id" => self::AUDIT_ID,
"timestamp" => $timestamp,
"signature" => $signature,
"content_type" => $contentType,
"extend" => $extendJsonStr,
];
$params[$imageId] = $param;
$urls[$imageId] = $url;
}
$data = [];
if (!empty($urls)) {
$begin = microtime(true);
$encryptRes = HttpUtil::concurrencyPost($urls, $params);
$end = microtime(true);
$totalTime = round(($end - $begin), 4) * 1000;
$resCode = $encryptRes["response"]['code'] === 0 ? 200 : $encryptRes["response"]['code'];
MonUtil::proxyMon($url, $resCode, 'ks3api', $totalTime);
if (!empty($encryptRes)) {
foreach ($encryptRes as $imageId => $item) {
if (!empty($item["response"]["data"]["url"])) {
$data[$imageId] = $item["response"]["data"]["url"];
}
}
}
}
return $data;
}
}
\ No newline at end of file
......@@ -149,7 +149,7 @@ class RedisSession extends Session
}
return $this->getSessionRedis()
->del(array_unique($sessionIds), CacheUtil::PREFIX_BLANK_PREFIX);
->del(array_unique($sessionIds));
}
/**
......@@ -199,7 +199,7 @@ class RedisSession extends Session
protected function getSessionRedis()
{
if (is_null($this->redis)) {
$this->redis = RedisUtil::getInstance('redis', ['serverRegion'=> 'master', 'serializer' => 'php', 'master'=> true]);
$this->redis = RedisUtil::getInstance('cache', ['serverRegion'=> 'master', 'serializer' => 'php', 'master'=> true]);
}
return $this->redis;
}
......
......@@ -119,7 +119,7 @@ class RedisSessionHandler implements \SessionHandlerInterface
public function getRedis()
{
if (is_null($this->redis)) {
$this->redis = RedisUtil::getInstance('redis', ['serverRegion'=> 'master', 'serializer' => 'php', 'master'=> true]);
$this->redis = RedisUtil::getInstance('cache', ['serverRegion'=> 'master', 'serializer' => 'php', 'master'=> true]);
}
return $this->redis;
}
......
......@@ -24,7 +24,7 @@ class CacheUtil
const EXPIRE_AFTER_THREE_DAYS = 259200;
const EXPIRE_AFTER_FIFTEEN_DAYS = 1296000;
const EXPIRE_AFTER_ONE_MONTH = 2592000;
private static $instances = [];
private $handler = null;
private $expire = 86400; //@todo 讨论一下,如果不设置有效时间,默认1天,不允许设置长期有效
......@@ -40,7 +40,7 @@ class CacheUtil
* @param boolean $master
* @return mixed
*/
public static function getInstance($serverName = '', array $options = [])
public static function getInstance($serverName = '', array $options = [])
{
if (empty($serverName)) {
return null;
......@@ -53,7 +53,7 @@ class CacheUtil
if (isset(self::$instances[$instancesKey])) {
return self::$instances[$instancesKey];
}
return self::$instances[$instancesKey] = new Self($serverName, $options);
}
......@@ -87,7 +87,7 @@ class CacheUtil
if (is_null($value) || false === $value) {
return $default;
}
return $value;
return json_decode($value,true);
}
/**
......@@ -123,9 +123,9 @@ class CacheUtil
$key = $this->getCacheKey($prefix, $key);
//对数组/对象数据进行缓存处理,保证数据完整性
if (is_int($expire) && $expire) {
$result = $this->handler->setex($key, $expire, $value);
$result = $this->handler->setex($key, $expire, json_encode($value));
} else {
$result = $this->handler->set($key, $value);
$result = $this->handler->set($key, json_encode($value));
}
return $result;
}
......@@ -282,7 +282,7 @@ class CacheUtil
$keyArr[] = $prefix . $val;
}
return $keyArr;
}
}
return $prefix . $key;
}
}
......@@ -74,7 +74,7 @@ class FileLog
$log .= ' [debug_backtrace info: ]' . print_r(debug_backtrace(), 1);
error_log($log);
if (empty($mail_to)) {
$mail_to = 'bp-server@yidian-inc.com';
$mail_to = 'guwenxu@yidian-inc.com';
}
$subject = 'App api #' . $signature . '# ' . $_SERVER['SERVER_NAME'] . ' (' . $_SERVER['SERVER_ADDR'] . ') Alert Message';
$body = 'Error: ' . $signature . "\n\n";
......@@ -86,11 +86,10 @@ class FileLog
if (!is_array($mail_to)) {
$mail_to = [$mail_to];
}
if (self::shouldSendEmail(md5($signature)) === true) {
foreach ($mail_to as $mail) {
Email::sendMail('bp-noreply@yidian-inc.com', $mail, $subject, $body);
Email::sendMail('noreply@linkingcities.com', $mail, $subject, $body);
}
}
}
public static function accessLog()
......
......@@ -4,6 +4,7 @@ namespace Api\PhpUtils\Message;
use Yaf\Application;
class Email
{
/**
......@@ -16,61 +17,82 @@ class Email
* @param string $content_type body内容的格式和编码, 可支持html格式邮件 $content_type="Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
* @return bool
*/
public static function sendMail($from, $to, $subject, $body, $smtp_server = 'smtp.yidian.com', $smtp_port = 25, $content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n\r\n")
public static function sendMail($from, $to, $subject, $body, $smtp_server = 'smtp.mxhichina.com', $smtp_port = 25 , $content_type = 1)
{
if (empty($to)) {
return false;
}
$env = Application::app()->environ();
if($env != 'test' && $env != 'prod' && $env != 'perf'){
return false;
}
if (empty($from)) {
$from = 'bp-noreply@yidian-inc.com';
}
try {
$email = explode('@', $from);
$domain = $email[1];
$sock = fsockopen($smtp_server, $smtp_port, $errno, $errstr, 1);
fwrite($sock, "HELO " . $domain . "\r\n");
fgets($sock);
// $smtp_to = 'bp_all@linkingcities.com';
$smtp_to = ' wenxu.gu@linkingcities.com';
$smtp_host = 'smtp.mxhichina.com'; //SMTP服务器地址
$smtp_port = 25; //SMTP服务器端口
$smtp_openssl = 0; //是否使用SSL加密通信
$smtp_debug = 0; //是否debug输出运行信息
$smtp_username = 'bp@linkingcities.com'; //用户账号
$smtp_password = 'Hello1234'; //用户密码
$smtp_from = 'bp@linkingcities.com'; //发件邮箱
fwrite($sock, "auth login\r\n");
fgets($sock);
$smtp = array(
//1.EHLO 向服务器标识用户身份发送者 收到 220 或 250 时OK
array("EHLO " . $smtp_host . "\r\n", "220,250", "EHLO: "),
//2.AUTH LOGIN 请求登录认证 334 OK
array("AUTH LOGIN\r\n", "334", "AUTH LOGIN: "),
//3.发送经Base64加密的用户账号 334 OK
array(base64_encode($smtp_username) . "\r\n", "334", "Send Base64 Encode username: "),
//4.发送经Base64加密的用户密码 235 OK
array(base64_encode($smtp_password) . "\r\n", "235", "Send Base64 Encode password: "),
//5.Mail From 发送发件人邮箱 250 OK
array("MAIL FROM: <" . $smtp_from . ">\r\n", "250", "MAIL FROM: "),
//6.发送收件人邮箱 250 OK
array("RCPT TO: <" . $smtp_to . ">\r\n", "250", "RCPT TO: "),
//7.开始发送邮件数据 354 OK
array("DATA\r\n", "354", "DATA Start: "),
//8.0 发件人
array("From: " . $smtp_from . "\r\n", "", ""),
//8.1 收件人
array("To: " . $smtp_to . "\r\n", "", ""),
//8.2 发件代理客户端
array("X-Mailer: LANSEYUJIE WebMailer 1.0\r\n", "", ""),
//8.3 邮件重要级别 1(Highest) 3(Normal) 5(Lowest)
array("X-Priority: 1 (Highest)\r\n", "", ""),
//8.4 标题
array("Subject: " . $subject . "\r\n", "", ""),
//8.5 编码
array("Content-Type: text/html; charset=\"utf-8\"\r\n", "", ""),
//8.6 内容加密方式
array("Content-Transfer-Encoding: base64\r\n\r\n", "", ""),
//8.7 Base64 Encode Body
array(base64_encode($body) . "\r\n", "", ""),
//9.结束发送邮件数据 250
array("\r\n.\r\n", "250", "DATA End: "),
//10.结束会话
array("QUIT\r\n", "221", "QUIT: ")
);
fwrite($sock, "YnAtbm9yZXBseQ==\r\n");
fgets($sock);
$info = '';
fwrite($sock, "VlhObGNtNWhiJFclVTY=\r\n");
fgets($sock);
//建立socket通信
if (1 == $smtp_openssl) {
//SSL加密通信
$fp = @fsockopen('ssl://' . $smtp_host, $smtp_port);
} else {
//普通无加密通信
$fp = @fsockopen($smtp_host, $smtp_port);
}
if (!$fp) {
$info .= "FSOCKOPEN Error: Cannot conect to " . $smtp_host . "\r\n<br />";
}
fwrite($sock, "MAIL FROM:<" . $from . ">\r\n");
fgets($sock);
if (!is_array($to)) {
$to = [$to];
}
foreach ($to as $t) {
fwrite($sock, "RCPT TO:<" . $t . ">\r\n");
//发送smtp数组中的命令/数据
foreach ($smtp as $request) {
//发送请求信息
@fputs($fp, $request[0]);
//接收响应信息
if (1 == $smtp_debug && '' != $request[1]) {
//接收信息
$response = @fgets($fp, 128);
$info .= $request[2] . $response . "\r\n<br />";
}
fgets($sock);
fwrite($sock, "DATA\r\n");
fgets($sock);
fwrite($sock, "Subject: " . $subject . "\r\n");
fwrite($sock, "From: <" . $from . ">\r\n");
foreach ($to as $t) {
fwrite($sock, "To: <" . $t . ">\r\n");
}
fwrite($sock, $content_type);
fwrite($sock, $body . "\r\n.\r\n");
fgets($sock);
fwrite($sock, "QUIT\r\n");
fgets($sock);
fclose($sock);
} catch (\Exception $e) {
return false;
}
//关闭连接
@fclose($fp);
return $info;
}
}
......@@ -113,7 +113,7 @@ class RedisUtil
$this->connect();
}
private function initConfigure()
private function initConfigure()
{
$this->config = config('redis', $this->serverName . 'Connect');
if (!empty($this->config)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment