function edit()
 {
     global $wpdb;
     // trim
     array_map('trim', $_POST);
     // extract
     extract($_POST);
     // system
     $system_obj = mgm_get_class('system');
     // save
     if (isset($submit_download)) {
         // response
         $response = array('status' => 'error', 'message' => sprintf(__('Error while updating download <b>%s</b>!', 'mgm'), $title));
         // check duplicate
         if (mgm_is_duplicate(TBL_MGM_DOWNLOAD, array('title'), "id <> '{$id}'")) {
             $response['message'] = sprintf(__('Error while updating download <b>%s</b>, title exists!', 'mgm'), $title);
         } else {
             // set vars
             $members_only = isset($members_only) ? 'Y' : 'N';
             // set vars
             $restrict_acces_ip = isset($restrict_acces_ip) ? 'Y' : 'N';
             // is_s3_torrent
             $is_s3_torrent = isset($is_s3_torrent) ? 'Y' : 'N';
             // filename
             $filename = isset($download_file_new) ? $download_file_new : $direct_url;
             // real name
             $real_filename = isset($download_file_new_realname) ? $download_file_new_realname : basename($filename);
             // filesize
             $filesize = mgm_file_get_size($filename);
             // post vars
             $post_date = date('Y-m-d H:i:s');
             // user
             $current_user = wp_get_current_user();
             // data
             $data = array('title' => $title, 'filename' => $filename, 'real_filename' => $real_filename, 'filesize' => $filesize, 'post_date' => $post_date, 'restrict_acces_ip' => $restrict_acces_ip, 'user_id' => $current_user->ID, 'members_only' => $members_only, 'is_s3_torrent' => $is_s3_torrent);
             // null
             $null_columns = array();
             // download limit
             if (isset($download_limit) && (int) $download_limit > 0) {
                 $data['download_limit'] = (int) $download_limit;
             } else {
                 $null_columns[] = "`download_limit` = NULL ";
             }
             // expire date
             if (isset($expire_dt) && !empty($expire_dt)) {
                 $data['expire_dt'] = mgm_format_inputdate_to_mysql($expire_dt);
             } else {
                 $null_columns[] = "`expire_dt` = NULL ";
             }
             // code
             if (!isset($code) || isset($code) && empty($code)) {
                 $data['code'] = uniqid();
             }
             // update
             if ($wpdb->update(TBL_MGM_DOWNLOAD, $data, array('id' => $id))) {
                 // update null
                 if (count($null_columns) > 0) {
                     // join
                     $set_string = implode(',', $null_columns);
                     // clear old
                     $wpdb->query($wpdb->prepare('UPDATE `' . TBL_MGM_DOWNLOAD . '` SET ' . $set_string . ' WHERE `id` = %d', $id));
                 }
                 // clear old
                 $wpdb->query($wpdb->prepare('DELETE FROM `' . TBL_MGM_DOWNLOAD_POST_ASSOC . '` WHERE `download_id` = %d', $id));
                 // save
                 if (bool_from_yn($members_only)) {
                     if (isset($link_to_post_id)) {
                         // loop
                         foreach ($link_to_post_id as $post_id) {
                             // insert
                             $wpdb->insert(TBL_MGM_DOWNLOAD_POST_ASSOC, array('download_id' => $id, 'post_id' => $post_id));
                         }
                     }
                 }
                 // set message
                 $response['message'] = sprintf(__('Download updated successfully <b>%s</b>', 'mgm'), $title);
                 $response['status'] = 'success';
             } else {
                 $response['message'] = sprintf(__('Error while updating download <b>%s</b> Or nothing updated!', 'mgm'), $title);
             }
         }
         // return response
         echo json_encode($response);
         exit;
     }
     // data
     $data = array();
     // download
     $data['download'] = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . TBL_MGM_DOWNLOAD . "` WHERE id = %d", $id));
     // download_posts
     $data['download_posts'] = mgm_get_download_post_ids($id);
     // get all post types
     $post_types = mgm_get_post_types();
     //get all published posts - issue #1034
     $all_posts = mgm_field_values($wpdb->posts, 'ID', 'SUBSTR(post_title,1, 100) AS post_title', "AND `post_status` ='publish' AND `post_type` IN ({$post_types})", 'post_title');
     //get all scheduled posts
     $scheduled_posts = mgm_field_values($wpdb->posts, 'ID', 'SUBSTR(post_title,1, 100) AS post_title', "AND `post_status` ='future' AND `post_type` IN ({$post_types})", 'post_title');
     foreach ($scheduled_posts as $k => $scheduled_post) {
         $all_posts[$k] = $scheduled_post . '(S)';
     }
     //get all draft posts
     $draft_posts = mgm_field_values($wpdb->posts, 'ID', 'SUBSTR(post_title,1, 100) AS post_title', "AND `post_status` ='draft' AND `post_type` IN ({$post_types})", 'post_title');
     foreach ($draft_posts as $k => $draft_post) {
         $all_posts[$k] = $draft_post . '(D)';
     }
     //sort by post name
     asort($all_posts);
     //all posts
     $data['posts'] = $all_posts;
     // hook
     $data['download_hook'] = $system_obj->get_setting('download_hook', 'download');
     // slug
     $data['download_slug'] = $system_obj->get_setting('download_slug', 'download');
     // load template view
     $this->load->template('downloads/edit', array('data' => $data));
 }
/**
 * Magic Members verify file download
 *
 * @package MagicMembers
 * @since 2.5
 * @desc verify file download
 * @param string download code
 * @return none
 */
function mgm_download_file($code)
{
    global $wpdb;
    // current_user
    $current_user = wp_get_current_user();
    // system
    $system_obj = mgm_get_class('system');
    // url
    $no_access_redirect_download = $system_obj->get_setting('no_access_redirect_download');
    // redirect
    $do_redirect = empty($no_access_redirect_download) ? false : true;
    // allow default
    $allow_download = true;
    // data fetch
    if ($download = mgm_get_download_data($code)) {
        // for members
        if (bool_from_yn($download->members_only)) {
            // reset as restricted
            $allow_download = false;
            // user check
            if ($current_user->ID) {
                // allow admin
                if (is_super_admin()) {
                    // is_super_admin
                    $allow_download = true;
                } else {
                    // get post mapped
                    $posts = mgm_get_download_post_ids($download->id);
                    // loop
                    foreach ($posts as $post_id) {
                        // only  when user has access to mapped post
                        if (mgm_user_has_access($post_id)) {
                            // set access
                            $allow_download = true;
                            // skip
                            break;
                        }
                    }
                    //check download included in guest restrict via post/page access issue #1609
                    if (!$allow_download && isset($_REQUEST['guest_token']) && isset($_REQUEST['post_id'])) {
                        // only  when user has access to mapped post
                        if (mgm_user_has_access($_REQUEST['post_id'])) {
                            // set access
                            $allow_download = true;
                        }
                    }
                    // download limit user member access issue #902
                    if (!empty($download->download_limit) && (int) $download->download_limit > 0 && $allow_download) {
                        $download_limit = mgm_download_user_limit_check($download->id);
                        if (empty($download_limit)) {
                            mgm_download_user_limit_insert($download->id);
                        } else {
                            if ($download_limit->count < $download->download_limit) {
                                // count
                                $count = $download_limit->count + 1;
                                // update
                                mgm_download_user_limit_update($download->id, $count);
                            } else {
                                $allow_download = false;
                                // redirect
                                if ($do_redirect) {
                                    mgm_redirect(add_query_arg(array('error_code' => 1), $no_access_redirect_download));
                                }
                                // show mesage if redirect does not set
                                mgm_download_error(1);
                                exit;
                            }
                        }
                    }
                }
            } else {
                //check download included in guest restrict via post/page access issue #1609
                if (!$allow_download && isset($_REQUEST['guest_token']) && isset($_REQUEST['post_id'])) {
                    // only  when user has access to mapped post
                    if (mgm_user_has_access($_REQUEST['post_id'])) {
                        // set access
                        $allow_download = true;
                    }
                }
            }
            // end member restriction check
        } else {
            // download limit user member access issue #902
            if ($current_user->ID) {
                // download limit user member access issue #902
                if (!empty($download->download_limit) && (int) $download->download_limit > 0) {
                    $download_limit = mgm_download_user_limit_check($download->id);
                    if (empty($download_limit)) {
                        mgm_download_user_limit_insert($download->id);
                    } else {
                        if ($download_limit->count < $download->download_limit) {
                            $count = $download_limit->count + 1;
                            mgm_download_user_limit_update($download->id, $count);
                        } else {
                            $allow_download = false;
                            // redirect
                            if ($do_redirect) {
                                mgm_redirect(add_query_arg(array('error_code' => 1), $no_access_redirect_download));
                            }
                            // show mesage if redirect does not set
                            mgm_download_error(1);
                            exit;
                        }
                    }
                }
            } else {
                if (bool_from_yn($download->restrict_acces_ip)) {
                    // download limit ip member access issue #902
                    if (!empty($download->download_limit) && (int) $download->download_limit > 0) {
                        $download_limit = mgm_download_ip_limit_check($download->id);
                        if (empty($download_limit)) {
                            mgm_download_ip_limit_insert($download->id);
                        } else {
                            if ($download_limit->count < $download->download_limit) {
                                $count = $download_limit->count + 1;
                                mgm_download_ip_limit_update($download->id, $count);
                            } else {
                                $allow_download = false;
                                // redirect
                                if ($do_redirect) {
                                    mgm_redirect(add_query_arg(array('error_code' => 1), $no_access_redirect_download));
                                }
                                // show mesage if redirect does not set
                                mgm_download_error(1);
                                exit;
                            }
                        }
                    }
                }
            }
        }
        // check expire
        $download_expired = false;
        // allowed alreay
        if ($allow_download) {
            // expire date
            if (!is_null($download->expire_dt)) {
                // expired
                if (intval($download->expire_dt) && time() > strtotime($download->expire_dt)) {
                    $download_expired = true;
                }
            }
        }
        // allowed
        if ($allow_download && !$download_expired) {
            // check if s3 resource
            if (mgm_is_s3_file($download->filename)) {
                //decode - issue #1727
                $download->filename = urldecode($download->filename);
                // expired
                $aws_qsa_expires = $system_obj->get_setting('aws_qsa_expires', '1 HOUR');
                // check if torrent
                if (bool_from_yn($download->is_s3_torrent)) {
                    // redirect to amazon secure url
                    if ($torent_url = mgm_get_s3torent_url($download->filename, $aws_qsa_expires)) {
                        wp_redirect($torent_url);
                        exit;
                    }
                } else {
                    // check
                    if (bool_from_yn($system_obj->get_setting('aws_enable_qsa', 'N'))) {
                        // redirect to amazon secure url
                        if ($token_url = mgm_get_s3token_url($download->filename, $aws_qsa_expires)) {
                            wp_redirect($token_url);
                            exit;
                        }
                    }
                }
                // download as usual
                mgm_stream_download_s3($download->filename);
                exit;
            } else {
                // filepath
                $filepath = mgm_get_abs_file($download->filename);
                // check
                if (file_exists($filepath)) {
                    // do the  download
                    mgm_stream_download($filepath);
                    // delete if s3 file
                    if (mgm_is_s3_file($filepath)) {
                        // old code kept
                        // delete
                        mgm_delete_file($filepath);
                    }
                    // exit
                    exit;
                } else {
                    // redirect
                    if ($do_redirect) {
                        mgm_redirect(add_query_arg(array('error_code' => 2), $no_access_redirect_download));
                    }
                    // show mesage if redirect does not set
                    mgm_download_error(2);
                    exit;
                }
            }
        } else {
            // redirect
            $code = $download_expired ? '3' : '4';
            // redirect
            if ($do_redirect) {
                mgm_redirect(add_query_arg(array('error_code' => $code), $no_access_redirect_download));
            }
            // show mesage if redirect does not set
            mgm_download_error($code);
            exit;
        }
    } else {
        // redirect
        if ($do_redirect) {
            mgm_redirect(add_query_arg(array('error_code' => 4), $no_access_redirect_download));
        }
        // show mesage if redirect does not set
        mgm_download_error(4);
        exit;
    }
}