Add support for fetch favicon

This commit is contained in:
Knah Tsaeb 2018-10-01 14:08:36 +02:00
parent 2f21a6f6b7
commit 9d3a3062e8
4 changed files with 136 additions and 35 deletions

BIN
bin/loadingGen_favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -146,6 +146,7 @@ function tryWithCurl($hash, $log)
$line = trim($line); $line = trim($line);
$line = explode(' --- ', $line); $line = explode(' --- ', $line);
//$hash = sha1($GLOBALS['config']['salt'].rawurldecode($line[1])); //$hash = sha1($GLOBALS['config']['salt'].rawurldecode($line[1]));
$line[1] = iconv($charset, "UTF-8", $line[1]);
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $line[1]); curl_setopt($ch, CURLOPT_URL, $line[1]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@ -189,6 +190,7 @@ function tryWithOpenGraph($hash, $log)
$line = explode(' --- ', $line); $line = explode(' --- ', $line);
$url = $line[1]; $url = $line[1];
$startPath = ''; $startPath = '';
$url = iconv($charset, "UTF-8", $url);
$info = Embed::create($url); $info = Embed::create($url);
$ext = testIfImg($info->image); $ext = testIfImg($info->image);
@ -288,6 +290,7 @@ if (isset($ui['ac']) && $ui['ac'] === 'delete') {
} }
if (isset($ui['log']) && $ui['log'] === 'success') { if (isset($ui['log']) && $ui['log'] === 'success') {
$logs = parseSuccessLog(); $logs = parseSuccessLog();
createDir();
if (!empty($logs)) { if (!empty($logs)) {
$nbThumb = 0; $nbThumb = 0;
$nbThumb = count($logs['genTime']); $nbThumb = count($logs['genTime']);
@ -337,7 +340,7 @@ if (isset($ui['log']) && ($ui['log'] === 'suspect' || $ui['log'] === 'retry' ||
} }
} }
echo '</div>'; echo '</div>';
if (!empty($response) && $response['success'] === 1) { if (!empty($response) && !empty($response['success']) && $response['success'] === 1) {
echo '<div id="result">'; echo '<div id="result">';
echo '<img src="', $response['base64'], '" style="width:100%;"/>'; echo '<img src="', $response['base64'], '" style="width:100%;"/>';
echo '<p><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $response['filePath'], '" onclick="this.select()"/></p>'; echo '<p><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $response['filePath'], '" onclick="this.select()"/></p>';

View file

@ -32,6 +32,7 @@
if (!empty($success)) { if (!empty($success)) {
echo '<div id="result">'; echo '<div id="result">';
echo '<h3>This image will be removed in ', $GLOBALS['config']['expireCache'], 'h</h3>'; echo '<h3>This image will be removed in ', $GLOBALS['config']['expireCache'], 'h</h3>';
echo '<img src="data:image/png;base64,',base64_encode(file_get_contents($success['favicon'])), '"/><br/>';
echo '<img src="data:image/png;base64,',base64_encode(file_get_contents($success['thumb'])), '"/>'; echo '<img src="data:image/png;base64,',base64_encode(file_get_contents($success['thumb'])), '"/>';
echo '<p><label>Thumbshot</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['thumb'], '" onclick="this.select()"/>'; echo '<p><label>Thumbshot</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['thumb'], '" onclick="this.select()"/>';
if ($GLOBALS['config']['activeFullSize'] === true) { if ($GLOBALS['config']['activeFullSize'] === true) {
@ -40,5 +41,6 @@ if (!empty($success)) {
if ($GLOBALS['config']['activeComplete'] === true) { if ($GLOBALS['config']['activeComplete'] === true) {
echo '<p><label>Complete</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['complete'], '" onclick="this.select()"/></p>'; echo '<p><label>Complete</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['complete'], '" onclick="this.select()"/></p>';
} }
echo '<p><label>Favicon</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['favicon'], '" onclick="this.select()"/></p>';
echo '</div>'; echo '</div>';
} }

144
index.php
View file

@ -1,4 +1,5 @@
<?php <?php
use Embed\Embed;
session_start(); session_start();
if (empty($_SESSION['login'])) { if (empty($_SESSION['login'])) {
$_SESSION['login'] = false; $_SESSION['login'] = false;
@ -29,6 +30,8 @@ $GLOBALS['config']['thumbSize'] = array(
$GLOBALS['config']['activeFullSize'] = false; $GLOBALS['config']['activeFullSize'] = false;
// create thumbshot of complete page // create thumbshot of complete page
$GLOBALS['config']['activeComplete'] = false; $GLOBALS['config']['activeComplete'] = false;
// create favicon
$GLOBALS['config']['favicon'] = false;
// Remove image older than 12 hours // Remove image older than 12 hours
$GLOBALS['config']['expireCache'] = 12; $GLOBALS['config']['expireCache'] = 12;
// Disable exec command and use cron task // Disable exec command and use cron task
@ -98,33 +101,42 @@ if ($GLOBALS['config']['NoWebPage'] === true && empty($_GET)) {
echo "The page that you have requested could not be found."; echo "The page that you have requested could not be found.";
exit(); exit();
} }
function testExistImg($file) function testExistImg($file, $type)
{ {
if ($GLOBALS['config']['activeFullSize'] && $GLOBALS['config']['activeComplete']) { /*
if (file_exists($file . '_thumb.png') && file_exists($file . '.png') && file_exists($file . '_complete.png')) { type :
- c complete
- r full
- thumbnail
- fav favicon
*/
switch ($type) {
case 'c':
if (file_exists($file . '_complete.png')) {
return true; return true;
} else {
return false;
} }
} elseif ($GLOBALS['config']['activeFullSize']) { break;
if (file_exists($file . '_thumb.png') && file_exists($file . '.png')) { case 'r':
if (file_exists($file . '_full.png')) {
return true; return true;
} else {
return false;
} }
} elseif ($GLOBALS['config']['activeComplete']) { break;
if (file_exists($file . '_thumb.png') && file_exists($file . '_complete.png')) { case 'fav':
if (file_exists($file . '_favicon.png')) {
return true; return true;
} else {
return false;
} }
} else { break;
case 't':
if (file_exists($file . '_thumb.png')) { if (file_exists($file . '_thumb.png')) {
return true; return true;
} else { }
break;
default:
return false; return false;
break;
} }
}
return false;
} }
/** /**
@ -254,7 +266,7 @@ function unBan()
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() function createDir()
{ {
if (!is_writable('cache')) { if (!is_writable('cache')) {
die('Make dir "cache" writable'); die('Make dir "cache" writable');
@ -283,6 +295,11 @@ function install()
if (!is_dir('cache/cronTask') && !mkdir('cache/cronTask', 0705)) { if (!is_dir('cache/cronTask') && !mkdir('cache/cronTask', 0705)) {
die('Error on create dir "cache/cronTask".'); die('Error on create dir "cache/cronTask".');
} }
}
function install()
{
createDir();
$salt = sha1(uniqid(rand(), true) . '_' . mt_rand()); $salt = sha1(uniqid(rand(), true) . '_' . mt_rand());
$serverKey = substr(sha1(uniqid(rand(), true) . '_' . mt_rand() . $salt), 0, 12); $serverKey = substr(sha1(uniqid(rand(), true) . '_' . mt_rand() . $salt), 0, 12);
$encryptServerKey = sha1($serverKey . $salt); $encryptServerKey = sha1($serverKey . $salt);
@ -468,6 +485,9 @@ function printThumbShot($file, $renderType)
case 'r': case 'r':
echo file_get_contents($file . '.png'); echo file_get_contents($file . '.png');
break; break;
case 'fav':
echo file_get_contents($file . '_favicon.png');
break;
default: default:
echo file_get_contents($file . '_thumb.png'); echo file_get_contents($file . '_thumb.png');
break; break;
@ -496,7 +516,7 @@ function getPage($page)
function testIfImg($url) function testIfImg($url)
{ {
$ext = strtolower(pathinfo($url, PATHINFO_EXTENSION)); $ext = strtolower(pathinfo(strtok($url, '?'), PATHINFO_EXTENSION));
if ($ext === 'jpg' || $ext === 'jpeg' || $ext === 'png' || $ext === 'gif' || $ext === 'ico') { if ($ext === 'jpg' || $ext === 'jpeg' || $ext === 'png' || $ext === 'gif' || $ext === 'ico') {
return $ext; return $ext;
} else { } else {
@ -564,6 +584,63 @@ function makeHmac($url)
return hash_hmac('sha1', $url, $GLOBALS['config']['apikey']); return hash_hmac('sha1', $url, $GLOBALS['config']['apikey']);
} }
function makeHashPath($hashUrl)
{
return substr($hashUrl, 0, 2) . '/' . substr($hashUrl, 2, 2);
}
function makeFavicon($hash, $url, $filePath)
{
require_once 'vendor/autoload.php';
$resResize = '';
$data = array();
$data['hashPath'] = $hashPath = $filePath;
$data['file'] = $file = $filePath . $hash . '_favicon.png';
$info = Embed::create($url, [
'follow_canonical' => true,
'external_images' => true,
]);
$faviconUrl = $info->providerIcon;
$data['ext'] = testIfImg($faviconUrl);
$data['favicon'] = $faviconUrl;
$image = file_get_contents($faviconUrl);
$fullSize = file_put_contents('cache/tmp/' . $hash . '.' . $data['ext'], $image);
$tmpFile = 'cache/tmp/' . $hash . '.' . $data['ext'];
if ($data['ext'] === 'ico') {
$getBestSize = exec('identify ' . escapeshellarg($tmpFile), $originalFav);
if (is_array($originalFav)) {
end($originalFav);
$key = key($originalFav);
if (!is_dir($hashPath)) {
mkdir($hashPath, 0700, true);
}
exec('convert -background transparent ' . escapeshellarg($tmpFile[' . $key . ']) . ' -resize x32 ' . $file, $resResize);
unlink('cache/tmp/' . $hash . '.' . $data['ext']);
}
} else {
if (!is_dir($hashPath)) {
mkdir($hashPath, 0700, true);
}
exec('convert -background transparent ' . escapeshellarg($tmpFile) . ' -resize x32 ' . $file, $resResize);
unlink('cache/tmp/' . $hash . '.' . $data['ext']);
}
//n_print($data);
if (file_exists($file)) {
return $file;
}
}
function makeHashUrl($url)
{
return sha1($GLOBALS['config']['salt'] . $url);
}
/* /*
$image = file_get_contents('http://www.url.com/image.jpg'); $image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //save the image on your server file_put_contents('/images/image.jpg', $image); //save the image on your server
@ -585,19 +662,24 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
if (empty($ui['url'])) { if (empty($ui['url'])) {
die('You see in this world there\'s two kinds of people, my friend. Those with loaded guns, and those who dig. You dig.'); die('You see in this world there\'s two kinds of people, my friend. Those with loaded guns, and those who dig. You dig.');
} }
if ($ui['request'] === 'form') {
//$file = 'cache/img/shortLive/' . $startPath . $hashUrl;
$ui['s'] = 'shortLive';
}
$ui['sendUrl'] = $ui['url']; $ui['sendUrl'] = $ui['url'];
$ui['url'] = trim(rawurldecode($ui['url'])); $ui['url'] = trim(rawurldecode($ui['url']));
$ui['url'] = rtrim($ui['url'], '/'); $ui['url'] = rtrim($ui['url'], '/');
$hashUrl = sha1($GLOBALS['config']['salt'] . $ui['url']); $hashUrl = sha1($GLOBALS['config']['salt'] . $ui['url']);
$startPath = substr($hashUrl, 0, 2) . '/' . substr($hashUrl, 2, 2) . '/'; $startPath = substr($hashUrl, 0, 2) . '/' . substr($hashUrl, 2, 2) . '/';
$filePath = 'cache/img/' . $ui['s'] . '/' . $startPath;
$file = 'cache/img/' . $ui['s'] . '/' . $startPath . $hashUrl; $file = 'cache/img/' . $ui['s'] . '/' . $startPath . $hashUrl;
$testUrl = testValidUrl($ui['url']); $testUrl = testValidUrl($ui['url']);
$defUrl = $ui['url']; $defUrl = $ui['url'];
if ($ui['request'] === 'form') {
$file = 'cache/img/shortLive/' . $startPath . $hashUrl; // file exist we return img
$file = 'cache/img/shortLive/' . $startPath . $hashUrl; if (testExistImg($file, $ui['t']) === true && (int) $ui['fr'] !== 1) {
}
if (testExistImg($file) === true && (int) $ui['fr'] !== 1) {
if ($ui['request'] === 'api') { if ($ui['request'] === 'api') {
if (checkHmac($ui['hm'], $ui['sendUrl'])) { if (checkHmac($ui['hm'], $ui['sendUrl'])) {
printThumbShot($file, $ui['t']); printThumbShot($file, $ui['t']);
@ -611,6 +693,7 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
'normal' => $file . '.png', 'normal' => $file . '.png',
'thumb' => $file . '_thumb.png', 'thumb' => $file . '_thumb.png',
'complete' => $file . '_complete.png', 'complete' => $file . '_complete.png',
'favicon' => $file . '_favicon.png',
); );
} }
} else { } else {
@ -625,6 +708,8 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
} else { } else {
verifToken($ui['token']); verifToken($ui['token']);
} }
// url isn't correct put it in suspect log
if ($testUrl !== true) { if ($testUrl !== true) {
if (!file_exists('cache/logs/' . $hashUrl . 'log')) { if (!file_exists('cache/logs/' . $hashUrl . 'log')) {
file_put_contents('cache/logs/suspect/' . $hashUrl . '.log', $_SERVER['REMOTE_ADDR'] . ' --- ' . $ui['url'] . ' --- ' . $hashUrl . ' --- ' . $width . ' --- ' . $ui['s'] . ' --- ' . $renderType . ' --- true' . "\n"); file_put_contents('cache/logs/suspect/' . $hashUrl . '.log', $_SERVER['REMOTE_ADDR'] . ' --- ' . $ui['url'] . ' --- ' . $hashUrl . ' --- ' . $width . ' --- ' . $ui['s'] . ' --- ' . $renderType . ' --- true' . "\n");
@ -636,6 +721,7 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
'normal' => 'bin/error.png', 'normal' => 'bin/error.png',
'thumb' => 'bin/error_thumb.png', 'thumb' => 'bin/error_thumb.png',
'complete' => 'bin/error.png', 'complete' => 'bin/error.png',
'favicon' => 'bin/error.png',
); );
} }
} else { } else {
@ -648,6 +734,7 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
$makeImg = makeImgThumb($ui['url'], $ext, $hashUrl, $genWidth[0], 'cache/img/shortLive/' . $startPath, $ui['t']); $makeImg = makeImgThumb($ui['url'], $ext, $hashUrl, $genWidth[0], 'cache/img/shortLive/' . $startPath, $ui['t']);
} }
if ($makeImg === true) { if ($makeImg === true) {
//
$GLOBALS['config']['disableExec'] = true; $GLOBALS['config']['disableExec'] = true;
if ($ui['request'] === 'api') { if ($ui['request'] === 'api') {
printThumbShot($file, $ui['t']); printThumbShot($file, $ui['t']);
@ -656,20 +743,28 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
'normal' => $file . '.png', 'normal' => $file . '.png',
'thumb' => $file . '_thumb.png', 'thumb' => $file . '_thumb.png',
'complete' => $file . '_complete.png', 'complete' => $file . '_complete.png',
'favicon' => $file . '_favicon.png',
); );
} }
} }
} }
if ((isset($ui['iw']) && (int) $ui['iw'] === 1 && $ui['request'] === 'api')) { if ((isset($ui['iw']) && (int) $ui['iw'] === 1 && $ui['request'] === 'api')) {
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], true); $res = launchScript($defUrl, $hashUrl, $width, $ui['s'], true);
} elseif (isset($ui['iw']) && (int) $ui['iw'] === 1 && $ui['request'] === 'form') { } elseif (isset($ui['iw']) && (int) $ui['iw'] === 1 && $ui['request'] === 'form') { // img not exist or force refresh make it and return it
$res = launchScript($defUrl, $hashUrl, $width, 'shortLive', true); $res = launchScript($defUrl, $hashUrl, $width, 'shortLive', true);
makeFavicon($hashUrl, $ui['url'], $filePath);
} else {
if ($ui['t'] === 'fav') {
$res = makeFavicon($hashUrl, $ui['url'], $filePath);
} else { } else {
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], false); $res = launchScript($defUrl, $hashUrl, $width, $ui['s'], false);
} }
}
if ($ui['request'] === 'api') { if ($ui['request'] === 'api') {
$file = 'bin/loadingGen'; $file = 'bin/loadingGen';
printThumbShot($file, $ui['t']); printThumbShot($file, $ui['t']);
die();
} else { } else {
if ($GLOBALS['config']['disableExec'] === true) { if ($GLOBALS['config']['disableExec'] === true) {
$file = 'bin/loadingGen'; $file = 'bin/loadingGen';
@ -678,6 +773,7 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
'normal' => $file . '.png', 'normal' => $file . '.png',
'thumb' => $file . '_thumb.png', 'thumb' => $file . '_thumb.png',
'complete' => $file . '_complete.png', 'complete' => $file . '_complete.png',
'favicon' => $file . '_favicon.png',
); );
} }
} }