PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/packages/IDS/Report.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 341 lines | 132 code | 33 blank | 176 comment | 12 complexity | d79c6716fef2125aae3d28843afed906 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPIDS
  4. *
  5. * Requirements: PHP5, SimpleXML
  6. *
  7. * Copyright (c) 2008 PHPIDS group (https://phpids.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. /**
  33. * PHPIDS report object
  34. *
  35. * The report objects collects a number of events and thereby presents the
  36. * detected results. It provides a convenient API to work with the results.
  37. *
  38. * Note that this class implements Countable, IteratorAggregate and
  39. * a __toString() method
  40. *
  41. * @category Security
  42. * @package PHPIDS
  43. * @author Christian Matthies <ch0012@gmail.com>
  44. * @author Mario Heiderich <mario.heiderich@gmail.com>
  45. * @author Lars Strojny <lars@strojny.net>
  46. * @copyright 2007-2009 The PHPIDS Group
  47. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  48. * @version Release: $Id:Report.php 517 2007-09-15 15:04:13Z mario $
  49. * @link http://php-ids.org/
  50. */
  51. class IDS_Report implements Countable, IteratorAggregate
  52. {
  53. /**
  54. * Event container
  55. *
  56. * @var array
  57. */
  58. protected $events = array();
  59. /**
  60. * List of affected tags
  61. *
  62. * This list of tags is collected from the collected event objects on
  63. * demand when IDS_Report->getTags() is called
  64. *
  65. * @var array
  66. */
  67. protected $tags = array();
  68. /**
  69. * Impact level
  70. *
  71. * The impact level is calculated on demand by adding the results of the
  72. * event objects on IDS_Report->getImpact()
  73. *
  74. * @var integer
  75. */
  76. protected $impact = 0;
  77. /**
  78. * Centrifuge data
  79. *
  80. * This variable - initiated as an empty array - carries all information
  81. * about the centrifuge data if available
  82. *
  83. * @var array
  84. */
  85. protected $centrifuge = array();
  86. /**
  87. * Constructor
  88. *
  89. * @param array $events the events the report should include
  90. *
  91. * @return void
  92. */
  93. public function __construct(array $events = null)
  94. {
  95. if ($events) {
  96. foreach ($events as $event) {
  97. $this->addEvent($event);
  98. }
  99. }
  100. }
  101. /**
  102. * Adds an IDS_Event object to the report
  103. *
  104. * @param object $event IDS_Event
  105. *
  106. * @return object $this
  107. */
  108. public function addEvent(IDS_Event $event)
  109. {
  110. $this->clear();
  111. $this->events[$event->getName()] = $event;
  112. return $this;
  113. }
  114. /**
  115. * Get event by name
  116. *
  117. * In most cases an event is identified by the key of the variable that
  118. * contained maliciously appearing content
  119. *
  120. * @param scalar $name the event name
  121. *
  122. * @throws InvalidArgumentException if argument is invalid
  123. * @return mixed IDS_Event object or false if the event does not exist
  124. */
  125. public function getEvent($name)
  126. {
  127. if (!is_scalar($name)) {
  128. throw new InvalidArgumentException(
  129. 'Invalid argument type given'
  130. );
  131. }
  132. if ($this->hasEvent($name)) {
  133. return $this->events[$name];
  134. }
  135. return false;
  136. }
  137. /**
  138. * Returns list of affected tags
  139. *
  140. * @return array
  141. */
  142. public function getTags()
  143. {
  144. if (!$this->tags) {
  145. $this->tags = array();
  146. foreach ($this->events as $event) {
  147. $this->tags = array_merge($this->tags,
  148. $event->getTags());
  149. }
  150. $this->tags = array_values(array_unique($this->tags));
  151. }
  152. return $this->tags;
  153. }
  154. /**
  155. * Returns total impact
  156. *
  157. * Each stored IDS_Event object and its IDS_Filter sub-object are called
  158. * to calculate the overall impact level of this request
  159. *
  160. * @return integer
  161. */
  162. public function getImpact()
  163. {
  164. if (!$this->impact) {
  165. $this->impact = 0;
  166. foreach ($this->events as $event) {
  167. $this->impact += $event->getImpact();
  168. }
  169. }
  170. return $this->impact;
  171. }
  172. /**
  173. * Checks if a specific event with given name exists
  174. *
  175. * @param scalar $name the event name
  176. *
  177. * @throws InvalidArgumentException if argument is illegal
  178. *
  179. * @return boolean
  180. */
  181. public function hasEvent($name)
  182. {
  183. if (!is_scalar($name)) {
  184. throw new InvalidArgumentException('Invalid argument given');
  185. }
  186. return isset($this->events[$name]);
  187. }
  188. /**
  189. * Returns total amount of events
  190. *
  191. * @return integer
  192. */
  193. public function count()
  194. {
  195. return count($this->events);
  196. }
  197. /**
  198. * Return iterator object
  199. *
  200. * In order to provide the possibility to directly iterate over the
  201. * IDS_Event object the IteratorAggregate is implemented. One can easily
  202. * use foreach() to iterate through all stored IDS_Event objects.
  203. *
  204. * @return ArrayObject the event collection
  205. */
  206. public function getIterator()
  207. {
  208. return new ArrayObject($this->events);
  209. }
  210. /**
  211. * Checks if any events are registered
  212. *
  213. * @return boolean
  214. */
  215. public function isEmpty()
  216. {
  217. return empty($this->events);
  218. }
  219. /**
  220. * Clears calculated/collected values
  221. *
  222. * @return void
  223. */
  224. protected function clear()
  225. {
  226. $this->impact = 0;
  227. $this->tags = array();
  228. }
  229. /**
  230. * This method returns the centrifuge property or null if not
  231. * filled with data
  232. *
  233. * @return array/null
  234. */
  235. public function getCentrifuge()
  236. {
  237. return ($this->centrifuge && count($this->centrifuge) > 0)
  238. ? $this->centrifuge : null;
  239. }
  240. /**
  241. * This method sets the centrifuge property
  242. *
  243. * @param array $centrifuge the centrifuge data
  244. *
  245. * @throws InvalidArgumentException if argument is illegal
  246. *
  247. * @return boolean true is arguments were valid
  248. */
  249. public function setCentrifuge($centrifuge = array())
  250. {
  251. if (is_array($centrifuge) && $centrifuge) {
  252. $this->centrifuge = $centrifuge;
  253. return true;
  254. }
  255. throw new InvalidArgumentException('Invalid argument given');
  256. }
  257. /**
  258. * Directly outputs all available information
  259. *
  260. * @return string
  261. */
  262. public function __toString()
  263. {
  264. if (!$this->isEmpty()) {
  265. $output = '';
  266. $output .= 'Total impact: ' . $this->getImpact() . "<br/>\n";
  267. $output .= 'Affected tags: ' . join(', ', $this->getTags()) .
  268. "<br/>\n";
  269. foreach ($this->events as $event) {
  270. $output .= "<br/>\nVariable: " .
  271. htmlspecialchars($event->getName()) . ' | Value: ' .
  272. htmlspecialchars($event->getValue()) . "<br/>\n";
  273. $output .= 'Impact: ' . $event->getImpact() . ' | Tags: ' .
  274. join(', ', $event->getTags()) . "<br/>\n";
  275. foreach ($event as $filter) {
  276. $output .= 'Description: ' . $filter->getDescription() .
  277. ' | ';
  278. $output .= 'Tags: ' . join(', ', $filter->getTags()) .
  279. ' | ';
  280. $output .= 'ID: ' . $filter->getId() .
  281. "<br/>\n";
  282. }
  283. }
  284. $output .= '<br/>';
  285. if ($centrifuge = $this->getCentrifuge()) {
  286. $output .= 'Centrifuge detection data';
  287. $output .= '<br/> Threshold: ' .
  288. ((isset($centrifuge['threshold'])&&$centrifuge['threshold']) ?
  289. $centrifuge['threshold'] : '---');
  290. $output .= '<br/> Ratio: ' .
  291. ((isset($centrifuge['ratio'])&&$centrifuge['ratio']) ?
  292. $centrifuge['ratio'] : '---');
  293. if(isset($centrifuge['converted'])) {
  294. $output .= '<br/> Converted: ' . $centrifuge['converted'];
  295. }
  296. $output .= "<br/><br/>\n";
  297. }
  298. }
  299. return isset($output) ? $output : '';
  300. }
  301. }
  302. /**
  303. * Local variables:
  304. * tab-width: 4
  305. * c-basic-offset: 4
  306. * End:
  307. * vim600: sw=4 ts=4 expandtab
  308. */