/[drupal]/contributions/modules/aggregator2/aggregator2_easyfeed.module
ViewVC logotype

Diff of /contributions/modules/aggregator2/aggregator2_easyfeed.module

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

revision 1.1, Thu Jul 20 11:54:40 2006 UTC revision 1.1.2.1, Thu Jul 20 11:54:40 2006 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: $
3    
4    /*
5     * Aggregator2 Easy Feed module
6     *
7     * @file
8     * Adds "url" field to user registration form and creates feed note for that url after user is registered
9     * Sponsored by John Bransford.
10     */
11    
12    /*
13       Copyright (C) 2005-2006 by Marcin Konicki <ahwayakchih@gmail.com>
14    
15       This program is free software; you can redistribute it and/or modify
16       it under the terms of the GNU General Public License.
17       This program is distributed in the hope that it will be useful,
18       but WITHOUT ANY WARRANTY.
19    
20       See the LICENSE file for more details.
21    */
22    
23    
24    /**
25    * Implementation of hook_help().
26    */
27    function aggregator2_easyfeed_help($section) {
28      switch ($section) {
29        case 'admin/modules#description':
30          return t('Allows users to create aggregator2 feed at registration time in a very easy way.');
31    
32        case 'admin/settings/aggregator2_easyfeed':
33          return '<p>'. t('Allows users to create aggregator2 feed at registration time in a very easy way. Requires Aggregator2 module.') .'</p>';
34      }
35    }
36    
37    /**
38    * Implementation of hook_user().
39    */
40    function aggregator2_easyfeed_user($type, &$edit, &$edit_user, $category = NULL) {
41      switch ($type) {
42        case 'register':
43          $output = '';
44          $output .= form_textfield(t('Feed URL'), 'agg2_easyfeed_url', $edit['agg2_easyfeed_url'], 60, 250, t('If You want to add Your feed to this site, enter its URL here.'), NULL, NULL);
45          $output .= form_textfield(t('Feed Logo URL'), 'agg2_easyfeed_logo', $edit['agg2_easyfeed_logo'], 60, 250, t('Optionally You can give us URL of logo to be used for Your feed.'), NULL, NULL);
46          return array(array('title' => t('Add Your Feed'), 'data' => $output, 'weight' => 0));
47          break;
48        case 'insert':
49          _aggregator2_easyfeed_add($edit['agg2_easyfeed_url'], $edit);
50          $edit['agg2_easyfeed_url'] = NULL;
51          $edit['agg2_easyfeed_logo'] = NULL;
52          break;
53      }
54    }
55    
56    /**
57     * Implementation of hook_settings().
58     */
59    function aggregator2_easyfeed_settings() {
60      $output = '';
61    
62      $result = db_query('SELECT n.nid, n.title FROM {node} n, {aggregator2_feed} af WHERE n.nid = af.nid');
63      $list = array();
64      $list[0] = t('--------------');
65      while ($feed = db_fetch_object($result)) {
66        $list[$feed->nid] = $feed->title;
67      }
68      $output .= form_select(t('Feed Template'), 'agg2_easyfeed_template', variable_get('agg2_easyfeed_template', 0), $list, t('Select which aggregator2 feed will be used as template for all new feeds created by easyfeed module.'));
69    
70      return $output;
71    }
72    
73    /**
74     * Private function; Create aggregator2 feed node
75     */
76    function _aggregator2_easyfeed_add($url, &$user) {
77      $target = trim($url);
78      if (strlen($target) < 10 || !function_exists('aggregator2_http_request') || !variable_get('agg2_easyfeed_template', 0)) {
79        return;
80      }
81    
82      $result = db_query("SELECT nid FROM {aggregator2_feed} WHERE url = '%s'", $url);
83      if ($result) {
84        $nid = db_fetch_object($result);
85        if ($nid && $nid->nid) {
86          return;
87        }
88      }
89    
90      $result = aggregator2_http_request($url, array(), 15);
91      switch ($result->code) {
92        case 301:
93          _aggregator2_easyfeed_add($result->redirect_url);
94          break;
95    
96        case 200:
97        case 302:
98        case 307:
99        {
100          $edit = node_load(array('nid' => variable_get('agg2_easyfeed_template', 0)));
101          if (!$edit->nid) {
102            return;
103          }
104    
105          $xml_tree = aggregator2_parse_xml($result->data);
106          if ($xml_tree['parser_error']) {
107            return;
108          }
109    
110          /*
111          ** Taxonomy module doesn't add taxonomy terms at load time... so we have to do it by hand :((
112          */
113          $terms = module_invoke('taxonomy', 'node_get_terms', $edit->nid, 'tid');
114          foreach ($terms as $tid => $term) {
115            if ($term->tid) {
116              $edit->taxonomy[] = $term->tid;
117            }
118          }
119    
120          $edit->uid = $user['uid'];
121          $edit->name = $user['name'];
122          $edit->url = $url;
123          unset($edit->nid);
124          unset($edit->path); // avoid path alias conflicts!
125          unset($edit->parent_node); // avoid messing up relativity module's data :)
126          unset($edit->image); // cleanup logo
127          $edit->date = format_date(time(), 'custom', 'Y-m-d H:i O');
128          $edit->created = strtotime($edit->date);
129    
130          /*
131          ** Prepare data:
132          */
133          if ($xml_tree['RSS']) { // RSS 0.91, 0.92, 2.0
134            $root = &$xml_tree['RSS'][0];
135            $channel = &$root['CHANNEL'][0];
136            $title = &$channel['TITLE'][0]['VALUE'];
137            $image = &$channel['IMAGE'][0];
138            $description = &$channel['DESCRIPTION'][0]['VALUE'];
139            $link = &$channel['LINK'][0]['VALUE'];
140          }
141          else if ($xml_tree['RDF:RDF']) {
142            $root = &$xml_tree['RDF:RDF'][0];
143            $channel = &$root['CHANNEL'][0];
144            $title = &$channel['TITLE'][0]['VALUE'];
145            $image = &$root['IMAGE'][0];
146            $description = &$channel['DESCRIPTION'][0]['VALUE'];
147            $link = &$channel['LINK'][0]['VALUE'];
148          }
149          else if ($xml_tree['FEED']) { // Atom 0.3, 1.0
150            $root = &$xml_tree['FEED'][0];
151            $channel = &$root;
152            $title = &$channel['TITLE'][0]['VALUE'];
153            $image = &$channel['LOGO'][0]['VALUE'];
154            $description = ($channel['TAGLINE'][0]['VALUE'] ? $channel['TAGLINE'][0]['VALUE'] : '');
155            // TODO: remove this Atom hack when we have field mapping or at least specialized parsers in place
156            if (count($channel['LINK']) > 1) {
157              $link = $feed->link;
158              foreach ($channel['LINK'] as $l) {
159                if ($l['REL'] == 'alternate') {
160                  $link = $l['HREF'];
161                }
162              }
163            }
164            else {
165              $link = $channel['LINK'][0]['HREF'];
166            }
167          }
168          else if ($xml_tree['CHANNEL']) { // RSS 1.1
169            $root = &$xml_tree['CHANNEL'][0];
170            $channel = &$root;
171            $title = &$channel['TITLE'][0]['VALUE'];
172            $image = &$channel['IMAGE'][0];
173            $description = &$channel['DESCRIPTION'][0]['VALUE'];
174            $link = &$channel['LINK'][0]['VALUE'];
175          }
176          else {
177            // unsupported format
178            return;
179          }
180    
181          if (trim($title) != '') {
182            $edit->title = $title;
183          }
184          else {
185            $edit->title = t('New feed #%time', array('%time' => time()));
186          }
187          if ($description/* && valid_input_data($description)*/) {
188            $edit->body = $edit->teaser = $description;
189          }
190    
191          /*
192          ** Generate image link
193          */
194          if ($image['LINK'] && $image['URL'] && $image['TITLE']) {
195            if (strlen($image['TITLE'][0]['VALUE']) > 250) {
196              $image['TITLE'][0]['VALUE'] = trim(substr($image['TITLE'][0]['VALUE'], 0, 250)).'...';
197            }
198            $edit->image = '<a href="'. $image['LINK'][0]['VALUE'] .'" class="aggregator2_logo_link"><img src="'. ($user['agg2_easyfeed_logo'] ? $user['agg2_easyfeed_logo'] : $image['URL'][0]['VALUE']) .'" class="aggregator2_logo" alt="'. $image['TITLE'][0]['VALUE'] .'" /></a>';
199          }
200          else if ($user['agg2_easyfeed_logo'] && $link) {
201            $edit->image = '<a href="'. $link .'" class="aggregator2_logo_link"><img src="'. $link .'" class="aggregator2_logo" alt="'. $title .'" /></a>';
202          }
203    
204    
205          $feed = node_validate($edit);
206          // Re-set user because Drupal sets it to anonymous (newly registered user is not logged in)
207          $feed->uid = $user['uid'];
208          $feed->name = $user['name'];
209    
210          $errors = NULL;
211          if (!($errors = form_get_errors())) {
212            $nid = node_save($feed);
213            flush();
214            sleep(variable_get('aggregator2_sleep_interval', 3));
215            return $nid;
216          }
217          else {
218            watchdog('aggregator2', t('Failed to validate aggregator2-feed for %site: %error.', array('%site' => '<em>'. $feed->title .'</em>', '%error' => '<em>'. implode("\n", $errors) .'</em>')), WATCHDOG_ERROR, l(t('view'), 'node/'.$feed->nid));
219            drupal_set_message(t('Failed to validate aggregator2-feed for %site: %error.', array('%site' => '<em>'. $feed->title .'</em>', '%error' => '<em>'. implode("\n", $errors) .'</em>')));
220          }
221        }
222    
223        default:
224          break;
225      }
226    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2