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

/moodle/lib/htmlpurifier/HTMLPurifier/Config.php

https://bitbucket.org/geek745/moodle-db2
PHP | 494 lines | 285 code | 40 blank | 169 comment | 72 complexity | 0a5773cbececaa6a22e99b6bc1f01fe3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. require_once 'HTMLPurifier/ConfigSchema.php';
  3. // member variables
  4. require_once 'HTMLPurifier/HTMLDefinition.php';
  5. require_once 'HTMLPurifier/CSSDefinition.php';
  6. require_once 'HTMLPurifier/URIDefinition.php';
  7. require_once 'HTMLPurifier/Doctype.php';
  8. require_once 'HTMLPurifier/DefinitionCacheFactory.php';
  9. // accomodations for versions earlier than 4.3.10 and 5.0.2
  10. // borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <aidan@php.net>
  11. if (!defined('PHP_EOL')) {
  12. switch (strtoupper(substr(PHP_OS, 0, 3))) {
  13. case 'WIN':
  14. define('PHP_EOL', "\r\n");
  15. break;
  16. case 'DAR':
  17. define('PHP_EOL', "\r");
  18. break;
  19. default:
  20. define('PHP_EOL', "\n");
  21. }
  22. }
  23. /**
  24. * Configuration object that triggers customizable behavior.
  25. *
  26. * @warning This class is strongly defined: that means that the class
  27. * will fail if an undefined directive is retrieved or set.
  28. *
  29. * @note Many classes that could (although many times don't) use the
  30. * configuration object make it a mandatory parameter. This is
  31. * because a configuration object should always be forwarded,
  32. * otherwise, you run the risk of missing a parameter and then
  33. * being stumped when a configuration directive doesn't work.
  34. */
  35. class HTMLPurifier_Config
  36. {
  37. /**
  38. * HTML Purifier's version
  39. */
  40. var $version = '2.1.5';
  41. /**
  42. * Two-level associative array of configuration directives
  43. */
  44. var $conf;
  45. /**
  46. * Reference HTMLPurifier_ConfigSchema for value checking
  47. */
  48. var $def;
  49. /**
  50. * Indexed array of definitions
  51. */
  52. var $definitions;
  53. /**
  54. * Bool indicator whether or not config is finalized
  55. */
  56. var $finalized = false;
  57. /**
  58. * Bool indicator whether or not to automatically finalize
  59. * the object if a read operation is done
  60. */
  61. var $autoFinalize = true;
  62. /**
  63. * Namespace indexed array of serials for specific namespaces (see
  64. * getSerial for more info).
  65. */
  66. var $serials = array();
  67. /**
  68. * Serial for entire configuration object
  69. */
  70. var $serial;
  71. /**
  72. * @param $definition HTMLPurifier_ConfigSchema that defines what directives
  73. * are allowed.
  74. */
  75. function HTMLPurifier_Config(&$definition) {
  76. $this->conf = $definition->defaults; // set up, copy in defaults
  77. $this->def = $definition; // keep a copy around for checking
  78. }
  79. /**
  80. * Convenience constructor that creates a config object based on a mixed var
  81. * @static
  82. * @param mixed $config Variable that defines the state of the config
  83. * object. Can be: a HTMLPurifier_Config() object,
  84. * an array of directives based on loadArray(),
  85. * or a string filename of an ini file.
  86. * @return Configured HTMLPurifier_Config object
  87. */
  88. function create($config) {
  89. if (is_a($config, 'HTMLPurifier_Config')) {
  90. // pass-through
  91. return $config;
  92. }
  93. $ret = HTMLPurifier_Config::createDefault();
  94. if (is_string($config)) $ret->loadIni($config);
  95. elseif (is_array($config)) $ret->loadArray($config);
  96. return $ret;
  97. }
  98. /**
  99. * Convenience constructor that creates a default configuration object.
  100. * @static
  101. * @return Default HTMLPurifier_Config object.
  102. */
  103. function createDefault() {
  104. $definition =& HTMLPurifier_ConfigSchema::instance();
  105. $config = new HTMLPurifier_Config($definition);
  106. return $config;
  107. }
  108. /**
  109. * Retreives a value from the configuration.
  110. * @param $namespace String namespace
  111. * @param $key String key
  112. */
  113. function get($namespace, $key, $from_alias = false) {
  114. if (!$this->finalized && $this->autoFinalize) $this->finalize();
  115. if (!isset($this->def->info[$namespace][$key])) {
  116. // can't add % due to SimpleTest bug
  117. trigger_error('Cannot retrieve value of undefined directive ' . htmlspecialchars("$namespace.$key"),
  118. E_USER_WARNING);
  119. return;
  120. }
  121. if ($this->def->info[$namespace][$key]->class == 'alias') {
  122. $d = $this->def->info[$namespace][$key];
  123. trigger_error('Cannot get value from aliased directive, use real name ' . $d->namespace . '.' . $d->name,
  124. E_USER_ERROR);
  125. return;
  126. }
  127. return $this->conf[$namespace][$key];
  128. }
  129. /**
  130. * Retreives an array of directives to values from a given namespace
  131. * @param $namespace String namespace
  132. */
  133. function getBatch($namespace) {
  134. if (!$this->finalized && $this->autoFinalize) $this->finalize();
  135. if (!isset($this->def->info[$namespace])) {
  136. trigger_error('Cannot retrieve undefined namespace ' . htmlspecialchars($namespace),
  137. E_USER_WARNING);
  138. return;
  139. }
  140. return $this->conf[$namespace];
  141. }
  142. /**
  143. * Returns a md5 signature of a segment of the configuration object
  144. * that uniquely identifies that particular configuration
  145. * @note Revision is handled specially and is removed from the batch
  146. * before processing!
  147. * @param $namespace Namespace to get serial for
  148. */
  149. function getBatchSerial($namespace) {
  150. if (empty($this->serials[$namespace])) {
  151. $batch = $this->getBatch($namespace);
  152. unset($batch['DefinitionRev']);
  153. $this->serials[$namespace] = md5(serialize($batch));
  154. }
  155. return $this->serials[$namespace];
  156. }
  157. /**
  158. * Returns a md5 signature for the entire configuration object
  159. * that uniquely identifies that particular configuration
  160. */
  161. function getSerial() {
  162. if (empty($this->serial)) {
  163. $this->serial = md5(serialize($this->getAll()));
  164. }
  165. return $this->serial;
  166. }
  167. /**
  168. * Retrieves all directives, organized by namespace
  169. */
  170. function getAll() {
  171. if (!$this->finalized && $this->autoFinalize) $this->finalize();
  172. return $this->conf;
  173. }
  174. /**
  175. * Sets a value to configuration.
  176. * @param $namespace String namespace
  177. * @param $key String key
  178. * @param $value Mixed value
  179. */
  180. function set($namespace, $key, $value, $from_alias = false) {
  181. if ($this->isFinalized('Cannot set directive after finalization')) return;
  182. if (!isset($this->def->info[$namespace][$key])) {
  183. trigger_error('Cannot set undefined directive ' . htmlspecialchars("$namespace.$key") . ' to value',
  184. E_USER_WARNING);
  185. return;
  186. }
  187. if ($this->def->info[$namespace][$key]->class == 'alias') {
  188. if ($from_alias) {
  189. trigger_error('Double-aliases not allowed, please fix '.
  190. 'ConfigSchema bug with' . "$namespace.$key");
  191. }
  192. $this->set($this->def->info[$namespace][$key]->namespace,
  193. $this->def->info[$namespace][$key]->name,
  194. $value, true);
  195. return;
  196. }
  197. $value = $this->def->validate(
  198. $value,
  199. $type = $this->def->info[$namespace][$key]->type,
  200. $this->def->info[$namespace][$key]->allow_null
  201. );
  202. if (is_string($value)) {
  203. // resolve value alias if defined
  204. if (isset($this->def->info[$namespace][$key]->aliases[$value])) {
  205. $value = $this->def->info[$namespace][$key]->aliases[$value];
  206. }
  207. if ($this->def->info[$namespace][$key]->allowed !== true) {
  208. // check to see if the value is allowed
  209. if (!isset($this->def->info[$namespace][$key]->allowed[$value])) {
  210. trigger_error('Value not supported, valid values are: ' .
  211. $this->_listify($this->def->info[$namespace][$key]->allowed), E_USER_WARNING);
  212. return;
  213. }
  214. }
  215. }
  216. if ($this->def->isError($value)) {
  217. trigger_error('Value for ' . "$namespace.$key" . ' is of invalid type, should be ' . $type, E_USER_WARNING);
  218. return;
  219. }
  220. $this->conf[$namespace][$key] = $value;
  221. // reset definitions if the directives they depend on changed
  222. // this is a very costly process, so it's discouraged
  223. // with finalization
  224. if ($namespace == 'HTML' || $namespace == 'CSS') {
  225. $this->definitions[$namespace] = null;
  226. }
  227. $this->serials[$namespace] = false;
  228. }
  229. /**
  230. * Convenience function for error reporting
  231. * @private
  232. */
  233. function _listify($lookup) {
  234. $list = array();
  235. foreach ($lookup as $name => $b) $list[] = $name;
  236. return implode(', ', $list);
  237. }
  238. /**
  239. * Retrieves reference to the HTML definition.
  240. * @param $raw Return a copy that has not been setup yet. Must be
  241. * called before it's been setup, otherwise won't work.
  242. */
  243. function &getHTMLDefinition($raw = false) {
  244. $def =& $this->getDefinition('HTML', $raw);
  245. return $def; // prevent PHP 4.4.0 from complaining
  246. }
  247. /**
  248. * Retrieves reference to the CSS definition
  249. */
  250. function &getCSSDefinition($raw = false) {
  251. $def =& $this->getDefinition('CSS', $raw);
  252. return $def;
  253. }
  254. /**
  255. * Retrieves a definition
  256. * @param $type Type of definition: HTML, CSS, etc
  257. * @param $raw Whether or not definition should be returned raw
  258. */
  259. function &getDefinition($type, $raw = false) {
  260. if (!$this->finalized && $this->autoFinalize) $this->finalize();
  261. $factory = HTMLPurifier_DefinitionCacheFactory::instance();
  262. $cache = $factory->create($type, $this);
  263. if (!$raw) {
  264. // see if we can quickly supply a definition
  265. if (!empty($this->definitions[$type])) {
  266. if (!$this->definitions[$type]->setup) {
  267. $this->definitions[$type]->setup($this);
  268. $cache->set($this->definitions[$type], $this);
  269. }
  270. return $this->definitions[$type];
  271. }
  272. // memory check missed, try cache
  273. $this->definitions[$type] = $cache->get($this);
  274. if ($this->definitions[$type]) {
  275. // definition in cache, return it
  276. return $this->definitions[$type];
  277. }
  278. } elseif (
  279. !empty($this->definitions[$type]) &&
  280. !$this->definitions[$type]->setup
  281. ) {
  282. // raw requested, raw in memory, quick return
  283. return $this->definitions[$type];
  284. }
  285. // quick checks failed, let's create the object
  286. if ($type == 'HTML') {
  287. $this->definitions[$type] = new HTMLPurifier_HTMLDefinition();
  288. } elseif ($type == 'CSS') {
  289. $this->definitions[$type] = new HTMLPurifier_CSSDefinition();
  290. } elseif ($type == 'URI') {
  291. $this->definitions[$type] = new HTMLPurifier_URIDefinition();
  292. } else {
  293. trigger_error("Definition of $type type not supported");
  294. $false = false;
  295. return $false;
  296. }
  297. // quick abort if raw
  298. if ($raw) {
  299. if (is_null($this->get($type, 'DefinitionID'))) {
  300. // fatally error out if definition ID not set
  301. trigger_error("Cannot retrieve raw version without specifying %$type.DefinitionID", E_USER_ERROR);
  302. $false = new HTMLPurifier_Error();
  303. return $false;
  304. }
  305. return $this->definitions[$type];
  306. }
  307. // set it up
  308. $this->definitions[$type]->setup($this);
  309. // save in cache
  310. $cache->set($this->definitions[$type], $this);
  311. return $this->definitions[$type];
  312. }
  313. /**
  314. * Loads configuration values from an array with the following structure:
  315. * Namespace.Directive => Value
  316. * @param $config_array Configuration associative array
  317. */
  318. function loadArray($config_array) {
  319. if ($this->isFinalized('Cannot load directives after finalization')) return;
  320. foreach ($config_array as $key => $value) {
  321. $key = str_replace('_', '.', $key);
  322. if (strpos($key, '.') !== false) {
  323. // condensed form
  324. list($namespace, $directive) = explode('.', $key);
  325. $this->set($namespace, $directive, $value);
  326. } else {
  327. $namespace = $key;
  328. $namespace_values = $value;
  329. foreach ($namespace_values as $directive => $value) {
  330. $this->set($namespace, $directive, $value);
  331. }
  332. }
  333. }
  334. }
  335. /**
  336. * Returns a list of array(namespace, directive) for all directives
  337. * that are allowed in a web-form context as per an allowed
  338. * namespaces/directives list.
  339. * @param $allowed List of allowed namespaces/directives
  340. * @static
  341. */
  342. function getAllowedDirectivesForForm($allowed) {
  343. $schema = HTMLPurifier_ConfigSchema::instance();
  344. if ($allowed !== true) {
  345. if (is_string($allowed)) $allowed = array($allowed);
  346. $allowed_ns = array();
  347. $allowed_directives = array();
  348. $blacklisted_directives = array();
  349. foreach ($allowed as $ns_or_directive) {
  350. if (strpos($ns_or_directive, '.') !== false) {
  351. // directive
  352. if ($ns_or_directive[0] == '-') {
  353. $blacklisted_directives[substr($ns_or_directive, 1)] = true;
  354. } else {
  355. $allowed_directives[$ns_or_directive] = true;
  356. }
  357. } else {
  358. // namespace
  359. $allowed_ns[$ns_or_directive] = true;
  360. }
  361. }
  362. }
  363. $ret = array();
  364. foreach ($schema->info as $ns => $keypairs) {
  365. foreach ($keypairs as $directive => $def) {
  366. if ($allowed !== true) {
  367. if (isset($blacklisted_directives["$ns.$directive"])) continue;
  368. if (!isset($allowed_directives["$ns.$directive"]) && !isset($allowed_ns[$ns])) continue;
  369. }
  370. if ($def->class == 'alias') continue;
  371. if ($directive == 'DefinitionID' || $directive == 'DefinitionRev') continue;
  372. $ret[] = array($ns, $directive);
  373. }
  374. }
  375. return $ret;
  376. }
  377. /**
  378. * Loads configuration values from $_GET/$_POST that were posted
  379. * via ConfigForm
  380. * @param $array $_GET or $_POST array to import
  381. * @param $index Index/name that the config variables are in
  382. * @param $allowed List of allowed namespaces/directives
  383. * @param $mq_fix Boolean whether or not to enable magic quotes fix
  384. * @static
  385. */
  386. function loadArrayFromForm($array, $index, $allowed = true, $mq_fix = true) {
  387. $ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix);
  388. $config = HTMLPurifier_Config::create($ret);
  389. return $config;
  390. }
  391. /**
  392. * Merges in configuration values from $_GET/$_POST to object. NOT STATIC.
  393. * @note Same parameters as loadArrayFromForm
  394. */
  395. function mergeArrayFromForm($array, $index, $allowed = true, $mq_fix = true) {
  396. $ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix);
  397. $this->loadArray($ret);
  398. }
  399. /**
  400. * Prepares an array from a form into something usable for the more
  401. * strict parts of HTMLPurifier_Config
  402. * @static
  403. */
  404. function prepareArrayFromForm($array, $index, $allowed = true, $mq_fix = true) {
  405. $array = (isset($array[$index]) && is_array($array[$index])) ? $array[$index] : array();
  406. $mq = get_magic_quotes_gpc() && $mq_fix;
  407. $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed);
  408. $ret = array();
  409. foreach ($allowed as $key) {
  410. list($ns, $directive) = $key;
  411. $skey = "$ns.$directive";
  412. if (!empty($array["Null_$skey"])) {
  413. $ret[$ns][$directive] = null;
  414. continue;
  415. }
  416. if (!isset($array[$skey])) continue;
  417. $value = $mq ? stripslashes($array[$skey]) : $array[$skey];
  418. $ret[$ns][$directive] = $value;
  419. }
  420. return $ret;
  421. }
  422. /**
  423. * Loads configuration values from an ini file
  424. * @param $filename Name of ini file
  425. */
  426. function loadIni($filename) {
  427. if ($this->isFinalized('Cannot load directives after finalization')) return;
  428. $array = parse_ini_file($filename, true);
  429. $this->loadArray($array);
  430. }
  431. /**
  432. * Checks whether or not the configuration object is finalized.
  433. * @param $error String error message, or false for no error
  434. */
  435. function isFinalized($error = false) {
  436. if ($this->finalized && $error) {
  437. trigger_error($error, E_USER_ERROR);
  438. }
  439. return $this->finalized;
  440. }
  441. /**
  442. * Finalizes configuration only if auto finalize is on and not
  443. * already finalized
  444. */
  445. function autoFinalize() {
  446. if (!$this->finalized && $this->autoFinalize) $this->finalize();
  447. }
  448. /**
  449. * Finalizes a configuration object, prohibiting further change
  450. */
  451. function finalize() {
  452. $this->finalized = true;
  453. }
  454. }