Event.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /**
  33. * PHPIDS event object
  34. *
  35. * This class represents a certain event that occured while applying the filters
  36. * to the supplied data. It aggregates a bunch of IDS_Filter implementations and
  37. * is a assembled in IDS_Report.
  38. *
  39. * Note that this class implements both Countable and IteratorAggregate
  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:Event.php 517 2007-09-15 15:04:13Z mario $
  49. * @link http://php-ids.org/
  50. */
  51. class IDS_Event implements Countable, IteratorAggregate
  52. {
  53. /**
  54. * Event name
  55. *
  56. * @var scalar
  57. */
  58. protected $name = null;
  59. /**
  60. * Value of the event
  61. *
  62. * @var scalar
  63. */
  64. protected $value = null;
  65. /**
  66. * List of filter objects
  67. *
  68. * Filter objects in this array are those that matched the events value
  69. *
  70. * @var array
  71. */
  72. protected $filters = array();
  73. /**
  74. * Calculated impact
  75. *
  76. * Total impact of the event
  77. *
  78. * @var integer
  79. */
  80. protected $impact = 0;
  81. /**
  82. * Affecte tags
  83. *
  84. * @var array
  85. */
  86. protected $tags = array();
  87. /**
  88. * Constructor
  89. *
  90. * Fills event properties
  91. *
  92. * @param scalar $name the event name
  93. * @param scalar $value the event value
  94. * @param array $filters the corresponding filters
  95. *
  96. * @return void
  97. */
  98. public function __construct($name, $value, Array $filters)
  99. {
  100. if (!is_scalar($name)) {
  101. throw new InvalidArgumentException(
  102. 'Expected $name to be a scalar,' . gettype($name) . ' given'
  103. );
  104. }
  105. if (!is_scalar($value)) {
  106. throw new InvalidArgumentException('
  107. Expected $value to be a scalar,' . gettype($value) . ' given'
  108. );
  109. }
  110. $this->name = $name;
  111. $this->value = $value;
  112. foreach ($filters as $filter) {
  113. if (!$filter instanceof IDS_Filter) {
  114. throw new InvalidArgumentException(
  115. 'Filter must be derived from IDS_Filter'
  116. );
  117. }
  118. $this->filters[] = $filter;
  119. }
  120. }
  121. /**
  122. * Returns event name
  123. *
  124. * The name of the event usually is the key of the variable that was
  125. * considered to be malicious
  126. *
  127. * @return scalar
  128. */
  129. public function getName()
  130. {
  131. return $this->name;
  132. }
  133. /**
  134. * Returns event value
  135. *
  136. * @return scalar
  137. */
  138. public function getValue()
  139. {
  140. return $this->value;
  141. }
  142. /**
  143. * Returns calculated impact
  144. *
  145. * @return integer
  146. */
  147. public function getImpact()
  148. {
  149. if (!$this->impact) {
  150. $this->impact = 0;
  151. foreach ($this->filters as $filter) {
  152. $this->impact += $filter->getImpact();
  153. }
  154. }
  155. return $this->impact;
  156. }
  157. /**
  158. * Returns affected tags
  159. *
  160. * @return array
  161. */
  162. public function getTags()
  163. {
  164. $filters = $this->getFilters();
  165. foreach ($filters as $filter) {
  166. $this->tags = array_merge($this->tags,
  167. $filter->getTags());
  168. }
  169. $this->tags = array_values(array_unique($this->tags));
  170. return $this->tags;
  171. }
  172. /**
  173. * Returns list of filter objects
  174. *
  175. * @return array
  176. */
  177. public function getFilters()
  178. {
  179. return $this->filters;
  180. }
  181. /**
  182. * Returns number of filters
  183. *
  184. * To implement interface Countable this returns the number of filters
  185. * appended.
  186. *
  187. * @return integer
  188. */
  189. public function count()
  190. {
  191. return count($this->getFilters());
  192. }
  193. /**
  194. * IteratorAggregate iterator getter
  195. *
  196. * Returns an iterator to iterate over the appended filters.
  197. *
  198. * @return ArrayObject the filter collection
  199. */
  200. public function getIterator()
  201. {
  202. return new ArrayObject($this->getFilters());
  203. }
  204. }
  205. /**
  206. * Local variables:
  207. * tab-width: 4
  208. * c-basic-offset: 4
  209. * End:
  210. * vim600: sw=4 ts=4 expandtab
  211. */