Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * **************************************************
- * CHANGE THESE IF RUNNING ON SEPARATE SERVER
- * **************************************************
- * Database connection details.
- * Note: If you are running this script on another server, ensure your database
- * isn't locked down to just localhost. It'll need privileges to allow connections
- * from this host.
- */
- $databaseHost = "";
- $databaseUser = "";
- $databasePass = "";
- $databaseName = "";
- $directFileServers = array();
- /*
- * **************************************************
- * DIRECT FILE SERVER ACCESS DETAILS
- * **************************************************
- * Set these if you have direct file servers. Create a new set of the following values for
- * each file server. The 'file_server_id' must match the id set in your database
- * table 'file_server'. The 'file_storage_path' is the full base path, generally ending with
- * '/files'. Exclude the final forward slash.
- *
- $directFileServers[] = array(
- 'file_server_id' => 2,
- 'ssh_host' => 'fs1.yourhost.com',
- 'ssh_port' => '22',
- 'ssh_username' => 'username',
- 'ssh_password' => 'password',
- 'file_storage_path' => '/path/to/your/files',
- )
- /*
- * **************************************************
- * DONT CHANGE ANYTHING BELOW HERE
- * **************************************************
- */
- // if this is running in the same location as the script
- define('DOC_ROOT', dirname(__FILE__));
- $configPath = DOC_ROOT . '/../../../_config.inc.php';
- if (file_exists($configPath))
- {
- include_once($configPath);
- $databaseHost = _CONFIG_DB_HOST;
- $databaseUser = _CONFIG_DB_USER;
- $databasePass = _CONFIG_DB_PASS;
- $databaseName = _CONFIG_DB_NAME;
- define('ON_SCRIPT_INSTALL', true);
- }
- else
- {
- define('ON_SCRIPT_INSTALL', false);
- }
- define("DATABASE_HOST", $databaseHost);
- define("DATABASE_USER", $databaseUser);
- define("DATABASE_PASS", $databasePass);
- define("DATABASE_NAME", $databaseName);
- // make file server ssh details available in constant
- define("DIRECT_FILE_SERVER_DETAILS", serialize($directFileServers));
- /*
- * Connect database and load config from plugin settings.
- */
- try
- {
- $db = dbConnect();
- if ($db)
- {
- $stmt = $db->query("SELECT * FROM plugin WHERE folder_name='mediaconverter' LIMIT 1");
- $pluginDetails = $stmt->fetch(PDO::FETCH_ASSOC);
- $pluginSettings = $pluginDetails['plugin_settings'];
- if ($pluginSettings)
- {
- $pluginSettingsArr = json_decode($pluginSettings, true);
- }
- }
- }
- catch (Exception $e)
- {
- echo "\n" . $e->getMessage() . "\n";
- exit;
- }
- /*
- * For local storage this script needs to have access via SSH to the files. Set
- * the SSH details below.
- */
- define("LOCAL_STORAGE_SSH_HOST", $pluginSettingsArr['ssh_host']);
- define("LOCAL_STORAGE_SSH_USER", $pluginSettingsArr['ssh_user']);
- define("LOCAL_STORAGE_SSH_PASS", $pluginSettingsArr['ssh_password']);
- define("LOCAL_STORAGE_DEFAULT_PATH", $pluginSettingsArr['local_storage_path']);
- /*
- * General config.
- */
- // change this to set the maximum conversions that can be done at once.
- define("VIDEO_SCREENSHOT_ID", $pluginSettingsArr['video_screenshot_id']);
- // FFMPEG path
- define("FFMPEG_PATH", "ffmpeg");
- // show output, used for debugging
- define("SHOW_OUTPUT", $pluginSettingsArr['output_messages']);
- // local paths, shouldn't need changed
- define("SCRIPT_ROOT_FOLDER", dirname(__FILE__));
- define("CACHE_PATH", SCRIPT_ROOT_FOLDER . '/_cache');
- define("CACHE_SCREENSHOT_PATH", sys_get_temp_dir());
- define("SCREENSHOT_SECONDS", '15'); // in 2 digit format, i.e. 05 = 5 seconds.
- // export file type
- define("EXPORT_FILE_EXTENSION", $pluginSettingsArr['output_type']);
- define("EXPORT_FILE_MIMETYPE", "video/".$pluginSettingsArr['output_type']);
- /*
- * External includes
- */
- set_include_path(get_include_path() . PATH_SEPARATOR . SCRIPT_ROOT_FOLDER . '/phpseclib');
- include_once('Net/SFTP.php'); define("CACHE_SCREENSHOT_PATH", sys_get_temp_dir());
- /*
- * Connect database and load config from plugin settings.
- */
- try
- {
- $db = dbConnect();
- if ($db)
- {
- $stmt = $db->query("SELECT * FROM plugin WHERE folder_name='mediaconverter' LIMIT 1");
- $pluginDetails = $stmt->fetch(PDO::FETCH_ASSOC);
- $pluginSettings = $pluginDetails['plugin_settings'];
- if ($pluginSettings)
- {
- $pluginSettingsArr = json_decode($pluginSettings, true);
- }
- }
- }
- catch (Exception $e)
- {
- echo "\n" . $e->getMessage() . "\n";
- exit;
- }
- try {
- $db = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USER, DATABASE_PASS);
- $stmt = $db->query("SELECT * FROM file WHERE id=" . VIDEO_SCREENSHOT_ID);
- $files = $stmt->fetchAll();
- } catch(Exception $e) {
- echo "Error: " . $e->getMessage();
- }
- foreach($files as $file) {
- $localFile = '/home/teranode/public_html/files/' . $file['localFilePath'];
- // log
- output("Getting " . SCREENSHOT_SECONDS . " second screenshot...\n");
- // thumb path
- $thumbFilename = current(explode(".", MD5($file['id'])));
- $fullThumbPath = CACHE_SCREENSHOT_PATH;
- $fullThumbPath .= '/' . $thumbFilename . '.jpg';
- // ensure it doesn't exist from a previous convert
- if(file_exists($fullThumbPath))
- {
- unlink($fullThumbPath);
- }
- // default time to get the image
- $second = 1;
- // get the duration and a random place within that
- $thumbPathCmd = "ffmpeg -i $localFile 2>&1";
- if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$thumbPathCmd`, $time)) {
- $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
- $second = rand(1, ($total - 1));
- }
- // get the screenshot
- $thumbPathCmd = "ffmpeg -ss $second -i $localFile -deinterlace -an -t 00:00:01 -r 1 -y -q:v 1 -vframes 1 $fullThumbPath";
- if (SHOW_OUTPUT != 1)
- {
- $thumbPathCmd .= ' 2>&1';
- }
- $output = shell_exec($thumbPathCmd);
- // prepare notes
- $notesMessage = "ScreenShot Command:\n";
- $notesMessage .= $thumbPathCmd . "\n\n";
- $notesMessage .= "Result:\n";
- $notesMessage .= $output;
- output($notesMessage);
- output("\n");
- // if screen exists, move it to the core server for storage
- if ((file_exists($fullThumbPath)) && (filesize($fullThumbPath) > 0))
- {
- $remoteScriptRoot = $pluginSettingsArr['script_path_root'];
- $remoteScriptThumbPath = $remoteScriptRoot . '/core/cache/plugins/mediaconverter/'.$file['id'].'/original_thumb.jpg';
- // first try setting the file locally
- $done = false;
- if (ON_SCRIPT_INSTALL == true)
- {
- if (!file_exists($remoteScriptThumbPath))
- {
- @mkdir(dirname($remoteScriptThumbPath), 0777, true);
- }
- $done = rename($fullThumbPath, $remoteScriptThumbPath);
- }
- // try over ssh
- if ($done == false)
- {
- // connect to 'local' storage via SSH
- $sftp = new Net_SFTP(LOCAL_STORAGE_SSH_HOST);
- if (!$sftp->login(LOCAL_STORAGE_SSH_USER, LOCAL_STORAGE_SSH_PASS))
- {
- output("Error: Failed logging into " . LOCAL_STORAGE_SSH_HOST . " via SSH to transfer screenshot.\n");
- }
- else
- {
- // create folder structure
- $sftp->mkdir($remoteScriptRoot . '/core/cache/plugins/');
- $sftp->mkdir($remoteScriptRoot . '/core/cache/plugins/mediaconverter/');
- $sftp->mkdir($remoteScriptRoot . '/core/cache/plugins/mediaconverter/'.$file['id'].'/');
- // set folder to chmod 777
- //$sftp->chmod(0777, $remoteScriptRoot . '/core/cache/plugins/mediaconverter/'.$file['id'].'/');
- // upload screen
- $rs = $sftp->put($remoteScriptThumbPath, $fullThumbPath, NET_SFTP_LOCAL_FILE);
- if (!$rs)
- {
- output("Error: Failed uploading thumb to " . LOCAL_STORAGE_SSH_HOST . " via SSH. Local file: " . $fullThumbPath . ". Remote path: " . $remoteScriptThumbPath . "\n");
- }
- @unlink($fullThumbPath);
- // set file to chmod 777
- //$sftp->chmod(0777, $remoteScriptThumbPath);
- }
- }
- }
- }
- function dbConnect()
- {
- return new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USER, DATABASE_PASS);
- }
- function getFileContent($db, $file)
- {
- // setup local cached file
- $localFilename = MD5(microtime()) . '.' . $file['extension'];
- $localFilePath = CACHE_PATH . '/' . $localFilename;
- // figure out server storage setup
- $storageType = 'local';
- $storageLocation = LOCAL_STORAGE_DEFAULT_PATH;
- $uploadServerDetails = loadServer($db, $file);
- if ($uploadServerDetails != false)
- {
- $storageLocation = $uploadServerDetails['storagePath'];
- $storageType = $uploadServerDetails['serverType'];
- // if no storage path set & local, use system default
- if ((strlen($storageLocation) == 0) && ($storageType == 'local'))
- {
- $storageLocation = LOCAL_STORAGE_DEFAULT_PATH;
- }
- if ($storageType == 'direct')
- {
- $storageLocation = LOCAL_STORAGE_DEFAULT_PATH;
- }
- }
- // use ssh to get contents of 'local' files
- if (($storageType == 'local') || ($storageType == 'direct'))
- {
- // get remote file path
- $remoteFilePath = $storageLocation . $file['localFilePath'];
- // first try getting the file locally
- $done = false;
- if ((ON_SCRIPT_INSTALL == true) && file_exists($remoteFilePath))
- {
- $done = copy($remoteFilePath, $localFilePath);
- if ($done)
- {
- return $localFilePath;
- }
- }
- // try over ssh
- if ($done == false)
- {
- $sshHost = LOCAL_STORAGE_SSH_HOST;
- $sshUser = LOCAL_STORAGE_SSH_USER;
- $sshPass = LOCAL_STORAGE_SSH_PASS;
- // if 'direct' file server, get SSH details
- $serverDetails = getDirectFileServerSSHDetails($file['serverId']);
- if($serverDetails)
- {
- $sshHost = $serverDetails['ssh_host'];
- $sshPort = $serverDetails['ssh_port'];
- $sshUser = $serverDetails['ssh_username'];
- $sshPass = $serverDetails['ssh_password'];
- $basePath = $serverDetails['file_storage_path'];
- if(substr($basePath, strlen($basePath)-1, 1) == '/')
- {
- $basePath = substr($basePath, 0, strlen($basePath)-1);
- }
- $remoteFilePath = $basePath . '/' . $file['localFilePath'];
- }
- if(strlen($sshPort) == 0)
- {
- $sshPort = 22;
- }
- // connect to 'local' storage via SSH
- $sftp = new Net_SFTP($sshHost, $sshPort);
- if (!$sftp->login($sshUser, $sshPass))
- {
- output("Error: Failed logging into " . $sshHost . " (port: ".$sshPort.") via SSH..\n");
- return false;
- }
- // get file
- $rs = $sftp->get($remoteFilePath, $localFilePath);
- if ($rs)
- {
- return $localFilePath;
- }
- }
- return false;
- }
- // ftp
- if ($storageType == 'ftp')
- {
- // setup full path
- $prePath = $uploadServerDetails['storagePath'];
- if (substr($prePath, strlen($prePath) - 1, 1) == '/')
- {
- $prePath = substr($prePath, 0, strlen($prePath) - 1);
- }
- $remoteFilePath = $prePath . '/' . $file['localFilePath'];
- // connect via ftp
- $conn_id = ftp_connect($uploadServerDetails['ipAddress'], $uploadServerDetails['ftpPort'], 30);
- if ($conn_id === false)
- {
- output('Could not connect to ' . $uploadServerDetails['ipAddress'] . ' to upload file.');
- return false;
- }
- // authenticate
- $login_result = ftp_login($conn_id, $uploadServerDetails['ftpUsername'], $uploadServerDetails['ftpPassword']);
- if ($login_result === false)
- {
- output('Could not login to ' . $uploadServerDetails['ipAddress'] . ' with supplied credentials.');
- return false;
- }
- // get content
- $ret = ftp_get($conn_id, $localFilePath, $remoteFilePath, FTP_BINARY);
- while ($ret == FTP_MOREDATA)
- {
- $ret = ftp_nb_continue($conn_id);
- }
- }
- if (file_exists($localFilePath) && (filesize($localFilePath) > 0))
- {
- return $localFilePath;
- }
- return false;
- }
- function loadServer($db, $file)
- {
- // load the server the file is on
- if ((int) $file['serverId'])
- {
- // load from the db
- $db = dbConnect();
- $stmt = $db->query("SELECT * FROM file_server WHERE id = " . (int) $file['serverId']);
- $uploadServerDetails = $stmt->fetch(PDO::FETCH_ASSOC);
- if (!$uploadServerDetails)
- {
- return false;
- }
- return $uploadServerDetails;
- }
- return false;
- }
- function getDirectFileServerSSHDetails($serverId)
- {
- // get direct file server ssh details
- $directFileServers = unserialize(DIRECT_FILE_SERVER_DETAILS);
- if(COUNT($directFileServers) == 0)
- {
- return false;
- }
- foreach($directFileServers AS $directFileServer)
- {
- if($directFileServer['file_server_id'] == $serverId)
- {
- return $directFileServer;
- }
- }
- return false;
- }
- /*
- * Functions
- */
- function output($msg)
- {
- if (SHOW_OUTPUT == 0)
- {
- echo $msg;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement