1<?php
2/**
3 * CEmailLogRoute class file.
4 *
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.yiiframework.com/
7 * @copyright 2008-2013 Yii Software LLC
8 * @license http://www.yiiframework.com/license/
9 */
10
11/**
12 * CEmailLogRoute sends selected log messages to email addresses.
13 *
14 * The target email addresses may be specified via {@link setEmails emails} property.
15 * Optionally, you may set the email {@link setSubject subject}, the
16 * {@link setSentFrom sentFrom} address and any additional {@link setHeaders headers}.
17 *
18 * @property array $emails List of destination email addresses.
19 * @property string $subject Email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT.
20 * @property string $sentFrom Send from address of the email.
21 * @property array $headers Additional headers to use when sending an email.
22 *
23 * @author Qiang Xue <qiang.xue@gmail.com>
24 * @package system.logging
25 * @since 1.0
26 */
27class CEmailLogRoute extends CLogRoute
28{
29	/**
30	 * @var boolean set this property to true value in case log data you're going to send through emails contains
31	 * non-latin or UTF-8 characters. Emails would be UTF-8 encoded.
32	 * @since 1.1.13
33	 */
34	public $utf8=false;
35	/**
36	 * @var array list of destination email addresses.
37	 */
38	private $_email=array();
39	/**
40	 * @var string email subject
41	 */
42	private $_subject;
43	/**
44	 * @var string email sent from address
45	 */
46	private $_from;
47	/**
48	 * @var array list of additional headers to use when sending an email.
49	 */
50	private $_headers=array();
51
52	/**
53	 * Sends log messages to specified email addresses.
54	 * @param array $logs list of log messages
55	 */
56	protected function processLogs($logs)
57	{
58		$message='';
59		foreach($logs as $log)
60			$message.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
61		$message=wordwrap($message,70);
62		$subject=$this->getSubject();
63		if($subject===null)
64			$subject=Yii::t('yii','Application Log');
65		foreach($this->getEmails() as $email)
66			$this->sendEmail($email,$subject,$message);
67	}
68
69	/**
70	 * Sends an email.
71	 * @param string $email single email address
72	 * @param string $subject email subject
73	 * @param string $message email content
74	 */
75	protected function sendEmail($email,$subject,$message)
76	{
77		$headers=$this->getHeaders();
78		if($this->utf8)
79		{
80			$headers[]="MIME-Version: 1.0";
81			$headers[]="Content-Type: text/plain; charset=UTF-8";
82			$subject='=?UTF-8?B?'.base64_encode($subject).'?=';
83		}
84		if(($from=$this->getSentFrom())!==null)
85		{
86			$matches=array();
87			preg_match_all('/([^<]*)<([^>]*)>/iu',$from,$matches);
88			if(isset($matches[1][0],$matches[2][0]))
89			{
90				$name=$this->utf8 ? '=?UTF-8?B?'.base64_encode(trim($matches[1][0])).'?=' : trim($matches[1][0]);
91				$from=trim($matches[2][0]);
92				$headers[]="From: {$name} <{$from}>";
93			}
94			else
95				$headers[]="From: {$from}";
96			$headers[]="Reply-To: {$from}";
97		}
98		mail($email,$subject,$message,implode("\r\n",$headers));
99	}
100
101	/**
102	 * @return array list of destination email addresses
103	 */
104	public function getEmails()
105	{
106		return $this->_email;
107	}
108
109	/**
110	 * @param mixed $value list of destination email addresses. If the value is
111	 * a string, it is assumed to be comma-separated email addresses.
112	 */
113	public function setEmails($value)
114	{
115		if(is_array($value))
116			$this->_email=$value;
117		else
118			$this->_email=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY);
119	}
120
121	/**
122	 * @return string email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT
123	 */
124	public function getSubject()
125	{
126		return $this->_subject;
127	}
128
129	/**
130	 * @param string $value email subject.
131	 */
132	public function setSubject($value)
133	{
134		$this->_subject=$value;
135	}
136
137	/**
138	 * @return string send from address of the email
139	 */
140	public function getSentFrom()
141	{
142		return $this->_from;
143	}
144
145	/**
146	 * @param string $value send from address of the email
147	 */
148	public function setSentFrom($value)
149	{
150		$this->_from=$value;
151	}
152
153	/**
154	 * @return array additional headers to use when sending an email.
155	 * @since 1.1.4
156	 */
157	public function getHeaders()
158	{
159		return $this->_headers;
160	}
161
162	/**
163	 * @param mixed $value list of additional headers to use when sending an email.
164	 * If the value is a string, it is assumed to be line break separated headers.
165	 * @since 1.1.4
166	 */
167	public function setHeaders($value)
168	{
169		if (is_array($value))
170			$this->_headers=$value;
171		else
172			$this->_headers=preg_split('/\r\n|\n/',$value,-1,PREG_SPLIT_NO_EMPTY);
173	}
174}