Changeset 538

Show
Ignore:
Timestamp:
06/19/05 14:11:03 (3 years ago)
Author:
mbonetti
Message:

Fixes #19: more comprehensive error messages when adding rss feeds

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/rss/util.php

    r529 r538  
    814814 
    815815function add_channel($url, $folderid=0) { 
    816     assert("" != $url && strlen($url) > 7); 
    817     assert(is_numeric($folderid)); 
    818  
    819     $urlDB = htmlentities($url); 
    820  
    821     $res = rss_query("select count(*) as channel_exists from " .getTable("channels") ." where url='$urlDB'"); 
    822     list ($channel_exists) = rss_fetch_row($res); 
    823     if ($channel_exists > 0) { 
    824     return array(-1,"Looks like you are already subscribed to this channel"); 
    825     } 
    826  
    827     $res = rss_query("select 1+max(position) as np from " .getTable("channels")); 
    828     list($np) = rss_fetch_row($res); 
    829  
    830     if (!$np) { 
    831     $np = "0"; 
    832     } 
    833  
    834     // Here we go! 
    835     $old_level = error_reporting(E_ERROR); 
    836     $rss = fetch_rss( $url ); 
    837     error_reporting($old_level); 
    838      
    839     if ( $rss ) { 
    840  
    841     if (is_object($rss) && array_key_exists('title',$rss->channel)) { 
    842         $title= rss_real_escape_string ( $rss->channel['title'] ); 
     816    assert("" != $url && strlen($url) > 7); 
     817    assert(is_numeric($folderid)); 
     818     
     819    $urlDB = htmlentities($url); 
     820     
     821    $res = rss_query("select count(*) as channel_exists from " .getTable("channels") ." where url='$urlDB'"); 
     822    list ($channel_exists) = rss_fetch_row($res); 
     823    if ($channel_exists > 0) { 
     824        return array(-1,"Looks like you are already subscribed to this channel"); 
     825    } 
     826     
     827    $res = rss_query("select 1+max(position) as np from " .getTable("channels")); 
     828    list($np) = rss_fetch_row($res); 
     829     
     830    if (!$np) { 
     831        $np = "0"; 
     832    } 
     833     
     834    // Here we go! 
     835    $old_level = error_reporting(E_ERROR); 
     836    $rss = fetch_rss ( $url ); 
     837    error_reporting($old_level); 
     838 
     839    if ( $rss ) { 
     840         
     841        if (is_object($rss) && array_key_exists('title',$rss->channel)) { 
     842            $title= rss_real_escape_string ( $rss->channel['title'] ); 
     843        } else { 
     844            $title = ""; 
     845        } 
     846         
     847        if (is_object($rss) && array_key_exists('link',$rss->channel)) { 
     848            $siteurl= rss_real_escape_string (htmlentities($rss->channel['link'] )); 
     849        } else { 
     850            $siteurl = ""; 
     851        } 
     852         
     853        if (is_object($rss) && array_key_exists('description',$rss->channel)) { 
     854            $descr =  rss_real_escape_string ($rss->channel['description']); 
     855        } else { 
     856            $descr = ""; 
     857        } 
     858         
     859        //lets see if this server has a favicon 
     860        $icon = ""; 
     861        if (getConfig('rss.output.showfavicons')) { 
     862            // if we got nothing so far, lets try to fall back to 
     863            // favicons 
     864            if ($icon == "" && $siteurl  != "") { 
     865                $match = get_host($siteurl, $host); 
     866                $uri = "http://" . $host . "favicon.ico"; 
     867                if ($match && getContentType($uri, $contentType)) { 
     868                    if (preg_match("/image\/x-icon/", $contentType)) { 
     869                        $icon = $uri; 
     870                    } 
     871                } 
     872            } 
     873        } 
     874         
     875        if ($title != "") { 
     876             $sql = "insert into " .getTable("channels") ." (title, url, siteurl, parent, descr, dateadded, icon, position)" 
     877                ." values ('$title', '$urlDB', '$siteurl', $folderid, '$descr', now(), '$icon', $np)"; 
     878         
     879             rss_query($sql); 
     880             $newid = rss_insert_id(); 
     881             return array($newid,""); 
     882         
     883        } else { 
     884            return array (-1, "I'm sorry, I couldn't extract a valid RSS feed from <a href=\"$url\">$url</a>."); 
     885        } 
    843886    } else { 
    844         $title = ""; 
    845     } 
    846  
    847     if (is_object($rss) && array_key_exists('link',$rss->channel)) { 
    848         $siteurl= rss_real_escape_string (htmlentities($rss->channel['link'] )); 
    849     } else { 
    850         $siteurl = ""; 
    851     } 
    852  
    853     if (is_object($rss) && array_key_exists('description',$rss->channel)) { 
    854         $descr =  rss_real_escape_string ($rss->channel['description']); 
    855     } else { 
    856         $descr = ""; 
    857     } 
    858  
    859     //lets see if this server has a favicon 
    860     $icon = ""; 
    861     if (getConfig('rss.output.showfavicons')) { 
    862         // if we got nothing so far, lets try to fall back to 
    863         // favicons 
    864         if ($icon == "" && $siteurl  != "") { 
    865         $match = get_host($siteurl, $host); 
    866         $uri = "http://" . $host . "favicon.ico"; 
    867         //if ($match && (getHttpResponseCode($uri)))  { 
    868         if ($match && getContentType($uri, $contentType)) { 
    869             if (preg_match("/image\/x-icon/", $contentType)) { 
    870             $icon = $uri; 
    871             } 
    872         } 
    873         } 
    874     } 
    875  
    876     if ($title != "") { 
    877         $sql = "insert into " .getTable("channels") ." (title, url, siteurl, parent, descr, dateadded, icon, position)" 
    878           ." values ('$title', '$urlDB', '$siteurl', $folderid, '$descr', now(), '$icon', $np)"; 
    879  
    880         rss_query($sql); 
    881         $newid = rss_insert_id(); 
    882         return array($newid,""); 
    883  
    884     } else { 
    885         return array (-1, "I'm sorry, I couldn't extract a valid RSS feed from <a href=\"$url\">$url</a>."); 
    886     } 
    887     } else { 
    888     return array( -1, "I'm sorry, I couldn't retrieve <a href=\"$url\">$url</a>."); 
    889     } 
     887        global $MAGPIE_ERROR; 
     888        $retError = "I'm sorry, I couldn't retrieve <a href=\"$url\">$url</a>."; 
     889        if ($MAGPIE_ERROR) { 
     890            $retError .= "\n<br />$MAGPIE_ERROR\n"; 
     891        } 
     892        return array( -1, $retError); 
     893    } 
    890894} 
    891895