pterotype/activitypub-php

dev-master 2019-05-26 20:29 UTC

This package is auto-updated.

Last update: 2024-09-27 08:21:10 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 pterotype/activitypub-php

用法

基本用法示例

<?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.
}
?>