'),
);
$format_explanation_list_items = array(
t("@format-url is the CDN URL that should be used. E.g.:
'http://cdn-a.com
'.", $format_variables),
t("@format-extensions is an optional
setting to limit which files should be served from a CDN. E.g.:
'.css .jpg .jpeg .png
'.", $format_variables),
);
$example_list_items = array(
t('This would serve all files from a single CDN:
http://cdn-a.com
'),
t("This would serve CSS and images from CDN A, .zip files from CDN B and
everything else from CDN C:
http://cdn-a.com|.css .jpg .jpeg .png\nhttp://cdn-b.com|.zip\nhttp://cdn-c.com
"),
);
$form['settings'][CDN_BASIC_MAPPING_VARIABLE] = array(
'#type' => 'textarea',
'#title' => t('CDN mapping'),
'#description' => t("Define which files are mapped to which CDN.
Enter one mapping per line, in the format @format-url[|@format-extensions]:
!format-explanation-list
Sample mappings:
!example-list", $format_variables + array(
'!format-explanation-list' => theme('item_list', $format_explanation_list_items),
'!example-list' => theme('item_list', $example_list_items),
)),
'#size' => 35,
'#default_value' => variable_get(CDN_BASIC_MAPPING_VARIABLE, ''),
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:' . CDN_MODE_VARIABLE => array(CDN_MODE_BASIC)),
);
//
// Advanced mode settings.
//
$form['settings'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE] = array(
'#type' => 'textfield',
'#title' => t('Synced files database'),
'#description' => t('Enter the full path to File Conveyor\'s synced files database file.'),
'#size' => 60,
'#default_value' => variable_get(CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE, ''),
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:' . CDN_MODE_VARIABLE => array(CDN_MODE_ADVANCED)),
);
return system_settings_form($form);
}
/**
* Form definition; other settings.
*/
function cdn_admin_other_settings_form(&$form_state) {
// Prevent requirement errors from showing up twice.
if (empty($form_state['post'])) {
_cdn_admin_check_requirements();
}
$form['#submit'] = array('cdn_admin_settings_submit_show_cache_warning');
if (variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC) == CDN_MODE_ADVANCED) {
$form[CDN_DRUPAL_ROOT_VARIABLE] = array(
'#type' => 'textfield',
'#title' => t('Drupal root directory'),
'#description' => t(
'In 99% of the cases the default value is sufficient and correct. In
some advanced setups that make use of symbolic links however, it is
possible that the Drupal root directory is incorrectly detected. In
those cases, you should enter it here.
This setting only affects the status report, it does not affect
the CDN integration itself in any way.'
),
'#required' => TRUE,
'#default_value' => variable_get(CDN_DRUPAL_ROOT_VARIABLE, realpath('.')),
);
}
$form['https'] = array(
'#type' => 'fieldset',
'#title' => t('HTTPS'),
'#description' => t('When a page is being served via HTTPS and the CDN
does not support HTTPS, then the file will not be
served from the CDN, because it would make the visit
insecure.
When your CDN does support HTTPS, enable the
corresponding setting below, and your CDN will be
used on HTTPS pages.'),
);
$form['https'][CDN_HTTPS_SUPPORT_VARIABLE] = array(
'#type' => 'checkbox',
'#title' => t('CDN supports HTTPS support'),
'#default_value' => variable_get(CDN_HTTPS_SUPPORT_VARIABLE, FALSE),
'#process' => array('ctools_dependent_process'),
);
$path_explanation = t(
"Enter one file pattern per line. The '*' character is a wildcard.
Example file patterns are *.js
for all JavaScript files and
mytheme/*.css
for all CSS files in the mytheme
directory."
);
$form['exceptions'] = array(
'#type' => 'fieldset',
'#title' => t('Exceptions'),
'#description' => t('Which files should be served from a CDN is not as
simple as it seems: there are bound to be exceptions.
You can easily define those exceptions here, either
by file URL or for logged in users.'),
);
$form['exceptions']['path'] = array(
'#type' => 'fieldset',
'#title' => t('File URL'),
'#description' => t("Files that are marked to not be served from the CDN
because of a match in the blacklist, can be overridden to be served from
the CDN after all, if they have a match in the whitelist.
All JavaScript files are excluded by default. This is necessary
to ensure a painless out-of-the-box experience. For maximum
performance, you should only exclude problematic JavaScript files.
Full explanation: it's necessary prevent any
possible cross-domain AJAX requests, which would violate the same origin
policy of browsers. Such violations potentially result in broken
functionality. Note that even requests to subdomains such as
cdn.yourdomain.com count as cross-domain requests!
You can opt-in to including JavaScript files by default and then exclude
problematic ones, but then you should carefully note which JavaScript
files perform AJAX requests. You can prevent all potential problems by
using JSONP,
which is a work-around to allow for cross-domain AJAX
requests."),
'#collapsible' => TRUE,
);
$form['exceptions']['path'][CDN_EXCEPTION_PATH_BLACKLIST_VARIABLE] = array(
'#type' => 'textarea',
'#title' => t('Blacklist'),
'#default_value' => variable_get(CDN_EXCEPTION_PATH_BLACKLIST_VARIABLE, CDN_EXCEPTION_PATH_BLACKLIST_DEFAULT),
'#description' => $path_explanation,
);
$form['exceptions']['path'][CDN_EXCEPTION_PATH_WHITELIST_VARIABLE] = array(
'#type' => 'textarea',
'#title' => t('Whitelist'),
'#default_value' => variable_get(CDN_EXCEPTION_PATH_WHITELIST_VARIABLE, CDN_EXCEPTION_PATH_WHITELIST_DEFAULT),
'#description' => $path_explanation,
);
$form['exceptions']['auth_users'] = array(
'#type' => 'fieldset',
'#title' => t('Authenticated users'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['exceptions']['auth_users'][CDN_EXCEPTION_AUTH_USERS_BLACKLIST_VARIABLE] = array(
'#type' => 'textarea',
'#title' => t('Blacklist'),
'#default_value' => variable_get(CDN_EXCEPTION_AUTH_USERS_BLACKLIST_VARIABLE, CDN_EXCEPTION_AUTH_USERS_BLACKLIST_DEFAULT),
'#description' => $path_explanation,
);
return system_settings_form($form);
}
/**
* Default validate callback for the details form.
*/
function cdn_admin_details_form_validate($form, &$form_state) {
// If ctools_dependent supported required dependent form items, this ugly
// piece of code would not be necessary.
if ($form_state['values'][CDN_MODE_VARIABLE] == CDN_MODE_BASIC) {
if (empty($form_state['values'][CDN_BASIC_MAPPING_VARIABLE])) {
form_error($form['settings'][CDN_BASIC_MAPPING_VARIABLE], t('!name field is required.', array('!name' => $form['settings'][CDN_BASIC_MAPPING_VARIABLE]['#title'])));
}
}
else {
if (empty($form_state['values'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE])) {
form_error($form['settings'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE], t('!name field is required.', array('!name' => $form['settings'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE]['#title'])));
}
}
// When in advanced mode, validate the synced files DB.
if ($form_state['values'][CDN_MODE_VARIABLE] == CDN_MODE_ADVANCED) {
$synced_files_db = $form_state['values'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE];
_cdn_admin_validate_synced_files_db($synced_files_db, CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE);
}
}
/**
* Submit callback for all settings forms, that shows a warning whenever
* settings have been changed: the caches might need to be cleared.
*/
function cdn_admin_settings_submit_show_cache_warning($form, &$form_state) {
drupal_set_message(t('You have just changed some settings. This might affect
the file URLs that have been cached in nodes, views,
or elsewhere.
You now may want to clear the cached data, to ensure
the new CDN file URLs are used immediately. This can
be done in the "Clear cached data"
field set in the "Performance" settings form.',
array(
'!link' => url('admin/settings/performance', array('fragment' => 'clear-cache'))
)
),
'info'
);
}
//----------------------------------------------------------------------------
// Private functions.
/**
* Helper function to validate a possible synced files DB value.
*
* @param $synced_files_db
* A user-entered synced files DB value.
* @param $name
* The name of the form item on which to set errors, if any.
* @return
* FALSE if there were any errors, TRUE if there weren't any.
*/
function _cdn_admin_validate_synced_files_db($synced_files_db, $name) {
// Validate the file name.
if (strpos($synced_files_db, CDN_DAEMON_SYNCED_FILES_DB) === FALSE) {
form_set_error($name, t('The synced files database should have the file name %name.', array('%name' => CDN_DAEMON_SYNCED_FILES_DB)));
return FALSE;
}
// Validate the entered synced files database.
if (!file_exists($synced_files_db)) {
form_set_error($name, t('The synced files database does not exist.'));
return FALSE;
}
else {
if (!@fopen($synced_files_db, 'r')) {
form_set_error($name, t('The synced files database could not be opened for reading.'));
return FALSE;
}
}
return TRUE;
}
/**
* Helper function to check if the requirements of the CDN module have been
* met. If any requirement errors exist, they are aggregated into a single
* error message and are subsequently displayed.
*
* @return
* The number of requirement errors.
*/
function _cdn_admin_check_requirements() {
// Check run-time requirements of the CDN integration module.
include_once './includes/install.inc';
$requirements = cdn_requirements('runtime');
// Filter out the requirement errors and display these, with links back to
// the admin/reports/status page.
$errors = array();
foreach ($requirements as $requirement => $details) {
if ($details['severity'] == REQUIREMENT_ERROR) {
$errors[] = l($details['title'], 'admin/reports/status') . '
' . $details['description'];
}
}
if (!empty($errors)) {
$args = array(
'!error-list' => theme('item_list', $errors),
);
drupal_set_message(t('Please fix the following problems with the installation of the CDN integration module:
!error-list', $args), 'error');
}
return count($errors);
}