2013-02-13 11:22:51 +01:00
< ? php
2013-03-18 12:26:01 +01:00
define ( 'DEBUG' , false );
2013-02-13 11:22:51 +01:00
define ( 'XSAF_VERSION' , 3 );
define ( 'AUTOBLOG_FILE_NAME' , 'autoblog.php' );
2013-02-18 19:42:10 +01:00
define ( 'ALLOW_REMOTE_DB_DL' , false );
define ( 'ALLOW_REMOTE_MEDIA_DL' , false );
2013-04-09 15:50:00 +02:00
define ( 'EXEC_TIME' , 20 );
2013-02-13 11:22:51 +01:00
header ( " HTTP/1.0 403 Forbidden " ); /* Uncivilized method to prevent bot indexing, huh :) */
header ( 'X-Robots-Tag: noindex' ); /* more civilized method, but bots may not all take into account */
2013-02-18 19:42:10 +01:00
//header('Content-type: text/plain');
2013-02-13 11:22:51 +01:00
2013-04-09 15:50:00 +02:00
$expire = time () - 3600 ;
2013-02-18 19:42:10 +01:00
$lockfile = " .xsaflock " ; /* defaut delay: 7200 (2 hours) */
if ( file_exists ( $lockfile ) && filemtime ( $lockfile ) > $expire ) {
2013-02-13 11:22:51 +01:00
echo " too early " ;
die ;
2013-04-09 15:50:00 +02:00
}
2013-02-18 19:42:10 +01:00
else {
if ( file_exists ( $lockfile ) )
unlink ( $lockfile );
2013-04-09 15:50:00 +02:00
2013-02-18 19:42:10 +01:00
if ( file_put_contents ( $lockfile , date ( DATE_RFC822 )) === FALSE ) {
echo " Merci d'ajouter des droits d'écriture sur le dossier. " ;
2013-02-13 11:22:51 +01:00
die ;
2013-04-09 15:50:00 +02:00
}
2013-02-18 19:42:10 +01:00
}
2013-02-13 11:22:51 +01:00
define ( 'ROOT_DIR' , __DIR__ );
2013-02-18 19:42:10 +01:00
if ( file_exists ( " functions.php " )){
include " functions.php " ;
2013-02-18 22:15:15 +01:00
} else {
echo " functions.php not found ! " ;
die ;
2013-02-13 11:22:51 +01:00
}
2013-03-02 12:10:31 +01:00
if ( file_exists ( " config.php " )){
include " config.php " ;
} else {
echo " config.php not found ! " ;
die ;
}
2013-02-13 11:22:51 +01:00
function serverUrl () {
$https = ( ! empty ( $_SERVER [ 'HTTPS' ]) && ( strtolower ( $_SERVER [ 'HTTPS' ]) == 'on' )) || $_SERVER [ " SERVER_PORT " ] == '443' ; // HTTPS detection.
$serverport = ( $_SERVER [ " SERVER_PORT " ] == '80' || ( $https && $_SERVER [ " SERVER_PORT " ] == '443' ) ? '' : ':' . $_SERVER [ " SERVER_PORT " ]);
return 'http' . ( $https ? 's' : '' ) . '://' . $_SERVER [ " SERVER_NAME " ] . $serverport ;
}
libxml_use_internal_errors ( true );
2013-02-18 19:42:10 +01:00
2013-02-13 11:22:51 +01:00
// $max_exec_time = temps max d'exécution en seconde
function xsafimport ( $xsafremote , $max_exec_time ) {
2013-02-18 19:42:10 +01:00
if ( DEBUG )
echo " \n *Traitement $xsafremote en maximum $max_exec_time secondes " ;
2013-02-13 11:22:51 +01:00
$max_exec_time += time () - 1 ; // -1 car l'import prend environ 1 seconde
2013-04-09 15:50:00 +02:00
2013-02-18 19:42:10 +01:00
/* détection de ferme autoblog */
2013-02-13 11:22:51 +01:00
$json_import = file_get_contents ( $xsafremote );
if ( ! empty ( $json_import )) {
$to_update = array ();
$json_import = json_decode ( $json_import , true );
if ( ! isset ( $json_import [ 'meta' ]) || ! isset ( $json_import [ 'meta' ][ 'xsaf-version' ]) || $json_import [ 'meta' ][ 'xsaf-version' ] != XSAF_VERSION ){
if ( DEBUG ){
echo " \n xsaf-version différentes ! " ;
}
return false ;
}
2013-02-18 19:42:10 +01:00
$get_remote_db = ( $json_import [ 'meta' ][ 'xsaf-db_transfer' ] == " true " ) ? true : false ;
$get_remote_media = ( $json_import [ 'meta' ][ 'xsaf-media_transfer' ] == " true " ) ? true : false ;
2013-04-09 15:50:00 +02:00
2013-02-13 11:22:51 +01:00
if ( ! empty ( $json_import [ 'autoblogs' ])) {
foreach ( $json_import [ 'autoblogs' ] as $value ) {
2013-04-09 15:50:00 +02:00
2013-02-13 11:22:51 +01:00
if ( count ( $value ) == 4 && ! empty ( $value [ 'SITE_TYPE' ]) && ! empty ( $value [ 'SITE_TITLE' ]) && ! empty ( $value [ 'SITE_URL' ]) && ! empty ( $value [ 'FEED_URL' ])) {
2013-02-18 19:42:10 +01:00
$sitetype = escape ( $value [ 'SITE_TYPE' ]);
$sitename = escape ( $value [ 'SITE_TITLE' ]);
2013-02-13 11:22:51 +01:00
$siteurl = escape ( $value [ 'SITE_URL' ]);
2013-04-09 15:50:00 +02:00
if ( empty ( $value [ 'SITE_META_DESCRIPTION' ])){
$siteDesc = getSiteDesc ( escape ( $value [ 'SITE_URL' ]));
} else {
$siteDesc = escape ( $value [ 'SITE_META_DESCRIPTION' ]);
}
2013-02-18 19:42:10 +01:00
// Do not use DetectRedirect because it's slow and it has been used when the feed was added
//$rssurl = DetectRedirect(escape($value['FEED_URL']));
2013-02-13 11:22:51 +01:00
$rssurl = escape ( $value [ 'FEED_URL' ]);
}
2013-02-18 19:42:10 +01:00
/* TOO SLOW
$xml = simplexml_load_file ( $rssurl ); // quick feed check
// ATOM feed && RSS 1.0 /RDF && RSS 2.0
2013-04-09 15:50:00 +02:00
$result = ( ! isset ( $xml -> entry ) && ! isset ( $xml -> item ) && ! isset ( $xml -> channel -> item )) ? false : true ; */
2013-02-18 19:42:10 +01:00
$result = true ;
/* autoblog */
if ( $result === true ) {
2013-02-23 20:18:22 +01:00
$foldername = urlToFolderSlash ( $siteurl );
2013-02-18 19:42:10 +01:00
2013-04-09 15:50:00 +02:00
$errors = createAutoblog ( $sitetype , $sitename , $siteurl , $rssurl , $siteDesc );
2013-02-18 19:42:10 +01:00
foreach ( $errors AS $value ) {
if ( DEBUG )
echo '<p>' . $value . '</p>' ;
}
if ( empty ( $errors ) && DEBUG ) {
echo '<p>autoblog ' . $sitename . ' crée avec succès (DL DB : ' . var_dump ( $get_remote_db ) . ' - DL media : ' . var_dump ( $get_remote_media ) . ') : ' . $foldername . '</p>' ;
if ( ! ALLOW_REMOTE_DB_DL && ! ALLOW_REMOTE_MEDIA_DL )
echo '<iframe width="1" height="1" frameborder="0" src="' . $foldername . '/index.php"></iframe>' ;
}
/* ============================================================================================================================================================================== */
/* récupération de la DB distante */
2013-04-09 15:50:00 +02:00
if ( $get_remote_db == true && ALLOW_REMOTE_DB_DL ) {
$remote_db = str_replace ( " ?export " , $foldername . " /articles.db " , $xsafremote );
copy ( $remote_db , './' . $foldername . '/articles.db' );
2013-02-18 19:42:10 +01:00
}
if ( $get_remote_media == true && ALLOW_REMOTE_MEDIA_DL ) {
$remote_media = str_replace ( " ?export " , $foldername . " /?media " , $xsafremote );
$json_media_import = file_get_contents ( $remote_media );
if ( ! empty ( $json_media_import ))
{
mkdir ( './' . $foldername . '/media/' );
$json_media_import = json_decode ( $json_media_import , true );
$media_path = $json_media_import [ 'url' ];
if ( ! empty ( $json_media_import [ 'files' ])) {
foreach ( $json_media_import [ 'files' ] as $value ) {
copy ( $media_path . $value , './' . $foldername . '/media/' . $value );
2013-02-13 11:22:51 +01:00
}
}
}
}
2013-02-18 19:42:10 +01:00
/* ============================================================================================================================================================================== */
//TODO : tester si articles.db est une DB valide
//$to_update[] = serverUrl().preg_replace("/(.*)\/(.*)$/i","$1/".$foldername , $_SERVER['SCRIPT_NAME']); // url of the new autoblog
}
if ( DEBUG )
echo '<p>time : ' . ( $max_exec_time - time ()) . '</p>' ;
if ( time () >= $max_exec_time ) {
if ( DEBUG )
echo " <p>Time out !</p> " ;
2013-04-09 15:50:00 +02:00
break ;
2013-02-18 19:42:10 +01:00
}
}
2013-04-09 15:50:00 +02:00
}
2013-02-18 19:42:10 +01:00
else {
if ( DEBUG )
echo " Format JSON incorrect. " ;
return false ;
}
2013-02-13 11:22:51 +01:00
}
2013-04-09 15:50:00 +02:00
return ;
2013-02-13 11:22:51 +01:00
}
2013-04-09 15:50:00 +02:00
2013-02-18 19:42:10 +01:00
if ( DEBUG ) echo '<html><body>' ;
2013-04-10 13:08:15 +02:00
if ( ALLOW_NEW_AUTOBLOGS and ALLOW_NEW_AUTOBLOGS_BY_XSAF && ! empty ( $friends_autoblog_farm ) ) {
foreach ( $friends_autoblog_farm AS $value ) {
2013-02-18 19:42:10 +01:00
if ( ! empty ( $value ) )
xsafimport ( $value , EXEC_TIME );
}
2013-03-18 12:26:01 +01:00
if ( DEBUG ) echo " <p>XSAF import finished</p> " ;
2013-02-18 19:42:10 +01:00
}
elseif ( DEBUG )
2013-03-02 12:10:31 +01:00
echo " <p>XSAF désactivé. Positionnez les variables ALLOW_NEW_AUTOBLOGS et ALLOW_NEW_AUTOBLOGS_BY_XSAF à TRUE dans le fichier config.php pour l'activer.</p> " ;
2013-02-13 11:22:51 +01:00
2013-02-18 19:42:10 +01:00
if ( DEBUG ) echo '</body></html>' ;
2013-02-23 15:27:29 +01:00
?>