*/
function common_init_locale($language=null)
{
- if(!$language) {
+ if (!$language) {
$language = common_language();
}
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",
{
setlocale(LC_CTYPE, 'C');
// So we do not have to make people install the gettext locales
- $path = common_config('site','locale_path');
+ $path = common_config('site', 'locale_path');
bindtextdomain("statusnet", $path);
bind_textdomain_codeset("statusnet", "UTF-8");
textdomain("statusnet");
setlocale(LC_CTYPE, 'C');
// So we do not have to make people install the gettext locales
- $path = common_config('site','locale_path');
+ $path = common_config('site', 'locale_path');
bindtextdomain("statusnet", $path);
bind_textdomain_codeset("statusnet", "UTF-8");
textdomain("statusnet");
$httplang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null;
if (!empty($httplang)) {
$language = client_preferred_language($httplang);
- if ($language)
- return $language;
+ if ($language) {
+ return $language;
+ }
}
}
{
$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();
$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 {
}
}
}
- Event::handle('EndCheckPassword', array($nickname, $password, $authenticatedUser));
+ Event::handle('EndCheckPassword', [$nickname, $password, $authenticatedUser]);
return $authenticatedUser;
}
}
if (array_key_exists(session_name(), $_GET)) {
$id = $_GET[session_name()];
- } else if (array_key_exists(session_name(), $_COOKIE)) {
+ } elseif (array_key_exists(session_name(), $_COOKIE)) {
$id = $_COOKIE[session_name()];
}
if (isset($id)) {
$_cur = null;
unset($_SESSION['userid']);
return true;
- } else if (is_string($user)) {
+ } elseif (is_string($user)) {
$nickname = $user;
$user = User::getKV('nickname', $nickname);
- } else if (!$user instanceof User) {
+ } elseif (!$user instanceof User) {
return false;
}
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.
common_ensure_session();
$_SESSION['userid'] = $user->id;
$_cur = $user;
- Event::handle('EndSetUser', array($user));
+ Event::handle('EndSetUser', [$user]);
return $_cur;
}
}
}
if ($_cur === false) {
-
if (isset($_COOKIE[session_name()]) || isset($_GET[session_name()])
|| (isset($_SESSION['userid']) && $_SESSION['userid'])) {
common_ensure_session();
if ($id) {
$user = User::getKV('id', $id);
if ($user instanceof User) {
- $_cur = $user;
- return $_cur;
+ $_cur = $user;
+ return $_cur;
}
}
}
return $filtered;
}
-function common_purify($html, array $args=array())
+function common_purify($html, array $args=[])
{
require_once INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php';
* 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
// 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;
$purifier = new HTMLPurifier($cfg);
$purified = $purifier->purify($html);
- Event::handle('EndCommonPurify', array(&$purified, $html));
+ Event::handle('EndCommonPurify', [&$purified, $html]);
return $purified;
}
// 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']);
{
$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'];
$output = $xs->getString();
- Event::handle('EndLinkifyMention', array($mention, &$output));
+ Event::handle('EndLinkifyMention', [$mention, &$output]);
}
return $output;
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();
*/
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) {
continue;
}
- // primarily mention the profiles mentioned in the parent
+ // primarily mention the profiles mentioned in the parent
$mention_found_in_origMentions = false;
- foreach($origMentions as $origMentionsId=>$origMention) {
- if($origMention->getNickname() == $nickname) {
+ foreach ($origMentions as $origMentionsId=>$origMention) {
+ if ($origMention->getNickname() == $nickname) {
$mention_found_in_origMentions = $origMention;
- // don't mention same twice! the parent might have mentioned
+ // don't mention same twice! the parent might have mentioned
// two users with same nickname on different instances
unset($origMentions[$origMentionsId]);
break;
// Try to get a profile for this nickname.
// Start with parents mentions, then go to parents sender context
if ($mention_found_in_origMentions) {
- $mentioned = $mention_found_in_origMentions;
- } else if ($parent instanceof Notice && $parent->getProfile()->getNickname() === $nickname) {
+ $mentioned = $mention_found_in_origMentions;
+ } elseif ($parent instanceof Notice && $parent->getProfile()->getNickname() === $nickname) {
$mentioned = $parent->getProfile();
} else {
// sets to null if no match
$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),
- 'type' => 'mention',
- 'text' => $match[0],
- 'position' => $match[1],
- 'length' => mb_strlen($match[0]),
- 'title' => $mentioned->getFullname(),
- 'url' => $url);
+ $mention = ['mentioned' => [$mentioned],
+ 'type' => 'mention',
+ 'text' => $match[0],
+ 'position' => $match[1],
+ 'length' => mb_strlen($match[0]),
+ 'title' => $mentioned->getFullname(),
+ 'url' => $url];
$mentions[] = $mention;
}
// @#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);
$tagged = $sender->getTaggedSubscribers($tag);
$url = common_local_url('showprofiletag',
- array('nickname' => $sender->getNickname(),
- 'tag' => $tag));
-
- $mentions[] = array('mentioned' => $tagged,
- 'type' => 'list',
- 'text' => $hmatch[0],
- 'position' => $hmatch[1],
- 'length' => mb_strlen($hmatch[0]),
- 'url' => $url);
+ ['nickname' => $sender->getNickname(),
+ 'tag' => $tag]);
+
+ $mentions[] = ['mentioned' => $tagged,
+ 'type' => 'list',
+ 'text' => $hmatch[0],
+ 'position' => $hmatch[1],
+ 'length' => mb_strlen($hmatch[0]),
+ 'url' => $url];
}
$hmatches = common_find_mentions_raw($text, '!');
$profile = $group->getProfile();
- $mentions[] = array('mentioned' => array($profile),
- 'type' => 'group',
- 'text' => $hmatch[0],
- 'position' => $hmatch[1],
- 'length' => mb_strlen($hmatch[0]),
- 'url' => $group->permalink(),
- 'title' => $group->getFancyName());
+ $mentions[] = ['mentioned' => [$profile],
+ 'type' => 'group',
+ 'text' => $hmatch[0],
+ 'position' => $hmatch[1],
+ 'length' => mb_strlen($hmatch[0]),
+ 'url' => $group->permalink(),
+ 'title' => $group->getFancyName()];
}
- Event::handle('EndFindMentions', array($sender, $text, &$mentions));
+ Event::handle('EndFindMentions', [$sender, $text, &$mentions]);
}
return $mentions;
*/
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,
$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('/(^|\"\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/u',
- function ($m) { return "{$m[1]}#".common_tag_link($m[2]); }, $text);
+ $text = preg_replace_callback(
+ '/(^|\"\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/u',
+ function ($m) {
+ return "{$m[1]}#".common_tag_link($m[2]);
+ },
+ $text
+ );
// XXX: machine tags
return $text;
}
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,
'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,
- function ($scheme) use ($filter) {
- return is_null($filter) || ($scheme & $filter);
- })
- );
+ array_filter(
+ $schemes,
+ function ($scheme) use ($filter) {
+ return is_null($filter) || ($scheme & $filter);
+ })
+ );
}
/**
* @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;
'#ixu';
//preg_match_all($regex,$text,$matches);
//print_r($matches);
- return preg_replace_callback($regex, curry('callback_helper',$callback,$arg) ,$text);
+ return preg_replace_callback($regex, curry('callback_helper', $callback, $arg), $text);
}
/**
*
* @access private
*/
-function callback_helper($matches, $callback, $arg=null) {
+function callback_helper($matches, $callback, $arg=null)
+{
$url=$matches[1];
- $left = strpos($matches[0],$url);
+ $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{
+ do {
$original_url=$url;
- foreach($groupSymbolSets as $groupSymbolSet){
- if(substr($url,-1)==$groupSymbolSet['right']){
- $group_left_count = substr_count($url,$groupSymbolSet['left']);
- $group_right_count = substr_count($url,$groupSymbolSet['right']);
- if($group_left_count<$group_right_count){
+ foreach ($groupSymbolSets as $groupSymbolSet) {
+ if (substr($url, -1)==$groupSymbolSet['right']) {
+ $group_left_count = substr_count($url, $groupSymbolSet['left']);
+ $group_right_count = substr_count($url, $groupSymbolSet['right']);
+ if ($group_left_count<$group_right_count) {
$right-=1;
- $url=substr($url,0,-1);
+ $url=substr($url, 0, -1);
}
}
}
- if(in_array(substr($url,-1),$cannotEndWith)){
+ if (in_array(substr($url, -1), $cannotEndWith)) {
$right-=1;
- $url=substr($url,0,-1);
+ $url=substr($url, 0, -1);
}
- }while($original_url!=$url);
+ } while ($original_url!=$url);
- $result = call_user_func_array($callback, array($url, $arg));
- return substr($matches[0],0,$left) . $result . substr($matches[0],$right);
+ $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);
$canon = File_redirection::_canonUrl($url);
$longurl_data = File_redirection::where($canon, common_config('attachments', 'process_links'));
- if(isset($longurl_data->redir_url)) {
- $longurl = $longurl_data->redir_url;
+ if (isset($longurl_data->redir_url)) {
+ $longurl = $longurl_data->redir_url;
} else {
// e.g. local files
- $longurl = $longurl_data->url;
+ $longurl = $longurl_data->url;
}
}
- $attrs = array('href' => $longurl, 'title' => $longurl);
+ $attrs = ['href' => $longurl, 'title' => $longurl];
$is_attachment = false;
$attachment_id = 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);
}
}
function common_xml_safe_str($str)
{
// Replace common eol and extra whitespace input chars
- $unWelcome = array(
- "\t", // tab
- "\n", // newline
- "\r", // cr
- "\0", // null byte eos
- "\x0B" // vertical tab
- );
-
- $replacement = array(
- ' ', // single space
- ' ',
- '', // nothing
- '',
- ' '
- );
+ $unWelcome = ["\t", // tab
+ "\n", // newline
+ "\r", // cr
+ "\0", // null byte eos
+ "\x0B"]; // vertical tab
+
+ $replacement = [' ', // single space
+ ' ',
+ '', // nothing
+ '',
+ ' '];
$str = str_replace($unWelcome, $replacement, $str);
$str = substr($str, 0, 64);
return $str;
}
- $str = transliterator_transliterate(
- 'Any-Latin;' . // any charset to latin compatible
- 'NFD;' . // decompose
- '[:Nonspacing Mark:] Remove;' . // remove nonspacing marks (accents etc.)
- 'NFC;' . // composite again
- '[:Punctuation:] Remove;' . // remove punctuation (.,¿? etc.)
- 'Lower();' . // turn into lowercase
- 'Latin-ASCII;', // get ASCII equivalents (ð to d for example)
- $str);
+ $str = transliterator_transliterate('Any-Latin;' . // any charset to latin compatible
+ 'NFD;' . // decompose
+ '[:Nonspacing Mark:] Remove;' . // remove nonspacing marks (accents etc.)
+ 'NFC;' . // composite again
+ '[:Punctuation:] Remove;' . // remove punctuation (.,¿? etc.)
+ 'Lower();' . // turn into lowercase
+ 'Latin-ASCII;', // get ASCII equivalents (ð to d for example)
+ $str);
return preg_replace('/[^\pL\pN]/', '', $str);
}
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();
}
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);
$ssl = GNUsocial::useHTTPS();
- if (common_config('site','fancy')) {
+ if (common_config('site', 'fancy')) {
$url = common_path($path, $ssl, $addSession);
} else {
if (mb_strpos($path, '/index.php') === 0) {
$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;
}
if (is_string(common_config('site', 'sslserver')) &&
mb_strlen(common_config('site', 'sslserver')) > 0) {
$serverpart = common_config('site', 'sslserver');
- } else if (common_config('site', 'server')) {
+ } elseif (common_config('site', 'server')) {
$serverpart = common_config('site', 'server');
} else {
common_log(LOG_ERR, 'Site server not configured, unable to determine site name.');
// [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.');
}
'('.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.');
}
if ($now < $t) { // that shouldn't happen!
return common_exact_date($dt);
- } else if ($diff < 60) {
+ } elseif ($diff < 60) {
// TRANS: Used in notices to indicate when the notice was made compared to now.
return _('a few seconds ago');
- } else if ($diff < 92) {
+ } elseif ($diff < 92) {
// TRANS: Used in notices to indicate when the notice was made compared to now.
return _('about a minute ago');
- } else if ($diff < 3300) {
+ } elseif ($diff < 3300) {
$minutes = round($diff/60);
// TRANS: Used in notices to indicate when the notice was made compared to now.
- return sprintf( _m('about one minute ago', 'about %d minutes ago', $minutes), $minutes);
- } else if ($diff < 5400) {
+ return sprintf(_m('about one minute ago', 'about %d minutes ago', $minutes), $minutes);
+ } elseif ($diff < 5400) {
// TRANS: Used in notices to indicate when the notice was made compared to now.
return _('about an hour ago');
- } else if ($diff < 22 * 3600) {
+ } elseif ($diff < 22 * 3600) {
$hours = round($diff/3600);
// TRANS: Used in notices to indicate when the notice was made compared to now.
- return sprintf( _m('about one hour ago', 'about %d hours ago', $hours), $hours);
- } else if ($diff < 37 * 3600) {
+ return sprintf(_m('about one hour ago', 'about %d hours ago', $hours), $hours);
+ } elseif ($diff < 37 * 3600) {
// TRANS: Used in notices to indicate when the notice was made compared to now.
return _('about a day ago');
- } else if ($diff < 24 * 24 * 3600) {
+ } elseif ($diff < 24 * 24 * 3600) {
$days = round($diff/(24*3600));
// TRANS: Used in notices to indicate when the notice was made compared to now.
- return sprintf( _m('about one day ago', 'about %d days ago', $days), $days);
- } else if ($diff < 46 * 24 * 3600) {
+ return sprintf(_m('about one day ago', 'about %d days ago', $days), $days);
+ } elseif ($diff < 46 * 24 * 3600) {
// TRANS: Used in notices to indicate when the notice was made compared to now.
return _('about a month ago');
- } else if ($diff < 330 * 24 * 3600) {
+ } elseif ($diff < 330 * 24 * 3600) {
$months = round($diff/(30*24*3600));
// TRANS: Used in notices to indicate when the notice was made compared to now.
- return sprintf( _m('about one month ago', 'about %d months ago',$months), $months);
- } else if ($diff < 480 * 24 * 3600) {
+ return sprintf(_m('about one month ago', 'about %d months ago', $months), $months);
+ } elseif ($diff < 480 * 24 * 3600) {
// TRANS: Used in notices to indicate when the notice was made compared to now.
return _('about a year ago');
} else {
function common_redirect($url, $code=307)
{
- static $status = array(301 => "Moved Permanently",
- 302 => "Found",
- 303 => "See Other",
- 307 => "Temporary Redirect");
+ static $status = [301 => "Moved Permanently",
+ 302 => "Found",
+ 303 => "See Other",
+ 307 => "Temporary Redirect"];
header('HTTP/1.1 '.$code.' '.$status[$code]);
header("Location: $url");
header("Connection: close");
$xo = new XMLOutputter();
- $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);
+ $xo->startXML(
+ 'a',
+ '-//W3C//DTD XHTML 1.0 Strict//EN',
+ 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
+ );
+ $xo->element('a', ['href' => $url], $url);
$xo->endXML();
exit;
}
function common_enqueue_notice($notice)
{
- static $localTransports = array('ping');
+ static $localTransports = ['ping'];
- $transports = array();
+ $transports = [];
if (common_config('sms', 'enabled')) {
$transports[] = 'sms';
}
$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;
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);
}
/**
{
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;
}
function common_log($priority, $msg, $filename=null)
{
- if(Event::handle('StartLog', array(&$priority, &$msg, &$filename))){
- $msg = (empty($filename)) ? $msg : basename($filename) . ' - ' . $msg;
+ if (Event::handle('StartLog', [&$priority, &$msg, &$filename])) {
+ $msg = (empty($filename)) ? $msg : basename($filename) . ' - ' . $msg;
$msg = '[' . common_request_id() . '] ' . $msg;
$logfile = common_config('site', 'logfile');
if ($logfile) {
common_ensure_syslog();
syslog($priority, $msg);
}
- Event::handle('EndLog', array($priority, $msg, $filename));
+ Event::handle('EndLog', [$priority, $msg, $filename]);
}
}
global $_PEAR;
$objstr = common_log_objstring($object);
- $last_error = &$_PEAR->getStaticProperty('DB_DataObject','lastError');
+ $last_error = &$_PEAR->getStaticProperty('DB_DataObject', 'lastError');
if (is_object($last_error)) {
$msg = $last_error->message;
} else {
return "(unknown)";
}
$arr = $object->toArray();
- $fields = array();
+ $fields = [];
foreach ($arr as $k => $v) {
if (is_object($v)) {
$fields[] = "$k='".get_class($v)."'";
{
$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
function common_accept_to_prefs($accept, $def = '*/*')
{
// No arg means accept anything (per HTTP spec)
- if(!$accept) {
- return array($def => 1);
+ if (!$accept) {
+ return [$def => 1];
}
- $prefs = array();
+ $prefs = [];
$parts = explode(',', $accept);
- foreach($parts as $part) {
+ foreach ($parts as $part) {
// FIXME: doesn't deal with params like 'text/html; level=1'
@list($value, $qpart) = explode(';', trim($part));
- $match = array();
- if(!isset($qpart)) {
+ $match = [];
+ if (!isset($qpart)) {
$prefs[$value] = 1;
- } elseif(preg_match('/q\s*=\s*(\d*\.\d+)/', $qpart, $match)) {
+ } elseif (preg_match('/q\s*=\s*(\d*\.\d+)/', $qpart, $match)) {
$prefs[$value] = $match[1];
}
}
// FIXME: Should we just accept the extension straight off when supported === true?
throw new UnknownExtensionMimeException($fileext);
}
- foreach($supported as $type => $ext) {
+ foreach ($supported as $type => $ext) {
if ($ext === $fileext) {
return $type;
}
{
$supported = common_config('attachments', 'supported');
if (is_array($supported)) {
- foreach($supported as $type => $ext) {
+ foreach ($supported as $type => $ext) {
if ($mimetype === $type) {
return $ext;
}
function common_mime_type_match($type, $avail)
{
- if(array_key_exists($type, $avail)) {
+ if (array_key_exists($type, $avail)) {
return $type;
} else {
$parts = explode('/', $type);
- if(array_key_exists($parts[0] . '/*', $avail)) {
+ if (array_key_exists($parts[0] . '/*', $avail)) {
return $parts[0] . '/*';
- } elseif(array_key_exists('*/*', $avail)) {
+ } elseif (array_key_exists('*/*', $avail)) {
return '*/*';
} else {
return null;
function common_negotiate_type($cprefs, $sprefs)
{
- $combine = array();
+ $combine = [];
- foreach(array_keys($sprefs) as $type) {
+ foreach (array_keys($sprefs) as $type) {
$parts = explode('/', $type);
- if($parts[1] != '*') {
+ if ($parts[1] != '*') {
$ckey = common_mime_type_match($type, $cprefs);
- if($ckey) {
+ if ($ckey) {
$combine[$type] = $sprefs[$type] * $cprefs[$ckey];
}
}
}
- foreach(array_keys($cprefs) as $type) {
+ foreach (array_keys($cprefs) as $type) {
$parts = explode('/', $type);
- if($parts[1] != '*' && !array_key_exists($type, $sprefs)) {
+ if ($parts[1] != '*' && !array_key_exists($type, $sprefs)) {
$skey = common_mime_type_match($type, $sprefs);
- if($skey) {
+ if ($skey) {
$combine[$type] = $sprefs[$skey] * $cprefs[$type];
}
}
$bestq = 0;
$besttype = 'text/html';
- foreach(array_keys($combine) as $type) {
- if($combine[$type] > $bestq) {
+ foreach (array_keys($combine) as $type) {
+ if ($combine[$type] > $bestq) {
$besttype = $type;
$bestq = $combine[$type];
}
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) &&
{
global $config;
if (!array_key_exists($main, $config)) {
- $config[$main] = array();
+ $config[$main] = [];
}
$config[$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);
}
*/
function common_copy_args($from)
{
- $to = array();
+ $to = [];
$strip = get_magic_quotes_gpc();
foreach ($from as $k => $v) {
- if(is_array($v)) {
+ if (is_array($v)) {
$to[$k] = common_copy_args($v);
} else {
if ($strip) {
*/
function common_remove_magic_from_request()
{
- if(get_magic_quotes_gpc()) {
- $_POST=array_map('stripslashes',$_POST);
- $_GET=array_map('stripslashes',$_GET);
+ if (get_magic_quotes_gpc()) {
+ $_POST=array_map('stripslashes', $_POST);
+ $_GET=array_map('stripslashes', $_GET);
}
}
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
}
if (is_null($args)) {
- $args = array();
+ $args = [];
}
// XXX: not very efficient
$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);
}
$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]);
}
}
function common_license_terms($uri)
{
- if(preg_match('/creativecommons.org\/licenses\/([^\/]+)/', $uri, $matches)) {
- return explode('-',$matches[1]);
+ if (preg_match('/creativecommons.org\/licenses\/([^\/]+)/', $uri, $matches)) {
+ return explode('-', $matches[1]);
}
- return array($uri);
+ return [$uri];
}
function common_compatible_license($from, $to)
{
$from_terms = common_license_terms($from);
// public domain and cc-by are compatible with everything
- if(count($from_terms) == 1 && ($from_terms[0] == 'publicdomain' || $from_terms[0] == 'by')) {
+ if (count($from_terms) == 1 && ($from_terms[0] == 'publicdomain' || $from_terms[0] == 'by')) {
return true;
}
$to_terms = common_license_terms($to);
// sa is compatible across versions. IANAL
- if(in_array('sa',$from_terms) || in_array('sa',$to_terms)) {
+ if (in_array('sa', $from_terms) || in_array('sa', $to_terms)) {
return count(array_diff($from_terms, $to_terms)) == 0;
}
// XXX: better compatibility check needed here!
*/
function common_database_tablename($tablename)
{
- if(common_config('db','quote_identifiers')) {
- $tablename = '"'. $tablename .'"';
- }
- //table prefixes could be added here later
- return $tablename;
+ if (common_config('db', 'quote_identifiers')) {
+ $tablename = '"'. $tablename .'"';
+ }
+ //table prefixes could be added here later
+ return $tablename;
}
/**
$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 {
}
}
- 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);
// 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
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");
function common_strip_html($html, $trim=true, $save_whitespace=false)
{
// first replace <br /> with \n
- $html = preg_replace('/\<(\s*)?br(\s*)?\/?(\s*)?\>/i', "\n", $html);
+ $html = preg_replace('/\<(\s*)?br(\s*)?\/?(\s*)?\>/i', "\n", $html);
// then, unless explicitly avoided, remove excessive whitespace
if (!$save_whitespace) {
$html = preg_replace('/\s+/', ' ', $html);