2011-12-13 12:29:05 +01:00
< ? php
if ( IN_serendipity !== true ) {
die ( " Don't hack! " );
}
// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname ( __FILE__ ) . '/' . $serendipity [ 'charset' ] . 'lang_' . $serendipity [ 'lang' ] . '.inc.php' ;
if ( file_exists ( $probelang )) {
include $probelang ;
}
include dirname ( __FILE__ ) . '/lang_en.inc.php' ;
class serendipity_event_autoupdate extends serendipity_event {
var $title = PLUGIN_EVENT_AUTOUPDATE_NAME ;
function introspect ( & $propbag ) {
global $serendipity ;
$propbag -> add ( 'name' , PLUGIN_EVENT_AUTOUPDATE_NAME );
$propbag -> add ( 'description' , PLUGIN_EVENT_AUTOUPDATE_DESC );
$propbag -> add ( 'stackable' , false );
2013-04-27 10:37:11 +02:00
$propbag -> add ( 'author' , 'onli, Ian' );
2016-10-26 15:07:46 +02:00
$propbag -> add ( 'version' , '1.1.8' );
2011-12-13 12:29:05 +01:00
$propbag -> add ( 'requirements' , array (
2012-08-14 20:20:52 +02:00
'serendipity' => '0.8' ,
2015-07-31 12:18:07 +02:00
'php' => '5.2'
2011-12-13 12:29:05 +01:00
));
$propbag -> add ( 'event_hooks' , array ( 'plugin_dashboard_updater' => true ,
'backend_sidebar_entries_event_display_update' => true ));
$propbag -> add ( 'groups' , array ( 'BACKEND_FEATURES' ));
2015-07-24 16:38:46 +02:00
if ( $serendipity [ 'version' ][ 0 ] < 2 ) {
$this -> dependencies = array ( 'serendipity_event_dashboard' => 'keep' );
}
2011-12-13 12:29:05 +01:00
}
function generate_content ( & $title ) {
$title = $this -> title ;
}
2013-05-30 14:47:40 +02:00
function install () {
global $serendipity ;
if ( ! $serendipity [ 'serendipityUserlevel' ] >= USERLEVEL_ADMIN ) {
return false ;
}
}
2011-12-13 12:29:05 +01:00
/* function introspect_config_item ( $name , & $propbag ) {
} */
2013-04-27 10:37:11 +02:00
/**
* flush progress or error messages
*/
function show_message ( $message = '' , $pname = '' , $next = '' ) {
2015-07-31 12:18:07 +02:00
if ( ! empty ( $pname )) {
2013-04-27 10:37:11 +02:00
// Total processes
$total = 3 ;
2014-02-12 16:13:22 +01:00
ob_implicit_flush ( 1 );
2013-04-27 10:37:11 +02:00
// fake processing loop
2015-07-31 12:18:07 +02:00
for ( $i = 1 ; $i <= $total ; $i ++ ) {
2013-04-27 10:37:11 +02:00
// Calculate the percentation
$percent = intval ( $i / $total * 100 ) . " % " ;
// Javascript for updating the progress bar and information
echo '
2014-06-09 19:01:43 +02:00
< script type = " text/javascript " >
2013-04-27 10:37:11 +02:00
document . getElementById ( " progress " ) . innerHTML = " <div style= \" width:'. $percent .';background-color:#ddd; \" > </div> " ;
document . getElementById ( " information " ) . innerHTML = " '. $percent .' processed. " ;
</ script > ' ;
2014-02-12 16:13:22 +01:00
//this is for the buffer achieve the minimum size in order to flush data
2014-02-13 09:11:20 +01:00
echo str_repeat ( ' ' , 1024 * 64 ); // need to keep here since this also flushes the progress bar on fastCGI
2014-02-12 16:13:22 +01:00
2013-04-27 10:37:11 +02:00
// Send output to browser immediately
2014-02-12 16:31:20 +01:00
ob_flush ();
2013-04-27 10:37:11 +02:00
flush ();
// Sleep one second so we can see the delay
sleep ( 1 );
}
$wait = strstr ( $pname , 'Function' ) ? ' Please wait ... Processing: ' . $next . ' ...' : '' ; // no tags allowed here
// Tell user that the process is completed
2014-06-09 19:01:43 +02:00
echo '<script type="text/javascript">document.getElementById("information").innerHTML="' . $pname . ' completed!' . $wait . '"</script>' ;
2013-04-27 10:37:11 +02:00
}
echo " $message\n " ;
$levels = ob_get_level ();
for ( $i = 0 ; $i < $levels ; $i ++ ) {
ob_end_flush ();
}
2014-02-13 09:11:20 +01:00
ob_flush ();
2013-04-27 10:37:11 +02:00
flush ();
}
2011-12-13 12:29:05 +01:00
2012-06-13 14:47:45 +02:00
function event_hook ( $event , & $bag , & $eventData , $addData = null ) {
2011-12-13 12:29:05 +01:00
global $serendipity ;
$hooks = & $bag -> get ( 'event_hooks' );
if ( isset ( $hooks [ $event ])) {
switch ( $event ) {
case 'plugin_dashboard_updater' :
$eventData = ' < form action = " ?serendipity[adminModule]=event_display&serendipity[adminAction]=update " method = " POST " >
< input type = " hidden " name = " serendipity[newVersion] " value = " '. $addData .' " />
2014-12-23 18:47:04 +01:00
' . ($serendipity[' version '][0] > 1 ? ' < button type = " submit " > '.PLUGIN_EVENT_AUTOUPDATE_UPDATEBUTTON.' </ button > ' : ' < input type = " submit " value = " '.PLUGIN_EVENT_AUTOUPDATE_UPDATEBUTTON.' " /> ') . '
2011-12-13 12:29:05 +01:00
</ form > ' ;
return true ;
break ;
case 'backend_sidebar_entries_event_display_update' :
2015-07-31 12:34:19 +02:00
if ( ! ( serendipity_checkPermission ( 'siteConfiguration' ) || serendipity_checkPermission ( 'blogConfiguration' ))) {
2011-12-13 12:29:05 +01:00
return ;
}
2013-10-06 16:03:41 +02:00
if ( ! extension_loaded ( 'zip' )) {
trigger_error ( ' ZIP extension has not been compiled or loaded in php.' , E_USER_WARNING );
return ;
}
2013-04-27 10:37:11 +02:00
@ ini_set ( 'max_execution_time' , 210 ); // 180 + (21+9 gui happenings)
#@ini_set('memory_limit', '-1'); // extending memory_limit may be prevented by suhosin extension.
2012-08-14 20:20:52 +02:00
/*
As long scripts are not running within safe_mode they are free to change the memory_limit to whatever value they want .
Suhosin changes this fact and disallows setting the memory_limit to a value greater than the one the script started with ,
when this option is left at 0. A value greater than 0 means that Suhosin will disallows scripts setting the memory_limit
to a value above this configured hard limit . This is for example usefull if you want to run the script normaly with a limit
of 16 M but image processing scripts may raise it to 20 M .
Edit / etc / php5 / conf . d / suhosin . ini and add e . g . suhosin . memory_limit = 512 M ...
*/
2014-11-25 01:15:57 +01:00
$self_info = sprintf ( USER_SELF_INFO , ( function_exists ( 'serendipity_specialchars' ) ? serendipity_specialchars ( $serendipity [ 'serendipityUser' ]) : htmlspecialchars ( $serendipity [ 'serendipityUser' ], ENT_COMPAT , LANG_CHARSET )), $serendipity [ 'permissionLevels' ][ $serendipity [ 'serendipityUserlevel' ]]);
2013-04-27 10:37:11 +02:00
$lang_char = LANG_CHARSET ;
$ad_suite = SERENDIPITY_ADMIN_SUITE ;
2013-04-27 11:17:32 +02:00
$css_upd = file_get_contents ( dirname ( __FILE__ ) . '/upgrade.min.css' );
2014-11-25 01:15:57 +01:00
$nv = ( function_exists ( 'serendipity_specialchars' ) ? serendipity_specialchars ( $_REQUEST [ 'serendipity' ][ 'newVersion' ]) : htmlspecialchars ( $_REQUEST [ 'serendipity' ][ 'newVersion' ], ENT_COMPAT , LANG_CHARSET )); // reduce to POST only?
2013-04-27 10:37:11 +02:00
$logmsg = '' ;
echo <<< EOS
<! DOCTYPE html >
< html >
< head >
< base target = " _self " />
< title > Startpage - { $ad_suite } </ title >
< meta http - equiv = " Content-Type " content = " text/html; charset= { $lang_char } " >
< style type = " text/css " >
{ $css_upd }
</ style >
</ head >
< body id = " serendipity_admin_page " >
< header id = " top " >
< div class = " clearfix " >
< div id = " banner " >
< h1 > { $ad_suite } </ h1 >
< span class = " block_level " > { $self_info } </ span >
</ div >
< nav id = " user_menu " >
< h2 class = " visuallyhidden " > User menu </ h2 >
< ul >
2016-10-26 15:07:46 +02:00
< li >< a class = " icon_link " href = " serendipity_admin.php " title = " Startpage " >< span class = " icon-home " aria - hidden = " true " ></ span >< span class = " visuallyhidden " > Startpage </ span ></ a ></ li >
2013-04-27 10:37:11 +02:00
</ ul >
</ nav >
</ div >
</ header >
< div id = " main " class = " clearfix " >
< div id = " serendipity_updater " class = " clearfix " >
< header >
< h2 > Serendipity Auto - Upgrade Processor </ h2 >
<!-- Progress bar holder -->
< div id = " progress " style = " width:500px;border:1px solid #ccc; " ></ div >
<!-- Progress information -->
< div id = " information " style = " width " ></ div >
< div id = " loader " >< span ></ span >< span ></ span >< span ></ span ></ div >
</ header >
< article >
EOS ;
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_notice"><span class="icon-attention" aria-hidden="true"></span>Download, verify, check, unzip, copy, remove temporary stuff for Serendipity Update: ' . $_REQUEST [ 'serendipity' ][ 'newVersion' ] . ' may take a little while...<br>Please don\'t get nervous and do not close this page while in progress!</p><hr>' );
$this -> show_message ( '<p class="msg_notice" style="font-size: small;color: #999;"><span class="icon-attention" aria-hidden="true"></span>Please note: If this page ever stops with an error message during procession, you can normally just RELOAD your browser [by keyboard shortcut] to get another run. This does not do any harm to a continued upgrade.</p>' );
$this -> show_message ( '<p class="msg_notice"><span class="icon-attention" aria-hidden="true"></span>PHP max execution time set to 210 seconds</p>' );
2012-08-14 20:20:52 +02:00
$start = microtime ( true );
2015-07-31 12:18:07 +02:00
if ( false === ( $update = $this -> fetchUpdate ( $nv ))) {
2013-04-27 10:37:11 +02:00
$this -> close_page ( true );
}
2014-12-23 18:47:04 +01:00
usleep ( 3 );
2012-08-14 20:20:52 +02:00
$time = microtime ( true ) - $start ;
2013-04-27 10:37:11 +02:00
$logmsg .= $lmsg = sprintf ( " In %0.4d seconds run fcn fetchUpdate()... \n " , $time ); // print in readable format 1.2345
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_run"><span class="icon-clock" aria-hidden="true"></span><em>' . $lmsg . '</em></p>' , 'Function fetch update' , 'verify the update pack' );
2015-07-31 12:18:07 +02:00
if ( ! empty ( $update )) {
2012-08-14 20:20:52 +02:00
$start = microtime ( true );
2011-12-13 12:29:05 +01:00
if ( $this -> verifyUpdate ( $update , $nv )) {
2014-12-23 18:47:04 +01:00
usleep ( 3 );
2012-08-14 20:20:52 +02:00
$time = microtime ( true ) - $start ;
2013-04-27 10:37:11 +02:00
$logmsg .= $lmsg = sprintf ( " In %0.4d seconds run fcn verifyUpdate()... \n " , $time ); // print in readable format 1.2345
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_run"><span class="icon-clock" aria-hidden="true"></span><em>' . $lmsg . '</em></p>' , 'Function verify update' , 'checking write permissions' );
2012-08-14 20:20:52 +02:00
$start = microtime ( true );
2011-12-13 12:29:05 +01:00
if ( $this -> checkWritePermissions ( $update )) {
2014-12-23 18:47:04 +01:00
usleep ( 3 );
2012-08-14 20:20:52 +02:00
$time = microtime ( true ) - $start ;
2013-04-27 10:37:11 +02:00
$logmsg .= $lmsg = sprintf ( " In %0.4d seconds run fcn checkWritePermissions()... \n " , $time ); // print in readable format 1.2345
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_run"><span class="icon-clock" aria-hidden="true"></span><em>' . $lmsg . '</em></p>' , 'Function check write permissions' , 'unpacking the update' );
2012-08-14 20:20:52 +02:00
$start = microtime ( true );
2011-12-13 12:29:05 +01:00
$unpacked = $this -> unpackUpdate ( $nv );
2014-12-23 18:47:04 +01:00
usleep ( 3 );
2012-08-14 20:20:52 +02:00
$time = microtime ( true ) - $start ;
2013-04-27 10:37:11 +02:00
$logmsg .= $lmsg = sprintf ( " In %0.4d seconds run fcn unpackUpdate()... \n " , $time ); // print in readable format 1.2345
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_run"><span class="icon-clock" aria-hidden="true"></span><em>' . $lmsg . '</em></p>' , 'Function unpack update' , 'checking integrity' );
2011-12-13 12:29:05 +01:00
if ( $unpacked ) {
2012-08-14 20:20:52 +02:00
$start = microtime ( true );
2011-12-13 12:29:05 +01:00
if ( $this -> checkIntegrity ( $nv )) {
2014-12-23 18:47:04 +01:00
usleep ( 3 );
2012-08-14 20:20:52 +02:00
$time = microtime ( true ) - $start ;
2013-04-27 10:37:11 +02:00
$logmsg .= $lmsg = sprintf ( " In %0.4d seconds run fcn checkIntegrity()... \n " , $time ); // print in readable format 1.2345
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_run"><span class="icon-clock" aria-hidden="true"></span><em>' . $lmsg . '</em></p>' , 'Function check integrity' , 'finally copy update' );
2012-08-14 20:20:52 +02:00
$start = microtime ( true );
2012-06-08 14:46:31 +02:00
$copied = $this -> copyUpdate ( $nv );
2014-12-23 18:47:04 +01:00
usleep ( 3 );
2012-08-14 20:20:52 +02:00
$time = microtime ( true ) - $start ;
2013-04-27 10:37:11 +02:00
$logmsg .= $lmsg = sprintf ( " In %0.4d seconds run fcn copyUpdate()... \n " , $time ); // print in readable format 1.2345
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_run"><span class="icon-clock" aria-hidden="true"></span><em>' . $lmsg . '</em></p>' , 'Function copy update' , 'cleaning up temporary directory' );
2012-06-08 14:46:31 +02:00
if ( $copied ) {
2012-08-14 20:20:52 +02:00
$start = microtime ( true );
2013-04-27 10:37:11 +02:00
if ( true === $this -> cleanTemplatesC ( $nv , true ) ) {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_success"><span class="icon-ok" aria-hidden="true"></span>Cleanup download temp done!</p>' );
2012-08-14 20:20:52 +02:00
}
2014-12-23 18:47:04 +01:00
usleep ( 3 );
2012-08-14 20:20:52 +02:00
$time = microtime ( true ) - $start ;
2013-04-27 10:37:11 +02:00
$logmsg .= $lmsg = sprintf ( " In %0.4d seconds run fcn cleanTemplatesC()... \n " , $time ); // print in readable format 1.2345
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_run"><span class="icon-clock" aria-hidden="true"></span><em>' . $lmsg . '</em></p>' , 'Function cleanup templates_c' , 'finish processing unit' );
2013-04-27 10:37:11 +02:00
sleep ( 2 );
2014-06-09 19:01:43 +02:00
echo '<script type="text/javascript">var el = document.getElementById("loader"); el.style.display = "none";</script>' ;
2013-04-27 10:37:11 +02:00
sleep ( 2 );
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_notice"><span class="icon-attention" aria-hidden="true"></span><a href="' . $serendipity [ 'serendipityHTTPPath' ] . '">click to start Serendipity Installer here</a>!</p>' );
2013-04-27 10:37:11 +02:00
sleep ( 1 );
$this -> doUpdate (); //$logmsg
2012-06-08 14:46:31 +02:00
} else {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Copying the files for the update failed</p>' );
2012-06-08 14:46:31 +02:00
}
2011-12-13 12:29:05 +01:00
} else {
$this -> showChecksumErrors ( $nv );
echo ' < form action = " ?serendipity[adminModule]=event_display&serendipity[adminAction]=update " method = " POST " >
< input type = " hidden " name = " serendipity[newVersion] " value = " '. $nv .' " />
< input type = " submit " value = " '.PLUGIN_EVENT_AUTOUPDATE_UPDATEBUTTON.' " />
</ form > ' ;
}
} else {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Unpacking the update failed</p>' );
2015-07-31 12:34:19 +02:00
if ( true === $this -> cleanTemplatesC ( $nv , false )) {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_success"><span class="icon-ok" aria-hidden="true"></span>Cleaning up the failed unpack directory!</p>' );
2013-04-27 10:37:11 +02:00
}
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_notice"><span class="icon-attention" aria-hidden="true"></span>Please reload this page by F5 to have another try upgrading your Blog successfully!</p>' );
2011-12-13 12:29:05 +01:00
}
} else {
$this -> showNotWriteable ( $update );
echo ' < form action = " ?serendipity[adminModule]=event_display&serendipity[adminAction]=update " method = " POST " >
< input type = " hidden " name = " serendipity[newVersion] " value = " '. $nv .' " />
< input type = " submit " value = " '.PLUGIN_EVENT_AUTOUPDATE_UPDATEBUTTON.' " />
</ form > ' ;
}
}
}
2013-04-27 10:37:11 +02:00
$this -> close_page ( true );
2011-12-13 12:29:05 +01:00
return true ;
break ;
default :
return false ;
}
} else {
return false ;
}
}
2012-08-14 20:20:52 +02:00
/**
* fetch the zip file from server
2015-03-13 18:27:40 +01:00
* @ param string $version Version
* @ return mixed updatepath / bool
2012-08-14 20:20:52 +02:00
*/
2011-12-13 12:29:05 +01:00
function fetchUpdate ( $version ) {
global $serendipity ;
2015-07-24 16:38:46 +02:00
$url = ( string ) " https://github.com/s9y/Serendipity/releases/download/ $version /serendipity- $version .zip " ;
2016-10-03 20:55:04 +02:00
if ( strpos ( $version , 'beta' ) !== FALSE ) {
$url = ( string ) " https://github.com/s9y/Serendipity/archive/ $version .zip " ;
}
2012-08-14 20:20:52 +02:00
$update = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version .zip " ;
2016-09-26 22:23:29 +02:00
2015-07-31 12:18:07 +02:00
// do we already have it and is it eventually broken?
if ( file_exists ( $update )) {
$zip = new ZipArchive ;
$res = $zip -> open ( $update );
if ( $res === TRUE ) {
$done = true ;
} else {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Existing Zip file Error, Code:' . $res . '. The autoupdater will try to download again...' );
2015-07-31 14:20:55 +02:00
@ unlink ( $update );
2015-07-31 12:18:07 +02:00
sleep ( 1 );
$done = @ copy ( $url , $update ) ? true : false ;
sleep ( 1 );
}
$zip -> close ();
} else {
$done = @ copy ( $url , $update ) ? true : false ;
sleep ( 1 );
}
2013-04-27 10:37:11 +02:00
2015-07-31 12:18:07 +02:00
if ( ! $done ) {
2011-12-13 12:29:05 +01:00
//try it again with curl if copy was forbidden
if ( function_exists ( 'curl_init' )) {
2015-03-13 18:27:40 +01:00
$out = @ fopen ( $update , 'wb' );
2015-07-31 12:18:07 +02:00
$ch = @ curl_init ();
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
@ curl_setopt ( $ch , CURLOPT_FILE , $out );
@ curl_setopt ( $ch , CURLOPT_HEADER , 0 );
2015-03-13 18:27:40 +01:00
@ curl_setopt ( $ch , CURLOPT_URL , $url );
2011-12-13 12:29:05 +01:00
2012-08-14 20:20:52 +02:00
$success = @ curl_exec ( $ch );
2015-07-31 12:18:07 +02:00
if ( ! $success ) {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Downloading update failed (curl installed, but failed). Does ' . $url . ' exist?</p>' );
2013-04-27 10:37:11 +02:00
return false ;
2011-12-13 12:29:05 +01:00
}
2016-09-26 22:23:29 +02:00
} else {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Downloading update failed (copy failed, curl not available). Does ' . $url . ' exist?</p>' );
2016-09-26 22:23:29 +02:00
return false ;
}
2011-12-13 12:29:05 +01:00
}
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_success"><span class="icon-ok" aria-hidden="true"></span>Fetch download to templates_c done</p>' );
2011-12-13 12:29:05 +01:00
return $update ;
}
2012-08-14 20:20:52 +02:00
/**
* compare the md5 of downloaded archive with the md5 posted on the downloadpage
* @ param string updatePath
* @ param string version
* @ return boolean
*/
2011-12-13 12:29:05 +01:00
function verifyUpdate ( $update , $version ) {
2015-07-24 16:38:46 +02:00
$url = ( string ) " https://github.com/s9y/Serendipity/releases/download/ $version /serendipity- $version .zip " ;
$updatePage = ( string ) $this -> getPage ( " https://github.com/s9y/Serendipity/releases/tag/ $version " );
2013-04-27 10:37:11 +02:00
$found = array ();
2012-08-14 20:20:52 +02:00
// grep the checksum
2015-07-24 19:40:09 +02:00
preg_match ( " / \ (MD5: (.*) \ )/ " , $updatePage , $found );
2011-12-13 12:29:05 +01:00
$checksum = $found [ 1 ];
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_notice"><span class="icon-attention" aria-hidden="true"></span>Checking MD5 zip file checksum: ' . $checksum . '</p>' );
2011-12-13 12:29:05 +01:00
$check = md5_file ( $update );
2016-10-03 20:55:04 +02:00
if ( strpos ( $version , 'beta' ) !== FALSE ) {
return true ;
}
2011-12-13 12:29:05 +01:00
if ( $check == $checksum ) {
return true ;
} else {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Error. Could not verify the update.</p>' );
2011-12-13 12:29:05 +01:00
return false ;
}
}
2012-08-14 20:20:52 +02:00
/**
* get file content of updatePage
* @ param string url
* @ return page content
*/
2011-12-13 12:29:05 +01:00
function getPage ( $url ) {
$page = file_get_contents ( $url );
2015-07-31 12:18:07 +02:00
if ( empty ( $page )) {
2011-12-13 12:29:05 +01:00
//try it again with curl if fopen was forbidden
if ( function_exists ( 'curl_init' )) {
$ch = curl_init ( $url );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt ( $ch , CURLOPT_TIMEOUT , " 20 " );
$page = curl_exec ( $ch );
curl_close ( $ch );
}
}
return $page ;
}
2012-08-14 20:20:52 +02:00
/**
* unpack the update to templates_c
* @ param string version
* @ return boolean
*/
2011-12-13 12:29:05 +01:00
function unpackUpdate ( $version ) {
global $serendipity ;
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
$update = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version .zip " ;
$updateDir = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version / " ;
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
// do we already have it?
2013-04-27 10:37:11 +02:00
if ( is_dir ( $updateDir ) && is_file ( $updateDir . '/serendipity/README.markdown' ) && is_file ( $updateDir . '/serendipity/checksums.inc.php' )) {
2012-08-14 20:20:52 +02:00
return true ;
}
2011-12-13 12:29:05 +01:00
$zip = new ZipArchive ;
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
if ( $zip -> open ( $update ) === true ) {
2012-08-14 20:20:52 +02:00
// 1.get all filenames apart from the root 'serendipity'
2011-12-13 12:29:05 +01:00
$i = 1 ;
$files = array ();
$name = $zip -> getNameIndex ( $i );
while ( ! empty ( $name )) {
$files [] = $name ;
$name = $zip -> getNameIndex ( $i );
$i += 1 ;
}
2012-08-14 20:20:52 +02:00
// 2.extraxt all files to temp
2011-12-13 12:29:05 +01:00
$zip -> extractTo ( $updateDir );
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_success"><span class="icon-ok" aria-hidden="true"></span>Extracting the zip in templates_c done</p>' );
2011-12-13 12:29:05 +01:00
$zip -> close ();
} else {
return false ;
}
return true ;
}
2012-08-14 20:20:52 +02:00
/**
* copy the update from templates_c over the existing files
* @ param string version
* @ return boolean
*/
2011-12-13 12:29:05 +01:00
function copyUpdate ( $version ) {
global $serendipity ;
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
$update = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version .zip " ;
$updateDir = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version / " ;
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
$zip = new ZipArchive ;
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
if ( $zip -> open ( $update ) === true ) {
2012-08-14 20:20:52 +02:00
// 1.get all filenames apart from the root 'serendipity'
2011-12-13 12:29:05 +01:00
$i = 1 ;
$files = array ();
$name = $zip -> getNameIndex ( $i );
2012-08-14 20:20:52 +02:00
while ( ! empty ( $name ) ) {
2011-12-13 12:29:05 +01:00
$files [] = $name ;
$name = $zip -> getNameIndex ( $i );
$i += 1 ;
}
$zip -> close ();
2012-08-14 20:20:52 +02:00
// 2. copy them over
2011-12-13 12:29:05 +01:00
foreach ( $files as $file ) {
2016-10-03 20:55:04 +02:00
$target = $serendipity [ 'serendipityPath' ] . preg_replace ( '/[^\/]*/' , '' , $file , 1 ); # removes leading Serendipity[beta…]
2012-06-08 14:46:31 +02:00
if ( is_dir ( $updateDir . $file )) {
if ( ! file_exists ( $target )) {
$success = mkdir ( $target );
} else {
$success = true ;
}
} else {
2012-08-14 20:20:52 +02:00
$success = @ copy ( $updateDir . $file , $target );
2012-06-08 14:46:31 +02:00
}
2015-07-31 12:18:07 +02:00
if ( ! $success ) {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Error copying file ' . $updateDir . $file . ' to ' . $target . '</p>' );
2011-12-13 12:29:05 +01:00
return false ;
}
}
} else {
return false ;
}
return true ;
}
2012-08-14 20:20:52 +02:00
/**
* check write permissions
* @ param string updatePath
* @ return boolean
*/
2011-12-13 12:29:05 +01:00
function checkWritePermissions ( $update ) {
global $serendipity ;
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
$zip = new ZipArchive ;
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
if ( $zip -> open ( $update ) === true ) {
$i = 0 ;
$files = array ();
$name = $zip -> getNameIndex ( $i );
while ( ! empty ( $name )) {
$files [] = $name ;
$name = $zip -> getNameIndex ( $i );
$i += 1 ;
}
2015-07-31 12:18:07 +02:00
$zip -> close ();
2011-12-13 12:29:05 +01:00
foreach ( $files as $file ) {
$target = $serendipity [ 'serendipityPath' ] . substr ( $file , 12 );
2015-07-31 12:18:07 +02:00
if (( ! is_writable ( $target )) && file_exists ( $target )) {
2011-12-13 12:29:05 +01:00
return false ;
}
}
}
return true ;
}
2012-08-14 20:20:52 +02:00
/**
* show not writable
* @ param string updatePath
* @ return error
*/
2011-12-13 12:29:05 +01:00
function showNotWriteable ( $update ) {
global $serendipity ;
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
$zip = new ZipArchive ;
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
if ( $zip -> open ( $update ) === true ) {
$i = 0 ;
$files = array ();
$name = $zip -> getNameIndex ( $i );
while ( ! empty ( $name )) {
$files [] = $name ;
$name = $zip -> getNameIndex ( $i );
$i += 1 ;
}
2015-07-31 12:18:07 +02:00
$zip -> close ();
2011-12-13 12:29:05 +01:00
$notWritable = array ();
2016-10-26 15:07:46 +02:00
2011-12-13 12:29:05 +01:00
foreach ( $files as $file ) {
$target = $serendipity [ 'serendipityPath' ] . substr ( $file , 12 );
2015-07-31 12:34:19 +02:00
if (( ! is_writable ( $target )) && file_exists ( $target )) {
2011-12-13 12:29:05 +01:00
$notWriteable [] = $target ;
}
}
}
2013-04-27 10:37:11 +02:00
ob_start ();
2016-10-26 15:07:46 +02:00
echo '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Unpacking the update failed, because following files were not writeable:</p>' ;
2011-12-13 12:29:05 +01:00
echo " <ul> " ;
foreach ( $notWriteable as $file ) {
echo " <li> $file </li> " ;
}
echo " </ul> " ;
2013-04-27 10:37:11 +02:00
$write_error = ob_get_contents ();
ob_end_clean ();
$this -> show_message ( $write_error );
2011-12-13 12:29:05 +01:00
}
2012-08-14 20:20:52 +02:00
/**
* checks updates checksum file array with updates realfiles
* @ param string version
* @ return boolean
*/
2011-12-13 12:29:05 +01:00
function checkIntegrity ( $version ) {
global $serendipity ;
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
$updateDir = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version / " ;
$checksumFile = ( string ) $updateDir . " serendipity/checksums.inc.php " ;
2013-04-27 10:37:11 +02:00
2016-09-23 10:20:49 +02:00
if ( strpos ( $version , 'beta' ) !== FALSE ) {
return true ;
}
2011-12-13 12:29:05 +01:00
include_once $checksumFile ;
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
$checksums = $serendipity [ 'checksums_' . $version ];
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
foreach ( $checksums as $file => $checksum ) {
$check = serendipity_FTPChecksum ( $updateDir . " serendipity/ " . $file );
if ( $checksum != $check ) {
return false ;
}
}
return true ;
}
2012-08-14 20:20:52 +02:00
/**
* checks updates checksum file array with updates realfiles
* @ param string version
* @ return error
*/
2011-12-13 12:29:05 +01:00
function showChecksumErrors ( $version ) {
global $serendipity ;
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
$updateDir = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version / " ;
$checksumFile = ( string ) $updateDir . " serendipity/checksums.inc.php " ;
2013-04-27 10:37:11 +02:00
2011-12-13 12:29:05 +01:00
include_once $checksumFile ;
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
$checksums = $serendipity [ 'checksums_' . $version ];
2013-04-27 10:37:11 +02:00
$errors = array ();
2011-12-13 12:29:05 +01:00
foreach ( $checksums as $file => $checksum ) {
$check = serendipity_FTPChecksum ( $updateDir . " serendipity/ " . $file );
if ( $checksum != $check ) {
$errors [] = $updateDir . " serendipity/ " . $file ;
}
}
2013-04-27 10:37:11 +02:00
ob_start ();
2016-10-26 15:07:46 +02:00
echo '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Updating failed, because the integrity-test for the following files failed:</p>' ;
2011-12-13 12:29:05 +01:00
echo " <ul> " ;
foreach ( $errors as $file ) {
echo " <li> $file </li> " ;
}
echo " </ul> " ;
2013-04-27 10:37:11 +02:00
$integrity_error = ob_get_contents ();
ob_end_clean ();
$this -> show_message ( $integrity_error );
}
/**
* close the autoupdate progress page
* @ param bool 007 title ; - )
*/
function close_page ( $terminate = false ) {
echo <<< EOS
</ article >
</ div >
</ div >
2014-12-23 18:47:04 +01:00
EOS ;
2015-07-31 12:34:19 +02:00
if ( $terminate ) {
2014-12-23 18:47:04 +01:00
echo <<< EOS
2013-04-27 10:37:11 +02:00
</ body >
</ html >
EOS ;
2014-12-23 18:47:04 +01:00
}
2015-07-31 12:34:19 +02:00
if ( $terminate ) die ();
2011-12-13 12:29:05 +01:00
}
2012-08-14 20:20:52 +02:00
/**
2013-04-27 10:37:11 +02:00
* visit the rootpage to manually start the update installer process
2012-08-14 20:20:52 +02:00
* @ param string update messages
* @ return refresh page
*/
2013-04-27 10:37:11 +02:00
function doUpdate () {
2011-12-13 12:29:05 +01:00
global $serendipity ;
2012-08-14 20:20:52 +02:00
2014-12-23 18:47:04 +01:00
$msg = " Autoupdate successfully done! \\ nWe now refresh to Serendipity Installer! \\ n " ; // escape for js
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_success"><span class="icon-ok" aria-hidden="true"></span>Autoupdate successfully done - refresh to Serendipity Installer</p>' , 'Autoupdate' );
2013-04-27 10:37:11 +02:00
$this -> close_page ();
// this is working for me.... is it for you?
2015-07-31 12:18:07 +02:00
if ( die ( '<script type="text/javascript">alert("' . $msg . '"); window.location = "' . $serendipity [ 'serendipityHTTPPath' ] . '";</script>' . " \n </body> \n </html> " )) {
2012-08-14 20:20:52 +02:00
return ;
} else {
2015-07-31 12:34:19 +02:00
if ( ! headers_sent ()) {
if ( header ( 'Location: http://' . $_SERVER [ 'HTTP_HOST' ] . $serendipity [ 'serendipityHTTPPath' ])) exit ;
2012-08-14 20:20:52 +02:00
} else {
echo '<script type="text/javascript">' ;
echo ' window.location.href="' . $serendipity [ 'serendipityHTTPPath' ] . '"' ;
echo '</script>' . " \n " ;
echo '<noscript>' ;
echo ' <meta http-equiv="refresh" content="0;url=' . $serendipity [ 'serendipityHTTPPath' ] . '" />' ;
echo '</noscript>' ;
exit ;
}
}
2011-12-13 12:29:05 +01:00
}
2012-08-14 20:20:52 +02:00
/**
* empty a directory using the Standard PHP Library ( SPL ) iterator
* @ param string directory
*/
function empty_dir ( $dir ) {
2014-12-23 18:47:04 +01:00
if ( ! is_dir ( $dir )) return ;
try {
$_dir = new RecursiveDirectoryIterator ( $dir );
// NOTE: UnexpectedValueException thrown for PHP >= 5.3
} catch ( Exception $e ) {
return ;
}
$iterator = new RecursiveIteratorIterator ( $_dir , RecursiveIteratorIterator :: CHILD_FIRST );
//$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
2012-08-14 20:20:52 +02:00
foreach ( $iterator as $file ) {
if ( $file -> isFile ()) {
unlink ( $file -> __toString ());
} else {
2013-04-27 10:37:11 +02:00
@ rmdir ( $file -> __toString ());
2012-08-14 20:20:52 +02:00
}
}
}
/**
* delete all cache - files in cache templates_c to prevent display - errors after update
* @ param string version
* @ return boolean
*/
2013-04-27 10:37:11 +02:00
function cleanTemplatesC ( $version , $finish ) {
2012-06-08 14:46:31 +02:00
global $serendipity ;
2012-08-14 20:20:52 +02:00
$zip = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version .zip " ;
$zipDir = ( string ) $serendipity [ 'serendipityPath' ] . 'templates_c/' . " serendipity- $version " ;
2013-04-27 10:37:11 +02:00
2012-08-14 20:20:52 +02:00
// leave rm zip untouched here as not causing any errors
2015-07-31 12:34:19 +02:00
#unlink($zip);// if (unlink($zip)) { else error note?
2016-10-26 15:07:46 +02:00
#$this->show_message('<p class="msg_success"><span class="icon-ok" aria-hidden="true"></span>Removing the zip file in templates_c done</p>');
2012-08-14 20:20:52 +02:00
// As trying to remove a directory that php is still using, we use open/closedir($handle) to be sure
2015-07-31 12:18:07 +02:00
if ( $handle = opendir ( $zipDir )) {
2012-08-14 20:20:52 +02:00
$this -> empty_dir ( $zipDir );
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_success"><span class="icon-ok" aria-hidden="true"></span>Removing all files in ' . $zipDir . ' done</p>' );
2012-08-14 20:20:52 +02:00
closedir ( $handle );
}
2015-07-31 12:18:07 +02:00
if ( rmdir ( $zipDir )) {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_success"><span class="icon-ok" aria-hidden="true"></span>Removing the empty directory: ' . $zipDir . ' done</p>' );
2013-04-27 10:37:11 +02:00
} else {
2016-10-26 15:07:46 +02:00
$this -> show_message ( '<p class="msg_error"><span class="icon-error" aria-hidden="true"></span>Removing the empty directory: ' . $zipDir . ' failed!</p>' );
2012-08-14 20:20:52 +02:00
}
2013-04-27 10:37:11 +02:00
// We clear all compiles smarty template files in templates_c which only leaves the page we are on: /serendipity/templates/default/admin/index.tpl
if ( $finish ) {
2016-10-26 15:07:46 +02:00
// We have to reduce this call() = all tpl files, to clear the blogs template only, to not have the following automated recompile, force the servers memory
2013-04-27 10:37:11 +02:00
// to get exhausted, when using serendipity_event_gravatar plugin, which can eat up some MB...
2015-07-24 16:38:46 +02:00
if ( method_exists ( $serendipity [ 'smarty' ], 'clearCompiledTemplate' )) { // SMARTY 3
if ( $serendipity [ 'smarty' ] -> clearCompiledTemplate ( null , $serendipity [ 'template' ])) {
2013-04-27 10:37:11 +02:00
return true ;
}
}
2015-07-24 16:38:46 +02:00
if ( method_exists ( $serendipity [ 'smarty' ], 'clear_compiled_tpl' )) { // SMARTY 2
if ( $serendipity [ 'smarty' ] -> clear_compiled_tpl ( null , $serendipity [ 'template' ])) {
2013-04-27 10:37:11 +02:00
return true ;
}
2012-08-14 20:20:52 +02:00
}
2012-06-08 14:46:31 +02:00
}
}
2012-08-14 20:20:52 +02:00
/**
* debug
* @ param string msg
*/
2011-12-13 12:29:05 +01:00
function debugMsg ( $msg ) {
2012-08-14 20:20:52 +02:00
global $serendipity ;
$this -> debug_fp = @ fopen ( $serendipity [ 'serendipityPath' ] . 'templates_c/autoupdate.log' , 'a' );
2015-07-31 12:18:07 +02:00
if ( ! $this -> debug_fp ) {
2012-08-14 20:20:52 +02:00
return false ;
}
2015-07-31 12:18:07 +02:00
if ( empty ( $msg )) {
2012-08-14 20:20:52 +02:00
fwrite ( $this -> debug_fp , " failure \n " );
} else {
fwrite ( $this -> debug_fp , print_r ( $msg , true ) );
}
fclose ( $this -> debug_fp );
}
2011-12-13 12:29:05 +01:00
}
2016-10-26 15:07:46 +02:00
/* vim: set sts=4 ts=4 expandtab : */