$privacy_max = $group->group_privacy_max($user);
if (!($privacy_max & $group->group_info['group_privacy'])) {
    header("Location: " . $url->url_create("group", NULL, $group->group_info['group_id']));
    exit;
}
// GET MEDIA IN ALBUM FOR CAROUSEL
$media_array = array();
$media_query = $database->database_query("SELECT groupmedia_id, groupmedia_ext, '{$group->group_info['group_id']}' AS groupalbum_group_id FROM se_groupmedia WHERE groupmedia_groupalbum_id='{$media_info['groupalbum_id']}' ORDER BY groupmedia_date DESC");
while ($thismedia = $database->database_fetch_assoc($media_query)) {
    $media_array[$thismedia['groupmedia_id']] = $thismedia;
}
// IF USER IS ALLOWED, CHECK TASK
if ($group->user_rank == 2 || $group->user_rank == 1 || $media_info['uploader']->user_exists && $user->user_info['user_id'] == $media_info['uploader']->user_info['user_id']) {
    // DELETE PHOTO
    if ($task == "media_delete") {
        $media_path = $group->group_dir($group->group_info['group_id']) . $media_info['groupmedia_id'] . "." . $media_info['groupmedia_ext'];
        if (file_exists($media_path)) {
            @unlink($media_path);
        }
        $thumb_path = $group->group_dir($group->group_info['group_id']) . $media_info['groupmedia_id'] . "_thumb.jpg";
        if (file_exists($thumb_path)) {
            @unlink($thumb_path);
        }
        $action_thumb_path = $url->url_base . substr($group->group_dir($group->group_info['group_id']), 2) . $media_info['groupmedia_id'] . "_thumb.jpg";
        // DELETE ACTION MEDIA IF NECESSARY
        $database->database_query("DELETE FROM se_actionmedia WHERE actionmedia_path = '{$action_thumb_path}'");
        // DELETE MEDIA FROM DATABASE
        $database->database_query("DELETE FROM se_groupmedia, se_groupmediacomments, se_groupmediatags USING se_groupmedia LEFT JOIN se_groupmediacomments ON se_groupmedia.groupmedia_id=se_groupmediacomments.groupmediacomment_groupmedia_id LEFT JOIN se_groupmediatags ON se_groupmedia.groupmedia_id=se_groupmediatags.groupmediatag_groupmedia_id WHERE se_groupmedia.groupmedia_id='{$media_info['groupmedia_id']}'");
        // UPDATE CACHED TOTALS
        $database->database_query("UPDATE se_groupalbums SET groupalbum_totalfiles=groupalbum_totalfiles-1, groupalbum_totalspace=groupalbum_totalspace-'{$media_info['groupmedia_filesize']}' WHERE groupalbum_id='{$media_info['groupmedia_groupalbum_id']}' LIMIT 1");
        // SEND USER TO NEXT PHOTO
function search_group()
{
    global $database, $url, $results_per_page, $p, $search_text, $t, $search_objects, $results, $total_results;
    // GET GROUP FIELDS
    $fields = $database->database_query("SELECT groupfield_id AS field_id, groupfield_type AS field_type, groupfield_options AS field_options FROM se_groupfields WHERE groupfield_type<>'5' AND (groupfield_dependency<>'0' OR (groupfield_dependency='0' AND groupfield_display<>'0'))");
    $group_query = "se_groups.group_title LIKE '%{$search_text}%' OR se_groups.group_desc LIKE '%{$search_text}%'";
    // LOOP OVER FIELDS
    while ($field_info = $database->database_fetch_assoc($fields)) {
        // TEXT FIELD OR TEXTAREA
        if ($field_info['field_type'] == 1 || $field_info['field_type'] == 2) {
            if ($group_query != "") {
                $group_query .= " OR ";
            }
            $group_query .= "se_groupvalues.groupvalue_" . $field_info['field_id'] . " LIKE '%{$search_text}%'";
            // RADIO OR SELECT BOX
        } elseif ($field_info['field_type'] == 3 || $field_info['field_type'] == 4) {
            $options = unserialize($field_info['field_options']);
            $langids = array();
            $cases = array();
            for ($i = 0, $max = count($options); $i < $max; $i++) {
                $cases[] = "WHEN languagevar_id={$options[$i]['label']} THEN {$options[$i]['value']}";
                $langids[] = $options[$i]['label'];
            }
            if (count($cases) != 0) {
                if ($group_query != "") {
                    $group_query .= " OR ";
                }
                $group_query .= "se_groupvalues.groupvalue_" . $field_info['field_id'] . " IN (SELECT CASE " . implode(" ", $cases) . " END AS value FROM se_languagevars WHERE languagevar_id IN (" . implode(", ", $langids) . ") AND languagevar_value LIKE '%{$search_text}%')";
            }
            // CHECKBOX
        } elseif ($field_info['field_type'] == 6) {
            $options = unserialize($field_info['field_options']);
            $langids = array();
            $cases = array();
            for ($i = 0, $max = count($options); $i < $max; $i++) {
                $cases[] = "WHEN languagevar_id={$options[$i]['label']} THEN " . pow(2, $i);
                $langids[] = $options[$i][label];
            }
            if (count($cases) != 0) {
                if ($group_query != "") {
                    $group_query .= " OR ";
                }
                $group_query .= "se_groupvalues.groupvalue_" . $field_info['field_id'] . " & (SELECT sum(CASE " . implode(" ", $cases) . " END) AS value FROM se_languagevars WHERE languagevar_id IN (" . implode(", ", $langids) . ") AND languagevar_value LIKE '%{$search_text}%')";
            }
        }
    }
    // CONSTRUCT QUERY
    $group_query = "\r\n\t(\r\n\tSELECT \r\n\t  '1' AS sub_type,\r\n\t  se_groups.group_id AS group_id, \r\n\t  se_groups.group_title AS group_title, \r\n\t  se_groups.group_photo AS group_photo,\r\n\t  '' AS title,\r\n\t  se_groups.group_desc AS description,\r\n\t  '' AS id,\r\n\t  '' AS extra\r\n\tFROM \r\n\t  se_groupvalues \r\n\tLEFT JOIN \r\n\t  se_groups \r\n\tON \r\n\t  se_groupvalues.groupvalue_group_id=se_groups.group_id \r\n\tWHERE \r\n\t  se_groups.group_search='1' \r\n\t  AND \r\n\t  ({$group_query})\r\n\tORDER BY group_id DESC\r\n\t)\r\n\tUNION ALL\r\n\t(\r\n\tSELECT\r\n          '2' AS sub_type,\r\n\t  se_groups.group_id AS group_id, \r\n\t  se_groups.group_title AS group_title, \r\n\t  se_groups.group_photo AS group_photo,\r\n\t  se_groupmedia.groupmedia_title AS title,\r\n\t  se_groupmedia.groupmedia_desc AS description,\r\n\t  se_groupmedia.groupmedia_id AS id,\r\n\t  se_groupmedia.groupmedia_ext AS extra\r\n\tFROM\r\n\t  se_groupmedia,\r\n\t  se_groupalbums,\r\n\t  se_groups\r\n\tWHERE\r\n\t  se_groupmedia.groupmedia_groupalbum_id=se_groupalbums.groupalbum_id AND\r\n\t  se_groupalbums.groupalbum_group_id=se_groups.group_id AND\r\n\t  se_groups.group_search='1'\r\n\t  AND\r\n\t  (\r\n\t    se_groupmedia.groupmedia_title LIKE '%{$search_text}%' OR\r\n\t    se_groupmedia.groupmedia_desc LIKE '%{$search_text}%'\r\n\t  )\r\n\tORDER BY groupmedia_id DESC\r\n\t)\r\n\tUNION ALL\r\n\t(\r\n\tSELECT\r\n          '3' AS sub_type,\r\n\t  se_groups.group_id AS group_id, \r\n\t  se_groups.group_title AS group_title, \r\n\t  se_groups.group_photo AS group_photo,\r\n\t  se_grouptopics.grouptopic_subject AS title,\r\n\t  se_groupposts.grouppost_body AS description,\r\n\t  se_grouptopics.grouptopic_id AS id,\r\n\t  se_groupposts.grouppost_id AS extra\r\n\tFROM\r\n\t  se_groupposts,\r\n\t  se_grouptopics,\r\n\t  se_groups\r\n\tWHERE\r\n\t  se_groupposts.grouppost_grouptopic_id=se_grouptopics.grouptopic_id AND\r\n\t  se_grouptopics.grouptopic_group_id=se_groups.group_id AND\r\n\t  se_groups.group_search='1'\r\n\t  AND\r\n\t  (\r\n\t    se_groupposts.grouppost_body LIKE '%{$search_text}%'\r\n\t  )\r\n\tORDER BY grouppost_id DESC\r\n\t)";
    // GET TOTAL GROUP RESULTS
    $total_groups = $database->database_num_rows($database->database_query($group_query . " LIMIT 201"));
    // IF NOT TOTAL ONLY
    if ($t == "group") {
        // MAKE GROUP PAGES
        $start = ($p - 1) * $results_per_page;
        $limit = $results_per_page + 1;
        // SEARCH GROUPS
        $groups = $database->database_query($group_query . " LIMIT {$start}, {$limit}");
        while ($group_info = $database->database_fetch_assoc($groups)) {
            // SET UP GROUP
            $group = new se_group();
            $group->group_info['group_id'] = $group_info['group_id'];
            $group->group_info['group_photo'] = $group_info['group_photo'];
            $thumb_path = $group->group_photo('./images/nophoto.gif', TRUE);
            // IF DESCRIPTION IS LONG
            if (strlen($group_info['description']) > 150) {
                $group_info['description'] = substr($group_info['description'], 0, 147) . "...";
            }
            if (strlen($group_info['group_desc']) > 150) {
                $group_info['group_desc'] = substr($group_info['group_desc'], 0, 147) . "...";
            }
            // RESULT IS A GROUP
            if ($group_info[sub_type] == 1) {
                $result_url = $url->url_create('group', NULL, $group_info['group_id']);
                $result_name = 2000292;
                $result_name_1 = $group_info['group_title'];
                $result_desc = 2000295;
                $result_desc_1 = $group_info['description'];
            } elseif ($group_info['sub_type'] == 2) {
                $result_url = $url->url_create('group_media', NULL, $group_info['group_id'], $group_info['id']);
                $result_name = 2000293;
                $result_name_1 = $group_info['title'];
                $result_desc = 2000296;
                $result_desc_1 = $url->url_create('group', NULL, $group_info['group_id']);
                $result_desc_2 = $group_info['group_title'];
                $result_desc_3 = $group_info['description'];
                // SET THUMBNAIL, IF AVAILABLE
                switch ($group_info['extra']) {
                    case "jpeg":
                    case "jpg":
                    case "gif":
                    case "png":
                    case "bmp":
                        $thumb_path = $group->group_dir($group->group_info['group_id']) . $group_info['id'] . "_thumb.jpg";
                        break;
                    case "mp3":
                    case "mp4":
                    case "wav":
                        $thumb_path = "./images/icons/audio_big.gif";
                        break;
                    case "mpeg":
                    case "mpg":
                    case "mpa":
                    case "avi":
                    case "swf":
                    case "mov":
                    case "ram":
                    case "rm":
                        $thumb_path = "./images/icons/video_big.gif";
                        break;
                    default:
                        $thumb_path = "./images/icons/file_big.gif";
                }
                if (!file_exists($thumb_path)) {
                    $thumb_path = "./images/icons/file_big.gif";
                }
            } else {
                $result_url = $url->url_create('group_discussion_post', NULL, $group_info['group_id'], $group_info['id'], $group_info['extra']);
                $result_name = 2000294;
                $result_name_1 = $group_info['title'];
                $result_desc = 2000297;
                $result_desc_1 = $url->url_create('group', NULL, $group_info['group_id']);
                $result_desc_2 = $group_info['group_title'];
                $result_desc_3 = $group_info['description'];
            }
            $results[] = array('result_url' => $result_url, 'result_icon' => $thumb_path, 'result_name' => $result_name, 'result_name_1' => $result_name_1, 'result_desc' => $result_desc, 'result_desc_1' => $result_desc_1, 'result_desc_2' => $result_desc_2, 'result_desc_3' => $result_desc_3);
        }
        // SET TOTAL RESULTS
        $total_results = $total_groups;
    }
    // SET ARRAY VALUES
    SE_Language::_preload_multi(2000291, 2000292, 2000293, 2000294, 2000295, 2000296, 2000297);
    if ($total_groups > 200) {
        $total_groups = "200+";
    }
    $search_objects[] = array('search_type' => 'group', 'search_lang' => 2000291, 'search_total' => $total_groups);
}
 $file_result = array();
 // WORKAROUND FOR FLASH UPLOADER
 if ($_FILES['file1']['type'] == "application/octet-stream" && $isAjax) {
     $file_types = explode(",", str_replace(" ", "", strtolower($group->groupowner_level_info['level_group_album_mimes'])));
     $_FILES['file1']['type'] = $file_types[0];
 }
 // RUN FILE UPLOAD FUNCTION FOR EACH SUBMITTED FILE
 $update_album = 0;
 $action_media = array();
 for ($f = 1; $f < 6; $f++) {
     $fileid = "file" . $f;
     if ($_FILES[$fileid]['name'] != "") {
         $file_result[$fileid] = $group->group_media_upload($fileid, $groupalbum_info['groupalbum_id'], $space_left);
         if ($file_result[$fileid]['is_error'] == 0) {
             $file_result[$fileid]['message'] = 2000248;
             $media_path = str_replace('./', '', $group->group_dir($group->group_info['group_id']) . $file_result[$fileid]['groupmedia_id'] . "_thumb.jpg");
             $media_link = "group_album_file.php?group_id={$group->group_info['group_id']}&groupmedia_id={$file_result[$fileid]['groupmedia_id']}";
             if (file_exists($media_path)) {
                 $media_width = $misc->photo_size($media_path, "100", "100", "w");
                 $media_height = $misc->photo_size($media_path, "100", "100", "h");
                 $action_media[] = array('media_link' => $media_link, 'media_path' => $media_path, 'media_width' => $media_width, 'media_height' => $media_height);
             }
             $update_album = 1;
         } else {
             $file_result[$fileid]['message'] = $file_result[$fileid]['is_error'];
         }
         SE_Language::_preload($file_result[$fileid]['message']);
     }
 }
 // UPDATE ALBUM UPDATED DATE AND ALBUM COVER IF FILE UPLOADED
 if ($update_album) {
function he_wall_group_photo_upload($group_id, $media_desc, $new_action_id)
{
    global $user, $url, $misc, $setting;
    $group = new se_group($user->user_info['user_id'], $group_id);
    // CHECK IF USER IS ALLOWED TO UPLOAD PHOTOS
    $privacy_max = $group->group_privacy_max($user);
    if (!($privacy_max & $group->group_info['group_privacy']) || !($privacy_max & $group->group_info['group_upload'])) {
        $result = array('result' => 0, 'error' => SE_Language::get(690706101));
    }
    $album_id = he_wall::get_group_album($group_id);
    // GET TOTAL SPACE USED
    $space_used = $group->group_media_space();
    if ($group->groupowner_level_info['level_group_album_storage']) {
        $space_left = $group->groupowner_level_info['level_group_album_storage'] - $space_used;
    } else {
        $space_left = $dfs = disk_free_space("/") ? $dfs : pow(2, 32);
    }
    $update_album = 0;
    $file_result = $group->group_media_upload('wall_photo', $album_id, $space_left);
    if ($file_result['is_error'] == 0) {
        $media_path = str_replace('./', '', $group->group_dir($group->group_info['group_id']) . $file_result['groupmedia_id'] . "_thumb.jpg");
        $media_link = "group_album_file.php?group_id={$group->group_info['group_id']}&groupmedia_id={$file_result['groupmedia_id']}";
        if (file_exists($media_path)) {
            $thumb_width = (int) $setting['setting_he_wall_photo_thumb_width'];
            $thumb_height = (int) $setting['setting_he_wall_photo_thumb_height'];
            $media_width = $misc->photo_size($media_path, $thumb_width, $thumb_height, "w");
            $media_height = $misc->photo_size($media_path, $thumb_width, $thumb_height, "h");
            $result = array('result' => 1, 'media_path' => $media_path, 'media_width' => $media_width, 'media_height' => $media_height);
        }
        // UPDATE ALBUM UPDATED DATE
        he_wall::update_group_album($album_id);
        he_wall::update_group_album_media($file_result['groupmedia_id'], $media_desc);
        he_wall::add_group_album_media($new_action_id, $file_result['groupmedia_id']);
        // UPDATE LAST UPDATE DATE (SAY THAT 10 TIMES FAST)
        $group->group_lastupdate();
    } else {
        $error_msg = sprintf(SE_Language::_get($file_result['is_error']), $file_result['file_name']);
        $result = array('result' => 0, 'error' => $error_msg);
    }
    return $result;
}