diff --git a/cdn_seo.admin.inc b/cdn_seo.admin.inc index 8fabdd5..51587b1 100644 --- a/cdn_seo.admin.inc +++ b/cdn_seo.admin.inc @@ -2,49 +2,45 @@ /** * @file - * Admin file. + * Settings administration UI. */ /** - * Page generation function for admin/settings/cdn/seo. - */ -function cdn_seo_admin_page() { - $output = ''; - return $output . drupal_get_form('cdn_seo_admin_settings_form'); -} - -/** - * Form builder; Configure cdn seo settings. + * Form definition; SEO settings. * * @ingroup forms * @see system_settings_form() */ function cdn_seo_admin_settings_form() { - $blacklist = variable_get('cdn_seo_blacklist', cdn_get_domains()); - $form['cdn_seo_blacklist'] = array( + $blacklist = variable_get(CDN_SEO_MODE_BLACKLIST, cdn_get_domains()); + $form[CDN_SEO_MODE_BLACKLIST] = array( '#type' => 'textarea', '#title' => t('Blacklist'), '#default_value' => implode("\n", $blacklist), - '#description' => t('If you try to access domains in this list, odds are it will return a 404. The exception being if the request comes into the files directory and there is a menu callback for it (like imagecache or advagg). Input one domain per line: www.example.com'), + '#description' => t('If you try to access domains in this list, odds are + it will return a 404. The exception being if the + request comes into the files directory and there is + a menu callback for it (like ImageCache or AdvAgg). + Input one domain per line: www.example.com'), ); return system_settings_form($form); } /** - * validate cdn_seo_admin_settings_form submissions. + * Default validate callback for the SEO settings form. */ function cdn_seo_admin_settings_form_validate($form, &$form_state) { $op = $form_state['values']['op']; // Do not validate if resetting. - if ($op == 'Reset to defaults') { + if ($op == t('Reset to defaults')) { return; } // Convert the list to an array. - $blacklist = explode("\n", $form_state['values']['cdn_seo_blacklist']); + $blacklist = explode("\n", $form_state['values'][CDN_SEO_MODE_BLACKLIST]); $blacklist = array_filter(array_map('trim', $blacklist)); // Simple check on the domain. @@ -52,7 +48,11 @@ function cdn_seo_admin_settings_form_validate($form, &$form_state) { // Check for slashes. if (strpos($domain, '/') !== FALSE) { $bad = TRUE; - form_set_error('cdn_seo_blacklist', t('Only provide the domain name. %domain is not formatted correctly; strip out the http:// or the trailing slash from the domain.', array('%domain' => $domain))); + form_set_error(CDN_SEO_MODE_BLACKLIST, + t('Only provide the domain name. %domain is not formatted + correctly; strip out the http:// or the + trailing slash from the domain.', + array('%domain' => $domain))); } // Make sure the domain is valid. @@ -60,12 +60,14 @@ function cdn_seo_admin_settings_form_validate($form, &$form_state) { $long = ip2long($ip); if ($long == -1 || $long === FALSE) { $bad = TRUE; - form_set_error('cdn_seo_blacklist', t('The domain %domain is not valid.', array('%domain' => $domain))); + form_set_error(CDN_SEO_MODE_BLACKLIST, + t('The domain %domain is not valid.', + array('%domain' => $domain))); } } // Set the value to the array. - if (empty($bad) && $op == 'Save configuration') { - $form_state['values']['cdn_seo_blacklist'] = array_combine($blacklist, $blacklist); + if (empty($bad) && $op == t('Save configuration')) { + $form_state['values']['CDN_SEO_MODE_BLACKLIST'] = array_combine($blacklist, $blacklist); } } diff --git a/cdn_seo.info b/cdn_seo.info index 0db1bc1..3a48370 100644 --- a/cdn_seo.info +++ b/cdn_seo.info @@ -1,5 +1,6 @@ - name = CDN SEO -description = SEO tweak for CDN. -package = Performance and scalability +description = SEO tweak for the CDN module + core = 6.x +package = Performance and scalability +dependencies[] = cdn diff --git a/cdn_seo.install b/cdn_seo.install index 334893e..49714db 100644 --- a/cdn_seo.install +++ b/cdn_seo.install @@ -2,14 +2,14 @@ /** * @file - * Install file. + * Install, update and uninstall functions for the CDN SEO module. */ /** * Implementation of hook_install(). */ function cdn_seo_install() { - // CDN_SEO should run after just about everything else. + // cdn_seo should run after just about everything else. db_query("UPDATE {system} SET weight = 10001 WHERE name = 'cdn_seo'"); } diff --git a/cdn_seo.module b/cdn_seo.module index c03ed81..df03353 100644 --- a/cdn_seo.module +++ b/cdn_seo.module @@ -2,38 +2,35 @@ /** * @file - * CDN SEO module + * Implementation of the core hooks, defines, public and private functions. */ -/** - * Default value to see if the CDN SEO module is enabled. - */ -define('CDN_SEO_ENABLED', TRUE); +// Variables and values. +define('CDN_SEO_MODE_VARIABLE', 'cdn_seo_mode'); +define('CDN_SEO_MODE_BLACKLIST', 'blacklist'); +define('CDN_SEO_MODE_WHITELIST', 'whitelist'); +define('CDN_SEO_MODE_DEFAULT', CDN_SEO_MODE_BLACKLIST); +define('CDN_SEO_ENABLED_VARIABLE', 'cdn_seo_enabled'); +define('CDN_SEO_ENABLED_DEFAULT', TRUE); +define('CDN_SEO_BLACKLIST_VARIABLE', 'cdn_seo_blacklist'); +define('CDN_SEO_WHITELIST_VARIABLE', 'cdn_seo_whitelist'); -/** - * Defined value for the blacklist. - */ -define('CDN_SEO_BLACKLIST', 1); -/** - * Default value to see what mode to use. - */ -define('CDN_SEO_HOST_MODE', CDN_SEO_BLACKLIST); +//---------------------------------------------------------------------------- +// Drupal core. /** * Implementation of hook_menu(). */ function cdn_seo_menu() { - $file_path = drupal_get_path('module', 'cdn_seo'); - $items['admin/settings/cdn/seo'] = array( 'title' => 'SEO', - 'description' => 'Configuration for CDN SEO.', - 'page callback' => 'cdn_seo_admin_page', + 'description' => 'Configure CDN SEO settings', + 'access arguments' => array('administer site configuration'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('cdn_seo_admin_settings_form'), 'type' => MENU_LOCAL_TASK, 'weight' => -2, - 'access arguments' => array('administer site configuration'), - 'file path' => $file_path, 'file' => 'cdn_seo.admin.inc', ); @@ -45,18 +42,18 @@ function cdn_seo_menu() { */ function cdn_seo_init() { // Exit early if this is disabled. - if (!variable_get('cdn_seo_enabled', CDN_SEO_ENABLED)) { + if (!variable_get(CDN_SEO_ENABLED_VARIABLE, CDN_SEO_ENABLED_DEFAULT)) { return; } // Get context. $host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']; - $file_dir = file_directory_path(); + $files_dir = file_directory_path(); // Special handling for requests to the files dir. - if (strpos($_GET['q'], $file_dir) === 0) { - - // If this path has a menu item then exit here and let the callback work it. + if (strpos($_GET['q'], $files_dir) === 0) { + // If this path has a menu item, then exit here and let the menu callback + // do its thing. This is necessary for e.g. ImageCache or AdvAgg. $router_item = menu_get_item(); if (!empty($router_item)) { return; @@ -64,24 +61,31 @@ function cdn_seo_init() { } // Get all CDN domains. - $mode = variable_get('cdn_seo_host_mode', CDN_SEO_HOST_MODE); - if ($mode == CDN_SEO_BLACKLIST) { - $blacklisted_domains = variable_get('cdn_seo_blacklist', cdn_get_domains()); + $mode = variable_get(CDN_SEO_MODE_VARIABLE, CDN_SEO_MODE_DEFAULT); + if ($mode == CDN_SEO_MODE_BLACKLIST) { + $blacklisted_domains = variable_get(CDN_SEO_BLACKLIST_VARIABLE, cdn_get_domains()); + } + else if ($mode == CDN_SEO_MODE_WHITELIST) { + } - // If this host is blacklisted then fast 404. + // If this host is blacklisted, then do a fast 404. if (isset($blacklisted_domains[$host])) { - cdn_seo_fast404('Blacklisted domain'); + _cdn_seo_fast404('Blacklisted domain'); } } + +//---------------------------------------------------------------------------- +// Private functions. + /** * Send out a fast 404 and exit. * * @param $msg - * send this message in the header. + * Send this message in the custom X-CDN-SEO header. */ -function cdn_seo_fast404($msg = '') { +function _cdn_seo_fast404($msg = '') { global $base_path; if (!headers_sent()) { header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');