Addon.php was in DOS format

This commit is contained in:
Michael 2018-02-06 19:35:10 +00:00
parent b0fe83216d
commit 66e3c1836e

View file

@ -1,315 +1,315 @@
<?php <?php
/** /**
* @file src/Core/Addon.php * @file src/Core/Addon.php
*/ */
namespace Friendica\Core; namespace Friendica\Core;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use dba; use dba;
require_once 'include/dba.php'; require_once 'include/dba.php';
/** /**
* Some functions to handle addons * Some functions to handle addons
*/ */
class Addon class Addon
{ {
/** /**
* @brief uninstalls an addon. * @brief uninstalls an addon.
* *
* @param string $addon name of the addon * @param string $addon name of the addon
* @return boolean * @return boolean
*/ */
public static function uninstall($addon) public static function uninstall($addon)
{ {
logger("Addons: uninstalling " . $addon); logger("Addons: uninstalling " . $addon);
dba::delete('addon', ['name' => $addon]); dba::delete('addon', ['name' => $addon]);
@include_once('addon/' . $addon . '/' . $addon . '.php'); @include_once('addon/' . $addon . '/' . $addon . '.php');
if (function_exists($addon . '_uninstall')) { if (function_exists($addon . '_uninstall')) {
$func = $addon . '_uninstall'; $func = $addon . '_uninstall';
$func(); $func();
} }
} }
/** /**
* @brief installs an addon. * @brief installs an addon.
* *
* @param string $addon name of the addon * @param string $addon name of the addon
* @return bool * @return bool
*/ */
public static function install($addon) public static function install($addon)
{ {
// silently fail if addon was removed // silently fail if addon was removed
if (!file_exists('addon/' . $addon . '/' . $addon . '.php')) { if (!file_exists('addon/' . $addon . '/' . $addon . '.php')) {
return false; return false;
} }
logger("Addons: installing " . $addon); logger("Addons: installing " . $addon);
$t = @filemtime('addon/' . $addon . '/' . $addon . '.php'); $t = @filemtime('addon/' . $addon . '/' . $addon . '.php');
@include_once('addon/' . $addon . '/' . $addon . '.php'); @include_once('addon/' . $addon . '/' . $addon . '.php');
if (function_exists($addon . '_install')) { if (function_exists($addon . '_install')) {
$func = $addon . '_install'; $func = $addon . '_install';
$func(); $func();
$addon_admin = (function_exists($addon."_addon_admin") ? 1 : 0); $addon_admin = (function_exists($addon."_addon_admin") ? 1 : 0);
dba::insert('addon', ['name' => $addon, 'installed' => true, dba::insert('addon', ['name' => $addon, 'installed' => true,
'timestamp' => $t, 'plugin_admin' => $addon_admin]); 'timestamp' => $t, 'plugin_admin' => $addon_admin]);
// we can add the following with the previous SQL // we can add the following with the previous SQL
// once most site tables have been updated. // once most site tables have been updated.
// This way the system won't fall over dead during the update. // This way the system won't fall over dead during the update.
if (file_exists('addon/' . $addon . '/.hidden')) { if (file_exists('addon/' . $addon . '/.hidden')) {
dba::update('addon', ['hidden' => true], ['name' => $addon]); dba::update('addon', ['hidden' => true], ['name' => $addon]);
} }
return true; return true;
} else { } else {
logger("Addons: FAILED installing " . $addon); logger("Addons: FAILED installing " . $addon);
return false; return false;
} }
} }
/** /**
* reload all updated addons * reload all updated addons
*/ */
public static function reload() public static function reload()
{ {
$addons = Config::get('system', 'addon'); $addons = Config::get('system', 'addon');
if (strlen($addons)) { if (strlen($addons)) {
$r = dba::select('addon', [], ['installed' => 1]); $r = dba::select('addon', [], ['installed' => 1]);
if (DBM::is_result($r)) { if (DBM::is_result($r)) {
$installed = dba::inArray($r); $installed = dba::inArray($r);
} else { } else {
$installed = []; $installed = [];
} }
$addon_list = explode(',', $addons); $addon_list = explode(',', $addons);
if (count($addon_list)) { if (count($addon_list)) {
foreach ($addon_list as $addon) { foreach ($addon_list as $addon) {
$addon = trim($addon); $addon = trim($addon);
$fname = 'addon/' . $addon . '/' . $addon . '.php'; $fname = 'addon/' . $addon . '/' . $addon . '.php';
if (file_exists($fname)) { if (file_exists($fname)) {
$t = @filemtime($fname); $t = @filemtime($fname);
foreach ($installed as $i) { foreach ($installed as $i) {
if (($i['name'] == $addon) && ($i['timestamp'] != $t)) { if (($i['name'] == $addon) && ($i['timestamp'] != $t)) {
logger('Reloading addon: ' . $i['name']); logger('Reloading addon: ' . $i['name']);
@include_once($fname); @include_once($fname);
if (function_exists($addon . '_uninstall')) { if (function_exists($addon . '_uninstall')) {
$func = $addon . '_uninstall'; $func = $addon . '_uninstall';
$func(); $func();
} }
if (function_exists($addon . '_install')) { if (function_exists($addon . '_install')) {
$func = $addon . '_install'; $func = $addon . '_install';
$func(); $func();
} }
dba::update('addon', ['timestamp' => $t], ['id' => $i['id']]); dba::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
} }
} }
} }
} }
} }
} }
} }
/** /**
* @brief check if addon is enabled * @brief check if addon is enabled
* *
* @param string $addon * @param string $addon
* @return boolean * @return boolean
*/ */
public static function isEnabled($addon) public static function isEnabled($addon)
{ {
return dba::exists('addon', ['installed' => true, 'name' => $addon]); return dba::exists('addon', ['installed' => true, 'name' => $addon]);
} }
/** /**
* @brief registers a hook. * @brief registers a hook.
* *
* @param string $hook the name of the hook * @param string $hook the name of the hook
* @param string $file the name of the file that hooks into * @param string $file the name of the file that hooks into
* @param string $function the name of the function that the hook will call * @param string $function the name of the function that the hook will call
* @param int $priority A priority (defaults to 0) * @param int $priority A priority (defaults to 0)
* @return mixed|bool * @return mixed|bool
*/ */
public static function registerHook($hook, $file, $function, $priority = 0) public static function registerHook($hook, $file, $function, $priority = 0)
{ {
$condition = ['hook' => $hook, 'file' => $file, 'function' => $function]; $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
$exists = dba::exists('hook', $condition); $exists = dba::exists('hook', $condition);
if ($exists) { if ($exists) {
return true; return true;
} }
$r = dba::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]); $r = dba::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
return $r; return $r;
} }
/** /**
* @brief unregisters a hook. * @brief unregisters a hook.
* *
* @param string $hook the name of the hook * @param string $hook the name of the hook
* @param string $file the name of the file that hooks into * @param string $file the name of the file that hooks into
* @param string $function the name of the function that the hook called * @param string $function the name of the function that the hook called
* @return array * @return array
*/ */
public static function unregisterHook($hook, $file, $function) public static function unregisterHook($hook, $file, $function)
{ {
$condition = ['hook' => $hook, 'file' => $file, 'function' => $function]; $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
$r = dba::delete('hook', $condition); $r = dba::delete('hook', $condition);
return $r; return $r;
} }
/** /**
* Load hooks * Load hooks
*/ */
public static function loadHooks() public static function loadHooks()
{ {
$a = get_app(); $a = get_app();
$a->hooks = []; $a->hooks = [];
$r = dba::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]); $r = dba::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
while ($rr = dba::fetch($r)) { while ($rr = dba::fetch($r)) {
if (! array_key_exists($rr['hook'], $a->hooks)) { if (! array_key_exists($rr['hook'], $a->hooks)) {
$a->hooks[$rr['hook']] = []; $a->hooks[$rr['hook']] = [];
} }
$a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']]; $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
} }
dba::close($r); dba::close($r);
} }
/** /**
* @brief Calls a hook. * @brief Calls a hook.
* *
* Use this function when you want to be able to allow a hook to manipulate * Use this function when you want to be able to allow a hook to manipulate
* the provided data. * the provided data.
* *
* @param string $name of the hook to call * @param string $name of the hook to call
* @param string|array &$data to transmit to the callback handler * @param string|array &$data to transmit to the callback handler
*/ */
public static function callHooks($name, &$data = null) public static function callHooks($name, &$data = null)
{ {
$a = get_app(); $a = get_app();
if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) { if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
foreach ($a->hooks[$name] as $hook) { foreach ($a->hooks[$name] as $hook) {
self::callSingleHook($a, $name, $hook, $data); self::callSingleHook($a, $name, $hook, $data);
} }
} }
} }
/** /**
* @brief Calls a single hook. * @brief Calls a single hook.
* *
* @param string $name of the hook to call * @param string $name of the hook to call
* @param array $hook Hook data * @param array $hook Hook data
* @param string|array &$data to transmit to the callback handler * @param string|array &$data to transmit to the callback handler
*/ */
public static function callSingleHook($a, $name, $hook, &$data = null) public static function callSingleHook($a, $name, $hook, &$data = null)
{ {
// Don't run a theme's hook if the user isn't using the theme // Don't run a theme's hook if the user isn't using the theme
if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false) { if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false) {
return; return;
} }
@include_once($hook[0]); @include_once($hook[0]);
if (function_exists($hook[1])) { if (function_exists($hook[1])) {
$func = $hook[1]; $func = $hook[1];
$func($a, $data); $func($a, $data);
} else { } else {
// remove orphan hooks // remove orphan hooks
$condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]]; $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
dba::delete('hook', $condition); dba::delete('hook', $condition);
} }
} }
/** /**
* check if an app_menu hook exist for addon $name. * check if an app_menu hook exist for addon $name.
* Return true if the addon is an app * Return true if the addon is an app
*/ */
public static function isApp($name) public static function isApp($name)
{ {
$a = get_app(); $a = get_app();
if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) { if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) {
foreach ($a->hooks['app_menu'] as $hook) { foreach ($a->hooks['app_menu'] as $hook) {
if ($hook[0] == 'addon/'.$name.'/'.$name.'.php') { if ($hook[0] == 'addon/'.$name.'/'.$name.'.php') {
return true; return true;
} }
} }
} }
return false; return false;
} }
/** /**
* @brief Parse addon comment in search of addon infos. * @brief Parse addon comment in search of addon infos.
* *
* like * like
* \code * \code
* * Name: addon * * Name: addon
* * Description: An addon which plugs in * * Description: An addon which plugs in
* . * Version: 1.2.3 * . * Version: 1.2.3
* * Author: John <profile url> * * Author: John <profile url>
* * Author: Jane <email> * * Author: Jane <email>
* * Maintainer: Jess <email> * * Maintainer: Jess <email>
* * * *
* *\endcode * *\endcode
* @param string $addon the name of the addon * @param string $addon the name of the addon
* @return array with the addon information * @return array with the addon information
*/ */
public static function getInfo($addon) public static function getInfo($addon)
{ {
$a = get_app(); $a = get_app();
$info = [ $info = [
'name' => $addon, 'name' => $addon,
'description' => "", 'description' => "",
'author' => [], 'author' => [],
'maintainer' => [], 'maintainer' => [],
'version' => "", 'version' => "",
'status' => "" 'status' => ""
]; ];
if (!is_file("addon/$addon/$addon.php")) { if (!is_file("addon/$addon/$addon.php")) {
return $info; return $info;
} }
$stamp1 = microtime(true); $stamp1 = microtime(true);
$f = file_get_contents("addon/$addon/$addon.php"); $f = file_get_contents("addon/$addon/$addon.php");
$a->save_timestamp($stamp1, "file"); $a->save_timestamp($stamp1, "file");
$r = preg_match("|/\*.*\*/|msU", $f, $m); $r = preg_match("|/\*.*\*/|msU", $f, $m);
if ($r) { if ($r) {
$ll = explode("\n", $m[0]); $ll = explode("\n", $m[0]);
foreach ($ll as $l) { foreach ($ll as $l) {
$l = trim($l, "\t\n\r */"); $l = trim($l, "\t\n\r */");
if ($l != "") { if ($l != "") {
list($type, $v) = array_map("trim", explode(":", $l, 2)); list($type, $v) = array_map("trim", explode(":", $l, 2));
$type = strtolower($type); $type = strtolower($type);
if ($type == "author" || $type == "maintainer") { if ($type == "author" || $type == "maintainer") {
$r = preg_match("|([^<]+)<([^>]+)>|", $v, $m); $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
if ($r) { if ($r) {
$info[$type][] = ['name' => $m[1], 'link' => $m[2]]; $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
} else { } else {
$info[$type][] = ['name' => $v]; $info[$type][] = ['name' => $v];
} }
} else { } else {
if (array_key_exists($type, $info)) { if (array_key_exists($type, $info)) {
$info[$type] = $v; $info[$type] = $v;
} }
} }
} }
} }
} }
return $info; return $info;
} }
} }