]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenX/OpenXPlugin.php
b857a7440bd38263a20078c84519f1c4cdc4049c
[quix0rs-gnu-social.git] / plugins / OpenX / OpenXPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin for OpenX ad server
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Ads
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2010 StatusNet Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Plugin for OpenX Ad Server
36  *
37  * This plugin supports the OpenX ad server, http://www.openx.org/
38  *
39  * We support the 4 ad sizes for the Universal Ad Platform (UAP):
40  *
41  *     Medium Rectangle
42  *     (Small) Rectangle
43  *     Leaderboard
44  *     Wide Skyscraper
45  *
46  * They fit in different places on the default theme. Some themes
47  * might interact quite poorly with this plugin.
48  *
49  * To enable advertising, you will need an OpenX server. You'll need
50  * to set up a "zone" for your StatusNet site that identifies a
51  * kind of ad you want to place (of the above 4 sizes).
52  *
53  * Add the plugin to config.php like so:
54  *
55  *     addPlugin('OpenX', array('adScript' => 'full path to script',
56  *                              'rectangle' => 1));
57  *
58  * Here, the 'adScript' parameter is the full path to the OpenX
59  * ad script, like 'http://example.com/www/delivery/ajs.php'. Note
60  * that we don't do any magic to swap between HTTP and HTTPS, so
61  * if you want HTTPS, say so.
62  *
63  * The 'rectangle' parameter is the zone ID for that ad space on
64  * your site. If you've configured another size, try 'mediumRectangle',
65  * 'leaderboard', or 'wideSkyscraper'.
66  *
67  * If for some reason your ad server is different from the default,
68  * use the 'adScript' parameter to set the full path to the ad script.
69  *
70  * @category Ads
71  * @package  StatusNet
72  * @author   Evan Prodromou <evan@status.net>
73  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
74  * @link     http://status.net/
75  *
76  * @seeAlso  UAPPlugin
77  */
78 class OpenXPlugin extends UAPPlugin
79 {
80     const PLUGIN_VERSION = '2.0.0';
81
82     public $adScript = null;
83
84     function initialize()
85     {
86         parent::initialize();
87
88         // A little bit of chicanery so we avoid overwriting values that
89         // are passed in with the constructor
90         foreach (array('mediumRectangle', 'rectangle', 'leaderboard', 'wideSkyscraper', 'adScript') as $setting) {
91             $value = common_config('openx', $setting);
92             if (!empty($value)) { // not found
93                 $this->$setting = $value;
94             }
95         }
96
97         return true;
98     }
99
100     /**
101      * Show a medium rectangle 'ad'
102      *
103      * @param Action $action Action being shown
104      *
105      * @return void
106      */
107     protected function showMediumRectangle($action)
108     {
109         $this->showAd($action, $this->mediumRectangle);
110     }
111
112     /**
113      * Show a rectangle 'ad'
114      *
115      * @param Action $action Action being shown
116      *
117      * @return void
118      */
119     protected function showRectangle($action)
120     {
121         $this->showAd($action, $this->rectangle);
122     }
123
124     /**
125      * Show a wide skyscraper ad
126      *
127      * @param Action $action Action being shown
128      *
129      * @return void
130      */
131     protected function showWideSkyscraper($action)
132     {
133         $this->showAd($action, $this->wideSkyscraper);
134     }
135
136     /**
137      * Show a leaderboard ad
138      *
139      * @param Action $action Action being shown
140      *
141      * @return void
142      */
143     protected function showLeaderboard($action)
144     {
145         $this->showAd($action, $this->leaderboard);
146     }
147
148     /**
149      * Show an ad using OpenX
150      *
151      * @param Action  $action Action being shown
152      * @param integer $zone   Zone to show
153      *
154      * @return void
155      */
156     protected function showAd($action, $zone)
157     {
158 $scr = <<<ENDOFSCRIPT
159 var m3_u = '%s';
160 var m3_r = Math.floor(Math.random()*99999999999);
161 if (!document.MAX_used) document.MAX_used = ',';
162 document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
163 document.write ("?zoneid=%d");
164 document.write ('&amp;cb=' + m3_r);
165 if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
166 document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
167 document.write ("&amp;loc=" + escape(window.location));
168 if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
169 if (document.context) document.write ("&context=" + escape(document.context));
170 if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
171 document.write ("'><\/scr"+"ipt>");
172 ENDOFSCRIPT;
173
174         $action->inlineScript(sprintf($scr, $this->adScript, $zone));
175         return true;
176     }
177
178     function onRouterInitialized($m)
179     {
180         $m->connect('panel/openx',
181                     ['action' => 'openxadminpanel']);
182
183         return true;
184     }
185
186     function onEndAdminPanelNav($menu) {
187         if (AdminPanelAction::canAdmin('openx')) {
188             // TRANS: Menu item title.
189             $menu_title = _m('OpenX configuration.');
190             // TRANS: Menu item for site administration
191             $menu->out->menuItem(common_local_url('openxadminpanel'), _m('OpenX'),
192                                  $menu_title, $action_name == 'openxadminpanel', 'nav_openx_admin_panel');
193         }
194         return true;
195     }
196
197     /**
198      * Add our version information to output
199      *
200      * @param array &$versions Array of version-data arrays
201      *
202      * @return boolean hook value
203      */
204     function onPluginVersion(array &$versions)
205     {
206         $versions[] = array('name' => 'OpenX',
207                             'version' => self::PLUGIN_VERSION,
208                             'author' => 'Evan Prodromou',
209                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/OpenX',
210                             'rawdescription' =>
211                             // TRANS: Plugin description.
212                             _m('Plugin for <a href="http://www.openx.org/">OpenX Ad Server</a>.'));
213         return true;
214     }
215 }