2015-08-23 22:38:18 +02:00
< ? php
function rate_init ( & $a ) {
if ( ! local_channel ())
return ;
2016-04-17 16:29:18 +02:00
$channel = App :: get_channel ();
2015-08-23 22:38:18 +02:00
$target = $_REQUEST [ 'target' ];
if ( ! $target )
return ;
2016-04-17 16:29:18 +02:00
App :: $data [ 'target' ] = $target ;
2015-08-23 22:38:18 +02:00
if ( $target ) {
$r = q ( " SELECT * FROM xchan where xchan_hash like '%s' LIMIT 1 " ,
dbesc ( $target )
);
if ( $r ) {
2016-04-17 16:29:18 +02:00
App :: $poi = $r [ 0 ];
2015-08-23 22:38:18 +02:00
}
else {
2015-10-24 13:04:14 +02:00
$r = q ( " select * from site where site_url like '%s' and site_type = %d " ,
dbesc ( '%' . $target ),
intval ( SITE_TYPE_ZOT )
2015-08-23 22:38:18 +02:00
);
if ( $r ) {
2016-04-17 16:29:18 +02:00
App :: $data [ 'site' ] = $r [ 0 ];
App :: $data [ 'site' ][ 'site_url' ] = strtolower ( $r [ 0 ][ 'site_url' ]);
2015-08-23 22:38:18 +02:00
}
}
}
return ;
}
function rate_post ( & $a ) {
if ( ! local_channel ())
return ;
2016-04-17 16:29:18 +02:00
if ( ! App :: $data [ 'target' ])
2015-08-23 22:38:18 +02:00
return ;
if ( ! $_REQUEST [ 'execute' ])
return ;
2016-04-17 16:29:18 +02:00
$channel = App :: get_channel ();
2015-08-23 22:38:18 +02:00
$rating = intval ( $_POST [ 'rating' ]);
if ( $rating < ( - 10 ))
$rating = ( - 10 );
if ( $rating > 10 )
$rating = 10 ;
$rating_text = trim ( escape_tags ( $_REQUEST [ 'rating_text' ]));
2016-04-17 16:29:18 +02:00
$signed = App :: $data [ 'target' ] . '.' . $rating . '.' . $rating_text ;
2015-08-23 22:38:18 +02:00
$sig = base64url_encode ( rsa_sign ( $signed , $channel [ 'channel_prvkey' ]));
$z = q ( " select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' and xlink_static = 1 limit 1 " ,
dbesc ( $channel [ 'channel_hash' ]),
2016-04-17 16:29:18 +02:00
dbesc ( App :: $data [ 'target' ])
2015-08-23 22:38:18 +02:00
);
if ( $z ) {
$record = $z [ 0 ][ 'xlink_id' ];
$w = q ( " update xlink set xlink_rating = '%d', xlink_rating_text = '%s', xlink_sig = '%s', xlink_updated = '%s'
where xlink_id = % d " ,
intval ( $rating ),
dbesc ( $rating_text ),
dbesc ( $sig ),
dbesc ( datetime_convert ()),
intval ( $record )
);
}
else {
$w = q ( " insert into xlink ( xlink_xchan, xlink_link, xlink_rating, xlink_rating_text, xlink_sig, xlink_updated, xlink_static ) values ( '%s', '%s', %d, '%s', '%s', '%s', 1 ) " ,
dbesc ( $channel [ 'channel_hash' ]),
2016-04-17 16:29:18 +02:00
dbesc ( App :: $data [ 'target' ]),
2015-08-23 22:38:18 +02:00
intval ( $rating ),
dbesc ( $rating_text ),
dbesc ( $sig ),
dbesc ( datetime_convert ())
);
$z = q ( " select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' and xlink_static = 1 limit 1 " ,
dbesc ( $channel [ 'channel_hash' ]),
2016-04-17 16:29:18 +02:00
dbesc ( App :: $data [ 'target' ])
2015-08-23 22:38:18 +02:00
);
if ( $z )
$record = $z [ 0 ][ 'xlink_id' ];
}
if ( $record ) {
proc_run ( 'php' , 'include/ratenotif.php' , 'rating' , $record );
}
}
function rate_content ( & $a ) {
if ( ! local_channel ()) {
notice ( t ( 'Permission denied.' ) . EOL );
return ;
}
2016-04-17 16:29:18 +02:00
// if(! App::$data['target']) {
2015-08-23 22:38:18 +02:00
// notice( t('No recipients.') . EOL);
// return;
// }
$poco_rating = get_config ( 'system' , 'poco_rating_enable' );
if (( ! $poco_rating ) && ( $poco_rating !== false )) {
notice ( 'Ratings are disabled on this site.' );
return ;
}
2016-04-17 16:29:18 +02:00
$channel = App :: get_channel ();
2015-08-23 22:38:18 +02:00
$r = q ( " select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' and xlink_static = 1 " ,
dbesc ( $channel [ 'channel_hash' ]),
2016-04-17 16:29:18 +02:00
dbesc ( App :: $data [ 'target' ])
2015-08-23 22:38:18 +02:00
);
if ( $r ) {
2016-04-17 16:29:18 +02:00
App :: $data [ 'xlink' ] = $r [ 0 ];
2015-08-23 22:38:18 +02:00
$rating_val = $r [ 0 ][ 'xlink_rating' ];
$rating_text = $r [ 0 ][ 'xlink_rating_text' ];
}
else {
$rating_val = 0 ;
$rating_text = '' ;
}
// if unset default to enabled
if ( $poco_rating === false )
$poco_rating = true ;
if ( $poco_rating ) {
$rating = replace_macros ( get_markup_template ( 'rating_slider.tpl' ), array (
'$min' => - 10 ,
'$val' => $rating_val
));
}
else {
$rating = false ;
}
$o = replace_macros ( get_markup_template ( 'rating_form.tpl' ), array (
'$header' => t ( 'Rating' ),
'$website' => t ( 'Website:' ),
2016-04-17 16:29:18 +02:00
'$site' => (( App :: $data [ 'site' ]) ? '<a href="' . App :: $data [ 'site' ][ 'site_url' ] . '" >' . App :: $data [ 'site' ][ 'site_url' ] . '</a>' : '' ),
'target' => App :: $data [ 'target' ],
'$tgt_name' => (( App :: $poi && App :: $poi [ 'xchan_name' ]) ? App :: $poi [ 'xchan_name' ] : sprintf ( t ( 'Remote Channel [%s] (not yet known on this site)' ), substr ( App :: $data [ 'target' ], 0 , 16 ))),
2015-08-23 22:38:18 +02:00
'$lbl_rating' => t ( 'Rating (this information is public)' ),
'$lbl_rating_txt' => t ( 'Optionally explain your rating (this information is public)' ),
'$rating_txt' => $rating_text ,
'$rating' => $rating ,
'$rating_val' => $rating_val ,
'$slide' => $slide ,
'$submit' => t ( 'Submit' )
));
return $o ;
}