CNoteLogRoute.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * CEmailLogRoute sends selected log messages to email addresses.
  12. *
  13. * The target email addresses may be specified via {@link setEmails emails} property.
  14. * Optionally, you may set the email {@link setSubject subject}, the
  15. * {@link setSentFrom sentFrom} address and any additional {@link setHeaders headers}.
  16. *
  17. * @property array $emails List of destination email addresses.
  18. * @property string $subject Email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT.
  19. * @property string $sentFrom Send from address of the email.
  20. * @property array $headers Additional headers to use when sending an email.
  21. *
  22. * @author Qiang Xue <qiang.xue@gmail.com>
  23. * @package system.logging
  24. * @since 1.0
  25. */
  26. class CNoteLogRoute extends CLogRoute
  27. {
  28. /**
  29. * @var boolean set this property to true value in case log data you're going to send through emails contains
  30. * non-latin or UTF-8 characters. Emails would be UTF-8 encoded.
  31. * @since 1.1.13
  32. */
  33. public $utf8=false;
  34. /**
  35. * @var array list of destination API_URL addresses.
  36. */
  37. private $_api_url = array();
  38. private $_subject;
  39. /**
  40. * Sends log messages to specified email addresses.
  41. * @param array $logs list of log messages
  42. */
  43. protected function send_api($array)
  44. {
  45. if($this->_api_url && isset($this->_api_url['url']) && isset($this->_api_url['username']) && isset($this->_api_url['password']))
  46. {
  47. $basicAuth = $this->_api_url['username'].":".$this->_api_url['password'];
  48. if($basicAuth)
  49. {
  50. $this->apipost($this->_api_url['url'],$array,array(),$basicAuth);
  51. }else
  52. {
  53. $this->apipost($this->_api_url['url'],$array);
  54. }
  55. }
  56. }
  57. protected function processLogs($logs)
  58. {
  59. $message='';
  60. foreach($logs as $log)
  61. {
  62. $message.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
  63. }
  64. $this->apipost($this->_api_url['url'],array('sysType'=>$this->subject,'env'=>YII_ENV,'msg'=>$message));
  65. }
  66. /**
  67. * @return array list of destination api addresses
  68. */
  69. public function getApiUrl()
  70. {
  71. return $this->_api_url;
  72. }
  73. /**
  74. * @param mixed $value list of destination api addresses. If the value is
  75. * a string, it is assumed to be comma-separated api addresses.
  76. */
  77. public function setApiUrl($value)
  78. {
  79. if(is_array($value))
  80. {
  81. $this->_api_url = $value;
  82. }else
  83. {
  84. $this->_api_url = array(
  85. 'prefix' => $value,
  86. 'username' => 'zxhx',
  87. 'password' => '533166afe82356ff5bc22ae9a263fb4e'
  88. ) ;
  89. }
  90. return $this->_api_url = $value;
  91. }
  92. public function getSubject()
  93. {
  94. return $this->_subject;
  95. }
  96. public function setSubject($value)
  97. {
  98. $this->_subject=$value;
  99. }
  100. private function apipost($url, $array = array(), $header = array(), $basicAuth = "zxhx:533166afe82356ff5bc22ae9a263fb4e"){
  101. $ch = curl_init();
  102. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  103. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  104. curl_setopt($ch, CURLOPT_URL, $url);
  105. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  106. if($basicAuth)
  107. $header = array("Authorization: Basic ".base64_encode($basicAuth));
  108. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  109. if($array){
  110. $array = http_build_query($array);
  111. curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
  112. }
  113. $data = curl_exec($ch);
  114. curl_close($ch);
  115. return $data;
  116. }
  117. }