1<?php 2 3/** 4 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 5 * @author Esther Brunner <wikidesign@gmail.com> 6 */ 7 8// must be run within Dokuwiki 9if (!defined('DOKU_INC')) die(); 10 11if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 12if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 13 14 15class helper_plugin_shorturl extends DokuWiki_Plugin { 16 17 var $configtocache = ''; 18 var $savedir = ''; 19 20 /** 21 * Constructor gets default preferences and language strings 22 */ 23 function helper_plugin_shorturl() { 24 global $ID, $conf; 25 26 $this->configtocache = $this->getConf('saveconftocachedir'); 27 28 if ( $this->configtocache ) { 29 $this->savedir = rtrim($conf['savedir'],"/") . "/cache"; 30 } else { 31 $this->savedir = dirname(__FILE__); 32 } 33 34 } 35 36 37 function getInfo() { 38 return array( 39 40 ); 41 } 42 43 function getMethods() { 44 $result = array(); 45 $result[] = array( 46 'name' => 'autoGenerateShortUrl', 47 'desc' => 'returns the short url if exists, otherwise create the short url', 48 'return' => array('shortID' => 'string'), 49 ); 50 $result[] = array( 51 'name' => 'shorturlPrintLink', 52 'desc' => 'returns a link to the short url if it exists, otherwise a link to create the short url', 53 'return' => array('html' => 'string'), 54 ); 55 return $result; 56 } 57 58 /** 59 * returns shortID for pageID 60 * creates and saves forwarding to shortID if not 61 */ 62 function autoGenerateShortUrl ($pageID) { 63 $redirects = confToHash($this->savedir.'/shorturl.conf'); 64 if (in_array($pageID, $redirects)) { 65 $shortID = array_search($pageID, $redirects); 66 } else { 67 $shortID = $this->generateShortUrl($pageID); 68 } 69 return $shortID; 70 } 71 72 /** 73 * generates short page id from current page id 74 * 75 * @author Frank Schiebel <frank@linuxmuster.net> 76 * @param none 77 * @return string shortid 78 * @url http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/ 79 * 80 **/ 81 function generateShortUrl($pageID) { 82 $output = array(); 83 $base32 = array ( 84 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 85 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 86 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 87 'y', 'z', '0', '1', '2', '3', '4', '5' 88 ); 89 90 $hex = md5($pageID); 91 $hexLen = strlen($hex); 92 $subHexLen = $hexLen / 8; 93 94 for ($i = 0; $i < $subHexLen; $i++) { 95 $subHex = substr ($hex, $i * 8, 8); 96 $int = hexdec('0x'.$subHex); 97 $out = ''; 98 99 for ($j = 0; $j < 6; $j++) { 100 $val = 0x0000001F & $int; 101 $out .= $base32[$val]; 102 $int = $int >> 5; 103 } 104 105 $output[] = $out; 106 } 107 108 // save redirect to file 109 $redirects = confToHash($this->savedir.'/shorturl.conf'); 110 // check for duplicates in database and select alternative shorty when needed 111 $shorturl = $output[0]; 112 for ($j = 0; $j < 6; $j++) { 113 if ( $redirects["$shorturl"] && $redirects["$shorturl"] != $pageID ) { 114 $shorturl = $output[$j+1]; 115 } 116 } 117 $redirects["$shorturl"] = $pageID; 118 $filecontents = ""; 119 foreach ( $redirects as $short => $long ) { 120 $filecontents .= $short . " " . $long . "\n"; 121 } 122 io_saveFile($this->savedir.'/shorturl.conf',$filecontents); 123 124 return $shorturl; 125 126 } 127 128 /** 129 * if a short id exists in db: get it 130 * 131 * @author Frank Schiebel <frank@linuxmuster.net> 132 * @param none 133 * @return string regular id 134 * 135 */ 136 function shorturlPrintLink ($pageID) { 137 138 if (file_exists($this->savedir.'/shorturl.conf') ) { 139 $redirects = confToHash($this->savedir.'/shorturl.conf'); 140 } else { 141 $redirects = array(); 142 } 143 144 if (in_array($pageID, $redirects)) { 145 $shortID = array_search($pageID, $redirects); 146 $linktext = $this->getLang('shortlinktext'); 147 return '<a href="' . wl($shortID, "", true) . '"> ' . $linktext . '</a>' ; 148 } else { 149 $linktext = $this->getLang('generateshortlink'); 150 return'<a href="' . wl($pageID, array(generateShortURL=>yes), true) . '"> ' . $linktext . '</a>' ; 151 } 152 } 153 154 155} 156// vim:ts=4:sw=4:et:enc=utf-8: 157