Skip to content

Commit

Permalink
Merge pull request #3754 from splitbrain/sexplode
Browse files Browse the repository at this point in the history
introduce sexplode() as a PHP8 safe explode()
  • Loading branch information
splitbrain committed Oct 19, 2022
2 parents 3c30dcf + ec34bb3 commit 0ba8a0d
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 35 deletions.
2 changes: 1 addition & 1 deletion _test/core/TestRequest.php
Expand Up @@ -197,7 +197,7 @@ protected function setUri($uri) {
}

$params = array();
list($uri, $query) = array_pad(explode('?', $uri, 2), 2, null);
list($uri, $query) = sexplode('?', $uri, 2);
if($query) parse_str($query, $params);

$this->script = substr($uri, 1);
Expand Down
2 changes: 1 addition & 1 deletion inc/Extension/PluginController.php
Expand Up @@ -383,7 +383,7 @@ protected function getListByType($type, $enabled)
protected function splitName($name)
{
if (!isset($this->masterList[$name])) {
return explode('_', $name, 2);
return sexplode('_', $name, 2, '');
}

return array($name, '');
Expand Down
6 changes: 3 additions & 3 deletions inc/Extension/PluginTrait.php
Expand Up @@ -67,7 +67,7 @@ public function getPluginType()
*/
public function getPluginName()
{
list(/* $t */, /* $p */, $n) = explode('_', get_class($this), 4);
list(/* $t */, /* $p */, $n) = sexplode('_', get_class($this), 4, '');
return $n;
}

Expand All @@ -76,8 +76,8 @@ public function getPluginName()
*/
public function getPluginComponent()
{
list(/* $t */, /* $p */, /* $n */, $c) = explode('_', get_class($this), 4);
return (isset($c) ? $c : '');
list(/* $t */, /* $p */, /* $n */, $c) = sexplode('_', get_class($this), 4, '');
return $c;
}

// endregion
Expand Down
2 changes: 1 addition & 1 deletion inc/File/PageResolver.php
Expand Up @@ -20,7 +20,7 @@ public function resolveId($id, $rev = '', $isDateAt = false)

// pages may have a hash attached, we separate it on resolving
if (strpos($id, '#') !== false) {
list($id, $hash) = explode('#', $id, 2);
list($id, $hash) = sexplode('#', $id, 2);
$hash = cleanID($hash);
} else {
$hash = '';
Expand Down
8 changes: 4 additions & 4 deletions inc/HTTP/HTTPClient.php
Expand Up @@ -340,8 +340,8 @@ public function sendRequest($url,$data='',$method='GET'){
$this->resp_headers = $this->parseHeaders($r_headers);
if(isset($this->resp_headers['set-cookie'])){
foreach ((array) $this->resp_headers['set-cookie'] as $cookie){
list($cookie) = explode(';',$cookie,2);
list($key,$val) = explode('=',$cookie,2);
list($cookie) = sexplode(';', $cookie, 2, '');
list($key, $val) = sexplode('=', $cookie, 2, '');
$key = trim($key);
if($val == 'deleted'){
if(isset($this->cookies[$key])){
Expand Down Expand Up @@ -770,9 +770,9 @@ protected function parseHeaders($string){
$lines = explode("\n",$string);
array_shift($lines); //skip first line (status)
foreach($lines as $line){
@list($key, $val) = explode(':',$line,2);
list($key, $val) = sexplode(':', $line, 2, '');
$key = trim($key);
$val = trim($val ?? '');
$val = trim($val);
$key = strtolower($key);
if(!$key) continue;
if(isset($headers[$key])){
Expand Down
2 changes: 1 addition & 1 deletion inc/Parsing/Lexer/Lexer.php
Expand Up @@ -266,7 +266,7 @@ protected function invokeHandler($content, $is_match, $pos)
// modes starting with plugin_ are all handled by the same
// handler but with an additional parameter
if (substr($handler, 0, 7)=='plugin_') {
list($handler,$plugin) = explode('_', $handler, 2);
list($handler,$plugin) = sexplode('_', $handler, 2, '');
return $this->handler->$handler($content, $is_match, $pos, $plugin);
}

Expand Down
2 changes: 1 addition & 1 deletion inc/Remote/Api.php
Expand Up @@ -93,7 +93,7 @@ public function call($method, $args = array())
$args = array();
}
// Ensure we have at least one '.' in $method
list($type, $pluginName, /* $call */) = explode('.', $method . '.', 3);
list($type, $pluginName, /* $call */) = sexplode('.', $method . '.', 3, '');
if ($type === 'plugin') {
return $this->callPlugin($pluginName, $method, $args);
}
Expand Down
2 changes: 1 addition & 1 deletion inc/StyleUtils.php
Expand Up @@ -138,7 +138,7 @@ protected function getValidatedStyles($stylesheets, $file, $mode, $incbase, $web
{
global $conf;
if (!file_exists($incbase . $file)) {
list($extension, $basename) = array_map('strrev', explode('.', strrev($file), 2));
list($extension, $basename) = array_map('strrev', sexplode('.', strrev($file), 2, ''));
$newExtension = $extension === 'css' ? 'less' : 'css';
if (file_exists($incbase . $basename . '.' . $newExtension)) {
$stylesheets[$mode][$incbase . $basename . '.' . $newExtension] = $webbase;
Expand Down
2 changes: 1 addition & 1 deletion inc/auth.php
Expand Up @@ -1281,7 +1281,7 @@ function auth_getCookie() {
if(!isset($_COOKIE[DOKU_COOKIE])) {
return array(null, null, null);
}
list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3);
list($user, $sticky, $pass) = sexplode('|', $_COOKIE[DOKU_COOKIE], 3, '');
$sticky = (bool) $sticky;
$pass = base64_decode($pass);
$user = base64_decode($user);
Expand Down
22 changes: 21 additions & 1 deletion inc/common.php
Expand Up @@ -29,6 +29,26 @@ function hsc($string) {
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8');
}

/**
* A safer explode for fixed length lists
*
* This works just like explode(), but will always return the wanted number of elements.
* If the $input string does not contain enough elements, the missing elements will be
* filled up with the $default value. If the input string contains more elements, the last
* one will NOT be split up and will still contain $separator
*
* @param string $separator The boundary string
* @param string $string The input string
* @param int $limit The number of expected elements
* @param mixed $default The value to use when filling up missing elements
* @see explode
* @return array
*/
function sexplode($separator, $string, $limit, $default = null)
{
return array_pad(explode($separator, $string, $limit), $limit, $default);
}

/**
* Checks if the given input is blank
*
Expand Down Expand Up @@ -1191,7 +1211,7 @@ function rawWikiSlices($range, $id, $rev = '') {
$text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);

// Parse range
list($from, $to) = explode('-', $range, 2);
list($from, $to) = sexplode('-', $range, 2);
// Make range zero-based, use defaults if marker is missing
$from = !$from ? 0 : ($from - 1);
$to = !$to ? strlen($text) : ($to - 1);
Expand Down
2 changes: 1 addition & 1 deletion inc/confutils.php
Expand Up @@ -137,7 +137,7 @@ function getCdnUrls() {
foreach($lines as $line) {
$line = trim(preg_replace('/#.*$/', '', $line));
if($line === '') continue;
list($key, $val) = explode('=', $line, 2);
list($key, $val) = sexplode('=', $line, 2, '');
$key = trim($key);
$val = trim($val);
$versions[$key] = $val;
Expand Down
12 changes: 6 additions & 6 deletions inc/parser/handler.php
Expand Up @@ -674,7 +674,7 @@ public function file($match, $state, $pos) {
*/
public function code($match, $state, $pos, $type='code') {
if ( $state == DOKU_LEXER_UNMATCHED ) {
$matches = explode('>',$match,2);
$matches = sexplode('>',$match,2,'');
// Cut out variable options enclosed in []
preg_match('/\[.*\]/', $matches[0], $options);
if (!empty($options[0])) {
Expand Down Expand Up @@ -833,8 +833,8 @@ public function internallink($match, $state, $pos) {
$link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);

// Split title from URL
$link = explode('|',$link,2);
if ( !isset($link[1]) ) {
$link = sexplode('|',$link,2);
if ( $link[1] === null ) {
$link[1] = null;
} else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
// If the title is an image, convert it to an array containing the image details
Expand All @@ -846,7 +846,7 @@ public function internallink($match, $state, $pos) {

if ( link_isinterwiki($link[0]) ) {
// Interwiki
$interwiki = explode('>',$link[0],2);
$interwiki = sexplode('>',$link[0],2,'');
$this->addCall(
'interwikilink',
array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
Expand Down Expand Up @@ -942,7 +942,7 @@ public function rss($match, $state, $pos) {
$link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);

// get params
list($link,$params) = explode(' ',$link,2);
list($link, $params) = sexplode(' ', $link, 2, '');

$p = array();
if(preg_match('/\b(\d+)\b/',$params,$match)){
Expand Down Expand Up @@ -1072,7 +1072,7 @@ function Doku_Handler_Parse_Media($match) {
$link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);

// Split title from URL
$link = explode('|',$link,2);
$link = sexplode('|', $link, 2);

// Check alignment
$ralign = (bool)preg_match('/^ /',$link[0]);
Expand Down
2 changes: 1 addition & 1 deletion inc/parser/metadata.php
Expand Up @@ -479,7 +479,7 @@ public function internallink($id, $name = null)
// first resolve and clean up the $id
$resolver = new \dokuwiki\File\PageResolver($ID);
$id = $resolver->resolveId($id);
@list($page) = explode('#', $id, 2);
list($page) = sexplode('#', $id, 2);

// set metadata
$this->meta['relation']['references'][$page] = page_exists($page);
Expand Down
4 changes: 2 additions & 2 deletions inc/parser/renderer.php
Expand Up @@ -821,7 +821,7 @@ public function _simpleTitle($name) {
global $conf;

//if there is a hash we use the ancor name only
@list($name, $hash) = explode('#', $name, 2);
list($name, $hash) = sexplode('#', $name, 2);
if($hash) return $hash;

if($conf['useslash']) {
Expand Down Expand Up @@ -893,7 +893,7 @@ public function _resolveInterWiki(&$shortcut, $reference, &$exists = null) {
$urlparam = null;
$id = $url;
if (strpos($url, '?') !== false) {
list($id, $urlparam) = explode('?', $url, 2);
list($id, $urlparam) = sexplode('?', $url, 2, '');
}
$url = wl(cleanID($id), $urlparam);
$exists = page_exists($id);
Expand Down
9 changes: 4 additions & 5 deletions inc/parser/xhtml.php
Expand Up @@ -918,7 +918,7 @@ public function internallink($id, $name = null, $search = null, $returnonly = fa
}

//keep hash anchor
@list($id, $hash) = explode('#', $id, 2);
list($id, $hash) = sexplode('#', $id, 2);
if(!empty($hash)) $hash = $this->_headerToLink($hash);

//prepare for formating
Expand Down Expand Up @@ -1181,7 +1181,7 @@ public function internalmedia($src, $title = null, $align = null, $width = null,
$height = null, $cache = null, $linking = null, $return = false) {
global $ID;
if (strpos($src, '#') !== false) {
list($src, $hash) = explode('#', $src, 2);
list($src, $hash) = sexplode('#', $src, 2);
}
$src = (new MediaResolver($ID))->resolveId($src,$this->date_at,true);
$exists = media_exists($src);
Expand Down Expand Up @@ -1253,16 +1253,15 @@ public function internalmedia($src, $title = null, $align = null, $width = null,
public function externalmedia($src, $title = null, $align = null, $width = null,
$height = null, $cache = null, $linking = null, $return = false) {
if(link_isinterwiki($src)){
list($shortcut, $reference) = explode('>', $src, 2);
list($shortcut, $reference) = sexplode('>', $src, 2, '');
$exists = null;
$src = $this->_resolveInterWiki($shortcut, $reference, $exists);
if($src == '' && empty($title)){
// make sure at least something will be shown in this case
$title = $reference;
}
}
// Squelch the warning in case there is no hash in the URL
@list($src, $hash) = explode('#', $src, 2);
list($src, $hash) = sexplode('#', $src, 2);
$noLink = false;
if($src == '') {
// only output plaintext without link if there is no src
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/authad/auth.php
Expand Up @@ -318,10 +318,10 @@ public function cleanUser($user)
$domain = '';

// get NTLM or Kerberos domain part
list($dom, $user) = array_pad(explode('\\', $user, 2), 2, '');
list($dom, $user) = sexplode('\\', $user, 2, '');
if (!$user) $user = $dom;
if ($dom) $domain = $dom;
list($user, $dom) = array_pad(explode('@', $user, 2), 2, '');
list($user, $dom) = sexplode('@', $user, 2, '');
if ($dom) $domain = $dom;

// clean up both
Expand Down Expand Up @@ -651,7 +651,7 @@ protected function initAdLdap($domain)
*/
public function getUserDomain($user)
{
list(, $domain) = array_pad(explode('@', $user, 2), 2, '');
list(, $domain) = sexplode('@', $user, 2, '');
return $domain;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/extension/helper/extension.php
Expand Up @@ -845,7 +845,7 @@ protected function readManagerData()
$file = @file($managerpath);
if (!empty($file)) {
foreach ($file as $line) {
list($key, $value) = explode('=', trim($line, DOKU_LF), 2);
list($key, $value) = sexplode('=', trim($line, DOKU_LF), 2, '');
$key = trim($key);
$value = trim($value);
// backwards compatible with old plugin manager
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/logviewer/admin.php
Expand Up @@ -110,7 +110,7 @@ protected function displayLog()
$i -= 1; // rewind the counter
} else {
// other lines are actual log lines in three parts
list($dt, $file, $msg) = explode("\t", $line, 3);
list($dt, $file, $msg) = sexplode("\t", $line, 3, '');
echo '<dt>';
echo '<span class="datetime">' . hsc($dt) . '</span>';
echo '<span class="log">';
Expand Down

0 comments on commit 0ba8a0d

Please sign in to comment.