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

164
index.php
View File

@ -1,4 +1,5 @@
<?php
use Embed\Embed;
session_start();
if (empty($_SESSION['login'])) {
$_SESSION['login'] = false;
@ -29,6 +30,8 @@ $GLOBALS['config']['thumbSize'] = array(
$GLOBALS['config']['activeFullSize'] = false;
// create thumbshot of complete page
$GLOBALS['config']['activeComplete'] = false;
// create favicon
$GLOBALS['config']['favicon'] = false;
// Remove image older than 12 hours
$GLOBALS['config']['expireCache'] = 12;
// 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.";
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')) {
return true;
} else {
/*
type :
- c complete
- r full
- thumbnail
- fav favicon
*/
switch ($type) {
case 'c':
if (file_exists($file . '_complete.png')) {
return true;
}
break;
case 'r':
if (file_exists($file . '_full.png')) {
return true;
}
break;
case 'fav':
if (file_exists($file . '_favicon.png')) {
return true;
}
break;
case 't':
if (file_exists($file . '_thumb.png')) {
return true;
}
break;
default:
return false;
}
} elseif ($GLOBALS['config']['activeFullSize']) {
if (file_exists($file . '_thumb.png') && file_exists($file . '.png')) {
return true;
} else {
return false;
}
} elseif ($GLOBALS['config']['activeComplete']) {
if (file_exists($file . '_thumb.png') && file_exists($file . '_complete.png')) {
return true;
} else {
return false;
}
} else {
if (file_exists($file . '_thumb.png')) {
return true;
} else {
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?>");
}
function install()
function createDir()
{
if (!is_writable('cache')) {
die('Make dir "cache" writable');
@ -283,6 +295,11 @@ function install()
if (!is_dir('cache/cronTask') && !mkdir('cache/cronTask', 0705)) {
die('Error on create dir "cache/cronTask".');
}
}
function install()
{
createDir();
$salt = sha1(uniqid(rand(), true) . '_' . mt_rand());
$serverKey = substr(sha1(uniqid(rand(), true) . '_' . mt_rand() . $salt), 0, 12);
$encryptServerKey = sha1($serverKey . $salt);
@ -468,6 +485,9 @@ function printThumbShot($file, $renderType)
case 'r':
echo file_get_contents($file . '.png');
break;
case 'fav':
echo file_get_contents($file . '_favicon.png');
break;
default:
echo file_get_contents($file . '_thumb.png');
break;
@ -496,7 +516,7 @@ function getPage($page)
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') {
return $ext;
} else {
@ -504,7 +524,7 @@ function testIfImg($url)
}
}
function makeImgThumb($url, $ext, $hashUrl, $width, $path, $renderType=null)
function makeImgThumb($url, $ext, $hashUrl, $width, $path, $renderType = null)
{
if (!function_exists('imagecreatefromjpeg')) {
return false;
@ -564,6 +584,63 @@ function makeHmac($url)
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');
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'])) {
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['url'] = trim(rawurldecode($ui['url']));
$ui['url'] = rtrim($ui['url'], '/');
$hashUrl = sha1($GLOBALS['config']['salt'] . $ui['url']);
$startPath = substr($hashUrl, 0, 2) . '/' . substr($hashUrl, 2, 2) . '/';
$filePath = 'cache/img/' . $ui['s'] . '/' . $startPath;
$file = 'cache/img/' . $ui['s'] . '/' . $startPath . $hashUrl;
$testUrl = testValidUrl($ui['url']);
$defUrl = $ui['url'];
if ($ui['request'] === 'form') {
$file = 'cache/img/shortLive/' . $startPath . $hashUrl;
$file = 'cache/img/shortLive/' . $startPath . $hashUrl;
}
if (testExistImg($file) === true && (int) $ui['fr'] !== 1) {
// file exist we return img
if (testExistImg($file, $ui['t']) === true && (int) $ui['fr'] !== 1) {
if ($ui['request'] === 'api') {
if (checkHmac($ui['hm'], $ui['sendUrl'])) {
printThumbShot($file, $ui['t']);
@ -611,6 +693,7 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
'normal' => $file . '.png',
'thumb' => $file . '_thumb.png',
'complete' => $file . '_complete.png',
'favicon' => $file . '_favicon.png',
);
}
} else {
@ -625,6 +708,8 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
} else {
verifToken($ui['token']);
}
// url isn't correct put it in suspect log
if ($testUrl !== true) {
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");
@ -636,6 +721,7 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
'normal' => 'bin/error.png',
'thumb' => 'bin/error_thumb.png',
'complete' => 'bin/error.png',
'favicon' => 'bin/error.png',
);
}
} 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']);
}
if ($makeImg === true) {
//
$GLOBALS['config']['disableExec'] = true;
if ($ui['request'] === 'api') {
printThumbShot($file, $ui['t']);
@ -656,20 +743,28 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
'normal' => $file . '.png',
'thumb' => $file . '_thumb.png',
'complete' => $file . '_complete.png',
'favicon' => $file . '_favicon.png',
);
}
}
}
if ((isset($ui['iw']) && (int) $ui['iw'] === 1 && $ui['request'] === 'api')) {
$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);
makeFavicon($hashUrl, $ui['url'], $filePath);
} else {
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], false);
if ($ui['t'] === 'fav') {
$res = makeFavicon($hashUrl, $ui['url'], $filePath);
} else {
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], false);
}
}
if ($ui['request'] === 'api') {
$file = 'bin/loadingGen';
printThumbShot($file, $ui['t']);
die();
} else {
if ($GLOBALS['config']['disableExec'] === true) {
$file = 'bin/loadingGen';
@ -678,6 +773,7 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
'normal' => $file . '.png',
'thumb' => $file . '_thumb.png',
'complete' => $file . '_complete.png',
'favicon' => $file . '_favicon.png',
);
}
}