File: /home/httpd/html/baretube.com.new/admin/functions.ceph.php
<?php
function cephDebugLog($mystring) {
global $cephDebug;
global $basepath;
global $cephDebugLogFile;
if ($cephDebug) {
if ($cephDebugLogFile) {
$myfile = fopen($cephDebugLogFile, 'a');
} else {
$myfile = fopen($basepath . '/admin/logs/debug.log', 'a');
}
fwrite($myfile, date("c") . " - [CEPH] - " . $mystring . PHP_EOL);
fclose($myfile);
}
}
function cephUploadObject($filename,$object) {
define('MB', 1024 * 1024);
global $ceph_key;
global $ceph_secret;
global $ceph_canonicle_id;
global $ceph_canonicle_name;
global $ceph_bucket;
global $ceph_host;
global $basepath;
cephDebugLog("cephUploadObject($filename,$object) called");
include_once("$basepath/admin/aws-sdk-for-php/sdk.class.php");
$Connection = new AmazonS3(array(
'key' => $ceph_key,
'secret' => $ceph_secret,
'canonical_id' => $ceph_canonicle_id,
'canonical_name' => $ceph_canonicle_name,
));
$Connection->set_hostname($ceph_host);
$Connection->allow_hostname_override(false);
if(file_exists($filename)) {
cephDebugLog("$filename exists");
if(filesize($filename) < 50000000) {
cephDebugLog("$filename is under 50MB, doing singlepart upload");
if($Connection->create_object($ceph_bucket, $object,array('fileUpload'=>$filename))) {
cephDebugLog("$filename uploaded to s3://$ceph_bucket/$object");
if(cdnInvalidateObject($object)) {
cephDebugLog("File purged okay");
}
return true;
} else {
cephDebugLog("$filename failed to upload to object storage");
return false;
}
} else {
cephDebugLog("$filename over 50MB, doing multipart upload");
$response = $Connection->initiate_multipart_upload($ceph_bucket, $object, array(
'contentType' => 'video/mp4',
'acl' => AmazonS3::ACL_PUBLIC,
'storage' => AmazonS3::STORAGE_STANDARD,
));
// Get the Upload ID
$upload_id = (string) $response->body->UploadId;
$parts = $Connection->get_multipart_counts(filesize($filename), 100*MB);
foreach ($parts as $i => $part)
{
$Connection->batch()->upload_part($ceph_bucket, $object, $upload_id, array(
'expect' => '100-continue',
'fileUpload' => $filename,
'partNumber' => ($i + 1),
'seekTo' => (integer) $part['seekTo'],
'length' => (integer) $part['length'],
));
}
$batch_responses = $Connection->batch()->send();
cephDebugLog(var_dump($batch_responses->areOK()));
$parts = $Connection->list_parts($ceph_bucket, $object, $upload_id);
$response = $Connection->complete_multipart_upload($ceph_bucket, $object, $upload_id, $parts);
if($response->isOK()) {
cephDebugLog("$filename uploaded to s3://$ceph_bucket/$object");
if(cdnInvalidateObject($object)) {
cephDebugLog("File purged okay");
}
return true;
} else {
cephDebugLog("$filename failed multipart upload");
$response = $Connection->abort_multipart_upload($ceph_bucket, $object, $upload_id);
if($response->isOK()) {
cephDebugLog("successfully aborted multipart upload_id $upload_id");
}
return false;
}
}
} else {
cephDebugLog("$filename does not exist on local disk");
return false;
}
}
function cephListObjects($prefix) {
global $ceph_key;
global $ceph_secret;
global $ceph_canonicle_id;
global $ceph_canonicle_name;
global $ceph_bucket;
global $ceph_host;
global $basepath;
include_once("$basepath/admin/aws-sdk-for-php/sdk.class.php");
$Connection = new AmazonS3(array(
'key' => $ceph_key,
'secret' => $ceph_secret,
'canonical_id' => $ceph_canonicle_id,
'canonical_name' => $ceph_canonicle_name,
));
$Connection->set_hostname($ceph_host);
$Connection->allow_hostname_override(false);
$ObjectsListResponse = $Connection->list_objects($ceph_bucket, array('prefix' => $prefix));
$Objects = $ObjectsListResponse->body->Contents;
// foreach ($Objects as $Object) {
// echo $Object->Key . "\t" . $Object->Size . "\t" . $Object->LastModified . "\n";
// }
return($Objects);
}
function cephDeleteObject($object) {
global $ceph_key;
global $ceph_secret;
global $ceph_canonicle_id;
global $ceph_canonicle_name;
global $ceph_bucket;
global $ceph_host;
global $basepath;
include_once("$basepath/admin/aws-sdk-for-php/sdk.class.php");
$Connection = new AmazonS3(array(
'key' => $ceph_key,
'secret' => $ceph_secret,
'canonical_id' => $ceph_canonicle_id,
'canonical_name' => $ceph_canonicle_name,
));
$Connection->set_hostname($ceph_host);
$Connection->allow_hostname_override(false);
if($Connection->if_object_exists($ceph_bucket, $object)) {
if($Connection->delete_object($ceph_bucket, $object)) {
cephDebugLog("deleted file $object");
if(cdnInvalidateObject($object)) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}
function ceph_move_uploaded_file($source, $dest) {
// wrapper function for PHP built-in move_uploaded_file
global $basepath;
global $ceph_enabled;
global $ceph_delete_local;
if($ceph_enabled) {
if(is_uploaded_file($source)) {
@copy($source, $dest);
if(cephUploadObject($source,str_replace("$basepath/media/","",$dest))) {
if($ceph_delete_local) {
if(file_exists($source)) {
@unlink($source);
}
}
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return move_uploaded_file($source, $dest);
}
}
function ceph_makeImageThumbnail($input, $output, $thumbwidth, $thumbheight, $mode = 'convert') {
// modified version of makeImageThumbnail function, originally defined
// in functions.general.php, to patch in object-storage support
global $convert_path;
global $mogrify_path;
global $jpegoptim_path;
global $jpegoptim_command;
global $basepath;
global $ceph_enabled;
global $ceph_delete_local;
if($ceph_enabled) {
$output = str_replace("$basepath/media/","",$output);
$tmpfile = tempnam('/tmp', 'mb_thumb_');
@unlink($tmpfile);
// wise to write to a temp file, in case the /media folder
// is missing, or does not have all needful paths
if ($mode == 'convert') {
$convert_mode = $convert_path;
} else {
$convert_mode = $mogrify_path;
}
shell_exec("$convert_mode -adaptive-resize " . ($thumbwidth) . "x" . ($thumbheight) . "^ -gravity Center -extent " . $thumbwidth . "x" . $thumbheight . " \"$input\" \"$tmpfile\"");
if($jpegoptim_path) { shell_exec("$jpegoptim_path $jpegoptim_command \"$input\""); }
if($jpegoptim_path) { shell_exec("$jpegoptim_path $jpegoptim_command \"$tmpfile\""); }
if(cephUploadObject($tmpfile, $output)) {
if($ceph_delete_local) {
if(file_exists($tmpfile)) {
@unlink($tmpfile);
}
}
}
} else {
if ($input == $output) {
exit("Error: ceph_makeImageThumbnail() input and output cannot be the same!");
}
if ($mode == 'convert') {
$convert_mode = $convert_path;
} else {
$convert_mode = $mogrify_path;
}
shell_exec("$convert_mode -adaptive-resize " . ($thumbwidth) . "x" . ($thumbheight) . "^ -gravity Center -extent " . $thumbwidth . "x" . $thumbheight . " \"$input\" \"$output\"");
if($jpegoptim_path) { shell_exec("$jpegoptim_path $jpegoptim_command \"$input\""); }
if($jpegoptim_path) { shell_exec("$jpegoptim_path $jpegoptim_command \"$output\""); }
}
}
function ceph_file_put_contents($objectname,$body) {
global $ceph_key;
global $ceph_secret;
global $ceph_canonicle_id;
global $ceph_canonicle_name;
global $ceph_bucket;
global $ceph_host;
global $basepath;
global $ceph_enabled;
if($ceph_enabled) {
// because many things call the original file for thumbnailing
// best to write the file regardless of if it's going to ceph
file_put_contents($objectname,$body);
$objectname = str_replace("$basepath/media/","",$objectname);
include_once("$basepath/admin/aws-sdk-for-php/sdk.class.php");
$Connection = new AmazonS3(array(
'key' => $ceph_key,
'secret' => $ceph_secret,
'canonical_id' => $ceph_canonicle_id,
'canonical_name' => $ceph_canonicle_name,
));
$Connection->set_hostname($ceph_host);
$Connection->allow_hostname_override(false);
if($Connection->create_object($ceph_bucket, $objectname,array('body'=>$body))) {
if(cdnInvalidateObject($object)) {
return true;
}
} else {
return false;
}
} else {
file_put_contents($objectname,$body);
}
}
function ceph_delete_local($file) {
global $ceph_enabled;
global $ceph_delete_local;
if($ceph_enabled) {
if($ceph_delete_local) {
if(file_exists($file)) {
@unlink($file);
}
}
}
}
function ceph_mogrifyImageThumbnail($input, $thumbwidth, $thumbheight) {
global $basepath;
global $ceph_enabled;
global $ceph_delete_local;
global $mogrify_path;
$input = escapeshellcmd($input);
shell_exec("$mogrify_path -adaptive-resize ".($thumbwidth)."x".($thumbheight)."^ -gravity Center -extent ".$thumbwidth."x".$thumbheight." \"$input\"");
if($ceph_enabled) {
if(cephUploadObject($input,str_replace("$basepath/media/","",$input))) {
if($ceph_delete_local) {
if(file_exists($input)) {
@unlink($input);
}
}
return true;
} else {
return false;
}
}
}
function ceph_file_exists($object) {
global $ceph_key;
global $ceph_secret;
global $ceph_canonicle_id;
global $ceph_canonicle_name;
global $ceph_bucket;
global $ceph_host;
global $basepath;
global $ceph_enabled;
if($ceph_enabled) {
if(file_exists($object)) {
return true;
}
$object = str_replace("$basepath/media/","",$object);
include_once("$basepath/admin/aws-sdk-for-php/sdk.class.php");
$Connection = new AmazonS3(array(
'key' => $ceph_key,
'secret' => $ceph_secret,
'canonical_id' => $ceph_canonicle_id,
'canonical_name' => $ceph_canonicle_name,
));
$Connection->set_hostname($ceph_host);
$Connection->allow_hostname_override(false);
if($Connection->if_object_exists($ceph_bucket, $object)) {
return true;
} else {
error_log("$ceph_bucket $object does not exist");
return false;
}
}
else {
return (file_exists($object));
}
}
function cephDeleteFolder($prefix) {
// only works on thumbnail folders
global $ceph_key;
global $ceph_secret;
global $ceph_canonicle_id;
global $ceph_canonicle_name;
global $ceph_bucket;
global $ceph_host;
global $basepath;
include_once("$basepath/admin/aws-sdk-for-php/sdk.class.php");
$Connection = new AmazonS3(array(
'key' => $ceph_key,
'secret' => $ceph_secret,
'canonical_id' => $ceph_canonicle_id,
'canonical_name' => $ceph_canonicle_name,
));
$Connection->set_hostname($ceph_host);
$Connection->allow_hostname_override(false);
cdnDebugLog("request to delete folder $prefix");
preg_match('/^thumbs(\/[0-9a-f]){5}\/.*|^galleries\/([0-9a-f]){12,14}(\/)?.*/', $prefix, $matches);
if (sizeof($matches) <= 0) {
cdnDebugLog("currently cephDeleteFolder only supports thumbnail/gallery folders for safety, failing");
return false;
}
foreach (cephListObjects($prefix) as $object) {
cephDeleteObject($object->Key);
}
return true;
}