main.inc.php 1.47 KB
Newer Older
Jan Grewe's avatar
Jan Grewe committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/*
Version: 0.1
Plugin Name: CDN
Plugin URI: // Here comes a link to the Piwigo extension gallery, after
            // publication of your plugin. For auto-updates of the plugin.
Author: Jan Grewe <jan@faked.org>
Description: Set a different host to use as a CDN/Caching Proxy
*/

// Add an entry to the 'Plugins' menu.
add_event_handler('get_admin_plugin_menu_links', 'cdn_admin_menu');

function cdn_admin_menu($menu) {
	global $page,$conf;
	if ($conf['cdn_enabled'] == 'true' && empty($conf['cdn_host']) and in_array($page['page'], array('intro','plugins_list'))) {
		$page['errors'][] = l10n('You need to set your CDN host');
	}
	$admin_url = get_admin_plugin_menu_link(dirname(__FILE__).'/admin.php');
	array_push($menu, array(
		'NAME'  => 'CDN',
		'URL'   => get_admin_plugin_menu_link(dirname(__FILE__)).'/admin.php'
	)
 );
 return $menu;
}

add_event_handler('get_derivative_url', 'cdn_add_prefix');
add_event_handler('get_src_image_url', 'cdn_add_prefix');

function cdn_add_prefix($content) {
	global $conf;
	if ($conf['cdn_enabled'] == 'true') {
		$cdnHost = str_replace('#', rand(0, $conf['cdn_count']-1), $conf['cdn_host']);
		$cdnUrl = 'http://'.$cdnHost.'/'.$_SERVER['SERVER_NAME'].make_index_url();
		if(substr($content, 0, 5) == '_data') {
			$content = $cdnUrl.$content;
		}elseif(substr($content, 0, 5) == 'i.php') {
			$content = $cdnUrl.$content;
		}elseif(substr($content, 0, 11) == './galleries') {
			$content = $cdnUrl.substr($content, 2);
		}
	}
	return $content;
}

?>