| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Settings administration UI.
|
| 7 |
*/
|
| 8 |
|
| 9 |
|
| 10 |
//----------------------------------------------------------------------------
|
| 11 |
// Form API callbacks.
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Form definition; settings.
|
| 15 |
*/
|
| 16 |
function cdn_admin_settings_form() {
|
| 17 |
$form[CDN_STATUS_VARIABLE] = array(
|
| 18 |
'#type' => 'radios',
|
| 19 |
'#title' => t('Status'),
|
| 20 |
'#description' => t(
|
| 21 |
"If you don't want to use the CDN to serve files to your visitors yet,
|
| 22 |
but you do want to see if it's working well for your site, then enable
|
| 23 |
debug mode.<br />It will only serve files from the CDN if they have the
|
| 24 |
%cdn-debug-mode-permission permission.",
|
| 25 |
array('%cdn-debug-mode-permission' => 'access files on CDN when in debug mode')
|
| 26 |
),
|
| 27 |
'#options' => array(
|
| 28 |
CDN_DISABLED => t('Disabled'),
|
| 29 |
CDN_DEBUG => t('Debug mode'),
|
| 30 |
CDN_ENABLED => t('Enabled'),
|
| 31 |
),
|
| 32 |
'#default_value' => variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED),
|
| 33 |
);
|
| 34 |
|
| 35 |
$form[CDN_MODE_VARIABLE] = array(
|
| 36 |
'#type' => 'radios',
|
| 37 |
'#title' => t('Mode'),
|
| 38 |
'#description' => t(
|
| 39 |
"<strong>Basic mode</strong> will simply prepend another URL to the
|
| 40 |
complete file URL. Therefor, it only works for CDNs that support the
|
| 41 |
Origin Pull technique.<br />
|
| 42 |
<strong>Advanced mode</strong> uses the daemon to synchronize files and
|
| 43 |
can be used with both Origin Pull and Push CDNs. If you've got an Origin
|
| 44 |
Pull CDN and want to process files before they're synced to the CDN, it
|
| 45 |
is also recommended to use this mode."
|
| 46 |
),
|
| 47 |
'#options' => array(
|
| 48 |
CDN_MODE_BASIC => t('Basic'),
|
| 49 |
CDN_MODE_ADVANCED => t('Advanced'),
|
| 50 |
),
|
| 51 |
'#default_value' => variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC),
|
| 52 |
);
|
| 53 |
|
| 54 |
$form[CDN_STATS_VARIABLE] = array(
|
| 55 |
'#type' => 'radios',
|
| 56 |
'#title' => t('Show statistics for the current page'),
|
| 57 |
'#description' => t(
|
| 58 |
'Shows the CDN integration statistics of the current page, to users with
|
| 59 |
the %access-per-page-statistics permission.',
|
| 60 |
array('%access-per-page-statistics' => 'access per-page statistics')
|
| 61 |
),
|
| 62 |
'#options' => array(
|
| 63 |
CDN_DISABLED => t('Disabled'),
|
| 64 |
CDN_ENABLED => t('Enabled'),
|
| 65 |
),
|
| 66 |
'#default_value' => variable_get(CDN_STATS_VARIABLE, CDN_DISABLED),
|
| 67 |
);
|
| 68 |
|
| 69 |
return system_settings_form($form);
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Form definition; basic settings.
|
| 74 |
*/
|
| 75 |
function cdn_admin_basic_settings_form() {
|
| 76 |
$form[CDN_BASIC_URL_VARIABLE] = array(
|
| 77 |
'#type' => 'textfield',
|
| 78 |
'#title' => t('CDN URL'),
|
| 79 |
'#description' => t("The CDN URL prefix that should be used. Only works
|
| 80 |
for CDNs that support Origin Pull.<br />
|
| 81 |
<strong>WARNING</strong>: do not use subdirectories
|
| 82 |
when you're serving CSS files from the CDN. The
|
| 83 |
references to images and fonts from within the CSS
|
| 84 |
files will break because the URLs are no longer
|
| 85 |
valid."),
|
| 86 |
'#size' => 35,
|
| 87 |
'#default_value' => variable_get(CDN_BASIC_URL_VARIABLE, ''),
|
| 88 |
);
|
| 89 |
|
| 90 |
$form[CDN_BASIC_EXTENSIONS_VARIABLE] = array(
|
| 91 |
'#type' => 'textfield',
|
| 92 |
'#title' => t('Allowed extensions'),
|
| 93 |
'#description' => t('Only files with these extensions will be synced.'),
|
| 94 |
'#default_value' => variable_get(CDN_BASIC_EXTENSIONS_VARIABLE, CDN_BASIC_EXTENSIONS_DEFAULT),
|
| 95 |
);
|
| 96 |
|
| 97 |
return system_settings_form($form);
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Form definition; advanced settings.
|
| 102 |
*/
|
| 103 |
function cdn_admin_advanced_settings_form() {
|
| 104 |
// Immediately show the user the current status, unless the user is
|
| 105 |
// currently editing the values in the form.
|
| 106 |
if (!isset($_POST['form_build_id'])) {
|
| 107 |
$synced_files_db = variable_get(CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE, FALSE);
|
| 108 |
if ($synced_files_db !== FALSE) {
|
| 109 |
if (file_exists($synced_files_db) && @fopen($synced_files_db, 'r')) {
|
| 110 |
drupal_set_message(t('The synced files database was found and can be opened for reading.'));
|
| 111 |
}
|
| 112 |
else {
|
| 113 |
drupal_set_message(t('The synced files database could not be opened for reading!', 'error'));
|
| 114 |
}
|
| 115 |
}
|
| 116 |
}
|
| 117 |
|
| 118 |
$form[CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE] = array(
|
| 119 |
'#type' => 'textfield',
|
| 120 |
'#title' => t('Synced files database'),
|
| 121 |
'#description' => t('Enter the full path to the daemon\'s synced files database file.'),
|
| 122 |
'#size' => 60,
|
| 123 |
'#default_value' => variable_get(CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE, ''),
|
| 124 |
);
|
| 125 |
|
| 126 |
return system_settings_form($form);
|
| 127 |
}
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Form definition; other settings.
|
| 131 |
*/
|
| 132 |
function cdn_admin_other_settings_form() {
|
| 133 |
$form[CDN_DRUPAL_ROOT_VARIABLE] = array(
|
| 134 |
'#type' => 'textfield',
|
| 135 |
'#title' => t('Drupal root directory'),
|
| 136 |
'#description' => t(
|
| 137 |
'In 99% of the cases the default value is sufficient and correct. In
|
| 138 |
some advanced setups that make use of symbolic links however, it is
|
| 139 |
possible that the Drupal root directory is incorrectly detected. In
|
| 140 |
those cases, you should enter it here.<br />
|
| 141 |
<strong>This setting only affects the status report, it does not affect
|
| 142 |
the CDN integration itself in any way.</strong>'
|
| 143 |
),
|
| 144 |
'#default_value' => variable_get(CDN_DRUPAL_ROOT_VARIABLE, realpath('.')),
|
| 145 |
);
|
| 146 |
|
| 147 |
return system_settings_form($form);
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
| 151 |
* Default validate callback for the settings form.
|
| 152 |
*/
|
| 153 |
function cdn_admin_settings_form_validate($form, &$form_state) {
|
| 154 |
// Validate the synced files DB whenever advanced mode is enabled. It must
|
| 155 |
// be configured (on the advanced settings form) *before* advanced mode can
|
| 156 |
// be enabled!
|
| 157 |
$enabled = isset($form_state['values'][CDN_STATUS_VARIABLE]) && $form_state['values'][CDN_STATUS_VARIABLE] != CDN_DISABLED;
|
| 158 |
$advanced = isset($form_state['values'][CDN_MODE_VARIABLE]) && $form_state['values'][CDN_MODE_VARIABLE] == CDN_MODE_ADVANCED;
|
| 159 |
if ($enabled && $advanced) {
|
| 160 |
$synced_files_db = variable_get(CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE, '');
|
| 161 |
if (!_validate_synced_files_db($synced_files_db, CDN_STATUS_VARIABLE)) {
|
| 162 |
drupal_set_message(t('Please correct the above error in the <em>Advanced mode</em> settings form first.'), 'error');
|
| 163 |
}
|
| 164 |
}
|
| 165 |
}
|
| 166 |
|
| 167 |
/**
|
| 168 |
* Default validate callback for the advancedsettings form.
|
| 169 |
*/
|
| 170 |
function cdn_admin_advanced_settings_form_validate($form, &$form_state) {
|
| 171 |
// Validate the synced files DB when it's filled out on the advanced
|
| 172 |
// settings form.
|
| 173 |
if (isset($form_state['values'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE])) {
|
| 174 |
$synced_files_db = $form_state['values'][CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE];
|
| 175 |
_validate_synced_files_db($synced_files_db, CDN_ADVANCED_SYNCED_FILES_DB_VARIABLE);
|
| 176 |
}
|
| 177 |
}
|
| 178 |
|
| 179 |
|
| 180 |
//----------------------------------------------------------------------------
|
| 181 |
// Private functions.
|
| 182 |
|
| 183 |
/**
|
| 184 |
* Helper function to validate a possible synced files DB value.
|
| 185 |
*
|
| 186 |
* @param $synced_files_db
|
| 187 |
* A user-entered synced files DB value.
|
| 188 |
* @param $name
|
| 189 |
* The name of the form item on which to set errors, if any.
|
| 190 |
* @return
|
| 191 |
* FALSE if there were any errors, TRUE if there weren't any.
|
| 192 |
*/
|
| 193 |
function _validate_synced_files_db($synced_files_db, $name) {
|
| 194 |
// Validate the file name.
|
| 195 |
if (strpos($synced_files_db, CDN_DAEMON_SYNCED_FILES_DB) === FALSE) {
|
| 196 |
form_set_error($name, t('The synced files database should have the file name %name.', array('%name' => CDN_DAEMON_SYNCED_FILES_DB)));
|
| 197 |
return FALSE;
|
| 198 |
}
|
| 199 |
|
| 200 |
// Validate the entered synced files database.
|
| 201 |
if (!file_exists($synced_files_db)) {
|
| 202 |
form_set_error($name, t('The synced files database does not exist.'));
|
| 203 |
return FALSE;
|
| 204 |
}
|
| 205 |
else {
|
| 206 |
if (!@fopen($synced_files_db, 'r')) {
|
| 207 |
form_set_error($name, t('The synced files database could not be opened for reading.'));
|
| 208 |
return FALSE;
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
| 212 |
return TRUE;
|
| 213 |
}
|