<?php/** * Secure Download File */if(isset($_GET['secure_download'])){secure_download_file($_GET['secure_download'],'');}/** * Encrypt All Attachment Links */add_action('template_redirect','encrypt_all_attachment_links');functionencrypt_all_attachment_links(){ob_start(function($buffer){return preg_replace_callback('|/wp-content/uploads/\d{4}/[^\s"\']+(\.[^\s"\')]+)|',function($matches){$file_url= get_site_url().$matches[0];return encrypt_file_url($file_url,false,'');},$buffer);});}/** * Method Encrypt File Url * * This method will encrypt the file url. * * @paramstring $attachment_url The url of the file. * @parambool $site_url_prefix (Optional) Should it add the site_url() before the file path? Default is true. * @paramstring $ciphering (Optional) The encryption algorithm. Leave it empty for use base64. * @paramstring $key (Optional) The encryption key. * @returnEncrypted_URL The encrypted url. */functionencrypt_file_url(string$attachment_url,bool$site_url_prefix=true,string$ciphering='AES-128-CBC',string$key='YOUR_KEY_HERE'){// Parse the URL into components$attachment_arr=parse_url($attachment_url,-1);// Store the scheme and host into a separate variable$attachment_site=$attachment_arr['scheme'].'://'.$attachment_arr['host'].'/';// Store the path component of the URL$attachment_path=$attachment_arr['path'];// Encrypt the path component of the URLif(empty($ciphering)){//Just use base64$encrypted_url=base64_encode($attachment_path);}else{//Use ciphering algorithm$encrypted_url=openssl_encrypt($attachment_path,$ciphering,$key,0, base64_encode($key));//Use base64 for becoming url friendly$encrypted_url=base64_encode($encrypted_url);}// Concatenate the scheme and host with the encrypted path and return the resultif($site_url_prefix){return$attachment_site.'?secure_download='.$encrypted_url;}else{return'?secure_download='.$encrypted_url;}}/** * Method Secure Download File * * This method will decrypt the file path and view or download it. * * @paramstring $file_hash The encrypted url. * @paramstring $ciphering (Optional) The encryption algorithm. Leave it empty to use base64. * @paramstring $key (Optional) The encryption key. * @paramstring $disposition (Optional) The beheaviour: inline means that it will open in the browser / attachment will download the file. * @returnvoid */functionsecure_download_file(string$file_hash,string$ciphering='AES-128-CBC',string$key='YOUR_KEY_HERE',string$disposition='inline'){//Decode base64$file_hash_decoded=base64_decode($file_hash);//Decrypt path to fileif(empty($ciphering)){$decrypted_url=$file_hash_decoded;}else{//Decrypt using cipher$decrypted_url=openssl_decrypt($file_hash_decoded,$ciphering,$key,0, base64_encode($key));}//Build full file path$file_path=site_url().$decrypted_url;//Download fileheader('Content-Description: File Transfer');header('Content-Type: application/octet-stream');header('Content-Disposition: '.$disposition.'; filename="'.basename($file_path).'"');header('Expires: 0');header('Cache-Control: must-revalidate');header('Pragma: public');//Ignore SSLstream_context_set_default(['ssl'=>['verify_peer'=>false,'verify_peer_name'=>false,],]);readfile($file_path);}
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.