| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
function friendlist_system($field) {
|
| 5 |
$system["description"] = t("Enable Friend List functionality");
|
| 6 |
return $system[$field];
|
| 7 |
}
|
| 8 |
|
| 9 |
function friendlist_perm() {
|
| 10 |
return array("maintain friend list");
|
| 11 |
}
|
| 12 |
|
| 13 |
function friendlist_user($type, &$edit, &$thisuser) {
|
| 14 |
global $user;
|
| 15 |
|
| 16 |
if (!isset($user->uid)) {
|
| 17 |
return null;
|
| 18 |
}
|
| 19 |
|
| 20 |
if (!isset($_SESSION['friendlist'])) {
|
| 21 |
friendlist_update_session();
|
| 22 |
}
|
| 23 |
|
| 24 |
if (isset($thisuser->uid) and $type == "view_public") {
|
| 25 |
if (user_access("maintain friend list")) {
|
| 26 |
// don't let just any user manage friends
|
| 27 |
if (in_array($thisuser->uid, $_SESSION['friendlist'])) {
|
| 28 |
$output .= form_item(t("Friend List"), l(t("Remove from Friend List"), "friendlist/delete/" . $thisuser->uid));
|
| 29 |
}
|
| 30 |
else {
|
| 31 |
$output .= form_item(t("Friend List"), l(t("Add to Friend List"), "friendlist/add/" . $thisuser->uid));
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
// look up this user's friends
|
| 36 |
$friends = friendlist_list_friends($thisuser->uid, 0, 1);
|
| 37 |
$output .= form_item(t("Friends"), $friends, "These are the users this user has friended");
|
| 38 |
|
| 39 |
// look up users who consider this user a friend
|
| 40 |
$friend_of = friendlist_list_friends_of($thisuser->uid, 1);
|
| 41 |
$output .= form_item(t("Friend Of"), $friend_of, "These are the users that have friended this user");
|
| 42 |
}
|
| 43 |
return $output;
|
| 44 |
}
|
| 45 |
|
| 46 |
function friendlist_update_session() {
|
| 47 |
global $user;
|
| 48 |
@session_start();
|
| 49 |
|
| 50 |
$_SESSION['friendlist'] = array();
|
| 51 |
if (isset($user->uid)) {
|
| 52 |
$result = db_query("SELECT friend FROM friendlist WHERE uid=%d ORDER BY timestamp DESC",$user->uid);
|
| 53 |
while ($friend = db_fetch_object($result)) {
|
| 54 |
$_SESSION['friendlist'][] = $friend->friend;
|
| 55 |
}
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
function friendlist_block($op = "list", $delta = 0) {
|
| 60 |
global $user;
|
| 61 |
if ($op == "list") {
|
| 62 |
|
| 63 |
$block[0]["info"] = t("Friend List");
|
| 64 |
$block[1]["info"] = t("Friend Blogs");
|
| 65 |
return $block;
|
| 66 |
|
| 67 |
}
|
| 68 |
elseif ($op == 'view' and user_access("access content")) {
|
| 69 |
|
| 70 |
if (isset($user->uid)) {
|
| 71 |
switch ($delta) {
|
| 72 |
case 0 :
|
| 73 |
$result = db_query("SELECT b.friend as uid, u.name FROM friendlist b, users u WHERE b.uid=%d AND u.uid=b.friend ORDER BY b.timestamp DESC",$user->uid);
|
| 74 |
while ($account = db_fetch_object($result)) {
|
| 75 |
$account->link = l((strlen($account->name) > 15 ? substr($account->name, 0, 15) . '...' : $account->name), "user/view/$account->uid");
|
| 76 |
/*
|
| 77 |
** To Enable the added functionality , which requires the extended theme_user_list patch, uncomment the following
|
| 78 |
** line and comment the line after it.
|
| 79 |
*/
|
| 80 |
//$users[] = $account;
|
| 81 |
$users[] = $account->link;
|
| 82 |
}
|
| 83 |
|
| 84 |
$block["content"] = theme("user_list",$users);
|
| 85 |
$block["subject"] = t("Friend List");
|
| 86 |
return $block;
|
| 87 |
break;
|
| 88 |
|
| 89 |
case 1:
|
| 90 |
$block["content"] = node_title_list(db_query("SELECT distinct n.nid, u.uid, u.name, n.created, n.title FROM friendlist b, node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 AND n.uid=b.friend AND b.uid=%d ORDER BY n.nid DESC LIMIT 10",$user->uid));
|
| 91 |
if (!sizeof($block["content"])) {
|
| 92 |
return NULL;
|
| 93 |
}
|
| 94 |
$block["subject"] = t("My Friends' Blogs");
|
| 95 |
return $block;
|
| 96 |
break;
|
| 97 |
}
|
| 98 |
}
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
function friendlist_icon_link($id) {
|
| 103 |
global $user;
|
| 104 |
|
| 105 |
if (!isset($user->uid)) {
|
| 106 |
return null;
|
| 107 |
}
|
| 108 |
|
| 109 |
if (!isset($_SESSION['friendlist'])) {
|
| 110 |
friendlist_update_session();
|
| 111 |
}
|
| 112 |
|
| 113 |
if (in_array($id,$_SESSION['friendlist'])) {
|
| 114 |
return friendlist_del_link($id);
|
| 115 |
}
|
| 116 |
else {
|
| 117 |
return friendlist_add_link($id);
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
function friendlist_add_link($id) {
|
| 122 |
global $theme;
|
| 123 |
$icon = isset($theme->add_icon) ? $theme->add_icon : "[+]";
|
| 124 |
return l($icon,"friendlist/add/$id");
|
| 125 |
}
|
| 126 |
|
| 127 |
function friendlist_del_link($id) {
|
| 128 |
global $theme;
|
| 129 |
$icon = isset($theme->del_icon) ? $theme->del_icon : "[-]";
|
| 130 |
return l($icon,"friendlist/delete/$id");
|
| 131 |
}
|
| 132 |
|
| 133 |
function friendlist_link($type, $node = 0, $main) {
|
| 134 |
global $user;
|
| 135 |
|
| 136 |
if ($type == "system") {
|
| 137 |
if (user_access("maintain friend list")) {
|
| 138 |
menu("friendlist", t("friend list"), "friendlist_page", 0, MENU_SHOW);
|
| 139 |
menu("friendlist/list", t("view friendlist"), "friendlist_page", 1, MENU_SHOW);
|
| 140 |
menu("friendlist/feed/$user->uid", t("xml feed"), "friendlist_page", 2, MENU_SHOW);
|
| 141 |
menu("friendlist/feed", t("xml feed"), "friendlist_page", 0, MENU_HIDE);
|
| 142 |
menu("friendlist/add", t("add to friendlist"), "friendlist_page", 0, MENU_HIDE);
|
| 143 |
menu("friendlist/save", t("user added to friendlist"), "friendlist_page", 0, MENU_HIDE);
|
| 144 |
menu("friendlist/delete", t("delete friendlist"), "friendlist_page", 0, MENU_HIDE);
|
| 145 |
menu("friendlist/remove", t("user removed from friendlist"), "friendlist_page", 0, MENU_HIDE);
|
| 146 |
menu("friendlist/view", t("view friendlist"), "friendlist_page", 0, MENU_HIDE);
|
| 147 |
}
|
| 148 |
else {
|
| 149 |
// non-authorized users must specify a uid as part of the URL. This should still accommodate.
|
| 150 |
menu("friendlist/feed", t("xml feed"), "friendlist_page", 0, MENU_HIDE);
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
function friendlist_page() {
|
| 156 |
global $user;
|
| 157 |
|
| 158 |
$edit = $_POST["edit"];
|
| 159 |
$op = $_POST["op"];
|
| 160 |
if (empty($op)) {
|
| 161 |
$op = arg(1);
|
| 162 |
}
|
| 163 |
|
| 164 |
if($op == 'feed') {
|
| 165 |
// arg(2) is optional uid
|
| 166 |
friendlist_feed(arg(2));
|
| 167 |
return;
|
| 168 |
}
|
| 169 |
|
| 170 |
switch ($op) {
|
| 171 |
case 'add' :
|
| 172 |
$title = t("Confirm Addition to Friend List?");
|
| 173 |
$output = friendlist_addfriend_form(arg(2));
|
| 174 |
break;
|
| 175 |
case "Add User" :
|
| 176 |
case "save" :
|
| 177 |
$output = friendlist_add($edit['friend_id']);
|
| 178 |
break;
|
| 179 |
|
| 180 |
case 'delete' :
|
| 181 |
$title = t("Confirm Removal from Friend List?");
|
| 182 |
$output = friendlist_deletefriend_form(arg(2));
|
| 183 |
break;
|
| 184 |
|
| 185 |
case 'list' :
|
| 186 |
$title = t("Your Friend List");
|
| 187 |
$output = friendlist_list_friends();
|
| 188 |
break;
|
| 189 |
|
| 190 |
case "Remove User" :
|
| 191 |
case "remove" :
|
| 192 |
$output = friendlist_remove($edit['friend_id']);
|
| 193 |
break;
|
| 194 |
|
| 195 |
case "view" :
|
| 196 |
default :
|
| 197 |
if(arg(2)) {
|
| 198 |
$acct = user_load(array('uid' => arg(2)));
|
| 199 |
if(is_object($acct)) {
|
| 200 |
$title = t("$acct->name's Friends");
|
| 201 |
$output = friendlist_list_content($acct);
|
| 202 |
}
|
| 203 |
else {
|
| 204 |
$title = t("Unknown User");
|
| 205 |
$output = t("Please check the URL that you entered. The specified user was not found.");
|
| 206 |
}
|
| 207 |
}
|
| 208 |
else {
|
| 209 |
$title = t("My Friends");
|
| 210 |
$output = friendlist_list_content();
|
| 211 |
}
|
| 212 |
break;
|
| 213 |
}
|
| 214 |
print theme("page", $output, $title);
|
| 215 |
}
|
| 216 |
|
| 217 |
function friendlist_addfriend_form($uid) {
|
| 218 |
global $user;
|
| 219 |
friendlist_update_session();
|
| 220 |
$friend = user_load(array('uid' => $uid));
|
| 221 |
|
| 222 |
if (empty($friend->name)) {
|
| 223 |
return t("This user does not exist");
|
| 224 |
}
|
| 225 |
elseif (in_array($uid, $_SESSION['friendlist'])) {
|
| 226 |
return t("This user is already on your friend list");
|
| 227 |
}
|
| 228 |
elseif ($user->uid == $uid) {
|
| 229 |
return t("Cannot add yourself to friend list");
|
| 230 |
}
|
| 231 |
else {
|
| 232 |
$form = t("Add User <b>'") . l($friend->name,"user/" . $uid) . t("'</b> to Friend List?<br /><br />");
|
| 233 |
$form .= form_hidden("friend_id", $uid);
|
| 234 |
$form .= form_submit(t("Add User"));
|
| 235 |
return form($form,"post",url("friendlist/save"));
|
| 236 |
}
|
| 237 |
}
|
| 238 |
|
| 239 |
function friendlist_deletefriend_form($uid) {
|
| 240 |
global $user;
|
| 241 |
friendlist_update_session();
|
| 242 |
$friend = user_load(array('uid' => $uid));
|
| 243 |
|
| 244 |
if (empty($friend->name)) {
|
| 245 |
return "This user does not exist";
|
| 246 |
}
|
| 247 |
elseif (!in_array($uid, $_SESSION['friendlist'])) {
|
| 248 |
return t("This user is not on your friend list");
|
| 249 |
}
|
| 250 |
else {
|
| 251 |
$form = t("Remove User <b>'") . l($friend->name,"user/" . $uid) . t("'</b> from Friend List?<br /><br />");
|
| 252 |
$form .= form_hidden("friend_id", $uid);
|
| 253 |
$form .= form_submit(t("Remove User"));
|
| 254 |
return form($form,"post",url("friendlist/remove"));
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
| 258 |
function friendlist_add($id) {
|
| 259 |
global $user;
|
| 260 |
db_query("INSERT INTO friendlist (uid,friend,timestamp) VALUES (%d,%d,%d)" , $user->uid , $id , time());
|
| 261 |
friendlist_update_session();
|
| 262 |
}
|
| 263 |
|
| 264 |
function friendlist_remove($id) {
|
| 265 |
global $user;
|
| 266 |
db_query("DELETE FROM friendlist where uid=%d and friend=%d" , $user->uid , $id);
|
| 267 |
friendlist_update_session();
|
| 268 |
}
|
| 269 |
|
| 270 |
// prints out an xml feed of all of a given users' friends' blog entries
|
| 271 |
function friendlist_feed($uid=0) {
|
| 272 |
global $user;
|
| 273 |
|
| 274 |
if(!$uid) {
|
| 275 |
$acct = $user;
|
| 276 |
$use_sess = true;
|
| 277 |
}
|
| 278 |
else {
|
| 279 |
$acct = user_load(array('uid' => $uid));
|
| 280 |
$use_sess = false;
|
| 281 |
}
|
| 282 |
|
| 283 |
if($use_sess) {
|
| 284 |
|
| 285 |
// perhaps your session expired, or you think you should have friends
|
| 286 |
if (!isset($_SESSION['friendlist']) || sizeof($_SESSION['friendlist']) == 0) {
|
| 287 |
friendlist_update_session();
|
| 288 |
}
|
| 289 |
|
| 290 |
// MySQL doesn't like empty $friends lists
|
| 291 |
if(sizeof($_SESSION['friendlist']) > 0) {
|
| 292 |
$friends = implode(',', $_SESSION['friendlist']);
|
| 293 |
$result = db_query_range("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 AND n.uid IN ($friends) ORDER BY n.nid DESC", 0, 30);
|
| 294 |
}
|
| 295 |
else {
|
| 296 |
$err_title = t("No friends found for you. Please verify that you are logged in and have friends.");
|
| 297 |
}
|
| 298 |
}
|
| 299 |
elseif(is_object($acct)) {
|
| 300 |
$result = db_query_range("SELECT distinct n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM friendlist b, node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 AND n.uid=b.friend AND b.uid=$acct->uid ORDER BY n.nid DESC", 0, 30);
|
| 301 |
}
|
| 302 |
else {
|
| 303 |
// user was not found
|
| 304 |
$err_title = t("User ($uid) not found, check url");
|
| 305 |
}
|
| 306 |
|
| 307 |
if($err_title){
|
| 308 |
$channel["title"] = $err_title;
|
| 309 |
}
|
| 310 |
else {
|
| 311 |
$channel["title"] = t("%name's friends' blogs", array ('%name' => $acct->name));
|
| 312 |
}
|
| 313 |
$channel["link"] = url("friendlist/view/$acct->uid", null, null, 1); // use absolute urls
|
| 314 |
$channel["description"] = '';
|
| 315 |
node_feed($result, $channel);
|
| 316 |
}
|
| 317 |
|
| 318 |
// Lists out all of a user's friends' content
|
| 319 |
function friendlist_list_content($acct=null) {
|
| 320 |
global $user;
|
| 321 |
if(is_object($acct)) {
|
| 322 |
$uid = $acct->uid;
|
| 323 |
}
|
| 324 |
else {
|
| 325 |
$uid = $user->uid;
|
| 326 |
}
|
| 327 |
$result = pager_query("SELECT distinct n.nid, n.type, u.uid, u.name, n.created, n.title FROM friendlist b, node n LEFT JOIN users u ON n.uid = u.uid WHERE n.status = 1 AND n.uid=b.friend AND b.uid=$uid ORDER BY n.nid DESC",variable_get('default_nodes_main', 10));
|
| 328 |
if (db_num_rows($result)) {
|
| 329 |
drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="RSS" href="'. url("friendlist/feed/$url") .'" />');
|
| 330 |
|
| 331 |
$output = '';
|
| 332 |
while ($node = db_fetch_object($result)) {
|
| 333 |
$output .= node_view(node_load(array('nid' => $node->nid, 'type' => $node->type)), 1);
|
| 334 |
}
|
| 335 |
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
|
| 336 |
}
|
| 337 |
else {
|
| 338 |
$output = t("<p>None of these friends have posted any blog entries.</p>");
|
| 339 |
}
|
| 340 |
|
| 341 |
return $output;
|
| 342 |
}
|
| 343 |
|
| 344 |
// Lists all of a user's friends. Optionally show links to remove and/or include count.
|
| 345 |
function friendlist_list_friends($uid=null, $show_remove_link=1, $show_count=0) {
|
| 346 |
global $user;
|
| 347 |
if(!$uid && $user->uid > 0) {
|
| 348 |
$uid = $user->uid;
|
| 349 |
}
|
| 350 |
|
| 351 |
$result = db_query("SELECT b.friend as uid, u.name FROM friendlist b, users u WHERE b.uid=%d AND u.uid=b.friend ORDER BY b.timestamp DESC",$uid);
|
| 352 |
while ($account = db_fetch_object($result)) {
|
| 353 |
$link = l($account->name, "user/view/$account->uid");
|
| 354 |
if($show_remove_link) {
|
| 355 |
$link .= " " . l(t("[remove]"), "friendlist/delete/$account->uid");
|
| 356 |
}
|
| 357 |
$users[] = $link;
|
| 358 |
}
|
| 359 |
|
| 360 |
if($show_count) {
|
| 361 |
$cnt = sizeof($users);
|
| 362 |
$title = "<b>($cnt user" . ($cnt == 1? '':'s') . ")</b>";
|
| 363 |
}
|
| 364 |
|
| 365 |
return theme("user_list",$users, $title);
|
| 366 |
|
| 367 |
}
|
| 368 |
|
| 369 |
// Lists all of the users who consider this user a friend. Optionally include count.
|
| 370 |
function friendlist_list_friends_of($uid=null, $show_count=0) {
|
| 371 |
global $user;
|
| 372 |
if(!$uid && $user->uid > 0) {
|
| 373 |
$uid = $user->uid;
|
| 374 |
}
|
| 375 |
|
| 376 |
$result = db_query("SELECT b.uid as uid, u.name FROM friendlist b, users u WHERE b.friend=%d AND u.uid=b.uid ORDER BY b.timestamp DESC",$uid);
|
| 377 |
while ($account = db_fetch_object($result)) {
|
| 378 |
$link = l($account->name, "user/view/$account->uid");
|
| 379 |
$users[] = $link;
|
| 380 |
}
|
| 381 |
|
| 382 |
if($show_count) {
|
| 383 |
$cnt = sizeof($users);
|
| 384 |
$title = "<b>($cnt " . t("user" . ($cnt == 1? '':'s')) . ")</b>";
|
| 385 |
}
|
| 386 |
|
| 387 |
return theme("user_list",$users, $title);
|
| 388 |
|
| 389 |
}
|
| 390 |
?>
|