└── download_content ├── README.md ├── addon.setup.php └── pi.download_content.php /download_content/README.md: -------------------------------------------------------------------------------- 1 | # Download Content Plugin 2 | 3 | This plugin takes the content within the tag pair and forces a file download in the web browser with its contents, allowing you to turn template output into downloadable files. 4 | 5 | ## Usage 6 | 7 | ### {exp:download_content filename="foo.txt"} 8 | 9 | To use this plugin, place the plugin tag around the content that you wish to send to the browser as a file download, supplying the desired file name. Optionally, the file can also be saved to disk on the server. 10 | 11 | **Note:** When this plugin tag is processed, all other processing on the template will immediately cease and whatever state the content is in will be sent to the browser. This may influence where you use this tag in relation to your layouts, for instance. 12 | 13 | #### Example Usage 14 | 15 | ``` 16 | {exp:download_content filename="foo.txt"} 17 | Some random content, maybe from a channel tag. 18 | {/exp:download_content} 19 | ``` 20 | 21 | #### Available Parameters 22 | 23 | ##### filename 24 | 25 | Name of the file you want the browser download to be named. 26 | 27 | ##### save_to_server_path 28 | 29 | Full server path to a writable directory on the server. If the directory does not exist, the plugin will attempt to create it. Overwrites identically named files. If you do not wish to overwrite existing files, ensure that you are using unique filenames. If the plugin is unable to save the file, it will abort and will not prompt a browser download either. To debug, turn on debugging in your Debugging & Output preferences and look for an error in the Template Log. 30 | 31 | **Warning:** If you use dynamically unique filenames with this plugin on a publicly accessible template, stored files could grow out of control or even be a target of a DoS attack designed to run your web server out of disk space. It is recommended that if you save to a server path at all that you have external mechanisms in place to manage the permanent storage of these files. 32 | 33 | ## Change Log 34 | 35 | ### 3.1.0 36 | 37 | - Changed the plugin namespace to make it EE6 compatible. 38 | 39 | ### 3.0.1 40 | 41 | - Correcting spelling and documentation errors. 42 | 43 | ### 3.0 44 | 45 | - Updated plugin to be 3.0 compatible 46 | - Added ability to save copies of the file to the server 47 | 48 | ## License 49 | 50 | Copyright (C) 2011 - 2021 Packet Tide, LLC. 51 | 52 | Permission is hereby granted, free of charge, to any person obtaining a copy 53 | of this software and associated documentation files (the "Software"), to deal 54 | in the Software without restriction, including without limitation the rights 55 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 56 | copies of the Software, and to permit persons to whom the Software is 57 | furnished to do so, subject to the following conditions: 58 | 59 | The above copyright notice and this permission notice shall be included in 60 | all copies or substantial portions of the Software. 61 | 62 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 63 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 64 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 65 | PACKET TIDE, LLC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 66 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 67 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 68 | 69 | Except as contained in this notice, the name of Packet Tide, LLC. shall not be 70 | used in advertising or otherwise to promote the sale, use or other dealings 71 | in this Software without prior written authorization from Packet Tide, LLC. 72 | -------------------------------------------------------------------------------- /download_content/addon.setup.php: -------------------------------------------------------------------------------- 1 | 'Packet Tide', 5 | 'author_url' => 'https://packettide.com/', 6 | 'name' => 'Download Content', 7 | 'description' => 'Takes the content within the tag pair and forces a file download in the web browser with its contents.', 8 | 'version' => '3.1.0', 9 | 'namespace' => 'ExpressionEngine\Addons\DownloadContent', 10 | 'settings_exist' => FALSE, 11 | 'plugin.typography' => FALSE 12 | ); 13 | -------------------------------------------------------------------------------- /download_content/pi.download_content.php: -------------------------------------------------------------------------------- 1 | TMPL->tagdata); 49 | $filename = ee()->TMPL->fetch_param('filename'); 50 | $save_path = ee()->TMPL->fetch_param('save_to_server_path'); 51 | 52 | if ($filename == '') 53 | { 54 | ee()->TMPL->log_item('Download Content: No filename given, aborting download'); 55 | return FALSE; 56 | } 57 | 58 | // do some final prep on the tagdata 59 | $data = ee()->TMPL->advanced_conditionals($data); 60 | $data = ee()->TMPL->parse_globals($data); 61 | 62 | // cleanup any stray annotations or comments 63 | $data = preg_replace("/\{!--.*?--\}/s", '', $data); 64 | 65 | if ($save_path !== FALSE) 66 | { 67 | $path = realpath($save_path); 68 | 69 | if ( ! @is_dir($path)) 70 | { 71 | if ( ! @mkdir($path) OR ! is_really_writeable($path)) 72 | { 73 | ee()->TMPL->log_item('Download Content: Save path does not exist, could not create, or is not writeable: '.htmlentities($save_path)); 74 | return FALSE; 75 | } 76 | } 77 | 78 | ee()->load->helper('file'); 79 | write_file($path.'/'.$filename, $data); 80 | } 81 | 82 | ee()->load->helper('download'); 83 | force_download($filename, $data); 84 | } 85 | 86 | // -------------------------------------------------------------------- 87 | 88 | } 89 | // END CLASS 90 | 91 | // EOF 92 | --------------------------------------------------------------------------------