diff --git a/functions.php b/functions.php index b1f68d1..ec49743 100755 --- a/functions.php +++ b/functions.php @@ -56,14 +56,12 @@ 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); if(!empty($response['Location'])) { $response2 = get_headers($response['Location'], 1); if(!empty($response2['Location'])) { - //die('too much redirection'); throw new Exception('too much redirection: '. escape ($url) ); } else { return $response['Location']; } @@ -89,7 +87,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 +98,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); @@ -122,7 +119,7 @@ function createAutoblog($type, $sitename, $siteurl, $rssurl, $error = array()) { $fp = fopen($foldername .'/index.php', 'w+'); if( !fwrite($fp, "") ) - $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 +132,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 ) { diff --git a/index.php b/index.php index 69a41c1..788fcbb 100755 --- a/index.php +++ b/index.php @@ -113,9 +113,11 @@ function create_from_opml($opml) { $sitename = escape( $outline['title'] ); $siteurl = escape($outline['htmlUrl']); - $sitetype = escape($outline['text']); if ( $sitetype == 'generic' or $sitetype == 'microblog' or $sitetype == 'shaarli') { } else { $sitetype = 'generic'; } + $sitetype = escape($outline['text']); + if ( $sitetype != 'microblog' && $sitetype != 'shaarli' && $sitetype != 'twitter' && $sitetype != 'identica' ) + $sitetype = 'generic'; - $error = array_merge( $error, createAutoblog( $sitetype, $sitename, $siteurl, $rssurl, $error ) ); + createAutoblog( $sitetype, $sitename, $siteurl, $rssurl ); if( empty ( $error )) $success[] = 'Autoblog "'. $sitename .'" crée avec succès. → afficher l\'autoblog.'; @@ -438,7 +440,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 .= ''; $form .= '

Autoblog '. $sitename .' ajouté avec succès.
'; @@ -500,16 +503,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 TwitterBridge).'; } 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 +538,19 @@ 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' ) { + try { $headers = get_headers($rssurl, 1); - if (strpos($headers[0], '200') == FALSE) { - $error[] = "Flux inaccessible (compte inexistant ?)"; + if (strpos($headers[0], '200') === FALSE) { + throw new Exception('Flux inaccessible (compte inexistant ?)'); } + + createAutoblog($sitetype, ucfirst($socialinstance) .' - '. $socialaccount, $siteurl, $rssurl); + $success[] = ''.ucfirst($socialinstance) .' - '. $socialaccount.' ajouté avec succès.'; } - if( empty($error) ) { - $error = array_merge( $error, createAutoblog($sitetype, ucfirst($socialinstance) .' - '. $socialaccount, $siteurl, $rssurl, $error)); - if( empty($error)) - $success[] = ''.ucfirst($socialinstance) .' - '. $socialaccount.' ajouté avec succès.'; + catch (Exception $e) { + echo $error[] = $e->getMessage(); } } } @@ -574,10 +578,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[] = 'Autoblog '. $sitename .' crée avec succès.afficher l\'autoblog'; + $success[] = 'Autoblog '. $sitename .' crée avec succès.afficher l\'autoblog'; } else { // checking procedure