/[drupal]/contributions/modules/openads/openads.module
ViewVC logotype

Contents of /contributions/modules/openads/openads.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Fri Mar 21 05:51:32 2008 UTC (20 months ago) by jaydub
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.6: +96 -9 lines
File MIME type: text/x-php
added in support for OpenX XML-RPC ad invocation method. This requires that external libraries be installed! Javascript method is used as fallback if serving a cached page.
1 <?php
2 // $Id$
3
4 /**
5 * Created on 12.01.2007
6 *
7 * @author Gerd Riesselmann
8 *
9 * jaydub is new maintainer March 2008
10 *
11 */
12
13 /**
14 * Implementation of hook_help().
15 */
16 function openads_help($section) {
17 switch ($section) {
18 case 'admin/help#openads':
19 case 'admin/modules#description':
20 return t('OpenX AdServer integration');
21 }
22 }
23
24 /**
25 * Implementation of hook_menu().
26 */
27 function openads_menu($may_cache) {
28 $items = array();
29 if ($may_cache) {
30 $items[] = array(
31 'path' => 'admin/settings/openads',
32 'title' => t('OpenX (Openads) Adserver'),
33 'description' => t('Configure OpenX (Openads) AdServer integration behavior and appearance.'),
34 'callback' => 'drupal_get_form',
35 'callback arguments' => array('openads_admin_settings'),
36 'access' => user_access('administer site configuration'),
37 'type' => MENU_NORMAL_ITEM, // optional
38 );
39 }
40 return $items;
41 }
42
43 /**
44 * Implemenation of hook_settings().
45 */
46 function openads_admin_settings() {
47 $form = array();
48
49 $form['adserver'] = array(
50 '#type' => 'fieldset',
51 '#title' => t('Adserver settings'),
52 '#description' => t('The OpenX (Openads) adserver is highly configurable regarding the URL it uses. The according settings can be found at <strong>Settings =&gt; Main Settings</strong> in your adserver adminstration interface.'),
53 '#collapsible' => TRUE,
54 );
55
56 if (variable_get('openads_delivery_method', 'js') == 'xmlrpc') {
57 $PEAR_missing = $openads_xmlrpc_missing = FALSE;
58 if (!@include('XML/RPC.php')) {
59 $PEAR_missing = TRUE;
60 }
61 if (!@include('openads-xmlrpc.inc.php')) {
62 $openads_xmlrpc_missing = TRUE;
63 }
64 }
65
66 $form['adserver']['openads_delivery_method'] = array(
67 '#type' => 'radios',
68 '#title' => t('Delivery Method'),
69 '#description' => t('Select which delivery method to use - JavaScript or XML-RPC. <b>Note</b> to use XML-RPC method you must have the PEAR XML-RPC package installed and the OpenX openads-xmlrpc.inc.php file must be in your PHP include path. '. ($PEAR_missing ? '<br><span style="color:#f00;">PEAR XML-RPC appears to not be installed!</span> You must install the <a href="@link">PEAR XML-RPC</a> package.' : '') . ($openads_xmlrpc_missing ? '<br><span style="color:#f00;">opeands-xmlrpc.inc.php file appears to not be installed!</span> You must put the file openads-xmlrpc.inc.php from the OpenX installation files into your PHP path.' : ''), array('@link' => 'http://pear.php.net/package/XML_RPC')),
70 '#options' => array('js' => 'JavaScript', 'xmlrpc' => 'XML-RPC'),
71 '#default_value' => variable_get('openads_delivery_method', 'js'),
72 );
73
74 $form['adserver']['openads_delivery_url'] = array(
75 '#type' => 'textfield',
76 '#default_value' => variable_get('openads_delivery_url', ''),
77 '#title' => t('The OpenX delivery url'),
78 '#description' => t('The OpenX server address, for example "ads.example.org/delivery". "http://" is automatically prefixed.')
79 );
80 $form['adserver']['openads_delivery_url_https'] = array(
81 '#type' => 'textfield',
82 '#default_value' => variable_get('openads_delivery_url_https', ''),
83 '#title' => t('The OpenX https delivery url'),
84 '#description' => t('The OpenX server address, for example "ads.example.org/delivery". "https://" is automatically prefixed.')
85 );
86 $form['adserver']['openads_js_delivery_filename'] = array(
87 '#type' => 'textfield',
88 '#default_value' => variable_get('openads_js_delivery_filename', 'ajs.php'),
89 '#title' => t('The OpenX JavaScript delivery filename'),
90 '#description' => t('The OpenX JavaScript delivery filename for example "ajs.php"')
91 );
92 $form['adserver']['openads_xmlrpc_delivery_filename'] = array(
93 '#type' => 'textfield',
94 '#default_value' => variable_get('openads_xmlrpc_delivery_filename', 'axmlrpc.php'),
95 '#title' => t('The OpenX XML-RPC delivery filename'),
96 '#description' => t('The OpenX XML-RPC delivery filename for example "axmlrpc.php"')
97 );
98
99 $form['adserver']['xmlrpc_options'] = array(
100 '#type' => 'fieldset',
101 '#title' => t('XML-RPC Options'),
102 '#description' => t('The following options affect the XML-RPC connection settings'),
103 '#collapsible' => TRUE,
104 '#collapsed' => TRUE,
105 );
106 $form['adserver']['xmlrpc_options']['openads_xmlrpc_port'] = array(
107 '#type' => 'textfield',
108 '#title' => t('XML-RPC Port'),
109 '#description' => t('If your XML-RPC server is on a non-standard port, specify it here.'),
110 '#default_value' => variable_get('openads_xmlrpc_port', 80),
111 '#size' => 4,
112 );
113 $form['adserver']['xmlrpc_options']['openads_xmlrpc_use_ssl'] = array(
114 '#type' => 'checkbox',
115 '#title' => t('Use SSL'),
116 '#description' => t('If you wish to use SSL to connect to your OpenX XML-RPC server, check this option'),
117 '#default_value' => variable_get('openads_xmlrpc_use_ssl', ''),
118 );
119 $form['adserver']['xmlrpc_options']['openads_xmlrpc_timeout'] = array(
120 '#type' => 'textfield',
121 '#title' => t('Timeout'),
122 '#description' => t('Set the timeout in seconds to connect to the OpenX XML-RPC server'),
123 '#default_value' => variable_get('openads_xmlrpc_timeout', 2),
124 '#size' => 4,
125 );
126
127 $form['publisher'] = array(
128 '#type' => 'fieldset',
129 '#title' => t('Publisher'),
130 '#description' => t('The following settings can be copied form the publisher page of your adserver.'),
131 '#collapsible' => TRUE,
132 );
133 $form['publisher']['openads_id'] = array(
134 '#type' => 'textfield',
135 '#default_value' => variable_get('openads_id', ''),
136 '#title' => t('The Publisher ID for this page'),
137 '#description' => t("This is the publisher's mnemonic on the publisher's properties page."),
138 '#size' => 10,
139 );
140 $form['publisher']['openads_num_zones'] = array(
141 '#type' => 'textfield',
142 '#default_value' => variable_get('openads_num_zones', '5'),
143 '#title' => t('The number of zones defined'),
144 '#description' => t('This is the number of zones you can enter below'),
145 '#size' => 10,
146 );
147
148 $form['openads_zones'] = array(
149 '#theme' => 'openads_admin_settings_form',
150 '#type' => 'fieldset',
151 '#title' => t('Zones'),
152 '#description' => t('Configure your zones here. '.
153 '<ul>'.
154 '<li>Both zone id and code are generated by your adserver, so copy it from there.</li>'.
155 '<li>The name can be freely chosen, and you are able to invoke ads using this name later one.</li>'.
156 '<li>For each user role, you can either show (checked) or hide (unchecked) ad of the according zone</li>'.
157 '</ul>'.
158 'Once you have convigured your zones, you can invoke them within your templates using either the '.
159 'index number (0-4, for example, this is the first column) or by the names you gave your zones. '.
160 'The according code is <strong>&lt;?php print openads_invoke(0);&gt;</strong> or <strong>&lt;?php print openads_invoke("my zone name");&gt;</strong>'),
161 '#collapsible' => TRUE,
162 );
163 $zones = openads_get_zones();
164 $roles = user_roles();
165 $i = 0;
166 foreach ($zones as $zone) {
167 $form['openads_zones'][$i]["openads_zones_id_$i"] = array(
168 '#type' => 'textfield',
169 '#default_value' => $zone['id'],
170 '#size' => '6'
171 );
172 $form['openads_zones'][$i]["openads_zones_code_$i"] = array(
173 '#type' => 'textfield',
174 '#default_value' => $zone['code'],
175 '#size' => '12'
176 );
177 $form['openads_zones'][$i]["openads_zones_name_$i"] = array(
178 '#type' => 'textfield',
179 '#default_value' => $zone['name'],
180 '#size' => '16'
181 );
182 foreach ($roles as $key => $display) {
183 $element_key = 'openads_zones_roles_'. $key .'_'. $i;
184 $form['openads_zones'][$i][$element_key] = array(
185 '#type' => 'checkbox',
186 '#default_value' => $zone['roles'][$key]
187 );
188 }
189 $i++;
190 }
191
192 return system_settings_form($form);
193 }
194
195 /**
196 * Implementation of hook_block().
197 */
198 function openads_block($op = 'list', $delta = 0, $edit = array()) {
199 if ($op == 'list') {
200 $zones = openads_get_zones();
201 foreach ($zones as $index => $zone) {
202 if ($zone['id']) {
203 $blocks[$index] = array(
204 'info' => t('OpenX Zone '. $zone['id'] .' ('. ($zone['name'] != '' ? $zone['name'] : 'untitled') .')'),
205 'weight' => 0,
206 'enabled' => 0,
207 'region' => 'left'
208 );
209 }
210 }
211 return $blocks;
212 }
213 else if ($op == 'view') {
214 $block = array(
215 'subject' => t('Advertising'),
216 'content' => theme('openads_block', $delta)
217 );
218 return $block;
219 }
220 }
221
222 /**
223 * Implement hook_footer to inject our JavaScript
224 */
225 function openads_footer($main = 0) {
226 $channel = '
227 <script type="text/javascript">
228 <!--
229 var az_u = "'. _openads_trim_path(variable_get('openads_delivery_url', '')) .'";
230 var az_us = "'. _openads_trim_path(variable_get('openads_delivery_url_https', '')) .'";
231 var az_js = "'. variable_get('openads_js_delivery_filename', '') .'";
232 -->
233 </script>';
234
235 drupal_set_html_head($channel);
236 drupal_add_js(drupal_get_path('module', 'openads') .'/openads.js');
237 }
238
239 /**
240 * Prepare a server path
241 */
242 function _openads_trim_path($path) {
243 return trim($path, '/') .'/';
244 }
245
246 /**
247 * Returns all stored zones
248 */
249 function openads_get_zones() {
250 static $ret = NULL;
251 if ($ret) {
252 return $ret;
253 }
254
255 $c = variable_get('openads_num_zones', 5);
256 $ret = array();
257 for ($i = 0; $i < $c; $i ++) {
258 $ret[$i]['id'] = variable_get("openads_zones_id_$i", NULL);
259 $ret[$i]['code'] = variable_get("openads_zones_code_$i", NULL);
260 $ret[$i]['name'] = variable_get("openads_zones_name_$i", NULL);
261 $ret[$i]['roles'] = array();
262 foreach (user_roles() as $key => $name) {
263 $element_key = 'openads_zones_roles_'. $key .'_'. $i;
264 $ret[$i]['roles'][$key] = variable_get($element_key, TRUE);
265 }
266 }
267 return $ret;
268 }
269
270 /**
271 * Return zone with given index
272 */
273 function openads_get_zone($index_or_key) {
274 $zones = openads_get_zones();
275 if (isset($zones[$index_or_key])) {
276 return $zones[$index_or_key];
277 }
278 else {
279 foreach ($zones as $zone) {
280 if ($zone['name'] == $index_or_key) {
281 return $zone;
282 }
283 }
284 }
285 return array('id' => FALSE, 'code' => FALSE, 'name' => '', 'roles' => array());
286 }
287
288 /**
289 * Builds and returns at_channel content
290 */
291 function openads_channel() {
292 return theme('openads_channel');
293 }
294
295 /**
296 * Builds the call to openads
297 */
298 function openads_invoke($index_or_key) {
299 $zone = openads_get_zone($index_or_key);
300 $id = $zone['id'];
301 $code = $zone['code'];
302 if (empty($id) || empty($zone)) {
303 return '';
304 }
305
306 global $user;
307 foreach ($user->roles as $key => $value) {
308 if (!$zone['roles'][$key]) {
309 return '';
310 }
311 }
312
313 return theme('openads_invoke', $id, $code);
314 }
315
316 function theme_openads_admin_settings_form($form) {
317 $roles = user_roles();
318 foreach ($form as $name => $element) {
319 $id_col = "openads_zones_id_$name";
320 $code_col = "openads_zones_code_$name";
321 $name_col = "openads_zones_name_$name";
322 if (isset($element[$id_col]) && is_array($element[$id_col])) {
323 $arr = array(
324 check_plain($name),
325 drupal_render($element[$id_col]),
326 drupal_render($element[$code_col]),
327 drupal_render($element[$name_col])
328 );
329 foreach ($roles as $key => $rolename) {
330 $element_key = 'openads_zones_roles_'. $key .'_'. $name;
331 $arr[] = drupal_render($element[$element_key]);
332 }
333 $rows[] = $arr;
334 }
335 }
336
337 $header = array(t('Number'), t('Zone ID'), t('Code'), t('Name'));
338 foreach ($roles as $key => $rolename) {
339 $header[] = $rolename;
340 }
341 $output .= theme('table', $header, $rows);
342
343 return $output;
344 }
345
346 function theme_openads_invoke($zoneid, $code) {
347 global $user;
348
349 $cache = variable_get('cache', CACHE_DISABLED);
350 if (($cache != 0 && isset($user->uid) && $user->uid == 0) || variable_get('openads_delivery_method', 'js') == 'js') {
351 $out = "
352 <script type=\"text/javascript\"><!--
353 az_adjs($zoneid, '$code');
354 --></script>";
355 }
356 else if (variable_get('openads_delivery_method', 'js') == 'xmlrpc') {
357 static $oaXmlRpc;
358 static $OA_context;
359
360 if (!isset($OA_context)) {
361 $OA_context = array();
362 }
363 if (!$oaXmlRpc) {
364 $xmlrpc_settings = _openads_get_xmlrpc_settings();
365 $oaXmlRpc = _openads_connect_xmlrpc($xmlrpc_settings);
366 }
367 $ad = $oaXmlRpc->view('zone:'. $zoneid, 0, '', '', 0, $OA_context);
368 $out = $ad['html'];
369 }
370 return $out;
371 }
372
373 function _openads_get_xmlrpc_settings() {
374 $xmlrpc_settings['url'] = variable_get('openads_delivery_url', '');
375 list($xmlrpc_settings['host'], $xmlrpc_settings['path']) = split('/', $xmlrpc_settings['url'], 2);
376 $xmlrpc_settings['file'] = variable_get('openads_xmlrpc_delivery_filename', 'axmlrpc.php');
377 $xmlrpc_settings['port'] = variable_get('openads_xmlrpc_port', 80);
378 $xmlrpc_settings['ssl'] = (variable_get('openads_xmlrpc_use_ssl', 0) == 0) ? FALSE : TRUE ;
379 $xmlrpc_settings['timeout'] = variable_get('openads_xmlrpc_timeout', 2);
380 return $xmlrpc_settings;
381 }
382
383 function _openads_connect_xmlrpc($xmlrpc_settings) {
384 require('openads-xmlrpc.inc.php');
385 $oaXmlRpc = new OA_XmlRpc($xmlrpc_settings['host'], '/'. $xmlrpc_settings['path'] .'/'. $xmlrpc_settings['file'], $xmlrpc_settings['port'], $xmlrpc_settings['ssl'], $xmlrpc_settings['timeout']);
386 return $oaXmlRpc;
387 }
388
389 function theme_openads_channel() {
390 global $user;
391 $url = check_plain($_GET['q']);
392 $channel[] = variable_get('openads_id', '') .'/'. $url;
393
394 if ($user) {
395 if ($user->uid == 1) {
396 $channel[] = 'type_admin';
397 }
398 foreach ($user->roles as $roleid => $rolename) {
399 $channel[] = 'type_'. str_replace(' ', '_', $rolename);
400 }
401 }
402
403 $channel[] = 'url_'. $url;
404 return implode(',', $channel);
405 }
406
407 function theme_openads_block($index) {
408 return openads_invoke($index);
409 }

  ViewVC Help
Powered by ViewVC 1.1.2