From 0f7008a02e84515e83c4399fe5736ed8d2dc8796 Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Wed, 23 Mar 2011 13:47:05 +0100 Subject: [PATCH 1/5] Friendika widgets. --- addon/widgets/widgets.js | 61 ++++++++++++++ addon/widgets/widgets.php | 169 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 addon/widgets/widgets.js create mode 100644 addon/widgets/widgets.php diff --git a/addon/widgets/widgets.js b/addon/widgets/widgets.js new file mode 100644 index 0000000000..7a6cdeb0a0 --- /dev/null +++ b/addon/widgets/widgets.js @@ -0,0 +1,61 @@ +/** + * @author Fabio Comuni + */ + +var f9a_widget = { + entrypoint : "$entrypoint", + key : "$key", + widgetid: "$widget_id", + xmlhttp : null, + + getXHRObj : function(){ + if (window.XMLHttpRequest) { + // code for IE7+, Firefox, Chrome, Opera, Safari + this.xmlhttp = new XMLHttpRequest(); + } else { + // code for IE6, IE5 + this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); + } + }, + + dorequest : function(args, cb) { + if (args===null) args = new Array(); + args['k']=this.key; + args['s']=window.location; + var urlencodedargs = new Array(); + for(k in args){ urlencodedargs.push( encodeURIComponent(k)+"="+encodeURIComponent(args[k]) ); } + + var url = this.entrypoint + "?"+ urlencodedargs.join("&"); + + this.xmlhttp.open("GET", url ,true); + this.xmlhttp.send(); + this.xmlhttp.onreadystatechange=function(){ + if (this.readyState==4){ + if (this.status==200) { + cb(this.responseText); + } else { + document.getElementById(f9a_widget.widgetid).innerHTML="Error loading widget."; + } + } + } + + }, + + requestcb: function(responseText) { + document.getElementById(f9a_widget.widgetid).innerHTML=responseText; + }, + + load : function (){ + this.getXHRObj(); + this.dorequest(null, this.requestcb); + } + +}; + +(function() { + f9a_widget.load(); +})(); + +document.writeln("
"); +document.writeln(""); +document.writeln("
"); diff --git a/addon/widgets/widgets.php b/addon/widgets/widgets.php new file mode 100644 index 0000000000..98567753d8 --- /dev/null +++ b/addon/widgets/widgets.php @@ -0,0 +1,169 @@ +Widgets +
+ + +
+
+
+ + + '.$key.' +
+
+ + + +
+ +
+ '; + + if ($key!='' and $site!='') { + $o.='

Widgets:

+ + '; + } + +} + +function widgets_module() { + return; +} + +function _abs_url($s){ + $a = get_app(); + return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s); +} + + +function widgets_content(&$a) { + + if (!isset($_GET['k'])) { + if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();} + return; + } + + $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig WHERE v='%s')AND cat='widgets'", + dbesc($_GET['k']) + ); + if (!count($r)){ + if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();} + return; + } + $conf = array(); + $conf['uid'] = $r[0]['uid']; + foreach($r as $e) { $conf[$e['k']]=$e['v']; } + + $o = ""; + +// echo "
"; var_dump($a->argv); die();
+	if ($a->argv[2]=="cb"){
+		switch($a->argv[1]) {
+			case 'friends': 
+				widget_friends_content($a, $o, $conf);
+				break;
+		}
+		
+	} else {
+
+		
+		if (isset($_GET['p'])) {
+			$o .= "";
+			$o .= "

Preview Widget

"; + $o .= ''. t("Plugin Settings") .''; + $o .= "

"; + $o .= " +

+

Copy and paste this code

+ " + + .htmlspecialchars('') + .""; + return $o; + } + + } + + echo $o; + killme(); +} + + +function widget_friends_content(&$a, &$o, $conf){ + if (!local_user()){ + if (!isset($_GET['s'])) + header('HTTP/1.0 400 Bad Request'); + + if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site']) + header('HTTP/1.0 400 Bad Request'); + } + $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile` + LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid` + WHERE `user`.`uid` = %s AND `profile`.`is-default` = 1 LIMIT 1", + intval($conf['uid']) + ); + + if(!count($r)) return; + $a->profile = $r[0]; + + $o .= _abs_url(contact_block()); + $o .= "profile['nickname']."'>". t('Connect on Friendika!') .""; + + +} + +?> From 7b59ea7b130f959460680e817f05bdf47764c96a Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Wed, 23 Mar 2011 15:32:18 +0100 Subject: [PATCH 2/5] add some style to the widget --- addon/widgets/widgets.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/addon/widgets/widgets.php b/addon/widgets/widgets.php index 98567753d8..7c75c66672 100644 --- a/addon/widgets/widgets.php +++ b/addon/widgets/widgets.php @@ -160,6 +160,10 @@ function widget_friends_content(&$a, &$o, $conf){ if(!count($r)) return; $a->profile = $r[0]; + $o .= ""; $o .= _abs_url(contact_block()); $o .= "profile['nickname']."'>". t('Connect on Friendika!') .""; From cd34051b68441fc8d0b786b0eadbd5d1afe81452 Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Tue, 29 Mar 2011 14:12:06 +0200 Subject: [PATCH 3/5] modular widgets --- addon/widgets/widget_friends.php | 28 ++++++++++++++ addon/widgets/widget_like.php | 12 ++++++ addon/widgets/widgets.php | 63 +++++++++++++++----------------- 3 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 addon/widgets/widget_friends.php create mode 100644 addon/widgets/widget_like.php diff --git a/addon/widgets/widget_friends.php b/addon/widgets/widget_friends.php new file mode 100644 index 0000000000..d794f7d267 --- /dev/null +++ b/addon/widgets/widget_friends.php @@ -0,0 +1,28 @@ +profile = $r[0]; + + $o .= ""; + $o .= _abs_url(contact_block()); + $o .= "profile['nickname']."'>". t('Connect on Friendika!') .""; +} \ No newline at end of file diff --git a/addon/widgets/widget_like.php b/addon/widgets/widget_like.php new file mode 100644 index 0000000000..9549fc4f0c --- /dev/null +++ b/addon/widgets/widget_like.php @@ -0,0 +1,12 @@ +Shows likes
Search "; +} + +function widget_args(){ + return Array("KEY"); +} + +function widget_content(&$a, &$o, $conf){ +} \ No newline at end of file diff --git a/addon/widgets/widgets.php b/addon/widgets/widgets.php index 7c75c66672..305423cb95 100644 --- a/addon/widgets/widgets.php +++ b/addon/widgets/widgets.php @@ -56,6 +56,7 @@ function widgets_settings(&$a,&$o) { $o.='

Widgets:

'; } @@ -92,21 +93,37 @@ function widgets_content(&$a) { $o = ""; -// echo "
"; var_dump($a->argv); die();
+	$widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
+	if (file_exists($widgetfile)){
+		require_once($widgetfile);
+	} else {
+		if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
+		return;
+	}		
+	
+
+
+
+	//echo "
"; var_dump($a->argv); die();
 	if ($a->argv[2]=="cb"){
-		switch($a->argv[1]) {
-			case 'friends': 
-				widget_friends_content($a, $o, $conf);
-				break;
-		}
+		if (!local_user()){
+			if (!isset($_GET['s']))
+				{header('HTTP/1.0 400 Bad Request'); killme();}
+			
+			if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site'])
+				{header('HTTP/1.0 400 Bad Request'); killme();}
+		} 
+		widget_content($a, $o, $conf);
 		
 	} else {
 
 		
-		if (isset($_GET['p'])) {
+		if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
 			$o .= "";
 			$o .= "

Preview Widget

"; $o .= ''. t("Plugin Settings") .''; + $o .= "

"; + widget_help($a, $o, $conf); $o .= "

"; $o .= "

Copy and paste this code

" - .htmlspecialchars('') + .htmlspecialchars('') .""; return $o; } @@ -143,31 +165,6 @@ function widgets_content(&$a) { } -function widget_friends_content(&$a, &$o, $conf){ - if (!local_user()){ - if (!isset($_GET['s'])) - header('HTTP/1.0 400 Bad Request'); - - if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site']) - header('HTTP/1.0 400 Bad Request'); - } - $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile` - LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid` - WHERE `user`.`uid` = %s AND `profile`.`is-default` = 1 LIMIT 1", - intval($conf['uid']) - ); - - if(!count($r)) return; - $a->profile = $r[0]; - $o .= ""; - $o .= _abs_url(contact_block()); - $o .= "profile['nickname']."'>". t('Connect on Friendika!') .""; - - -} ?> From 9f05b71018c5e99e76ae5697472a5493ac01db5a Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Mon, 18 Apr 2011 16:56:29 +0200 Subject: [PATCH 4/5] todo widget_like --- addon/widgets/widget_like.php | 1 + 1 file changed, 1 insertion(+) diff --git a/addon/widgets/widget_like.php b/addon/widgets/widget_like.php index 9549fc4f0c..001ec9c62c 100644 --- a/addon/widgets/widget_like.php +++ b/addon/widgets/widget_like.php @@ -9,4 +9,5 @@ function widget_args(){ } function widget_content(&$a, &$o, $conf){ + $o .= " #TODO# "; } \ No newline at end of file From 55fd53ecbd45e1ef28e576edc943cf11b9326cb5 Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Wed, 11 May 2011 17:04:41 +0200 Subject: [PATCH 5/5] More work on widgets addon --- addon/widgets/widget_friends.php | 16 ++++--- addon/widgets/widget_like.php | 21 ++++++--- addon/widgets/widgets.js | 15 ++++--- addon/widgets/widgets.php | 74 ++++++++++++++++---------------- 4 files changed, 70 insertions(+), 56 deletions(-) diff --git a/addon/widgets/widget_friends.php b/addon/widgets/widget_friends.php index d794f7d267..2286f68cad 100644 --- a/addon/widgets/widget_friends.php +++ b/addon/widgets/widget_friends.php @@ -1,28 +1,32 @@ profile = $r[0]; + $o = ""; $o .= ""; $o .= _abs_url(contact_block()); $o .= "profile['nickname']."'>". t('Connect on Friendika!') .""; -} \ No newline at end of file + return $o; +} diff --git a/addon/widgets/widget_like.php b/addon/widgets/widget_like.php index 001ec9c62c..9b54212a80 100644 --- a/addon/widgets/widget_like.php +++ b/addon/widgets/widget_like.php @@ -1,13 +1,22 @@ Shows likes
Search "; +function like_widget_name() { + return "Shows likes"; +} +function like_widget_help() { + return "Search first item wich contains KEY and print like/dislike count"; } -function widget_args(){ +function like_widget_args(){ return Array("KEY"); } -function widget_content(&$a, &$o, $conf){ - $o .= " #TODO# "; -} \ No newline at end of file +function like_widget_content(&$a, $conf){ + $args = explode(",",$_GET['a']); + + if ($args[0]!=""){ + return " #TODO like/dislike count for item with " .$args[0]. " # "; + } else { + return " #TODO# "; + } +} diff --git a/addon/widgets/widgets.js b/addon/widgets/widgets.js index 7a6cdeb0a0..45d36c4d7d 100644 --- a/addon/widgets/widgets.js +++ b/addon/widgets/widgets.js @@ -2,10 +2,11 @@ * @author Fabio Comuni */ -var f9a_widget = { +var f9a_widget_$widget_id = { entrypoint : "$entrypoint", key : "$key", widgetid: "$widget_id", + argstr: "$args", xmlhttp : null, getXHRObj : function(){ @@ -22,6 +23,7 @@ var f9a_widget = { if (args===null) args = new Array(); args['k']=this.key; args['s']=window.location; + args['a']=this.argstr; var urlencodedargs = new Array(); for(k in args){ urlencodedargs.push( encodeURIComponent(k)+"="+encodeURIComponent(args[k]) ); } @@ -29,20 +31,21 @@ var f9a_widget = { this.xmlhttp.open("GET", url ,true); this.xmlhttp.send(); + this.xmlhttp.obj = this; this.xmlhttp.onreadystatechange=function(){ if (this.readyState==4){ if (this.status==200) { - cb(this.responseText); + cb(this.obj, this.responseText); } else { - document.getElementById(f9a_widget.widgetid).innerHTML="Error loading widget."; + document.getElementById(this.obj.widgetid).innerHTML="Error loading widget."; } } } }, - requestcb: function(responseText) { - document.getElementById(f9a_widget.widgetid).innerHTML=responseText; + requestcb: function(obj, responseText) { + document.getElementById(obj.widgetid).innerHTML=responseText; }, load : function (){ @@ -53,7 +56,7 @@ var f9a_widget = { }; (function() { - f9a_widget.load(); + f9a_widget_$widget_id.load(); })(); document.writeln("
"); diff --git a/addon/widgets/widgets.php b/addon/widgets/widgets.php index 305423cb95..6bd7a73d17 100644 --- a/addon/widgets/widgets.php +++ b/addon/widgets/widgets.php @@ -17,8 +17,8 @@ function widgets_install() { function widgets_settings_post(){ if (isset($_POST['widgets-submit'])){ - set_pconfig(local_user(), 'widgets', 'site', $_POST['widgets-site']); - set_pconfig(local_user(), 'widgets', 'key', $_POST['widgets-key']); + del_pconfig(local_user(), 'widgets', 'key'); + } } @@ -26,41 +26,37 @@ function widgets_settings(&$a,&$o) { if(! local_user()) return; - $key = get_pconfig(local_user(), 'widgets', 'key' ); - $site = get_pconfig(local_user(), 'widgets', 'site' ); - - if ($key=='') $key = mt_rand(); - - $o .=' -

Widgets

-
- - -
-
-
- - - '.$key.' -
-
+ + $key = get_pconfig(local_user(), 'widgets', 'key' ); + if ($key=='') { $key = mt_rand(); set_pconfig(local_user(), 'widgets', 'key', $key); } + + $o .='

Widgets

'; - + $o.=' +
+ '. t('Widgets key: ') .''.$key.' +
+
- -
- '; + +
'; - if ($key!='' and $site!='') { - $o.='

Widgets:

- - '; + + $o.='

Widgets:

'; + $o .= '
    '; + $d = dir(dirname(__file__)); + while(false !== ($f = $d->read())) { + if(substr($f,0,7)=="widget_") { + preg_match("|widget_([^.]+).php|", $f, $m); + $w=$m[1]; + require_once($f); + $o.='
  • '. call_user_func($w."_widget_name") .'
  • '; + } } + $o .= '
'; + } function widgets_module() { @@ -90,7 +86,7 @@ function widgets_content(&$a) { $conf = array(); $conf['uid'] = $r[0]['uid']; foreach($r as $e) { $conf[$e['k']]=$e['v']; } - + $o = ""; $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php"; @@ -106,14 +102,14 @@ function widgets_content(&$a) { //echo "
"; var_dump($a->argv); die();
 	if ($a->argv[2]=="cb"){
-		if (!local_user()){
+		/*if (!local_user()){
 			if (!isset($_GET['s']))
 				{header('HTTP/1.0 400 Bad Request'); killme();}
 			
 			if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site'])
 				{header('HTTP/1.0 400 Bad Request'); killme();}
-		} 
-		widget_content($a, $o, $conf);
+		} */
+		$o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
 		
 	} else {
 
@@ -122,8 +118,9 @@ function widgets_content(&$a) {
 			$o .= "";
 			$o .= "

Preview Widget

"; $o .= ''. t("Plugin Settings") .''; - $o .= "

"; - widget_help($a, $o, $conf); + + $o .= "

".call_user_func($a->argv[1].'_widget_name')."

"; + $o .= call_user_func($a->argv[1].'_widget_help'); $o .= "

"; $o .= "