diff --git a/skin.module b/skin.module index 3f1c965e06ea9846847e5c5f68762d7a44c16ab8..a1d1bc96e161cb3bad95b00c9a9af3f8a89dc585 100755 --- a/skin.module +++ b/skin.module @@ -1,100 +1,103 @@ - '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_help(). - */ -function skin_help($path, $arg) { - switch ($path) { - case 'admin/settings/skin': - return '
'. t('This module makes it easy to create a "skin" of your theme. Define your paths below, visit your skinned pages, and view the source to save your skinned HTML.') .'
'; - } -} - -/** - * 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 .'"'; -} + '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_help(). + */ +function skin_help($path, $arg) { + switch ($path) { + case 'admin/settings/skin': + return ''. t('This module makes it easy to create a "skin" of your theme. Define your paths below, visit your skinned pages, and view the source to save your skinned HTML.') .'
'; + } +} + +/** + * Implementation of hook_boot(). + */ +function skin_boot() { + // Bootstrap the "path" phase, so we can match the path (because if we're + // viewing a cached page, these functions won't have been defined yet). + drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH); + + // Viewing a "skin" page. + if (drupal_match_path(drupal_get_path_alias($_GET['q']), variable_get('skin_paths', ''))) { + // Bootstrap the rest of Drupal, so we have access to the "file_" functions + // in _skin_replace_callback. + drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); + // Capture the output so we can manipulate it in skin_exit(). + ob_start(); + } +} + +/** + * Implementation of hook_exit(). + */ +function skin_exit() { + // On skin page; grab the output and manipulate it. + if (drupal_match_path(drupal_get_path_alias($_GET['q']), variable_get('skin_paths', ''))) { + 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 .'"'; +}