2013-02-18 19:42:10 +01:00
< ? php
2013-03-23 15:07:02 +01:00
2013-04-11 18:27:25 +02:00
/**
* DO NOT EDIT THESE LINES
* You can override these options by setting them in config . php
**/
2013-04-10 13:08:15 +02:00
if ( ! defined ( 'ROOT_DIR' ))
{
define ( 'ROOT_DIR' , dirname ( $_SERVER [ 'SCRIPT_FILENAME' ]));
}
define ( 'LOCAL_URI' , '' );
if ( ! defined ( 'AUTOBLOGS_FOLDER' )) define ( 'AUTOBLOGS_FOLDER' , './autoblogs/' );
if ( ! defined ( 'DOC_FOLDER' )) define ( 'DOC_FOLDER' , './docs/' );
if ( ! defined ( 'RESOURCES_FOLDER' )) define ( 'RESOURCES_FOLDER' , './resources/' );
2013-04-29 22:06:31 +02:00
if ( ! defined ( 'FOLDER_MAX_LENGTH' )) define ( 'FOLDER_MAX_LENGTH' , 80 );
2013-04-10 13:08:15 +02:00
date_default_timezone_set ( 'Europe/Paris' );
setlocale ( LC_TIME , 'fr_FR.UTF-8' , 'fr_FR' , 'fr' );
2013-02-18 19:42:10 +01:00
2013-04-11 18:27:25 +02:00
if ( ! defined ( 'ALLOW_FULL_UPDATE' )) define ( 'ALLOW_FULL_UPDATE' , TRUE );
if ( ! defined ( 'ALLOW_CHECK_UPDATE' )) define ( 'ALLOW_CHECK_UPDATE' , TRUE );
// If you set ALLOW_NEW_AUTOBLOGS to FALSE, the following options do not matter.
if ( ! defined ( 'ALLOW_NEW_AUTOBLOGS' )) define ( 'ALLOW_NEW_AUTOBLOGS' , TRUE );
if ( ! defined ( 'ALLOW_NEW_AUTOBLOGS_BY_LINKS' )) define ( 'ALLOW_NEW_AUTOBLOGS_BY_LINKS' , TRUE );
if ( ! defined ( 'ALLOW_NEW_AUTOBLOGS_BY_SOCIAL' )) define ( 'ALLOW_NEW_AUTOBLOGS_BY_SOCIAL' , TRUE );
if ( ! defined ( 'ALLOW_NEW_AUTOBLOGS_BY_BUTTON' )) define ( 'ALLOW_NEW_AUTOBLOGS_BY_BUTTON' , TRUE );
if ( ! defined ( 'ALLOW_NEW_AUTOBLOGS_BY_OPML_FILE' )) define ( 'ALLOW_NEW_AUTOBLOGS_BY_OPML_FILE' , TRUE );
if ( ! defined ( 'ALLOW_NEW_AUTOBLOGS_BY_OPML_LINK' )) define ( 'ALLOW_NEW_AUTOBLOGS_BY_OPML_LINK' , TRUE );
if ( ! defined ( 'ALLOW_NEW_AUTOBLOGS_BY_XSAF' )) define ( 'ALLOW_NEW_AUTOBLOGS_BY_XSAF' , TRUE );
// More about TwitterBridge : https://github.com/mitsukarenai/twitterbridge
if ( ! defined ( 'API_TWITTER' )) define ( 'API_TWITTER' , FALSE );
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>.' );
2013-04-22 15:57:24 +02:00
/**
* Functions
**/
2013-02-18 19:42:10 +01:00
function NoProtocolSiteURL ( $url ) {
2013-02-23 20:18:22 +01:00
$protocols = array ( " http:// " , " https:// " );
$siteurlnoproto = str_replace ( $protocols , " " , $url );
2013-02-18 19:42:10 +01:00
// Remove the / at the end of string
if ( $siteurlnoproto [ strlen ( $siteurlnoproto ) - 1 ] == '/' )
$siteurlnoproto = substr ( $siteurlnoproto , 0 , - 1 );
2013-02-23 20:18:22 +01:00
// Remove index.php/html at the end of string
if ( strpos ( $url , 'index.php' ) || strpos ( $url , 'index.html' ) ) {
$siteurlnoproto = preg_replace ( '#(.*)/index\.(html|php)$#' , '$1' , $siteurlnoproto );
}
2013-02-18 19:42:10 +01:00
return $siteurlnoproto ;
}
function DetectRedirect ( $url )
{
2013-03-04 21:40:43 +01:00
if ( parse_url ( $url , PHP_URL_HOST ) == FALSE ) {
2013-04-04 18:50:02 +02:00
throw new Exception ( 'Not a URL: ' . escape ( $url ) );
2013-03-04 21:40:43 +01:00
}
2013-04-30 12:04:03 +02:00
2013-04-22 15:57:24 +02:00
try { $response = get_headers ( $url , 1 ); }
2013-04-30 12:04:03 +02:00
catch ( Exception $e ) { throw new Exception ( 'RSS URL unreachable: ' . escape ( $url ) ); }
2013-02-18 19:42:10 +01:00
if ( ! empty ( $response [ 'Location' ])) {
2013-04-22 15:57:24 +02:00
try { $response2 = get_headers ( $response [ 'Location' ], 1 ); }
2013-04-30 12:04:03 +02:00
catch ( Exception $e ) { throw new Exception ( 'RSS URL unreachable: ' . escape ( $url ) ); }
2013-02-18 19:42:10 +01:00
if ( ! empty ( $response2 [ 'Location' ])) {
2013-04-22 15:57:24 +02:00
throw new Exception ( 'Too much redirection: ' . escape ( $url ) );
2013-02-18 19:42:10 +01:00
}
else { return $response [ 'Location' ]; }
}
else {
return $url ;
}
}
2013-04-29 22:06:31 +02:00
function urlHash ( $rssurl ) {
return sha1 ( NoProtocolSiteURL ( $rssurl ));
2013-02-18 19:42:10 +01:00
}
2013-04-29 22:06:31 +02:00
function urlToFolder ( $siteurl , $rssurl ) {
return AUTOBLOGS_FOLDER . substr ( preg_replace ( " /[^a-z0-9]/ " , '' , strtolower ( NoProtocolSiteURL ( $siteurl ))), 0 , FOLDER_MAX_LENGTH ) . '_' . urlHash ( $rssurl ) . '/' ;
2013-02-20 20:14:12 +01:00
}
2013-04-29 22:06:31 +02:00
function folderExists ( $siteurl , $rssurl ) {
return file_exists ( urlToFolder ( $siteurl , $rssurl ));
2013-02-23 20:18:22 +01:00
}
2013-02-18 19:42:10 +01:00
function escape ( $str ) {
return htmlspecialchars ( $str , ENT_COMPAT , 'UTF-8' , false );
}
2013-04-09 15:50:00 +02:00
function getSiteDesc ( $url ){
$allMeta = get_meta_tags ( $url );
if ( ! empty ( $allMeta [ 'description' ])){
return $allMeta [ 'description' ];
} else {
return 'Pas de description trouvé' ;
}
}
function shortSiteDesc ( $desc , $length = 145 ){
$charset = 'UTF-8' ;
if ( mb_strlen ( $desc , $charset ) > $length ) {
$desc = mb_substr ( $desc , 0 , $length , $charset ) . '...' ;
}
return $desc ;
}
function createAutoblog ( $type , $sitename , $siteurl , $rssurl , $siteDesc , $error = array ()) {
2013-02-19 12:20:13 +01:00
if ( $type == 'generic' || empty ( $type )) {
2013-02-18 19:42:10 +01:00
$var = updateType ( $siteurl );
$type = $var [ 'type' ];
2013-03-01 12:14:46 +01:00
if ( ! empty ( $var [ 'name' ]) ) {
if ( ! stripos ( $siteurl , $var [ 'name' ] === false ) )
$sitename = ucfirst ( $var [ 'name' ]) . ' - ' . $sitename ;
}
2013-02-18 19:42:10 +01:00
}
2013-04-09 15:50:00 +02:00
2013-04-30 12:04:03 +02:00
if ( folderExists ( $siteurl , $rssurl )) {
2013-04-22 15:57:24 +02:00
throw new Exception ( 'Erreur : l\'autoblog ' . $sitename . ' existe déjà.' );
2013-02-18 19:42:10 +01:00
}
2013-02-20 20:14:12 +01:00
2013-04-30 12:04:03 +02:00
$foldername = urlToFolder ( $siteurl , $rssurl );
2013-04-08 14:05:35 +02:00
if ( mkdir ( $foldername , 0755 , false ) ) {
2013-04-09 15:50:00 +02:00
2013-04-08 14:05:35 +02:00
$fp = fopen ( $foldername . '/index.php' , 'w+' );
2013-04-08 13:59:49 +02:00
if ( ! fwrite ( $fp , " <?php require_once '../autoblog.php'; ?> " ) )
2013-04-22 15:57:24 +02:00
throw new Exception ( 'Impossible d\'écrire le fichier index.php' );
2013-02-18 19:42:10 +01:00
fclose ( $fp );
2013-04-08 14:05:35 +02:00
$fp = fopen ( $foldername . '/vvb.ini' , 'w+' );
2013-02-18 19:42:10 +01:00
if ( ! fwrite ( $fp , ' [ VroumVroumBlogConfig ]
SITE_TYPE = " '. $type .' "
SITE_TITLE = " '. $sitename .' "
SITE_DESCRIPTION = " Site original : <a href= \ ''. $siteurl .' \ '>'. $sitename .'</a> "
2013-04-09 15:50:00 +02:00
SITE_META_DESCRIPTION = " '. $siteDesc .' "
2013-02-18 19:42:10 +01:00
SITE_URL = " '. $siteurl .' "
FEED_URL = " '. $rssurl .' "
2013-02-23 15:27:29 +01:00
ARTICLES_PER_PAGE = " '. getArticlesPerPage( $type ) .' "
2013-02-18 19:42:10 +01:00
UPDATE_INTERVAL = " '. getInterval( $type ) .' "
2013-02-23 15:27:29 +01:00
UPDATE_TIMEOUT = " '. getTimeout( $type ) .' " ' ) )
2013-04-22 15:57:24 +02:00
throw new Exception ( 'Impossible d\'écrire le fichier vvb.ini' );
2013-02-18 19:42:10 +01:00
fclose ( $fp );
}
else
2013-04-22 15:57:24 +02:00
throw new Exception ( 'Impossible de créer le répertoire.' );
2013-02-18 19:42:10 +01:00
2013-04-29 22:06:31 +02:00
updateXML ( 'new_autoblog_added' , 'new' , $foldername , $sitename , $siteurl , $rssurl );
2013-02-18 19:42:10 +01:00
}
2013-02-23 15:27:29 +01:00
function getArticlesPerPage ( $type ) {
switch ( $type ) {
case 'microblog' :
return 20 ;
case 'shaarli' :
return 20 ;
default :
return 5 ;
}
}
2013-02-18 19:42:10 +01:00
function getInterval ( $type ) {
switch ( $type ) {
case 'microblog' :
return 300 ;
case 'shaarli' :
return 1800 ;
default :
return 3600 ;
}
}
2013-02-23 15:27:29 +01:00
function getTimeout ( $type ) {
switch ( $type ) {
case 'microblog' :
return 30 ;
case 'shaarli' :
return 30 ;
default :
return 30 ;
}
}
2013-02-18 19:42:10 +01:00
function updateType ( $siteurl ) {
if ( strpos ( $siteurl , 'twitter.com' ) !== FALSE ) {
2013-03-14 18:25:10 +01:00
return array ( 'type' => 'twitter' , 'name' => 'twitter' );
2013-02-18 19:42:10 +01:00
}
elseif ( strpos ( $siteurl , 'identi.ca' ) !== FALSE ) {
2013-03-14 18:25:10 +01:00
return array ( 'type' => 'identica' , 'name' => 'identica' );
2013-02-18 19:42:10 +01:00
}
2013-04-09 15:50:00 +02:00
elseif ( strpos ( $siteurl , 'shaarli' ) !== FALSE ) {
2013-02-18 19:42:10 +01:00
return array ( 'type' => 'shaarli' , 'name' => 'shaarli' );
}
else
return array ( 'type' => 'generic' , 'name' => '' );
}
2013-02-23 20:18:22 +01:00
function debug ( $data )
{
2013-03-12 18:17:16 +01:00
echo '<pre>' ;
var_dump ( $data );
echo '</pre>' ;
2013-02-23 20:18:22 +01:00
}
2013-03-18 12:26:01 +01:00
2013-04-09 15:50:00 +02:00
/**
* Améliore la sortie print
*
* @ author Tatane http :// www . tatane . info / index . php / print_rn
* @ author http :// www . blog . cactuscrew . com / 77 - print_rn . html
* @ param $data ( array ) tableau à examiner
* @ param $name ( string ) nom a affiché
* @ return false affiche les clef valeur du tableau $data
*/
function n_print ( $data , $name = '' ) {
$aBackTrace = debug_backtrace ();
echo '<h2>' , $name , '</h2>' ;
echo '<fieldset style="border: 1px solid orange; padding: 5px;color: #333; background-color: #fff;">' ;
echo '<legend style="border:1px solid orange;padding: 1px;background-color:#eee;color:orange;">' , basename ( $aBackTrace [ 0 ][ 'file' ]), ' ligne => ' , $aBackTrace [ 0 ][ 'line' ], '</legend>' ;
echo '<pre>' , htmlentities ( print_r ( $data , 1 )), '</pre>' ;
echo '</fieldset><br />' ;
}
2013-03-18 12:26:01 +01:00
function __ ( $str )
{
switch ( $str )
{
case 'Search' :
return 'Recherche' ;
case 'Update' :
return 'Mise à jour' ;
case 'Updating database... Please wait.' :
return 'Mise à jour de la base de données, veuillez patienter...' ;
case '<b>%d</b> results for <i>%s</i>' :
return '<b>%d</b> résultats pour la recherche <i>%s</i>' ;
case 'Not Found' :
return 'Introuvable' ;
case 'Article not found.' :
return 'Cet article n\'a pas été trouvé.' ;
case 'Older' :
return 'Plus anciens' ;
case 'Newer' :
return 'Plus récents' ;
2013-04-07 13:04:19 +02:00
case 'ATOM Feed' :
return 'Flux ATOM' ;
2013-03-18 12:26:01 +01:00
case 'Update complete!' :
return 'Mise à jour terminée !' ;
case 'Click here to reload this webpage.' :
return 'Cliquez ici pour recharger cette page.' ;
case 'Source:' :
return 'Source :' ;
case '_date_format' :
return '%A %e %B %Y à %H:%M' ;
case 'configuration' :
case 'articles' :
return $str ;
case 'Media export' :
return 'Export fichiers media' ;
default :
return $str ;
}
}
2013-04-22 12:00:26 +02:00
function updateXML ( $status , $response_code , $autoblog_url , $autoblog_title , $autoblog_sourceurl , $autoblog_sourcefeed )
{
$json = json_decode ( file_get_contents ( RESOURCES_FOLDER . 'rss.json' ), true );
$json [] = array (
'timestamp' => time (),
'autoblog_url' => $autoblog_url ,
'autoblog_title' => $autoblog_title ,
'autoblog_sourceurl' => $autoblog_sourceurl ,
'autoblog_sourcefeed' => $autoblog_sourcefeed ,
'status' => $status ,
'response_code' => $response_code
);
2013-04-25 11:34:34 +02:00
if ( file_put_contents ( RESOURCES_FOLDER . 'rss.json' , json_encode ( $json ), LOCK_EX ) === FALSE )
{ return FALSE ; }
else { return TRUE ; }
2013-04-22 12:00:26 +02:00
}
2013-04-29 22:32:03 +02:00
function displayXMLstatus ( $status , $response_code , $autoblog_url , $autoblog_title , $autoblog_sourceurl , $autoblog_sourcefeed ) {
2013-04-22 12:00:26 +02:00
switch ( $status )
{
case 'unavailable' :
return 'Autoblog "' . $autoblog_title . '": site distant inaccessible (code ' . $response_code . ')<br>Autoblog: <a href="' . serverUrl ( false ) . AUTOBLOGS_FOLDER . $autoblog_url . '">' . $autoblog_title . '</a><br>Site: <a href="' . $autoblog_sourceurl . '">' . $autoblog_sourceurl . '</a><br>RSS: <a href="' . $autoblog_sourcefeed . '">' . $autoblog_sourcefeed . '</a>' ;
case 'moved' :
return 'Autoblog "' . $autoblog_title . '": site distant redirigé (code ' . $response_code . ')<br>Autoblog: <a href="' . serverUrl ( false ) . AUTOBLOGS_FOLDER . $autoblog_url . '">' . $autoblog_title . '</a><br>Site: <a href="' . $autoblog_sourceurl . '">' . $autoblog_sourceurl . '</a><br>RSS: <a href="' . $autoblog_sourcefeed . '">' . $autoblog_sourcefeed . '</a>' ;
case 'not_found' :
return 'Autoblog "' . $autoblog_title . '": site distant introuvable (code ' . $response_code . ')<br>Autoblog: <a href="' . serverUrl ( false ) . AUTOBLOGS_FOLDER . $autoblog_url . '">' . $autoblog_title . '</a><br>Site: <a href="' . $autoblog_sourceurl . '">' . $autoblog_sourceurl . '</a><br>RSS: <a href="' . $autoblog_sourcefeed . '">' . $autoblog_sourcefeed . '</a>' ;
case 'remote_error' :
return 'Autoblog "' . $autoblog_title . '": site distant a problème serveur (code ' . $response_code . ')<br>Autoblog: <a href="' . serverUrl ( false ) . AUTOBLOGS_FOLDER . $autoblog_url . '">' . $autoblog_title . '</a><br>Site: <a href="' . $autoblog_sourceurl . '">' . $autoblog_sourceurl . '</a><br>RSS: <a href="' . $autoblog_sourcefeed . '">' . $autoblog_sourcefeed . '</a>' ;
case 'available' :
return 'Autoblog "' . $autoblog_title . '": site distant à nouveau opérationnel (code ' . $response_code . ')<br>Autoblog: <a href="' . serverUrl ( false ) . AUTOBLOGS_FOLDER . $autoblog_url . '">' . $autoblog_title . '</a><br>Site: <a href="' . $autoblog_sourceurl . '">' . $autoblog_sourceurl . '</a><br>RSS: <a href="' . $autoblog_sourcefeed . '">' . $autoblog_sourcefeed . '</a>' ;
case 'new_autoblog_added' :
return 'Autoblog "' . $autoblog_title . '" ajouté (code ' . $response_code . ')<br>Autoblog: <a href="' . serverUrl ( false ) . AUTOBLOGS_FOLDER . $autoblog_url . '">' . $autoblog_title . '</a><br>Site: <a href="' . $autoblog_sourceurl . '">' . $autoblog_sourceurl . '</a><br>RSS: <a href="' . $autoblog_sourcefeed . '">' . $autoblog_sourcefeed . '</a>' ;
}
}
2013-04-29 22:32:03 +02:00
function displayXML () {
2013-04-22 12:00:26 +02:00
header ( 'Content-type: application/rss+xml; charset=utf-8' );
echo ' < ? xml version = " 1.0 " encoding = " UTF-8 " ?>
< rss version = " 2.0 " xmlns : atom = " http://www.w3.org/2005/Atom " >< channel >< link > '.serverUrl(true).' </ link > ' ;
2013-04-29 22:32:03 +02:00
echo '<atom:link href="' . serverUrl ( false ) . '/?rss" rel="self" type="application/rss+xml"/><title>Projet Autoblog' . (( strlen ( HEAD_TITLE ) > 0 ) ? ' | ' . HEAD_TITLE : '' ) . '</title><description>' . serverUrl ( true ), " Projet Autoblog - RSS : Ajouts et changements de disponibilité. " . '</description>' ;
2013-04-22 12:00:26 +02:00
if ( file_exists ( RESOURCES_FOLDER . 'rss.json' ))
{
$json = json_decode ( file_get_contents ( RESOURCES_FOLDER . 'rss.json' ), true );
2013-04-30 00:22:40 +02:00
rsort ( $json );
2013-04-22 12:00:26 +02:00
foreach ( $json as $item )
{
2013-04-29 22:32:03 +02:00
$description = displayXMLstatus ( $item [ 'status' ], $item [ 'response_code' ], $item [ 'autoblog_url' ], $item [ 'autoblog_title' ], $item [ 'autoblog_sourceurl' ], $item [ 'autoblog_sourcefeed' ]);
2013-04-22 12:00:26 +02:00
$link = serverUrl ( true ) . AUTOBLOGS_FOLDER . $item [ 'autoblog_url' ];
$date = date ( " r " , $item [ 'timestamp' ]);
print <<< EOT
< item >
< title > { $item [ 'autoblog_title' ]} </ title >
< description ><! [ CDATA [{ $description }]] ></ description >
< link > { $link } </ link >
< guid isPermaLink = " false " > { $item [ 'timestamp' ]} </ guid >
< author > admin @ { $_SERVER [ 'SERVER_NAME' ]} </ author >
< pubDate > { $date } </ pubDate >
</ item >
EOT ;
}
}
echo '</channel></rss>' ;
}
2013-02-20 20:14:12 +01:00
?>