From c69548107cad7eb250bd9d38c6dcc855391f0511 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 8 Nov 2013 15:14:27 +0100 Subject: [PATCH] YOURLS support. issue #6 --- htdocs/application/config/stikked.php.default | 16 ++++- htdocs/application/models/pastes.php | 70 ++++++++++++++----- 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/htdocs/application/config/stikked.php.default b/htdocs/application/config/stikked.php.default index a6dbe0c..9f63304 100644 --- a/htdocs/application/config/stikked.php.default +++ b/htdocs/application/config/stikked.php.default @@ -68,12 +68,24 @@ $config['combine_assets'] = false; $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/ * **/ +$config['yourls_url'] = ''; +$config['yourls_signature'] = ''; $config['gwgd_url'] = ''; /** diff --git a/htdocs/application/models/pastes.php b/htdocs/application/models/pastes.php index 2e380eb..f8a00f0 100644 --- a/htdocs/application/models/pastes.php +++ b/htdocs/application/models/pastes.php @@ -7,6 +7,7 @@ * - countReplies() * - createPaste() * - _get_url() + * - _shorten_url() * - checkPaste() * - getPaste() * - calculate_hits() @@ -121,22 +122,8 @@ class Pastes extends CI_Model else { $url = $this->_get_url($data['pid']); - $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; - $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; - } + $shorturl = $this->_shorten_url($url); + $data['snipurl'] = $shorturl; } $data['ip_address'] = $this->input->ip_address(); $this->db->insert('pastes', $data); @@ -148,6 +135,57 @@ class Pastes extends CI_Model $override_url = $this->config->item('displayurl_override'); 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) {