CKFinder 3 – PHP Connector Documentation
Plugins

Introduction

CKFinder uses two types of plugins:

  • JavaScript plugins, used by the CKFinder frontend, which can be used to alter and extend CKFinder UI.
  • PHP plugins that can be used to change and extend the behavior of the server-side connector.

Below you can find information and examples for CKFinder PHP plugins. For details about JavaScript plugins please refer to Creating CKFinder 3.x JavaScript Plugins documentation.

Plugin Installation

Manual installation requires downloading plugin source and unpacking it inside the CKFinder plugins directory. The directory structure needs to follow the following pattern:

1 plugins
2 └── PluginName
3  └── PluginName.php

Configuration

After installation the plugin has to be enabled in the CKFinder configuration file. See plugins for details.

Plugins usually offer a few configuration options that can be set in the main CKFinder configuration file. Please check plugin documentation for details.

Plugin Development

Default CKFinder behavior can be changed and extended with custom plugins. There are a few constraints that a plugin must meet to be recognized as valid:

  1. A plugin must have a unique name that meets the following conditions:
    • It must start with an uppercase letter.
    • It must be a valid PHP class name.
    • It must be unique within the CKSource\CKFinder\Plugin namespace.
  2. A plugin must be located in its own namespace under CKSource\CKFinder\Plugin\<plugin_name>.
  3. The main plugin class name must match the plugin name and has to be located in the plugin's namespace. For example, let us assume you want to create a plugin named ImageWatermark. In this case the plugin namespace will be CKSource\CKFinder\Plugin\ImageWatermark, and the main class of the plugin needs to be accessible as CKSource\CKFinder\Plugin\ImageWatermark\ImageWatermark.
  4. The plugin class needs to implement the PluginInterface.

Plugin Interface

Each CKFinder plugin has to implement the PluginInterface:

1 namespace CKSource\CKFinder\Plugin;
2 
3 interface PluginInterface
4 {
5  public function setContainer(CKFinder $app);
6  public function getDefaultConfig();
7 }

It contains two methods that need to be implemented in the plugin class:

  • setContainer is used to inject CKFinder dependency injection container to the plugin scope (see Dependency Injection Container for details).
  • getDefaultConfig returns an array with default configuration options for the plugin.

Plugin Configuration

Default configuration options returned by the getDefaultConfig method are merged to CKFinder Config under a node corresponding to the plugin name. Let us assume that the ImageWatermark plugin getDefaultConfig method looks as follows:

1 public function getDefaultConfig()
2 {
3  return array(
4  'imagePath' => '/path/stamp.png',
5  'position' => array(
6  'right' => 10,
7  'bottom' => 'center'
8  )
9  );
10 }

To access plugin configuration settings, first you need to get the Config object from the dependency injection container injected in the setContainer plugin method:

1 $config = $this->app['config'];

Later in the plugin code you can access plugin options in the following way:

1 $config->get('ImageWatermark.imagePath'); // '/path/stamp.png'
2 $config->get('ImageWatermark.position.bottom'); // 'center'
3 $config->get('ImageWatermark.position'); // an array: array('right' => 10, 'bottom' => 'center')
4 $config->get('ImageWatermark'); // an array: whole plugin configuration array

All default plugin configuration options can be overwritten in the main CKFinder configuration file. If the user wants to use a different image for the ImageWatermark plugin, it can be added in an appropriate option:

1 // ...
2 'ImageWatermark' => array(
3  'imagePath' => '/custom/path/image.png'
4 ),

After that:

1 $config->get('ImageWatermark.imagePath'); // '/custom/path/image.png'

Plugin Types

Plugins can perform many different tasks. Depending on your plugin purpose you need to decide what type of plugin you need. There are two main plugin types:

  • Event subscribers – Plugins that perform actions for defined application events (see Events for details). If you want to create this type of plugin, you have to implement EventSubscriberInterface in your plugin class.
  • Command plugins – Plugins that behave like other CKFinder commands (see Commands for details). In this case you need to extend the CommandAbstract class, or any of the existing CKFinder command classes.

Plugin Structure Example

Getting back to the ImageWatermark plugin example, let us assume it should work in the following way:

  • Listen for the file upload event, after the file is validated (and if it is an image — also resized), and just before it is saved.
  • If the file is a supported image type, get uploaded file content and add a watermark.
  • Set the watermarked image as uploaded file content.

The plugin will be an event subscriber type then, listening to CKFinderEvent::FILE_UPLOAD. The basic code structure of the ImageWatermark plugin class can look as below:

1 namespace CKSource\CKFinder\Plugin\ImageWatermark;
2 
3 use CKSource\CKFinder\CKFinder;
4 use CKSource\CKFinder\Config;
5 use CKSource\CKFinder\Event\CKFinderEvent;
6 use CKSource\CKFinder\Event\FileUploadEvent;
7 use CKSource\CKFinder\Plugin\PluginInterface;
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9 
10 class ImageWatermark implements PluginInterface, EventSubscriberInterface
11 {
12  protected $app;
13 
14  public function setContainer(CKFinder $app)
15  {
16  $this->app = $app;
17  }
18 
19  public function getDefaultConfig()
20  {
21  return [
22  'imagePath' => __DIR__ . '/stamp.png',
23  'position' => [
24  'right' => 0,
25  'bottom' => 0
26  ]
27  ];
28  }
29 
30  public function addWatermark(FileUploadEvent $event)
31  {
32  $config = $this->app['config'];
33 
34  $uploadedFile = $event->getUploadedFile();
35  $imageData = $uploadedFile->getContents();
36  $watermarkImagePath = $config->get('ImageWatermark.imagePath');
37 
38  // Process uploaded image.
39 
40  $uploadedFile->setContents($processedImageData);
41  }
42 
43  public static function getSubscribedEvents()
44  {
45  return [CKFinderEvent::FILE_UPLOAD => 'addWatermark'];
46  }
47 }

You can find a complete working example of the ImageWatermark plugin on GitHub.