diff options
| author | Appleman1234 <applemansigloo@gmail.com> | 2022-09-25 13:30:13 +1000 |
|---|---|---|
| committer | Appleman1234 <applemansigloo@gmail.com> | 2022-09-25 13:30:13 +1000 |
| commit | 2c69095a1edf7474073451285238659d0f62ce3f (patch) | |
| tree | 5dee476605402c66dc2853d0836b29f6aedae62b /inc/lib | |
| parent | e9a61e2fbbd930eaa80d6a5f33420cedc59d748c (diff) | |
PHP8.1 fixes from Rector
Diffstat (limited to 'inc/lib')
| -rw-r--r-- | inc/lib/webm/ffmpeg.php | 36 | ||||
| -rw-r--r-- | inc/lib/webm/matroska.php | 78 | ||||
| -rw-r--r-- | inc/lib/webm/posthandler.php | 8 | ||||
| -rw-r--r-- | inc/lib/webm/videodata.php | 18 |
4 files changed, 64 insertions, 76 deletions
diff --git a/inc/lib/webm/ffmpeg.php b/inc/lib/webm/ffmpeg.php index 0747b420..56339cff 100644 --- a/inc/lib/webm/ffmpeg.php +++ b/inc/lib/webm/ffmpeg.php @@ -5,12 +5,12 @@ */ function get_webm_info($filename) { global $board, $config; - $filename = escapeshellarg($filename); + $filename = escapeshellarg((string) $filename); $ffprobe = $config['webm']['ffprobe_path']; - $ffprobe_out = array(); - $webminfo = array(); + $ffprobe_out = []; + $webminfo = []; exec("$ffprobe -v quiet -print_format json -show_format -show_streams $filename", $ffprobe_out); - $ffprobe_out = json_decode(implode("\n", $ffprobe_out), 1); + $ffprobe_out = json_decode(implode("\n", $ffprobe_out), 1, 512, JSON_THROW_ON_ERROR); $webminfo['error'] = is_valid_webm($ffprobe_out); if(empty($webminfo['error'])) { $webminfo['width'] = $ffprobe_out['streams'][0]['width']; @@ -22,34 +22,34 @@ function get_webm_info($filename) { function is_valid_webm($ffprobe_out) { global $board, $config; if (empty($ffprobe_out)) - return array('code' => 1, 'msg' => $config['error']['genwebmerror']); - $extension = pathinfo($ffprobe_out['format']['filename'], PATHINFO_EXTENSION); + return ['code' => 1, 'msg' => $config['error']['genwebmerror']]; + $extension = pathinfo((string) $ffprobe_out['format']['filename'], PATHINFO_EXTENSION); if ($extension === 'webm') { if ($ffprobe_out['format']['format_name'] != 'matroska,webm') - return array('code' => 2, 'msg' => $config['error']['invalidwebm']); + return ['code' => 2, 'msg' => $config['error']['invalidwebm']]; } elseif ($extension === 'mp4') { if ($ffprobe_out['streams'][0]['codec_name'] != 'h264' && $ffprobe_out['streams'][1]['codec_name'] != 'aac') - return array('code' => 2, 'msg' => $config['error']['invalidwebm']); + return ['code' => 2, 'msg' => $config['error']['invalidwebm']]; } else { - return array('code' => 1, 'msg' => $config['error']['genwebmerror']); + return ['code' => 1, 'msg' => $config['error']['genwebmerror']]; } - if ((count($ffprobe_out['streams']) > 1) && (!$config['webm']['allow_audio'])) - return array('code' => 3, 'msg' => $config['error']['webmhasaudio']); + if (((is_countable($ffprobe_out['streams']) ? count($ffprobe_out['streams']) : 0) > 1) && (!$config['webm']['allow_audio'])) + return ['code' => 3, 'msg' => $config['error']['webmhasaudio']]; if (empty($ffprobe_out['streams'][0]['width']) || (empty($ffprobe_out['streams'][0]['height']))) - return array('code' => 2, 'msg' => $config['error']['invalidwebm']); + return ['code' => 2, 'msg' => $config['error']['invalidwebm']]; if ($ffprobe_out['format']['duration'] > $config['webm']['max_length']) - return array('code' => 4, 'msg' => sprintf($config['error']['webmtoolong'], $config['webm']['max_length'])); + return ['code' => 4, 'msg' => sprintf($config['error']['webmtoolong'], $config['webm']['max_length'])]; } function make_webm_thumbnail($filename, $thumbnail, $width, $height, $duration) { global $board, $config; - $filename = escapeshellarg($filename); - $thumbnailfc = escapeshellarg($thumbnail); // Should be safe by default but you + $filename = escapeshellarg((string) $filename); + $thumbnailfc = escapeshellarg((string) $thumbnail); // Should be safe by default but you // can never be too safe. - $width = escapeshellarg($width); - $height = escapeshellarg($height); // Same as above. + $width = escapeshellarg((string) $width); + $height = escapeshellarg((string) $height); // Same as above. $ffmpeg = $config['webm']['ffmpeg_path']; $ret = 0; - $ffmpeg_out = array(); + $ffmpeg_out = []; exec("$ffmpeg -strict -2 -ss " . floor($duration / 2) . " -i $filename -v quiet -an -vframes 1 -f mjpeg -vf scale=$width:$height $thumbnailfc 2>&1", $ffmpeg_out, $ret); // Work around for https://trac.ffmpeg.org/ticket/4362 if (filesize($thumbnail) === 0) { diff --git a/inc/lib/webm/matroska.php b/inc/lib/webm/matroska.php index 5f1c38ee..baab054f 100644 --- a/inc/lib/webm/matroska.php +++ b/inc/lib/webm/matroska.php @@ -16,12 +16,12 @@ class EBMLElementTypeList { public function __construct($filename) { $lines = file($filename); foreach($lines as $line) { - $fields = explode(' ', trim($line)); + $fields = explode(' ', trim((string) $line)); $t = new EBMLElementType; $id = hexdec($fields[0]); $t->datatype = $fields[1]; $t->name = $fields[2]; - $t->validParents = array(); + $t->validParents = []; for ($i = 0; $i + 3 < count($fields); $i++) { if ($fields[$i+3] == '*' || $fields[$i+3] == 'root') { $t->validParents[$i] = $fields[$i+3]; @@ -44,7 +44,7 @@ class EBMLElementTypeList { } public function id($name) { - $name = strtoupper($name); + $name = strtoupper((string) $name); if (!isset($this->_ids[$name])) return NULL; return $this->_ids[$name]; } @@ -64,13 +64,13 @@ class EBMLElementTypeList { // Matroska element types global $EBML_ELEMENTS; -$EBML_ELEMENTS = new EBMLElementTypeList(dirname(__FILE__) . '/matroska-elements.txt'); +$EBML_ELEMENTS = new EBMLElementTypeList(__DIR__ . '/matroska-elements.txt'); // Decode big-endian integer function ebmlDecodeInt($data, $signed=FALSE, $carryIn=0) { $n = $carryIn; - if (strlen($data) > 8) throw new Exception('not supported: integer too long'); - for ($i = 0; $i < strlen($data); $i++) { + if (strlen((string) $data) > 8) throw new Exception('not supported: integer too long'); + for ($i = 0; $i < strlen((string) $data); $i++) { if ($n > (PHP_INT_MAX >> 8) || $n < ((-PHP_INT_MAX-1) >> 8)) { $n = floatval($n); } @@ -84,16 +84,16 @@ function ebmlDecodeInt($data, $signed=FALSE, $carryIn=0) { // Decode big-endian IEEE float function ebmlDecodeFloat($data) { - switch (strlen($data)) { + switch (strlen((string) $data)) { case 0: return 0; case 4: switch(pack('f', 1e9)) { case '(knN': - $arr = unpack('f', strrev($data)); + $arr = unpack('f', strrev((string) $data)); return $arr[1]; case 'Nnk(': - $arr = unpack('f', $data); + $arr = unpack('f', (string) $data); return $arr[1]; default: error_log('cannot decode floats'); @@ -102,10 +102,10 @@ function ebmlDecodeFloat($data) { case 8: switch(pack('d', 1e9)) { case "\x00\x00\x00\x00\x65\xcd\xcd\x41": - $arr = unpack('d', strrev($data)); + $arr = unpack('d', strrev((string) $data)); return $arr[1]; case "\x41\xcd\xcd\x65\x00\x00\x00\x00": - $arr = unpack('d', $data); + $arr = unpack('d', (string) $data); return $arr[1]; default: error_log('cannot decode floats'); @@ -120,33 +120,27 @@ function ebmlDecodeFloat($data) { // Decode big-endian signed offset from Jan 01, 2000 in nanoseconds // Convert to offset from Jan 01, 1970 in seconds function ebmlDecodeDate($data) { - return ebmlDecodeInt($data, TRUE) * 1e-9 + 946684800; + return ebmlDecodeInt($data, TRUE) * 1e-9 + 946_684_800; } // Decode data of specified datatype function ebmlDecode($data, $datatype) { - switch ($datatype) { - case 'int': return ebmlDecodeInt($data, TRUE); - case 'uint': return ebmlDecodeInt($data, FALSE); - case 'float': return ebmlDecodeFloat($data); - case 'string': return chop($data, "\0"); - case 'date': return ebmlDecodeDate($data); - case 'binary': return $data; - default: throw new Exception('unknown datatype'); - } + return match ($datatype) { + 'int' => ebmlDecodeInt($data, TRUE), + 'uint' => ebmlDecodeInt($data, FALSE), + 'float' => ebmlDecodeFloat($data), + 'string' => chop((string) $data, "\0"), + 'date' => ebmlDecodeDate($data), + 'binary' => $data, + default => throw new Exception('unknown datatype'), + }; } // Methods for reading data from section of EBML file class EBMLReader { - private $_fileHandle; - private $_offset; - private $_size; private $_position; - public function __construct($fileHandle, $offset=0, $size=NULL) { - $this->_fileHandle = $fileHandle; - $this->_offset = $offset; - $this->_size = $size; + public function __construct(private $_fileHandle, private $_offset=0, private $_size=NULL) { $this->_position = 0; } @@ -269,19 +263,13 @@ class EBMLReader { // EBML element class EBMLElement { - private $_id; private $_name; private $_datatype; - private $_content; - private $_headSize; - public function __construct($id, $content, $headSize) { + public function __construct(private $_id, private $_content, private $_headSize) { global $EBML_ELEMENTS; - $this->_id = $id; $this->_name = $EBML_ELEMENTS->name($this->_id); $this->_datatype = $EBML_ELEMENTS->datatype($this->_id); - $this->_content = $content; - $this->_headSize = $headSize; } public function id() {return $this->_id;} @@ -309,11 +297,11 @@ class EBMLElement { class EBMLElementList extends EBMLElement implements Iterator { private $_cache; private $_position; - private static $MAX_ELEMENTS = 10000; + private static int $MAX_ELEMENTS = 10000; public function __construct($id, $content, $headSize) { parent::__construct($id, $content, $headSize); - $this->_cache = array(); + $this->_cache = []; $this->_position = 0; } @@ -389,7 +377,7 @@ class EBMLElementList extends EBMLElement implements Iterator { foreach ($this as $element) { $iElement++; if ($iElement > self::$MAX_ELEMENTS) throw new Exception('not supported: too many elements'); - if (strtoupper($element->name()) == strtoupper($name)) { + if (strtoupper((string) $element->name()) == strtoupper((string) $name)) { return $element->value(); } } @@ -399,10 +387,10 @@ class EBMLElementList extends EBMLElement implements Iterator { // Parse block class MatroskaBlock { - const LACING_NONE = 0; - const LACING_XIPH = 1; - const LACING_EBML = 3; - const LACING_FIXED = 2; + final const LACING_NONE = 0; + final const LACING_XIPH = 1; + final const LACING_EBML = 3; + final const LACING_FIXED = 2; public $trackNumber; public $timecode; public $keyframe; @@ -430,7 +418,7 @@ class MatroskaBlock { } else { $nsizes = ord($reader->read(1)); } - $sizes = array(); + $sizes = []; switch ($this->lacing) { case self::LACING_XIPH: for ($i = 0; $i < $nsizes; $i++) { @@ -468,7 +456,7 @@ class MatroskaBlock { } # Frames - $this->frames = array(); + $this->frames = []; for ($i = 0; $i < $nsizes; $i++) { $this->frames[$i] = $reader->nextSlice($sizes[$i]); } @@ -520,5 +508,5 @@ function ebmlEncodeElementName($name) { } function ebmlEncodeElement($name, $content) { - return ebmlEncodeElementName($name) . ebmlEncodeVarInt(strlen($content)) . $content; + return ebmlEncodeElementName($name) . ebmlEncodeVarInt(strlen((string) $content)) . $content; } diff --git a/inc/lib/webm/posthandler.php b/inc/lib/webm/posthandler.php index 15c6600d..4cd26d6a 100644 --- a/inc/lib/webm/posthandler.php +++ b/inc/lib/webm/posthandler.php @@ -5,7 +5,7 @@ function postHandler($post) { global $board, $config; if ($post->has_file) foreach ($post->files as &$file) if ($file->extension == 'webm' || $file->extension == 'mp4') { if ($config['webm']['use_ffmpeg']) { - require_once dirname(__FILE__) . '/ffmpeg.php'; + require_once __DIR__ . '/ffmpeg.php'; $webminfo = get_webm_info($file->file_path); if (empty($webminfo['error'])) { $file->width = $webminfo['width']; @@ -29,7 +29,7 @@ function postHandler($post) { } } else { - require_once dirname(__FILE__) . '/videodata.php'; + require_once __DIR__ . '/videodata.php'; $videoDetails = videoData($file->file_path); if (!isset($videoDetails['container']) || $videoDetails['container'] != 'webm') return "not a WebM file"; // Set thumbnail @@ -39,7 +39,7 @@ function postHandler($post) { $file = webm_set_spoiler($file); } elseif (isset($videoDetails['frame']) && $thumbFile = fopen($thumbName, 'wb')) { // Use single frame from video as pseudo-thumbnail - fwrite($thumbFile, $videoDetails['frame']); + fwrite($thumbFile, (string) $videoDetails['frame']); fclose($thumbFile); $file->thumb = $file->file_id . '.webm'; } else { @@ -60,7 +60,7 @@ function postHandler($post) { } function set_thumbnail_dimensions($post,$file) { global $board, $config; - $tn_dimensions = array(); + $tn_dimensions = []; $tn_maxw = $post->op ? $config['thumb_op_width'] : $config['thumb_width']; $tn_maxh = $post->op ? $config['thumb_op_height'] : $config['thumb_height']; if ($file->width > $tn_maxw || $file->height > $tn_maxh) { diff --git a/inc/lib/webm/videodata.php b/inc/lib/webm/videodata.php index d54d484b..38fd4d00 100644 --- a/inc/lib/webm/videodata.php +++ b/inc/lib/webm/videodata.php @@ -1,6 +1,6 @@ <?php /* This file is dedicated to the public domain; you may do as you wish with it. */ -require dirname(__FILE__) . '/matroska.php'; +require __DIR__ . '/matroska.php'; function matroskaSeekElement($name, $pos) { return ebmlEncodeElement('Seek', @@ -46,11 +46,11 @@ function muxWebMFrame($videoTrack, $frame) { ebmlEncodeElement('CueTime', "\x00") . ebmlEncodeElement('CueTrackPositions', ebmlEncodeElement('CueTrack', pack('N', $videoTrack->get('TrackNumber'))) - . ebmlEncodeElement('CueClusterPosition', pack('N', $lenSeekHead + strlen($info) + strlen($tracks) + $lenCues)) + . ebmlEncodeElement('CueClusterPosition', pack('N', $lenSeekHead + strlen((string) $info) + strlen((string) $tracks) + $lenCues)) ) ) ); - if (strlen($cues) != $lenCues) throw new Exception('length of Cues element wrong'); + if (strlen((string) $cues) != $lenCues) throw new Exception('length of Cues element wrong'); $cluster = ebmlEncodeElement('Cluster', ebmlEncodeElement('Timecode', "\x00") . ebmlEncodeElement($frame->name(), $frame->content()->readAll()) @@ -58,11 +58,11 @@ function muxWebMFrame($videoTrack, $frame) { ); $seekHead = ebmlEncodeElement('SeekHead', matroskaSeekElement('Info', $lenSeekHead) - . matroskaSeekElement('Tracks', $lenSeekHead + strlen($info)) - . matroskaSeekElement('Cues', $lenSeekHead + strlen($info) + strlen($tracks)) - . matroskaSeekElement('Cluster', $lenSeekHead + strlen($info) + strlen($tracks) + $lenCues) + . matroskaSeekElement('Tracks', $lenSeekHead + strlen((string) $info)) + . matroskaSeekElement('Cues', $lenSeekHead + strlen((string) $info) + strlen((string) $tracks)) + . matroskaSeekElement('Cluster', $lenSeekHead + strlen((string) $info) + strlen((string) $tracks) + $lenCues) ); - if (strlen($seekHead) != $lenSeekHead) throw new Exception('length of SeekHead element wrong'); + if (strlen((string) $seekHead) != $lenSeekHead) throw new Exception('length of SeekHead element wrong'); $segment = ebmlEncodeElement('Segment', $seekHead . $info . $tracks . $cues . $cluster); return $ebml . $segment; @@ -93,11 +93,11 @@ function firstWebMFrame($segment, $trackNumber, $skip=0) { } } } - return isset($frame1) ? $frame1 : NULL; + return $frame1 ?? NULL; } function videoData($filename) { - $data = array(); + $data = []; // Open file $fileHandle = fopen($filename, 'rb'); |
