/[drupal]/contributions/modules/feedburner/feedburner.admin.inc
ViewVC logotype

Contents of /contributions/modules/feedburner/feedburner.admin.inc

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


Revision 1.1 - (show annotations) (download) (as text)
Fri Jul 27 05:12:36 2007 UTC (2 years, 4 months ago) by davereid
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
Admin functions
1 <?php
2 // $Id$
3
4 function _feedburner_build_overview() {
5 // Add the filter form above the overview table.
6 $keys = _feedburner_path_keys();
7 $output = drupal_get_form('_feedburner_build_filter_form', $keys);
8
9 if (!empty($keys)) {
10 // Replace wildcards with MySQL/PostgreSQL wildcards.
11 $keys = preg_replace('!\*+!', '%', $keys);
12 $sql = "SELECT f.local_uri, f.fb_uri, f.verified FROM {feedburner_feeds} f LEFT JOIN {url_alias} u ON f.local_uri = u.src WHERE f.local_uri LIKE '%%%s%%' OR f.fb_uri LIKE '%%%s%%' OR u.dst LIKE '%%%s%%'";
13 }
14 else {
15 $sql = 'SELECT f.local_uri, f.fb_uri, f.verified FROM {feedburner_feeds} f';
16 }
17 $header = array(
18 array('data' => t('System Path'), 'field' => 'f.local_uri', 'sort' => 'asc'),
19 array('data' => t('FeedBurner'), 'field' => 'f.fb_uri'),
20 array('data' => t('Verified'), 'field' => 'f.verified'),
21 array('data' => t('Operations'), 'colspan' => '3'),
22 );
23 $sql .= tablesort_sql($header);
24 $result = pager_query($sql, 50, 0, null, $keys, $keys, $keys);
25
26 $rows = array();
27 //$destination = drupal_get_destination();
28 while ($data = db_fetch_object($result)) {
29 $alias = drupal_get_path_alias($data->local_uri);
30 $rows[] = array(
31 l($data->local_uri . ($data->local_uri != $alias ? ' ('. $alias .')' : ''), $data->local_uri),
32 l($data->fb_uri, _feedburner_url($data->fb_uri)),
33 ($data->verified > 0 ? 'Yes' : l(t('No'), 'feedburner/verify/'. $data->fb_uri, null, 'local_uri='. $data->local_uri)),
34 l(t('edit'), 'admin/build/feedburner/edit/'. $data->local_uri),
35 l(t('remove'), 'admin/build/feedburner/remove/'. $data->local_uri),
36 l(t('source'), $data->local_uri, array(), 'FEEDBURNER=NO_REDIRECT'),
37 );
38 }
39
40 if (empty($rows)) {
41 if (empty($keys)) {
42 $alias = drupal_get_path_alias('rss.xml');
43 $rows[] = array(
44 l('rss.xml'. ('rss.xml' != $alias ? ' ('. $alias .')' : ''), 'rss.xml'),
45 'None', '', l(t('redirect'), 'admin/build/feedburner/add/rss.xml'),
46 );
47 }
48 else {
49 $rows[] = array(array('data' => t('No FeedBurner feeds found.'), 'colspan' => '5'));
50 }
51 }
52
53 $output .= theme('table', $header, $rows);
54 $output .= theme('pager', null, 50, 0);
55
56 $output .= feedburner_help('feedburner_about');
57
58 return $output;
59 }
60
61 /**
62 * Return a form to filter FeedBurner feeds.
63 */
64 function _feedburner_build_filter_form($keys = '') {
65 $form['#attributes'] = array(
66 'class' => 'search-form',
67 );
68 $form['basic'] = array(
69 '#type' => 'fieldset',
70 '#title' => t('Filter feeds'),
71 );
72 $form['basic']['inline'] = array(
73 '#prefix' => '<div class="container-inline">',
74 '#suffix' => '</div>',
75 );
76 $form['basic']['inline']['filter'] = array(
77 '#type' => 'textfield',
78 '#title' => '',
79 '#default_value' => $keys,
80 '#maxlength' => 64,
81 '#size' => 25,
82 );
83 $form['basic']['inline']['submit'] = array(
84 '#type' => 'submit',
85 '#value' => t('Filter'),
86 );
87
88 return $form;
89 }
90
91 /**
92 * Process filter form submission.
93 */
94 function _feedburner_build_filter_form_submit($form_id, $form_values) {
95 return 'admin/build/feedburner/list/'. trim($form_values['filter']);
96 }
97
98 function _feedburner_build_edit_form($edit = '') {
99 $form['#base'] = '_feedburner_build_edit_form';
100
101 $form['local_uri'] = array(
102 '#type' => 'textfield',
103 '#title' => t('Local Feed'),
104 '#default_value' => $edit['local_uri'],
105 '#maxlength' => 128,
106 '#size' => 30,
107 //'#disabled' => !empty($local_uri),
108 '#description' => t('Specify the existing feed on your site from which to redirect.'),
109 '#field_prefix' => url(null, null, null, true) . (variable_get('clean_url', 0) ? '' : '?q='),
110 '#validate' => array('_feedburner_build_edit_validate' => array('local_uri')),
111 '#autocomplete_path' => 'feedburner/autocomplete',
112 );
113 $form['fb_uri'] = array(
114 '#type' => 'textfield',
115 '#title' => t('FeedBurner Feed'),
116 '#default_value' => $edit['fb_uri'],
117 '#maxlength' => 100,
118 '#size' => 30,
119 '#description' => t('Requests to the above feed will be requested to this FeedBurner feed. This URI is case-sensitive and alphanumeric.'),
120 '#field_prefix' => _feedburner_url(),
121 '#validate' => array('_feedburner_build_edit_validate' => array('fb_uri')),
122 );
123 $form['submit'] = array(
124 '#type' => 'submit',
125 '#value' => t('Save redirection'),
126 );
127
128 return $form;
129 }
130
131 function _feedburner_build_edit_validate($form_element, $field_name) {
132 $value = $form_element['#value'];
133 switch ($field_name) {
134 case 'local_uri':
135 $value = strtolower(drupal_get_normal_path($value));
136 form_set_value($form_element, $value);
137 /*$data = drupal_http_request(url($value, null, null, true));
138 if ($data->code != 200) {
139 drupal_set_message(t('The local feed %path could not be validated.'), 'error')
140 }*/
141 break;
142 case 'fb_uri':
143 if (!preg_match('/\\A[\\w\\x2D\\x2F]*\\z/i', $value)) {
144 form_set_error($field_name, 'Invalid FeedBurner URI');
145 }
146 break;
147 }
148 }
149
150 function _feedburner_build_edit_form_submit($form_id, $form_values) {
151 if (_feedburner_save($form_values['local_uri'], $form_values['fb_uri']) == true) {
152 drupal_set_message(t('The FeedBurner redirect has been saved.'));
153 _feedburner_verify_feed($form_values['fb_uri'], $form_values['local_uri']);
154 //return 'feedburner/verify/'. $form_values['fb_uri'];
155 }
156 return 'admin/build/feedburner';
157 }
158
159 function _feedburner_build_remove() {
160 $path = _feedburner_path_keys();
161 if (!empty($path) && _feedburner_save($path) == true) {
162 drupal_set_message(t('The FeedBurner redirection for %path has been removed.', array('%path' => $path)));
163 }
164 drupal_goto('admin/build/feedburner');
165 }
166
167 function _feedburner_settings_form() {
168 $form['redirection'] = array(
169 '#type' => 'fieldset',
170 '#title' => t('Feed Redirection'),
171 );
172 $form['redirection']['feedburner_redirect_status'] = array(
173 '#type' => 'select',
174 '#title' => t('Redirect Header Status'),
175 '#default_value' => variable_get('feedburner_redirect_status', FEEDBURNER_DEFAULT_REDIRECT_STATUS),
176 '#options' => array(
177 301 => '301 Moved Permanently',
178 302 => '302 Found',
179 307 => '307 Temporary Redirect'),
180 '#description' => t("Please note that the '307 Temporary Redirect' is recommended but if your subscribers are having problems you may want to try a different status code. For detailed descriptions of HTTP redirect status codes, see <a href=\"@link\">the Wikipedia article on HTTP status codes</a>.", array('@link' => 'http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection')),
181 );
182 $form['redirection']['feedburner_blogs'] = array(
183 '#type' => 'checkbox',
184 '#title' => t("Allow users to specify their own blogs' FeedBurner feed."),
185 '#default_value' => variable_get('feedburner_blogs', false),
186 '#description' => t("If the user has the permission 'edit own blog', this option is found in his/her 'edit account' page."),
187 );
188
189 $form['feedflare'] = array(
190 '#type' => 'fieldset',
191 '#title' => t('FeedFlare/Standard Stats/Ad Network'),
192 '#description' => t('See <a href="@link">@link</a> for more information.', array('@link' => 'http://www.feedburner.com/fb/a/publishers/feedflare')),
193 '#collapsible' => false,
194 );
195 /*if (!array_key_exists('rss2.xml', _feedburner_get_redirects())) {
196 $form['feedflare']['notice'] = array(
197 '#value' => '<p><strong>FeedFlare is only enabled when you have a FeedBurner feed for your site\'s main feed (rss.xml).</strong></p>',
198 );
199 }*/
200 $form['feedflare']['feedburner_feedflare_node_types'] = array(
201 //'#type' => 'checkboxes',
202 '#type' => 'select',
203 '#title' => t('Node types'),
204 '#multiple' => true,
205 '#default_value' => variable_get('feedburner_feedflare_node_types', array()),
206 '#options' => node_get_types('names'),
207 );
208 $form['feedflare']['feedburner_feedflare'] = array(
209 '#type' => 'select',
210 '#title' => t('FeedFlare in nodes'),
211 '#default_value' => variable_get('feedburner_feedflare', 0),
212 //'#disabled' => !array_key_exists('rss.xml', _feedburner_get_redirects()),
213 '#options' => array(
214 0 => t('Disabled'),
215 1 => t('Teaser view'),
216 2 => t('Full-page view'),
217 3 => t('Teasers and full-page view')),
218 '#description' => t('When to display the FeedFlare after the node text.'),
219 );
220
221 /*$form['account'] = array(
222 '#type' => 'fieldset',
223 '#title' => t('FeedBurner Account'),
224 '#description' => t('Currently your username and password are stored as plaintext in the Drupal database. FeedBurner has yet to allow more secure methods of authentication.'),
225 '#collapsible' => true,
226 '#collapsed' => true,
227 );
228 $form['account']['feedburner_username'] = array(
229 '#type' => 'textfield',
230 '#title' => t('Your FeedBurner username'),
231 '#default_value' => variable_get('feedburner_username', ''),
232 '#size' => 30,
233 '#maxlength' => 30,
234 );*/
235 /*$form['account']['feedburner_password'] = array(
236 '#type' => 'password',
237 '#title' => t('Your FeedBurner account password'),
238 '#default_value' => variable_get('feedburner_password', ''),
239 '#size' => 30,
240 '#maxlength' => 30,
241 );*/
242
243 $form['advanced'] = array(
244 '#type' => 'fieldset',
245 '#title' => t('Advanced Settings'),
246 '#collapsible' => true,
247 '#collapsed' => true,
248 );
249 $form['advanced']['feedburner_useragents'] = array(
250 '#type' => 'textarea',
251 '#title' => t('User Agents Allowed Direct Feed Access'),
252 '#description' => t('Enter a list of user agents (one per line) that are allowed direct access to your site\'s Feeds and will not be redirected to the FeedBurner feeds.'),
253 '#default_value' => preg_replace('/\\|/', "\n", variable_get('feedburner_useragents', FEEDBURNER_DEFAULT_USERAGENTS)),
254 '#rows' => 3,
255 '#validate' => array('_feedburner_settings_validate' => array('feedburner_useragents')),
256 );
257 $form['advanced']['feedburner_domain'] = array(
258 '#type' => 'textfield',
259 '#title' => t('MyBrand Custom Domain'),
260 '#description' => t('<strong>For Advanced Users Only!</strong> MyBrand is a premium FeedBurner service that allows you to showcase your feeds by serving them from your own domain (e.g., @domain). You must have access to change your CNAME entry in the DNS records for the domain you wish to change. For more information, see <a href="@link">@link</a>.', array('@link' => 'http://www.feedburner.com/fb/a/mybrand', '@domain' => 'feeds.'. $_SERVER['SERVER_NAME'])),
261 '#default_value' => variable_get('feedburner_domain', FEEDBURNER_DEFAULT_DOMAIN),
262 '#field_prefix' => 'http://',
263 '#field_suffix' => '/feedname',
264 '#size' => 30,
265 '#maxlength' => 30,
266 '#validate' => array('_feedburner_settings_validate' => array('feedburner_domain')),
267 );
268
269 $form['blah'] = array(
270 '#type' => 'markup',
271 '#prefix' => '',
272 '#suffix' => '',
273 '#value' => feedburner_help('feedburner_about'),
274 '#weight' => 10,
275 );
276
277 return system_settings_form($form);
278 }
279
280 /**
281 * Validate and correct values from the settings form.
282 * - Format user agents from a list to a regex-ready string
283 * - Check custom domain name syntax
284 */
285 function _feedburner_settings_validate($form_element, $field_name) {
286 $value = $form_element['#value'];
287 switch ($field_name) {
288 case 'feedburner_useragents':
289 // Replace one or more consecutive line breaks or pipes with regex pipes,
290 // remove any non-word non-pipe characters and also remove any pipes at
291 // the beginning or end of the string
292 $value = preg_replace(array('/[\\r\\n\\|]+/', '/[^\\|\\w]|\\A\\|+|\\|+\\z/'), array('|', ''), $value);
293 form_set_value($form_element, $value);
294 break;
295 case 'feedburner_domain':
296 if ($value != FEEDBURNER_DEFAULT_DOMAIN) {
297 $data = drupal_http_request('http://'. $value);
298 if (!isset($data->redirect_url) && ($data->redirect_url != 'http://www.feedburner.com/fb/a/home' || preg_match('/\/fb\/a\/home$/', $data->redirect_url))) {
299 drupal_set_message($field_name, 'The MyBrand domain could not be confirmed. Make sure to edit the CNAME settings for the domain before enabling this option.', 'error');
300 }
301 }
302 break;
303 }
304 }
305
306 /*
307 * Extract the remaining part of the query url from a certain argument onward.
308 * @param pos the position in the query argument, default 5th
309 * @return the remaining url paramenters (including any backslashes)
310 */
311 function _feedburner_path_keys($pos = 5) {
312 $path = explode('/', $_GET['q'], $pos);
313 return count($path) == $pos ? $path[$pos - 1] : '';
314 }
315
316 /**
317 * Updates or removes a feed in the database.
318 * @param local_uri The local system feed uri to update.
319 * @param fb_uri The FeedBurner feed uri (set to null to remove redirection).
320 * @return true if the redirection is updated or removed, false otherwise.
321 */
322 function _feedburner_save($local_uri, $fb_uri = null) {
323 //$result = _feedburner_load($local_uri);
324 if (empty($fb_uri)) {
325 db_query("DELETE FROM {feedburner_feeds} WHERE local_uri = '%s'", $local_uri);
326 }
327 else {
328 db_query("UPDATE {feedburner_feeds} SET fb_uri = '%s' WHERE local_uri = '%s'", $fb_uri, $local_uri);
329 $rows = db_affected_rows();
330 if (empty($rows)) {
331 db_query("INSERT INTO {feedburner_feeds} (local_uri, fb_uri) VALUES ('%s', '%s')", $local_uri, $fb_uri);
332 }
333 return true;
334 }
335 return false;
336 }
337
338 /**
339 * Removes a feed's output from the Drupal cache. This is used just to be sure
340 * that no one is accidentally served the cached version of a feed instead of
341 * being redirected.
342 * @param local_uri The local system URI of the feed.
343 */
344 /*function _feedburner_clear_feed_cache($local_uri) {
345 // TODO: replace base_url with url function calls
346 // Need to clear the cache for the feed page and also an alias if it is set
347 // ?? cache_clear_all(url($local_uri), 'cache_page');
348 global $base_url;
349 cache_clear_all($base_url .'/'. $local_uri, 'cache_page');
350
351 $alias = drupal_get_path_alias($local_uri);
352 if ($alias != $local_uri) {
353 cache_clear_all($base_url .'/'. $alias, 'cache_page');
354 }
355 }*/
356
357 /**
358 * Verifies a FeedBurner feed and its details and features available.
359 * @param fb_uri The FeedBurner feed uri to verify.
360 */
361 function _feedburner_verify_feed($fb_uri, $local_uri = null) {
362 if ($local_uri === true) {
363 $local_uri = db_result(db_query("SELECT local_uri FROM {feedburner_feeds} WHERE fb_uri = '%s'", $fb_uri));
364 }
365
366 $fb_request = drupal_http_request(_feedburner_url($fb_uri));
367 if ($fb_request->code == 200) {
368 $feed = array();
369
370 // Feed ID (email subscription link) detection
371 if (preg_match('/(\\d+)<\/feedburner:emailServiceId>/i', $fb_request->data, $regs)) {
372 $feed['id'] = intval($regs[1]);
373 }
374
375 // Awareness API detection
376 if (preg_match('/<feedburner:awareness>\\S+<\/feedburner:awareness>/i', $fb_request->data)) {
377 $feed['awareness_api'] = true;
378 }
379
380 // FeedFlare detection
381 if (preg_match('/feedflare/i', $fb_request->data) && preg_match('/~f\/'. $fb_uri .'/i', $fb_request->data)) {
382 $feed['feedflare'] = true;
383 }
384
385 // Ad Network detection
386 if (preg_match('/~a\/'. $fb_uri .'/i', $fb_request->data)) {
387 $feed['ad_network'] = true;
388 }
389
390 // Update database with details
391 db_query("UPDATE {feedburner_feeds} SET fid = %d, verified = %d, details = '%s' WHERE fb_uri = '%s' AND local_uri = '%s'", (isset($feed['id']) ? $feed['id'] : 0), time(), serialize($feed), $fb_uri, $local_uri);
392 drupal_set_message(t('The FeedBurner feed %path was successfully verified.', array('%path' => $fb_url)));
393 //return true;
394 }
395 else {
396 db_query("UPDATE {feedburner_feeds} SET fid = 0, verified = 0, details = '' WHERE fb_uri = '%s' AND local_uri = '%s'", $fb_uri, $local_uri);
397 drupal_set_message(t('The FeedBurner feed %path could not be verified. Please check that you have set up the feed in <a href="@fb-account">your FeedBurner account</a> or you can <a href="@fb-create">burn the feed at Feedburner</a>.', array('%path' => $fb_uri, '@fb-account' => 'http://www.feedburner.com/fb/a/myfeeds', '@fb-create' => 'https://www.feedburner.com/fb/a/addfeed?sourceUrl='. url($local_uri, null, null, true) .'&mappedUri='. $fb_uri)), 'error');
398 //return false;
399 }
400
401 if (isset($local_uri)) {
402 $local_request = drupal_http_request(url($local_uri, 'feedburner=no_redirect', null, true));
403 if ($local_request->code != 200
404 || !isset($local_request->headers['Content-Type'])
405 || !preg_match('/xml|rss|atom|rdf/i', $local_request->headers['Content-Type'])) {
406 drupal_set_message(t('The local feed source %path could not be verified. Please make sure it exists.', array('%path' => $local_uri)), 'error');
407 }
408
409 $redirect_request = drupal_http_request(url($local_uri, null, null, true));
410 if (!isset($redirect_request->redirect_url)
411 || $redirect_request->redirect_url != _feedburner_url($fb_uri)
412 || !isset($redirect->code)
413 || $redirect->code != variable_get('feedburner_redirect_status', FEEDBURNER_DEFAULT_REDIRECT_STATUS)) {
414 drupal_set_message(t('Redirection from the local feed %local to the FeedBurner feed %fb could not be confirmed. Please check to make sure it works.', array('%local' => local_uri, '%fb' => $fb_uri)));
415 }
416 }
417 }
418
419 /**
420 * Pings the FeedBurner service. Called when a redirection has changed.
421 * @return If the ping request was successful.
422 */
423 function _feedburner_ping() {
424 $request = drupal_http_request('http://www.feedburner.com/fb/a/ping?bloglink='. urlencode(url(null, null, null, true)));
425 return ($request->code == 200);
426 }
427
428
429 /**
430 * Retrieve a pipe delimited string of autocomplete suggestions for feed uris.
431 */
432 function _feedburner_autocomplete() {
433 $matches = array();
434 $string = _feedburner_path_keys(3);
435
436 if ($string) {
437 $result = _feedburner_list_feeds();
438 $string = preg_replace(array('/\\//', '/\\./'), array('\\/', '\.'), $string);
439 foreach ($result as $feed) {
440 if (preg_match('/'. $string .'/i', $feed)) {
441 $matches[$feed] = check_plain($feed);
442
443 }
444 }
445 $matches = array_slice($matches, 0, 10);
446 }
447
448 echo drupal_to_js($matches);
449 exit();
450 }
451
452
453 function _feedburner_list_feeds() {
454 $feeds = array();
455 $feeds[] = 'rss.xml';
456
457 // Comment RSS integration
458 if (module_exists('commentrss')) {
459 $feeds[] = 'crss';
460 }
461
462 // View RSS integration
463 if (module_exists('views') && module_exists('views_rss')) {
464 $result = db_query("SELECT url FROM {view_view} WHERE page_type = 'views_rss'");
465 while ($view_feed = db_fetch_object($result)) {
466 $feeds[] = $view_feed->url;
467 }
468 }
469
470 // Blog integration
471 if (module_exists('blog')) {
472 $feeds[] = 'blog/feed';
473 // Get each user's blog feed as well
474 $result = db_query('SELECT uid FROM {users} WHERE status = 1');
475 while ($user_details = db_fetch_object($result)) {
476 $feeds[] = 'blog/'. $user_details->uid .'/feed';
477 }
478 }
479
480 // Taxonomy integration
481 if (module_exists('taxonomy')) {
482 $result = db_query('SELECT tid FROM {term_data}');
483 while ($term_details = db_fetch_object($result)) {
484 $feeds[] = 'taxonomy/term/'. $term_details->tid .'/0/feed';
485 }
486 }
487
488
489 /*$aliases = array();
490 // Change each url into it's path alias?
491 foreach ($feeds as $feed) {
492 $alias = drupal_get_path_alias($feed);
493 if ($feed != $alias) {
494 $aliases[] = $alias;
495 }
496 }
497 $feeds = array_merge($feeds, $aliases);*/
498
499 foreach ($feeds as &$feed) {
500 $feed = drupal_get_path_alias($feed);
501 }
502
503 sort($feeds);
504 return $feeds;
505 }

  ViewVC Help
Powered by ViewVC 1.1.2