aggregator2-4.6.x-1.x-dev/aggregator2_easyfeed.module
aggregator2_easyfeed.module
<?php
/*
* Aggregator2 Easy Feed module
*
* @file
* Adds "url" field to user registration form and creates feed note for that url after user is registered
* Sponsored by John Bransford.
*/
/*
Copyright (C) 2005-2006 by Marcin Konicki <ahwayakchih@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the LICENSE file for more details.
*/
/**
* Implementation of hook_help().
*/
function aggregator2_easyfeed_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Allows users to create aggregator2 feed at registration time in a very easy way.');
case 'admin/settings/aggregator2_easyfeed':
return '<p>'. t('Allows users to create aggregator2 feed at registration time in a very easy way. Requires Aggregator2 module.') .'</p>';
}
}
/**
* Implementation of hook_user().
*/
function aggregator2_easyfeed_user($type, &$edit, &$edit_user, $category = NULL) {
switch ($type) {
case 'register':
$output = '';
$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);
$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);
return array(array('title' => t('Add Your Feed'), 'data' => $output, 'weight' => 0));
break;
case 'insert':
_aggregator2_easyfeed_add($edit['agg2_easyfeed_url'], $edit);
$edit['agg2_easyfeed_url'] = NULL;
$edit['agg2_easyfeed_logo'] = NULL;
break;
}
}
/**
* Implementation of hook_settings().
*/
function aggregator2_easyfeed_settings() {
$output = '';
$result = db_query('SELECT n.nid, n.title FROM {node} n, {aggregator2_feed} af WHERE n.nid = af.nid');
$list = array();
$list[0] = t('--------------');
while ($feed = db_fetch_object($result)) {
$list[$feed->nid] = $feed->title;
}
$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.'));
return $output;
}
/**
* Private function; Create aggregator2 feed node
*/
function _aggregator2_easyfeed_add($url, &$user) {
$target = trim($url);
if (strlen($target) < 10 || !function_exists('aggregator2_http_request') || !variable_get('agg2_easyfeed_template', 0)) {
return;
}
$result = db_query("SELECT nid FROM {aggregator2_feed} WHERE url = '%s'", $url);
if ($result) {
$nid = db_fetch_object($result);
if ($nid && $nid->nid) {
return;
}
}
$result = aggregator2_http_request($url, array(), 15);
switch ($result->code) {
case 301:
_aggregator2_easyfeed_add($result->redirect_url);
break;
case 200:
case 302:
case 307:
{
$edit = node_load(array('nid' => variable_get('agg2_easyfeed_template', 0)));
if (!$edit->nid) {
return;
}
$xml_tree = aggregator2_parse_xml($result->data);
if ($xml_tree['parser_error']) {
return;
}
/*
** Taxonomy module doesn't add taxonomy terms at load time... so we have to do it by hand :((
*/
$terms = module_invoke('taxonomy', 'node_get_terms', $edit->nid, 'tid');
foreach ($terms as $tid => $term) {
if ($term->tid) {
$edit->taxonomy[] = $term->tid;
}
}
$edit->uid = $user['uid'];
$edit->name = $user['name'];
$edit->url = $url;
unset($edit->nid);
unset($edit->path); // avoid path alias conflicts!
unset($edit->parent_node); // avoid messing up relativity module's data :)
unset($edit->image); // cleanup logo
$edit->date = format_date(time(), 'custom', 'Y-m-d H:i O');
$edit->created = strtotime($edit->date);
/*
** Prepare data:
*/
if ($xml_tree['RSS']) { // RSS 0.91, 0.92, 2.0
$root = &$xml_tree['RSS'][0];
$channel = &$root['CHANNEL'][0];
$title = &$channel['TITLE'][0]['VALUE'];
$image = &$channel['IMAGE'][0];
$description = &$channel['DESCRIPTION'][0]['VALUE'];
$link = &$channel['LINK'][0]['VALUE'];
}
else if ($xml_tree['RDF:RDF']) {
$root = &$xml_tree['RDF:RDF'][0];
$channel = &$root['CHANNEL'][0];
$title = &$channel['TITLE'][0]['VALUE'];
$image = &$root['IMAGE'][0];
$description = &$channel['DESCRIPTION'][0]['VALUE'];
$link = &$channel['LINK'][0]['VALUE'];
}
else if ($xml_tree['FEED']) { // Atom 0.3, 1.0
$root = &$xml_tree['FEED'][0];
$channel = &$root;
$title = &$channel['TITLE'][0]['VALUE'];
$image = &$channel['LOGO'][0]['VALUE'];
$description = ($channel['TAGLINE'][0]['VALUE'] ? $channel['TAGLINE'][0]['VALUE'] : '');
// TODO: remove this Atom hack when we have field mapping or at least specialized parsers in place
if (count($channel['LINK']) > 1) {
$link = $feed->link;
foreach ($channel['LINK'] as $l) {
if ($l['REL'] == 'alternate') {
$link = $l['HREF'];
}
}
}
else {
$link = $channel['LINK'][0]['HREF'];
}
}
else if ($xml_tree['CHANNEL']) { // RSS 1.1
$root = &$xml_tree['CHANNEL'][0];
$channel = &$root;
$title = &$channel['TITLE'][0]['VALUE'];
$image = &$channel['IMAGE'][0];
$description = &$channel['DESCRIPTION'][0]['VALUE'];
$link = &$channel['LINK'][0]['VALUE'];
}
else {
// unsupported format
return;
}
if (trim($title) != '') {
$edit->title = $title;
}
else {
$edit->title = t('New feed #%time', array('%time' => time()));
}
if ($description/* && valid_input_data($description)*/) {
$edit->body = $edit->teaser = $description;
}
/*
** Generate image link
*/
if ($image['LINK'] && $image['URL'] && $image['TITLE']) {
if (strlen($image['TITLE'][0]['VALUE']) > 250) {
$image['TITLE'][0]['VALUE'] = trim(substr($image['TITLE'][0]['VALUE'], 0, 250)).'...';
}
$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>';
}
else if ($user['agg2_easyfeed_logo'] && $link) {
$edit->image = '<a href="'. $link .'" class="aggregator2_logo_link"><img src="'. $link .'" class="aggregator2_logo" alt="'. $title .'" /></a>';
}
$feed = node_validate($edit);
// Re-set user because Drupal sets it to anonymous (newly registered user is not logged in)
$feed->uid = $user['uid'];
$feed->name = $user['name'];
$errors = NULL;
if (!($errors = form_get_errors())) {
$nid = node_save($feed);
flush();
sleep(variable_get('aggregator2_sleep_interval', 3));
return $nid;
}
else {
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));
drupal_set_message(t('Failed to validate aggregator2-feed for %site: %error.', array('%site' => '<em>'. $feed->title .'</em>', '%error' => '<em>'. implode("\n", $errors) .'</em>')));
}
}
default:
break;
}
}
