Skip to content

Commit 0165955

Browse files
committed
Update for OC8
1 parent 40d119e commit 0165955

File tree

9 files changed

+299
-5
lines changed

9 files changed

+299
-5
lines changed

appinfo/application.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* ownCloud - shorten
4+
*
5+
* This file is licensed under the Affero General Public License version 3 or
6+
* later. See the COPYING file.
7+
*
8+
* @author Ben Curtis <ownclouddev@nosolutions.com>
9+
* @copyright Ben Curtis 2015
10+
*/
11+
12+
namespace OCA\Shorten\AppInfo;
13+
14+
15+
use \OCP\AppFramework\App;
16+
use \OCP\IContainer;
17+
18+
use \OCA\Shorten\Controller\ShortenApiController;
19+
20+
21+
class Application extends App {
22+
23+
24+
public function __construct (array $urlParams=array()) {
25+
parent::__construct('shorten', $urlParams);
26+
27+
$container = $this->getContainer();
28+
29+
/**
30+
* Controllers
31+
*/
32+
$container->registerService('ShortenApiController', function($c){
33+
return new ShortenApiController(
34+
$c->query('AppName'),
35+
$c->query('Request')
36+
);
37+
});
38+
39+
40+
/**
41+
* Core
42+
*/
43+
$container->registerService('UserId', function(IContainer $c) {
44+
return \OCP\User::getUser();
45+
});
46+
47+
}
48+
}

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<types>
1010
<filesystem/>
1111
</types>
12-
<version>0.0.9</version>
12+
<version>0.0.10</version>
1313
<default_enable/>
1414
</info>

appinfo/routes.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* ownCloud - shorten
4+
*
5+
* This file is licensed under the Affero General Public License version 3 or
6+
* later. See the COPYING file.
7+
*
8+
* @author Ben Curtis <ownclouddev@nosolutions.com>
9+
* @copyright Ben Curtis 2015
10+
*/
11+
12+
namespace OCA\Shorten\AppInfo;
13+
14+
/**
15+
* Create your routes in here. The name is the lowercase name of the controller
16+
* without the controller part, the stuff after the hash is the method.
17+
* e.g. page#index -> PageController->index()
18+
*
19+
* The controller class has to be registered in the application.php file since
20+
* it's instantiated in there
21+
*/
22+
$application = new Application();
23+
24+
$application->registerRoutes($this, array('routes' => array(
25+
array('name' => 'shorten_api#makeurl', 'url' => '/makeurl', 'verb' => 'POST'),
26+
array('name' => 'shorten_api#setval', 'url' => '/setval', 'verb' => 'POST'),
27+
array('name' => 'shorten_api#preflighted_cors', 'url' => '/api/v0.2/{path}', 'verb' => 'OPTIONS', 'requirements' => array('path' => '.+')),
28+
)));
29+
30+
$this->create('shorten_code', '/code.php')->action( function($params){ require '/var/www/owncloud/apps/shorten/lib/code.php'; } );
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* ownCloud - shorten
4+
*
5+
* This file is licensed under the Affero General Public License version 3 or
6+
* later. See the COPYING file.
7+
*
8+
* @author Ben Curtis <ownclouddev@nosolutions.com>
9+
* @copyright Ben Curtis 2015
10+
*/
11+
12+
namespace OCA\Shorten\Controller;
13+
14+
use \OCP\AppFramework\ApiController;
15+
use \OCP\AppFramework\Http\JSONResponse;
16+
use \OCP\AppFramework\Http\Response;
17+
use \OCP\AppFramework\Http;
18+
use \OCP\IRequest;
19+
20+
#\OCP\User::checkLoggedIn();
21+
\OCP\App::checkAppEnabled('shorten');
22+
23+
24+
class ShortenApiController extends ApiController {
25+
26+
private $userId;
27+
28+
public function __construct($appName, IRequest $request){
29+
parent::__construct($appName, $request);
30+
}
31+
32+
33+
/**
34+
* @NoAdminRequired
35+
* @CORS
36+
* @NoCSRFRequired
37+
*/
38+
public function makeurl() {
39+
require_once 'shorten/lib/makeurl.php';
40+
return generateUrl();
41+
}
42+
43+
/**
44+
* @NoAdminRequired
45+
* @CORS
46+
* @NoCSRFRequired
47+
*/
48+
public function code() {
49+
require_once 'shorten/lib/code.php';
50+
return runCode();
51+
}
52+
53+
/**
54+
* @CORS
55+
* @NoCSRFRequired
56+
*/
57+
public function setval() {
58+
require_once 'shorten/lib/setval.php';
59+
return setAdminVal();
60+
}
61+
}

js/admin.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@ function endsWith(str, suffix) {
22
return str.indexOf(suffix, str.length - suffix.length) !== -1;
33
}
44

5+
function ocUrl(url) {
6+
var newurl = OC.linkTo("shorten",url).replace("apps/shorten","index.php/apps/shorten");
7+
return newurl;
8+
}
9+
510
$(document).ready(function() {
611
$('#shorten-host-url').change(function() {
712
var val = $(this).val();
813
if (endsWith(val, "/")) {
914
val = val.substring(0, val.length-1);
1015
$(this).val(val);
1116
}
12-
$.post(OC.linkTo("shorten","ajax/setval.php"), { host: val }, function (data) {
17+
$.post(ocUrl("setval"), { host: val }, function (data) {
1318
console.log('response', data);
1419
});
1520
});
1621
$('#shorten-api').change(function() {
1722
var val = $(this).val();
18-
$.post(OC.linkTo("shorten","ajax/setval.php"), { api: val }, function (data) {
23+
$.post(ocUrl("setval"), { api: val }, function (data) {
1924
console.log('response', data);
2025
});
2126
});
2227
$('#shorten-type').change(function() {
2328
var val = $(this).val();
24-
$.post(OC.linkTo("shorten","ajax/setval.php"), { type: val }, function (data) {
29+
$.post(ocUrl("setval"), { type: val }, function (data) {
2530
console.log('response', data);
2631
});
2732
if (val == "internal") {

js/script.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ function replaceUrl() {
4949
}
5050

5151
function makeUrl(curUrl, partUrl) {
52-
$.post(OC.linkTo("shorten","ajax/makeurl.php"), { curUrl: curUrl }, function (data) {
52+
//alert(OC.linkTo("shorten","ajax/makeurl.php"));
53+
//$.post(OC.linkTo("shorten","ajax/makeurl.php"), { curUrl: curUrl }, function (data) {
54+
$.post("/owncloud/index.php/apps/shorten/makeurl", { curUrl: curUrl }, function (data) {
5355
$('#linkText').val(data);
5456
});
5557
}

lib/code.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
\OCP\App::checkAppEnabled('shorten');
4+
5+
function validateCode($string) {
6+
if(preg_match("/^[\w]+$/", $string)) {
7+
return true;
8+
} else {
9+
return false;
10+
}
11+
}
12+
13+
$code = $_GET['code'];
14+
if (validateCode($code)) {
15+
$url = "";
16+
$query = OCP\DB::prepare('SELECT * FROM *PREFIX*shorten WHERE shortcode=?');
17+
$results = $query->execute(Array($code))->fetchAll();
18+
if ($results) {
19+
foreach($results as $result) {
20+
$url = $result['url'];
21+
}
22+
}
23+
if ($url != "") {
24+
//header("Location: ".$url."&download");
25+
$url = $url."/download";
26+
ini_set('user_agent','ownCloud Downloader;');
27+
$headers = get_headers($url);
28+
$binary = false;
29+
foreach ($headers as $h) {
30+
if ($h == "Content-Transfer-Encoding: binary")
31+
$binary = true;
32+
}
33+
if ($binary) {
34+
foreach ($headers as $h) {
35+
header($h);
36+
}
37+
$f = fopen($url, 'r');
38+
while(!feof($f)){
39+
print fgets($f, 1024);
40+
}
41+
fclose($f);
42+
} else {
43+
header("Location: ".$url);
44+
}
45+
} else {
46+
echo "Invalid link.";
47+
}
48+
} else {
49+
echo "Invalid link.";
50+
}

lib/makeurl.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
\OCP\User::checkLoggedIn();
4+
\OCP\App::checkAppEnabled('shorten');
5+
6+
function startsWith($haystack, $needle) {
7+
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
8+
}
9+
10+
function rand_chars($length) {
11+
$urlString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
12+
$arr = str_split($urlString);
13+
shuffle($arr);
14+
$arr = array_slice($arr, 0, $length);
15+
$str = implode('', $arr);
16+
return $str;
17+
}
18+
19+
function getShortcode($url) {
20+
$shortcode = '';
21+
$query = OCP\DB::prepare('SELECT shortcode FROM *PREFIX*shorten WHERE url=?');
22+
$results = $query->execute(Array($url))->fetchAll();
23+
if ($results) {
24+
foreach($results as $result) {
25+
$shortcode = $result['shortcode'];
26+
}
27+
}
28+
if ($shortcode == "") {
29+
$shortcode = rand_chars(6);
30+
$found = true;
31+
while ($found) {
32+
$query = OCP\DB::prepare('SELECT id FROM *PREFIX*shorten WHERE shortcode=?');
33+
$results = $query->execute(Array($shortcode))->fetchAll();
34+
if (!$results) {
35+
$found = false;
36+
$uid = \OCP\User::getUser();
37+
$query = OCP\DB::prepare('INSERT INTO *PREFIX*shorten (uid, shortcode, url) VALUES (?,?,?)');
38+
$query->execute(Array($uid,$shortcode,$url));
39+
$id = OCP\DB::insertid('*PREFIX*shorten');
40+
} else
41+
$shortcode = rand_chars(6);
42+
}
43+
}
44+
return $shortcode;
45+
}
46+
47+
function generateUrl() {
48+
//$newHost = "https://nowsci.com/s/";
49+
$host = OCP\Config::getAppValue('shorten', 'host', '');
50+
$type = OCP\Config::getAppValue('shorten', 'type', '');
51+
$api = OCP\Config::getAppValue('shorten', 'api', '');
52+
$curUrl = $_POST['curUrl'];
53+
$ret = "";
54+
if (isset($type) && ($type == "" || $type == "internal")) {
55+
if ($host == "" || startsWith($curUrl, $host)) {
56+
$ret = $curUrl;
57+
} else {
58+
$shortcode = getShortcode($curUrl);
59+
$newUrl = $host."?".$shortcode;
60+
$ret = $newUrl;
61+
}
62+
} elseif ($type == "googl") {
63+
if ($api && $api != "") {
64+
require_once __DIR__ . '/../lib/class.googl.php';
65+
$googl = new googl($api);
66+
$short = $googl->s($curUrl);
67+
$ret = $short;
68+
} else {
69+
$ret = $curUrl;
70+
}
71+
} else {
72+
$ret = $curUrl;
73+
}
74+
return $ret;
75+
}
76+
77+
?>

lib/setval.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
OCP\User::checkAdminUser();
5+
\OCP\App::checkAppEnabled('shorten');
6+
7+
function setAdminVal() {
8+
if (isset($_POST['host'])) {
9+
OCP\Config::setAppValue('shorten', 'host', $_POST['host']);
10+
echo "host:".$_POST['host'];
11+
}
12+
if (isset($_POST['api'])) {
13+
OCP\Config::setAppValue('shorten', 'api', $_POST['api']);
14+
echo "api:".$_POST['api'];
15+
}
16+
if (isset($_POST['type'])) {
17+
OCP\Config::setAppValue('shorten', 'type', $_POST['type']);
18+
echo "type:".$_POST['type'];
19+
}
20+
}
21+
?>

0 commit comments

Comments
 (0)