diff --git a/lib/Controller.php b/lib/Controller.php
index 62fa1e99..8d7f0f13 100644
--- a/lib/Controller.php
+++ b/lib/Controller.php
@@ -136,6 +136,9 @@ class Controller
case 'jsonld':
$this->_jsonld($this->_request->getParam('jsonld'));
return;
+ case 'yourlsproxy':
+ $this->_yourlsproxy($this->_request->getParam('link'));
+ break;
}
// output JSON or HTML
@@ -378,9 +381,15 @@ class Controller
);
$page = new View;
+ $page->assign('CSPHEADER', $metacspheader);
+ $page->assign('ERROR', I18n::_($this->_error));
+ if ($this->_request->getOperation() === 'yourlsproxy') {
+ $page->assign('SHORTURL', $this->_status);
+ $page->draw('yourlsproxy');
+ return;
+ }
$page->assign('NAME', $this->_conf->getKey('name'));
$page->assign('BASEPATH', I18n::_($this->_conf->getKey('basepath')));
- $page->assign('ERROR', I18n::_($this->_error));
$page->assign('STATUS', I18n::_($this->_status));
$page->assign('VERSION', self::VERSION);
$page->assign('DISCUSSION', $this->_conf->getKey('discussion'));
@@ -405,7 +414,6 @@ class Controller
$page->assign('HTTPWARNING', $this->_conf->getKey('httpwarning'));
$page->assign('HTTPSLINK', 'https://' . $this->_request->getHost() . $this->_request->getRequestUri());
$page->assign('COMPRESSION', $this->_conf->getKey('compression'));
- $page->assign('CSPHEADER', $metacspheader);
$page->draw($this->_conf->getKey('template'));
}
@@ -439,6 +447,22 @@ class Controller
echo $content;
}
+ /**
+ * proxies link to YOURLS, updates status or error with response
+ *
+ * @access private
+ * @param string $link
+ */
+ private function _yourlsproxy($link)
+ {
+ $yourls = new YourlsProxy($this->_conf, $link);
+ if ($yourls->isError()) {
+ $this->_error = $yourls->getError();
+ } else {
+ $this->_status = $yourls->getUrl();
+ }
+ }
+
/**
* prepares JSON encoded status message
*
diff --git a/lib/Request.php b/lib/Request.php
index 2eff8a96..9ed73074 100644
--- a/lib/Request.php
+++ b/lib/Request.php
@@ -120,6 +120,7 @@ class Request
if (
!array_key_exists('pasteid', $this->_params) &&
!array_key_exists('jsonld', $this->_params) &&
+ !array_key_exists('link', $this->_params) &&
array_key_exists('QUERY_STRING', $_SERVER) &&
!empty($_SERVER['QUERY_STRING'])
) {
@@ -135,6 +136,11 @@ class Request
}
} elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) {
$this->_operation = 'jsonld';
+ } elseif (array_key_exists('link', $this->_params) && !empty($this->_params['link'])) {
+ $request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
+ if (strpos($request_url, '/shortenviayourls?link=') !== false) {
+ $this->_operation = 'yourlsproxy';
+ }
}
}
diff --git a/lib/YourlsProxy.php b/lib/YourlsProxy.php
new file mode 100644
index 00000000..6cc0f24e
--- /dev/null
+++ b/lib/YourlsProxy.php
@@ -0,0 +1,120 @@
+getKey('basepath') . '/?') !== false) {
+ // Init the CURL session
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $conf->getKey("apiurl", "yourls"));
+ curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
+ curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
+ curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
+ 'signature' => $conf->getKey("signature", "yourls"),
+ 'format' => 'json',
+ 'action' => 'shorturl',
+ 'url' => $link
+ ));
+ // Fetch and return content
+ $data = curl_exec($ch);
+ curl_close($ch);
+
+ if (!($data === FALSE) && is_string($data))
+ {
+ $data = json_decode( $data, true);
+
+ if (!is_null($data) && array_key_exists('statusCode', $data)
+ && array_key_exists('shorturl', $data) && ($data['statusCode'] == 200))
+ {
+ $this->_url = $data['shorturl'];
+ $opSuccess = TRUE;
+ } else {
+ $this->_error = 'Error parsing YOURLS response.';
+ }
+ } else {
+ $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
+ }
+ } else {
+ $this->_error = 'Trying to shorten a URL not pointing to our PrivateBin instance.';
+ }
+ }
+
+ /**
+ * Returns the (untranslated) error message
+ *
+ * @access public
+ * @return string
+ */
+ public function getError()
+ {
+ return $this->_error;
+ }
+
+ /**
+ * Returns the shortened URL
+ *
+ * @access public
+ * @return string
+ */
+ public function getUrl()
+ {
+ return $this->_url;
+ }
+
+ /**
+ * Returns true if any error has occurred
+ *
+ * @access public
+ * @return bool
+ */
+ public function isError()
+ {
+ return !empty($this->_error);
+ }
+}
diff --git a/shortenviayourls.php b/shortenviayourls.php
deleted file mode 100644
index 28022819..00000000
--- a/shortenviayourls.php
+++ /dev/null
@@ -1,85 +0,0 @@
-getKey( "basepath") . "/?")) {
-
- // Init the CURL session
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $conf->getKey( "apiurl", "yourls"));
- curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
- curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
- curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
- 'signature' => $conf->getKey( "signature", "yourls"),
- 'format' => 'json',
- 'action' => 'shorturl',
- 'url' => $originalUrl
- ));
- // Fetch and return content
- $data = curl_exec($ch);
- curl_close($ch);
- if (!($data === FALSE) && is_string($data))
- {
- $data = json_decode( $data, true);
-
- if (!is_null($data) && array_key_exists('statusCode', $data)
- && array_key_exists('shorturl', $data) && ($data['statusCode'] == 200))
- {
- $shortenedUrl = $data['shorturl'];
- $opSuccess = TRUE;
- } else {
- // error with contents of YOURLS response.
- $errCode = 3;
- }
- } else {
- // error when calling YOURLS - probably a PrivateBin configuration issue, like wrong/missing apiurl or signature
- $errCode = 2;
- }
- } else {
- // trying to shorten a URL not pointing to our PrivateBin instance.
- $errCode = 1;
- }
-}
-
-if ($opSuccess)
-{
- print("
Your shortened paste is $shortenedUrl");
-}
-else
-{
- print("
Error: An error occured while trying to shorten the given URL (error code $errCode)");
-}
-
-function getGetData() {
- $data = http_build_query($_GET);
- return $data;
-}
-
-function startsWith($haystack, $needle)
-{
- $length = strlen($needle);
- return (substr($haystack, 0, $length) === $needle);
-}
-?>
diff --git a/tpl/yourlsproxy.php b/tpl/yourlsproxy.php
new file mode 100644
index 00000000..c451c5a2
--- /dev/null
+++ b/tpl/yourlsproxy.php
@@ -0,0 +1,25 @@
+
+
+
%s (Hit [Ctrl]+[c] to copy)', $SHORTURL, $SHORTURL); ?>
+ + + + +