<?php
namespace Submitters;
use Image;
class VideoSubmitter extends Submitter {
private $video;
protected function execute()
{
$this->json = $this->getJSON();
$this->video = $this->prepareData();
$this->submit();
}
private function getJSON() {
$path = PATH_TEMP . '/mediadata.json';
if (file_exists($path)) {
$json = file_get_contents($path);
return json_decode($json);
} else { return NULL; }
}
private function prepareData() {
$json = $this->json;
$width = (int) $this->post('width') ?: (isset($json->width) ? $json->width : 0);
$height = (int) $this->post('height') ?: (isset($json->height) ? $json->height : 0);
if ($this->post('rawgif')) { // GIF form
$headers = $this->web->Request($this->post('external'), ['head' => true]);
if ($headers['content-type'] != 'image/gif') exit;
$vars = (object) [
'from' => 'gifurl',
'width' => (int) $width,
'height' => (int) $height,
'hash' => $this->post('hash'),
'gifsize' => (int) $this->post('size'),
'source' => $this->post('source', '')
];
} else { // Video form
$vars = (object) $this->validate([
'id' => isset($json->id) ? $json->id : '',
'from' => $this->post('from'),
'hash' => $this->post('hash'),
'width' => (int) $width,
'height' => (int) $height,
'source' => $this->post('source'),
'file' => isset($json->file) ? $json->file : '',
'external' => isset($json->external) ? $json->external : '',
'embed_url' => isset($json->embed_url) ? $json->embed_url : null
]);
}
$array = ['youtube','facebook','vk','twitter','cda','vevo','soundcloud','coub','custom',
'dailymotion','liveleak','streamable','vimeo','metacafe','openload','mixcloud'];
switch (true) {
case $this->post('embed'): $this->mode = 'frame'; break;
case in_array($vars->source, $array): $this->mode = 'embed'; break;
default: $this->mode = 'local';
}
return (object) array_filter( (array) $vars );
}
private function validate($vars) {
if ( !preg_match('/[a-z0-9]/', $vars['hash'])
or !preg_match('/[a-z0-9]/', $vars['source'])
or !in_array($vars['from'], ['url','downloaded','uploaded','iframe'])
) { exit; } else { return $vars; }
}
private function parseJSON($json, $gif, $frame) {
if ($gif) { unset($json["file"]); }
if ($frame) {
$json["embed"] = true;
$json["external"] = $this->post('url');
}
unset($json["image"], $json["binary"]);
return json_encode($json);
}
private function submit() {
$output = $this->output;
$this->countTags($output->tags);
$isGIF = $this->post('rawgif', false);
$isFrame = $this->post('embed') && $this->post('from') == 'iframe';
$params = [
'type' => $isFrame ? 'video' : $output->type,
'tagi' => $output->tags, 'tytul' => $output->title,
'czas' => $output->time, 'user' => $output->user,
'opis' => $output->desc, 'category' => $output->category,
'logged' => $output->logged, 'status' => $output->status
];
if ($postID = $this->sql->insert('posts', $params)) {
$this->dailystats($postID, 'post', $params['type'], 'views');
if ($isGIF) {
$this->saveGif();
$this->saveThumbs();
}
$this->addTags($output->tags);
$dir = $this->makeFolder($postID);
$this->moveFiles($dir->save);
if ($output->logged) {
$this->note->addNote('', $output->user, 'post', '', $postID , json_encode(['post_type' => $output->type]));
}
$filtered_json = array_filter((array) $this->json);
$filtered_video = array_filter((array) $this->video);
$json = array_merge($filtered_json, $filtered_video);
$json = $this->parseJSON($json, $isGIF, $isFrame);
// Saving output JSON file
file_put_contents("{$dir->save}/mediadata.json", $json);
chmod_u("{$dir->save}/mediadata.json", 0666);
$this->sql->where('id', $postID)->update('posts', [
'dir' => $dir->date,
'mediadata' => $json,
'mode' => $this->mode,
'hash' => $this->video->hash
]);
$this->clearFolder();
sleep(1); echo 1;
} else { $this->Error('submit_error'); }
}
private function saveGif() {
$path = PATH_TEMP . '/' . $this->post('hash') . '.gif';
$this->web->Request($this->post('external'), null, true)->SaveToFile($path);
}
private function saveThumbs() {
$path = PATH_TEMP . '/' . $this->post('hash');
$img = new Image($path . '.gif');
$img->toFile($path . '.jpg', 'image/jpeg');
$img->resize(200, null)->toFile(PATH_TEMP . '/m_' . $this->post('hash') .'.jpg', 'image/jpeg');
}
}