Plugin Directory

Changeset 1169857


Ignore:
Timestamp:
05/29/2015 05:30:04 AM (11 years ago)
Author:
tcmccarthy1
Message:

Big updates to allow for the use of environment variables, feedback for misconfiguration, and the deactivation of fallback URLs allowing URLs to point directly at S3

Location:
tcs3
Files:
823 added
2 edited

Legend:

Unmodified
Added
Removed
  • tcs3/trunk/readme.txt

    r991421 r1169857  
    11=== tcS3 ===
    22Contributors: tcmccarthy1
    3 Tags: Amazon, S3, upload, media, multisite
     3Tags: Amazon, S3, upload, media, multisite, aws
    44Requires at least: 3.5
    5 Tested up to: 4.0
    6 Stable tag: 1.1
     5Tested up to: 4.2.2
     6Stable tag: 1.5
    77License: GPL, version 2
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6060
    6161== Changelog ==
    62  
     62
     63= 1.5 =
     64* Adds fallback option where users can opt to use the tcS3_media endpoint OR link directly to their S3 bucket
     65* Increases hook priority to 20 to allow for various image editing plugins to have their work pushed to S3
     66* Adds support for the use of environment variables to store AWS keys and secrets
     67
    6368= 1.1 =
    6469* Bug fix where some environments don't show menu options for plugin when plugin installed from the Wordpress repository
  • tcs3/trunk/tcS3.php

    r991411 r1169857  
    22/**
    33 * Plugin Name: TCS3 -- Send uploads directly to S3
    4  * Plugin URI: http://www.tc-mccarthy.com
     4 * Plugin URI: http://tcm.io
    55 * Description: Allows site admins to push uploads to S3
    6  * Version: 1.1
     6 * Version: 1.5
    77 * Author: TC McCarthy
    8  * Author URI: http://www.tc-mccarthy.com
     8 * Author URI: http://tcm.io
    99 * License: GPL2
    1010 */
     
    3838        }
    3939
     40
    4041        //setup options
    4142        $this->networkActivated = $this->network_activation_check();
    4243        $this->options = ($this->networkActivated) ? get_site_option("tcS3_options") : get_option("tcS3_options");
    43         $use_S3 = ($this->options["access_key"] != "" && $this->options["access_secret"] != "" && $this->options["bucket"] != "" && $this->options["bucket_path"] != "" && $this->options["bucket_region"] != "") ? true : false;
     44
     45        $this->accessKey = ($this->options["env_toggle"] == 1) ? getenv($this->options["access_key_variable"]) : $this->options["access_key"];
     46        $this->accessSecret = ($this->options["env_toggle"] == 1) ? getenv($this->options["access_secret_variable"]) : $this->options["access_secret"];
     47       
     48
     49        $use_S3 = ($this->accessKey != "" && $this->accessSecret != "" && $this->options["bucket"] != "" && $this->options["bucket_path"] != "" && $this->options["bucket_region"] != "") ? true : false;
    4450
    4551        //setup admin menu
     
    5662            //send new uploads to S3
    5763            add_action('add_attachment', array($this, 'create_on_S3')); //for non-images
    58             add_filter('wp_generate_attachment_metadata', array($this, 'create_image_on_S3')); //for images
     64            add_filter('wp_generate_attachment_metadata', array($this, 'create_image_on_S3'), 20, 5); //for images
    5965
    6066            //send delete requests to S3
     
    6268
    6369            //send crop/rotate/file changes to S3
    64             add_filter('wp_update_attachment_metadata', array($this, 'update_on_s3'), 10, 5);
     70            add_filter('wp_update_attachment_metadata', array($this, 'update_on_s3'), 20, 5);
    6571
    6672            //add new column to media library
     
    94100            add_action("wp_ajax_get_attachment_ids", array($this, "tcS3_ajax_get_attachment_ids"));
    95101            add_action("wp_ajax_mark_all_synced", array($this, "tcS3_ajax_mark_all_synced"));
    96         }
     102        } else{
     103            add_action('admin_head', array($this, "tcS3_dashAlert"));
     104        }
     105    }
     106
     107    public function tcS3_dashAlert(){
     108        echo "<style type=\"text/css\">
     109        .alert {
     110            padding:1em;
     111            background-color:#ebfbff;
     112            border-bottom:1px solid #CCC;
     113            display:none;
     114            text-align: center;
     115            font-size: 1.5em;
     116        }
     117        </style>
     118        <script type=\"text/javascript\">
     119        \$j = jQuery;
     120        \$j().ready(function(){ //when page has fully loaded
     121          \$j('h2').parent().prev().after('<div id=\"my-plugin-alert\" class=\"alert\">Oops! It looks like you\'ve activated tcS3 but it\'s not properly configured to upload to your bucket. <a href=\"" . admin_url("options-general.php?page=tcS3-admin") . "\">Please check your settings!</a></div>');
     122          setTimeout(\"\$j('#my-plugin-alert').fadeIn('slow');clearTimeout();\",1000);
     123        });
     124        </script>";
    97125    }
    98126
    99127    public function build_aws_config() {
    100128        return array(
    101             'key' => $this->options["access_key"],
    102             'secret' => $this->options["access_secret"],
     129            'key' => $this->accessKey,
     130            'secret' => $this->accessSecret,
    103131            'region' => $this->options["bucket_region"]
    104132            );
     
    117145            "s3_url" => "",
    118146            "local_url" => "http://{$_SERVER["HTTP_HOST"]}/wp-content/",
     147            "tcS3_fallback" => 1,
    119148            "s3_cache_time" => 172800,
    120149            "s3_permalink_reset" => 0,
    121150            "s3_redirect_cache_time" => 86400,
    122             "s3_redirect_cache_memcached" => ""
     151            "s3_redirect_cache_memcached" => "",
     152            "access_key_variable" => "",
     153            "access_secret_variable" => "",
     154            "env_toggle" => 0
    123155            );
    124156
     
    510542        preg_match("/\/([0-9]+\/[0-9]+\/[^\/]+)$/", $url, $matches);
    511543        $protocol = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "") ? "https" : "http";
    512         $url = $protocol . "://" . $_SERVER["HTTP_HOST"] . "/tcS3_media" . $this->uploadDir . '/' . $matches[1];
    513         return $url;
     544        if($this->options["tcS3_fallback"] == 1){
     545            $url = get_site_url() . "/tcS3_media" . $this->uploadDir . '/' . $matches[1];
     546        } else{
     547            $url = $this->options["s3_url"] . $this->uploadDir . "/" . $matches[1];
     548        }
     549       
     550        return preg_replace("/([^:])\/\//", "$1/", $url);
    514551    }
    515552
     
    614651
    615652        add_settings_field(
     653            'access_key_variable', 'AWS Access Key environment variable', array($this, 'aws_key_variable_callback'), 'tcS3-setting-admin', 'tcS3-settings'
     654            );
     655
     656        add_settings_field(
     657            'access_secret_variable', 'AWS Access Key Secret environment variable', array($this, 'aws_secret_variable_callback'), 'tcS3-setting-admin', 'tcS3-settings'
     658            );
     659
     660        add_settings_field(
     661            'env_toggle', 'Use environment variables', array($this, 'env_variable_callback'), 'tcS3-setting-admin', 'tcS3-settings'
     662            );
     663
     664        add_settings_field(
    616665            's3_bucket', 'S3 Bucket', array($this, 's3_bucket_callback'), 'tcS3-setting-admin', 'tcS3-settings'
    617666            );
     
    627676        add_settings_field(
    628677            'local_url', 'S3 URL', array($this, 's3_url_callback'), 'tcS3-setting-admin', 'tcS3-settings'
     678            );
     679
     680        add_settings_field(
     681            'tcS3_fallback', 'Use fallback', array($this, 'tcS3_fallback_callback'), 'tcS3-setting-admin', 'tcS3-settings'
    629682            );
    630683
     
    735788    }
    736789
     790    public function aws_key_variable_callback() {
     791        $optionKey = 'access_key_variable';
     792        $helperText = 'For security reasons, you may prefer to store your AWS key and secret in an environment variable instead of your DB. If that is your preference enter the name of the env variable for your key';
     793
     794        printf(
     795            '<input type="hidden" name="tcS3_option_submit" value="1" /><input type="text" id="%s" name="tcS3_option[%s]" value="%s" /><div><small>%s</small></div>', $optionKey, $optionKey, isset($this->options[$optionKey]) ? esc_attr($this->options[$optionKey]) : '', $helperText
     796            );
     797    }
     798
     799    public function aws_secret_variable_callback() {
     800        $optionKey = 'access_secret_variable';
     801        $helperText = 'For security reasons, you may prefer to store your AWS key and secret in an environment variable instead of your DB. If that is your preference enter the name of the env variable for your secret';
     802
     803        printf(
     804            '<input type="text" id="%s" name="tcS3_option[%s]" value="%s" /><div><small>%s</small></div>', $optionKey, $optionKey, isset($this->options[$optionKey]) ? esc_attr($this->options[$optionKey]) : '', $helperText
     805            );
     806    }
     807
    737808    public function s3_bucket_callback() {
    738809        $optionKey = 'bucket';
     
    826897    }
    827898
     899    public function env_variable_callback() {
     900        $optionKey = 'env_toggle';
     901        $helperText = 'For security reasons, you may prefer to store your AWS key and secret in an environment variable instead of your DB. If that is your preference toggle this to \'yes\', otherwise the key and secret entered will be used';
     902
     903        printf(
     904            '<input type="radio" id="%s" name="tcS3_option[%s]" value="1" %s /> Yes
     905            <br /><input type="radio" id="%s" name="tcS3_option[%s]" value="0" %s /> No
     906            <div><small>%s</small></div>', $optionKey, $optionKey, ($this->options[$optionKey] == 1) ? "checked" : "", $optionKey, $optionKey, ($this->options[$optionKey] == 0) ? "checked" : "", $helperText
     907            );
     908    }
     909
     910    public function tcS3_fallback_callback() {
     911        $optionKey = 'tcS3_fallback';
     912        $helperText = 'If selected tcS3 will use a tcS3_media URL to allow the plugin to check for files locally if they\'re not available on S3. This option slows down load time';
     913
     914        printf(
     915            '<input type="radio" id="%s" name="tcS3_option[%s]" value="1" %s /> Yes
     916            <br /><input type="radio" id="%s" name="tcS3_option[%s]" value="0" %s /> No
     917            <div><small>%s</small></div>', $optionKey, $optionKey, ($this->options[$optionKey] == 1) ? "checked" : "", $optionKey, $optionKey, ($this->options[$optionKey] == 0) ? "checked" : "", $helperText
     918            );
     919    }
     920
    828921    public function s3_cache_time_callback() {
    829922        $optionKey = 's3_cache_time';
Note: See TracChangeset for help on using the changeset viewer.