richarddern/php-gemini

此软件包已被放弃,不再维护。未建议替代软件包。

PHP 的 Gemini 项目库

dev-main 2021-02-26 20:40 UTC

This package is auto-updated.

Last update: 2021-03-26 20:54:27 UTC


README

项目已迁移至 https://git.athaliasoft.com/richard/php-gemini !

php-gemini

php-gemini 是一个库,实现了Project Gemini 协议,作为客户端和服务器端。

安装

composer require richarddern/php-gemini

常见功能

  • 支持客户端和服务器端使用 TLS,归功于 react/socket
  • 包含一个脚本来生成您自己的自签名证书,以防您无法或不想使用官方证书
  • 解析、验证和解析 URI,因此您还可以使用相对 URI,归功于 league/uri
  • 详细的日志记录,归功于 monolog
  • league/flysystem 提供的文件系统抽象,允许您从各种位置提供文件

文档

客户端

要查询远程服务器,您首先需要实例化 Client

use RichardDern\Gemini\Client;

// This will connect to a server running on the same computer
$client = new Client();

// This will connect to a remote server on default port
//$client = new Client('gemini.circumlunar.space');

// And this will connect to a server using a custom port
//$client = new Client('10.0.0.1', 1966);

// You can define the server and remote port after instanciating the class,
// or whenever you want before your query:
$client->setServer('127.0.0.1');
$client->setPort(1965);

您现在可以查询服务器了

// You can define the base URI that will be used to resolve subsequent
// relative queries
$client->setBaseUri('gemini://127.0.0.1:1965/absolute_path');

// This will then resolve to 
// gemini://127.0.0.1:1965/absolute_path/relative_path/document
$client->request('relative_path/document');

// Or, you can request an absolute URI directly:
$client->request('gemini://127.0.0.1:1965/absolute_path/relative_path/document');

结果将是一个 RichardDern\Gemini\Response 对象,它公开以下属性

  • $status,一个两位状态码;您可以在Gemini 规范附录1中查看状态码的完整列表
  • $meta,包含有关响应的多种信息,如 MIME 类型、重定向 URL 或语言,具体取决于服务器的响应
  • $body,响应主体的原始、未格式化的内容

服务器

此库还允许您轻松运行 Gemini 服务器。

use RichardDern\Gemini\Server;

// This will create a server on 127.0.0.1 and listening on default port (1965)
$server = new Server();

// You can set the binding address and port when instanciating the class...
//$server = new Server('[::1]', 1966);

// ...or after
$server->setAddress('[::1]');
$server->setPort(1965);

在实际启动服务器之前,您需要向服务器提供证书文件的路径。

$server->setCertificatePath('./localhost.pem');

您可以使用提供的 bin/generate-self-signed-certificate.php 文件。

php ./bin/generate-self-signed-certificate.php > localhost.pem

此实现支持基本目录索引,但您需要手动启用它。

$server->enableDirectoryIndex(true);

此实现允许您从各种文件系统提供文件,包括本地文件系统、FTP 服务器,甚至内存文件系统。请参阅 league/flysystem 文档以了解您可以使用哪些适配器。

除非指定其他,否则服务器将使用 LocalFilesystemAdapter,并将查找位于您启动服务器位置的 www 文件夹中的文件。

但是,如果您想使用不同的适配器,也可以

// Serving files on Gemini from a FTP site
$adapter = new League\Flysystem\Ftp\FtpAdapter(
    // Connection options
    League\Flysystem\Ftp\FtpConnectionOptions::fromArray([
        'host' => 'hostname', // required
        'root' => '/root/path/', // required
        'username' => 'username', // required
        'password' => 'password', // required
        'port' => 21
    ])
);

$server->setFileSystemAdapter($adapter);

然后您可以启动您的服务器

$server->start();

您需要使用进程管理器来确保服务器持续运行。您可以使用systemd或supervisor来完成这项任务。文档将很快更新一些示例。

日志记录

客户端和服务器使用相同的方法来配置日志记录。您应该在实例化客户端或服务器后立即设置日志记录以适应您的需求。

$client = new Client();

// You can define the log level
$client->setLogLevel(Logger::DEBUG);

// Here, we will define a simple StreamHandler, but we will choose where to log
$handler = new StreamHandler('/var/log/gemini.log', Logger::INFO);
$client->setLogHandler($handler);

// The channel will help you find your way into the logs
$client->setLogChannel('my-gemini-client');

作者

Richard Dern - https://github.com/RichardDern

许可证

MIT

替代方案

  • Ergol - 可以为每个vhost使用一个证书,支持带有CSS的HTTP交付