Example #1
0
function db_get_schema($schema_file, $folder = "", $tview = "", $cache = true, $popup = false)
{
    static $data = array();
    if (!$cache) {
        $data = array();
    }
    $cid = $schema_file . $folder;
    if (!empty($data[$cid])) {
        if ($tview == "") {
            return $data[$cid];
        }
        if (!isset($data[$cid][$tview])) {
            $tview = sys_array_shift(array_keys($data[$cid]));
        }
        return $data[$cid][$tview];
    }
    if (!file_exists($schema_file)) {
        if (basename($schema_file) == "nodb_.xml") {
            sys_warning(sprintf("{t}Folder not found.{/t} (%s)", $folder));
        } else {
            sys_log_message_alert("php-fail", sprintf("{t}Schemafile not found. (%s){/t}", $schema_file . " " . $folder));
        }
        $schema_file = "modules/schema/blank.xml";
    }
    $schema = basename(substr($schema_file, 0, -4));
    $cache_file = SIMPLE_CACHE . "/schema/" . CORE_SGSML_VERSION . "_" . $schema . "_" . LANG . ".ser";
    $custom_schema = "";
    if ($folder != "") {
        if (file_exists(sys_custom($schema_file . "." . $folder))) {
            $schema_file = sys_custom($schema_file . "." . $folder);
            $cache_file .= "." . $folder;
        }
        $custom_schema = db_select_value("simple_sys_tree", "custom_schema", "id=@id@", array("id" => $folder));
        // TODO optimize
        $rows = db_select("simple_sys_custom_fields", array("custom_schema"), array("module=@schema@", "(ffolder='' or ffolder like @folder@)", "activated=1"), "id asc", "", array("schema" => $schema, "folder" => "%|" . $folder . "|%"));
        if (is_array($rows) and count($rows) > 0) {
            $custom_schema = str_replace("</table>", "", $custom_schema);
            if (!strpos($custom_schema, "<table")) {
                $custom_schema = "<table>";
            }
            foreach ($rows as $row) {
                $custom_schema .= $row["custom_schema"] . "\n";
            }
            $custom_schema .= "</table>";
        }
        if ($custom_schema != "") {
            $cache_file .= "." . sha1($custom_schema);
        }
    }
    $custom_dir = sys_custom_dir(substr($schema_file, 0, -4));
    if (is_dir($custom_dir)) {
        $cache_file .= "." . filemtime($custom_dir);
    }
    $schema_mtime = filemtime($schema_file);
    if (APC) {
        $data[$cid] = apc_fetch("sgsml" . basename($cache_file) . $schema_mtime);
    } else {
        if (file_exists($cache_file) and filemtime($cache_file) == $schema_mtime) {
            $data[$cid] = unserialize(file_get_contents($cache_file));
        }
    }
    if (empty($data[$cid])) {
        if (DEBUG and empty($_REQUEST["iframe"])) {
            echo "reload schema";
        }
        $schema_content = sgsml_parser::file_get_contents($schema_file, $schema, $custom_schema);
        $data[$cid] = sgsml_parser::parse_schema($schema_content, $schema, $schema_mtime, $cache_file);
        if (defined("SETUP_DB_HOST")) {
            sys_log_message_log("info", sprintf("{t}Updating schema %s from %s.{/t} {t}Folder{/t}: %s", $schema, $schema_file, $folder));
        }
    }
    if ($tview == "") {
        return $data[$cid];
    }
    if ($folder != "") {
        $write = true;
        if ($popup) {
            $ftype = str_replace("simple_", "", $data[$cid]["att"]["NAME"]);
            if (!in_array($ftype, explode("\n", file_get_contents(sys_custom("modules/core/popup_write.txt"))))) {
                $write = false;
            }
        }
        $superadmin = sys_is_super_admin($_SESSION["username"]);
        foreach (array_keys($data[$cid]["views"]) as $view) {
            if (isset($data[$cid]["views"][$view]["RIGHT"])) {
                $right = $data[$cid]["views"][$view]["RIGHT"];
            } else {
                $right = "read";
            }
            if (($write or $right != "write") and ($superadmin or db_get_right($folder, $right, $view))) {
                continue;
            }
            unset($data[$cid][$view]);
            unset($data[$cid]["views"][$view]);
        }
    }
    if (isset($data[$cid][$tview])) {
        return $data[$cid][$tview];
    } else {
        if ($tview != "none") {
            sys_warning("{t}Item(s) not found or access denied.{/t} (view={$tview})");
        }
        $GLOBALS["tview"] = sys_array_shift(array_keys($data[$cid]["views"]));
        if (empty($GLOBALS["tview"])) {
            return db_get_schema("modules/schema/blank.xml", "", "display");
        }
        return $data[$cid][$GLOBALS["tview"]];
    }
}