Update hook names

update hook names and other plugin references.
This commit is contained in:
Adam Magness 2018-01-20 08:22:18 -05:00
parent ac374f7773
commit 5d03735238
4 changed files with 286 additions and 283 deletions

View file

@ -62,8 +62,8 @@ function admin_post(App $a)
if ($a->argc > 2 && if ($a->argc > 2 &&
is_file("addon/" . $a->argv[2] . "/" . $a->argv[2] . ".php")) { is_file("addon/" . $a->argv[2] . "/" . $a->argv[2] . ".php")) {
@include_once("addon/" . $a->argv[2] . "/" . $a->argv[2] . ".php"); @include_once("addon/" . $a->argv[2] . "/" . $a->argv[2] . ".php");
if (function_exists($a->argv[2] . '_plugin_admin_post')) { if (function_exists($a->argv[2] . '_addon_admin_post')) {
$func = $a->argv[2] . '_plugin_admin_post'; $func = $a->argv[2] . '_addon_admin_post';
$func($a); $func($a);
} }
} }
@ -1790,7 +1790,7 @@ function admin_page_addons(App $a)
$admin_form = ""; $admin_form = "";
if (in_array($addon, $a->addons_admin)) { if (in_array($addon, $a->addons_admin)) {
@require_once("addon/$addon/$addon.php"); @require_once("addon/$addon/$addon.php");
$func = $addon . '_plugin_admin'; $func = $addon . '_addon_admin';
$func($a, $admin_form); $func($a, $admin_form);
} }

View file

@ -170,23 +170,25 @@ function settings_post(App $a)
icon='%s', icon='%s',
uid=%d uid=%d
WHERE client_id='%s'", WHERE client_id='%s'",
dbesc($key), dbesc($key),
dbesc($secret), dbesc($secret),
dbesc($name), dbesc($name),
dbesc($redirect), dbesc($redirect),
dbesc($icon), dbesc($icon),
local_user(), local_user(),
dbesc($key)); dbesc($key)
);
} else { } else {
q("INSERT INTO clients q("INSERT INTO clients
(client_id, pw, name, redirect_uri, icon, uid) (client_id, pw, name, redirect_uri, icon, uid)
VALUES ('%s', '%s', '%s', '%s', '%s',%d)", VALUES ('%s', '%s', '%s', '%s', '%s',%d)",
dbesc($key), dbesc($key),
dbesc($secret), dbesc($secret),
dbesc($name), dbesc($name),
dbesc($redirect), dbesc($redirect),
dbesc($icon), dbesc($icon),
local_user()); local_user()
);
} }
} }
goaway(System::baseUrl(true)."/settings/oauth/"); goaway(System::baseUrl(true)."/settings/oauth/");
@ -196,12 +198,12 @@ function settings_post(App $a)
if (($a->argc > 1) && ($a->argv[1] == 'addon')) { if (($a->argc > 1) && ($a->argv[1] == 'addon')) {
check_form_security_token_redirectOnErr('/settings/addon', 'settings_addon'); check_form_security_token_redirectOnErr('/settings/addon', 'settings_addon');
Addon::callHooks('plugin_settings_post', $_POST); Addon::callHooks('addon_settings_post', $_POST);
return; return;
} }
if (($a->argc > 1) && ($a->argv[1] == 'connectors')) { if (($a->argc > 1) && ($a->argv[1] == 'connectors'))
{
check_form_security_token_redirectOnErr('/settings/connectors', 'settings_connectors'); check_form_security_token_redirectOnErr('/settings/connectors', 'settings_connectors');
if (x($_POST, 'general-submit')) { if (x($_POST, 'general-submit')) {
@ -749,12 +751,12 @@ function settings_content(App $a)
if (($a->argc > 1) && ($a->argv[1] === 'addon')) { if (($a->argc > 1) && ($a->argv[1] === 'addon')) {
$settings_addons = ""; $settings_addons = "";
$r = q("SELECT * FROM `hook` WHERE `hook` = 'plugin_settings' "); $r = q("SELECT * FROM `hook` WHERE `hook` = 'addon_settings' ");
if (!DBM::is_result($r)) { if (!DBM::is_result($r)) {
$settings_addons = t('No Addon settings configured'); $settings_addons = t('No Addon settings configured');
} }
Addon::callHooks('plugin_settings', $settings_addons); Addon::callHooks('addon_settings', $settings_addons);
$tpl = get_markup_template('settings/addons.tpl'); $tpl = get_markup_template('settings/addons.tpl');

View file

@ -17,298 +17,299 @@ require_once 'include/dba.php';
*/ */
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."_plugin_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]);
if (DBM::is_result($r)) {
$installed = $r;
} else {
$installed = [];
}
$r = dba::select('addon', [], ['installed' => 1]); $addon_list = explode(',', $addons);
if (DBM::is_result($r)) {
$installed = $r;
} else {
$installed = [];
}
$addon_list = explode(',', $addons); if (count($addon_list)) {
foreach ($addon_list as $addon) {
$addon = trim($addon);
$fname = 'addon/' . $addon . '/' . $addon . '.php';
if (count($addon_list)) { if (file_exists($fname)) {
foreach ($addon_list as $addon) { $t = @filemtime($fname);
foreach ($installed as $i) {
if (($i['name'] == $addon) && ($i['timestamp'] != $t)) {
logger('Reloading addon: ' . $i['name']);
@include_once($fname);
$addon = trim($addon); if (function_exists($addon . '_uninstall')) {
$func = $addon . '_uninstall';
$func();
}
if (function_exists($addon . '_install')) {
$func = $addon . '_install';
$func();
}
dba::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
}
}
}
}
}
}
}
$fname = 'addon/' . $addon . '/' . $addon . '.php'; /**
* @brief check if addon is enabled
if (file_exists($fname)) { *
$t = @filemtime($fname); * @param string $addon
foreach ($installed as $i) { * @return boolean
if (($i['name'] == $addon) && ($i['timestamp'] != $t)) { */
logger('Reloading addon: ' . $i['name']); public static function isEnabled($addon)
@include_once($fname); {
return dba::exists('addon', ['installed' => true, 'name' => $addon]);
if (function_exists($addon . '_uninstall')) { }
$func = $addon . '_uninstall';
$func();
}
if (function_exists($addon . '_install')) {
$func = $addon . '_install';
$func();
}
dba::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
}
}
}
}
}
}
}
/**
* @brief check if addon is enabled
*
* @param string $addon
* @return boolean
*/
public static function isEnabled($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
*/
public static function loadHooks()
{
$a = get_app();
$a->hooks = [];
$r = dba::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
public static function loadHooks() { while ($rr = dba::fetch($r)) {
$a = get_app(); if (! array_key_exists($rr['hook'], $a->hooks)) {
$a->hooks = []; $a->hooks[$rr['hook']] = [];
$r = dba::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]); }
$a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
}
dba::close($r);
}
while ($rr = dba::fetch($r)) { /**
if (! array_key_exists($rr['hook'],$a->hooks)) { * @brief Calls a hook.
$a->hooks[$rr['hook']] = []; *
} * Use this function when you want to be able to allow a hook to manipulate
$a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']]; * the provided data.
} *
dba::close($r); * @param string $name of the hook to call
} * @param string|array &$data to transmit to the callback handler
*/
public static function callHooks($name, &$data = null)
{
$a = get_app();
/** if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
* @brief Calls a hook. foreach ($a->hooks[$name] as $hook) {
* self::callSingleHook($a, $name, $hook, $data);
* Use this function when you want to be able to allow a hook to manipulate }
* the provided data. }
* }
* @param string $name of the hook to call
* @param string|array &$data to transmit to the callback handler
*/
public static function callHooks($name, &$data = null)
{
$a = get_app();
if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) { /**
foreach ($a->hooks[$name] as $hook) { * @brief Calls a single hook.
self::callSingleHook($a, $name, $hook, $data); *
} * @param string $name of the hook to call
} * @param array $hook Hook data
} * @param string|array &$data to transmit to the callback handler
*/
public static function callSingleHook($a, $name, $hook, &$data = null)
{
// 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) {
return;
}
/** @include_once($hook[0]);
* @brief Calls a single hook. if (function_exists($hook[1])) {
* $func = $hook[1];
* @param string $name of the hook to call $func($a, $data);
* @param array $hook Hook data } else {
* @param string|array &$data to transmit to the callback handler // remove orphan hooks
*/ $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
public static function callSingleHook($a, $name, $hook, &$data = null) dba::delete('hook', $condition);
{ }
// 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)
return;
@include_once($hook[0]); /**
if (function_exists($hook[1])) { * check if an app_menu hook exist for addon $name.
$func = $hook[1]; * Return true if the addon is an app
$func($a, $data); */
} else { public static function isApp($name)
// remove orphan hooks {
$condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]]; $a = get_app();
dba::delete('hook', $condition);
}
}
/** if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) {
* check if an app_menu hook exist for addon $name. foreach ($a->hooks['app_menu'] as $hook) {
* Return true if the addon is an app if ($hook[0] == 'addon/'.$name.'/'.$name.'.php') {
*/ return true;
function isApp($name) }
{ }
$a = get_app(); }
if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) { return false;
foreach ($a->hooks['app_menu'] as $hook) { }
if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
return true;
}
}
return false; /**
} * @brief Parse addon comment in search of addon infos.
*
* like
* \code
*...* Name: addon
* * Description: An addon which plugs in
* . * Version: 1.2.3
* * Author: John <profile url>
* * Author: Jane <email>
* *
* *\endcode
* @param string $addon the name of the addon
* @return array with the addon information
*/
/** public static function getInfo($addon)
* @brief Parse addon comment in search of addon infos. {
* $a = get_app();
* like
* \code
*...* Name: addon
* * Description: An addon which plugs in
* . * Version: 1.2.3
* * Author: John <profile url>
* * Author: Jane <email>
* *
* *\endcode
* @param string $addon the name of the addon
* @return array with the addon information
*/
public static function getInfo($addon) { $info=[
'name' => $addon,
'description' => "",
'author' => [],
'version' => "",
'status' => ""
];
$a = get_app(); if (!is_file("addon/$addon/$addon.php")) {
return $info;
}
$info=[ $stamp1 = microtime(true);
'name' => $addon, $f = file_get_contents("addon/$addon/$addon.php");
'description' => "", $a->save_timestamp($stamp1, "file");
'author' => [],
'version' => "",
'status' => ""
];
if (!is_file("addon/$addon/$addon.php")) { $r = preg_match("|/\*.*\*/|msU", $f, $m);
return $info;
}
$stamp1 = microtime(true); if ($r) {
$f = file_get_contents("addon/$addon/$addon.php"); $ll = explode("\n", $m[0]);
$a->save_timestamp($stamp1, "file"); foreach ($ll as $l) {
$l = trim($l, "\t\n\r */");
$r = preg_match("|/\*.*\*/|msU", $f, $m); if ($l != "") {
list($k,$v) = array_map("trim", explode(":", $l, 2));
if ($r) { $k= strtolower($k);
$ll = explode("\n", $m[0]); if ($k == "author") {
foreach ( $ll as $l ) { $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
$l = trim($l,"\t\n\r */"); if ($r) {
if ($l != "") { $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];
list($k,$v) = array_map("trim", explode(":",$l,2)); } else {
$k= strtolower($k); $info['author'][] = ['name'=>$v];
if ($k == "author") { }
$r=preg_match("|([^<]+)<([^>]+)>|", $v, $m); } else {
if ($r) { if (array_key_exists($k, $info)) {
$info['author'][] = ['name'=>$m[1], 'link'=>$m[2]]; $info[$k]=$v;
} else { }
$info['author'][] = ['name'=>$v]; }
} }
} else { }
if (array_key_exists($k,$info)) { }
$info[$k]=$v; return $info;
} }
}
}
}
}
return $info;
}
} }

View file

@ -661,7 +661,7 @@ class DBStructure {
$database = []; $database = [];
$database["addon"] = [ $database["addon"] = [
"comment" => "registered plugins", "comment" => "registered addons",
"fields" => [ "fields" => [
"id" => ["type" => "int", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => ""], "id" => ["type" => "int", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => ""],
"name" => ["type" => "varchar(190)", "not null" => "1", "default" => "", "comment" => ""], "name" => ["type" => "varchar(190)", "not null" => "1", "default" => "", "comment" => ""],
@ -1081,7 +1081,7 @@ class DBStructure {
] ]
]; ];
$database["hook"] = [ $database["hook"] = [
"comment" => "plugin hook registry", "comment" => "addon hook registry",
"fields" => [ "fields" => [
"id" => ["type" => "int", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => ""], "id" => ["type" => "int", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => ""],
"hook" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "hook" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],