wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.LinkConverter.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.LinkConverter.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/core/class.WCMFException.php");
20 require_once(BASE."wcmf/lib/persistence/converter/class.DataConverter.php");
21 require_once(BASE."wcmf/lib/util/class.URIUtil.php");
22 require_once(BASE."wcmf/lib/util/class.InifileParser.php");
23 
24 // this is stored in a global variable to allow static method calls
26 
27 /**
28  * @class LinkConverter
29  * @ingroup Converter
30  * @brief LinkConverter converts internal absolute urls to relative ones when saving
31  * to the database and vice versa. Since StringUtil::getUrls() is used to
32  * detect urls, only urls in hmtl elements are detected when converting from
33  * storage to application (relative to absolute). If the url is not in an
34  * html element (e.g. if the whole data is an url) the conversion works only
35  * from application to storage (absolute to relative).
36  *
37  * @author ingo herwig <ingo@wemove.com>
38  */
40 {
41  /**
42  * @see DataConverter::convertStorageToApplication()
43  */
44  function convertStorageToApplication($data, $type, $name)
45  {
46  $urls = StringUtil::getUrls($data);
47  foreach ($urls as $url)
48  {
49  if ($url != '#' && !LinkConverter::isExternalUrl($url))
50  {
51  // convert relative url
52  $urlConv = LinkConverter::makeUrlAbsolute($url);
53 
54  // replace url
55  $data = str_replace('"'.$url.'"', '"'.$urlConv.'"', $data);
56  }
57  }
58 
59  return $data;
60  }
61  /**
62  * @see DataConverter::convertApplicationToStorage()
63  */
64  function convertApplicationToStorage($data, $type, $name)
65  {
66  $urls = StringUtil::getUrls($data);
67  foreach ($urls as $url)
68  {
69  if ($url != '#' && !LinkConverter::isExternalUrl($url))
70  {
71  // convert absolute url
72  $urlConv = LinkConverter::makeUrlRelative($url);
73 
74  // replace url
75  $data = str_replace('"'.$url.'"', '"'.$urlConv.'"', $data);
76  }
77  }
78 
79  // convert if whole data is an url
80  if ($url != '#' && !LinkConverter::isExternalUrl($data))
81  $data = LinkConverter::makeUrlRelative($data);
82 
83  return $data;
84  }
85  /**
86  * Convert an absolute resource url on the server where the script runs to a relative one.
87  * The converted url is relative to the directory configured in the config key
88  * 'htmlBaseDir' section 'cms'
89  * @param url The url to convert
90  * @return The converted url
91  */
92  function makeUrlRelative($url)
93  {
94  $urlConv = $url;
95 
96  // get base url
97  $baseUrl = LinkConverter::getBaseUrl();
98 
99  // strip base url
100  if (strpos($url, $baseUrl) === 0)
101  $urlConv = str_replace($baseUrl, '', $url);
102 
103  return $urlConv;
104  }
105  /**
106  * Convert an relative url to a absolute one.
107  * @param url The url to convert
108  * @return The converted url
109  */
110  function makeUrlAbsolute($url)
111  {
112  $urlConv = $url;
113 
114  // get base url
115  $baseUrl = LinkConverter::getBaseUrl();
116 
117  if (strpos($url, $baseUrl) !== 0 && strpos($url, 'javascript') !== 0)
118  $urlConv = $baseUrl.$url;
119 
120  return $urlConv;
121  }
122  /**
123  * Get the absolute http url of the base directory. The relative path to
124  * that directory as seen from the script is configured in the config key
125  * 'htmlBaseDir' section 'cms'.
126  * @return The base url.
127  */
128  function getBaseUrl()
129  {
130  if ($gLinkConverterBaseUrl == null)
131  {
132  $parser = InifileParser::getInstance();
133  if (($resourceBaseDir = $parser->getValue('htmlBaseDir', 'cms')) === false)
134  WCMFException::throwEx($parser->getErrorMsg(), __FILE__, __LINE__);
135 
136  $refURL = UriUtil::getProtocolStr().$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
137  $gLinkConverterBaseUrl = URIUtil::makeAbsolute($resourceBaseDir, $refURL);
138  }
139  return $gLinkConverterBaseUrl;
140  }
141  /**
142  * Check if an url is absolute
143  * @param url The url to check
144  * @return True/False wether the url is absolute
145  */
146  function isAbsoluteUrl($url)
147  {
148  return strpos($url, 'http://') === 0 || strpos($url, 'https://');
149  }
150  /**
151  * Check if an url is external
152  * @param url The url to check
153  * @return True/False wether the url is external
154  */
155  function isExternalUrl($url)
156  {
157  return !(strpos($url, UriUtil::getProtocolStr().$_SERVER['HTTP_HOST']) === 0 ||
158  strpos($url, 'http://') === false || strpos($url, 'https://') === false);
159  }
160 }
161 ?>
throwEx($message, $file='', $line='')
makeAbsolute($rel_uri, $base, $REMOVE_LEADING_DOTS=true)
getUrls($string)
DataConverter is the base class for all converter classes. It defines the interface for converting da...