| @@ -1,12 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Video Stream | |
| 4 | - * | |
| 5 | - * @package Tutor\VideoStream | |
| 6 | - * @author Themeum <support@themeum.com> | |
| 7 | - * @link https://themeum.com | |
| 8 | - * @since 1.0.0 | |
| 3 | + * Created by PhpStorm. | |
| 4 | + * User: themeum | |
| 5 | + * Date: 24/9/18 | |
| 6 | + * Time: 4:03 PM | |
| 9 | 7 | */ |
| 10 | 8 | |
| 11 | 9 | namespace TUTOR; |
| 12 | 10 | |
| @@ -17,84 +15,27 @@ | ||
| 17 | 15 | |
| 18 | 16 | /** |
| 19 | 17 | * Class Video_Stream |
| 20 | 18 | * |
| 21 | - * @since 1.0.0 | |
| 19 | + * @package TUTOR | |
| 20 | + * | |
| 21 | + * TUTOR Video Stream Class | |
| 22 | + * @since v.1.0.0 | |
| 22 | 23 | */ |
| 24 | + | |
| 23 | 25 | class Video_Stream { |
| 24 | 26 | |
| 25 | - /** | |
| 26 | - * Path | |
| 27 | - * | |
| 28 | - * @since 1.0.0 | |
| 29 | - * | |
| 30 | - * @var string | |
| 31 | - */ | |
| 32 | - private $path = ''; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Stream | |
| 36 | - * | |
| 37 | - * @since 1.0.0 | |
| 38 | - * | |
| 39 | - * @var string | |
| 40 | - */ | |
| 27 | + private $path = ''; | |
| 41 | 28 | private $stream = ''; |
| 42 | - | |
| 43 | - /** | |
| 44 | - * Buffer time | |
| 45 | - * | |
| 46 | - * @since 1.0.0 | |
| 47 | - * | |
| 48 | - * @var string | |
| 49 | - */ | |
| 50 | 29 | private $buffer = 102400; |
| 30 | + private $start = -1; | |
| 31 | + private $end = -1; | |
| 32 | + private $size = 0; | |
| 51 | 33 | |
| 52 | - /** | |
| 53 | - * Start | |
| 54 | - * | |
| 55 | - * @since 1.0.0 | |
| 56 | - * | |
| 57 | - * @var int | |
| 58 | - */ | |
| 59 | - private $start = -1; | |
| 34 | + private $videoFormats; | |
| 60 | 35 | |
| 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( | |
| 36 | + function __construct( $filePath ) { | |
| 37 | + $this->videoFormats = apply_filters( | |
| 97 | 38 | 'tutor_video_types', |
| 98 | 39 | array( |
| 99 | 40 | 'mp4' => 'video/mp4', |
| 100 | 41 | 'webm' => 'video/webm', |
| @@ -100,49 +41,16 @@ | ||
| 100 | 41 | 'webm' => 'video/webm', |
| 101 | 42 | 'ogg' => 'video/ogg', |
| 102 | 43 | ) |
| 103 | 44 | ); |
| 104 | - $this->path = $file_path; | |
| 45 | + $this->path = $filePath; | |
| 105 | 46 | } |
| 106 | 47 | |
| 107 | 48 | /** |
| 108 | - * Whether the file is a real video inside the uploads directory. | |
| 109 | - * | |
| 110 | - * @since 4.0.2 | |
| 111 | - * | |
| 112 | - * @param string $file_path file path. | |
| 113 | - * | |
| 114 | - * @return bool | |
| 115 | - */ | |
| 116 | - private function is_allowed_path( $file_path ) { | |
| 117 | - $real_file = $file_path ? realpath( $file_path ) : false; | |
| 118 | - if ( ! $real_file || ! is_file( $real_file ) ) { | |
| 119 | - return false; | |
| 120 | - } | |
| 121 | - | |
| 122 | - $uploads = wp_get_upload_dir(); | |
| 123 | - $real_uploads = ! empty( $uploads['basedir'] ) ? realpath( $uploads['basedir'] ) : false; | |
| 124 | - if ( ! $real_uploads ) { | |
| 125 | - return false; | |
| 126 | - } | |
| 127 | - | |
| 128 | - $real_file = wp_normalize_path( $real_file ); | |
| 129 | - $real_uploads = trailingslashit( wp_normalize_path( $real_uploads ) ); | |
| 130 | - $is_allowed = 0 === strpos( $real_file, $real_uploads ); | |
| 131 | - | |
| 132 | - return (bool) apply_filters( 'tutor_video_stream_is_allowed_path', $is_allowed, $real_file ); | |
| 133 | - } | |
| 134 | - | |
| 135 | - /** | |
| 136 | 49 | * Open stream |
| 137 | - * | |
| 138 | - * @since 1.0.0 | |
| 139 | - * | |
| 140 | - * @return void | |
| 141 | 50 | */ |
| 142 | 51 | private function open() { |
| 143 | - $this->stream = fopen( $this->path, 'rb' ); | |
| 144 | - if ( ! ( $this->stream ) ) { | |
| 52 | + if ( ! ( $this->stream = fopen( $this->path, 'rb' ) ) ) { | |
| 145 | 53 | die( 'Could not open stream for reading' ); |
| 146 | 54 | } |
| 147 | 55 | } |
| 148 | 56 | |
| @@ -147,17 +55,13 @@ | ||
| 147 | 55 | } |
| 148 | 56 | |
| 149 | 57 | /** |
| 150 | 58 | * Set proper header to serve the video content |
| 151 | - * | |
| 152 | - * @since 1.0.0 | |
| 153 | - * | |
| 154 | - * @return void | |
| 155 | 59 | */ |
| 156 | - private function set_header() { | |
| 60 | + private function setHeader() { | |
| 157 | 61 | ob_get_clean(); |
| 158 | 62 | |
| 159 | - header( 'Content-Type: ' . $this->video_format[ strtolower( pathinfo( $this->path, PATHINFO_EXTENSION ) ) ] ); | |
| 63 | + header( 'Content-Type: ' . $this->videoFormats[ strtolower( pathinfo( $this->path, PATHINFO_EXTENSION ) ) ] ); | |
| 160 | 64 | header( 'Cache-Control: max-age=2592000, public' ); |
| 161 | 65 | header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', tutor_time() + 2592000 ) . ' GMT' ); |
| 162 | 66 | header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', @filemtime( $this->path ) ) . ' GMT' ); |
| 163 | 67 | $this->start = 0; |
| @@ -166,11 +70,11 @@ | ||
| 166 | 70 | header( 'Accept-Ranges: 0-' . $this->end ); |
| 167 | 71 | |
| 168 | 72 | if ( isset( $_SERVER['HTTP_RANGE'] ) ) { |
| 169 | 73 | $c_end = $this->end; |
| 170 | - list(, $range) = explode( '=', sanitize_text_field( wp_unslash( $_SERVER['HTTP_RANGE'] ) ), 2 ); | |
| 74 | + list(, $range) = explode( '=', $_SERVER['HTTP_RANGE'], 2 ); | |
| 171 | 75 | |
| 172 | - if ( '-' == $range ) { | |
| 76 | + if ( $range == '-' ) { | |
| 173 | 77 | $c_start = $this->size - substr( $range, 1 ); |
| 174 | 78 | } else { |
| 175 | 79 | $range = explode( '-', $range ); |
| 176 | 80 | $c_start = $range[0]; |
| @@ -196,13 +100,9 @@ | ||
| 196 | 100 | |
| 197 | 101 | } |
| 198 | 102 | |
| 199 | 103 | /** |
| 200 | - * Close currently opened stream | |
| 201 | - * | |
| 202 | - * @since 1.0.0 | |
| 203 | - * | |
| 204 | - * @return void | |
| 104 | + * close currently opened stream | |
| 205 | 105 | */ |
| 206 | 106 | private function end() { |
| 207 | 107 | fclose( $this->stream ); |
| 208 | 108 | exit; |
| @@ -208,44 +108,32 @@ | ||
| 208 | 108 | exit; |
| 209 | 109 | } |
| 210 | 110 | |
| 211 | 111 | /** |
| 212 | - * Perform the streaming of calculated range | |
| 213 | - * | |
| 214 | - * @since 1.0.0 | |
| 215 | - * | |
| 216 | - * @return void | |
| 112 | + * perform the streaming of calculated range | |
| 217 | 113 | */ |
| 218 | 114 | private function stream() { |
| 219 | 115 | $i = $this->start; |
| 220 | 116 | set_time_limit( 0 ); |
| 221 | 117 | while ( ! feof( $this->stream ) && $i <= $this->end ) { |
| 222 | - $bytes_to_read = $this->buffer; | |
| 223 | - if ( ( $i + $bytes_to_read ) > $this->end ) { | |
| 224 | - $bytes_to_read = $this->end - $i + 1; | |
| 118 | + $bytesToRead = $this->buffer; | |
| 119 | + if ( ( $i + $bytesToRead ) > $this->end ) { | |
| 120 | + $bytesToRead = $this->end - $i + 1; | |
| 225 | 121 | } |
| 226 | - $data = @stream_get_contents( $this->stream, $bytes_to_read, $i ); | |
| 227 | - echo wp_kses_post( $data ); | |
| 122 | + // $data = fread($this->stream, $bytesToRead); | |
| 123 | + $data = @stream_get_contents( $this->stream, $bytesToRead, $i ); | |
| 124 | + echo $data; | |
| 228 | 125 | flush(); |
| 229 | - $i += $bytes_to_read; | |
| 126 | + $i += $bytesToRead; | |
| 230 | 127 | } |
| 231 | 128 | } |
| 232 | 129 | |
| 233 | 130 | /** |
| 234 | 131 | * Start streaming tutor video content |
| 235 | - * | |
| 236 | - * @since 1.0.0 | |
| 237 | - * | |
| 238 | - * @return void | |
| 239 | 132 | */ |
| 240 | - public function start() { | |
| 241 | - if ( ! $this->is_allowed_path( $this->path ) ) { | |
| 242 | - status_header( 403 ); | |
| 243 | - exit; | |
| 244 | - } | |
| 245 | - | |
| 133 | + function start() { | |
| 246 | 134 | $this->open(); |
| 247 | - $this->set_header(); | |
| 135 | + $this->setHeader(); | |
| 248 | 136 | $this->stream(); |
| 249 | 137 | $this->end(); |
| 250 | 138 | } |
| 251 | 139 | } |