2007-12-18 20:28:08 +00:00
< ? php
2008-08-05 22:43:14 +00:00
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
2007-12-18 20:28:08 +00:00
// stuff related to "buddy lists"
require_once ( " ../inc/forum_db.inc " );
require_once ( " ../inc/profile.inc " );
2007-12-30 22:02:16 +00:00
// see if there's already a request,
// and whether the notification record is there
2007-12-18 20:28:08 +00:00
//
2007-12-30 22:02:16 +00:00
function check_pending ( $user , $destuser ) {
$friend = BoincFriend :: lookup ( $user -> id , $destuser -> id );
if ( $friend ) {
if ( $friend -> reciprocated ) {
2008-12-13 21:56:53 +00:00
error_page ( tra ( " Already friends " ));
2007-12-30 22:02:16 +00:00
}
$notify = BoincNotify :: lookup ( $destuser -> id , NOTIFY_FRIEND_REQ , $user -> id );
if ( $notify ) {
2008-12-13 21:56:53 +00:00
page_head ( tra ( " Request pending " ));
2007-12-30 22:02:16 +00:00
$t = date_str ( $friend -> create_time );
2008-12-13 21:56:53 +00:00
echo tra ( " You requested friendship with %1 on %2. " , $destuser -> name , $t ) . "
< p > " .
tra ( " This request is still pending confirmation. " );
2007-12-30 22:02:16 +00:00
page_tail ();
exit ();
}
BoincFriend :: delete ( $user -> id , $destuser -> id );
}
2007-12-18 20:28:08 +00:00
}
2008-01-08 00:20:30 +00:00
function check_ignoring ( $srcuser , $destuser ) {
BoincForumPrefs :: lookup ( $destuser );
if ( is_ignoring ( $destuser , $srcuser )) {
2008-12-13 21:56:53 +00:00
error_page ( tra ( " %1 is not accepting friendship requests from you " , $destuser -> name ));
2008-01-08 00:20:30 +00:00
}
}
2007-12-18 20:28:08 +00:00
// user has clicked "add to friends". Ask them if they really mean it.
//
function handle_add ( $user ) {
$destid = get_int ( 'userid' );
if ( $destid == $user -> id ) {
2008-12-13 21:56:53 +00:00
error_page ( tra ( " You can't be friends with yourself " ));
2007-12-18 20:28:08 +00:00
}
$destuser = BoincUser :: lookup_id ( $destid );
2008-12-13 21:56:53 +00:00
if ( ! $destuser ) error_page ( tra ( " No such user " ));
2007-12-30 22:02:16 +00:00
check_pending ( $user , $destuser );
2008-01-08 00:20:30 +00:00
check_ignoring ( $user , $destuser );
2007-12-30 22:02:16 +00:00
2008-12-13 21:56:53 +00:00
page_head ( tra ( " Add friend " ));
2007-12-18 20:28:08 +00:00
echo "
< form method = post action = friend . php >
< input type = hidden name = userid value = $destid >
2008-12-13 21:56:53 +00:00
< input type = hidden name = action value = add_confirm > " .
tra ( " You have asked to add %1 as a friend. We will notify %2 and will ask him/her to confirm that you are friends. " ,
2010-11-04 18:20:57 +00:00
" <b> " . $destuser -> name . " </b> " , " <b> " . $destuser -> name . " </b> " ) . "
2008-12-13 21:56:53 +00:00
< p > " .
tra ( " Add an optional message here: " ) . "
2007-12-18 20:28:08 +00:00
< br >
< textarea name = message cols = 64 rows = 4 ></ textarea >
< p >
< input type = submit value = OK >
2007-12-30 22:02:16 +00:00
</ form >
2007-12-18 20:28:08 +00:00
" ;
page_tail ();
}
// User really means it. Make DB entry and send notification
//
function handle_add_confirm ( $user ) {
$destid = post_int ( 'userid' );
$destuser = BoincUser :: lookup_id ( $destid );
2008-12-13 21:56:53 +00:00
if ( ! $destuser ) error_page ( tra ( " No such user " ));
2007-12-18 20:28:08 +00:00
2007-12-30 22:02:16 +00:00
check_pending ( $user , $destuser );
2008-01-08 00:20:30 +00:00
check_ignoring ( $user , $destuser );
2007-12-30 22:02:16 +00:00
2007-12-18 20:28:08 +00:00
$msg = post_str ( 'message' , true );
2008-12-14 22:18:49 +00:00
if ( $msg ) $msg = strip_tags ( BoincDb :: escape_string ( $msg ));
2007-12-18 20:28:08 +00:00
$now = time ();
2008-06-11 19:36:10 +00:00
$ret = BoincFriend :: replace (
" user_src= $user->id , user_dest= $destid , message=' $msg ', create_time= $now , reciprocated=0 "
);
2007-12-18 20:28:08 +00:00
if ( ! $ret ) {
2008-12-13 21:56:53 +00:00
error_page ( tra ( " Database error " ));
2007-12-18 20:28:08 +00:00
}
2007-12-30 22:02:16 +00:00
$now = time ();
$type = NOTIFY_FRIEND_REQ ;
BoincNotify :: replace ( " userid= $destid , create_time= $now , type= $type , opaque= $user->id " );
BoincForumPrefs :: lookup ( $destuser );
if ( $destuser -> prefs -> pm_notification == 1 ) {
send_friend_request_email ( $user , $destuser , $msg );
2007-12-18 20:28:08 +00:00
}
2008-12-13 21:56:53 +00:00
page_head ( tra ( " Friend request sent " ));
echo tra ( " We have notified %1 of your request. " , " <b> " . $destuser -> name . " </b> " );
2007-12-18 20:28:08 +00:00
page_tail ();
}
// Show destination user the details of request, ask if they accept
//
function handle_query ( $user ) {
$srcid = get_int ( 'userid' );
$srcuser = BoincUser :: lookup_id ( $srcid );
2008-12-13 21:56:53 +00:00
if ( ! $srcuser ) error_page ( tra ( " No such user " ));
2007-12-18 20:28:08 +00:00
$friend = BoincFriend :: lookup ( $srcid , $user -> id );
2008-12-13 21:56:53 +00:00
if ( ! $friend ) error_page ( tra ( " Request not found " ));
page_head ( tra ( " Friend request " ));
2007-12-27 18:37:22 +00:00
$x = user_links ( $srcuser , true );
2008-12-13 21:56:53 +00:00
echo tra ( " %1 has added you as a friend. " , $x );
2007-12-18 20:28:08 +00:00
if ( strlen ( $friend -> message )) {
2008-12-13 21:56:53 +00:00
echo " <p> " . tra ( " %1 says: %2 " , $srcuser -> name , $friend -> message ) . " </p> " ;
2007-12-18 20:28:08 +00:00
}
echo "
2008-09-27 09:52:28 +00:00
< p >< ul class = \ " actionlist \" > " ;
show_actionlist_button ( " friend.php?action=accept&userid= " . $srcid , tra ( " Accept friendship " ), tra ( " Click accept if %1 is in fact a friend " , $srcuser -> name ));
show_actionlist_button ( " friend.php?action=ignore&userid= " . $srcid , tra ( " Decline " ), tra ( " Click decline if %1 is not a friend " , $srcuser -> name ));
echo " <p>
2007-12-18 20:28:08 +00:00
" ;
page_tail ();
}
// Here if they accepted
//
function handle_accept ( $user ) {
$srcid = get_int ( 'userid' );
$srcuser = BoincUser :: lookup_id ( $srcid );
2008-12-13 21:56:53 +00:00
if ( ! $srcuser ) error_page ( tra ( " No such user " ));
2007-12-18 20:28:08 +00:00
$friend = BoincFriend :: lookup ( $srcid , $user -> id );
if ( ! $friend ) {
2008-12-13 21:56:53 +00:00
error_page ( tra ( " No request " ));
2007-12-18 20:28:08 +00:00
}
$friend -> update ( " reciprocated=1 " );
// "accept message" not implemented in interface yet
$msg = post_str ( 'message' , true );
2008-12-14 22:18:49 +00:00
if ( $msg ) $msg = strip_tags ( BoincDb :: escape_string ( $msg ));
2007-12-18 20:28:08 +00:00
$now = time ();
2007-12-30 22:02:16 +00:00
$ret = BoincFriend :: replace ( " user_src= $user->id , user_dest= $srcid , message=' $msg ', create_time= $now , reciprocated=1 " );
2007-12-18 20:28:08 +00:00
if ( ! $ret ) {
2008-12-13 21:56:53 +00:00
error_page ( tra ( " Database error " ));
2007-12-18 20:28:08 +00:00
}
2007-12-30 22:02:16 +00:00
$type = NOTIFY_FRIEND_ACCEPT ;
BoincNotify :: replace ( " userid= $srcid , create_time= $now , type= $type , opaque= $user->id " );
BoincForumPrefs :: lookup ( $srcuser );
if ( $srcuser -> prefs -> pm_notification == 1 ) {
send_friend_accept_email ( $user , $srcuser , $msg );
2007-12-18 20:28:08 +00:00
}
$notify = BoincNotify :: lookup ( $user -> id , NOTIFY_FRIEND_REQ , $srcid );
if ( $notify ) {
$notify -> delete ();
}
2008-12-13 21:56:53 +00:00
page_head ( tra ( " Friendship confirmed " ));
echo tra ( " Your friendship with %1 has been confirmed. " , " <b> " . $srcuser -> name . " </b> " );
2007-12-18 20:28:08 +00:00
page_tail ();
}
// Here if they declined
//
function handle_ignore ( $user ) {
$srcid = get_int ( 'userid' );
$srcuser = BoincUser :: lookup_id ( $srcid );
if ( ! $srcuser ) error_page ( " No such user " );
$friend = BoincFriend :: lookup ( $srcid , $user -> id );
if ( ! $friend ) {
error_page ( " No request " );
}
$notify = BoincNotify :: lookup ( $user -> id , NOTIFY_FRIEND_REQ , $srcid );
if ( $notify ) {
$notify -> delete ();
}
2008-12-13 21:56:53 +00:00
page_head ( tra ( " Friendship declined " ));
echo tra ( " You have declined friendship with %1 " , " <b> " . $srcuser -> name . " </b> " );
2007-12-18 20:28:08 +00:00
page_tail ();
}
// Here if initiator clicked on "confirmed" notification.
// Delete notification
//
function handle_accepted ( $user ) {
$destid = get_int ( 'userid' );
$destuser = BoincUser :: lookup_id ( $destid );
2008-12-13 21:56:53 +00:00
if ( ! $destuser ) error_page ( tra ( " No such user " ));
2007-12-18 20:28:08 +00:00
$notify = BoincNotify :: lookup ( $user -> id , NOTIFY_FRIEND_ACCEPT , $destid );
if ( $notify ) {
$notify -> delete ();
} else {
2008-12-13 21:56:53 +00:00
echo tra ( " Notification not found " );
2007-12-18 20:28:08 +00:00
}
2008-12-13 21:56:53 +00:00
page_head ( tra ( " Friend confirmed " ));
echo tra ( " You are now friends with %1. " , $destuser -> name );
2007-12-18 20:28:08 +00:00
page_tail ();
}
2007-12-30 22:02:16 +00:00
function handle_cancel_confirm ( $user ) {
$destid = get_int ( 'userid' );
$destuser = BoincUser :: lookup_id ( $destid );
2008-12-13 21:56:53 +00:00
if ( ! $destuser ) error_page ( tra ( " No such user " ));
page_head ( tra ( " Cancel friendship? " ));
echo tra ( " Are you sure you want to cancel your friendship with %1? " , $destuser -> name ) . "
2007-12-30 22:02:16 +00:00
< p >
2008-09-27 09:52:28 +00:00
< ul class = \ " actionlist \" > " ;
2008-12-13 21:56:53 +00:00
show_actionlist_button ( " friend.php?action=cancel&userid= $destid " , tra ( " Yes " ), tra ( " Cancel friendship " ));
show_actionlist_button ( " home.php " , tra ( " No " ), tra ( " Stay friends " ));
2008-09-27 09:52:28 +00:00
echo " </ul> " ;
2007-12-30 22:02:16 +00:00
page_tail ();
}
function handle_cancel ( $user ) {
$destid = get_int ( 'userid' );
$destuser = BoincUser :: lookup_id ( $destid );
2008-12-13 21:56:53 +00:00
if ( ! $destuser ) error_page ( tra ( " No such user " ));
2007-12-30 22:02:16 +00:00
BoincFriend :: delete ( $user -> id , $destid );
2008-12-13 21:56:53 +00:00
page_head ( tra ( " Friendship cancelled " ));
echo tra ( " Your friendship with %1 has been cancelled. " , $destuser -> name );
2007-12-30 22:02:16 +00:00
page_tail ();
}
2007-12-18 20:28:08 +00:00
// "home page" has Requests area
// (icon) N friend request(s)
$user = get_logged_in_user ();
$action = get_str ( 'action' , true );
if ( ! $action ) $action = post_str ( 'action' );
switch ( $action ) {
case 'add' :
handle_add ( $user );
break ;
case 'add_confirm' :
handle_add_confirm ( $user );
break ;
case 'query' :
handle_query ( $user );
break ;
case 'accept' :
handle_accept ( $user );
break ;
case 'accepted' :
handle_accepted ( $user );
break ;
case 'ignore' :
handle_ignore ( $user );
break ;
2007-12-30 22:02:16 +00:00
case 'cancel_confirm' :
handle_cancel_confirm ( $user );
break ;
case 'cancel' :
handle_cancel ( $user );
break ;
2007-12-18 20:28:08 +00:00
default :
2008-12-13 21:56:53 +00:00
error_page ( tra ( " Unknown action " ));
2007-12-18 20:28:08 +00:00
}
?>