'radios',
'#title' => t('Status'),
'#description' => t(
"If you don't want to use the CDN to serve files to your visitors yet,
but you do want to see if it's working well for your site, then enable
testing mode.
It will only serve files from the CDN if they have
the %cdn-testing-mode-permission permission.",
array('%cdn-testing-mode-permission' => 'access files on CDN when in testing mode')
),
'#required' => TRUE,
'#options' => array(
CDN_DISABLED => t('Disabled'),
CDN_TESTING => t('Testing mode'),
CDN_ENABLED => t('Enabled'),
),
'#default_value' => variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED),
);
$form[CDN_STATS_VARIABLE] = array(
'#type' => 'checkbox',
'#title' => t('Display statistics'),
'#description' => t(
'Shows the CDN integration statistics of the current page, to users with
the %access-per-page-statistics permission.',
array('%access-per-page-statistics' => 'access per-page statistics')
),
'#return_value' => CDN_ENABLED,
'#default_value' => variable_get(CDN_STATS_VARIABLE, CDN_DISABLED),
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:' . CDN_STATUS_VARIABLE => array(CDN_TESTING, CDN_ENABLED)),
);
return system_settings_form($form);
}
/**
* Form definition; details.
*/
function cdn_admin_details_form(&$form_state) {
ctools_include('dependent');
// 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');
$form[CDN_MODE_VARIABLE] = array(
'#type' => 'radios',
'#title' => t('Mode'),
'#description' => t(
"Basic mode will simply prepend another URL to the
complete file URL. Therefor, it only works for CDNs that support the
Origin Pull technique.
Advanced mode uses the daemon to synchronize files and
can be used with both Origin Pull and Push CDNs. If you've got an Origin
Pull CDN and want to process files before they're synced to the CDN, it
is also recommended to use this mode."
),
'#required' => TRUE,
'#options' => array(
CDN_MODE_BASIC => t('Basic'),
CDN_MODE_ADVANCED => t('Advanced'),
),
'#default_value' => variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC),
);
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Mode-specific settings'),
'#description' => t('Settings specific to the mode you have currently selected.'),
);
//
// Basic mode settings.
//
$form['settings'][CDN_BASIC_URL_VARIABLE] = array(
'#type' => 'textfield',
'#title' => t('CDN URL'),
'#description' => t("The CDN URL prefix that should be used (do not
include a trailing slash). Only works for CDNs that
support Origin Pull.
WARNING: do not use subdirectories
when you're serving CSS files from the CDN. The
references to images and fonts from within the CSS
files will break because the URLs are no longer
valid."),
'#size' => 35,
'#default_value' => variable_get(CDN_BASIC_URL_VARIABLE, ''),
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:' . CDN_MODE_VARIABLE => array(CDN_MODE_BASIC)),
);
$form['settings'][CDN_BASIC_EXTENSIONS_VARIABLE] = array(
'#type' => 'textfield',
'#title' => t('Allowed extensions'),
'#description' => t('Only files with these extensions will be served from the CDN.'),
'#default_value' => variable_get(CDN_BASIC_EXTENSIONS_VARIABLE, CDN_BASIC_EXTENSIONS_DEFAULT),
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:' . CDN_MODE_VARIABLE => array(CDN_MODE_BASIC)),
);
$form['settings'][CDN_BASIC_HTTPS_VARIABLE] = array(
'#type' => 'checkbox',
'#title' => t('CDN supports HTTPS'),
'#description' => t('Check this if your CDN supports HTTPS (on the same
host) and you would like to serve CDN content via
HTTPS if your page is being served via HTTPS.'),
'#default_value' => variable_get(CDN_BASIC_HTTPS_VARIABLE, FALSE),
'#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 the daemon\'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)),
);
$form['settings'][CDN_ADVANCED_HTTPS_VARIABLE] = array(
'#type' => 'checkbox',
'#title' => t('CDN supports HTTPS'),
'#description' => t('Check this if your CDN supports HTTPS (on the same
host) and you would like to serve CDN content via
HTTPS if your page is being served via HTTPS.'),
'#default_value' => variable_get(CDN_ADVANCED_HTTPS_VARIABLE, FALSE),
'#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[CDN_EXCLUSION_VARIABLE] = array(
'#type' => 'textarea',
'#title' => t('Paths excluded from the CDN'),
'#default_value' => variable_get(CDN_EXCLUSION_VARIABLE, CDN_EXCLUSION_DEFAULT),
'#description' => t(
"Enter one page per line as Drupal paths. The '*' character is a
wildcard. Example paths are %blog for the blog page and %blog-wildcard
for every personal blog. %front is the front page.
JavaScript files are excluded by default. This is necessary to
ensure a painless out-of-the-box experience.
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.",
array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => ''
)
),
);
$form[CDN_EXCLUSION_LOGGED_IN_VARIABLE] = array(
'#type' => 'textarea',
'#title' => t('Paths excluded from the CDN if logged in'),
'#default_value' => variable_get(CDN_EXCLUSION_LOGGED_IN_VARIABLE, CDN_EXCLUSION_LOGGED_IN_DEFAULT),
'#description' => t("Enter one Drupal path per line. Same rules as above apply here."),
);
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_URL_VARIABLE])) {
form_error($form['settings'][CDN_BASIC_URL_VARIABLE], t('!name field is required.', array('!name' => $form['settings'][CDN_BASIC_URL_VARIABLE]['#title'])));
}
if (empty($form_state['values'][CDN_BASIC_EXTENSIONS_VARIABLE])) {
form_error($form['settings'][CDN_BASIC_EXTENSIONS_VARIABLE], t('!name field is required.', array('!name' => $form['settings'][CDN_BASIC_EXTENSIONS_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 integration 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);
}