Add internal URL shortener
This commit is contained in:
parent
ba2128f2d1
commit
570c7b63a2
144
actions/redirecturl.php
Normal file
144
actions/redirecturl.php
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* StatusNet - the distributed open-source microblogging tool
|
||||||
|
* Copyright (C) 2010, StatusNet, Inc.
|
||||||
|
*
|
||||||
|
* Redirect to the given URL
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @category URL
|
||||||
|
* @package StatusNet
|
||||||
|
* @author Evan Prodromou <evan@status.net>
|
||||||
|
* @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 <evan@status.net>
|
||||||
|
* @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)) . '"';
|
||||||
|
}
|
||||||
|
}
|
@ -99,23 +99,28 @@ class UrlsettingsAction extends SettingsAction
|
|||||||
$this->hidden('token', common_session_token());
|
$this->hidden('token', common_session_token());
|
||||||
$this->elementStart('ul', 'form_data');
|
$this->elementStart('ul', 'form_data');
|
||||||
|
|
||||||
$shorteners = array(_('[none]') => array('freeService' => false));
|
|
||||||
|
|
||||||
Event::handle('GetUrlShorteners', array(&$shorteners));
|
Event::handle('GetUrlShorteners', array(&$shorteners));
|
||||||
|
|
||||||
$services = array();
|
$services = array();
|
||||||
foreach($shorteners as $name=>$value)
|
|
||||||
|
foreach ($shorteners as $name => $value)
|
||||||
{
|
{
|
||||||
$services[$name]=$name;
|
$services[$name] = $name;
|
||||||
if($value['freeService']){
|
if ($value['freeService']) {
|
||||||
// TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
|
// 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: 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).
|
// 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);
|
asort($services);
|
||||||
|
|
||||||
$this->elementStart('li');
|
$this->elementStart('li');
|
||||||
|
@ -295,17 +295,7 @@ $default =
|
|||||||
'logincommand' =>
|
'logincommand' =>
|
||||||
array('disabled' => true),
|
array('disabled' => true),
|
||||||
'plugins' =>
|
'plugins' =>
|
||||||
array('default' => array('LilUrl' => array('shortenerName'=>'ur1.ca',
|
array('default' => array('Geonames' => null,
|
||||||
'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,
|
|
||||||
'Mapstraction' => null,
|
'Mapstraction' => null,
|
||||||
'OStatus' => null,
|
'OStatus' => null,
|
||||||
'WikiHashtags' => null,
|
'WikiHashtags' => null,
|
||||||
|
@ -974,6 +974,12 @@ class Router
|
|||||||
array('action' => 'AtomPubMembershipFeed'),
|
array('action' => 'AtomPubMembershipFeed'),
|
||||||
array('profile' => '[0-9]+'));
|
array('profile' => '[0-9]+'));
|
||||||
|
|
||||||
|
// URL shortening
|
||||||
|
|
||||||
|
$m->connect('url/:id',
|
||||||
|
array('action' => 'redirecturl',
|
||||||
|
'id' => '[0-9]+'));
|
||||||
|
|
||||||
// user stuff
|
// user stuff
|
||||||
|
|
||||||
Event::handle('RouterInitialized', array($m));
|
Event::handle('RouterInitialized', array($m));
|
||||||
|
16
lib/util.php
16
lib/util.php
@ -2073,6 +2073,7 @@ function common_shorten_url($long_url, User $user=null, $force = false)
|
|||||||
common_debug("maxUrlLength = $maxUrlLength");
|
common_debug("maxUrlLength = $maxUrlLength");
|
||||||
|
|
||||||
// $force forces shortening even if it's not strictly needed
|
// $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) {
|
if (mb_strlen($long_url) < $maxUrlLength && !$force) {
|
||||||
common_debug("Skipped shortening URL.");
|
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'");
|
common_debug("Shortener name = '$shortenerName'");
|
||||||
|
|
||||||
if (Event::handle('StartShortenUrl', array($long_url, $shortenerName, &$shortenedUrl))) {
|
if (Event::handle('StartShortenUrl',
|
||||||
//URL wasn't shortened, so return the long url
|
array($long_url, $shortenerName, &$shortenedUrl))) {
|
||||||
|
if ($shortenerName == 'internal') {
|
||||||
|
$f = File::processNew($long_url);
|
||||||
|
if (empty($f)) {
|
||||||
return $long_url;
|
return $long_url;
|
||||||
|
} else {
|
||||||
|
$shortenedUrl = common_local_url('redirecturl',
|
||||||
|
array('id' => $f->id));
|
||||||
|
return $shortenedUrl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return $long_url;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
//URL was shortened, so return the result
|
//URL was shortened, so return the result
|
||||||
return trim($shortenedUrl);
|
return trim($shortenedUrl);
|
||||||
|
Loading…
Reference in New Issue
Block a user