noodles/_public.php

244 lines
6.8 KiB
PHP
Raw Normal View History

2021-09-08 22:53:13 +00:00
<?php
/**
2021-09-08 23:05:05 +00:00
* @brief noodles, a plugin for Dotclear 2
2022-11-17 20:00:42 +00:00
*
* @package Dotclear
2021-09-08 23:05:05 +00:00
* @subpackage Plugin
2022-11-17 20:00:42 +00:00
*
2021-09-08 23:05:05 +00:00
* @author Jean-Christian Denis and contributors
2022-11-17 20:00:42 +00:00
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-09-09 07:12:31 +00:00
if (!defined('DC_RC_PATH')) {
return null;
}
2021-09-08 22:53:13 +00:00
2022-11-17 20:00:42 +00:00
if (!dcCore::app()->blog->settings->noodles->noodles_active) {
2021-09-09 07:12:31 +00:00
return null;
}
2021-09-08 22:53:13 +00:00
2022-11-17 20:00:42 +00:00
include __DIR__ . '/inc/_default_noodles.php';
require_once __DIR__ . '/inc/_noodles_functions.php';
2021-09-08 22:53:13 +00:00
2022-11-17 20:00:42 +00:00
dcCore::app()->addBehavior('publicHeadContent', ['publicNoodles', 'publicHeadContent']);
2021-09-08 22:53:13 +00:00
2022-11-17 20:00:42 +00:00
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates');
2021-09-08 22:53:13 +00:00
global $__noodles;
2022-11-17 20:00:42 +00:00
$__noodles = noodles::decode(dcCore::app()->blog->settings->noodles->noodles_object);
2021-09-08 22:53:13 +00:00
2021-09-09 07:12:31 +00:00
if ($__noodles->isEmpty()) {
$__noodles = $__default_noodles;
2021-09-09 07:12:31 +00:00
}
2021-09-08 22:53:13 +00:00
//$GLOBALS['__noodles'] =& $__noodles;
2021-11-01 09:28:22 +00:00
foreach ($__noodles->noodles() as $noodle) {
2021-09-09 07:12:31 +00:00
if ($noodle->active && $noodle->hasPhpCallback()) {
2022-11-17 20:00:42 +00:00
$noodle->phpCallback(dcCore::app());
2021-09-09 07:12:31 +00:00
}
2021-09-08 22:53:13 +00:00
}
class publicNoodles
{
2022-11-17 20:00:42 +00:00
public static function publicHeadContent()
{
2021-10-30 19:53:30 +00:00
echo
2022-11-17 20:00:42 +00:00
dcUtils::cssLoad(dcCore::app()->blog->url . dcCore::app()->url->getURLFor('noodlescss')) .
dcUtils::jsLoad(dcCore::app()->blog->url . dcCore::app()->url->getBase('noodlesmodule') . '/js/jquery.noodles.js') .
dcUtils::jsLoad(dcCore::app()->blog->url . dcCore::app()->url->getURLFor('noodlesjs'));
2021-10-30 19:53:30 +00:00
}
}
class urlNoodles extends dcUrlHandlers
{
public static function css($args)
{
2022-11-17 20:00:42 +00:00
global $__noodles;
2021-10-30 19:53:30 +00:00
$css = '';
2021-11-01 09:28:22 +00:00
foreach ($__noodles->noodles() as $noodle) {
2021-09-09 07:12:31 +00:00
if (!$noodle->active || !$noodle->hasJsCallback()) {
continue;
}
$css .= '.noodles-' . $noodle->id() . '{' . $noodle->css . '}' . "\n";
}
2021-10-30 19:53:30 +00:00
header('Content-Type: text/css; charset=UTF-8');
echo $css;
exit;
}
public static function js($args)
{
2022-11-17 20:00:42 +00:00
global $__noodles;
2021-10-30 19:53:30 +00:00
$targets = [];
2021-11-01 09:28:22 +00:00
foreach ($__noodles->noodles() as $noodle) {
2021-10-30 19:53:30 +00:00
if (!$noodle->active || !$noodle->hasJsCallback()) {
continue;
}
2021-11-01 09:28:22 +00:00
$targets[] = '$(\'' . html::escapeJS($noodle->target) . '\').noodles({' .
2021-10-30 19:53:30 +00:00
' imgId:\'' . html::escapeJS($noodle->id()) . '\',' .
' imgPlace:\'' . html::escapeJS($noodle->place) . '\'' .
'});';
2021-09-09 07:12:31 +00:00
}
2021-10-30 19:53:30 +00:00
header('Content-Type: text/javascript; charset=UTF-8');
2021-11-01 09:28:22 +00:00
echo
2021-10-30 19:53:30 +00:00
"\$(function(){if(!document.getElementById){return;} \n" .
2022-11-17 20:00:42 +00:00
"\$.fn.noodles.defaults.service_url = '" . html::escapeJS(dcCore::app()->blog->url . dcCore::app()->url->getBase('noodlesservice') . '/') . "'; \n" .
2021-10-30 19:53:30 +00:00
"\$.fn.noodles.defaults.service_func = '" . html::escapeJS('getNoodle') . "'; \n" .
implode("\n", $targets) .
"})\n";
2021-09-08 22:53:13 +00:00
2021-10-30 19:53:30 +00:00
exit;
}
public static function service($args)
{
header('Content-Type: text/xml; charset=UTF-8');
$rsp = new xmlTag('rsp');
$i = !empty($_POST['noodleId']) ? $_POST['noodleId'] : null;
$c = !empty($_POST['noodleContent']) ? $_POST['noodleContent'] : null;
2022-11-17 20:00:42 +00:00
if (!dcCore::app()->blog->settings->noodles->noodles_active) {
$rsp->status = 'failed';
$rsp->message(__('noodles is disabled on this blog'));
2022-11-17 20:00:42 +00:00
echo $rsp->toXML(true);
2021-11-01 09:28:22 +00:00
return false;
}
2021-09-09 07:12:31 +00:00
if ($i === null || $c === null) {
$rsp->status = 'failed';
$rsp->message(__('noodles failed because of missing informations'));
2022-11-17 20:00:42 +00:00
echo $rsp->toXML(true);
2021-11-01 09:28:22 +00:00
return false;
}
2021-09-09 07:12:31 +00:00
try {
2022-11-17 20:00:42 +00:00
$__noodles = noodles::decode(dcCore::app()->blog->settings->noodles->noodles_object);
2021-09-09 07:12:31 +00:00
if ($__noodles->isEmpty()) {
$__noodles = $GLOBALS['__default_noodles'];
}
2021-11-01 09:28:22 +00:00
} catch (Excetpion $e) {
$rsp->status = 'failed';
$rsp->message(__('Failed to load default noodles'));
2022-11-17 20:00:42 +00:00
echo $rsp->toXML(true);
2021-11-01 09:28:22 +00:00
return false;
}
2021-09-09 07:12:31 +00:00
if (!$__noodles->exists($i)) {
$rsp->status = 'failed';
$rsp->message(__('Failed to load noodle'));
2022-11-17 20:00:42 +00:00
echo $rsp->toXML(true);
2021-11-01 09:28:22 +00:00
return false;
}
$m = $__noodles->get($i)->jsCallback($__noodles->get($i), $c);
$s = $__noodles->get($i)->size;
$r = $__noodles->get($i)->rating;
2022-11-17 20:00:42 +00:00
$d = dcCore::app()->blog->settings->noodles->noodles_image ?
urlencode(noodlesLibImagePath::getUrl('noodles')) : '';
2022-11-17 20:00:42 +00:00
$u = dcCore::app()->blog->settings->noodles->noodles_api;
2021-10-30 20:42:09 +00:00
if (empty($u)) {
$u = 'http://www.gravatar.com/';
}
2021-09-09 07:12:31 +00:00
if (!$m) {
$m = 'nobody@nowhere.tld';
}
if (!$s) {
$s = 32;
}
if (!$r) {
$r = 'g';
}
2021-11-01 09:28:22 +00:00
$m = md5(strtolower(trim($m)));
$im = new xmlTag('noodle');
$im->size = $s;
2021-11-01 09:28:22 +00:00
$im->src = sprintf('%savatar/%s?s=%s&amp;r=%s&amp;d=%s', $u, $m, $s, $r, $d);
$rsp->insertNode($im);
$rsp->status = 'ok';
2022-11-17 20:00:42 +00:00
echo $rsp->toXML(true);
exit;
}
public static function noodles($args)
{
2022-11-17 20:00:42 +00:00
if (!dcCore::app()->blog->settings->noodles->noodles_active) {
self::p404();
2021-11-01 09:28:22 +00:00
return;
}
2021-09-09 07:12:31 +00:00
if (!preg_match('#^(.*?)$#', $args, $m)) {
self::p404();
2021-11-01 09:28:22 +00:00
return;
}
$f = $m[1];
2021-09-09 07:12:31 +00:00
if (!($f = self::searchTplFiles($f))) {
self::p404();
2021-11-01 09:28:22 +00:00
return;
}
2021-09-09 07:12:31 +00:00
$allowed_types = ['png', 'jpg', 'jpeg', 'gif', 'css', 'js', 'swf'];
if (!in_array(files::getExtension($f), $allowed_types)) {
self::p404();
2021-11-01 09:28:22 +00:00
return;
}
$type = files::getMimeType($f);
2021-09-09 07:12:31 +00:00
header('Content-Type: ' . $type . '; charset=UTF-8');
header('Content-Length: ' . filesize($f));
2022-11-17 20:00:42 +00:00
if ($type != 'text/css' || dcCore::app()->blog->settings->system->url_scan == 'path_info') {
readfile($f);
2021-09-09 07:12:31 +00:00
} else {
echo preg_replace(
2021-11-01 09:28:22 +00:00
'#url\((?!(http:)|/)#',
2022-11-17 20:00:42 +00:00
'url(' . dcCore::app()->blog->url . dcCore::app()->url->getBase('noodlesmodule') . '/',
2021-09-09 07:12:31 +00:00
file_get_contents($f)
);
}
exit;
}
# Search noodles files like JS, CSS in default-templates subdirectories
private static function searchTplFiles($file)
{
2021-11-01 09:28:22 +00:00
if (strstr($file, '..') !== false) {
return false;
}
2022-11-17 20:00:42 +00:00
$paths = dcCore::app()->tpl->getPath();
2021-11-01 09:28:22 +00:00
foreach ($paths as $path) {
if (preg_match('/tpl(\/|)$/', $path)) {
2021-09-09 07:12:31 +00:00
$path = path::real($path . '/..');
}
2021-09-09 07:12:31 +00:00
if (file_exists($path . '/' . $file)) {
return $path . '/' . $file;
}
}
2021-11-01 09:28:22 +00:00
return false;
}
2022-11-17 20:00:42 +00:00
}