/** * get_new_people * * @param integer $offset * @param boolean $random * @return array */ public function get_new_people($offset = 0, $random = false) { global $db, $system; // If random is true, use cached version to avoid slow queries if ($random) { return $this->get_new_people_cached($offset); } $results = []; $offset *= $system['min_results']; // prepare where statement $where = ""; /* user not IN (friends, followings, friend requests & friend requests sent) */ $old_people_ids = array_unique(array_merge($this->_data['friends_ids'], $this->_data['followings_ids'], $this->_data['friend_requests_ids'], $this->_data['friend_requests_sent_ids'])); $where .= sprintf("WHERE users.user_id != '1' AND users.user_banned = '0' AND users.user_suggestions_hidden = '0' AND users.user_id != %s AND users.user_id NOT IN (%s)", secure($this->_data['user_id'], 'int'), $this->spread_ids($old_people_ids)); /* check if activation enabled */ if ($system['activation_enabled']) { $where .= " AND users.user_activated = '1'"; } /* get users */ if ($system['location_finder_enabled']) { $unit = ($system['system_distance'] == "mile") ? 3958 : 6371; $distance = 10000; if ($random) { $get_users = $db->query(sprintf("SELECT * FROM (SELECT user_id, user_name, user_firstname, user_lastname, user_gender, user_picture, user_subscribed, user_verified, (%s * acos(cos(radians(%s)) * cos(radians(user_latitude)) * cos(radians(user_longitude) - radians(%s)) + sin(radians(%s)) * sin(radians(user_latitude))) ) AS distance FROM users " . $where . " HAVING distance < %s ORDER BY distance ASC LIMIT %s) tmp ORDER BY RAND() LIMIT %s", secure($unit, 'int'), secure($this->_data['user_latitude']), secure($this->_data['user_longitude']), secure($this->_data['user_latitude']), secure($distance, 'int'), secure($system['max_results'] * 2, 'int', false), secure($system['min_results'], 'int', false))); } else { $get_users = $db->query(sprintf("SELECT user_id, user_name, user_firstname, user_lastname, user_gender, user_picture, user_subscribed, user_verified, (%s * acos(cos(radians(%s)) * cos(radians(user_latitude)) * cos(radians(user_longitude) - radians(%s)) + sin(radians(%s)) * sin(radians(user_latitude))) ) AS distance FROM users " . $where . " HAVING distance < %s ORDER BY distance ASC LIMIT %s, %s", secure($unit, 'int'), secure($this->_data['user_latitude']), secure($this->_data['user_longitude']), secure($this->_data['user_latitude']), secure($distance, 'int'), secure($offset, 'int', false), secure($system['min_results'], 'int', false))); } } else { if ($random) { // Use cached version instead return $this->get_new_people_cached($offset); } else { $get_users = $db->query(sprintf("SELECT user_id, user_name, user_firstname, user_lastname, user_gender, user_picture, user_subscribed, user_verified FROM users " . $where . " LIMIT %s, %s", secure($offset, 'int', false), secure($system['min_results'], 'int', false))); } } if ($get_users->num_rows > 0) { while ($user = $get_users->fetch_assoc()) { /* check if there is any blocking between the viewer & the target user */ if ($this->blocked($user['user_id'])) { continue; } $user['user_picture'] = get_picture($user['user_picture'], $user['user_gender']); $user['mutual_friends_count'] = $this->get_mutual_friends_count($user['user_id']); $results[] = $user; } } return $results; } /** * get_new_people_cached - Cached version for random suggestions * * @param integer $offset * @return array */ public function get_new_people_cached($offset = 0) { global $db, $system; $results = []; // First try to get from cache table $cache_query = "SELECT user_data FROM random_users_cache ORDER BY RAND() LIMIT " . secure($system['min_results'], 'int', false); $get_cached = $db->query($cache_query); if ($get_cached->num_rows > 0) { while ($row = $get_cached->fetch_assoc()) { $data = explode('|', $row['user_data']); $user = [ 'user_id' => $data[0], 'user_name' => $data[1], 'user_firstname' => $data[2], 'user_lastname' => $data[3], 'user_gender' => $data[4], 'user_picture' => get_picture($data[5], $data[4]), 'user_subscribed' => $data[6], 'user_verified' => $data[7] ]; $user['mutual_friends_count'] = $this->get_mutual_friends_count($user['user_id']); $results[] = $user; } return $results; } // Fallback: use limited query $where = ""; $old_people_ids = array_unique(array_merge($this->_data['friends_ids'], $this->_data['followings_ids'], $this->_data['friend_requests_ids'], $this->_data['friend_requests_sent_ids'])); $where .= sprintf("WHERE users.user_banned = '0' AND users.user_suggestions_hidden = '0' AND users.user_id != %s AND users.user_id NOT IN (%s)", secure($this->_data['user_id'], 'int'), $this->spread_ids($old_people_ids)); if ($system['activation_enabled']) { $where .= " AND users.user_activated = '1'"; } $limit = 50; // Limit to 50 to avoid full table scan $get_users = $db->query(sprintf("SELECT user_id, user_name, user_firstname, user_lastname, user_gender, user_picture, user_subscribed, user_verified FROM users " . $where . " ORDER BY RAND() LIMIT %s", secure($limit, 'int', false))); if ($get_users->num_rows > 0) { while ($user = $get_users->fetch_assoc()) { if ($this->blocked($user['user_id'])) { continue; } $user['user_picture'] = get_picture($user['user_picture'], $user['user_gender']); $user['mutual_friends_count'] = $this->get_mutual_friends_count($user['user_id']); $results[] = $user; } } return $results; }