Source for file layersmenu-common.inc.php
Documentation is available at layersmenu-common.inc.php
// PHP Layers Menu 3.2.0-rc (C) 2001-2004 Marco Pratesi - http://www.marcopratesi.it/
* This file contains the code of the LayersMenuCommon class.
* This is the "common" class of the PHP Layers Menu library.
* You need to include PEAR.php and DB.php if (and only if) you want to use the DB support provided by ths class.
* The name of the package
* The version of the package
* The copyright of the package
* The author of the package
* URL to be prepended to the menu hrefs
* Do you want that code execution halts on error?
var $haltOnError = 'yes';
* The base directory where the package is installed
* The "libjs" directory of the package
* The directory where images related to the menu can be found
* The http path corresponding to imgdir
* The directory where icons of menu items can be found
* The http path corresponding to icondir
* This array may contain width and height of all icons
* If this var is false, width and height of icons have to be detected; if this var is true, width and height of icons are not detected and are retrieved from the iconsize array
var $issetIconsize = false;
* The directory where templates can be found
* The string containing the menu structure
* It counts nodes for all menus
* A multi-dimensional array to store informations for each menu entry
* A multi-dimensional array used only with the DB support; for each $menu_name, it stores the $cnt associated to each item id
* This array is needed for selection of the current item
* through the corresponding id (see the DB table structure)
* as, internally, items are stored, sorted and addressed in terms of $cnt
* The maximum hierarchical level of menu items
* An array that counts the number of first level items for each menu
* An array containing the number identifying the first item of each menu
* An array containing the number identifying the last item of each menu
* Data Source Name: the connection string for PEAR DB
var $dsn = 'pgsql://dbuser:dbpass@dbhost/dbname';
* DB connections are either persistent or not persistent
* Name of the table storing data describing the menu
var $tableName = 'phplayersmenu';
* Name of the i18n table corresponding to $tableName
var $tableName_i18n = 'phplayersmenu_i18n';
* Names of fields of the table storing data describing the menu
* default field names correspond to the same field names foreseen
* by the menu structure format
var $tableFields = array(
'parent_id' => 'parent_id',
'orderfield' => 'orderfield',
* Names of fields of the i18n table corresponding to $tableName
var $tableFields_i18n = array(
'language' => 'language',
* A temporary array to store data retrieved from the DB and to perform the depth-first search
var $_tmpArray = array();
* The constructor method; it initializates the menu system
$this->_packageName = 'PHP Layers Menu';
$this->version = '3.2.0-rc';
$this->copyright = '(C) 2001-2004';
$this->author = 'Marco Pratesi - http://www.marcopratesi.it/';
$this->prependedUrl = '';
$this->libjsdir = './libjs/';
$this->imgdir = './menuimages/';
$this->imgwww = 'menuimages/';
$this->icondir = './menuicons/';
$this->iconwww = 'menuicons/';
$this->tpldir = './templates/';
$this->menuStructure = '';
$this->treecnt = array();
$this->_maxLevel = array();
$this->_firstLevelCnt = array();
$this->_firstItem = array();
$this->_lastItem = array();
* The method to set the prepended URL
// We do not perform any check
$this->prependedUrl = $prependedUrl;
* The method to set the dirroot directory
$this->error("setDirroot: $dirroot is not a directory.");
if (substr($dirroot, - 1) != '/') {
$oldlength = strlen($this->dirroot);
$foobar = strpos($this->libjsdir, $this->dirroot);
if (!($foobar === false || $foobar != 0)) {
$this->libjsdir = $dirroot . substr($this->libjsdir, $oldlength);
$foobar = strpos($this->imgdir, $this->dirroot);
if (!($foobar === false || $foobar != 0)) {
$this->imgdir = $dirroot . substr($this->imgdir, $oldlength);
$foobar = strpos($this->icondir, $this->dirroot);
if (!($foobar === false || $foobar != 0)) {
$this->icondir = $dirroot . substr($this->icondir, $oldlength);
$foobar = strpos($this->tpldir, $this->dirroot);
if (!($foobar === false || $foobar != 0)) {
$this->tpldir = $dirroot . substr($this->tpldir, $oldlength);
$this->dirroot = $dirroot;
* The method to set the libjsdir directory
if ($libjsdir != '' && substr($libjsdir, - 1) != '/') {
if ($libjsdir == '' || substr($libjsdir, 0, 1) != '/') {
$foobar = strpos($libjsdir, $this->dirroot);
if ($foobar === false || $foobar != 0) {
$libjsdir = $this->dirroot . $libjsdir;
$this->error("setLibjsdir: $libjsdir is not a directory.");
$this->libjsdir = $libjsdir;
* The method to set the imgdir directory
if ($imgdir != '' && substr($imgdir, - 1) != '/') {
if ($imgdir == '' || substr($imgdir, 0, 1) != '/') {
$foobar = strpos($imgdir, $this->dirroot);
if ($foobar === false || $foobar != 0) {
$imgdir = $this->dirroot . $imgdir;
$this->error("setImgdir: $imgdir is not a directory.");
* The method to set imgwww
if ($imgwww != '' && substr($imgwww, - 1) != '/') {
* The method to set the icondir directory
if ($icondir != '' && substr($icondir, - 1) != '/') {
if ($icondir == '' || substr($icondir, 0, 1) != '/') {
$foobar = strpos($icondir, $this->dirroot);
if ($foobar === false || $foobar != 0) {
$icondir = $this->dirroot . $icondir;
$this->error("setIcondir: $icondir is not a directory.");
$this->icondir = $icondir;
* The method to set iconwww
if ($iconwww != '' && substr($iconwww, - 1) != '/') {
$this->iconwww = $iconwww;
* The method to set the iconsize array
$this->iconsize['width'] = ($width == (int) $width) ? $width : 0;
$this->iconsize['height'] = ($height == (int) $height) ? $height : 0;
$this->issetIconsize = true;
* The method to unset the iconsize array
unset ($this->iconsize['width']);
unset ($this->iconsize['height']);
$this->issetIconsize = false;
* The method to set the tpldir directory
if ($tpldir != '' && substr($tpldir, - 1) != '/') {
if ($tpldir == '' || substr($tpldir, 0, 1) != '/') {
$foobar = strpos($tpldir, $this->dirroot);
if ($foobar === false || $foobar != 0) {
$tpldir = $this->dirroot . $tpldir;
$this->error("setTpldir: $tpldir is not a directory.");
* The method to read the menu structure from a file
* @param string $tree_file the menu structure file
if (!($fd = fopen($tree_file, 'r'))) {
$this->error("setMenuStructureFile: unable to open file $tree_file.");
$this->menuStructure = '';
while ($buffer = fgets($fd, 4096)) {
$buffer = ereg_replace(chr(13), '', $buffer); // Microsoft Stupidity Suppression
$this->menuStructure .= $buffer;
if ($this->menuStructure == '') {
$this->error("setMenuStructureFile: $tree_file is empty.");
* The method to set the menu structure passing it through a string
* @param string $tree_string the menu structure string
$this->menuStructure = ereg_replace(chr(13), '', $tree_string); // Microsoft Stupidity Suppression
if ($this->menuStructure == '') {
$this->error('setMenuStructureString: empty string.');
* The method to set the value of separator
$this->separator = $separator;
* The method to set parameters for the DB connection
* @param string $dns Data Source Name: the connection string for PEAR DB
* @param bool $persistent DB connections are either persistent or not persistent
$this->error('initdb: $dsn is not an string.');
$this->error('initdb: $persistent is not a boolean.');
$this->persistent = $persistent;
* The method to set the name of the table storing data describing the menu
$this->error('setTableName: $tableName is not a string.');
$this->tableName = $tableName;
* The method to set the name of the i18n table corresponding to $tableName
$this->error('setTableName_i18n: $tableName_i18n is not a string.');
$this->tableName_i18n = $tableName_i18n;
* The method to set names of fields of the table storing data describing the menu
$this->error('setTableFields: $tableFields is not an array.');
if (count($tableFields) == 0) {
$this->error('setTableFields: $tableFields is a zero-length array.');
while (list ($key, $value) = each($tableFields)) {
$this->tableFields[$key] = ($value == '') ? "''" : $value;
* The method to set names of fields of the i18n table corresponding to $tableName
$this->error('setTableFields_i18n: $tableFields_i18n is not an array.');
if (count($tableFields_i18n) == 0) {
$this->error('setTableFields_i18n: $tableFields_i18n is a zero-length array.');
reset ($tableFields_i18n);
while (list ($key, $value) = each($tableFields_i18n)) {
$this->tableFields_i18n[$key] = ($value == '') ? "''" : $value;
* The method to parse the current menu structure and correspondingly update related variables
* @param string $menu_name the name to be attributed to the menu
* whose structure has to be parsed
$menu_name = '' // non consistent default...
$this->_maxLevel[$menu_name] = 0;
$this->_firstLevelCnt[$menu_name] = 0;
$this->_firstItem[$menu_name] = $this->_nodesCount + 1;
$cnt = $this->_firstItem[$menu_name];
$menuStructure = $this->menuStructure;
/* *********************************************** */
/* Partially based on a piece of code taken from */
/* TreeMenu 1.1 - Bjorge Dijkstra (bjorge@gmx.net) */
/* *********************************************** */
while ($menuStructure != '') {
$before_cr = strcspn($menuStructure, "\n");
$buffer = substr($menuStructure, 0, $before_cr);
$menuStructure = substr($menuStructure, $before_cr+ 1);
if (substr($buffer, 0, 1) == '#') {
continue; // commented item line...
$node = explode($this->separator, $tmp);
for ($i= count($node); $i<= 6; $i++ ) {
$this->tree[$cnt]['level'] = strlen($node[0]);
$this->tree[$cnt]['text'] = $node[1];
$this->tree[$cnt]['href'] = $node[2];
$this->tree[$cnt]['title'] = $node[3];
$this->tree[$cnt]['icon'] = $node[4];
$this->tree[$cnt]['target'] = $node[5];
$this->tree[$cnt]['expanded'] = $node[6];
/* *********************************************** */
$this->_lastItem[$menu_name] = count($this->tree);
$this->_nodesCount = $this->_lastItem[$menu_name];
$this->tree[$this->_lastItem[$menu_name]+ 1]['level'] = 0;
$this->_postParse($menu_name);
* The method to parse the current menu table and correspondingly update related variables
* @param string $menu_name the name to be attributed to the menu
* whose structure has to be parsed
* @param string $language i18n language; either omit it or pass
* an empty string ('') if you do not want to use any i18n table
$menu_name = '', // non consistent default...
$this->_maxLevel[$menu_name] = 0;
$this->_firstLevelCnt[$menu_name] = 0;
unset ($this->tree[$this->_nodesCount+ 1]);
$this->_firstItem[$menu_name] = $this->_nodesCount + 1;
$time_start = $this->_getmicrotime();
$db = DB::connect($this->dsn, $this->persistent);
$this->error('scanTableForMenu: ' . $db->getMessage());
$this->tableFields['id'] . ' AS id, ' .
$this->tableFields['parent_id'] . ' AS parent_id, ' .
$this->tableFields['text'] . ' AS text, ' .
$this->tableFields['href'] . ' AS href, ' .
$this->tableFields['title'] . ' AS title, ' .
$this->tableFields['icon'] . ' AS icon, ' .
$this->tableFields['target'] . ' AS target, ' .
$this->tableFields['expanded'] . ' AS expanded
FROM ' . $this->tableName . '
WHERE ' . $this->tableFields['id'] . ' <> 1
ORDER BY ' . $this->tableFields['orderfield'] . ', ' . $this->tableFields['text'] . ' ASC
$this->_tmpArray = array();
$this->_tmpArray[$row['id']]['parent_id'] = $row['parent_id'];
$this->_tmpArray[$row['id']]['text'] = $row['text'];
$this->_tmpArray[$row['id']]['href'] = $row['href'];
$this->_tmpArray[$row['id']]['title'] = $row['title'];
$this->_tmpArray[$row['id']]['icon'] = $row['icon'];
$this->_tmpArray[$row['id']]['target'] = $row['target'];
$this->_tmpArray[$row['id']]['expanded'] = $row['expanded'];
$this->tableFields_i18n['id'] . ' AS id, ' .
$this->tableFields_i18n['text'] . ' AS text, ' .
$this->tableFields_i18n['title'] . ' AS title
FROM ' . $this->tableName_i18n . '
WHERE ' . $this->tableFields_i18n['id'] . ' <> 1
AND ' . $this->tableFields_i18n['language'] . ' = ' . "'$language'" . '
if (isset ($this->_tmpArray[$row['id']])) {
$this->_tmpArray[$row['id']]['text'] = $row['text'];
$this->_tmpArray[$row['id']]['title'] = $row['title'];
$this->_depthFirstSearch($menu_name, $this->_tmpArray, 1, 1);
$time_end = $this->_getmicrotime();
$time = $time_end - $time_start;
print "TIME ELAPSED = $time\n<br>";
$this->_lastItem[$menu_name] = count($this->tree);
$this->_nodesCount = $this->_lastItem[$menu_name];
$this->tree[$this->_lastItem[$menu_name]+ 1]['level'] = 0;
$this->_postParse($menu_name);
return ((float) $usec + (float) $sec);
* Recursive method to perform the depth-first search of the tree data taken from the current menu table
* @param string $menu_name the name to be attributed to the menu
* whose structure has to be parsed
* @param array $tmpArray the temporary array that stores data to perform
* @param integer $parent_id id of the item whose children have
* @param integer $level the hierarchical level of children to be searched for
function _depthFirstSearch($menu_name, $tmpArray, $parent_id= 1, $level= 1)
while (list ($id, $foobar) = each($tmpArray)) {
if ($foobar['parent_id'] == $parent_id) {
unset ($this->_tmpArray[$id]);
$cnt = count($this->tree) + 1;
$this->tree[$cnt]['level'] = $level;
$this->tree[$cnt]['text'] = $foobar['text'];
$this->tree[$cnt]['href'] = $foobar['href'];
$this->tree[$cnt]['title'] = $foobar['title'];
$this->tree[$cnt]['icon'] = $foobar['icon'];
$this->tree[$cnt]['target'] = $foobar['target'];
$this->tree[$cnt]['expanded'] = $foobar['expanded'];
$this->treecnt[$menu_name][$id] = $cnt;
$this->_depthFirstSearch($menu_name, $this->_tmpArray, $id, $level+ 1);
* A method providing parsing needed after both file/string parsing and DB table parsing
* @param string $menu_name the name of the menu for which the parsing
$menu_name = '' // non consistent default...
for ($cnt= $this->_firstItem[$menu_name]; $cnt<= $this->_lastItem[$menu_name]; $cnt++ ) { // this counter scans all nodes of the new menu
$this->tree[$cnt]['child_of_root_node'] = ($this->tree[$cnt]['level'] == 1);
$this->tree[$cnt]['parsed_text'] = stripslashes($this->tree[$cnt]['text']);
$this->tree[$cnt]['parsed_href'] = (ereg_replace(' ', '', $this->tree[$cnt]['href']) == '') ? '#' : $this->prependedUrl . $this->tree[$cnt]['href'];
$this->tree[$cnt]['parsed_title'] = ($this->tree[$cnt]['title'] == '') ? '' : ' title="' . stripslashes($this->tree[$cnt]['title']) . '"';
$fooimg = $this->icondir . $this->tree[$cnt]['icon'];
if ($this->tree[$cnt]['icon'] != '' && (substr($this->tree[$cnt]['icon'], 0, 7) == 'http://' || substr($this->tree[$cnt]['icon'], 0, 8) == 'https://')) {
$this->tree[$cnt]['parsed_icon'] = $this->tree[$cnt]['icon'];
if ($this->issetIconsize) {
$this->tree[$cnt]['iconwidth'] = $this->iconsize['width'];
$this->tree[$cnt]['iconheight'] = $this->iconsize['height'];
$this->tree[$cnt]['iconwidth'] = $foobar[0];
$this->tree[$cnt]['iconheight'] = $foobar[1];
} elseif ($this->tree[$cnt]['icon'] != '' && file_exists($fooimg)) {
$this->tree[$cnt]['parsed_icon'] = $this->iconwww . $this->tree[$cnt]['icon'];
if ($this->issetIconsize) {
$this->tree[$cnt]['iconwidth'] = $this->iconsize['width'];
$this->tree[$cnt]['iconheight'] = $this->iconsize['height'];
$this->tree[$cnt]['iconwidth'] = $foobar[0];
$this->tree[$cnt]['iconheight'] = $foobar[1];
$this->tree[$cnt]['parsed_icon'] = '';
$this->tree[$cnt]['parsed_target'] = ($this->tree[$cnt]['target'] == '') ? '' : ' target="' . $this->tree[$cnt]['target'] . '"';
// $this->tree[$cnt]['expanded'] = ($this->tree[$cnt]['expanded'] == '') ? 0 : $this->tree[$cnt]['expanded'];
$this->_maxLevel[$menu_name] = max($this->_maxLevel[$menu_name], $this->tree[$cnt]['level']);
if ($this->tree[$cnt]['level'] == 1) {
$this->_firstLevelCnt[$menu_name]++ ;
* A method to replace strings in all URLs (hrefs) of a menu
* @param string $menu_name the name of the menu for which the replacement
* @param string $string the string to be replaced
* @param string $value the replacement string
for ($cnt= $this->_firstItem[$menu_name]; $cnt<= $this->_lastItem[$menu_name]; $cnt++ ) { // this counter scans all nodes of the new menu
$this->tree[$cnt]['parsed_href'] = str_replace($string, $value, $this->tree[$cnt]['parsed_href']);
* A method to set the same target for all links of a menu
* @param string $menu_name the name of the menu for which the targets
* @param string $target the target to be set for all links
for ($cnt= $this->_firstItem[$menu_name]; $cnt<= $this->_lastItem[$menu_name]; $cnt++ ) { // this counter scans all nodes of the new menu
$this->tree[$cnt]['parsed_target'] = ' target="' . $target . '"';
* A method to select the current item of $menu_name in terms of $cnt, i.e., very likely, in terms of its line number in the corresponding menu structure file (excluding from the count commented out lines, if any)
* @param string $menu_name the name of the menu for which the current item
* @param integer $count the line number of the current item
* in the corresponding menu structure file
* (excluding from the count commented out lines, if any)
$this->error("setSelectedItemByCount: the \$count argument is $count, but \$count can not be lower than 1");
if ($count > $this->_lastItem[$menu_name] - $this->_firstItem[$menu_name] + 1) {
$this->error("setSelectedItemByCount: the \$count argument is $count and is larger than the number of items of the '$menu_name' menu");
$cnt = $this->_firstItem[$menu_name] + $count - 1;
$this->tree[$cnt]['selected'] = true;
* A method to select the current item of $menu_name in terms of the corresponding id (see the DB table structure); obviously, this method can be used only together with the DB support
* @param string $menu_name the name of the menu for which the current item
* @param integer $id the id of the current item in the corresponding DB table
if (!isset ($this->treecnt[$menu_name][$id])) {
$this->error("setSelectedItemById: there is not any item with \$id = $id in the '$menu_name' menu");
$cnt = $this->treecnt[$menu_name][$id];
$this->tree[$cnt]['selected'] = true;
* A method to select the current item of $menu_name specifying a string that occurs in the current URL
* @param string $menu_name the name of the menu for which the current item
* @param string $url a string that occurs in the current URL
for ($cnt= $this->_firstItem[$menu_name]; $cnt<= $this->_lastItem[$menu_name]; $cnt++ ) { // this counter scans all nodes of the new menu
if (!(strpos($this->tree[$cnt]['parsed_href'], $url) === false)) {
$this->tree[$cnt]['selected'] = true;
* A method to select the current item of $menu_name specifying a regular expression that matches (a substring of) the current URL; just the same as the setSelectedItemByUrl() method, but using eregi() instead of strpos()
* @param string $menu_name the name of the menu for which the current item
* @param string $url_eregi the regular expression that matches
* (a substring of) the current URL
for ($cnt= $this->_firstItem[$menu_name]; $cnt<= $this->_lastItem[$menu_name]; $cnt++ ) { // this counter scans all nodes of the new menu
if (eregi($url_eregi, $this->tree[$cnt]['parsed_href'])) {
$this->tree[$cnt]['selected'] = true;
* Method to handle errors
* @param string $errormsg the error message
function error($errormsg)
print "<b>LayersMenu Error:</b> $errormsg<br />\n";
if ($this->haltOnError == 'yes') {
die("<b>Halted.</b><br />\n");
|