YOURLS support. issue #6

This commit is contained in:
Claude 2013-11-08 15:14:27 +01:00
parent d2f89d7fb5
commit c69548107c
2 changed files with 68 additions and 18 deletions

View File

@ -68,12 +68,24 @@ $config['combine_assets'] = false;
$config['cron_key'] = ''; $config['cron_key'] = '';
/** /**
* gw.gd config * url shortener config
* *
* Your own instance of the gw.gd URL-shortener (Download: https://github.com/neofutur/gwgd) * If yourls_url is set, yourls is used instead of gw.gd
*
* yourls_url: Your own instance of yourls URL-shortener (Download: http://yourls.org/)
* Example: http://example.com/yourls/
*
* yourls_signature: Your signature, used to authenticate API requests.
* You can find your signature under http://your-yourls-installation.com/admin/tools.php
*
* OR
*
* gwgd_url: Your own instance of the gw.gd URL-shortener (Download: https://github.com/neofutur/gwgd)
* Default: http://gw.gd/ * Default: http://gw.gd/
* *
**/ **/
$config['yourls_url'] = '';
$config['yourls_signature'] = '';
$config['gwgd_url'] = ''; $config['gwgd_url'] = '';
/** /**

View File

@ -7,6 +7,7 @@
* - countReplies() * - countReplies()
* - createPaste() * - createPaste()
* - _get_url() * - _get_url()
* - _shorten_url()
* - checkPaste() * - checkPaste()
* - getPaste() * - getPaste()
* - calculate_hits() * - calculate_hits()
@ -121,22 +122,8 @@ class Pastes extends CI_Model
else else
{ {
$url = $this->_get_url($data['pid']); $url = $this->_get_url($data['pid']);
$url = urlencode($url); $shorturl = $this->_shorten_url($url);
$config_gwgd_url = $this->config->item('gwgd_url'); $data['snipurl'] = $shorturl;
$gwgd_url = ($config_gwgd_url ? $config_gwgd_url : 'http://gw.gd/');
$target = $gwgd_url . 'api.php?long=' . $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'identity');
$resp = curl_exec($ch);
curl_close($ch);
$data['snipurl'] = $resp;
if (empty($data['snipurl']))
{
$data['snipurl'] = false;
}
} }
$data['ip_address'] = $this->input->ip_address(); $data['ip_address'] = $this->input->ip_address();
$this->db->insert('pastes', $data); $this->db->insert('pastes', $data);
@ -148,6 +135,57 @@ class Pastes extends CI_Model
$override_url = $this->config->item('displayurl_override'); $override_url = $this->config->item('displayurl_override');
return ($override_url ? str_replace('$id', $pid, $override_url) : site_url('view/' . $pid)); return ($override_url ? str_replace('$id', $pid, $override_url) : site_url('view/' . $pid));
} }
private
function _shorten_url($url)
{
$config_yourls_url = $this->config->item('yourls_url');
if ($config_yourls_url)
{
//use yourls
$config_yourls_url = $this->config->item('yourls_url');
$config_yourls_signature = $this->config->item('yourls_signature');
$timestamp = time();
$signature = md5($timestamp . $config_yourls_signature);
// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config_yourls_url . 'yourls-api.php');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'url' => $url,
'format' => 'simple',
'action' => 'shorturl',
'signature' => $signature,
'timestamp' => $timestamp,
));
$resp = curl_exec($ch);
curl_close($ch);
$shorturl = (empty($resp) ? false : $resp);
}
else
{
//use gdgw
$url = urlencode($url);
$config_gwgd_url = $this->config->item('gwgd_url');
$gwgd_url = ($config_gwgd_url ? $config_gwgd_url : 'http://gw.gd/');
$target = $gwgd_url . 'api.php?long=' . $url;
// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'identity');
$resp = curl_exec($ch);
curl_close($ch);
$shorturl = (empty($resp) ? false : $resp);
}
return $shorturl;
}
function checkPaste($seg = 2) function checkPaste($seg = 2)
{ {