Skip to content

Commit

Permalink
Merge pull request osTicket#500 from protich/feature/upgrader_revisited
Browse files Browse the repository at this point in the history
Upgrader revisited
  • Loading branch information
protich committed Mar 11, 2013
2 parents 6934f91 + 293d845 commit 731baca
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 59 deletions.
2 changes: 1 addition & 1 deletion ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ function clientLoginPage($msg='Unauthorized') {
url_get('^client', 'client')
))
);
print $dispatcher->resolve($_SERVER['PATH_INFO']);
print $dispatcher->resolve($ost->get_path_info());
?>
2 changes: 1 addition & 1 deletion api/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
);

# Call the respective function
print $dispatcher->resolve($_SERVER['PATH_INFO']);
print $dispatcher->resolve($ost->get_path_info());
?>
44 changes: 37 additions & 7 deletions include/class.osticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,43 @@ function purgeLogs() {

return true;
}
/*
* Util functions
*
*/

function get_var($index, $vars, $default='', $type=null) {

if(is_array($vars)
&& array_key_exists($index, $vars)
&& (!$type || gettype($vars[$index])==$type))
return $vars[$index];

return $default;
}

function get_db_input($index, $vars, $quote=true) {
return db_input($this->get_var($index, $vars), $quote);
}

function get_path_info() {
if(isset($_SERVER['PATH_INFO']))
return $_SERVER['PATH_INFO'];

if(isset($_SERVER['ORIG_PATH_INFO']))
return $_SERVER['ORIG_PATH_INFO'];

//TODO: conruct possible path info.

return null;
}

/* returns true if script is being executed via commandline */
function is_cli() {
return (!strcasecmp(substr(php_sapi_name(), 0, 3), 'cli')
|| (!$_SERVER['REQUEST_METHOD'] && !$_SERVER['HTTP_HOST']) //Fallback when php-cgi binary is used via cli
);
}

/**** static functions ****/
function start($configId) {
Expand All @@ -338,13 +375,6 @@ function start($configId) {

return $ost;
}

/* is_cli */
function is_cli() {
return (!strcasecmp(substr(php_sapi_name(), 0, 3), 'cli')
|| (!$_SERVER['REQUEST_METHOD'] && !$_SERVER['HTTP_HOST']) //Fallback when php-cgi binary is used via cli
);
}
}

?>
79 changes: 53 additions & 26 deletions include/class.upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ class Upgrader extends SetupWizard {
var $sqldir;
var $signature;

var $state;
var $mode;

function Upgrader($signature, $prefix, $sqldir) {

$this->signature = $signature;
$this->shash = substr($signature, 0, 8);
$this->prefix = $prefix;
$this->sqldir = $sqldir;
$this->errors = array();
$this->mode = 'ajax'; //

//Disable time limit if - safe mode is set.
if(!ini_get('safe_mode'))
Expand All @@ -38,15 +41,17 @@ function Upgrader($signature, $prefix, $sqldir) {
//Init persistent state of upgrade.
$this->state = &$_SESSION['ost_upgrader']['state'];

$this->mode = &$_SESSION['ost_upgrader']['mode'];

//Init the task Manager.
if(!isset($_SESSION['ost_upgrader'][$this->getShash()]))
$_SESSION['ost_upgrader'][$this->getShash()]['tasks']=array();

//Tasks to perform - saved on the session.
$this->tasks = &$_SESSION['ost_upgrader'][$this->getShash()]['tasks'];

//Database migrater
$this->migrater = new DatabaseMigrater($this->signature, SCHEMA_SIGNATURE, $this->sqldir);
//Database migrater
$this->migrater = null;
}

function onError($error) {
Expand Down Expand Up @@ -87,7 +92,7 @@ function getSchemaSignature() {
}

function getShash() {
return $this->shash;
return substr($this->getSchemaSignature(), 0, 8);
}

function getTablePrefix() {
Expand All @@ -106,13 +111,31 @@ function setState($state) {
$this->state = $state;
}

function getMode() {
return $this->mode;
}

function setMode($mode) {
$this->mode = $mode;
}

function getMigrater() {
if(!$this->migrater)
$this->migrater = new DatabaseMigrater($this->signature, SCHEMA_SIGNATURE, $this->sqldir);

return $this->migrater;
}

function getPatches() {
return $this->migrater->getPatches();
$patches = array();
if($this->getMigrater())
$patches = $this->getMigrater()->getPatches();

return $patches;
}

function getNextPatch() {
$p = $this->getPatches();
return (count($p)) ? $p[0] : false;
return (($p=$this->getPatches()) && count($p)) ? $p[0] : false;
}

function getNextVersion() {
Expand Down Expand Up @@ -140,7 +163,7 @@ function getNextAction() {
$action='Upgrade osTicket to '.$this->getVersion();
if($this->getNumPendingTasks() && ($task=$this->getNextTask())) {
$action = $task['desc'];
if($task['status']) //Progress report...
if($task['status']) //Progress report...
$action.=' ('.$task['status'].')';
} elseif($this->isUpgradable() && ($nextversion = $this->getNextVersion())) {
$action = "Upgrade to $nextversion";
Expand All @@ -161,9 +184,9 @@ function getPendingTasks() {
foreach($tasks as $k => $task) {
if(!$task['done'])
$pending[$k] = $task;
}
}
}

return $pending;
}

Expand Down Expand Up @@ -198,7 +221,11 @@ function doTasks() {
if(!($tasks=$this->getPendingTasks()))
return true; //Nothing to do.

$ost->logDebug('Upgrader', sprintf('There are %d pending upgrade tasks', count($tasks)));
$c = count($tasks);
$ost->logDebug(
sprintf('Upgrader - %s (%d pending tasks).', $this->getShash(), $c),
sprintf('There are %d pending upgrade tasks for %s patch', $c, $this->getShash())
);
$start_time = Misc::micro_time();
foreach($tasks as $k => $task) {
//TODO: check time used vs. max execution - break if need be
Expand All @@ -211,7 +238,7 @@ function doTasks() {

return $this->getPendingTasks();
}

function upgrade() {
global $ost;

Expand All @@ -227,18 +254,20 @@ function upgrade() {
if (!$this->load_sql_file($patch, $this->getTablePrefix()))
return false;

//clear previous patch info -
//clear previous patch info -
unset($_SESSION['ost_upgrader'][$this->getShash()]);

$phash = substr(basename($patch), 0, 17);
$shash = substr($phash, 9, 8);

//Log the patch info
$logMsg = "Patch $phash applied ";
$logMsg = "Patch $phash applied successfully ";
if(($info = $this->readPatchInfo($patch)) && $info['version'])
$logMsg.= ' ('.$info['version'].') ';

$ost->logDebug('Upgrader - Patch applied', $logMsg);

$ost->logDebug("Upgrader - $shash applied", $logMsg);
$this->signature = $shash; //Update signature to the *new* HEAD

//Check if the said patch has scripted tasks
if(!($tasks=$this->getTasksForPatch($phash))) {
//Break IF elapsed time is greater than 80% max time allowed.
Expand All @@ -250,16 +279,14 @@ function upgrade() {
}

//We have work to do... set the tasks and break.
$shash = substr($phash, 9, 8);
$_SESSION['ost_upgrader'][$shash]['tasks'] = $tasks;
$_SESSION['ost_upgrader'][$shash]['state'] = 'upgrade';

$ost->logDebug('Upgrader', sprintf('Found %d tasks to be executed for %s',
count($tasks), $shash));
break;

}

//Reset the migrater
$this->migrater = null;

return true;

}
Expand All @@ -286,9 +313,9 @@ function getTasksForPatch($phash) {
break;
}

//Check IF SQL cleanup exists.
//Check IF SQL cleanup exists.
$file=$this->getSQLDir().$phash.'.cleanup.sql';
if(file_exists($file))
if(file_exists($file))
$tasks[] = array('func' => 'cleanup',
'desc' => 'Post-upgrade cleanup!',
'phash' => $phash);
Expand All @@ -306,7 +333,7 @@ function cleanup($taskId) {
if(!file_exists($file)) //No cleanup script.
return 0;

//We have a cleanup script ::XXX: Don't abort on error?
//We have a cleanup script ::XXX: Don't abort on error?
if($this->load_sql_file($file, $this->getTablePrefix(), false, true))
return 0;

Expand All @@ -317,7 +344,7 @@ function cleanup($taskId) {

function migrateAttachments2DB($taskId) {
global $ost;

if(!($max_time = ini_get('max_execution_time')))
$max_time = 30; //Default to 30 sec batches.

Expand All @@ -330,7 +357,7 @@ function migrateAttachments2DB($taskId) {

function migrateSessionFile2DB($taskId) {
# How about 'dis for a hack?
osTicketSession::write(session_id(), session_encode());
osTicketSession::write(session_id(), session_encode());
return 0;
}

Expand Down
24 changes: 19 additions & 5 deletions include/upgrader/upgrade.inc.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php
if(!defined('OSTSCPINC') || !$thisstaff || !$thisstaff->isAdmin()) die('Access Denied');

//See if we need to switch the mode of upgrade...e.g from ajax (default) to manual
if(($mode = $ost->get_var('m', $_GET)) && $mode!=$upgrader->getMode()) {
//Set Persistent mode/
$upgrader->setMode($mode);
//Log warning about ajax calls - most likely culprit is AcceptPathInfo directive.
if($mode=='manual')
$ost->logWarning('Ajax calls are failing',
'Make sure your server has AcceptPathInfo directive set to "ON" or get technical help');
}

$action=$upgrader->getNextAction();
?>
<h2>osTicket Upgrade</h2>
Expand All @@ -9,7 +20,7 @@
<p>Thank you for taking the time to upgrade your osTicket intallation!</p>
<p>Please don't cancel or close the browser, any errors at this stage will be fatal.</p>
</div>
<h2><?php echo $action ?></h2>
<h2 id="task"><?php echo $action ?></h2>
<p>The upgrade wizard will now attempt to upgrade your database and core settings!</p>
<ul>
<li>Database enhancements</li>
Expand All @@ -20,8 +31,9 @@
<form method="post" action="upgrade.php" id="upgrade">
<?php csrf_token(); ?>
<input type="hidden" name="s" value="upgrade">
<input type="hidden" id="mode" name="m" value="<?php echo $upgrader->getMode(); ?>">
<input type="hidden" name="sh" value="<?php echo $upgrader->getSchemaSignature(); ?>">
<input class="btn" type="submit" name="submit" value="Do It Now!">
<input class="btn" type="submit" name="submit" value="Upgrade Now!">
</form>
</div>
</div>
Expand All @@ -33,9 +45,11 @@
</div>
<div class="clear"></div>
<div id="upgrading">
<h4><?php echo $action; ?></h4>
<h4 id="action"><?php echo $action; ?></h4>
Please wait... while we upgrade your osTicket installation!
<div id="msg" style="font-weight: bold;padding-top:10px;">Smile!</div>
<div id="msg" style="font-weight: bold;padding-top:10px;">
<?php echo sprintf("%s - Relax!", $thisstaff->getFirstName()); ?>
</div>
</div>
</div>
<div class="clear"></div>`
<div class="clear"></div>
4 changes: 2 additions & 2 deletions scp/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function staffLoginPage($msg='Unauthorized') {
ini_set('display_errors','0'); //Disable error display
ini_set('display_startup_errors','0');

//TODO: disable direct access via the browser? i,e All request must have REFER?
//TODO: disable direct access via the browser? i,e All request must have REFER?
if(!defined('INCLUDE_DIR')) Http::response(500, 'Server configuration error');

require_once INCLUDE_DIR.'/class.dispatcher.php';
Expand Down Expand Up @@ -65,5 +65,5 @@ function staffLoginPage($msg='Unauthorized') {
);

# Call the respective function
print $dispatcher->resolve($_SERVER['PATH_INFO']);
print $dispatcher->resolve($ost->get_path_info());
?>
Loading

0 comments on commit 731baca

Please sign in to comment.