diff options
Diffstat (limited to 'include/plugin.php')
-rwxr-xr-x | include/plugin.php | 169 |
1 files changed, 110 insertions, 59 deletions
diff --git a/include/plugin.php b/include/plugin.php index 8dceb8fb1..cb206d944 100755 --- a/include/plugin.php +++ b/include/plugin.php @@ -5,8 +5,6 @@ * @brief Some functions to handle addons and themes. */ -require_once("include/smarty.php"); - /** * @brief unloads an addon. @@ -43,7 +41,7 @@ function uninstall_plugin($plugin) { $func(); } - q("DELETE FROM `addon` WHERE `name` = '%s' ", + q("DELETE FROM `addon` WHERE `aname` = '%s' ", dbesc($plugin) ); } @@ -68,7 +66,7 @@ function install_plugin($plugin) { $plugin_admin = (function_exists($plugin . '_plugin_admin') ? 1 : 0); - q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ", + q("INSERT INTO `addon` (`aname`, `installed`, `tstamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ", dbesc($plugin), intval($t), $plugin_admin @@ -113,7 +111,7 @@ function load_plugin($plugin) { } function plugin_is_installed($name) { - $r = q("select name from addon where name = '%s' and installed = 1 limit 1", + $r = q("select aname from addon where aname = '%s' and installed = 1 limit 1", dbesc($name) ); if($r) @@ -145,8 +143,8 @@ function reload_plugins() { if(file_exists($fname)) { $t = @filemtime($fname); foreach($installed as $i) { - if(($i['name'] == $pl) && ($i['timestamp'] != $t)) { - logger('Reloading plugin: ' . $i['name']); + if(($i['aname'] == $pl) && ($i['tstamp'] != $t)) { + logger('Reloading plugin: ' . $i['aname']); @include_once($fname); if(function_exists($pl . '_unload')) { @@ -157,7 +155,7 @@ function reload_plugins() { $func = $pl . '_load'; $func(); } - q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d", + q("UPDATE `addon` SET `tstamp` = %d WHERE `id` = %d", intval($t), intval($i['id']) ); @@ -169,6 +167,12 @@ function reload_plugins() { } } +function visible_plugin_list() { + $r = q("select * from addon where hidden = 0 order by aname asc"); + return(($r) ? ids_to_array($r,'aname') : array()); +} + + /** * @brief registers a hook. @@ -180,15 +184,15 @@ function reload_plugins() { * @return mixed|bool */ function register_hook($hook, $file, $function, $priority = 0) { - $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1", + $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `fn` = '%s' LIMIT 1", dbesc($hook), dbesc($file), dbesc($function) ); - if(count($r)) + if($r) return true; - $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' )", + $r = q("INSERT INTO `hook` (`hook`, `file`, `fn`, `priority`) VALUES ( '%s', '%s', '%s', '%s' )", dbesc($hook), dbesc($file), dbesc($function), @@ -208,7 +212,7 @@ function register_hook($hook, $file, $function, $priority = 0) { * @return array */ function unregister_hook($hook, $file, $function) { - $r = q("DELETE FROM hook WHERE hook = '%s' AND `file` = '%s' AND `function` = '%s'", + $r = q("DELETE FROM hook WHERE hook = '%s' AND `file` = '%s' AND `fn` = '%s'", dbesc($hook), dbesc($file), dbesc($function) @@ -226,9 +230,8 @@ function unregister_hook($hook, $file, $function) { function load_hooks() { - $a = get_app(); -// if(! is_array(App::$hooks)) - App::$hooks = array(); + + App::$hooks = array(); $r = q("SELECT * FROM hook WHERE true ORDER BY priority DESC"); if($r) { @@ -236,10 +239,10 @@ function load_hooks() { if(! array_key_exists($rr['hook'],App::$hooks)) App::$hooks[$rr['hook']] = array(); - App::$hooks[$rr['hook']][] = array($rr['file'],$rr['function']); + App::$hooks[$rr['hook']][] = array($rr['file'],$rr['fn'],$rr['priority'],$rr['hook_version']); } } -//logger('hooks: ' . print_r(App::$hooks,true)); + //logger('hooks: ' . print_r(App::$hooks,true)); } /** @@ -259,15 +262,15 @@ function load_hooks() { * @param string $fn * function name of callback handler */ -function insert_hook($hook, $fn) { - $a = get_app(); +function insert_hook($hook, $fn, $version = 0, $priority = 0) { + if(! is_array(App::$hooks)) App::$hooks = array(); if(! array_key_exists($hook, App::$hooks)) App::$hooks[$hook] = array(); - App::$hooks[$hook][] = array('', $fn); + App::$hooks[$hook][] = array('', $fn, $priority, $version); } /** @@ -280,23 +283,41 @@ function insert_hook($hook, $fn) { * @param string|array &$data to transmit to the callback handler */ function call_hooks($name, &$data = null) { - $a = get_app(); - + $a = 0; if((is_array(App::$hooks)) && (array_key_exists($name, App::$hooks))) { foreach(App::$hooks[$name] as $hook) { + $origfn = $hook[1]; if($hook[0]) @include_once($hook[0]); + if(preg_match('|^a:[0-9]+:{.*}$|s', $hook[1])) { + $hook[1] = unserialize($hook[1]); + } + elseif(strpos($hook[1],'::')) { + // We shouldn't need to do this, but it appears that PHP + // isn't able to directly execute a string variable with a class + // method in the manner we are attempting it, so we'll + // turn it into an array. + $hook[1] = explode('::',$hook[1]); + } - if(function_exists($hook[1])) { + if(is_callable($hook[1])) { $func = $hook[1]; - $func($a, $data); - } else { - - q("DELETE FROM hook WHERE hook = '%s' AND file = '%s' AND function = '%s'", - dbesc($name), - dbesc($hook[0]), - dbesc($hook[1]) - ); + if($hook[3]) + $func($data); + else + $func($a, $data); + } + else { + + // Don't do any DB write calls if we're currently logging a possibly failed DB call. + if(! DBA::$logging) { + // The hook should be removed so we don't process it. + q("DELETE FROM hook WHERE hook = '%s' AND file = '%s' AND fn = '%s'", + dbesc($name), + dbesc($hook[0]), + dbesc($origfn) + ); + } } } } @@ -491,7 +512,7 @@ function get_theme_info($theme){ * @return string */ function get_theme_screenshot($theme) { - $a = get_app(); + $exts = array('.png', '.jpg'); foreach($exts as $ext) { if(file_exists('view/theme/' . $theme . '/img/screenshot' . $ext)) @@ -512,7 +533,7 @@ function head_add_css($src, $media = 'screen') { } function head_remove_css($src, $media = 'screen') { - $a = get_app(); + $index = array_search(array($src, $media), App::$css_sources); if ($index !== false) unset(App::$css_sources[$index]); @@ -530,13 +551,22 @@ function head_get_css() { } function format_css_if_exists($source) { - if (strpos($source[0], '/') !== false) + $path_prefix = script_path() . '/'; + + if (strpos($source[0], '/') !== false) { + // The source is a URL $path = $source[0]; - else + // If the url starts with // then it's an absolute URL + if($source[0][0] === '/' && $source[0][1] === '/') $path_prefix = ''; + } else { + // It's a file from the theme $path = theme_include($source[0]); + } - if($path) - return '<link rel="stylesheet" href="' . script_path() . '/' . $path . '" type="text/css" media="' . $source[1] . '">' . "\r\n"; + if($path) { + $qstring = ((parse_url($path, PHP_URL_QUERY)) ? '&' : '?') . 'v=' . STD_VERSION; + return '<link rel="stylesheet" href="' . $path_prefix . $path . $qstring . '" type="text/css" media="' . $source[1] . '">' . "\r\n"; + } } /* @@ -575,26 +605,38 @@ function script_path() { return $scheme . '://' . $hostname; } -function head_add_js($src) { - App::$js_sources[] = $src; +function head_add_js($src, $priority = 0) { + if(! is_array(App::$js_sources[$priority])) + App::$js_sources[$priority] = array(); + App::$js_sources[$priority][] = $src; } -function head_remove_js($src) { - $a = get_app(); - $index = array_search($src, App::$js_sources); +function head_remove_js($src, $priority = 0) { + + $index = array_search($src, App::$js_sources[$priority]); if($index !== false) - unset(App::$js_sources[$index]); + unset(App::$js_sources[$priority][$index]); } +// We should probably try to register main.js with a high priority, but currently we handle it +// separately and put it at the end of the html head block in case any other javascript is +// added outside the head_add_js construct. + function head_get_js() { + $str = ''; - $sources = App::$js_sources; - if(count($sources)) - foreach($sources as $source) { - if($source === 'main.js') - continue; - $str .= format_js_if_exists($source); + if(App::$js_sources) { + ksort(App::$js_sources,SORT_NUMERIC); + foreach(App::$js_sources as $sources) { + if(count($sources)) { + foreach($sources as $source) { + if($src === 'main.js') + continue; + $str .= format_js_if_exists($source); + } + } } + } return $str; } @@ -608,17 +650,25 @@ function head_get_main_js() { } function format_js_if_exists($source) { - if(strpos($source,'/') !== false) + $path_prefix = script_path() . '/'; + + if(strpos($source,'/') !== false) { + // The source is a URL $path = $source; - else + // If the url starts with // then it's an absolute URL + if($source[0] === '/' && $source[1] === '/') $path_prefix = ''; + } else { + // It's a file from the theme $path = theme_include($source); - if($path) - return '<script src="' . script_path() . '/' . $path . '" ></script>' . "\r\n" ; + } + if($path) { + $qstring = ((parse_url($path, PHP_URL_QUERY)) ? '&' : '?') . 'v=' . STD_VERSION; + return '<script src="' . $path_prefix . $path . $qstring . '" ></script>' . "\r\n" ; + } } function theme_include($file, $root = '') { - $a = get_app(); // Make sure $root ends with a slash / if it's not blank if($root !== '' && $root[strlen($root)-1] !== '/') @@ -631,12 +681,13 @@ function theme_include($file, $root = '') { else $parent = 'NOPATH'; - $theme = current_theme(); + $theme = Zotlabs\Render\Theme::current(); + $thname = $theme[0]; $ext = substr($file,strrpos($file,'.')+1); $paths = array( - "{$root}view/theme/$theme/$ext/$file", + "{$root}view/theme/$thname/$ext/$file", "{$root}view/theme/$parent/$ext/$file", "{$root}view/site/$ext/$file", "{$root}view/$ext/$file", @@ -655,7 +706,7 @@ function theme_include($file, $root = '') { function get_intltext_template($s, $root = '') { - $a = get_app(); + $t = App::template_engine(); $template = $t->get_intltext_template($s, $root); @@ -664,7 +715,7 @@ function get_intltext_template($s, $root = '') { function get_markup_template($s, $root = '') { - $a = get_app(); + $t = App::template_engine(); $template = $t->get_markup_template($s, $root); return $template; @@ -678,4 +729,4 @@ function folder_exists($folder) // If it exist, check if it's a directory return (($path !== false) && is_dir($path)) ? $path : false; -}
\ No newline at end of file +} |