| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author Victor Dubiniuk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (c) 2015, ownCloud, Inc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license AGPL-3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * This code is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * it under the terms of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * as published by the Free Software Foundation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * You should have received a copy of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | namespace Owncloud\Updater\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\Console\Input\StringInput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\Console\Output\BufferedOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Owncloud\Updater\Formatter\HtmlOutputFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Owncloud\Updater\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use League\Plates\Engine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use League\Plates\Extension\Asset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class IndexController { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	/** @var \Pimple\Container */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	protected $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** @var Request */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	protected $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	/** @var string $command */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	protected $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	public function __construct(\Pimple\Container $container, $request = null){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		if (is_null($request)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			$this->request = new Request(['post' => $_POST]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			$this->request = $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$this->command = $this->request->postParameter('command'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	public function dispatch(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		if (is_null($this->command)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			$templates = new Engine('/home/deo/public_html/updater/src/Resources/views/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			$templates->loadExtension(new Asset('/home/deo/public_html/updater/pub/', false)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			// TODO: Check for user permissions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			//$content = $templates->render('partials/login', ['title' => 'Login Required']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			$content = $templates->render('partials/inner', ['title' => 'Updater']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$content = json_encode($this->ajaxAction(), JSON_UNESCAPED_SLASHES); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 	public function ajaxAction(){ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		$application = $this->container['application']; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		$input = new StringInput($this->command); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		$input->setInteractive(false); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		$output = new BufferedOutput(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 		$formatter = $output->getFormatter(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		$formatter->setDecorated(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 		$output->setFormatter(new HtmlOutputFormatter($formatter)); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		$application->setAutoExit(false); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | // Some commands  dump things out instead of returning a value | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		ob_start(); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 		$errorCode = $application->run($input, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 		if (!$result = $output->fetch()){ | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 			$result = ob_get_contents(); // If empty, replace it by the catched output | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 		ob_end_clean(); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 		return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 			'input' => $this->command, | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 			'output' => $result, | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 			'environment' => '', | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 			'error_code' => $errorCode | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 		]; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |