function _put($options = array()) { $result = ''; $to_path = empty($options['path']) ? '' : $options['path']; $from_path = empty($options['url']) ? '' : $options['url']; if (!($to_path && $from_path)) { throw new InvalidArgumentException('Send array with url and path keys'); } // if the next character is also a slash, then we're talking an absolute path $relative = substr($to_path, 0, 1) != '/'; $mime = empty($options['mime']) ? '' : $options['mime']; if (empty($mime)) { $mime = mime_from_path($from_path); } //if (substr($to_path, 0, 1) == '/') $to_path = substr($to_path, 1); $extract = $mime == mime_from_extension('tgz'); if ($extract) { throw new RuntimeException("CoderArchiveExtension not supported with File::System"); } if (!safe_path($to_path)) { throw new RuntimeException('Could not create path: ' . $to_path); } $file = basename($from_path); if (!@rename($from_path, $to_path)) { throw new RuntimeException("Could not rename from: {$from_path} to: {$to_path}"); } if ($this->getOption('Verbose')) { $this->log("Renamed {$from_path} to {$to_path}"); } @chmod($to_path, 0777); // TODO: fix this terrible kludge that allows ftp user to delete files //@chmod(dirname($to_path), 0777); //@chmod(dirname(dirname($to_path)), 0777); }
function _post($type) { $dir_jobs_queued = $this->_options['DirJobsQueued']; $job_id = empty($this->_options['JobID']) ? '' : $this->_options['JobID']; // make sure needed configuration is there if (!$dir_jobs_queued) { throw new UnexpectedValueException('Configuration option DirJobsQueued required'); } // if no JobID configuration option was set make up an ID if (!$job_id) { $job_id = unique_id($type . 'job'); } $path = end_with_slash($dir_jobs_queued); $path .= $job_id . '.xml'; // make sure we have a directory to write the job xml file to if (!safe_path($path)) { throw new RuntimeException('Could not create path: ' . $path); } // build job xml and write to file $xml_str = $this->_xmlBody($type); if (!@file_put_contents($path, $xml_str)) { throw new RuntimeException('Could not create file: ' . $path); } return $job_id; }
function extract_archive($path, $archive_dir) { $result = FALSE; if (class_exists('Archive_Tar')) { if (safe_path($archive_dir)) { $tar = new Archive_Tar($path); $tar->extract(dirname($archive_dir)); $result = file_exists($archive_dir); } } return $result; }
function http_get_file($abs_url, $file_path) { $result = FALSE; $ch = curl_init($abs_url); if ($ch) { $options = array(); $options[CURLOPT_FOLLOWLOCATION] = 1; $options[CURLOPT_HEADER] = 1; $options[CURLOPT_NOBODY] = 1; $options[CURLOPT_RETURNTRANSFER] = 1; __curl_set_options($ch, $options); $mime = curl_exec($ch); curl_close($ch); if ($mime && strpos($mime, '200 OK') !== FALSE) { if (safe_path($file_path) && (!file_exists($file_path) || !is_dir($file_path))) { $fp = fopen($file_path, "w"); if ($fp) { $ch = curl_init($abs_url); if ($ch) { $options = array(); $options[CURLOPT_FOLLOWLOCATION] = 1; $options[CURLOPT_FILE] = $fp; $options[CURLOPT_FOLLOWLOCATION] = 1; $options[CURLOPT_HEADER] = 0; __curl_set_options($ch, $options); curl_exec($ch); curl_close($ch); fflush($fp); @chmod($file_path, 0777); $result = $mime; } fclose($fp); } } } } return $result; }
function __flashStartProcessing() { $partial_path = substr($this->_buildDir, strlen($this->_options['DirTemporary'])); $mash_id = $partial_path . 'build'; $xml = $this->__flashConfiguration($mash_id); $mash_path = $this->_options['DirCache'] . md5($mash_id) . '.xml'; if (!safe_path($mash_path)) { throw new RuntimeException('Could create path: ' . $mash_path); } if (!@file_put_contents($mash_path, $xml)) { throw new RuntimeException('Could not write file: ' . $mash_path); } $pid = pcntl_fork(); if ($pid == -1) { throw new RuntimeException('pcntl_fork failed'); } if (!$pid) { $local_host = $this->getOption('HostLocal'); // launch flashplayer through xvfb $version = $this->getVersion(); $cmd = ''; $cmd .= escapeshellcmd($this->_options['PathXvfb']) . ' '; //$cmd .= '-l '; // listen to tcp? $cmd .= '-f /root/.Xauthority '; // this MUST be here $cmd .= '-e ' . escapeshellcmd($this->_buildDir) . 'Xerr.txt '; $size = explode('x', $this->_options['DecoderDimensions']); $size[1] += $this->__menuHeight; // menu bars $size[0] += 4; $size[1] += 4; $cmd .= '-s "-screen 0 ' . escapeshellcmd($size[0]) . 'x' . escapeshellcmd($size[1]) . 'x16" '; // -fp /usr/share/X11/fonts/misc/" '; $cmd .= escapeshellcmd($this->_options['PathFlashplayer']) . ' "'; $cmd .= escapeshellcmd($this->_options['DecoderAppletURL']); $cmd .= '?base=' . escapeshellcmd(urlencode($this->_options['CoderBaseURL'])); $cmd .= '&policy=' . urlencode('http://' . escapeshellcmd($local_host) . '/crossdomain.xml'); $cmd .= '&debug=' . urlencode('http://' . escapeshellcmd($local_host) . '/mm.' . $version . '/error/?path=' . $partial_path); $cmd .= '&config=' . urlencode('http://' . escapeshellcmd($local_host) . '/mm.' . $version . '/cache/' . md5($mash_id) . '.xml'); $cmd .= '"'; $this->_progressStep('LaunchPlayer', 20, 'Preparing Mash'); // this will launch flashplayer $result = $this->_shellExecute($cmd, ' 2>&1', 1); if (file_exists($this->_buildDir . 'processing.txt')) { @unlink($this->_buildDir . 'processing.txt'); } if ($this->_options['Verbose'] && file_exists($mash_path)) { @unlink($mash_path); } //if ($this->_options['Verbose']) $this->log(__METHOD__ . ' child exiting'); exit; } }
$archive_dir = $tmp_path . '/' . $filename . '/'; if (!extract_archive($file['tmp_name'], $archive_dir)) { $err = 'Could not extract to ' . $archive_dir; } } // move select files from the archive to media directory if (!$err) { switch ($type) { case 'audio': case 'video': // move any soundtrack $frag = 'media/audio.' . $encoder_audio_extension; $media_path = $media_dir . $frag; $archive_path = $archive_dir . $frag; if (file_exists($archive_path)) { if (!safe_path($media_path)) { $err = 'Could not create directories for ' . $media_path; } elseif (!@rename($archive_path, $media_path)) { $err = 'Could not move audio file from ' . $archive_path . ' to ' . $media_path; } else { // move any soundtrack waveform graphic $frag = 'media/audio.' . $encoder_wave_extension; $archive_path = $archive_dir . $frag; $media_path = $media_dir . $frag; if (file_exists($archive_path)) { if (!@rename($archive_path, $media_path)) { $err = 'Could not move audio file from ' . $archive_path . ' to ' . $media_path; } } } }
function set_file_info($path, $key, $data = '') { $result = FALSE; if ($key && $path) { $a = array(); if (is_array($key)) { $a = $key; } else { $a[$key] = $data; } foreach ($a as $k => $v) { if ($v) { $info_file_path = meta_file_path($k, $path); $result = $info_file_path && safe_path($info_file_path) && @file_put_contents($info_file_path, $v); } if (!$result) { break; } @chmod($info_file_path, 0777); } } return $result; }
function move_files_having_extension($extension, $archive_path, $media_path, $dont_replace = FALSE) { $result = FALSE; // make sure parameters are defined if ($extension && $archive_path && $media_path) { $archive_path = end_with_slash($archive_path); $media_path = end_with_slash($media_path); // make sure archive path exists if (file_exists($archive_path)) { // make sure we have somewhere to move to if (safe_path($media_path)) { if ($handle = opendir($archive_path)) { $result = TRUE; while ($result && FALSE !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (is_file($archive_path . $file)) { $ext = file_extension($archive_path . $file); if ($ext == $extension) { if (!$dont_replace || !file_exists($media_path . $file)) { $result = @rename($archive_path . $file, $media_path . $file); } } } } } closedir($handle); } } } } return $result; }
function __daemonize() { /** * 1) Check is daemon already running * 2) Fork child process * 3) Sets identity * 4) Make current process a session leader * 5) Write process ID to file * 6) Change home path * 7) umask(0) */ $result = FALSE; if (!$this->__isDaemonRunning() && $this->__fork() && $this->__setIdentity() && posix_setsid()) { try { $dir_pid = $this->_options['DirPID']; if (!safe_path($dir_pid)) { $this->log('Could not create ' . $dir_pid); } elseif (!@file_put_contents($dir_pid . 'MovieMasher_' . $this->_options['module'] . '_' . $this->_options[$this->_options['module']] . '.pid', $this->__pid)) { $this->log('Could not write to PID file'); } else { @chdir('/'); umask(0); pcntl_signal(SIGCHLD, array(&$this, '__sigHandler')); pcntl_signal(SIGTERM, array(&$this, '__sigHandler')); $result = TRUE; } } catch (Exception $ex) { $this->log($ex); } } return $result; }
function __buildImage($file_path) { $this->_progressStep('EncodeImage', 5, 'Encoding Image'); $ext = file_extension($file_path); $parent_dir = dir_path($file_path); $tmp_file = $file_path; $orig_dimensions = get_file_info('dimensions', $file_path); if (!$orig_dimensions) { throw new RuntimeException('Could not read image: ' . $file_path); } $cmd = ''; $target_dimensions = scale_proud($orig_dimensions, $this->_options['EncoderDimensions']); if ($target_dimensions) { $cmd .= ' -size ' . $orig_dimensions; $cmd .= ' -resize ' . $target_dimensions; } else { $target_dimensions = $this->_options['EncoderDimensions']; } $frame_path = frame_file_path($file_path, $this->_options['EncoderDimensions'], 1); $file = $frame_path . '0.' . $this->_options['EncoderExtension']; if (!safe_path($file)) { throw new RuntimeException('Could not create path: ' . $file); } if ($cmd || $this->_options['EncoderExtension'] != substr($tmp_file, -strlen($this->_options['EncoderExtension']))) { $cmd = $this->_options['PathCropper'] . $cmd . ' ' . $tmp_file; $cmd .= ' -quality ' . $this->_options['EncoderImageQuality']; $cmd .= ' -type TrueColor'; switch ($this->_options['EncoderExtension']) { case 'ping': case 'png': case 'giff': case 'gif': $cmd .= 'Matte -depth 32'; } $cmd .= ' ' . $file; $shell_result = $this->_shellExecute($cmd); if ($shell_result) { throw new RuntimeException('Could not create image: ' . $shell_result); } } else { copy($tmp_file, $file); } if (!file_exists($file)) { throw new RuntimeException('Failed to create image: ' . $file . ' ' . $cmd); } $this->_progressStep('EncodeImage', 100, 'Encoded Image'); }
function codeFile($job_id) { if ($this->getOption('Verbose')) { $this->log(print_r($this->_options, 1)); } $has_callback = !(empty($this->_options['CoderDoneURL']) && empty($this->_options['CoderErrorURL'])); $ex = FALSE; $err = ''; // grab configuration options and make sure they're not empty $dir_temporary = $this->_options['DirTemporary']; $dir_cache = $this->_options['DirCache']; if (!($dir_temporary && $dir_cache)) { throw new UnexpectedValueException('Configuration options DirTemporary, DirCache required'); } // set unique _tempDir for job $uid = 'Coder_' . unique_id('codertemp'); $this->_tempDir = $dir_temporary . $uid . '/'; // set build directory within it, and make sure we can create it $this->_buildDir = $this->_tempDir . $this->_options['CoderFilename'] . '/'; if (!safe_path($this->_buildDir)) { throw new RuntimeException('Could not create path: ' . $this->_buildDir); } $this->__jobID = $job_id; // set progress path and make sure we can write to it (will create DirCache if it doesn't exist) $progress_path = $dir_cache . $this->__jobID . '/media.xml'; if (!safe_path($progress_path)) { throw new RuntimeException('Could not create path: ' . $progress_path); } $this->_ignore = array(); chdir($dir_temporary); $this->__percentDone = 1; $this->__steps = array('total_steps' => 0); $this->__outputProgress(substr($this->_coderName, 0, -1) . 'ing'); if (!set_file_info($progress_path, 'cached', gmdate("Y-m-d H:i:s"))) { throw new RuntimeException('Could not set cached meta data'); } // for progress calculations $this->_setupSteps(); if (!empty($this->_options['CoderFileURL'])) { $this->_addStep('StoreFile'); } if ($has_callback) { $this->_addStep('CallbackURL'); } try { $coder_error = $this->getOption('CoderError'); if ($coder_error) { throw new RuntimeException($coder_error); } $file_path = $this->_codeFile(); // see if archive desired if (!empty($this->_options['CoderArchiveExtension'])) { if (substr($file_path, -1) == '/') { $file_path = substr($file_path, 0, -1); } //$this->log('Archiving ' . $file_path . ' to ' . $archive_path); $file_path = $this->__createArchive($file_path); } $this->__storeFile($file_path); if (!empty($this->_options['CoderArchiveExtension'])) { @unlink($file_path); } } catch (Exception $ex) { $err .= $ex->getMessage(); } try { if (!$err) { if ($has_callback) { $this->_progressStep('CallbackURL', 1, 'Calling Callback URL'); } } else { $this->__upProgress(-1, $err); } } catch (Exception $ex) { // ignore any possible errors from upping progress } try { $this->__sendDone($err); } catch (Exception $ex) { $err .= $ex->getMessage(); } try { if (!$err) { $this->__percentDone = 100; $this->__outputProgress($this->_coderName . 'd'); } } catch (Exception $ex) { // ignore any possible errors from upping progress } if (file_exists($this->_tempDir) && empty($this->_options['CoderSaveTemporaryFiles'])) { // remove temporary files, even in error cases $this->_shellExecute('rm -R ' . $this->_tempDir); } try { flush_cache_files($dir_cache, $this->_options['CacheSize']); } catch (Exception $ex) { // ignore any possible errors from flushing cache } if ($err) { throw new RuntimeException($err); } }
} } if (!$err) { // build XML string $media_tag = (string) $media_xml->media->asXML(); $xml_str = MOVIEMASHER_XML_DECLARATION; $xml_str .= '<moviemasher>'; $xml_str .= "\n\t" . $media_tag . "\n"; $children = $media_file_xml->children(); $z = sizeof($children); for ($i = 0; $i < $z; $i++) { $xml_str .= "\t" . $children[$i]->asXML() . "\n"; } $xml_str .= '</moviemasher>' . "\n"; // write file if (!safe_path($media_file_xml_path)) { $err = 'Could not create path to ' . $media_file_xml_path; } else { if (!@file_put_contents($media_file_xml_path, $xml_str)) { $err = 'Problem writing ' . $media_file_xml_path; } } } if ($err) { header('HTTP/1.1: 400 Bad Request'); header('Status: 400 Bad Request'); if (!empty($moviemasher_file)) { $moviemasher_file->log($err); } } else { $moviemasher_file->log($media_tag);
$err = 'Could not determine mash label'; } } // make sure clip tags are found if (!$err) { if (!sizeof($mash_xml->mash[0]->clip)) { $err = 'No clip tags found'; } } // save mash xml if (!$err) { $path_media .= authenticated_userid() . '/'; $xml_path = $dir_host . $path_media . $id . '.xml'; // must be writable by the web server process $mash_existed = file_exists($xml_path); if (!safe_path($xml_path)) { $err = 'Could not create path to ' . $xml_path; } else { if (!@file_put_contents($xml_path, $mash_string)) { $err = 'Problem saving mash'; } } } // try reading in media.xml file containing existing media items if (!$err) { if (substr($path_media, 0, strlen($path_site)) == $path_site) { $partial_media_path = substr($path_media, strlen($path_site)); } else { $partial_media_path = '/' . $path_media; } $media_file_xml_path = $dir_host . $path_media . 'media.xml';