You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

912 lines
31KB

  1. <?php
  2. /**
  3. * Configuration object that triggers customizable behavior.
  4. *
  5. * @warning This class is strongly defined: that means that the class
  6. * will fail if an undefined directive is retrieved or set.
  7. *
  8. * @note Many classes that could (although many times don't) use the
  9. * configuration object make it a mandatory parameter. This is
  10. * because a configuration object should always be forwarded,
  11. * otherwise, you run the risk of missing a parameter and then
  12. * being stumped when a configuration directive doesn't work.
  13. *
  14. * @todo Reconsider some of the public member variables
  15. */
  16. class HTMLPurifier_Config
  17. {
  18. /**
  19. * HTML Purifier's version
  20. * @type string
  21. */
  22. public $version = '4.6.0';
  23. /**
  24. * Whether or not to automatically finalize
  25. * the object if a read operation is done.
  26. * @type bool
  27. */
  28. public $autoFinalize = true;
  29. // protected member variables
  30. /**
  31. * Namespace indexed array of serials for specific namespaces.
  32. * @see getSerial() for more info.
  33. * @type string[]
  34. */
  35. protected $serials = array();
  36. /**
  37. * Serial for entire configuration object.
  38. * @type string
  39. */
  40. protected $serial;
  41. /**
  42. * Parser for variables.
  43. * @type HTMLPurifier_VarParser_Flexible
  44. */
  45. protected $parser = null;
  46. /**
  47. * Reference HTMLPurifier_ConfigSchema for value checking.
  48. * @type HTMLPurifier_ConfigSchema
  49. * @note This is public for introspective purposes. Please don't
  50. * abuse!
  51. */
  52. public $def;
  53. /**
  54. * Indexed array of definitions.
  55. * @type HTMLPurifier_Definition[]
  56. */
  57. protected $definitions;
  58. /**
  59. * Whether or not config is finalized.
  60. * @type bool
  61. */
  62. protected $finalized = false;
  63. /**
  64. * Property list containing configuration directives.
  65. * @type array
  66. */
  67. protected $plist;
  68. /**
  69. * Whether or not a set is taking place due to an alias lookup.
  70. * @type bool
  71. */
  72. private $aliasMode;
  73. /**
  74. * Set to false if you do not want line and file numbers in errors.
  75. * (useful when unit testing). This will also compress some errors
  76. * and exceptions.
  77. * @type bool
  78. */
  79. public $chatty = true;
  80. /**
  81. * Current lock; only gets to this namespace are allowed.
  82. * @type string
  83. */
  84. private $lock;
  85. /**
  86. * Constructor
  87. * @param HTMLPurifier_ConfigSchema $definition ConfigSchema that defines
  88. * what directives are allowed.
  89. * @param HTMLPurifier_PropertyList $parent
  90. */
  91. public function __construct($definition, $parent = null)
  92. {
  93. $parent = $parent ? $parent : $definition->defaultPlist;
  94. $this->plist = new HTMLPurifier_PropertyList($parent);
  95. $this->def = $definition; // keep a copy around for checking
  96. $this->parser = new HTMLPurifier_VarParser_Flexible();
  97. }
  98. /**
  99. * Convenience constructor that creates a config object based on a mixed var
  100. * @param mixed $config Variable that defines the state of the config
  101. * object. Can be: a HTMLPurifier_Config() object,
  102. * an array of directives based on loadArray(),
  103. * or a string filename of an ini file.
  104. * @param HTMLPurifier_ConfigSchema $schema Schema object
  105. * @return HTMLPurifier_Config Configured object
  106. */
  107. public static function create($config, $schema = null)
  108. {
  109. if ($config instanceof HTMLPurifier_Config) {
  110. // pass-through
  111. return $config;
  112. }
  113. if (!$schema) {
  114. $ret = HTMLPurifier_Config::createDefault();
  115. } else {
  116. $ret = new HTMLPurifier_Config($schema);
  117. }
  118. if (is_string($config)) {
  119. $ret->loadIni($config);
  120. } elseif (is_array($config)) $ret->loadArray($config);
  121. return $ret;
  122. }
  123. /**
  124. * Creates a new config object that inherits from a previous one.
  125. * @param HTMLPurifier_Config $config Configuration object to inherit from.
  126. * @return HTMLPurifier_Config object with $config as its parent.
  127. */
  128. public static function inherit(HTMLPurifier_Config $config)
  129. {
  130. return new HTMLPurifier_Config($config->def, $config->plist);
  131. }
  132. /**
  133. * Convenience constructor that creates a default configuration object.
  134. * @return HTMLPurifier_Config default object.
  135. */
  136. public static function createDefault()
  137. {
  138. $definition = HTMLPurifier_ConfigSchema::instance();
  139. $config = new HTMLPurifier_Config($definition);
  140. return $config;
  141. }
  142. /**
  143. * Retrieves a value from the configuration.
  144. *
  145. * @param string $key String key
  146. * @param mixed $a
  147. *
  148. * @return mixed
  149. */
  150. public function get($key, $a = null)
  151. {
  152. if ($a !== null) {
  153. $this->triggerError(
  154. "Using deprecated API: use \$config->get('$key.$a') instead",
  155. E_USER_WARNING
  156. );
  157. $key = "$key.$a";
  158. }
  159. if (!$this->finalized) {
  160. $this->autoFinalize();
  161. }
  162. if (!isset($this->def->info[$key])) {
  163. // can't add % due to SimpleTest bug
  164. $this->triggerError(
  165. 'Cannot retrieve value of undefined directive ' . htmlspecialchars($key),
  166. E_USER_WARNING
  167. );
  168. return;
  169. }
  170. if (isset($this->def->info[$key]->isAlias)) {
  171. $d = $this->def->info[$key];
  172. $this->triggerError(
  173. 'Cannot get value from aliased directive, use real name ' . $d->key,
  174. E_USER_ERROR
  175. );
  176. return;
  177. }
  178. if ($this->lock) {
  179. list($ns) = explode('.', $key);
  180. if ($ns !== $this->lock) {
  181. $this->triggerError(
  182. 'Cannot get value of namespace ' . $ns . ' when lock for ' .
  183. $this->lock .
  184. ' is active, this probably indicates a Definition setup method ' .
  185. 'is accessing directives that are not within its namespace',
  186. E_USER_ERROR
  187. );
  188. return;
  189. }
  190. }
  191. return $this->plist->get($key);
  192. }
  193. /**
  194. * Retrieves an array of directives to values from a given namespace
  195. *
  196. * @param string $namespace String namespace
  197. *
  198. * @return array
  199. */
  200. public function getBatch($namespace)
  201. {
  202. if (!$this->finalized) {
  203. $this->autoFinalize();
  204. }
  205. $full = $this->getAll();
  206. if (!isset($full[$namespace])) {
  207. $this->triggerError(
  208. 'Cannot retrieve undefined namespace ' .
  209. htmlspecialchars($namespace),
  210. E_USER_WARNING
  211. );
  212. return;
  213. }
  214. return $full[$namespace];
  215. }
  216. /**
  217. * Returns a SHA-1 signature of a segment of the configuration object
  218. * that uniquely identifies that particular configuration
  219. *
  220. * @param string $namespace Namespace to get serial for
  221. *
  222. * @return string
  223. * @note Revision is handled specially and is removed from the batch
  224. * before processing!
  225. */
  226. public function getBatchSerial($namespace)
  227. {
  228. if (empty($this->serials[$namespace])) {
  229. $batch = $this->getBatch($namespace);
  230. unset($batch['DefinitionRev']);
  231. $this->serials[$namespace] = sha1(serialize($batch));
  232. }
  233. return $this->serials[$namespace];
  234. }
  235. /**
  236. * Returns a SHA-1 signature for the entire configuration object
  237. * that uniquely identifies that particular configuration
  238. *
  239. * @return string
  240. */
  241. public function getSerial()
  242. {
  243. if (empty($this->serial)) {
  244. $this->serial = sha1(serialize($this->getAll()));
  245. }
  246. return $this->serial;
  247. }
  248. /**
  249. * Retrieves all directives, organized by namespace
  250. *
  251. * @warning This is a pretty inefficient function, avoid if you can
  252. */
  253. public function getAll()
  254. {
  255. if (!$this->finalized) {
  256. $this->autoFinalize();
  257. }
  258. $ret = array();
  259. foreach ($this->plist->squash() as $name => $value) {
  260. list($ns, $key) = explode('.', $name, 2);
  261. $ret[$ns][$key] = $value;
  262. }
  263. return $ret;
  264. }
  265. /**
  266. * Sets a value to configuration.
  267. *
  268. * @param string $key key
  269. * @param mixed $value value
  270. * @param mixed $a
  271. */
  272. public function set($key, $value, $a = null)
  273. {
  274. if (strpos($key, '.') === false) {
  275. $namespace = $key;
  276. $directive = $value;
  277. $value = $a;
  278. $key = "$key.$directive";
  279. $this->triggerError("Using deprecated API: use \$config->set('$key', ...) instead", E_USER_NOTICE);
  280. } else {
  281. list($namespace) = explode('.', $key);
  282. }
  283. if ($this->isFinalized('Cannot set directive after finalization')) {
  284. return;
  285. }
  286. if (!isset($this->def->info[$key])) {
  287. $this->triggerError(
  288. 'Cannot set undefined directive ' . htmlspecialchars($key) . ' to value',
  289. E_USER_WARNING
  290. );
  291. return;
  292. }
  293. $def = $this->def->info[$key];
  294. if (isset($def->isAlias)) {
  295. if ($this->aliasMode) {
  296. $this->triggerError(
  297. 'Double-aliases not allowed, please fix '.
  298. 'ConfigSchema bug with' . $key,
  299. E_USER_ERROR
  300. );
  301. return;
  302. }
  303. $this->aliasMode = true;
  304. $this->set($def->key, $value);
  305. $this->aliasMode = false;
  306. $this->triggerError("$key is an alias, preferred directive name is {$def->key}", E_USER_NOTICE);
  307. return;
  308. }
  309. // Raw type might be negative when using the fully optimized form
  310. // of stdclass, which indicates allow_null == true
  311. $rtype = is_int($def) ? $def : $def->type;
  312. if ($rtype < 0) {
  313. $type = -$rtype;
  314. $allow_null = true;
  315. } else {
  316. $type = $rtype;
  317. $allow_null = isset($def->allow_null);
  318. }
  319. try {
  320. $value = $this->parser->parse($value, $type, $allow_null);
  321. } catch (HTMLPurifier_VarParserException $e) {
  322. $this->triggerError(
  323. 'Value for ' . $key . ' is of invalid type, should be ' .
  324. HTMLPurifier_VarParser::getTypeName($type),
  325. E_USER_WARNING
  326. );
  327. return;
  328. }
  329. if (is_string($value) && is_object($def)) {
  330. // resolve value alias if defined
  331. if (isset($def->aliases[$value])) {
  332. $value = $def->aliases[$value];
  333. }
  334. // check to see if the value is allowed
  335. if (isset($def->allowed) && !isset($def->allowed[$value])) {
  336. $this->triggerError(
  337. 'Value not supported, valid values are: ' .
  338. $this->_listify($def->allowed),
  339. E_USER_WARNING
  340. );
  341. return;
  342. }
  343. }
  344. $this->plist->set($key, $value);
  345. // reset definitions if the directives they depend on changed
  346. // this is a very costly process, so it's discouraged
  347. // with finalization
  348. if ($namespace == 'HTML' || $namespace == 'CSS' || $namespace == 'URI') {
  349. $this->definitions[$namespace] = null;
  350. }
  351. $this->serials[$namespace] = false;
  352. }
  353. /**
  354. * Convenience function for error reporting
  355. *
  356. * @param array $lookup
  357. *
  358. * @return string
  359. */
  360. private function _listify($lookup)
  361. {
  362. $list = array();
  363. foreach ($lookup as $name => $b) {
  364. $list[] = $name;
  365. }
  366. return implode(', ', $list);
  367. }
  368. /**
  369. * Retrieves object reference to the HTML definition.
  370. *
  371. * @param bool $raw Return a copy that has not been setup yet. Must be
  372. * called before it's been setup, otherwise won't work.
  373. * @param bool $optimized If true, this method may return null, to
  374. * indicate that a cached version of the modified
  375. * definition object is available and no further edits
  376. * are necessary. Consider using
  377. * maybeGetRawHTMLDefinition, which is more explicitly
  378. * named, instead.
  379. *
  380. * @return HTMLPurifier_HTMLDefinition
  381. */
  382. public function getHTMLDefinition($raw = false, $optimized = false)
  383. {
  384. return $this->getDefinition('HTML', $raw, $optimized);
  385. }
  386. /**
  387. * Retrieves object reference to the CSS definition
  388. *
  389. * @param bool $raw Return a copy that has not been setup yet. Must be
  390. * called before it's been setup, otherwise won't work.
  391. * @param bool $optimized If true, this method may return null, to
  392. * indicate that a cached version of the modified
  393. * definition object is available and no further edits
  394. * are necessary. Consider using
  395. * maybeGetRawCSSDefinition, which is more explicitly
  396. * named, instead.
  397. *
  398. * @return HTMLPurifier_CSSDefinition
  399. */
  400. public function getCSSDefinition($raw = false, $optimized = false)
  401. {
  402. return $this->getDefinition('CSS', $raw, $optimized);
  403. }
  404. /**
  405. * Retrieves object reference to the URI definition
  406. *
  407. * @param bool $raw Return a copy that has not been setup yet. Must be
  408. * called before it's been setup, otherwise won't work.
  409. * @param bool $optimized If true, this method may return null, to
  410. * indicate that a cached version of the modified
  411. * definition object is available and no further edits
  412. * are necessary. Consider using
  413. * maybeGetRawURIDefinition, which is more explicitly
  414. * named, instead.
  415. *
  416. * @return HTMLPurifier_URIDefinition
  417. */
  418. public function getURIDefinition($raw = false, $optimized = false)
  419. {
  420. return $this->getDefinition('URI', $raw, $optimized);
  421. }
  422. /**
  423. * Retrieves a definition
  424. *
  425. * @param string $type Type of definition: HTML, CSS, etc
  426. * @param bool $raw Whether or not definition should be returned raw
  427. * @param bool $optimized Only has an effect when $raw is true. Whether
  428. * or not to return null if the result is already present in
  429. * the cache. This is off by default for backwards
  430. * compatibility reasons, but you need to do things this
  431. * way in order to ensure that caching is done properly.
  432. * Check out enduser-customize.html for more details.
  433. * We probably won't ever change this default, as much as the
  434. * maybe semantics is the "right thing to do."
  435. *
  436. * @throws HTMLPurifier_Exception
  437. * @return HTMLPurifier_Definition
  438. */
  439. public function getDefinition($type, $raw = false, $optimized = false)
  440. {
  441. if ($optimized && !$raw) {
  442. throw new HTMLPurifier_Exception("Cannot set optimized = true when raw = false");
  443. }
  444. if (!$this->finalized) {
  445. $this->autoFinalize();
  446. }
  447. // temporarily suspend locks, so we can handle recursive definition calls
  448. $lock = $this->lock;
  449. $this->lock = null;
  450. $factory = HTMLPurifier_DefinitionCacheFactory::instance();
  451. $cache = $factory->create($type, $this);
  452. $this->lock = $lock;
  453. if (!$raw) {
  454. // full definition
  455. // ---------------
  456. // check if definition is in memory
  457. if (!empty($this->definitions[$type])) {
  458. $def = $this->definitions[$type];
  459. // check if the definition is setup
  460. if ($def->setup) {
  461. return $def;
  462. } else {
  463. $def->setup($this);
  464. if ($def->optimized) {
  465. $cache->add($def, $this);
  466. }
  467. return $def;
  468. }
  469. }
  470. // check if definition is in cache
  471. $def = $cache->get($this);
  472. if ($def) {
  473. // definition in cache, save to memory and return it
  474. $this->definitions[$type] = $def;
  475. return $def;
  476. }
  477. // initialize it
  478. $def = $this->initDefinition($type);
  479. // set it up
  480. $this->lock = $type;
  481. $def->setup($this);
  482. $this->lock = null;
  483. // save in cache
  484. $cache->add($def, $this);
  485. // return it
  486. return $def;
  487. } else {
  488. // raw definition
  489. // --------------
  490. // check preconditions
  491. $def = null;
  492. if ($optimized) {
  493. if (is_null($this->get($type . '.DefinitionID'))) {
  494. // fatally error out if definition ID not set
  495. throw new HTMLPurifier_Exception(
  496. "Cannot retrieve raw version without specifying %$type.DefinitionID"
  497. );
  498. }
  499. }
  500. if (!empty($this->definitions[$type])) {
  501. $def = $this->definitions[$type];
  502. if ($def->setup && !$optimized) {
  503. $extra = $this->chatty ?
  504. " (try moving this code block earlier in your initialization)" :
  505. "";
  506. throw new HTMLPurifier_Exception(
  507. "Cannot retrieve raw definition after it has already been setup" .
  508. $extra
  509. );
  510. }
  511. if ($def->optimized === null) {
  512. $extra = $this->chatty ? " (try flushing your cache)" : "";
  513. throw new HTMLPurifier_Exception(
  514. "Optimization status of definition is unknown" . $extra
  515. );
  516. }
  517. if ($def->optimized !== $optimized) {
  518. $msg = $optimized ? "optimized" : "unoptimized";
  519. $extra = $this->chatty ?
  520. " (this backtrace is for the first inconsistent call, which was for a $msg raw definition)"
  521. : "";
  522. throw new HTMLPurifier_Exception(
  523. "Inconsistent use of optimized and unoptimized raw definition retrievals" . $extra
  524. );
  525. }
  526. }
  527. // check if definition was in memory
  528. if ($def) {
  529. if ($def->setup) {
  530. // invariant: $optimized === true (checked above)
  531. return null;
  532. } else {
  533. return $def;
  534. }
  535. }
  536. // if optimized, check if definition was in cache
  537. // (because we do the memory check first, this formulation
  538. // is prone to cache slamming, but I think
  539. // guaranteeing that either /all/ of the raw
  540. // setup code or /none/ of it is run is more important.)
  541. if ($optimized) {
  542. // This code path only gets run once; once we put
  543. // something in $definitions (which is guaranteed by the
  544. // trailing code), we always short-circuit above.
  545. $def = $cache->get($this);
  546. if ($def) {
  547. // save the full definition for later, but don't
  548. // return it yet
  549. $this->definitions[$type] = $def;
  550. return null;
  551. }
  552. }
  553. // check invariants for creation
  554. if (!$optimized) {
  555. if (!is_null($this->get($type . '.DefinitionID'))) {
  556. if ($this->chatty) {
  557. $this->triggerError(
  558. 'Due to a documentation error in previous version of HTML Purifier, your ' .
  559. 'definitions are not being cached. If this is OK, you can remove the ' .
  560. '%$type.DefinitionRev and %$type.DefinitionID declaration. Otherwise, ' .
  561. 'modify your code to use maybeGetRawDefinition, and test if the returned ' .
  562. 'value is null before making any edits (if it is null, that means that a ' .
  563. 'cached version is available, and no raw operations are necessary). See ' .
  564. '<a href="http://htmlpurifier.org/docs/enduser-customize.html#optimized">' .
  565. 'Customize</a> for more details',
  566. E_USER_WARNING
  567. );
  568. } else {
  569. $this->triggerError(
  570. "Useless DefinitionID declaration",
  571. E_USER_WARNING
  572. );
  573. }
  574. }
  575. }
  576. // initialize it
  577. $def = $this->initDefinition($type);
  578. $def->optimized = $optimized;
  579. return $def;
  580. }
  581. throw new HTMLPurifier_Exception("The impossible happened!");
  582. }
  583. /**
  584. * Initialise definition
  585. *
  586. * @param string $type What type of definition to create
  587. *
  588. * @return HTMLPurifier_CSSDefinition|HTMLPurifier_HTMLDefinition|HTMLPurifier_URIDefinition
  589. * @throws HTMLPurifier_Exception
  590. */
  591. private function initDefinition($type)
  592. {
  593. // quick checks failed, let's create the object
  594. if ($type == 'HTML') {
  595. $def = new HTMLPurifier_HTMLDefinition();
  596. } elseif ($type == 'CSS') {
  597. $def = new HTMLPurifier_CSSDefinition();
  598. } elseif ($type == 'URI') {
  599. $def = new HTMLPurifier_URIDefinition();
  600. } else {
  601. throw new HTMLPurifier_Exception(
  602. "Definition of $type type not supported"
  603. );
  604. }
  605. $this->definitions[$type] = $def;
  606. return $def;
  607. }
  608. public function maybeGetRawDefinition($name)
  609. {
  610. return $this->getDefinition($name, true, true);
  611. }
  612. public function maybeGetRawHTMLDefinition()
  613. {
  614. return $this->getDefinition('HTML', true, true);
  615. }
  616. public function maybeGetRawCSSDefinition()
  617. {
  618. return $this->getDefinition('CSS', true, true);
  619. }
  620. public function maybeGetRawURIDefinition()
  621. {
  622. return $this->getDefinition('URI', true, true);
  623. }
  624. /**
  625. * Loads configuration values from an array with the following structure:
  626. * Namespace.Directive => Value
  627. *
  628. * @param array $config_array Configuration associative array
  629. */
  630. public function loadArray($config_array)
  631. {
  632. if ($this->isFinalized('Cannot load directives after finalization')) {
  633. return;
  634. }
  635. foreach ($config_array as $key => $value) {
  636. $key = str_replace('_', '.', $key);
  637. if (strpos($key, '.') !== false) {
  638. $this->set($key, $value);
  639. } else {
  640. $namespace = $key;
  641. $namespace_values = $value;
  642. foreach ($namespace_values as $directive => $value2) {
  643. $this->set($namespace .'.'. $directive, $value2);
  644. }
  645. }
  646. }
  647. }
  648. /**
  649. * Returns a list of array(namespace, directive) for all directives
  650. * that are allowed in a web-form context as per an allowed
  651. * namespaces/directives list.
  652. *
  653. * @param array $allowed List of allowed namespaces/directives
  654. * @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy
  655. *
  656. * @return array
  657. */
  658. public static function getAllowedDirectivesForForm($allowed, $schema = null)
  659. {
  660. if (!$schema) {
  661. $schema = HTMLPurifier_ConfigSchema::instance();
  662. }
  663. if ($allowed !== true) {
  664. if (is_string($allowed)) {
  665. $allowed = array($allowed);
  666. }
  667. $allowed_ns = array();
  668. $allowed_directives = array();
  669. $blacklisted_directives = array();
  670. foreach ($allowed as $ns_or_directive) {
  671. if (strpos($ns_or_directive, '.') !== false) {
  672. // directive
  673. if ($ns_or_directive[0] == '-') {
  674. $blacklisted_directives[substr($ns_or_directive, 1)] = true;
  675. } else {
  676. $allowed_directives[$ns_or_directive] = true;
  677. }
  678. } else {
  679. // namespace
  680. $allowed_ns[$ns_or_directive] = true;
  681. }
  682. }
  683. }
  684. $ret = array();
  685. foreach ($schema->info as $key => $def) {
  686. list($ns, $directive) = explode('.', $key, 2);
  687. if ($allowed !== true) {
  688. if (isset($blacklisted_directives["$ns.$directive"])) {
  689. continue;
  690. }
  691. if (!isset($allowed_directives["$ns.$directive"]) && !isset($allowed_ns[$ns])) {
  692. continue;
  693. }
  694. }
  695. if (isset($def->isAlias)) {
  696. continue;
  697. }
  698. if ($directive == 'DefinitionID' || $directive == 'DefinitionRev') {
  699. continue;
  700. }
  701. $ret[] = array($ns, $directive);
  702. }
  703. return $ret;
  704. }
  705. /**
  706. * Loads configuration values from $_GET/$_POST that were posted
  707. * via ConfigForm
  708. *
  709. * @param array $array $_GET or $_POST array to import
  710. * @param string|bool $index Index/name that the config variables are in
  711. * @param array|bool $allowed List of allowed namespaces/directives
  712. * @param bool $mq_fix Boolean whether or not to enable magic quotes fix
  713. * @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy
  714. *
  715. * @return mixed
  716. */
  717. public static function loadArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null)
  718. {
  719. $ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix, $schema);
  720. $config = HTMLPurifier_Config::create($ret, $schema);
  721. return $config;
  722. }
  723. /**
  724. * Merges in configuration values from $_GET/$_POST to object. NOT STATIC.
  725. *
  726. * @param array $array $_GET or $_POST array to import
  727. * @param string|bool $index Index/name that the config variables are in
  728. * @param array|bool $allowed List of allowed namespaces/directives
  729. * @param bool $mq_fix Boolean whether or not to enable magic quotes fix
  730. */
  731. public function mergeArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true)
  732. {
  733. $ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix, $this->def);
  734. $this->loadArray($ret);
  735. }
  736. /**
  737. * Prepares an array from a form into something usable for the more
  738. * strict parts of HTMLPurifier_Config
  739. *
  740. * @param array $array $_GET or $_POST array to import
  741. * @param string|bool $index Index/name that the config variables are in
  742. * @param array|bool $allowed List of allowed namespaces/directives
  743. * @param bool $mq_fix Boolean whether or not to enable magic quotes fix
  744. * @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy
  745. *
  746. * @return array
  747. */
  748. public static function prepareArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null)
  749. {
  750. if ($index !== false) {
  751. $array = (isset($array[$index]) && is_array($array[$index])) ? $array[$index] : array();
  752. }
  753. $mq = $mq_fix && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc();
  754. $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $schema);
  755. $ret = array();
  756. foreach ($allowed as $key) {
  757. list($ns, $directive) = $key;
  758. $skey = "$ns.$directive";
  759. if (!empty($array["Null_$skey"])) {
  760. $ret[$ns][$directive] = null;
  761. continue;
  762. }
  763. if (!isset($array[$skey])) {
  764. continue;
  765. }
  766. $value = $mq ? stripslashes($array[$skey]) : $array[$skey];
  767. $ret[$ns][$directive] = $value;
  768. }
  769. return $ret;
  770. }
  771. /**
  772. * Loads configuration values from an ini file
  773. *
  774. * @param string $filename Name of ini file
  775. */
  776. public function loadIni($filename)
  777. {
  778. if ($this->isFinalized('Cannot load directives after finalization')) {
  779. return;
  780. }
  781. $array = parse_ini_file($filename, true);
  782. $this->loadArray($array);
  783. }
  784. /**
  785. * Checks whether or not the configuration object is finalized.
  786. *
  787. * @param string|bool $error String error message, or false for no error
  788. *
  789. * @return bool
  790. */
  791. public function isFinalized($error = false)
  792. {
  793. if ($this->finalized && $error) {
  794. $this->triggerError($error, E_USER_ERROR);
  795. }
  796. return $this->finalized;
  797. }
  798. /**
  799. * Finalizes configuration only if auto finalize is on and not
  800. * already finalized
  801. */
  802. public function autoFinalize()
  803. {
  804. if ($this->autoFinalize) {
  805. $this->finalize();
  806. } else {
  807. $this->plist->squash(true);
  808. }
  809. }
  810. /**
  811. * Finalizes a configuration object, prohibiting further change
  812. */
  813. public function finalize()
  814. {
  815. $this->finalized = true;
  816. $this->parser = null;
  817. }
  818. /**
  819. * Produces a nicely formatted error message by supplying the
  820. * stack frame information OUTSIDE of HTMLPurifier_Config.
  821. *
  822. * @param string $msg An error message
  823. * @param int $no An error number
  824. */
  825. protected function triggerError($msg, $no)
  826. {
  827. // determine previous stack frame
  828. $extra = '';
  829. if ($this->chatty) {
  830. $trace = debug_backtrace();
  831. // zip(tail(trace), trace) -- but PHP is not Haskell har har
  832. for ($i = 0, $c = count($trace); $i < $c - 1; $i++) {
  833. // XXX this is not correct on some versions of HTML Purifier
  834. if ($trace[$i + 1]['class'] === 'HTMLPurifier_Config') {
  835. continue;
  836. }
  837. $frame = $trace[$i];
  838. $extra = " invoked on line {$frame['line']} in file {$frame['file']}";
  839. break;
  840. }
  841. }
  842. trigger_error($msg . $extra, $no);
  843. }
  844. /**
  845. * Returns a serialized form of the configuration object that can
  846. * be reconstituted.
  847. *
  848. * @return string
  849. */
  850. public function serialize()
  851. {
  852. $this->getDefinition('HTML');
  853. $this->getDefinition('CSS');
  854. $this->getDefinition('URI');
  855. return serialize($this);
  856. }
  857. }
  858. // vim: et sw=4 sts=4