Free web development scripts for the webmasterDevelopment Scripts


 social media marketing services
 Site Navigation
Clicky Web Analytics
 o Development Scripts
 o Text Only Edition
 o PHP.net News
 o Tutorials
 o Register an account
 o New entries this week
 o Affiliates/Links
 o Contact Us
 o About us
 o Advanced Search

 Browse Path
 o 1 user online
 o Most users online: 67
 o Home
  o PHP
   o XML
    o Script

 Member Login
User Name
Password

 Standards
Valid XHTML 1.0!
Valid 
CSS!

 Recommended Links
 o PHP Freaks
 o Paper Free Invoicing
 o Tutorials
 o Expired Domain Crawler
 o PFB UK
 o OxyScripts
 o PFB

PHP-ResourceManager (Rated 5)

Description:

PHP-ResourceManager is a collection of some functions and classes which allows the webmaster to implement a cms-like functionality for language independent web-programming. The (language independent) content is stored in xml-based resource-files. The webmaster has at two possibilities to use PHP-ResourceManager: a) the resource-file could be set in the code or b) the resource-file could be set by the languages which comes with the users browser. - To get a deeper look go to the description on the official website (http://phpscript.axelschneider.info/).

Code starts here


<?php

/* **********************************************************************************************
* The resource-manager provides some classes and functions to manage language-dependent content.
*
* The classes are:
* - ResourceManager4
* - ResourceManager5
*
* The static functions are:
* - getResourceManagerByBrowserLanguage
* - getResourceManagerByResourceFile
* - getBrowserLanguages
*
* Author: Axel Schneider
* Contact: Mail@AxelSchneider.info
* Licence: This is freeware.
* Last edit: 03.01.2007
* Version: 1.0.1
*
* Designed for php-version 4 and 5.
*
* Dependencies: no
*
* History: 01.01.2007 - Build the classes (ResourceManager4 and ResourceManager5) and
* the functions (getResourceManagerByBrowserLanguage,
* getResourceManagerByResourceFile and getBrowserLanguages).
*
* Hints: The resource-files should include a dtd so that a certain order of the
* xml-nodes and its contents (e.g. Umlaute) could be used.
*
* ToDo:
*
* ********************************************************************************************** */

/* ***********************
* dependencies
* *********************** */

session_start();

/* ***********************
* public static functions
* *********************** */


/*
* Gets an instance of an resource-manager depending of the browser-languages and
* the given default-language.
*
* The name of a resource-file is expected in the following way:
* [any name].[country-code as 2-letter-iso-code].xml
* Examples:
* - strings.de.xml
* - resources.en.xml
*/
function getResourceManagerByBrowserLanguage($resourceName, $defaultLanguage="de")
{
//get array of browser-languages
$bLanguages = getBrowserLanguages();

//try to get an resource-file which corresponds to the browser-languages
$fileName = "";
$foundResourceFile = false;
foreach (
$bLanguages as $bLanguage)
{
$fileName = $resourceName.".".$bLanguage['code'].".xml";
if(
file_exists($fileName))
{
$foundResourceFile = true;
break;
}
}

if(
$foundResourceFile)
{
return
getResourceManagerByResourceFile($fileName);
}
else
{
$fileName = $resourceName.".".$defaultLanguage.".xml";
return
getResourceManagerByResourceFile($fileName);
}
}

/*
* Gets an instance of an resource-manager. If the php-version is not 5 or 4 the
* function will return false.
*/
function getResourceManagerByResourceFile($resourceFile)
{
$v = phpversion();
$mainV = substr($v, 0, 1);
$rmTmp = null;

if(
$mainV=="5")
{
$rmTmp = new ResourceManager5($resourceFile);
}
else if(
$mainV=="4")
{
//version 4 is a bit tricky thats is why we might check if a special function is available
//special function: domxml_open_file
$rmTmp = new ResourceManager4($resourceFile);
}
else
{
return
false;
}

//check if any error occured
if($rmTmp->ERRORMESSAGE!="")
{
return
false;
}
else
return
$rmTmp;
}

/*
* Gets an array of the browser-languages which will be read from HTTP_ACCEPT_LANGUAGE.
* The array will be filled in the following way:
* array['code', 'coef', 'morecode', 'fullcode']
*/
function getBrowserLanguages($str=NULL)
{
// getting http instruction if not provided
$str=$str?$str:$_SERVER['HTTP_ACCEPT_LANGUAGE'];

// exploding accepted languages
$langs=explode(',',$str);

// creating output list
$accepted=array();
foreach (
$langs as $lang)
{
// parsing language preference instructions
// 2_digit_code[-longer_code][;q=coefficient]
ereg('([a-z]{1,2})(-([a-z0-9]+))?(;q=([0-9\.]+))?',$lang,$found);

// 2 digit lang code
$code=$found[1];

// lang code complement
$morecode=$found[3];

// full lang code
$fullcode=$morecode?$code.'-'.$morecode:$code;

// coefficient
$coef=sprintf('%3.1f',$found[5]?$found[5]:'1');

// for sorting by coefficient
$key=$coef.'-'.$code;

// adding
//echo "adding code: ".$code.", coef=".$coef.", morecode=".$morecode.", fullcode=".$fullcode."<br>";
$accepted[$key]=array('code'=>$code,'coef'=>$coef,'morecode'=>$morecode,'fullcode'=>$fullcode);
}

// sorting the list by coefficient desc
krsort($accepted);

return
$accepted;
}


/* **************
* public classes
* ************** */


/* *************************************
* ResourceManager for php in version 4.
*
* The public members are:
* - FOUNDRESOURCES
* - RESOURCEFILE
* - ERRORMESSAGE
* - SPECIALSIGNS
*
* The public constructors are:
* - ResourceManager4
*
* The public functions are:
* - changeResourceFile
* - prepareResources
* - getValue
* - getHtmlEncodedValue
* ************************************* */
class ResourceManager4
{
var
$FOUNDRESOURCES = null; //list of nodes
var $RESOURCEFILE = null; //name of the resource-file
var $ERRORMESSAGE = null; //error-message
var $SPECIALSIGNS = null; //list of special html-signs (php-internal)

/*
* Ctor.
*/
function ResourceManager4($fileName)
{
$this->prepareResources($fileName);
}

/*
* Changes the resource-file. Returns false if any error occurs.
*/
function changeResourceFile($fileName)
{
//check if we have to change the resource-file
if($this->RESOURCEFILE==$fileName)
{
return
true;
}
else
{
return
$this->prepareResources($fileName);
}
}

/*
* Initializes the instance by
* - checking if the resource-file exists
* - loading the resource-file
* - loading all xml-nodes with the nodename 'resource'
*/
function prepareResources($fileName)
{
if(!
file_exists($fileName)) {
$this->ERRORMESSAGE="The file ".$fileName." does not exist!";
$_SESSION["log"]->error($this->ERRORMESSAGE);
return
false;
}
$this->RESOURCEFILE = $fileName;

$RESOURCES = domxml_open_file($this->RESOURCEFILE);

if(
$RESOURCES->has_child_nodes())
{
$temp = $RESOURCES->get_elements_by_tagname('resource');
if(
$temp==null)
{
$this->ERRORMESSAGE="Found no nodes with the nodename 'resource' in file '".$fileName."'!";
return
false;
}
else
{
$this->FOUNDRESOURCES = $temp;
}
}
else
{
$this->ERRORMESSAGE="Could not load file '".$fileName."'!";
return
false;
}

$RESOURCES = null;
return
true;
}

/*
* Gets the nodevalue of a resource-node with a certain attribute.
*/
function getValue($KEY)
{
$returnValue = null;
if(
$this->ERRORMESSAGE!=null) {
$returnValue = $this->ERRORMESSAGE;
$this->ERRORMESSAGE = null;
} else {
foreach (
$this->FOUNDRESOURCES as $RESOURCE) {
if(
$attributeValue = $RESOURCE->get_attribute_node('key')) {
if (
$attributeValue->value()==$KEY) { $returnValue = $RESOURCE->get_content();
break;
}
}
}
}

if(
$returnValue==null)
{
$msg = "Node 'resource' or key '".$KEY."' not found in file '".$this->RESOURCEFILE."'!";
return
$msg;
}
else
return
$returnValue;
}

/*
* Gets the html-encoded nodevalue of a resource-node with a certain attribute.
*/
function getHtmlEncodedValue($KEY)
{
$returnValue = null;

if(
$this->SPECIALSIGNS==null) {
$this->SPECIALSIGNS = get_html_translation_table(HTML_SPECIALCHARS);
}

$returnValue = $this->getValue($KEY);
$returnValue = strtr($returnValue, $this->SPECIALSIGNS);

return
$returnValue;
}

}

/* *************************************
* ResourceManager for php in version 5.
*
* The public members are:
* - FOUNDRESOURCES
* - RESOURCEFILE
* - ERRORMESSAGE
* - SPECIALSIGNS
*
* The public constructors are:
* - ResourceManager5
*
* The public functions are:
* - changeResourceFile
* - prepareResources
* - getValue
* - getHtmlEncodedValue
* ************************************* */
class ResourceManager5
{
var
$FOUNDRESOURCES = null; //list of nodes
var $RESOURCEFILE = null; //name of the resource-file
var $ERRORMESSAGE = null; //error-message
var $SPECIALSIGNS = null; //list of special html-signs (php-internal)

/*
* Ctor.
*/
function ResourceManager5($fileName)
{
$this->prepareResources($fileName);
}

/*
* Changes the resource-file. Returns false if any error occurs.
*/
function changeResourceFile($fileName)
{
//check if we have to change the resource-file
if($this->RESOURCEFILE==$fileName)
{
return
true;
}
else
{
return
$this->prepareResources($fileName);
}
}

/*
* Initializes the instance by
* - checking if the resource-file exists
* - loading the resource-file
* - loading all xml-nodes with the nodename 'resource'
*/
function prepareResources($fileName)
{
if(!
file_exists($fileName))
{
$this->ERRORMESSAGE="The file ".$fileName." does not exist!";
return
false;
}
$this->RESOURCEFILE = $fileName;

$RESOURCES = new DOMDocument();
$RESOURCES->validateOnParse = true;
$RESOURCES->preserveWhiteSpace = false;
$RESOURCES->load($this->RESOURCEFILE);

if(
$RESOURCES->hasChildNodes())
{
$temp = $RESOURCES->getElementsByTagname('resource');
if(
$temp==null)
{
$this->ERRORMESSAGE="Found no nodes with the nodename 'resource' in file '".$fileName."'!";
return
false;
}
else
{
$this->FOUNDRESOURCES = $temp;
}
}
else
{
$this->ERRORMESSAGE="Could not load file '".$fileName."'!";
return
false;
}

$RESOURCES = null;
return
true;
}

/*
* Gets the nodevalue of a resource-node with a certain attribute.
*/
function getValue($KEY)
{
$returnValue = null;
if(
$this->ERRORMESSAGE!=null)
{
$returnValue = $this->ERRORMESSAGE;
$this->ERRORMESSAGE = null;
}
else
{
foreach (
$this->FOUNDRESOURCES as $RESOURCE)
{
if (
$RESOURCE->getAttribute('key') == $KEY)
{
$returnValue = $RESOURCE->nodeValue;
}
}
}

if(
$returnValue==null)
{
$msg = "Node 'resource' or key '".$KEY."' not found in file '".$this->RESOURCEFILE."'!";
return
$msg;
}
else
{
return
$returnValue;
}
}

/*
* Gets the html-encoded nodevalue of a resource-node with a certain attribute.
*/
function getHtmlEncodedValue($KEY)
{
$returnValue = null;

if(
$this->SPECIALSIGNS==null) {
$this->SPECIALSIGNS = get_html_translation_table(HTML_SPECIALCHARS);
}

$returnValue = $this->getValue($KEY);
$returnValue = strtr($returnValue, $this->SPECIALSIGNS);

return
$returnValue;
}

}


?>


Submitted by Axel2407 on 01-04-2007 18:54


Recent Additions (from all categories)

(PHP) T Shirt Printing Software

(PHP) Youtube Video Organizer Script

(PHP) MP3z - MP3 Search Engine Script

(PHP) Recipez- PHP Recipe Script

(ASP) Stop image hotlinking and downloading. Web image protection is easy with Image Trapper.

Top Rated Scripts

(Javascripts) Javascript Delayed Redirection Rating: 5

(PHP) Random Links and images with PHP & MySQL Rating: 5

(PHP) RSS/XML news feed headline grabber! Rating: 5

(PHP) Random Signatures/Avatars Rating: 5

(Javascripts) Detecting Client Web Browser Rating: 5

© Copyright 2003 - Devscripts.net  Web Analytics