Project

General

Profile

1
<?php
2
/*
3
 * @param  string $content : contains the full content of the current page
4
 * @return string
5
 * @description replace all "[wblink{page_id}{?addon=n&item=n...}]" with real links to accessfiles<br />
6
 *     All modules must offer the class 'WbLink'(implementing 'WbLinkAbstract'), to be taken into consideration.
7
 */
8
	function doFilterWbLink($sContent)
9
	{
10
		$oDb  = WbDatabase::getInstance();
11
		$oReg = WbAdaptor::getInstance();
12
		$aReplaceList = array();
13
		$sPattern = '/\[wblink([0-9]+)\??([^\]]*)\]/is';
14
		if(preg_match_all($sPattern,$sContent,$aMatches))
15
		{
16
		// iterate through all found matches
17
			foreach($aMatches[0] as $iKey => $sKeyString )
18
			{
19
				$aReplaceList[$sKeyString] = array();
20
			// use original Tag to save PageId
21
				$aReplaceList[$sKeyString]['PageId'] = $aMatches[1][$iKey];
22
			// if there are additional arguments given
23
				if($aMatches[2][$iKey])
24
				{
25
					$aReplaceList[$sKeyString]['Args'] = array();
26
					$aArgs = explode('&', $aMatches[2][$iKey]);
27
					foreach($aArgs as $sArgument)
28
					{
29
						$aTmp = explode('=', $sArgument);
30
						$aReplaceList[$sKeyString]['Args'][$aTmp[0]] = $aTmp[1];
31
					}
32
				}
33
			}
34
			if(sizeof($aReplaceList) > 0)
35
			{ // iterate list if replacements are available
36
				foreach($aReplaceList as $sKey => $aReplacement)
37
				{
38
				// set link on failure ('#' means, still stay on current page)
39
					$aReplaceList[$sKey] = '#';
40
				// handle normal pages links
41
					if(!isset($aReplacement['Args'])) 
42
					{
43
					// read corresponding link from table 'pages'
44
						$sql = 'SELECT `link` FROM `'.$oDb->TablePrefix.'pages` WHERE `page_id` = '.(int)$aReplacement['PageId'];
45
						if(($sLink = $oDb->get_one($sql)))
46
						{
47
							$sLink = trim(str_replace('\\', '/', $sLink), '/');
48
						// test if valid accessfile is available
49
							if(is_readable($oReg->AppPath.$oReg->PagesDir.$sLink.$oReg->PageExtension))
50
							{
51
							// create absolute URL
52
								$aReplaceList[$sKey] = $oReg->AppUrl.$oReg->PagesDir.$sLink.$oReg->PageExtension;
53
							}
54
						}
55
					// handle links of modules
56
					}else 
57
					{
58
					// build name of the needed class
59
						$sClass = 'm_'.$aReplacement['Args']['addon'].'_WbLink';
60
					// remove addon name from replacement array
61
						unset($aReplacement['Args']['addon']);
62
						if(class_exists($sClass))
63
						{
64
						// instantiate class
65
							$oWbLink = new $sClass();
66
						// the class must implement the interface
67
							if($oWbLink instanceof WbLinkAbstract)
68
							{
69
							// create real link from replacement data
70
								$aReplaceList[$sKey] = $oWbLink->makeLinkFromTag($aReplacement);
71
							}
72
						}
73
					}
74
				}
75
			// extract indexes into a new array
76
				$aSearchList = array_keys($aReplaceList);
77
			// replace all identified wblink-tags in content
78
				$sContent = str_replace($aSearchList, $aReplaceList, $sContent);
79
			}
80
		}
81
		return $sContent;
82
	}
(6-6/6)