|
| 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 | +?> |
0 commit comments