PSR2-format

This commit is contained in:
brunoccast 2019-06-04 18:56:20 +01:00 committed by Diogo Cordeiro
parent 061e7891e9
commit 00b4a084ad
1 changed files with 277 additions and 253 deletions

View File

@ -52,7 +52,8 @@ function common_init_locale($language=null)
}
putenv('LANGUAGE='.$language);
putenv('LANG='.$language);
$ok = setlocale(LC_ALL, $language . ".utf8",
$ok = setlocale(LC_ALL,
$language . ".utf8",
$language . ".UTF8",
$language . ".utf-8",
$language . ".UTF-8",
@ -198,10 +199,11 @@ function common_language()
$httplang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null;
if (!empty($httplang)) {
$language = client_preferred_language($httplang);
if ($language)
if ($language) {
return $language;
}
}
}
// Finally, if none of the above worked, use the site's default...
return common_config('site', 'language');
@ -214,8 +216,8 @@ function common_munge_password($password, Profile $profile=null)
{
$hashed = null;
if (Event::handle('StartHashPassword', array(&$hashed, $password, $profile))) {
Event::handle('EndHashPassword', array(&$hashed, $password, $profile));
if (Event::handle('StartHashPassword', [&$hashed, $password, $profile])) {
Event::handle('EndHashPassword', [&$hashed, $password, $profile]);
}
if (empty($hashed)) {
throw new PasswordHashException();
@ -236,8 +238,7 @@ function common_check_user($nickname, $password)
$authenticatedUser = false;
if (Event::handle('StartCheckPassword', array($nickname, $password, &$authenticatedUser))) {
if (Event::handle('StartCheckPassword', [$nickname, $password, &$authenticatedUser])) {
if (common_is_email($nickname)) {
$user = User::getKV('email', common_canonical_email($nickname));
} else {
@ -251,7 +252,7 @@ function common_check_user($nickname, $password)
}
}
}
Event::handle('EndCheckPassword', array($nickname, $password, $authenticatedUser));
Event::handle('EndCheckPassword', [$nickname, $password, $authenticatedUser]);
return $authenticatedUser;
}
@ -328,7 +329,7 @@ function common_set_user($user)
}
if ($user) {
if (Event::handle('StartSetUser', array(&$user))) {
if (Event::handle('StartSetUser', [&$user])) {
if (!empty($user)) {
if (!$user->hasRight(Right::WEBLOGIN)) {
// TRANS: Authorisation exception thrown when a user a not allowed to login.
@ -337,7 +338,7 @@ function common_set_user($user)
common_ensure_session();
$_SESSION['userid'] = $user->id;
$_cur = $user;
Event::handle('EndSetUser', array($user));
Event::handle('EndSetUser', [$user]);
return $_cur;
}
}
@ -486,7 +487,6 @@ function common_current_user()
}
if ($_cur === false) {
if (isset($_COOKIE[session_name()]) || isset($_GET[session_name()])
|| (isset($_SESSION['userid']) && $_SESSION['userid'])) {
common_ensure_session();
@ -590,7 +590,7 @@ function common_to_alphanumeric($str)
return $filtered;
}
function common_purify($html, array $args=array())
function common_purify($html, array $args=[])
{
require_once INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php';
@ -604,7 +604,7 @@ function common_purify($html, array $args=array())
* Source: http://microformats.org/wiki/rel
*/
$cfg->set('Attr.AllowedRel', ['bookmark', 'enclosure', 'nofollow', 'tag', 'noreferrer']);
$cfg->set('HTML.ForbiddenAttributes', array('style')); // id, on* etc. are already filtered by default
$cfg->set('HTML.ForbiddenAttributes', ['style']); // id, on* etc. are already filtered by default
$cfg->set('URI.AllowedSchemes', array_fill_keys(common_url_schemes(), true));
if (isset($args['URI.Base'])) {
$cfg->set('URI.Base', $args['URI.Base']); // if null this is like unsetting it I presume
@ -620,7 +620,7 @@ function common_purify($html, array $args=array())
// Remove more elements than what the default filter removes, default in GNU social are remotely
// linked resources such as img, video, audio
$forbiddenElements = array();
$forbiddenElements = [];
foreach (common_config('htmlfilter') as $tag=>$filter) {
if ($filter === true) {
$forbiddenElements[] = $tag;
@ -632,7 +632,7 @@ function common_purify($html, array $args=array())
$purifier = new HTMLPurifier($cfg);
$purified = $purifier->purify($html);
Event::handle('EndCommonPurify', array(&$purified, $html));
Event::handle('EndCommonPurify', [&$purified, $html]);
return $purified;
}
@ -678,17 +678,15 @@ function common_linkify_mentions($text, Profile $author, Notice $parent=null)
// so our positions stay valid despite our fudging with the
// string!
$points = array();
$points = [];
foreach ($mentions as $mention)
{
foreach ($mentions as $mention) {
$points[$mention['position']] = $mention;
}
krsort($points);
foreach ($points as $position => $mention) {
$linkText = common_linkify_mention($mention);
$text = substr_replace($text, $linkText, $position, $mention['length']);
@ -701,12 +699,11 @@ function common_linkify_mention(array $mention)
{
$output = null;
if (Event::handle('StartLinkifyMention', array($mention, &$output))) {
if (Event::handle('StartLinkifyMention', [$mention, &$output])) {
$xs = new XMLStringer(false);
$attrs = array('href' => $mention['url'],
'class' => 'h-card u-url p-nickname '.$mention['type']);
$attrs = ['href' => $mention['url'],
'class' => 'h-card u-url p-nickname '.$mention['type']];
if (!empty($mention['title'])) {
$attrs['title'] = $mention['title'];
@ -716,7 +713,7 @@ function common_linkify_mention(array $mention)
$output = $xs->getString();
Event::handle('EndLinkifyMention', array($mention, &$output));
Event::handle('EndLinkifyMention', [$mention, &$output]);
}
return $output;
@ -725,7 +722,7 @@ function common_linkify_mention(array $mention)
function common_get_attentions($text, Profile $sender, Notice $parent=null)
{
$mentions = common_find_mentions($text, $sender, $parent);
$atts = array();
$atts = [];
foreach ($mentions as $mention) {
foreach ($mention['mentioned'] as $mentioned) {
$atts[$mentioned->getUri()] = $mentioned->getObjectType();
@ -757,11 +754,11 @@ function common_get_attentions($text, Profile $sender, Notice $parent=null)
*/
function common_find_mentions($text, Profile $sender, Notice $parent=null)
{
$mentions = array();
$mentions = [];
if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) {
if (Event::handle('StartFindMentions', [$sender, $text, &$mentions])) {
// Get the context of the original notice, if any
$origMentions = array();
$origMentions = [];
// Does it have a parent notice for context?
if ($parent instanceof Notice) {
foreach ($parent->getAttentionProfiles() as $repliedTo) {
@ -812,16 +809,16 @@ function common_find_mentions($text, Profile $sender, Notice $parent=null)
$url = $mentioned->getUrl();
}
} catch (InvalidUrlException $e) {
$url = common_local_url('userbyid', array('id' => $mentioned->getID()));
$url = common_local_url('userbyid', ['id' => $mentioned->getID()]);
}
$mention = array('mentioned' => array($mentioned),
$mention = ['mentioned' => [$mentioned],
'type' => 'mention',
'text' => $match[0],
'position' => $match[1],
'length' => mb_strlen($match[0]),
'title' => $mentioned->getFullname(),
'url' => $url);
'url' => $url];
$mentions[] = $mention;
}
@ -829,8 +826,12 @@ function common_find_mentions($text, Profile $sender, Notice $parent=null)
// @#tag => mention of all subscriptions tagged 'tag'
preg_match_all('/'.Nickname::BEFORE_MENTIONS.'@#([\pL\pN_\-\.]{1,64})/',
$text, $hmatches, PREG_OFFSET_CAPTURE);
preg_match_all(
'/'.Nickname::BEFORE_MENTIONS.'@#([\pL\pN_\-\.]{1,64})/',
$text,
$hmatches,
PREG_OFFSET_CAPTURE
);
foreach ($hmatches[1] as $hmatch) {
$tag = common_canonical_tag($hmatch[0]);
$plist = Profile_list::getByTaggerAndTag($sender->getID(), $tag);
@ -840,15 +841,15 @@ function common_find_mentions($text, Profile $sender, Notice $parent=null)
$tagged = $sender->getTaggedSubscribers($tag);
$url = common_local_url('showprofiletag',
array('nickname' => $sender->getNickname(),
'tag' => $tag));
['nickname' => $sender->getNickname(),
'tag' => $tag]);
$mentions[] = array('mentioned' => $tagged,
$mentions[] = ['mentioned' => $tagged,
'type' => 'list',
'text' => $hmatch[0],
'position' => $hmatch[1],
'length' => mb_strlen($hmatch[0]),
'url' => $url);
'url' => $url];
}
$hmatches = common_find_mentions_raw($text, '!');
@ -862,16 +863,16 @@ function common_find_mentions($text, Profile $sender, Notice $parent=null)
$profile = $group->getProfile();
$mentions[] = array('mentioned' => array($profile),
$mentions[] = ['mentioned' => [$profile],
'type' => 'group',
'text' => $hmatch[0],
'position' => $hmatch[1],
'length' => mb_strlen($hmatch[0]),
'url' => $group->permalink(),
'title' => $group->getFancyName());
'title' => $group->getFancyName()];
}
Event::handle('EndFindMentions', array($sender, $text, &$mentions));
Event::handle('EndFindMentions', [$sender, $text, &$mentions]);
}
return $mentions;
@ -887,13 +888,13 @@ function common_find_mentions($text, Profile $sender, Notice $parent=null)
*/
function common_find_mentions_raw($text, $preMention='@')
{
$tmatches = array();
$tmatches = [];
preg_match_all('/^T (' . Nickname::DISPLAY_FMT . ') /',
$text,
$tmatches,
PREG_OFFSET_CAPTURE);
$atmatches = array();
$atmatches = [];
// the regexp's "(?!\@)" makes sure it doesn't matches the single "@remote" in "@remote@server.com"
preg_match_all('/'.Nickname::BEFORE_MENTIONS.preg_quote($preMention, '/').'(' . Nickname::DISPLAY_FMT . ')\b(?!\@)/',
$text,
@ -911,8 +912,13 @@ function common_render_text($text)
$text = preg_replace('/[\x{0}-\x{8}\x{b}-\x{c}\x{e}-\x{19}]/', '', $text);
$text = common_replace_urls_callback($text, 'common_linkify');
$text = preg_replace_callback('/(^|\&quot\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/u',
function ($m) { return "{$m[1]}#".common_tag_link($m[2]); }, $text);
$text = preg_replace_callback(
'/(^|\&quot\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/u',
function ($m) {
return "{$m[1]}#".common_tag_link($m[2]);
},
$text
);
// XXX: machine tags
return $text;
}
@ -925,8 +931,7 @@ define('_URL_SCHEME_COLON_COORDINATES', 8);
function common_url_schemes($filter=null)
{
// TODO: move these to $config
$schemes = [
'http' => _URL_SCHEME_COLON_DOUBLE_SLASH,
$schemes = ['http' => _URL_SCHEME_COLON_DOUBLE_SLASH,
'https' => _URL_SCHEME_COLON_DOUBLE_SLASH,
'ftp' => _URL_SCHEME_COLON_DOUBLE_SLASH,
'ftps' => _URL_SCHEME_COLON_DOUBLE_SLASH,
@ -950,11 +955,11 @@ function common_url_schemes($filter=null)
'tel' => _URL_SCHEME_SINGLE_COLON,
'xmpp' => _URL_SCHEME_SINGLE_COLON,
'magnet' => _URL_SCHEME_NO_DOMAIN,
'geo' => _URL_SCHEME_COLON_COORDINATES,
];
'geo' => _URL_SCHEME_COLON_COORDINATES,];
return array_keys(
array_filter($schemes,
array_filter(
$schemes,
function ($scheme) use ($filter) {
return is_null($filter) || ($scheme & $filter);
})
@ -968,7 +973,8 @@ function common_url_schemes($filter=null)
* @param function($text, $arg) $callback: return replacement text
* @param mixed $arg: optional argument will be passed on to the callback
*/
function common_replace_urls_callback($text, $callback, $arg = null) {
function common_replace_urls_callback($text, $callback, $arg = null)
{
$geouri_labeltext_regex = '\pN\pL\-';
$geouri_mark_regex = '\-\_\.\!\~\*\\\'\(\)'; // the \\\' is really pretty
$geouri_unreserved_regex = '\pN\pL' . $geouri_mark_regex;
@ -1049,30 +1055,31 @@ function common_replace_urls_callback($text, $callback, $arg = null) {
*
* @access private
*/
function callback_helper($matches, $callback, $arg=null) {
function callback_helper($matches, $callback, $arg=null)
{
$url=$matches[1];
$left = strpos($matches[0], $url);
$right = $left+strlen($url);
$groupSymbolSets=array(
array(
$groupSymbolSets=[
[
'left'=>'(',
'right'=>')'
),
array(
],
[
'left'=>'[',
'right'=>']'
),
array(
],
[
'left'=>'{',
'right'=>'}'
),
array(
],
[
'left'=>'<',
'right'=>'>'
)
);
$cannotEndWith=array('.','?',',','#');
]
];
$cannotEndWith=['.','?',',','#'];
$original_url=$url;
do {
$original_url=$url;
@ -1092,13 +1099,14 @@ function callback_helper($matches, $callback, $arg=null) {
}
} while ($original_url!=$url);
$result = call_user_func_array($callback, array($url, $arg));
$result = call_user_func_array($callback, [$url, $arg]);
return substr($matches[0], 0, $left) . $result . substr($matches[0], $right);
}
require_once INSTALLDIR . "/lib/curry.php";
function common_linkify($url) {
function common_linkify($url)
{
// It comes in special'd, so we unspecial it before passing to the stringifying
// functions
$url = htmlspecialchars_decode($url);
@ -1119,7 +1127,7 @@ function common_linkify($url) {
}
}
$attrs = array('href' => $longurl, 'title' => $longurl);
$attrs = ['href' => $longurl, 'title' => $longurl];
$is_attachment = false;
$attachment_id = null;
@ -1200,9 +1208,9 @@ function common_shorten_links($text, $always = false, User $user=null)
$maxLength = User_urlshortener_prefs::maxNoticeLength($user);
if ($always || ($maxLength != -1 && mb_strlen($text) > $maxLength)) {
return common_replace_urls_callback($text, array('File_redirection', 'forceShort'), $user);
return common_replace_urls_callback($text, ['File_redirection', 'forceShort'], $user);
} else {
return common_replace_urls_callback($text, array('File_redirection', 'makeShort'), $user);
return common_replace_urls_callback($text, ['File_redirection', 'makeShort'], $user);
}
}
@ -1236,21 +1244,17 @@ function common_validate_utf8($str)
function common_xml_safe_str($str)
{
// Replace common eol and extra whitespace input chars
$unWelcome = array(
"\t", // tab
$unWelcome = ["\t", // tab
"\n", // newline
"\r", // cr
"\0", // null byte eos
"\x0B" // vertical tab
);
"\x0B"]; // vertical tab
$replacement = array(
' ', // single space
$replacement = [' ', // single space
' ',
'', // nothing
'',
' '
);
' '];
$str = str_replace($unWelcome, $replacement, $str);
@ -1268,8 +1272,7 @@ function common_slugify($str)
$str = substr($str, 0, 64);
return $str;
}
$str = transliterator_transliterate(
'Any-Latin;' . // any charset to latin compatible
$str = transliterator_transliterate('Any-Latin;' . // any charset to latin compatible
'NFD;' . // decompose
'[:Nonspacing Mark:] Remove;' . // remove nonspacing marks (accents etc.)
'NFC;' . // composite again
@ -1286,17 +1289,13 @@ function common_tag_link($tag)
if (common_config('singleuser', 'enabled')) {
// regular TagAction isn't set up in 1user mode
$nickname = User::singleUserNickname();
$url = common_local_url('showstream',
array('nickname' => $nickname,
'tag' => $canonical));
$url = common_local_url('showstream', ['nickname' => $nickname, 'tag' => $canonical]);
} else {
$url = common_local_url('tag', array('tag' => $canonical));
$url = common_local_url('tag', ['tag' => $canonical]);
}
$xs = new XMLStringer();
$xs->elementStart('span', 'tag');
$xs->element('a', array('href' => $url,
'rel' => 'tag'),
$tag);
$xs->element('a', ['href' => $url, 'rel' => 'tag'], $tag);
$xs->elementEnd('span');
return $xs->getString();
}
@ -1375,7 +1374,7 @@ function common_relative_profile($sender, $nickname, $dt=null)
function common_local_url($action, $args=null, $params=null, $fragment=null, $addSession=true)
{
if (Event::handle('StartLocalURL', array(&$action, &$params, &$fragment, &$addSession, &$url))) {
if (Event::handle('StartLocalURL', [&$action, &$params, &$fragment, &$addSession, &$url])) {
$r = Router::get();
$path = $r->build($action, $args, $params, $fragment);
@ -1390,7 +1389,7 @@ function common_local_url($action, $args=null, $params=null, $fragment=null, $ad
$url = common_path('index.php/'.$path, $ssl, $addSession);
}
}
Event::handle('EndLocalURL', array(&$action, &$params, &$fragment, &$addSession, &$url));
Event::handle('EndLocalURL', [&$action, &$params, &$fragment, &$addSession, &$url]);
}
return $url;
}
@ -1449,7 +1448,10 @@ function common_fake_local_fancy_url($url)
// [3] site path, or if that is empty just '/' (to retain the /)
'('.preg_quote(common_config('site', 'path') ?: '/', '/').')' .
// [4] + [5] extract index.php (+ possible leading double /) and the rest of the URL separately.
'(\/?index\.php\/)(.*)$/', $url, $matches)) {
'(\/?index\.php\/)(.*)$/',
$url,
$matches
)) {
// if preg_match failed to match
throw new Exception('No known change could be made to the URL.');
}
@ -1481,7 +1483,10 @@ function common_fake_local_nonfancy_url($url)
'('.preg_quote(common_config('site', 'path') ?: '/', '/').')' .
// [4] should be empty (might contain one or more / and then maybe also index.php). Will be overwritten.
// [5] will have the extracted actual URL part (besides site path)
'((?!index.php\/)\/*(?:index.php\/)?)(.*)$/', $url, $matches)) {
'((?!index.php\/)\/*(?:index.php\/)?)(.*)$/',
$url,
$matches
)) {
// if preg_match failed to match
throw new Exception('No known change could be made to the URL.');
}
@ -1643,20 +1648,22 @@ function common_sql_weight($column, $dropoff)
function common_redirect($url, $code=307)
{
static $status = array(301 => "Moved Permanently",
static $status = [301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
307 => "Temporary Redirect");
307 => "Temporary Redirect"];
header('HTTP/1.1 '.$code.' '.$status[$code]);
header("Location: $url");
header("Connection: close");
$xo = new XMLOutputter();
$xo->startXML('a',
$xo->startXML(
'a',
'-//W3C//DTD XHTML 1.0 Strict//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
$xo->element('a', array('href' => $url), $url);
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
);
$xo->element('a', ['href' => $url], $url);
$xo->endXML();
exit;
}
@ -1665,9 +1672,9 @@ function common_redirect($url, $code=307)
function common_enqueue_notice($notice)
{
static $localTransports = array('ping');
static $localTransports = ['ping'];
$transports = array();
$transports = [];
if (common_config('sms', 'enabled')) {
$transports[] = 'sms';
}
@ -1680,16 +1687,14 @@ function common_enqueue_notice($notice)
$transports = array_merge($transports, $localTransports);
}
if (Event::handle('StartEnqueueNotice', array($notice, &$transports))) {
if (Event::handle('StartEnqueueNotice', [$notice, &$transports])) {
$qm = QueueManager::get();
foreach ($transports as $transport)
{
foreach ($transports as $transport) {
$qm->enqueue($notice, $transport);
}
Event::handle('EndEnqueueNotice', array($notice, $transports));
Event::handle('EndEnqueueNotice', [$notice, $transports]);
}
return true;
@ -1697,8 +1702,11 @@ function common_enqueue_notice($notice)
function common_profile_url($nickname)
{
return common_local_url('showstream', array('nickname' => $nickname),
null, null, false);
return common_local_url('showstream',
['nickname' => $nickname],
null,
null,
false);
}
/**
@ -1810,16 +1818,19 @@ function common_ensure_syslog()
{
static $initialized = false;
if (!$initialized) {
openlog(common_config('syslog', 'appname'), 0,
common_config('syslog', 'facility'));
openlog(
common_config('syslog', 'appname'),
0,
common_config('syslog', 'facility')
);
$initialized = true;
}
}
function common_log_line($priority, $msg)
{
static $syslog_priorities = array('LOG_EMERG', 'LOG_ALERT', 'LOG_CRIT', 'LOG_ERR',
'LOG_WARNING', 'LOG_NOTICE', 'LOG_INFO', 'LOG_DEBUG');
static $syslog_priorities = ['LOG_EMERG', 'LOG_ALERT', 'LOG_CRIT', 'LOG_ERR',
'LOG_WARNING', 'LOG_NOTICE', 'LOG_INFO', 'LOG_DEBUG'];
return date('Y-m-d H:i:s') . ' ' . $syslog_priorities[$priority] . ': ' . $msg . PHP_EOL;
}
@ -1845,7 +1856,7 @@ function common_request_id()
function common_log($priority, $msg, $filename=null)
{
if(Event::handle('StartLog', array(&$priority, &$msg, &$filename))){
if (Event::handle('StartLog', [&$priority, &$msg, &$filename])) {
$msg = (empty($filename)) ? $msg : basename($filename) . ' - ' . $msg;
$msg = '[' . common_request_id() . '] ' . $msg;
$logfile = common_config('site', 'logfile');
@ -1860,7 +1871,7 @@ function common_log($priority, $msg, $filename=null)
common_ensure_syslog();
syslog($priority, $msg);
}
Event::handle('EndLog', array($priority, $msg, $filename));
Event::handle('EndLog', [$priority, $msg, $filename]);
}
}
@ -1896,7 +1907,7 @@ function common_log_objstring(&$object)
return "(unknown)";
}
$arr = $object->toArray();
$fields = array();
$fields = [];
foreach ($arr as $k => $v) {
if (is_object($v)) {
$fields[] = "$k='".get_class($v)."'";
@ -1942,7 +1953,9 @@ function common_valid_domain($domain)
{
$octet = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])";
$ipv4 = "(?:$octet(?:\.$octet){3})";
if (preg_match("/^$ipv4$/u", $domain)) return true;
if (preg_match("/^$ipv4$/u", $domain)) {
return true;
}
$group = "(?:[0-9a-f]{1,4})";
$ipv6 = "(?:\[($group(?::$group){0,7})?(::)?($group(?::$group){0,7})?\])"; // http://tools.ietf.org/html/rfc3513#section-2.2
@ -1986,17 +1999,17 @@ function common_accept_to_prefs($accept, $def = '*/*')
{
// No arg means accept anything (per HTTP spec)
if (!$accept) {
return array($def => 1);
return [$def => 1];
}
$prefs = array();
$prefs = [];
$parts = explode(',', $accept);
foreach ($parts as $part) {
// FIXME: doesn't deal with params like 'text/html; level=1'
@list($value, $qpart) = explode(';', trim($part));
$match = array();
$match = [];
if (!isset($qpart)) {
$prefs[$value] = 1;
} elseif (preg_match('/q\s*=\s*(\d*\.\d+)/', $qpart, $match)) {
@ -2085,7 +2098,7 @@ function common_mime_type_match($type, $avail)
function common_negotiate_type($cprefs, $sprefs)
{
$combine = array();
$combine = [];
foreach (array_keys($sprefs) as $type) {
$parts = explode('/', $type);
@ -2128,7 +2141,7 @@ function common_config($main, $sub=null)
global $config;
if (is_null($sub)) {
// Return the config category array
return array_key_exists($main, $config) ? $config[$main] : array();
return array_key_exists($main, $config) ? $config[$main] : [];
}
// Return the config value
return (array_key_exists($main, $config) &&
@ -2139,7 +2152,7 @@ function common_config_set($main, $sub, $value)
{
global $config;
if (!array_key_exists($main, $config)) {
$config[$main] = array();
$config[$main] = [];
}
$config[$main][$sub] = $value;
}
@ -2148,13 +2161,13 @@ function common_config_append($main, $sub, $value)
{
global $config;
if (!array_key_exists($main, $config)) {
$config[$main] = array();
$config[$main] = [];
}
if (!array_key_exists($sub, $config[$main])) {
$config[$main][$sub] = array();
$config[$main][$sub] = [];
}
if (!is_array($config[$main][$sub])) {
$config[$main][$sub] = array($config[$main][$sub]);
$config[$main][$sub] = [$config[$main][$sub]];
}
array_push($config[$main][$sub], $value);
}
@ -2168,7 +2181,7 @@ function common_config_append($main, $sub, $value)
*/
function common_copy_args($from)
{
$to = array();
$to = [];
$strip = get_magic_quotes_gpc();
foreach ($from as $k => $v) {
if (is_array($v)) {
@ -2199,8 +2212,11 @@ function common_remove_magic_from_request()
function common_user_uri(&$user)
{
return common_local_url('userbyid', array('id' => $user->id),
null, null, false);
return common_local_url('userbyid',
['id' => $user->id],
null,
null,
false);
}
// 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits
@ -2229,7 +2245,7 @@ function common_markup_to_html($c, $args=null)
}
if (is_null($args)) {
$args = array();
$args = [];
}
// XXX: not very efficient
@ -2238,10 +2254,18 @@ function common_markup_to_html($c, $args=null)
$c = preg_replace('/%%arg.'.$name.'%%/', $value, $c);
}
$c = preg_replace_callback('/%%user.(\w+)%%/', function ($m) { return common_user_property($m[1]); }, $c);
$c = preg_replace_callback('/%%action.(\w+)%%/', function ($m) { return common_local_url($m[1]); }, $c);
$c = preg_replace_callback('/%%doc.(\w+)%%/', function ($m) { return common_local_url('doc', array('title'=>$m[1])); }, $c);
$c = preg_replace_callback('/%%(\w+).(\w+)%%/', function ($m) { return common_config($m[1], $m[2]); }, $c);
$c = preg_replace_callback('/%%user.(\w+)%%/', function ($m) {
return common_user_property($m[1]);
}, $c);
$c = preg_replace_callback('/%%action.(\w+)%%/', function ($m) {
return common_local_url($m[1]);
}, $c);
$c = preg_replace_callback('/%%doc.(\w+)%%/', function ($m) {
return common_local_url('doc', ['title'=>$m[1]]);
}, $c);
$c = preg_replace_callback('/%%(\w+).(\w+)%%/', function ($m) {
return common_config($m[1], $m[2]);
}, $c);
return \Michelf\Markdown::defaultTransform($c);
}
@ -2282,12 +2306,12 @@ function common_profile_uri($profile)
$uri = null;
if (!empty($profile)) {
if (Event::handle('StartCommonProfileURI', array($profile, &$uri))) {
if (Event::handle('StartCommonProfileURI', [$profile, &$uri])) {
$user = User::getKV('id', $profile->id);
if ($user instanceof User) {
$uri = $user->getUri();
}
Event::handle('EndCommonProfileURI', array($profile, &$uri));
Event::handle('EndCommonProfileURI', [$profile, &$uri]);
}
}
@ -2359,7 +2383,7 @@ function common_license_terms($uri)
if (preg_match('/creativecommons.org\/licenses\/([^\/]+)/', $uri, $matches)) {
return explode('-', $matches[1]);
}
return array($uri);
return [$uri];
}
function common_compatible_license($from, $to)
@ -2419,12 +2443,14 @@ function common_shorten_url($long_url, User $user=null, $force = false)
$shortenerName = User_urlshortener_prefs::urlShorteningService($user);
if (Event::handle('StartShortenUrl',
array($long_url, $shortenerName, &$shortenedUrl))) {
if (Event::handle(
'StartShortenUrl',
[$long_url, $shortenerName, &$shortenedUrl]
)) {
if ($shortenerName == 'internal') {
try {
$f = File::processNew($long_url);
$shortenedUrl = common_local_url('redirecturl', array('id' => $f->id));
$shortenedUrl = common_local_url('redirecturl', ['id' => $f->id]);
if ((mb_strlen($shortenedUrl) < mb_strlen($long_url)) || $force) {
return $shortenedUrl;
} else {
@ -2474,12 +2500,12 @@ function common_client_ip()
}
}
return array($proxy, $ip);
return [$proxy, $ip];
}
function common_url_to_nickname($url)
{
static $bad = array('query', 'user', 'password', 'port', 'fragment');
static $bad = ['query', 'user', 'password', 'port', 'fragment'];
$parts = parse_url($url);
@ -2495,16 +2521,14 @@ function common_url_to_nickname($url)
// If it's just a host...
if (array_key_exists('host', $parts) &&
(!array_key_exists('path', $parts) || strcmp($parts['path'], '/') == 0))
{
(!array_key_exists('path', $parts) || strcmp($parts['path'], '/') == 0)) {
$hostparts = explode('.', $parts['host']);
// Try to catch common idiom of nickname.service.tld
if ((count($hostparts) > 2) &&
(strlen($hostparts[count($hostparts) - 2]) > 3) && # try to skip .co.uk, .com.au
(strcmp($hostparts[0], 'www') != 0))
{
(strcmp($hostparts[0], 'www') != 0)) {
return common_nicknamize($hostparts[0]);
} else {
// Do the whole hostname
@ -2551,7 +2575,7 @@ function common_perf_counter($key, $val=null)
if (array_key_exists($key, $_perfCounters)) {
$_perfCounters[$key][] = $val;
} else {
$_perfCounters[$key] = array($val);
$_perfCounters[$key] = [$val];
}
if (common_config('site', 'logperf_detail')) {
common_log(LOG_DEBUG, "PERF COUNTER HIT: $key $val");