]>
Commit | Line | Data |
---|---|---|
1 | <?php\r | |
2 | \r | |
3 | class HTMLPurifier_DoctypeRegistry\r | |
4 | {\r | |
5 | \r | |
6 | /**\r | |
7 | * Hash of doctype names to doctype objects.\r | |
8 | * @type array\r | |
9 | */\r | |
10 | protected $doctypes;\r | |
11 | \r | |
12 | /**\r | |
13 | * Lookup table of aliases to real doctype names.\r | |
14 | * @type array\r | |
15 | */\r | |
16 | protected $aliases;\r | |
17 | \r | |
18 | /**\r | |
19 | * Registers a doctype to the registry\r | |
20 | * @note Accepts a fully-formed doctype object, or the\r | |
21 | * parameters for constructing a doctype object\r | |
22 | * @param string $doctype Name of doctype or literal doctype object\r | |
23 | * @param bool $xml\r | |
24 | * @param array $modules Modules doctype will load\r | |
25 | * @param array $tidy_modules Modules doctype will load for certain modes\r | |
26 | * @param array $aliases Alias names for doctype\r | |
27 | * @param string $dtd_public\r | |
28 | * @param string $dtd_system\r | |
29 | * @return HTMLPurifier_Doctype Editable registered doctype\r | |
30 | */\r | |
31 | public function register(\r | |
32 | $doctype,\r | |
33 | $xml = true,\r | |
34 | $modules = array(),\r | |
35 | $tidy_modules = array(),\r | |
36 | $aliases = array(),\r | |
37 | $dtd_public = null,\r | |
38 | $dtd_system = null\r | |
39 | ) {\r | |
40 | if (!is_array($modules)) {\r | |
41 | $modules = array($modules);\r | |
42 | }\r | |
43 | if (!is_array($tidy_modules)) {\r | |
44 | $tidy_modules = array($tidy_modules);\r | |
45 | }\r | |
46 | if (!is_array($aliases)) {\r | |
47 | $aliases = array($aliases);\r | |
48 | }\r | |
49 | if (!is_object($doctype)) {\r | |
50 | $doctype = new HTMLPurifier_Doctype(\r | |
51 | $doctype,\r | |
52 | $xml,\r | |
53 | $modules,\r | |
54 | $tidy_modules,\r | |
55 | $aliases,\r | |
56 | $dtd_public,\r | |
57 | $dtd_system\r | |
58 | );\r | |
59 | }\r | |
60 | $this->doctypes[$doctype->name] = $doctype;\r | |
61 | $name = $doctype->name;\r | |
62 | // hookup aliases\r | |
63 | foreach ($doctype->aliases as $alias) {\r | |
64 | if (isset($this->doctypes[$alias])) {\r | |
65 | continue;\r | |
66 | }\r | |
67 | $this->aliases[$alias] = $name;\r | |
68 | }\r | |
69 | // remove old aliases\r | |
70 | if (isset($this->aliases[$name])) {\r | |
71 | unset($this->aliases[$name]);\r | |
72 | }\r | |
73 | return $doctype;\r | |
74 | }\r | |
75 | \r | |
76 | /**\r | |
77 | * Retrieves reference to a doctype of a certain name\r | |
78 | * @note This function resolves aliases\r | |
79 | * @note When possible, use the more fully-featured make()\r | |
80 | * @param string $doctype Name of doctype\r | |
81 | * @return HTMLPurifier_Doctype Editable doctype object\r | |
82 | */\r | |
83 | public function get($doctype)\r | |
84 | {\r | |
85 | if (isset($this->aliases[$doctype])) {\r | |
86 | $doctype = $this->aliases[$doctype];\r | |
87 | }\r | |
88 | if (!isset($this->doctypes[$doctype])) {\r | |
89 | trigger_error('Doctype ' . htmlspecialchars($doctype) . ' does not exist', E_USER_ERROR);\r | |
90 | $anon = new HTMLPurifier_Doctype($doctype);\r | |
91 | return $anon;\r | |
92 | }\r | |
93 | return $this->doctypes[$doctype];\r | |
94 | }\r | |
95 | \r | |
96 | /**\r | |
97 | * Creates a doctype based on a configuration object,\r | |
98 | * will perform initialization on the doctype\r | |
99 | * @note Use this function to get a copy of doctype that config\r | |
100 | * can hold on to (this is necessary in order to tell\r | |
101 | * Generator whether or not the current document is XML\r | |
102 | * based or not).\r | |
103 | * @param HTMLPurifier_Config $config\r | |
104 | * @return HTMLPurifier_Doctype\r | |
105 | */\r | |
106 | public function make($config)\r | |
107 | {\r | |
108 | return clone $this->get($this->getDoctypeFromConfig($config));\r | |
109 | }\r | |
110 | \r | |
111 | /**\r | |
112 | * Retrieves the doctype from the configuration object\r | |
113 | * @param HTMLPurifier_Config $config\r | |
114 | * @return string\r | |
115 | */\r | |
116 | public function getDoctypeFromConfig($config)\r | |
117 | {\r | |
118 | // recommended test\r | |
119 | $doctype = $config->get('HTML.Doctype');\r | |
120 | if (!empty($doctype)) {\r | |
121 | return $doctype;\r | |
122 | }\r | |
123 | $doctype = $config->get('HTML.CustomDoctype');\r | |
124 | if (!empty($doctype)) {\r | |
125 | return $doctype;\r | |
126 | }\r | |
127 | // backwards-compatibility\r | |
128 | if ($config->get('HTML.XHTML')) {\r | |
129 | $doctype = 'XHTML 1.0';\r | |
130 | } else {\r | |
131 | $doctype = 'HTML 4.01';\r | |
132 | }\r | |
133 | if ($config->get('HTML.Strict')) {\r | |
134 | $doctype .= ' Strict';\r | |
135 | } else {\r | |
136 | $doctype .= ' Transitional';\r | |
137 | }\r | |
138 | return $doctype;\r | |
139 | }\r | |
140 | }\r | |
141 | \r | |
142 | // vim: et sw=4 sts=4\r |