cms/drupal/sites/all/modules/htmlpurifier/htmlpurifier.install
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 /**
       
     3  * @file
       
     4  * Install, update and uninstall functions for the HTML Purifier module.
       
     5  */
       
     6 /**
       
     7  * Implements hook_schema().
       
     8  */
       
     9 function htmlpurifier_schema() {
       
    10   $t = get_t();
       
    11   $schema['cache_htmlpurifier'] = drupal_get_schema_unprocessed('system', 'cache');
       
    12   $schema['cache_htmlpurifier']['description'] = $t(<<<DESCRIPTION
       
    13 Cache table for the HTML Purifier module just like cache_filter, except that
       
    14 cached text is stored permanently until flushed or manually invalidated.
       
    15 This helps prevent recurrent cache slams on pages with lots of segments of HTML.
       
    16 DESCRIPTION
       
    17   );
       
    18   return $schema;
       
    19 }
       
    20 
       
    21 /**
       
    22  * Implements hook_uninstall().
       
    23  */
       
    24 function htmlpurifier_uninstall() {
       
    25   db_delete('variable')
       
    26     ->condition('name', 'htmlpurifier%', 'LIKE')
       
    27     ->execute();
       
    28 }
       
    29 
       
    30 /**
       
    31  * Implements hook_requirements().
       
    32  *
       
    33  * Checks the version of HTML Purifier on install and issues an error if there is a problem
       
    34  */
       
    35 function htmlpurifier_requirements($phase) {
       
    36   // This version of HTML Purifier is required
       
    37   static $req_version = '4.0.0';
       
    38   $requirements = array();
       
    39   $t = get_t();
       
    40 
       
    41   // HACK: If libraries api module is not installed but available, load
       
    42   // it.  This can arise when an install profile is installing multiple
       
    43   // modules, because the HTMLPurifier module does not publish a
       
    44   // libraries dependency in order to stay backwards-compatible.  This
       
    45   // fixes Bug #839490.
       
    46   if (!function_exists('libraries_get_path') && file_exists(dirname(__FILE__) . '/../libraries/libraries.module')) {
       
    47     require_once(dirname(__FILE__) . '/../libraries/libraries.module');
       
    48   }
       
    49 
       
    50   // If it's still not available, use something else
       
    51   $complain_loc = FALSE;
       
    52   if (function_exists('libraries_get_path')) {
       
    53     $library_path = libraries_get_path('htmlpurifier');
       
    54     $using_libraries = TRUE;
       
    55     if (!file_exists("$library_path/library/HTMLPurifier.auto.php")) {
       
    56       $library_path = dirname(__FILE__);
       
    57       $complain_loc = TRUE;
       
    58       $using_libraries = FALSE;
       
    59     }
       
    60   }
       
    61   else {
       
    62     $library_path = dirname(__FILE__);
       
    63     $using_libraries = FALSE;
       
    64   }
       
    65 
       
    66   $s = DIRECTORY_SEPARATOR;
       
    67   if (!file_exists("$library_path/library/HTMLPurifier.auto.php")) {
       
    68     $requirements['htmlpurifier_library'] = array(
       
    69       'title' => $t('HTML Purifier library'),
       
    70       'severity' => REQUIREMENT_ERROR,
       
    71       'description' => $t("Could not find HTML Purifier
       
    72           installation in @path. Please copy contents
       
    73           of the library folder in the HTML Purifier tarball or zip
       
    74           to this folder or ensure HTMLPurifier.auto.php exists.
       
    75           You can download HTML Purifier at
       
    76           <a href=\"http://htmlpurifier.org/download.html\">htmlpurifier.org</a>.", array('@path' => "$library_path{$s}library")
       
    77       ),
       
    78     );
       
    79     return $requirements;
       
    80   }
       
    81 
       
    82   if ($complain_loc) {
       
    83     $requirements['htmlpurifier_library_loc'] = array(
       
    84       'title' => $t('HTML Purifier library location'),
       
    85       'severity' => REQUIREMENT_WARNING,
       
    86       'description' => $t("The HTML Purifier library currently lives in
       
    87         <code>@oldpath</code>, but should actually be placed in the shared
       
    88         libraries API at <code>@newpath</code>.  You should move the folder
       
    89         such that <code>@somefile</code> exists (you will need to create an
       
    90         <code>htmlpurifier</code> folder to hold the <code>library</code>
       
    91         folder).  For future updates, you can simply replace the
       
    92         htmlpurifier folder with the htmlpurifier-x.y.z folder that a
       
    93         new HTML Purifier tarball unzips to (you'll be reminded in an
       
    94         update notification).",
       
    95           array(
       
    96             '@oldpath' => dirname(__FILE__) . '/library',
       
    97             '@newpath' => libraries_get_path('htmlpurifier') . '/library',
       
    98             '@somefile' => libraries_get_path('htmlpurifier') . '/library/HTMLPurifier.auto.php',
       
    99             )),
       
   100     );
       
   101   }
       
   102 
       
   103   if ($phase=='runtime') {
       
   104     $current = variable_get('htmlpurifier_version_current', FALSE);
       
   105     if (!$current) {
       
   106       $current = htmlpurifier_check_version();
       
   107     }
       
   108     $ours = variable_get('htmlpurifier_version_ours', FALSE);
       
   109     if (!$ours || version_compare($ours, $req_version, '<')) {
       
   110       // Can't use _htmlpurifier_load(), since it assumes a later
       
   111       // version
       
   112       require_once "$library_path/library/HTMLPurifier.auto.php";
       
   113       if (defined('HTMLPurifier::VERSION')) {
       
   114         $version = HTMLPurifier::VERSION;
       
   115       }
       
   116       else {
       
   117         $purifier = new HTMLPurifier;
       
   118         $version = $purifier->version;
       
   119       }
       
   120       variable_set('htmlpurifier_version_ours', $version);
       
   121       if (version_compare($version, $req_version, '<')) {
       
   122 
       
   123         $requirements['htmlpurifier_library'] = array(
       
   124           'title' => $t('HTML Purifier library'),
       
   125           'severity' => REQUIREMENT_ERROR,
       
   126           'description' => $t("HTML Purifier @old is not compatible
       
   127             with this module: HTML Purifier <strong>@required</strong> or later is required.
       
   128             If the required version is a dev version, you will need to
       
   129             <a href=\"http://htmlpurifier.org/download.html#Git\">check
       
   130             code out of Git</a> or
       
   131             <a href=\"http://htmlpurifier.org/download.html#NightlyBuilds\">download a nightly</a>
       
   132             to use this module.", array('@old' => $version, '@required' => $req_version)
       
   133           ),
       
   134         );
       
   135 
       
   136         return $requirements;
       
   137       }
       
   138     }
       
   139 
       
   140     if (!$current) {
       
   141       $requirements['htmlpurifier_check'] = array(
       
   142         'title' => $t('HTML Purifier Library'),
       
   143         'value' => $ours,
       
   144         'description' => $t('Unable to check for the latest version of the
       
   145         HTML Purifier library.  You will need to check manually at
       
   146         <a href="http://htmlpurifier.org">htmlpurifier.org</a> to find out if
       
   147         the version you are using is out of date.'),
       
   148         'severity' => REQUIREMENT_WARNING,
       
   149       );
       
   150     }
       
   151     elseif (!$ours || version_compare($current, $ours, '>')) {
       
   152       // Update our version number if it can't be found, or there's a
       
   153       // mismatch.  This won't do anything if _htmlpurifier_load() has
       
   154       // already been called.  An equivalent formulation would be
       
   155       // to always call _htmlpurifier_load() before retrieving the
       
   156       // variable, but this has the benefit of not always loading
       
   157       // HTML Purifier!
       
   158       _htmlpurifier_load();
       
   159       $ours = variable_get('htmlpurifier_version_ours', FALSE);
       
   160     }
       
   161     if ($current && $ours && version_compare($current, $ours, '>')) {
       
   162       $description = $t('Your HTML Purifier library is out of date. The
       
   163       latest version is %version, which you can download from <a
       
   164       href="http://htmlpurifier.org">htmlpurifier.org</a>. ',
       
   165         array('%version' => $current));
       
   166       if ($using_libraries) {
       
   167         $how_to_update = $t('To update, replace
       
   168         <code>%path</code> with the new directory the downloaded archive
       
   169         extracts into. ',
       
   170           array('%path' => libraries_get_path('htmlpurifier')));
       
   171       }
       
   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   }
       
   198 
       
   199   return $requirements;
       
   200 }
       
   201 
       
   202 // -- Update functions ------------------------------------------------------ //
       
   203 
       
   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();
       
   227 }
       
   228 
       
   229 function htmlpurifier_update_6201() {}
       
   230 
       
   231 /**
       
   232  *
       
   233  * Migrate filter settings into D7 format from D6
       
   234  */
       
   235 function htmlpurifier_update_7000() {
       
   236   // Make sure {d6_upgrade_filter} exists. It won't exist for people on
       
   237   // native D7 sites (not upgraded from D6).
       
   238   $d6_table = 'd6_upgrade_filter';
       
   239   $module = 'htmlpurifier';
       
   240   if (db_table_exists($d6_table)) {
       
   241     $query = db_select($d6_table)
       
   242       ->fields($d6_table, array('format', 'weight', 'delta'))
       
   243       ->condition('module', $module)
       
   244       ->distinct();
       
   245     foreach ($query->execute() as $record) {
       
   246       // Pull out the filter settings from variables
       
   247       $settings = array();
       
   248       $settings['htmlpurifier_help'] = variable_get("htmlpurifier_help_{$record->format}", NULL);
       
   249       variable_del("htmlpurifier_help_{$record->format}");
       
   250       $settings['htmlpurifier_basic_config'] = variable_get("htmlpurifier_config_{$record->format}", NULL);
       
   251       variable_del("htmlpurifier_config_{$record->format}");
       
   252       // Determine the filter type (basic/advanced)
       
   253       $filter_name = $module . "_basic";
       
   254       if ($record->delta == '1') {
       
   255         $filter_name = $module . "_advanced";
       
   256       }
       
   257       // Store away in the new D7 manner
       
   258       db_insert('filter')
       
   259         ->fields(array(
       
   260           'format' => $record->format,
       
   261           'module' => $module,
       
   262           'name' => $filter_name,
       
   263           'weight' => $record->weight,
       
   264           'settings' => serialize($settings),
       
   265           'status' => 1,
       
   266         ))
       
   267         ->execute();
       
   268       }
       
   269     // Double caching was removed in D7
       
   270     variable_del(variable_get("htmlpurifier_doublecache", NULL));
       
   271     // Remove all entries from the migration table
       
   272     db_delete($d6_table)
       
   273       ->condition('module', $module)
       
   274       ->execute();
       
   275     // Drop d6 migration table if it is empty
       
   276     $query = db_select($d6_table)
       
   277       ->fields($d6_table, array('fid'));
       
   278     if (mysql_num_rows($query) == 0) {
       
   279       db_drop_table($d6_table);
       
   280     }
       
   281   }
       
   282 }
       
   283 /**
       
   284  * Clean up the D6 cache_htmlpurifier schema since it was a clone of the system cache schema
       
   285  */
       
   286 function htmlpurifier_update_7001() {
       
   287   if (db_field_exists('cache_htmlpurifier', 'headers')) {
       
   288     db_drop_field('cache_htmlpurifier', 'headers');
       
   289   }
       
   290 }
       
   291 /**
       
   292  * Load and resave all text formats to update cache settings.
       
   293  *
       
   294  * This update will only work for 7-7 updates and will need to be run again
       
   295  * once there is a 6-7 upgrade path.
       
   296  *
       
   297  * @see http://drupal.org/node/993230
       
   298  */
       
   299 function htmlpurifier_update_7002() {
       
   300   $formats = filter_formats();
       
   301   foreach ($formats as $format) {
       
   302     $format->filters = filter_list_format($format->format);
       
   303     // filter_format_save() expects filters to be an array, however
       
   304     // filter_list_format() gives us objects.
       
   305     foreach ($format->filters as $key => $value) {
       
   306       $format->filters[$key] = (array) $value;
       
   307     }
       
   308     filter_format_save($format);
       
   309   }
       
   310 }
       
   311 // vim: syntax=php
       
   312