function show_downloads_options()
        {
            global $action, $page, $M_options;
            $messages = array();
            $messages[1] = __('Your options have been updated.', 'membership');
            ?>
				<div class="icon32" id="icon-options-general"><br></div>
				<h2><?php 
            _e('Download / Media Options', 'membership');
            ?>
</h2>

				<?php 
            if (isset($_GET['msg'])) {
                echo '<div id="message" class="updated fade"><p>' . $messages[(int) $_GET['msg']] . '</p></div>';
                $_SERVER['REQUEST_URI'] = remove_query_arg(array('message'), $_SERVER['REQUEST_URI']);
            }
            ?>

				<div id="poststuff" class="metabox-holder m-settings">
				<form action='' method='post'>

					<input type='hidden' name='page' value='<?php 
            echo $page;
            ?>
' />
					<input type='hidden' name='action' value='updateoptions' />

					<?php 
            wp_nonce_field('update-membership-options');
            ?>

					<div class="postbox">
						<h3 class="hndle" style='cursor:auto;'><span><?php 
            _e('Downloads / Media protection', 'membership');
            ?>
</span></h3>
						<div class="inside">

							<p class='description'><?php 
            _e('Downloads and media files can be protected by remapping their perceived location.', 'membership');
            ?>
</p>
							<p class='description'><?php 
            _e('Note: If a user determines a files actual location on your server, there is very little we can do to prevent its download, so please be careful about giving out URLs.', 'membership');
            ?>
</p>

							<table class="form-table">
							<tbody>
								<tr valign="top">
									<th scope="row"><?php 
            _e('Protection method', 'membership');
            ?>
										<?php 
            echo $this->_tips->add_tip(__('The method of protection can be changed depending on your needs. Membership offers three methods, <strong>Basic</strong> masks your media directory but leaves any filenames the same, <strong>Complete</strong> masks the media directory and changes the image filename as well and <strong>Hybrid</strong> is for use if you are using a host or server that has a problem with the system (such as some installs of nginx).', 'membership'));
            ?>
									</th>
									<td>
										<?php 
            if (empty($M_options['protection_type'])) {
                $M_options['protection_type'] = 'basic';
            }
            ?>
										<input type='radio' name='protection_type' value='basic' <?php 
            checked($M_options['protection_type'], 'basic');
            ?>
 />&nbsp;&nbsp;<?php 
            echo __('Basic protection', 'membership');
            ?>
<br/>
										<input type='radio' name='protection_type' value='complete' <?php 
            checked($M_options['protection_type'], 'complete');
            ?>
/>&nbsp;&nbsp;<?php 
            echo __('Complete protection', 'membership');
            ?>
<br/>
										<input type='radio' name='protection_type' value='hybrid' <?php 
            checked($M_options['protection_type'], 'hybrid');
            ?>
/>&nbsp;&nbsp;<?php 
            echo __('Hybrid protection', 'membership');
            ?>
<br/>
									</td>
								</tr>
								<tr valign="top">
									<th scope="row"><?php 
            _e('Your uploads location', 'membership');
            ?>
										<?php 
            echo $this->_tips->add_tip(__('This is where membership thinks you have your images stored, if this is not correct then download protection may not work correctly.', 'membership'));
            ?>
									</th>
									<td>
										<?php 
            echo membership_upload_url();
            ?>
									</td>
								</tr>
								<tr valign="top">
									<th scope="row"><?php 
            _e('Masked download URL', 'membership');
            ?>
										<?php 
            echo $this->_tips->add_tip(__('This is the URL that the user will see. You can change the end part to something unique.', 'membership'));
            ?>
									</th>
									<td>
										<?php 
            if (!isset($M_options['masked_url'])) {
                $M_options['masked_url'] = '';
            }
            esc_html_e(trailingslashit(get_option('home')));
            ?>
&nbsp;<input type='text' name='masked_url' id='masked_url' value='<?php 
            esc_attr_e($M_options['masked_url']);
            ?>
' />&nbsp;/
									</td>
								</tr>

								<tr valign="top">
									<th scope="row"><?php 
            _e('Protected groups', 'membership');
            ?>
									<?php 
            echo $this->_tips->add_tip(__('Place each download group name on a new line, removing used groups will leave content visible to all users/members.', 'membership'));
            ?>
									</th>
									<td>
										<textarea name='membershipdownloadgroups' id='membershipdownloadgroups' rows='10' cols='40'><?php 
            if (!empty($M_options['membershipdownloadgroups'])) {
                foreach ($M_options['membershipdownloadgroups'] as $key => $value) {
                    if (!empty($value)) {
                        esc_html_e(stripslashes($value)) . "\n";
                    }
                }
            }
            ?>
</textarea>
									</td>
								</tr>

							</tbody>
							</table>
						</div>
					</div>

					<?php 
            do_action('membership_downloadsoptions_page');
            ?>

					<p class="submit">
						<input type="submit" name="Submit" class="button-primary" value="<?php 
            esc_attr_e('Save Changes', 'membership');
            ?>
" />
					</p>

				</form>
				</div>
			<?php 
        }
 function protect_download_content($the_content)
 {
     global $M_options;
     $origpath = membership_upload_url();
     $newpath = trailingslashit(trailingslashit(get_option('home')) . $M_options['masked_url']);
     // Find all the urls in the post and then we'll check if they are protected
     /* Regular expression from http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the */
     $url_exp = '/((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\\$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?(?:[.\\!\\/\\w]*))?)/';
     $matches = array();
     if (preg_match_all($url_exp, $the_content, $matches)) {
         $home = get_option('home');
         if (!empty($matches) && !empty($matches[2])) {
             foreach ((array) $matches[2] as $key => $domain) {
                 if (untrailingslashit($home) == untrailingslashit($domain)) {
                     $foundlocal = $key;
                     $file = basename($matches[4][$foundlocal]);
                     $filename_exp = '/(.+)\\-(\\d+[x]\\d+)\\.(.+)$/';
                     $filematch = array();
                     if (preg_match($filename_exp, $file, $filematch)) {
                         // We have an image with an image size attached
                         $newfile = $filematch[1] . "." . $filematch[3];
                         $size_extension = "-" . $filematch[2];
                     } else {
                         $newfile = $file;
                         $size_extension = '';
                     }
                     $sql = $this->db->prepare("SELECT post_id FROM {$this->db->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value LIKE %s", '%' . $newfile . '%');
                     $post_id = $this->db->get_var($sql);
                     if (empty($post_id)) {
                         // Can't find the file in the first pass, try the second pass.
                         $sql = $this->db->prepare("SELECT post_id FROM {$this->db->postmeta} WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s", '%' . $file . '%');
                         $post_id = $this->db->get_var($sql);
                     }
                     if (!empty($post_id)) {
                         // Found the file and it's in the media library
                         $protected = get_post_meta($post_id, '_membership_protected_content_group', true);
                         if (!empty($protected)) {
                             // We have a protected file - so we'll mask it
                             switch ($M_options['protection_type']) {
                                 case 'complete':
                                     $protectedfilename = MEMBERSHIP_FILE_NAME_PREFIX . ($post_id + (int) MEMBERSHIP_FILE_NAME_INCREMENT) . $size_extension;
                                     $protectedfilename .= "." . pathinfo($newfile, PATHINFO_EXTENSION);
                                     $the_content = str_replace($matches[0][$foundlocal], $newpath . $protectedfilename, $the_content);
                                     break;
                                 case 'hybrid':
                                     $protectedfilename = MEMBERSHIP_FILE_NAME_PREFIX . ($post_id + (int) MEMBERSHIP_FILE_NAME_INCREMENT) . $size_extension;
                                     $protectedfilename .= "." . pathinfo($newfile, PATHINFO_EXTENSION);
                                     $the_content = str_replace($matches[0][$foundlocal], $newpath . "?file=" . $protectedfilename, $the_content);
                                     break;
                                 case 'basic':
                                 default:
                                     $the_content = str_replace($matches[0][$foundlocal], str_replace($origpath, $newpath, $matches[0][$foundlocal]), $the_content);
                                     break;
                             }
                         }
                     }
                 }
             }
         }
     }
     return $the_content;
 }