1 | <?php |
---|
2 | /***************************************************************\ |
---|
3 | * This is 'Not Evil Ads', a plugin for Dotclear 2 * |
---|
4 | * * |
---|
5 | * Copyright (c) 2007 * |
---|
6 | * Oleksandr Syenchuk and contributors. * |
---|
7 | * * |
---|
8 | * This is an open source software, distributed under the GNU * |
---|
9 | * General Public License (version 2) terms and conditions. * |
---|
10 | * * |
---|
11 | * You should have received a copy of the GNU General Public * |
---|
12 | * License along 'Not evil ads' (see COPYING.txt); * |
---|
13 | * if not, write to the Free Software Foundation, Inc., * |
---|
14 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
15 | \***************************************************************/ |
---|
16 | if (!defined('DC_RC_PATH')) { return; } |
---|
17 | |
---|
18 | include_once dirname(__FILE__).'/lib/class.notevilads.php'; |
---|
19 | |
---|
20 | $core->addBehavior('publicBeforeDocument',array('publicNotEvilAds','adjustCache')); |
---|
21 | |
---|
22 | $core->url->register('notEvilAdsXML','notEvilAdsXML','^notEvilAdsXML.*$',array('publicNotEvilAds','XMLinterface')); |
---|
23 | |
---|
24 | $core->tpl->addValue('notEvilAdsShowAd', array('publicNotEvilAds','showAd')); |
---|
25 | $core->tpl->addValue('notEvilAdsShowTrigger', array('publicNotEvilAds','showTrigger')); |
---|
26 | $core->tpl->addValue('neaTriggerURI',array('publicNotEvilAds','tplTriggerURI')); |
---|
27 | $core->tpl->addBlock('neaIf',array('publicNotEvilAds','tplStatusIf')); |
---|
28 | |
---|
29 | global $__notEvilAds; |
---|
30 | $nea_settings = notEvilAds::loadSettings($core->blog->settings->get('nea_settings')); |
---|
31 | $nea_ads = notEvilAds::loadAds($core->blog->settings->get('nea_ads')); |
---|
32 | |
---|
33 | if (!is_array($nea_ads) || !is_array($nea_settings)) { |
---|
34 | $__notEvilAds = null; |
---|
35 | return; |
---|
36 | } |
---|
37 | |
---|
38 | $__notEvilAds = new notEvilAds($nea_settings,$nea_ads); |
---|
39 | |
---|
40 | # This will work if JavaScript fail |
---|
41 | if (!empty($_REQUEST['notEvilAdsTriggerOn'])) { |
---|
42 | $__notEvilAds->setStatus(true); |
---|
43 | publicNotEvilAds::triggerUserPrefs(); |
---|
44 | } |
---|
45 | elseif (!empty($_REQUEST['notEvilAdsTriggerOff'])) { |
---|
46 | $__notEvilAds->setStatus(false); |
---|
47 | publicNotEvilAds::triggerUserPrefs(); |
---|
48 | } |
---|
49 | |
---|
50 | if (isset($_REQUEST['showAds'])) |
---|
51 | { |
---|
52 | $__notEvilAds->setStatus((bool) $_REQUEST['showAds']); |
---|
53 | publicNotEvilAds::triggerUserPrefs(); |
---|
54 | } |
---|
55 | |
---|
56 | class publicNotEvilAds |
---|
57 | { |
---|
58 | public static function triggerUserPrefs() |
---|
59 | { |
---|
60 | # Set cookie for 365 days |
---|
61 | setcookie('user_upddt',time(),time()+31536000,'/'); |
---|
62 | } |
---|
63 | |
---|
64 | public static function adjustCache(&$core) |
---|
65 | { |
---|
66 | if (!empty($_COOKIE['user_upddt'])) { |
---|
67 | $GLOBALS['mod_ts'][] = (int) $_COOKIE['user_upddt']; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | private static function getOperator($op) |
---|
72 | { |
---|
73 | switch (strtolower($op)) { |
---|
74 | case 'or': |
---|
75 | case '||': |
---|
76 | return '||'; |
---|
77 | case 'and': |
---|
78 | case '&&': |
---|
79 | default: |
---|
80 | return '&&'; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | public static function tplStatusIf($args,$content) |
---|
85 | { |
---|
86 | $if = array(); |
---|
87 | |
---|
88 | if (isset($args['status'])) { |
---|
89 | $sign = (boolean) $args['status'] ? '' : '!'; |
---|
90 | $if[] = $sign.'$__notEvilAds->getStatus()'; |
---|
91 | } |
---|
92 | |
---|
93 | if (empty($if)) { |
---|
94 | return $content; |
---|
95 | } |
---|
96 | else { |
---|
97 | $op = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&'; |
---|
98 | return '<?php if ($__notEvilAds !== null && ('.implode(' '.$op.' ',$if).')) : ?>'.$content.'<?php endif; ?>'; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | public static function triggerURI($status=null) |
---|
103 | { |
---|
104 | global $__notEvilAds; |
---|
105 | |
---|
106 | if ($__notEvilAds === null) { |
---|
107 | $status = false; |
---|
108 | } |
---|
109 | elseif ($status === null) { |
---|
110 | $status = !$GLOBALS['__notEvilAds']->getStatus(); |
---|
111 | } |
---|
112 | $status = $status ? '1' : '0'; |
---|
113 | |
---|
114 | $uri = http::getSelfURI(); |
---|
115 | |
---|
116 | # If showAds setting is already present in URI, we will replace its value |
---|
117 | if (preg_match('/(\\?|&)showAds\\=[^&]*/',$uri)) { |
---|
118 | $uri = preg_replace( |
---|
119 | '/(\\?|&)(showAds\\=)([^&]*)/', |
---|
120 | '$1${2}'.$status, |
---|
121 | $uri); |
---|
122 | } |
---|
123 | else { |
---|
124 | $ext = |
---|
125 | (strpos($uri,'?') === false |
---|
126 | ? '?' |
---|
127 | : (empty($_SERVER['QUERY_STRING']) |
---|
128 | ? '' |
---|
129 | : '&')). |
---|
130 | 'showAds='; |
---|
131 | $uri = $uri.$ext.$status; |
---|
132 | } |
---|
133 | |
---|
134 | return $uri; |
---|
135 | } |
---|
136 | |
---|
137 | public static function tplTriggerURI($args) |
---|
138 | { |
---|
139 | if (isset($args['status'])) { |
---|
140 | $status = $args['status'] ? 'true' : 'false'; |
---|
141 | } |
---|
142 | else { |
---|
143 | $status = 'null'; |
---|
144 | } |
---|
145 | return '<?php echo publicNotEvilAds::triggerURI('.$status.'); ?>'; |
---|
146 | } |
---|
147 | |
---|
148 | public static function showAdsInWidgets(&$w,$i) |
---|
149 | { |
---|
150 | global $core,$__notEvilAds; |
---|
151 | |
---|
152 | $ad = $__notEvilAds->showAd($w->identifier); |
---|
153 | |
---|
154 | # Show nothing on error or if not allowed |
---|
155 | if ($ad === null || $ad['disable'] || ($ad['nothome'] && $core->url->type == 'default')) |
---|
156 | return ''; |
---|
157 | |
---|
158 | $optTitle = $ad['title'] ? '<h2>'.$ad['title'].'</h2>'."\n" : ''; |
---|
159 | |
---|
160 | return ($__notEvilAds->getStatus() || !$ad['notevil']) |
---|
161 | ? "<div id=\"".$ad['identifier']."\" ".$ad['attr'].">\n". |
---|
162 | $optTitle.$ad['htmlcode']. |
---|
163 | "\n</div>" |
---|
164 | : ''; |
---|
165 | } |
---|
166 | |
---|
167 | public static function triggerAdsInWidgets(&$w) |
---|
168 | { |
---|
169 | global $core,$__notEvilAds; |
---|
170 | |
---|
171 | if ($__notEvilAds->getStatus()) |
---|
172 | { $submitName = 'Off'; $submitValue=html::escapeHTML($w->hValue); } |
---|
173 | else |
---|
174 | { $submitName = 'On'; $submitValue=html::escapeHTML($w->sValue); } |
---|
175 | |
---|
176 | return ' |
---|
177 | <div id="notEvilAdsTriggerDiv" style="text-align:center"> |
---|
178 | <form id="notEvilAdsForm" action="'.http::getSelfURI().'" method="post"> |
---|
179 | <p><input type="submit" name="notEvilAdsTrigger'.$submitName.'" id="notEvilAdsTrigger" value="'.$submitValue.'"/></p> |
---|
180 | </form> |
---|
181 | </div>'; |
---|
182 | } |
---|
183 | |
---|
184 | public static function showAd($attr) |
---|
185 | { |
---|
186 | global $core,$__notEvilAds; |
---|
187 | |
---|
188 | if (empty($attr['id'])) |
---|
189 | return '<p><em>'.__('No identifier specified.').'</em></p>'; |
---|
190 | |
---|
191 | $res = |
---|
192 | "\$id = \"".addslashes($attr['id'])."\"; |
---|
193 | |
---|
194 | \$ad = \$__notEvilAds->showAd(\$id); |
---|
195 | |
---|
196 | # Show nothing on error or if not allowed |
---|
197 | if (\$ad === null |
---|
198 | || \$ad['disable'] |
---|
199 | || (\$ad['nothome'] && \$core->url->type == 'default')) |
---|
200 | echo ''; |
---|
201 | else |
---|
202 | echo |
---|
203 | '<div id=\"'.\$ad['identifier'].'\" '.\$ad['attr'].'>'. |
---|
204 | (\$__notEvilAds->getStatus() |
---|
205 | ? \$ad['htmlcode'] : ''). |
---|
206 | '</div>';"; |
---|
207 | |
---|
208 | return '<?php '.$res.' ?>'; |
---|
209 | } |
---|
210 | |
---|
211 | public static function showTrigger($attr) |
---|
212 | { |
---|
213 | global $core,$__notEvilAds; |
---|
214 | |
---|
215 | if (is_array($attr)) |
---|
216 | { |
---|
217 | $attr = isset($attr['extra']) ? ' '.$attr['extra'] : ''; |
---|
218 | $hValue = isset($attr['hide']) ? $attr['hide'] : __('Hide ads'); |
---|
219 | $sValue = isset($attr['show']) ? $attr['show'] : __('Show me ads'); |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | $attr = ''; |
---|
224 | $hValue = __('Hide ads'); |
---|
225 | $sValue = __('Show me ads'); |
---|
226 | } |
---|
227 | $res = |
---|
228 | " |
---|
229 | if (\$__notEvilAds->getStatus()) |
---|
230 | { \$submitName = 'Off'; \$submitValue=html::escapeHTML(\"".$hValue."\"); } |
---|
231 | else |
---|
232 | { \$submitName = 'On'; \$submitValue=html::escapeHTML(\"".addslashes($sValue)."\"); } |
---|
233 | |
---|
234 | echo '<div id=\"notEvilAdsTriggerDiv\"".addcslashes($attr,"'")."> |
---|
235 | <form id=\"notEvilAdsForm\" action=\"'.http::getSelfURI().'\" method=\"post\"> |
---|
236 | <p><input type=\"submit\" name=\"notEvilAdsTrigger'.\$submitName.'\" id=\"notEvilAdsTrigger\" value=\"'.\$submitValue.'\"/></p> |
---|
237 | </form> |
---|
238 | </div>';"; |
---|
239 | |
---|
240 | return '<?php '.$res.' ?>'; |
---|
241 | } |
---|
242 | |
---|
243 | public static function XMLinterface() |
---|
244 | { |
---|
245 | global $core,$__notEvilAds; |
---|
246 | |
---|
247 | if (isset($_REQUEST['notEvilAdsGetContent'])) |
---|
248 | { |
---|
249 | $id = (string) $_REQUEST['notEvilAdsGetContent']; |
---|
250 | echo $__notEvilAds->sendHTMLCode($id); |
---|
251 | } |
---|
252 | else |
---|
253 | { |
---|
254 | echo $__notEvilAds->sendXMLStatus(); |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | ?> |
---|