Advertisement
Guest User

user.class.php

a guest
Oct 19th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 172.03 KB | None | 0 0
  1. <?php
  2.  
  3. class userquery extends CBCategory
  4. {
  5.     var $userid = '';
  6.     var $username = '';
  7.     var $level = '';
  8.     var $permissions = '';
  9.     var $access_type_list = []; //Access list
  10.     var $usr_levels = [];
  11.     var $custom_signup_fields = [];
  12.     var $custom_profile_fields = [];
  13.     var $custom_profile_fields_groups = [];
  14.     var $delete_user_functions = [];
  15.     var $logout_functions = [];
  16.     var $user_account = [];
  17.     var $profileItem = '';
  18.     var $sessions = '';
  19.     var $is_login = false;
  20.     var $custom_subscription_email_vars = [];
  21.  
  22.     var $dbtbl = [
  23.         'user_permission_type'  => 'user_permission_types',
  24.         'user_permissions'      => 'user_permissions',
  25.         'user_level_permission' => 'user_levels_permissions',
  26.         'user_profile'          => 'user_profile',
  27.         'users'                 => 'users',
  28.         'action_log'            => 'action_log',
  29.         'subtbl'                => 'subscriptions',
  30.         'contacts'              => 'contacts'
  31.     ];
  32.  
  33.     var $udetails = [];
  34.  
  35.     private $basic_fields = [];
  36.     private $extra_fields = [];
  37.  
  38.     function __construct()
  39.     {
  40.         global $cb_columns;
  41.  
  42.         $this->cat_tbl = 'user_categories';
  43.  
  44.         $basic_fields = [
  45.             'userid', 'username', 'email', 'avatar', 'sex', 'avatar_url',
  46.             'dob', 'level', 'usr_status', 'user_session_key', 'featured',
  47.             'ban_status', 'total_photos', 'profile_hits', 'total_videos',
  48.             'subscribers', 'total_subscriptions'
  49.         ];
  50.  
  51.         $cb_columns->object('users')->register_columns($basic_fields);
  52.     }
  53.  
  54.     /**
  55.      * @throws Exception
  56.      */
  57.     function init()
  58.     {
  59.         global $sess, $Cbucket;
  60.  
  61.         $this->sess_salt = $sess->get('sess_salt');
  62.         $this->sessions = $this->get_sessions();
  63.  
  64.         if ($this->sessions['smart_sess']) {
  65.             $this->userid = $this->sessions['smart_sess']['session_user'];
  66.         }
  67.  
  68.         $udetails = '';
  69.  
  70.         if ($this->userid) {
  71.             $udetails = $this->get_user_details($this->userid, true);
  72.             $user_profile = $this->get_user_profile($this->userid);
  73.             if ($udetails && $user_profile) {
  74.                 $udetails['profile'] = $user_profile;
  75.             }
  76.         }
  77.  
  78.         if ($udetails) {
  79.             $this->udetails = $udetails;
  80.             $this->username = $udetails['username'];
  81.             $this->level = $this->udetails['level'];
  82.             $this->permission = $this->get_user_level(user_id());
  83.  
  84.             //Calling Logout Functions
  85.             $funcs = $this->init_login_functions ?? false;
  86.             if (is_array($funcs) && count($funcs) > 0) {
  87.                 foreach ($funcs as $func) {
  88.                     if (function_exists($func)) {
  89.                         $func();
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             if ($sess->get('dummy_username') == '') {
  95.                 $this->UpdateLastActive(user_id());
  96.             }
  97.         } else {
  98.             $this->permission = $this->get_user_level(4, true);
  99.         }
  100.  
  101.         //Adding Actions such Report, share,fav etc
  102.         $this->action = new cbactions();
  103.         $this->action->type = 'u';
  104.         $this->action->name = 'user';
  105.         $this->action->obj_class = 'userquery';
  106.         $this->action->check_func = 'user_exists';
  107.         $this->action->type_tbl = $this->dbtbl['users'];
  108.         $this->action->type_id_field = 'userid';
  109.  
  110.         define('AVATAR_SIZE', config('max_profile_pic_width'));
  111.         define('AVATAR_SMALL_SIZE', 40);
  112.         define('BG_SIZE', config('max_bg_width'));
  113.         define('BACKGROUND_URL', config('background_url'));
  114.         define('BACKGROUND_COLOR', config('background_color'));
  115.         if (isSectionEnabled('channels')) {
  116.             $Cbucket->search_types['channels'] = 'userquery';
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * Function used to create user session key
  122.      */
  123.     function create_session_key($session, $pass): string
  124.     {
  125.         $newkey = $session . $pass;
  126.         $newkey = md5($newkey);
  127.         return $newkey;
  128.     }
  129.  
  130.     /**
  131.      * Function used to create user session code
  132.      * just for session authentication incase user wants to login again
  133.      */
  134.     function create_session_code(): int
  135.     {
  136.         return rand(10000, 99999);
  137.     }
  138.  
  139.     /**
  140.      * @return array
  141.      */
  142.     function get_basic_fields(): array
  143.     {
  144.         return $this->basic_fields;
  145.     }
  146.  
  147.     function set_basic_fields($fields = [])
  148.     {
  149.         return $this->basic_fields = $fields;
  150.     }
  151.  
  152.     function basic_fields_setup()
  153.     {
  154.         # Set basic video fields
  155.        $basic_fields = [
  156.             'userid', 'username', 'email', 'avatar', 'sex', 'avatar_url',
  157.             'dob', 'level', 'usr_status', 'user_session_key', 'total_photos', 'profile_hits', 'total_videos', 'total_subscriptions'
  158.         ];
  159.  
  160.         return $this->set_basic_fields($basic_fields);
  161.     }
  162.  
  163.     function get_extra_fields(): array
  164.     {
  165.         return $this->extra_fields;
  166.     }
  167.  
  168.     function set_extra_fields($fields = [])
  169.     {
  170.         return $this->extra_fields = $fields;
  171.     }
  172.  
  173.     function get_user_db_fields($extra_fields = [])
  174.     {
  175.         $fields = $this->get_basic_fields();
  176.         $extra = $this->get_extra_fields();
  177.  
  178.         if (empty($fields)) {
  179.             $fields = $this->basic_fields_setup();
  180.         }
  181.  
  182.         if (!empty($extra)) {
  183.             $fields = array_merge($fields, $extra);
  184.         }
  185.  
  186.         if (!empty($extra_fields)) {
  187.             if (is_array($extra_fields)) {
  188.                 $fields = array_merge($fields, $extra_fields);
  189.             } else {
  190.                 $fields[] = $extra_fields;
  191.             }
  192.         }
  193.  
  194.         # Do make array unique, otherwise we might get duplicate
  195.        # fields
  196.        return array_unique($fields);
  197.     }
  198.  
  199.     function add_user_field($field)
  200.     {
  201.         $extra_fields = $this->get_extra_fields();
  202.  
  203.         if (is_array($field)) {
  204.             $extra_fields = array_merge($extra_fields, $field);
  205.         } else {
  206.             $extra_fields[] = $field;
  207.         }
  208.  
  209.         return $this->set_extra_fields($extra_fields);
  210.     }
  211.  
  212.     /**
  213.      * Neat and clean function to login user
  214.      * this function was made for v2.x with User Level System
  215.      * param VARCHAR $username
  216.      * param TEXT $password
  217.      *
  218.      * @param      $username
  219.      * @param      $password
  220.      * @param bool $remember
  221.      *
  222.      * @return bool
  223.      * @throws Exception
  224.      */
  225.     function login_user($username, $password, $remember = false): bool
  226.     {
  227.         global $sess, $db;
  228.  
  229.         //First we will check weather user is already logged in or not
  230.         if ($this->login_check(null, true)) {
  231.             $msg[] = e(lang('you_already_logged'));
  232.         } else {
  233.             $user = $this->get_user_id($username);
  234.             if (!$user) {
  235.                 $msg[] = e(lang('usr_login_err'));
  236.             } else {
  237.                 $uid = $user['userid'];
  238.                 $pass = pass_code($password, $uid);
  239.                 $udetails = $this->get_user_with_pass($username, $pass);
  240.  
  241.                 // This code is used to update user password hash, may be deleted someday
  242.                 if (!$udetails) // Let's try old password method
  243.                 {
  244.                     $oldpass = pass_code_unsecure($password);
  245.                     $udetails = $this->get_user_with_pass($username, $oldpass);
  246.  
  247.                     if ($udetails) // This account still use old password method, let's update it
  248.                     {
  249.                         $db->update(tbl('users'), ['password'], [$pass], ' userid=\'' . $uid . '\'');
  250.                     }
  251.                 }
  252.  
  253.                 if (!$udetails) {
  254.                     $msg[] = e(lang('usr_login_err'));
  255.                 } elseif (strtolower($udetails['usr_status']) != 'ok') {
  256.                     $msg[] = e(lang('user_inactive_msg'), 'e', false);
  257.                 } elseif ($udetails['ban_status'] == 'yes') {
  258.                     $msg[] = e(lang('usr_ban_err'));
  259.                 } else {
  260.                     if ($remember) {
  261.                         $sess->timeout = 86400 * REMBER_DAYS;
  262.                     }
  263.  
  264.                     //Starting special sessions for security
  265.                     $session_salt = RandomString(5);
  266.                     $sess->set('sess_salt', $session_salt);
  267.                     $sess->set('PHPSESSID', $sess->id);
  268.  
  269.                     $smart_sess = md5($udetails['user_session_key'] . $session_salt);
  270.  
  271.                     $db->delete(tbl('sessions'), ['session', 'session_string'], [$sess->id, 'guest']);
  272.                     $sess->add_session($udetails['userid'], 'smart_sess', $smart_sess);
  273.  
  274.                     //Setting Vars
  275.                     $this->userid = $udetails['userid'];
  276.                     $this->username = $udetails['username'];
  277.                     $this->level = $udetails['level'];
  278.  
  279.                     //Updating User last login , num of visits and ip
  280.                     $db->update(tbl('users'),
  281.                         ['num_visits', 'last_logged', 'ip'],
  282.                         ['|f|num_visits+1', NOW(), $_SERVER['REMOTE_ADDR']],
  283.                         'userid=\'' . $udetails['userid'] . '\''
  284.                     );
  285.  
  286.                     $this->init();
  287.  
  288.                     //Logging Action
  289.                     $log_array = [
  290.                         'username'  => $username,
  291.                         'userid'    => $udetails['userid'],
  292.                         'useremail' => $udetails['email'],
  293.                         'success'   => 'yes',
  294.                         'level'     => $udetails['level']
  295.                     ];
  296.                     insert_log('Login', $log_array);
  297.                     return true;
  298.                 }
  299.             }
  300.         }
  301.  
  302.         //Error Logging
  303.         if (!empty($msg)) {
  304.             //Logging Action
  305.             $log_array['success'] = 'no';
  306.             $log_array['details'] = $msg[0]['val'];
  307.             $log_array['username'] = $username;
  308.             insert_log('Login', $log_array);
  309.         }
  310.         return false;
  311.     }
  312.  
  313.     /**
  314.      * Function used to check weather user is login or not
  315.      * it will also check weather user has access or not
  316.      *
  317.      * @param $access string access type it can be admin_access, upload_acess etc
  318.      * you can either set it as level id
  319.      * @param bool $check_only
  320.      * @param bool $verify_logged_user
  321.      *
  322.      * @return bool
  323.      * @throws Exception
  324.      */
  325.     function login_check($access = null, $check_only = false, $verify_logged_user = true)
  326.     {
  327.         global $Cbucket;
  328.  
  329.         if ($verify_logged_user) {
  330.             //First check weather userid is here or not
  331.             if (!user_id()) {
  332.                 if (!$check_only) {
  333.                     e(lang('you_not_logged_in'));
  334.                 }
  335.                 return false;
  336.             }
  337.  
  338.             //Now Check if logged in user exists or not
  339.             if (!$this->user_exists(user_id(), true)) {
  340.                 if (!$check_only) {
  341.                     e(lang('invalid_user'));
  342.                 }
  343.                 return false;
  344.             }
  345.  
  346.             //Now Check logged in user is banned or not
  347.             if ($this->is_banned(user_id()) == 'yes') {
  348.                 if (!$check_only) {
  349.                     e(lang('usr_ban_err'));
  350.                 }
  351.                 return false;
  352.             }
  353.         }
  354.  
  355.         //Now user have passed all the stages, now checking if user has level access or not
  356.         if ($access) {
  357.             $access_details = $this->permission;
  358.  
  359.             if (is_numeric($access)) {
  360.                 if ($access_details['level_id'] == $access) {
  361.                     return true;
  362.                 }
  363.  
  364.                 if (!$check_only) {
  365.                     e(lang('insufficient_privileges'));
  366.                 }
  367.                 $Cbucket->show_page(false);
  368.                 return false;
  369.             }
  370.  
  371.             if ($access_details[$access] == 'yes') {
  372.                 return true;
  373.             }
  374.  
  375.             if (!$check_only) {
  376.                 e(lang('insufficient_privileges'));
  377.                 $Cbucket->show_page(false);
  378.             }
  379.             return false;
  380.         }
  381.         return true;
  382.     }
  383.  
  384.     /**
  385.      * This function was used to check
  386.      * user is logged in or not -- for v1.7.x and old
  387.      * it has been replaced by login_check in v2
  388.      * this function is sitll in use so
  389.      * we are just replace the lil code of it
  390.      *
  391.      * @param null $access
  392.      * @param bool $redirect
  393.      *
  394.      * @return bool
  395.      * @throws Exception
  396.      */
  397.     function logincheck($access = null, $redirect = true): bool
  398.     {
  399.  
  400.         if (!$this->login_check($access)) {
  401.             if ($redirect) {
  402.                 redirect_to(cblink(['name' => 'signup'], true));
  403.             }
  404.             return false;
  405.         }
  406.         return true;
  407.     }
  408.  
  409.     /**
  410.      * Function used to get user details using username and password
  411.      *
  412.      * @param $username
  413.      * @param $pass
  414.      *
  415.      * @return bool|array
  416.      * @throws Exception
  417.      */
  418.     function get_user_with_pass($username, $pass)
  419.     {
  420.         global $db;
  421.         $results = $db->select(tbl('users'),
  422.             'userid,email,level,usr_status,user_session_key,user_session_code,ban_status',
  423.             "(username='$username' OR userid='$username') AND password='$pass'");
  424.         if (count($results) > 0) {
  425.             return $results[0];
  426.         }
  427.         return false;
  428.     }
  429.  
  430.     /**
  431.      * @throws Exception
  432.      */
  433.     function get_user_id($username)
  434.     {
  435.         global $db;
  436.         $results = $db->select(tbl('users'), 'userid', "(username='$username' OR userid='$username')");
  437.         if (count($results) > 0) {
  438.             return $results[0];
  439.         }
  440.         return false;
  441.     }
  442.  
  443.  
  444.     /**
  445.      * Function used to check weather user is banned or not
  446.      *
  447.      * @param $uid
  448.      *
  449.      * @return mixed
  450.      * @throws Exception
  451.      */
  452.     function is_banned($uid)
  453.     {
  454.         if (empty($this->udetails['ban_status']) && user_id()) {
  455.             $this->udetails['ban_status'] = $this->get_user_field($uid, 'ban_status');
  456.         }
  457.         return $this->udetails['ban_status'];
  458.     }
  459.  
  460.     /**
  461.      * Function used to check user is admin or not
  462.      *
  463.      * @param $check_only bool if true, after checking user will be redirected to login page if needed
  464.      *
  465.      * @return bool
  466.      * @throws Exception
  467.      */
  468.     function admin_login_check($check_only = false): bool
  469.     {
  470.         if (!has_access('admin_access', true)) {
  471.             if (!$check_only) {
  472.                 redirect_to('login.php');
  473.             }
  474.             return false;
  475.         }
  476.         return true;
  477.     }
  478.  
  479.     //This Function Is Used to Logout
  480.  
  481.     /**
  482.      * @throws Exception
  483.      */
  484.     function logout($page = 'login.php')
  485.     {
  486.         global $sess;
  487.  
  488.         //Calling Logout Functions
  489.         $funcs = $this->logout_functions;
  490.         if (is_array($funcs) && count($funcs) > 0) {
  491.             foreach ($funcs as $func) {
  492.                 if (function_exists($func)) {
  493.                     $func();
  494.                 }
  495.             }
  496.         }
  497.  
  498.         $sess->un_set('sess_salt');
  499.         $sess->destroy();
  500.     }
  501.  
  502.     /**
  503.      * Function used to delete user
  504.      *
  505.      * @param $uid
  506.      *
  507.      * @throws Exception
  508.      */
  509.     function delete_user($uid)
  510.     {
  511.         global $db;
  512.  
  513.         if ($this->user_exists($uid)) {
  514.             $udetails = $this->get_user_details($uid);
  515.  
  516.             if (user_id() != $uid && has_access('admin_access', true) && $uid != 1) {
  517.                 //list of functions to perform while deleting a video
  518.                 $del_user_funcs = $this->delete_user_functions;
  519.                 if (is_array($del_user_funcs)) {
  520.                     foreach ($del_user_funcs as $func) {
  521.                         if (function_exists($func)) {
  522.                             $func($udetails);
  523.                         }
  524.                     }
  525.                 }
  526.  
  527.                 //Removing Subscriptions and subscribers
  528.                 $this->remove_user_subscriptions($uid);
  529.                 $this->remove_user_subscribers($uid);
  530.  
  531.                 //Changing User Videos To Anonymous
  532.                 $db->execute('UPDATE ' . tbl('video') . ' SET userid=\'' . $this->get_anonymous_user() . '\' WHERE userid=\'' . $uid . '\'');
  533.                 //Changing User Group To Anonymous
  534.                 $db->execute('UPDATE ' . tbl('groups') . ' SET userid=\'' . $this->get_anonymous_user() . '\' WHERE userid=\'' . $uid . '\'');
  535.                 //Deleting User Contacts
  536.                 $this->remove_contacts($uid);
  537.  
  538.                 //Deleting User PMS
  539.                 $this->remove_user_pms($uid);
  540.                 //Changing From Messages to Anonymous
  541.                 $db->execute('UPDATE ' . tbl('messages') . ' SET message_from=\'' . $this->get_anonymous_user() . '\' WHERE message_from=\'' . $uid . '\'');
  542.                 //Finally Removing Database entry of user
  543.                 $db->execute('DELETE FROM ' . tbl('users') . ' WHERE userid=\'' . $uid . '\'');
  544.                 $db->execute('DELETE FROM ' . tbl('user_profile') . ' WHERE userid=\'' . $uid . '\'');
  545.  
  546.                 e(lang('usr_del_msg'), 'm');
  547.             } else {
  548.                 e(lang('you_cant_delete_this_user'));
  549.             }
  550.         } else {
  551.             e(lang('user_doesnt_exist'));
  552.         }
  553.     }
  554.  
  555.     /**
  556.      * Remove all user subscriptions
  557.      *
  558.      * @param $uid
  559.      * @throws Exception
  560.      */
  561.     function remove_user_subscriptions($uid)
  562.     {
  563.         global $db;
  564.         if (!$this->user_exists($uid)) {
  565.             e(lang('user_doesnt_exist'));
  566.         } elseif (!has_access('admin_access')) {
  567.             e(lang('you_dont_hv_perms'));
  568.         } else {
  569.             $db->execute('DELETE FROM ' . tbl($this->dbtbl['subtbl']) . ' WHERE userid=\'' . $uid . '\'');
  570.             e(lang('user_subs_hv_been_removed'), 'm');
  571.         }
  572.     }
  573.  
  574.     /**
  575.      * Remove all user subscribers
  576.      *
  577.      * @param $uid
  578.      * @throws Exception
  579.      */
  580.     function remove_user_subscribers($uid)
  581.     {
  582.         global $db;
  583.         if (!$this->user_exists($uid)) {
  584.             e(lang('user_doesnt_exist'));
  585.         } elseif (!has_access('admin_access')) {
  586.             e(lang('you_dont_hv_perms'));
  587.         } else {
  588.             $db->execute('DELETE FROM ' . tbl($this->dbtbl['subtbl']) . ' WHERE subscribed_to=\'' . $uid . '\'');
  589.             e(lang('user_subsers_hv_removed'), 'm');
  590.         }
  591.     }
  592.  
  593.     /**
  594.      * @throws Exception
  595.      */
  596.     function user_exists($id, $global = false): bool
  597.     {
  598.         global $db;
  599.  
  600.         if (is_numeric($id)) {
  601.             $field = 'userid';
  602.         } else {
  603.             $field = 'username';
  604.         }
  605.         $result = $db->count(tbl($this->dbtbl['users']), 'userid', $field.'=\'' . $id . '\'', '',60);
  606.  
  607.         if ($result > 0) {
  608.             return true;
  609.         }
  610.         return false;
  611.     }
  612.  
  613.     /**
  614.      * Function used to get user details using userid
  615.      *
  616.      * @param null $id
  617.      * @param bool $checksess
  618.      * @param bool $email
  619.      *
  620.      * @return bool|STRING
  621.      * @throws Exception
  622.      */
  623.     function get_user_details($id = null, $checksess = false, $email = false)
  624.     {
  625.         global $sess;
  626.  
  627.         $is_email = strpos($id, '@') !== false;
  628.         $select_field = (!$is_email && !is_numeric($id)) ? 'username' : (!is_numeric($id) ? 'email' : 'userid');
  629.         if (!$email) {
  630.             $fields = table_fields(['users' => ['*']]);
  631.         } else {
  632.             $fields = table_fields(['users' => ['email']]);
  633.         }
  634.  
  635.         $query = "SELECT $fields FROM " . cb_sql_table('users');
  636.         $query .= " WHERE users.$select_field = '$id'";
  637.  
  638.         $result = select($query, 60);
  639.  
  640.         if ($result) {
  641.             $details = $result[0];
  642.  
  643.             if (!$checksess) {
  644.                 return apply_filters($details, 'get_user');
  645.             }
  646.  
  647.             $session = $this->sessions['smart_sess'];
  648.             $smart_sess = md5($details['user_session_key'] . $sess->get('sess_salt'));
  649.  
  650.             if ($smart_sess == $session['session_value']) {
  651.                 $this->is_login = true;
  652.                 return apply_filters($details, 'get_user');
  653.             }
  654.         }
  655.  
  656.         return false;
  657.     }
  658.  
  659.     /**
  660.      * @throws Exception
  661.      */
  662.     function activate_user_with_avcode($user, $avcode)
  663.     {
  664.         global $eh;
  665.         $data = $this->get_user_details($user);
  666.         if (!$data || !$user) {
  667.             e(lang("usr_exist_err"));
  668.         } elseif ($data['usr_status'] == 'Ok') {
  669.             e(lang('usr_activation_err'));
  670.         } elseif ($data['ban_status'] == 'yes') {
  671.             e(lang('ban_status'));
  672.         } elseif ($data['avcode'] != $avcode) {
  673.             e(lang('avcode_incorrect'));
  674.         } else {
  675.             $this->action('activate', $data['userid']);
  676.             $eh->flush();
  677.             e(lang("usr_activation_msg"), "m");
  678.  
  679.             if ($data['welcome_email_sent'] == 'no') {
  680.                 $this->send_welcome_email($data, true);
  681.             }
  682.         }
  683.     }
  684.  
  685.     /**
  686.      * Function used to send activation code
  687.      * to user
  688.      *
  689.      * @param : $usenrma,$email or $userid
  690.      *
  691.      * @throws Exception
  692.      */
  693.     function send_activation_code($email)
  694.     {
  695.         global $cbemail;
  696.         $udetails = $this->get_user_details($email);
  697.  
  698.         if (!$udetails || !$email) {
  699.             e(lang("usr_exist_err"));
  700.         } elseif ($udetails['usr_status'] == 'Ok') {
  701.             e(lang('usr_activation_err'));
  702.         } elseif ($udetails['ban_status'] == 'yes') {
  703.             e(lang('ban_status'));
  704.         } else {
  705.             $tpl = $cbemail->get_template('avcode_request_template');
  706.             $var = [
  707.                 '{username}' => $udetails['username'],
  708.                 '{email}'    => $udetails['email'],
  709.                 '{avcode}'   => $udetails['avcode']
  710.             ];
  711.  
  712.             $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  713.             $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  714.  
  715.             //Now Finally Sending Email
  716.             cbmail(['to' => $udetails['email'], 'from' => SUPPORT_EMAIL, 'subject' => $subj, 'content' => $msg]);
  717.             e(lang('usr_activation_em_msg'), 'm');
  718.         }
  719.     }
  720.  
  721.     /**
  722.      * Function used to send welcome email
  723.      *
  724.      * @param      $user
  725.      * @param bool $update_email_status
  726.      *
  727.      * @throws Exception
  728.      */
  729.     function send_welcome_email($user, $update_email_status = false)
  730.     {
  731.         global $db, $cbemail;
  732.  
  733.         if (!is_array($user)) {
  734.             $udetails = $this->get_user_details($user);
  735.         } else {
  736.             $udetails = $user;
  737.         }
  738.  
  739.         if (!$udetails) {
  740.             e(lang('usr_exist_err'));
  741.         } else {
  742.             $tpl = $cbemail->get_template('welcome_message_template');
  743.             $var = [
  744.                 '{username}' => $udetails['username'],
  745.                 '{email}'    => $udetails['email']
  746.             ];
  747.             $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  748.             $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  749.  
  750.             //Now Finally Sending Email
  751.             cbmail(['to' => $udetails['email'], 'from' => WELCOME_EMAIL, 'subject' => $subj, 'content' => $msg]);
  752.  
  753.             if ($update_email_status) {
  754.                 $db->update(tbl($this->dbtbl['users']), ['welcome_email_sent'], ['yes'], ' userid=\'' . $udetails['userid'] . '\' ');
  755.             }
  756.         }
  757.     }
  758.  
  759.     /**
  760.      * @throws Exception
  761.      */
  762.     function change_password($array)
  763.     {
  764.         global $db;
  765.  
  766.         $old_pass = $array['old_pass'];
  767.         $new_pass = $array['new_pass'];
  768.         $c_new_pass = $array['c_new_pass'];
  769.  
  770.         $uid = $array['userid'];
  771.  
  772.         if (!$this->get_user_with_pass($uid, pass_code($old_pass, $uid))) {
  773.             e(lang('usr_pass_err'));
  774.         } elseif (empty($new_pass)) {
  775.             e(lang('usr_pass_err2'));
  776.         } elseif ($new_pass != $c_new_pass) {
  777.             e(lang('usr_cpass_err1'));
  778.         } else {
  779.             $db->update(tbl($this->dbtbl['users']), ['password'], [pass_code($array['new_pass'], $uid)], ' userid=\'' . $uid . '\'');
  780.             e(lang('usr_pass_email_msg'), 'm');
  781.         }
  782.  
  783.         return $msg;
  784.     }
  785.  
  786.     /**
  787.      * Function used to add contact
  788.      *
  789.      * @param $uid
  790.      * @param $fid
  791.      *
  792.      * @throws Exception
  793.      */
  794.     function add_contact($uid, $fid)
  795.     {
  796.         global $cbemail, $db;
  797.  
  798.         $friend = $this->get_user_details($fid);
  799.         $sender = $this->get_user_details($uid);
  800.  
  801.         if (!$friend) {
  802.             e(lang('usr_exist_err'));
  803.         } elseif ($this->is_requested_friend($uid, $fid)) {
  804.             e(lang('you_already_sent_frend_request'));
  805.         } elseif ($this->is_requested_friend($uid, $fid, 'in')) {
  806.             $this->confirm_friend($fid, $uid);
  807.             e(lang('friend_added'));
  808.         } elseif ($uid == $fid) {
  809.             e(lang('friend_add_himself_error'));
  810.         } else {
  811.             $db->insert(tbl($this->dbtbl['contacts']), ['userid', 'contact_userid', 'date_added', 'request_type'],
  812.                 [$uid, $fid, now(), 'out']);
  813.             $insert_id = $db->insert_id();
  814.  
  815.             e(lang('friend_request_sent'), 'm');
  816.  
  817.             //Sending friendship request email
  818.             $tpl = $cbemail->get_template('friend_request_email');
  819.  
  820.             $var = [
  821.                 '{reciever}'     => $friend['username'],
  822.                 '{sender}'       => $sender['username'],
  823.                 '{sender_link}'  => $this->profile_link($sender),
  824.                 '{request_link}' => '/manage_contacts.php?mode=request&confirm=' . $uid
  825.             ];
  826.  
  827.             $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  828.             $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  829.  
  830.             //Now Finally Sending Email
  831.             #cbmail(['to'=>$friend['email'],'from'=>WEBSITE_EMAIL,'subject'=>$subj,'content'=>$msg]);
  832.        }
  833.  
  834.     }
  835.  
  836.     /**
  837.      * Function used to check weather users are confirmed friends or not
  838.      *
  839.      * @param $uid
  840.      * @param $fid
  841.      *
  842.      * @return bool
  843.      * @throws Exception
  844.      */
  845.     function is_confirmed_friend($uid, $fid): bool
  846.     {
  847.         global $db;
  848.         $count = $db->count(tbl($this->dbtbl['contacts']), 'contact_id',
  849.             " (userid='$uid' AND contact_userid='$fid') OR (userid='$fid' AND contact_userid='$uid') AND confirmed='yes'");
  850.         if ($count[0] > 0) {
  851.             return true;
  852.         }
  853.         return false;
  854.     }
  855.  
  856.     /**
  857.      * function used to check weather users are friends or not
  858.      *
  859.      * @param $uid
  860.      * @param $fid
  861.      *
  862.      * @return bool
  863.      * @throws Exception
  864.      */
  865.     function is_friend($uid, $fid): bool
  866.     {
  867.         global $db;
  868.         $count = $db->count(tbl($this->dbtbl['contacts']), 'contact_id',
  869.             " (userid='$uid' AND contact_userid='$fid') OR (userid='$fid' AND contact_userid='$uid')");
  870.         if ($count[0] > 0) {
  871.             return true;
  872.         }
  873.         return false;
  874.     }
  875.  
  876.     /**
  877.      * Function used to check weather user has already requested friendship or not
  878.      *
  879.      * @param        $uid
  880.      * @param        $fid
  881.      * @param string $type
  882.      * @param null $confirm
  883.      *
  884.      * @return bool
  885.      * @throws Exception
  886.      */
  887.     function is_requested_friend($uid, $fid, $type = 'out', $confirm = null): bool
  888.     {
  889.         global $db;
  890.  
  891.         $query = '';
  892.         if ($confirm) {
  893.             $query = " AND confirmed='$confirm' ";
  894.         }
  895.  
  896.         if ($type == 'out') {
  897.             $count = $db->count(tbl($this->dbtbl['contacts']), 'contact_id', " userid='$uid' AND contact_userid='$fid' $query");
  898.         } else {
  899.             $count = $db->count(tbl($this->dbtbl['contacts']), 'contact_id', " userid='$fid' AND contact_userid='$uid' $query");
  900.         }
  901.  
  902.         if ($count[0] > 0) {
  903.             return true;
  904.         }
  905.         return false;
  906.     }
  907.  
  908.     /**
  909.      * Function used to confirm friend
  910.      *
  911.      * @param      $uid
  912.      * @param      $rid
  913.      * @param bool $msg
  914.      *
  915.      * @throws Exception
  916.      */
  917.     function confirm_friend($uid, $rid, $msg = true)
  918.     {
  919.         global $cbemail, $db;
  920.         if (!$this->is_requested_friend($rid, $uid, 'out', 'no')) {
  921.             if ($msg) {
  922.                 e(lang('friend_confirm_error'));
  923.             }
  924.         } else {
  925.             addFeed(['action' => 'add_friend', 'object_id' => $rid, 'object' => 'friend', 'uid' => $uid]);
  926.             addFeed(['action' => 'add_friend', 'object_id' => $uid, 'object' => 'friend', 'uid' => $rid]);
  927.  
  928.             $db->insert(tbl($this->dbtbl['contacts']), ['userid', 'contact_userid', 'date_added', 'request_type', 'confirmed'],
  929.                 [$uid, $rid, now(), 'in', 'yes']);
  930.             $db->update(tbl($this->dbtbl['contacts']), ['confirmed'], ['yes'], ' userid=\'' . $rid . '\' AND contact_userid=\'' . $uid . '\' ');
  931.             if ($msg) {
  932.                 e(lang('friend_confirmed'), 'm');
  933.             }
  934.             //Sending friendship confirmation email
  935.             $tpl = $cbemail->get_template('friend_confirmation_email');
  936.  
  937.             $friend = $this->get_user_details($rid);
  938.             $sender = $this->get_user_details($uid);
  939.  
  940.             $more_var = [
  941.                 '{reciever}'    => $friend['username'],
  942.                 '{sender}'      => $sender['username'],
  943.                 '{sender_link}' => $this->profile_link($sender)
  944.             ];
  945.             if (!isset($var)) {
  946.                 $var = [];
  947.             }
  948.             $var = array_merge($more_var, $var);
  949.             $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  950.             $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  951.  
  952.             //Now Finally Sending Email
  953.             cbmail(['to' => $friend['email'], 'from' => WEBSITE_EMAIL, 'subject' => $subj, 'content' => $msg]);
  954.  
  955.             //Logging Friendship
  956.  
  957.             $log_array = [
  958.                 'success'       => 'yes',
  959.                 'action_obj_id' => $friend['userid'],
  960.                 'details'       => 'friend with ' . $friend['username']
  961.             ];
  962.  
  963.             insert_log('add_friend', $log_array);
  964.  
  965.             $log_array = [
  966.                 'success'       => 'yes',
  967.                 'username'      => $friend['username'],
  968.                 'userid'        => $friend['userid'],
  969.                 'userlevel'     => $friend['level'],
  970.                 'useremail'     => $friend['email'],
  971.                 'action_obj_id' => $insert_id,
  972.                 'details'       => 'friend with ' . user_id()
  973.             ];
  974.  
  975.             //Login Upload
  976.             insert_log('add_friend', $log_array);
  977.         }
  978.     }
  979.  
  980.     /**
  981.      * Function used to confirm request
  982.      *
  983.      * @param      $rid
  984.      * @param null $uid
  985.      *
  986.      * @throws Exception
  987.      */
  988.     function confirm_request($rid, $uid = null)
  989.     {
  990.         global $db;
  991.  
  992.         if (!$uid) {
  993.             $uid = user_id();
  994.         }
  995.  
  996.         $result = $db->select(tbl($this->dbtbl['contacts']), '*', " userid='$rid' AND contact_userid='$uid' ");
  997.  
  998.         if (count($result) == 0) {
  999.             e(lang("friend_request_not_found"));
  1000.         } elseif ($uid != $result[0]['contact_userid']) {
  1001.             e(lang("you_cant_confirm_this_request"));
  1002.         } elseif ($result[0]['confirmed'] == 'yes') {
  1003.             e(lang("friend_request_already_confirmed"));
  1004.         } else {
  1005.             $this->confirm_friend($uid, $result[0]['userid']);
  1006.         }
  1007.     }
  1008.  
  1009.     /**
  1010.      * Function used to get user contacts
  1011.      *
  1012.      * @param      $uid
  1013.      * @param int $group
  1014.      * @param null $confirmed
  1015.      * @param bool $count_only
  1016.      * @param null $type
  1017.      *
  1018.      * @return array|bool
  1019.      * @throws Exception
  1020.      */
  1021.     function get_contacts($uid, $group = 0, $confirmed = null, $count_only = false, $type = null)
  1022.     {
  1023.         global $db;
  1024.  
  1025.         $query = '';
  1026.         if ($confirmed) {
  1027.             $query .= ' AND ' . tbl('contacts') . ".confirmed='$confirmed' ";
  1028.         }
  1029.  
  1030.         if ($type) {
  1031.             $query .= ' AND ' . tbl('contacts') . ".request_type='$type' ";
  1032.         }
  1033.  
  1034.         if (!$count_only) {
  1035.             $result = $db->select(tbl('contacts,users'),
  1036.                 tbl('contacts.contact_userid,contacts.confirmed,contacts.request_type ,users.*'),
  1037.                 tbl('contacts.userid') . "='$uid' AND " . tbl('users.userid') . '=' . tbl('contacts.contact_userid') . $query . '
  1038.             AND ' . tbl('contacts') . ".contact_group_id='$group' ");
  1039.  
  1040.             if (count($result) > 0) {
  1041.                 return $result;
  1042.             }
  1043.             return false;
  1044.         }
  1045.  
  1046.         return $db->count(tbl('contacts'),
  1047.             tbl('contacts.contact_userid'),
  1048.             tbl('contacts.userid') . "='$uid'
  1049.        $query AND " . tbl('contacts') . ".contact_group_id='$group' ");
  1050.     }
  1051.  
  1052.     /**
  1053.      * Function used to get pending contacts
  1054.      *
  1055.      * @param      $uid
  1056.      * @param int $group
  1057.      * @param bool $count_only
  1058.      *
  1059.      * @return array|bool
  1060.      * @throws Exception
  1061.      */
  1062.     function get_pending_contacts($uid, $group = 0, $count_only = false)
  1063.     {
  1064.         global $db;
  1065.  
  1066.         if (!$count_only) {
  1067.             $result = $db->select(tbl('contacts,users'),
  1068.                 tbl('contacts.userid,contacts.confirmed,contacts.request_type ,users.*'),
  1069.                 tbl('contacts.contact_userid') . "='$uid' AND " . tbl('users.userid') . '=' . tbl('contacts.userid') . "
  1070.            AND " . tbl('contacts.confirmed') . "='no' AND " . tbl('contacts') . ".contact_group_id='$group' ");
  1071.             if (count($result) > 0) {
  1072.                 return $result;
  1073.             }
  1074.             return false;
  1075.         }
  1076.  
  1077.         $count = $db->count(tbl('contacts'),
  1078.             tbl('contacts.contact_userid'),
  1079.             tbl('contacts.contact_userid') . "='$uid' AND " . tbl('contacts.confirmed') . "='no' AND " . tbl('contacts') . ".contact_group_id='$group' ");
  1080.         return $count;
  1081.     }
  1082.  
  1083.     /**
  1084.      * Function used to remove user from contact list
  1085.      * @param $fid {id of friend that user wants to remove}
  1086.      * @param $uid {id of user who is removing other from friendlist}
  1087.      * @throws Exception
  1088.      */
  1089.     function remove_contact($fid, $uid = null)
  1090.     {
  1091.         global $db;
  1092.         if (!$uid) {
  1093.             $uid = user_id();
  1094.         }
  1095.  
  1096.         if (!$this->is_friend($fid, $uid)) {
  1097.             e(lang('user_no_in_contact_list'));
  1098.         } else {
  1099.             $db->execute('DELETE FROM ' . tbl($this->dbtbl['contacts']) . " WHERE
  1100.                        (userid='$uid' AND contact_userid='$fid') OR (userid='$fid' AND contact_userid='$uid')");
  1101.             e(lang('user_removed_from_contact_list'), 'm');
  1102.         }
  1103.     }
  1104.  
  1105.     /**
  1106.      * Function used to increas user total_watched field
  1107.      *
  1108.      * @param $userid
  1109.      * @throws Exception
  1110.      */
  1111.     function increment_watched_vides($userid)
  1112.     {
  1113.         global $db;
  1114.         $db->update(tbl($this->dbtbl['users']), ['total_watched'], ['|f|total_watched+1'], ' userid=\'' . $userid . '\'');
  1115.     }
  1116.  
  1117.     /**
  1118.      * Function used to subscribe user
  1119.      *
  1120.      * @param      $to
  1121.      * @param null $user
  1122.      * @throws Exception
  1123.      */
  1124.     function subscribe_user($to, $user = null)
  1125.     {
  1126.         if (!$user) {
  1127.             $user = user_id();
  1128.         }
  1129.         global $db;
  1130.  
  1131.         $to_user = $this->get_user_details($to);
  1132.  
  1133.         if (!$this->user_exists($to)) {
  1134.             e(lang('usr_exist_err'));
  1135.         } elseif (!$user) {
  1136.             e(sprintf(lang('please_login_subscribe'), $to_user['username']));
  1137.         } elseif ($this->is_subscribed($to, $user)) {
  1138.             e(sprintf(lang('usr_sub_err'), '<strong>' . $to_user['username'] . '</strong>'));
  1139.         } elseif ($to_user['userid'] == $user) {
  1140.             e(lang('you_cant_sub_yourself'));
  1141.         } else {
  1142.             $db->insert(tbl($this->dbtbl['subtbl']), ['userid', 'subscribed_to', 'date_added'],
  1143.                 [$user, $to, NOW()]);
  1144.             $db->update(tbl($this->dbtbl['users']), ['subscribers'],
  1145.                 [$this->get_user_subscribers($to, true)], " userid='$to' ");
  1146.             $db->update(tbl($this->dbtbl['users']), ['total_subscriptions'],
  1147.                 [$this->get_user_subscriptions($user, 'count')], " userid='$user' ");
  1148.             //Logging Comment
  1149.             $log_array = [
  1150.                 'success'        => 'yes',
  1151.                 'details'        => 'subsribed to ' . $to_user['username'],
  1152.                 'action_obj_id'  => $to_user['userid'],
  1153.                 'action_done_id' => $db->insert_id()
  1154.             ];
  1155.             insert_log('subscribe', $log_array);
  1156.  
  1157.             e(sprintf(lang('usr_sub_msg'), $to_user['username']), 'm');
  1158.         }
  1159.     }
  1160.  
  1161.     /**
  1162.      * Function used to check weather user is already subscribed or not
  1163.      *
  1164.      * @param      $to
  1165.      * @param null $user
  1166.      *
  1167.      * @return array|bool
  1168.      * @throws Exception
  1169.      */
  1170.     function is_subscribed($to, $user = null)
  1171.     {
  1172.         if (!$user) {
  1173.             $user = user_id();
  1174.         }
  1175.         global $db;
  1176.  
  1177.         if (!$user) {
  1178.             return false;
  1179.         }
  1180.  
  1181.         $result = $db->select(tbl($this->dbtbl['subtbl']), '*', " subscribed_to='$to' AND userid='$user'");
  1182.         if (count($result) > 0) {
  1183.             return $result;
  1184.         }
  1185.         return false;
  1186.     }
  1187.  
  1188.     /**
  1189.      * Function used to remove user subscription
  1190.      *
  1191.      * @param      $subid
  1192.      * @param null $uid
  1193.      *
  1194.      * @return bool
  1195.      * @throws Exception
  1196.      */
  1197.     function remove_subscription($subid, $uid = null): bool
  1198.     {
  1199.         global $db;
  1200.         if (!$uid) {
  1201.             $uid = user_id();
  1202.         }
  1203.  
  1204.         if ($this->is_subscribed($subid, $uid)) {
  1205.             $db->execute('DELETE FROM ' . tbl($this->dbtbl['subtbl']) . " WHERE userid='$uid' AND subscribed_to='$subid'");
  1206.             e(lang('class_unsub_msg'), 'm');
  1207.  
  1208.             $db->update(tbl($this->dbtbl['users']), ['subscribers'],
  1209.                 [$this->get_user_subscribers($subid, true)], " userid='$subid' ");
  1210.             $db->update(tbl($this->dbtbl['users']), ['total_subscriptions'],
  1211.                 [$this->get_user_subscriptions($uid, 'count')], " userid='$uid' ");
  1212.             return true;
  1213.         }
  1214.  
  1215.         e(lang('you_not_subscribed'));
  1216.         return false;
  1217.     }
  1218.  
  1219.     /**
  1220.      * @throws Exception
  1221.      */
  1222.     function unsubscribe_user($subid, $uid = null)
  1223.     {
  1224.         return $this->remove_subscription($subid, $uid);
  1225.     }
  1226.  
  1227.     /**
  1228.      * Function used to get user subscribers
  1229.      *
  1230.      * @param $id
  1231.      * @param bool $count
  1232.      *
  1233.      * @return array|bool
  1234.      * @throws Exception
  1235.      */
  1236.     function get_user_subscribers($id, $count = false)
  1237.     {
  1238.         global $db;
  1239.         if (!$count) {
  1240.             $result = $db->select(tbl('subscriptions'), '*',
  1241.                 " subscribed_to='$id' ");
  1242.             if (count($result) > 0) {
  1243.                 return $result;
  1244.             }
  1245.             return false;
  1246.         }
  1247.         return $db->count(tbl($this->dbtbl['subtbl']), 'subscription_id', " subscribed_to='$id' ");
  1248.     }
  1249.  
  1250.     /**
  1251.      * function used to get user subscribers with details
  1252.      *
  1253.      * @param      $id
  1254.      * @param null $limit
  1255.      *
  1256.      * @return array|bool
  1257.      * @throws Exception
  1258.      */
  1259.     function get_user_subscribers_detail($id, $limit = null)
  1260.     {
  1261.         global $db;
  1262.         $result = $db->select(tbl('users,' . $this->dbtbl['subtbl']), '*', ' ' . tbl('subscriptions.subscribed_to') . " = '$id' AND " . tbl('subscriptions.userid') . '=' . tbl('users.userid'), $limit);
  1263.         if (count($result) > 0) {
  1264.             return $result;
  1265.         }
  1266.         return false;
  1267.     }
  1268.  
  1269.     /**
  1270.      * Function used to get user subscriptions
  1271.      *
  1272.      * @param      $id
  1273.      * @param null $limit
  1274.      *
  1275.      * @return array|bool
  1276.      * @throws Exception
  1277.      */
  1278.     function get_user_subscriptions($id, $limit = null)
  1279.     {
  1280.         global $db;
  1281.         if ($limit != 'count') {
  1282.             $result = $db->select(tbl('users,' . $this->dbtbl['subtbl']), '*', ' ' . tbl('subscriptions.userid') . " = '$id' AND " . tbl('subscriptions.subscribed_to') . '=' . tbl('users.userid'), $limit);
  1283.  
  1284.             if (count($result) > 0) {
  1285.                 return $result;
  1286.             }
  1287.             return false;
  1288.         }
  1289.  
  1290.         return $db->count(tbl($this->dbtbl['subtbl']), 'subscription_id', " userid = '$id'");
  1291.     }
  1292.  
  1293.     /**
  1294.      * Function used to reset user password
  1295.      * it has two steps
  1296.      * 1 to send confirmation
  1297.      * 2 to reset the password
  1298.      *
  1299.      * @param      $step
  1300.      * @param      $input
  1301.      * @param null $code
  1302.      *
  1303.      * @return bool
  1304.      * @throws \PHPMailer\PHPMailer\Exception
  1305.      */
  1306.  
  1307.     function reset_password($step, $input, $code = null)
  1308.     {
  1309.         global $cbemail, $db;
  1310.         switch ($step) {
  1311.             case 1:
  1312.                 $udetails = $this->get_user_details($input);
  1313.                 if (!$udetails) {
  1314.                     e(lang('usr_exist_err'));
  1315.                 } elseif (!verify_captcha()) {
  1316.                     e(lang('recap_verify_failed'));
  1317.                 } else {
  1318.                     //Sending confirmation email
  1319.                     $tpl = $cbemail->get_template('password_reset_request');
  1320.                     $avcode = $udetails['avcode'];
  1321.                     if (!$udetails['avcode']) {
  1322.                         $avcode = RandomString(10);
  1323.                         $db->update(tbl($this->dbtbl['users']), ['avcode'], [$avcode], " userid='" . $udetails['userid'] . "'");
  1324.                     }
  1325.  
  1326.                     $more_var = [
  1327.                         '{username}' => $udetails['username'],
  1328.                         '{email}'    => $udetails['email'],
  1329.                         '{avcode}'   => $avcode,
  1330.                         '{userid}'   => $udetails['userid']
  1331.                     ];
  1332.                     if (!is_array($var)) {
  1333.                         $var = [];
  1334.                     }
  1335.                     $var = array_merge($more_var, $var);
  1336.                     $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  1337.                     $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  1338.  
  1339.                     //Now Finally Sending Email
  1340.                     cbmail(['to' => $udetails['email'], 'from' => WEBSITE_EMAIL, 'subject' => $subj, 'content' => $msg]);
  1341.  
  1342.                     e(lang('usr_rpass_email_msg'), 'm');
  1343.                     return true;
  1344.                 }
  1345.                 break;
  1346.  
  1347.             case 2:
  1348.                 $udetails = $this->get_user_details($input);
  1349.                 if (!$udetails) {
  1350.                     e(lang('usr_exist_err'));
  1351.                 } elseif ($udetails['avcode'] != $code) {
  1352.                     e(lang('recap_verify_failed'));
  1353.                 } else {
  1354.                     $newpass = RandomString(6);
  1355.                     $pass = pass_code($newpass, $udetails['userid']);
  1356.                     $avcode = RandomString(10);
  1357.                     $db->update(tbl($this->dbtbl['users']), ['password', 'avcode'], [$pass, $avcode], " userid='" . $udetails['userid'] . "'");
  1358.                     //sending new password email...
  1359.                     //Sending confirmation email
  1360.                     $tpl = $cbemail->get_template('password_reset_details');
  1361.                     $more_var = [
  1362.                         '{username}' => $udetails['username'],
  1363.                         '{email}'    => $udetails['email'],
  1364.                         '{avcode}'   => $udetails['avcode'],
  1365.                         '{userid}'   => $udetails['userid'],
  1366.                         '{password}' => $newpass
  1367.                     ];
  1368.                     if (!is_array($var)) {
  1369.                         $var = [];
  1370.                     }
  1371.                     $var = array_merge($more_var, $var);
  1372.                     $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  1373.                     $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  1374.  
  1375.                     //Now Finally Sending Email
  1376.                     cbmail(['to' => $udetails['email'], 'from' => WEBSITE_EMAIL, 'subject' => $subj, 'content' => $msg]);
  1377.                     e(lang('usr_pass_email_msg'), 'm');
  1378.                     return true;
  1379.                 }
  1380.                 break;
  1381.         }
  1382.     }
  1383.  
  1384.     /**
  1385.      * Function used to recover username
  1386.      */
  1387.     function recover_username($email): string
  1388.     {
  1389.         global $cbemail;
  1390.         $udetails = $this->get_user_details($email);
  1391.         if (!$udetails) {
  1392.             e(lang('no_user_associated_with_email'));
  1393.         } elseif (!verify_captcha()) {
  1394.             e(lang('recap_verify_failed'));
  1395.         } else {
  1396.             $tpl = $cbemail->get_template('forgot_username_request');
  1397.             $more_var = [
  1398.                 '{username}' => $udetails['username']
  1399.             ];
  1400.             if (!is_array($var)) {
  1401.                 $var = [];
  1402.             }
  1403.             $var = array_merge($more_var, $var);
  1404.             $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  1405.             $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  1406.  
  1407.             //Now Finally Sending Email
  1408.             cbmail(['to' => $udetails['email'], 'from' => SUPPORT_EMAIL, 'subject' => $subj, 'content' => $msg]);
  1409.             e(lang("usr_uname_email_msg"), 'm');
  1410.         }
  1411.         return $msg;
  1412.     }
  1413.  
  1414.     //FUNCTION USED TO UPDATE LAST ACTIVE FOR OF USER
  1415.     // @ Param : username
  1416.     /**
  1417.      * @throws Exception
  1418.      */
  1419.     function UpdateLastActive($username)
  1420.     {
  1421.         global $db;
  1422.  
  1423.         $sql = 'UPDATE ' . tbl("users") . " SET last_active = '" . NOW() . "' WHERE username='" . $username . "' OR userid='" . $username . "' ";
  1424.         $db->execute($sql);
  1425.     }
  1426.  
  1427.     /**
  1428.      * FUNCTION USED TO GE USER THUMBNAIL
  1429.      *
  1430.      * @param array $udetails
  1431.      * @param string $size
  1432.      * @param null $uid
  1433.      *
  1434.      * @return string
  1435.      * @throws Exception
  1436.      */
  1437.     function getUserThumb($udetails, $size = '', $uid = null): string
  1438.     {
  1439.         if (empty($udetails['userid']) && $uid) {
  1440.             $udetails = $this->get_user_details($uid);
  1441.         }
  1442.  
  1443.         $avatar = $avatar_path = '';
  1444.         if (!empty($udetails)) {
  1445.             $avatar = $udetails['avatar'];
  1446.             $avatar_path = AVATARS_DIR . DIRECTORY_SEPARATOR . $avatar;
  1447.         }
  1448.         if (!empty($avatar) && file_exists($avatar_path)) {
  1449.             return AVATARS_URL . DIRECTORY_SEPARATOR . $avatar;
  1450.         }
  1451.  
  1452.         if (!empty($udetails['avatar_url'])) {
  1453.             return display_clean($udetails['avatar_url']);
  1454.         }
  1455.  
  1456.         $thesize = AVATAR_SIZE;
  1457.         $default = $this->get_default_thumb();
  1458.         if ($size == 'small') {
  1459.             $thesize = AVATAR_SMALL_SIZE;
  1460.             $default = $this->get_default_thumb('small');
  1461.         }
  1462.  
  1463.         if (config('gravatars') == 'yes' && (!empty($udetails['email']) || !empty($udetails['anonym_email']))) {
  1464.             $email = $udetails['email'] ? $udetails['email'] : $udetails['anonym_email'];
  1465.             $gravatar = new Gravatar($email, BASEURL . $default);
  1466.             $gravatar->size = $thesize;
  1467.             $gravatar->rating = 'G';
  1468.             $gravatar->border = 'FF0000';
  1469.  
  1470.             return $gravatar->getSrc();
  1471.         }
  1472.  
  1473.         return $default;
  1474.     }
  1475.  
  1476.     function avatar($udetails, $size = '', $uid = null): string
  1477.     {
  1478.         return $this->getUserThumb($udetails, $size, $uid);
  1479.     }
  1480.  
  1481.     /**
  1482.      * Function used to get default user thumb
  1483.      *
  1484.      * @param null $size
  1485.      *
  1486.      * @return string
  1487.      */
  1488.     function get_default_thumb($size = null): string
  1489.     {
  1490.         if ($size == 'small' && file_exists(TEMPLATEDIR . '/images/avatars/no_avatar-small.png')) {
  1491.             return '/images/avatars/no_avatar-small.png';
  1492.         }
  1493.  
  1494.         if (file_exists(TEMPLATEDIR . '/images/avatars/no_avatar.png') && !$size) {
  1495.             return '/images/avatars/no_avatar.png';
  1496.         }
  1497.  
  1498.         if ($size == 'small') {
  1499.             return '/images/avatars/no_avatar-small.png';
  1500.         }
  1501.         return '/images/avatars/no_avatar.png';
  1502.     }
  1503.  
  1504.     /**
  1505.      * Function used to get user Background
  1506.      *
  1507.      * @param : bg file
  1508.      * @param bool $check
  1509.      *
  1510.      * @return bool|string
  1511.      */
  1512.     function getUserBg($udetails, $check = false)
  1513.     {
  1514.         $file = $udetails['background'];
  1515.         $bgfile = USER_BG_DIR . DIRECTORY_SEPARATOR . $file;
  1516.  
  1517.         if (file_exists($bgfile) && $file) {
  1518.             return USER_BG_URL . '/' . $file;
  1519.         }
  1520.  
  1521.         if (!empty($udetails['background_url']) && BACKGROUND_URL == 'yes') {
  1522.             return $udetails['background_url'];
  1523.         }
  1524.  
  1525.         if (!empty($udetails['background_color']) && BACKGROUND_COLOR == 'yes' && $check) {
  1526.             return true;
  1527.         }
  1528.  
  1529.         return false;
  1530.     }
  1531.  
  1532.     /**
  1533.      * Function used to get user field
  1534.      * @ param INT userid
  1535.      * @ param FIELD name
  1536.      *
  1537.      * @param $uid
  1538.      * @param $field
  1539.      *
  1540.      * @return bool|array
  1541.      * @throws Exception
  1542.      */
  1543.     function get_user_field($uid, $field)
  1544.     {
  1545.         global $db;
  1546.  
  1547.         if (is_numeric($uid)) {
  1548.             $results = $db->select(tbl('users'), $field, "userid='$uid'");
  1549.         } else {
  1550.             $results = $db->select(tbl('users'), $field, "username='$uid'");
  1551.         }
  1552.  
  1553.         if (count($results) > 0) {
  1554.             return $results[0];
  1555.         }
  1556.         return false;
  1557.     }
  1558.  
  1559.     function get_user_fields($uid, $field)
  1560.     {
  1561.         return $this->get_user_field($uid, $field);
  1562.     }
  1563.  
  1564.     /**
  1565.      * This function will return
  1566.      * user field without array
  1567.      */
  1568.     function get_user_field_only($uid, $field)
  1569.     {
  1570.         $fields = $this->get_user_field($uid, $field);
  1571.         return $fields[$field];
  1572.     }
  1573.  
  1574.     /**
  1575.      * Function used to get user level and its details
  1576.      *
  1577.      * @param INT userid
  1578.      * @param bool $is_level
  1579.      *
  1580.      * @return bool|mixed
  1581.      * @throws Exception
  1582.      */
  1583.     function get_user_level($uid, $is_level = false)
  1584.     {
  1585.         global $db;
  1586.  
  1587.         if ($is_level) {
  1588.             $level = $uid;
  1589.         } else {
  1590.             $level = $this->udetails['level'] ?? false;
  1591.         }
  1592.  
  1593.         if ($level == user_id() or $level == $this->udetails['level']) {
  1594.             if (isset($this->permission)) {
  1595.                 return $this->permission;
  1596.             }
  1597.         }
  1598.  
  1599.         $result = $db->select(tbl('user_levels,user_levels_permissions'), '*',
  1600.             tbl('user_levels_permissions.user_level_id') . "='" . $level . "'
  1601.                              AND " . tbl('user_levels_permissions.user_level_id') . ' = ' . tbl('user_levels.user_level_id'), false, false, false, 600);
  1602.  
  1603.         //Now Merging the two arrays
  1604.         return $result[0] ?? false;
  1605.     }
  1606.  
  1607.     /**
  1608.      * Function used to get all levels
  1609.      *
  1610.      * @param : filter
  1611.      *
  1612.      * @return array|bool
  1613.      * @throws Exception
  1614.      */
  1615.     function get_levels($filter = null)
  1616.     {
  1617.         global $db;
  1618.         $results = $db->select(tbl('user_levels'), '*', null, null, ' user_level_id ASC');
  1619.  
  1620.         if (count($results) > 0) {
  1621.             return $results;
  1622.         }
  1623.         return false;
  1624.     }
  1625.  
  1626.     /**
  1627.      * Function used to get level details
  1628.      *
  1629.      * @param : level_id INT
  1630.      *
  1631.      * @return bool|int
  1632.      * @throws Exception
  1633.      */
  1634.     function get_level_details($lid)
  1635.     {
  1636.         global $db;
  1637.         $results = $db->select(tbl('user_levels'), '*', " user_level_id='$lid' ");
  1638.         if (count($results) > 0) {
  1639.             return $results[0];
  1640.         }
  1641.  
  1642.         e(lang('cant_find_level'));
  1643.         return false;
  1644.     }
  1645.  
  1646.     /**
  1647.      * Function used to get users of particular level
  1648.      *
  1649.      * @param : level_id
  1650.      * @param bool $count
  1651.      * @param string $fields
  1652.      *
  1653.      * @return array|int
  1654.      * @throws Exception
  1655.      */
  1656.     function get_level_users($id, $count = false, $fields = 'level')
  1657.     {
  1658.         global $db;
  1659.         if ($fields == 'all') {
  1660.             $fields = '*';
  1661.         }
  1662.  
  1663.         $results = $db->select(tbl('users'), $fields, " level='$id'");
  1664.         if (count($results) > 0) {
  1665.             if ($count) {
  1666.                 return count($results);
  1667.             }
  1668.             return $results;
  1669.         }
  1670.  
  1671.         return 0;
  1672.     }
  1673.  
  1674.     /**
  1675.      * Function used to add user level
  1676.      * @throws Exception
  1677.      */
  1678.     function add_user_level($array)
  1679.     {
  1680.         global $db;
  1681.         if (!is_array($array)) {
  1682.             $array = $_POST;
  1683.         }
  1684.         $level_name = mysql_clean($array['level_name']);
  1685.         if (empty($level_name)) {
  1686.             e(lang('please_enter_level_name'));
  1687.         } else {
  1688.             $db->insert(tbl('user_levels'), ['user_level_name'], [$level_name]);
  1689.             $iid = $db->insert_id();
  1690.  
  1691.             $fields_array[] = 'user_level_id';
  1692.             $value_array[] = $iid;
  1693.             foreach ($this->get_access_type_list() as $access => $name) {
  1694.                 $fields_array[] = $access;
  1695.                 $value_array[] = $array[$access] ? $array[$access] : 'no';
  1696.             }
  1697.             $db->insert(tbl('user_levels_permissions'), $fields_array, $value_array);
  1698.             return true;
  1699.         }
  1700.     }
  1701.  
  1702.     /**
  1703.      * Function usewd to get level permissions
  1704.      *
  1705.      * @param $id
  1706.      *
  1707.      * @return bool|array
  1708.      * @throws Exception
  1709.      */
  1710.     function get_level_permissions($id)
  1711.     {
  1712.         global $db;
  1713.         $results = $db->select(tbl('user_levels_permissions'), '*', " user_level_id = '$id'");
  1714.         if (count($results) > 0) {
  1715.             return $results[0];
  1716.         }
  1717.         return false;
  1718.     }
  1719.  
  1720.     /**
  1721.      * Function used to get custom permissions
  1722.      */
  1723.     function get_access_type_list(): array
  1724.     {
  1725.         if (!$this->access_type_list) {
  1726.             $perms = $this->get_permissions();
  1727.             foreach ($perms as $perm) {
  1728.                 $this->add_access_type($perm['permission_code'], $perm['permission_name']);
  1729.             }
  1730.         }
  1731.         return $this->access_type_list;
  1732.     }
  1733.  
  1734.     /**
  1735.      * Function used to add new custom permission
  1736.      */
  1737.     function add_access_type($access, $name)
  1738.     {
  1739.         if (!empty($access) && !empty($name)) {
  1740.             $this->access_type_list[$access] = $name;
  1741.         }
  1742.     }
  1743.  
  1744.     /**
  1745.      * Function used to update user level
  1746.      * @param INT level_id
  1747.      * @param ARRAY perm_level
  1748.      * @throws Exception
  1749.      */
  1750.     function update_user_level($id, $array): bool
  1751.     {
  1752.         global $db;
  1753.         if (!is_array($array)) {
  1754.             $array = $_POST;
  1755.         }
  1756.  
  1757.         //First Checking Level
  1758.         $level = $this->get_level_details($id);
  1759.  
  1760.         if ($level) {
  1761.             foreach ($this->get_access_type_list() as $access => $name) {
  1762.                 $fields_array[] = $access;
  1763.                 $value_array[] = $array[$access];
  1764.             }
  1765.  
  1766.             //Checking level Name
  1767.             if (!empty($array['level_name'])) {
  1768.                 $level_name = mysql_clean($array['level_name']);
  1769.                 //Updating Now
  1770.                 $db->update(tbl('user_levels'), ['user_level_name'], [$level_name], " user_level_id = '$id'");
  1771.             }
  1772.  
  1773.             if (isset($_POST['plugin_perm'])) {
  1774.                 $fields_array[] = 'plugins_perms';
  1775.                 $value_array[] = '|no_mc|' . json_encode($_POST['plugin_perm']);
  1776.             }
  1777.  
  1778.             //Updating Permissions
  1779.             $db->update(tbl('user_levels_permissions'), $fields_array, $value_array, " user_level_id = '$id'");
  1780.  
  1781.             e(lang('level_updated'), 'm');
  1782.             return true;
  1783.         }
  1784.  
  1785.         return false;
  1786.     }
  1787.  
  1788.     /**
  1789.      * Function used to delete user levels
  1790.      * @param INT level_id
  1791.      * @throws Exception
  1792.      */
  1793.     function delete_user_level($id): bool
  1794.     {
  1795.         global $db;
  1796.         $level_details = $this->get_level_details($id);
  1797.         $de_level = $this->get_level_details(3);
  1798.         if ($level_details) {
  1799.             //CHeck if leve is deleteable or not
  1800.             if ($level_details['user_level_is_default'] == 'no') {
  1801.                 $db->delete(tbl('user_levels'), ['user_level_id'], [$id]);
  1802.                 $db->delete(tbl('user_levels_permissions'), ['user_level_id'], [$id]);
  1803.                 e(sprintf(lang('level_del_sucess'), $de_level['user_level_name']));
  1804.  
  1805.                 $db->update(tbl('users'), ['level'], [3], " level='$id'");
  1806.                 return true;
  1807.             }
  1808.  
  1809.             e(lang("level_not_deleteable"));
  1810.             return false;
  1811.         }
  1812.     }
  1813.  
  1814.     /**
  1815.      * Function used to count total video comments
  1816.      * @throws Exception
  1817.      */
  1818.     function count_profile_comments($id)
  1819.     {
  1820.         global $db;
  1821.         return $db->count(tbl('comments'), 'comment_id', "type='c' AND type_id='$id' AND parent_id='0'");
  1822.     }
  1823.  
  1824.     /**
  1825.      * Function used to update user comments count
  1826.      * @throws Exception
  1827.      */
  1828.     function update_comments_count($id)
  1829.     {
  1830.         global $db;
  1831.         $total_comments = $this->count_profile_comments($id);
  1832.         $db->update(tbl('users'), ['comments_count', 'last_commented'], [$total_comments, now()], " userid='$id'");
  1833.     }
  1834.  
  1835.     /**
  1836.      * Function used to add comment on users profile
  1837.      * @throws Exception
  1838.      */
  1839.     function add_comment($comment, $obj_id, $reply_to = null, $type = 'c')
  1840.     {
  1841.         global $myquery;
  1842.         if (!$this->user_exists($obj_id)) {
  1843.             e(lang('usr_exist_err'));
  1844.         } else {
  1845.             $add_comment = $myquery->add_comment($comment, $obj_id, $reply_to, $type, $obj_id);
  1846.         }
  1847.  
  1848.         if ($add_comment) {
  1849.             //Logging Comment
  1850.             $log_array = [
  1851.                 'success'        => 'yes',
  1852.                 'details'        => 'comment on a profile',
  1853.                 'action_obj_id'  => $obj_id,
  1854.                 'action_done_id' => $add_comment
  1855.             ];
  1856.             insert_log('profile_comment', $log_array);
  1857.  
  1858.             //Updating Number of comments of user if comment is not a reply
  1859.             if ($reply_to < 1) {
  1860.                 $this->update_comments_count($obj_id);
  1861.             }
  1862.         }
  1863.         return $add_comment;
  1864.     }
  1865.  
  1866.     /**
  1867.      * Function used to remove video comment
  1868.      * @throws Exception
  1869.      */
  1870.     function delete_comment($cid, $is_reply = false)
  1871.     {
  1872.         global $myquery;
  1873.         $remove_comment = $myquery->delete_comment($cid, 'c', $is_reply);
  1874.         if ($remove_comment) {
  1875.             //Updating Number of comments of video
  1876.             $this->update_comments_count($obj_id);
  1877.         }
  1878.         return $remove_comment;
  1879.     }
  1880.  
  1881.     /**
  1882.      * Function used to get number of videos uploaded by user
  1883.      *
  1884.      * @param      $uid
  1885.      * @param null $cond
  1886.      * @param bool $count_only
  1887.      * @param bool $myacc
  1888.      *
  1889.      * @return array|bool|int
  1890.      * @throws Exception
  1891.      */
  1892.     function get_user_vids($uid, $cond = null, $count_only = false, $myacc = false)
  1893.     {
  1894.         global $db;
  1895.         if ($cond != null) {
  1896.             $cond = " AND $cond ";
  1897.         }
  1898.  
  1899.         $limit = '';
  1900.         $order = '';
  1901.         if ($myacc) {
  1902.             $limit = ' 0,15 ';
  1903.             $order = ' videoid DESC';
  1904.         }
  1905.  
  1906.         $results = $db->select(tbl('video'), '*', " userid = '$uid' $cond", "$limit", "$order");
  1907.         if (count($results) > 0) {
  1908.             if ($myacc) {
  1909.                 return $results;
  1910.             }
  1911.  
  1912.             if ($count_only) {
  1913.                 return count($results);
  1914.             }
  1915.             return $results[0];
  1916.         }
  1917.         return false;
  1918.     }
  1919.  
  1920.     /**
  1921.      * Function used to get logged in username
  1922.      */
  1923.     function get_logged_username()
  1924.     {
  1925.         return $this->get_user_field_only(user_id(), 'username');
  1926.     }
  1927.  
  1928.     /**
  1929.      * FUnction used to get username from userid
  1930.      */
  1931.     function get_username($uid)
  1932.     {
  1933.         return $this->get_user_field_only($uid, 'username');
  1934.     }
  1935.  
  1936.     /**
  1937.      * Function used to create profile link
  1938.      *
  1939.      * @param $udetails
  1940.      *
  1941.      * @return string
  1942.      * @throws Exception
  1943.      */
  1944.     function profile_link($udetails): string
  1945.     {
  1946.         if (!is_array($udetails) && is_numeric($udetails)) {
  1947.             $udetails = $this->get_user_details($udetails);
  1948.         }
  1949.  
  1950.         $username = display_clean($udetails['username']);
  1951.         if (SEO != 'yes') {
  1952.             return '/view_channel.php?user=' . $username;
  1953.         }
  1954.  
  1955.         return '/user/' . $username;
  1956.     }
  1957.  
  1958.     /**
  1959.      * Function used to get permission types
  1960.      */
  1961.     function get_level_types(): array
  1962.     {
  1963.         global $db;
  1964.         return $db->select(tbl($this->dbtbl['user_permission_type']), '*');
  1965.     }
  1966.     /**
  1967.      * Function used to get permissions
  1968.      *
  1969.      * @param null $type
  1970.      *
  1971.      * @return array|bool
  1972.      * @throws Exception
  1973.      */
  1974.     function get_permissions($type = null)
  1975.     {
  1976.         global $db;
  1977.         $cond = '';
  1978.         if ($type) {
  1979.             $cond = " permission_type ='$type'";
  1980.         }
  1981.         $result = $db->select(tbl($this->dbtbl['user_permissions']), '*', $cond);
  1982.         if (count($result) > 0) {
  1983.             return $result;
  1984.         }
  1985.  
  1986.         return false;
  1987.     }
  1988.  
  1989.     /**
  1990.      * Function used to check weather current user has permission
  1991.      * to view page or not
  1992.      * it will also check weather current page requires login
  1993.      * if login is required, user will be redirected to signup page
  1994.      *
  1995.      * @param string $access
  1996.      * @param bool $check_login
  1997.      * @param bool $control_page
  1998.      *
  1999.      * @param bool $silent
  2000.      *
  2001.      * @return bool
  2002.      * @throws Exception
  2003.      */
  2004.     function perm_check($access = '', $check_login = false, $control_page = true, $silent = false): bool
  2005.     {
  2006.         global $Cbucket;
  2007.         $access_details = $this->permission;
  2008.         if (is_numeric($access)) {
  2009.             if ($access_details['level_id'] == $access) {
  2010.                 return true;
  2011.             }
  2012.  
  2013.             if (!$check_only && !$silent) {
  2014.                 e(lang('insufficient_privileges'));
  2015.             }
  2016.  
  2017.             if ($control_page) {
  2018.                 $Cbucket->show_page(false);
  2019.             }
  2020.             return false;
  2021.         }
  2022.  
  2023.         if ($access_details[$access] == 'yes') {
  2024.             return true;
  2025.         }
  2026.  
  2027.         if (!$silent) {
  2028.             if (!$check_login || user_id()) {
  2029.                 e(lang('insufficient_privileges'));
  2030.             } else {
  2031.                 e(sprintf(lang('insufficient_privileges_loggin'), cblink(['name' => 'signup']), cblink(['name' => 'signup'])));
  2032.             }
  2033.         }
  2034.  
  2035.         if ($control_page) {
  2036.             $Cbucket->show_page(false);
  2037.         }
  2038.         return false;
  2039.     }
  2040.  
  2041.     /**
  2042.      * Function used to get user profile details
  2043.      *
  2044.      * @param $uid
  2045.      *
  2046.      * @return bool|array
  2047.      * @throws Exception
  2048.      */
  2049.     function get_user_profile($uid)
  2050.     {
  2051.         global $db;
  2052.         $select = '';
  2053.         $join = '';
  2054.         $group = '';
  2055.         $version = get_current_version();
  2056.         if ($version['version'] > '5.5.0' || ($version['version'] == '5.5.0' && $version['revision'] >= 264)) {
  2057.             $select = ', GROUP_CONCAT(T.name SEPARATOR \',\') as profile_tags';
  2058.             $join = ' LEFT JOIN ' . tbl('user_tags') . ' UT ON UP.userid = UT.id_user
  2059.                    LEFT JOIN ' . tbl('tags') . ' T ON T.id_tag = UT.id_tag';
  2060.             $group = ' GROUP BY UP.userid';
  2061.         }
  2062.         $query = 'SELECT UP.* ' . $select . '
  2063.                    FROM ' . tbl($this->dbtbl['user_profile']) . ' UP
  2064.                   ' . $join . '
  2065.                    WHERE UP.userid = ' . mysql_clean($uid) . '
  2066.                   ' . $group;
  2067.         $result = $db->_select($query, 60);
  2068.  
  2069.         if (count($result) > 0) {
  2070.             return $result[0];
  2071.         }
  2072.         return false;
  2073.     }
  2074.  
  2075.     /**
  2076.      * User Profile Fields
  2077.      *
  2078.      * @param $default
  2079.      *
  2080.      * @return array
  2081.      * @throws Exception
  2082.      */
  2083.     function load_profile_fields($default): array
  2084.     {
  2085.         if (!$default) {
  2086.             $default = $_POST;
  2087.         }
  2088.  
  2089.         $profile_fields = $this->load_personal_details($default);
  2090.         $other_details = $this->load_location_fields($default);
  2091.         $more_details = $this->load_education_interests($default);
  2092.         $channel = $this->load_channel_settings($default);
  2093.         $privacy_field = $this->load_privacy_field($default);
  2094.         return array_merge($profile_fields, $other_details, $more_details, $channel, $privacy_field);
  2095.     }
  2096.  
  2097.     /**
  2098.      * Function used to update use details
  2099.      *
  2100.      * @param $array
  2101.      * @throws Exception
  2102.      */
  2103.     function update_user($array)
  2104.     {
  2105.         global $db, $Upload;
  2106.         if (is_null($array)) {
  2107.             $array = $_POST;
  2108.         }
  2109.  
  2110.         if (is_array($_FILES)) {
  2111.             $array = array_merge($array, $_FILES);
  2112.         }
  2113.  
  2114.         $userfields = $this->load_profile_fields($array);
  2115.         $custom_signup_fields = $this->load_custom_signup_fields($array);
  2116.  
  2117.         //Adding Custom Form Fields
  2118.         if (count($this->custom_profile_fields) > 0) {
  2119.             $userfields = array_merge($userfields, $this->custom_profile_fields);
  2120.         }
  2121.  
  2122.         //Adding custom fields from group
  2123.         if (count($this->custom_profile_fields_groups) > 0) {
  2124.             $custom_fields_from_group_fields = [];
  2125.             $custom_fields_from_group = $this->custom_profile_fields_groups;
  2126.             foreach ($custom_fields_from_group as $cffg) {
  2127.                 $custom_fields_from_group_fields = array_merge($custom_fields_from_group_fields, $cffg['fields']);
  2128.             }
  2129.  
  2130.             $userfields = array_merge($userfields, $custom_fields_from_group_fields);
  2131.         }
  2132.  
  2133.         validate_cb_form($custom_signup_fields, $array);
  2134.         validate_cb_form($userfields, $array);
  2135.  
  2136.         foreach ($userfields as $field) {
  2137.             $name = formObj::rmBrackets($field['name']);
  2138.             $val = $array[$name];
  2139.  
  2140.             if ($field['use_func_val']) {
  2141.                 $val = $field['validate_function']($val);
  2142.             }
  2143.  
  2144.             if (!empty($field['db_field'])) {
  2145.                 $query_field[] = $field['db_field'];
  2146.             }
  2147.  
  2148.             if (is_array($val)) {
  2149.                 $new_val = '';
  2150.                 foreach ($val as $v) {
  2151.                     $new_val .= '#' . $v . '# ';
  2152.                 }
  2153.                 $val = $new_val;
  2154.             }
  2155.             if ($field['clean_func'] && (function_exists($field['clean_func']) || is_array($field['clean_func']))) {
  2156.                 $val = apply_func($field['clean_func'], $val);
  2157.             }
  2158.  
  2159.             if (!empty($field['db_field'])) {
  2160.                 $query_val[] = $val;
  2161.             }
  2162.         }
  2163.  
  2164.         //Category
  2165.         if ($cat_field) {
  2166.             $field = $cat_field;
  2167.             $name = formObj::rmBrackets($field['name']);
  2168.             $val = $array[$name];
  2169.  
  2170.             if ($field['use_func_val']) {
  2171.                 $val = $field['validate_function']($val);
  2172.             }
  2173.  
  2174.             if (!empty($field['db_field'])) {
  2175.                 $uquery_field[] = $field['db_field'];
  2176.             }
  2177.  
  2178.             if (is_array($val)) {
  2179.                 $new_val = '';
  2180.                 foreach ($val as $v) {
  2181.                     $new_val .= '#' . $v . '# ';
  2182.                 }
  2183.                 $val = $new_val;
  2184.             }
  2185.             if ($field['clean_func'] && (function_exists($field['clean_func']) || is_array($field['clean_func']))) {
  2186.                 $val = apply_func($field['clean_func'], $val);
  2187.             }
  2188.  
  2189.             if (!empty($field['db_field'])) {
  2190.                 $uquery_val[] = $val;
  2191.             }
  2192.         }
  2193.  
  2194.         //updating user detail
  2195.         if (has_access('admin_access', true) && isset($array['admin_manager'])) {
  2196.             //Checking Username
  2197.             if (empty($array['username'])) {
  2198.                 e(lang('usr_uname_err'));
  2199.             } elseif (!username_check($array['username'])) {
  2200.                 e(lang('usr_uname_err3'));
  2201.             } else {
  2202.                 $username = $array['username'];
  2203.             }
  2204.  
  2205.             //Checking Email
  2206.             if (empty($array['email'])) {
  2207.                 e(lang('usr_email_err1'));
  2208.             } elseif (!is_valid_syntax('email', $array['email'])) {
  2209.                 e(lang('usr_email_err2'));
  2210.             } elseif (email_exists($array['email']) && $array['email'] != $array['demail']) {
  2211.                 e(lang('usr_email_err3'));
  2212.             } else {
  2213.                 $email = $array['email'];
  2214.             }
  2215.  
  2216.             $uquery_field[] = 'username';
  2217.             $uquery_val[] = $username;
  2218.  
  2219.             $uquery_field[] = 'email';
  2220.             $uquery_val[] = $email;
  2221.  
  2222.             //Changning Password
  2223.             if (!empty($array['pass'])) {
  2224.                 if ($array['pass'] != $array['cpass']) {
  2225.                     e(lang('pass_mismatched'));
  2226.                 } else {
  2227.                     $pass = pass_code($array['pass'], $array['userid']);
  2228.                 }
  2229.                 $uquery_field[] = 'password';
  2230.                 $uquery_val[] = $pass;
  2231.             }
  2232.  
  2233.             //Changing User Level
  2234.             $uquery_field[] = 'level';
  2235.             $uquery_val[] = $array['level'];
  2236.  
  2237.             //Checking for user stats
  2238.             $uquery_field[] = 'profile_hits';
  2239.             $uquery_val[] = $array['profile_hits'];
  2240.             $uquery_field[] = 'total_watched';
  2241.             $uquery_val[] = $array['total_watched'];
  2242.             $uquery_field[] = 'total_videos';
  2243.             $uquery_val[] = $array['total_videos'];
  2244.             $uquery_field[] = 'total_comments';
  2245.             $uquery_val[] = $array['total_comments'];
  2246.             $uquery_field[] = 'subscribers';
  2247.             $uquery_val[] = $array['subscribers'];
  2248.             $uquery_field[] = 'comments_count';
  2249.             $uquery_val[] = $array['comments_count'];
  2250.             $query_field[] = 'rating';
  2251.  
  2252.             $rating = $array['rating'];
  2253.             if ($rating < 1 || $rating > 10) {
  2254.                 $rating = 1;
  2255.             }
  2256.             $query_val[] = $rating;
  2257.             $query_field[] = 'rated_by';
  2258.             $query_val[] = $array['rated_by'];
  2259.  
  2260.             //Changing Joined Date
  2261.             if (isset($array['doj'])) {
  2262.                 $uquery_field[] = 'doj';
  2263.                 $uquery_val[] = $array['doj'];
  2264.             }
  2265.         }
  2266.  
  2267.         //Changing Gender
  2268.         if ($array['sex']) {
  2269.             $uquery_field[] = 'sex';
  2270.             $uquery_val[] = $array['sex'];
  2271.         }
  2272.  
  2273.         //Changing Country
  2274.         if ($array['country']) {
  2275.             $uquery_field[] = 'country';
  2276.             $uquery_val[] = $array['country'];
  2277.         }
  2278.  
  2279.         //Changing Date of birth
  2280.         if (isset($array['dob']) && $array['dob'] != '0000-00-00') {
  2281.             $uquery_field[] = 'dob';
  2282.  
  2283.             // Converting date from custom format to MySQL
  2284.             $dob_datetime = DateTime::createFromFormat(DATE_FORMAT, $array['dob']);
  2285.             if ($dob_datetime) {
  2286.                 $uquery_val[] = $dob_datetime->format('Y-m-d');
  2287.             } else {
  2288.                 $uquery_val[] = $array['dob'];
  2289.             }
  2290.         }
  2291.  
  2292.         //Changing category
  2293.         if (isset($array['category'])) {
  2294.             $uquery_field[] = 'category';
  2295.             $uquery_val[] = $array['category'];
  2296.         }
  2297.  
  2298.         //Updating User Avatar
  2299.         if ($array['avatar_url']) {
  2300.             $uquery_field[] = 'avatar_url';
  2301.             $uquery_val[] = $array['avatar_url'];
  2302.         }
  2303.  
  2304.         if ($array['remove_avatar_url'] == 'yes') {
  2305.             $uquery_field[] = 'avatar_url';
  2306.             $uquery_val[] = '';
  2307.         }
  2308.  
  2309.         //Deleting User Avatar
  2310.         if ($array['delete_avatar'] == 'yes') {
  2311.             $udetails = $this->get_user_details($array['userid']);
  2312.  
  2313.             $file = AVATARS_DIR . '/' . $udetails['avatar'];
  2314.             if (file_exists($file) && $udetails['avatar'] != '') {
  2315.                 unlink($file);
  2316.             }
  2317.  
  2318.             $uquery_field[] = 'avatar';
  2319.             $uquery_val[] = '';
  2320.         } else {
  2321.             if (isset($_FILES['avatar_file']['name']) && !empty($_FILES['avatar_file']['name'])) {
  2322.                 $file = $Upload->upload_user_file('a', $_FILES['avatar_file'], $array['userid']);
  2323.                 if ($file) {
  2324.                     $uquery_field[] = 'avatar';
  2325.                     $uquery_val[] = $file;
  2326.                 }
  2327.             }
  2328.         }
  2329.  
  2330.         //Deleting User Bg
  2331.         if ($array['delete_bg'] == 'yes') {
  2332.             $file = USER_BG_DIR . DIRECTORY_SEPARATOR . $array['bg_file_name'];
  2333.             if (file_exists($file) && $array['bg_file_name']) {
  2334.                 unlink($file);
  2335.             }
  2336.         }
  2337.  
  2338.         //Updating User Background
  2339.         if ($array['background_url']) {
  2340.             $uquery_field[] = 'background_url';
  2341.             $uquery_val[] = $array['background_url'];
  2342.         }
  2343.  
  2344.         if ($array['background_color']) {
  2345.             $uquery_field[] = 'background_color';
  2346.             $uquery_val[] = $array['background_color'];
  2347.         }
  2348.  
  2349.         if ($array['background_repeat']) {
  2350.             $uquery_field[] = 'background_repeat';
  2351.             $uquery_val[] = $array['background_repeat'];
  2352.         }
  2353.  
  2354.         if (isset($_FILES['background_file']['name']) && !empty($_FILES['background_file']['name'])) {
  2355.             $file = $Upload->upload_user_file('b', $_FILES['background_file'], $array['userid']);
  2356.             if ($file) {
  2357.                 $uquery_field[] = 'background';
  2358.                 $uquery_val[] = $file;
  2359.             }
  2360.         }
  2361.  
  2362.         //Adding Custom Field
  2363.         if (is_array($custom_signup_fields)) {
  2364.             foreach ($custom_signup_fields as $field) {
  2365.                 $name = formObj::rmBrackets($field['name']);
  2366.                 $val = $array[$name];
  2367.  
  2368.                 if ($field['use_func_val']) {
  2369.                     $val = $field['validate_function']($val);
  2370.                 }
  2371.  
  2372.                 if (!empty($field['db_field'])) {
  2373.                     $uquery_field[] = $field['db_field'];
  2374.                 }
  2375.  
  2376.                 if (is_array($val)) {
  2377.                     $new_val = '';
  2378.                     foreach ($val as $v) {
  2379.                         $new_val .= '#' . $v . '# ';
  2380.                     }
  2381.                     $val = $new_val;
  2382.                 }
  2383.                 if ($field['clean_func'] && (function_exists($field['clean_func']) || is_array($field['clean_func']))) {
  2384.                     $val = apply_func($field['clean_func'], $val);
  2385.                 }
  2386.  
  2387.                 if (!empty($field['db_field'])) {
  2388.                     $uquery_val[] = $val;
  2389.                 }
  2390.             }
  2391.         }
  2392.  
  2393.         if (!error() && is_array($uquery_field)) {
  2394.             $db->update(tbl($this->dbtbl['users']), $uquery_field, $uquery_val, " userid='" . mysql_clean($array['userid']) . "'");
  2395.             e(lang('usr_upd_succ_msg'), 'm');
  2396.         }
  2397.  
  2398.         //updating user profile
  2399.         if (!error()) {
  2400.             $log_array = [
  2401.                 'success' => 'yes',
  2402.                 'details' => 'updated profile'
  2403.             ];
  2404.             //Login Upload
  2405.             insert_log('profile_update', $log_array);
  2406.  
  2407.             $db->update(tbl($this->dbtbl['user_profile']), $query_field, $query_val, " userid='" . mysql_clean($array['userid']) . "'");
  2408.  
  2409.             Tags::saveTags($array['profile_tags'], 'profile', $array['userid']);
  2410.             e(lang('usr_pof_upd_msg'), 'm');
  2411.         }
  2412.     }
  2413.  
  2414.     /**
  2415.      * Function used to update user avatar and background only
  2416.      *
  2417.      * @param $array
  2418.      * @throws Exception
  2419.      */
  2420.     function update_user_avatar_bg($array)
  2421.     {
  2422.         global $db, $Upload;
  2423.  
  2424.         //Deleting User Avatar
  2425.         if ($array['delete_avatar'] == 'yes') {
  2426.             $udetails = $this->get_user_details(user_id());
  2427.  
  2428.             $file = AVATARS_DIR . DIRECTORY_SEPARATOR . $udetails['avatar_url'];
  2429.             if (file_exists($file) && $udetails['avatar_url'] != '') {
  2430.                 unlink($file);
  2431.             }
  2432.  
  2433.             $uquery_field[] = 'avatar';
  2434.             $uquery_val[] = '';
  2435.  
  2436.             $uquery_field[] = 'avatar_url';
  2437.             $uquery_val[] = '';
  2438.         } else {
  2439.             if (config('picture_url') == 'yes') {
  2440.                 //Updating User Avatar
  2441.                 $uquery_field[] = 'avatar_url';
  2442.                 $uquery_val[] = $array['avatar_url'];
  2443.             }
  2444.  
  2445.             if (isset($_FILES['avatar_file']['name'])) {
  2446.                 $file = $Upload->upload_user_file('a', $_FILES['avatar_file'], user_id());
  2447.                 if ($file) {
  2448.                     $uquery_field[] = 'avatar';
  2449.                     $uquery_val[] = $file;
  2450.                 }
  2451.             }
  2452.         }
  2453.  
  2454.         //Deleting User Bg
  2455.         if ($array['delete_bg'] == 'yes') {
  2456.             $file = USER_BG_DIR . DIRECTORY_SEPARATOR . $array['bg_file_name'];
  2457.             if (file_exists($file) && $array['bg_file_name'] != '') {
  2458.                 unlink($file);
  2459.             }
  2460.         }
  2461.  
  2462.         if (config('background_url') == 'yes') {
  2463.             //Updating User Background
  2464.             $uquery_field[] = 'background_url';
  2465.             $uquery_val[] = $array['background_url'];
  2466.         }
  2467.  
  2468.         $uquery_field[] = 'background_color';
  2469.         $uquery_val[] = $array['background_color'];
  2470.  
  2471.         if ($array['background_repeat']) {
  2472.             $uquery_field[] = 'background_repeat';
  2473.             $uquery_val[] = $array['background_repeat'];
  2474.         }
  2475.  
  2476.         if (isset($_FILES['background_file']['name'])) {
  2477.             $file = $Upload->upload_user_file('b', $_FILES['background_file'], user_id());
  2478.             if ($file) {
  2479.                 $uquery_field[] = 'background';
  2480.                 $uquery_val[] = $file;
  2481.             }
  2482.         }
  2483.  
  2484.         $log_array = [
  2485.             'success' => 'yes',
  2486.             'details' => 'updated profile'
  2487.         ];
  2488.  
  2489.         //Login Upload
  2490.         insert_log('profile_update', $log_array);
  2491.  
  2492.         $db->update(tbl($this->dbtbl['users']), $uquery_field, $uquery_val, ' userid=\'' . user_id() . '\'');
  2493.         e(lang('usr_avatar_bg_update'), 'm');
  2494.     }
  2495.  
  2496.     public function updateBackground($file = []): array
  2497.     {
  2498.         if (empty($file)) {
  2499.             return [
  2500.                 'status' => false,
  2501.                 'msg'    => 'no data was sent'
  2502.             ];
  2503.         }
  2504.  
  2505.         if ($file['size'] / 1024 > config('max_bg_size')) {
  2506.             return [
  2507.                 'status' => false,
  2508.                 'msg'    => sprintf(lang('file_size_exceeds'), config('max_bg_size'))
  2509.             ];
  2510.         }
  2511.  
  2512.         $av_details = getimagesize($file['tmp_name']);
  2513.         if ($av_details[0] > config('max_bg_width')) {
  2514.             return [
  2515.                 'status' => false,
  2516.                 'msg'    => lang('File width exeeds') . ' ' . config('max_bg_width') . 'px'
  2517.             ];
  2518.         }
  2519.  
  2520.         if (!file_exists($file['tmp_name'])) {
  2521.             return [
  2522.                 'status' => false,
  2523.                 'msg'    => lang('class_error_occured')
  2524.             ];
  2525.         }
  2526.  
  2527.         $ext = getext($file['name']);
  2528.         $file_name = $file['userid'] . '.' . $ext;
  2529.         $file_path = USER_BG_DIR . DIRECTORY_SEPARATOR . $file_name;
  2530.         if (move_uploaded_file($file['tmp_name'], $file_path)) {
  2531.             $imgObj = new ResizeImage();
  2532.             if (!$imgObj->ValidateImage($file_path, $ext)) {
  2533.                 @unlink($file_path);
  2534.                 return [
  2535.                     'status' => false,
  2536.                     'msg'    => 'Invalid file type'
  2537.                 ];
  2538.             }
  2539.             return [
  2540.                 'status' => true,
  2541.                 'msg'    => 'Succesfully Uploaded'
  2542.             ];
  2543.         }
  2544.         return [
  2545.             'status' => false,
  2546.             'msg'    => lang('class_error_occured')
  2547.         ];
  2548.     }
  2549.  
  2550.     public function getBackground($userId = false)
  2551.     {
  2552.         if (!$userId) {
  2553.             $userId = user_id();
  2554.         }
  2555.  
  2556.         global $userquery;
  2557.         return $userquery->getUserBg($userquery->get_user_details($userId));
  2558.     }
  2559.  
  2560.     public function getImageExt($imageName = false)
  2561.     {
  2562.         if ($imageName) {
  2563.             $nameParts = explode('.', $imageName);
  2564.             return array_pop($nameParts);
  2565.         }
  2566.     }
  2567.  
  2568.     /**
  2569.      * Function used to check weather username exists or not
  2570.      *
  2571.      * @param $i
  2572.      *
  2573.      * @return bool
  2574.      * @throws Exception
  2575.      */
  2576.     function username_exists($i)
  2577.     {
  2578.         global $db;
  2579.         return $db->count(tbl($this->dbtbl['users']), 'username', " username='$i'");
  2580.     }
  2581.  
  2582.     /**
  2583.      * function used to check weather email exists or not
  2584.      *
  2585.      * @param $i
  2586.      *
  2587.      * @return bool
  2588.      * @throws Exception
  2589.      */
  2590.     function email_exists($i): bool
  2591.     {
  2592.         global $db;
  2593.         $result = $db->select(tbl($this->dbtbl['users']), 'email', " email='$i'");
  2594.         if (count($result) > 0) {
  2595.             return true;
  2596.         }
  2597.         return false;
  2598.     }
  2599.  
  2600.     public function check_email_domain($email): bool
  2601.     {
  2602.         $email_domain_restriction = config('email_domain_restriction');
  2603.         if ($email_domain_restriction != '') {
  2604.             $list_domains = explode(',', $email_domain_restriction);
  2605.             foreach ($list_domains as $domain) {
  2606.                 if (strpos($email, '@' . $domain) !== false) {
  2607.                     return true;
  2608.                 }
  2609.             }
  2610.             return false;
  2611.         }
  2612.         return true;
  2613.     }
  2614.  
  2615.     /**
  2616.      * Function used to get user access log
  2617.      *
  2618.      * @param      $uid
  2619.      * @param null $limit
  2620.      *
  2621.      * @return array|bool
  2622.      * @throws Exception
  2623.      */
  2624.     function get_user_action_log($uid, $limit = null)
  2625.     {
  2626.         global $db;
  2627.         $result = $db->select(tbl($this->dbtbl['action_log']), '*', " action_userid='$uid'", $limit, ' date_added DESC');
  2628.         if (count($result) > 0) {
  2629.             return $result;
  2630.         }
  2631.         return false;
  2632.     }
  2633.  
  2634.     /**
  2635.      * Load Custom Profile Field
  2636.      *
  2637.      * @param      $data
  2638.      * @param bool $group_based
  2639.      *
  2640.      * @return array
  2641.      */
  2642.     function load_custom_profile_fields($data, $group_based = false): array
  2643.     {
  2644.         if (!$group_based) {
  2645.             $new_array = [];
  2646.             $array = $this->custom_profile_fields;
  2647.             foreach ($array as $key => $fields) {
  2648.                 if ($data[$fields['db_field']]) {
  2649.                     $value = $data[$fields['db_field']];
  2650.                 } else {
  2651.                     if ($data[$fields['name']]) {
  2652.                         $value = $data[$fields['name']];
  2653.                     }
  2654.                 }
  2655.  
  2656.                 if ($fields['type'] == 'radiobutton' ||
  2657.                     $fields['type'] == 'checkbox' ||
  2658.                     $fields['type'] == 'dropdown') {
  2659.                     $fields['checked'] = $value;
  2660.                 } else {
  2661.                     $fields['value'] = $value;
  2662.                 }
  2663.  
  2664.                 $new_array[$key] = $fields;
  2665.             }
  2666.             return $new_array;
  2667.         }
  2668.  
  2669.         $groups = $this->custom_profile_fields_groups;
  2670.  
  2671.         $new_grp = [];
  2672.         if ($groups) {
  2673.             foreach ($groups as $grp) {
  2674.                 $fields = [];
  2675.                 foreach ($grp['fields'] as $key => $fields) {
  2676.                     if ($data[$fields['db_field']]) {
  2677.                         $value = $data[$fields['db_field']];
  2678.                     } elseif ($data[$fields['name']]) {
  2679.                         $value = $data[$fields['name']];
  2680.                     }
  2681.  
  2682.                     if ($fields['type'] == 'radiobutton' ||
  2683.                         $fields['type'] == 'checkbox' ||
  2684.                         $fields['type'] == 'dropdown') {
  2685.                         $fields['checked'] = $value;
  2686.                     } else {
  2687.                         $fields['value'] = $value;
  2688.                     }
  2689.                 }
  2690.                 $grp['fields'][$key] = $fields;
  2691.                 $new_grp[] = $grp;
  2692.             }
  2693.         }
  2694.         return $new_grp;
  2695.     }
  2696.  
  2697.     /**
  2698.      * Load Custom Signup Field
  2699.      *
  2700.      * @param      $data
  2701.      * @param bool $ck_display_admin
  2702.      * @param bool $ck_display_user
  2703.      *
  2704.      * @return mixed
  2705.      */
  2706.     function load_custom_signup_fields($data, $ck_display_admin = false, $ck_display_user = false)
  2707.     {
  2708.         $array = $this->custom_signup_fields;
  2709.         foreach ($array as $key => $fields) {
  2710.             $ok = 'yes';
  2711.             if ($ck_display_admin) {
  2712.                 if ($fields['display_admin'] == 'no_display') {
  2713.                     $ok = 'no';
  2714.                 }
  2715.             }
  2716.  
  2717.             if ($ck_display_user) {
  2718.                 if ($fields['display_user'] == 'no_display') {
  2719.                     $ok = 'no';
  2720.                 }
  2721.             }
  2722.  
  2723.             if ($ok == 'yes') {
  2724.                 if (!$fields['value']) {
  2725.                     $fields['value'] = $data[$fields['db_field']];
  2726.                 }
  2727.                 $new_array[$key] = $fields;
  2728.             }
  2729.         }
  2730.  
  2731.         return $new_array;
  2732.     }
  2733.  
  2734.     /**
  2735.      * Function used to get user videos link
  2736.      */
  2737.     function get_user_videos_link($u)
  2738.     {
  2739.         return cblink(['name' => 'user_videos']) . $u['username'];
  2740.     }
  2741.  
  2742.     /*
  2743.     * Get number of all unread messages of a user using his userid
  2744.     */
  2745.     /**
  2746.      * @throws Exception
  2747.      */
  2748.     function get_unread_msgs($userid, $label = false)
  2749.     {
  2750.         global $db;
  2751.         $userid = '#' . $userid . '#';
  2752.         $results = $db->select(tbl('messages'), '*', "message_to='$userid' AND message_status='unread'");
  2753.         $count = count($results);
  2754.  
  2755.         if ($label) {
  2756.             echo '<span class="label label-default">' . $count . '</span></h3>';
  2757.         }
  2758.  
  2759.         return $count;
  2760.     }
  2761.  
  2762.     /**
  2763.      * My Account links Edited on 12 march 2014 for user account links
  2764.      */
  2765.     function my_account_links()
  2766.     {
  2767.         $array[lang('account')] = [
  2768.             lang('my_account')        => 'myaccount.php',
  2769.             lang('block_users')       => 'edit_account.php?mode=block_users',
  2770.             lang('user_change_pass')  => 'edit_account.php?mode=change_password',
  2771.             lang('user_change_email') => 'edit_account.php?mode=change_email',
  2772.             lang('com_manage_subs')   => 'edit_account.php?mode=subscriptions',
  2773.             lang('account_settings')  => 'edit_account.php?mode=account'
  2774.         ];
  2775.  
  2776.         $udetails = $this->get_user_details(user_id());
  2777.         if (config('picture_upload') == 'yes' || config('picture_url') == 'yes' || !empty($udetails['avatar_url']) || !empty($udetails['avatar'])) {
  2778.             $array[lang('account')][lang('change_avatar')] = 'edit_account.php?mode=avatar_bg';
  2779.         }
  2780.  
  2781.         if (isSectionEnabled('channels')) {
  2782.             $array[lang('user_channel_profiles')] = [
  2783.                 lang('user_profile_settings') => 'edit_account.php?mode=profile',
  2784.                 lang('contacts_manager')      => 'manage_contacts.php'
  2785.             ];
  2786.         }
  2787.  
  2788.         if (isSectionEnabled('videos')) {
  2789.             $array[lang('videos')] = [
  2790.                 lang('uploaded_videos') => 'manage_videos.php',
  2791.                 lang('user_fav_videos') => 'manage_videos.php?mode=favorites',
  2792.             ];
  2793.         }
  2794.  
  2795.         if (isSectionEnabled('playlists')) {
  2796.             $array[lang('playlists')] = [
  2797.                 lang('manage_playlists') => 'manage_playlists.php',
  2798.             ];
  2799.         }
  2800.  
  2801.         $array[lang('messages')] = [
  2802.             lang('inbox') . '(' . $this->get_unread_msgs($this->userid) . ')' => 'private_message.php?mode=inbox',
  2803.             lang('notifications')                                             => 'private_message.php?mode=notification',
  2804.             lang('sent')                                                      => 'private_message.php?mode=sent',
  2805.             lang('title_crt_new_msg')                                         => cblink(['name' => 'compose_new']),
  2806.         ];
  2807.  
  2808.         if (count($this->user_account) > 0) {
  2809.             foreach ($this->user_account as $key => $acc) {
  2810.                 if (array_key_exists($key, $array)) {
  2811.                     foreach ($acc as $title => $link)
  2812.                         $array[$key][$title] = $link;
  2813.                 } else {
  2814.                     $array[$key] = $acc;
  2815.                 }
  2816.             }
  2817.         }
  2818.         return $array;
  2819.     }
  2820.  
  2821.  
  2822.     /**
  2823.      * Function used to change email
  2824.      *
  2825.      * @param $array
  2826.      * @throws Exception
  2827.      */
  2828.     function change_email($array)
  2829.     {
  2830.         global $db;
  2831.         //function used to change user email
  2832.         if (!isValidEmail($array['new_email']) || $array['new_email'] == '') {
  2833.             e(lang("usr_email_err2"));
  2834.         } elseif ($array['new_email'] != $array['cnew_email']) {
  2835.             e(lang('user_email_confirm_email_err'));
  2836.         } elseif (!$this->user_exists($array['userid'])) {
  2837.             e(lang('usr_exist_err'));
  2838.         } elseif ($this->email_exists($array['new_email'])) {
  2839.             e(lang('usr_email_err3'));
  2840.         } else {
  2841.             $db->update(tbl($this->dbtbl['users']), ['email'], [$array['new_email']], " userid='" . $array['userid'] . "'");
  2842.             e(lang('email_change_msg'), 'm');
  2843.         }
  2844.     }
  2845.  
  2846.     /**
  2847.      * Function used to ban users
  2848.      *
  2849.      * @param      $users
  2850.      * @param null $uid
  2851.      *
  2852.      * @return void
  2853.      * @throws Exception
  2854.      */
  2855.     function block_users($users, $uid = null)
  2856.     {
  2857.         $this->ban_users($users, $uid);
  2858.     }
  2859.  
  2860.     /**
  2861.      * @throws Exception
  2862.      */
  2863.     function ban_users($users, $uid = null)
  2864.     {
  2865.         global $db;
  2866.         if (!$uid) {
  2867.             $uid = user_id();
  2868.         }
  2869.         $users_array = explode(',', $users);
  2870.         $new_users = [];
  2871.         foreach ($users_array as $user) {
  2872.             if ($user != user_name() && !is_numeric($user) && $this->user_exists($user)) {
  2873.                 $new_users[] = $user;
  2874.             }
  2875.         }
  2876.         if (count($new_users) > 0) {
  2877.             $new_users = array_unique($new_users);
  2878.             $banned_users = implode(',', $new_users);
  2879.             $db->update(tbl($this->dbtbl['users']), ['banned_users'], [$banned_users], " userid='$uid'");
  2880.             e(lang('user_ban_msg'), 'm');
  2881.         } else {
  2882.             if (!$users) {
  2883.                 $db->update(tbl($this->dbtbl['users']), ['banned_users'], [$users], " userid='$uid'");
  2884.                 e(lang('no_user_ban_msg'), 'm');
  2885.             }
  2886.         }
  2887.     }
  2888.  
  2889.     /**
  2890.      * Function used to ban single user
  2891.      *
  2892.      * @param $user
  2893.      * @throws Exception
  2894.      */
  2895.     function ban_user($user)
  2896.     {
  2897.         global $db;
  2898.         $uid = user_id();
  2899.  
  2900.         if (!$uid) {
  2901.             e(lang('you_not_logged_in'));
  2902.         } else {
  2903.             if ($user != user_name() && !is_numeric($user) && $this->user_exists($user)) {
  2904.                 $banned_users = $this->udetails['banned_users'];
  2905.                 if ($banned_users) {
  2906.                     $banned_users .= ",$user";
  2907.                 } else {
  2908.                     $banned_users = "$user";
  2909.                 }
  2910.  
  2911.                 if (!$this->is_user_banned($user)) {
  2912.                     $db->update(tbl($this->dbtbl['users']), ['banned_users'], [$banned_users], " userid='$uid'");
  2913.                     e(lang('user_blocked'), 'm');
  2914.                 } else {
  2915.                     e(lang('user_already_blocked'));
  2916.                 }
  2917.             } else {
  2918.                 e(lang('you_cant_del_user'));
  2919.             }
  2920.         }
  2921.     }
  2922.  
  2923.     /**
  2924.      * Function used to check weather user is banned or not
  2925.      *
  2926.      * @param      $ban
  2927.      * @param null $user
  2928.      * @param null $banned_users
  2929.      *
  2930.      * @return bool
  2931.      * @throws Exception
  2932.      */
  2933.     function is_user_banned($ban, $user = null, $banned_users = null): bool
  2934.     {
  2935.         global $db;
  2936.         if (!$user) {
  2937.             $user = user_id();
  2938.         }
  2939.  
  2940.         if (!$banned_users) {
  2941.             if (is_numeric($user)) {
  2942.                 $result = $db->select(tbl($this->dbtbl['users']), 'banned_users', " userid='$user' ");
  2943.             } else {
  2944.                 $result = $db->select(tbl($this->dbtbl['users']), 'banned_users', " username='$user' ");
  2945.             }
  2946.             $banned_users = $result[0]['banned_users'];
  2947.         }
  2948.  
  2949.         $ban_user = explode(',', $banned_users);
  2950.         if (in_array($ban, $ban_user)) {
  2951.             return true;
  2952.         }
  2953.         return false;
  2954.     }
  2955.  
  2956.     /**
  2957.      * function used to get user details with profile
  2958.      *
  2959.      * @param null $uid
  2960.      *
  2961.      * @return array
  2962.      * @throws Exception
  2963.      */
  2964.     function get_user_details_with_profile($uid = null): array
  2965.     {
  2966.         global $db;
  2967.         if (!$uid) {
  2968.             $uid = user_id();
  2969.         }
  2970.         $result = $db->select(tbl($this->dbtbl['users'] . ',' . $this->dbtbl['user_profile']), '*', tbl($this->dbtbl['users']) . ".userid ='$uid' AND " . tbl($this->dbtbl['users']) . '.userid = ' . tbl($this->dbtbl['user_profile']) . '.userid');
  2971.         return $result[0];
  2972.     }
  2973.  
  2974.     /**
  2975.      * @throws \PHPMailer\PHPMailer\Exception
  2976.      * @throws Exception
  2977.      */
  2978.     function load_signup_fields($input = null): array
  2979.     {
  2980.         global $Cbucket;
  2981.  
  2982.         $default = [];
  2983.  
  2984.         if (isset($input)) {
  2985.             $default = $input;
  2986.         }
  2987.         /**
  2988.          * this function will create initial array for user fields
  2989.          * this will tell
  2990.          * array(
  2991.          *       title [text that will represents the field]
  2992.          *       type [type of field, either radio button, textfield or text area]
  2993.          *       name [name of the fields, input NAME attribute]
  2994.          *       id [id of the fields, input ID attribute]
  2995.          *       value [value of the fields, input VALUE attribute]
  2996.          *       size
  2997.          *       class
  2998.          *       label
  2999.          *       extra_params
  3000.          *       hint_1 [hint before field]
  3001.          *       hint_2 [hint after field]
  3002.          *       anchor_before [anchor before field]
  3003.          *       anchor_after [anchor after field]
  3004.          *      )
  3005.          */
  3006.  
  3007.         if (empty($default)) {
  3008.             $default = $_POST;
  3009.         }
  3010.  
  3011.         if (empty($default)) {
  3012.             $default = $_POST;
  3013.         }
  3014.  
  3015.         $username = $default['username'] ?? '';
  3016.         $email = $default['email'] ?? '';
  3017.         $dob = $default['dob'] ?? '';
  3018.  
  3019.         if ($dob != '' && $dob != '0000-00-00') {
  3020.             $dob_datetime = DateTime::createFromFormat('Y-m-d', $dob);
  3021.             if ($dob_datetime) {
  3022.                 $dob = $dob_datetime->format(DATE_FORMAT);
  3023.             }
  3024.         }
  3025.  
  3026.         $user_signup_fields = [
  3027.             'username'  => [
  3028.                 'title'               => lang('username'),
  3029.                 'type'                => 'textfield',
  3030.                 'placehoder'          => lang('username'),
  3031.                 'name'                => 'username',
  3032.                 'id'                  => 'username',
  3033.                 'value'               => $username,
  3034.                 'hint_2'              => lang('user_allowed_format'),
  3035.                 'db_field'            => 'username',
  3036.                 'required'            => 'yes',
  3037.                 'validate_function'   => 'username_check',
  3038.                 'function_error_msg'  => lang('user_contains_disallow_err'),
  3039.                 'db_value_check_func' => 'user_exists',
  3040.                 'db_value_exists'     => false,
  3041.                 'db_value_err'        => lang('usr_uname_err2'),
  3042.                 'min_length'          => config('min_username'),
  3043.                 'max_length'          => config('max_username')
  3044.             ],
  3045.             'email'     => [
  3046.                 'title'               => lang('email'),
  3047.                 'type'                => 'textfield',
  3048.                 'placehoder'          => 'Email',
  3049.                 'name'                => 'email',
  3050.                 'id'                  => 'email',
  3051.                 'value'               => $email,
  3052.                 'db_field'            => 'email',
  3053.                 'required'            => 'yes',
  3054.                 'syntax_type'         => 'email',
  3055.                 'db_value_check_func' => 'email_exists',
  3056.                 'db_value_exists'     => false,
  3057.                 'db_value_err'        => lang('usr_email_err3'),
  3058.                 'validate_function'   => 'isValidEmail',
  3059.                 'constraint_func'     => 'check_email_domain',
  3060.                 'constraint_err'      => lang('signup_error_email_unauthorized')
  3061.             ],
  3062.             'password'  => [
  3063.                 'title'         => lang('password'),
  3064.                 'type'          => 'password',
  3065.                 'placehoder'    => lang('password'),
  3066.                 'name'          => 'password',
  3067.                 'id'            => 'password',
  3068.                 'required'      => 'yes',
  3069.                 'invalid_err'   => lang('usr_pass_err2'),
  3070.                 'relative_to'   => 'cpassword',
  3071.                 'relative_type' => 'exact',
  3072.                 'relative_err'  => lang('usr_pass_err3'),
  3073.             ],
  3074.             'cpassword' => [
  3075.                 'title'       => lang('user_confirm_pass'),
  3076.                 'type'        => 'password',
  3077.                 'placehoder'  => lang('user_confirm_pass'),
  3078.                 'name'        => 'cpassword',
  3079.                 'id'          => 'cpassword',
  3080.                 'required'    => 'no',
  3081.                 'invalid_err' => lang('usr_cpass_err')
  3082.             ],
  3083.             'dob'       => [
  3084.                 'title'             => lang('user_date_of_birth'),
  3085.                 'type'              => 'textfield',
  3086.                 'name'              => 'dob',
  3087.                 'readonly'          => 'true',
  3088.                 'id'                => 'dob',
  3089.                 'anchor_after'      => 'date_picker',
  3090.                 'value'             => $dob,
  3091.                 'validate_function' => 'verify_age',
  3092.                 'db_field'          => 'dob',
  3093.                 'required'          => 'yes',
  3094.                 'placehoder'        => lang('user_date_of_birth'),
  3095.                 'invalid_err'       => sprintf(lang('register_min_age_request'), config('min_age_reg'))
  3096.             ]
  3097.         ];
  3098.  
  3099.         if( config('enable_country') == 'yes' ){
  3100.             $countries = $Cbucket->get_countries();
  3101.             $selected_cont = null;
  3102.             $pick_geo_country = config('pick_geo_country');
  3103.             if ($pick_geo_country == 'yes') {
  3104.                 $user_ip = $_SERVER['REMOTE_ADDR']; // getting user's ip
  3105.                 $user_country = ip_info($user_ip, 'country'); // get country using IP
  3106.                 foreach ($countries as $code => $name) {
  3107.                     $name = strtolower($name);
  3108.                     $user_country = strtolower($user_country);
  3109.                     if ($name == $user_country) {
  3110.                         $selected_cont = $code;
  3111.                         break;
  3112.                     }
  3113.                 }
  3114.             } else {
  3115.                 $selected_cont = config('default_country_iso2');
  3116.             }
  3117.  
  3118.             if (strlen($selected_cont) != 2) {
  3119.                 $selected_cont = 'PK';
  3120.             }
  3121.  
  3122.             $user_signup_fields['country'] = [
  3123.                 'title'    => lang('country'),
  3124.                 'type'     => 'dropdown',
  3125.                 'value'    => $countries,
  3126.                 'id'       => 'country',
  3127.                 'name'     => 'country',
  3128.                 'checked'  => $selected_cont,
  3129.                 'db_field' => 'country',
  3130.                 'required' => 'yes'
  3131.             ];
  3132.         }
  3133.  
  3134.         if( config('enable_gender') == 'yes' ) {
  3135.             $user_signup_fields['gender'] = [
  3136.                 'title'    => lang('gender'),
  3137.                 'type'     => 'radiobutton',
  3138.                 'name'     => 'gender',
  3139.                 'class'    => 'radio',
  3140.                 'id'       => 'gender',
  3141.                 'value'    => ['Male' => lang('male'), 'Female' => lang('female')],
  3142.                 'sep'      => '&nbsp;',
  3143.                 'checked'  => 'Male',
  3144.                 'db_field' => 'sex',
  3145.                 'required' => 'yes'
  3146.             ];
  3147.         }
  3148.  
  3149.         if( config('enable_user_category') == 'yes' ) {
  3150.             $user_signup_fields['cat'] = [
  3151.                 'title'            => lang('category'),
  3152.                 'type'             => 'dropdown',
  3153.                 'name'             => 'category',
  3154.                 'id'               => 'category',
  3155.                 'value'            => ['category', ($default['category'] ?? '')],
  3156.                 'db_field'         => 'category',
  3157.                 'checked'          => ($default['category'] ?? ''),
  3158.                 'required'         => 'yes',
  3159.                 'invalid_err'      => lang('select_category'),
  3160.                 'display_function' => 'convert_to_categories',
  3161.                 'category_type'    => 'user'
  3162.             ];
  3163.         }
  3164.  
  3165.         $new_array = [];
  3166.         foreach ($user_signup_fields as $id => $fields) {
  3167.             $the_array = $fields;
  3168.             if (isset($the_array['hint_1'])) {
  3169.                 $the_array['hint_before'] = $the_array['hint_1'];
  3170.             }
  3171.  
  3172.             if (isset($the_array['hint_2'])) {
  3173.                 $the_array['hint_after'] = $the_array['hint_2'];
  3174.             }
  3175.  
  3176.             $new_array[$id] = $the_array;
  3177.         }
  3178.  
  3179.         $new_array[] = $this->load_custom_profile_fields($default, false);
  3180.  
  3181.         return $new_array;
  3182.     }
  3183.  
  3184.     /**
  3185.      * Function used to validate Signup Form
  3186.      *
  3187.      * @param null $array
  3188.      * @throws \PHPMailer\PHPMailer\Exception
  3189.      */
  3190.     function validate_form_fields($array = null)
  3191.     {
  3192.         $fields = $this->load_signup_fields($array);
  3193.  
  3194.         if ($array == null) {
  3195.             $array = $_POST;
  3196.         }
  3197.  
  3198.         if (is_array($_FILES)) {
  3199.             $array = array_merge($array, $_FILES);
  3200.         }
  3201.  
  3202.         //Merging Array
  3203.         $signup_fields = array_merge($fields, $this->custom_signup_fields);
  3204.  
  3205.         validate_cb_form($signup_fields, $array);
  3206.     }
  3207.  
  3208.     /**
  3209.      * Function used to validate signup form
  3210.      *
  3211.      * @param null $array
  3212.      * @param bool $send_signup_email
  3213.      *
  3214.      * @return bool|mixed
  3215.      * @throws Exception
  3216.      */
  3217.     function signup_user($array = null, $send_signup_email = true)
  3218.     {
  3219.         global $db, $userquery;
  3220.  
  3221.         $isSocial = false;
  3222.         if (isset($array['social_account_id'])) {
  3223.             $isSocial = true;
  3224.         }
  3225.         if ($array == null) {
  3226.             $array = $_POST;
  3227.         }
  3228.  
  3229.         if (is_array($_FILES)) {
  3230.             $array = array_merge($array, $_FILES);
  3231.         }
  3232.         $this->validate_form_fields($array);
  3233.         //checking terms and policy agreement
  3234.         if ($array['agree'] != 'yes' && !has_access('admin_access', true)) {
  3235.             e(lang('usr_ament_err'));
  3236.         }
  3237.  
  3238.         // first checking if captcha plugin is enabled
  3239.         // do not trust the form cb_captcha_enabled value
  3240.         if (get_captcha() && !$userquery->admin_login_check(true) && !$isSocial) {
  3241.             // now checking if the user posted captcha value is not empty and cb_captcha_enabled == yes
  3242.             if (!isset($array['cb_captcha_enabled']) || $array['cb_captcha_enabled'] == 'no') {
  3243.                 e(lang('recap_verify_failed'));
  3244.             }
  3245.             if (!verify_captcha()) {
  3246.                 e(lang('recap_verify_failed'));
  3247.             }
  3248.         }
  3249.         if (!error()) {
  3250.             $signup_fields = $this->load_signup_fields($array);
  3251.  
  3252.             //Adding Custom Signup Fields
  3253.             if (count($this->custom_signup_fields) > 0) {
  3254.                 $signup_fields = array_merge($signup_fields, $this->custom_signup_fields);
  3255.             }
  3256.  
  3257.             foreach ($signup_fields as $field) {
  3258.                 $name = formObj::rmBrackets($field['name']);
  3259.                 $val = $array[$name];
  3260.  
  3261.                 if ($name == 'dob') {
  3262.                     $dob_datetime = DateTime::createFromFormat(DATE_FORMAT, $val);
  3263.                     if ($dob_datetime) {
  3264.                         $val = $dob_datetime->format('Y-m-d');
  3265.                     }
  3266.                 }
  3267.  
  3268.                 if ($field['use_func_val']) {
  3269.                     $val = $field['validate_function']($val);
  3270.                 }
  3271.  
  3272.                 if (!empty($field['db_field'])) {
  3273.                     $query_field[] = $field['db_field'];
  3274.                 }
  3275.  
  3276.                 if (is_array($val)) {
  3277.                     $new_val = '';
  3278.                     foreach ($val as $v) {
  3279.                         $new_val .= '#' . $v . '# ';
  3280.                     }
  3281.                     $val = $new_val;
  3282.                 }
  3283.                 if (!$field['clean_func'] || (!function_exists($field['clean_func']) && !is_array($field['clean_func']))) {
  3284.                     $val = mysql_clean($val);
  3285.                 } else {
  3286.                     $val = apply_func($field['clean_func'], mysql_clean('|no_mc|' . $val));
  3287.                 }
  3288.  
  3289.                 if (!empty($field['db_field'])) {
  3290.                     $query_val[] = $val;
  3291.                 }
  3292.             }
  3293.  
  3294.             // Setting Verification type
  3295.             if (EMAIL_VERIFICATION == '1') {
  3296.                 $usr_status = 'ToActivate';
  3297.                 $welcome_email = 'no';
  3298.             } else {
  3299.                 $usr_status = 'Ok';
  3300.                 $welcome_email = 'yes';
  3301.             }
  3302.  
  3303.             if (has_access('admin_access', true)) {
  3304.                 if ($array['active'] == 'Ok') {
  3305.                     $usr_status = 'Ok';
  3306.                     $welcome_email = 'yes';
  3307.                 } else {
  3308.                     $usr_status = 'ToActivate';
  3309.                     $welcome_email = 'no';
  3310.                 }
  3311.  
  3312.                 $query_field[] = 'level';
  3313.                 $query_val[] = $array['level'];
  3314.             }
  3315.             global $Upload;
  3316.             $custom_fields_array = $Upload->load_custom_form_fields(false, false, false, true);
  3317.             foreach ($custom_fields_array as $cfield) {
  3318.                 $db_field = $cfield['db_field'];
  3319.                 $query_field[] = $db_field;
  3320.                 $query_val[] = $array[$db_field];
  3321.             }
  3322.  
  3323.             $query_field[] = 'usr_status';
  3324.             $query_val[] = $usr_status;
  3325.  
  3326.             $query_field[] = 'welcome_email_sent';
  3327.             $query_val[] = $welcome_email;
  3328.  
  3329.             //Creating AV Code
  3330.             $avcode = RandomString(10);
  3331.             $query_field[] = 'avcode';
  3332.             $query_val[] = $avcode;
  3333.  
  3334.             //Signup IP
  3335.             $signup_ip = $_SERVER['REMOTE_ADDR'];
  3336.             $query_field[] = 'signup_ip';
  3337.             $query_val[] = $signup_ip;
  3338.  
  3339.             //Date Joined
  3340.             $now = NOW();
  3341.             $query_field[] = 'doj';
  3342.             $query_val[] = $now;
  3343.  
  3344.             // Featured video
  3345.             $query_field[] = 'featured_video';
  3346.             $query_val[] = '';
  3347.  
  3348.             /**
  3349.              * A VERY IMPORTANT PART OF
  3350.              * OUR SIGNUP SYSTEM IS
  3351.              * SESSION KEY AND CODE
  3352.              * WHEN A USER IS LOGGED IN
  3353.              * IT IS ONLY VALIDATED BY
  3354.              * ITS SIGNUP KEY AND CODE
  3355.              */
  3356.             $sess_key = $this->create_session_key($_COOKIE['PHPSESSID'], $array['password']);
  3357.             $sess_code = $this->create_session_code();
  3358.  
  3359.             $query_field[] = 'user_session_key';
  3360.             $query_val[] = $sess_key;
  3361.  
  3362.             $query_field[] = 'user_session_code';
  3363.             $query_val[] = $sess_code;
  3364.  
  3365.             $query = 'INSERT INTO ' . tbl('users') . ' (';
  3366.             $total_fields = count($query_field);
  3367.  
  3368.             //Adding Fields to query
  3369.             $i = 0;
  3370.             foreach ($query_field as $qfield) {
  3371.                 $i++;
  3372.                 $query .= $qfield;
  3373.                 if ($i < $total_fields) {
  3374.                     $query .= ',';
  3375.                 }
  3376.             }
  3377.  
  3378.             $query .= ') VALUES (';
  3379.  
  3380.             $i = 0;
  3381.             //Adding Fields Values to query
  3382.             foreach ($query_val as $qval) {
  3383.                 $i++;
  3384.                 $query .= '\'' . $qval . '\'';
  3385.                 if ($i < $total_fields) {
  3386.                     $query .= ',';
  3387.                 }
  3388.             }
  3389.  
  3390.             //Finalizing Query
  3391.             $query .= ')';
  3392.             $db->execute($query);
  3393.             $insert_id = $db->insert_id();
  3394.  
  3395.             $db->update(tbl($this->dbtbl['users']), ['password'], [pass_code($array['password'], $insert_id)], ' userid=\'' . $insert_id . '\'');
  3396.  
  3397.             $fields_list = [];
  3398.             $fields_data = [];
  3399.  
  3400.             $fields_list[] = 'userid';
  3401.             $fields_data[] = $insert_id;
  3402.  
  3403.             // Specify default values for user_profile fields without one
  3404.             $fields_list[] = 'profile_title';
  3405.             $fields_data[] = '';
  3406.             $fields_list[] = 'profile_desc';
  3407.             $fields_data[] = '';
  3408.             $fields_list[] = 'featured_video';
  3409.             $fields_data[] = '';
  3410.             $fields_list[] = 'about_me';
  3411.             $fields_data[] = '';
  3412.             $fields_list[] = 'schools';
  3413.             $fields_data[] = '';
  3414.             $fields_list[] = 'occupation';
  3415.             $fields_data[] = '';
  3416.             $fields_list[] = 'companies';
  3417.             $fields_data[] = '';
  3418.             $fields_list[] = 'hobbies';
  3419.             $fields_data[] = '';
  3420.             $fields_list[] = 'fav_movies';
  3421.             $fields_data[] = '';
  3422.             $fields_list[] = 'fav_music';
  3423.             $fields_data[] = '';
  3424.             $fields_list[] = 'fav_books';
  3425.             $fields_data[] = '';
  3426.             $fields_list[] = 'background';
  3427.             $fields_data[] = '';
  3428.             $fields_list[] = 'voters';
  3429.             $fields_data[] = '';
  3430.  
  3431.             $db->insert(tbl($userquery->dbtbl['user_profile']), $fields_list, $fields_data);
  3432.  
  3433.             if (!has_access('admin_access', true) && EMAIL_VERIFICATION && $send_signup_email) {
  3434.                 global $cbemail;
  3435.                 $tpl = $cbemail->get_template('email_verify_template');
  3436.                 $more_var = [
  3437.                     '{username}' => post('username'),
  3438.                     '{password}' => post('password'),
  3439.                     '{email}'    => post('email'),
  3440.                     '{avcode}'   => $avcode
  3441.                 ];
  3442.  
  3443.                 $var = [];
  3444.                 $var = array_merge($more_var, $var);
  3445.                 $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  3446.                 $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  3447.  
  3448.                 //Now Finally Sending Email
  3449.                 cbmail(['to' => post('email'), 'from' => WEBSITE_EMAIL, 'subject' => $subj, 'content' => $msg]);
  3450.             } elseif (!has_access('admin_access', true) && $send_signup_email) {
  3451.                 $this->send_welcome_email($insert_id);
  3452.             }
  3453.  
  3454.             $log_array = [
  3455.                 'username'  => $array['username'],
  3456.                 'userid'    => $insert_id,
  3457.                 'userlevel' => $array['level'],
  3458.                 'useremail' => $array['email'],
  3459.                 'success'   => 'yes',
  3460.                 'details'   => sprintf('%s signed up', $array['username'])
  3461.             ];
  3462.  
  3463.             //Login Signup
  3464.             insert_log('signup', $log_array);
  3465.  
  3466.             //Adding User has Signup Feed
  3467.             addFeed(['action' => 'signup', 'object_id' => $insert_id, 'object' => 'signup', 'uid' => $insert_id]);
  3468.  
  3469.             return $insert_id;
  3470.         }
  3471.         return false;
  3472.     }
  3473.  
  3474.     function duplicate_email($name): bool
  3475.     {
  3476.         $myquery = new myquery();
  3477.         if ($myquery->check_email($name)) {
  3478.             return true;
  3479.         }
  3480.         return false;
  3481.     }
  3482.  
  3483.     /**
  3484.      * Function used to get users
  3485.      *
  3486.      * @param null $params
  3487.      * @param bool $force_admin
  3488.      *
  3489.      * @return array|bool|int|void
  3490.      * @throws Exception
  3491.      */
  3492.     function get_users($params = null, $force_admin = false)
  3493.     {
  3494.         global $db;
  3495.  
  3496.         $limit = $params['limit'];
  3497.         $order = $params['order'];
  3498.  
  3499.         $cond = '';
  3500.         if (!has_access('admin_access', true) && !$force_admin) {
  3501.             $cond .= " users.usr_status='Ok' AND users.ban_status ='no' ";
  3502.         } else {
  3503.             if (!empty($params['ban'])) {
  3504.                 $cond .= " users.ban_status ='" . $params['ban'] . "'";
  3505.             }
  3506.  
  3507.             if (!empty($params['status'])) {
  3508.                 if ($cond != '') {
  3509.                     $cond .= ' AND';
  3510.                 }
  3511.                 $cond .= " users.usr_status='" . $params['status'] . "'";
  3512.             }
  3513.         }
  3514.  
  3515.         //Setting Category Condition
  3516.         if (!empty($params['category']) && !is_array($params['category'])) {
  3517.             $is_all = strtolower($params['category']);
  3518.         }
  3519.  
  3520.         if (isset($params['category']) && $params['category'] != '' && $is_all != lang('all')) {
  3521.             if ($cond != '') {
  3522.                 $cond .= ' AND';
  3523.             }
  3524.  
  3525.             $cond .= ' (';
  3526.  
  3527.             if (!empty($params['category']) && !is_array($params['category'])) {
  3528.                 $cats = explode(',', $params['category']);
  3529.             } else {
  3530.                 $cats = $params['category'];
  3531.             }
  3532.  
  3533.             $count = 0;
  3534.             foreach ($cats as $cat_params) {
  3535.                 $count++;
  3536.                 if ($count > 1) {
  3537.                     $cond .= ' OR ';
  3538.                 }
  3539.                 $cond .= " users.category LIKE '%$cat_params%' ";
  3540.             }
  3541.  
  3542.             $cond .= ')';
  3543.         }
  3544.  
  3545.         //date span
  3546.         if (!empty($params['date_span'])) {
  3547.             if ($cond != '') {
  3548.                 $cond .= ' AND';
  3549.             }
  3550.             $cond .= ' ' . cbsearch::date_margin('users.doj', $params['date_span']);
  3551.         }
  3552.  
  3553.         //FEATURED
  3554.         if (!empty($params['featured'])) {
  3555.             if ($cond != '') {
  3556.                 $cond .= ' AND';
  3557.             }
  3558.             $cond .= " users.featured = '" . $params['featured'] . "' ";
  3559.         }
  3560.  
  3561.         if (!empty($params['search_username'])) {
  3562.             if ($cond != '') {
  3563.                 $cond .= ' AND ';
  3564.             }
  3565.             $cond .= " users.username LIKE '%" . $params['search_username'] . "%'";
  3566.         }
  3567.  
  3568.         //Username
  3569.         if (isset($params['username']) && $params['username'] != '') {
  3570.             if ($cond != '') {
  3571.                 $cond .= ' AND';
  3572.             }
  3573.             $cond .= " users.username = '" . $params['username'] . "' ";
  3574.         }
  3575.  
  3576.         //Email
  3577.         if (isset($params['email']) && $params['email'] != '') {
  3578.             if ($cond != '') {
  3579.                 $cond .= ' AND';
  3580.             }
  3581.             $cond .= " users.email = '" . $params['email'] . "' ";
  3582.         }
  3583.  
  3584.         //Exclude Users
  3585.         if (!empty($params['exclude'])) {
  3586.             if ($cond != '') {
  3587.                 $cond .= ' AND';
  3588.             }
  3589.             $cond .= " users.userid <> '" . $params['exclude'] . "' ";
  3590.         }
  3591.  
  3592.         //Getting specific User
  3593.         if (isset($params['userid']) && $params['userid'] != '') {
  3594.             if ($cond != '') {
  3595.                 $cond .= ' AND';
  3596.             }
  3597.             $cond .= " users.userid = '" . $params['userid'] . "' ";
  3598.         }
  3599.  
  3600.         //Sex
  3601.         if (!empty($params['gender'])) {
  3602.             if ($cond != '') {
  3603.                 $cond .= ' AND';
  3604.             }
  3605.             $cond .= " users.sex = '" . $params['gender'] . "' ";
  3606.         }
  3607.  
  3608.         //Level
  3609.         if (isset($params['level']) && $params['level'] != '') {
  3610.             if ($cond != '') {
  3611.                 $cond .= ' AND';
  3612.             }
  3613.             $cond .= " users.level = '" . $params['level'] . "' ";
  3614.         }
  3615.  
  3616.         if (!empty($params['cond'])) {
  3617.             if ($cond != '') {
  3618.                 $cond .= ' AND';
  3619.             }
  3620.             $cond .= ' ' . $params['cond'] . ' ';
  3621.         }
  3622.  
  3623.         if (empty($params['count_only'])) {
  3624.             $fields = [
  3625.                 'users'   => get_user_fields(),
  3626.                 'profile' => ['rating', 'rated_by', 'voters', 'first_name', 'last_name', 'profile_title', 'profile_desc', 'city', 'hometown']
  3627.             ];
  3628.             $fields['users'][] = 'last_active';
  3629.             $fields['users'][] = 'total_collections';
  3630.             $fields['users'][] = 'total_groups';
  3631.             $query = ' SELECT ' . table_fields($fields) . ' FROM ' . tbl('users') . ' AS users ';
  3632.             $query .= ' LEFT JOIN ' . cb_sql_table('user_profile', 'profile') . ' ON users.userid = profile.userid ';
  3633.  
  3634.             if ($cond) {
  3635.                 $query .= ' WHERE ' . $cond;
  3636.             }
  3637.  
  3638.             if ($order) {
  3639.                 $query .= ' ORDER BY ' . $order;
  3640.             }
  3641.  
  3642.             if ($limit) {
  3643.                 $query .= ' LIMIT ' . $limit;
  3644.             }
  3645.  
  3646.             $result = select($query);
  3647.         } else {
  3648.             $result = $db->count(tbl('users') . ' AS users ', 'userid', $cond);
  3649.         }
  3650.  
  3651.         if (isset($params['assign']) && $params['assign'] != '') {
  3652.             assign($params['assign'], $result);
  3653.         } else {
  3654.             return $result ?? false;
  3655.         }
  3656.     }
  3657.  
  3658.     /**
  3659.      * Function used to perform several actions with a video
  3660.      */
  3661.     function action($case, $uid)
  3662.     {
  3663.         global $db;
  3664.         $udetails = $this->get_user_details(user_id());
  3665.         $logged_user_level = $udetails['level'];
  3666.         if ($logged_user_level > 1) {
  3667.             $data = $this->get_user_details($uid);
  3668.             if ($data['level'] == 1) {
  3669.                 e('You do not have sufficient permissions to edit an Admininstrator');
  3670.                 return false;
  3671.             }
  3672.         }
  3673.  
  3674.         if (!$this->user_exists($uid)) {
  3675.             return false;
  3676.         }
  3677.         //Lets just check weathter user exists or not
  3678.         $tbl = tbl($this->dbtbl['users']);
  3679.         switch ($case) {
  3680.             //Activating a user
  3681.             case 'activate':
  3682.             case 'av':
  3683.             case 'a':
  3684.                 $avcode = RandomString(10);
  3685.                 $db->update($tbl, ['usr_status', 'avcode'], ['Ok', $avcode], " userid='$uid' ");
  3686.                 e(lang('usr_ac_msg'), 'm');
  3687.                 break;
  3688.  
  3689.             //Deactivating a user
  3690.             case 'deactivate':
  3691.             case 'dav':
  3692.             case 'd':
  3693.                 $avcode = RandomString(10);
  3694.                 $db->update($tbl, ['usr_status', 'avcode'], ['ToActivate', $avcode], " userid='$uid' ");
  3695.                 e(lang('usr_dac_msg'), 'm');
  3696.                 break;
  3697.  
  3698.             //Featuring user
  3699.             case 'feature':
  3700.             case 'featured':
  3701.             case 'f':
  3702.                 $db->update($tbl, ['featured', 'featured_date'], ['yes', now()], " userid='$uid' ");
  3703.                 e(lang('User has been set as featured'), 'm');
  3704.                 break;
  3705.  
  3706.             //Unfeatured user
  3707.             case 'unfeature':
  3708.             case 'unfeatured':
  3709.             case 'uf':
  3710.                 $db->update($tbl, ['featured'], ['no'], " userid='$uid' ");
  3711.                 e(lang('User has been removed from featured users'), 'm');
  3712.                 break;
  3713.  
  3714.             //Ban User
  3715.             case 'ban':
  3716.             case 'banned':
  3717.                 $db->update($tbl, ['ban_status'], ['yes'], " userid='$uid' ");
  3718.                 e(lang('usr_uban_msg'), 'm');
  3719.                 break;
  3720.  
  3721.             //Ban User
  3722.             case 'unban':
  3723.             case 'unbanned':
  3724.                 $db->update($tbl, ['ban_status'], ['no'], " userid='$uid' ");
  3725.                 e(lang('usr_uuban_msg'), 'm');
  3726.                 break;
  3727.         }
  3728.     }
  3729.  
  3730.     /**
  3731.      * Function used to use to initialize search object for video section
  3732.      * op=>operator (AND OR)
  3733.      */
  3734.     function init_search()
  3735.     {
  3736.         $this->search = new cbsearch;
  3737.         $this->search->db_tbl = 'users';
  3738.         // added more conditions usr_status='Ok' and ban_status='no'
  3739.         /*
  3740.         array('field'=>'usr_status','type'=>'=','var'=>'Ok','op'=>'AND','value'=>'static'),
  3741.             array('field'=>'ban_status','type'=>'=','var'=>'no','op'=>'AND','value'=>'static'),
  3742.         */
  3743.         if (!has_access('admin_access', true)) {
  3744.             $this->search->columns = [
  3745.                 ['field' => 'username', 'type' => 'LIKE', 'var' => '%{KEY}%'],
  3746.                 ['field' => 'usr_status', 'type' => '=', 'var' => 'Ok', 'op' => 'AND', 'value' => 'static'],
  3747.                 ['field' => 'ban_status', 'type' => '=', 'var' => 'no', 'op' => 'AND', 'value' => 'static']
  3748.             ];
  3749.         } else {
  3750.             $this->search->columns = [
  3751.                 ['field' => 'username', 'type' => 'LIKE', 'var' => '%{KEY}%'],
  3752.             ];
  3753.         }
  3754.         $version = get_current_version();
  3755.         if ($version['version'] > '5.5.0' || ($version['version'] == '5.5.0' && $version['revision'] >= 264)) {
  3756.             $this->search->columns[] = ['field' => 'name', 'type' => 'LIKE', 'var' => '%{KEY}%', 'op' => 'OR', 'db'=>'tags'];
  3757.         }
  3758.  
  3759.         $this->search->cat_tbl = $this->cat_tbl;
  3760.  
  3761.         $this->search->display_template = LAYOUT . '/blocks/user.html';
  3762.         $this->search->template_var = 'user';
  3763.         $this->search->multi_cat = false;
  3764.         $this->search->date_added_colum = 'doj';
  3765.         $this->search->results_per_page = config('users_items_search_page');
  3766.  
  3767.         /**
  3768.          * Setting up the sorting thing
  3769.          */
  3770.  
  3771.         $sorting = [
  3772.             'doj'            => lang('date_added'),
  3773.             'profile_hits'   => lang('views'),
  3774.             'total_comments' => lang('comments'),
  3775.             'total_videos'   => lang('videos')
  3776.         ];
  3777.  
  3778.         $this->search->sorting = [
  3779.             'doj'            => ' doj DESC',
  3780.             'profile_hits'   => ' profile_hits DESC',
  3781.             'total_comments' => ' total_comments DESC',
  3782.             'total_videos'   => ' total_videos DESC'
  3783.         ];
  3784.  
  3785.         /**
  3786.          * Setting Up The Search Fields
  3787.          */
  3788.         $default = $_GET;
  3789.         if (is_array($default['category'])) {
  3790.             $cat_array = [$default['category']];
  3791.         }
  3792.         $uploaded = $default['datemargin'];
  3793.         $sort = $default['sort'];
  3794.  
  3795.         $this->search->search_type['channels'] = ['title' => lang('users')];
  3796.  
  3797.         $fields = [
  3798.             'query'       => [
  3799.                 'title' => lang('keywords'),
  3800.                 'type'  => 'textfield',
  3801.                 'name'  => 'query',
  3802.                 'id'    => 'query',
  3803.                 'value' => mysql_clean($default['query'])
  3804.             ],
  3805.             'category'    => [
  3806.                 'title'         => lang('category'),
  3807.                 'type'          => 'checkbox',
  3808.                 'name'          => 'category[]',
  3809.                 'id'            => 'category',
  3810.                 'value'         => ['category', $cat_array],
  3811.                 'category_type' => 'user'
  3812.             ],
  3813.             'date_margin' => [
  3814.                 'title'   => lang('joined'),
  3815.                 'type'    => 'dropdown',
  3816.                 'name'    => 'datemargin',
  3817.                 'id'      => 'datemargin',
  3818.                 'value'   => $this->search->date_margins(),
  3819.                 'checked' => $uploaded
  3820.             ],
  3821.             'sort'        => [
  3822.                 'title'   => lang('sort_by'),
  3823.                 'type'    => 'dropdown',
  3824.                 'name'    => 'sort',
  3825.                 'value'   => $sorting,
  3826.                 'checked' => $sort
  3827.             ]
  3828.         ];
  3829.  
  3830.         $this->search->search_type['users']['fields'] = $fields;
  3831.     }
  3832.  
  3833.     /**
  3834.      * Function used to get number of users online
  3835.      *
  3836.      * @param bool $group
  3837.      * @param bool $count
  3838.      *
  3839.      * @return array|bool
  3840.      * @throws Exception
  3841.      */
  3842.     function get_online_users($group = true, $count = false)
  3843.     {
  3844.         global $db;
  3845.  
  3846.         if ($group) {
  3847.             $results = $db->select(tbl('sessions') . ' LEFT JOIN (' . tbl('users') . ") ON
  3848.             (" . tbl('sessions.session_user=') . tbl('users') . '.userid)',
  3849.                 tbl('sessions.*,users.username,users.userid,users.email') . ',count(' . tbl('sessions.session_user') . ') AS logins'
  3850.                 , ' TIMESTAMPDIFF(MINUTE,' . tbl('sessions.last_active') . ",'" . NOW() . "')  < 6 GROUP BY " . tbl('users.userid'));
  3851.         } else {
  3852.             if ($count) {
  3853.                 $results = $db->count(tbl('sessions') . ' LEFT JOIN (' . tbl('users') . ') ON
  3854.                 (' . tbl('sessions.session_user=') . tbl('users') . '.userid)',
  3855.                     tbl('sessions.session_id')
  3856.                     , ' TIMESTAMPDIFF(MINUTE,' . tbl('sessions.last_active') . ",'" . NOW() . "')  < 6 ");
  3857.             } else {
  3858.                 $results = $db->select(tbl('sessions') . ' LEFT JOIN (' . tbl('users') . ') ON
  3859.                 (' . tbl('sessions.session_user=') . tbl('users') . '.userid)',
  3860.                     tbl('sessions.*,users.username,users.userid,users.email')
  3861.                     , ' TIMESTAMPDIFF(MINUTE,' . tbl('sessions.last_active') . ",'" . NOW() . "')  < 6 ");
  3862.             }
  3863.         }
  3864.  
  3865.         return $results;
  3866.     }
  3867.  
  3868.     /**
  3869.      * Function will let admin to login as user
  3870.      *
  3871.      * @param      $id
  3872.      * @param bool $realtime
  3873.      *
  3874.      * @return bool
  3875.      * @throws Exception
  3876.      */
  3877.     function login_as_user($id, $realtime = false): bool
  3878.     {
  3879.         global $sess, $db;
  3880.         $udetails = $this->get_user_details($id);
  3881.         if ($udetails) {
  3882.             if (!$realtime) {
  3883.                 $sess->set('dummy_sess_salt', $sess->get('sess_salt'));
  3884.                 $sess->set('dummy_PHPSESSID', $sess->get('PHPSESSID'));
  3885.                 $sess->set('dummy_userid', user_id());
  3886.                 $sess->set('dummy_user_session_key', $this->udetails['user_session_key']);
  3887.  
  3888.                 $userid = $udetails['userid'];
  3889.                 $session_salt = RandomString(5);
  3890.                 $sess->set('sess_salt', $session_salt);
  3891.                 $sess->set('PHPSESSID', $sess->id);
  3892.  
  3893.                 $smart_sess = md5($udetails['user_session_key'] . $session_salt);
  3894.  
  3895.                 $db->delete(tbl('sessions'), ['session'], [$sess->id]);
  3896.                 $sess->add_session($userid, 'smart_sess', $smart_sess);
  3897.             } else {
  3898.                 if ($this->login_check(null, true)) {
  3899.                     $msg[] = e(lang('you_already_logged'));
  3900.                 } elseif (!$this->user_exists($udetails['username'])) {
  3901.                     $msg[] = e(lang('user_doesnt_exist'));
  3902.                 } elseif (strtolower($udetails['usr_status']) != 'ok') {
  3903.                     $msg[] = e(lang('user_inactive_msg'), 'e', false);
  3904.                 } elseif ($udetails['ban_status'] == 'yes') {
  3905.                     $msg[] = e(lang('usr_ban_err'));
  3906.                 } else {
  3907.                     $userid = $udetails['userid'];
  3908.                     $log_array['userid'] = $userid;
  3909.                     $log_array['useremail'] = $udetails['email'];
  3910.                     $log_array['success'] = 'yes';
  3911.                     $log_array['level'] = $udetails['level'];
  3912.  
  3913.                     //Starting special sessions for security
  3914.                     $session_salt = RandomString(5);
  3915.                     $sess->set('sess_salt', $session_salt);
  3916.                     $sess->set('PHPSESSID', $sess->id);
  3917.  
  3918.                     $smart_sess = md5($udetails['user_session_key'] . $session_salt);
  3919.  
  3920.                     $db->delete(tbl('sessions'), ['session', 'session_string'], [$sess->id, 'guest']);
  3921.                     $sess->add_session($userid, 'smart_sess', $smart_sess);
  3922.  
  3923.                     //Setting Vars
  3924.                     $this->userid = $udetails['userid'];
  3925.                     $this->username = $udetails['username'];
  3926.                     $this->level = $udetails['level'];
  3927.  
  3928.                     //Updating User last login , num of visits and ip
  3929.                     $db->update(tbl('users'),
  3930.                         ['num_visits', 'last_logged', 'ip'],
  3931.                         ['|f|num_visits+1', NOW(), $_SERVER['REMOTE_ADDR']],
  3932.                         'userid=\'' . $userid . '\''
  3933.                     );
  3934.  
  3935.                     $this->init();
  3936.                     //Logging Action
  3937.                     insert_log('Login as', $log_array);
  3938.                     return true;
  3939.                 }
  3940.  
  3941.                 //Error Logging
  3942.                 if (!empty($msg)) {
  3943.                     //Logging Action
  3944.                     $log_array['success'] = 'no';
  3945.                     $log_array['details'] = $msg[0]['val'];
  3946.                     insert_log('Login as', $log_array);
  3947.                 }
  3948.             }
  3949.  
  3950.             return true;
  3951.         }
  3952.  
  3953.         e(lang('usr_exist_err'));
  3954.         return false;
  3955.     }
  3956.  
  3957.     /**
  3958.      * Function used to revert back to admin
  3959.      */
  3960.     function revert_from_user()
  3961.     {
  3962.         global $sess, $db;
  3963.         if ($this->is_admin_logged_as_user()) {
  3964.             $userid = $sess->get('dummy_userid');
  3965.             $session_salt = $sess->get('dummy_sess_salt');
  3966.             $user_session_key = $sess->get('dummy_user_session_key');
  3967.             $smart_sess = md5($user_session_key . $session_salt);
  3968.  
  3969.             $sess->set('sess_salt', $session_salt);
  3970.             $sess->set('PHPSESSID', $sess->get('dummy_PHPSESSID'));
  3971.  
  3972.             $db->delete(tbl('sessions'), ['session'], [$sess->get('dummy_PHPSESSID')]);
  3973.             $sess->add_session($userid, 'smart_sess', $smart_sess);
  3974.  
  3975.             $sess->set('dummy_sess_salt', '');
  3976.             $sess->set('dummy_PHPSESSID', '');
  3977.             $sess->set('dummy_userid', '');
  3978.             $sess->set('dummy_user_session_key', '');
  3979.         }
  3980.     }
  3981.  
  3982.     /**
  3983.      * Function used to check weather user is logged in as admin or not
  3984.      */
  3985.     function is_admin_logged_as_user(): bool
  3986.     {
  3987.         global $sess;
  3988.         if ($sess->get('dummy_sess_salt') != '') {
  3989.             return true;
  3990.         }
  3991.         return false;
  3992.     }
  3993.  
  3994.     /**
  3995.      * Function used to get anonymous user
  3996.      */
  3997.     function get_anonymous_user()
  3998.     {
  3999.         global $db;
  4000.         /*Added to resolve bug 222*/
  4001.         $result = $db->select(tbl('users'), 'userid', " username='anonymous%' AND email='anonymous%'", '1');
  4002.         if ($result[0]['userid']) {
  4003.             return $result[0]['userid'];
  4004.         }
  4005.  
  4006.         $result = $db->select(tbl('users'), 'userid', " level='6' AND usr_status='ToActivate' ", '1');
  4007.         if ($result[0]['userid']) {
  4008.             return $result[0]['userid'];
  4009.         }
  4010.  
  4011.         $pass = RandomString(10);
  4012.  
  4013.         if ($_SERVER['HTTP_HOST'] != 'localhost' && $_SERVER['HTTP_HOST'] != '127.0.0.1') {
  4014.             $email = 'anonymous' . RandomString(5) . '@' . $_SERVER['HTTP_HOST'];
  4015.         } else {
  4016.             $email = 'anonymous' . RandomString(5) . '@' . $_SERVER['HTTP_HOST'] . '.tld';
  4017.         }
  4018.  
  4019.         //Create Anonymous user
  4020.         return $this->signup_user(
  4021.             [
  4022.                 'username'  => 'anonymous' . RandomString(5),
  4023.                 'email'     => $email,
  4024.                 'password'  => $pass,
  4025.                 'cpassword' => $pass,
  4026.                 'country'   => config('default_country_iso2'),
  4027.                 'gender'    => 'Male',
  4028.                 'dob'       => '2000-10-10',
  4029.                 'category'  => '1',
  4030.                 'level'     => '6',
  4031.                 'active'    => 'yes',
  4032.                 'agree'     => 'yes'
  4033.             ], false);
  4034.     }
  4035.  
  4036.     /**
  4037.      * Function used to delete user videos
  4038.      *
  4039.      * @param $uid
  4040.      * @throws Exception
  4041.      */
  4042.     function delete_user_vids($uid)
  4043.     {
  4044.         global $cbvid, $eh;
  4045.         $vids = get_videos(['user' => $uid]);
  4046.         if (is_array($vids)) {
  4047.             foreach ($vids as $vid) {
  4048.                 $cbvid->delete_video($vid['videoid']);
  4049.             }
  4050.         }
  4051.         $eh->flush_msg();
  4052.         e(lang('user_vids_hv_deleted'), 'm');
  4053.     }
  4054.  
  4055.     /**
  4056.      * Function used to remove user contacts
  4057.      *
  4058.      * @param $uid
  4059.      * @throws Exception
  4060.      */
  4061.     function remove_contacts($uid)
  4062.     {
  4063.         global $eh;
  4064.         $contacts = $this->get_contacts($uid);
  4065.         if (is_array($contacts)) {
  4066.             foreach ($contacts as $contact) {
  4067.                 $this->remove_contact($contact['userid'], $contact['contact_userid']);
  4068.             }
  4069.         }
  4070.         $eh->flush_msg();
  4071.         e(lang('user_contacts_hv_removed'), 'm');
  4072.     }
  4073.  
  4074.     /**
  4075.      * Function used to remove user private messages
  4076.      *
  4077.      * @param        $uid
  4078.      * @param string $box
  4079.      * @throws Exception
  4080.      */
  4081.     function remove_user_pms($uid, $box = 'both')
  4082.     {
  4083.         global $cbpm, $eh;
  4084.  
  4085.         if ($box == 'inbox' || $box == 'both') {
  4086.             $inboxs = $cbpm->get_user_inbox_messages($uid);
  4087.             if (is_array($inboxs)) {
  4088.                 foreach ($inboxs as $inbox) {
  4089.                     $cbpm->delete_msg($inbox['message_id'], $uid);
  4090.                 }
  4091.             }
  4092.             $eh->flush_msg();
  4093.             e(lang('all_user_inbox_deleted'), 'm');
  4094.         }
  4095.  
  4096.         if ($box == 'sent' || $box == 'both') {
  4097.             $outs = $cbpm->get_user_outbox_messages($uid);
  4098.             if (is_array($outs)) {
  4099.                 foreach ($outs as $out) {
  4100.                     $cbpm->delete_msg($out['message_id'], $uid, 'out');
  4101.                 }
  4102.             }
  4103.             $eh->flush_msg();
  4104.             e(lang('all_user_sent_messages_deleted'), 'm');
  4105.         }
  4106.     }
  4107.  
  4108.     /**
  4109.      * This will get user subscriptions
  4110.      * uploaded videos and photos
  4111.      * This is a test function
  4112.      *
  4113.      * @param        $uid
  4114.      * @param int $limit
  4115.      * @param string $uploadsType
  4116.      * @param string $uploadsTimeSpan
  4117.      *
  4118.      * @return bool|array
  4119.      * @throws Exception
  4120.      */
  4121.     function getSubscriptionsUploadsWeek($uid, $limit = 20, $uploadsType = 'both', $uploadsTimeSpan = 'this_week')
  4122.     {
  4123.         $user_cond = "";
  4124.         $users = $this->get_user_subscriptions($uid);
  4125.         if ($users) {
  4126.             foreach ($users as $user) {
  4127.                 if ($user_cond) {
  4128.                     $user_cond .= ' OR ';
  4129.                 }
  4130.                 $user_cond .= tbl('users.userid') . "='" . $user[0] . "' ";
  4131.             }
  4132.             $user_cond = ' (' . $user_cond . ') ';
  4133.             global $cbphoto, $cbvideo;
  4134.             $photoCount = 1;
  4135.             $videoCount = 1;
  4136.             switch ($uploadsType) {
  4137.                 case 'both':
  4138.                 default:
  4139.                     $photos = $cbphoto->get_photos(['limit' => $limit, 'extra_cond' => $user_cond, 'order' => ' date_added DESC', 'date_span' => $uploadsTimeSpan]);
  4140.                     $videos = $cbvideo->get_videos(['limit' => $limit, 'cond' => ' AND' . $user_cond, 'order' => ' date_added DESC', 'date_span' => $uploadsTimeSpan]);
  4141.                     if (!empty($photos) && !empty($videos)) {
  4142.                         $finalResult = array_merge($videos, $photos);
  4143.                     } elseif (empty($photos) && !empty($videos)) {
  4144.                         $finalResult = array_merge($videos, []);
  4145.                     } elseif (!empty($photos) && empty($videos)) {
  4146.                         $finalResult = array_merge($photos, []);
  4147.                     }
  4148.  
  4149.                     if (!empty($finalResult)) {
  4150.                         foreach ($finalResult as $result) {
  4151.                             if ($result['videoid']) {
  4152.                                 $videoArr[] = $result;
  4153.                                 $return['videos'] = [
  4154.                                     'title' => lang('videos'),
  4155.                                     'total' => $videoCount++,
  4156.                                     'items' => $videoArr
  4157.                                 ];
  4158.                             }
  4159.  
  4160.                             if ($result['photo_id']) {
  4161.                                 $photosArr[] = $result;
  4162.                                 $return['photos'] = [
  4163.                                     'title' => lang('photos'),
  4164.                                     'total' => $photoCount++,
  4165.                                     'items' => $photosArr
  4166.                                 ];
  4167.                             }
  4168.                         }
  4169.                         return $return;
  4170.                     }
  4171.                     return false;
  4172.  
  4173.  
  4174.                 case 'photos':
  4175.                 case 'photo' :
  4176.                 case 'p':
  4177.                     $photos = $cbphoto->get_photos(['limit' => $limit, 'extra_cond' => $user_cond, 'order' => ' date_added DESC', 'date_span' => $uploadsTimeSpan]);
  4178.                     if ($photos) {
  4179.                         foreach ($photos as $photo) {
  4180.                             $photosArr[] = $photo;
  4181.                             $return['photos'] = [
  4182.                                 'title' => lang('photos'),
  4183.                                 'total' => $photoCount++,
  4184.                                 'items' => $photosArr
  4185.                             ];
  4186.                         }
  4187.                     } else {
  4188.                         return false;
  4189.                     }
  4190.                     break;
  4191.  
  4192.                 case 'videos':
  4193.                 case 'video':
  4194.                 case 'v':
  4195.                     $videos = $cbvideo->get_videos(['limit' => $limit, 'cond' => ' AND' . $user_cond, 'order' => ' date_added DESC', 'date_span' => $uploadsTimeSpan]);
  4196.                     if ($videos) {
  4197.                         foreach ($videos as $video) {
  4198.                             $videoArr[] = $video;
  4199.                             $return['videos'] = [
  4200.                                 'title' => lang('videos'),
  4201.                                 'total' => $videoCount++,
  4202.                                 'items' => $videoArr
  4203.                             ];
  4204.                         }
  4205.                     } else {
  4206.                         return false;
  4207.                     }
  4208.                     break;
  4209.             }
  4210.             return $return;
  4211.         }
  4212.     }
  4213.  
  4214.     /**
  4215.      * Function used to set item as profile item
  4216.      *
  4217.      * @param        $id
  4218.      * @param string $type
  4219.      * @param null $uid
  4220.      *
  4221.      * @return bool|void
  4222.      * @throws Exception
  4223.      */
  4224.     function setProfileItem($id, $type = 'v', $uid = null)
  4225.     {
  4226.         global $cbvid, $db, $cbphoto;
  4227.         if (!$uid) {
  4228.             $uid = user_id();
  4229.         }
  4230.  
  4231.         if (!$uid) {
  4232.             e('user_doesnt_exist');
  4233.             return false;
  4234.         }
  4235.  
  4236.         switch ($type) {
  4237.             case 'v':
  4238.                 if ($cbvid->video_exists($id)) {
  4239.                     $array['type'] = 'v';
  4240.                     $array['id'] = $id;
  4241.                     $db->update(tbl('user_profile'), ['profile_item'], ['|no_mc|' . json_encode($array)]
  4242.                         , " userid='$uid' ");
  4243.  
  4244.                     e(sprintf(lang('this_has_set_profile_item'), lang('video')), 'm');
  4245.                 } else {
  4246.                     e('class_vdo_del_err');
  4247.                 }
  4248.                 break;
  4249.  
  4250.             case 'p':
  4251.                 if ($cbphoto->photo_exists($id)) {
  4252.                     $array['type'] = 'p';
  4253.                     $array['id'] = $id;
  4254.                     $db->update(tbl('user_profile'), ['profile_item'], ['|no_mc|' . json_encode($array)]
  4255.                         , " userid='$uid' ");
  4256.  
  4257.                     e(sprintf(lang('this_has_set_profile_item'), lang('photo')), 'm');
  4258.                 } else {
  4259.                     e('photo_not_exist');
  4260.                 }
  4261.                 break;
  4262.         }
  4263.     }
  4264.  
  4265.     /**
  4266.      * Remove Profile item
  4267.      *
  4268.      * @param null $uid
  4269.      *
  4270.      * @return bool|void
  4271.      * @throws Exception
  4272.      */
  4273.     function removeProfileItem($uid = null)
  4274.     {
  4275.         global $db;
  4276.         if (!$uid) {
  4277.             $uid = user_id();
  4278.         }
  4279.  
  4280.         if (!$uid) {
  4281.             e('user_doesnt_exist');
  4282.             return false;
  4283.         }
  4284.  
  4285.         $db->update(tbl('user_profile'), ['profile_item'], [""]
  4286.             , " userid='$uid' ");
  4287.  
  4288.         e(lang('profile_item_removed'), 'm');
  4289.     }
  4290.  
  4291.     /**
  4292.      * function used to get profile item
  4293.      *
  4294.      * @param null $uid
  4295.      * @param bool $withDetails
  4296.      *
  4297.      * @return bool|mixed|STRING
  4298.      * @throws Exception
  4299.      */
  4300.     function getProfileItem($uid = null, $withDetails = false)
  4301.     {
  4302.         global $db, $cbvid, $cbphoto;
  4303.         if (!$uid) {
  4304.             $uid = user_id();
  4305.         }
  4306.  
  4307.         if (!$uid) {
  4308.             e('user_doesnt_exist');
  4309.             return false;
  4310.         }
  4311.  
  4312.         if ($uid == user_id() && $this->profileItem && !$withDetails) {
  4313.             return $this->profileItem;
  4314.         }
  4315.  
  4316.         $profileItem = $db->select(tbl('user_profile'), 'profile_item', " userid='$uid'");
  4317.         $profileItem = $profileItem[0]['profile_item'];
  4318.  
  4319.         $profileItem = json_decode($profileItem, true);
  4320.  
  4321.         if ($withDetails) {
  4322.             switch ($profileItem['type']) {
  4323.                 case 'p':
  4324.                     $photo = $cbphoto->get_photo($profileItem['id']);
  4325.                     $photo['type'] = 'p';
  4326.                     if ($photo) {
  4327.                         return $photo;
  4328.                     }
  4329.                     break;
  4330.  
  4331.                 case 'v':
  4332.                     $video = $cbvid->get_video($profileItem['id']);
  4333.                     $video['type'] = 'v';
  4334.                     if ($video) {
  4335.                         return $video;
  4336.                     }
  4337.                     break;
  4338.             }
  4339.         }
  4340.         return $this->profileItem = $profileItem;
  4341.     }
  4342.  
  4343.     /**
  4344.      * Function used to check weather input given item
  4345.      * is profile item or not
  4346.      *
  4347.      * @param        $id
  4348.      * @param string $type
  4349.      * @param null $uid
  4350.      *
  4351.      * @return bool
  4352.      * @throws Exception
  4353.      */
  4354.     function isProfileItem($id, $type = 'v', $uid = null): bool
  4355.     {
  4356.         $profileItem = $this->getProfileItem($uid);
  4357.  
  4358.         if ($profileItem['type'] == $type && $profileItem['id'] == $id) {
  4359.             return true;
  4360.         }
  4361.         return false;
  4362.     }
  4363.  
  4364.     /**
  4365.      * FUnction loading personal details
  4366.      *
  4367.      * @param $default
  4368.      *
  4369.      * @return array
  4370.      * @throws Exception
  4371.      */
  4372.     function load_personal_details($default): array
  4373.     {
  4374.         if (!$default) {
  4375.             $default = $_POST;
  4376.         }
  4377.  
  4378.         return [
  4379.             'first_name'      => [
  4380.                 'title'       => lang('user_fname'),
  4381.                 'type'        => 'textfield',
  4382.                 'name'        => 'first_name',
  4383.                 'id'          => 'first_name',
  4384.                 'value'       => $default['first_name'],
  4385.                 'db_field'    => 'first_name',
  4386.                 'required'    => 'no',
  4387.                 'syntax_type' => 'name',
  4388.                 'auto_view'   => 'yes'
  4389.             ],
  4390.             'last_name'       => [
  4391.                 'title'       => lang('user_lname'),
  4392.                 'type'        => 'textfield',
  4393.                 'name'        => 'last_name',
  4394.                 'id'          => 'last_name',
  4395.                 'value'       => $default['last_name'],
  4396.                 'db_field'    => 'last_name',
  4397.                 'syntax_type' => 'name',
  4398.                 'auto_view'   => 'yes'
  4399.             ],
  4400.             'relation_status' => [
  4401.                 'title'     => lang('user_relat_status'),
  4402.                 'type'      => 'dropdown',
  4403.                 'name'      => 'relation_status',
  4404.                 'id'        => 'last_name',
  4405.                 'value'     => [
  4406.                     lang('usr_arr_no_ans'),
  4407.                     lang('usr_arr_single'),
  4408.                     lang('usr_arr_married'),
  4409.                     lang('usr_arr_comitted'),
  4410.                     lang('usr_arr_open_relate')
  4411.                 ],
  4412.                 'checked'   => $default['relation_status'],
  4413.                 'db_field'  => 'relation_status',
  4414.                 'auto_view' => 'yes'
  4415.             ],
  4416.             'show_dob'        => [
  4417.                 'title'       => lang('show_dob'),
  4418.                 'type'        => 'radiobutton',
  4419.                 'name'        => 'show_dob',
  4420.                 'id'          => 'show_dob',
  4421.                 'value'       => ['yes' => lang('yes'), 'no' => lang('no')],
  4422.                 'checked'     => $default['show_dob'],
  4423.                 'db_field'    => 'show_dob',
  4424.                 'syntax_type' => 'name',
  4425.                 'auto_view'   => 'no',
  4426.                 'sep'         => '&nbsp;'
  4427.             ],
  4428.             'about_me'        => [
  4429.                 'title'      => lang('user_about_me'),
  4430.                 'type'       => 'textarea',
  4431.                 'name'       => 'about_me',
  4432.                 'id'         => 'about_me',
  4433.                 'value'      => mysql_clean($default['about_me']),
  4434.                 'db_field'   => 'about_me',
  4435.                 'auto_view'  => 'no',
  4436.                 'clean_func' => 'Replacer'
  4437.             ],
  4438.             'profile_tags'    => [
  4439.                 'title'     => lang('profile_tags'),
  4440.                 'type'      => 'hidden',
  4441.                 'name'      => 'profile_tags',
  4442.                 'id'        => 'profile_tags',
  4443.                 'value'     => genTags($default['profile_tags']),
  4444.                 'auto_view' => 'no'
  4445.             ],
  4446.             'web_url'         => [
  4447.                 'title'            => lang('website'),
  4448.                 'type'             => 'textfield',
  4449.                 'name'             => 'web_url',
  4450.                 'id'               => 'web_url',
  4451.                 'value'            => $default['web_url'],
  4452.                 'db_field'         => 'web_url',
  4453.                 'auto_view'        => 'yes',
  4454.                 'display_function' => 'outgoing_link'
  4455.             ]
  4456.         ];
  4457.     }
  4458.  
  4459.     /**
  4460.      * function used to load location fields
  4461.      *
  4462.      * @param $default
  4463.      *
  4464.      * @return array
  4465.      * @throws Exception
  4466.      */
  4467.     function load_location_fields($default): array
  4468.     {
  4469.         if (!$default) {
  4470.             $default = $_POST;
  4471.         }
  4472.         return [
  4473.             'postal_code' => [
  4474.                 'title'     => lang('postal_code'),
  4475.                 'type'      => 'textfield',
  4476.                 'name'      => 'postal_code',
  4477.                 'id'        => 'postal_code',
  4478.                 'value'     => $default['postal_code'],
  4479.                 'db_field'  => 'postal_code',
  4480.                 'auto_view' => 'yes'
  4481.             ],
  4482.             'hometown'    => [
  4483.                 'title'     => lang('hometown'),
  4484.                 'type'      => 'textfield',
  4485.                 'name'      => 'hometown',
  4486.                 'id'        => 'hometown',
  4487.                 'value'     => $default['hometown'],
  4488.                 'db_field'  => 'hometown',
  4489.                 'auto_view' => 'yes'
  4490.             ],
  4491.             'city'        => [
  4492.                 'title'     => lang('city'),
  4493.                 'type'      => 'textfield',
  4494.                 'name'      => 'city',
  4495.                 'id'        => 'city',
  4496.                 'value'     => $default['city'],
  4497.                 'db_field'  => 'city',
  4498.                 'auto_view' => 'yes'
  4499.             ]
  4500.         ];
  4501.     }
  4502.  
  4503.     /**
  4504.      * Function used to load experice fields
  4505.      *
  4506.      * @param $default
  4507.      *
  4508.      * @return array
  4509.      * @throws Exception
  4510.      */
  4511.     function load_education_interests($default): array
  4512.     {
  4513.         if (!$default) {
  4514.             $default = $_POST;
  4515.         }
  4516.  
  4517.         return [
  4518.             'education'  => [
  4519.                 'title'     => lang('education'),
  4520.                 'type'      => 'dropdown',
  4521.                 'name'      => 'education',
  4522.                 'id'        => 'education',
  4523.                 'value'     => [
  4524.                     lang('usr_arr_no_ans'),
  4525.                     lang('usr_arr_elementary'),
  4526.                     lang('usr_arr_hi_school'),
  4527.                     lang('usr_arr_some_colg'),
  4528.                     lang('usr_arr_assoc_deg'),
  4529.                     lang('usr_arr_bach_deg'),
  4530.                     lang('usr_arr_mast_deg'),
  4531.                     lang('usr_arr_phd'),
  4532.                     lang('usr_arr_post_doc')
  4533.                 ],
  4534.                 'checked'   => $default['education'],
  4535.                 'db_field'  => 'education',
  4536.                 'auto_view' => 'yes'
  4537.             ],
  4538.             'schools'    => [
  4539.                 'title'      => lang('schools'),
  4540.                 'type'       => 'textarea',
  4541.                 'name'       => 'schools',
  4542.                 'id'         => 'schools',
  4543.                 'value'      => mysql_clean($default['schools']),
  4544.                 'db_field'   => 'schools',
  4545.                 'clean_func' => 'Replacer',
  4546.                 'auto_view'  => 'yes'
  4547.             ],
  4548.             'occupation' => [
  4549.                 'title'      => lang('occupation'),
  4550.                 'type'       => 'textarea',
  4551.                 'name'       => 'occupation',
  4552.                 'id'         => 'occupation',
  4553.                 'value'      => mysql_clean($default['occupation']),
  4554.                 'db_field'   => 'occupation',
  4555.                 'clean_func' => 'Replacer',
  4556.                 'auto_view'  => 'yes'
  4557.             ],
  4558.             'companies'  => [
  4559.                 'title'      => lang('companies'),
  4560.                 'type'       => 'textarea',
  4561.                 'name'       => 'companies',
  4562.                 'id'         => 'companies',
  4563.                 'value'      => mysql_clean($default['companies']),
  4564.                 'db_field'   => 'companies',
  4565.                 'clean_func' => 'Replacer',
  4566.                 'auto_view'  => 'yes'
  4567.             ],
  4568.             'hobbies'    => [
  4569.                 'title'      => lang('hobbies'),
  4570.                 'type'       => 'textarea',
  4571.                 'name'       => 'hobbies',
  4572.                 'id'         => 'hobbies',
  4573.                 'value'      => mysql_clean($default['hobbies']),
  4574.                 'db_field'   => 'hobbies',
  4575.                 'clean_func' => 'Replacer',
  4576.                 'auto_view'  => 'yes'
  4577.             ],
  4578.             'fav_movies' => [
  4579.                 'title'      => lang('user_fav_movs_shows'),
  4580.                 'type'       => 'textarea',
  4581.                 'name'       => 'fav_movies',
  4582.                 'id'         => 'fav_movies',
  4583.                 'value'      => mysql_clean($default['fav_movies']),
  4584.                 'db_field'   => 'fav_movies',
  4585.                 'clean_func' => 'Replacer',
  4586.                 'auto_view'  => 'yes'
  4587.             ],
  4588.             'fav_music'  => [
  4589.                 'title'      => lang('user_fav_music'),
  4590.                 'type'       => 'textarea',
  4591.                 'name'       => 'fav_music',
  4592.                 'id'         => 'fav_music',
  4593.                 'value'      => mysql_clean($default['fav_music']),
  4594.                 'db_field'   => 'fav_music',
  4595.                 'clean_func' => 'Replacer',
  4596.                 'auto_view'  => 'yes'
  4597.             ],
  4598.             'fav_books'  => [
  4599.                 'title'      => lang('user_fav_books'),
  4600.                 'type'       => 'textarea',
  4601.                 'name'       => 'fav_books',
  4602.                 'id'         => 'fav_books',
  4603.                 'value'      => mysql_clean($default['fav_books']),
  4604.                 'db_field'   => 'fav_books',
  4605.                 'clean_func' => 'Replacer',
  4606.                 'auto_view'  => 'yes'
  4607.             ]
  4608.         ];
  4609.     }
  4610.  
  4611.  
  4612.     /**
  4613.      * Function used to load privacy fields
  4614.      *
  4615.      * @param $default
  4616.      *
  4617.      * @return array
  4618.      * @throws Exception
  4619.      */
  4620.     function load_privacy_field($default): array
  4621.     {
  4622.         if (!$default) {
  4623.             $default = $_POST;
  4624.         }
  4625.  
  4626.         return [
  4627.             'online_status'      => [
  4628.                 'title'    => lang('online_status'),
  4629.                 'type'     => 'dropdown',
  4630.                 'name'     => 'privacy',
  4631.                 'id'       => 'privacy',
  4632.                 'value'    => ['online' => lang('online'), 'offline' => lang('offline'), 'custom' => lang('custom')],
  4633.                 'checked'  => $default['online_status'],
  4634.                 'db_field' => 'online_status'
  4635.             ],
  4636.             'show_profile'       => [
  4637.                 'title'    => lang('show_profile'),
  4638.                 'type'     => 'dropdown',
  4639.                 'name'     => 'show_profile',
  4640.                 'id'       => 'show_profile',
  4641.                 'value'    => ['all' => lang('all'), 'members' => lang('members'), 'friends' => lang('friends')],
  4642.                 'checked'  => $default['show_profile'],
  4643.                 'db_field' => 'show_profile',
  4644.                 'sep'      => '&nbsp;'
  4645.             ],
  4646.             'allow_comments'     => [
  4647.                 'title'    => lang('vdo_allow_comm'),
  4648.                 'type'     => 'radiobutton',
  4649.                 'name'     => 'allow_comments',
  4650.                 'id'       => 'allow_comments',
  4651.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4652.                 'checked'  => strtolower($default['allow_comments']),
  4653.                 'db_field' => 'allow_comments',
  4654.                 'sep'      => '&nbsp;'
  4655.             ],
  4656.             'allow_ratings'      => [
  4657.                 'title'    => lang('allow_ratings'),
  4658.                 'type'     => 'radiobutton',
  4659.                 'name'     => 'allow_ratings',
  4660.                 'id'       => 'allow_ratings',
  4661.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4662.                 'checked'  => strtolower($default['allow_ratings']),
  4663.                 'db_field' => 'allow_ratings',
  4664.                 'sep'      => '&nbsp;'
  4665.             ],
  4666.             'allow_subscription' => [
  4667.                 'title'    => lang('allow_subscription'),
  4668.                 'type'     => 'radiobutton',
  4669.                 'name'     => 'allow_subscription',
  4670.                 'id'       => 'allow_subscription',
  4671.                 'hint_1'   => lang('allow_subscription_hint'),
  4672.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4673.                 'checked'  => strtolower($default['allow_subscription']),
  4674.                 'db_field' => 'allow_subscription',
  4675.                 'sep'      => '&nbsp;'
  4676.             ]
  4677.         ];
  4678.     }
  4679.  
  4680.     /**
  4681.      * load_channel_settings
  4682.      *
  4683.      * @param $default array values for channel settings
  4684.      * @return array of channel info fields
  4685.      * @throws Exception
  4686.      */
  4687.     function load_channel_settings($default): array
  4688.     {
  4689.         if (!$default) {
  4690.             $default = $_POST;
  4691.         }
  4692.  
  4693.         return [
  4694.             'profile_title'         => [
  4695.                 'title'     => lang('channel_title'),
  4696.                 'type'      => 'textfield',
  4697.                 'name'      => 'profile_title',
  4698.                 'id'        => 'profile_title',
  4699.                 'value'     => $default['profile_title'],
  4700.                 'db_field'  => 'profile_title',
  4701.                 'auto_view' => 'no'
  4702.             ],
  4703.             'profile_desc'          => [
  4704.                 'title'      => lang('channel_desc'),
  4705.                 'type'       => 'textarea',
  4706.                 'name'       => 'profile_desc',
  4707.                 'id'         => 'profile_desc',
  4708.                 'value'      => $default['profile_desc'],
  4709.                 'db_field'   => 'profile_desc',
  4710.                 'auto_view'  => 'yes',
  4711.                 'clean_func' => 'Replacer'
  4712.             ],
  4713.             'show_my_friends'       => [
  4714.                 'title'    => lang('show_my_friends'),
  4715.                 'type'     => 'radiobutton',
  4716.                 'name'     => 'show_my_friends',
  4717.                 'id'       => 'show_my_friends',
  4718.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4719.                 'checked'  => strtolower($default['show_my_friends']),
  4720.                 'db_field' => 'show_my_friends',
  4721.                 'sep'      => '&nbsp;'
  4722.             ],
  4723.             'show_my_videos'        => [
  4724.                 'title'    => lang('show_my_videos'),
  4725.                 'type'     => 'radiobutton',
  4726.                 'name'     => 'show_my_videos',
  4727.                 'id'       => 'show_my_videos',
  4728.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4729.                 'checked'  => strtolower($default['show_my_videos']),
  4730.                 'db_field' => 'show_my_videos',
  4731.                 'sep'      => '&nbsp;'
  4732.             ],
  4733.             'show_my_photos'        => [
  4734.                 'title'    => lang('show_my_photos'),
  4735.                 'type'     => 'radiobutton',
  4736.                 'name'     => 'show_my_photos',
  4737.                 'id'       => 'show_my_photos',
  4738.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4739.                 'checked'  => strtolower($default['show_my_photos']),
  4740.                 'db_field' => 'show_my_photos',
  4741.                 'sep'      => '&nbsp;'
  4742.             ],
  4743.             'show_my_subscriptions' => [
  4744.                 'title'    => lang('show_my_subscriptions'),
  4745.                 'type'     => 'radiobutton',
  4746.                 'name'     => 'show_my_subscriptions',
  4747.                 'id'       => 'show_my_subscriptions',
  4748.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4749.                 'checked'  => strtolower($default['show_my_subscriptions']),
  4750.                 'db_field' => 'show_my_subscriptions',
  4751.                 'sep'      => '&nbsp;'
  4752.             ],
  4753.             'show_my_subscribers'   => [
  4754.                 'title'    => lang('show_my_subscribers'),
  4755.                 'type'     => 'radiobutton',
  4756.                 'name'     => 'show_my_subscribers',
  4757.                 'id'       => 'show_my_subscribers',
  4758.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4759.                 'checked'  => strtolower($default['show_my_subscribers']),
  4760.                 'db_field' => 'show_my_subscribers',
  4761.                 'sep'      => '&nbsp;'
  4762.             ],
  4763.             'show_my_collections'   => [
  4764.                 'title'    => lang('show_my_collections'),
  4765.                 'type'     => 'radiobutton',
  4766.                 'name'     => 'show_my_collections',
  4767.                 'id'       => 'show_my_collections',
  4768.                 'value'    => ['yes' => lang('yes'), 'no' => lang('no')],
  4769.                 'checked'  => strtolower($default['show_my_collections']),
  4770.                 'db_field' => 'show_my_collections',
  4771.                 'sep'      => '&nbsp;'
  4772.             ]
  4773.         ];
  4774.     }
  4775.  
  4776.     /**
  4777.      * load_user_fields
  4778.      *
  4779.      * @param        $default array values for user profile fields
  4780.      * @param string $type
  4781.      *
  4782.      * @return array of user fields
  4783.      *
  4784.      * Function used to load Video fields
  4785.      * in Clipbucket v2.1 , video fields are loaded in form of groups arrays
  4786.      * each group has it name and fields wrapped in array
  4787.      * and that array will be part of video fields
  4788.      * @throws Exception
  4789.      */
  4790.     function load_user_fields($default, $type = 'all')
  4791.     {
  4792.         $getChannelSettings = false;
  4793.         $getProfileSettings = false;
  4794.         $fields = [];
  4795.  
  4796.         switch ($type) {
  4797.             case 'all':
  4798.                 $getChannelSettings = true;
  4799.                 $getProfileSettings = true;
  4800.                 break;
  4801.  
  4802.             case 'channel':
  4803.             case 'channels':
  4804.                 $getChannelSettings = true;
  4805.                 break;
  4806.  
  4807.             case 'profile':
  4808.             case 'profile_settings':
  4809.                 $getProfileSettings = true;
  4810.                 break;
  4811.         }
  4812.  
  4813.         if ($getChannelSettings) {
  4814.             $channel_settings = [
  4815.                 [
  4816.                     'group_name' => lang('channel_settings'),
  4817.                     'group_id'   => 'channel_settings',
  4818.                     'fields'     => array_merge($this->load_channel_settings($default)
  4819.                         , $this->load_privacy_field($default)),
  4820.                 ]
  4821.             ];
  4822.         }
  4823.  
  4824.         if ($getProfileSettings) {
  4825.             $profile_settings = [
  4826.                 [
  4827.                     'group_name' => lang('profile_basic_info'),
  4828.                     'group_id'   => 'profile_basic_info',
  4829.                     'fields'     => $this->load_personal_details($default),
  4830.                 ],
  4831.                 [
  4832.                     'group_name' => lang('location'),
  4833.                     'group_id'   => 'profile_location',
  4834.                     'fields'     => $this->load_location_fields($default)
  4835.                 ],
  4836.                 [
  4837.                     'group_name' => lang('profile_education_interests'),
  4838.                     'group_id'   => 'profile_education_interests',
  4839.                     'fields'     => $this->load_education_interests($default)
  4840.                 ]
  4841.             ];
  4842.  
  4843.             //Adding Custom Fields
  4844.             $custom_fields = $this->load_custom_profile_fields($default, false);
  4845.             if ($custom_fields) {
  4846.                 $more_fields_group = [
  4847.                     'group_name' => lang('more_fields'),
  4848.                     'group_id'   => 'custom_fields',
  4849.                     'fields'     => $custom_fields
  4850.                 ];
  4851.             }
  4852.  
  4853.             //Loading Custom Profile Forms
  4854.             $custom_fields_with_group = $this->load_custom_profile_fields($default, true);
  4855.  
  4856.             //Finally putting them together in their main array called $fields
  4857.             if ($custom_fields_with_group) {
  4858.                 $custFieldGroups = $custom_fields_with_group;
  4859.  
  4860.                 foreach ($custFieldGroups as $gKey => $fieldGroup) {
  4861.                     $group_id = $fieldGroup['group_id'];
  4862.  
  4863.                     foreach ($profile_settings as $key => $field) {
  4864.                         if ($field['group_id'] == $group_id) {
  4865.                             $inputFields = $field['fields'];
  4866.                             //Setting field values
  4867.                             $newFields = $fieldGroup['fields'];
  4868.                             $mergeField = array_merge($inputFields, $newFields);
  4869.  
  4870.                             //Finally Updating array
  4871.                             $newGroupArray = [
  4872.                                 'group_name' => $field['group_name'],
  4873.                                 'group_id'   => $field['group_id'],
  4874.                                 'fields'     => $mergeField
  4875.                             ];
  4876.  
  4877.                             $fields[$key] = $newGroupArray;
  4878.                             $matched = true;
  4879.                             break;
  4880.                         } else {
  4881.                             $matched = false;
  4882.                         }
  4883.                     }
  4884.                     if (!$matched) {
  4885.                         $profile_settings[] = $fieldGroup;
  4886.                     }
  4887.                 }
  4888.             }
  4889.  
  4890.         }
  4891.  
  4892.         if ($channel_settings) {
  4893.             $fields = array_merge($fields, $channel_settings);
  4894.         }
  4895.         if ($profile_settings) {
  4896.             $fields = array_merge($fields, $profile_settings);
  4897.         }
  4898.         if ($more_fields_group) {
  4899.             $fields[] = $more_fields_group;
  4900.         }
  4901.         return $fields;
  4902.     }
  4903.  
  4904.     /**
  4905.      * Used to rate photo
  4906.      *
  4907.      * @param $id
  4908.      * @param $rating
  4909.      *
  4910.      * @return array
  4911.      * @throws Exception
  4912.      */
  4913.     function rate_user($id, $rating): array
  4914.     {
  4915.         global $db;
  4916.  
  4917.         if (!is_numeric($rating) || $rating <= 9) {
  4918.             $rating = 0;
  4919.         }
  4920.         if ($rating >= 10) {
  4921.             $rating = 10;
  4922.         }
  4923.  
  4924.         $c_rating = $this->current_rating($id);
  4925.         $voters = $c_rating['voters'];
  4926.         $new_rate = $c_rating['rating'];
  4927.         $rated_by = $c_rating['rated_by'];
  4928.  
  4929.         $voters = json_decode($voters, true);
  4930.  
  4931.         if (!empty($voters)) {
  4932.             $already_voted = array_key_exists(user_id(), $voters);
  4933.         }
  4934.  
  4935.         if (!user_id()) {
  4936.             e(lang('please_login_to_rate'));
  4937.         } elseif (user_id() == $c_rating['userid'] && !config('own_channel_rating')) {
  4938.             e(lang('you_cant_rate_own_channel'));
  4939.         } elseif (!empty($already_voted)) {
  4940.             e(lang('you_have_already_voted_channel'));
  4941.         } elseif ($c_rating['allow_ratings'] == 'no' || !config('channel_rating')) {
  4942.             e(lang('channel_rating_disabled'));
  4943.         } else {
  4944.             $voters[user_id()] = [
  4945.                 'userid'   => user_id(),
  4946.                 'username' => user_name(),
  4947.                 'time'     => now(),
  4948.                 'rating'   => $rating
  4949.             ];
  4950.             $voters = json_encode($voters);
  4951.  
  4952.             $t = $c_rating['rated_by'] * $c_rating['rating'];
  4953.             $rated_by = $c_rating['rated_by'] + 1;
  4954.             $new_rate = ($t + $rating) / $rated_by;
  4955.             $db->update(tbl('user_profile'), ['rating', 'rated_by', 'voters'], ["$new_rate", "$rated_by", "|no_mc|$voters"], ' userid = ' . $id . '');
  4956.             $userDetails = [
  4957.                 'object_id' => $id,
  4958.                 'type'      => 'user',
  4959.                 'time'      => now(),
  4960.                 'rating'    => $rating,
  4961.                 'userid'    => user_id(),
  4962.                 'username'  => user_name()
  4963.             ];
  4964.             /* Updating user details */
  4965.             update_user_voted($userDetails);
  4966.             e(lang('thnx_for_voting'), 'm');
  4967.         }
  4968.  
  4969.         return ['rating' => $new_rate, 'rated_by' => $rated_by, 'total' => 10, 'id' => $id, 'type' => 'user', 'disable' => 'disabled'];
  4970.     }
  4971.  
  4972.     /**
  4973.      * Used to get current rating
  4974.      *
  4975.      * @param $id
  4976.      *
  4977.      * @return bool|array
  4978.      * @throws Exception
  4979.      */
  4980.     function current_rating($id)
  4981.     {
  4982.         global $db;
  4983.         $result = $db->select(tbl('user_profile'), 'userid,allow_ratings,rating,rated_by,voters', ' userid = ' . mysql_clean($id) );
  4984.         if ($result) {
  4985.             return $result[0];
  4986.         }
  4987.         return false;
  4988.     }
  4989.  
  4990.     /**
  4991.      * function used to check weather user is  online or not
  4992.      *
  4993.      * @param      $last_active
  4994.      * @param null $status
  4995.      *
  4996.      * @return bool
  4997.      */
  4998.     function isOnline($last_active, $status = null): bool
  4999.     {
  5000.         $time = strtotime($last_active);
  5001.         $timeDiff = time() - $time;
  5002.         if ($timeDiff > 60 || $status == 'offline') {
  5003.             return false;
  5004.         }
  5005.         return true;
  5006.     }
  5007.  
  5008.     /**
  5009.      * Function used to get list of subscribed users and then
  5010.      * send subscription email
  5011.      *
  5012.      * @param      $vidDetails
  5013.      * @param bool $updateStatus
  5014.      *
  5015.      * @return bool
  5016.      * @throws Exception
  5017.      */
  5018.     function sendSubscriptionEmail($vidDetails, bool $updateStatus = true): bool
  5019.     {
  5020.         global $cbemail, $db;
  5021.         if (!$vidDetails['videoid']) {
  5022.             e(lang('invalid_videoid'));
  5023.             return false;
  5024.         }
  5025.  
  5026.         if (!$vidDetails['userid']) {
  5027.             e(lang('invalid_userid'));
  5028.             return false;
  5029.         }
  5030.  
  5031.         //Lets get the list of subscribers
  5032.         $subscribers = $this->get_user_subscribers_detail($vidDetails['userid'], false);
  5033.         //Now lets get details of our uploader bhai saab
  5034.         $uploader = $this->get_user_details($vidDetails['userid']);
  5035.         //Loading subscription email template
  5036.         $tpl = $cbemail->get_template('video_subscription_email');
  5037.  
  5038.         if ($subscribers) {
  5039.             foreach ($subscribers as $subscriber) {
  5040.                 $var = [
  5041.                     '{username}'          => $subscriber['username'],
  5042.                     '{uploader}'          => $uploader['username'],
  5043.                     '{video_title}'       => $vidDetails['title'],
  5044.                     '{video_description}' => $vidDetails['description'],
  5045.                     '{video_link}'        => video_link($vidDetails),
  5046.                     '{video_thumb}'       => get_thumb($vidDetails)
  5047.                 ];
  5048.  
  5049.                 $more_var = $this->custom_subscription_email_vars;
  5050.                 if( !empty($more_var) && is_array($more_var) ){
  5051.                     $var = array_merge($var, $more_var);
  5052.                 }
  5053.  
  5054.                 $subj = $cbemail->replace($tpl['email_template_subject'], $var);
  5055.                 $msg = nl2br($cbemail->replace($tpl['email_template'], $var));
  5056.  
  5057.                 //Now Finally Sending Email
  5058.                 cbmail(['to' => $subscriber['email'], 'from' => WELCOME_EMAIL, 'subject' => $subj, 'content' => $msg]);
  5059.             }
  5060.  
  5061.             $total_subscribers = count($subscribers);
  5062.             $s = '';
  5063.             if ($total_subscribers > 1) {
  5064.                 $s = 's';
  5065.             }
  5066.             e(sprintf(lang('subs_email_sent_to_users'), $total_subscribers, $s), 'm');
  5067.         }
  5068.  
  5069.         //Updating video subscription email status to sent
  5070.         if ($updateStatus) {
  5071.             $db->update(tbl('video'), ['subscription_email'], ['sent'], " videoid='" . $vidDetails['videoid'] . "'");
  5072.         }
  5073.  
  5074.         return true;
  5075.     }
  5076.  
  5077.     /**
  5078.      * function used to get user sessions
  5079.      * @throws Exception
  5080.      */
  5081.     function get_sessions(): array
  5082.     {
  5083.         global $sess;
  5084.         $sessions = $sess->get_sessions();
  5085.         $new_sessions = [];
  5086.         if ($sessions) {
  5087.             foreach ($sessions as $session) {
  5088.                 $new_sessions[$session['session_string']] = $session;
  5089.             }
  5090.         } else {
  5091.             $sess->add_session(0, 'guest', 'guest');
  5092.         }
  5093.  
  5094.         return $new_sessions;
  5095.     }
  5096.  
  5097.     /**
  5098.      * @throws Exception
  5099.      */
  5100.     function update_user_voted($array, $userid = null)
  5101.     {
  5102.         global $db;
  5103.         if (!$userid) {
  5104.             $userid = user_id();
  5105.         }
  5106.  
  5107.         if (is_array($array)) {
  5108.             $voted = '';
  5109.             $votedDetails = $db->select(tbl('users'), 'voted', " userid = '$userid'");
  5110.             if (!empty($votedDetails)) {
  5111.                 if (!empty($js)) {
  5112.                     $voted = $js->json_decode($votedDetails[0]['voted'], true);
  5113.                 } else {
  5114.                     $voted = json_decode($votedDetails[0]['voted'], true);
  5115.                 }
  5116.             }
  5117.  
  5118.             if (!empty($js)) {
  5119.                 $votedEncode = $js->json_encode($voted);
  5120.             } else {
  5121.                 $votedEncode = json_encode($voted);
  5122.             }
  5123.  
  5124.             if (!empty($votedEncode)) {
  5125.                 $db->update(tbl('users'), ['voted'], ["|no_mc|$votedEncode"], " userid='$userid'");
  5126.             }
  5127.         }
  5128.     }
  5129.  
  5130.     /**
  5131.      * Function used to display user manger link
  5132.      *
  5133.      * @param $link
  5134.      * @param $vid
  5135.      *
  5136.      * @return string
  5137.      */
  5138.     function user_manager_link($link, $vid): string
  5139.     {
  5140.         if (function_exists($link) && !is_array($link)) {
  5141.             return $link($vid);
  5142.         }
  5143.  
  5144.         if (!empty($link['title']) && !empty($link['link'])) {
  5145.             return '<a href="' . $link['link'] . '">' . display_clean($link['title']) . '</a>';
  5146.         }
  5147.     }
  5148.  
  5149.     /**
  5150.      * Fetches all friend requests sent by given user
  5151.      *
  5152.      * @param : { integer } { $user } { id of user to fetch requests against }
  5153.      *
  5154.      * @return array : { array } { $data } { array with all sent requests details }
  5155.      * @throws Exception
  5156.      * @author : Saqib Razzaq
  5157.      * @since : 15th April, 2016, ClipBucket 2.8.1
  5158.      */
  5159.     function sent_contact_requests($user): array
  5160.     {
  5161.         global $db;
  5162.         return $db->select(tbl('contacts'), '*', "userid = $user AND confirmed = 'no'");
  5163.     }
  5164.  
  5165.     /**
  5166.      * Fetches all friend requests recieved by given user
  5167.      *
  5168.      * @param : { integer } { $user } { id of user to fetch requests against }
  5169.      *
  5170.      * @return array : { array } { $data } { array with all recieved requests details }
  5171.      * @throws Exception
  5172.      * @author : Saqib Razzaq
  5173.      * @since : 15th April, 2016, ClipBucket 2.8.1
  5174.      */
  5175.     function recieved_contact_requests($user): array
  5176.     {
  5177.         global $db;
  5178.         return $db->select(tbl('contacts'), '*', "contact_userid = $user AND confirmed = 'no'");
  5179.     }
  5180.  
  5181.     /**
  5182.      * Fetches all friends of given user
  5183.      *
  5184.      * @param : { integer } { $user } { id of user to fetch friends against }
  5185.      *
  5186.      * @return array : { array } { $data } { array with all friends details }
  5187.      * @throws Exception
  5188.      * @author : Saqib Razzaq
  5189.      * @since : 15th April, 2016, ClipBucket 2.8.1
  5190.      */
  5191.     function added_contacts($user): array
  5192.     {
  5193.         global $db;
  5194.         return $db->select(tbl('contacts'), '*', "(contact_userid = $user OR userid = $user) AND confirmed = 'yes'");
  5195.     }
  5196.  
  5197.     /**
  5198.      * Fetches friendship status of two users
  5199.      *
  5200.      * @param $logged_in_user
  5201.      * @param $channel_user
  5202.      *
  5203.      * @return string : { string } { s = sent, r = recieved, f = friends }
  5204.      * @throws Exception
  5205.      */
  5206.     function friendship_status($logged_in_user, $channel_user): string
  5207.     {
  5208.         if (!user_id()) {
  5209.             return '';
  5210.         }
  5211.         $sent = $this->sent_contact_requests($logged_in_user);
  5212.         $pending = $this->recieved_contact_requests($logged_in_user);
  5213.         $friends = $this->added_contacts($logged_in_user);
  5214.  
  5215.         foreach ($sent as $key => $data) {
  5216.             if ($data['contact_userid'] == $channel_user) {
  5217.                 return 's'; // sent
  5218.             }
  5219.         }
  5220.  
  5221.         foreach ($pending as $key => $data) {
  5222.             if ($data['userid'] == $channel_user) {
  5223.                 return 'r'; // received
  5224.             }
  5225.         }
  5226.  
  5227.         foreach ($friends as $key => $data) {
  5228.             if ($data['contact_userid'] == $channel_user) {
  5229.                 return 'f'; // friends
  5230.             }
  5231.         }
  5232.         return '';
  5233.     }
  5234.  
  5235.     function updateUser2FAcheckboxstate($inputUserId, $inputUser2FACheckboxState){ 
  5236.         global $db;
  5237.         $db->updateUser2FACheckBoxState(tbl('users'), $inputUserId, $inputUser2FACheckboxState);       
  5238.     }
  5239.    
  5240.     function getStateOf2FAUserCheckbox($inputUserId):bool{
  5241.         global $db;
  5242.         return $db->getStatusOfUser2FACheckBox(tbl('users'), $inputUserId);
  5243.     }
  5244.  
  5245. }
  5246.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement