PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/packages/IDS/Log/Email.php

https://github.com/michaelmcandrew/vaw
PHP | 402 lines | 167 code | 43 blank | 192 comment | 21 complexity | 55b0a030d9069a0aa5fc7f58aeba6953 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPIDS
  4. *
  5. * Requirements: PHP5, SimpleXML
  6. *
  7. * Copyright (c) 2008 PHPIDS group (http://php-ids.org)
  8. *
  9. * PHPIDS is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * PHPIDS is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * PHP version 5.1.6+
  23. *
  24. * @category Security
  25. * @package PHPIDS
  26. * @author Mario Heiderich <mario.heiderich@gmail.com>
  27. * @author Christian Matthies <ch0012@gmail.com>
  28. * @author Lars Strojny <lars@strojny.net>
  29. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  30. * @link http://php-ids.org/
  31. */
  32. require_once 'IDS/Log/Interface.php';
  33. /**
  34. * Email logging wrapper
  35. *
  36. * The Email wrapper is designed to send reports via email. It implements the
  37. * singleton pattern.
  38. *
  39. * @category Security
  40. * @package PHPIDS
  41. * @author Christian Matthies <ch0012@gmail.com>
  42. * @author Mario Heiderich <mario.heiderich@gmail.com>
  43. * @author Lars Strojny <lars@strojny.net>
  44. * @copyright 2007-2009 The PHPIDS Group
  45. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  46. * @version Release: $Id:Email.php 517 2007-09-15 15:04:13Z mario $
  47. * @link http://php-ids.org/
  48. */
  49. class IDS_Log_Email implements IDS_Log_Interface
  50. {
  51. /**
  52. * Recipient container
  53. *
  54. * @var array
  55. */
  56. protected $recipients = array();
  57. /**
  58. * Mail subject
  59. *
  60. * @var string
  61. */
  62. protected $subject = null;
  63. /**
  64. * Additional mail headers
  65. *
  66. * @var string
  67. */
  68. protected $headers = null;
  69. /**
  70. * Safemode switch
  71. *
  72. * Using this switch it is possible to enable safemode, which is a spam
  73. * protection based on the alert frequency.
  74. *
  75. * @var boolean
  76. */
  77. protected $safemode = true;
  78. /**
  79. * Urlencode for result strings
  80. *
  81. * This switch is true by default. Setting it to false removes
  82. * the 'better safe than sorry' urlencoding for the result string in
  83. * the report mails. Enhances readability but maybe XSSes email clients.
  84. *
  85. * @var boolean
  86. */
  87. protected $urlencode = true;
  88. /**
  89. * Send rate
  90. *
  91. * If safemode is enabled, this property defines how often reports will be
  92. * sent out. Default value is 15, which means that a mail will be sent on
  93. * condition that the last email has not been sent earlier than 15 seconds ago.
  94. *
  95. * @var integer
  96. */
  97. protected $allowed_rate = 15;
  98. /**
  99. * PHPIDS temp directory
  100. *
  101. * When safemod is enabled, a path to a temp directory is needed to
  102. * store some information. Default is IDS/tmp/
  103. *
  104. * @var string
  105. */
  106. protected $tmp_path = 'IDS/tmp/';
  107. /**
  108. * File prefix for tmp files
  109. *
  110. * @var string
  111. */
  112. protected $file_prefix = 'PHPIDS_Log_Email_';
  113. /**
  114. * Holds current remote address
  115. *
  116. * @var string
  117. */
  118. protected $ip = 'local/unknown';
  119. /**
  120. * Instance container
  121. *
  122. * @var array
  123. */
  124. protected static $instance = array();
  125. /**
  126. * Constructor
  127. *
  128. * @param mixed $config IDS_Init instance | array
  129. *
  130. * @return void
  131. */
  132. protected function __construct($config)
  133. {
  134. if ($config instanceof IDS_Init) {
  135. $this->recipients = $config->config['Logging']['recipients'];
  136. $this->subject = $config->config['Logging']['subject'];
  137. $this->headers = $config->config['Logging']['header'];
  138. $this->envelope = $config->config['Logging']['envelope'];
  139. $this->safemode = $config->config['Logging']['safemode'];
  140. $this->urlencode = $config->config['Logging']['urlencode'];
  141. $this->allowed_rate = $config->config['Logging']['allowed_rate'];
  142. $this->tmp_path = $config->getBasePath()
  143. . $config->config['General']['tmp_path'];
  144. } elseif (is_array($config)) {
  145. $this->recipients[] = $config['recipients'];
  146. $this->subject = $config['subject'];
  147. $this->additionalHeaders = $config['header'];
  148. }
  149. // determine correct IP address and concat them if necessary
  150. $this->ip = $_SERVER['REMOTE_ADDR'] .
  151. (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ?
  152. ' (' . $_SERVER['HTTP_X_FORWARDED_FOR'] . ')' : '');
  153. }
  154. /**
  155. * Returns an instance of this class
  156. *
  157. * This method allows the passed argument to be either an instance of
  158. * IDS_Init or an array.
  159. *
  160. * @param mixed $config IDS_Init | array
  161. * @param string $classname the class name to use
  162. *
  163. * @return object $this
  164. */
  165. public static function getInstance($config, $classname = 'IDS_Log_Email')
  166. {
  167. if (!self::$instance) {
  168. self::$instance = new $classname($config);
  169. }
  170. return self::$instance;
  171. }
  172. /**
  173. * Permitting to clone this object
  174. *
  175. * For the sake of correctness of a singleton pattern, this is necessary
  176. *
  177. * @return void
  178. */
  179. private function __clone()
  180. {
  181. }
  182. /**
  183. * Detects spam attempts
  184. *
  185. * To avoid mail spam through this logging class this function is used
  186. * to detect such attempts based on the alert frequency.
  187. *
  188. * @return boolean
  189. */
  190. protected function isSpamAttempt()
  191. {
  192. /*
  193. * loop through all files in the tmp directory and
  194. * delete garbage files
  195. */
  196. $dir = $this->tmp_path;
  197. $numPrefixChars = strlen($this->file_prefix);
  198. $files = scandir($dir);
  199. foreach ($files as $file) {
  200. if (is_file($dir . $file)) {
  201. if (substr($file, 0, $numPrefixChars) == $this->file_prefix) {
  202. $lastModified = filemtime($dir . $file);
  203. if ((
  204. time() - $lastModified) > 3600) {
  205. unlink($dir . $file);
  206. }
  207. }
  208. }
  209. }
  210. /*
  211. * end deleting garbage files
  212. */
  213. $remoteAddr = $this->ip;
  214. $userAgent = $_SERVER['HTTP_USER_AGENT'];
  215. $filename = $this->file_prefix . md5($remoteAddr.$userAgent) . '.tmp';
  216. $file = $dir . DIRECTORY_SEPARATOR . $filename;
  217. if (!file_exists($file)) {
  218. $handle = fopen($file, 'w');
  219. fwrite($handle, time());
  220. fclose($handle);
  221. return false;
  222. }
  223. $lastAttack = file_get_contents($file);
  224. $difference = time() - $lastAttack;
  225. if ($difference > $this->allowed_rate) {
  226. unlink($file);
  227. } else {
  228. return true;
  229. }
  230. return false;
  231. }
  232. /**
  233. * Prepares data
  234. *
  235. * Converts given data into a format that can be read in an email.
  236. * You might edit this method to your requirements.
  237. *
  238. * @param mixed $data the report data
  239. *
  240. * @return string
  241. */
  242. protected function prepareData($data)
  243. {
  244. $format = "The following attack has been detected by PHPIDS\n\n";
  245. $format .= "IP: %s \n";
  246. $format .= "Date: %s \n";
  247. $format .= "Impact: %d \n";
  248. $format .= "Affected tags: %s \n";
  249. $attackedParameters = '';
  250. foreach ($data as $event) {
  251. $attackedParameters .= $event->getName() . '=' .
  252. ((!isset($this->urlencode) ||$this->urlencode)
  253. ? urlencode($event->getValue())
  254. : $event->getValue()) . ", ";
  255. }
  256. $format .= "Affected parameters: %s \n";
  257. $format .= "Request URI: %s \n";
  258. $format .= "Origin: %s \n";
  259. return sprintf($format,
  260. $this->ip,
  261. date('c'),
  262. $data->getImpact(),
  263. join(' ', $data->getTags()),
  264. trim($attackedParameters),
  265. urlencode($_SERVER['REQUEST_URI']),
  266. $_SERVER['SERVER_ADDR']);
  267. }
  268. /**
  269. * Sends the report to registered recipients
  270. *
  271. * @param object $data IDS_Report instance
  272. *
  273. * @throws Exception if data is no string
  274. * @return boolean
  275. */
  276. public function execute(IDS_Report $data)
  277. {
  278. if ($this->safemode) {
  279. if ($this->isSpamAttempt()) {
  280. return false;
  281. }
  282. }
  283. /*
  284. * In case the data has been modified before it might
  285. * be necessary to convert it to string since it's pretty
  286. * senseless to send array or object via e-mail
  287. */
  288. $data = $this->prepareData($data);
  289. if (is_string($data)) {
  290. $data = trim($data);
  291. // if headers are passed as array, we need to make a string of it
  292. if (is_array($this->headers)) {
  293. $headers = "";
  294. foreach ($this->headers as $header) {
  295. $headers .= $header . "\r\n";
  296. }
  297. } else {
  298. $headers = $this->headers;
  299. }
  300. if (!empty($this->recipients)) {
  301. if (is_array($this->recipients)) {
  302. foreach ($this->recipients as $address) {
  303. $this->send(
  304. $address,
  305. $data,
  306. $headers,
  307. $this->envelope
  308. );
  309. }
  310. } else {
  311. $this->send(
  312. $this->recipients,
  313. $data,
  314. $headers,
  315. $this->envelope
  316. );
  317. }
  318. }
  319. } else {
  320. throw new Exception(
  321. 'Please make sure that data returned by
  322. IDS_Log_Email::prepareData() is a string.'
  323. );
  324. }
  325. return true;
  326. }
  327. /**
  328. * Sends an email
  329. *
  330. * @param string $address email address
  331. * @param string $data the report data
  332. * @param string $headers the mail headers
  333. * @param string $envelope the optional envelope string
  334. *
  335. * @return boolean
  336. */
  337. protected function send($address, $data, $headers, $envelope = null)
  338. {
  339. if (!$envelope || strpos(ini_get('sendmail_path'),' -f') !== false) {
  340. return mail($address,
  341. $this->subject,
  342. $data,
  343. $headers);
  344. } else {
  345. return mail($address,
  346. $this->subject,
  347. $data,
  348. $headers,
  349. '-f' . $envelope);
  350. }
  351. }
  352. }
  353. /**
  354. * Local variables:
  355. * tab-width: 4
  356. * c-basic-offset: 4
  357. * End:
  358. * vim600: sw=4 ts=4 expandtab
  359. */