Plugin Directory

Changeset 403515


Ignore:
Timestamp:
07/01/2011 10:46:43 PM (15 years ago)
Author:
imthiaz
Message:

Work in progress commit
Added cache clear
Added folder prefix

Location:
wp-s3/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-s3/trunk/s3-options.php

    r402696 r403515  
    105105            <span class="description"><?php _e('No of files to upload on each cron execution.') ?></span>
    106106        </td>
    107         </tr>   
     107        </tr>
     108        <tr valign="top">
     109        <th scope="row"><label for="s3plugin_cloudfrontURL"><?php _e('Path prefix') ?></label></th>
     110        <td>
     111            <input name="s3plugin_path_prefix" type="text" id="s3plugin_path_prefix" value="<?php form_option('s3plugin_dir_prefix'); ?>" readonly="readonly" size="7" />
     112            <span class="description"><?php _e('Path prefix. This will be auto generated when you clear cache.') ?></span>
     113        </td>
     114        </tr>       
     115        <tr valign="top">
     116        <th scope="row"><?php _e('Clear Cache') ?></th>
     117        <td>
     118            <fieldset>
     119            <legend class="screen-reader-text"><span><?php _e('Use SSL') ?></span></legend>
     120            <label for="s3plugin_clear_cache">
     121                <input name="s3plugin_clear_cache" type="checkbox" id="s3plugin_clear_cache" value="1" <?php checked('1', get_option('s3plugin_clear_cache')); ?> />
     122                <?php _e('Clear Cache. This will change the upload prefix.') ?>
     123            </label>
     124            </fieldset>
     125        </td>
     126        </tr>       
     127       
    108128    </table>
    109129    <p class="submit">
  • wp-s3/trunk/s3.php

    r403224 r403515  
    99  Author URI: http://imthi.com/
    1010 */
     11
     12error_reporting(E_ALL);
    1113
    1214class S3Plugin {
     
    2224    var $s3UseCloudFrontURL;
    2325    var $s3CloudFrontURL;
     26    var $s3DirPrefix;
    2427    var $cronScheduleTime;
    2528    var $cronUploadLimit;
     
    5962    }
    6063
    61     $this->blockDirectory = array('wpcf7_captcha');
     64    $this->blockDirectory = array('wpcf7_captcha', 'wp-admin');
    6265    $this->blockExtension = array('php', 'htm', 'html');
    6366
     
    6972    $this->s3BucketName = get_option('s3plugin_amazon_bucket_name');
    7073    $this->s3UseSSL = (bool) get_option('s3plugin_use_ssl', 0);
     74    $this->s3DirPrefix = get_option('s3plugin_dir_prefix');
    7175
    7276    $this->s3UseCloudFrontURL = (bool) get_option('s3plugin_use_cloudfrontURL', 0);
     
    96100    add_filter('style_loader_src', array(&$this, 'style_loader_src'), 99);
    97101
    98 
    99102    if (isset($_GET ['page']) && $_GET ['page'] == 's3plugin-options') {
    100103        ob_start();
     
    147150        delete_option('s3plugin_use_cloudfrontURL');
    148151        }
     152        if (isset($_POST ['s3plugin_clear_cache'])) {
     153        $this->recursive_remove_directory($this->s3CacheFolder, FALSE);
     154        $this->db->query("DELETE FROM `{$this->tabeImageQueue}` WHERE 1=1;");
     155        update_option('s3plugin_dir_prefix', substr(md5(time() + microtime()), 0, 6) . '/');
     156        }
     157
    149158        update_option('s3plugin_cloudfrontURL', $_POST ['s3plugin_cloudfrontURL']);
    150159
     
    201210    ignore_user_abort(true);
    202211    set_time_limit(0);
     212   
     213    print "Hello";
    203214
    204215    include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 's3-php5-curl' . DIRECTORY_SEPARATOR . 'S3.php';
     
    206217    $s3Adapter = new S3($this->s3AccessKey, $this->s3SecretKey, $this->s3UseSSL);
    207218    $availableBuckets = @$s3Adapter->listBuckets();
     219    cmVarDebug($availableBuckets);
     220   
    208221    if (!empty($availableBuckets) && in_array($this->s3BucketName, $availableBuckets) == TRUE) {
    209222        $query = "SELECT * FROM {$this->tabeImageQueue} WHERE status='queue' ORDER BY added LIMIT {$this->cronUploadLimit};";
     
    214227            $fileStatus = 'error';
    215228            $filePath = ABSPATH . $fileInfo ['path'];
    216             $fileObjectInfo = $s3Adapter->getObjectInfo($this->s3BucketName, $fileInfo ['path'], TRUE);
     229            $fileObjectInfo = $s3Adapter->getObjectInfo($this->s3BucketName, $this->s3DirPrefix . $fileInfo ['path'], TRUE);
    217230            if (!empty($fileObjectInfo)) {
    218231            if ($fileObjectInfo ['size'] != filesize($filePath)) {
    219                 if ($s3Adapter->deleteObject($this->s3BucketName, $fileInfo ['path']) === FALSE) {
     232                if ($s3Adapter->deleteObject($this->s3BucketName, $this->s3DirPrefix . $fileInfo ['path']) === FALSE) {
    220233                $shouldUpload = FALSE;
    221234                }
     
    227240            if ($shouldUpload) {
    228241            $fileMimeType = $this->getFileType($filePath);
    229             if ($s3Adapter->putObjectFile($filePath, $this->s3BucketName, $fileInfo ['path'], S3::ACL_PUBLIC_READ, array(), $fileMimeType) === TRUE) {
     242            if ($s3Adapter->putObjectFile($filePath, $this->s3BucketName, $this->s3DirPrefix . $fileInfo ['path'], S3::ACL_PUBLIC_READ, array(), $fileMimeType) === TRUE) {
    230243                $fileStatus = 'done';
    231244            }
     
    380393        if ($fileContents == 'done') {
    381394            if ($instance->isCloudFrontURLEnabled) {
    382             return $instance->s3CloudFrontURL . '/' . $relativePath;
     395            return $instance->s3CloudFrontURL . '/' . $instance->s3DirPrefix . $relativePath;
    383396            } else {
    384             return "http://{$instance->s3BucketName}.s3.amazonaws.com/" . $relativePath;
     397            return "http://{$instance->s3BucketName}.s3.amazonaws.com/" . $instance->s3DirPrefix . $relativePath;
    385398            }
    386399        }
     
    408421    }
    409422
     423    public static function scanForImages($htmlContent) {
     424    $instance = self::getInstance();
     425    $mediaList = $instance->getMediaFromContent($htmlContent);
     426    if (!empty($mediaList)) {
     427        foreach ($mediaList as $fileURL) {
     428        $fileCDNURL = self::getCDNURL($fileURL);
     429        if ($fileCDNURL !== FALSE) {
     430            $htmlContent = str_replace($fileURL, $fileCDNURL, $htmlContent);
     431        }
     432        }
     433    }
     434    return $htmlContent;
     435    }
     436
    410437    function theContent($the_content) {
    411438    $id = 0;
     
    414441        return $the_content;
    415442    }
    416     $mediaList = $this->getMediaFromContent($the_content);
    417     if (!empty($mediaList)) {
    418         foreach ($mediaList as $fileURL) {
    419         $fileCDNURL = self::getCDNURL($fileURL);
    420         if ($fileCDNURL !== FALSE) {
    421             $the_content = str_replace($fileURL, $fileCDNURL, $the_content);
    422         }
    423         }
    424     }
    425     return $the_content;
     443    return self::scanForImages($the_content);
    426444    }
    427445
     
    466484    }
    467485
     486    function recursive_remove_directory($directory, $empty=FALSE) {
     487    if (substr($directory, -1) == '/') {
     488        $directory = substr($directory, 0, -1);
     489    }
     490    if (!file_exists($directory) || !is_dir($directory)) {
     491        return FALSE;
     492    } elseif (is_readable($directory)) {
     493        $handle = opendir($directory);
     494        while (FALSE !== ($item = readdir($handle))) {
     495        if ($item != '.' && $item != '..') {
     496            $path = $directory . '/' . $item;
     497            if (is_dir($path)) {
     498            $this->recursive_remove_directory($path);
     499            } else {
     500            unlink($path);
     501            }
     502        }
     503        }
     504        closedir($handle);
     505        if ($empty == FALSE) {
     506        if (!rmdir($directory)) {
     507            return FALSE;
     508        }
     509        }
     510    }
     511    return TRUE;
     512    }
     513
    468514}
    469515
Note: See TracChangeset for help on using the changeset viewer.