Mercurial > defr > drupal > ad
comparison external/ad_external.module @ 0:d8a3998dac8e ad
ajout module ad
author | pierre |
---|---|
date | Fri, 20 Feb 2009 14:04:09 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d8a3998dac8e |
---|---|
1 <?php | |
2 // $Id: ad_external.module,v 1.1.2.6.2.7 2009/02/17 18:56:26 jeremy Exp $ | |
3 | |
4 /** | |
5 * @file | |
6 * Enhances the ad module to support externally hosted ads, served via IFrames. | |
7 * It is recommended that you configure "Administer >> Content management >> | |
8 * Ads >> Settings >> Global settings >> Display type" to "Raw". | |
9 * If you configure the "Display type" to IFrame, you will be serving IFrames | |
10 * within IFrames. | |
11 * | |
12 * Copyright (c) 2005-2009. | |
13 * Jeremy Andrews <jeremy@tag1consulting.com>. | |
14 */ | |
15 | |
16 /** | |
17 * Function used to display the selected ad. | |
18 */ | |
19 function ad_external_display_ad($ad) { | |
20 return theme('ad_external_ad', $ad); | |
21 } | |
22 | |
23 /** | |
24 * Return a themed ad of type ad_remote. | |
25 * | |
26 * @param @ad | |
27 * The ad object. | |
28 * @return | |
29 * A string containing the ad markup. | |
30 */ | |
31 function theme_ad_external_ad($ad) { | |
32 global $base_url; | |
33 | |
34 if (isset($ad->aid)) { | |
35 $output = '<div class="external-advertisement" id="ad-'. $ad->aid .'">'; | |
36 // For now, we hard code this to only support caching with the file cache. | |
37 // As more cache types are introduced, we'll expand this. Ideally it should | |
38 // be cache type agnostic, but that seems to be unrealistic. | |
39 if (variable_get('ad_cache', 'none') == 'file') { | |
40 $adserve = variable_get('adserve', ''); | |
41 $display_variables = '&c=file'. module_invoke('ad_cache_file', 'adcacheapi', 'display_variables', array()); | |
42 $external = url("$base_url/$adserve?o=external&n=$ad->aid&$display_variables"); | |
43 } | |
44 else { | |
45 $external = db_result(db_query('SELECT url FROM {ad_external} WHERE aid = %d', $ad->aid)); | |
46 } | |
47 $append = 'frameborder="'. variable_get('ad_iframe_frameborder', 0) .'" '; | |
48 $append .= 'scrolling="'. variable_get('ad_iframe_scroll', 'auto') .'" '; | |
49 if ($height = variable_get('ad_iframe_height', '')) { | |
50 $append .= "height=\"$height\" "; | |
51 } | |
52 if ($width = variable_get('ad_iframe_width', '')) { | |
53 $append .= "width=\"$width\" "; | |
54 } | |
55 $output .= "<iframe src=\"$external\" $append></iframe>"; | |
56 $output .= '</div>'; | |
57 return $output; | |
58 } | |
59 } | |
60 | |
61 /** | |
62 * Implementation of hook_theme(). | |
63 */ | |
64 function ad_external_theme() { | |
65 return array( | |
66 'ad_external_ad' => array( | |
67 'file' => 'ad_external.module', | |
68 'arguments' => array( | |
69 'ad' => NULL, | |
70 ), | |
71 ), | |
72 ); | |
73 } | |
74 | |
75 /** | |
76 * Implementation of hook_help(). | |
77 */ | |
78 function ad_external_help($path, $arg) { | |
79 $output = ''; | |
80 switch ($path) { | |
81 case 'node/add/ad#external': | |
82 $output = t('An external advertisement, displayed in an IFrame.'); | |
83 break; | |
84 } | |
85 return $output; | |
86 } | |
87 | |
88 /** | |
89 * Implementation of hook_access(). | |
90 */ | |
91 function ad_external_access($op, $node, $account) { | |
92 return ad_access($op, $node, $account); | |
93 } | |
94 | |
95 /** | |
96 * Implementation of hook_adapi(). | |
97 */ | |
98 function ad_external_adapi($op, &$node) { | |
99 switch ($op) { | |
100 case 'load': | |
101 return db_fetch_array(db_query('SELECT * FROM {ad_external} WHERE aid = %d', $node['aid'])); | |
102 | |
103 case 'insert': | |
104 db_query("INSERT INTO {ad_external} (aid, url) VALUES(%d, '%s')", $node->nid, $node->url); | |
105 break; | |
106 | |
107 case 'update': | |
108 db_query("UPDATE {ad_external} SET url = '%s' WHERE aid = %d", $node->url, $node->nid); | |
109 break; | |
110 | |
111 case 'delete': | |
112 db_query('DELETE FROM {ad_external} WHERE aid = %d', $node->nid); | |
113 break; | |
114 | |
115 case 'form': | |
116 return ad_external_node_form($node); | |
117 | |
118 case 'view': | |
119 return ad_external_node_view($node); | |
120 | |
121 case 'redirect': | |
122 // TODO: Would it ever make sense to have redirects for this ad type? | |
123 watchdog('ad', 'Unexpected redirect attempt in external ad type.'); | |
124 return; | |
125 | |
126 case 'type': | |
127 return array( | |
128 'external' => array( | |
129 'name' => t('External ad'), | |
130 'module' => 'ad_external', | |
131 'description' => t('An external advertisement, displayed in an IFrame.'), | |
132 'help' => t('An external advertisement, displayed in an IFrame.'), | |
133 ), | |
134 ); | |
135 } | |
136 } | |
137 | |
138 /** | |
139 * Adapi helper function for displaying a node form. | |
140 */ | |
141 function ad_external_node_form(&$node) { | |
142 $form = array(); | |
143 | |
144 $form['ad_external'] = array( | |
145 '#type' => 'fieldset', | |
146 '#title' => t('External'), | |
147 '#collapsible' => TRUE, | |
148 ); | |
149 | |
150 $form['ad_external']['preview'] = array( | |
151 '#type' => 'markup', | |
152 '#value' => ad_external_display_ad($node), | |
153 ); | |
154 | |
155 if ((arg(1) == 'add' || arg(2) == 'edit') && | |
156 user_access('create advertisements')) { | |
157 $form['ad_external']['url'] = array( | |
158 '#type' => 'textfield', | |
159 '#title' => t('External Source URL'), | |
160 '#required' => TRUE, | |
161 '#default_value' => isset($node->url) ? $node->url : '', | |
162 '#description' => t('Enter the complete URL where your external ad his hosted. The URL must begin with http:// or https://. For example, %url.', array('%url' => t('http://www.sample.org/external/ad.php'))), | |
163 ); | |
164 | |
165 } | |
166 | |
167 return $form; | |
168 } | |
169 | |
170 /** | |
171 * Adapi helper function for displaying ad itself. | |
172 */ | |
173 function ad_external_node_view(&$node) { | |
174 $node->content['ad'] = array( | |
175 '#value' => theme('box', '', stripslashes(ad_external_display_ad($node))), | |
176 '#weight' => -1, | |
177 ); | |
178 $link = t('Links to !url.', array('!url' => $node->url)); | |
179 if (variable_get('ad_filter', 0)) { | |
180 $link = check_markup($link, $node->format, FALSE); | |
181 } | |
182 $node->content['ad-link'] = array( | |
183 '#value' => "<div class=\"links-to\">$link<div>", | |
184 '#weight' => 1, | |
185 ); | |
186 } | |
187 | |
188 /** | |
189 * Download external pages and store them in the filecache. | |
190 */ | |
191 function ad_external_ad_build_cache() { | |
192 $cache = array(); | |
193 $result = db_query('SELECT * FROM {ad_external}'); | |
194 while ($external = db_fetch_object($result)) { | |
195 $contents = file_get_contents($external->url); | |
196 if ($contents === FALSE) { | |
197 // Failed to download external url, don't cache the error page. | |
198 // TODO: Watchdog log this. | |
199 // TODO: Would it be possible to obtain the old version from the cache, | |
200 // if we already downloaded this page once? | |
201 continue; | |
202 } | |
203 $cache['ad_external'][$external->aid]['url'] = $external->url; | |
204 $cache['ad_external'][$external->aid]['contents'] = $contents; | |
205 } | |
206 return $cache; | |
207 } |