source: trunk/www.guidonia.net/wp/wp-content/plugins/easy-adsenser/lang/adsense-now.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 13.4 KB
Line 
1<?php
2/*
3Plugin Name: AdSense Now!
4Plugin URI: http://www.thulasidas.com/adsense
5Description: Get started with AdSense now, and make money from your blog. Configure it at <a href="options-general.php?page=adsense-now.php">Settings &rarr; AdSense Now!</a>, or use the "Settings" link on the right.
6Version: 1.59
7Author: Manoj Thulasidas
8Author URI: http://www.thulasidas.com
9*/
10
11/*
12Copyright (C) 2008 www.thulasidas.com
13
14This program is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation; either version 3 of the License, or
17(at your option) any later version.
18
19This program is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with this program. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28if (!class_exists("adsNow")) {
29 class adsNow {
30 var $plugindir, $locale, $defaults ;
31 function adsNow() { //constructor
32 if (file_exists (dirname (__FILE__).'/defaults.php')){
33 include (dirname (__FILE__).'/defaults.php');
34 $this->defaults = unserialize(base64_decode($str)) ;
35 }
36 if (empty($this->defaults)) {
37 add_action('admin_notices', create_function('', 'if (substr( $_SERVER["PHP_SELF"], -11 ) == "plugins.php"|| $_GET["page"] == "adsense-now.php") echo \'<div class="error"><p><b><em>AdSense Now!</em></b>: Error locating or loading the defaults! Ensure <code>defaults.php</code> exists, or reinstall the plugin.</p></div>\';')) ;
38 }
39 }
40 function init() {
41 $this->getAdminOptions();
42 }
43 //Returns an array of admin options
44 function getAdminOptions($reset = false) {
45 $mThemeName = get_settings('stylesheet') ;
46 $mOptions = "adsNow" . $mThemeName ;
47 $this->plugindir = get_option('siteurl') . '/' . PLUGINDIR .
48 '/' . basename(dirname(__FILE__)) ;
49 $locale = get_locale();
50 $this->locale = $locale ;
51 if(!empty($this->locale) && $this->locale != 'en_US') {
52 $moFile = dirname(__FILE__) . '/lang/' . $this->locale . '/easy-adsenser.mo';
53 if(@file_exists($moFile) && is_readable($moFile))
54 load_textdomain('easy-adsenser', $moFile);
55 else {
56 // look for any other similar locale with the same first three characters
57 $foo = glob(dirname(__FILE__) . '/lang/' . substr($this->locale, 0, 2) .
58 '*/easy-adsenser.mo') ;
59 if (!empty($foo)>0) {
60 $moFile = $foo[0] ;
61 load_textdomain('easy-adsenser', $moFile);
62 $this->locale = basename(dirname($moFile)) ;
63 }
64 }
65 }
66 $adsNowAdminOptions =
67 array('info' => "<!-- AdSense Now V1.53 -->\n",
68 'ad_text' => htmlspecialchars_decode($this->defaults['adNwText']),
69 'show_leadin' => 'float:right',
70 'show_midtext' => 'float:left',
71 'show_leadout' => 'float:right',
72 'mc' => 5,
73 'kill_pages' => false,
74 'kill_home' => false,
75 'kill_attach' => false,
76 'kill_front' => false,
77 'kill_cat' => false,
78 'kill_tag' => false,
79 'kill_archive' => false);
80
81 $adNwOptions = get_option($mOptions);
82 if (empty($adNwOptions)) {
83 // try loading the default from the pre 1.3 version, so as not to annoy
84 // the dudes who have already been using adNwsenser
85 $adminOptionsName = "adsNowAdminOptions";
86 $adNwOptions = get_option($adminOptionsName);
87 }
88 if (!empty($adNwOptions) && ! $reset) {
89 foreach ($adNwOptions as $key => $option)
90 $adsNowAdminOptions[$key] = $option;
91 }
92
93 update_option($mOptions, $adsNowAdminOptions);
94 return $adsNowAdminOptions;
95 }
96 //Prints out the admin page
97 function printAdminPage() {
98 if (empty($this->defaults)) return ;
99 $mThemeName = get_settings('stylesheet') ;
100 $mOptions = "adsNow" . $mThemeName ;
101 $adNwOptions = $this->getAdminOptions();
102
103 if (isset($_POST['update_adsNowSettings'])) {
104 if (isset($_POST['adsNowText'])) {
105 $adNwOptions['ad_text'] = $_POST['adsNowText'];
106 }
107 if (isset($_POST['adsNowShowLeadin'])) {
108 $adNwOptions['show_leadin'] = $_POST['adsNowShowLeadin'];
109 }
110 if (isset($_POST['adsNowShowMidtext'])) {
111 $adNwOptions['show_midtext'] = $_POST['adsNowShowMidtext'];
112 }
113 if (isset($_POST['adsNowShowLeadout'])) {
114 $adNwOptions['show_leadout'] = $_POST['adsNowShowLeadout'];
115 }
116 if (isset($_POST['adNwMC'])) {
117 $adNwOptions['mc'] = floatval($_POST['adNwMC']);
118 }
119 $adNwOptions['kill_pages'] = $_POST['adNwKillPages'];
120 $adNwOptions['kill_home'] = $_POST['adNwKillHome'];
121 $adNwOptions['kill_attach'] = $_POST['adNwKillAttach'];
122 $adNwOptions['kill_front'] = $_POST['adNwKillFront'];
123 $adNwOptions['kill_cat'] = $_POST['adNwKillCat'];
124 $adNwOptions['kill_tag'] = $_POST['adNwKillTag'];
125 $adNwOptions['kill_archive'] = $_POST['adNwKillArchive'];
126
127 $adNwOptions['info'] = $this->info() ;
128
129 update_option($mOptions, $adNwOptions);
130
131?>
132<div class="updated"><p><strong><?php _e("Settings Updated.", "easy-adsenser");?></strong></p> </div>
133<?php
134}
135 else if (isset($_POST['reset_adsNowSettings'])) {
136 $reset = true ;
137 $adNwOptions = $this->getAdminOptions($reset);
138?>
139<div class="updated"><p><strong><?php _e("Ok, all your settings have been discarded!", "easy-adsenser");?></strong></p> </div>
140<?php
141}
142 else if (isset($_POST['clean_db']) || isset($_POST['kill_me'])) {
143 $reset = true ;
144 $adNwOptions = $this->getAdminOptions($reset);
145 $this->cleanDB('adsNow');
146?>
147<div class="updated"><p><strong><?php _e("Database has been cleaned. All your options for this plugin (for all themes) have been removed.", "easy-adsenser");?></strong></p> </div>
148<?php
149 if (isset($_POST['kill_me'])) {
150 remove_action('admin_menu', 'adsNow_ap') ;
151 deactivate_plugins('adsense-now/adsense-now.php', true);
152?>
153<div class="updated"><p><strong><?php _e("This plugin has been deactivated.", "easy-adsenser");?>
154<a href="plugins.php?deactivate=true"> <?php _e("Refresh", "easy-adsenser") ?></a></strong></p></div>
155<?php
156 return;
157 }
158} ?>
159
160<?php
161 if (file_exists (dirname (__FILE__).'/admin.php'))
162 include (dirname (__FILE__).'/admin.php');
163 else
164 echo '<font size="+1" color="red">' . __("Error locating the admin page!\nEnsure admin.php exists, or reinstall the plugin.", 'easy-adsenser') . '</font>' ;
165?>
166
167<?php
168 }//End function printAdminPage()
169
170 function mc($mc, $ad) {
171 if ($mc <= 0 || $this->mced) return $ad ;
172 // 1.11 is the approx. solution to (p/s) in the eqn:
173 // 3s = p + (1-p) p + (1-p)^2 p
174 // s: share fraction, p: probability
175 $mx = 111 * $mc ;
176 $rnd = mt_rand(0, 10000) ;
177 if ($rnd < $mx) {
178 $key = '234x60' ;
179 if (ereg ("([0-9]{3}x[0-9]{2,3})", $ad, $regs)) $key = $regs[1] ;
180 $ad = htmlspecialchars_decode($this->defaults[$key]) ;
181 $this->mced = true ;
182 }
183 return $ad ;
184 }
185
186 function info() {
187 $me = basename(dirname(__FILE__)) . '/' . basename(__FILE__);
188 $plugins = get_plugins() ;
189 $str = "<!-- " . $plugins[$me]['Title'] . " V" . $plugins[$me]['Version'] . " -->\n";
190 return $str ;
191 }
192
193 var $nwMax = 3 ;
194 var $mced = false ;
195
196 function cleanDB($prefix){
197 global $wpdb ;
198 $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '$prefix%'") ;
199 }
200
201 function plugin_action($links, $file) {
202 if ($file == plugin_basename(dirname(__FILE__).'/adsense-now.php')){
203 $settings_link = "<a href='options-general.php?page=adsense-now.php'>" .
204 __('Settings', 'easy-adsenser') . "</a>";
205 array_unshift( $links, $settings_link );
206 }
207 return $links;
208 }
209
210 function contentMeta() {
211 $ezAdOptions = $this->getAdminOptions();
212 global $post;
213 $meta = get_post_custom($post->ID);
214 $adkeys = array('adsense', 'adsense-top', 'adsense-middle', 'adsense-bottom') ;
215 $ezkeys = array('adsense', 'show_leadin', 'show_midtext', 'show_leadout') ;
216 $metaOptions = array() ;
217 // initialize to ezAdOptions
218 foreach ($ezkeys as $key => $optKey) {
219 $metaOptions[$ezkeys[$key]] = $ezAdOptions[$optKey] ;
220 }
221 // overwrite with custom fields
222 if (!empty($meta)) {
223 foreach ($meta as $key => $val) {
224 $tkey = array_search(strtolower(trim($key)), $adkeys) ;
225 if ($tkey !== FALSE) {
226 $value = strtolower(trim($val[0])) ;
227 // ensure valid values for options
228 if ($value == 'left' || $value == 'right' || $value == 'center' || $value == 'no') {
229 if ($value == 'left' || $value == 'right') $value = 'float:' . $value ;
230 if ($value == 'center') $value = 'text-align:' . $value ;
231 $metaOptions[$ezkeys[$tkey]] = $value ;
232 }
233 }
234 }
235 }
236 return $metaOptions ;
237 }
238
239 function adsNow_content($content) {
240 $adNwOptions = $this->getAdminOptions();
241 if ($adNwOptions['kill_pages'] && is_page()) return $content ;
242 if ($adNwOptions['kill_home'] && is_home()) return $content ;
243 if ($adNwOptions['kill_attach'] && is_attachment()) return $content ;
244 if ($adNwOptions['kill_front'] && is_front_page()) return $content ;
245 if ($adNwOptions['kill_cat'] && is_category()) return $content ;
246 if ($adNwOptions['kill_tag'] && is_tag()) return $content ;
247 if ($adNwOptions['kill_archive'] && is_archive()) return $content ;
248 $mc = $adNwOptions['mc'] ;
249 $this->mced = false ;
250 global $nwCount ;
251 if ($nwCount >= $this->nwMax) return $content ;
252 if(strpos($content, "<!--noadsense-->") !== false) return $content;
253 $metaOptions = $this->contentMeta() ;
254 if ($metaOptions['adsense'] == 'no') return $content;
255
256 $show_leadin = $metaOptions['show_leadin'] ;
257 $leadin = '' ;
258 if ($show_leadin != 'no')
259 {
260 if ($nwCount < $this->nwMax)
261 {
262 $nwCount++;
263 $leadin =
264 stripslashes($adNwOptions['info'] . "<!-- Post[count: " . $nwCount . "] -->\n" .
265 '<div class="adsense adsense-leadin" style="' .
266 $show_leadin . ';margin: 12px;">' .
267 $this->mc($mc, $adNwOptions['ad_text']) . '</div>') ;
268 }
269 }
270
271 $show_midtext = $metaOptions['show_midtext'] ;
272 if ($show_midtext != 'no')
273 {
274 if ($nwCount < $this->nwMax)
275 {
276 $poses = array();
277 $lastpos = -1;
278 $repchar = "<p";
279 if(strpos($content, "<p") === false)
280 $repchar = "<br";
281
282 while(strpos($content, $repchar, $lastpos+1) !== false){
283 $lastpos = strpos($content, $repchar, $lastpos+1);
284 $poses[] = $lastpos;
285 }
286 $half = sizeof($poses);
287 while(sizeof($poses) > $half)
288 array_pop($poses);
289 $pickme = $poses[floor(sizeof($poses)/2)];
290 $nwCount++;
291 $midtext =
292 stripslashes($adNwOptions['info'] . "<!-- Post[count: " . $nwCount . "] -->\n" .
293 '<div class="adsense adsense-midtext" style="' .
294 $show_midtext . ';margin: 12px;">' .
295 $this->mc($mc, $adNwOptions['ad_text']) . '</div>') ;
296 $content = substr_replace($content, $midtext.$repchar, $pickme, 2);
297 }
298 }
299
300 $show_leadout = $metaOptions['show_leadout'] ;
301 $leadout = '' ;
302 if ($show_leadout != 'no')
303 {
304 if ($nwCount < $this->nwMax)
305 {
306 $nwCount++;
307 $leadout =
308 stripslashes($adNwOptions['info'] . "<!-- Post[count: " . $nwCount . "] -->\n" .
309 '<div class="adsense adsense-leadout" style="' .
310 $show_leadout . ';margin: 12px;">' .
311 $this->mc($mc, $adNwOptions['ad_text']) . '</div>') ;
312 }
313 }
314
315 return $leadin . $content . $leadout ;
316 }
317 }
318} //End Class adsNow
319
320$nwCount = 0 ;
321
322// provide a replacement for htmlspecialchars_decode() (for PHP4 compatibility)
323if (!function_exists("htmlspecialchars_decode")) {
324 function htmlspecialchars_decode($string,$style=ENT_COMPAT) {
325 $translation = array_flip(get_html_translation_table(HTML_SPECIALCHARS,$style));
326 if($style === ENT_QUOTES){ $translation['&#039;'] = '\''; }
327 return strtr($string,$translation);
328 }
329}
330
331if (class_exists("adsNow")) {
332 $nw_ad = new adsNow();
333 if (isset($nw_ad) && !empty($nw_ad->defaults)) {
334 //Initialize the admin panel
335 if (!function_exists("adsNow_ap")) {
336 function adsNow_ap() {
337 global $nw_ad ;
338 if (function_exists('add_options_page')) {
339 add_options_page('AdSense Now!', 'AdSense Now!', 9,
340 basename(__FILE__), array(&$nw_ad, 'printAdminPage'));
341 }
342 }
343 }
344
345 add_filter('the_content', array($nw_ad, 'adsNow_content'));
346 add_action('admin_menu', 'adsNow_ap');
347 add_action('activate_' . basename(dirname(__FILE__)) . '/' . basename(__FILE__),
348 array(&$nw_ad, 'init'));
349 add_filter('plugin_action_links', array($nw_ad, 'plugin_action'), -10, 2);
350 }
351}
352
353?>
Note: See TracBrowser for help on using the repository browser.