1<?php
2/**
3 * Google Ads for DokuWiki
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Bernd Zeimetz <bernd@bzed.de>, based on code by Terence J. Grant<tjgrant@tatewake.com>
7 */
8
9$gads_loaded = 0;
10
11function gads_load()
12{
13	global $gads_loaded, $gads_settings;
14
15	$gads_file = dirname(__FILE__).'/local_pref.php';
16
17	if ($gads_loaded == 0)
18	{
19		if(file_exists($gads_file))
20		{
21			include($gads_file);
22
23			$gads_loaded = 1;
24		}
25	}
26}
27
28function gads_write($fp, $name, $val)
29{
30	fwrite($fp, '$gads_settings[\''.$name.'\'] = \''.$val.'\';'."\n");
31}
32
33function gads_save()
34{
35	global $gads_loaded, $gads_settings;
36
37	$gads_file = dirname(__FILE__).'/local_pref.php';
38
39	if (is_writable($gads_file) || is_writable(dirname(__FILE__)))
40	{
41		$fp = fopen($gads_file, "w");
42		fwrite($fp, '<?php'."\n// This file is automatically generated\n");
43		//gads_write($fp, 'code', $gads_settings['code']);
44		gads_write($fp, 'enabled', $gads_settings['enabled']);
45		gads_write($fp, 'debug', $gads_settings['debug']);
46		gads_write($fp, 'dontcountadmin', $gads_settings['dontcountadmin']);
47		gads_write($fp, 'dontcountusers', $gads_settings['dontcountusers']);
48
49		gads_write($fp, 'google_ad_client', $gads_settings['google_ad_client']);
50		gads_write($fp, 'google_color_border', $gads_settings['google_color_border']);
51		gads_write($fp, 'google_color_bg', $gads_settings['google_color_bg']);
52		gads_write($fp, 'google_color_link', $gads_settings['google_color_link']);
53		gads_write($fp, 'google_color_url', $gads_settings['google_color_url']);
54		gads_write($fp, 'google_color_text', $gads_settings['google_color_text']);
55
56		fclose($fp);
57
58		ptln('<div class="success">'.'Google Adsense pref saved successfully.'.'</div>');
59	}
60	else
61	{
62		ptln('<div class="error">'.'Google Adsense pref is not writable by the server.'.'</div>');
63	}
64}
65
66// this function should be included within some templates
67function gads_code($id, $styles=NULL)
68{
69        global $gads_loaded, $gads_settings, $conf;
70
71	$styles_array = gads_getstyle($styles);
72
73	$code = gads_showADS($id, $styles_array);
74
75	if ($code)
76	{
77		ptln($code);
78		ptln('<div class="clearer">&nbsp;</div>');
79	}
80}
81
82function gads_getstyle($str) {
83	if (!strlen($str)) return array();
84
85	$styles = array();
86
87	$tokens = preg_split('/\s+/', $str, 9);                 // limit is defensive
88	foreach ($tokens as $token) {
89		if (preg_match('/^\d*\.?\d+(%|px|em|ex|pt|cm|mm|pi|in)$/', $token)) {
90			$styles['width'] = $token;
91			continue;
92		}
93
94		// restrict token (class names) characters to prevent any malicious data
95		if (preg_match('/[^A-Za-z0-9_-]/',$token)) continue;
96		$styles['class'] = (isset($styles['class']) ? $styles['class'].' ' : '').$token;
97	}
98
99	return $styles;
100}
101
102function gads_boxopen($style, $title=NULL)
103{
104	if( $title )
105	{
106		$class = "class='ads debug" . (isset($style['class']) ? ' '.$style['class'] : '') . "'";
107	}
108	else
109	{
110                $class = "class='ads" . (isset($style['class']) ? ' '.$style['class'] : '') . "'";
111	}
112	$style = isset($style['width']) ? " style='width: {$style['width']};'" : '';
113
114	$html = "<div $class$style>\n";
115
116	if( $title )
117	{
118		$html .= "  <p class='ads_title'>" . $title . "</p>\n";
119	}
120
121	return $html;
122}
123
124function gads_boxcontent($content)
125{
126	return "  <div class='ads_content'>" . $content . "\n  </div>\n";
127}
128
129function gads_boxclose()
130{
131	$html = "</div>\n";
132
133	return $html;
134}
135
136function gads_showADS($id, $style=NULL)
137{
138	global $gads_loaded, $gads_settings, $conf;
139
140	//FIXME: hardcoded value !!
141	$id = 'googleads:' . $id;
142
143	$obj = '';
144
145	if( !$gads_settings['enabled'] )
146	{
147		$obj .= "\n" . '<!-- Google ADS DISABLED -->' . "\n";
148	}
149	elseif( $gads_settings['dontcountadmin'] && auth_isadmin() )
150	{
151		$obj .= "\n" . '<!-- Google ADS DISABLED for SuperUser -->' . "\n";
152	}
153	elseif( $gads_settings['dontcountusers'] && $_SERVER['REMOTE_USER'] )
154	{
155		$obj .= "\n" . '<!-- Google ADS DISABLED for NormalUser -->' . "\n";
156	}
157	elseif( $gads_settings['debug'] && auth_isadmin() )
158	{
159		$obj .= "\n" . '<!-- google_ads \'' . $id . '\' start -->' . "\n";
160		$obj .= gads_boxopen($style, $id);
161                $obj .= gads_boxcontent(gads_getADS($id));
162		$obj .= gads_boxclose();
163		$obj .= "\n" . '<!-- google_ads \'' . $id . '\' end -->' . "\n";
164	}
165	else
166	{
167		$obj .= "\n" . '<!-- google_ads \'' . $id . '\' start -->' . "\n";
168		$obj .= gads_boxopen($style);
169                $obj .= gads_boxcontent(gads_getADS($id));
170		$obj .= gads_boxclose();
171		$obj .= "\n" . '<!-- google_ads \'' . $id . '\' end -->' . "\n";
172	}
173
174	return $obj;
175}
176
177function gads_getADS($id)
178{
179	global $gads_loaded, $gads_settings;
180
181	$obj = '';
182
183	//check permission
184	$perm = auth_quickaclcheck($id);
185	if ($perm < AUTH_READ) return false;
186
187	//Read embeded page
188	$page = io_readfile(wikiFN($id));
189
190	//set pattern to be replaced
191	$patterns[0] = '/<default_client>/';
192        $replacements[0] = $gads_settings['google_ad_client'];
193	$patterns[1] = '/<default_border>/';
194        $replacements[1] = $gads_settings['google_color_border'];
195        $patterns[2] = '/<default_bg>/';
196        $replacements[2] = $gads_settings['google_color_bg'];
197        $patterns[3] = '/<default_link>/';
198        $replacements[3] = $gads_settings['google_color_link'];
199        $patterns[4] = '/<default_url>/';
200        $replacements[4] = $gads_settings['google_color_url'];
201        $patterns[5] = '/<default_text>/';
202        $replacements[5] = $gads_settings['google_color_text'];
203
204	//do replace
205	$obj .= preg_replace($patterns, $replacements, $page);
206
207	return $obj;
208}
209
210//Load settings
211gads_load();
212