Fix widgets crosssite ajax, make like widget works

This commit is contained in:
Fabio Comuni 2011-10-18 17:18:58 +02:00
parent 056921b1e8
commit 255685c176
5 changed files with 68 additions and 20 deletions

View file

@ -23,8 +23,13 @@ function friends_widget_content(&$a, $conf){
$o = "";
$o .= "<style>
.f9k_widget {font-size: 0.8em;}
.f9k_widget #contact-block { overflow: hidden; height: auto; }
.f9k_widget .contact-block-h4 { float: left; margin: 0px; }
.f9k_widget .allcontact-link { float: right; margin: 0px; }
.f9k_widget .contact-block-content { clear:both; }
.f9k_widget .contact-block-div { display: block !important; float: left!important; width: 50px!important; height: 50px!important; margin: 2px!important;}
.f9k_widget #contact-block-end { clear: left; }
</style>";
$o .= _abs_url(contact_block());
$o .= "<a href='".$a->get_baseurl().'/profile/'.$a->profile['nickname']."'>". t('Connect on Friendika!') ."</a>";

View file

@ -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 <em>" .$args[0]. "</em> # ";
} 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;
}

2
widgets/widget_like.tpl Normal file
View file

@ -0,0 +1,2 @@
<span class='f9k_like' title="$strlike">$like <img src="$baseurl/images/like.gif" alt="like"/></span>
<span class='f9k_dislike' title="$strdislike">$dislike <img src="$baseurl/images/dislike.gif" alt="dislike"/></span>

View file

@ -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);

View file

@ -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 "<pre>"; 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("</em>,<em>", call_user_func($a->argv[1].'_widget_args'));
$wargs = call_user_func($a->argv[1].'_widget_args');
$jsargs = implode("</em>,<em>", $wargs);
if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
$o .= "</script>
@ -155,6 +152,8 @@ function widgets_content(&$a) {
.$jsargs
.htmlspecialchars('"></script>')
."</code>";
return $o;
}