Initial commit

This commit is contained in:
Knah Tsaeb 2013-07-12 17:25:57 +02:00
commit 20c0dbbcce
12 changed files with 610 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
data
tmp
videos
.project

1
bin/.htaccess Normal file
View File

@ -0,0 +1 @@
Deny from All

31
bin/cronTask.sh Normal file
View File

@ -0,0 +1,31 @@
#!/bin/bash/
finalDest=/srv/http/mytube.local/videos/
tempDest=/srv/http/mytube.local/tmp/
threads=3 # nb cpu core - 1
cd $tempDest
nbProcessLauch=$(ps -e | grep -v grep | grep youtube-dl | wc -l)
if [ $nbProcessLauch != 0 ]
then
echo 'bye'
exit
fi
for file in $tempDest*.hash
do
if [[ -f $file ]];
then
while read urlHash url destination
do
youtube-dl -R 0 -q -c --write-thumbnail --write-info-json --recode-video webm --no-check-certificate -f 46/37/45/22/44/35/34/5 -o "$urlHash.%(ext)s" "$url"
sleep 5
if [ ! -f $urlHash.*.part ]
then
rm $file
sleep 1
mv $urlHash* $finalDest$destination
fi
done < $file
fi
done

1
inc/.htaccess Normal file
View File

@ -0,0 +1 @@
Deny from All

6
inc/addVideo.php Normal file
View File

@ -0,0 +1,6 @@
<?php
require 'inc/topMenu.php';
n_print($CONFIG);
?>

176
inc/content.php Normal file
View File

@ -0,0 +1,176 @@
<?php
#todo n_print(chunk_split($text,8,'/'));
require 'inc/topMenu.php';
if (!empty($ci['ac']) && $ci['ac'] === 'login') {
echo '<form action="?" method="post">
<fieldset>
<legend>Login</login>
<label>Login</label>
<input type="text" name="loginName" required/>
<label>Password</label>
<input type="password" name="loginPass" required/>
<input type="hidden" name="op" value="2"/>
<input type="hidden" name="ac" value="login"/>
<input type="hidden" name="bc" value="'.$ci['bc'].'" />
<input type="hidden" name="token" value="', genToken(), '"/>
<input type="submit" />
</fieldset>
</form>';
}
if (!empty($ci['ac']) && $ci['ac'] === 'newDir') {
if (!empty($ci['op']) && (int)$ci['op'] === 1) {
verifToken($ci['token']);
$addDir = addDir($ci['newDir'], $ci['bc'], $fullListDir);
} else {
echo '<form action="?" method="post">
<fieldset>
<legend>
New directory
</legend>
<label>Name</label>
<input type="text" name="newDir"/>
<input type="hidden" name="op" value="1"/>
<input type="hidden" name="ac" value="newDir" />
<input type="hidden" name="bc" value="'.$ci['bc'].'" />
<input type="hidden" name="token" value="', genToken(), '"/>
<input type="submit"/>
</fieldset>
</form>';
}
}
if (!empty($ci['ac']) && $ci['ac'] === 'addVideo') {
if (!empty($ci['op']) && (int)$ci['op'] === 1) {
verifToken($ci['token']);
$addVideo = addVideo($ci, $fullListFile);
} else {
echo '<form action="?" method="post">
<fieldset>
<legend>
New video
</legend>
<label>Url</label>
<input type="url" name="url" />
<label>Name</label>
<input type="text" name="name"/>
<label>Description</label>
<textarea></textarea>
<label>Via</label>
<input type="url" name="via" />
<label>Licence</label>
<select name="licence">
<option>CC-NC</option>
</select>
<input type="hidden" name="op" value="1"/>
<input type="hidden" name="ac" value="addVideo" />
<input type="hidden" name="bc" value="'.$ci['bc'].'" />
<input type="hidden" name="token" value="', genToken(), '"/>
<input type="submit"/>
</fieldset>
</form>';
}
}
if (!empty($ci['ac']) && $ci['ac'] === 'view') {
if (!empty($ci['op']) && (int)$ci['op'] === 1) {
verifToken($ci['token']);
} else {
$videoInfo = getVideoInfo($ci['file'], $ci['bc'], $fullListFile);
}
}
if (empty($ci['bc'])) {
$path = 'videos/';
} else {
$splitBc = chunk_split($ci['bc'], 8, '/');
$path = 'videos/'.$splitBc;
$breadcrumb = makeBreadcrumb($ci['bc'], $fullListDir);
$nbBread = count($breadcrumb);
}
$listCurrentDir = listCurrentDir($path);
$listCurrentFile = listVideo($path);
echo '<div id="breadcrumb">';
$i = 1;
echo '<a href="?">Home</a> >>';
if (!empty($breadcrumb)) {
foreach ($breadcrumb as $key => $value) {
if ($i === $nbBread) {
echo $value;
} else {
echo '<a href="?bc=', $key, '">', $value, '</a> >> ';
}
$i++;
}
}
echo '</div>';
echo '<div id="listFile">';
if (!empty($listCurrentDir)) {
foreach ($listCurrentDir as $value) {
if (file_exists($value.'.folder')) {
echo '<a href="?bc=', $ci['bc'].$value, '"><img alt="', $path.$value, '" src="', $path.$value, '.folder" />', $fullListDir[$value], '</a> ';
} else {
echo '<a href="?bc=', $ci['bc'].$value, '">', $fullListDir[$value], '</a> ';
}
}
}
if (!empty($listCurrentFile) && $ci['ac'] !== 'view') {
foreach ($listCurrentFile as $value) {
echo '<a href="?bc=', $ci['bc'], '&file=', $value, '&ac=view"><img alt="', $path.$value, '" src="', $path.$value, '" />', $fullListFile[str_replace('.jpg', '', $value)]['title'], '</a>';
}
}
if ($ci['ac'] === 'view' && !empty($listCurrentFile)) {
if (!empty($videoInfo)) {
echo '<div id="viewFile">';
echo '<h2>', $videoInfo['title'], ' (', $videoInfo['originalTitle'], ')</h2>';
echo '<video width="400" controls="controls" poster="', $path.$videoInfo['hash'], '.jpg">';
echo '<source src="', $path.$videoInfo['hash'], '.webm" type="video/webm" />';
echo '
<object type="application/x-shockwave-flash" data="lib/flowplayer-3.2.1.swf" width="640" height="360">
<param name="movie" value="lib/flowplayer-3.2.1.swf" />
<param name="allowFullScreen" value="true" />
<param name="wmode" value="transparent" />
<param name="flashVars" value="config={\'playlist\':[\'', $path.$videoInfo['hash'], '.jpg\',{\'url\':\'', $path.$videoInfo['hash'], '.webm', '\'autoPlay\':false}]}" />
<img alt="Preview" src="', $path.$videoInfo['hash'], '.jpg" width="640" height="360" title="No video playback capabilities, please download the video below" />
</object>';
echo 'Oops your browser can\'t play webm video. Try a modern browser. Look <a href="http://browsehappy.com/">browsehappy.com</a> for list.';
echo '</video>';
}
echo '<table>
<tr>
<th>Title</th>
<td>', $videoInfo['title'], ' (', $videoInfo['originalTitle'], ')</td>
</tr>
<tr>
<th>Description</th>
<td>', $videoInfo['desc'], '</td
</tr>
<tr>
<th>Original description</th>
<td>', $videoInfo['originalDesc'], '</td
</tr>
<tr>
<tr>
<th>Url</th>
<td>', $videoInfo['url'], '</td
</tr>
<tr>
<th>Date</th>
<td>', $videoInfo['uploadDate'], '</td
</tr>
<tr>
<th>Via</th>
<td>', $videoInfo['via'], '</td
</tr>
<tr>
<th>Licence</th>
<td>', $videoInfo['licence'], '</td
</tr>
</table>
</div>';
}
echo '</div>';
?>

14
inc/install.php Normal file
View File

@ -0,0 +1,14 @@
<form action="index.php" method="post">
<fieldset>
<legend>Install</legend>
<label>Login</label>
<input type="text" name="loginName" required/>
<label>Password</label>
<input type="password" name="loginPass" required/>
<label>Password (verification)</label>
<input type="password" name="passwordVerif" required/>
<input type="hidden" name="install" value="install"/>
<input type="hidden" name="token" value="<?php echo genToken(); ?>"/>
<input type="submit" />
</fieldset>
</form>

12
inc/topMenu.php Normal file
View File

@ -0,0 +1,12 @@
<h3><a href="index.php">Home</a></h3>
<ul>
<?php
if ($_SESSION['log'] === true) {
echo '
<li><a href="?ac=newDir&bc=', $ci['bc'], '">New directory</a></li>
<li><a href="?ac=addVideo&bc=', $ci['bc'], '">Add videos</a></li>';
} else {
echo '<li><a href="?ac=login&bc=', $ci['bc'], '">Login</a></li>';
}
?>
</ul>

291
index.php Normal file
View File

@ -0,0 +1,291 @@
<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();
$startTime = microtime(true);
date_default_timezone_set('UTC');
// Data subdirectory
$GLOBALS['config']['DATADIR'] = 'data';
// Video subdirectory
$GLOBALS['config']['VIDEODIR'] = 'videos';
// Video subdirectory before renaming
$GLOBALS['config']['TMPDIR'] = 'tmp';
// Configuration file (user login/password)
$GLOBALS['config']['LISTDIR'] = $GLOBALS['config']['DATADIR'].'/listDir.php';
// Configuration file (user login/password)
$GLOBALS['config']['LISTFILE'] = $GLOBALS['config']['DATADIR'].'/listFile.php';
// Configuration file (user login/password)
$GLOBALS['config']['CONFIG_FILE'] = $GLOBALS['config']['DATADIR'].'/config.php';
// File storage for failures and bans.
$GLOBALS['config']['IPBANS_FILENAME'] = $GLOBALS['config']['DATADIR'].'/ipbans.php';
// Ban IP after this many failures.
$GLOBALS['config']['BAN_AFTER'] = 4;
// Ban duration for IP address after login failures (in seconds) (1800 sec. = 30 minutes)
$GLOBALS['config']['BAN_DURATION'] = 1800;
// Page cache directory.
$GLOBALS['config']['PAGECACHE'] = 'pagecache';
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);
}
$ci = array_map('htmlentities', array_merge($_POST, $_GET));
$_SESSION['bc'] = $ci['bc'];
if (!file_exists($GLOBALS['config']['CONFIG_FILE']) || strlen(file_get_contents($GLOBALS['config']['CONFIG_FILE'])) == 0) {
$page = install($ci);
}
require $GLOBALS['config']['CONFIG_FILE'];
require $GLOBALS['config']['LISTDIR'];
require $GLOBALS['config']['LISTFILE'];
function install($ci) {
if (!is_dir($GLOBALS['config']['DATADIR']) && !mkdir($GLOBALS['config']['DATADIR'], 0755)) {
die('Error on create dir "DATADIR".');
}
if (!is_dir($GLOBALS['config']['VIDEODIR']) && !mkdir($GLOBALS['config']['VIDEODIR'], 0755)) {
die('Error on create dir "VIDEODIR".');
}
if (!is_dir($GLOBALS['config']['PAGECACHE']) && !mkdir($GLOBALS['config']['PAGECACHE'], 0755)) {
die('Error on create dir "PAGECACHE".');
}
if (!is_dir($GLOBALS['config']['TMPDIR']) && !mkdir($GLOBALS['config']['TMPDIR'], 0755)) {
die('Error on create dir "TMPDIR".');
}
if (!copy('inc/.htaccess', $GLOBALS['config']['DATADIR'].'/.htaccess')) {
die('Error on create .httaccess in dir "'.$GLOBALS['config']['DATADIR'].'"');
}
if (!copy('inc/.htaccess', $GLOBALS['config']['TMPDIR'].'/.htaccess')) {
die('Error on create .httaccess in dir "'.$GLOBALS['config']['TMPDIR'].'"');
}
if (!is_file($GLOBALS['config']['CONFIG_FILE'])) {
touch($GLOBALS['config']['CONFIG_FILE']);
}
if (!is_file($GLOBALS['config']['IPBANS_FILENAME'])) {
touch($GLOBALS['config']['IPBANS_FILENAME']);
}
if (!is_file($GLOBALS['config']['LISTDIR'])) {
touch($GLOBALS['config']['LISTDIR']);
}
if (!is_file($GLOBALS['config']['LISTFILE'])) {
touch($GLOBALS['config']['LISTFILE']);
}
$page = 'install';
if ($ci['install'] == 'install') {
verifToken($ci['token']);
$GLOBALS['loginName']['salt'] = hash('sha256', uniqid(rand(), true).'_'.mt_rand());
$GLOBALS['loginName']['loginName'] = hash('sha256', $ci['loginName'].$GLOBALS['loginName']['salt']);
$GLOBALS['loginName']['password'] = hash('sha256', $ci['loginPass'].$GLOBALS['loginName']['salt']);
$data = '<?php
$GLOBALS[\'loginName\'][\'salt\'] = \''.$GLOBALS['loginName']['salt'].'\';
$GLOBALS[\'loginName\'][\'loginName\'] = \''.$GLOBALS['loginName']['loginName'].'\';
$GLOBALS[\'loginName\'][\'password\'] = \''.$GLOBALS['loginName']['password'].'\';
?>
';
file_put_contents($GLOBALS['config']['CONFIG_FILE'], $data);
$page = 'content';
}
return $page;
}
function checkSecure() {
}
function logout() {
}
function genToken() {
$token = sha1(uniqid(rand(), true).'_'.mt_rand());
$_SESSION['token'] = $token;
$_SESSION['tokenTime'] = time();
return $token;
}
function verifToken($token) {
if ($token !== $_SESSION['token'] || $_SESSION['tokenTime'] <= time() - 24000) {
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 $GLOBALS['config']['IPBANS_FILENAME'];
$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($GLOBALS['config']['IPBANS_FILENAME'], "<?php\n\$banList=".var_export($banList, true).";\n?>");
}
function unBan() {
require $GLOBALS['config']['IPBANS_FILENAME'];
$userIp = $_SERVER['REMOTE_ADDR'];
unset($banList[$userIp]);
file_put_contents($GLOBALS['config']['IPBANS_FILENAME'], "<?php\n\$banList=".var_export($banList, true).";\n?>");
}
function n_print($data, $name = '') {
$aBackTrace = debug_backtrace();
echo '<h2>', $name, '</h2>';
echo '
<fieldset style="border: 1px solid orange; padding: 5px;color: #333; background-color: #fff;">';
echo '
<legend style="border:1px solid orange;padding: 1px;background-color:#eee;color:orange;">
', basename($aBackTrace[0]['file']), ' ligne => ', $aBackTrace[0]['line'], '
</legend>';
echo '<pre>', htmlentities(print_r($data, 1)), '</pre>';
echo '
</fieldset>
<br />';
}
function addDir($dir, $breadcrumb, $fullListDir) {
if (empty($dir)) {
return 'error';
}
$dirHash = substr(sha1($dir), 0, 8);
$fullListDir[$dirHash] = $dir;
$path = chunk_split($breadcrumb, 8, '/');
file_put_contents($GLOBALS['config']['LISTDIR'], "<?php\n\$fullListDir=".var_export($fullListDir, true).";\n?>");
if (mkdir('videos/'.$path.'/'.$dirHash)) {
return 'success';
} else {
return 'error';
}
}
function addVideo($ci, $fullListFile) {
$nameHash = substr(sha1($ci['name']), 0, 10);
$url = escapeshellcmd($ci['url']);
$path = chunk_split($ci['bc'], 8, '/');
$data = $nameHash.' '.$url.' '.$path."\n";
file_put_contents($GLOBALS['config']['TMPDIR'].'/'.$nameHash.'.hash', $data);
$fullListFile[$nameHash] = array(
'title' => $ci['name'],
'url' => $ci['url'],
'desc' => $ci['desc'],
'via' => $ci['via'],
'licence' => $ci['licence']
);
file_put_contents($GLOBALS['config']['LISTFILE'], "<?php\n\$fullListFile=".var_export($fullListFile, true).";\n?>");
}
function listCurrentDir($breadcrumb) {
chdir($breadcrumb);
return glob("*", GLOB_ONLYDIR);
}
function makeBreadcrumb($bc, $fullListDir) {
$bcToArray = str_split($bc, 8);
$nbBc = count($bcToArray);
for ($i = 0; $i < $nbBc; $i++) {
$hrefBc .= $bcToArray[$i];
$breadcrumb[$hrefBc] = $fullListDir[$bcToArray[$i]];
}
return $breadcrumb;
}
function renameDir() {
}
function listVideo($breadcrumb) {
return glob("*.jpg");
}
function parseVideoDescription($file, $fullInfo = false) {
$value = json_decode(file_get_contents($file));
}
function getVideoInfo($file, $dir, $fullListFile) {
$file = substr($file, 0, 10);
$postInfo = $fullListFile[$file];
$path = chunk_split($dir, 8, '/');
$descFile = glob("videos/".$path.$file."*.json");
$jsonInfo = json_decode(file_get_contents($descFile[0]));
$info = array(
'hash' => $file,
'title' => $postInfo['title'],
'originalTitle' => htmlentities($jsonInfo -> stitle),
'url' => $postInfo['url'],
'desc' => $postInfo['desc'],
'originalDesc' => htmlentities($jsonInfo -> description),
'uploadDate' => htmlentities($jsonInfo -> upload_date),
'format' => htmlentities($jsonInfo -> format),
'ext' => htmlentities($jsonInfo -> ext),
'duration' => htmlentities(round(($jsonInfo -> duration / 60), 2)),
'via' => $postInfo['via'],
'licence' => $postInfo['licence']
);
return $info;
}
function editVideoDescription() {
}
function testCache() {
}
function makeCache() {
}
function login($ci) {
if (hash('sha256', $ci['loginName'].$GLOBALS['loginName']['salt']) === $GLOBALS['loginName']['loginName'] && $GLOBALS['loginName']['password'] === hash('sha256', $ci['loginPass'].$GLOBALS['loginName']['salt'])) {
$_SESSION['log'] = true;
return true;
} else {
return false;
}
}
if (!empty($ci['ac']) && $ci['ac'] === 'login' && !empty($ci['op']) && (int)$ci['op'] === 2) {
verifToken($ci['token']);
if (login($ci) === true) {
unset($ci['op'], $ci['ac']);
}
}
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Please find a fucking apps name</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
if (!isset($page) && empty($page)) {
require 'inc/content.php';
} else {
require 'inc/'.$page.'.php';
}
echo '<p id="time">Exec time : '.round((microtime(TRUE) - $startTime), 5).'</p>';
?>
</body>
</html>

31
lib/cronTask.sh Normal file
View File

@ -0,0 +1,31 @@
#!/bin/bash/
finalDest=/srv/http/mytube.local/videos/
tempDest=/srv/http/mytube.local/tmp/
threads=3 # nb cpu core - 1
cd $tempDest
nbProcessLauch=$(ps -e | grep -v grep | grep youtube-dl | wc -l)
if [ $nbProcessLauch != 0 ]
then
echo 'bye'
exit
fi
for file in $tempDest*.hash
do
if [[ -f $file ]];
then
while read urlHash url destination
do
youtube-dl -R 0 -q -c --write-thumbnail --write-info-json --recode-video webm --no-check-certificate -f 46/37/45/22/44/35/34/5 -o "$urlHash.%(ext)s" "$url"
sleep 5
if [ ! -f $urlHash.*.part ]
then
rm $file
sleep 1
mv $urlHash* $finalDest$destination
fi
done < $file
fi
done

BIN
lib/flowplayer-3.2.1.swf Normal file

Binary file not shown.

43
style.css Normal file
View File

@ -0,0 +1,43 @@
*{
color:#dfdfdf;
}
body {
background-color: #1E1915;
}
#listFile a {
width: 170px;
height: 170px;
display: block;
border: 2px solid #dfdfdf;
float:left;
margin:0.3em;
padding:0.3em;
text-decoration: none;
text-align:center;
overflow: hidden;
text-overflow: "...";
}
#listFile img {
width : 100%;
vertical-align: middle;
}
#time {
clear :both;
}
#viewFile {
text-align:center;
}
#viewFile table {
width : 60%;
margin : 0 auto;
}
input[type="submit"] {
color : #1E1915;
}