Dotclear

source: plugins/notEvilAds/_public.php @ 271

Revision 271, 7.0 KB checked in by sacha, 16 years ago (diff)

Plugins first import.

SOME PLUGINS MAY BE BROKEN

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

Sites map