[add] #453 : Remove image older than

[add] #466 : Another dir organisation
This commit is contained in:
Knah Tsaeb 2013-05-03 11:01:17 +02:00
parent cb76512a08
commit c665a3cdec
3 changed files with 94 additions and 44 deletions

View file

@ -6,10 +6,11 @@
site=$1 # url for thumbshot
hashUrl=$2 # hash(url) is the name of final image
thumbSize=$3 # size of thumb widthxheight ex : 190x90
onlyThumb=$4 # make only thumbshot no full size image
waitForResult=$5 # if true we try to make soon as possible or add to queue
sizeNameDir=$4 # name of size dir
onlyThumb=$5 # make only thumbshot no full size image
waitForResult=$6 # if true we try to make soon as possible or add to queue
maxThread=1 # max parralle work. For me the best is nb core - 1
timeOut=60 # default time out after this time the site are declared in error
timeOut=60 # default time out, after this time the site are declared in error
log=false # log all generation success and error
randomSleep=`echo $((RANDOM%20))`
optimizeTool='' # external tool form optimize png
@ -17,7 +18,8 @@ maxQueue=50 # max process in queue
currentProcess=$(ps -e | grep -v grep | grep thumb_ | wc -l)
firstLevel=${hashUrl:0:2}
secondLevel=${hashUrl:2:2}
startPath=$firstLevel/$secondLevel/
startPath=$sizeNameDir/$firstLevel/$secondLevel/
mkdir -p "cache/img/$startPath"
if [ -f "cache/config/serverOptions.php" ]

View file

@ -6,7 +6,7 @@ html {
background-color: #dedede;
}
form, #result{
form, #result, #header{
padding: 1em;
text-align: center;
width: 50%;
@ -43,7 +43,7 @@ form .info {
font-size : 0.8em;
}
#result {
#result, #header {
text-align: center;
padding: 1em;
margin: 1em auto;

124
index.php
View file

@ -2,24 +2,32 @@
if (empty($_SESSION)) {
session_start();
}
date_default_timezone_set('Europe/Paris');
// change if you want no inpact
$GLOBALS['config']['onlyLocalServer'] = false;
date_default_timezone_set('Europe/Paris');
// soshot only accept request by 127.0.0.1
$GLOBALS['config']['NoWebPage'] = false;
$GLOBALS['config']['onlyLocalServer'] = false;
// No form for post url only acces by GET method
$GLOBALS['config']['maxErrorBeforeBan'] = 3;
$GLOBALS['config']['NoWebPage'] = false;
// After 3 criticla error ban user
$GLOBALS['config']['banTime'] = 60;
$GLOBALS['config']['maxErrorBeforeBan'] = 3;
// Ban for 60 minutes
$GLOBALS['config']['defaultUrl'] = 'https://google.com';
$GLOBALS['config']['banTime'] = 60;
// default url for form
$GLOBALS['config']['defaultThumbSize'] = '120x90';
$GLOBALS['config']['defaultUrl'] = 'https://google.com';
// default size for thumbnail
$GLOBALS['config']['onlyThumb'] = TRUE;
$GLOBALS['config']['defaultThumbSize'] = '120x90';
// generate only thumbnail or generate thumbnail + 1280x1024 image
$GLOBALS['config']['thumbSize'] = array('s' => '120x90', 'm' => '200x160', 'l' => '300x240', 'xl' => '400x320', 'xxl' => '500x400');
$GLOBALS['config']['onlyThumb'] = TRUE;
// list of available size for thumb
$GLOBALS['config']['thumbSize'] = array(
's' => '120x90',
'm' => '200x160',
'l' => '300x240',
'xl' => '400x320',
'xxl' => '500x400'
);
// Remove image older than 12 hours
$GLOBALS['config']['expireCache'] = 12;
if (!file_exists('cache/config/genConf.php')) {
install();
@ -55,13 +63,13 @@ if (get_magic_quotes_gpc()) {
function testExistImg($file) {
if ($GLOBALS['config']['onlyThumb'] === true) {
if (file_exists($file . '_thumb.png')) {
if (file_exists($file.'_thumb.png')) {
return true;
} else {
return false;
}
} else {
if (file_exists($file . '_thumb.png') && file_exists($file . '.png')) {
if (file_exists($file.'_thumb.png') && file_exists($file.'.png')) {
return true;
} else {
return false;
@ -75,13 +83,13 @@ function testExistImg($file) {
* @author Knah Tsaeb
* @date 2013-02-12
* @param $url (string) url for thumbshot
* @param $hashUrl (md5) md5($url)
* @param $hashUrl (md5) md5($url)
* @param $width (string) size of thumbnail 190x90
* @param $onlyThumb (bool)
* @param $waiForResult (bool)
* @return
*/
function launchScript($url, $hashUrl, $width, $onlyThumb, $waitForResult = false) {
function launchScript($url, $hashUrl, $width, $size, $onlyThumb, $waitForResult = false) {
$hashUrl = escapeshellarg($hashUrl);
$url = escapeshellarg($url);
$width = escapeshellarg($width);
@ -92,17 +100,21 @@ function launchScript($url, $hashUrl, $width, $onlyThumb, $waitForResult = false
}
if ($waitForResult === false) {
exec('bin/thumb_server.sh ' . $url . ' ' . $hashUrl . ' ' . $width . ' ' . $onlyThumb . ' > /dev/null &', $result);
exec('bin/thumb_server.sh '.$url.' '.$hashUrl.' '.$width.' '.$size.' '.$onlyThumb.' > /dev/null &', $result);
} else {
exec('bin/thumb_server.sh ' . $url . ' ' . $hashUrl . ' ' . $width . ' ' . $onlyThumb . ' 1', $result);
exec('bin/thumb_server.sh '.$url.' '.$hashUrl.' '.$width.' '.$size.' '.$onlyThumb.' 1', $result);
}
return $result;
}
function testValidUrl($url) {
$url = trim($url);
if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED)) {
$url = parse_url($url);
if (!in_array($url['scheme'], array('http', 'https'))) {
if (!in_array($url['scheme'], array(
'http',
'https'
))) {
return array('msg' => 'Url must be start by http or https.');
}
return true;
@ -112,7 +124,7 @@ function testValidUrl($url) {
}
function genToken() {
$token = sha1(uniqid(rand(), true) . '_' . mt_rand());
$token = sha1(uniqid(rand(), true).'_'.mt_rand());
$_SESSION['token'] = $token;
return $token;
}
@ -147,14 +159,14 @@ function ban() {
$banList[$userIp]['lastBan'] = time();
$banList[$userIp]['nbBan'] = 1;
}
file_put_contents('cache/logs/banUser.php', "<?php\n\$banList=" . var_export($banList, true) . ";\n?>");
file_put_contents('cache/logs/banUser.php', "<?php\n\$banList=".var_export($banList, true).";\n?>");
}
function unBan() {
require 'cache/logs/banUser.php';
$userIp = $_SERVER['REMOTE_ADDR'];
unset($banList[$userIp]);
file_put_contents('cache/logs/banUser.php', "<?php\n\$banList=" . var_export($banList, true) . ";\n?>");
file_put_contents('cache/logs/banUser.php', "<?php\n\$banList=".var_export($banList, true).";\n?>");
}
function install() {
@ -173,9 +185,9 @@ function install() {
if (!mkdir('cache/tmp', 0705)) {
die('Error on create dir "cache/tmp".');
}
$salt = sha1(uniqid(rand(), true) . '_' . mt_rand());
$serverKey = substr(sha1(uniqid(rand(), true) . '_' . mt_rand() . $salt), 0, 12);
$encryptServerKey = sha1($serverKey . $salt);
$salt = sha1(uniqid(rand(), true).'_'.mt_rand());
$serverKey = substr(sha1(uniqid(rand(), true).'_'.mt_rand().$salt), 0, 12);
$encryptServerKey = sha1($serverKey.$salt);
if (!is_file('cache/config/genConf.php')) {
file_put_contents('cache/config/genConf.php', "<?php\n\$GLOBALS['config']['serverKey'] = '$encryptServerKey';\n\$GLOBALS['config']['salt'] = '$salt';\n?>");
@ -192,7 +204,13 @@ function install() {
if (!is_file('cache/logs/log.txt')) {
touch('cache/logs/log.txt');
}
echo '<div>This is the key for generate thumbnail whith GET method. Save it, this key is secret, don\'t share it. <p class="alert">', $serverKey, '</p></div>';
echo '
<div>
This is the key for generate thumbnail whith GET method. Save it, this key is secret, don\'t share it.
<p class="alert">
', $serverKey, '
</p>
</div>';
checkInstall();
}
@ -211,17 +229,39 @@ function checkInstall() {
}
}
function removeOlderThan($dir = 'cache/img/shortLived') {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object !== '.' && $object !== '..' && $object) {
if (filetype($dir.'/'.$object) === 'dir') {
removeOlderThan($dir.'/'.$object);
} else {
if(fileatime($dir.'/'.$object) < time() - 3600*$GLOBALS['config']['expireCache']){
unlink($dir.'/'.$object);
}
}
}
}
reset($objects);
}
}
checkInstall();
removeOlderThan();
if ($_GET) {
$ui = $_GET;
unset($_GET);
if (empty($ui['key']) || empty($ui['url'])) {
die('Are you Ken ?');
}
if (sha1($ui['key'] . $GLOBALS['config']['salt']) !== $GLOBALS['config']['serverKey']) {
if (sha1($ui['key'].$GLOBALS['config']['salt']) !== $GLOBALS['config']['serverKey']) {
ban();
die('I take a chips and give it to Godzilla. I print a shoes and .............. KAMOULOX ! Well done Jean Pierre.');
}
if (!array_key_exists($ui['s'], $GLOBALS['config']['thumbSize'])) {
die('Die another day !');
}
$ui['url'] = rawurldecode($ui['url']);
$testUrl = testValidUrl($ui['url']);
@ -239,15 +279,15 @@ if ($_GET) {
$width = $GLOBALS['config']['defaultThumbSize'];
}
$hashUrl = sha1($GLOBALS['config']['serverKey'] . $defUrl);
$startPath = substr($hashUrl, 0, 2) . '/' . substr($hashUrl, 2, 2) . '/';
$file = 'cache/img/' . $startPath . $hashUrl;
$hashUrl = sha1($GLOBALS['config']['serverKey'].$defUrl);
$startPath = substr($hashUrl, 0, 2).'/'.substr($hashUrl, 2, 2).'/';
$file = 'cache/img/'.$ui['s'].'/'.$startPath.$hashUrl;
if (testExistImg($file) !== true || isset($ui['fr']) && (int)$ui['fr'] === 1) {
if (isset($ui['iw']) && (int)$ui['iw'] === 1) {
launchScript($defUrl, $hashUrl, $width, $GLOBALS['config']['onlyThumb'], true);
launchScript($defUrl, $hashUrl, $width, $ui['s'], $GLOBALS['config']['onlyThumb'], true);
} else {
$res = launchScript($defUrl, $hashUrl, $width, $GLOBALS['config']['onlyThumb'], false);
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], $GLOBALS['config']['onlyThumb'], false);
header("Content-type: image/png");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo file_get_contents('bin/loadingGen.png');
@ -255,11 +295,11 @@ if ($_GET) {
}
}
header("Content-type: image/png");
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
header('Expires: ', gmdate('D, d M Y H:i:s', time()).' GMT');
if (!empty($ui['t']) && $ui['t'] === 'full') {
echo file_get_contents($file . '.png');
echo file_get_contents($file.'.png');
} else {
echo file_get_contents($file . '_thumb.png');
echo file_get_contents($file.'_thumb.png');
}
exit();
}
@ -280,6 +320,10 @@ if ($_POST) {
exit();
}
if (!array_key_exists($ui['s'], $GLOBALS['config']['thumbSize'])) {
die('Die another day !');
}
$defUrl = $ui['url'];
if (isset($ui['s']) && array_key_exists($ui['s'], $GLOBALS['config']['thumbSize'])) {
@ -288,14 +332,17 @@ if ($_POST) {
$width = $GLOBALS['config']['defaultThumbSize'];
}
$hashUrl = sha1($GLOBALS['config']['serverKey'] . $defUrl);
$startPath = substr($hashUrl, 0, 2) . '/' . substr($hashUrl, 2, 2) . '/';
$file = 'cache/img/' . $startPath . $hashUrl;
$hashUrl = sha1($GLOBALS['config']['serverKey'].$defUrl);
$startPath = substr($hashUrl, 0, 2).'/'.substr($hashUrl, 2, 2).'/';
$file = 'cache/img/shortLived/'.$startPath.$hashUrl;
if (testExistImg($file) !== true || isset($ui['fr']) && (int)$ui['fr'] === 1) {
launchScript($defUrl, $hashUrl, $width, $GLOBALS['config']['onlyThumb'], true);
launchScript($defUrl, $hashUrl, $width, 'shortLived', $GLOBALS['config']['onlyThumb'], true);
}
$success = array('normal' => $file . '.png', 'thumb' => $file . '_thumb.png');
$success = array(
'normal' => $file.'.png',
'thumb' => $file.'_thumb.png'
);
}
if (empty($defUrl)) {
@ -347,7 +394,8 @@ if (empty($width)) {
<?php
if (!empty($success)) {
echo '<div id="result">';
echo '<img src="', $success['thumb'], '?r=', time(), '"/>';
echo '<h3>This image will removed in 24h</h3>';
echo '<img src="', $success['thumb'], '?r=', time(), '"/>';
echo '<p><label>', $width, '</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['thumb'], '" onclick="this.select()"/></p>';
if ($GLOBALS['config']['onlyThumb'] === false) {
echo '<p><label>1280x1024</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['normal'], '" onclick="this.select()"/></p>';