* @link http://www.yiiframework.com/ * @copyright 2008-2013 Yii Software LLC * @license http://www.yiiframework.com/license/ */ /** * CEmailLogRoute sends selected log messages to email addresses. * * The target email addresses may be specified via {@link setEmails emails} property. * Optionally, you may set the email {@link setSubject subject}, the * {@link setSentFrom sentFrom} address and any additional {@link setHeaders headers}. * * @property array $emails List of destination email addresses. * @property string $subject Email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT. * @property string $sentFrom Send from address of the email. * @property array $headers Additional headers to use when sending an email. * * @author Qiang Xue * @package system.logging * @since 1.0 */ class CNoteLogRoute extends CLogRoute { /** * @var boolean set this property to true value in case log data you're going to send through emails contains * non-latin or UTF-8 characters. Emails would be UTF-8 encoded. * @since 1.1.13 */ public $utf8=false; /** * @var array list of destination API_URL addresses. */ private $_api_url = array(); private $_subject; /** * Sends log messages to specified email addresses. * @param array $logs list of log messages */ protected function send_api($array) { if($this->_api_url && isset($this->_api_url['url']) && isset($this->_api_url['username']) && isset($this->_api_url['password'])) { $basicAuth = $this->_api_url['username'].":".$this->_api_url['password']; if($basicAuth) { $this->apipost($this->_api_url['url'],$array,array(),$basicAuth); }else { $this->apipost($this->_api_url['url'],$array); } } } protected function processLogs($logs) { $message=''; foreach($logs as $log) { $message.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]); } $this->apipost($this->_api_url['url'],array('sysType'=>$this->subject,'env'=>YII_ENV,'msg'=>$message)); } /** * @return array list of destination api addresses */ public function getApiUrl() { return $this->_api_url; } /** * @param mixed $value list of destination api addresses. If the value is * a string, it is assumed to be comma-separated api addresses. */ public function setApiUrl($value) { if(is_array($value)) { $this->_api_url = $value; }else { $this->_api_url = array( 'prefix' => $value, 'username' => 'zxhx', 'password' => '533166afe82356ff5bc22ae9a263fb4e' ) ; } return $this->_api_url = $value; } public function getSubject() { return $this->_subject; } public function setSubject($value) { $this->_subject=$value; } private function apipost($url, $array = array(), $header = array(), $basicAuth = "zxhx:533166afe82356ff5bc22ae9a263fb4e"){ $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if($basicAuth) $header = array("Authorization: Basic ".base64_encode($basicAuth)); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); if($array){ $array = http_build_query($array); curl_setopt($ch, CURLOPT_POSTFIELDS, $array); } $data = curl_exec($ch); curl_close($ch); return $data; } }