| @@ -1,131 +1,218 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Created by PhpStorm. | |
| 4 | - * User: mhshohel | |
| 5 | - * Date: 24/9/18 | |
| 6 | - * Time: 4:03 PM | |
| 3 | + * Video Stream | |
| 4 | + * | |
| 5 | + * @package Tutor\VideoStream | |
| 6 | + * @author Themeum <support@themeum.com> | |
| 7 | + * @link https://themeum.com | |
| 8 | + * @since 1.0.0 | |
| 7 | 9 | */ |
| 8 | 10 | |
| 9 | 11 | namespace TUTOR; |
| 10 | 12 | |
| 11 | -if ( ! defined( 'ABSPATH' ) ) | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | 14 | exit; |
| 15 | +} | |
| 13 | 16 | |
| 14 | 17 | |
| 15 | 18 | /** |
| 16 | 19 | * Class Video_Stream |
| 17 | - * @package TUTOR | |
| 18 | 20 | * |
| 19 | - * TUTOR Video Stream Class | |
| 20 | - * @since v.1.0.0 | |
| 21 | + * @since 1.0.0 | |
| 21 | 22 | */ |
| 23 | +class Video_Stream { | |
| 22 | 24 | |
| 23 | -class Video_Stream { | |
| 25 | + /** | |
| 26 | + * Path | |
| 27 | + * | |
| 28 | + * @since 1.0.0 | |
| 29 | + * | |
| 30 | + * @var string | |
| 31 | + */ | |
| 32 | + private $path = ''; | |
| 24 | 33 | |
| 25 | - private $path = ""; | |
| 26 | - private $stream = ""; | |
| 34 | + /** | |
| 35 | + * Stream | |
| 36 | + * | |
| 37 | + * @since 1.0.0 | |
| 38 | + * | |
| 39 | + * @var string | |
| 40 | + */ | |
| 41 | + private $stream = ''; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Buffer time | |
| 45 | + * | |
| 46 | + * @since 1.0.0 | |
| 47 | + * | |
| 48 | + * @var string | |
| 49 | + */ | |
| 27 | 50 | private $buffer = 102400; |
| 28 | - private $start = -1; | |
| 29 | - private $end = -1; | |
| 30 | - private $size = 0; | |
| 31 | 51 | |
| 32 | - private $videoFormats; | |
| 52 | + /** | |
| 53 | + * Start | |
| 54 | + * | |
| 55 | + * @since 1.0.0 | |
| 56 | + * | |
| 57 | + * @var int | |
| 58 | + */ | |
| 59 | + private $start = -1; | |
| 33 | 60 | |
| 34 | - function __construct($filePath) { | |
| 35 | - $this->videoFormats = apply_filters('tutor_video_types', array("mp4"=>"video/mp4", "webm"=>"video/webm", "ogg"=>"video/ogg")) ; | |
| 36 | - $this->path = $filePath; | |
| 61 | + /** | |
| 62 | + * End | |
| 63 | + * | |
| 64 | + * @since 1.0.0 | |
| 65 | + * | |
| 66 | + * @var int | |
| 67 | + */ | |
| 68 | + private $end = -1; | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Size | |
| 72 | + * | |
| 73 | + * @since 1.0.0 | |
| 74 | + * | |
| 75 | + * @var int | |
| 76 | + */ | |
| 77 | + private $size = 0; | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Video format | |
| 81 | + * | |
| 82 | + * @since 1.0.0 | |
| 83 | + * | |
| 84 | + * @var string | |
| 85 | + */ | |
| 86 | + private $video_format; | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Resolve dependencies | |
| 90 | + * | |
| 91 | + * @since 1.0.0 | |
| 92 | + * | |
| 93 | + * @param string $file_path file path. | |
| 94 | + */ | |
| 95 | + public function __construct( $file_path ) { | |
| 96 | + $this->video_format = apply_filters( | |
| 97 | + 'tutor_video_types', | |
| 98 | + array( | |
| 99 | + 'mp4' => 'video/mp4', | |
| 100 | + 'webm' => 'video/webm', | |
| 101 | + 'ogg' => 'video/ogg', | |
| 102 | + ) | |
| 103 | + ); | |
| 104 | + $this->path = $file_path; | |
| 37 | 105 | } |
| 38 | 106 | |
| 39 | 107 | /** |
| 40 | 108 | * Open stream |
| 109 | + * | |
| 110 | + * @since 1.0.0 | |
| 111 | + * | |
| 112 | + * @return void | |
| 41 | 113 | */ |
| 42 | 114 | private function open() { |
| 43 | - if (!($this->stream = fopen($this->path, 'rb'))) { | |
| 44 | - die('Could not open stream for reading'); | |
| 115 | + $this->stream = fopen( $this->path, 'rb' ); | |
| 116 | + if ( ! ( $this->stream ) ) { | |
| 117 | + die( 'Could not open stream for reading' ); | |
| 45 | 118 | } |
| 46 | 119 | } |
| 47 | 120 | |
| 48 | 121 | /** |
| 49 | 122 | * Set proper header to serve the video content |
| 123 | + * | |
| 124 | + * @since 1.0.0 | |
| 125 | + * | |
| 126 | + * @return void | |
| 50 | 127 | */ |
| 51 | - private function setHeader() { | |
| 128 | + private function set_header() { | |
| 52 | 129 | ob_get_clean(); |
| 53 | 130 | |
| 54 | - header("Content-Type: {$this->videoFormats[strtolower(pathinfo($this->path, PATHINFO_EXTENSION))]}"); | |
| 55 | - header("Cache-Control: max-age=2592000, public"); | |
| 56 | - header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT'); | |
| 57 | - header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' ); | |
| 131 | + header( 'Content-Type: ' . $this->video_format[ strtolower( pathinfo( $this->path, PATHINFO_EXTENSION ) ) ] ); | |
| 132 | + header( 'Cache-Control: max-age=2592000, public' ); | |
| 133 | + header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', tutor_time() + 2592000 ) . ' GMT' ); | |
| 134 | + header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', @filemtime( $this->path ) ) . ' GMT' ); | |
| 58 | 135 | $this->start = 0; |
| 59 | - $this->size = filesize($this->path); | |
| 136 | + $this->size = filesize( $this->path ); | |
| 60 | 137 | $this->end = $this->size - 1; |
| 61 | - header("Accept-Ranges: 0-".$this->end); | |
| 138 | + header( 'Accept-Ranges: 0-' . $this->end ); | |
| 62 | 139 | |
| 63 | - if (isset($_SERVER['HTTP_RANGE'])) { | |
| 64 | - $c_end = $this->end; | |
| 65 | - list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); | |
| 140 | + if ( isset( $_SERVER['HTTP_RANGE'] ) ) { | |
| 141 | + $c_end = $this->end; | |
| 142 | + list(, $range) = explode( '=', sanitize_text_field( wp_unslash( $_SERVER['HTTP_RANGE'] ) ), 2 ); | |
| 66 | 143 | |
| 67 | - if ($range == '-') { | |
| 68 | - $c_start = $this->size - substr($range, 1); | |
| 69 | - }else{ | |
| 70 | - $range = explode('-', $range); | |
| 144 | + if ( '-' == $range ) { | |
| 145 | + $c_start = $this->size - substr( $range, 1 ); | |
| 146 | + } else { | |
| 147 | + $range = explode( '-', $range ); | |
| 71 | 148 | $c_start = $range[0]; |
| 72 | 149 | |
| 73 | - $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end; | |
| 150 | + $c_end = ( isset( $range[1] ) && is_numeric( $range[1] ) ) ? $range[1] : $c_end; | |
| 74 | 151 | } |
| 75 | - $c_end = ($c_end > $this->end) ? $this->end : $c_end; | |
| 76 | - if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) { | |
| 77 | - header('HTTP/1.1 416 Requested Range Not Satisfiable'); | |
| 78 | - header("Content-Range: bytes $this->start-$this->end/$this->size"); | |
| 152 | + $c_end = ( $c_end > $this->end ) ? $this->end : $c_end; | |
| 153 | + if ( $c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size ) { | |
| 154 | + header( 'HTTP/1.1 416 Requested Range Not Satisfiable' ); | |
| 155 | + header( "Content-Range: bytes $this->start-$this->end/$this->size" ); | |
| 79 | 156 | exit; |
| 80 | 157 | } |
| 81 | 158 | $this->start = $c_start; |
| 82 | - $this->end = $c_end; | |
| 83 | - $length = $this->end - $this->start + 1; | |
| 84 | - header('HTTP/1.1 206 Partial Content'); | |
| 85 | - header("Content-Length: ".$length); | |
| 86 | - header("Content-Range: bytes $this->start-$this->end/".$this->size); | |
| 87 | - header("Accept-Ranges: bytes"); | |
| 159 | + $this->end = $c_end; | |
| 160 | + $length = $this->end - $this->start + 1; | |
| 161 | + header( 'HTTP/1.1 206 Partial Content' ); | |
| 162 | + header( 'Content-Length: ' . $length ); | |
| 163 | + header( "Content-Range: bytes $this->start-$this->end/" . $this->size ); | |
| 164 | + header( 'Accept-Ranges: bytes' ); | |
| 165 | + } else { | |
| 166 | + header( 'Content-Length: ' . $this->size ); | |
| 88 | 167 | } |
| 89 | - else { | |
| 90 | - header("Content-Length: ".$this->size); | |
| 91 | - } | |
| 92 | 168 | |
| 93 | 169 | } |
| 94 | 170 | |
| 95 | 171 | /** |
| 96 | - * close currently opened stream | |
| 172 | + * Close currently opened stream | |
| 173 | + * | |
| 174 | + * @since 1.0.0 | |
| 175 | + * | |
| 176 | + * @return void | |
| 97 | 177 | */ |
| 98 | 178 | private function end() { |
| 99 | - fclose($this->stream); | |
| 179 | + fclose( $this->stream ); | |
| 100 | 180 | exit; |
| 101 | 181 | } |
| 102 | 182 | |
| 103 | 183 | /** |
| 104 | - * perform the streaming of calculated range | |
| 184 | + * Perform the streaming of calculated range | |
| 185 | + * | |
| 186 | + * @since 1.0.0 | |
| 187 | + * | |
| 188 | + * @return void | |
| 105 | 189 | */ |
| 106 | 190 | private function stream() { |
| 107 | 191 | $i = $this->start; |
| 108 | - set_time_limit(0); | |
| 109 | - while(!feof($this->stream) && $i <= $this->end) { | |
| 110 | - $bytesToRead = $this->buffer; | |
| 111 | - if(($i+$bytesToRead) > $this->end) { | |
| 112 | - $bytesToRead = $this->end - $i + 1; | |
| 192 | + set_time_limit( 0 ); | |
| 193 | + while ( ! feof( $this->stream ) && $i <= $this->end ) { | |
| 194 | + $bytes_to_read = $this->buffer; | |
| 195 | + if ( ( $i + $bytes_to_read ) > $this->end ) { | |
| 196 | + $bytes_to_read = $this->end - $i + 1; | |
| 113 | 197 | } |
| 114 | - //$data = fread($this->stream, $bytesToRead); | |
| 115 | - $data = @stream_get_contents($this->stream, $bytesToRead, $i); | |
| 116 | - echo $data; | |
| 198 | + $data = @stream_get_contents( $this->stream, $bytes_to_read, $i ); | |
| 199 | + echo wp_kses_post( $data ); | |
| 117 | 200 | flush(); |
| 118 | - $i += $bytesToRead; | |
| 201 | + $i += $bytes_to_read; | |
| 119 | 202 | } |
| 120 | 203 | } |
| 121 | 204 | |
| 122 | 205 | /** |
| 123 | 206 | * Start streaming tutor video content |
| 207 | + * | |
| 208 | + * @since 1.0.0 | |
| 209 | + * | |
| 210 | + * @return void | |
| 124 | 211 | */ |
| 125 | - function start() { | |
| 212 | + public function start() { | |
| 126 | 213 | $this->open(); |
| 127 | - $this->setHeader(); | |
| 214 | + $this->set_header(); | |
| 128 | 215 | $this->stream(); |
| 129 | 216 | $this->end(); |
| 130 | 217 | } |
| 131 | -} | |
| 218 | +} | |