| 1 |
<?php
|
| 2 |
// $Id: style_settings.module,v 1.4 2008/02/14 09:29:14 fokke Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_preprocess_page().
|
| 6 |
*/
|
| 7 |
function style_settings_preprocess_page(&$variables) {
|
| 8 |
$css = array();
|
| 9 |
|
| 10 |
// Replace all theme stylesheets by the rewritten ones.
|
| 11 |
foreach ($variables['css']['all']['theme'] as $old_path => $preprocess) {
|
| 12 |
$css[style_settings_rewrite($old_path)] = $preprocess;
|
| 13 |
}
|
| 14 |
|
| 15 |
$variables['css']['all']['theme'] = $css;
|
| 16 |
$variables['styles'] = drupal_get_css($variables['css']);
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Rewrite a style sheet and return the path to the rewritten one.
|
| 21 |
*/
|
| 22 |
function style_settings_rewrite($old_path) {
|
| 23 |
global $theme_key, $conf;
|
| 24 |
|
| 25 |
$change = (string) @filectime($old_path);
|
| 26 |
$remote = empty($change);
|
| 27 |
$settings = theme_get_settings($theme_key);
|
| 28 |
$checksum = md5(serialize($settings).serialize($conf).$change);
|
| 29 |
$new_path = file_directory_path().'/style/'.$checksum.'.'.basename($old_path);
|
| 30 |
|
| 31 |
if ($remote || !file_exists($new_path)) {
|
| 32 |
file_check_directory(dirname($new_path), FILE_CREATE_DIRECTORY);
|
| 33 |
$old_data = file_get_contents(url($old_path, array('absolute' => TRUE)));
|
| 34 |
|
| 35 |
// Prefix all paths, ignoring absolute paths.
|
| 36 |
_drupal_build_css_path(NULL, base_path().dirname($old_path).'/');
|
| 37 |
$new_data = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $old_data);
|
| 38 |
|
| 39 |
// Replace all custom css
|
| 40 |
$new_data = preg_replace_callback('/\/\*\s*(setting|variable)\s*:\s*([a-z0-9_]+)\s*\*\/(.*)\/\*\s*.*\s*\*\//iU', 'style_settings_replace', $new_data);
|
| 41 |
|
| 42 |
file_save_data($new_data, $new_path, FILE_EXISTS_REPLACE);
|
| 43 |
}
|
| 44 |
|
| 45 |
return $new_path;
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Callback to replace custom css tags.
|
| 50 |
*/
|
| 51 |
function style_settings_replace($matches) {
|
| 52 |
global $theme_key;
|
| 53 |
static $settings;
|
| 54 |
|
| 55 |
$type = $matches[1];
|
| 56 |
$name = $matches[2];
|
| 57 |
$original = $matches[3];
|
| 58 |
|
| 59 |
// Cache settings for next calls
|
| 60 |
if ($type == 'setting' && !$settings) {
|
| 61 |
$settings = theme_get_settings($theme_key);
|
| 62 |
}
|
| 63 |
|
| 64 |
// Variable
|
| 65 |
if ($type == 'variable') {
|
| 66 |
$replacement = variable_get($name, NULL);
|
| 67 |
|
| 68 |
if (!$replacement || !(is_string($replacement) || is_numeric($replacement))) {
|
| 69 |
return $original;
|
| 70 |
}
|
| 71 |
|
| 72 |
// Theme setting
|
| 73 |
} else {
|
| 74 |
$replacement = $settings[$name];
|
| 75 |
|
| 76 |
if (!$replacement) {
|
| 77 |
return $original;
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
| 81 |
// Since a comment cannot be in url(), we have to add this around the replacement (if the user doesn't).
|
| 82 |
if (preg_match('/url\(.*\)/i', $original) && !preg_match('/url\(.*\)/i', $replacement)) {
|
| 83 |
|
| 84 |
// Prefix path, ignoring absolute paths.
|
| 85 |
if (!preg_match('/^([a-z]+:|\/+)/', $replacement)) {
|
| 86 |
$replacement = base_path().$replacement;
|
| 87 |
}
|
| 88 |
|
| 89 |
$replacement = "url('".$replacement."')";
|
| 90 |
}
|
| 91 |
|
| 92 |
return $replacement;
|
| 93 |
}
|
| 94 |
|
| 95 |
?>
|