| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
define('LT_CWD', sprintf("%s/logos", variable_get('file_directory_path', 'files')));
|
| 5 |
if (!module_exists('locale')) variable_set('logotool_locales', 0);
|
| 6 |
|
| 7 |
/**
|
| 8 |
* Implementation of hook_help
|
| 9 |
*/
|
| 10 |
function logotool_help($section) {
|
| 11 |
switch ($section) {
|
| 12 |
case 'admin/settings/logotool/logos':
|
| 13 |
return t('A list of all the logos in your logo folder showing which pages they\'re displayed on:');
|
| 14 |
}
|
| 15 |
return $output;
|
| 16 |
} // logotool_help
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Implementation of hook_perm
|
| 20 |
*/
|
| 21 |
function logotool_perm() {
|
| 22 |
return array('administer logotool');
|
| 23 |
} // logotool_perm
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Implementation of hook_menu
|
| 27 |
*/
|
| 28 |
function logotool_menu($may_cache) {
|
| 29 |
$items = array();
|
| 30 |
$admin_access = user_access('administer logotool');
|
| 31 |
if ($may_cache) {
|
| 32 |
$items[] = array('title' => t('Logotool'),
|
| 33 |
'path' => 'admin/settings/logotool',
|
| 34 |
'callback' => 'drupal_get_form',
|
| 35 |
'callback arguments' => array('logotool_settings'),
|
| 36 |
'description' => t('Manage logotool settings'),
|
| 37 |
'access' => $admin_access
|
| 38 |
);
|
| 39 |
$items[] = array(
|
| 40 |
'path' => 'admin/settings/logotool/settings',
|
| 41 |
'title' => t('Logotool Settings'),
|
| 42 |
'access' => $admin_access,
|
| 43 |
'callback' => 'logotool_settings',
|
| 44 |
'weight' => 0,
|
| 45 |
'type' => MENU_DEFAULT_LOCAL_TASK
|
| 46 |
);
|
| 47 |
$items[] = array('title' => 'Logotool Logos',
|
| 48 |
'path' => 'admin/settings/logotool/logos',
|
| 49 |
'callback' => 'logotool_logos',
|
| 50 |
'access' => $admin_access,
|
| 51 |
'weight' => 1,
|
| 52 |
'type' => MENU_LOCAL_TASK
|
| 53 |
);
|
| 54 |
$items[] = array('path' => 'admin/settings/logotool/delete',
|
| 55 |
'callback' => 'logotool_delete',
|
| 56 |
'access' => $admin_access,
|
| 57 |
'type' => MENU_CALLBACK
|
| 58 |
);
|
| 59 |
}
|
| 60 |
return $items;
|
| 61 |
} // logotool_menu
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of hook_settings
|
| 65 |
*/
|
| 66 |
function logotool_settings() {
|
| 67 |
$form['logo1'] = array(
|
| 68 |
'#type' => 'fieldset',
|
| 69 |
'#title' => t('Logo Tool Settings:'),
|
| 70 |
'#collapsible' => TRUE,
|
| 71 |
'#collapsed' => FALSE,
|
| 72 |
);
|
| 73 |
$form['logo1']['logotool_path'] = array(
|
| 74 |
'#type' => 'textfield',
|
| 75 |
'#title' => t('Path to default/ main logo'),
|
| 76 |
'#default_value' => variable_get('logotool_path', 'misc/druplicon.png'),
|
| 77 |
'#description' => t('The path to the file you would like to use as your default/ main logo.'),
|
| 78 |
);
|
| 79 |
$form['logo1']['logotool_folder'] = array(
|
| 80 |
'#type' => 'textfield',
|
| 81 |
'#title' => t('Path to your logos folder'),
|
| 82 |
'#default_value' => variable_get('logotool_folder', LT_CWD),
|
| 83 |
'#description' => t('The <strong>FULL SYSTEM PATH</strong> to the folder which contains your logos.'),
|
| 84 |
);
|
| 85 |
if (module_exists('locale')) {
|
| 86 |
$form['logo1']['logotool_locales'] = array(
|
| 87 |
'#type' => 'checkbox',
|
| 88 |
'#title' => t('Enable \'locale\' module integration'),
|
| 89 |
'#default_value' => variable_get('logotool_locales', 0),
|
| 90 |
'#description' => t('If you\'ve enabled the locale module then checking this will allow you to specify locale specific
|
| 91 |
logos.<br>The checking process works like this (for example locale \'en\' which is English);<p><ul><li>Check for logo contained in \'logos/en\'
|
| 92 |
folder. If not present then;</li><li>Check for logo contained in \'logos\' folder.</li></ul></p>'
|
| 93 |
),
|
| 94 |
);
|
| 95 |
}
|
| 96 |
$form['logo1']['logotool_display'] = array(
|
| 97 |
'#type' => 'select',
|
| 98 |
'#title' => t('Display mode'),
|
| 99 |
'#default_value' => variable_get('logotool_display', 0),
|
| 100 |
'#options' => array(0 => 'Normal mode', 1 => 'Random logos', 2 => 'Specified logos', 3 => 'Specified AND Random logos'),
|
| 101 |
'#description' => t('<strong>Normal mode</strong>: Displays the default/ main logo on every page.
|
| 102 |
<p><strong>Random logos</strong>: Displays a random logo on every page refresh.</p>
|
| 103 |
<p><strong>Specified logos</strong>: Displays a specific logo on each specified page from the
|
| 104 |
information below, pages not specified will display the default/ main logo.</p>
|
| 105 |
<p><strong>Specified AND Random logos</strong>: Displays a specific logo on each specified page from the
|
| 106 |
information below, pages not specified will display a random logo.</p>'
|
| 107 |
),
|
| 108 |
);
|
| 109 |
$form['logo1']['logotool_frequency'] = array(
|
| 110 |
'#type' => 'select',
|
| 111 |
'#title' => t('Random Frequency'),
|
| 112 |
'#description' => t('Select how often your logo is rotated (if you\'ve set it to be random).'),
|
| 113 |
'#options' => array(0 => t('Every page refresh'), 60 => t('Every minute'), 3600 => t('Every hour'), 86400 => t('Daily')),
|
| 114 |
'#default_value' => variable_get('logotool_frequency', 0),
|
| 115 |
);
|
| 116 |
$form['logo1']['logotool_pages'] = array(
|
| 117 |
'#type' => 'textarea',
|
| 118 |
'#title' => t('Specific logos for specific pages'),
|
| 119 |
'#default_value' => variable_get('logotool_pages', ''),
|
| 120 |
'#cols' => 35,
|
| 121 |
'#rows' => 20,
|
| 122 |
'#description' => t('Specify here pages which will have a specific logo, any pages not specified
|
| 123 |
will display the default/ main logo.<br>Structure your entries like this:
|
| 124 |
<p><strong><front>|homepage.png<br />node/1|logo1.jpg<br />node/2|logo5.jpg<br />admin*|admin.png</strong></p>
|
| 125 |
<p>Note that you don\'t have to specify the full path to your logos, it is taken from the folder
|
| 126 |
setting above and also note that a \'<strong>|</strong>\' separates the address from it\'s logo
|
| 127 |
and a newline separates each entry.<br />The wildcard character \'<strong>*</strong>\' can be
|
| 128 |
used to apply the logo recursively as in the example above. \'admin*\' will match menu item \'admin\'
|
| 129 |
and everything above it. \'admin/*\' however will only match what\'s above the admin menu item but
|
| 130 |
NOT THAT ITEM ITSELF.</p><p>Exact matches always take preference over wildcard entries, so it\'s
|
| 131 |
possible to use:</p>
|
| 132 |
<p><strong>node/add*|createcontent.png<br />node/add/story|addstory.png</strong></p>
|
| 133 |
<p>If you\'re mixing wildcards entries please be aware that the first valid match takes priority, so
|
| 134 |
you\'ll need to structure those entries first come, first served like this:</p>
|
| 135 |
<p><strong>admin/settings*|settings.png<br />admin*|admin.png</strong></p>'
|
| 136 |
),
|
| 137 |
);
|
| 138 |
$form['logo1']['logotool_ignore'] = array(
|
| 139 |
'#type' => 'textarea',
|
| 140 |
'#title' => t('Files to ignore'),
|
| 141 |
'#default_value' => variable_get('logotool_ignore', ""),
|
| 142 |
'#description' => t('(optional) if you have page specific logos you don\'t wish to appear on random pages then
|
| 143 |
specify them here (for use with Display Mode:"Specified AND Random logos").<br />As before, separate each entry with a newline.'
|
| 144 |
),
|
| 145 |
);
|
| 146 |
if (!is_file('logo.php')) drupal_set_message(t('logo.php not found - please move it into your top level Drupal folder'), 'error');
|
| 147 |
if (!is_dir(variable_get('logotool_folder', LT_CWD))) {
|
| 148 |
drupal_set_message(t('Incorrect path set for logo folder'), 'error');
|
| 149 |
}
|
| 150 |
else {
|
| 151 |
$result = logotool_scan_dir(variable_get('logotool_folder', LT_CWD));
|
| 152 |
if (!$result) {
|
| 153 |
drupal_set_message(t('Logo folder is empty/ files cannot be read by the server?'), 'error');
|
| 154 |
}
|
| 155 |
logotool_check();
|
| 156 |
}
|
| 157 |
return system_settings_form($form);
|
| 158 |
} // logotool_settings
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Returns a list of the logos contained in 'logotool_folder'
|
| 162 |
*/
|
| 163 |
function logotool_logos() {
|
| 164 |
$result = logotool_scan_dir(variable_get('logotool_folder', LT_CWD));
|
| 165 |
|
| 166 |
if (module_exists('locale') && variable_get('logotool_locales', 0)) {
|
| 167 |
$languages = logotool_scan_dir(variable_get('logotool_folder', LT_CWD), TRUE);
|
| 168 |
$supported = locale_supported_languages();
|
| 169 |
}
|
| 170 |
|
| 171 |
if (!$result) {
|
| 172 |
drupal_set_message(t('Logo folder is either empty or the logo files cannot be read by the server'), 'error');
|
| 173 |
$list[] = array(array('data' => 'None Found'));
|
| 174 |
}
|
| 175 |
else {
|
| 176 |
ksort($result);
|
| 177 |
foreach ($result as $a) {
|
| 178 |
$file = str_replace(array('/'), array('\/'), $a->basename);
|
| 179 |
preg_match_all("/(.*)\|$file/", variable_get('logotool_pages', ''), $match);
|
| 180 |
$count = count($match['0']);
|
| 181 |
if (!$count) {
|
| 182 |
$list[] = array(array('data' => '<a href="'.url('admin/settings/logotool/delete/'.$a->basename).'">Delete</a>', 'align' => 'center'), array('data' => $a->basename), array('data' => ''), array('data' => theme('logotool_locales', $a, $supported, $languages), 'align' => 'center'));
|
| 183 |
}
|
| 184 |
unset($flag);
|
| 185 |
foreach ($match[0] as $no => $m) {
|
| 186 |
if (!$flag) {
|
| 187 |
$list[] = array(array('data' => '<a href="'.url('admin/settings/logotool/delete/'.$a->basename).'">Delete</a>', 'align' => 'center', 'rowspan' => $count), array('data' => $a->basename, 'rowspan' => $count), array('data' => $match[1][$no]), array('data' => theme('logotool_locales', $a, $supported, $languages), 'align' => 'center', 'rowspan' => $count));
|
| 188 |
$flag = TRUE;
|
| 189 |
}
|
| 190 |
else {
|
| 191 |
$list[] = array(array('data' => $match[1][$no]));
|
| 192 |
}
|
| 193 |
}
|
| 194 |
}
|
| 195 |
}
|
| 196 |
logotool_check();
|
| 197 |
$output = theme('table', array('Action', 'Filename', 'Path', 'Logo'), $list, array('border' => '1'));
|
| 198 |
return '<div align="center">'.$output.'</div>';
|
| 199 |
} // logotool_logos
|
| 200 |
|
| 201 |
/**
|
| 202 |
* Checks that all the logos specified actually exists in the logo folder
|
| 203 |
*/
|
| 204 |
function logotool_check() {
|
| 205 |
preg_match_all("/.*\|(.*)/", variable_get('logotool_pages', ''), $match);
|
| 206 |
foreach ($match['0'] as $no => $m) {
|
| 207 |
if (!is_file(trim(variable_get('logotool_folder', LT_CWD).'/'.$match[1][$no]))) {
|
| 208 |
drupal_set_message(t('Entry: \'%file\' is incorrect, logo either does not exist or is not readable by the server', array('%file' => $m)), 'error');
|
| 209 |
}
|
| 210 |
}
|
| 211 |
} // logotool_check
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Displays the confirm form for deleting a logo
|
| 215 |
*/
|
| 216 |
function logotool_delete($logo = NULL) {
|
| 217 |
if (!$logo) {
|
| 218 |
drupal_set_message(t('Internal error, no logo specified for deletion'), 'error');
|
| 219 |
drupal_goto('admin/settings/logotool/logos');
|
| 220 |
}
|
| 221 |
return drupal_get_form('logotool_delete_form', $logo);
|
| 222 |
} // logotool_delete
|
| 223 |
|
| 224 |
/**
|
| 225 |
* Displays the confirm form for deleting a logo
|
| 226 |
*/
|
| 227 |
function logotool_delete_form($logo) {
|
| 228 |
$form['piccy'] = array(
|
| 229 |
'#type' => 'item',
|
| 230 |
'#value' => sprintf('<img src="%slogo.php?logo=%s">', base_path(), $logo),
|
| 231 |
);
|
| 232 |
$form['logo'] = array(
|
| 233 |
'#type' => 'hidden',
|
| 234 |
'#value' => $logo,
|
| 235 |
);
|
| 236 |
return confirm_form($form, t("Are you sure you want to delete logo '%logo'?", array('%logo' => $logo)), 'admin/settings/logotool/logos', NULL, t('Delete'), t('Cancel'));
|
| 237 |
} // logotool_delete_form
|
| 238 |
|
| 239 |
/**
|
| 240 |
* Submit function for deleting a logo
|
| 241 |
*/
|
| 242 |
function logotool_delete_submit($form_id, $form) {
|
| 243 |
$delete = file_delete(realpath(file_check_location(sprintf("%s/%s", variable_get('logotool_folder', LT_CWD), $form['logo']), variable_get('logotool_folder', LT_CWD))));
|
| 244 |
if ($delete) {
|
| 245 |
$message = t('Logo deleted successfully.');
|
| 246 |
}
|
| 247 |
else {
|
| 248 |
if (!file_check_path(realpath(sprintf("%s/%s", variable_get('logotool_folder', LT_CWD), $form['logo']), variable_get('logotool_folder', LT_CWD)))) {
|
| 249 |
$message = t('Error deleting logo - You have a problem with folder permissions.');
|
| 250 |
}
|
| 251 |
elseif (is_file(realpath(file_check_location(sprintf("%s/%s", variable_get('logotool_folder', LT_CWD), $form['logo']), variable_get('logotool_folder', LT_CWD))))) {
|
| 252 |
$message = t('Error deleting logo - You have a problem with file permissions.');
|
| 253 |
}
|
| 254 |
else {
|
| 255 |
$message = t('Unknown error deleting logo.');
|
| 256 |
}
|
| 257 |
}
|
| 258 |
drupal_set_message($message);
|
| 259 |
drupal_goto('admin/settings/logotool/logos');
|
| 260 |
} // logotool_delete_submit
|
| 261 |
|
| 262 |
/**
|
| 263 |
* Implementation of hook_init
|
| 264 |
*/
|
| 265 |
function logotool_init() {
|
| 266 |
if (variable_get('logotool_frequency', 0) && (variable_get('logotool_timestamp', 0) + variable_get('logotool_frequency', 0)) <= time()) $time = TRUE;
|
| 267 |
switch (variable_get('logotool_display', 0)) {
|
| 268 |
case '0':
|
| 269 |
variable_set('logotool_logo', variable_get('logotool_path', 'misc/druplicon.png'));
|
| 270 |
break;
|
| 271 |
case '1':
|
| 272 |
if ($time || !variable_get('logotool_frequency', 0)) {
|
| 273 |
$logos = logotool_scan_dir(variable_get('logotool_folder', LT_CWD));
|
| 274 |
if ($time) variable_set('logotool_timestamp', time());
|
| 275 |
variable_set('logotool_logo', substr(array_rand($logos), strlen(variable_get('logotool_folder', LT_CWD))));
|
| 276 |
}
|
| 277 |
break;
|
| 278 |
case '2':
|
| 279 |
$match = logotool_fetch_matches();
|
| 280 |
// If there's a match, set the logo variable
|
| 281 |
if ($match) {
|
| 282 |
if (module_exists('locale') && variable_get('logotool_locales', 0)) {
|
| 283 |
global $locale;
|
| 284 |
if (is_file(sprintf("%s/%s/%s", variable_get('logotool_folder', LT_CWD), $locale, trim($match)))) {
|
| 285 |
variable_set('logotool_logo', sprintf("%s/%s", $locale, trim($match)));
|
| 286 |
}
|
| 287 |
else {
|
| 288 |
variable_set('logotool_logo', trim($match));
|
| 289 |
}
|
| 290 |
}
|
| 291 |
else {
|
| 292 |
variable_set('logotool_logo', trim($match));
|
| 293 |
}
|
| 294 |
}
|
| 295 |
// Else set the logo to default
|
| 296 |
else {
|
| 297 |
variable_set('logotool_logo', variable_get('logotool_path', 'misc/druplicon.png'));
|
| 298 |
}
|
| 299 |
break;
|
| 300 |
case '3':
|
| 301 |
$match = logotool_fetch_matches();
|
| 302 |
// If there's a match, set the logo variable
|
| 303 |
if ($match) {
|
| 304 |
if (module_exists('locale') && variable_get('logotool_locales', 0)) {
|
| 305 |
global $locale;
|
| 306 |
if (is_file(sprintf("%s/%s/%s", variable_get('logotool_folder', LT_CWD), $locale, trim($match)))) {
|
| 307 |
variable_set('logotool_logo', sprintf("%s/%s", $locale, trim($match)));
|
| 308 |
}
|
| 309 |
else {
|
| 310 |
if (!variable_get('logotool_flag', '')) variable_set('logotool_flag', variable_get('logotool_logo', ''));
|
| 311 |
variable_set('logotool_logo', trim($match));
|
| 312 |
}
|
| 313 |
}
|
| 314 |
else {
|
| 315 |
if (!variable_get('logotool_flag', '')) variable_set('logotool_flag', variable_get('logotool_logo', ''));
|
| 316 |
variable_set('logotool_logo', trim($match));
|
| 317 |
}
|
| 318 |
}
|
| 319 |
else {
|
| 320 |
if ($time || !variable_get('logotool_frequency', 0) || variable_get('logotool_flag', '')) {
|
| 321 |
$logos = logotool_scan_dir(variable_get('logotool_folder', LT_CWD), FALSE, TRUE);
|
| 322 |
if ($time) variable_set('logotool_timestamp', time());
|
| 323 |
if (variable_get('logotool_flag', '') && !$time && variable_get('logotool_frequency', 0)) {
|
| 324 |
variable_set('logotool_logo', variable_get('logotool_flag', ''));
|
| 325 |
}
|
| 326 |
else {
|
| 327 |
variable_set('logotool_logo', substr(array_rand($logos), strlen(variable_get('logotool_folder', LT_CWD))));
|
| 328 |
variable_set('logotool_flag', '');
|
| 329 |
}
|
| 330 |
}
|
| 331 |
}
|
| 332 |
break;
|
| 333 |
} // end switch
|
| 334 |
} // logotool_init
|
| 335 |
|
| 336 |
function logotool_fetch_matches() {
|
| 337 |
foreach (explode("\n", variable_get('logotool_pages', '')) as $page) {
|
| 338 |
$explode = explode("|", $page);
|
| 339 |
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($explode[0], '/')) .')$/';
|
| 340 |
preg_match($regexp, $_GET['q'], $match);
|
| 341 |
// Todo: if there's any call for it, fix the path alias below for cached pages
|
| 342 |
if (!$match && function_exists('drupal_get_path_alias')) preg_match($regexp, drupal_get_path_alias($_GET['q']), $match);
|
| 343 |
if ($match) return $explode[1];
|
| 344 |
}
|
| 345 |
return FALSE;
|
| 346 |
} // logotool_fetch_matches
|
| 347 |
|
| 348 |
/**
|
| 349 |
* file_scan_directory does a good job, but we need slightly more flexibility than that...
|
| 350 |
*/
|
| 351 |
function logotool_scan_dir($dir, $recurse = FALSE, $ignore = FALSE) {
|
| 352 |
$key = 'filename';
|
| 353 |
if ($ignore) {
|
| 354 |
$ignore = explode("\n", variable_get('logotool_ignore', ""));
|
| 355 |
foreach ($ignore as $n => $i) {
|
| 356 |
$list[$n + 3] = trim($i);
|
| 357 |
}
|
| 358 |
}
|
| 359 |
else {
|
| 360 |
$list = array();
|
| 361 |
}
|
| 362 |
$files = array();
|
| 363 |
if (is_dir($dir) && $handle = opendir($dir)) {
|
| 364 |
while ($file = readdir($handle)) {
|
| 365 |
if (!in_array($file, array('.', '..', 'CVS') + $list) && $file[0] != '.') {
|
| 366 |
if (is_dir("$dir/$file") && !$recurse) {
|
| 367 |
continue;
|
| 368 |
}
|
| 369 |
if (ereg('.*', $file)) {
|
| 370 |
$filename = "$dir/$file";
|
| 371 |
$basename = basename($file);
|
| 372 |
$files[$$key] = new stdClass();
|
| 373 |
$files[$$key]->filename = $filename;
|
| 374 |
$files[$$key]->basename = $basename;
|
| 375 |
}
|
| 376 |
}
|
| 377 |
}
|
| 378 |
|
| 379 |
closedir($handle);
|
| 380 |
}
|
| 381 |
|
| 382 |
return $files;
|
| 383 |
} // logotool_scan_dir
|
| 384 |
|
| 385 |
/**
|
| 386 |
* Returns a table of different locales (if locale is enabled)
|
| 387 |
*/
|
| 388 |
function theme_logotool_locales($logo, $supported = FALSE, $languages = array()) {
|
| 389 |
if (!module_exists('locale') || !variable_get('logotool_locales', 0)) {
|
| 390 |
return sprintf('<img src="%slogo.php?logo=%s">', base_path(), $logo->basename);
|
| 391 |
}
|
| 392 |
$count = 0;
|
| 393 |
foreach ($languages as $language) {
|
| 394 |
$current = sprintf("%s/%s", $language->filename, $logo->basename);
|
| 395 |
if ($supported['name'][$language->basename] && is_file($current)) {
|
| 396 |
$output[] = array(array('data' => sprintf("%s<br>(%s)", $supported['name'][$language->basename], $language->basename), 'align' => 'center'), array('data' => sprintf('<img src="%slogo.php?logo=%s/%s">', base_path(), $language->basename, $logo->basename), 'align' => 'center'));
|
| 397 |
$count++;
|
| 398 |
}
|
| 399 |
}
|
| 400 |
if (!$output) return sprintf('<img src="%slogo.php?logo=%s">', base_path(), $logo->basename);
|
| 401 |
$output[0][] = array('data' => sprintf('<img src="%slogo.php?logo=%s">', base_path(), $logo->basename), 'rowspan' => $count, 'align' => 'center');
|
| 402 |
return theme('table', array(array('data' => 'Locales:', 'colspan' => 2), 'Default:'), $output, array('border' => '1'));
|
| 403 |
} // theme_logotool_locales
|