1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class IDS_Rule 9{ 10 11 protected $id; 12 protected $regex; 13 protected $description; 14 protected $tags; 15 protected $impact; 16 17 public function __construct($id) 18 { 19 $this->id = $id; 20 } 21 22 public static function getCustomFilePath() 23 { 24 25 global $prefs; 26 27 $path = $prefs['ids_custom_rules_file']; 28 29 if (empty($path)) { 30 $path = 'temp/ids_custom_rules.json'; 31 } 32 33 return $path; 34 } 35 36 /** 37 * @return mixed 38 */ 39 public function getId() 40 { 41 return $this->id; 42 } 43 44 /** 45 * @return mixed 46 */ 47 public function getRegex() 48 { 49 return $this->regex; 50 } 51 52 /** 53 * @return mixed 54 */ 55 public function getDescription() 56 { 57 return $this->description; 58 } 59 60 /** 61 * @return mixed 62 */ 63 public function getTags() 64 { 65 return $this->tags; 66 } 67 68 /** 69 * @return mixed 70 */ 71 public function getImpact() 72 { 73 return $this->impact; 74 } 75 76 /** 77 * @param mixed $regex 78 */ 79 public function setRegex($regex) 80 { 81 $this->regex = $regex; 82 } 83 84 /** 85 * @param mixed $description 86 */ 87 public function setDescription($description) 88 { 89 $this->description = $description; 90 } 91 92 /** 93 * @param mixed $tags 94 */ 95 public function setTags($tags) 96 { 97 if (! is_array($tags)) { 98 $tags = explode(',', $tags); 99 } 100 101 foreach ($tags as $key => $tag) { 102 $tags[$key] = trim($tag); 103 } 104 105 $this->tags = $tags; 106 } 107 108 /** 109 * @param mixed $impact 110 */ 111 public function setImpact($impact) 112 { 113 $this->impact = $impact; 114 } 115 116 public function save() 117 { 118 119 $customRules = self::getAllRules(); 120 121 $updated = false; 122 foreach ($customRules as $key => $rule) { 123 if ($rule->id == $this->id) { 124 $customRules[$key] = $this; 125 $updated = true; 126 break; 127 } 128 } 129 130 if (! $updated) { 131 $customRules[] = $this; 132 } 133 134 return $this->writeRules($customRules); 135 } 136 137 public function delete() 138 { 139 140 $customRules = self::getAllRules(); 141 142 foreach ($customRules as $key => $rule) { 143 if ($rule->id == $this->id) { 144 unset($customRules[$key]); 145 break; 146 } 147 } 148 149 return $this->writeRules($customRules); 150 } 151 152 private function writeRules($customRules) 153 { 154 $rules = []; 155 foreach ($customRules as $customRule) { 156 $rules[] = [ 157 'id' => $customRule->id, 158 'rule' => $customRule->regex, 159 'description' => $customRule->description, 160 'tags' => [ 161 'tag' => $customRule->tags, 162 ], 163 'impact' => $customRule->impact, 164 ]; 165 } 166 167 $filter = [ 168 'filters' => $rules 169 ]; 170 171 $filename = self::getCustomFilePath(); 172 173 return file_put_contents($filename, json_encode($filter)); 174 } 175 176 /** 177 * @return array 178 */ 179 public static function getAllRules() 180 { 181 182 $filename = self::getCustomFilePath(); 183 184 if (! file_exists($filename)) { 185 return []; 186 } 187 188 $data = file_get_contents($filename); 189 $customRules = json_decode($data, true); 190 191 $rules = []; 192 foreach ($customRules['filters'] as $customRule) { 193 $rule = new self($customRule['id']); 194 $rule->setRegex($customRule['rule']); 195 $rule->setDescription($customRule['description']); 196 $rule->setTags($customRule['tags']['tag']); 197 $rule->setImpact($customRule['impact']); 198 199 $rules[] = $rule; 200 } 201 202 return $rules; 203 } 204 205 /** 206 * @param $ruleID 207 * @return IDS_Rule | false 208 */ 209 public static function getRule($ruleID) 210 { 211 212 $customRules = self::getAllRules(); 213 214 foreach ($customRules as $rule) { 215 if ($rule->id == $ruleID) { 216 return $rule; 217 } 218 } 219 220 return false; 221 } 222} 223