summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoel Stein2010-09-20 21:07:14 (GMT)
committerJoel Stein2010-09-20 21:07:14 (GMT)
commitccbfc14bbbcfbd46ed96cb22d6b56bffc42f90a1 (patch)
tree4b1e2da6ff277430e712fa487e48f9a7e9d43492
Initial commit.6.x-1.0
-rw-r--r--skin.admin.inc62
-rwxr-xr-xskin.info4
-rwxr-xr-xskin.module90
3 files changed, 156 insertions, 0 deletions
diff --git a/skin.admin.inc b/skin.admin.inc
new file mode 100644
index 0000000..cac2a83
--- /dev/null
+++ b/skin.admin.inc
@@ -0,0 +1,62 @@
+<?php
+// $Id$
+
+/**
+ * Settings form.
+ */
+function skin_settings_form() {
+ $form['skin_paths'] = array(
+ '#title' => t('Skin paths'),
+ '#type' => 'textarea',
+ '#description' => t("Enter each path you want skinned, one per line. The '*' character is a wildcard. %front is the front page.", array('%front' => '<front>')),
+ '#default_value' => variable_get('skin_paths', ''),
+ );
+ $form['skin_save'] = array(
+ '#type' => 'radios',
+ '#title' => t('CSS and Javascript'),
+ '#description' => t('Optimized CSS and Javascript files are frequently deleted, which could break your skin. You can save an additional copy of these files, so they persist until the next time you build your skin. You can also adjust your optimization settings on the <a href="@url">performance page</a>.', array('@url' => url('admin/settings/performance'))),
+ '#options' => array(
+ 0 => "Don't save any files",
+ 1 => "Save a copy of optimized CSS and JS files",
+ 2 => "Save a copy of all CSS and JS files",
+ ),
+ '#default_value' => variable_get('skin_save', 1),
+ );
+ $form['skin_clear_cache'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Clear cached CSS and Javascript skin files.'),
+ '#description' => t('Warning: this could cause CSS and Javascript paths to become outdated in your skins.'),
+ );
+ $form['#submit'][] = 'skin_settings_form_submit';
+ return system_settings_form($form);
+}
+
+/**
+ * Settings form submit.
+ */
+function skin_settings_form_submit($form, &$form_state) {
+ if ($form_state['values']['skin_clear_cache']) {
+ _skin_recursive_delete(file_create_path('skin'));
+ }
+}
+
+/**
+ * Recursive delete function.
+ */
+function _skin_recursive_delete($path) {
+ if (is_file($path) or is_link($path)) {
+ unlink($path);
+ }
+ else if (is_dir($path)) {
+ $d = dir($path);
+ while (($entry = $d->read()) !== FALSE) {
+ if ($entry == '.' or $entry == '..') {
+ continue;
+ }
+ $entry_path = $path .'/'. $entry;
+ _skin_recursive_delete($entry_path);
+ }
+ $d->close();
+ rmdir($path);
+ }
+}
diff --git a/skin.info b/skin.info
new file mode 100755
index 0000000..9d6d982
--- /dev/null
+++ b/skin.info
@@ -0,0 +1,4 @@
+; $Id$
+name = Skin
+description = Turns your theme into a flexible, offsite skin.
+core = 6.x
diff --git a/skin.module b/skin.module
new file mode 100755
index 0000000..eb013bb
--- /dev/null
+++ b/skin.module
@@ -0,0 +1,90 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_perm().
+ */
+function skin_perm() {
+ return array('administer skin paths');
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+function skin_menu() {
+ $items['admin/settings/skin'] = array(
+ 'title' => 'Skin settings',
+ 'description' => 'Settings for the Skin module.',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('skin_settings_form'),
+ 'access arguments' => array('administer skin paths'),
+ 'file' => 'skin.admin.inc',
+ );
+ return $items;
+}
+
+/**
+ * Implementation of hook_init().
+ */
+function skin_init() {
+ $skin = drupal_match_path(drupal_get_path_alias($_GET['q']), variable_get('skin_paths', ''));
+
+ // If viewing a "skin" page, capture the output so we can manipulate it in
+ // skin_exit().
+ if ($skin) {
+ ob_start();
+ }
+}
+
+/**
+ * Implementation of hook_exit().
+ */
+function skin_exit() {
+ $skin = drupal_match_path(drupal_get_path_alias($_GET['q']), variable_get('skin_paths', ''));
+
+ // On skin page; grab the output and manipulate it.
+ if ($skin) {
+ print preg_replace_callback('/"\/([^"]+)?"/', '_skin_replace_callback', ob_get_clean());
+ }
+}
+
+/**
+ * Manipulates the rendered HTML.
+ */
+function _skin_replace_callback($matches) {
+ $filepath_minus_query = preg_replace('/\?.*/', '', $matches[1]);
+ $info = pathinfo($filepath_minus_query);
+
+ $save = variable_get('skin_save', 1);
+
+ // If we're interested in saving CSS or JS files...
+ if ($save > 0 and in_array($info['extension'], array('css', 'js'))) {
+
+ // Figure out if this is an optimized file.
+ $optimized = in_array(substr(str_replace(file_directory_path(), '', $filepath_minus_query), 0, 4), array('/css', '/js/'));
+
+ // Saving...
+ if ($save == 2 or ($save == 1 and $optimized)) {
+
+ // Save file in directory resembling its original location.
+ $skin_path = file_create_path('skin') . (!$optimized ? '/'. dirname($filepath_minus_query) : '');
+ if (file_check_directory($skin_path, FILE_CREATE_DIRECTORY) or mkdir($skin_path, 0755, TRUE)) {
+ $filename = $skin_path .'/'. $info['filename'] .'.'. $info['extension'];
+ $contents = file_get_contents($filepath_minus_query);
+ if (!file_save_data($contents, $filename, FILE_EXISTS_REPLACE)) {
+ unset($filename);
+ }
+ }
+ }
+ }
+
+ // If filename was not made, then we are not saving.
+ if (!isset($filename)) {
+ $filename = $matches[1];
+ }
+
+ // Convert URL to absolute.
+ global $base_url;
+ global $base_path;
+ return '"'. $base_url . $base_path . $filename .'"';
+}