function delete()
 {
     global $wpdb;
     // extract
     extract($_POST);
     // get file name
     $filename = $wpdb->get_var($wpdb->prepare("SELECT `filename` FROM `" . TBL_MGM_DOWNLOAD . "` WHERE id = %d", $id));
     // check s3
     if (!mgm_is_s3_file($filename)) {
         // delete file if locally stored
         mgm_delete_file(MGM_FILES_DOWNLOAD_DIR . basename($filename));
     }
     // delete
     $wpdb->query($wpdb->prepare('DELETE FROM `' . TBL_MGM_DOWNLOAD . '`	WHERE id = %d', $id));
     $wpdb->query($wpdb->prepare('DELETE FROM `' . TBL_MGM_DOWNLOAD_POST_ASSOC . '` WHERE `download_id` = %d', $id));
     $wpdb->query($wpdb->prepare('DELETE FROM `' . TBL_MGM_DOWNLOAD_LIMIT_ASSOC . '` WHERE `download_id` = %d', $id));
     // return response
     echo json_encode(array('status' => 'success', 'message' => __('Download deleted successfully', 'mgm')));
     exit;
 }
 function module_file_upload()
 {
     // name of module
     $module = mgm_request_var('module', '', true);
     // file
     $file_element = 'logo_' . $module;
     // init
     $logo = array();
     // init messages
     $status = 'error';
     $message = __('Logo upload failed.', 'mgm');
     // upload check
     if (is_uploaded_file($_FILES[$file_element]['tmp_name'])) {
         // random filename
         $uniquename = substr(microtime(), 2, 8);
         // paths
         $oldname = strtolower($_FILES[$file_element]['name']);
         $newname = preg_replace('/(.*)\\.(png|jpg|jpeg|gif)$/i', $uniquename . '.$2', $oldname);
         $filepath = MGM_FILES_MODULE_DIR . $newname;
         // upload
         if (move_uploaded_file($_FILES[$file_element]['tmp_name'], $filepath)) {
             // get thumb
             $thumb = image_make_intermediate_size(MGM_FILES_MODULE_DIR . $newname, 100, 100);
             // set logo
             if ($thumb) {
                 $logo = array('image_name' => $thumb['file'], 'image_url' => MGM_FILES_MODULE_URL . $thumb['file']);
                 // remove main file, we dont need it
                 mgm_delete_file($filepath);
             } else {
                 $logo = array('image_name' => $newname, 'image_url' => MGM_FILES_MODULE_URL . $newname);
             }
             // status
             $status = 'success';
             $message = __('logo uploaded successfully, it will be attached when you update the settings.', 'mgm');
         }
     }
     // send ouput
     @ob_end_clean();
     // PRINT
     echo json_encode(array('status' => $status, 'message' => $message, 'logo' => $logo));
     // end out put
     @ob_flush();
     exit;
 }
/**
 * 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;
    }
}