commit 7cee3ed6f5c54fb3d6d5eccef9eede9e446fba45 Author: Knah Tsaeb Date: Thu Mar 14 11:56:21 2013 +0100 First release diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a7e9fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +cache/* +.project diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..8c115f6 --- /dev/null +++ b/.htaccess @@ -0,0 +1,5 @@ +AddDefaultCharset UTF-8 +Options -Indexes +DirectoryIndex index.php index.html +FileETag none +SetOutputFilter DEFLATE diff --git a/bin/.htaccess b/bin/.htaccess new file mode 100644 index 0000000..b584d98 --- /dev/null +++ b/bin/.htaccess @@ -0,0 +1,2 @@ +Allow from none +Deny from all diff --git a/bin/index.html b/bin/index.html new file mode 100644 index 0000000..e69de29 diff --git a/bin/phantomjs b/bin/phantomjs new file mode 100755 index 0000000..20cf231 Binary files /dev/null and b/bin/phantomjs differ diff --git a/bin/rasterize.js b/bin/rasterize.js new file mode 100644 index 0000000..db312af --- /dev/null +++ b/bin/rasterize.js @@ -0,0 +1,24 @@ +var page = new WebPage(), + address, output, size; + +if (phantom.args.length < 2) { + console.log('Usage: rasterize.js URL filename'); + phantom.exit(); +} else { + address = phantom.args[0]; + output = phantom.args[1]; + + page.viewportSize = { width: 1280, height: 1024 }; + + page.open(address, function (status) { + if (status !== 'success') { + console.log('Unable to load the address!'); + } else { + window.setTimeout(function () { + page.render(output); + phantom.exit(); + }, 500); + } + }); +} + diff --git a/bin/thumb_server.sh b/bin/thumb_server.sh new file mode 100755 index 0000000..f3e93ad --- /dev/null +++ b/bin/thumb_server.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# inspirated source http://www.cambus.net/blog/page/3/ + +site=$1 +md5Site=$2 +thumbWidth=$3 +#thumbHeight=$5 + +#firstLevel=${md5Site:0:2} +#secondLevel=${md5Site:2:2} +#mkdir -p "cache/img/$firstLevel/$secondLevel/" +./bin/phantomjs --ignore-ssl-errors=true /bin/rasterize.js "$site" "cache/tmp/$md5Site.png" +#convert "cache/tmp/$md5Site.png" -crop 1280x1024+0+0 "cache/img/$firstLevel/$secondLevel/$md5Site.png" +#convert "cache/img/$firstLevel/$secondLevel/$md5Site.png" -filter Lanczos -thumbnail "$thumbWidth" "cache/img/$firstLevel/$secondLevel/$md5Site""_thumb.png" +convert "cache/tmp/$md5Site.png" -crop 1280x1024+0+0 "cache/img/$md5Site.png" && +convert "cache/img/$md5Site.png" -filter Lanczos -thumbnail "$thumbWidth" "cache/img/$md5Site""_thumb.png" +rm "cache/tmp/$md5Site.png" + +#if [[ -f "cache/img/$firstLevel/$secondLevel/$md5Site""_thumb.png" && -f "cache/img/$firstLevel/$secondLevel/$md5Site.png" ]] +if [[ -f "cache/img/$md5Site""_thumb.png" && -f "cache/img/$md5Site.png" ]] +then + echo 1 +else + echo 0 +fi diff --git a/inc/style.css b/inc/style.css new file mode 100644 index 0000000..a44819f --- /dev/null +++ b/inc/style.css @@ -0,0 +1,77 @@ +html { + padding: 1em; + font-family: arial, sans-serif; + font-weight: bold; + line-height: 2em; + background-color: #dedede; +} + +form { + padding: 1em; + text-align: center; + width: 50%; + margin: 0 auto; + background-color: #ffffff; + border-radius: 5px; + box-shadow: 0 0 8px rgba(0, 0, 0, 0.50), 0 85px 180px 0 #FFFFFF, 0 12px 8px -5px rgba(0, 0, 0, 0.95); +} + +input, select { + height: 2em; +} + +input[type=url] { + width: 100%; +} + +label { + margin-right: 1em; +} + +form .info { + text-align : right; + margin:0 0; + line-height : 0.8em; + font-size : 0.8em; +} + +#result { + text-align: center; + padding: 1em; +} + +#result img { + box-shadow: 3px 3px 3px #cdcdcd; + border: 1px solid #cdcdcd; + border-radius: 5px; +} + +@media screen and (min-width: 320px) and (max-width: 800px) { + + html { + padding: 0.1em; + } + + form { + width: 100%; + padding: 0.1em; + } + + input[type=url] { + width: 99%; + height: 2em; + } + input[type=submit] { + width: 99%; + height: 2em; + } + + label { + display: block; + } + select { + width: 99%; + height: 2em; + display: block; + } +} \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..0d31fee --- /dev/null +++ b/index.php @@ -0,0 +1,291 @@ + '100x80', '200x160', '300x240', '400x320', '500x400'); + +if (!file_exists('cache/config/genConf.php')) { + install(); +} + +require 'cache/config/genConf.php'; +$ui = ''; + +if (($GLOBALS['config']['onlyLocalServer'] === true && $_SERVER['REMOTE_ADDR'] !== '127.0.0.1') || checkIfBan() === true) { + header("HTTP/1.0 404 Not Found"); + echo "

404 Not Found

"; + echo "The page that you have requested could not be found."; + exit(); +} + +if (get_magic_quotes_gpc()) { + function stripslashes_deep($value) { + $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); + return $value; + } + + $_POST = array_map('stripslashes_deep', $_POST); + $_GET = array_map('stripslashes_deep', $_GET); + $_COOKIE = array_map('stripslashes_deep', $_COOKIE); +} + +function testExistImg($file) { + if (file_exists($file . '_thumb.png') && file_exists($file . '.png')) { + return true; + } else { + return false; + } +} + +function launchScript($url, $md5Url, $width, $height = '') { + $md5Url = escapeshellarg($md5Url); + $url = escapeshellarg($url); + $width = escapeshellarg($width); + exec('bin/thumb_server.sh ' . $url . ' ' . $md5Url . ' ' . $width . ' ' . $height, $result); + return $result; +} + +function testValidUrl($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'))) { + return array('msg' => 'Url must be start by http or https.'); + } + return true; + } else { + return array('msg' => 'Url are not valid.'); + } +} + +function genToken() { + $token = sha1(uniqid(rand(), true) . '_' . mt_rand()); + $_SESSION['token'] = $token; + return $token; +} + +function verifToken($token) { + if ($token !== $_SESSION['token']) { + ban(); + die('So Long, and Thanks for All the Fish.'); + } +} + +function checkIfBan() { + require 'cache/logs/banUser.php'; + $userIp = $_SERVER['REMOTE_ADDR']; + if (isset($banList[$userIp]) && $banList[$userIp]['nbBan'] >= $GLOBALS['config']['maxErrorBeforeBan'] && $banList[$userIp]['lastBan'] + $GLOBALS['config']['banTime'] > time()) { + return true; + } elseif (isset($banList[$userIp]) && $banList[$userIp]['lastBan'] + $GLOBALS['config']['banTime'] < time()) { + unban(); + return false; + } else { + return false; + } +} + +function ban() { + require 'cache/logs/banUser.php'; + $userIp = $_SERVER['REMOTE_ADDR']; + if (isset($banList[$userIp])) { + $banList[$userIp]['lastBan'] = time(); + $banList[$userIp]['nbBan']++; + } else { + $banList[$userIp]['lastBan'] = time(); + $banList[$userIp]['nbBan'] = 1; + } + file_put_contents('cache/logs/banUser.php', ""); +} + +function unBan() { + require 'cache/logs/banUser.php'; + $userIp = $_SERVER['REMOTE_ADDR']; + unset($banList[$userIp]); + file_put_contents('cache/logs/banUser.php', ""); +} + +function install() { + if (!is_writable('cache')) { + die('Make dir "cache" writable'); + } + if (!mkdir('cache/config', 0705)) { + die('Error on create dir "cache/config".'); + } + if (!mkdir('cache/img', 0705)) { + die('Error on create dir "cache/img".'); + } + if (!mkdir('cache/logs', 0705)) { + die('Error on create dir "cache/logs".'); + } + 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); + + if (!is_file('cache/config/genConf.php')) { + file_put_contents('cache/config/genConf.php', ""); + } + if (!is_file('cache/logs/banUser.php')) { + file_put_contents('cache/logs/banUser.php', ""); + } + if (!is_file('cache/index.html')) { + file_put_contents('cache/index.html', " "); + } + echo '
This is the key for generate thumbnail whith GET method. Save it, this key is secret, don\'t share it.

', $serverKey, '

'; + checkInstall(); +} + +function checkInstall() { + if (!is_file('.htaccess')) { + file_put_contents('.htaccess', "AddDefaultCharset UTF-8\nOptions -Indexes\nDirectoryIndex index.php index.html\nFileETag none\nSetOutputFilter DEFLATE\n"); + } + if (!is_file('cache/logs/.htaccess')) { + file_put_contents('cache/logs/.htaccess', "Allow from none\nDeny from all\n"); + } + if (!is_file('cache/config/.htaccess')) { + file_put_contents('cache/logs/.htaccess', "Allow from none\nDeny from all\n"); + } + if (!is_file('bin/.htaccess')) { + file_put_contents('bin/.htaccess', "Allow from none\nDeny from all\n"); + } +} + +checkInstall(); +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']) { + ban(); + die('I take a chips and give it to Godzilla. I print a shoes and .............. KAMOULOX ! Well done Jean Pierre.'); + } + + $ui['url'] = urldecode($ui['url']); + $testUrl = testValidUrl($ui['url']); + if ($testUrl !== true) { + echo $testUrl['msg']; + exit(); + } + + $defUrl = $ui['url']; + if (isset($ui['s']) && (int)$ui['s']) { + $width = $GLOBALS['config']['thumbSize'][$ui['s']]; + } else { + $width = $GLOBALS['config']['defaultThumbSize']; + } + + $md5Url = md5($defUrl); + $file = 'cache/img/' . $md5Url; + + if (testExistImg($file) !== true || isset($ui['fr']) && (int)$ui['fr'] === 1) { + $res = launchScript($defUrl, $md5Url, $width); + if ((int)$res !== 1) { + die('Internal error'); + } + } + + header("Content-type: image/png"); + if (!empty($ui['t']) && $ui['t'] === 'full') { + echo file_get_contents($file . '.png'); + } else { + echo file_get_contents($file . '_thumb.png'); + } + exit(); +} + +if ($_POST) { + $ui = $_POST; + unset($_POST); + if (empty($ui['url'])) { + die('No url, no thumb'); + } + verifToken($ui['token']); + + $ui['url'] = urldecode($ui['url']); + + $testUrl = testValidUrl($ui['url']); + if ($testUrl !== true) { + echo $testUrl['msg']; + exit(); + } + + $defUrl = $ui['url']; + + if (isset($ui['s']) && (int)$ui['s']) { + $width = $GLOBALS['config']['thumbSize'][$ui['s']]; + } else { + $width = $GLOBALS['config']['defaultThumbSize']; + } + + $md5Url = md5($defUrl); + $file = 'cache/img/' . $md5Url; + + if (testExistImg($file) !== true || isset($ui['fr']) && (int)$ui['fr'] === 1) { + $res = launchScript($defUrl, $md5Url, $width); + if ((int)$res !== 1) { + die('Internal error'); + } + } + $success = array('normal' => $file . '.png', 'thumb' => $file . '_thumb.png'); +} + +if (empty($defUrl)) { + $defUrl = $GLOBALS['config']['defaultUrl']; +} +?> + + + + + KT WebThumb + + + + +
+

+ +

+

+ + + + +

+

+ + +

+

+ Homepage +

+
+
+ '; + } + ?> +
+ + \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..8e09907 --- /dev/null +++ b/readme.txt @@ -0,0 +1 @@ +All info in the homepage http://forge.leslibres.org/projects/soshot