']; private $title = ''; private $config = [ 'subclass_prefix' => null, ]; private $secure_directory_button = 'Click to Secure Installer and Continue...'; public function __construct($options = []){ if(!defined('BASEPATH')){ define('BASEPATH', ''); } try{ $this->rootdir = @$_SERVER['DOCUMENT_ROOT'] ?? dirname(__DIR__); require($this->rootdir . '/application/config/config.php'); } catch(\Exception $e){ die(var_dump($e)); } $this->ci_config($config); $this->repository_url = @$options['repository_url'] ? $options['repository_url'] : env('BUILD_REPOSITORY_URL', $this->repository_url); $this->install_dir = @$options['install_dir'] ? $options['install_dir'] : $this->rootdir . $this->install_dir; $this->installer_parent_dirname = @$options['installer_parent_dirname'] ? $options['installer_parent_dirname'] : $this->installer_parent_dirname; if(!$this->preflight()) return; $requirements_ok = $this->check_requirements(); if(!empty($_POST['continue'])){ $this->title = ""; if($requirements_ok && $this->should_install(true)){ $this->install(); $this->post_install(); } } else if(!empty($_GET['secure_directory'])){ $this->secure_installer(); header('Location: /'); } else if(empty($this->errors)){ if($this->should_install()){ $this->title = "Review the information below and click 'Continue' to proceed with the installation or update"; $this->results[] = ''; } } else{ $this->title = "Please correct the issues listed below"; } } private function set_config(){ if(!file_exists($this->install_dir . '/vendor/autoload.php')) return false; require $this->install_dir . '/vendor/autoload.php'; try{ BlueRaster\PowerBIAuthProxy\Auth::config(); } catch(Exception $e){ $_GET['configure'] = true; $this->results[] = require $this->install_dir . '/install.php'; return false; } return true; } private function ci_config(Array $config){ $this->config['subclass_prefix'] = $config['subclass_prefix']; } private function get_http($url){ $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_USERAGENT => 'G3N1US cURL Fn' )); $resp = curl_exec($curl); curl_close($curl); return $resp; } private function install(){ file_put_contents($this->install_dir . '/current.zip', $this->get_http($this->repository_url . '/current.zip')); file_put_contents($this->install_dir . '/hash.txt', $this->get_http($this->repository_url . '/hash.txt')); $zip = new ZipArchive; if ($zip->open($this->install_dir . '/current.zip') === TRUE) { $zip->extractTo($this->install_dir); $zip->close(); $this->results[] = "
Update completed successfully.
"; } else { $this->errors[] = "
An error occurred with unzipping the update/install package
"; } $app_path = dirname(dirname($this->install_dir)); // create controller file $controller_content = 'require_once APPPATH . \'/third_party/powerbi_auth_proxy/vendor/autoload.php\';' .PHP_EOL . PHP_EOL; $controller_content .= 'BlueRaster\\PowerBIAuthProxy\\Routes::route();'; $controllerpath = $app_path . '/core/' . $this->config['subclass_prefix'] . 'Controller.php'; file_put_contents($controllerpath, "errors)){ $this->results[] = '

Installation/Update Complete.

'; $this->results[] = $this->secure_directory_button; } } private function post_install(){ @unlink($this->install_dir . '/current.zip'); $config_complete = $this->set_config(); } private function secure_installer(){ file_put_contents(__DIR__.'/.htaccess', 'deny from all' . PHP_EOL); // test that installer is no longer available via the web $uri = @$_SERVER['HTTP_REFERER']; $contents = !!$this->get_http($uri); if($contents){ $this->results[] = '
ERROR - This script should no longer be accessible from the web for security purposes.
'; } } private function preflight(){ $folder = $this->installer_parent_dirname; $thisfilename = basename(__FILE__); $scriptdirname = basename(dirname(__FILE__)); if($scriptdirname != $this->installer_parent_dirname && !defined('AUTH_PROXY_INSTALLER_EMBEDDED')){ $this->results[] = '
ERROR
'; $this->results[] = "This installer script ($thisfilename) must be placed inside a folder named: $folder in the root folder of your website."; return false; } return true; } private function check_requirements(){ $proceed = true; // are directories writable if(!empty($_POST['create_install_dir'])) @mkdir($this->install_dir); if(!is_writable($this->install_dir)){ $me = function_exists('shell_exec') ? shell_exec('whoami') : "web user cannot be determined automatically"; $this->errors[] = "
The installation directory (".$this->install_dir.") does not exist or is not writable.
Create a folder at ".$this->install_dir." and ensure that the user: $mecan write to it. Click to attempt to do this automatically.
"; $proceed = false; } else{ $this->output[] = "
Installation directory (".$this->install_dir.") exists and is writable
"; } // is zip extension installed if(!class_exists('ZipArchive')){ $this->errors[] = "
Zip extension is missing
"; $proceed = false; } else{ $this->output[] = "
Zip extension installed
"; } // is cURL extension installed if(!function_exists('curl_init')){ $this->errors[] = "
cURL extension is missing
"; $proceed = false; } else{ $this->output[] = "
cURL extension installed
"; } if(!$proceed){ $this->results[] = "
There are ". count($this->errors) ." issue(s) that must be fixed before proceeding.
"; } return $proceed; } private function should_install($quiet = false){ $proceed = false; $step = false; if(!is_dir($this->install_dir . '/vendor')) { $proceed = true; $step = 'Application not found, so it will be installed'; } else{ $remotehash = $this->get_http($this->repository_url . '/hash.txt'); $localhash = file_get_contents($this->install_dir . '/hash.txt'); $compared = trim($remotehash) != trim($localhash); if($compared) $step = 'Application exists but an update is available, so it will be updated'; $proceed = $compared; } if(!$quiet){ if(!$proceed) { $this->results[] = "
The application is already installed and is up-to-date
"; $this->results[] = $this->secure_directory_button; } else{ $this->results[] = "
$step
"; } } return $proceed; } public function __toString(){ $title = '

' .$this->title. '

'; return $title . implode(PHP_EOL, $this->errors) . implode(PHP_EOL, $this->output) . implode(PHP_EOL, $this->results); } } if(!function_exists('dd')){ function dd($val){ die(var_dump($val)); } } $content = new CodeigniterPowerBIAuthProxyInstaller($_GET); $html = '

PowerBI Auth Proxy Installer/Updater

'.$content.'
'; if(defined('AUTH_PROXY_INSTALLER_EMBEDDED')){ return $content; } else{ echo trim($html); die(); }