Fixes #23, handle errors, few bugfixes and optimization
This commit is contained in:
parent
cf237b0ab6
commit
ad27d9a02e
2 changed files with 83 additions and 50 deletions
|
@ -35,7 +35,9 @@ if( !defined('LOGO')) define( 'LOGO', 'icon-logo.svg' );
|
|||
if( !defined('HEAD_TITLE')) define( 'HEAD_TITLE', '');
|
||||
if( !defined('FOOTER')) define( 'FOOTER', 'D\'après les premières versions de <a href="http://sebsauvage.net">SebSauvage</a> et <a href="http://bohwaz.net/">Bohwaz</a>.');
|
||||
|
||||
// Functions
|
||||
/**
|
||||
* Functions
|
||||
**/
|
||||
function NoProtocolSiteURL($url) {
|
||||
$protocols = array("http://", "https://");
|
||||
$siteurlnoproto = str_replace($protocols, "", $url);
|
||||
|
@ -56,15 +58,17 @@ function NoProtocolSiteURL($url) {
|
|||
function DetectRedirect($url)
|
||||
{
|
||||
if(parse_url($url, PHP_URL_HOST)==FALSE) {
|
||||
//die('Not a URL');
|
||||
throw new Exception('Not a URL: '. escape ($url) );
|
||||
}
|
||||
$response = get_headers($url, 1);
|
||||
|
||||
try { $response = get_headers($url, 1); }
|
||||
catch (Exception $e) { throw new Exception('RSS URL unreachable: '. escape($url) ); }
|
||||
if(!empty($response['Location'])) {
|
||||
$response2 = get_headers($response['Location'], 1);
|
||||
try { $response2 = get_headers($response['Location'], 1); }
|
||||
catch (Exception $e) { throw new Exception('RSS URL unreachable: '. escape($url) ); }
|
||||
|
||||
if(!empty($response2['Location'])) {
|
||||
//die('too much redirection');
|
||||
throw new Exception('too much redirection: '. escape ($url) );
|
||||
throw new Exception('Too much redirection: '. escape ($url) );
|
||||
}
|
||||
else { return $response['Location']; }
|
||||
}
|
||||
|
@ -89,7 +93,7 @@ function escape($str) {
|
|||
return htmlspecialchars($str, ENT_COMPAT, 'UTF-8', false);
|
||||
}
|
||||
|
||||
function createAutoblog($type, $sitename, $siteurl, $rssurl, $error = array()) {
|
||||
function createAutoblog($type, $sitename, $siteurl, $rssurl) {
|
||||
if( $type == 'generic' || empty( $type )) {
|
||||
$var = updateType( $siteurl );
|
||||
$type = $var['type'];
|
||||
|
@ -100,8 +104,7 @@ function createAutoblog($type, $sitename, $siteurl, $rssurl, $error = array()) {
|
|||
}
|
||||
|
||||
if(folderExists($siteurl)) {
|
||||
$error[] = 'Erreur : l\'autoblog '. $sitename .' existe déjà.';
|
||||
return $error;
|
||||
throw new Exception('Erreur : l\'autoblog '. $sitename .' existe déjà.');
|
||||
}
|
||||
|
||||
$foldername = AUTOBLOGS_FOLDER . urlToFolderSlash($siteurl);
|
||||
|
@ -110,7 +113,7 @@ function createAutoblog($type, $sitename, $siteurl, $rssurl, $error = array()) {
|
|||
|
||||
/**
|
||||
* RSS
|
||||
**/
|
||||
*
|
||||
try { // à déplacer après la tentative de création de l'autoblog crée avec succès ?
|
||||
require_once('class_rssfeed.php');
|
||||
$rss = new AutoblogRSS(RSS_FILE);
|
||||
|
@ -118,11 +121,11 @@ function createAutoblog($type, $sitename, $siteurl, $rssurl, $error = array()) {
|
|||
}
|
||||
catch (Exception $e) {
|
||||
;
|
||||
}
|
||||
}*/
|
||||
|
||||
$fp = fopen($foldername .'/index.php', 'w+');
|
||||
if( !fwrite($fp, "<?php require_once '../autoblog.php'; ?>") )
|
||||
$error[] = "Impossible d'écrire le fichier index.php";
|
||||
throw new Exception('Impossible d\'écrire le fichier index.php');
|
||||
fclose($fp);
|
||||
|
||||
$fp = fopen($foldername .'/vvb.ini', 'w+');
|
||||
|
@ -135,13 +138,14 @@ FEED_URL="'. $rssurl .'"
|
|||
ARTICLES_PER_PAGE="'. getArticlesPerPage( $type ) .'"
|
||||
UPDATE_INTERVAL="'. getInterval( $type ) .'"
|
||||
UPDATE_TIMEOUT="'. getTimeout( $type ) .'"') )
|
||||
$error[] = "Impossible d'écrire le fichier vvb.ini";
|
||||
throw new Exception('Impossible d\'écrire le fichier vvb.ini');
|
||||
fclose($fp);
|
||||
}
|
||||
else
|
||||
$error[] = "Impossible de créer le répertoire.";
|
||||
updateXML('new_autoblog_added', 'new', $foldername, $sitename, $siteurl, $rssurl); /* éventuellement une conditionnelle ici: if(empty($error)) ? */
|
||||
return $error;
|
||||
throw new Exception('Impossible de créer le répertoire.');
|
||||
|
||||
/* @Mitsu: Il faudrait remonter les erreurs d'I/O */
|
||||
updateXML('new_autoblog_added', 'new', $foldername, $sitename, $siteurl, $rssurl);
|
||||
}
|
||||
|
||||
function getArticlesPerPage( $type ) {
|
||||
|
|
97
index.php
97
index.php
|
@ -36,6 +36,21 @@ if(file_exists("functions.php")){
|
|||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the data from a URL
|
||||
* http://davidwalsh.name/curl-download
|
||||
**/
|
||||
function get_data($url) {
|
||||
$ch = curl_init();
|
||||
$timeout = 5;
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
||||
$data = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return $data;
|
||||
}
|
||||
|
||||
function get_title_from_feed($url) {
|
||||
return get_title_from_datafeed(file_get_contents($url));
|
||||
}
|
||||
|
@ -105,19 +120,28 @@ function check_antibot($number, $text_number) {
|
|||
|
||||
function create_from_opml($opml) {
|
||||
global $error, $success;
|
||||
|
||||
$cpt = 0;
|
||||
foreach( $opml->body->outline as $outline ) {
|
||||
if ( !empty( $outline['title'] ) && !empty( $outline['text'] ) && !empty( $outline['xmlUrl']) && !empty( $outline['htmlUrl'] )) {
|
||||
try {
|
||||
$rssurl = DetectRedirect(escape( $outline['xmlUrl']));
|
||||
|
||||
$sitename = escape( $outline['title'] );
|
||||
$siteurl = escape($outline['htmlUrl']);
|
||||
$sitetype = escape($outline['text']); if ( $sitetype == 'generic' or $sitetype == 'microblog' or $sitetype == 'shaarli') { } else { $sitetype = 'generic'; }
|
||||
|
||||
// Lighten process by checking folderExists first
|
||||
// A CHANGER SELON ISSUE #20
|
||||
if(folderExists($siteurl))
|
||||
throw new Exception('Erreur : l\'autoblog '. $sitename .' existe déjà.');
|
||||
|
||||
$sitetype = escape($outline['text']);
|
||||
if ( $sitetype != 'microblog' && $sitetype != 'shaarli' && $sitetype != 'twitter' && $sitetype != 'identica' )
|
||||
$sitetype = 'generic';
|
||||
|
||||
$rssurl = DetectRedirect(escape($outline['xmlUrl']));
|
||||
|
||||
$error = array_merge( $error, createAutoblog( $sitetype, $sitename, $siteurl, $rssurl, $error ) );
|
||||
|
||||
if( empty ( $error ))
|
||||
createAutoblog( $sitetype, $sitename, $siteurl, $rssurl );
|
||||
|
||||
// Do not print iframe on big import (=> heavy and useless)
|
||||
if( ++$cpt < 10 )
|
||||
$success[] = '<iframe width="1" height="1" frameborder="0" src="'. AUTOBLOGS_FOLDER . urlToFolderSlash( $siteurl ) .'/index.php"></iframe>Autoblog "'. $sitename .'" crée avec succès. → <a target="_blank" href="'. AUTOBLOGS_FOLDER . urlToFolderSlash( $siteurl ) .'">afficher l\'autoblog</a>.';
|
||||
}
|
||||
catch (Exception $e) {
|
||||
|
@ -438,7 +462,8 @@ if(!empty($_GET['via_button']) && $_GET['number'] === '17' && ALLOW_NEW_AUTOBLOG
|
|||
$sitetype = updateType($siteurl); // Disabled input doesn't send POST data
|
||||
$sitetype = $sitetype['type'];
|
||||
|
||||
$error = array_merge( $error, createAutoblog($sitetype, $sitename, $siteurl, $rssurl, $error));
|
||||
createAutoblog( $sitetype, $sitename, $siteurl, $rssurl );
|
||||
|
||||
if( empty($error)) {
|
||||
$form .= '<iframe width="1" height="1" frameborder="0" src="'. AUTOBLOGS_FOLDER . urlToFolderSlash($siteurl) .'/index.php"></iframe>';
|
||||
$form .= '<p><span style="color:darkgreen">Autoblog <a href="'. AUTOBLOGS_FOLDER . urlToFolderSlash($siteurl) .'">'. $sitename .'</a> ajouté avec succès.</span><br>';
|
||||
|
@ -500,16 +525,16 @@ if(!empty($_POST['socialaccount']) && !empty($_POST['socialinstance']) && ALLOW_
|
|||
if($socialinstance === 'twitter') {
|
||||
if( API_TWITTER !== FALSE ) {
|
||||
$sitetype = 'twitter';
|
||||
$siteurl = "http://twitter.com/$socialaccount";
|
||||
$siteurl = 'http://twitter.com/$socialaccount';
|
||||
$rssurl = API_TWITTER.$socialaccount;
|
||||
}
|
||||
else
|
||||
$error[] = "Twitter veut mettre à mort son API ouverte. Du coup on peut plus faire ça comme ça.";
|
||||
$error[] = 'Vous devez définir une API Twitter -> RSS dans votre fichier de configuration (see <a href="https://github.com/mitsukarenai/twitterbridge">TwitterBridge</a>).';
|
||||
}
|
||||
elseif($socialinstance === 'identica') {
|
||||
$sitetype = 'identica';
|
||||
$siteurl = "http://identi.ca/$socialaccount";
|
||||
$rssurl = "http://identi.ca/api/statuses/user_timeline/$socialaccount.rss";
|
||||
$siteurl = 'http://identi.ca/$socialaccount';
|
||||
$rssurl = 'http://identi.ca/api/statuses/user_timeline/$socialaccount.rss';
|
||||
}
|
||||
elseif($socialinstance === 'statusnet' && !empty($_POST['statusneturl'])) {
|
||||
$sitetype = 'microblog';
|
||||
|
@ -535,18 +560,22 @@ if(!empty($_POST['socialaccount']) && !empty($_POST['socialinstance']) && ALLOW_
|
|||
$socialaccount = get_title_from_feed($rssurl);
|
||||
}
|
||||
|
||||
|
||||
if( empty($error) ) {
|
||||
// Twitterbridge do NOT allow this user yet => No check
|
||||
if( $sitetype != 'twitter' ) {
|
||||
$headers = get_headers($rssurl, 1);
|
||||
if (strpos($headers[0], '200') == FALSE) {
|
||||
$error[] = "Flux inaccessible (compte inexistant ?)";
|
||||
try {
|
||||
// TwitterBridge user will be allowed after Autoblog creation
|
||||
// TODO: Twitter user does not exist ?
|
||||
if($sitetype != 'twitter') {
|
||||
$headers = get_headers($rssurl, 1);
|
||||
if (strpos($headers[0], '200') === FALSE)
|
||||
throw new Exception('Flux inaccessible (compte inexistant ?)');
|
||||
}
|
||||
|
||||
createAutoblog($sitetype, ucfirst($socialinstance) .' - '. $socialaccount, $siteurl, $rssurl);
|
||||
$success[] = '<iframe width="1" height="1" frameborder="0" src="'. AUTOBLOGS_FOLDER . urlToFolderSlash( $siteurl ) .'/index.php"></iframe><b style="color:darkgreen">'.ucfirst($socialinstance) .' - '. $socialaccount.' <a href="'. AUTOBLOGS_FOLDER .urlToFolderSlash( $siteurl ).'">ajouté avec succès</a>.</b>';
|
||||
}
|
||||
if( empty($error) ) {
|
||||
$error = array_merge( $error, createAutoblog($sitetype, ucfirst($socialinstance) .' - '. $socialaccount, $siteurl, $rssurl, $error));
|
||||
if( empty($error))
|
||||
$success[] = '<iframe width="1" height="1" frameborder="0" src="'. AUTOBLOGS_FOLDER . urlToFolderSlash( $siteurl ) .'/index.php"></iframe><b style="color:darkgreen">'.ucfirst($socialinstance) .' - '. $socialaccount.' <a href="'. AUTOBLOGS_FOLDER .urlToFolderSlash( $siteurl ).'">ajouté avec succès</a>.</b>';
|
||||
catch (Exception $e) {
|
||||
echo $error[] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -574,10 +603,9 @@ if( !empty($_POST['generic']) && ALLOW_NEW_AUTOBLOGS && ALLOW_NEW_AUTOBLOGS_BY_L
|
|||
$siteurl = escape($_POST['siteurl']);
|
||||
$sitename = get_title_from_feed($rssurl);
|
||||
|
||||
$error = array_merge( $error, createAutoblog('generic', $sitename, $siteurl, $rssurl, $error));
|
||||
createAutoblog('generic', $sitename, $siteurl, $rssurl);
|
||||
|
||||
if( empty($error))
|
||||
$success[] = '<iframe width="1" height="1" frameborder="0" src="'. AUTOBLOGS_FOLDER . urlToFolderSlash( $siteurl ) .'/index.php"></iframe><b style="color:darkgreen">Autoblog '. $sitename .' crée avec succès.</b> → <a target="_blank" href="'. AUTOBLOGS_FOLDER . urlToFolderSlash( $siteurl ) .'">afficher l\'autoblog</a>';
|
||||
$success[] = '<iframe width="1" height="1" frameborder="0" src="'. AUTOBLOGS_FOLDER . urlToFolderSlash( $siteurl ) .'/index.php"></iframe><b style="color:darkgreen">Autoblog '. $sitename .' crée avec succès.</b> → <a target="_blank" href="'. AUTOBLOGS_FOLDER . urlToFolderSlash( $siteurl ) .'">afficher l\'autoblog</a>';
|
||||
}
|
||||
else {
|
||||
// checking procedure
|
||||
|
@ -647,7 +675,7 @@ if( !empty($_POST['opml_file']) && ALLOW_NEW_AUTOBLOGS && ALLOW_NEW_AUTOBLOGS_BY
|
|||
if(parse_url($opml_url, PHP_URL_HOST)==FALSE) {
|
||||
$error[] = "URL du fichier OPML non valide.";
|
||||
} else {
|
||||
if ( ($opml = simplexml_load_file( $opml_url )) !== false ) {
|
||||
if ( ($opml = simplexml_load_string( get_data($opml_url) )) !== false ) {
|
||||
create_from_opml($opml);
|
||||
} else {
|
||||
$error[] = "Impossible de lire le contenu du fichier OPML ou d'accéder à l'URL donnée.";
|
||||
|
@ -678,10 +706,10 @@ if( !empty($_POST['opml_file']) && ALLOW_NEW_AUTOBLOGS && ALLOW_NEW_AUTOBLOGS_BY
|
|||
</a></h1>
|
||||
|
||||
<div class="pbloc">
|
||||
<?php
|
||||
if (defined('LOGO'))
|
||||
echo '<img id="logo" src="'. RESOURCES_FOLDER . LOGO .'" alt="">';
|
||||
?>
|
||||
<?php
|
||||
if (defined('LOGO'))
|
||||
echo '<img id="logo" src="'. RESOURCES_FOLDER . LOGO .'" alt="">';
|
||||
?>
|
||||
<h2>Présentation</h2>
|
||||
|
||||
<p>
|
||||
|
@ -694,6 +722,11 @@ if( !empty($_POST['opml_file']) && ALLOW_NEW_AUTOBLOGS && ALLOW_NEW_AUTOBLOGS_BY
|
|||
Voici une liste d'autoblogs hébergés sur <i><?php echo $_SERVER['SERVER_NAME']; ?></i>
|
||||
(<a href="http://sebsauvage.net/streisand.me/fr/">plus d'infos sur le projet</a>).
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Autres fermes</b>
|
||||
→ <a href="https://duckduckgo.com/?q=!g%20%22Voici%20une%20liste%20d'autoblogs%20hébergés%22">Rechercher</a>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<?php if( $update_available ) { ?>
|
||||
|
@ -849,11 +882,7 @@ if( !empty($_POST['opml_file']) && ALLOW_NEW_AUTOBLOGS && ALLOW_NEW_AUTOBLOGS_BY
|
|||
|
||||
<div class="pbloc">
|
||||
<h2>Autoblogs hébergés <a href="?rss" title="RSS des changements"><img src="<?php echo RESOURCES_FOLDER; ?>rss.png" alt="rss"/></a></h2>
|
||||
<p>
|
||||
<b>Autres fermes</b>
|
||||
→ <a href="https://duckduckgo.com/?q=!g%20%22Voici%20une%20liste%20d'autoblogs%20hébergés%22">Rechercher</a>
|
||||
</p>
|
||||
|
||||
|
||||
<div class="clear"><a href="?sitemap">sitemap</a> | <a href="?export">export<sup> JSON</sup></a> | <a href="?exportopml">export<sup> OPML</sup></a></div>
|
||||
<div id="contentVignette">
|
||||
<?php
|
||||
|
|
Loading…
Reference in a new issue