/**
  * Upgrade the application code to the latest version.
  * @throws Exception
  * @param bool $verify_updatable Whether or not to verify if installation is updatable, defaults to false
  * @return array Backup file information
  */
 public function runUpdate($file_path, $verify_updatable = false)
 {
     $app_dir = preg_replace("/\\/_lib\\/controller/", '', $file_path);
     // do we have the disk space we need?
     $disk_util = new AppUpgraderDiskUtil($app_dir);
     $disk_space_megs = $disk_util->getAvailableDiskSpace();
     // do we have the perms to do what we need?
     $disk_util->validateUpdatePermissions($app_dir);
     // do we need to update?
     $update_client = new AppUpgraderClient();
     $update_info = $update_client->getLatestVersionInfo();
     require dirname(__FILE__) . '/../../install/version.php';
     $version = Config::GetInstance()->getvalue('THINKUP_VERSION');
     if ($update_info['version'] < $version) {
         throw new Exception("You are running the latest version of ThinkUp.");
     }
     if ($verify_updatable == true) {
         return array('latest_version' => $update_info['version']);
     }
     // download zip...
     $update_zip_data = $update_client->getLatestVersionZip($update_info['url']);
     $update_zip = $disk_util->writeZip($update_zip_data);
     $zip = new ZipArchive();
     $open_result = $zip->open($update_zip);
     if ($open_result !== true) {
         unlink($update_zip);
         throw new Exception("Unable to extract " . $update_zip . ". ZipArchive::open failed with error code " . $open_result);
     }
     $num_files = $zip->numFiles;
     if ($num_files < 1) {
         unlink($update_zip);
         throw new Exception("Unable to extract " . $update_zip . ". ZipArchive->numFiles is " . $num_files);
     }
     $backup_file_info = array();
     $backup_file_info = $disk_util->backupInstall();
     $disk_util->deleteOldInstall();
     $data_path = FileDataManager::getDataPath();
     if ($zip->extractTo($data_path) !== true) {
         throw new Exception("Unable to extract new files into {$app_dir}: " . $zip->getStatusString());
     } else {
         $new_version_dir = $data_path . 'thinkup';
         $disk_util->recurseCopy($new_version_dir, $app_dir);
         // delete install files
         $disk_util->deleteDir($new_version_dir);
         unlink($update_zip);
     }
     //replace config file
     copy($backup_file_info['config'], "{$app_dir}/config.inc.php");
     return $backup_file_info;
 }
 public function testNotEnoughAvailableFileSpace()
 {
     // More disk space than is available
     AppUpgraderDiskUtil::$DISK_SPACE_NEEDED = disk_free_space(dirname(__FILE__)) + 1024 * 1024 * 10;
     $upgrade_controller = new UpgradeApplicationController(true);
     try {
         $upgrade_controller->runUpdate($this->test_web_dir);
         $this->fail("Should throw an exception...");
     } catch (Exception $e) {
         $this->assertPattern('/There is not enough free disk space to perform an update/', $e->getMessage());
     }
     AppUpgraderDiskUtil::$DISK_SPACE_NEEDED = 104857600;
     // set back to 100 MB
 }
 /**
  * Set the amount of disk space needed in bytes.
  * @param $bytes
  */
 public function setDiskSpaceNeeded($bytes)
 {
     self::$DISK_SPACE_NEEDED = $bytes;
 }