<?php
/**
* Utility static class for Cartboard Box Manager
*
* @version 1.0
* @author Edoardo Tenani <hide@address.com>
* @license GNU Public License
* <http://opensource.org/licenses/gpl-3.0.php>
* @copyright Edoardo Tenani - 2010
*
* @package cboxmanager
* @subpackage utility
* @category utils
* @since 0.0.11
*
* @static
*/
class Utils {
/**
* Class contructor
*
* @return void
*
* @ignore
*/
function __construct() {}
/* OBJECT FUNCTION OVERRIDE
************************************************/
/**
* When the class is destroyed this function is called.
*
* @return void
*
* @ignore
*/
public function __destruct() { }
/**
* When an inaccesible method of the class is invoked this function
* is called.
*
* @return void
*/
public function __call($name, $arguments) {
echo get_class($this)."::Error in calling object method <b>'$name'</b>"."<br>\n";
}
/**
* When an inaccesible static method of the class is invoked this
* function is called.
*
* @return void
*/
public static function __callStatic($name, $arguments) {
echo get_class($this)."::Error in calling object static method <b>'$name'</b>"."<br>\n";
}
/**
* When the object is converted to a string this function is used to
* format the result string.
* Values are separated by "|". If the class var is an array, values
* in it will be separated by ",", or NULL if is not set.
* The string starts and ends with "-".
*
* @return string a string with all class vars values separated by |
*
* @ignore
*/
public function __toString() {
return NULL;
}
/* SET & GET FUNCTION
************************************************/
/**
* The function set a specified class var.
* Is not case sensitive.
* It's not possible to set the ID class var.
*
* @access public
* @param string $var the name of the var to be set
* @param mixed $value the new value to set; if not specified NULL
* value is used
* @return bool true
*
* @link http://php.net/strtoupper
*
* @ignore
*/
public function set($var, $value = NULL) {
$this->{strtoupper($var)} = $value;
return true;
}
/**
* The function get a specified class var.
* Is not case sensitive.
*
* @param string $var the name of the var to be get
* @return mixed the class var
*
* @link http://php.net/strtoupper
*
* @ignore
*/
public function get($var) {
return $this->{strtoupper($var)};
}
/* PUBLIC FUNCTION
************************************************/
/**
* Display the navigation menu
*
* Create the string from menu file and return it to index page.
*
* @static
*
* @param string $action the string to select which menu item need to be
* edited
* @return string the menu as a string
*
* @internal the class load a different XML menu file according to
* $_CONFIG["ajax_menu"] var
*
* @ignore
* @deprecated deprecated since 0.1.1 ( function is no more used; the
* xml file which function refers to won't exists anymore )
*/
public static function display_menu($action) {
global $_CONFIG;
$menu = $_CONFIG["ajax_menu"]
? simplexml_load_file("./view/menu-ajax_".$_CONFIG["lang"].".xml")
: simplexml_load_file("./view/menu_".$_CONFIG["lang"].".xml");
foreach ($menu->children() as $second_gen) {
if ( $second_gen["id"] == "menu-".$action ) {
$second_gen->addAttribute("class", "active");
}
}
return substr($menu->asXML(), 22); // substr() to delete <\?xml version="1.0"?\>\n
}
/**
* Display the page content
*
* According to the specified action includes the relative content page.
*
* @static
*
* @param string action the string to select content
* @return void
*
* @internal Uses a switch() to choose between all includes; default
* value is to display the 404 error page
*/
public static function display_content($action) {
switch ( $action ) {
case "404":
include "./view/error_404.php";
break;
case "index":
include "./view/index.php";
break;
case "view":
include "./view/view.php";
break;
case "add":
include "./view/add.php";
break;
case "mod":
include "./view/mod.php";
break;
case "del":
include "./view/del.php";
break;
case "admin":
include "./view/admin.php";
break;
case "update":
include "./view/update.php";
break;
case "doc":
include "./view/doc.php";
break;
default:
include "./view/error_404.php";
break;
}
}
/**
* Display or return translated strings
*
* Load translated strings from language file according to the
* selected language. By default prints the string, but can only
* return it. This function manage the multilanguage capability
* ( i18n ).
*
* Examples:<br>
* <code>
* tt("page", "identifier"); // direct print
* echo tt("page", "identifier", true); // return print
* </code>
*
* @static
*
* @param string $page the name of the page where function is used,
* without extension
* @param string $identifier the unique identifier of the string
* @param bool $return if true
* @param string $path_to_root the path to project root directory
* @return mixed can return void if $return = false or the translated
* string if $return = true
*
* @internal uses absolute path to translation file via
* $_CONFIG["folder"]
*
* @see function tt()
* @uses $_CONFIG["folder"]
*
* @todo check if is possible to move the include in the index page
*/
public static function tt($page, $identifier, $return = false) {
global $_CONFIG;
//require $_CONFIG["folder"]."\lib\lang".'\\'.$_CONFIG["lang"].".php";
require $_CONFIG["folder"]."/lib/lang".'/'.$_CONFIG["lang"].".php";
if ( !$return )
echo ${$page}["$identifier"];
else
return ${$page}["$identifier"];
}
/**
* Retrieve categories from database
*
* Can retrieve categories from database and print out a complete
* <i><select></i> html element.
*
* @static
*
* @param mixed $db a link to a database resource
* @param bool $html_select if true function will return a complete
* <i><select></i> html element with all data, if false will
* return an array of values
* @param mixed $selected default is false; if an id ( int ) is
* passed, the category with the same id will be selected by
* default when the <i><select></i> is displayed
* @param string $html_select_name the name attribute of the <i>
* <select></i> html element
* @return mixed can return an array of values, if <b>$html_select</b>
* is set to false, a complete <i><select></i> html element if
* set to true, or a string ( $function["no-categories"] ) if
* there is no category in the database
*
* @internal query directly "category" table in the database
*
* @uses Database::query()
* @link http://php.net/mysql_num_rows
* @link http://php.net/mysql_fetch_array
*/
public static function display_categories($db, $html_select = false, $selected = false, $html_select_name = "category") {
$db->query("SELECT ID, name FROM categories ORDER BY name");
if ( $db->num_rows() ) {
$i = 0;
while ( $row = $db->fetch_array() ) {
$array[$i++] = $row;
}
if ( !$html_select )
return $array;
$select = "<select name=\"".$html_select_name."\">";
$selected_str = "";
foreach ( $array as $cat ) {
if ( $selected ) {
if ( $cat["ID"] == $selected ) {
$selected_str = " selected ";
}
}
$select .= "<option value=\"".$cat["ID"]."\"".$selected_str.">".$cat["name"]."</option>";
$selected_str = "";
}
$select .= "</select>";
return $select;
}
else
return tt("function", "no-categories");
}
/**
* Retrieve brands from database
*
* Can retrieve brands from database and print out a complete
* <i><select></i> html element.
*
* @static
*
* @param mixed $db a link to a database resource
* @param bool $html_select if true function will return a complete
* <i><select></i> html element with all data, if false will
* return an array of values
* @param mixed $selected default is false; if an id ( int ) is
* passed, the brand with the same id will be selected by
* default when the <i><select></i> is displayed
* @param string $html_select_name the name attribute of the <i>
* <select></i> html element
* @return mixed can return an array of values, if <b>$html_select</b>
* is set to false, a complete <i><select></i> html element if
* set to true, or a string ( $function["no-brands"] ) if
* there is no category in the database
*
* @internal query directly "brand" table in the database
*
* @uses Database::query()
* @link http://php.net/mysql_num_rows
* @link http://php.net/mysql_fetch_array
*/
public static function display_brands($db, $html_select = false, $selected = false, $html_select_name = "brand") {
$db->query("SELECT ID, name FROM brands ORDER BY name");
if ( $db->num_rows() ) {
$i = 0;
while ( $row = $db->fetch_array() ) {
$array[$i++] = $row;
}
if ( !$html_select )
return $array;
$select = "<select name=\"".$html_select_name."\">";
$selected_str = "";
foreach ( $array as $brand ) {
if ( $selected ) {
if ( $brand["ID"] == $selected ) {
$selected_str = " selected ";
}
}
$select .= "<option value=\"".$brand["ID"]."\"".$selected_str.">".$brand["name"]."</option>";
$selected_str = "";
}
$select .= "</select>";
return $select;
}
else
return tt("function", "no-brands");
}
/**
* Display a list of changelog file with link
*
* Display an unordered list of link to the changelog files inside
* <i>/doc/changelog</i>
*
* @static
*
* @return void
*
* @internal read changelog directory to display changelog files
*
* @link http://php.net/opendir
* @link http://php.net/readdir
* @link http://php.net/closedir
* @link http://php.net/natsort
* @link http://php.net/array_reverse
*/
public static function display_changelogs() {
if ( $handle = opendir('./doc/changelog/') ) {
while ( false !== ( $file = readdir($handle) ) ) {
if ( $file != "." && $file != ".." && $file != "TODO" ) {
$files[] = $file;
}
}
closedir($handle);
}
natsort($files);
$files = array_reverse($files);
foreach ( $files as $file ) {
echo "<li>", "<a href=\"doc/changelog/".$file."\"><code>".$file."</code></a>", "</li>";
}
}
/**
* Display errors messages
*
* Display an unordered list of errors returned by a page
*
* @static
*
* @param mixed $errors the $_GET["e"] var, which if is set, contains
* errors
*
* @return void
*
* @internal $_GET["e"] is serialized, the function before using it
* unserialize the object; if $errors is an array use a foreach to
* list it all, if not simply print the element
*
* @link http://php.net/unserialize
*/
public static function display_errors($errors) {
if ( isset($errors) && !empty($errors) && is_array($errors) ) {
echo "<ul>";
foreach ( unserialize($errors) as $key => $value ) {
echo "<li>", "<b class=\"capitalize\">", $key, ":</b> ", $value, "</li>";
}
echo "</ul>";
}
else {
echo "<ul>";
echo "<li>", unserialize($errors), "</li>";
echo "</ul>";
}
}
/**
* Display a list of available languages in a HTML select
*
* @static
*
* @param string $select_name the name to be inserted in the HTML
* in the "name" attribute of the select
*
* @return string a HTML select to choose between available languages
*/
public static function display_lang($select_name = "lang") {
$lang = Utils::dir_filelist("lang");
$select = '<select id="" name="'.$select_name.'">';
foreach ( $lang as $l ) {
$l = substr($l, 0, -4);
$select .= '<option value="'.$l.'">'.$l.'</option>';
}
$select .= '</select>';
return $select;
}
/**
* List files in a directory
*
* @static
*
* @access public
*
* @param string $dir the name of the directory to be scanned ( no
* trailing slash )
*
* @return array an array ( ordere by name ) of files in a directory
*
* @link
*/
public static function dir_filelist($dir) {
$file_list = array();
$dh = opendir($dir);
while ( $file = readdir($dh) ) {
if ( $file != "." && $file != ".." && is_file($dir."/".$file) ) {
$file_list[] = $file;
}
}
closedir($dh);
return $file_list;
}
// old version, no more supported
/*public static function local_version() {
if ( file_exists("README") ) {
$fp = fopen("README", "r");
$line = fgets($fp);
while ( !strpos($line, "Version:") ) {
$line = fgets($fp);
}
fclose($fp);
return trim(substr($line, strpos($line, "VERSION:") + 11));
}
else
return tt("function", "project_version");
}*/
/**
* Get application local version
*
* Read from <i>/version.xml</i> the application version.
*
* @static
*
* @return string the application version
*
* @see http://php.net/simplexml_load_file
*/
public static function local_version() {
global $_CONFIG;
$app = simplexml_load_file($_CONFIG["folder"]."/version.xml");
return $app->version;
}
/**
* Get application server version
*
* Read from <i>[SERVER]/version.xml</i> the application version. It
* connects to the update server ( defined in config.php ) and
* retrieve the last release.
*
* @static
*
* @return string the application version
*
* @see http://php.net/simplexml_load_file
*/
public static function server_version() {
global $_CONFIG;
$app = simplexml_load_file($_CONFIG["update"]["dir"]."/version.xml");
return $app->version;
}
/**
* Get application developer version
*
* Read from <i>[SERVER]/version.xml</i> the application developer
* version. Itconnects to the update server ( defined in config.php )
* and retrieve the last release.
*
* @static
*
* @return string the application version
*
* @see http://php.net/simplexml_load_file
*/
public static function server_dev_version() {
global $_CONFIG;
$app = simplexml_load_file($_CONFIG["update"]["dir"]."/version.xml");
return $app->dev_version;
}
/**
* Get language file local version
*
* Read from <i>/version.xml</i> the language file version.
*
* @static
*
* @return string the application version
*
* @see http://php.net/simplexml_load_file
*/
public static function local_lang_version() {
global $_CONFIG;
$lang = simplexml_load_file($_CONFIG["lang_settings"]["load_dir"]."/".$_CONFIG["lang"].".xml");
return $lang["version"];
}
/**
* Get language file server version
*
* Read from <i>[SERVER]/version.xml</i> the language file version. It
* connects to the update server ( defined in config.php ) and
* retrieve the last release.
*
* @static
*
* @return string the application version
*
* @see http://php.net/simplexml_load_file
*/
public static function server_lang_version() {
global $_CONFIG;
$app = simplexml_load_file($_CONFIG["update"]["dir"]."/version.xml");
return $app->lang_version;
}
/**
* Get language compiled file version
*
* Get file version from the compiled language file.
*
* @static
*
* @ignore
*/
public static function compiled_lang_version() {}
/**
* Compare results from Utils::local_version() and
* Utils::server_version()
*
* Compares results to detect which is greater. Used to determine if
* application is up to date or not.
*
* @static
*
* @return int returns < 0 if local_version() is less than
* server_version(); > 0 if local_version() is greater than
* server_version(), and 0 if they are equal.
*
* @uses Utils::compare()
* @link http://php.net/explode
*
* @internal Execute Utils::local_version() and
* Utils::server_version(), explode their results and execute
* Utils::compare() whit the exploded values
*/
public static function compare_version() {
$local = Utils::local_version();
$server = Utils::server_version();
$local = explode(" ", $local);
$server = explode(" ", $server);
return Utils::compare($local, $server);
}
/**
* Compare results from Utils::local_lang_version() and
* Utils::server_lang_version()
*
* Compares results to detect which is greater. Use to determine if
* language file is up to date or not.
*
* @static
*
* @return int returns < 0 if local_lang_version() is less than
* server_lang_version(); > 0 if local_lang_version() is greater
* than server_lang_version(), and 0 if they are equal.
*
* @uses Utils::compare()
* @link http://php.net/explode
*
* @internal Execute Utils::local_lang_version() and
* Utils::server_lang_version(), explode their results and execute
* Utils::compare() whit the exploded values
*/
public static function compare_lang_version() {
$local = Utils::local_lang_version();
$server = Utils::server_lang_version();
$local = explode(" ", $local);
$server = explode(" ", $server);
return Utils::compare($local, $server);
}
/**
* Set a specified flag
*
* Create a new flag inside <i>/tmp/flags</i>. Used to store options
* that need to be setted once in a lifetime.
*
* @static
*
* @param string $name the name of the flag to be setted
* @param string $content optionally a content to be written inside
* file
* @return bool true if flag is correctly setted, false otherwise
*
* @link http://php.net/fopen
* @link http://php.net/fclose
*/
public static function set_flag($name, $content = "") {
$fp = fopen("tmp/flags/".$name, "w+");
if ( !empty($content) )
fwrite($fp, $content);
return fclose($fp);
}
/**
* Get a specified flag
*
* Get a flag from <i>/tmp/flags</i>. Check if a file whit specified
* name exists in <i>/tmp/flags</i>. Check for file content.
*
* @static
*
* @param string $name the name of the flag to be got
* @return true if flag exists, false otherwise, the content of the
* file if exists
*
* @link http://php.net/file_exists
*/
public static function get_flag($name) {
if ( file_exists("tmp/flags/".$name) ) {
$file = file_get_contents("tmp/flags/".$name);
if ( !empty( $file ) )
return $file;
}
else
return false;
return true;
}
/**
* Remove a flag
*
* Delete a flag from <i>/tmp/flags</i>. Before deletion check if
* exists.
*
* @static
*
* @param string $name the name of the flag to be destroyed
* @return true if all goes correctly, false otherwise
*
* @uses Utils::get_flag()
* @link http://php.net/unlink
*/
public static function del_flag($name) {
return Utils::get_flag($name) ? unlink("tmp/flags/".$name) : true;
}
/**
* Make a variable ready to be passed by URL
*
* Serialize and url encode vars to be passed by URL.
*
*
* @static
*
* @param mixed $var the var to be passed by URL
* @return mixed the variable url encoded and serialized
*
* @link http://php.net/serialize
* @link http://php.net/urlencode
*/
public static function url_ready($var) {
return urlencode(serialize($var));
}
/* PRIVATE FUNCTION
************************************************/
/**
* Compare two version strings using a natural sorting algorithm
*
* Compare local and server versions with a string comparison.
*
* @access private
* @static
*
* @param string $local string of local version
* @param string $server string of server version
*
* @return int returns < 0 if local is less than server; > 0 if local
* is greater than server, and 0 if they are equal.
*
* @internal the string comparison use a natural order algorithm,
* because in natural sorting 10 is after 9, while in "normal"
* sorting 10 is after 1
*
* @link http://php.net/strnatcmp
*/
private static function compare($local, $server) {
if ( ( $strcmp = strnatcmp($local[0], $server[0]) ) == 0 ) {
if ( isset($local[1]) && isset($server[1]) )
return strnatcmp($local[1], $server[1]);
else
return $strcmp;
}
else
return $strcmp;
}
/* DEPRECATED FUNCTION
************************************************/
}
?>