Retrieves an instance of the application-wide configuration definition.
public static instance ( HTMLPurifier_ConfigSchema $prototype = null ) : HTMLPurifier_ConfigSchema | ||
$prototype | HTMLPurifier_ConfigSchema | |
return | HTMLPurifier_ConfigSchema |
/** * {@inheritDoc} */ public static function instance($prototype = null) { $instance = parent::instance($prototype); $instance->add('AutoFormat.LinkifyWithTextLengthLimit.Limit', null, 'mixed', true); $instance->add('AutoFormat.LinkifyWithTextLengthLimit.Suffix', ' ...', 'string', true); $instance->add('AutoFormat.LinkifyWithTextLengthLimit.RemoveProtocol', true, 'bool', false); return $instance; }
public function register(Application $app) { parent::register($app); $app['form.secret'] = $app->share(function () use($app) { return md5('form_secret' . $app['salt']); }); $app['html_purifier.allowed_elements'] = ['p', 'ul', 'ol', 'li', 'b', 'i', 'strong', 'em', 'img', 'sub', 'sup', 'blockquote', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'address', 'br', 'dl', 'dt', 'dd']; $app['html_purifier.allowed_attributes'] = ['*.class', 'img.src', 'img.alt', 'a.href', 'a.title', 'td.abbr', 'td.colspan', 'td.rowspan', 'th.abbr', 'th.colspan', 'th.rowspan', 'table.summary']; $app['html_purifier.config'] = $app->share(function () use($app) { $config = new \HTMLPurifier_Config(\HTMLPurifier_ConfigSchema::instance()); $config->set('HTML.Doctype', 'XHTML 1.0 Strict'); $config->set('HTML.AllowedElements', implode(',', $app['html_purifier.allowed_elements'])); $config->set('HTML.AllowedAttributes', implode(',', $app['html_purifier.allowed_attributes'])); $config->set('AutoFormat.RemoveEmpty', true); $config->set('AutoFormat.AutoParagraph', true); $config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true); $cachePath = $app['paths.cache'] . '/htmlpurifier'; if (!is_dir($cachePath)) { mkdir($cachePath, 0777, true); } $config->set('Cache.SerializerPath', $cachePath); return $config; }); $app['html_purifier'] = $app->share(function () use($app) { return new \HTMLPurifier($app['html_purifier.config']); }); $app['form.html_purifier'] = $app->share(function () use($app) { return new HtmlPurifier($app['html_purifier']); }); $app['form.html_extension'] = $app->share(function () use($app) { return new HtmlExtension(new HtmlType($app['form.html_purifier']), new HtmlTypeGuesser($app['annotations.reader'])); }); $app['form.extensions'] = $app->share($app->extend('form.extensions', function (array $extensions) use($app) { $extensions[] = new DoctrineOrmExtension($app['orm.manager_registry']); $extensions[] = $app['form.html_extension']; return $extensions; })); $app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function (array $extensions) use($app) { $extensions[] = new FormTypeExtension(); $extensions[] = new DateTypeExtension(); $extensions[] = new TimeTypeExtension(); return $extensions; })); }
/** * Returns a list of array(namespace, directive) for all directives * that are allowed in a web-form context as per an allowed * namespaces/directives list. * * @param array $allowed List of allowed namespaces/directives * @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy * * @return array */ public static function getAllowedDirectivesForForm($allowed, $schema = null) { if (!$schema) { $schema = HTMLPurifier_ConfigSchema::instance(); } if ($allowed !== true) { if (is_string($allowed)) { $allowed = array($allowed); } $allowed_ns = array(); $allowed_directives = array(); $blacklisted_directives = array(); foreach ($allowed as $ns_or_directive) { if (strpos($ns_or_directive, '.') !== false) { // directive if ($ns_or_directive[0] == '-') { $blacklisted_directives[substr($ns_or_directive, 1)] = true; } else { $allowed_directives[$ns_or_directive] = true; } } else { // namespace $allowed_ns[$ns_or_directive] = true; } } } $ret = array(); foreach ($schema->info as $key => $def) { list($ns, $directive) = explode('.', $key, 2); if ($allowed !== true) { if (isset($blacklisted_directives["{$ns}.{$directive}"])) { continue; } if (!isset($allowed_directives["{$ns}.{$directive}"]) && !isset($allowed_ns[$ns])) { continue; } } if (isset($def->isAlias)) { continue; } if ($directive == 'DefinitionID' || $directive == 'DefinitionRev') { continue; } $ret[] = array($ns, $directive); } return $ret; }
<?php // overload default configuration schema temporarily $custom_schema = new HTMLPurifier_ConfigSchema(); $old = HTMLPurifier_ConfigSchema::instance(); $custom_schema =& HTMLPurifier_ConfigSchema::instance($custom_schema); HTMLPurifier_ConfigSchema::defineNamespace('Element', 'Chemical substances that cannot be further decomposed'); HTMLPurifier_ConfigSchema::define('Element', 'Abbr', 'H', 'string', 'Abbreviation of element name.'); HTMLPurifier_ConfigSchema::define('Element', 'Name', 'hydrogen', 'istring', 'Full name of atoms.'); HTMLPurifier_ConfigSchema::define('Element', 'Number', 1, 'int', 'Atomic number, is identity.'); HTMLPurifier_ConfigSchema::define('Element', 'Mass', 1.00794, 'float', 'Atomic mass.'); HTMLPurifier_ConfigSchema::define('Element', 'Radioactive', false, 'bool', 'Does it have rapid decay?'); HTMLPurifier_ConfigSchema::define('Element', 'Isotopes', array('1' => true, '2' => true, '3' => true), 'lookup', 'What numbers of neutrons for this element have been observed?'); HTMLPurifier_ConfigSchema::define('Element', 'Traits', array('nonmetallic', 'odorless', 'flammable'), 'list', 'What are general properties of the element?'); HTMLPurifier_ConfigSchema::define('Element', 'IsotopeNames', array('1' => 'protium', '2' => 'deuterium', '3' => 'tritium'), 'hash', 'Lookup hash of neutron counts to formal names.'); HTMLPurifier_ConfigSchema::defineNamespace('Instrument', 'Of the musical type.'); HTMLPurifier_ConfigSchema::define('Instrument', 'Manufacturer', 'Yamaha', 'string', 'Who made it?'); HTMLPurifier_ConfigSchema::defineAllowedValues('Instrument', 'Manufacturer', array('Yamaha', 'Conn-Selmer', 'Vandoren', 'Laubin', 'Buffet', 'other')); HTMLPurifier_ConfigSchema::defineValueAliases('Instrument', 'Manufacturer', array('Selmer' => 'Conn-Selmer')); HTMLPurifier_ConfigSchema::define('Instrument', 'Family', 'woodwind', 'istring', 'What family is it?'); HTMLPurifier_ConfigSchema::defineAllowedValues('Instrument', 'Family', array('brass', 'woodwind', 'percussion', 'string', 'keyboard', 'electronic')); HTMLPurifier_ConfigSchema::defineValueAliases('Instrument', 'Family', array('synth' => 'electronic')); HTMLPurifier_ConfigSchema::defineNamespace('ReportCard', 'It is for grades.'); HTMLPurifier_ConfigSchema::define('ReportCard', 'English', null, 'string/null', 'Grade from English class.'); HTMLPurifier_ConfigSchema::define('ReportCard', 'Absences', 0, 'int', 'How many times missing from school?'); HTMLPurifier_ConfigSchema::defineNamespace('Text', 'This stuff is long, boring, and English.'); HTMLPurifier_ConfigSchema::define('Text', 'AboutUs', 'Nothing much, but this should be decently long so that a textarea would be better', 'text', 'Who are we? What are we up to?'); HTMLPurifier_ConfigSchema::define('Text', 'Hash', "not-case-sensitive\nstill-not-case-sensitive\nsuper-not-case-sensitive", 'itext', 'This is of limited utility, but of course it ends up being used.');
/** @see HTMLPurifier_ConfigSchema->addAlias() */ public static function defineAlias($namespace, $name, $new_namespace, $new_name) { HTMLPurifier_ConfigSchema::deprecated(__METHOD__); $def = HTMLPurifier_ConfigSchema::instance(); $def->addAlias($namespace, $name, $new_namespace, $new_name); }
/** * Defines a directive alias for backwards compatibility * @param $namespace * @param $name Directive that will be aliased * @param $new_namespace * @param $new_name Directive that the alias will be to */ public static function defineAlias($namespace, $name, $new_namespace, $new_name) { $def =& HTMLPurifier_ConfigSchema::instance(); if (HTMLPURIFIER_SCHEMA_STRICT) { if (!isset($def->info[$namespace])) { trigger_error('Cannot define directive alias in undefined namespace', E_USER_ERROR); return; } if (!ctype_alnum($name)) { trigger_error('Directive name must be alphanumeric', E_USER_ERROR); return; } if (isset($def->info[$namespace][$name])) { trigger_error('Cannot define alias over directive', E_USER_ERROR); return; } if (!isset($def->info[$new_namespace][$new_name])) { trigger_error('Cannot define alias to undefined directive', E_USER_ERROR); return; } if ($def->info[$new_namespace][$new_name]->class == 'alias') { trigger_error('Cannot define alias to alias', E_USER_ERROR); return; } } $def->info[$namespace][$name] = new HTMLPurifier_ConfigDef_DirectiveAlias($new_namespace, $new_name); $def->info[$new_namespace][$new_name]->directiveAliases[] = "{$namespace}.{$name}"; }
<?php require_once 'common.php'; if (isset($_GET['doc'])) { if (file_exists('testSchema.html') && filemtime('testSchema.php') < filemtime('testSchema.html') && !isset($_GET['purge'])) { echo file_get_contents('testSchema.html'); exit; } if (version_compare('5', PHP_VERSION, '>')) { exit('Requires PHP 5 or higher.'); } // setup schema for parsing require_once 'testSchema.php'; $new_schema = $custom_schema; // dereference the reference HTMLPurifier_ConfigSchema::instance($old); // restore old version // setup ConfigDoc environment require_once '../configdoc/library/ConfigDoc.auto.php'; // perform the ConfigDoc generation $configdoc = new ConfigDoc(); $html = $configdoc->generate($new_schema, 'plain', array('css' => '../configdoc/styles/plain.css', 'title' => 'Sample Configuration Documentation')); $configdoc->cleanup(); file_put_contents('testSchema.html', $html); echo $html; exit; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
if (file_exists($file)) { trigger_error("File already exists; skipped {$name}"); return; } $file = new FSTools_File($file); $file->open('w'); $multiline = false; foreach ($hash as $key => $value) { $multiline = $multiline || strpos($value, "\n") !== false; if ($multiline) { $file->put("--{$key}--" . PHP_EOL); $file->put(str_replace("\n", PHP_EOL, $value) . PHP_EOL); } else { if ($key == 'ID') { $file->put("{$value}" . PHP_EOL); } else { $file->put("{$key}: {$value}" . PHP_EOL); } } } $file->close(); } $schema = HTMLPurifier_ConfigSchema::instance(); $adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($schema); foreach ($schema->info as $ns => $ns_array) { saveHash($adapter->get($ns)); foreach ($ns_array as $dir => $x) { saveHash($adapter->get($ns, $dir)); } } // vim: et sw=4 sts=4
/** * Creates a configuration object using the default config schema instance * * @return HTMLPurifier_Config */ public static function createDefault() { $schema = HTMLPurifier_ConfigSchema::instance(); $config = self::create(null, $schema); return $config; }