require_once("conf/dokuwiki.php");
require_once("inc/common.php");
/**
* Assembles all parts defined by the link formater below
* Returns HTML for the link
*/
function format_link_build($link){
$ret = '';
$ret .= $link['pre'];
$ret .= ' with proper special char encoding
* $link['class'] CSS class to set on link
* $link['target'] which target to use (blank) for current window
* $link['style'] Additonal style attribute set with style=""
* $link['pre'] HTML to prepend to link
* $link['suf'] HTML to append to link
* $link['more'] Additonal HTML to include into the anchortag
*
*/
function format_link_wiki($link){
global $conf;
global $ID; //we use this to get the current namespace
//obvious setup
$link['target'] = $conf['target']['wiki'];
$link['style'] = '';
$link['pre'] = '';
$link['suf'] = '';
$link['more'] = 'onclick="return svchk()"';
//if link contains no namespace. add current namespace (if any)
if(strpos($ID,':')!==false && strpos($link['url'],':') === false){
$ns = substr($ID,0,strrpos($ID,':')); #get current ns
$link['url'] = $ns.':'.$link['url'];
}
//keep hashlink if exists
list($link['url'],$hash) = split('#',$link['url'],2);
$hash = cleanID($hash);
//use link WITH namespace as name
if(empty($link['name'])) $link['name'] = $link['url'];
$link['name'] = htmlspecialchars($link['name']);
//set class depending on existance
$link['url'] = cleanID($link['url']);
$file = wikiFN($link['url']);
if(file_exists($file)){
$link['class']="wikilink1";
}else{
$link['class']="wikilink2";
}
//construct the full link
$link['url'] = wl($link['url']);
//add hash if exists
if($hash) $link['url'] .= '#'.$hash;
return $link;
}
function format_link_externalurl($link){
global $conf;
//simple setup
$link['class'] = 'urlextern';
$link['target'] = $conf['target']['extern'];
$link['pre'] = '';
$link['suf'] = '';
$link['style'] = '';
$link['more'] = 'onclick="return svchk()"';
$link['url'] = $link['url']; //keep it
if(!$link['name']) $link['name'] = htmlspecialchars($link['url']);
//thats it :-)
return $link;
}
//this only works in IE :-(
function format_link_windows($link){
global $conf;
global $lang;
//simple setup
$link['class'] = 'windows';
$link['target'] = $conf['target']['windows'];
$link['pre'] = '';
$link['suf'] = '';
$link['style'] = '';
//Display error on browsers other than IE
$link['more'] = 'onclick="if(document.all == null){alert(\''.htmlspecialchars($lang['nosmblinks'],ENT_QUOTES).'\');}"';
if(!$link['name']) $link['name'] = htmlspecialchars($link['url']);
$link['url'] = str_replace('\\','/',$link['url']);
$link['url'] = 'file:///'.$link['url'];
return $link;
}
function format_link_email($link){
global $conf;
//simple setup
$link['class'] = 'mail';
$link['target'] = '';
$link['pre'] = '';
$link['suf'] = '';
$link['style'] = '';
$link['more'] = '';
$link['name'] = htmlspecialchars($link['name']);
//shields up
if($conf['mailguard']=='visible'){
//the mail name gets some visible encoding
$link['url'] = str_replace('@',' [at] ',$link['url']);
$link['url'] = str_replace('.',' [dot] ',$link['url']);
$link['url'] = str_replace('-',' [dash] ',$link['url']);
}elseif($conf['mailguard']=='hex'){
for ($x=0; $x < strlen($link['url']); $x++) {
$encode .= '' . bin2hex($link['url'][$x]).';';
}
$link['url'] = $encode;
}
if(!$link['name']) $link['name'] = $link['url'];
$link['url'] = 'mailto:'.$link['url'];
return $link;
}
function format_link_interwiki($link){
global $conf;
//obvious ones
$link['class'] = 'interwiki';
$link['target'] = $conf['target']['interwiki'];
$link['pre'] = '';
$link['suf'] = '';
$link['more'] = 'onclick="return svchk()"';
//get interwiki short name
list($wiki,$link['url']) = split('>',$link['url'],2);
$wiki = strtolower(trim($wiki)); //always use lowercase
$link['url'] = trim($link['url']);
if(!$link['name']) $link['name'] = $link['url'];
//encode special chars
$link['name'] = htmlspecialchars($link['name']);
//set default to google
$url = 'http://www.google.com/search?q=';
$ico = 'google';
//load interwikilinks
//FIXME: loading this once may enhance speed a little bit
$iwlinks = file('conf/interwiki.conf');
//add special case 'this'
$iwlinks[] = 'this '.getBaseURL(true).'{NAME}';
//go through iwlinks and find URL for wiki
foreach ($iwlinks as $line){
$line = preg_replace('/#.*/','',$line); //skip comments
$line = trim($line);
list($iw,$iwurl) = preg_split('/\s+/',$line);
if(!$iw or !$iwurl) continue; //skip broken or empty lines
//check for match
if(strtolower($iw) == $wiki){
$ico = $wiki;
$url = $iwurl;
break;
}
}
//if ico exists set additonal style
if(file_exists('interwiki/'.$ico.'.png')){
$link['style']='background: transparent url(interwiki/'.$ico.'.png) 0px 1px no-repeat;';
}elseif(file_exists('interwiki/'.$ico.'.gif')){
$link['style']='background: transparent url(interwiki/'.$ico.'.gif) 0px 1px no-repeat;';
}
//do we stay at the same server? Use local target
if( strpos($url,getBaseURL(true)) === 0 ){
$link['target'] = $conf['target']['wiki'];
}
//replace placeholder
if(strstr($url,'{URL}') !== false){
$link['url'] = str_replace('{URL}',urlencode($link['url']),$url);
}elseif(strstr($url,'{NAME}') !== false){
$link['url'] = str_replace('{NAME}',$link['url'],$url);
}else{
$link['url'] = $url.urlencode($link['url']);
}
//done :-)
return $link;
}
function format_link_media($link){
global $conf;
$link['class'] ="media";
$link['style'] = '';
$link['pre'] = '';
$link['suf'] = '';
$link['more'] = 'onclick="return svchk()"';
list($link['name'],$title) = split('\|',$link['name'],2);
$t = htmlspecialchars($title);
//set alignment from spaces
if(substr($link['name'],0,1)==' ' && substr($link['name'],-1,1)==' '){
$link['pre'] = "
\n";
$link['suf'] = "
\n";
}elseif(substr($link['name'],0,1)==' '){
$a = 'align="right"';
}elseif(substr($link['name'],-1,1)==' '){
$a = 'align="left"';
}else{
$a = 'align="middle"';
}
$link['name'] = trim($link['name']);
//split into src and parameters
list($src,$param) = split('\?',$link['name'],2);
//parse width and height
if(preg_match('#(\d*)(x(\d*))?#i',$param,$size)){
if($size[1]) $w = $size[1];
if($size[3]) $h = $size[3];
}
//check for nocache param
$nocache = preg_match('/nocache/i',$param);
//do image caching, resizing and src rewriting
$cache = $src;
$isimg = img_cache($cache,$src,$w,$h,$nocache);
//set link to src if none given
if(!$link['url']){
$link['url'] = $src;
$link['target'] = $conf['target']['media'];
}
//prepare name
if($isimg){
$link['name'] = '';
if($rss){
foreach ($rss->items as $item ) {
$link = array();
$link['url'] = $item['link'];
$link['name'] = $item['title'];
$link = format_link_externalurl($link);
$ret .= '
'.format_link_build($link).'';
}
}else{
$link['url'] = $url;
$link = format_link_externalurl($link);
$ret .= '';
$ret .= ''.$lang['rssfailed'].'';
$ret .= format_link_build($link);
$ret .= '';
}
$ret .= '';
return $ret;
}
function img_cache(&$csrc,&$src,&$w,&$h,$nocache){
global $conf;
//container for various paths
$f['full']['web'] = $src;
$f['resz']['web'] = $src;
$f['full']['fs'] = $src;
$f['resz']['fs'] = $src;
//generate cachename
$md5 = md5($src);
//check if it is an image
if(preg_match('#\.(jpe?g|gif|png)$#i',$src,$match)){
$ext = strtolower($match[1]);
$isimg = true;
}
//check if it is external or a local mediafile
if(preg_match('#^([a-z0-9]+?)://#i',$src)){
$isurl = true;
}else{
$src = str_replace(':','/',$src);
$f['full']['web'] = $conf['mediaweb'].'/'.$src;
$f['resz']['web'] = $conf['mediaweb'].'/'.$src;
$f['full']['fs'] = $conf['mediadir'].'/'.$src;
$f['resz']['fs'] = $conf['mediadir'].'/'.$src;
}
//download external images if allowed
if($isurl && $isimg && !$nocache){
$cache = $conf['mediadir']."/.cache/$md5.$ext";
if (file_exists($cache) || download($src,$cache)){
$f['full']['web'] = $conf['mediaweb']."/.cache/$md5.$ext";
$f['resz']['web'] = $conf['mediaweb']."/.cache/$md5.$ext";
$f['full']['fs'] = $conf['mediadir']."/.cache/$md5.$ext";
$f['resz']['fs'] = $conf['mediadir']."/.cache/$md5.$ext";
$isurl = false;
}
}
//for local images (cached or media) do resizing
if($isimg && (!$isurl) && $w){
$info = getImageSize($f['full']['fs']);
//if $h not given calcualte it with correct aspect ratio
if(!$h){
$h = round(($w * $info[1]) / $info[0]);
}
$cache = $conf['mediadir'].'/.cache/'.$md5.'.'.$w.'x'.$h.'.'.$ext;
//delete outdated cachefile
if(file_exists($cache) && (filemtime($cache)