2013-04-18 16:11:06 +02:00
< ? php
// Update site config files for Full-Text RSS
// Author: Keyvan Minoukadeh
2017-02-18 16:06:19 +01:00
// Copyright (c) 2015 Keyvan Minoukadeh
2013-04-18 16:11:06 +02:00
// License: AGPLv3
2017-02-18 16:06:19 +01:00
// Date: 2015-06-10
2013-04-18 16:11:06 +02:00
// More info: http://fivefilters.org/content-only/
// Help: http://help.fivefilters.org
/*
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Affero General Public License for more details .
You should have received a copy of the GNU Affero General Public License
along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
// Usage
// -----
2014-05-15 23:03:31 +02:00
// * Access this file in your browser and follow the instructions to update your site config files.
// * See section on automatic updates for a URL you can fetch periodically (e.g. with cron) to update site config files
2013-04-18 16:11:06 +02:00
error_reporting ( E_ALL ^ E_NOTICE );
ini_set ( " display_errors " , 1 );
@ set_time_limit ( 120 );
////////////////////////////////
// Load config file
////////////////////////////////
$admin_page = 'update' ;
2014-05-15 22:49:16 +02:00
require_once ( '../config.php' );
2015-06-14 02:03:20 +02:00
require_once ( '../libraries/humble-http-agent/HumbleHttpAgent.php' );
require_once ( '../libraries/humble-http-agent/CookieJar.php' );
2014-05-15 23:03:31 +02:00
require_once 'template.php' ;
2013-04-18 16:11:06 +02:00
tpl_header ( 'Update site patterns' );
2014-05-15 23:03:31 +02:00
//////////////////////////////////
// Username and password must be available
//////////////////////////////////
if ( ! isset ( $options -> admin_credentials ) || $options -> admin_credentials [ 'username' ] == '' || $options -> admin_credentials [ 'password' ] == '' ) {
header ( " X-Robots-Tag: noindex, nofollow " , true );
die ( '<h2>Username and password not set</h2><p>Full-Text RSS has not been configured with admin credentials.</p><p>If you are the administrator, please edit your <tt>custom_config.php</tt> file and enter the credentials in the appropriate section. When you\'ve done that, this page will prompt you for your admin credentials.</p>' );
}
$admin_hash = sha1 ( $options -> admin_credentials [ 'username' ] . '+' . $options -> admin_credentials [ 'password' ]);
$_self_host = $_SERVER [ 'HTTP_HOST' ];
$_self_path = rtrim ( dirname ( $_SERVER [ 'SCRIPT_NAME' ]), '/\\' );
$self_update_url = 'http://' . htmlspecialchars ( $_self_host . $_self_path ) . '/update.php?key=' . urlencode ( $admin_hash );
$latest_remote = 'https://codeload.github.com/fivefilters/ftr-site-config/zip/master' ;
$version = @ file_get_contents ( '../site_config/standard/version.txt' );
2013-04-18 16:11:06 +02:00
/////////////////////////////////
2014-05-15 23:03:31 +02:00
// Check for update key
2013-04-18 16:11:06 +02:00
/////////////////////////////////
if ( ! isset ( $_REQUEST [ 'key' ]) || trim ( $_REQUEST [ 'key' ]) == '' ) {
2014-05-15 23:03:31 +02:00
require_once 'require_login.php' ;
2013-04-18 16:11:06 +02:00
if ( $_SERVER [ 'REQUEST_METHOD' ] == 'POST' ) {
header ( 'Location: update.php' );
exit ;
2014-05-15 23:03:31 +02:00
}
2013-04-18 16:11:06 +02:00
$auto = true ;
$no_auto_reasons = array ();
if ( ! class_exists ( 'ZipArchive' )) {
$auto = false ;
$no_auto_reasons [] = 'zip support (PHP\'s <a href="http://php.net/manual/en/zip.requirements.php">ZipArchive</a> class) is missing' ;
}
if ( ! is_writable ( '../site_config' )) {
$auto = false ;
$no_auto_reasons [] = 'your <tt>site_config/</tt> folder is not writable - change permissions to 777 and try again.</p>' ;
}
2014-05-15 23:03:31 +02:00
if ( ! file_exists ( '../site_config/standard/version.txt' )) {
die ( 'Could not determine current version of your site pattern files (site_config/standard/version.txt). Make sure you\'re using at least version 3.2 of Full-Text RSS.' );
2013-04-18 16:11:06 +02:00
}
?>
< p > You have Full - Text RSS < strong >< ? php echo _FF_FTR_VERSION ; ?> </strong>
( Site Patterns version : < strong >< ? php echo ( isset ( $version ) ? $version : 'Unknown' ); ?> </strong>)
</ p >
< p > To see if you have the latest versions , < a href = " http://fivefilters.org/content-only/latest_version.php?version=<?php echo urlencode(_FF_FTR_VERSION).'&site_config='.urlencode(@ $version ); ?> " > check for updates </ a >.</ p >
< ? php
if ( $auto ) {
2014-05-15 23:03:31 +02:00
echo '<p>This update tool will attempt to fetch the latest site patterns from our <a href="https://github.com/fivefilters/ftr-site-config/">GitHub repository</a>.</p>' ;
2013-04-18 16:11:06 +02:00
echo '<p><strong>Important: </strong>if you\'ve modified or added your own config files in the <tt>site_config/standard/</tt> folder, please move them to <tt>site_config/custom/</tt> — the update process will attempt to replace everything in <tt>site_config/standard/</tt> with our updated version.</p>' ;
2014-05-15 23:03:31 +02:00
echo '<form method="post" action="update.php" class="well">' ;
echo '<input type="hidden" name="key" value="' . $admin_hash . '" />' ;
echo '<input type="submit" value="Update site config files" />' ;
echo '</form>' ;
echo '<h3>Automatic updates</h3>' ;
echo '<p>You can schedule automatic updates using something like cron. The URL to call is:</p>' ;
echo '<p class="well">' . $self_update_url . '</p>' ;
echo '<p>We recommend you schedule this URL to be fetched once a day. If you do not have access to a scheduling service ' ;
echo 'you may want to consider one of these online services: <a href="http://www.easycron.com/">Easycron</a>, <a href="https://www.setcronjob.com/">SetCronJob</a>, <a href="http://www.onlinecronjobs.com/">onlinecronjobs.com</a>.</p>' ;
echo '<p>Note: the key contained in the URL is a hash value generated from your admin credentials. If you change these, the key will also change.</p>' ;
2013-04-18 16:11:06 +02:00
} else {
echo '<div class="notice">' ;
echo '<p>We cannot automatically update your site pattern files because:</p>' ;
echo '<ul>' ;
foreach ( $no_auto_reasons as $reason ) {
echo '<li>' , $reason , '</li>' ;
}
echo '</ul>' ;
echo '<p>You can still manually update by downloading the zip file and replacing everything in your <tt>site_config/standard/</tt> folder with the contents of the zip file.</p>' ;
echo '</div>' ;
2014-05-15 23:03:31 +02:00
echo '<p><a href="' . $latest_remote . '">Download site config files (zip)</a></p>' ;
2013-04-18 16:11:06 +02:00
}
echo '<h3>Help</h3>' ;
echo '<p>If you have any trouble, please contact us via our <a href="http://help.fivefilters.org">support site</a>.</p>' ;
exit ;
}
2014-05-15 23:03:31 +02:00
//////////////////////////////////
// Check update key valid
//////////////////////////////////
if ( $_REQUEST [ 'key' ] !== $admin_hash ) {
println ( " Sorry, invalid key supplied. " );
exit ;
}
2013-04-18 16:11:06 +02:00
//////////////////////////////////
// Check for updates
//////////////////////////////////
2014-05-15 23:03:31 +02:00
//$ff_version = @file_get_contents('http://fivefilters.org/content-only/site_config/standard/version.txt');
2015-06-14 02:03:20 +02:00
$http = new HumbleHttpAgent ();
$latest_info_json = $http -> get ( 'https://api.github.com/repos/fivefilters/ftr-site-config' );
//$_context = stream_context_create(array('http' => array('user_agent' => 'PHP/5.5'), 'ssl'=>array('verify_peer'=>false)));
//$latest_info_json = file_get_contents('https://api.github.com/repos/fivefilters/ftr-site-config', false, $_context);
2014-05-15 23:03:31 +02:00
if ( ! $latest_info_json ) {
println ( " Sorry, couldn't get info on latest site config files. Please try again later or contact us. " );
exit ;
}
2015-06-14 02:03:20 +02:00
$latest_info_json = $latest_info_json [ 'body' ];
2014-05-15 23:03:31 +02:00
$latest_info_json = @ json_decode ( $latest_info_json );
if ( ! is_object ( $latest_info_json )) {
println ( " Sorry, couldn't parse JSON from GitHub. Please try again later or contact us. " );
exit ;
}
2015-06-14 02:03:20 +02:00
$ff_version = $latest_info_json -> pushed_at ;
2014-05-15 23:03:31 +02:00
if ( $version == $ff_version ) {
2013-04-18 16:11:06 +02:00
die ( 'Your site config files are up to date! If you have trouble extracting from a particular site, please email us: help@fivefilters.org' );
} else {
2014-05-15 23:03:31 +02:00
println ( " Updated site patterns are available (version $ff_version )... " );
2013-04-18 16:11:06 +02:00
}
//////////////////////////////////
// Prepare
//////////////////////////////////
$tmp_latest_local = '../site_config/latest_site_config.zip' ;
$tmp_latest_local_dir = '../site_config/standard_latest' ;
$tmp_old_local_dir = '../site_config/standard_old' ;
if ( file_exists ( $tmp_latest_local )) unlink ( $tmp_latest_local );
2014-05-15 23:03:31 +02:00
if ( file_exists ( $tmp_latest_local_dir )) {
if ( ! rrmdir ( $tmp_latest_local_dir )) {
println ( " Sorry, couldn't remove old folder from last update " );
exit ;
}
}
2013-04-18 16:11:06 +02:00
if ( file_exists ( $tmp_old_local_dir )) {
rrmdir ( $tmp_old_local_dir );
}
$standard_local_dir = '../site_config/standard/' ;
//@copy($latest_remote, $tmp_latest_local);
//copy() does not appear to fill $http_response_header in certain environments
2015-06-14 02:03:20 +02:00
//@file_put_contents($tmp_latest_local, @file_get_contents($latest_remote, false, $_context));
$latest_remote_response = $http -> get ( $latest_remote );
if ( ! is_array ( $latest_remote_response )) {
println ( " Sorry, something went wrong. Please contact us if the problem persists. " );
exit ;
}
@ file_put_contents ( $tmp_latest_local , $latest_remote_response [ 'body' ]);
//$headers = implode("\n", $http_response_header);
$headers = $latest_remote_response [ 'headers' ];
2013-04-18 16:11:06 +02:00
//var_dump($headers); exit;
2014-09-15 22:24:06 +02:00
if (( strpos ( $headers , 'HTTP/1.0 200' ) === false ) && ( strpos ( $headers , 'HTTP/1.1 200' ) === false )) {
2014-05-15 23:03:31 +02:00
println ( " Sorry, something went wrong. Please contact us if the problem persists. " );
2013-04-18 16:11:06 +02:00
exit ;
}
if ( class_exists ( 'ZipArchive' ) && file_exists ( $tmp_latest_local )) {
println ( " Downloaded latest copy of the site pattern files to $tmp_latest_local " );
$zip = new ZipArchive ;
if ( $zip -> open ( $tmp_latest_local ) === TRUE ) {
$zip -> extractTo ( $tmp_latest_local_dir );
$zip -> close ();
@ unlink ( $tmp_latest_local );
if ( file_exists ( $tmp_latest_local_dir )) {
println ( " Unzipped contents to $tmp_latest_local_dir " );
2014-05-15 23:03:31 +02:00
if ( ! file_exists ( $tmp_latest_local_dir . '/ftr-site-config-master/README.md' )) {
2013-04-18 16:11:06 +02:00
println ( " There was a problem extracting the latest site patterns archive - your current site patterns remain untouched. " );
println ( " Please <a href= \" $latest_remote\ " > update manually </ a >. " );
exit ;
}
2014-05-15 23:03:31 +02:00
@ file_put_contents ( $tmp_latest_local_dir . '/ftr-site-config-master/version.txt' , $ff_version );
if ( ! file_exists ( $tmp_latest_local_dir . '/ftr-site-config-master/version.txt' )) {
println ( " There was a problem writing the new version number - your current site patterns remain untouched. " );
println ( " Please <a href= \" $latest_remote\ " > update manually </ a >. " );
exit ;
}
2013-04-18 16:11:06 +02:00
rename ( $standard_local_dir , $tmp_old_local_dir );
if ( file_exists ( $tmp_old_local_dir )) println ( " Renamed $standard_local_dir to $tmp_old_local_dir " );
2014-05-15 23:03:31 +02:00
rename ( $tmp_latest_local_dir . " /ftr-site-config-master " , $standard_local_dir );
if ( file_exists ( $standard_local_dir )) println ( " Renamed $tmp_latest_local_dir /ftr-site-config-master to $standard_local_dir " );
rmdir ( $tmp_latest_local_dir );
// clear cached site config files from APC
if ( $options -> apc && function_exists ( 'apc_delete' ) && function_exists ( 'apc_cache_info' )) {
$_apc_data = apc_cache_info ( 'user' );
foreach ( $_apc_data [ 'cache_list' ] as $_apc_item ) {
2014-09-15 22:24:06 +02:00
// APCu keys incompatible with original APC keys, apparently fixed in newer versions, but not in 4.0.4
// So let's look for those keys and fix here (key -> info).
if ( isset ( $_apc_item [ 'key' ])) $_apc_item [ 'info' ] = $_apc_item [ 'key' ];
2014-05-15 23:03:31 +02:00
if ( substr ( $_apc_item [ 'info' ], 0 , 3 ) == 'sc.' ) {
apc_delete ( $_apc_item [ 'info' ]);
}
}
println ( 'Cleared site config cache in APC.' );
}
// all done!
2013-04-18 16:11:06 +02:00
println ( " <strong style= \" color: darkgreen; \" >All done!</strong> Your old site config files are in $tmp_old_local_dir — these will be removed next time you go through the update process. " );
} else {
if ( file_exists ( $tmp_latest_local )) @ unlink ( $tmp_latest_local );
println ( " Failed to unzip to $tmp_latest_local_dir - your current site patterns remain untouched " );
}
} else {
if ( file_exists ( $tmp_latest_local )) @ unlink ( $tmp_latest_local );
println ( " Failed to extract from $tmp_latest_local - your current site patterns remain untouched " );
}
} else {
println ( " Could not download the latest site config files. Please <a href= \" $latest_remote\ " > update manually </ a > - your current site patterns remain untouched . " );
}
function println ( $txt ) {
echo $txt , " <br /> \n " ;
ob_end_flush ();
ob_flush ();
flush ();
}
function rrmdir ( $dir ) {
2017-02-18 16:06:19 +01:00
foreach ( glob ( $dir . '/{*.txt,*.php,*.com,.*.txt,.*.php,.*.com,.gitattributes,.gitignore,ftr-site-config-master,README.md}' , GLOB_BRACE | GLOB_NOSORT ) as $file ) {
2013-04-18 16:11:06 +02:00
if ( is_dir ( $file )) {
rrmdir ( $file );
} else {
unlink ( $file );
}
}
2014-05-15 23:03:31 +02:00
return rmdir ( $dir );
}