/**
* The required files
*/
require_once(HTML_BASE_UTIL_PATH.'/Link.php');
require_once(HTML_BASIC_UTIL_PATH.'/Message.php');
if (defined('HTML_LOG_UTIL_PATH')) {
require_once(HTML_LOG_UTIL_PATH.'/Log.php');
}
if (defined('HTML_LANGUAGE_UTIL_PATH')) {
require_once(HTML_LANGUAGE_UTIL_PATH.'/Translate.php');
}
/**
* Get the url or Link from the specified key
* <code>
* Usage:
* $key = 'URL_HOME';
* $url = new Url();
* print $url->get($key);
* print $url->link($key);
* Or
* $fileName = '/jquery.js';
* $filePath = HTML_JQUERY_PATH;
* $packageName = MYPHP_JQUERY_NAME;
* $packagePath = HTML_JQUERY_PATH;
* $src = Url::src($fileName, $filePath, $packageName, $packagePath);
* Or
* print Url::get($key);
* Or
* print Url::link($key);
* </code>
* @package util
*/
class Url {
/**
* Constructor
*/
function Url() {
}
/**
* Get the SRC to the url for the *.js or *.gif or *.css files
* <code>
* Usage:
* $fileName = '/jquery.js';
* $filePath = HTML_JQUERY_PATH;
* $packageName = MYPHP_JQUERY_NAME;
* $packagePath = HTML_JQUERY_PATH;
* $src = Url::src($fileName, $filePath, $packageName, $packagePath);
* </code>
* @param String $fileName The name of the javascript file. I.e. '/foo.js'
* @param String $filePath The path to the javascript file. I.e. '/js'
* @param String $packageName The name of the package, where the javascript file is located
* @param String $packagePath The Path to the package, where the javascript file is located
* @return String The calculated SRC for the src include files
*/
function src($fileName, $filePath='', $packageName='', $packagePath='') {
$url = PROJECT_URL;
if ($packageName != '') {
$url = $url.MYPHP_PREFIX.CURRENT_VERSION;
$url = $url.'/'.basename($packageName);
}
if ($packagePath != '') {
$url = $url.'/'.basename($packagePath);
}
if ($filePath != '') {
$url = $url.'/'.basename($filePath);
}
if ($fileName != '') {
$url = $url.'/'.basename($fileName);
}
return $url;
}
/**
* Returns the full link for the specified key
* <code>
* Usage:
* $key = 'URL_HOME';
* $url = new Url();
* print $url->link($key);
* Or
* print Url::link($key);
* </code>
* @static
* @param String $key The key to the url to get
* @return String the complete link or '' if not found
*/
function link($key) {
$html = '';
$href = Url::get($key);
if ($href != null) {
$text = $href;
if (defined('HTML_LANGUAGE_UTIL_PATH')) {
$text = Translate::get($text);
}
/**
* TODO how to specify the relative url ?
*/
$link = new Link($text,$href);
$html .= $link->getHtml();
} else {
$msg = "Url Not found in GLOBALS[DEFINE_URL], key:$key";
if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) {
Log::error(__FILE__,__LINE__,$msg);
//Message::add($msg, __FILE__, __LINE__);
} else {
Message::add($msg, __FILE__, __LINE__);
}
}
return $html;
}
/**
* Returns the url for the specified key
* @see HTML_BASE_RESOURCE_PATH/Define.php
* <code>
* Usage:
* $key = 'URL_HOME';
* $url = new Url();
* print $url->get($key);
* Or
* print Url::get($key);
* </code>
* @static
* @param String $key The key to the url to get
* @return String the complete url or null if not found
*/
function get($key) {
$url = null;
$theKey = md5($key);
if (array_key_exists($theKey,$GLOBALS[DEFINE_URL])) {
$url = $GLOBALS[DEFINE_URL][$theKey];
} else {
$msg = "Key not Not found in GLOBALS[DEFINE_URL], key:$key - md5:$theKey";
if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) {
Log::error(__FILE__,__LINE__,$msg);
//Message::add($msg, __FILE__, __LINE__);
} else {
Message::add($msg, __FILE__, __LINE__);
}
}
return $url;
}
/**
* Get the real subdomain name, if the domain is a subdomain
* or return "" if a real domain name
* Special handling for my own subdomains
* sample: test.eksperter.dk => eksperter.dk
* <code>
* Usage:
* $domain = 'test.eksperter.dk';
* $subdomain = Url::subdomain($domain);
* print $subdomain; // eksperter.dk
* Or
* $domain = 'eksperter.dk';
* $subdomain = Url::subdomain($domain);
* print $subdomain; // ""
* </code>
* @static
* @param String $subdomain The subdomain name. Escape special chars like '.'
* @retrun String The real domain name, stripped away the subdomain or empty ""
*/
function subdomain($subdomain) {
$domainname = '';
$names = split("\.", $subdomain);
if (count($names) == 3) {
$domainname = $names[1].'.'.$names[2];
if ($subdomain != $domainname) {
$domainname = 'http:/'.'/'.$domainname;
}
}
return $domainname;
}
}
?>