/lib/misc-classes/filters-Tag.php

https://github.com/cpainchaud/pan-configurator · PHP · 424 lines · 420 code · 3 blank · 1 comment · 5 complexity · 61a4827a5192212d56228dd1fd08fe94 MD5 · raw file

  1. <?php
  2. // <editor-fold desc=" ***** Tag filters *****" defaultstate="collapsed" >
  3. RQuery::$defaultFilters['tag']['refcount']['operators']['>,<,=,!'] = Array(
  4. 'eval' => '$object->countReferences() !operator! !value!',
  5. 'arg' => true,
  6. 'ci' => Array(
  7. 'fString' => '(%PROP% 1)',
  8. 'input' => 'input/panorama-8.0.xml'
  9. )
  10. );
  11. RQuery::$defaultFilters['tag']['object']['operators']['is.unused'] = Array(
  12. 'Function' => function(TagRQueryContext $context )
  13. {
  14. return $context->object->countReferences() == 0;
  15. },
  16. 'arg' => false,
  17. 'ci' => Array(
  18. 'fString' => '(%PROP%)',
  19. 'input' => 'input/panorama-8.0.xml'
  20. )
  21. );
  22. RQuery::$defaultFilters['tag']['name']['operators']['is.in.file'] = Array(
  23. 'Function' => function(TagRQueryContext $context )
  24. {
  25. $object = $context->object;
  26. if( !isset($context->cachedList) )
  27. {
  28. $text = file_get_contents($context->value);
  29. if( $text === false )
  30. derr("cannot open file '{$context->value}");
  31. $lines = explode("\n", $text);
  32. foreach( $lines as $line)
  33. {
  34. $line = trim($line);
  35. if(strlen($line) == 0)
  36. continue;
  37. $list[$line] = true;
  38. }
  39. $context->cachedList = &$list;
  40. }
  41. else
  42. $list = &$context->cachedList;
  43. return isset($list[$object->name()]);
  44. },
  45. 'arg' => true
  46. );
  47. RQuery::$defaultFilters['tag']['object']['operators']['is.tmp'] = Array(
  48. 'Function' => function(TagRQueryContext $context )
  49. {
  50. return $context->object->isTmp();
  51. },
  52. 'arg' => false,
  53. 'ci' => Array(
  54. 'fString' => '(%PROP%)',
  55. 'input' => 'input/panorama-8.0.xml'
  56. )
  57. );
  58. RQuery::$defaultFilters['tag']['name']['operators']['eq'] = Array(
  59. 'Function' => function(TagRQueryContext $context )
  60. {
  61. return $context->object->name() == $context->value;
  62. },
  63. 'arg' => true,
  64. 'ci' => Array(
  65. 'fString' => '(%PROP% grp.shared-group1)',
  66. 'input' => 'input/panorama-8.0.xml'
  67. )
  68. );
  69. RQuery::$defaultFilters['tag']['name']['operators']['eq.nocase'] = Array(
  70. 'Function' => function(TagRQueryContext $context )
  71. {
  72. return strtolower($context->object->name()) == strtolower($context->value);
  73. },
  74. 'arg' => true,
  75. 'ci' => Array(
  76. 'fString' => '(%PROP% grp.shared-group1)',
  77. 'input' => 'input/panorama-8.0.xml'
  78. )
  79. );
  80. RQuery::$defaultFilters['tag']['name']['operators']['contains'] = Array(
  81. 'Function' => function(TagRQueryContext $context )
  82. {
  83. return strpos($context->object->name(), $context->value) !== false;
  84. },
  85. 'arg' => true,
  86. 'ci' => Array(
  87. 'fString' => '(%PROP% grp)',
  88. 'input' => 'input/panorama-8.0.xml'
  89. )
  90. );
  91. RQuery::$defaultFilters['tag']['name']['operators']['regex'] = Array(
  92. 'Function' => function(TagRQueryContext $context )
  93. {
  94. $object = $context->object;
  95. $value = $context->value;
  96. if( strlen($value) > 0 && $value[0] == '%')
  97. {
  98. $value = substr($value, 1);
  99. if( !isset($context->nestedQueries[$value]) )
  100. derr("regular expression filter makes reference to unknown string alias '{$value}'");
  101. $value = $context->nestedQueries[$value];
  102. }
  103. $matching = preg_match($value, $object->name());
  104. if( $matching === FALSE )
  105. derr("regular expression error on '{$value}'");
  106. if( $matching === 1 )
  107. return true;
  108. return false;
  109. },
  110. 'arg' => true,
  111. 'ci' => Array(
  112. 'fString' => '(%PROP% /-group/)',
  113. 'input' => 'input/panorama-8.0.xml'
  114. )
  115. );
  116. RQuery::$defaultFilters['tag']['location']['operators']['is'] = Array(
  117. 'Function' => function(TagRQueryContext $context )
  118. {
  119. $owner = $context->object->owner->owner;
  120. if( strtolower($context->value) == 'shared' )
  121. {
  122. if( $owner->isPanorama() )
  123. return true;
  124. if( $owner->isFirewall() )
  125. return true;
  126. return false;
  127. }
  128. if( strtolower($context->value) == strtolower($owner->name()) )
  129. return true;
  130. return false;
  131. },
  132. 'arg' => true,
  133. 'ci' => Array(
  134. 'fString' => '(%PROP% shared )',
  135. 'input' => 'input/panorama-8.0.xml'
  136. )
  137. );
  138. RQuery::$defaultFilters['tag']['location']['operators']['regex'] = Array(
  139. 'Function' => function(TagRQueryContext $context )
  140. {
  141. $name = $context->object->getLocationString();
  142. $matching = preg_match($context->value, $name);
  143. if( $matching === FALSE )
  144. derr("regular expression error on '{$context->value}'");
  145. if( $matching === 1 )
  146. return true;
  147. return false;
  148. },
  149. 'arg' => true,
  150. 'ci' => Array(
  151. 'fString' => '(%PROP% /shared/)',
  152. 'input' => 'input/panorama-8.0.xml'
  153. )
  154. );
  155. RQuery::$defaultFilters['tag']['location']['operators']['is.child.of'] = Array(
  156. 'Function' => function(TagRQueryContext $context )
  157. {
  158. $tag_location = $context->object->getLocationString();
  159. $sub = $context->object->owner;
  160. while( get_class($sub ) == "TagStore" || get_class($sub ) == "DeviceGroup" || get_class($sub) == "VirtualSystem" )
  161. $sub = $sub->owner;
  162. if( get_class($sub) == "PANConf")
  163. derr( "filter location is.child.of is not working against a firewall configuration" );
  164. if( strtolower($context->value) == 'shared' )
  165. return true;
  166. $DG = $sub->findDeviceGroup( $context->value );
  167. if( $DG == null )
  168. {
  169. print "ERROR: location '$context->value' was not found. Here is a list of available ones:\n";
  170. print " - shared\n";
  171. foreach( $sub->getDeviceGroups() as $sub1 )
  172. {
  173. print " - ".$sub1->name()."\n";
  174. }
  175. print "\n\n";
  176. exit(1);
  177. }
  178. $childDeviceGroups = $DG->childDeviceGroups( TRUE );
  179. if( strtolower($context->value) == strtolower($tag_location) )
  180. return true;
  181. foreach( $childDeviceGroups as $childDeviceGroup )
  182. {
  183. if( $childDeviceGroup->name() == $tag_location )
  184. return true;
  185. }
  186. return false;
  187. },
  188. 'arg' => true,
  189. 'help' => 'returns TRUE if object location (shared/device-group/vsys name) matches / is child the one specified in argument',
  190. 'ci' => Array(
  191. 'fString' => '(%PROP% Datacenter-Firewalls)',
  192. 'input' => 'input/panorama-8.0.xml'
  193. )
  194. );
  195. RQuery::$defaultFilters['tag']['location']['operators']['is.parent.of'] = Array(
  196. 'Function' => function(TagRQueryContext $context )
  197. {
  198. $tag_location = $context->object->getLocationString();
  199. $sub = $context->object->owner;
  200. while( get_class($sub ) == "TagStore" || get_class($sub ) == "DeviceGroup" || get_class($sub) == "VirtualSystem" )
  201. $sub = $sub->owner;
  202. if( get_class($sub) == "PANConf")
  203. derr( "filter location is.parent.of is not working against a firewall configuration" );
  204. if( strtolower($context->value) == 'shared' )
  205. return true;
  206. $DG = $sub->findDeviceGroup( $context->value );
  207. if( $DG == null )
  208. {
  209. print "ERROR: location '$context->value' was not found. Here is a list of available ones:\n";
  210. print " - shared\n";
  211. foreach( $sub->getDeviceGroups() as $sub1 )
  212. {
  213. print " - ".$sub1->name()."\n";
  214. }
  215. print "\n\n";
  216. exit(1);
  217. }
  218. $parentDeviceGroups = $DG->parentDeviceGroups( );
  219. if( strtolower($context->value) == strtolower($tag_location) )
  220. return true;
  221. if( $tag_location == 'shared' )
  222. return true;
  223. foreach( $parentDeviceGroups as $childDeviceGroup )
  224. {
  225. if( $childDeviceGroup->name() == $tag_location )
  226. return true;
  227. }
  228. return false;
  229. },
  230. 'arg' => true,
  231. 'help' => 'returns TRUE if object location (shared/device-group/vsys name) matches / is parent the one specified in argument',
  232. 'ci' => Array(
  233. 'fString' => '(%PROP% Datacenter-Firewalls)',
  234. 'input' => 'input/panorama-8.0.xml'
  235. )
  236. );
  237. RQuery::$defaultFilters['tag']['reflocation']['operators']['is'] = Array(
  238. 'Function' => function(TagRQueryContext $context )
  239. {
  240. $object = $context->object;
  241. $owner = $context->object->owner->owner;
  242. $reflocation_array = $object->getReferencesLocation();
  243. if( strtolower($context->value) == 'shared' )
  244. {
  245. if( $owner->isPanorama() )
  246. return true;
  247. if( $owner->isFirewall() )
  248. return true;
  249. return false;
  250. }
  251. foreach( $reflocation_array as $reflocation )
  252. {
  253. #if( strtolower($reflocation) == strtolower($owner->name()) )
  254. if( strtolower($reflocation) == strtolower($context->value) )
  255. return true;
  256. }
  257. return false;
  258. },
  259. 'arg' => true,
  260. 'ci' => Array(
  261. 'fString' => '(%PROP% shared )',
  262. 'input' => 'input/panorama-8.0.xml'
  263. )
  264. );
  265. RQuery::$defaultFilters['tag']['reflocation']['operators']['is.only'] = Array(
  266. 'Function' => function(TagRQueryContext $context )
  267. {
  268. $owner = $context->object->owner->owner;
  269. $reflocations = $context->object->getReferencesLocation();
  270. $reftypes = $context->object->getReferencesType();
  271. $refstore = $context->object->getReferencesStore();
  272. if( strtolower($context->value) == 'shared' )
  273. {
  274. if( $owner->isPanorama() )
  275. return true;
  276. if( $owner->isFirewall() )
  277. return true;
  278. return false;
  279. }
  280. $return = false;
  281. foreach( $reflocations as $reflocation )
  282. {
  283. if( strtolower($reflocation) == strtolower($context->value) )
  284. $return = true;
  285. }
  286. if( count( $reflocations ) == 1 && $return )
  287. return true;
  288. else
  289. return false;
  290. },
  291. 'arg' => true,
  292. 'ci' => Array(
  293. 'fString' => '(%PROP% shared )',
  294. 'input' => 'input/panorama-8.0.xml'
  295. )
  296. );
  297. RQuery::$defaultFilters['tag']['refstore']['operators']['is'] = Array(
  298. 'Function' => function(TagRQueryContext $context )
  299. {
  300. $value = $context->value;
  301. $value = strtolower($value);
  302. $context->object->ReferencesStoreValidation( $value );
  303. $refstore = $context->object->getReferencesStore();
  304. if( array_key_exists( $value, $refstore ) )
  305. return true;
  306. return false;
  307. },
  308. 'arg' => true,
  309. 'ci' => Array(
  310. 'fString' => '(%PROP% rulestore )',
  311. 'input' => 'input/panorama-8.0.xml'
  312. )
  313. );
  314. RQuery::$defaultFilters['tag']['reftype']['operators']['is'] = Array(
  315. 'Function' => function(TagRQueryContext $context )
  316. {
  317. $value = $context->value;
  318. $value = strtolower($value);
  319. $context->object->ReferencesTypeValidation( $value );
  320. $reftype = $context->object->getReferencesType();
  321. if( array_key_exists( $value, $reftype ) )
  322. return true;
  323. return false;
  324. },
  325. 'arg' => true,
  326. 'ci' => Array(
  327. 'fString' => '(%PROP% securityrule )',
  328. 'input' => 'input/panorama-8.0.xml'
  329. )
  330. );
  331. RQuery::$defaultFilters['tag']['color']['operators']['eq'] = Array(
  332. 'Function' => function(TagRQueryContext $context )
  333. {
  334. return $context->object->getColor() == strtolower( $context->value );
  335. },
  336. 'arg' => true,
  337. 'ci' => Array(
  338. 'fString' => '(%PROP% none)',
  339. 'input' => 'input/panorama-8.0.xml'
  340. )
  341. );
  342. RQuery::$defaultFilters['tag']['comments']['operators']['regex'] = Array(
  343. 'Function' => function(TagRQueryContext $context )
  344. {
  345. $name = $context->object->getComments();
  346. $matching = preg_match($context->value, $name);
  347. if( $matching === FALSE )
  348. derr("regular expression error on '{$context->value}'");
  349. if( $matching === 1 )
  350. return true;
  351. return false;
  352. },
  353. 'arg' => true,
  354. 'ci' => Array(
  355. 'fString' => '(%PROP% /test/)',
  356. 'input' => 'input/panorama-8.0.xml'
  357. )
  358. );
  359. RQuery::$defaultFilters['tag']['comments']['operators']['is.empty'] = Array(
  360. 'Function' => function(TagRQueryContext $context )
  361. {
  362. $desc = $context->object->getComments();
  363. if( $desc === null || strlen($desc) == 0 )
  364. return true;
  365. return false;
  366. },
  367. 'arg' => false,
  368. 'ci' => Array(
  369. 'fString' => '(%PROP%)',
  370. 'input' => 'input/panorama-8.0.xml'
  371. )
  372. );
  373. // </editor-fold>