Soshot/inc/admin.php

367 lines
13 KiB
PHP
Raw Normal View History

<?php
2018-09-26 09:20:09 +02:00
use Embed\Embed;
$acceptParam['log'] = array(
2018-09-26 09:20:09 +02:00
'suspect',
'success',
'other',
'retry',
);
if (isset($ui['pwd'])) {
2018-09-26 09:20:09 +02:00
checkAdmin($ui['pwd']);
}
if (isset($ui['log']) && !in_array($ui['log'], $acceptParam['log']) || $_SESSION['login'] !== true) {
2018-09-26 09:20:09 +02:00
die('Tell me, Mr Anderson, what good is a phone call if you\'re unable to speak ?');
}
2013-09-30 10:39:05 +02:00
if (empty($ui['log']) && empty($ui['ac'])) {
2018-09-26 09:20:09 +02:00
$ui['log'] = 'success';
}
2018-09-26 09:20:09 +02:00
function clearLog($log)
{
if (file_exists('cache/logs/' . $log . '.txt') && is_file('cache/logs/' . $log . '.txt')) {
file_put_contents('cache/logs/' . $log . '.txt', '');
}
}
2018-09-26 09:20:09 +02:00
function parseSuccessLog()
{
if (file_exists('cache/logs/success.txt')) {
$file = fopen('cache/logs/success.txt', "r");
$res = array();
if (!empty($file)) {
while (!feof($file)) {
$currentLine = trim(fgets($file));
$currentLine = explode(' ', $currentLine);
$currentLine = str_replace('s', '', $currentLine);
if (!empty($currentLine[5])) {
$res['genTime'][] = $currentLine[5];
}
}
} else {
$res['genTime'] = array();
}
fclose($file);
return $res;
}
}
function parseFailLog($log)
{
if (empty($log) || ($log !== 'suspect' && $log !== 'retry' && $log !== 'other')) {
die('Are you sure about this.');
}
$res = array();
$list = glob('cache/logs/' . $log . '/*.log');
if (!empty($list)) {
foreach ($list as $value) {
$line = file_get_contents($value);
$line = trim($line);
$line = explode(' --- ', $line);
if (!empty($line[7])) {
$res[] = array(
$line[1],
$line[2],
$line[4],
'Error ' . $line[7] . ' - ',
);
} else {
$res[] = array(
$line[1],
$line[2],
$line[4],
'',
);
}
}
}
return $res;
}
2018-09-26 09:20:09 +02:00
function manualGen($hash, $log)
{
$line = file_get_contents('cache/logs/' . $log . '/' . $hash . '.log');
$line = trim($line);
$line = explode(' --- ', $line);
$hash = sha1($GLOBALS['config']['salt'] . rawurldecode($line[1]));
file_put_contents('cache/tmp/manual.txt', $line[1]);
chdir('bin/');
exec('bash thumb_server.sh manual ' . escapeshellarg($hash) . ' ' . escapeshellarg($line[3]) . ' ' . escapeshellarg($line[4]) . ' trc 1', $result);
chdir('../');
if ((int) $result[0] == 1) {
$res['success'] = 1;
$res['filePath'] = pathForFile($line[4], $hash) . '.png';
$res['base64'] = 'data:image/png;base64,' . base64_encode(file_get_contents($res['filePath']));
} else {
$res['error'] = 1;
$res['msg'] = 'Can\'t generate thumbshot manualy, try curl method.';
}
2018-09-26 09:20:09 +02:00
return $res;
}
2018-09-26 09:20:09 +02:00
function delAndRegen($hash, $size)
{
$path = pathForFile($size, $hash);
if (file_exists($path . '.png')) {
unlink($path . '.png');
}
if (file_exists($path . '_thumb.png')) {
unlink($path . '_thumb.png');
}
if (file_exists($path . '_complete.png')) {
unlink($path . '_complete.png');
}
}
2018-09-26 09:20:09 +02:00
function tryRootDomain($hash, $log)
{
$line = file_get_contents('cache/logs/' . $log . '/' . $hash . '.log');
$line = trim($line);
$line = explode(' --- ', $line);
$hash = sha1($GLOBALS['config']['salt'] . rawurldecode($line[1]));
$parts = parse_url($line[1]);
$url = $parts['scheme'] . '://' . trim($parts['host']);
file_put_contents('cache/tmp/manual.txt', $url);
chdir('bin/');
exec('bash thumb_server.sh manual ' . escapeshellarg($hash) . ' ' . escapeshellarg($line[3]) . ' ' . escapeshellarg($line[4]) . ' trc 1', $result);
chdir('../');
if ((int) $result[0] === 1) {
$res['success'] = 1;
$res['filePath'] = pathForFile($line[4], $hash) . '.png';
$res['base64'] = 'data:image/png;base64,' . base64_encode(file_get_contents($res['filePath']));
return $res;
} else {
$res['error'] = 1;
$res['msg'] = 'Error with root domain';
return $res;
}
}
2018-09-26 09:20:09 +02:00
function keepThisThumb($hash, $log)
{
unlink('cache/logs/' . $log . '/' . $hash . '.log');
}
2018-09-26 09:20:09 +02:00
function tryWithCurl($hash, $log)
{
$line = file_get_contents('cache/logs/' . $log . '/' . $hash . '.log');
$line = trim($line);
$line = explode(' --- ', $line);
//$hash = sha1($GLOBALS['config']['salt'].rawurldecode($line[1]));
2018-10-01 14:08:36 +02:00
$line[1] = iconv($charset, "UTF-8", $line[1]);
2018-09-26 09:20:09 +02:00
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $line[1]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'SoShot - perSOnal thumbSHOTer');
$resultat = curl_exec($ch);
curl_close($ch);
$searchStr = array(
'href="//',
'src="//',
'content="//',
);
$replaceStr = array(
'href="http://',
'src="http://',
'content="http://',
);
$resultat = str_replace($searchStr, $replaceStr, $resultat);
file_put_contents('cache/tmp/' . $line[2] . '.html', $resultat);
chdir('bin/');
exec('bash thumb_server.sh ../cache/tmp/' . $hash . '.html ' . escapeshellarg($line[2]) . ' ' . escapeshellarg($line[3]) . ' ' . escapeshellarg($line[4]) . ' trc 1 noErrorLog', $result);
chdir('../');
unlink('cache/tmp/' . $hash . '.html');
if ((int) $result[0] == 1) {
$res['success'] = 1;
$res['filePath'] = pathForFile($line[4], $hash) . '.png';
$res['base64'] = 'data:image/png;base64,' . base64_encode(file_get_contents($res['filePath']));
return $res;
} else {
$res['error'] = 1;
$res['msg'] = 'Error with curl'.n_print($result);
return $res;
}
}
2018-09-26 09:20:09 +02:00
function tryWithOpenGraph($hash, $log)
{
require_once 'vendor/autoload.php';
$line = file_get_contents('cache/logs/' . $log . '/' . $hash . '.log');
$line = trim($line);
$line = explode(' --- ', $line);
$url = $line[1];
$startPath = '';
2018-10-01 14:08:36 +02:00
$url = iconv($charset, "UTF-8", $url);
2018-09-26 09:20:09 +02:00
$info = Embed::create($url);
$ext = testIfImg($info->image);
if ($ext !== false) {
$original = file_get_contents($info->image);
file_put_contents('cache/tmp/' . $hash . '.' . $ext, $original);
$res['success'] = 1;
$res['filePath'] = 'cache/tmp/' . $hash . '.' . $ext;
$res['base64'] = 'data:image/png;base64,' . base64_encode($original);
unlink('cache/tmp/' . $hash . '.' . $ext);
$startPath = 'cache/img/' . $line[4] . '/' . substr($hash, 0, 2) . '/' . substr($hash, 2, 2) . '/';
$width = explode("x", $line[3]);
$thumb = makeImgThumb($info->image, $ext, $hash, $width[0], $startPath);
return $res;
} else {
$res['error'] = 1;
$res['msg'] = 'Error with OpenGraph data';
return $res;
}
}
2018-09-26 09:20:09 +02:00
if (isset($ui['clear']) && (int) $ui['clear'] === 1) {
clearLog($ui['log']);
}
2013-09-30 10:39:05 +02:00
if (isset($ui['ac']) && $ui['ac'] === 'delete' && isset($ui['thumb']) && !empty($ui['thumb'])) {
2018-09-26 09:20:09 +02:00
$imgUrl = pathForFile('m', $ui['thumb']);
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $imgUrl . '.png')) {
unlink($_SERVER['DOCUMENT_ROOT'] . '/' . $imgUrl . '.png');
}
if (file_exists($imgUrl . '_thumb.png')) {
unlink($_SERVER['DOCUMENT_ROOT'] . '/' . $imgUrl . '_thumb.png');
}
2013-09-30 10:39:05 +02:00
}
echo '<div class="hMenu">
<ul>
<li>
<a href="?p=admin&log=success">Success</a>
</li>
<li>
<a href="?p=admin&log=suspect">Suspect</a>
</li>
<li>
<a href="?p=admin&log=retry">Retry</a>
</li>
<li>
<a href="?p=admin&log=other">Other error</a>
</li>
2013-09-30 10:39:05 +02:00
<li>
<a href="?p=admin&ac=delete">Delete thumbshot</a>
</li>
<li>
<a href="?logout=1">Logout</a>
</li>
</ul>
</div>
2013-09-30 10:39:05 +02:00
<div id="log">';
if (isset($ui['ac']) && $ui['ac'] === 'delete') {
2018-09-26 09:20:09 +02:00
if (!empty($ui['delThumb']) && $ui['delThumb']) {
delAndRegen($ui['delThumb'], $size);
}
if (isset($ui['deleteUrl']) && testValidUrl($ui['deleteUrl']) !== true) {
unset($ui['deleteUrl']);
echo '<div>Not a valid url.</div>';
}
echo '
2013-09-30 10:39:05 +02:00
<form method="post" action="?">
<p>
<label>Url </label>';
2018-09-26 09:20:09 +02:00
if (empty($ui['deleteUrl'])) {
echo '<input type="text" name="deleteUrl"/>';
} else {
echo '<input type="text" name="deleteUrl" value="', $ui['deleteUrl'], '"/>';
}
echo '</p>
2013-09-30 10:39:05 +02:00
<p>
<input type="hidden" name="p" value="admin"/>
<input type="hidden" name="ac" value="delete"/>
<input type="submit"/>
</p>
</form>';
2018-09-26 09:20:09 +02:00
if (!empty($ui['deleteUrl'])) {
$ui['deleteUrl'] = trim(rawurldecode($ui['deleteUrl']));
$ui['deleteUrl'] = rtrim($ui['deleteUrl'], '/');
$hashUrl = sha1($GLOBALS['config']['salt'] . $ui['deleteUrl']);
$imgUrl = pathForFile('', $hashUrl);
echo '</div>
2013-09-30 10:39:05 +02:00
<div id="result">
<p>
<a href="', $imgUrl, '.png"><img src="', $imgUrl, '_thumb.png"/></a>
</p>
2015-07-10 11:04:56 +02:00
<a href="?p=admin&amp;ac=delete&amp;delThumb=', $hashUrl, '">Delete this thumb</a>
2013-09-30 10:39:05 +02:00
</div>';
2018-09-26 09:20:09 +02:00
}
2013-09-30 10:39:05 +02:00
}
if (isset($ui['log']) && $ui['log'] === 'success') {
2018-09-26 09:20:09 +02:00
$logs = parseSuccessLog();
2018-10-01 14:08:36 +02:00
createDir();
2018-09-26 09:20:09 +02:00
if (!empty($logs)) {
$nbThumb = 0;
$nbThumb = count($logs['genTime']);
echo 'Total request : ', $nbThumb, '</br>';
echo 'Moyenne per request : ', round(array_sum($logs['genTime']) / $nbThumb, 2), 's (min (', min($logs['genTime']), 's) max (', max($logs['genTime']), 's))</br>';
} else {
echo 'No result';
}
echo '<p class="clear" ><a href="?p=admin&log=success&clear=1">Clear success log</a></p>';
}
2013-09-30 10:39:05 +02:00
if (isset($ui['log']) && ($ui['log'] === 'suspect' || $ui['log'] === 'retry' || $ui['log'] === 'other')) {
2018-09-26 09:20:09 +02:00
if (isset($ui['hash']) && !empty($ui['hash'])) {
//$ui['hash'] = validHash($ui['hash']);
if (isset($ui['root']) && (int) $ui['root'] === 1) {
$response = tryRootDomain($ui['hash'], $ui['log']);
}
if (isset($ui['cache']) && (int) $ui['cache'] === 1) {
$response['success'] = 1;
$response['filePath'] = pathForFile($ui['s'], $ui['hash']) . '.png';
$response['base64'] = 'data:image/png;base64,' . base64_encode(file_get_contents($response['filePath']));
if (!file_exists($response['filePath'])) {
$response['base64'] = 'data:image/png;base64,' . base64_encode(file_get_contents('bin/error.png'));
}
}
if (isset($ui['keep']) && (int) $ui['keep'] === 1) {
keepThisThumb($ui['hash'], $ui['log']);
}
if (isset($ui['mGen']) && (int) $ui['mGen'] === 1) {
$response = manualGen($ui['hash'], $ui['log']);
}
if (isset($ui['tryCurl']) && (int) $ui['tryCurl'] === 1) {
$response = tryWithCurl($ui['hash'], $ui['log']);
}
if (isset($ui['openGraph']) && (int) $ui['openGraph'] === 1) {
$response = tryWithOpenGraph($ui['hash'], $ui['log']);
}
}
2018-09-26 09:20:09 +02:00
$logs = parseFailLog($ui['log']);
if (!empty($logs)) {
echo '<ul>';
foreach ($logs as $value) {
echo '<li>', htmlentities(strip_tags($value['3'])), htmlentities(strip_tags($value['0'])), '</br><a href="?p=admin&log=', $ui['log'], '&hash=', $value['1'], '&mGen=1#result">manuel launch</a> <a href="', htmlentities(strip_tags($value['0'])), '">view site</a> <a href="?p=admin&log=', $ui['log'], '&hash=', $value['1'], '&cache=1&s=', $value['2'], '#result">view image in cache</a></li>';
}
echo '</ul>';
} else {
echo 'No result';
}
}
echo '</div>';
2018-10-01 14:08:36 +02:00
if (!empty($response) && !empty($response['success']) && $response['success'] === 1) {
2018-09-26 09:20:09 +02:00
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>';
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&mGen=1">Manuel launch</a> / ';
if (extension_loaded("curl")) {
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&tryCurl=1">Try with curl</a> / ';
}
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&root=1">Try with root domain</a> / ';
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&openGraph=1">Try with OpenGraph</a> / ';
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&keep=1">Keep this thumbshot</a>';
echo '</div>';
}
2018-09-26 09:20:09 +02:00
if (!empty($response) && !empty($response['error']) && $response['error'] === 1) {
echo '<div id="result">';
echo '<p>', $response['msg'], '</p>';
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&mGen=1">Manuel launch</a> / ';
if (extension_loaded("curl")) {
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&tryCurl=1">Try with curl</a> / ';
}
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&root=1">Try with root domain</a> / ';
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&openGraph=1">Try with OpenGraph</a> / ';
echo '<a href="?p=admin&log=', $ui['log'], '&hash=', $ui['hash'], '&keep=1">Keep this thumbshot</a>';
echo '</div>';
}