因爲 PHP7.1 開始廢棄傳統的加密函數 mcrypt_encrypt 而改用 openssl_encrypt,所以該類支持的 PHP 版本爲: (PHP 5 >= 5.3.0, PHP 7)php
<?php /** * 簡易加解密類 */ class Mcrypt { // 通用祕鑰 private $KEY = 'YOUR SECRET KEY'; private $METHOD = 'AES-128-ECB'; // 全部可用的加密類型可參考: http://php.net/manual/zh/function.openssl-get-cipher-methods.php /** * 構造函數 * * @param string 加密類型。若不傳,則爲默認的 AES-128-ECB. * @return void */ public function __constructor($method = '') { if (!empty($method)) { $this->METHOD = $method; } } /** * 加密字符串 * * @param String 待加密數據 * @param string 加密祕鑰,若爲空,則使用通用祕鑰 * @return void */ public function encrypt($input, $key = '') { if (empty($key)) $key = $this->KEY; $data = openssl_encrypt($input, $this->METHOD, $key, OPENSSL_RAW_DATA); $data = base64_encode($data); return $data; } /** * 解密字符串 * * @param String 待解密字符串 * @param string 解密祕鑰,若爲空,則使用通用祕鑰 * @return void */ public function decrypt($input, $key = '') { if (empty($key)) $key = $this->KEY; $data = openssl_decrypt(base64_decode($input), $this->METHOD, $key, OPENSSL_RAW_DATA); return $data; } }
先下載 PHPMailer,解壓後,將其放置你的項目中,並修改類中的引入路徑。git
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require dirname(__FILE__).'/PHPMailer/Exception.php'; require dirname(__FILE__).'/PHPMailer/PHPMailer.php'; require dirname(__FILE__).'/PHPMailer/SMTP.php'; class Mail { private $_email = null; private $_baseURL = ''; public function __construct() { $this->_init(); $this->_baseURL = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST']; } private function _init() { if ($this->_email == null) { $mail = new PHPMailer(true); try { $mail->SMTPDebug = 0; $mail->isSMTP(); $mail->Host = get_global_config('mail.Host'); $mail->SMTPAuth = true; $mail->Username = get_global_config('mail.Username'); $mail->Password = get_global_config('mail.Password'); $mail->SMTPSecure = get_global_config('mail.SMTPSecure'); $mail->Port = get_global_config('mail.Port'); $mail->setFrom(get_global_config('mail.Username'), get_global_config('mail.Fromname')); $this->_email = $mail; return true; } catch (Exception $e) { // return null; return false; } } return true; } public function sendEmail($toEmail, $toUsername, $subject, $body) { if (!$this->_email) return false; try { $this->_email->addAddress($toEmail, $toUsername); $this->_email->isHTML(true); $this->_email->Subject = $subject; $this->_email->Body = $body; // 當郵件不支持 HTML 時顯示的郵件內容 $this->_email->AltBody = $body; $this->_email->send(); // echo 'Message has been sent'; return true; } catch (Exception $e) { return false; // echo 'Message could not be sent.'; // echo 'Mailer Error: ' . $this->_email->ErrorInfo; } } }
使用 Redis 模擬進程鎖,參考至 discuz 的進程鎖源碼。github
<?php /** * 模擬進程鎖 */ class Process { protected $cache; /** * 構造函數,初始化 redis */ public function __construct() { $this->cache = Roc::redis(); $this->cache->select(Roc::get('redis.db')); } /** * 是否被鎖 * * @param string 鎖名 * @param integer 上鎖時間 * @return boolean */ public function islocked($process, $ttl = 0) { $ttl = $ttl < 1 ? 600 : intval($ttl); return $this->_status('get', $process) || $this->_find($process, $ttl); } /** * 解鎖 * * @param string 鎖名 * @return void */ public function unlock($process) { $this->_status('rm', $process); $this->_cmd('rm', $process); } private function _status($action, $process) { static $plist = array(); switch ($action) { case 'set' : $plist[$process] = true; break; case 'get' : return !empty($plist[$process]); break; case 'rm' : $plist[$process] = null; break; case 'clear' : $plist = array(); break; } return true; } private function _find($name, $ttl) { if(!$this->_cmd('get', $name)) { $this->_cmd('set', $name, $ttl); $ret = false; } else { $ret = true; } // $this->_status('set', $name); return $ret; } private function _cmd($cmd, $name, $ttl = 0) { return $this->_process_cmd_memory($cmd, $name, $ttl); // static $allowmem; // if($allowmem === null) { // $allowmem = Roc::get('redis.db'); // } // if($allowmem) { // return self::_process_cmd_memory($cmd, $name, $ttl); // } else { // return false; // // return self::_process_cmd_db($cmd, $name, $ttl); // } } private function _process_cmd_memory($cmd, $name, $ttl = 0) { $ret = ''; $name = 'process_lock_'.$name; switch ($cmd) { case 'set' : $ret = $this->cache->setex($name, time(), $ttl); // $ret = memory('set', 'process_lock_'.$name, time(), $ttl); break; case 'get' : $ret = $this->cache->get($name); // $ret = memory('get', 'process_lock_'.$name); break; case 'rm' : $ret = $this->cache->delete($name); // $ret = memory('rm', 'process_lock_'.$name); } echo $cmd . ':'; var_dump($ret); return $ret; } private function _process_cmd_db($cmd, $name, $ttl = 0) { $ret = ''; switch ($cmd) { case 'set': $ret = C::t('common_process')->insert(array('processid' => $name, 'expiry' => time() + $ttl), FALSE, true); break; case 'get': $ret = C::t('common_process')->fetch($name); if(empty($ret) || $ret['expiry'] < time()) { $ret = false; } else { $ret = true; } break; case 'rm': $ret = C::t('common_process')->delete_process($name, time()); break; } return $ret; } }
一個簡易的 Redis 操做類,支持在查找不到緩存時,進行相對應的回調操做。redis
<?php class Cache { protected $cache; protected $prefix; public function __construct() { // 根據項目實際狀況初始化你的 Reids。 $this->cache = new Redis(); } // ============= String 操做 /** * 設置緩存 * * @param String 緩存key值 * @param String/Object 緩存數據,可爲字符串可爲數組。 * @param Int 過時時間 * @return Boolean */ public function set($key, $value, $ttl = null) { if (is_array($value)) $value = serialize($value); if ($ttl !== null && $ttl > 0) return $this->cache->setex($this->_key($key), $ttl, $value); else return $this->cache->set($this->_key($key), $value); } /** * 獲取緩存數據 * * @param String 值 * @param Data 默認值,設置後,若是緩存沒有數據則返回默認值 * @return Data */ public function get($key, $callback = false) { $res = $this->cache->get($this->_key($key)); if ($res === false && $callback === true) { $method_name = "_$key"; if (method_exists($this, $method_name)) { return $this->$method_name(); } else { return false; } } // 若是反序列化成功,則返回反序列化的數據。 // @防止報 warning。 if (@unserialize($res) !== false) return unserialize($res); return $res; } /** * 刪除緩存 * * @param String 須要刪除的鍵名 * @return true or false */ public function rm($key) { return $this->cache->delete($this->_key($key)); } public function inc($key, $step = 1) { return $this->cache->incr($this->_key($key), $step); } public function dec($key, $step = 1) { return $this->cache->decr($this->_key($key), $step); } private function _key($key) { return $this->prefix . $key; } public function getMultiple($keys) { $_keys = []; foreach ($keys as $key) $_keys[] = $this->_key($key); return $this->cache->getMultiple($_keys); } // ============== Hash 操做 public function hset($hash, $key, $value) { return $this->cache->hSet($hash, $key, $value); } public function hget($hash, $key) { return $this->cache->hGet($hash, $key); } public function hgetall($hash) { return $this->cache->hGetAll($hash); } public function hvals($hash) { return $this->cache->hVals($hash); } public function hkeys($hash) { return $this->cache->hKeys($hash); } // ================ 通用回調 // Your callback code here. }