From 2dd5a5f86d5242362499c381c8d9519f40a8925b Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Thu, 27 Aug 2009 12:06:45 -0400 Subject: Do not used named capturing groups I'm not sure all php 5.2's are compiled with a PCRE library that supported named captures. --- lib/util.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/util.php') diff --git a/lib/util.php b/lib/util.php index 7c1e21913..1e7834057 100644 --- a/lib/util.php +++ b/lib/util.php @@ -413,7 +413,7 @@ function common_replace_urls_callback($text, $callback, $notice_id = null) { // Start off with a regex $regex = '#'. '(?:^|[\s\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'. - '(?P'. + '('. '(?:'. '(?:'. //Known protocols '(?:'. @@ -454,7 +454,7 @@ function common_replace_urls_callback($text, $callback, $notice_id = null) { } function callback_helper($matches, $callback, $notice_id) { - $url=$matches['url']; + $url=$matches[1]; $left = strpos($matches[0],$url); $right = $left+strlen($url); -- cgit v1.2.3-54-g00ecf From 0056b635c661c4aaf6bce08795af925388e35f5c Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Thu, 27 Aug 2009 20:06:03 -0700 Subject: reformat curry() to make my editor happy --- lib/util.php | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'lib/util.php') diff --git a/lib/util.php b/lib/util.php index cedb70873..79a219fd6 100644 --- a/lib/util.php +++ b/lib/util.php @@ -457,7 +457,7 @@ function callback_helper($matches, $callback, $notice_id) { $url=$matches[1]; $left = strpos($matches[0],$url); $right = $left+strlen($url); - + $groupSymbolSets=array( array( 'left'=>'(', @@ -491,9 +491,7 @@ function callback_helper($matches, $callback, $notice_id) { $url=substr($url,0,-1); } }while($original_url!=$url); - - - + if(empty($notice_id)){ $result = call_user_func_array($callback,$url); }else{ @@ -508,16 +506,13 @@ function curry($fn) { array_shift($args); $id = uniqid('_partial'); $GLOBALS[$id] = array($fn, $args); - return create_function( - '', - ' - $args = func_get_args(); - return call_user_func_array( - $GLOBALS["'.$id.'"][0], - array_merge( - $args, - $GLOBALS["'.$id.'"][1])); - '); + return create_function('', + '$args = func_get_args(); '. + 'return call_user_func_array('. + '$GLOBALS["'.$id.'"][0],'. + 'array_merge('. + '$args,'. + '$GLOBALS["'.$id.'"][1]));'); } function common_linkify($url) { -- cgit v1.2.3-54-g00ecf From 34ce75c71d37754fa941233c805c042a47910184 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Thu, 27 Aug 2009 20:09:07 -0700 Subject: remove duplicate save of Notice and streamline attachment detection --- classes/Notice.php | 13 +------------ lib/util.php | 41 ++++++++++++----------------------------- 2 files changed, 13 insertions(+), 41 deletions(-) (limited to 'lib/util.php') diff --git a/classes/Notice.php b/classes/Notice.php index e59712864..28d5b8ddf 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -58,7 +58,7 @@ class Notice extends Memcached_DataObject /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE - /* Notice types */ + /* Notice types */ const LOCAL_PUBLIC = 1; const REMOTE_OMB = 0; const LOCAL_NONPUBLIC = -1; @@ -248,17 +248,6 @@ class Notice extends Memcached_DataObject $notice->saveUrls(); - // FIXME: why do we have to re-render the content? - // Remove this if it's not necessary. - - $orig2 = clone($notice); - - $notice->rendered = common_render_content($final, $notice); - if (!$notice->update($orig2)) { - common_log_db_error($notice, 'UPDATE', __FILE__); - return _('Problem saving notice.'); - } - $notice->query('COMMIT'); Event::handle('EndNoticeSave', array($notice)); diff --git a/lib/util.php b/lib/util.php index 79a219fd6..070b4232c 100644 --- a/lib/util.php +++ b/lib/util.php @@ -542,8 +542,7 @@ function common_linkify($url) { $attachment_id = null; $has_thumb = false; - // Check to see whether there's a filename associated with this URL. - // If there is, it's an upload and qualifies as an attachment + // Check to see whether this is a known "attachment" URL. $localfile = File::staticGet('url', $longurl); @@ -551,33 +550,17 @@ function common_linkify($url) { if (isset($localfile->filename)) { $is_attachment = true; $attachment_id = $localfile->id; - } - } - - // if this URL is an attachment, then we set class='attachment' and id='attahcment-ID' - // where ID is the id of the attachment for the given URL. - // - // we need a better test telling what can be shown as an attachment - // we're currently picking up oembeds only. - // I think the best option is another file_view table in the db - // and associated dbobject. - - $query = "select file_oembed.file_id as file_id from file join file_oembed on file.id = file_oembed.file_id where file.url='$longurl'"; - $file = new File; - $file->query($query); - $file->fetch(); - - if (!empty($file->file_id)) { - $is_attachment = true; - $attachment_id = $file->file_id; - - $query = "select file_thumbnail.file_id as file_id from file join file_thumbnail on file.id = file_thumbnail.file_id where file.url='$longurl'"; - $file2 = new File; - $file2->query($query); - $file2->fetch(); - - if (!empty($file2)) { - $has_thumb = true; + } else { // if it has OEmbed info, it's an attachment, too + $foe = File_oembed::staticGet('file_id', $localfile->id); + if (!empty($foe)) { + $is_attachment = true; + $attachment_id = $localfile->id; + + $thumb = File_thumbnail::staticGet('file_id', $localfile->id); + if (!empty($thumb)) { + $has_thumb = true; + } + } } } -- cgit v1.2.3-54-g00ecf From c0d03fc2799c7b0c57d05166b404a3d13427f497 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Thu, 27 Aug 2009 20:23:31 -0700 Subject: make URL analyzer save new info on URLs --- classes/File.php | 8 +++++--- lib/util.php | 21 +++++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) (limited to 'lib/util.php') diff --git a/classes/File.php b/classes/File.php index 0cebfc55e..f4d0a3a48 100644 --- a/classes/File.php +++ b/classes/File.php @@ -85,7 +85,7 @@ class File extends Memcached_DataObject return $x; } - function processNew($given_url, $notice_id) { + function processNew($given_url, $notice_id=null) { if (empty($given_url)) return -1; // error, no url to process $given_url = File_redirection::_canonUrl($given_url); if (empty($given_url)) return -1; // error, no url to process @@ -96,7 +96,7 @@ class File extends Memcached_DataObject $redir_data = File_redirection::where($given_url); $redir_url = $redir_data['url']; // TODO: max field length - if ($redir_url === $given_url || strlen($redir_url) > 255) { + if ($redir_url === $given_url || strlen($redir_url) > 255) { $x = File::saveNew($redir_data, $given_url); $file_id = $x->id; } else { @@ -119,7 +119,9 @@ class File extends Memcached_DataObject } } - File_to_post::processNew($file_id, $notice_id); + if (!empty($notice_id)) { + File_to_post::processNew($file_id, $notice_id); + } return $x; } diff --git a/lib/util.php b/lib/util.php index 070b4232c..8a56be55d 100644 --- a/lib/util.php +++ b/lib/util.php @@ -520,7 +520,7 @@ function common_linkify($url) { // functions $url = htmlspecialchars_decode($url); - if(strpos($url, '@')!==false && strpos($url, ':')===false){ + if(strpos($url, '@') !== false && strpos($url, ':') === false) { //url is an email address without the mailto: protocol return XMLStringer::estring('a', array('href' => "mailto:$url", 'rel' => 'external'), $url); } @@ -544,19 +544,24 @@ function common_linkify($url) { // Check to see whether this is a known "attachment" URL. - $localfile = File::staticGet('url', $longurl); + $f = File::staticGet('url', $longurl); - if (!empty($localfile)) { - if (isset($localfile->filename)) { + if (empty($f)) { + // XXX: this writes to the database. :< + $f = File::processNew($longurl); + } + + if (!empty($f)) { + if (isset($f->filename)) { $is_attachment = true; - $attachment_id = $localfile->id; + $attachment_id = $f->id; } else { // if it has OEmbed info, it's an attachment, too - $foe = File_oembed::staticGet('file_id', $localfile->id); + $foe = File_oembed::staticGet('file_id', $f->id); if (!empty($foe)) { $is_attachment = true; - $attachment_id = $localfile->id; + $attachment_id = $f->id; - $thumb = File_thumbnail::staticGet('file_id', $localfile->id); + $thumb = File_thumbnail::staticGet('file_id', $f->id); if (!empty($thumb)) { $has_thumb = true; } -- cgit v1.2.3-54-g00ecf From b4ca06edb286a4518b0a59e2a59e50e1c4b6ecb1 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 28 Aug 2009 08:43:28 -0700 Subject: fix 'callback_helper' --- lib/util.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/util.php') diff --git a/lib/util.php b/lib/util.php index 8a56be55d..87d5800f6 100644 --- a/lib/util.php +++ b/lib/util.php @@ -450,7 +450,7 @@ function common_replace_urls_callback($text, $callback, $notice_id = null) { '#ixu'; preg_match_all($regex,$text,$matches); //print_r($matches); - return preg_replace_callback($regex, curry(callback_helper,$callback,$notice_id) ,$text); + return preg_replace_callback($regex, curry('callback_helper',$callback,$notice_id) ,$text); } function callback_helper($matches, $callback, $notice_id) { -- cgit v1.2.3-54-g00ecf From 3368452ebf3c738933b05e902c277296684e280b Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Fri, 28 Aug 2009 16:18:05 -0400 Subject: Add % and ~ as valid characters in the path, querystring, and fragment parts of URLs --- lib/util.php | 10 +++++----- tests/URLDetectionTest.php | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'lib/util.php') diff --git a/lib/util.php b/lib/util.php index 8a56be55d..edc08d4c1 100644 --- a/lib/util.php +++ b/lib/util.php @@ -421,7 +421,7 @@ function common_replace_urls_callback($text, $callback, $notice_id = null) { '|'. '(?:(?:mailto|aim|tel|xmpp):)'. ')'. - '(?:[\pN\pL\-\_\+]+(?::[\pN\pL\-\_\+]+)?\@)?'. //user:pass@ + '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@ '(?:'. '(?:'. '\[[\pN\pL\-\_\:\.]+(?127.0.0.1:99'), array('127.0.0.1/test.php', '127.0.0.1/test.php'), + array('127.0.0.1/~test', + '127.0.0.1/~test'), + array('127.0.0.1/test%20stuff', + '127.0.0.1/test%20stuff'), array('http://[::1]:99/test.php', 'http://[::1]:99/test.php'), array('http://::1/test.php', -- cgit v1.2.3-54-g00ecf From 20423af689fbf4dd139e1254dc49ae3df1db0de2 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Mon, 31 Aug 2009 10:33:37 -0400 Subject: Allow :'s in the path, query string, and fragment parts of the url (Mediawiki URLs often do this) --- lib/util.php | 6 +++--- tests/URLDetectionTest.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/util.php') diff --git a/lib/util.php b/lib/util.php index 7228b3fe3..6e79ffda4 100644 --- a/lib/util.php +++ b/lib/util.php @@ -442,9 +442,9 @@ function common_replace_urls_callback($text, $callback, $notice_id = null) { ')'. '(?:'. '(?:\:\d+)?'. //:port - '(?:/[\pN\pL$\[\]\,\!\(\)\.\-\_\+\/\=\&\;\%\~]*)?'. // /path - '(?:\?[\pN\pL\$\[\]\,\!\(\)\.\-\_\+\/\=\&\;\%\~\/]*)?'. // ?query string - '(?:\#[\pN\pL$\[\]\,\!\(\)\.\-\_\+\/\=\&\;\%\~\/\?\#]*)?'. // #fragment + '(?:/[\pN\pL$\[\]\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~]*)?'. // /path + '(?:\?[\pN\pL\$\[\]\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\/]*)?'. // ?query string + '(?:\#[\pN\pL$\[\]\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\/\?\#]*)?'. // #fragment ')(?127.0.0.1'), array('127.0.0.1:99', '127.0.0.1:99'), - array('127.0.0.1/test.php', - '127.0.0.1/test.php'), + array('127.0.0.1/Name:test.php', + '127.0.0.1/Name:test.php'), array('127.0.0.1/~test', '127.0.0.1/~test'), array('127.0.0.1/test%20stuff', -- cgit v1.2.3-54-g00ecf From 951a787877f450fc7f225cba4331f0763b71dbc2 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Mon, 31 Aug 2009 15:33:23 -0400 Subject: Fix attachment saving --- lib/util.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/util.php') diff --git a/lib/util.php b/lib/util.php index 6e79ffda4..f4ba3a6c2 100644 --- a/lib/util.php +++ b/lib/util.php @@ -495,7 +495,7 @@ function callback_helper($matches, $callback, $notice_id) { if(empty($notice_id)){ $result = call_user_func_array($callback,$url); }else{ - $result = call_user_func_array($callback, array($url,$notice_id) ); + $result = call_user_func_array($callback, array(array($url,$notice_id)) ); } return substr($matches[0],0,$left) . $result . substr($matches[0],$right); } -- cgit v1.2.3-54-g00ecf