View source
<?php
function brilliant_gallery_testext($filename) {
$testending = substr(strtolower($filename), -4, 4);
if ($testending == ".jpg" or substr(strtolower($filename), -5, 5) == ".jpeg" or $testending == ".gif" or $testending == ".png") {
return true;
}
return false;
}
function load_dir_to_array($absolpath, $imagewidth, $fullresolutionmaxwidth, $brilliant_gallery_sort, $imagecrop, $maximumnumbertoshow = '') {
$poct = -1;
$retval_dimensions = array();
$actualpath = realpath(FILE_DIRECTORY_PATH) . $absolpath;
$handle = @opendir($actualpath);
$imagemaxh = 0;
$hidden_file_names = brilliant_gallery_get_picasa_hidden_imagenames($actualpath);
$filearray = array();
while ($file = @readdir($handle)) {
if (!brilliant_gallery_testext($file)) {
continue;
}
if (in_array($file, $hidden_file_names)) {
continue;
}
$filearray[] = $file;
}
@closedir($handle);
if (empty($filearray)) {
watchdog('Brilliant Gal', 'No displayable images in ' . $absolpath . '!');
}
else {
foreach ($filearray as $file) {
$poct += 1;
$retval_dimensions[$poct]['file'] = $file;
$retval_dimensions[$poct]['imgcrop'] = $imagecrop;
$temp = getimagesize($actualpath . '/' . $file);
if ($temp === false) {
continue;
}
if ($temp[0] - $temp[1] >= 0 or $maximumnumbertoshow == 1) {
if ($imagecrop == 'yes') {
$retval_dimensions[$poct]['imgw'] = $imagewidth;
$retval_dimensions[$poct]['imgh'] = $imagewidth;
}
else {
$retval_dimensions[$poct]['imgw'] = $imagewidth;
$retval_dimensions[$poct]['imgh'] = round($temp[1] / $temp[0] * $imagewidth);
}
$retval_dimensions[$poct]['imgwbig'] = $fullresolutionmaxwidth;
$retval_dimensions[$poct]['imghbig'] = round($temp[1] / $temp[0] * $fullresolutionmaxwidth);
}
else {
if ($imagecrop == 'yes') {
$retval_dimensions[$poct]['imgw'] = round($temp[0] / $temp[1] * $imagewidth);
$retval_dimensions[$poct]['imgh'] = $imagewidth;
}
else {
$retval_dimensions[$poct]['imgw'] = round($temp[0] / $temp[1] * ($temp[0] / $temp[1]) * $imagewidth);
$retval_dimensions[$poct]['imgh'] = round($temp[0] / $temp[1] * $imagewidth);
}
$retval_dimensions[$poct]['imgwbig'] = round($temp[0] / $temp[1] * ($temp[0] / $temp[1]) * $fullresolutionmaxwidth);
$retval_dimensions[$poct]['imghbig'] = round($temp[0] / $temp[1] * $fullresolutionmaxwidth);
}
if ($imagemaxh < $retval_dimensions[$poct]['imgh']) {
$imagemaxh = $retval_dimensions[$poct]['imgh'];
}
}
}
if ($brilliant_gallery_sort == '1') {
@sort($retval_dimensions);
}
else {
shuffle($retval_dimensions);
}
return array(
$retval_dimensions,
$imagemaxh,
);
}
function brilliant_gallery_brokenimage($msg, $width = 150, $height = 30) {
header('Content-type: image/jpeg');
$im = imagecreatetruecolor($width, $height);
$bgc = imagecolorallocate($im, 0, 0, 0);
$tc = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, $msg, $tc);
imagejpeg($im);
exit;
}
function brilliant_gallery_image_properties_set($bgimgproperties_array) {
$bgimgproperties_hash = md5(serialize($bgimgproperties_array));
$countie = db_query("SELECT COUNT(*) FROM {brilliant_gallery_image_arrays} WHERE hash = :hash", array(
':hash' => $bgimgproperties_hash,
))
->fetchField();
if ($countie == 0) {
$fields = new stdClass();
$fields->hash = $bgimgproperties_hash;
$fields->array = serialize($bgimgproperties_array);
$fields->datetime = REQUEST_TIME;
drupal_write_record("brilliant_gallery_image_arrays", $fields);
}
else {
$change = array(
'datetime' => REQUEST_TIME,
'hash' => $bgimgproperties_hash,
);
drupal_write_record('brilliant_gallery_image_arrays', $change, 'hash');
}
return $bgimgproperties_hash;
}
function brilliant_gallery_image_properties_get($bgimgproperties_hash) {
$bgimgproperties_array = db_query("SELECT array FROM {brilliant_gallery_image_arrays} WHERE hash = :hash LIMIT 1", array(
':hash' => $bgimgproperties_hash,
))
->fetchField();
$bgimgproperties_array = unserialize($bgimgproperties_array);
return $bgimgproperties_array;
}
function brilliant_gallery_get_extension($imagename) {
$imagename = explode(".", $imagename);
$extension = strtolower(array_pop($imagename));
return $extension;
}
function brilliant_gallery_get_days_in_seconds($days) {
return $days * 24 * 3600;
}
function brilliant_gallery_deliver_image() {
$queryexplode = explode('/', $_GET['q']);
$bgimgproperties_hash = array_pop($queryexplode);
$bgimgproperties_hash = substr($bgimgproperties_hash, strlen('bg_cached_resized_'));
$bgimgproperties_hash_array = explode('.', $bgimgproperties_hash);
$bgimgproperties_hash_array = $bgimgproperties_hash_array[0];
$bgimgproperties_array = brilliant_gallery_image_properties_get($bgimgproperties_hash_array);
if (!is_array($bgimgproperties_array)) {
watchdog('Brilliant Gal', 'Wrong image: ' . $bgimgproperties_hash);
return;
}
foreach ($bgimgproperties_array as $key => $val) {
$_GET[$key] = $val;
}
$urlpath = $_GET['imgp'];
if (sizeof($_GET) > 5 || strpos($urlpath, "://") !== false || strpos($urlpath, "..") !== false || preg_match('/\\D/', $_GET['imgw'] . $_GET['imgh']) > 0 || $_GET['imgw'] + $_GET['imgh'] < 10 || $_GET['imgw'] + $_GET['imgh'] > 20000) {
header("HTTP/1.0 404 Not Found");
exit;
}
$imagepath = realpath(FILE_DIRECTORY_PATH . $urlpath);
$GLOBALS['devel_shutdown'] = FALSE;
$my_data = resizeimage_wrapper_dbcache(FALSE, $imagepath, $bgimgproperties_hash);
$imgsize = @getimagesize($imagepath);
$head = "Content-type: {$imgsize['mime']}";
header($head);
echo $my_data;
exit;
return;
}
function resizeimage_wrapper_dbcache($reset = FALSE, $imagepath, $bgimgproperties_hash) {
$bgcachexpire = brilliant_gallery_get_days_in_seconds(variable_get('brilliant_gallery_cache_duration', 90));
static $my_data;
$pcache = variable_get('brilliant_gallery_pcache', BRILLIANT_GALLERY_DEFAULT_CACHE_DIR);
$foqen = FILE_DIRECTORY_PATH . '/' . $pcache . '/bg_cached_resized_' . $bgimgproperties_hash;
$lastchanged = @filemtime($foqen);
if ($lastchanged === FALSE or REQUEST_TIME - $lastchanged > $bgcachexpire) {
@unlink($foqen);
$my_data = resizeimage($_GET['imgp'], $_GET['imgw'], $_GET['imgh'], @$_GET['imgcrop'], $imagepath);
$image = $my_data;
if (strlen($image) > 0) {
$fp = fopen($foqen, 'w');
fwrite($fp, $image);
fclose($fp);
}
}
return $my_data;
}
function resizeimage($imgp, $imgw, $imgh, $imgcrop, $imagepath) {
$imagepathexploded = explode('.', strtolower($imagepath));
$suffix = end($imagepathexploded);
if ($suffix == "gif") {
$img = @imagecreatefromgif($imagepath);
if (!$img) {
brilliant_gallery_brokenimage("Error loading GIF", $imgw, $imgh);
}
}
else {
if ($suffix == "jpg" or $suffix == "jpeg") {
$img = @imagecreatefromjpeg($imagepath);
if (!$img) {
brilliant_gallery_brokenimage("Error loading JPG", $imgw, $imgh);
}
}
else {
if ($suffix == "png") {
$img = @imagecreatefrompng($imagepath);
if (!$img) {
brilliant_gallery_brokenimage("Error loading PNG", $imgw, $imgh);
}
}
}
}
$src_h = ImageSY($img);
$src_w = ImageSX($img);
$dst_img = 0;
if ($imgcrop == 'yes') {
if ($src_h > $src_w) {
$dst_img = imagecreatetruecolor($imgh, $imgh);
imagecopyresampled($dst_img, $img, 0, 0, 0, ($src_h - $src_w) / 2, $imgh, $imgh, $src_w, $src_w);
}
else {
$dst_img = imagecreatetruecolor($imgw, $imgw);
imagecopyresampled($dst_img, $img, 0, 0, ($src_w - $src_h) / 2, 0, $imgw, $imgw, $src_h, $src_h);
}
}
else {
$dst_img = imagecreatetruecolor($imgw, $imgh);
imagecopyresampled($dst_img, $img, 0, 0, 0, 0, $imgw, $imgh, $src_w, $src_h);
}
$img = $dst_img;
ob_start();
if ($suffix == "gif") {
Imagegif($img);
}
else {
if ($suffix == "jpg" or $suffix == "jpeg") {
Imagejpeg($img, NULL, 90);
}
else {
if ($suffix == "png") {
Imagepng($img, NULL, 9);
}
}
}
$result = ob_get_clean();
$result = $result;
return $result;
}
function replace_brilliant_gallery_tags($str) {
$matchlink = array();
$orig_match = array();
$matches = array();
preg_match_all("/(\\[)bg(\\|)[^\\]]*(\\])/s", $str, $matches);
foreach ($matches[0] as $match) {
$omatch = $match;
$orig_match[] = $omatch;
$match = substr($match, 1, strlen($match) - 2);
$match = strip_tags($match);
$match = explode("|", $match);
$allowed_params = brilliant_gallery_get_allowed_params();
$newmatch = array();
$newgenmatch = array();
$isnewgettag = true;
foreach ($match as $val) {
$tmp = trim($val);
if ($tmp != '') {
$tmp2 = explode(' = ', $tmp);
if (sizeof($tmp2) == 2) {
$tmp2[0] = strtolower(trim($tmp2[0]));
$tmp2[1] = trim($tmp2[1]);
if (in_array($tmp2[0], $allowed_params)) {
$newgenmatch[$tmp2[0]] = $tmp2[1];
}
else {
$msg = 'Parameter ' . $tmp2[0] . ' is invalid!';
watchdog('Brilliant Gal', $msg);
}
}
else {
if ($tmp != 'bg') {
$isnewgettag = false;
}
$newmatch[] = $tmp;
}
}
}
if (!$isnewgettag) {
$match = $newmatch;
}
else {
$match = array();
foreach ($newgenmatch as $key => $val) {
$match[array_search($key, $allowed_params)] = $val;
}
}
$pathurl_btsync_check = brilliant_gallery_pathurl_btsync_check(@$match[1]);
if ($pathurl_btsync_check) {
$match[1] = $pathurl_btsync_check;
}
if (@$match[13] != '' and strpos(@$match[13], ',') === FALSE) {
$match[5] = 1;
}
$bgcachexpire = brilliant_gallery_get_days_in_seconds(variable_get('brilliant_gallery_cache_duration', 90));
$mbgtag = md5($omatch);
if ($cache = cache_get('bg_gallery_table_' . $mbgtag) and !empty($cache->data)) {
$galhere = $cache->data;
}
else {
$galhere = render_brilliant_gallery($match);
cache_set('bg_gallery_table_' . $mbgtag, $galhere, 'cache', brilliant_gallery_get_time_this_many_seconds_from_now($bgcachexpire));
}
$matchlink[] = $galhere;
}
$str = str_replace($orig_match, $matchlink, $str);
return $str;
}
function brilliant_gallery_pathurl_btsync_check($pathurl) {
preg_match("/[A-Z2-7]{33}/", $pathurl, $output_array);
if (!array_key_exists(0, $output_array)) {
return FALSE;
}
if (variable_get('brilliant_gallery_folder', '') == '') {
return;
}
$btsyncdir = 'public://' . variable_get('brilliant_gallery_folder', '') . '/btsync';
$dirtest = file_prepare_directory($btsyncdir, FILE_CREATE_DIRECTORY);
if (!$dirtest) {
watchdog('Brilliant Gal', 'Special folder "btsync" cannot be created or is not writable in ' . ($btsyncdir = 'public://' . variable_get('brilliant_gallery_folder', '')));
return FALSE;
}
$btsyncgallerydir = $btsyncdir . '/' . $pathurl;
$dirtest = file_prepare_directory($btsyncgallerydir, FILE_CREATE_DIRECTORY);
if (!$dirtest) {
watchdog('Brilliant Gal', 'Sync folder "' . $pathurl . '" cannot be created or is not writable in ' . $btsyncdir);
return FALSE;
}
if (function_exists('btsync_method_callback')) {
btsync_method_callback('add_folder', array(
'dir' => drupal_realpath($btsyncgallerydir),
'secret' => $pathurl,
));
}
else {
watchdog('Brilliant Gal', 'BT Sync gallery formatter selected but the BitTorrent Sync API module ("btsync") is not enabled.');
return FALSE;
}
$pathurl = 'btsync/' . $pathurl;
return $pathurl;
}
function brilliant_gallery_get_time_this_many_seconds_from_now($period) {
$period = REQUEST_TIME + $period;
return $period;
}
function brilliant_gallery_get_picasa_hidden_imagenames($absolute_album_path) {
$filenames = array();
if (file_exists($absolute_album_path . "/.picasa.ini")) {
$picasaini = file_get_contents($absolute_album_path . "/.picasa.ini");
if (strpos($picasaini, "hidden=yes") !== FALSE) {
$picasaini_lines = file($absolute_album_path . "/.picasa.ini");
$picasaini_lines = array_map('trim', $picasaini_lines);
$dir_files = scandir($absolute_album_path);
$dir_files = array_diff($dir_files, array(
'..',
'.',
'.picasa.ini',
));
foreach ($dir_files as $key => $filename) {
$searched_filename = '[' . $filename . ']';
$is_in_picasaini = array_search($searched_filename, $picasaini_lines);
if ($is_in_picasaini === FALSE) {
continue;
}
$ishidden = FALSE;
foreach ($picasaini_lines as $key => $val) {
if ($key <= $is_in_picasaini) {
continue;
}
if ($val == 'hidden=yes') {
$ishidden = TRUE;
break;
}
if (substr($val, 0, 1) == '[') {
break;
}
}
if (!$ishidden) {
continue;
}
$filenames[] = $filename;
}
}
}
return $filenames;
}