在浏览器中使用 PHP 脚本运行编辑器

2022-08-30 12:13:28

想知道是否可以使用一个小的PHP包装器从浏览器执行,因为我无法访问对服务器的shell访问。composer

不确定是否可以使用 cURL 执行此操作?


答案 1

Danack 解决方案的另一种方法是作为依赖项包含在 您的 中,并且只使用它的 API,而不是从 中提取内容。"composer/composer"composer.jsoncomposer.phar

composer.json

...
"require-dev": {
  "composer/composer": "dev-master",
}
...

手动运行,因此您将能够在以下脚本中要求它:composer install

composer_install.php

<?php
require 'vendor/autoload.php'; // require composer dependencies

use Composer\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;

// Composer\Factory::getHomeDir() method 
// needs COMPOSER_HOME environment variable set
putenv('COMPOSER_HOME=' . __DIR__ . '/vendor/bin/composer');

// call `composer install` command programmatically
$input = new ArrayInput(array('command' => 'install'));
$application = new Application();
$application->setAutoExit(false); // prevent `$application->run` method from exitting the script
$application->run($input);

echo "Done.";

从浏览器访问脚本时,该命令应按预期运行。


答案 2

是的,你可以用一个小的PHP包装器运行Composer。所有 Composer 源代码都可以在 Phar 文件中找到,因此可以提取它,然后在设置 InputInterface 以替换期望通过命令行传入命令的 Composer 后运行它。

如果像这样设置目录结构:

./project  
./project/composer.json
./project/composer.lock
./project/webroot/composerExtractor.php  
./project/var/

将下面的代码放入 composerExtractor.php然后从 Web 浏览器运行它,Composer 应该将所有库下载到:

./project/vendors/

以及在该目录中生成类装入器文件。

作曲家提取器.php

<?php

define('EXTRACT_DIRECTORY', "../var/extractedComposer");


if (file_exists(EXTRACT_DIRECTORY.'/vendor/autoload.php') == true) {
    echo "Extracted autoload already exists. Skipping phar extraction as presumably it's already extracted.";
}
else{
    $composerPhar = new Phar("Composer.phar");
    //php.ini setting phar.readonly must be set to 0
    $composerPhar->extractTo(EXTRACT_DIRECTORY);
}

//This requires the phar to have been extracted successfully.
require_once (EXTRACT_DIRECTORY.'/vendor/autoload.php');

//Use the Composer classes
use Composer\Console\Application;
use Composer\Command\UpdateCommand;
use Symfony\Component\Console\Input\ArrayInput;

// change out of the webroot so that the vendors file is not created in
// a place that will be visible to the intahwebz
chdir('../');

//Create the commands
$input = new ArrayInput(array('command' => 'update'));

//Create the application and run it with the commands
$application = new Application();
$application->run($input);

?>

虽然这是可能的,但这不是一个绝妙的想法,但如果你不能使用一个给你ssh访问权限的主机,这可能是必要的。

我强烈建议至少为自己或您的办公室获取一个静态IP地址,然后将访问限制为仅访问您自己的IP,以及在服务器上运行此脚本后删除该脚本,以防止它再次意外运行。


推荐