From 10eb79a629278e11a0ed56193adef9895ff73638 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Sun, 19 Jun 2011 15:10:31 +0200 Subject: [PATCH 1/3] add array support to set_config and get_config --- boot.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/boot.php b/boot.php index 5165dc990e..3f69fd0847 100644 --- a/boot.php +++ b/boot.php @@ -1243,8 +1243,10 @@ function get_config($family, $key, $instore = false) { dbesc($key) ); if(count($ret)) { - $a->config[$family][$key] = $ret[0]['v']; - return $ret[0]['v']; + // manage array value + $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']); + $a->config[$family][$key] = $val; + return $val; } else { $a->config[$family][$key] = '!!'; @@ -1258,22 +1260,25 @@ function get_config($family, $key, $instore = false) { if(! function_exists('set_config')) { function set_config($family,$key,$value) { - global $a; + + // manage array value + $dbvalue = (is_array($value)?serialize($value):$value); if(get_config($family,$key,true) === false) { $a->config[$family][$key] = $value; $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ", dbesc($family), dbesc($key), - dbesc($value) + dbesc($dbvalue) ); if($ret) return $value; return $ret; } + $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1", - dbesc($value), + dbesc($dbvalue), dbesc($family), dbesc($key) ); From b650183cd4b82325dcaf74a42386882ef1802750 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Sun, 19 Jun 2011 15:42:06 +0200 Subject: [PATCH 2/3] Save in db if addon define "$addon_plugin_admin" function. Add "plugin_admin" column to "addon" table --- boot.php | 10 +++++++--- database.sql | 3 ++- update.php | 5 ++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/boot.php b/boot.php index 3f69fd0847..555d2405c1 100644 --- a/boot.php +++ b/boot.php @@ -6,7 +6,7 @@ ini_set('pcre.backtrack_limit', 250000); define ( 'FRIENDIKA_VERSION', '2.2.1015' ); define ( 'DFRN_PROTOCOL_VERSION', '2.21' ); -define ( 'DB_UPDATE_VERSION', 1063 ); +define ( 'DB_UPDATE_VERSION', 1064 ); define ( 'EOL', "
\r\n" ); define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' ); @@ -477,9 +477,13 @@ function install_plugin($plugin){ if(function_exists($plugin . '_install')) { $func = $plugin . '_install'; $func(); - $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`) VALUES ( '%s', 1, %d ) ", + + $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0); + + $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ", dbesc($plugin), - intval($t) + intval($t), + $plugin_admin ); } }} diff --git a/database.sql b/database.sql index fa18c50dca..54e5f39831 100644 --- a/database.sql +++ b/database.sql @@ -469,7 +469,8 @@ CREATE TABLE IF NOT EXISTS `addon` ( `name` CHAR( 255 ) NOT NULL , `version` CHAR( 255 ) NOT NULL , `installed` TINYINT( 1 ) NOT NULL DEFAULT '0' , -`timestamp` BIGINT NOT NULL DEFAULT '0' +`timestamp` BIGINT NOT NULL DEFAULT '0' , +`plugin_admin` TINYINT( 1 ) NOT NULL DEFAULT '0' ) ENGINE = MYISAM DEFAULT CHARSET=utf8; diff --git a/update.php b/update.php index e1bc736191..541a12ef56 100644 --- a/update.php +++ b/update.php @@ -1,6 +1,6 @@ Date: Sun, 19 Jun 2011 15:43:10 +0200 Subject: [PATCH 3/3] Change logic in admin.php to load plugin settings form. Add statusnet settings form. --- addon/statusnet/admin.tpl | 16 +++++++++ addon/statusnet/statusnet.php | 64 ++++++++++++++++++++++++++++++++++- addon/twitter/twitter.php | 7 ++-- mod/admin.php | 13 ++++--- 4 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 addon/statusnet/admin.tpl diff --git a/addon/statusnet/admin.tpl b/addon/statusnet/admin.tpl new file mode 100644 index 0000000000..b40adf35a7 --- /dev/null +++ b/addon/statusnet/admin.tpl @@ -0,0 +1,16 @@ +{{ for $sites as $s }} + {{ inc field_input.tpl with $field=$s.sitename }}{{ endinc }} + {{ inc field_input.tpl with $field=$s.apiurl }}{{ endinc }} + {{ inc field_input.tpl with $field=$s.secret }}{{ endinc }} + {{ inc field_input.tpl with $field=$s.key }}{{ endinc }} + {{ if $s.delete }} + {{ inc field_checkbox.tpl with $field=$s.delete }}{{ endinc }} +
+ {{ else }} +

Fill this form to add a new site

+ {{ endif }} + +{{ endfor }} + + +
diff --git a/addon/statusnet/statusnet.php b/addon/statusnet/statusnet.php index e24ebc1ee0..cb0f877646 100644 --- a/addon/statusnet/statusnet.php +++ b/addon/statusnet/statusnet.php @@ -59,7 +59,6 @@ function statusnet_install() { register_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post'); register_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook'); register_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets'); - logger("installed statusnet"); } @@ -350,3 +349,66 @@ function statusnet_post_hook(&$a,&$b) { } } +function statusnet_plugin_admin_post(&$a){ + + $sites = array(); + + foreach($_POST['sitename'] as $id=>$sitename){ + $sitename=trim($sitename); + $apiurl=trim($_POST['apiurl'][$id]); + $secret=trim($_POST['secret'][$id]); + $key=trim($_POST['key'][$id]); + if ($sitename!="" && + $apiurl!="" && + $secret!="" && + $key!="" && + !x($_POST['delete'][$id])){ + + $sites[] = Array( + 'sitename' => $sitename, + 'apiurl' => $apiurl, + 'secret' => $secret, + 'key' => $key + ); + } + } + + $sites = set_config('statusnet','sites', $sites); + +} + +function statusnet_plugin_admin(&$a, &$o){ + + $sites = get_config('statusnet','sites'); + $sitesform=array(); + if (is_array($sites)){ + foreach($sites as $id=>$s){ + $sitesform[] = Array( + 'sitename' => Array("sitename[$id]", "Site name", $s['sitename'], ""), + 'apiurl' => Array("apiurl[$id]", "Api url", $s['apiurl'], ""), + 'secret' => Array("secret[$id]", "Secret", $s['secret'], ""), + 'key' => Array("key[$id]", "Key", $s['key'], ""), + 'delete' => Array("delete[$id]", "Delete", False , "Check to delete this preset"), + ); + } + } + /* empty form to add new site */ + $id++; + $sitesform[] = Array( + 'sitename' => Array("sitename[$id]", "Site name", "", ""), + 'apiurl' => Array("apiurl[$id]", "Api url", "", ""), + 'secret' => Array("secret[$id]", "Secret", "", ""), + 'key' => Array("key[$id]", "Key", "", ""), + ); + + + $t = file_get_contents( dirname(__file__). "/admin.tpl" ); + $o = replace_macros($t, array( + '$submit' => t('Submit'), + + '$sites' => $sitesform, + + )); + + +} diff --git a/addon/twitter/twitter.php b/addon/twitter/twitter.php index 3141cf0c94..aeb9cc9376 100644 --- a/addon/twitter/twitter.php +++ b/addon/twitter/twitter.php @@ -47,7 +47,6 @@ function twitter_install() { register_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post'); register_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook'); register_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets'); - register_hook('plugin_admin', 'addon/twitter/twitter.php', 'plugin_admin'); logger("installed twitter"); } @@ -57,7 +56,6 @@ function twitter_uninstall() { unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post'); unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook'); unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets'); - unregister_hook('plugin_admin', 'addon/twitter/twitter.php', 'plugin_admin'); } function twitter_jot_nets(&$a,&$b) { @@ -242,14 +240,15 @@ function twitter_post_hook(&$a,&$b) { } } } -function plugin_admin_post(&$a){ + +function twitter_plugin_admin_post(&$a){ $consumerkey = ((x($_POST,'consumerkey')) ? notags(trim($_POST['consumerkey'])) : ''); $consumersecret = ((x($_POST,'consumersecret')) ? notags(trim($_POST['consumersecret'])): ''); set_config('twitter','consumerkey',$consumerkey); set_config('twitter','consumersecret',$consumersecret); info( t('Settings updated.'). EOL ); } -function plugin_admin(&$a, &$o){ +function twitter_plugin_admin(&$a, &$o){ $t = file_get_contents( dirname(__file__). "/admin.tpl" ); $o = replace_macros($t, array( '$submit' => t('Submit'), diff --git a/mod/admin.php b/mod/admin.php index 1ae0490e8d..df5b6600ab 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -29,8 +29,9 @@ function admin_post(&$a){ if ($a->argc > 2 && is_file("addon/".$a->argv[2]."/".$a->argv[2].".php")){ @include_once("addon/".$a->argv[2]."/".$a->argv[2].".php"); - if(function_exists('plugin_admin_post')) { - plugin_admin_post($a); + if(function_exists($a->argv[2].'_plugin_admin_post')) { + $func = $a->argv[2].'_plugin_admin_post'; + $func($a); } } goaway($a->get_baseurl() . '/admin/plugins/' . $a->argv[2] ); @@ -65,10 +66,10 @@ function admin_content(&$a) { /* get plugins admin page */ - $r = q("SELECT * FROM `hook` WHERE `hook`='plugin_admin'"); + $r = q("SELECT * FROM `addon` WHERE `plugin_admin`=1"); $aside['plugins_admin']=Array(); foreach ($r as $h){ - $plugin = explode("/",$h['file']); $plugin = $plugin[1]; + $plugin =$h['name']; $aside['plugins_admin'][] = Array($a->get_baseurl()."/admin/plugins/".$plugin, $plugin, "plugin"); // temp plugins with admin $a->plugins_admin[] = $plugin; @@ -526,7 +527,9 @@ function admin_page_plugins(&$a){ $admin_form=""; if (in_array($plugin, $a->plugins_admin)){ - call_hooks('plugin_admin', $admin_form); + @require_once("addon/$plugin/$plugin.php"); + $func = $plugin.'_plugin_admin'; + $func($a, $admin_form); } $t = get_markup_template("admin_plugins_details.tpl");