Beispiel #1
0
 static function build_views_sql($rows)
 {
     $t = $GLOBALS["t"];
     $tname = $t["title"];
     $tfolder = $t["folder"];
     $today = $_SESSION[$tname]["_" . $tfolder]["today"];
     $type = $_SESSION[$tname]["_" . $tfolder]["markdate"];
     $weekstart = $_SESSION[$tname]["_" . $tfolder]["weekstart"];
     if ($type == "all" or !empty($GLOBALS["current_view"]["HIDE_CALENDAR"])) {
         if (count($t["sqllimit"]) == 2 and count($rows) > $t["sqllimit"][1]) {
             return array_slice($rows, $t["sqllimit"][0], $t["sqllimit"][1]);
         }
         return $rows;
     }
     // fields: 0-start,1-end,2-recurrence,3-until,4-allday,5-repeatinterval,6-repeatexcludes
     $fields = explode(",", $GLOBALS["current_view"]["ENABLE_CALENDAR"]);
     if (count($fields) < 7) {
         return $rows;
     }
     if ($type == "month" or $type == "year" or $type == "gantt") {
         $today_arr = sys_getdate($today);
         if ($type == "month" or $type == "gantt") {
             $start_day = mktime(0, 0, 0, $today_arr["mon"], 1, $today_arr["year"]);
             $last_day = strtotime("+1 month", $start_day);
         } else {
             $start_day = mktime(0, 0, 0, $today_arr["mon"], 1, $today_arr["year"]);
             $last_day = mktime(0, 0, 0, $today_arr["mon"], 1, $today_arr["year"] + 1);
         }
         $num = sys_date("w", $last_day);
         if ((int) $num != (int) $weekstart) {
             $last_day = strtotime("+" . (7 + $weekstart - $num) % 7 . " days", $last_day);
         }
         $num = sys_date("w", $start_day);
         if ($num != $weekstart) {
             $start_day = strtotime("-" . (7 + $num - $weekstart) % 7 . " days", $start_day);
         }
         $all_begin = $start_day;
         $all_end = $last_day - 1;
     } else {
         if ($type == "week") {
             $num = sys_date("w", $today);
             if ($num != $weekstart) {
                 $today = strtotime("-" . (7 + $num - $weekstart) % 7 . " days", $today);
             }
             $all_begin = $today;
             $all_end = strtotime("+1 week -1 second", $today);
         } else {
             if ($type == "day") {
                 $all_begin = $today;
                 $all_end = $today + 86399;
             }
         }
     }
     $removed = 0;
     $all_begin_arr = sys_getdate($all_begin);
     foreach ($rows as $key => $item) {
         $item_start = $item[$fields[0]];
         if ($item_start == "") {
             continue;
         }
         $item_end = $item[$fields[1]];
         $item_until = $item[$fields[3]];
         $item_recurrence = $item[$fields[2]];
         $item_interval = $item[$fields[5]];
         if ($item[$fields[6]] == "") {
             $item_exclusions = array();
         } else {
             $item_exclusions = explode("|", trim($item[$fields[6]], "|"));
         }
         $normalized = self::_build_check_hitdate($all_begin, $all_begin_arr, $all_end, $item_start, $item_end, $item_until, $item_recurrence, $item_interval, $item_exclusions);
         if (count($normalized) == 0) {
             unset($rows[$key]);
             $removed++;
         }
     }
     if ($removed > 0) {
         _asset_process_pages($t["maxdatasets"] - $removed);
     }
     if (count($t["sqllimit"]) == 2 and count($rows) > $t["sqllimit"][1]) {
         $rows = array_slice($rows, $t["sqllimit"][0], $t["sqllimit"][1]);
     }
     return $rows;
 }
Beispiel #2
0
function asset_process_session_request()
{
    $t =& $GLOBALS["t"];
    $tname = $t["title"];
    $tview = $t["view"];
    $tfolder = $t["folder"];
    if (SYNC4J and !empty($GLOBALS["sel_folder"]["anchor"])) {
        _asset_process_syncml_requests();
    }
    if ($tview == "search") {
        if (empty($_SESSION["_" . $tfolder]["request"]["search"])) {
            $_SESSION["_" . $tfolder]["request"]["search"] = array("query" => "*", "module" => "", "subfolders" => 1);
        }
        if (isset($_REQUEST["search"])) {
            if (empty($_REQUEST["module"])) {
                $_REQUEST["module"] = "";
            }
            if (empty($_REQUEST["similar"])) {
                $_REQUEST["similar"] = "";
            }
            if (!isset($_REQUEST["subfolders"])) {
                $_REQUEST["subfolders"] = 1;
            }
            $_SESSION["_" . $tfolder]["request"]["search"] = array("query" => $_REQUEST["search"], "module" => $_REQUEST["module"], "similar" => $_REQUEST["similar"], "subfolders" => $_REQUEST["subfolders"]);
        }
        $t["search"] = $_SESSION["_" . $tfolder]["request"]["search"];
        $value = modify::searchindex($t["search"]["query"]);
        $search_snd = "";
        if (!empty($t["search"]["similar"])) {
            $search_arr = explode(" ", $value);
            foreach ($search_arr as $key => $value) {
                $val = soundex($value);
                if ($val != "0000") {
                    $search_arr[$key] = $val;
                } else {
                    unset($search_arr[$key]);
                }
            }
            $search_snd = implode(" ", $search_arr);
        }
        if ($search_snd != "") {
            $search_snd = "%" . $search_snd . "%";
        }
        if ($value != "") {
            $term_two = "%" . $value . "%";
        } else {
            $term_two = "";
        }
        $search_rights = str_replace("r@right@_", "t.rread_", $_SESSION["permission_sql"]) . " and " . str_replace("r@right@_", "s.rread_", $_SESSION["permission_sql"]);
        if (!isset($GLOBALS["sel_folder"]["lft"])) {
            $folder = array("lft" => 0, "rgt" => 0);
        } else {
            $folder = $GLOBALS["sel_folder"];
        }
        $vars = array("lft" => $folder["lft"], "rgt" => $folder["rgt"], "search" => $value, "search2" => $term_two, "search_snd" => $search_snd, "module" => $t["search"]["module"], "subfolders" => $t["search"]["subfolders"]);
        $t["sqlvars"] = array_merge($t["sqlvars"], $vars);
        $t["sqlvarsnoquote"] = array_merge($t["sqlvarsnoquote"], array("search_rights" => $search_rights));
    }
    if ($t["default_sql"] != "" and $t["default_sql"] != "no_select") {
        $matches = "";
        if (preg_match_all("|@(.*?)@|i", $t["default_sql"], $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                if (count($match) != 2) {
                    continue;
                }
                $req_key = $match[1];
                if (isset($_SESSION[$req_key])) {
                    $t["default_sql"] = str_replace("@" . $req_key . "@", sys_correct_quote($_SESSION[$req_key]), $t["default_sql"]);
                }
            }
        }
    }
    if (isset($_REQUEST["session_remove_request"])) {
        $_SESSION["_" . $tfolder]["request"] = array();
        $_SESSION["_" . $tfolder][$tview] = array();
    }
    if (isset($_REQUEST["subitem"])) {
        $_SESSION["_" . $tfolder][$tview]["subitem"] = $_REQUEST["subitem"];
    }
    if (isset($_SESSION["_" . $tfolder][$tview]["subitem"])) {
        $t["subitem"] = $_SESSION["_" . $tfolder][$tview]["subitem"];
    }
    if (isset($_REQUEST["filters"])) {
        $_SESSION["_" . $tfolder][$tview]["filters"] = $_REQUEST["filters"];
    }
    if (isset($_SESSION["_" . $tfolder][$tview]["filters"])) {
        $t["filter"] = $_SESSION["_" . $tfolder][$tview]["filters"];
    }
    if ($t["filter"] != "") {
        $t["sqlfilters"] = array();
        $ops = array("eq", "neq", "lt", "gt", "like", "nlike", "starts", "oneof");
        foreach (explode("||", $t["filter"]) as $key => $filter) {
            $filter = explode("|", $filter);
            if (count($filter) == 3 and isset($t["fields"][$filter[0]]) and in_array($filter[1], $ops)) {
                if (trim($filter[2]) == "") {
                    continue;
                }
                if (!empty($t["search"])) {
                    if ($filter[0] == "lastmodifiedby") {
                        $t["search"]["user"] = $filter[2];
                    }
                    if ($filter[0] == "lastmodified") {
                        $t["search"]["modified"] = array("type" => $filter[1], "value" => $filter[2]);
                    }
                }
                $t["sqlfilters"][] = array("field" => $filter[0], "type" => $filter[1], "value" => $filter[2]);
                if ($t["fields"][$filter[0]]["SIMPLE_TYPE"] == "checkbox") {
                    $filter[2] = $filter[2] == "{t}yes{/t}" ? "1" : "0";
                }
                if ($filter[1] == "oneof") {
                    $filter[2] = explode(",", $filter[2]);
                }
                if (in_array($t["fields"][$filter[0]]["SIMPLE_TYPE"], array("date", "dateselect", "time", "datetime"))) {
                    $filter[2] = modify::datetime_to_int($filter[2]);
                }
                $op = "=";
                $key_var = "@filter_value_" . $key . "@";
                switch ($filter[1]) {
                    case "neq":
                        $op = "!=";
                        break;
                    case "oneof":
                        $op = "in";
                        $key_var = "(" . $key_var . ")";
                        break;
                    case "lt":
                        $op = "<";
                        break;
                    case "gt":
                        $op = ">";
                        break;
                    case "like":
                        $op = "like";
                        $filter[2] = "%" . $filter[2] . "%";
                        break;
                    case "nlike":
                        $op = "not like";
                        $filter[2] = "%" . $filter[2] . "%";
                        break;
                    case "starts":
                        $op = "like";
                        $filter[2] = $filter[2] . "%";
                        break;
                }
                $t["sqlvars"]["filter_value_" . $key] = $filter[2];
                $t["sqlwhere"][] = $filter[0] . " " . $op . " " . $key_var;
                $t["sqlwhere_default"][] = $filter[0] . " " . $op . " " . $key_var;
            }
        }
    }
    if (!empty($t["att"]["ENABLE_ASSET_RIGHTS"])) {
        $t["sqlvarsnoquote"]["permission_sql_read_nq"] = $_SESSION["permission_sql_read"];
        $t["sqlvarsnoquote"]["permission_sql_write_nq"] = $_SESSION["permission_sql_write"];
    }
    foreach ($t["sqlwhere_default"] as $key => $value) {
        if (!preg_match_all("|@(.*?)@|i", $value, $matches, PREG_SET_ORDER)) {
            continue;
        }
        foreach ($matches as $match) {
            if (count($match) != 2) {
                continue;
            }
            $req_key = $match[1];
            if (isset($_SESSION[$req_key])) {
                $t["sqlvars"][$req_key] = $_SESSION[$req_key];
            }
        }
    }
    if ($t["default_sql"] == "" or $t["default_sql"] != "no_select") {
        $vars = array("handler" => $t["handler"], "sqlvarsnoquote" => $t["sqlvarsnoquote"], "default_sql" => $t["default_sql"], "custom_name" => $t["custom_name"]);
        $fcount = $GLOBALS["sel_folder"]["fcount"];
        if (!empty($vars["handler"]) or !empty($vars["default_sql"]) or count($t["sqlvars"]["folders"]) > 1 or !empty($t["custom_name"])) {
            $t["maxdatasets"] = db_count($tname, $t["sqlwhere_default"], $t["sqlvars"], $vars);
        } else {
            if (!sys_strbegins($tname, "simple_sys_") and ($t["sqlwhere_default"] == array("folder in (@folders@)") or $fcount == 0)) {
                $t["maxdatasets"] = $fcount;
            } else {
                if ($fcount > 100) {
                    $cid = "dbcount_" . sha1(serialize(array($t["sqlvars"], $t["sqlwhere_default"], $fcount)));
                    $t["maxdatasets"] = sys_cache_get($cid);
                    if ($t["maxdatasets"] === false) {
                        $t["maxdatasets"] = db_count($tname, $t["sqlwhere_default"], $t["sqlvars"], $vars);
                        sys_cache_set($cid, $t["maxdatasets"], OUTPUT_CACHE);
                    }
                } else {
                    $t["maxdatasets"] = db_count($tname, $t["sqlwhere_default"], $t["sqlvars"], $vars);
                }
            }
        }
    } else {
        $t["maxdatasets"] = 0;
    }
    if (isset($_REQUEST["orderby"]) and isset($_REQUEST["order"])) {
        $_SESSION[$tname][$tview]["orderby"] = $_REQUEST["orderby"];
        $_SESSION[$tname][$tview]["order"] = $_REQUEST["order"];
    }
    $t["hidden_fields"] = array();
    if (isset($_REQUEST["hide_fields"])) {
        $_SESSION[$tname][$tview]["hidden"] = explode(",", $_REQUEST["hide_fields"]);
    }
    if (!empty($_SESSION[$tname][$tview]["hidden"]) and empty($t["views"][$tview]["SCHEMA_MODE"])) {
        $t["hidden_fields"] = $_SESSION[$tname][$tview]["hidden"];
        foreach ($t["hidden_fields"] as $field) {
            unset($t["fields"][$field]);
        }
    }
    if (isset($_SESSION[$tname][$tview]["order"]) and isset($_SESSION[$tname][$tview]["orderby"]) and in_array($_SESSION[$tname][$tview]["order"], array("asc", "desc")) and isset($t["fields"][$_SESSION[$tname][$tview]["orderby"]])) {
        $t["orderby"] = $_SESSION[$tname][$tview]["orderby"];
        $t["order"] = $_SESSION[$tname][$tview]["order"];
    }
    foreach ($t["sqlwhere"] as $key => $value) {
        if (!preg_match_all("|@(.*?)@|i", $value, $matches, PREG_SET_ORDER)) {
            continue;
        }
        foreach ($matches as $match) {
            if (count($match) != 2) {
                continue;
            }
            $req_key = $match[1];
            if ($req_key == "folders") {
                continue;
            }
            if ($req_key == "item") {
                $skey = $tview;
            } else {
                $skey = "request";
            }
            if (isset($_SESSION["_" . $tfolder][$skey][$req_key])) {
                $t["sqlvars"][$req_key] = $_SESSION["_" . $tfolder][$skey][$req_key];
            }
            if (isset($_SESSION[$req_key])) {
                $t["sqlvars"][$req_key] = $_SESSION[$req_key];
            }
            if (isset($_REQUEST[$req_key])) {
                $t["sqlvars"][$req_key] = $_REQUEST[$req_key];
                $_SESSION["_" . $tfolder][$skey][$req_key] = $_REQUEST[$req_key];
            }
            if (!isset($t["sqlvars"][$req_key]) and empty($t["sqlvarsnoquote"][$req_key])) {
                $t["sqlwhere"][$key] = "1=1";
            }
        }
    }
    if (!isset($_SESSION[$tname][$tview]["group"])) {
        $_SESSION[$tname][$tview]["group"] = $t["group"];
    }
    if (!isset($_SESSION[$tname][$tview]["groupby"])) {
        $_SESSION[$tname][$tview]["groupby"] = $t["groupby"];
    }
    if (isset($_REQUEST["group"])) {
        if ($_SESSION[$tname][$tview]["groupby"] == "") {
            $_SESSION[$tname][$tview]["groupby"] = $t["orderby"];
            $_SESSION[$tname][$tview]["group"] = $t["order"];
        } else {
            $_SESSION[$tname][$tview]["groupby"] = "";
            $_SESSION[$tname][$tview]["group"] = "";
        }
    }
    if (!isset($_REQUEST["plain"])) {
        $t["groupby"] = $_SESSION[$tname][$tview]["groupby"];
        $t["group"] = $_SESSION[$tname][$tview]["group"];
    } else {
        $t["groupby"] = "";
        $t["group"] = "";
    }
    if ($t["groupby"] != "" and isset($t["fields"][$t["groupby"]])) {
        $field = $t["fields"][$t["groupby"]];
        $field["WIDTH"] = 0;
        unset($t["fields"][$t["groupby"]]);
        unset($t["fields_query"][$t["groupby"]]);
        $t["fields"] = array_merge(array($t["groupby"] => $field), $t["fields"]);
        $t["fields_query"] = array_unique(array_merge(array($t["groupby"]), $t["fields_query"]));
    }
    _asset_process_pages($t["maxdatasets"]);
    if (!isset($t["views"][$tview]["NOSQLORDER"])) {
        $t["sqlorder"] = ($t["groupby"] != "" ? $t["groupby"] . " " . $t["group"] . "," : "") . $t["orderby"] . " " . $t["order"];
    }
    if (!isset($t["views"][$tview]["NOSQLLIMIT"]) and empty($_REQUEST["print_all"]) and (empty($_REQUEST["export"]) or !empty($_REQUEST["limit"]))) {
        $t["sqllimit"] = array(($t["page"] - 1) * $t["limit"], $t["limit"]);
    }
    if (!empty($_REQUEST["iframe"]) and isset($_REQUEST["session_remove_request"])) {
        $t["sqllimit"] = array();
    }
    // offline reading
    if (!empty($t["sqlvars"]["item"]) and $t["views"][$tview]["SCHEMA_MODE"] == "edit" and $t["maxdatasets"] == 0) {
        sys_warning("{t}Item(s) not found or access denied.{/t}");
    }
    if ($t["maxdatasets"] != 0) {
        _asset_get_rows();
    }
    if ((!empty($_REQUEST["form_submit_create"]) or !empty($_REQUEST["form_submit_edit"])) and $t["rights"]["write"] and $t["schema_mode"] != "") {
        $mode = $t["schema_mode"] == "edit" ? "edit" : "create";
        list($t["errors"], $defaults, $form_ids, $saved_ids) = asset::create_edit($tfolder, $tview, $mode);
        foreach ($t["limits"] as $key => $val) {
            if (in_array($val, $form_ids)) {
                unset($t["limits"][$key]);
            }
        }
        if (count($t["errors"]) == 0) {
            $t["limits"] = range(0, $t["limit"] - 1);
            if (!empty($_REQUEST["form_submit_return"])) {
                $arr = array_pop(array_slice($_SESSION["history"], -2, 1));
                if (empty($arr[2])) {
                    $arr[2] = "default";
                }
                sys_redirect("index.php?view=" . $arr[2] . "&" . sys::$urladdon);
            }
            if (!empty($_REQUEST["form_submit_go_edit"])) {
                $items = "";
                foreach ($saved_ids as $id) {
                    $items .= "&item[]=" . rawurlencode($id);
                }
                sys_redirect("index.php?view=edit" . $items . "&" . sys::$urladdon);
            }
        }
        foreach ($defaults as $id => $field) {
            foreach ($field as $field_name => $value) {
                $t["fields"][$field_name]["SIMPLE_DEFAULTS"][$id] = $value;
                $t["fields"][$field_name]["SIMPLE_DEFAULT"] = "";
            }
        }
    }
    sys::$smarty->assign_by_ref("t", $t);
}