function xmlParse()
 {
     if (!($this->xmlParser = xml_parser_create("UTF-8"))) {
         xmlHandler::setErr("Couldn't create XML parser!");
     }
     xml_set_object($this->xmlParser, $this);
     xml_set_element_handler($this->xmlParser, "startElementHandler", "endElementHandler");
     xml_set_character_data_handler($this->xmlParser, "characterDataHandler");
     if (!xml_parse($this->xmlParser, $this->xmlData)) {
         xmlHandler::setErr("Couldn't read XML");
     }
     return $this->xmlReturnData;
     xml_parser_free($this->xmlParser);
 }
Example #2
0
<?php

require_once 'xmlHandler.php';
if (!isset($_COOKIE["name"])) {
    header("Location: error.html");
    exit;
}
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
if (!$xmlh->fileExist()) {
    header("Location: error.html");
    exit;
}
// get the name from the cookie
$name = $_COOKIE["name"];
$xmlh->openFile();
// get the users element
$users_node = $xmlh->getElement("users");
// get all user nodes
$users_array = $xmlh->getChildNodes("user");
if ($users_array != null) {
    // delete the current user from the users element
    foreach ($users_array as $user) {
        $username = $xmlh->getAttribute($user, "name");
        if ($username == $name) {
            $xmlh->removeElement($users_node, $user);
        }
    }
}
$xmlh->saveFile();
// clear the cookie
Example #3
0
        }
        if (!move_uploaded_file($file['tmp_name'], $picName)) {
            echo "<font color='#FF0000'>error</font>";
            exit;
        } else {
        }
    }
}
if (empty($picName)) {
    $imgpath = "./image/auto.png";
} else {
    $imgpath = $picName;
}
require_once 'xmlHandler.php';
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
if (!$xmlh->fileExist()) {
    header("Location: error.html");
    exit;
}
$xmlh->openFile();
// get the 'users' element as the current element
$users_element = $xmlh->getElement("users");
// create a 'user' element for each user
$user_element = $xmlh->addElement($users_element, "user");
// add the name
$xmlh->setAttribute($user_element, "name", $_POST["name"]);
$xmlh->setAttribute($user_element, "pic", $imgpath);
$xmlh->saveFile();
// set the name to the cookie
setcookie("name", $_POST["name"]);
Example #4
0
 private function dev_link($die = true, $quick = false)
 {
     $realpages = listFiles(CONS_PATH_PAGES . $_SESSION['CODE'] . "/template/", "@^[^_](.*)\\.htm(l?)\$@i");
     $tmp = listFiles(CONS_PATH_PAGES . $_SESSION['CODE'] . "/actions/");
     $pages = array();
     foreach ($realpages as $x => $page) {
         // removes extensions
         $page = explode(".", $page);
         array_pop($page);
         $pages[] = implode(".", $page);
     }
     foreach ($tmp as $x => $page) {
         // adds actions that are not covered in content/template
         $page = explode(".", $page);
         array_pop($page);
         $page = implode(".", $page);
         if (!in_array($page, $pages)) {
             $pages[] = $page;
         }
     }
     if (isset($this->parent->dimconfig['_contentManager'])) {
         // add CMS pages
         if (!is_array($this->parent->dimconfig['_contentManager'])) {
             $this->parent->dimconfig['_contentManager'] = explode(",", $this->parent->dimconfig['_contentManager']);
         }
         foreach ($this->parent->dimconfig['_contentManager'] as $page) {
             $page = substr($page, 1);
             // removes initial /
             if ($page != '' && !in_array($page, $pages)) {
                 $pages[] = $page;
             }
         }
     }
     if (isset($this->parent->dimconfig['_seoManager'])) {
         // add SEO alias
         if (!is_array($this->parent->dimconfig['_seoManager'])) {
             $this->parent->dimconfig['_seoManager'] = explode(",", $this->parent->dimconfig['_seoManager']);
         }
         foreach ($this->parent->dimconfig['_seoManager'] as $page) {
             if ($page != '') {
                 if ($page[0] == '/') {
                     $page = substr($page, 1);
                 }
                 // removes initial /
                 if ($page != '' && !in_array($page, $pages)) {
                     $pages[] = $page;
                 }
             }
         }
     }
     $missingContent = array();
     if (!function_exists('xmlParamsParser')) {
         include CONS_PATH_SYSTEM . "lib/xmlHandler.php";
     }
     foreach ($realpages as $page) {
         $xhtml = new xmlHandler();
         $xhtml->cReadXML(CONS_PATH_PAGES . $_SESSION['CODE'] . "/template/" . $page, array(C_XML_AUTOPARSE => true, C_XML_LAX => true), true);
         $objects = $xhtml->XMLParsedContent();
         foreach ($objects[C_XHTML_LINKS] as $link) {
             $oL = $link;
             if ($link == '') {
                 $missingContent[] = $page . ": Empty link";
             } else {
                 if ($link[0] != "?" && $link[0] != "#" && strpos($link, "javascript:") === false && strpos($link, "mailto:") === false && strpos($link, "http") === false) {
                     if (strpos($link, "{") !== false) {
                         $link = explode("{", $link);
                         $newLink = "";
                         foreach ($link as $part) {
                             $part = explode("}", $part);
                             $part[0] = explode("|", $part[0]);
                             if ($part[0][0] == "seo") {
                                 $part = $part[0][1];
                                 if (strpos($part, ".") !== false) {
                                     $part = explode(".", $part);
                                     array_pop($part);
                                     $part = implode(".", $part);
                                 }
                                 if (strpos($part, "?") !== false) {
                                     $part = explode("?", $part);
                                     array_pop($part);
                                     $part = implode("?", $part);
                                 }
                                 if ($part != '' && $part[0] == '/') {
                                     $part = substr($part, 1);
                                 }
                                 // removes initial /
                                 if (!in_array($part, $pages)) {
                                     $missingContent[] = $page . ": " . $part . " (SEO from {$oL}) ";
                                 }
                             }
                         }
                     } else {
                         if (strpos($link, ".") !== false) {
                             $link = explode(".", $link);
                             array_pop($link);
                             $link = implode(".", $link);
                         }
                         if (strpos($link, "?") !== false) {
                             $link = explode("?", $link);
                             array_pop($link);
                             $link = implode("?", $link);
                         }
                         if ($link != '' && $link[0] == '/') {
                             $link = substr($link, 1);
                         }
                         // removes initial /
                         if (!in_array($link, $pages)) {
                             $missingContent[] = $page . ": " . $link . " ({$oL})";
                         }
                     }
                 }
             }
         }
     }
     if (!$quick) {
         if (count($missingContent) == 0) {
             echo "No link issues on " . count($realpages) . " pages";
         } else {
             echo "<b>" . count($missingContent) . " link issues on " . count($realpages) . " pages:</b><br/>";
             echo implode($missingContent, "<br/>\n");
         }
     }
     if ($die) {
         $this->parent->close(true);
         die;
     }
     return count($missingContent) > 0 ? $missingContent : false;
 }
Example #5
0
    if ($cached !== false) {
        return $cached;
    }
}
if (!function_exists("fget")) {
    include_once CONS_PATH_INCLUDE . "loadURL.php";
}
if (!function_exists("xmlParamsParser")) {
    include_once CONS_PATH_INCLUDE . "xmlHandler.php";
}
$feed = loadURL($url);
// reads feed
$output = array();
if ($feed !== false) {
    // valid content
    $feedXML = new xmlHandler();
    // parses into XML
    $feed = $feedXML->parseXML($feed[1], array(C_XML_RAW => true, C_XML_AUTOPARSE => true, C_XML_LAX => true, C_XML_REMOVECOMMENTS => true));
    // locates CHANNEL or channel (could skip this and get only items)
    $channel = $feed->getNode("CHANNEL");
    if ($channel === false) {
        $channel = $feed->getNode("channel");
    }
    if ($channel !== false) {
        // found channel
        $items = $channel[0]->getNode("ITEM");
        // <-- note this supports only the first channel
        if ($items == false) {
            $items = $channel->getNode("item");
        }
        // locate item's
if (!isset($_COOKIE["name"])) {
    header("Location: error.html");
    return;
}
// get the name from cookie
$name = $_COOKIE["name"];
$color = $_POST["color"];
// get the message content
$message = $_POST["message"];
if (trim($message) == "") {
    $message = "__EMPTY__";
}
require_once 'xmlHandler.php';
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
if (!$xmlh->fileExist()) {
    header("Location: error.html");
    exit;
}
// create the following DOM tree structure for a message and add it to the chatroom XML file
$xmlh->openFile();
// get the 'messages' element as the current element
$messages_element = $xmlh->getElement("messages");
// create a 'message' element for each message
$message_element = $xmlh->addElement($messages_element, "message");
// add the name
$xmlh->setAttribute($message_element, "name", $name);
$xmlh->setAttribute($message_element, "color", $color);
// add the content of the message
$xmlh->addText($message_element, $message);
Example #7
0
<?php

if (!isset($_COOKIE["name"])) {
    header("Location: error.html");
    return;
}
require_once 'xmlHandler.php';
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
if (!$xmlh->fileExist()) {
    header("Location: error.html");
    exit;
}
// get the name from the cookie
$name = $_COOKIE["name"];
$xmlh->openFile();
// get the users element
$users_node = $xmlh->getElement("users");
// get all user nodes
$users_array = $xmlh->getChildNodes("user");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>User List</title>
	<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
	<table border="1" cellspacing="0" cellpadding="5" align="left">
		<tr bgcolor="#ffffff">
Example #8
0
            //already set
        } elseif (isset($uploadmanager->exifdate)) {
            $smarty->assign('imagetaken', $uploadmanager->exifdate);
            //$smarty->assign('imagetakenmessage', ' ('.$uploadmanager->exifdate.' stated in exif header)');
        } else {
            $smarty->assign('imagetaken', '--');
        }
        if (isset($_SESSION['last_imagetaken'])) {
            $smarty->assign('last_imagetaken', $_SESSION['last_imagetaken']);
        }
        $smarty->assign('today_imagetaken', date("Y-m-d"));
    }
    //which step to display?
    $smarty->assign('step', $step);
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        customExpiresHeader(3600, false, true);
    }
} elseif (!empty($_POST['rss'])) {
    $xh = new xmlHandler();
    $nodeNames = array("PHOTO:THUMBNAIL", "PHOTO:IMGSRC", "TITLE");
    $xh->setElementNames($nodeNames);
    $xh->setStartTag("ITEM");
    $xh->setVarsDefault();
    $xh->setXmlParser();
    $xh->setXmlData(stripslashes($_POST['rss']));
    $pData = $xh->xmlParse();
    $smarty->assign_by_ref('pData', array_slice($pData, 0, 10));
} else {
    $template = "puploader_login.tpl";
}
$smarty->display($template, $cacheid);
Example #9
0
 function buildAdminMenu()
 {
     // this function builds the Ttree object for the menu, but does not handle the HTML. The menu stays in the private var $this->menudata
     if (!isset($_SESSION[CONS_SESSION_ACCESS_USER]['id_group'])) {
         return;
     }
     $file = CONS_PATH_CACHE . $_SESSION['CODE'] . "/admin" . $_SESSION[CONS_SESSION_ACCESS_USER]['id_group'] . ".cache";
     // HTML output with normal menu
     if (!is_file($file) || $this->parent->debugmode || isset($_REQUEST['nocache'])) {
         if (is_file($file)) {
             unlink($file);
         }
         if (!is_file(CONS_PATH_PAGES . $_SESSION['CODE'] . "/_config/admin.xml")) {
             $this->parent->errorControl->raise(517, "buildAdminMenu", "admin");
         }
         if (!defined('C_XHTML_AUTOTAB')) {
             include CONS_PATH_INCLUDE . "xmlHandler.php";
         }
         $xml = new xmlHandler();
         $menuXML = $xml->cReadXML(CONS_PATH_PAGES . $_SESSION['CODE'] . "/_config/admin.xml", array('C_XML_autoparse' => true, 'C_XML_lax' => true), false);
         $menu = array();
         $this->parent->lockPermissions();
         // guarantee permissions are loaded
         $this->addMenuItens($menuXML->getbranch(0), $menu, 0, $this->parent);
         if (!function_exists("mysort")) {
             function mysort($a, $b)
             {
                 if ($a['id_parent'] == $b['id_parent']) {
                     return $a['id'] <= $b['id'] ? -1 : 1;
                     // itens on the same level sorted by id
                 } else {
                     return $a['id_parent'] <= $b['id_parent'] ? -1 : 1;
                 }
                 // menus sorted by ID
             }
         }
         usort($menu, 'mysort');
         $this->menudata = new TTree();
         $this->menudata->arrayToTree($menu, '\\', 'id_parent', 'title');
         // save caches
         cWriteFile($file, serialize($this->menudata));
         // <- ttree object
     } else {
         @($this->menudata = unserialize(cReadFile($file)));
         if ($this->menudata === false || !is_object($this->menudata)) {
             $this->parent->log[] = "Error loading admin menu";
             $this->parent->setLog(CONS_LOGGING_ERROR);
         }
     }
 }
Example #10
0
<?php

require_once 'xmlHandler.php';
if (!isset($_COOKIE["name"])) {
    header("Location: error.html");
    exit;
}
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
if (!$xmlh->fileExist()) {
    header("Location: error.html");
    exit;
}
// get user name from cookie
$name = $_COOKIE["name"];
print $name;
// open the existing XML file
$xmlh->openFile();
// get the 'users' element
$users_element = $xmlh->getElement("users");
// get all 'user' nodes
$user_element = $xmlh->getChildNodes("user");
if ($user_element != null) {
    // delete the current user from the users element
    foreach ($user_element as $user) {
        $username = $xmlh->getAttribute($user, "name");
        if ($username == $name) {
            // Remove picture from our images folder
            $uploadpic = $xmlh->getAttribute($user, "pic-upload");
            unlink($uploadpic);
            // Remove the user element completely
Example #11
0
function parseHTML($html, $simplify = false)
{
    // reads and echos HTML (text) ... simple? not so fast, this must validate to echo, so if the HTML is broken, it will echo what CAN be understood
    // returns false if unable to parse
    $xObj = new xmlHandler();
    $html = $xObj->parseXML($html, array(C_XML_LAX => true, C_XML_REMOVECOMMENTS => true), false, true);
    // don't parse parameters, silent
    if ($html !== false) {
        $ok = $xObj->outputHTML($html, false, true, $simplify);
        unset($xObj);
        if ($ok !== false) {
            return $ok;
        }
    } else {
        unset($xObj);
    }
    return false;
}
Example #12
0
<?php

require_once 'xmlHandler.php';
$datasize = $_POST["datasize"];
// set the execution time limit for this php session to 60 seconds (default is in php.ini)
set_time_limit(60);
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
// check if the chatroom XML data file exists
if (!$xmlh->fileExist()) {
    // create new XML file
    $xmlh->openFile();
    // create a 'chatroom' element, as root element
    $root = $xmlh->addRootElement("chatroom");
    // create a 'messages' element for the set of messages
    $xmlh->addElement($root, "users");
    $xmlh->addElement($root, "messages");
    // save the XML file
    $xmlh->saveFile();
}
$start = gettimeofday();
while (1) {
    // if the file size of chatroom.xml is changed, that means new message has been added
    if ($xmlh->fileExist()) {
        // if data size is changed,
        // stop running this while loop to send messages to user
        if ($_POST["datasize"] != filesize("chatroom.xml")) {
            break;
        }
    }
    // clear the internal cache of file system functions from php
 function picasa_pre_upload()
 {
     global $serendipity;
     if (!serendipity_userLoggedIn()) {
         if (!serendipity_login()) {
             // save off the rss data because it won't be posted again
             if ($_POST['rss']) {
                 $_SESSION['picasa_rss'] = $_POST['rss'];
             }
             echo "<html>\n";
             echo "<head>\n";
             echo "<script language=javascript> function sf() { document.getElementById('serendipity[user]').focus(); }</script>\n";
             echo "</head>\n";
             echo "<body onload='javscript:sf()'>\n";
             echo "<form name='f' method='post' action='index.php?/plugin/picasa_pre_upload'>\n";
             echo "<h2>" . PLUGIN_EVENT_PICASA_UPLOAD_HEADER . $serendipity['baseURL'] . "</h2>\n";
             echo PLUGIN_EVENT_PICASA_UPLOAD_USERNAME . "<br />\n";
             echo "<input type='text' name='serendipity[user]' /><br />\n";
             echo PLUGIN_EVENT_PICASA_UPLOAD_PASSWORD . "<br />\n";
             echo "<input type='password' name='serendipity[pass]' /><br />\n";
             echo "<input id='autologin' type='checkbox' name='serendipity[auto]' /><label for='autologin'>" . PLUGIN_EVENT_PICASA_UPLOAD_REMEMBER_LOGIN . "</label><br />\n";
             echo "<input type='submit' name='submit' value='" . PLUGIN_EVENT_PICASA_UPLOAD_LOGIN . "' />";
             echo "<input type='button' value='" . PLUGIN_EVENT_PICASA_UPLOAD_DISCARD . "' onclick=\"location.href='minibrowser:close'\">\n";
             echo "</form>\n";
             echo "</body>\n";
             echo "</html>\n";
             return;
         }
     }
     if (!$_POST['rss']) {
         if (!$_SESSION['picasa_rss']) {
             echo PLUGIN_EVENT_PICASA_ERR_MISSING_RSS;
             return;
         } else {
             $rss = $_SESSION['picasa_rss'];
         }
     } else {
         $rss = $_POST['rss'];
     }
     $imgSize = $this->get_config('upload_image_size');
     $thumbSize = $serendipity['thumbSize'];
     $xh = new xmlHandler();
     $nodeNames = array("PHOTO:THUMBNAIL", "PHOTO:IMGSRC", "TITLE", "DESCRIPTION");
     $xh->setElementNames($nodeNames);
     $xh->setStartTag("ITEM");
     $xh->setXmlData($rss);
     $pData = $xh->xmlParse();
     // save this since we need to access the descriptions during upload
     $_SESSION['picasa_rss_parsed'] = $pData;
     echo "<html>\n";
     echo "<head>\n";
     echo "<script language=javascript> function sf() { document.getElementById('albumName').focus(); }</script>\n";
     echo "</head>\n";
     echo "<body onload='javscript:sf()'>\n";
     echo "<form name='f' method='post' action='index.php?/plugin/picasa_upload'>\n";
     echo "<h2>" . PLUGIN_EVENT_PICASA_UPLOAD_HEADER . $serendipity['baseURL'] . "</h2>\n";
     echo "<div>" . PLUGIN_EVENT_PICASA_UPLOAD_ALBUMNAME . "</div>\n";
     echo "<div><input type='text' name='albumName' tabindex='1'></div>\n";
     echo "<div>" . PLUGIN_EVENT_PICASA_UPLOAD_DESCRIPTION . "</div>\n";
     echo "<div><textarea name='albumDescription' rows='5' cols='50'></textarea></div>\n";
     echo "<div>" . PLUGIN_EVENT_PICASA_UPLOAD_PARENTDIR . "</div>\n";
     echo "<select name='parentDir' id='parentDir'>\n";
     echo "<option value=''>" . PLUGIN_EVENT_PICASA_UPLOAD_PARENTDIR_BASEDIR . "</option>\n";
     $picasapath = $this->get_config('picasapath');
     $paths = serendipity_traversePath($picasapath);
     $prunedPaths = array();
     foreach ($paths as $path) {
         $name = $path['name'];
         $relpath = $path['relpath'];
         // check if this is a subdirectory of an already pruned directory
         $subdirOfPruned = false;
         foreach ($prunedPaths as $prunedPath) {
             if (0 == strncmp($prunedPath, $relpath, strlen($prunedPath))) {
                 $subdirOfPruned = true;
                 break;
             }
         }
         // don't allow nesting of albums; if the album has any subdirectories, collisions could happen
         if (!file_exists($picasapath . '/' . $relpath . '/index.xml')) {
             if (!$subdirOfPruned) {
                 $splitPath = explode('/', $relpath);
                 $encodedRelpath = htmlentities($relpath, ENT_QUOTES, LANG_CHARSET);
                 $prefix = str_repeat('&nbsp;&nbsp;', count($splitPath));
                 echo "<option value='{$encodedRelpath}'>{$prefix} {$name}</option>\n";
             }
         } else {
             $prunedPaths[] = $relpath;
         }
     }
     echo "</select>\n";
     // Image request queue: add image requests for base image & clickthrough
     foreach ($pData as $e) {
         // use a thumbnail if you don't want exif (saves space)
         // thumbnail requests are clamped at 144 pixels
         // (negative values give square-cropped images)
         $small = $e['photo:thumbnail'] . "?size={$thumbSize}";
         $large = $e['photo:imgsrc'] . "?size={$imgSize}";
         echo "<input type='hidden' name='{$large}'>\n";
         echo "<input type='hidden' name='{$small}'>\n";
     }
     echo "<br />\n";
     echo "<input type=submit value='" . PLUGIN_EVENT_PICASA_UPLOAD_UPLOAD . "'>\n";
     echo "<input type=button value='" . PLUGIN_EVENT_PICASA_UPLOAD_DISCARD . "' onclick=\"location.href='minibrowser:close'\">\n";
     echo "</form><br />\n";
     // Preview "tray": draw thumbnails of each image that will be uploaded
     foreach ($pData as $e) {
         $thumb = $e['photo:thumbnail'];
         echo "<img src='{$thumb}?size={$thumbSize}'>\n";
     }
     echo "</body>\n";
     echo "</html>\n";
 }
Example #14
0
 function __construct($path = null)
 {
     $this->xmlHandler = new xmlHandler();
     $this->pages = $this->xmlHandler->ParserMain(empty($path) ? 'data/xml/page.xml' : $path . 'data/xml/page.xml');
 }
Example #15
0
 function loadMetadata()
 {
     if (!$this->debugmode) {
         return parent::loadMetadata();
     }
     $this->errorControl->raise(1000);
     $this->log = array();
     // we don't want the above "log" to cause an abort (yes, this function uses the log size to confirm an error - lame but extremelly effective)
     $this->allModulesLoaded = true;
     # initial clean up and check
     if (!is_dir(CONS_PATH_TEMP)) {
         safe_mkdir(CONS_PATH_TEMP);
     }
     if (!is_dir(CONS_PATH_CACHE)) {
         safe_mkdir(CONS_PATH_CACHE);
     }
     if (!is_dir(CONS_PATH_DINCONFIG)) {
         safe_mkdir(CONS_PATH_DINCONFIG);
     }
     if (!is_dir(CONS_PATH_CACHE . "locale/")) {
         safe_mkdir(CONS_PATH_CACHE . "locale/");
     }
     if (!is_dir(CONS_PATH_LOGS)) {
         safe_mkdir(CONS_PATH_LOGS);
     }
     if (!is_dir(CONS_PATH_LOGS . $_SESSION['CODE'] . "/")) {
         safe_mkdir(CONS_PATH_LOGS . $_SESSION['CODE'] . "/");
     }
     if (!is_dir(CONS_PATH_DINCONFIG . $_SESSION['CODE'] . "/")) {
         safe_mkdir(CONS_PATH_DINCONFIG . $_SESSION['CODE'] . "/");
     }
     if (!is_dir(CONS_PATH_CACHE . $_SESSION['CODE'] . "/")) {
         safe_mkdir(CONS_PATH_CACHE . $_SESSION['CODE'] . "/");
     }
     if (!is_dir(CONS_PATH_CACHE . $_SESSION['CODE'] . "/meta/")) {
         safe_mkdir(CONS_PATH_CACHE . $_SESSION['CODE'] . "/meta/");
     }
     if (!is_dir(CONS_PATH_CACHE . $_SESSION['CODE'] . "/meta/locale")) {
         safe_mkdir(CONS_PATH_CACHE . $_SESSION['CODE'] . "/meta/locale/");
     }
     if (!is_dir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/")) {
         safe_mkdir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/");
     }
     if (!is_dir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/actions/")) {
         safe_mkdir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/actions");
     }
     if (!is_dir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/content/")) {
         safe_mkdir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/content");
     }
     if (!is_dir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/_config/locale/")) {
         safe_mkdir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/_config/locale");
     }
     if (!is_dir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/files/")) {
         safe_mkdir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/files");
     }
     if (!is_dir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/template/")) {
         safe_mkdir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/template");
         copy(CONS_PATH_SETTINGS . "defaults/basefile.html", CONS_PATH_PAGES . $_SESSION['CODE'] . "/template/basefile.html");
         copy(CONS_PATH_SETTINGS . "defaults/index.html", CONS_PATH_PAGES . $_SESSION['CODE'] . "/template/index.html");
     }
     if (!is_dir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/mail/")) {
         safe_mkdir(CONS_PATH_PAGES . $_SESSION['CODE'] . "/mail");
     }
     # Dimconfig
     if (is_file(CONS_PATH_DINCONFIG . $_SESSION['CODE'] . "/din.dat")) {
         $this->dimconfig = unserialize(cReadFile(CONS_PATH_DINCONFIG . $_SESSION['CODE'] . "/din.dat"));
     }
     if ($this->dimconfig === false) {
         $this->dimconfig = array();
     }
     # Error on load
     $this->checkConfig();
     # clear the meta cache
     if (!$this->offlineMode) {
         $files = listFiles(CONS_PATH_CACHE . $_SESSION['CODE'] . "/meta/");
         foreach ($files as $file) {
             if (is_file(CONS_PATH_CACHE . $_SESSION['CODE'] . "/meta/" . $file)) {
                 @unlink(CONS_PATH_CACHE . $_SESSION['CODE'] . "/meta/" . $file);
             }
         }
         if (!$this->checkinstall()) {
             $this->errorControl->raise(118, array_unshift($this->log));
         }
         if (isset($_REQUEST['nocache'])) {
             recursive_del(CONS_PATH_CACHE . $_SESSION['CODE'] . "/pages/", true);
             recursive_del(CONS_PATH_CACHE . $_SESSION['CODE'] . "/", false, 'cache');
         }
     }
     # If no database, we are done
     if ($this->dbless) {
         return count($this->log) == 0;
     }
     # Search all necessary model files
     $parseXMLparams = array(C_XML_RAW => true, C_XML_AUTOPARSE => true, C_XML_REMOVECOMMENTS => true);
     $xml = new xmlHandler();
     $model = is_file(CONS_PATH_SETTINGS . "default.xml") ? cReadFile(CONS_PATH_SETTINGS . "default.xml") . "\n" : '';
     foreach ($this->loadedPlugins as $scriptName => $scriptObj) {
         if (is_file(CONS_PATH_SYSTEM . "plugins/" . $scriptName . "/meta.xml")) {
             $model .= cReadFile(CONS_PATH_SYSTEM . "plugins/" . $scriptName . "/meta.xml") . "\n";
         }
     }
     unset($scriptName);
     unset($scriptObj);
     if (is_file(CONS_PATH_PAGES . $_SESSION['CODE'] . "/_config/meta.xml")) {
         $model .= cReadFile(CONS_PATH_PAGES . $_SESSION['CODE'] . "/_config/meta.xml") . "\n";
     }
     $model = $xml->parseXML($model, $parseXMLparams, true);
     unset($xml);
     if ($model === false) {
         $this->errorControl->raise(119);
     }
     # browses the XML and loads modules
     $model =& $model->getbranch(0);
     $total = $model->total();
     $relation = array();
     # foreign keys are only created later
     $lastLoad = "";
     for ($c = 0; $c < $total; $c++) {
         # for each module ...
         $thisbranch =& $model->getbranch($c);
         $total_campos = $thisbranch->total();
         # creates the module as from XML settings
         $module = strtolower($thisbranch->data[0]);
         $param =& $thisbranch->data[1];
         $dbname = strtolower(isset($param['dbname']) ? $param['dbname'] : '');
         foreach ($this->modules as $name => $otherModule) {
             if ($otherModule->dbname == $dbname && $dbname != "" && $module != $otherModule->name) {
                 $this->errorControl->raise(120, $otherModule->name, $name, $dbname);
             }
         }
         if ($module == '') {
             $this->errorControl->raise(107, $dbname, "XML error", "Module after {$lastLoad} is corrupt");
         }
         $this->loadModule($module, $dbname);
         #MODULE CREATE
         $lastLoad = $module;
         # loads standard data from this object ---------------------------------------------------------------------
         # read parameters for the MODULE
         foreach ($this->moduleOptions as $mo) {
             $this->modules[$module]->options[$mo[0]] = $mo[3] != '' ? array() : '';
         }
         if (is_array($param)) {
             foreach ($param as $pkey => $pcontent) {
                 $pkey = strtolower($pkey);
                 switch ($pkey) {
                     case "key":
                     case "keys":
                         # will use default auto_increment "id" if none specified. If you specify more than one, none will be auto_increment and the system will use auto-numbering
                         $this->modules[$module]->keys = explode(",", $pcontent);
                         break;
                     case "title":
                         $this->modules[$module]->title = strtolower($pcontent);
                         break;
                     case "volatile":
                         # this module can be deleted as a stand-alone volatile item
                         $this->modules[$module]->options[CONS_MODULE_VOLATILE] = strtolower($pcontent) == "true";
                         break;
                     case "parent":
                         $this->modules[$module]->options[CONS_MODULE_PARENT] = strtolower($pcontent);
                         // field which denotes parenthood
                         break;
                     case "plugins":
                     case "plugin":
                         $this->modules[$module]->plugins = explode(",", strtolower($pcontent));
                         break;
                     case "order":
                         $this->modules[$module]->order = trim(strtolower($pcontent));
                         break;
                     case "permissionoverride":
                         if (strlen($pcontent) >= 9) {
                             $this->modules[$module]->permissionOverride = substr(strtolower($pcontent), 0, 9);
                         }
                         break;
                     case "linker":
                         $this->modules[$module]->linker = true;
                         break;
                     case "systemmodule":
                         $this->modules[$module]->options[CONS_MODULE_SYSTEM] = true;
                         break;
                     case "autoclean":
                         $this->modules[$module]->options[CONS_MODULE_AUTOCLEAN] = $pcontent;
                         break;
                     case "meta":
                         $this->modules[$module]->options[CONS_MODULE_META] = $pcontent;
                         break;
                     case "disallowmultiple":
                         if (strtolower($pcontent) == "true") {
                             $this->modules[$module]->options[CONS_MODULE_DISALLOWMULTIPLE] = true;
                         } else {
                             unset($this->modules[$module]->options[CONS_MODULE_DISALLOWMULTIPLE]);
                         }
                         break;
                     case "noundo":
                         if (strtolower($pcontent) == "true") {
                             $this->modules[$module]->options[CONS_MODULE_NOUNDO] = true;
                         } else {
                             unset($this->modules[$module]->options[CONS_MODULE_NOUNDO]);
                         }
                     default:
                         if ($pkey != "name" && $pkey != "dbname") {
                             $isMO = false;
                             foreach ($this->moduleOptions as $mo) {
                                 if ($mo[1] == $pkey) {
                                     $isMO = true;
                                     if ($mo[2]) {
                                         $pcontent = strtolower($pcontent);
                                     }
                                     if ($mo[3] != '') {
                                         $pcontent = explode($mo[3], $pcontent);
                                     }
                                     $this->modules[$module]->options[$mo[0]] = $pcontent;
                                     break;
                                 }
                             }
                             if (!$isMO) {
                                 $this->modules[$module]->options[$pkey] = $pcontent;
                             }
                         }
                         break;
                 }
             }
             #foreach
             unset($pkey);
             unset($pcontent);
         }
         if ($this->modules[$module]->options[CONS_MODULE_PARENT] != '' && strpos($this->modules[$module]->order, $this->modules[$module]->options[CONS_MODULE_PARENT]) === false) {
             # in tree mode, the field that defines parenthood must be in the order clause, the first if possible
             $this->modules[$module]->order = $this->modules[$module]->options[CONS_MODULE_PARENT] . "+" . ($this->modules[$module]->order != '' ? "," . $this->modules[$module]->order : '');
         }
         # -- ok on reading parameters
         $campos = array();
         $mandatory = 0;
         # browse FIELDS ---------------------------------------------------------------------------------
         for ($campo = 0; $campo < $total_campos; $campo++) {
             $thiscampo =& $thisbranch->getbranch($campo);
             ## processParameters #########################################
             $campos = $this->processParameters($thiscampo, $campos, $module);
             ##############################################################
             $nomecampo = strtolower($thiscampo->data[0]);
             if ($campos[$nomecampo][CONS_XML_TIPO] == CONS_TIPO_LINK) {
                 array_push($relation, array($module, $nomecampo, $campos[$nomecampo][CONS_XML_MODULE]));
                 // if this is a non-mandatory link to myself, called "id_parent", and I don't have parent ... well .. obviously this is it
                 if ($campos[$nomecampo][CONS_XML_MODULE] == $module && !isset($campos[$nomecampo][CONS_XML_MANDATORY]) && $nomecampo == "id_parent" && $this->modules[$module]->options[CONS_MODULE_PARENT] == '') {
                     $this->modules[$module]->options[CONS_MODULE_PARENT] = $nomecampo;
                 }
             } else {
                 if ($campos[$nomecampo][CONS_XML_TIPO] == CONS_TIPO_SERIALIZED) {
                     // browse fields looking for links
                     foreach ($campos[$nomecampo][CONS_XML_SERIALIZEDMODEL] as $exname => &$exfield) {
                         if ($exfield[CONS_XML_TIPO] == CONS_TIPO_LINK) {
                             array_push($relation, array($module, $nomecampo . ":" . $exname, $exfield[CONS_XML_MODULE]));
                         }
                     }
                 }
             }
             # checks if this field can be NULL or NOT depending on options and mandatory setting
             if (isset($campos[$nomecampo][CONS_XML_SQL]) && $campos[$nomecampo][CONS_XML_SQL] != "") {
                 # relation will not be set
                 if (isset($campos[$nomecampo][CONS_XML_MANDATORY]) || $campos[$nomecampo][CONS_XML_TIPO] == CONS_TIPO_OPTIONS || isset($campos[$nomecampo][CONS_XML_DEFAULT])) {
                     $campos[$nomecampo][CONS_XML_SQL] .= " NOT NULL";
                     $mandatory++;
                 } else {
                     $campos[$nomecampo][CONS_XML_SQL] .= " NULL";
                 }
                 if (isset($campos[$nomecampo][CONS_XML_DEFAULT])) {
                     $campos[$nomecampo][CONS_XML_SQL] .= " DEFAULT '" . $campos[$nomecampo][CONS_XML_DEFAULT] . "'";
                 }
             }
         }
         # this module has a database (it's possible to have modules without a database)
         if ($this->modules[$module]->dbname != "") {
             # checks standard key "id" if no key specified
             if (in_array("id", $this->modules[$module]->keys) && !isset($this->modules[$module]->fields['id']) && !isset($campos['id'])) {
                 if ($this->modules[$module]->linker) {
                     $this->modules[$module]->keys = array();
                     $keys = 0;
                     foreach ($campos as $fieldname => $fieldobj) {
                         if (isset($fieldobj[CONS_XML_MODULE])) {
                             $keys++;
                             $this->modules[$module]->keys[] = $fieldname;
                             if ($keys == 2) {
                                 break;
                             }
                         }
                     }
                     unset($fieldname);
                     unset($fieldobj);
                 } else {
                     $campos['id'][CONS_XML_SQL] = "INT (11) UNSIGNED NOT NULL" . (count($this->modules[$module]->keys) <= 1 ? " AUTO_INCREMENT" : "");
                     $campos['id'][CONS_XML_TIPO] = CONS_TIPO_INT;
                     if (count($this->modules[$module]->keys) > 1) {
                         $campos['id'][CONS_XML_RESTRICT] = 99;
                     }
                 }
             }
             # -- keys (this is done to prevent repeated keys)
             $chave = $this->modules[$module]->keys;
             $this->modules[$module]->keys = array();
             foreach ($chave as $x => $di) {
                 if (!in_array($di, $this->modules[$module]->keys) && $di != "") {
                     array_push($this->modules[$module]->keys, $di);
                 }
             }
             unset($x);
             unset($di);
             # if this is a re-definition, will TOTALLY overright the fields (you can redefine fields from the default.xml on the meta.xml)
             $this->modules[$module]->fields = array_merge($this->modules[$module]->fields, $campos);
             # -- makes sure all keys are mandatory and present
             foreach ($this->modules[$module]->keys as $x => $chave) {
                 if (!isset($this->modules[$module]->fields[$chave])) {
                     array_push($this->log, "Key not defined, considering INT 11, please fix the XML: {$module}.{$chave}");
                     $this->modules[$module]->fields[$chave] = array("CONS_XML_SQL" => "INT (11) UNSIGNED NOT NULL", "CONS_XML_TIPO" => CONS_TIPO_INT);
                 }
                 $this->modules[$module]->fields[$chave][CONS_XML_MANDATORY] = true;
                 // vc keys without case specified, force ucase
                 if ($this->modules[$module]->fields[$chave][CONS_XML_TIPO] == CONS_TIPO_VC && !isset($this->modules[$module]->fields[$chave][CONS_XML_SPECIAL])) {
                     $this->modules[$module]->fields[$chave][CONS_XML_SPECIAL] = "ucase";
                 }
             }
             unset($x);
             unset($chave);
         }
     }
     # -- foreach module
     $total_relacoes = count($relation);
     # check our relationship counts and build proper fields or support tables -------------
     for ($c = 0; $c < $total_relacoes; $c++) {
         $rel = $relation[$c];
         # relation: MODULE => FIELD => MODULE or MODULE => SFIELD:FIELD => MODULE for serialized fields
         if (!isset($this->modules[$rel[0]]) || !isset($this->modules[$rel[2]])) {
             array_push($this->log, "Error (pass 1) trying to build foreign keys from '" . $rel[0] . "' to '" . $rel[2] . "' at " . $rel[1] . ": one of the modules do not exist, ignoring relation");
         } else {
             $sfield = "";
             if (strpos($rel[1], ":") !== false) {
                 #serialized field
                 $field = explode(":", $field);
                 $sfield = $field[0];
                 $field = $field[1];
             } else {
                 $field = $rel[1];
             }
             if (substr($field, 0, 3) != "id_") {
                 array_push($this->log, "All relations to another modules MUST start with id_ on " . $rel[0] . "' to '" . $rel[2] . "' at " . $rel[1] . ": should be id_" . $field . " ?");
             }
             if ($sfield == '') {
                 $this->modules[$rel[2]]->volatile = false;
             }
             # keeps volatile if linked from serialized (a.k.a. serialized links are not safe, because they are meant to be dinamic)
             foreach ($this->modules[$rel[2]]->keys as $x => $chave) {
                 # will create required keys for foreign table, except any one in common with this table
                 if ($chave == "id" || !isset($this->modules[$rel[0]]->fields[$chave])) {
                     # only standard id exists (always link it), or it's not a standard key ... still have to test if it's not a key to this table
                     # basically, this will create the second+ keys on multikey relations
                     if (!($this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] == CONS_TIPO_LINK && $this->modules[$rel[2]]->fields[$chave][CONS_XML_MODULE] == $rel[0])) {
                         # ok not a key to this table (the FOREING key is not this table, pay attention! this will still be true for id_parent)
                         if ($sfield == "") {
                             # normal
                             if ($chave == "id") {
                                 # uses the name that came in the XML model
                                 if (!isset($this->modules[$rel[0]]->fields[$field])) {
                                     $this->modules[$rel[0]]->fields[$field] = array();
                                 }
                                 $this->modules[$rel[0]]->fields[$field][CONS_XML_SQL] = str_replace("AUTO_INCREMENT", "", $this->modules[$rel[2]]->fields[$chave][CONS_XML_SQL]);
                                 $this->modules[$rel[0]]->fields[$field][CONS_XML_TIPO] = CONS_TIPO_LINK;
                                 $this->modules[$rel[0]]->fields[$field][CONS_XML_LINKTYPE] = $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] != CONS_TIPO_LINK ? $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] : CONS_TIPO_INT;
                                 $this->modules[$rel[0]]->fields[$field][CONS_XML_MODULE] = $rel[2];
                                 # the creation system might have added this already, that's why testing before resetting the array
                                 if (isset($this->modules[$rel[0]]->fields[$field][CONS_XML_JOIN]) && $this->modules[$rel[0]]->fields[$field][CONS_XML_JOIN] == "inner" || isset($this->modules[$rel[0]]->fields[$field][CONS_XML_MANDATORY])) {
                                     // is set join to INNER or is explicitly mandatory, make sure both are set
                                     $this->modules[$rel[0]]->fields[$field][CONS_XML_MANDATORY] = true;
                                     if ($x == 0) {
                                         $this->modules[$rel[0]]->fields[$field][CONS_XML_JOIN] = "inner";
                                     }
                                 } else {
                                     // no join mode set (defaults to left), set to left, and no explicit mandatory tag
                                     if ($x == 0) {
                                         $this->modules[$rel[0]]->fields[$field][CONS_XML_JOIN] = "left";
                                     }
                                     $this->modules[$rel[0]]->fields[$field][CONS_XML_SQL] = str_replace("NOT NULL", "NULL", $this->modules[$rel[0]]->fields[$field][CONS_XML_SQL]);
                                 }
                             } else {
                                 if ($x == 0) {
                                     $nome = $field;
                                     # first key keeps the original name
                                     $this->modules[$rel[0]]->fields[$field][CONS_XML_LINKTYPE] = $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] != CONS_TIPO_LINK ? $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] : CONS_TIPO_INT;
                                 } else {
                                     $nome = $field . "_" . str_replace("id_", "", $chave);
                                 }
                                 # creates a composition with the model name and the foreign name
                                 $this->modules[$rel[0]]->fields[$nome][CONS_XML_SQL] = str_replace("AUTO_INCREMENT", "", $this->modules[$rel[2]]->fields[$chave][CONS_XML_SQL]);
                                 $this->modules[$rel[0]]->fields[$nome][CONS_XML_TIPO] = $x == 0 ? CONS_TIPO_LINK : $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO];
                                 $this->modules[$rel[0]]->fields[$nome][CONS_XML_MODULE] = isset($this->modules[$rel[2]]->fields[$chave][CONS_XML_MODULE]) ? $this->modules[$rel[2]]->fields[$chave][CONS_XML_MODULE] : $rel[2];
                                 if (isset($this->modules[$rel[0]]->fields[$field][CONS_XML_JOIN]) && $this->modules[$rel[0]]->fields[$field][CONS_XML_JOIN] == "inner" || isset($this->modules[$rel[0]]->fields[$nome][CONS_XML_MANDATORY])) {
                                     $this->modules[$rel[0]]->fields[$nome][CONS_XML_MANDATORY] = true;
                                     if ($x == 0) {
                                         $this->modules[$rel[0]]->fields[$nome][CONS_XML_JOIN] = "inner";
                                     }
                                 } else {
                                     if ($x == 0) {
                                         $this->modules[$rel[0]]->fields[$nome][CONS_XML_JOIN] = "left";
                                     }
                                     unset($this->modules[$rel[0]]->fields[$nome][CONS_XML_MANDATORY]);
                                     $this->modules[$rel[0]]->fields[$nome][CONS_XML_SQL] = str_replace("NOT NULL", "NULL", $this->modules[$rel[0]]->fields[$nome][CONS_XML_SQL]);
                                 }
                             }
                         } else {
                             # serialized
                             if ($chave == "id") {
                                 # uses the name that came in the XML model
                                 if (!isset($this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field])) {
                                     $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field] = array();
                                 }
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_SQL] = str_replace("AUTO_INCREMENT", "", $this->modules[$rel[2]]->fields[$chave][CONS_XML_SQL]);
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_TIPO] = CONS_TIPO_LINK;
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_LINKTYPE] = $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] != CONS_TIPO_LINK ? $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] : CONS_TIPO_INT;
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_MODULE] = $rel[2];
                                 # serialized links cannot be "inner"
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_JOIN] = "left";
                                 if (isset($this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_MANDATORY])) {
                                     $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_MANDATORY] = true;
                                 } else {
                                     $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_SQL] = str_replace("NOT NULL", "NULL", $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_SQL]);
                                 }
                             } else {
                                 if ($x == 0) {
                                     $nome = $field;
                                     # first key keeps the original name
                                     $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$field][CONS_XML_LINKTYPE] = $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] != CONS_TIPO_LINK ? $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO] : CONS_TIPO_INT;
                                 } else {
                                     $nome = $field . "_" . str_replace("id_", "", $chave);
                                 }
                                 # creates a composition with the model name and the foreign name
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$nome][CONS_XML_SQL] = str_replace("AUTO_INCREMENT", "", $this->modules[$rel[2]]->fields[$chave][CONS_XML_SQL]);
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$nome][CONS_XML_TIPO] = $x == 0 ? CONS_TIPO_LINK : $this->modules[$rel[2]]->fields[$chave][CONS_XML_TIPO];
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$nome][CONS_XML_MODULE] = isset($this->modules[$rel[2]]->fields[$chave][CONS_XML_MODULE]) ? $this->modules[$rel[2]]->fields[$chave][CONS_XML_MODULE] : $rel[2];
                                 # serialized links cannot be "inner"
                                 $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$nome][CONS_XML_JOIN] = "left";
                                 if (isset($this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$nome][CONS_XML_MANDATORY])) {
                                     $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$nome][CONS_XML_MANDATORY] = true;
                                 } else {
                                     $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$nome][CONS_XML_SQL] = str_replace("NOT NULL", "NULL", $this->modules[$rel[0]]->fields[$sfield][CONS_XML_SERIALIZEDMODEL][$nome][CONS_XML_SQL]);
                                 }
                             }
                         }
                         # sfield?
                     }
                 }
                 # secondary (multikey)?
             }
             # foreach
             unset($x);
             unset($chave);
             if (!isset($this->modules[$rel[0]]->fields[$field][CONS_XML_SQL])) {
                 array_push($this->log, "Error (pass 2) trying to build foreing keys from " . $rel[0] . " to " . $rel[2] . " at " . $field . ": ignoring relation");
             }
         }
     }
     # foreach for relations
     // now some automatic settings since all modules are loaded, and consistency check on build, partOf, etc ---------------------
     $cacheLinkNum = array();
     // module => modules which link to this
     foreach ($this->modules as $mname => &$module) {
         $links = 0;
         $fieldsRequiredToLinks = 0;
         foreach ($module->fields as $name => $field) {
             // check for linker modules
             if ($field[CONS_XML_TIPO] == CONS_TIPO_LINK && $field[CONS_XML_MODULE] != $mname) {
                 // links to OTHER link not myself
                 $links++;
                 # do not count PARENTS as links
                 $fieldsRequiredToLinks += count($this->modules[$field[CONS_XML_MODULE]]->keys);
                 # a module can have more than one key, thus to know if this module is a linker module, we need to check if ALL THIS HAVE are the keys for 2 modules
                 // vc links that have no case specified, force to upper
                 if ($field[CONS_XML_TIPO] == CONS_TIPO_LINK && $field[CONS_XML_LINKTYPE] == CONS_TIPO_VC && !isset($field[CONS_XML_SPECIAL])) {
                     $this->modules[$mname]->fields[$name][CONS_XML_SPECIAL] = "ucase";
                 }
             }
             if (isset($field[CONS_XML_FILTEREDBY])) {
                 foreach ($field[CONS_XML_FILTEREDBY] as $fbname) {
                     if (!isset($module->fields[$fbname])) {
                         $this->log[] = "Error on filteredby for {$mname}.{$name}: {$fbname} does not exist";
                     } else {
                         if (!isset($this->modules[$module->fields[$fbname][CONS_XML_MODULE]])) {
                             $this->log[] = "Error on filteredby for {$mname}.{$name}: module defined in {$fbname} does not exist";
                         }
                     }
                 }
             }
         }
         if ($links == 2 && count($module->fields) == $fieldsRequiredToLinks || $this->modules[$mname]->linker) {
             # this is a linker module!
             $this->modules[$mname]->linker = true;
         }
         if ($this->modules[$mname]->title == "" && !$this->modules[$mname]->options[CONS_MODULE_SYSTEM] && !$this->modules[$mname]->linker) {
             $this->modules[$mname]->title = $this->modules[$mname]->keys[0];
             // first key
         }
     }
     # here we finished the automatic settings
     # load plugins that are defined by METADATA
     foreach ($this->modules as $name => &$module) {
         foreach ($module->plugins as $sname) {
             if (!isset($this->loadedPlugins[$sname])) {
                 $this->addPlugin($sname, $name);
             } else {
                 $this->loadedPlugins[$sname]->moduleRelation = $name;
             }
         }
     }
     foreach ($this->loadedPlugins as $sname => $obj) {
         if ($obj->name == '' || $obj->name != $sname) {
             $this->errorControl->raise(9, $obj->name, $sname);
         }
     }
     # DIE FREAKING THUMBS.DB, DIE!
     function dieFreakingThumbs($folder)
     {
         if ($folder[strlen($folder) - 1] != '/') {
             $folder .= "/";
         }
         foreach (glob($folder . "*") as $file) {
             if (is_dir($file)) {
                 dieFreakingThumbs($file);
             } else {
                 $arf = explode(".", $file);
                 if (array_pop($arf) == 'db') {
                     @unlink($file);
                 }
             }
         }
     }
     dieFreakingThumbs(CONS_PATH_PAGES . $_SESSION['CODE'] . "/");
     $customxml = is_file(CONS_PATH_PAGES . $_SESSION["CODE"] . "/_config/custom.xml") ? cReadFile(CONS_PATH_PAGES . $_SESSION["CODE"] . "/_config/custom.xml") : '';
     # All plugins are loaded, check their manifest and customs
     foreach ($this->loadedPlugins as $sname => $plugin) {
         if (is_file(CONS_PATH_SYSTEM . "plugins/{$sname}/payloadmanifest.php")) {
             $copyFiles = (include CONS_PATH_SYSTEM . "plugins/{$sname}/payloadmanifest.php");
             foreach ($copyFiles as $from => $to) {
                 if ($from[strlen($from) - 1] == "/" && is_dir($from) && (!is_dir($to) || !CONS_ONSERVER && isset($_REQUEST['nocache']))) {
                     // FOLDER
                     if (!function_exists('recursive_copy')) {
                         include_once CONS_PATH_INCLUDE . "recursive_copy.php";
                     }
                     recursive_copy($from, $to);
                 } else {
                     if (is_file($from) && (!is_file($to) || !CONS_ONSERVER && isset($_REQUEST['nocache']))) {
                         // FILE
                         $path = explode("/", $to);
                         array_pop($path);
                         // bye file
                         $path = implode("/", $path);
                         makeDirs($path);
                         copy($from, $to);
                     }
                 }
             }
         }
         if (is_file(CONS_PATH_SYSTEM . "plugins/{$sname}/custom.xml")) {
             $customxml .= cReadFile(CONS_PATH_SYSTEM . "plugins/{$sname}/custom.xml");
         }
     }
     # Read custom metadata for dimconfig
     if ($customxml != '') {
         $parseXMLparams = array(C_XML_RAW => true, C_XML_AUTOPARSE => true, C_XML_REMOVECOMMENTS => true);
         $xml = new xmlHandler();
         $customxml = $xml->parseXML($customxml, $parseXMLparams, true);
         if ($customxml === false) {
             $this->errorControl->raise(180);
         }
         unset($xml);
         $customxml =& $customxml->getbranch(0);
         $total = $customxml->total();
         $dimconfigMD = array();
         // MetaData -------------------------------------
         for ($c = 0; $c < $total; $c++) {
             # for each module ...
             $thisbranch =& $customxml->getbranch($c);
             $configname = strtolower($thisbranch->data[0]);
             if (!isset($this->dimconfig[$configname])) {
                 $this->dimconfig[$configname] = '';
             }
             $dimconfigMD = $this->processParameters($thisbranch, $dimconfigMD, '');
         }
         foreach ($dimconfigMD as $name => $field) {
             if ($field[CONS_XML_TIPO] == CONS_TIPO_UPLOAD && (!isset($field['location']) || $field['location'][0] == '/')) {
                 $this->errorControl->raise(181, $name, 'dimconfig');
             }
             if ($field[CONS_XML_TIPO] != CONS_TIPO_ENUM) {
                 unset($dimconfigMD[$name][CONS_XML_SQL]);
             }
         }
         cWriteFile(CONS_PATH_CACHE . $_SESSION['CODE'] . "/meta/_dimconfig.dat", serialize($dimconfigMD));
         // this defines the type of each item on dimconfig
     }
     # Apply and raise metadata
     $this->applyMetaData();
     # no log = no error
     return $sucess = count($this->log) == 0;
 }