instalution/activitypub-php-fork

ActivityPub 库的分支

dev-master 2023-09-24 05:34 UTC

This package is auto-updated.

Last update: 2024-09-24 07:52:41 UTC


README

一个将任何 PHP 项目转换为完整的 ActivityPub 实现的库

此库仍在开发中。下面的文档反映了完成后的 API 将会是什么样子。

ActivityPub-PHP 是一个库,它将完整的 ActivityPub 服务器嵌入到任何 PHP 项目中。它与任何 SQL 数据库和任何 Web 框架兼容。从高层次上讲,它提供了一个请求处理器,可以将 ActivityPub 请求路由到其中,它会负责持久化接收到的活动,执行任何必要的副作用,并将活动交付给其他联盟服务器。它还提供了一个 PHP API,用于创建和管理行为者和活动。

它能做什么

  • 以可配置的方式将传入的活动存储到您项目的现有 SQL 数据库中
  • 实现 ActivityPub 协议的客户端到服务器和服务器到服务器部分
  • 验证传入的 ActivityPub 请求的 HTTP 签名,并签署出去的 ActivityPub 请求
  • 提供 PHP API,让您可以直接从代码中创建和管理行为者并发送活动
  • 集成到您应用程序的用户身份验证逻辑中,提供将您的用户与 ActivityPub 行为者关联的方式
  • 为您管理 JSON-LD 上下文,如果需要添加自定义字段,还提供钩子
  • 支持 PHP > 5.*

它不能做什么

  • 处理独立用户身份验证 - 这取决于您的特定应用程序
  • 支持非 SQL 数据库
  • 提供 UI

安装

ActivityPub-PHP 通过 Composer 提供

$ composer require instalution/activitypub-php-fork

用法

基本用法示例

<?php
use ActivityPub\ActivityPub;
use ActivityPub\Config\ActivityPubConfig;

// Constructing the ActivityPub instance
$config = ActivityPubConfig::createBuilder()
        // Function to determine if the current request should be associated
        // with an ActivityPub actor. It should return the actor id associated
        // with the current request, or false if the current request is not associated
        // with the actor. This is where you can plug in your application's user
        // management system:
        ->setAuthFunction( function() {
            if ( current_user_is_logged_in() ) {
                return get_actor_id_for_current_user();
            } else {
                return false;
            }
        } )
        // Database connection options, passed directly to Doctrine:
        ->setDbConnectionParams( array(
            'driver' => 'pdo_mysql',
            'user' => 'mysql'
            'password' => 'thePa$$word',
            'dbname' => 'my-database',
        ) )
        // Database table name prefix for compatibility with $wpdb->prefix, etc.:
        // Default: ''
        ->setDbPrefix( 'activitypub_' )
        ->build();
$activitypub = new ActivityPub( $config );

// Routing incoming ActivityPub requests to ActivityPub-PHP
if ( in_array( $_SERVER['HTTP_ACCEPT'],
               array( 'application/ld+json', 'application/activity+json' ) ) ) {
    // Handle the request, perform any side effects and delivery,
    // and return a Symfony Response
    $response = $activitypub->handle();
    // Send the response back to the client
    $response->send();
}

// Creating a new actor
function createActor()
{
    $actorArray = array(
        'id' => 'https://mysite.com/my_actor',
        'type' => 'Person',
        'preferredUsername' => 'myActor',
    );
    $actor = $activitypub->createActor( $actorArray );
    // $actor has all the ActivityPub actor fields, e.g. inbox, outbox, followers, etc.
}

// Posting activities from code
function postActivity()
{
    $actor = $activitypub->getActor( 'https://mysite.com/my_actor' );
    $note = array(
        'type' => 'Note',
        'content' => 'This is a great note',
        'to' => $actor['followers'],
    );
    $actor->create( $note );
    // also $actor->update(), $actor->delete(), etc.
}
?>