From 7ab126ebec8874eddb6ab211064ed7c9ec188cf6 Mon Sep 17 00:00:00 2001 From: Ozh Date: Wed, 24 Oct 2012 13:33:35 +0000 Subject: [PATCH] Maintenance mode (interrupt everything is activated). Closes issue 221. git-svn-id: http://yourls.googlecode.com/svn/trunk@785 12232710-3e20-11de-b438-597f59cd7555 --- admin/upgrade.php | 1 + includes/functions-install.php | 25 +++++++++++++++ includes/functions.php | 57 +++++++++++++++++----------------- includes/load-yourls.php | 6 ++-- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/admin/upgrade.php b/admin/upgrade.php index 249f30b..50b19fd 100644 --- a/admin/upgrade.php +++ b/admin/upgrade.php @@ -1,4 +1,5 @@ $success_msg, 'error' => $error_msg ); } + + +// Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise +function yourls_maintenance_mode( $maintenance = true ) { + + $file = YOURLS_ABSPATH . '/.maintenance' ; + + // Turn maintenance mode on : create .maintenance file + if ( (bool)$maintenance ) { + if ( ! ( $fp = @fopen( $file, 'w' ) ) ) + return false; + + $maintenance_string = ''; + @fwrite( $fp, $maintenance_string ); + @fclose( $fp ); + @chmod( $file, 0644 ); // Read and write for owner, read for everybody else + + // Not sure why the fwrite would fail if the fopen worked... Just in case + return( is_readable( $file ) ); + + // Turn maintenance mode off : delete the .maintenance file + } else { + return @unlink($file); + } +} \ No newline at end of file diff --git a/includes/functions.php b/includes/functions.php index 3000f06..c8312f2 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1563,35 +1563,6 @@ function yourls_get_remote_title( $url ) { return yourls_apply_filter( 'get_remote_title', $title, $url ); } -// Check for maintenance mode that will shortcut everything -function yourls_check_maintenance_mode() { - - // TODO: all cases that always display the sites (is_admin but not is_ajax?) - if( 1 ) - return; - - // first case: /user/maintenance.php file - if( file_exists( YOURLS_USERDIR.'/maintenance.php' ) ) { - include( YOURLS_USERDIR.'/maintenance.php' ); - die(); - } - - // second case: option in DB - if( yourls_get_option( 'maintenance_mode' ) !== false ) { - require_once( YOURLS_INC.'/functions-html.php' ); - $title = 'Service temporarily unavailable'; - $message = 'Our service is currently undergoing scheduled maintenance.

-

Things should not last very long, thank you for your patience and please excuse the inconvenience'; - yourls_die( $message, $title , 503 ); - } - -} - -// Toggle maintenance mode -function yourls_maintenance_mode( $maintenance = true ) { - yourls_update_option( 'maintenance_mode', (bool)$maintenance ); -} - // Quick UA check for mobile devices. Return boolean. function yourls_is_mobile_device() { // Strings searched @@ -1710,3 +1681,31 @@ function yourls_favicon( $echo = true ) { echo $favicon; return $favicon; } + +// Check for maintenance mode. If yes, die. See yourls_maintenance_mode(). Stolen from WP. +function yourls_check_maintenance_mode() { + + $file = YOURLS_ABSPATH . '/.maintenance' ; + if ( !file_exists( $file ) || defined( 'YOURLS_UPGRADING' ) || defined( 'YOURLS_INSTALLING' ) ) + return; + + global $maintenance_start; + + include( $file ); + // If the $maintenance_start timestamp is older than 10 minutes, don't die. + if ( ( time() - $maintenance_start ) >= 600 ) + return; + + // Use any /user/maintenance.php file + if( file_exists( YOURLS_USERDIR.'/maintenance.php' ) ) { + include( YOURLS_USERDIR.'/maintenance.php' ); + die(); + } + + // https://www.youtube.com/watch?v=Xw-m4jEY-Ns + $title = 'Service temporarily unavailable'; + $message = 'Our service is currently undergoing scheduled maintenance.

+

Things should not last very long, thank you for your patience and please excuse the inconvenience'; + yourls_die( $message, $title , 503 ); + +} diff --git a/includes/load-yourls.php b/includes/load-yourls.php index a27df88..afaaefc 100644 --- a/includes/load-yourls.php +++ b/includes/load-yourls.php @@ -115,6 +115,9 @@ // Allow early inclusion of a cache layer if( file_exists( YOURLS_USERDIR.'/cache.php' ) ) require_once( YOURLS_USERDIR.'/cache.php' ); + +// Check if we are in maintenance mode - if yes, it will die here. +yourls_check_maintenance_mode(); // If request for an admin page is http:// and SSL is required, redirect if( yourls_is_admin() && yourls_needs_ssl() && !yourls_is_ssl() ) { @@ -143,9 +146,6 @@ // Core now loaded yourls_do_action( 'init' ); // plugins can't see this, not loaded yet -// Check if we are in maintenance mode -yourls_check_maintenance_mode(); - // Check if need to redirect to install procedure if( !yourls_is_installed() && ( !defined('YOURLS_INSTALLING') || YOURLS_INSTALLING != true ) ) { yourls_redirect( yourls_admin_url( 'install.php' ), 302 ); -- 2.45.0