Download the PHP package rikudou/activity-pub without Composer
On this page you can find all versions of the php package rikudou/activity-pub. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rikudou/activity-pub
More information about rikudou/activity-pub
Files in rikudou/activity-pub
Package activity-pub
Short Description A strongly typed, validated and developer-friendly ActivityPub implementation in PHP
License MIT
Informations about the package activity-pub
ActivityPub for PHP
A strongly typed and developer friendly ActivityPub implementation. All Core and Extended types are implemented. Also some widely used unofficial extensions.
Table of contents
- ActivityPub for PHP
- Table of contents
- Installation
- Objects
- Naming
- Objects and activities
- Validations
- Parsing JSON into types
- Creating your own types
- Server
- Request signing
- Request validating
- Fetching objects
- Symfony usage
Installation
composer require rikudou/activity-pub
Objects
Naming
All object names are the same as in the ActivityPub/ActivityStreams specifications, with the sole exception of the
base Object
which is called BaseObject
because PHP disallows having a class called Object
.
Objects and activities
To construct an object, simply create it as you normally would, for example, let's construct a note:
This prints:
Validations
All property assignments are validated using various set of rules depending on the type of the property and object. There are multiple modes of validation:
- none - no validation takes place
- lax - not as strict as the strict mode, leaves out some stuff that is required by the specification but isn't required in real-world scenarios
- strict - strict adherence to the ActivityPub/ActivityStreams specifications
- recommended - a custom opinionated set of rules, stricter than strict, but should prevent you making some mistakes which are technically correct but make no real sense. Some bugs are possible for edge cases.
For example:
When running the snippet above, you get this exception:
Uncaught Rikudou\ActivityPub\Exception\InvalidPropertyValueException: The value for property 'id' is not valid: string(123): the value must be a valid uri
Now let's do the same with changing the validation level to none:
This prints the following JSON:
If you don't want to change the validator mode for every object individually, you can also use the GlobalSettings
class:
The above code prints the same JSON. Note that if you change the validator mode for an individual object, the global setting doesn't have any effect anymore, until you manually set it back to null.
The last option is to use the runInNoValidationContext
function:
The same caveats as for changing the global mode exist (because all this function does is it changes the global mode, runs your function, changes it back to the original value).
Non-standard properties
If you wish to use non-standard properties, you can use the setter:
Note that unless you disable validation, custom properties are not allowed, so the above needs to run in the no-validation context:
Parsing JSON into types
While exporting ActivityPub objects to JSON is great, you'll need the exact opposite if you want to handle incoming activities!
Luckily for us, there's a TypeParser
(more specifically, a class implementing the interface, DefaultTypeParser
).
Let's take the previous example as our input:
Creating your own types
All the ActivityPub objects can be extended by your own classes. The built-in ones use property hooks to automatically validate the values, but you can do it any other way, just make sure the properties are publicly readable.
Let's create a custom type:
Adding a property is easy:
Now, if you create your cat, you can check out the response JSON:
Now, if you want to make sure your cat always has some lives, you can mark the property as required:
You also need to specify the minimum validator mode that it's required on. If you set it to Lax
, it will be required
on Lax
, Strict
and Recommended
. If you set it to Strict
, it will be required on Strict
and Recommended
.
And now creating our cat throws this exception:
Now, let's get fancy and create our cat! And announce it to the world!
All this prints this complicated-looking ActivityPub activity which can be sent to every ActivityPub server in the whole world!
Now, if your Cat object ever becomes so popular that everything using ActivityPub sends them back and forth, you might want to register the type in the parser, otherwise it would just throw an exception saying that it doesn't know about the Cat object.
Server
In addition to the ActivityPub object, there are also various helpers for implementing ActivityPub in your server. All of them rely on the PSR abstractions, so it should be easy to use them with your favourite http client or a framework of choice.
Request signing
While not part of the ActivityPub protocol itself, you won't get far in the Fediverse without signing your request - almost no
mainstream software accepts activities that are unsigned. For signing to work, each actor must be publicly accessible at the URL
pointed to in its ID and have a publicKey
property with the public key defined.
For this reason, this package includes two things:
- A non-standard
publicKey
property available for all actors- If you use the
Recommended
validator mode, this property is required for all actors
- If you use the
- An
ActorKeyGenerator
service which generates a private and public key-pair that should be stored in a database for all actors.
Example:
Now you have an actor who can send signed requests!
Now let's take a look at a hypothetical service that sends your requests:
Request validating
Of course the reverse, validating an incoming request, is also possible!
Fetching objects
Fetching remote objects can be handled using the ObjectFetcher
and WebFinger
services (implemented by ActivityPubObjectFetcher
and DefaultWebFinger
respectively).
Symfony usage
To use this library in Symfony, simply configure the PSR-7 Bridge
and create the following file in config/packages/activity_pub.yaml
:
If you also want to use the built-in activity sender, you need to create a service implementing Rikudou\ActivityPub\Server\Abstraction\LocalActorResolver
and add the following to the above yaml:
All versions of activity-pub with dependencies
psr/http-client Version ^1.0
ext-openssl Version *
psr/http-factory Version ^1.1
rikudou/iterables Version ^1.3
rikudou/array-merge-recursive Version ^1.0