/** * Main - the one that actually does something */ public function execute() { $this->log('=== MDEPLOY EXECUTION START ==='); // Authorize access. None in CLI. Passphrase in HTTP. $this->authorize(); // Asking for help in the CLI mode. if ($this->input->get_option('help')) { $this->output->help(); $this->done(self::EXIT_HELP); } if ($this->input->get_option('upgrade')) { $this->log('Plugin upgrade requested'); // Fetch the ZIP file into a temporary location. $source = $this->input->get_option('package'); $target = $this->target_location($source); $this->log('Downloading package ' . $source); if ($this->download_file($source, $target)) { $this->log('Package downloaded into ' . $target); } else { $this->log('cURL error ' . $this->curlerrno . ' ' . $this->curlerror); $this->log('Unable to download the file from ' . $source . ' into ' . $target); throw new download_file_exception('Unable to download the package'); } // Compare MD5 checksum of the ZIP file $md5remote = $this->input->get_option('md5'); $md5local = md5_file($target); if ($md5local !== $md5remote) { $this->log('MD5 checksum failed. Expected: ' . $md5remote . ' Got: ' . $md5local); throw new checksum_exception('MD5 checksum failed'); } $this->log('MD5 checksum ok'); // Check that the specified typeroot is within the current site's dirroot. $plugintyperoot = $this->input->get_option('typeroot'); if (strpos(realpath($plugintyperoot), realpath($this->get_env('dirroot'))) !== 0) { throw new backup_folder_exception('Unable to backup the current version of the plugin (typeroot is invalid)'); } // Backup the current version of the plugin $pluginname = $this->input->get_option('name'); $sourcelocation = $plugintyperoot . '/' . $pluginname; $backuplocation = $this->backup_location($sourcelocation); $this->log('Current plugin code location: ' . $sourcelocation); $this->log('Moving the current code into archive: ' . $backuplocation); if (file_exists($sourcelocation)) { // We don't want to touch files unless we are pretty sure it would be all ok. if (!$this->move_directory_source_precheck($sourcelocation)) { throw new backup_folder_exception('Unable to backup the current version of the plugin (source precheck failed)'); } if (!$this->move_directory_target_precheck($backuplocation)) { throw new backup_folder_exception('Unable to backup the current version of the plugin (backup precheck failed)'); } // Looking good, let's try it. if (!$this->move_directory($sourcelocation, $backuplocation, true)) { throw new backup_folder_exception('Unable to backup the current version of the plugin (moving failed)'); } } else { // Upgrading missing plugin - this happens often during upgrades. if (!$this->create_directory_precheck($sourcelocation)) { throw new filesystem_exception('Unable to prepare the plugin location (cannot create new directory)'); } } // Unzip the plugin package file into the target location. $this->unzip_plugin($target, $plugintyperoot, $sourcelocation, $backuplocation); $this->log('Package successfully extracted'); // Redirect to the given URL (in HTTP) or exit (in CLI). $this->done(); } else { if ($this->input->get_option('install')) { $this->log('Plugin installation requested'); $plugintyperoot = $this->input->get_option('typeroot'); $pluginname = $this->input->get_option('name'); $source = $this->input->get_option('package'); $md5remote = $this->input->get_option('md5'); if (strpos(realpath($plugintyperoot), realpath($this->get_env('dirroot'))) !== 0) { throw new backup_folder_exception('Unable to prepare the plugin location (typeroot is invalid)'); } // Check if the plugin location if available for us. $pluginlocation = $plugintyperoot . '/' . $pluginname; $this->log('New plugin code location: ' . $pluginlocation); if (file_exists($pluginlocation)) { throw new filesystem_exception('Unable to prepare the plugin location (directory already exists)'); } if (!$this->create_directory_precheck($pluginlocation)) { throw new filesystem_exception('Unable to prepare the plugin location (cannot create new directory)'); } // Fetch the ZIP file into a temporary location. $target = $this->target_location($source); $this->log('Downloading package ' . $source); if ($this->download_file($source, $target)) { $this->log('Package downloaded into ' . $target); } else { $this->log('cURL error ' . $this->curlerrno . ' ' . $this->curlerror); $this->log('Unable to download the file'); throw new download_file_exception('Unable to download the package'); } // Compare MD5 checksum of the ZIP file $md5local = md5_file($target); if ($md5local !== $md5remote) { $this->log('MD5 checksum failed. Expected: ' . $md5remote . ' Got: ' . $md5local); throw new checksum_exception('MD5 checksum failed'); } $this->log('MD5 checksum ok'); // Unzip the plugin package file into the plugin location. $this->unzip_plugin($target, $plugintyperoot, $pluginlocation, false); $this->log('Package successfully extracted'); // Redirect to the given URL (in HTTP) or exit (in CLI). $this->done(); } } // Print help in CLI by default. $this->output->help(); $this->done(self::EXIT_UNKNOWN_ACTION); }
/** * Main - the one that actually does something */ public function execute() { $this->log('=== MDEPLOY EXECUTION START ==='); // Authorize access. None in CLI. Passphrase in HTTP. $this->authorize(); // Asking for help in the CLI mode. if ($this->input->get_option('help')) { $this->output->help(); $this->done(self::EXIT_HELP); } if ($this->input->get_option('upgrade')) { $this->log('Plugin upgrade requested'); // Fetch the ZIP file into a temporary location. $source = $this->input->get_option('package'); $target = $this->target_location($source); $this->log('Downloading package '.$source); if ($this->download_file($source, $target)) { $this->log('Package downloaded into '.$target); } else { $this->log('cURL error ' . $this->curlerrno . ' ' . $this->curlerror); $this->log('Unable to download the file'); throw new download_file_exception('Unable to download the package'); } // Compare MD5 checksum of the ZIP file $md5remote = $this->input->get_option('md5'); $md5local = md5_file($target); if ($md5local !== $md5remote) { $this->log('MD5 checksum failed. Expected: '.$md5remote.' Got: '.$md5local); throw new checksum_exception('MD5 checksum failed'); } $this->log('MD5 checksum ok'); // Backup the current version of the plugin $plugintyperoot = $this->input->get_option('typeroot'); $pluginname = $this->input->get_option('name'); $sourcelocation = $plugintyperoot.'/'.$pluginname; $backuplocation = $this->backup_location($sourcelocation); $this->log('Current plugin code location: '.$sourcelocation); $this->log('Moving the current code into archive: '.$backuplocation); // We don't want to touch files unless we are pretty sure it would be all ok. if (!$this->move_directory_source_precheck($sourcelocation)) { throw new backup_folder_exception('Unable to backup the current version of the plugin (source precheck failed)'); } if (!$this->move_directory_target_precheck($backuplocation)) { throw new backup_folder_exception('Unable to backup the current version of the plugin (backup precheck failed)'); } // Looking good, let's try it. if (!$this->move_directory($sourcelocation, $backuplocation, true)) { throw new backup_folder_exception('Unable to backup the current version of the plugin (moving failed)'); } // Unzip the plugin package file into the target location. $this->unzip_plugin($target, $plugintyperoot, $sourcelocation, $backuplocation); $this->log('Package successfully extracted'); // Redirect to the given URL (in HTTP) or exit (in CLI). $this->done(); } else if ($this->input->get_option('install')) { // Installing a new plugin not implemented yet. } // Print help in CLI by default. $this->output->help(); $this->done(self::EXIT_UNKNOWN_ACTION); }