public function get_movie_types_by_extension_test() { $this->assert_equal("video/x-flv", legal_file::get_movie_types_by_extension("flv")); // regular $this->assert_equal("video/x-flv", legal_file::get_movie_types_by_extension("FLV")); // all caps $this->assert_equal("video/mp4", legal_file::get_movie_types_by_extension("Mp4")); // some caps $this->assert_equal(null, legal_file::get_movie_types_by_extension("php")); // invalid $this->assert_equal(null, legal_file::get_movie_types_by_extension("php.flv")); // invalid w/ . // No extension returns full array $this->assert_equal(5, count(legal_file::get_movie_types_by_extension())); }
/** * Return the width, height, mime_type, extension and duration of the given movie file. * Metadata is first generated using ffmpeg (or set to defaults if it fails), * then can be modified by other modules using movie_get_file_metadata events. * * This function and its use cases are symmetric to those of photo::get_file_metadata. * * @param string $file_path * @return array array($width, $height, $mime_type, $extension, $duration) * * Use cases in detail: * Input is standard movie type (flv/mp4/m4v) * -> return metadata from ffmpeg * Input is *not* standard movie type that is supported by ffmpeg (e.g. avi, mts...) * -> return metadata from ffmpeg * Input is *not* standard movie type that is *not* supported by ffmpeg but is legal * -> return zero width, height, and duration; mime type and extension according to legal_file * Input is illegal, unidentifiable, unreadable, or does not exist * -> throw exception * Note: movie_get_file_metadata events can change any of the above cases (except the last one). */ static function get_file_metadata($file_path) { if (!is_readable($file_path)) { throw new Exception("@todo UNREADABLE_FILE"); } $metadata = new stdClass(); $ffmpeg = movie::find_ffmpeg(); if (!empty($ffmpeg)) { // ffmpeg found - use it to get width, height, and duration. $cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($file_path) . " 2>&1"; $result = `{$cmd}`; if (preg_match("/Stream.*?Video:.*?, (\\d+)x(\\d+)/", $result, $matches_res)) { if (preg_match("/Stream.*?Video:.*? \\[.*?DAR (\\d+):(\\d+).*?\\]/", $result, $matches_dar) && $matches_dar[1] >= 1 && $matches_dar[2] >= 1) { // DAR is defined - determine width based on height and DAR // (should always be int, but adding round to be sure) $matches_res[1] = round($matches_res[2] * $matches_dar[1] / $matches_dar[2]); } list($metadata->width, $metadata->height) = array($matches_res[1], $matches_res[2]); } else { list($metadata->width, $metadata->height) = array(0, 0); } if (preg_match("/Duration: (\\d+:\\d+:\\d+\\.\\d+)/", $result, $matches)) { $metadata->duration = movie::hhmmssdd_to_seconds($matches[1]); } else { if (preg_match("/duration.*?:.*?(\\d+)/", $result, $matches)) { $metadata->duration = $matches[1]; } else { $metadata->duration = 0; } } } else { // ffmpeg not found - set width, height, and duration to zero. $metadata->width = 0; $metadata->height = 0; $metadata->duration = 0; } $extension = pathinfo($file_path, PATHINFO_EXTENSION); if (!$extension || !($metadata->mime_type = legal_file::get_movie_types_by_extension($extension))) { // Extension is empty or illegal. $metadata->extension = null; $metadata->mime_type = null; } else { // Extension is legal (and mime is already set above). $metadata->extension = strtolower($extension); } // Run movie_get_file_metadata events which can modify the class. module::event("movie_get_file_metadata", $file_path, $metadata); // If the post-events results are invalid, throw an exception. Note that, unlike photos, having // zero width and height isn't considered invalid (as is the case when FFmpeg isn't installed). if (!$metadata->mime_type || !$metadata->extension || $metadata->mime_type != legal_file::get_movie_types_by_extension($metadata->extension)) { throw new Exception("@todo ILLEGAL_OR_UNINDENTIFIABLE_FILE"); } return array($metadata->width, $metadata->height, $metadata->mime_type, $metadata->extension, $metadata->duration); }
/** * Return a view for movies. By default, this uses MediaElementPlayer on an HTML5-compliant * <video> object, but movie_img events can override this and provide their own player/view. * If none are found and the player can't play the movie, this returns a simple download link. * @param array $extra_attrs * @return string */ public function movie_img($extra_attrs) { $player_width = module::get_var("gallery", "resize_size", 640); $width = $this->width; $height = $this->height; if ($width == 0 || $height == 0) { // Not set correctly, likely because FFmpeg isn't available. Making the window 0x0 causes the // player to be unviewable during loading. So, let's guess: set width to player_width and // guess a height (using 4:3 aspect ratio). Once the video metadata is loaded, the player // will correct these values. $width = $player_width; $height = ceil($width * 3 / 4); } $div_attrs = array_merge(array("id" => "g-item-id-{$this->id}"), $extra_attrs, array("class" => "g-movie", "style" => "width: {$player_width}px;")); // Run movie_img events, which can either: // - generate a view, which is used in place of the standard MediaElementPlayer // (use view variable) // - change the file sent to the player // (use width, height, url, and filename variables) // - alter the arguments sent to the player // (use video_attrs and player_options variables) $movie_img = new stdClass(); $movie_img->width = $width; $movie_img->height = $height; $movie_img->url = $this->file_url(true); $movie_img->filename = $this->name; $movie_img->div_attrs = $div_attrs; // attrs for the outer .g-movie <div> $movie_img->video_attrs = array(); // add'l <video> attrs $movie_img->player_options = array(); // add'l MediaElementPlayer options (will be json encoded) $movie_img->view = array(); module::event("movie_img", $movie_img, $this); if (count($movie_img->view) > 0) { // View generated - use it $view = implode("\n", $movie_img->view); } else { // View not generated - see if the filetype is supported by MediaElementPlayer. // Note that the extension list below doesn't use the legal_file helper but rather // is hard-coded based on player specifications. $extension = strtolower(pathinfo($movie_img->filename, PATHINFO_EXTENSION)); if (in_array($extension, array("webm", "ogv", "mp4", "flv", "m4v", "mov", "f4v", "wmv"))) { // Filetype supported by MediaElementPlayer - use it (default) $view = new View("movieplayer.html"); $view->width = $movie_img->width; $view->height = $movie_img->height; $view->div_attrs = $movie_img->div_attrs; $view->video_attrs = array_merge(array("controls" => "controls", "autoplay" => "autoplay", "style" => "max-width: 100%"), $movie_img->video_attrs); $view->source_attrs = array("type" => legal_file::get_movie_types_by_extension($extension), "src" => $movie_img->url); $view->player_options = $movie_img->player_options; } else { // Filetype not supported by MediaElementPlayer - display download link $div_attrs["class"] .= " g-movie-download-link"; // add class $div_attrs["download"] = $movie_img->filename; // force download (HTML5 only) $view = html::anchor($movie_img->url, t("Click here to download item."), $div_attrs); } } return $view; }
/** * Sanitize a filename for a given type (given as "photo" or "movie") and a target file format * (given as an extension). This returns a completely legal and valid filename, * or throws an exception if the type or extension given is invalid or illegal. It tries to * maintain the filename's original extension even if it's not identical to the given extension * (e.g. don't change "JPG" or "jpeg" to "jpg"). * * Note: it is not okay if the extension given is legal but does not match the type (e.g. if * extension is "mp4" and type is "photo", it will throw an exception) * * @param string $filename (with no directory) * @param string $extension (can be uppercase or lowercase) * @param string $type (as "photo" or "movie") * @return string sanitized filename (or null if bad extension argument) */ static function sanitize_filename($filename, $extension, $type) { // Check if the type is valid - if so, get the mime types of the // original and target extensions; if not, throw an exception. $original_extension = pathinfo($filename, PATHINFO_EXTENSION); switch ($type) { case "photo": $mime_type = legal_file::get_photo_types_by_extension($extension); $original_mime_type = legal_file::get_photo_types_by_extension($original_extension); break; case "movie": $mime_type = legal_file::get_movie_types_by_extension($extension); $original_mime_type = legal_file::get_movie_types_by_extension($original_extension); break; default: throw new Exception("@todo INVALID_TYPE"); } // Check if the target extension is blank or invalid - if so, throw an exception. if (!$extension || !$mime_type) { throw new Exception("@todo ILLEGAL_EXTENSION"); } // Check if the mime types of the original and target extensions match - if not, fix it. if (!$original_extension || $mime_type != $original_mime_type) { $filename = legal_file::change_extension($filename, $extension); } // It should be a filename without a directory - remove all slashes (and backslashes). $filename = str_replace("/", "_", $filename); $filename = str_replace("\\", "_", $filename); // Remove extra dots from the filename. This will also remove extraneous underscores. $filename = legal_file::smash_extensions($filename); // It's possible that the filename has no base (e.g. ".jpg") - if so, give it a generic one. if (empty($filename) || substr($filename, 0, 1) == ".") { $filename = $type . $filename; // e.g. "photo.jpg" or "movie.mp4" } return $filename; }
/** * Create a default list of allowed movie MIME types and then let modules modify it. * Can be used to add legal alternatives for default MIME types. * (e.g. flv maps to video/x-flv by default, but video/flv is still legal). */ static function get_movie_types() { $types_wrapper = new stdClass(); $types_wrapper->types = array_values(legal_file::get_movie_types_by_extension()); $types_wrapper->types[] = "video/flv"; module::event("legal_movie_types", $types_wrapper); return $types_wrapper->types; }
/** * Return the width, height, mime_type, extension and duration of the given movie file. */ static function get_file_metadata($file_path) { $ffmpeg = movie::find_ffmpeg(); if (empty($ffmpeg)) { throw new Exception("@todo MISSING_FFMPEG"); } $cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($file_path) . " 2>&1"; $result = `{$cmd}`; if (preg_match("/Stream.*?Video:.*?, (\\d+)x(\\d+)/", $result, $matches_res)) { if (preg_match("/Stream.*?Video:.*? \\[.*?DAR (\\d+):(\\d+).*?\\]/", $result, $matches_dar) && $matches_dar[1] >= 1 && $matches_dar[2] >= 1) { // DAR is defined - determine width based on height and DAR // (should always be int, but adding round to be sure) $matches_res[1] = round($matches_res[2] * $matches_dar[1] / $matches_dar[2]); } list($width, $height) = array($matches_res[1], $matches_res[2]); } else { list($width, $height) = array(0, 0); } $extension = strtolower(pathinfo($file_path, PATHINFO_EXTENSION)); $extension = $extension ? $extension : "flv"; // No extension? Assume FLV. $mime_type = legal_file::get_movie_types_by_extension($extension); $mime_type = $mime_type ? $mime_type : "video/x-flv"; // No MIME found? Default to video/x-flv. if (preg_match("/Duration: (\\d+):(\\d+):(\\d+\\.\\d+)/", $result, $matches)) { $duration = 3600 * $matches[1] + 60 * $matches[2] + $matches[3]; } else { if (preg_match("/duration.*?:.*?(\\d+)/", $result, $matches)) { $duration = $matches[1]; } else { $duration = 0; } } return array($width, $height, $mime_type, $extension, $duration); }