2021-09-08 22:53:13 +00:00
|
|
|
<?php
|
2021-09-08 23:02:10 +00:00
|
|
|
/**
|
2021-09-08 23:05:05 +00:00
|
|
|
* @brief noodles, a plugin for Dotclear 2
|
2021-09-08 23:02:10 +00:00
|
|
|
*
|
|
|
|
* @package Dotclear
|
2021-09-08 23:05:05 +00:00
|
|
|
* @subpackage Plugin
|
2021-09-08 23:02:10 +00:00
|
|
|
*
|
2021-09-08 23:05:05 +00:00
|
|
|
* @author Jean-Christian Denis and contributors
|
2021-09-08 23:02:10 +00:00
|
|
|
*
|
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
2021-09-08 22:53:13 +00:00
|
|
|
|
2021-09-09 07:12:31 +00:00
|
|
|
if (!defined('DC_RC_PATH')) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-08 22:53:13 +00:00
|
|
|
|
|
|
|
class noodles
|
|
|
|
{
|
2021-09-09 07:12:31 +00:00
|
|
|
private $noodles = [];
|
2021-09-08 23:02:10 +00:00
|
|
|
|
|
|
|
public static function decode($s)
|
|
|
|
{
|
|
|
|
$o = @unserialize(base64_decode($s));
|
2021-09-09 07:12:31 +00:00
|
|
|
if ($o instanceof self) {
|
|
|
|
return $o;
|
|
|
|
}
|
2021-09-08 23:02:10 +00:00
|
|
|
return new self;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function encode()
|
|
|
|
{
|
|
|
|
return base64_encode(serialize($this));
|
|
|
|
}
|
|
|
|
|
2021-09-09 07:12:31 +00:00
|
|
|
public function add($id, $name, $js_callback, $php_callback = null)
|
2021-09-08 23:02:10 +00:00
|
|
|
{
|
2021-09-09 07:12:31 +00:00
|
|
|
$this->noodles[$id] = new noodle($id, $name, $js_callback, $php_callback);
|
|
|
|
return $this->noodles[$id];
|
2021-09-08 23:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($id)
|
|
|
|
{
|
|
|
|
return isset($this->noodles[$id]) ? $this->noodles[$id] : null;
|
|
|
|
}
|
|
|
|
|
2021-09-09 07:12:31 +00:00
|
|
|
public function __set($id, $noodle)
|
2021-09-08 23:02:10 +00:00
|
|
|
{
|
|
|
|
return $this->noodles[$id] = $noodle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function exists($id)
|
|
|
|
{
|
|
|
|
return isset($this->noodles[$id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
return !count($this->noodles);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function noodles()
|
|
|
|
{
|
|
|
|
return $this->noodles;
|
|
|
|
}
|
2021-09-08 22:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class noodle
|
|
|
|
{
|
2021-09-08 23:02:10 +00:00
|
|
|
private $id;
|
|
|
|
private $name;
|
|
|
|
private $js_callback;
|
|
|
|
private $php_callback;
|
2021-09-09 07:12:31 +00:00
|
|
|
private $settings = [
|
2021-09-08 23:02:10 +00:00
|
|
|
'active' => 0,
|
|
|
|
'rating' => 'g',
|
|
|
|
'size' => 16,
|
|
|
|
'target' => '',
|
|
|
|
'place' => 'prepend'
|
2021-09-09 07:12:31 +00:00
|
|
|
];
|
2021-09-08 23:02:10 +00:00
|
|
|
|
2021-09-09 07:12:31 +00:00
|
|
|
public function __construct($id, $name, $js_callback, $php_callback = null)
|
2021-09-08 23:02:10 +00:00
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
$this->name = $name;
|
|
|
|
$this->js_callback = $js_callback;
|
|
|
|
$this->php_callback = $php_callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function id()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function name()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2021-09-09 07:12:31 +00:00
|
|
|
public function jsCallback($g, $content = '')
|
2021-09-08 23:02:10 +00:00
|
|
|
{
|
2021-09-09 07:12:31 +00:00
|
|
|
if (!is_callable($this->js_callback)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return call_user_func($this->js_callback, $g, $content);
|
2021-09-08 23:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasJsCallback()
|
|
|
|
{
|
|
|
|
return !empty($this->js_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function phpCallback($core)
|
|
|
|
{
|
2021-09-09 07:12:31 +00:00
|
|
|
if (!is_callable($this->php_callback)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return call_user_func($this->php_callback, $core, $this);
|
2021-09-08 23:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasPhpCallback()
|
|
|
|
{
|
|
|
|
return !empty($this->php_callback);
|
|
|
|
}
|
|
|
|
|
2021-09-09 07:12:31 +00:00
|
|
|
public function set($type, $value)
|
2021-09-08 23:02:10 +00:00
|
|
|
{
|
2021-09-09 07:12:31 +00:00
|
|
|
switch ($type) {
|
2021-09-08 23:02:10 +00:00
|
|
|
case 'active':
|
2021-09-09 07:12:31 +00:00
|
|
|
$this->settings['active'] = abs((integer) $value);
|
2021-09-08 23:02:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rating':
|
2021-09-09 07:12:31 +00:00
|
|
|
$this->settings['rating'] = in_array($value, ['g', 'pg', 'r', 'x']) ? $value : 'g';
|
2021-09-08 23:02:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'size':
|
2021-09-09 07:12:31 +00:00
|
|
|
$this->settings['size'] = in_array($value, [16, 24, 32, 48, 56, 64, 92, 128, 256]) ? $value : 16;
|
2021-09-08 23:02:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'css':
|
2021-09-09 07:12:31 +00:00
|
|
|
$this->settings['css'] = (string) $value;
|
2021-09-08 23:02:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'target':
|
2021-09-09 07:12:31 +00:00
|
|
|
$this->settings['target'] = (string) $value;
|
2021-09-08 23:02:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'place':
|
2021-09-09 07:12:31 +00:00
|
|
|
$this->settings['place'] = in_array($value, ['append', 'prepend', 'before', 'after']) ? $value : 'prepend';
|
2021-09-08 23:02:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-09-09 07:12:31 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function active($value)
|
|
|
|
{
|
|
|
|
return $this->set('active', $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rating($value)
|
|
|
|
{
|
|
|
|
return $this->set('rating', $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function size($value)
|
|
|
|
{
|
|
|
|
return $this->set('size', $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function css($value)
|
|
|
|
{
|
|
|
|
return $this->set('css', $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function target($value)
|
|
|
|
{
|
|
|
|
return $this->set('target', $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function place($value)
|
|
|
|
{
|
|
|
|
return $this->set('place', $value);
|
2021-09-08 23:02:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 07:12:31 +00:00
|
|
|
public function __set($type, $value)
|
2021-09-08 23:02:10 +00:00
|
|
|
{
|
2021-09-09 07:12:31 +00:00
|
|
|
$this->set($type, $value);
|
2021-09-08 23:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get($type)
|
|
|
|
{
|
|
|
|
return isset($this->settings[$type]) ? $this->settings[$type] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($type)
|
|
|
|
{
|
|
|
|
return $this->get($type);
|
|
|
|
}
|
|
|
|
}
|