| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Victor Dubiniuk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2015, ownCloud, Inc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license AGPL-3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This code is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * it under the terms of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * as published by the Free Software Foundation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * You should have received a copy of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | namespace Owncloud\Updater\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Symfony\Component\Console\Question\ChoiceQuestion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use GuzzleHttp\Event\ProgressEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Owncloud\Updater\Utils\Fetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Owncloud\Updater\Utils\Feed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Owncloud\Updater\Utils\ConfigReader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use \Owncloud\Updater\Controller\DownloadController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | class DetectCommand extends Command { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @var Fetcher $fetcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	protected $fetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @var ConfigReader $configReader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	protected $configReader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	protected $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * @param Fetcher $fetcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @param ConfigReader $configReader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	public function __construct(Fetcher $fetcher, ConfigReader $configReader){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$this->fetcher = $fetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$this->configReader = $configReader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	protected function configure(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 				->setName('upgrade:detect') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 				->setDescription('Detect | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | - 1. currently existing code,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | - 2. version in config.php,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | - 3. online available verison. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | (ASK) what to do? (download, upgrade, abort, …)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 						'exit-if-none', null, InputOption::VALUE_NONE, 'exit with non-zero status code if new version is not found' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 				) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 				->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 						'only-check', null, InputOption::VALUE_NONE, 'Only check if update is available' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 				) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	protected function execute(InputInterface $input, OutputInterface $output){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$registry = $this->container['utils.registry']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$registry->set('feed', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$locator = $this->container['utils.locator']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$fsHelper = $this->container['utils.filesystemhelper']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$downloadController = new DownloadController($this->fetcher, $registry, $fsHelper); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			$currentVersion = $this->configReader->getByPath('system.version'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			if (!strlen($currentVersion)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 				throw new \UnexpectedValueException('Could not detect installed version.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			$this->getApplication()->getLogger()->info('ownCloud ' . $currentVersion . ' found'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			$output->writeln('Current version is ' . $currentVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			$feedData = $downloadController->checkFeed(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			if (!$feedData['success']){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 				// Network errors, etc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 				$output->writeln("Can't fetch feed."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 				$output->writeln($feedData['exception']->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 				$this->getApplication()->logException($feedData['exception']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 				// Return a number to stop the queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 				return $input->getOption('exit-if-none') ? 4 : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$feed = $feedData['data']['feed']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			if (!$feed->isValid()){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 				// Feed is empty. Means there are no updates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				$output->writeln('No updates found online.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 				return $input->getOption('exit-if-none') ? 4 : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			$registry->set('feed', $feed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			$output->writeln($feed->getVersionString() . ' is found online'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			if ($input->getOption('only-check')){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 				return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			$action = $this->ask($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			if ($action === 'abort'){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 				$output->writeln('Exiting on user command.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 				return 128; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$this->output = $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			$packageData = $downloadController->downloadOwncloud([$this, 'progress']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			if (!$packageData['success']){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 				$registry->set('feed', null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 				throw $packageData['exception']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			if ($action === 'download'){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 				$output->writeln('Downloading has been completed. Exiting.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 				return 64; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		} catch (\Exception $e){ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  | 			$this->getApplication()->getLogger()->error($e->getMessage()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  | 			return 2; | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  | 	 * Ask what to do | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  | 	 * @param InputInterface $input | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  | 	 * @param OutputInterface $output | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  | 	 * @return int | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  | 	public function ask(InputInterface $input, OutputInterface $output){ | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  | 		$helper = $this->getHelper('question'); | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  | 		$question = new ChoiceQuestion( | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  | 			'What would you do next?', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 			['download', 'upgrade', 'abort'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 			'1' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		$action = $helper->ask($input, $output, $question); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		return $action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 * Callback to output download progress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 * @param ProgressEvent $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	public function progress(ProgressEvent $e){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		$percentString = ''; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		if ($e->downloadSize){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 			$percent = intval(100 * $e->downloaded / $e->downloadSize ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 			$percentString = $percent . '%'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 			$this->output->write( 'Downloaded ' . $percentString . ' (' . $e->downloaded . ' of ' . $e->downloadSize . ")\r"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 175 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.