Code Monkey home page Code Monkey logo

activitypub-php's Introduction

ActivityPub-PHP

A library to turn any PHP project into a full ActivityPub implementation

This library is a work-in-progress. The documentation below reflects what the API will look like once it's done.

ActivityPub-PHP is a library that embeds a full ActivityPub server into any PHP project. It works with any SQL database and any web framework. At a high level, it provides a request handler that you can route ActivityPub requests to which will take care of persisting the received activity, performing any necessary side effects, and delivering the activity to other federated servers. It also provides a PHP API to create and manage actors and activities.

What it does

  • stores incoming activities to your project's existing SQL database in a configurable fashion
  • implement both the client-to-server and the server-to-server parts of the ActivityPub protocol
  • verify HTTP signatures on incoming ActivityPub requests and sign outgoing ActivityPub requests
  • provide a PHP API so you can create and manage actors and send activities directly from code
  • hook into your application's user authentication logic to provide ways to associate your users with ActivityPub actors
  • manage the JSON-LD context for you, with hooks if you need to add custom fields
  • support PHP > 5.*

What it doesn't do

  • handle standalone user authentication - this is up to your particular application
  • support non-SQL databases
  • provide a UI

Installation

ActivityPub-PHP is available via Composer:

$ composer require pterotype/activitypub-php

Usage

Basic usage example:

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

activitypub-php's People

Contributors

jdormit avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.