return false;
}
- if ($user) {
+ if ($user instanceof User) {
if (Event::handle('StartSetUser', array(&$user))) {
if (!empty($user)) {
if (!$user->hasRight(Right::WEBLOGIN)) {
if ($user === false) {
$user = common_current_user();
}
- if ($user) {
+ if ($user instanceof User) {
return crc32($user->id . ':' . $user->nickname);
} else {
return '0';
return $email;
}
+function common_purify($html)
+{
+ require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
+
+ $config = array('safe' => 1, // means that elements=* means elements=*-applet-embed-iframe-object-script or so
+ 'elements' => '*',
+ 'deny_attribute' => 'id,style,on*');
+
+ // Remove more elements than what the 'safe' filter gives (elements must be '*' before this)
+ // http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/htmLawed_README.htm#s3.6
+ foreach (common_config('htmlfilter') as $tag=>$filter) {
+ if ($filter === true) {
+ $config['elements'] .= "-{$tag}";
+ }
+ }
+
+ $html = common_remove_unicode_formatting($html);
+
+ return htmLawed($html, $config);
+}
+
+function common_remove_unicode_formatting($text)
+{
+ // Strip Unicode text formatting/direction codes
+ // this is pretty dangerous for visualisation of text and can be used for mischief
+ return preg_replace('/[\\x{200b}-\\x{200f}\\x{202a}-\\x{202e}]/u', '', $text);
+}
+
/**
* Partial notice markup rendering step: build links to !group references.
*
*/
function common_render_content($text, Notice $notice)
{
- $r = common_render_text($text);
- $r = common_linkify_mentions($r, $notice);
- return $r;
+ $text = common_render_text($text);
+ $text = common_linkify_mentions($text, $notice);
+ return $text;
}
/**
* @param Notice $notice in-progress or complete Notice object for context
* @return string partially-rendered HTML
*/
-function common_linkify_mentions($text, $notice)
+function common_linkify_mentions($text, Notice $notice)
{
$mentions = common_find_mentions($text, $notice);
return $text;
}
-function common_linkify_mention($mention)
+function common_linkify_mention(array $mention)
{
$output = null;
*
* @access private
*/
-function common_find_mentions($text, $notice)
+function common_find_mentions($text, Notice $notice)
{
- try {
- $sender = Profile::getKV('id', $notice->profile_id);
- } catch (NoProfileException $e) {
- return array();
- }
+ // The getProfile call throws NoProfileException on failure
+ $sender = $notice->getProfile();
$mentions = array();
}
} catch (NoProfileException $e) {
common_log(LOG_WARNING, sprintf('Notice %d author profile id %d does not exist', $origNotice->id, $origNotice->profile_id));
- } catch (ServerException $e) {
- // Probably just no parent. Should get a specific NoParentException
+ } catch (NoParentNoticeException $e) {
+ // This notice is not in reply to anything
} catch (Exception $e) {
common_log(LOG_WARNING, __METHOD__ . ' got exception ' . get_class($e) . ' : ' . $e->getMessage());
}
function common_render_text($text)
{
- $r = nl2br(htmlspecialchars($text));
+ $text = common_remove_unicode_formatting($text);
+ $text = nl2br(htmlspecialchars($text));
- $r = preg_replace('/[\x{0}-\x{8}\x{b}-\x{c}\x{e}-\x{19}]/', '', $r);
- $r = common_replace_urls_callback($r, 'common_linkify');
- $r = preg_replace_callback('/(^|\"\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/u',
- function ($m) { return "{$m[1]}#".common_tag_link($m[2]); }, $r);
+ $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);
// XXX: machine tags
- return $r;
+ return $text;
}
/**
')'.
')'.
')'.
+ '|(?:(?:magnet):)'. // URLs without domain name
'|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'. //IPv4
'|(?:'. //IPv6
'\[?(?:(?:(?:[0-9A-Fa-f]{1,4}:){7}(?:(?:[0-9A-Fa-f]{1,4})|:))|(?:(?:[0-9A-Fa-f]{1,4}:){6}(?::|(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(?::[0-9A-Fa-f]{1,4})))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षा|XN--80AKHBYKNJ4F|испытание|XN--9T4B11YI5A|테스트|XN--DEBA0AD|טעסט|XN--G6W251D|測試|XN--HGBK6AJ7F53BBA|آزمایشی|XN--HLCJ6AYA9ESC7A|பரிட்சை|XN--JXALPDLP|δοκιμή|XN--KGBECHTV|إختبار|XN--ZCKZAH|テスト|YE|YT|YU|ZA|ZM|ZONE|ZW|local|loc|onion)'.
- ')(?![\pN\pL\-\_])'.
+ ')'.
+ (common_config('linkify', 'bare_domains')
+ ? '|(?:'. //DNS
+ '(?:[\pN\pL\-\_\+\%\~]+(?:\:[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@
+ '[\pN\pL\-\_]+(?:\.[\pN\pL\-\_]+)*\.'.
+ //tld list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt, also added local, loc, and onion
+ '(?:AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|ASIA|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|BZ|CA|CAT|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EDU|EE|EG|ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GOV|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|ID|IE|IL|IM|IN|INFO|INT|IO|IQ|IR|IS|IT|JE|JM|JO|JOBS|JP|KE|KG|KH|KI|KM|KN|KP|KR|KW|KY|KZ|LA|LB|LC|LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|ME|MG|MH|MIL|MK|ML|MM|MN|MO|MOBI|MP|MQ|MR|MS|MT|MU|MUSEUM|MV|MW|MX|MY|MZ|NA|NAME|NC|NE|NET|NF|NG|NI|NL|NO|NP|NR|NU|NZ|OM|ORG|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PRO|PS|PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|ST|SU|SV|SY|SZ|TC|TD|TEL|TF|TG|TH|TJ|TK|TL|TM|TN|TO|TP|TR|TRAVEL|TT|TV|TW|TZ|UA|UG|UK|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|XN--0ZWM56D|测试|XN--11B5BS3A9AJ6G|परीक्षा|XN--80AKHBYKNJ4F|испытание|XN--9T4B11YI5A|테스트|XN--DEBA0AD|טעסט|XN--G6W251D|測試|XN--HGBK6AJ7F53BBA|آزمایشی|XN--HLCJ6AYA9ESC7A|பரிட்சை|XN--JXALPDLP|δοκιμή|XN--KGBECHTV|إختبار|XN--ZCKZAH|テスト|YE|YT|YU|ZA|ZM|ZONE|ZW|local|loc|onion)'.
+ ')(?![\pN\pL\-\_])'
+ : '') . // if common_config('linkify', 'bare_domains') is false, don't add anything here
')'.
'(?:'.
'(?:\:\d+)?'. //:port
return preg_replace('/[\p{Cc}\p{Cs}]/u', '*', $str);
}
+function common_slugify($str)
+{
+ // php5-intl is highly recommended...
+ if (!function_exists('transliterator_transliterate')) {
+ $str = preg_replace('/[^\pL\pN]/u', '', $str);
+ $str = mb_convert_case($str, MB_CASE_LOWER, 'UTF-8');
+ $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);
+ return preg_replace('/[^\pL\pN]/', '', $str);
+}
+
function common_tag_link($tag)
{
$canonical = common_canonical_tag($tag);
function common_canonical_tag($tag)
{
- // only alphanum
- $tag = preg_replace('/[^\pL\pN]/u', '', $tag);
- $tag = mb_convert_case($tag, MB_CASE_LOWER, "UTF-8");
- $tag = substr($tag, 0, 64);
- return $tag;
+ $tag = common_slugify($tag);
+ $tag = substr($tag, 0, 64);
+ return $tag;
}
function common_valid_profile_tag($str)
$path = $r->build($action, $args, $params, $fragment);
$ssl = common_config('site', 'ssl') === 'always'
- || StatusNet::isHTTPS()
+ || GNUsocial::isHTTPS()
|| common_is_sensitive($action);
if (common_config('site','fancy')) {
$pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
if (($ssl && (common_config('site', 'ssl') === 'sometimes'))
- || StatusNet::isHTTPS()
+ || GNUsocial::isHTTPS()
|| common_config('site', 'ssl') === 'always') {
$proto = 'https';
if (is_string(common_config('site', 'sslserver')) &&
return $besttype;
}
-function common_config($main, $sub)
+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 the config value
return (array_key_exists($main, $config) &&
array_key_exists($sub, $config[$main])) ? $config[$main][$sub] : false;
}
$_perfCounters[$key] = array($val);
}
if (common_config('site', 'logperf_detail')) {
- common_log(LOG_DEBUG, "PERF COUNTER HIT: $key $val");
+ common_debug("PERF COUNTER HIT: $key $val");
}
}
}
if (isset($_startTime)) {
$endTime = microtime(true);
$diff = round(($endTime - $_startTime) * 1000);
- common_log(LOG_DEBUG, "PERF runtime: ${diff}ms");
+ common_debug("PERF runtime: ${diff}ms");
}
$counters = $_perfCounters;
ksort($counters);
foreach ($counters as $key => $values) {
$count = count($values);
$unique = count(array_unique($values));
- common_log(LOG_DEBUG, "PERF COUNTER: $key $count ($unique unique)");
+ common_debug("PERF COUNTER: $key $count ($unique unique)");
}
}
}