Fix #885130, Patch to INSTALL.txt to better explain site wide installations; thanks...
[htmlpurifier-drupal.git] / htmlpurifier.install
blob830f50fcb8c7517c19ded791509368127d01e6c5
1 <?php
3 /**
4  * Implementation of hook_schema().
5  */
6 function htmlpurifier_schema() {
7   $t = get_t();
8   $schema['cache_htmlpurifier'] = drupal_get_schema_unprocessed('system', 'cache');
9   $schema['cache_htmlpurifier']['description'] = $t(<<<DESCRIPTION
10 Cache table for the HTML Purifier module just like cache_filter, except that
11 cached text is stored permanently until flushed or manually invalidated.
12 This helps prevent recurrent cache slams on pages with lots of segments of HTML.
13 DESCRIPTION
14   );
15   return $schema;
18 /**
19  * Implementation of hook_install().
20  */
21 function htmlpurifier_install() {
22   drupal_install_schema('htmlpurifier');
25 /**
26  * Implementation of hook_uninstall().
27  */
28 function htmlpurifier_uninstall() {
29   drupal_uninstall_schema('htmlpurifier');
30   db_query("DELETE FROM {variable} WHERE name LIKE 'htmlpurifier%%'");
33 /**
34  * Implementation of hook_requirements().
35  *
36  * Checks the version of HTML Purifier on install and issues an error if there is a problem
37  */
38 function htmlpurifier_requirements($phase) {
39   // This version of HTML Purifier is required
40   static $req_version = '4.0.0';
41   $requirements = array();
42   $t = get_t();
44   // HACK: If libraries api module is not installed but available, load
45   // it.  This can arise when an install profile is installing multiple
46   // modules, because the HTMLPurifier module does not publish a
47   // libraries dependency in order to stay backwards-compatible.  This
48   // fixes Bug #839490.
49   if (!function_exists('libraries_get_path') && file_exists(dirname(__FILE__) . '/../libraries/libraries.module')) {
50     require_once(dirname(__FILE__) . '/../libraries/libraries.module');
51   }
53   // If it's still not available, use something else
54   $complain_loc = false;
55   if (function_exists('libraries_get_path')) {
56     $library_path = libraries_get_path('htmlpurifier');
57     $using_libraries = true;
58     if (!file_exists("$library_path/library/HTMLPurifier.auto.php")) {
59       $library_path = dirname(__FILE__);
60       $complain_loc = true;
61       $using_libraries = false;
62     }
63   } else {
64     $library_path = dirname(__FILE__);
65     $using_libraries = false;
66   }
68   $s = DIRECTORY_SEPARATOR;
69   if (!file_exists("$library_path/library/HTMLPurifier.auto.php")) {
70     $requirements['htmlpurifier_library'] = array (
71       'title' => $t('HTML Purifier library'),
72       'severity' => REQUIREMENT_ERROR,
73       'description' => $t("Could not find HTML Purifier
74           installation in @path. Please copy contents
75           of the library folder in the HTML Purifier tarball or zip
76           to this folder or ensure HTMLPurifier.auto.php exists.
77           You can download HTML Purifier at
78           <a href=\"http://htmlpurifier.org/download.html\">htmlpurifier.org</a>.", array('@path' => "$library_path{$s}library")
79       ),
80     );
81     return $requirements;
82   }
84   if ($complain_loc) {
85     $requirements['htmlpurifier_library_loc'] = array(
86       'title' => $t('HTML Purifier library location'),
87       'severity' => REQUIREMENT_WARNING,
88       'description' => $t("The HTML Purifier library currently lives in
89         <code>@oldpath</code>, but should actually be placed in the shared
90         libraries API at <code>@newpath</code>.  You should move the folder
91         such that <code>@somefile</code> exists (you will need to create an
92         <code>htmlpurifier</code> folder to hold the <code>library</code>
93         folder).  For future updates, you can simply replace the
94         htmlpurifier folder with the htmlpurifier-x.y.z folder that a
95         new HTML Purifier tarball unzips to (you'll be reminded in an
96         update notification).",
97           array(
98             '@oldpath' => dirname(__FILE__) . '/library',
99             '@newpath' => libraries_get_path('htmlpurifier') . '/library',
100             '@somefile' => libraries_get_path('htmlpurifier') . '/library/HTMLPurifier.auto.php',
101             )),
102     );
103   }
105   if ($phase=='runtime') {
106     $current = variable_get('htmlpurifier_version_current', FALSE);
107     if (!$current) {
108       $current = htmlpurifier_check_version();
109     }
110     $ours = variable_get('htmlpurifier_version_ours', FALSE);
111     if (!$ours || version_compare($ours, $req_version, '<')) {
112       // Can't use _htmlpurifier_load(), since it assumes a later
113       // version
114       require_once "$library_path/library/HTMLPurifier.auto.php";
115       if (defined('HTMLPurifier::VERSION')) {
116         $version = HTMLPurifier::VERSION;
117       } else {
118         $purifier = new HTMLPurifier;
119         $version = $purifier->version;
120       }
121       variable_set('htmlpurifier_version_ours', $version);
122       if (version_compare($version, $req_version, '<')) {
124         $requirements['htmlpurifier_library'] = array (
125           'title' => $t('HTML Purifier library'),
126           'severity' => REQUIREMENT_ERROR,
127           'description' => $t("HTML Purifier @old is not compatible
128             with this module: HTML Purifier <strong>@required</strong> or later is required.
129             If the required version is a dev version, you will need to
130             <a href=\"http://htmlpurifier.org/download.html#Git\">check
131             code out of Git</a> or
132             <a href=\"http://htmlpurifier.org/download.html#NightlyBuilds\">download a nightly</a>
133             to use this module.", array('@old' => $version, '@required' => $req_version)
134           ),
135         );
137         return $requirements;
138       }
139     }
141     if (!$current) {
142       $requirements['htmlpurifier_check'] = array(
143         'title' => $t('HTML Purifier Library'),
144         'value' => $ours,
145         'description' => $t('Unable to check for the latest version of the
146         HTML Purifier library.  You will need to check manually at
147         <a href="http://htmlpurifier.org">htmlpurifier.org</a> to find out if
148         the version you are using is out of date.'),
149         'severity' => REQUIREMENT_WARNING,
150       );
151     }
152     elseif (!$ours || version_compare($current, $ours, '>')) {
153       // Update our version number if it can't be found, or there's a
154       // mismatch.  This won't do anything if _htmlpurifier_load() has
155       // already been called.  An equivalent formulation would be
156       // to always call _htmlpurifier_load() before retrieving the
157       // variable, but this has the benefit of not always loading
158       // HTML Purifier!
159       _htmlpurifier_load();
160       $ours = variable_get('htmlpurifier_version_ours', FALSE);
161     }
162     if ($current && $ours && version_compare($current, $ours, '>')) {
163       $description = $t('Your HTML Purifier library is out of date. The
164       latest version is %version, which you can download from <a
165       href="http://htmlpurifier.org">htmlpurifier.org</a>. ',
166         array('%version' => $current));
167       if ($using_libraries) {
168         $how_to_update = $t('To update, replace
169         <code>%path</code> with the new directory the downloaded archive
170         extracts into. ',
171           array('%path' => libraries_get_path('htmlpurifier')));
172       } else {
173         $how_to_update = $t('To update, replace
174         <code>%path/library/</code> with the <code>library/</code>
175         directory from the downloaded archive. ',
176           array('%path' => dirname(__FILE__)));
177       }
178       $warning = $t('If you do not perform this operation correctly,
179       your Drupal installation will stop working.  Ensure that
180       <code>%path/library/HTMLPurifier.auto.php</code> exists after
181       the upgrade.',
182         array('%path' => $library_path));
183       $requirements['htmlpurifier_version'] = array(
184         'title' => $t('HTML Purifier Library'),
185         'value' => $ours,
186         'description' => $description . $how_to_update . $warning,
187         'severity' => REQUIREMENT_WARNING,
188       );
189     }
190     if (count($requirements) == 0) {
191       $requirements['htmlpurifier'] = array(
192         'severity' => REQUIREMENT_OK,
193         'title' => $t('HTML Purifier Library'),
194         'value' => $ours,
195       );
196     }
197   }
199   return $requirements;
202 // -- Update functions ------------------------------------------------------ //
204 function htmlpurifier_update_6200() {
205   // Migrate any old-style filter variables to new style.
206   $formats = filter_formats();
207   foreach ($formats as $format => $info) {
208     $filters = filter_list_format($format);
209     if (!isset($filters['htmlpurifier/0'])) continue;
210     $config_data = array(
211       'URI.DisableExternalResources' => variable_get("htmlpurifier_externalresources_$format", TRUE),
212       'Attr.EnableID' => variable_get("htmlpurifier_enableattrid_$format", FALSE),
213       'AutoFormat.Linkify' => variable_get("htmlpurifier_linkify_$format", TRUE),
214       'AutoFormat.AutoParagraph' => variable_get("htmlpurifier_autoparagraph_$format", TRUE),
215       'Null_HTML.Allowed' => !variable_get("htmlpurifier_allowedhtml_enabled_$format", FALSE),
216       'HTML.Allowed' => variable_get("htmlpurifier_allowedhtml_$format", ''),
217       'Filter.YouTube' => variable_get("htmlpurifier_preserveyoutube_$format", FALSE),
218     );
219     if (defined('HTMLPurifier::VERSION') && version_compare(HTMLPurifier::VERSION, '3.1.0-dev', '>=')) {
220       $config_data['HTML.ForbiddenElements']   = variable_get("htmlpurifier_forbiddenelements_$format", '');
221       $config_data['HTML.ForbiddenAttributes'] = variable_get("htmlpurifier_forbiddenattributes_$format", '');
222     }
223     variable_set("htmlpurifier_config_$format", $config_data);
224   }
225   
226   return array();
229 function htmlpurifier_update_6201() {}