Created
April 17, 2017 05:11
-
-
Save jckimble/91f45fe5ee79e92505815be5906a1cda to your computer and use it in GitHub Desktop.
Web Composer Installer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/**************************************************** | |
* Web Composer Installer | |
* Created By James Kimble <[email protected]> | |
* Downloads composer.phar, runs composer install, | |
* and cleans up after its self. | |
* | |
* LICENSE: Do What You Want | |
* I DO NOT TAKE ANY RESPONSIBILITY FOR ANY DAMAGE THIS | |
* PHP SCRIPT MIGHT DO OR IF IT KILLS YOUR CAT, FAMILY, | |
* OR GIRLFRIEND/WIFE/WHATEVER. | |
****************************************************/ | |
set_time_limit(0); //don't want this to fail half way | |
ini_set("phar.readonly",false); //enable extraction | |
function rrmdir($dir){ //function to recursively remove files | |
if (is_dir($dir)) { | |
$objects = scandir($dir); | |
foreach ($objects as $object) { | |
if ($object != "." && $object!= "..") { | |
if (is_dir($dir."/".$object)){ | |
rrmdir($dir."/".$object); | |
}else{ | |
unlink($dir."/".$object); | |
} | |
} | |
} | |
rmdir($dir); | |
} | |
} | |
function cleanup(){ //function to clean up files | |
$dirs=["","../","../../"]; | |
foreach($dirs as $dir){ | |
if(file_exists($dir."composer.json")){ | |
if(file_exists($dir."composer/vendor/autoload.php")){ | |
rrmdir($dir."composer"); | |
} | |
if(file_exists($dir."composer.phar")){ | |
unlink($dir."composer.phar"); | |
} | |
} | |
} | |
} | |
register_shutdown_function('cleanup'); | |
$msg=[]; | |
if(file_exists("composer.json")){//find where composer.json is at | |
$msg[]="composer.json found in webdirectory. I will still work but make sure the directory vendor can't be accessed from the domain!"; | |
}else if(file_exists("../composer.json")){ | |
chdir(".."); | |
}else if(file_exists("../../composer.json")){ | |
chdir("../.."); | |
} | |
if(file_exists("composer/vendor/autoload.php") == false){ //download & extract composer.phar | |
$domain="getcomposer.org"; | |
$path="/download/1.4.1/composer.phar"; | |
$composer = 'https://'.$domain.$path; | |
//download composer.phar | |
if(function_exists('curl_version') && !file_exists("composer.phar")){ | |
$fp = fopen("./composer.phar", "w"); | |
$opts=[ | |
CURLOPT_FILE => $fp, | |
CURLOPT_TIMEOUT => 28800, | |
CURLOPT_URL => $composer | |
]; | |
$ch = curl_init(); | |
curl_setopt_array($ch, $opts); | |
$result=curl_exec($ch); | |
curl_close($ch); | |
fclose($fp); | |
if(!$result){ | |
unlink("composer.phar"); | |
} | |
}else if(ini_get('allow_url_fopen') && !file_exists("composer.phar")){ | |
$h=fopen($composer,"rb"); | |
if($h){ | |
$fp = fopen("./composer.phar", "w"); | |
while(!feof($h)){ | |
$contents=fread($h,2048); | |
fwrite($fp,$contents); | |
} | |
fclose($h); | |
fclose($fp); | |
} | |
}else if(!file_exists("composer.phar")){ | |
$h=fsockopen($domain,80,$errno,$errstr,30); | |
if($h){ | |
$headers="GET ".$path." HTTP/1.1\r\n"; | |
$headers.="Host: ".$domain."\r\n"; | |
$headers.="Connection: Close\r\n\r\n"; | |
fwrite($h,$headers); | |
$fp = fopen("./composer.phar", "w"); | |
$dowrite=false; | |
while(!feof($h)){ | |
$contents=fgets($h,2048); | |
if($dowrite){ | |
fwrite($fp,$contents); | |
} | |
if($contents=="\r\n" && !$dowrite){ | |
$dowrite=true; | |
} | |
} | |
fclose($h); | |
fclose($fp); | |
} | |
} | |
//extract composer.phar | |
if(extension_loaded("Phar") && ini_get("phar.readonly") && file_exists("composer.phar")){ | |
$composerPhar = new Phar("composer.phar"); | |
$composerPhar->extractTo("composer"); | |
} | |
} | |
if(file_exists("composer/vendor/autoload.php")){ | |
require_once("composer/vendor/autoload.php"); | |
}else if(file_exists("composer.phar")){ | |
require_once("phar://composer.phar/vendor/autoload.php"); | |
}else{ | |
die("Unable to download composer.phar! Please upload composer.phar into the same directory that composer.json is in."); | |
} | |
use Composer\Console\Application; | |
use Composer\Command\InstallCommand; | |
use Symfony\Component\Console\Output\Output; | |
use Symfony\Component\Console\Input\ArrayInput; | |
class HTMLOutput extends Output { | |
function __construct(){ | |
parent::__construct(); | |
?> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> | |
<head> | |
<title>Web Composer Installer</title> | |
<style type="text/css"> | |
BODY{background:#000;} | |
pre { | |
background:#333; | |
border:1px solid #555; | |
color:#FFF; | |
margin:10px; | |
padding:15px; | |
border-radius: 15px; | |
} | |
</style> | |
</head> | |
<body> | |
<pre> | |
<?php | |
} | |
function __destruct(){ | |
?> | |
</pre> | |
</body> | |
</html> | |
<?php | |
} | |
function doWrite($message,$newline){ | |
echo $message; | |
if($newline) | |
echo "\n"; | |
flush(); | |
} | |
} | |
//can be used to run any composer command | |
$input = new ArrayInput(array('command'=>'install','--no-dev'=>true,'--no-suggest'=>true,'--no-interaction'=>true,'--no-progress'=>true,'--optimize-autoloader'=>true)); | |
$output = new HTMLOutput(); | |
foreach($msg as $m){ | |
$output->doWrite($m,true); | |
} | |
$application = new Application(); | |
$application->setAutoExit(false); | |
$application->run($input,$output); | |
$output->doWrite("Finished! Remember to remove this file",true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment