Skip to content

Commit

Permalink
3.3.6
Browse files Browse the repository at this point in the history
* The @{type} dynamic path prefix now works in all cases
* EDD Free Downloads now triggers a warning instead of an error.  It is usable with Media Cloud, but if you are using Imgix and offering image downloads, it will not work as you intend.
* Fix for server overload when running tasks
* Support for URL signing when using CloudFront
* Revamped Settings UI
* Updated Freemius SDK
* Queued deletes were on by default, they are now off by default
  • Loading branch information
jawngee committed Dec 2, 2019
1 parent f8adb08 commit 889c154
Show file tree
Hide file tree
Showing 44 changed files with 458 additions and 118 deletions.
69 changes: 64 additions & 5 deletions classes/Tasks/Model.php → classes/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************

namespace ILAB\MediaCloud\Tasks;

use function ILAB\MediaCloud\Utilities\arrayPath;
use function ILAB\MediaCloud\Utilities\gen_uuid;
use ILAB\MediaCloud\Utilities\Logging\Logger;
namespace ILAB\MediaCloud\Model;

/**
* Represents a database model
Expand Down Expand Up @@ -59,6 +55,13 @@ abstract class Model {
*/
protected $serializedProperties = [];

/**
* Name of properties that are JSON serialized, must map to database and must be properties on model class.
*
* @var array
*/
protected $jsonProperties = [];

//endregion

//region Static Methods
Expand Down Expand Up @@ -99,6 +102,10 @@ public function __construct($data = null) {
if (property_exists($data, $prop)) {
if (in_array($prop, $this->serializedProperties)) {
$this->{$prop} = maybe_unserialize($data->{$prop});
} else if (in_array($prop, $this->jsonProperties)) {
if (!empty($data->{$prop})) {
$this->{$prop} = json_decode($data->{$prop}, true);
}
} else {
$this->{$prop} = $data->{$prop};
}
Expand Down Expand Up @@ -186,6 +193,14 @@ public function save() {
foreach($this->modelProperties as $prop => $format) {
if (in_array($prop, $this->serializedProperties)) {
$data[$prop] = maybe_serialize($this->{$prop});
$formats[] = '%s';
} else if (in_array($prop, $this->jsonProperties)) {
if (!empty($this->{$prop})) {
$data[$prop] = json_encode($this->{$prop}, JSON_PRETTY_PRINT);
} else {
$data[$prop] = null;
}

$formats[] = '%s';
} else {
$data[$prop] = $this->{$prop};
Expand All @@ -211,6 +226,13 @@ public function save() {
return false;
}

/**
* Called before the model deletes itself
*/
protected function willDelete() {

}

/**
* Deletes the model.
*
Expand All @@ -222,6 +244,8 @@ public function delete() {
return false;
}

$this->willDelete();

global $wpdb;

$result = $wpdb->delete(static::table(), ['id' => $this->id]);
Expand Down Expand Up @@ -256,5 +280,40 @@ public static function instance($id) {

return null;
}

/**
* Returns all of the models
*
* @param null|string $orderBy
* @param int $limit
* @param int $page
*
* @return Model|null
* @throws \Exception
*/
public static function all($orderBy = null, $limit = 0, $page = 0) {
global $wpdb;

$table = static::table();

$query = "select * from {$table}";

if (!empty($orderBy)) {
$query = "{$query} order by {$orderBy}";
}

if (!empty($limit)) {
$offset = ($page * $limit);
$query = "{$query} limit {$limit} offset {$offset}";
}

$rows = $wpdb->get_results($query);

foreach($rows as $row) {
return new static($row);
}

return null;
}
//endregion
}
4 changes: 2 additions & 2 deletions classes/Storage/Driver/GoogleCloud/GoogleStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ public function url($key, $type = null) {
throw new InvalidStorageSettingsException('Storage settings are invalid');
}

if ($this->usesSignedURLs($type)) {
if (($type !== 'skip') && $this->usesSignedURLs($type)) {
$expiration = $this->signedURLExpirationForType($type);
return $this->presignedUrl($key, $expiration);
}
Expand Down Expand Up @@ -641,7 +641,7 @@ public function uploadUrl($key, $acl, $mimeType=null, $cacheControl = null, $exp
}

public function enqueueUploaderScripts() {
wp_enqueue_script('ilab-media-direct-upload-google', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-google.js', [], false, true);
wp_enqueue_script('ilab-media-direct-upload-google', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-google.js', [], MEDIA_CLOUD_VERSION, true);
}
//endregion

Expand Down
2 changes: 1 addition & 1 deletion classes/Storage/Driver/S3/DigitalOceanStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function settingsErrorOptionName() {

//region Direct Uploads
public function enqueueUploaderScripts() {
wp_enqueue_script('ilab-media-direct-upload-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-s3.js', [], false, true);
wp_enqueue_script('ilab-media-direct-upload-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-s3.js', [], MEDIA_CLOUD_VERSION, true);
}
//endregion

Expand Down
2 changes: 1 addition & 1 deletion classes/Storage/Driver/S3/OtherS3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function info( $key ) {

//region Direct Uploads
public function enqueueUploaderScripts() {
wp_enqueue_script('ilab-media-direct-upload-other-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-other-s3.js', [], false, true);
wp_enqueue_script('ilab-media-direct-upload-other-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-other-s3.js', [], MEDIA_CLOUD_VERSION, true);
}
//endregion

Expand Down
48 changes: 42 additions & 6 deletions classes/Storage/Driver/S3/S3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
namespace ILAB\MediaCloud\Storage\Driver\S3;

use FasterImage\FasterImage;
use ILAB\MediaCloud\Storage\Driver\GoogleCloud\GoogleStorageSettings;
use ILAB\MediaCloud\Storage\FileInfo;
use ILAB\MediaCloud\Storage\InvalidStorageSettingsException;
use ILAB\MediaCloud\Storage\StorageException;
use ILAB\MediaCloud\Storage\StorageFile;
use ILAB\MediaCloud\Storage\StorageGlobals;
use ILAB\MediaCloud\Storage\StorageManager;
use function ILAB\MediaCloud\Utilities\anyNull;
use function ILAB\MediaCloud\Utilities\arrayPath;
Expand All @@ -31,6 +33,7 @@
use ILAB\MediaCloud\Wizard\ConfiguresWizard;
use ILAB\MediaCloud\Wizard\StorageWizardTrait;
use ILAB\MediaCloud\Wizard\WizardBuilder;
use ILABAmazon\CloudFront\CloudFrontClient;
use ILABAmazon\Credentials\CredentialProvider;
use ILABAmazon\Exception\AwsException;
use ILABAmazon\Exception\CredentialsException;
Expand Down Expand Up @@ -786,19 +789,52 @@ protected function presignedRequest($key, $expiration = 0) {
}

public function presignedUrl($key, $expiration = 0) {
$req = $this->presignedRequest($key, $expiration);
$uri = $req->getUri();
$url = $uri->__toString();
if ((StorageManager::driver() === 's3') && ($this->settings->validSignedCDNSettings())) {
if (empty($expiration)) {
$expiration = $this->settings->presignedURLExpiration;
}

if (empty($expiration)) {
$expiration = 1;
}

$cloudfrontClient = new CloudFrontClient([
'profile' => 'default',
'version' => 'latest',
'region' => $this->settings->region
]);

$srcUrl = $this->client->getObjectUrl($this->settings->bucket, $key);
$cdnHost = parse_url($this->settings->signedCDNURL, PHP_URL_HOST);
$srcHost = parse_url($srcUrl, PHP_URL_HOST);
$srcScheme = parse_url($srcUrl, PHP_URL_SCHEME);

return $url;
$srcUrl = str_replace($srcScheme, 'https', $srcUrl);
$srcUrl = str_replace($srcHost, $cdnHost, $srcUrl);

$url = $cloudfrontClient->getSignedUrl([
'url' => $srcUrl,
'expires' => time() + ($expiration * 60),
'key_pair_id' => $this->settings->cloudfrontKeyID,
'private_key' => $this->settings->cloudfrontPrivateKey
]);

return $url;
} else {
$req = $this->presignedRequest($key, $expiration);
$uri = $req->getUri();
$url = $uri->__toString();

return $url;
}
}

public function url($key, $type = null) {
if(!$this->client) {
throw new InvalidStorageSettingsException('Storage settings are invalid');
}

if ($this->usesSignedURLs($type)) {
if (($type !== 'skip') && $this->usesSignedURLs($type)) {
$expiration = $this->signedURLExpirationForType($type);
return $this->presignedUrl($key, $expiration);
} else {
Expand Down Expand Up @@ -848,7 +884,7 @@ public function uploadUrl($key, $acl, $mimeType = null, $cacheControl = null, $e
}

public function enqueueUploaderScripts() {
wp_enqueue_script('ilab-media-direct-upload-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-s3.js', [], false, true);
wp_enqueue_script('ilab-media-direct-upload-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-s3.js', [], MEDIA_CLOUD_VERSION, true);
}
//endregion

Expand Down
61 changes: 60 additions & 1 deletion classes/Storage/Driver/S3/S3StorageSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ILAB\MediaCloud\Storage\StorageManager;
use ILAB\MediaCloud\Tools\ToolSettings;
use ILAB\MediaCloud\Utilities\Environment;
use function ILAB\MediaCloud\Utilities\gen_uuid;

/**
* Class S3StorageSettings
Expand All @@ -44,6 +45,9 @@
* @property int presignedURLExpirationForAudio
* @property int presignedURLExpirationForDocs
* @property bool settingsError
* @property string signedCDNURL
* @property string cloudfrontKeyID
* @property-read string cloudfrontPrivateKey
*
*/
class S3StorageSettings extends ToolSettings {
Expand All @@ -68,6 +72,8 @@ class S3StorageSettings extends ToolSettings {
'usePresignedURLsForDocs' => ['mcloud-storage-use-presigned-urls-docs', null, false],
'presignedURLExpirationForDocs' => ['mcloud-storage-presigned-expiration-docs', null, 0],
'settingsError' => ['ilab-backblaze-settings-error', null, false],
'signedCDNURL' => ['mcloud-storage-signed-cdn-base', null, false],
'cloudfrontKeyID' => ['mcloud-storage-cloudfront-key-id', null, false],
];

/** @var S3StorageInterface */
Expand All @@ -77,6 +83,12 @@ class S3StorageSettings extends ToolSettings {
private $_pathStyleEndpoint = null;
private $_region = null;
private $_settingsError = null;
private $_cloudfrontPrivateKey = null;

private $deletePrivateKey = false;

/** @var callable */
private $dieHandler = null;

/**
* S3StorageSettings constructor.
Expand All @@ -85,6 +97,13 @@ class S3StorageSettings extends ToolSettings {
*/
public function __construct($storage) {
$this->storage = $storage;

add_filter('wp_die_ajax_handler', [$this, 'hookDieHandler']);
add_filter('wp_die_json_handler', [$this, 'hookDieHandler']);
add_filter('wp_die_jsonp_handler', [$this, 'hookDieHandler']);
add_filter('wp_die_xmlrpc_handler', [$this, 'hookDieHandler']);
add_filter('wp_die_xml_handler', [$this, 'hookDieHandler']);
add_filter('wp_die_handler', [$this, 'hookDieHandler']);
}

public function __get($name) {
Expand Down Expand Up @@ -160,6 +179,29 @@ public function __get($name) {
$storageClass = get_class($this->storage);
$this->_settingsError = Environment::Option($storageClass::settingsErrorOptionName(), null, false);
return $this->_settingsError;
} else if ($name === 'cloudfrontPrivateKey') {
if ($this->_cloudfrontPrivateKey !== null) {
return $this->_cloudfrontPrivateKey;
}

$keyTemp = tempnam('/tmp', gen_uuid(8));

$keyFile = Environment::Option('mcloud-storage-cloudfront-private-key-file', null, null);
if (!empty($keyFile) && file_exists($keyFile)) {
$this->deletePrivateKey = false;
$this->_cloudfrontPrivateKey = $keyFile;
}

if (empty($this->_cloudfrontPrivateKey)) {
$keyContents = Environment::Option('mcloud-storage-cloudfront-private-key');
if (!empty($keyContents)) {
$this->deletePrivateKey = true;
file_put_contents($keyTemp, $keyContents);
$this->_cloudfrontPrivateKey = $keyTemp;
}
}

return $this->_cloudfrontPrivateKey;
}

return parent::__get($name);
Expand All @@ -180,11 +222,28 @@ public function __set($name, $value) {
}

public function __isset($name) {
if (in_array($name, ['region', 'endPointPathStyle', 'endpoint', 'settingsError'])) {
if (in_array($name, ['region', 'endPointPathStyle', 'endpoint', 'settingsError', 'cloudfrontPrivateKey'])) {
return true;
}

return parent::__isset($name);
}

public function validSignedCDNSettings() {
return (!empty($this->signedCDNURL) && !empty($this->cloudfrontKeyID) && !empty($this->cloudfrontPrivateKey));
}

public function hookDieHandler($handler) {
$this->dieHandler = $handler;
return [$this, 'deletePrivateKey'];
}

public function deletePrivateKey($message, $title = '', $args = array()) {
if ($this->deletePrivateKey && file_exists($this->_cloudfrontPrivateKey)) {
unlink($this->_cloudfrontPrivateKey);
}

call_user_func( $this->dieHandler, $message, $title, $args );
}

}
2 changes: 1 addition & 1 deletion classes/Storage/Driver/S3/WasabiStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function uploadUrl($key, $acl, $mimeType = null, $cacheControl = null, $e
}

public function enqueueUploaderScripts() {
wp_enqueue_script('ilab-media-upload-other-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-other-s3.js', [], false, true);
wp_enqueue_script('ilab-media-upload-other-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-other-s3.js', [], MEDIA_CLOUD_VERSION, true);
}
//endregion

Expand Down
Loading

0 comments on commit 889c154

Please sign in to comment.