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