<?php
/*
=====================================================
DataLife Engine - by SoftNews Media Group
-----------------------------------------------------
Доработка и адаптация: eXteRnaL ICQ: 395951912
-----------------------------------------------------
Copyright (c) 2004,2007 SoftNews Media Group
=====================================================
Данный код защищен авторскими правами
=====================================================
*/
final class Friends extends db
{
private $chpu;
private $self_path;
private $member_id;
private $is_logged;
private $is_friend = false;
private $user_name;
public $err = false;
private $lng;
function __construct($sys_conf, $member_id, $is_logged, $lng){
$this->user_name = urldecode($_REQUEST['user']);
if ($this->user_name == $member_id['name'] || !$is_logged) $this->is_friend = true;
$this->member_id = $member_id;
$this->is_logged = $is_logged;
$this->chpu = $sys_conf['allow_alt_url'];
if ($this->chpu == 'yes') $this->self_path = $sys_conf['http_home_url']."friends/";
else $this->self_path = $sys_conf['http_home_url']."?do=friends";
$this->engine_path = $sys_conf['http_home_url'];
$this->lng = $lng;
}
public function GetList() {
$fr_link = array();
$result = $this->query("SELECT u.name, f.friend_id FROM ".PREFIX."_users_friends f, ".PREFIX."_users u
WHERE f.user_id = (SELECT user_id FROM ".PREFIX."_users WHERE name = '{$this->user_name}')
AND f.approve = 1 AND u.user_id = f.friend_id ORDER BY u.name");
while ($row = $this->get_row($result)){
if (!$this->is_friend && $row['name'] == $this->member_id['name']) $this->is_friend = true;
if ($this->user_name != $this->member_id['name'] || !$this->is_logged) $del_link = "";
else {
$fr_key = base64_encode($row['friend_id'].'#no#'.$this->member_id['user_id']);
if ($this->chpu == 'yes') $del_link = " <a href='{$this->self_path}delete/$fr_key' style='color:red'>(X)</a>";
else $del_link = " <a href='{$this->self_path}&op=delete&key=$fr_key' style='color:red'>(X)</a>";
}
$l_name = urlencode($row['name']);
if ($this->chpu == 'yes') $fr_link[] = "<a href='{$this->engine_path}user/$l_name/'>$row[name]</a>".$del_link;
else $fr_link[] = "<a href='{$this->engine_path}?subaction=userinfo&user=$l_name'>$row[name]</a>".$del_link;
}
$this->free($result);
return implode(", ", $fr_link);
}
public function Profile($tpl){
$tpl->set("{friends}", $this->GetList());
if (!$this->is_friend){
$key = base64_encode($this->member_id['user_id'].'#'.$this->user_name);
if ($this->chpu == "yes") $inv_link = $this->self_path."invite/$key";
else $inv_link = $this->self_path."&op=invite&key=".$key;
$tpl->set('[invfriend]', "<a href='$inv_link'>");
$tpl->set('[/invfriend]', '</a>');
}
else $tpl->set_block("'\\[invfriend\\](.*?)\\[/invfriend\\]'si","");
}
public function Invite($config){
$ex = explode('#', base64_decode($_REQUEST['key']));
$friend_id = intval($ex[0]);
$to_user = $this->safesql($ex[1]);
$to_userid = $this->super_query("SELECT user_id FROM ".PREFIX."_users WHERE name = '$to_user' LIMIT 1");
if ($this->is_friend || $this->member_id['name'] == $to_user) {
$this->err = $this->lng['no_access'];
return;
}
$row = $this->super_query("SELECT approve FROM ".PREFIX."_users_friends WHERE friend_id = '$friend_id'
AND user_id = '{$to_userid['user_id']}' LIMIT 1");
if ($row) {
if ($row['approve'] == 1) $this->err = $this->lng['is_friend'];
else $this->err = $this->lng['friend_send'];
return;
}
$subject = $this->lng['friend_subj'];
$aprove = base64_encode($this->member_id['user_id']."#yes#".$to_userid['user_id']);
$notaprove = base64_encode($this->member_id['user_id']."#no#".$to_userid['user_id']);
$result = $this->query("SELECT template, name FROM ".PREFIX."_email WHERE name IN ('frnd_mail', 'frnd_pm') LIMIT 2");
$lname = urlencode($this->member_id['name']);
if ($this->chpu == "yes") {
$from_link = "<a href='{$this->engine_path}user/$lname/'>{$this->member_id['name']}</a>";
$aprove_lnk = $this->self_path."approve/$aprove";
$noaprove_lnk = $this->self_path."approve/$notaprove";
}
else {
$aprove_lnk = $this->self_path."&op=approve&key=$aprove";
$noaprove_lnk = $this->self_path."&op=approve&key=$notaprove";
$from_link = "<a href='{$this->engine_path}?subaction=userinfo&user=$lname'>{$this->member_id['name']}</a>";
}
while ($tpl = $this->get_row($result)){
$mail_find = array('{%username_to%}','{%username%}','{%site%}','{%aprove%}','{%notaprove%}');
$mail_replace = array($to_user, $from_link, $this->engine_path, $aprove_lnk, $noaprove_lnk);
$tpl['template'] = stripcslashes($tpl['template']);
$tpl['template'] = str_replace($mail_find, $mail_replace, $tpl['template']);
if($tpl['name'] == 'frnd_mail'){
$tpl['template'] = str_replace($mail_find, $mail_replace, str_replace("\n","<br />",$tpl['template']));
$mail = new dle_mail ($config);
$mail->html_mail = true;
$mail->send ($config['admin_mail'], $subject, $tpl['template']);
}
elseif($tpl['name'] == 'frnd_pm') {
$tpl['template'] = $this->safesql($tpl['template']);
$time = time();
$this->query("INSERT INTO ".PREFIX."_pm (subj, text, user, user_from, date, pm_read, folder)
VALUES ('$subject', '$tpl[template]',{$to_userid['user_id']}, '{$this->member_id['name']}',
'$time', 'no', 'inbox')");
$this->query("UPDATE ".PREFIX."_users SET pm_all = pm_all + 1, pm_unread = pm_unread + 1
WHERE user_id = {$to_userid['user_id']}");
}
}
$this->query("INSERT INTO ".PREFIX."_users_friends VALUES ('{$to_userid['user_id']}', '$friend_id', NOW(), 0, 0)");
$this->query("INSERT INTO ".PREFIX."_users_friends VALUES ('$friend_id', '{$to_userid['user_id']}', NOW(), 0, 0)");
$this->free();
$this->err = $this->lng['friend_send'];
}
public function Aprove(){
$ex = explode('#', base64_decode($_REQUEST['key']));
$to_userid = intval($ex[2]);
$from_userid = intval($ex[0]);
$status = $ex[1];
$row = $this->super_query("SELECT approve FROM ".PREFIX."_users_friends WHERE user_id = '$to_userid'
AND friend_id = '$from_userid' LIMIT 1");
if (($this->is_friend || $row['approve'] == 1) && $status != 'no'){
$this->err = $this->lng['err_act'];
return ;
}
if ($status == 'no' && $this->member_id['user_id'] == $to_userid){
$this->query("DELETE FROM ".PREFIX."_users_friends WHERE user_id = '$to_userid' AND friend_id = '$from_userid'");
$this->query("DELETE FROM ".PREFIX."_users_friends WHERE user_id = '$from_userid' AND friend_id = '$to_userid'");
}
else {
$this->query("UPDATE ".PREFIX."_users_friends SET approve = 1
WHERE user_id = '$to_userid' AND friend_id = '$from_userid'");
$this->query("UPDATE ".PREFIX."_users_friends SET approve = 1
WHERE user_id = '$from_userid' AND friend_id = '$to_userid'");
}
$this->free();
if (($to_userid / 100) < 1) $fldr = 1;
else $fldr = floor($to_userid / 100) * 100;
@unlink(ENGINE_DIR."/uploads/userpage/$fldr/$to_userid/cache/pp_friends.tmp");
if (($from_userid / 100) < 1) $fldr = 1;
else $fldr = floor($from_userid / 100) * 100;
@unlink(ENGINE_DIR."/uploads/userpage/$fldr/$from_userid/cache/pp_friends.tmp");
$this->err = $this->lng['friend_act'];
}
}
?>