From 255685c17698530da9e252e013c4ef1f1b048b7d Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Tue, 18 Oct 2011 17:18:58 +0200 Subject: [PATCH] Fix widgets crosssite ajax, make like widget works --- widgets/widget_friends.php | 7 +++++- widgets/widget_like.php | 50 ++++++++++++++++++++++++++++++++++---- widgets/widget_like.tpl | 2 ++ widgets/widgets.js | 8 +++--- widgets/widgets.php | 21 ++++++++-------- 5 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 widgets/widget_like.tpl diff --git a/widgets/widget_friends.php b/widgets/widget_friends.php index 2286f68c..fc7d7aa9 100644 --- a/widgets/widget_friends.php +++ b/widgets/widget_friends.php @@ -23,8 +23,13 @@ function friends_widget_content(&$a, $conf){ $o = ""; $o .= ""; $o .= _abs_url(contact_block()); $o .= "profile['nickname']."'>". t('Connect on Friendika!') .""; diff --git a/widgets/widget_like.php b/widgets/widget_like.php index 6927d432..b60d2d94 100644 --- a/widgets/widget_like.php +++ b/widgets/widget_like.php @@ -14,9 +14,49 @@ function like_widget_args(){ 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# "; - } + + $baseq="SELECT COUNT(`item`.`id`) as `c`, `p`.`id` + FROM `item`, + (SELECT `i`.`id` FROM `item` as `i` WHERE + `i`.`visible` = 1 AND `i`.`deleted` = 0 + AND (( `i`.`wall` = 1 AND `i`.`allow_cid` = '' + AND `i`.`allow_gid` = '' + AND `i`.`deny_cid` = '' + AND `i`.`deny_gid` = '' ) + OR `i`.`uid` = %d ) + AND `i`.`body` LIKE '%%%s%%' LIMIT 1) as `p` + WHERE `item`.`parent` = `p`.`id` "; + + // count likes + $r = q( $baseq . "AND `item`.`verb` = 'http://activitystrea.ms/schema/1.0/like'", + intval($conf['uid']), + dbesc($args[0]) + ); + $likes = $r[0]['c']; + $iid = $r[0]['id']; + + // count dislikes + $r = q( $baseq . "AND `item`.`verb` = 'http://purl.org/macgirvin/dfrn/1.0/dislike'", + intval($conf['uid']), + dbesc($args[0]) + ); + $dislikes = $r[0]['c']; + + + require_once("include/conversation.php"); + + $o = ""; + + $t = file_get_contents( dirname(__file__). "/widget_like.tpl" ); + $o .= replace_macros($t, array( + '$like' => $likes, + '$strlike' => sprintf( tt("%d person likes this", "%d people like this", $likes), $likes), + + '$dislike' => $dislikes, + '$strdislike'=> sprintf( tt("%d person doesn't like this", "%d people don't like this", $dislikes), $dislikes), + + '$baseurl' => $a->get_baseurl(), + )); + + return $o; } diff --git a/widgets/widget_like.tpl b/widgets/widget_like.tpl new file mode 100644 index 00000000..e43e7a54 --- /dev/null +++ b/widgets/widget_like.tpl @@ -0,0 +1,2 @@ +$like like +$dislike dislike diff --git a/widgets/widgets.js b/widgets/widgets.js index 45d36c4d..e6382d71 100644 --- a/widgets/widgets.js +++ b/widgets/widgets.js @@ -22,11 +22,13 @@ var f9a_widget_$widget_id = { dorequest : function(args, cb) { if (args===null) args = new Array(); args['k']=this.key; - args['s']=window.location; + args['s']=window.location.href; args['a']=this.argstr; var urlencodedargs = new Array(); - for(k in args){ urlencodedargs.push( encodeURIComponent(k)+"="+encodeURIComponent(args[k]) ); } - + for(k in args){ + if (typeof args[k] != 'function') + urlencodedargs.push( encodeURIComponent(k)+"="+encodeURIComponent(args[k]) ); + } var url = this.entrypoint + "?"+ urlencodedargs.join("&"); this.xmlhttp.open("GET", url ,true); diff --git a/widgets/widgets.php b/widgets/widgets.php index 13c4f93b..cedaea34 100644 --- a/widgets/widgets.php +++ b/widgets/widgets.php @@ -40,8 +40,10 @@ function widgets_settings(&$a,&$o) { if(substr($f,0,7)=="widget_") { preg_match("|widget_([^.]+).php|", $f, $m); $w=$m[1]; - require_once($f); - $widgets[] = array($w, call_user_func($w."_widget_name")); + if ($w!=""){ + require_once($f); + $widgets[] = array($w, call_user_func($w."_widget_name")); + } } } @@ -104,13 +106,7 @@ function widgets_content(&$a) { //echo "
"; var_dump($a->argv); die();
 	if ($a->argv[2]=="cb"){
-		/*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();}
-		} */
+		header('Access-Control-Allow-Origin: *');
 		$o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
 		
 	} else {
@@ -136,14 +132,15 @@ function widgets_content(&$a) {
 		$o .= replace_macros($script, array(
 			'$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
 			'$key' => $conf['key'],
-			'$widget_id' => 'f9k_'.$a->argv[1]."_".time(),
+			'$widget_id' => 'f9a_'.$a->argv[1]."_". ceil(microtime(true)*100),
 			'$loader' => $a->get_baseurl()."/images/rotator.gif",
 			'$args' => (isset($_GET['a'])?$_GET['a']:''),
 		));
 
 	
 		if (isset($_GET['p'])) {
-			$jsargs = implode(",", call_user_func($a->argv[1].'_widget_args'));
+			$wargs = call_user_func($a->argv[1].'_widget_args');
+			$jsargs = implode(",", $wargs);
 			if ($jsargs!='') $jsargs = "&a=".$jsargs."";
 				
 			$o .= "
@@ -155,6 +152,8 @@ function widgets_content(&$a) {
 			.$jsargs
 			.htmlspecialchars('">')
 			."";
+
+			
 			return $o;
 		}	
 		
-- 
2.39.5