1<?php 2/** 3 * DokuWiki Plugin shorty (Helper Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Fernando Ribeiro <pinguim.ribeiro@gmail.com> 7 */ 8 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11 12class helper_plugin_shorty extends DokuWiki_Plugin { 13 14var $savedir = ''; 15 16 /** 17 * Constructor gets default preferences and language strings 18 */ 19 function helper_plugin_shorty() { 20 global $conf; 21 22 $this->savedir = rtrim($conf['savedir'],"/") . "/cache"; 23 } 24 25 /** 26 * Return info about supported methods in this Helper Plugin 27 * 28 * @return array of public methods 29 */ 30 public function getMethods() { 31 return array( 32 array( 33 'name' => 'getShortUrl', 34 'desc' => 'returns the short url if exists, otherwise creates the short url', 35 'params' => array( 36 'ID' => 'string', 37 'service (optional)' => 'string' 38 ), 39 'return' => 'string' 40 ), 41 array( 42 // and more supported methods... 43 ) 44 ); 45 } 46 47 48 /** 49 * Generates a short url for the pageID using the required shortening service 50 * and adds the url to the database 51 * 52 * @return the shorturl or false in case of error 53 */ 54 function getShortUrl($pageID, $service='default') { 55 56 if ($service == 'default') 57 $service = $this->getconf('default_service'); 58 59 switch ($service) { 60 case 'bit.ly': 61 case 'bitly.com': 62 case 'j.mp': 63 $shortURL = $this->getBitlyURL($pageID, $service); 64 break; 65 case 'tinyurl': 66 $shortURL = $this->getTinyurlURL($pageID); 67 break; 68 case 'yourls': 69 $shortURL = $this->getyourlsURL($pageID); 70 break; 71 default: 72 $shortURL = false; 73 } 74 return $shortURL; 75 } 76 77 78 79 /** 80 * Generates a short url using the Bit.ly API 81 * 82 * @return the shorturl or false in case of error 83 */ 84 function getBitlyURL($pageID, $domain) { 85 // checks if the short url already exists in the database 86 $url = $this->readShortUrl($pageID, $domain); 87 if ($url == false ) { 88 // calls the service API to generate a short url 89 $longUrl = rawurlencode(wl($pageID,'',true)); 90 91 $uri = $this->getConf('bitly_oauth_api'); 92 $uri .= "shorten?access_token=" . $this->getConf('bitly_oauth_access_token'); 93 $uri .= "&format=json"; 94 $uri .= "&domain=" . $domain; 95 $uri .= "&longUrl=" . $longUrl; 96 97 $output = json_decode($this->getCurl($uri)); 98 99 if ($output->status_txt != "OK") { 100 return false; 101 } else{ 102 $url = $output->{'data'}->{'url'}; 103 // saves the new short url to the database 104 $this->writeShortUrl($pageID, $url, $domain); 105 } 106 } 107 return $url; 108 } 109 110 /** 111 * Generates a short url using the yourls API 112 * 113 * @return the shorturl or false in case of error 114 */ 115 function getyourlsURL($pageID) { 116 // checks if the short url already exists in the database 117 $url = $this->readShortUrl($pageID, 'yourls'); 118 if ($url == false ) { 119 // calls the service API to generate a short url 120 $longUrl = rawurlencode(wl($pageID,'',true)); 121 122 $uri = $this->getConf('yourls_url'); 123 $uri .= "/yourls-api.php?signature=" . $this->getConf('yourls_token'); 124 $uri .= "&action=shorturl&format=simple"; 125 $uri .= "&url=" . $longUrl; 126 127 $http = new DokuHTTPClient(); 128 $url = $http->get($uri); 129 // saves the new short url to the database 130 $this->writeShortUrl($pageID, $url, 'yourls'); 131 132 133 } 134 return $url; 135 } 136 137 138 /** 139 * Generates a short url using the Tinyurl API 140 * 141 * @return the shorturl or false in case of error 142 */ 143 function getTinyurlURL($pageID) { 144 // checks if the short url already exists in the database 145 $url = $this->readShortUrl($pageID, 'tinyurl'); 146 if ($url == false ) { 147 // calls the service API to generate a short url 148 $longUrl = rawurlencode(wl($pageID,'',true)); 149 $http = new DokuHTTPClient(); 150 $url = $http->get('http://tinyurl.com/api-create.php?url='.$longUrl); 151 // saves the new short url to the database 152 $this->writeShortUrl($pageID, $url, 'tinyurl'); 153 } 154 return $url; 155 } 156 157 158 159 /** 160 * reads shortURL for pageID from file 161 */ 162 function readShortUrl ($pageID, $file) { 163 $redirects = confToHash($this->savedir.'/'.$file.'.conf'); 164 if (in_array($pageID, $redirects)) { 165 $shortURL = array_search($pageID, $redirects); 166 } else { 167 $shortURL = false; 168 } 169 return $shortURL; 170 } 171 172 173 /** 174 * writes shortID for pageID to file 175 */ 176 function writeShortUrl ($pageID, $shortURL, $file) { 177 $redirects = confToHash($this->savedir.'/'.$file.'.conf'); 178 // check for duplicates in database and select alternative shorty when needed 179 $url = $output[0]; 180 for ($j = 0; $j < 6; $j++) { 181 if ( $redirects["$url"] && $redirects["$url"] != $pageID ) { 182 $url = $output[$j+1]; 183 } 184 } 185 $redirects["$shortURL"] = $pageID; 186 $filecontents = ""; 187 foreach ( $redirects as $short => $long ) { 188 $filecontents .= $short . " " . $long . "\n"; 189 } 190 io_saveFile($this->savedir.'/'.$file.'.conf',$filecontents); 191 } 192 193 194 /** 195 * Make a GET call using cURL. 196 * 197 * from https://github.com/Falicon/BitlyPHP/blob/master/bitly.php 198 * 199 * @param $uri 200 * URI to call. 201 */ 202 function getCurl($uri) { 203 $output = ""; 204 try { 205 $ch = curl_init($uri); 206 curl_setopt($ch, CURLOPT_HEADER, 0); 207 curl_setopt($ch, CURLOPT_TIMEOUT, 4); 208 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); 209 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 210 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 211 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 212 $output = curl_exec($ch); 213 } catch (Exception $e) { 214 } 215 return $output; 216 } 217 218} 219 220// vim:ts=4:sw=4:et: 221