Guest User

Untitled

a guest
Feb 21st, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.06 KB | None | 0 0
  1. <?php
  2.    
  3.     class user
  4.     {
  5.         public $id;
  6.         public $network;
  7.         public $is_logged;
  8.         public $info;
  9.         public $sess;
  10.        
  11.         public function __construct()
  12.         {
  13.             $this->id   = FALSE;
  14.             $this->network  = & $GLOBALS['network'];
  15.             $this->cache    = & $GLOBALS['cache'];
  16.             $this->db1      = & $GLOBALS['db1'];
  17.             $this->db2      = & $GLOBALS['db2'];
  18.             $this->info     = new stdClass;
  19.             $this->is_logged    = FALSE;
  20.             $this->sess     = array();
  21.         }
  22.        
  23.         public function LOAD()
  24.         {
  25.             if( ! $this->network->id ) {
  26.                 return FALSE;
  27.             }
  28.             global $C;
  29.             $this->_session_start();
  30.             if( isset($this->sess['IS_LOGGED'], $this->sess['LOGGED_USER']) && $this->sess['IS_LOGGED'] && $this->sess['LOGGED_USER'] ) {
  31.                 $u  = & $this->sess['LOGGED_USER'];
  32.                 $u  = $this->network->get_user_by_id($u->id);
  33.                 if( ! $u ) {
  34.                     return FALSE;
  35.                 }
  36.                 if( $this->network->id && $this->network->id == $u->network_id ) {
  37.                     $this->is_logged    = TRUE;
  38.                     $this->info = & $u;
  39.                     $this->id   = $this->info->id;
  40.                     $this->db2->query('UPDATE users SET lastclick_date="'.time().'" WHERE id="'.$this->id.'" LIMIT 1');
  41.                     $deflang    = $C->LANGUAGE;
  42.                     if( ! empty($this->info->language) ) {
  43.                         $C->LANGUAGE    = $this->info->language;
  44.                     }
  45.                     if( $C->LANGUAGE != $deflang ) {
  46.                         $current_language   = new stdClass;
  47.                         include($C->INCPATH.'languages/'.$C->LANGUAGE.'/language.php');
  48.                         date_default_timezone_set($current_language->php_timezone);
  49.                         setlocale(LC_ALL, $current_language->php_locale);
  50.                     }
  51.                     if( ! empty($this->info->timezone) ) {
  52.                         date_default_timezone_set($this->info->timezone);
  53.                     }
  54.                     if( $this->info->active == 0 ) {
  55.                         $this->logout();
  56.                         return FALSE;
  57.                     }
  58.                     return $this->id;
  59.                 }
  60.             }
  61.             if( $this->try_autologin() ) {
  62.                 $this->LOAD();
  63.             }
  64.             return FALSE;
  65.         }
  66.        
  67.         private function _session_start()
  68.         {
  69.             if( ! $this->network->id ) {
  70.                 return FALSE;
  71.             }
  72.             if( ! isset($_SESSION['NETWORKS_USR_DATA']) ) {
  73.                 $_SESSION['NETWORKS_USR_DATA']  = array();
  74.             }
  75.             if( ! isset($_SESSION['NETWORKS_USR_DATA'][$this->network->id]) ) {
  76.                 $_SESSION['NETWORKS_USR_DATA'][$this->network->id]  = array();
  77.             }
  78.             $this->sess = & $_SESSION['NETWORKS_USR_DATA'][$this->network->id];
  79.         }
  80.        
  81.         public function login($login, $pass, $rememberme=FALSE)
  82.         {
  83.             global $C;
  84.             if( ! $this->network->id ) {
  85.                 return FALSE;
  86.             }
  87.             if( $this->is_logged ) {
  88.                 return FALSE;
  89.             }
  90.             if( empty($login) ) {
  91.                 return FALSE;
  92.             }
  93.             $login  = $this->db2->escape($login);
  94.             $pass       = $this->db2->escape($pass);
  95.             $this->db2->query('SELECT id FROM users WHERE (email="'.$login.'" OR username="'.$login.'") AND password="'.$pass.'" AND active=1 LIMIT 1');
  96.             if( ! $obj = $this->db2->fetch_object() ) {
  97.                 return FALSE;
  98.             }
  99.             $this->info = $this->network->get_user_by_id($obj->id, TRUE);
  100.             if( ! $this->info ) {
  101.                 return FALSE;
  102.             }
  103.             $this->is_logged        = TRUE;
  104.             $this->sess['IS_LOGGED']    = TRUE;
  105.             $this->sess['LOGGED_USER']  = & $this->info;
  106.             $this->id   = $this->info->id;
  107.            
  108.             $ip = $this->db2->escape( ip2long($_SERVER['REMOTE_ADDR']) );
  109.             $this->db2->query('UPDATE users SET lastlogin_date="'.time().'", lastlogin_ip="'.$ip.'", lastclick_date="'.time().'" WHERE id="'.$this->id.'" LIMIT 1');
  110.             if( TRUE == $rememberme ) {
  111.                 $tmp    = $this->id.'_'.md5($this->info->username.'~~'.$this->info->password.'~~'.$_SERVER['HTTP_USER_AGENT']);
  112.                 setcookie('rememberme', $tmp, time()+60*24*60*60, '/', cookie_domain());
  113.             }
  114.            
  115.             $this->sess['total_pageviews']  = 0;
  116.             $this->sess['cdetails'] = $this->db2->fetch('SELECT * FROM users_details WHERE user_id="'.$this->id.'" LIMIT 1');
  117.             return TRUE;
  118.         }
  119.        
  120.         public function try_autologin()
  121.         {
  122.             if( ! $this->network->id ) {
  123.                 return FALSE;
  124.             }
  125.             if( $this->is_logged ) {
  126.                 return FALSE;
  127.             }
  128.             if( ! isset($_COOKIE['rememberme']) ) {
  129.                 return FALSE;
  130.             }
  131.             $tmp    = explode('_', $_COOKIE['rememberme']);
  132.             if ($tmp[0] != $this->id) {
  133.                 return FALSE;
  134.             }
  135.             $this->db2->query('SELECT username, password, email FROM users WHERE id="'.intval($tmp[0]).'" AND active=1 LIMIT 1');
  136.             if( ! $obj = $this->db2->fetch_object() ) {
  137.                 return FALSE;
  138.             }
  139.             $obj->username  = stripslashes($obj->username);
  140.             $obj->password  = stripslashes($obj->password);
  141.             if( $tmp[1] == md5($obj->username.'~~'.$obj->password.'~~'.$_SERVER['HTTP_USER_AGENT']) ) {
  142.                 return $this->login($obj->username, $obj->password, TRUE);
  143.             }
  144.             setcookie('rememberme', NULL, time()+30*24*60*60, '/', cookie_domain());
  145.             $_COOKIE['rememberme']  = NULL;
  146.             return FALSE;
  147.         }
  148.        
  149.         public function logout()
  150.         {
  151.             if( ! $this->is_logged ) {
  152.                 return FALSE;
  153.             }
  154.             setcookie('rememberme', NULL, time()+60*24*60*60, '/', cookie_domain());
  155.             $_COOKIE['rememberme']  = NULL;
  156.             $this->sess['IS_LOGGED']    = FALSE;
  157.             $this->sess['LOGGED_USER']  = NULL;
  158.             unset($this->sess['IS_LOGGED']);
  159.             unset($this->sess['LOGGED_USER']);
  160.             $this->id   = FALSE;
  161.             $this->info = new stdClass;
  162.             $this->is_logged    = FALSE;
  163.             $_SESSION['TWITTER_CONNECTED']  = FALSE;
  164.         }
  165.        
  166.         public function follow($whom_id, $how=TRUE)
  167.         {
  168.             if( ! $this->is_logged ) {
  169.                 return FALSE;
  170.             }
  171.             $whom   = $this->network->get_user_by_id($whom_id);
  172.             if( ! $whom ) {
  173.                 return FALSE;
  174.             }
  175.             $f  = $this->network->get_user_follows($this->id)->follow_users;
  176.             if( isset($f[$whom_id]) && $how==TRUE ) {
  177.                 return TRUE;
  178.             }
  179.             if( !isset($f[$whom_id]) && $how==FALSE ) {
  180.                 return TRUE;
  181.             }
  182.             if( $how == TRUE ) {
  183.                 $this->db2->query('INSERT INTO users_followed SET who="'.$this->id.'", whom="'.$whom_id.'", date="'.time().'", whom_from_postid="'.$this->network->get_last_post_id().'" ');
  184.                 $this->db2->query('UPDATE users SET num_followers=num_followers+1 WHERE id="'.$whom_id.'" LIMIT 1');
  185.                
  186.                 $n  = intval( $this->network->get_user_notif_rules($this->id)->ntf_them_if_i_follow_usr );
  187.                 if( $n == 1 ) {
  188.                     global $C, $page;
  189.                     $page->load_langfile('inside/notifications.php');
  190.                     $page->load_langfile('email/notifications.php');
  191.                     $send_post  = TRUE;
  192.                     $send_mail  = FALSE;
  193.                     $n  = intval( $this->network->get_user_notif_rules($whom_id)->ntf_me_if_u_follows_me );
  194.                     if( $n == 2 ) { $send_post = TRUE; } elseif( $n == 3 ) { $send_mail = TRUE; } elseif( $n == 1 ) { $send_post = TRUE; $send_mail = TRUE; }
  195.                     if( $send_post ) {
  196.                         $lng    = array('#USER#'=>'<a href="'.$C->SITE_URL.$this->info->username.'" title="'.htmlspecialchars($this->info->fullname).'"><span class="mpost_mentioned">@</span>'.$this->info->username.'</a>');
  197.                         $this->network->send_notification_post($whom_id, 0, 'msg_ntf_me_if_u_follows_me', $lng, $C->NOTIF_POSTS_HANDLE);
  198.                     }
  199.                     if( $send_mail ) {
  200.                         $ulng   = trim($this->network->get_user_by_id($whom_id)->language);
  201.                         $lng_txt    = array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'@'.$this->info->username, '#NAME#'=>$this->info->fullname, '#A0#'=>$C->SITE_URL.$this->info->username);
  202.                         $lng_htm    = array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'<a href="'.$C->SITE_URL.$this->info->username.'" title="'.htmlspecialchars($this->info->fullname).'" target="_blank">@'.$this->info->username.'</a>', '#NAME#'=>$this->info->fullname);
  203.                         $subject        = $page->lang('emlsubj_ntf_me_if_u_follows_me', $lng_txt, $ulng);
  204.                         $message_txt    = $page->lang('emltxt_ntf_me_if_u_follows_me', $lng_txt, $ulng);
  205.                         $message_htm    = $page->lang('emlhtml_ntf_me_if_u_follows_me', $lng_htm, $ulng);
  206.                         $this->network->send_notification_email($whom_id, 'u_follows_me', $subject, $message_txt, $message_htm);
  207.                     }
  208.                     $followers  = array_keys($this->network->get_user_follows($this->id)->followers);
  209.                     foreach($followers as $uid) {
  210.                         if( $uid == $whom_id ) { continue; }
  211.                         $send_post  = FALSE;
  212.                         $send_mail  = FALSE;
  213.                         $n  = intval( $this->network->get_user_notif_rules($uid)->ntf_me_if_u_follows_u2 );
  214.                         if( $n == 2 ) { $send_post = TRUE; } elseif( $n == 3 ) { $send_mail = TRUE; } elseif( $n == 1 ) { $send_post = TRUE; $send_mail = TRUE; }
  215.                         if( $send_post ) {
  216.                             $lng    = array('#USER#'=>'<a href="'.$C->SITE_URL.$this->info->username.'" title="'.htmlspecialchars($this->info->fullname).'"><span class="mpost_mentioned">@</span>'.$this->info->username.'</a>', '#USER2#'=>'<a href="'.$C->SITE_URL.$whom->username.'" title="'.htmlspecialchars($whom->fullname).'"><span class="mpost_mentioned">@</span>'.$whom->username.'</a>');
  217.                             $this->network->send_notification_post($uid, 0, 'msg_ntf_me_if_u_follows_u2', $lng, $C->NOTIF_POSTS_HANDLE);
  218.                         }
  219.                         if( $send_mail ) {
  220.                             $ulng   = trim($this->network->get_user_by_id($uid)->language);
  221.                             $lng_txt    = array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'@'.$this->info->username, '#NAME#'=>$this->info->fullname, '#A0#'=>$C->SITE_URL.$this->info->username, '#USER2#'=>$whom->username, '#NAME2#'=>$whom->fullname);
  222.                             $lng_htm    = array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'<a href="'.$C->SITE_URL.$this->info->username.'" title="'.htmlspecialchars($this->info->fullname).'" target="_blank">@'.$this->info->username.'</a>', '#NAME#'=>$this->info->fullname, '#USER2#'=>'<a href="'.$C->SITE_URL.$whom->username.'" title="'.htmlspecialchars($whom->fullname).'" target="_blank">@'.$whom->username.'</a>', '#NAME2#'=>$whom->fullname);
  223.                             $subject        = $page->lang('emlsubj_ntf_me_if_u_follows_u2', $lng_txt, $ulng);
  224.                             $message_txt    = $page->lang('emltxt_ntf_me_if_u_follows_u2', $lng_txt, $ulng);
  225.                             $message_htm    = $page->lang('emlhtml_ntf_me_if_u_follows_u2', $lng_htm, $ulng);
  226.                             $this->network->send_notification_email($uid, 'u_follows_u2', $subject, $message_txt, $message_htm);
  227.                         }
  228.                     }
  229.                 }
  230.             }
  231.             else {
  232.                 $this->db2->query('DELETE FROM users_followed WHERE who="'.$this->id.'" AND whom="'.$whom_id.'" ');
  233.                 $this->db2->query('UPDATE users SET num_followers=num_followers-1 WHERE id="'.$whom_id.'" LIMIT 1');
  234.                 $this->db2->query('DELETE FROM post_userbox WHERE user_id="'.$this->id.'" AND post_id IN(SELECT id FROM posts WHERE user_id="'.$whom_id.'")');
  235.                 $this->db2->query('DELETE FROM post_userbox_feeds WHERE user_id="'.$this->id.'" AND post_id IN(SELECT id FROM posts WHERE user_id="'.$whom_id.'")');
  236.             }
  237.             $this->network->get_user_by_id($whom_id, TRUE);
  238.             $this->network->get_user_follows($whom_id, TRUE);
  239.             $this->network->get_user_follows($this->id, TRUE);
  240.             return TRUE;
  241.         }
  242.        
  243.         public function follow_group($group_id, $how=TRUE)
  244.         {
  245.             if( ! $this->is_logged ) {
  246.                 return FALSE;
  247.             }
  248.             $group  = $this->network->get_group_by_id($group_id);
  249.             if( ! $group ) {
  250.                 return FALSE;
  251.             }
  252.             $priv_members   = array();
  253.             if( $group->is_private && !$this->info->is_network_admin ) {
  254.                 $priv_members   = $this->network->get_group_invited_members($group_id);
  255.                 if( ! $priv_members ) {
  256.                     return FALSE;
  257.                 }
  258.                 if( ! in_array(intval($this->id), $priv_members) ) {
  259.                     return FALSE;
  260.                 }
  261.             }
  262.             $f  = $this->network->get_user_follows($this->id)->follow_groups;
  263.             if( isset($f[$group_id]) && $how==TRUE ) {
  264.                 return TRUE;
  265.             }
  266.             if( !isset($f[$group_id]) && $how==FALSE ) {
  267.                 return TRUE;
  268.             }
  269.             if( $how == TRUE ) {
  270.                 $this->db2->query('INSERT INTO groups_followed SET user_id="'.$this->id.'", group_id="'.$group_id.'", date="'.time().'", group_from_postid="'.$this->network->get_last_post_id().'" ');
  271.                 $this->db2->query('UPDATE groups SET num_followers=num_followers+1 WHERE id="'.$group_id.'" LIMIT 1');
  272.                 $n  = intval( $this->network->get_user_notif_rules($this->id)->ntf_them_if_i_join_grp );
  273.                 if( $n == 1 ) {
  274.                     global $C, $page;
  275.                     $page->load_langfile('inside/notifications.php');
  276.                     $page->load_langfile('email/notifications.php');
  277.                     $followers  = array_keys($this->network->get_user_follows($this->id)->followers);
  278.                     foreach($followers as $uid) {
  279.                         $uid    = intval($uid);
  280.                         if( $group->is_private && !in_array($uid, $priv_members) ) {
  281.                             continue;
  282.                         }
  283.                         $send_post  = FALSE;
  284.                         $send_mail  = FALSE;
  285.                         $n  = intval( $this->network->get_user_notif_rules($uid)->ntf_me_if_u_joins_grp );
  286.                         if( $n == 2 ) { $send_post = TRUE; } elseif( $n == 3 ) { $send_mail = TRUE; } elseif( $n == 1 ) { $send_post = TRUE; $send_mail = TRUE; }
  287.                         if( $send_post ) {
  288.                             $lng    = array('#USER#'=>'<a href="'.$C->SITE_URL.$this->info->username.'" title="'.htmlspecialchars($this->info->fullname).'"><span class="mpost_mentioned">@</span>'.$this->info->username.'</a>', '#GROUP#'=>'<a href="'.$C->SITE_URL.$group->groupname.'" title="'.$group->title.'">'.$group->title.'</a>');
  289.                             $this->network->send_notification_post($uid, 0, 'msg_ntf_me_if_u_joins_grp', $lng, $C->NOTIF_POSTS_HANDLE);
  290.                         }
  291.                         if( $send_mail ) {
  292.                             $ulng   = trim($this->network->get_user_by_id($uid)->language);
  293.                             $lng_txt    = array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'@'.$this->info->username, '#NAME#'=>$this->info->fullname, '#GROUP#'=>$group->title, '#A0#'=>$C->SITE_URL.$group->groupname);
  294.                             $lng_htm    = array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'<a href="'.$C->SITE_URL.$this->info->username.'" title="'.htmlspecialchars($this->info->fullname).'" target="_blank">@'.$this->info->username.'</a>', '#NAME#'=>$this->info->fullname, '#GROUP#'=>'<a href="'.$C->SITE_URL.$group->groupname.'" title="'.$group->title.'" target="_blank">'.$group->title.'</a>');
  295.                             $subject        = $page->lang('emlsubj_ntf_me_if_u_joins_grp', $lng_txt, $ulng);
  296.                             $message_txt    = $page->lang('emltxt_ntf_me_if_u_joins_grp', $lng_txt, $ulng);
  297.                             $message_htm    = $page->lang('emlhtml_ntf_me_if_u_joins_grp', $lng_htm, $ulng);
  298.                             $this->network->send_notification_email($uid, 'u_joins_grp', $subject, $message_txt, $message_htm);
  299.                         }
  300.                     }
  301.                     $lng    = array('#USER#'=>'<a href="'.$C->SITE_URL.$this->info->username.'" title="'.htmlspecialchars($this->info->fullname).'"><span class="mpost_mentioned">@</span>'.$this->info->username.'</a>', '#GROUP#'=>'<a href="'.$C->SITE_URL.$group->groupname.'" title="'.$group->title.'">'.$group->title.'</a>');
  302.                     $this->network->send_notification_post(0, $group_id, 'msg_ntf_grp_if_u_joins', $lng, $C->NOTIF_POSTS_HANDLE);
  303.                 }
  304.             }
  305.             else {
  306.                 if( ! $this->if_can_leave_group($group_id) ) {
  307.                     return FALSE;
  308.                 }
  309.                 $this->db2->query('DELETE FROM groups_admins WHERE user_id="'.$this->id.'" AND group_id="'.$group_id.'" ');
  310.                 $this->db2->query('DELETE FROM groups_followed WHERE user_id="'.$this->id.'" AND group_id="'.$group_id.'" ');
  311.                 $this->db2->query('UPDATE groups SET num_followers=num_followers-1 WHERE id="'.$group_id.'" LIMIT 1');
  312.                 $not_in_users   = array_keys($this->network->get_user_follows($this->id)->follow_users);
  313.                 $not_in_users   = count($not_in_users)==0 ? '' : 'AND user_id NOT IN('.implode(', ', $not_in_users).')';
  314.                 $this->db2->query('DELETE FROM post_userbox WHERE user_id="'.$this->id.'" AND post_id IN(SELECT id FROM posts WHERE group_id="'.$group_id.'" AND user_id<>"'.$this->id.'" '.$not_in_users.' )');
  315.                 $this->db2->query('DELETE FROM post_userbox_feeds WHERE user_id="'.$this->id.'" AND post_id IN(SELECT id FROM posts WHERE group_id="'.$group_id.'" AND user_id<>"'.$this->id.'" '.$not_in_users.' )');
  316.             }
  317.             $this->network->get_group_by_id($group_id, TRUE);
  318.             $this->network->get_group_members($group_id, TRUE);
  319.             $this->network->get_user_follows($this->id, TRUE);
  320.             $this->get_top_groups(1, TRUE);
  321.             return TRUE;
  322.         }
  323.        
  324.         public function if_follow_user($user_id)
  325.         {
  326.             if( ! $this->is_logged ) {
  327.                 return FALSE;
  328.             }
  329.             return isset($this->network->get_user_follows($this->id)->follow_users[$user_id]);
  330.         }
  331.        
  332.         public function if_follow_group($group_id)
  333.         {
  334.             if( ! $this->is_logged ) {
  335.                 return FALSE;
  336.             }
  337.             return isset($this->network->get_user_follows($this->id)->follow_groups[$group_id]);
  338.         }
  339.        
  340.         public function if_can_leave_group($group_id)
  341.         {
  342.             if( ! $this->is_logged ) {
  343.                 return FALSE;
  344.             }
  345.             static $loaded=array();
  346.             if( ! isset($loaded[$group_id]) ) {
  347.                 $r  = $this->db2->query('SELECT user_id FROM groups_admins WHERE group_id="'.intval($group_id).'" AND user_id<>"'.$this->id.'" LIMIT 1', FALSE);
  348.                 $loaded[$group_id]  = $this->db2->num_rows($r)>0;   // демек ако има други админи освен мен, мога да куитна
  349.             }
  350.             return $loaded[$group_id];
  351.         }
  352.        
  353.         public function get_top_groups($num, $force_refresh=FALSE)
  354.         {
  355.             static $loaded=FALSE;
  356.             if( !$loaded || $force_refresh ) {
  357.                 $loaded = array();
  358.                 $tmp    = $this->network->get_user_follows($this->id)->follow_groups;
  359.                 foreach($tmp as $gid=>$sdf) {
  360.                     $g  = $this->network->get_group_by_id($gid, $force_refresh);
  361.                     if( ! $g ) {
  362.                         continue;
  363.                     }
  364.                     $loaded[]   = $g;
  365.                 }
  366.                 $loaded = array_reverse($loaded);
  367.             }
  368.             return array_slice($loaded, 0, $num);
  369.         }
  370.        
  371.         public function write_pageview()
  372.         {
  373.             if( ! $this->is_logged ) {
  374.                 return FALSE;
  375.             }
  376.             $this->sess['total_pageviews']  ++;
  377.             $dt = date('Y-m-d H');
  378.             $this->db2->query('UPDATE users_pageviews SET pageviews=pageviews+1 WHERE user_id="'.$this->id.'" AND date="'.$dt.'" LIMIT 1');
  379.             if( $this->db2->affected_rows() == 0 ) {
  380.                 $this->db2->query('INSERT INTO users_pageviews SET pageviews=1, user_id="'.$this->id.'", date="'.$dt.'" ');
  381.             }
  382.         }
  383.     }
  384.    
  385. ?>
Add Comment
Please, Sign In to add comment