From 570c7b63a2c0506e4876e27cd9ab56c16ce60b6c Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 23 Jan 2011 16:49:12 -0500 Subject: [PATCH] Add internal URL shortener --- actions/redirecturl.php | 144 ++++++++++++++++++++++++++++++++++++++++ actions/urlsettings.php | 21 +++--- lib/default.php | 12 +--- lib/router.php | 6 ++ lib/util.php | 18 ++++- 5 files changed, 179 insertions(+), 22 deletions(-) create mode 100644 actions/redirecturl.php diff --git a/actions/redirecturl.php b/actions/redirecturl.php new file mode 100644 index 0000000000..0a959b074f --- /dev/null +++ b/actions/redirecturl.php @@ -0,0 +1,144 @@ +. + * + * @category URL + * @package StatusNet + * @author Evan Prodromou + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + // This check helps protect against security problems; + // your code file can't be executed directly from the web. + exit(1); +} + +/** + * Redirect to a given URL + * + * This is our internal low-budget URL-shortener + * + * @category Action + * @package StatusNet + * @author Evan Prodromou + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +class RedirecturlAction extends Action +{ + protected $id = null; + protected $file = null; + + /** + * For initializing members of the class. + * + * @param array $argarray misc. arguments + * + * @return boolean true + */ + + function prepare($argarray) + { + parent::prepare($argarray); + + $this->id = $this->trimmed('id'); + + if (empty($this->id)) { + throw new ClientException(_('No id parameter')); + } + + $this->file = File::staticGet('id', $this->id); + + if (empty($this->file)) { + throw new ClientException(sprintf(_('No such file "%d"'), + $this->id), + 404); + } + + return true; + } + + /** + * Handler method + * + * @param array $argarray is ignored since it's now passed in in prepare() + * + * @return void + */ + + function handle($argarray=null) + { + common_redirect($this->file->url, 307); + return; + } + + /** + * Return true if read only. + * + * MAY override + * + * @param array $args other arguments + * + * @return boolean is read only action? + */ + + function isReadOnly($args) + { + return true; + } + + /** + * Return last modified, if applicable. + * + * MAY override + * + * @return string last modified http header + */ + + function lastModified() + { + // For comparison with If-Last-Modified + // If not applicable, return null + + return strtotime($this->file->modified); + } + + /** + * Return etag, if applicable. + * + * MAY override + * + * @return string etag http header + */ + + function etag() + { + return 'W/"' . implode(':', array($this->arg('action'), + common_user_cache_hash(), + common_language(), + $this->file->id)) . '"'; + } +} diff --git a/actions/urlsettings.php b/actions/urlsettings.php index f8780a7e86..140e28c999 100644 --- a/actions/urlsettings.php +++ b/actions/urlsettings.php @@ -99,23 +99,28 @@ class UrlsettingsAction extends SettingsAction $this->hidden('token', common_session_token()); $this->elementStart('ul', 'form_data'); - $shorteners = array(_('[none]') => array('freeService' => false)); - Event::handle('GetUrlShorteners', array(&$shorteners)); $services = array(); - foreach($shorteners as $name=>$value) + + foreach ($shorteners as $name => $value) { - $services[$name]=$name; - if($value['freeService']){ + $services[$name] = $name; + if ($value['freeService']) { // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a // TRANS: user's profile settings. This message has one space at the beginning. Use your // TRANS: language's word separator here if it has one (most likely a single space). - $services[$name].=_(' (free service)'); + $services[$name] .= _(' (free service)'); } } - if($services) - { + + // Include default values + + $services['none'] = _('[none]'); + $services['internal'] = _('[internal]'); + + if ($services) { + asort($services); $this->elementStart('li'); diff --git a/lib/default.php b/lib/default.php index 9528d9cf8b..186ac263c9 100644 --- a/lib/default.php +++ b/lib/default.php @@ -295,17 +295,7 @@ $default = 'logincommand' => array('disabled' => true), 'plugins' => - array('default' => array('LilUrl' => array('shortenerName'=>'ur1.ca', - 'freeService' => true, - 'serviceUrl'=>'http://ur1.ca/'), - 'PtitUrl' => array('shortenerName' => 'ptiturl.com', - 'serviceUrl' => 'http://ptiturl.com/?creer=oui&action=Reduire&url=%1$s'), - 'SimpleUrl' => array(array('shortenerName' => 'is.gd', 'serviceUrl' => 'http://is.gd/api.php?longurl=%1$s'), - array('shortenerName' => 'snipr.com', 'serviceUrl' => 'http://snipr.com/site/snip?r=simple&link=%1$s'), - array('shortenerName' => 'metamark.net', 'serviceUrl' => 'http://metamark.net/api/rest/simple?long_url=%1$s'), - array('shortenerName' => 'tinyurl.com', 'serviceUrl' => 'http://tinyurl.com/api-create.php?url=%1$s')), - 'TightUrl' => array('shortenerName' => '2tu.us', 'freeService' => true,'serviceUrl'=>'http://2tu.us/?save=y&url=%1$s'), - 'Geonames' => null, + array('default' => array('Geonames' => null, 'Mapstraction' => null, 'OStatus' => null, 'WikiHashtags' => null, diff --git a/lib/router.php b/lib/router.php index 99d8aeb043..10e5291588 100644 --- a/lib/router.php +++ b/lib/router.php @@ -974,6 +974,12 @@ class Router array('action' => 'AtomPubMembershipFeed'), array('profile' => '[0-9]+')); + // URL shortening + + $m->connect('url/:id', + array('action' => 'redirecturl', + 'id' => '[0-9]+')); + // user stuff Event::handle('RouterInitialized', array($m)); diff --git a/lib/util.php b/lib/util.php index 3d4adcf4bf..4aec05275c 100644 --- a/lib/util.php +++ b/lib/util.php @@ -2073,6 +2073,7 @@ function common_shorten_url($long_url, User $user=null, $force = false) common_debug("maxUrlLength = $maxUrlLength"); // $force forces shortening even if it's not strictly needed + // I doubt URL shortening is ever 'strictly' needed. - ESP if (mb_strlen($long_url) < $maxUrlLength && !$force) { common_debug("Skipped shortening URL."); @@ -2083,9 +2084,20 @@ function common_shorten_url($long_url, User $user=null, $force = false) common_debug("Shortener name = '$shortenerName'"); - if (Event::handle('StartShortenUrl', array($long_url, $shortenerName, &$shortenedUrl))) { - //URL wasn't shortened, so return the long url - return $long_url; + if (Event::handle('StartShortenUrl', + array($long_url, $shortenerName, &$shortenedUrl))) { + if ($shortenerName == 'internal') { + $f = File::processNew($long_url); + if (empty($f)) { + return $long_url; + } else { + $shortenedUrl = common_local_url('redirecturl', + array('id' => $f->id)); + return $shortenedUrl; + } + } else { + return $long_url; + } } else { //URL was shortened, so return the result return trim($shortenedUrl);