private static function _import_delete($folder, $id, $tname, $module) { $where = array("folder=@folder@"); if ($id[0] == "_") { $where[] = "id=@id@"; } else { $where[] = "syncid=@id@"; } $row_id = db_select_value($tname, "id", $where, array("id" => trim($id, "_"), "folder" => $folder)); if (!empty($row_id)) { $trash = db_select_value("simple_sys_tree", "id", "anchor=@anchor@", array("anchor" => "trash")); if (empty($trash)) { sys_warning("{t}Error{/t}: {t}Trash folder not found.{/t}"); return; } $id = folders::create(sys_date("{t}m/d/Y{/t}"), "blank", "", $trash, true); $id2 = folders::create($module, str_replace("simple_", "", $tname), "", $id, true); $data = array("folder" => $id2, "history" => sprintf("{t}Item deleted by %s at %s{/t}\n", $_SESSION["username"], sys_date("{t}m/d/y g:i:s a{/t}"))); db_update($tname, $data, array("id=@id@"), array("id" => $row_id)); db_update_treesize($tname, $folder); db_search_delete($tname, $row_id, $folder); sys_log_stat("deleted_records", 1); } }
private static function _restore($filename) { ob_end_flush(); $filename = str_replace(" ", "+", $filename); $filename = SIMPLE_STORE . "/backup/" . basename($filename); if (!file_exists($filename)) { return "{t}file not found.{/t} (" . $filename . ")"; } if (filesize($filename) == 0) { return "{t}No entries found.{/t} (" . $filename . ")"; } self::_out("{t}Extracting files{/t}: " . $filename); self::_out(""); if (self::$_restore_here) { $ftitle = str_replace(array("__", "---", "--"), array("/", " ", " "), substr(modify::basename($filename), 0, -4)); $ftitle = substr($ftitle, strrpos($ftitle, "/") + 1); $id = folders::create(substr($ftitle, 0, 40), "blank", "", $_SESSION["folder"], false); self::$_restore_folder = $id; self::_out("{t}Insert{/t}: simple_sys_tree: " . $ftitle . " [" . $id . "]"); } $result = sys_exec(sys_find_bin("tar") . " -tf " . modify::realfilename($filename)); $file_list = explode("\n", $result); if (count($file_list) == 0) { return ""; } $base_dir = SIMPLE_STORE . "/restore_" . NOW . "/"; sys_mkdir($base_dir); $cmd = "cd " . modify::realfilename($base_dir) . " && " . sys_find_bin("tar") . " -xf " . modify::realfilename($filename); if (DEBUG) { self::_out("TAR: " . $cmd . "\n\n"); } echo sys_exec($cmd); $update_ids = array(); $update_folders = array(); $restore_maps = array(); $xml_file = array_shift($file_list); self::_out("{t}Parsing{/t}: " . $xml_file); $xml = simplexml_load_file($base_dir . $xml_file); foreach ($xml->table as $data) { $data = get_object_vars($data->assetfolder); unset($data["@attributes"]); $id = $data["id"]; if (!empty($data["anchor"])) { $existing = db_select_first("simple_sys_tree", array("id", "'' as lastmodified"), "anchor=@anchor@", "", array("anchor" => $data["anchor"])); if (!empty($existing["id"])) { unset($data["anchor"]); } } else { $existing = db_select_first("simple_sys_tree", array("id", "lastmodified"), "id=@id@", "", array("id" => $id)); } if (!isset($data["fdescription"])) { $data["fdescription"] = ""; } $ftype = $data["ftype"]; $keys = array("fsizecount", "fchsizecount", "fcount", "fchcount", "ffcount", "lft", "rgt", "flevel", "folder", "id", "ftype"); foreach ($keys as $key) { unset($data[$key]); } if (isset($restore_maps[$data["parent"]])) { $data["parent"] = $restore_maps[$data["parent"]]; } if (empty($existing["id"]) or self::$_restore_here) { $parent = db_select_value("simple_sys_tree", "id", "id=@id@", array("id" => $data["parent"])); if (empty($parent) or count($restore_maps) == 0 and self::$_restore_here) { $data["parent"] = self::$_restore_folder; } $id2 = folders::create($data["ftitle"], $ftype, $data["fdescription"], $data["parent"], false); self::_out("{t}Insert{/t}: simple_sys_tree: " . $data["ftitle"] . " [ID " . $id . " -> parent/id: " . $data["parent"] . "/" . $id2 . "]"); $restore_maps[$id] = $id2; $id = $id2; } else { $restore_maps[$id] = $existing["id"]; } if (!self::$_restore_missing and (!self::$_restore_onlynewer or $data["lastmodified"] > $existing["lastmodified"])) { self::_out("{t}Update{/t}: simple_sys_tree " . $id); $error = db_update("simple_sys_tree", $data, array("id=@id@"), array("id" => $id)); if ($error) { self::_out($error); } } } foreach ($xml->table as $table_item) { if (!isset($table_item->asset) or count($table_item->asset) == 0) { continue; } foreach ($table_item->asset as $asset) { $table = $table_item["name"]; if ($table == "simple_sys_tree") { continue; } $data = get_object_vars($asset); unset($data["@attributes"]); foreach ($data as $dkey => $val) { $obj = $asset->{$dkey}; if (!isset($obj["is_file"]) or $val == "") { continue; } $file_arr[$key] = ""; $file_arr = explode("|", trim($val, "|")); foreach ($file_arr as $key => $value) { foreach ($file_list as $file) { if (basename($file) != basename($value)) { continue; } $value = $base_dir . $file; break; } $file_arr[$key] = $value; } $data[$dkey] = "|" . implode("|", $file_arr) . "|"; } $id = $data["id"]; $existing = db_select_first($table, array("id", "lastmodified"), "id=@id@", "", array("id" => $id)); $folder = $data["folder"]; if (isset($restore_maps[$folder])) { $data["folder"] = $restore_maps[$folder]; } if (empty($existing["id"]) or self::$_restore_here) { if (self::$_restore_missing) { $data["id"] = $id; } else { $data["id"] = sql_genID($table) * 100; } self::_out("{t}Insert{/t}: " . $table . ": " . $data["id"]); $error = db_insert($table, $data); if ($error) { self::_out($error); } $update_folders[$data["folder"]] = $table; $update_ids[$data["folder"]][] = $data["id"]; } else { if (!self::$_restore_missing) { if (!self::$_restore_onlynewer or $data["lastmodified"] > $existing["lastmodified"]) { self::_out("{t}Update{/t}: " . $table . " " . $id); $error = db_update($table, $data, array("id=@id@"), array("id" => $id)); if ($error) { self::_out($error); } $update_folders[$data["folder"]] = $table; $update_ids[$data["folder"]][] = $id; } } } } } if (count($update_folders) > 0) { foreach ($update_folders as $folder => $table) { if (strpos($table, "nodb_")) { continue; } db_update_treesize($table, $folder); $ftype = str_replace("simple_", "", $table); $schema = db_get_schema(sys_find_module($ftype)); if (empty($schema["views"]["display"])) { continue; } if (!empty($schema["att"]["SQL_HANDLER"]) or !empty($schema["att"]["NO_SEARCH_INDEX"])) { continue; } self::_out("... "); $fields = $schema["fields"]; if (folder_in_trash($folder)) { continue; } foreach ($update_ids[$folder] as $id) { self::_out("{t}Rebuild search index{/t}: " . $table . " [" . $id . "]"); db_search_update($table, $id, $fields); } } } self::_out(""); $message = "{t}Restore complete{/t}: " . str_replace(array("__", "---", "--"), array("/", "] [", " ["), substr(modify::basename($filename), 0, -4)) . "]"; sys_log_message_log("info", $message); self::_out($message); return ""; }
static function delete_items($folder, $view, $items, $mode = "delete") { if (empty($folder) or empty($view) or !is_array($items) or $mode == "") { return; } $sgsml = new sgsml($folder, $view, $items); $tname = $sgsml->tname; $handler = $sgsml->handler; if (!isset($sgsml->buttons[$mode]) or $mode == "delete" and count($items) == 0) { return; } if ($mode == "empty") { $sgsml->where = array("folder in (@folders@)"); } if ($mode == "purgeall") { $sgsml->where = array(); } if (in_array($mode, array("purge", "purgeall"))) { $delete = true; } else { $delete = false; } if (folder_in_trash($folder)) { $delete = true; } if ($handler == "") { $file_fields = $sgsml->get_fields_by_type("files"); } else { $file_fields = array(); } if (!empty($sgsml->att["TRIGGER_DELETE"])) { $fields = array("*"); } else { if (isset($sgsml->fields["notification"])) { $fields = array("id", "folder", "notification"); foreach ($sgsml->fields as $key => $field) { if (isset($field["REQUIRED"]) and $field["SIMPLE_TYPE"] != "files" and !in_array($key, $fields)) { $fields[] = $key; } } } else { $fields = array("id"); } $fields = array_unique(array_merge($fields, $file_fields)); } $rows = $sgsml->get_rows($fields); if (!is_array($rows) or count($rows) == 0 or count($rows) < count($items)) { exit("{t}Item(s) not found or access denied.{/t}"); } if ($delete) { foreach ($rows as $row) { foreach ($file_fields as $field) { $files = explode("|", $row[$field]); sys_unlink($files); } $data = array("id" => $row["id"], "folder" => $folder); db_delete($tname, array("id=@id@"), $data, array("handler" => $handler)); } } else { $trash = db_select_value("simple_sys_tree", "id", "anchor=@anchor@", array("anchor" => "trash")); if (empty($trash)) { exit("{t}Error{/t}: {t}Trash folder not found.{/t}"); } foreach ($rows as $row) { $id = folders::create(sys_date("{t}m/d/Y{/t}"), "blank", "", $trash, true); $tid = folders::create($sgsml->att["MODULENAME"], str_replace("simple_", "", $tname), "", $id, true); $data = array("folder" => $tid, "history" => sprintf("{t}Item deleted by %s at %s{/t}\n", $_SESSION["username"], sys_date("{t}m/d/y g:i:s a{/t}"))); db_update($tname, $data, array("id=@id@"), array("id" => $row["id"]), array("handler" => $handler)); db_update_treesize($tname, $tid); if (!isset($row["notification"])) { $row["notification"] = ""; } $tree_notification = db_select_value("simple_sys_tree", "notification", "id=@id@", array("id" => $folder)); if ($tree_notification) { $row["notification"] .= "," . $tree_notification; } if (!empty($row["notification"])) { $smtp_data = self::build_notification($tname, $sgsml->fields, $row, $data, $id); asset_process_trigger("sendmail", $row["id"], $smtp_data); } if (!empty($sgsml->att["TRIGGER_DELETE"])) { asset_process_trigger($sgsml->att["TRIGGER_DELETE"], $row["id"], $row, $tname); } db_update("simple_sys_tree", array("history" => "[" . $row["id"] . "/details] " . $data["history"]), array("id=@id@"), array("id" => $folder)); db_search_delete($tname, $row["id"], $folder); db_notification_delete($tname, $row["id"]); } } db_update_treesize($tname, $folder); sys_log_stat("deleted_records", count($rows)); }
static function folder_add_offline($folder, $view, $folder_name) { $offline_folder = db_select_value("simple_sys_tree", "id", "anchor=@anchor@", array("anchor" => "offline_" . $_SESSION["username"])); if (empty($offline_folder)) { exit(sprintf("{t}Item not found.{/t} (%s)", "{t}Offline folders{/t}")); } // TODO2 parameters for calendar, only future events ? $url = "index.php?folder=" . rawurlencode($folder) . "&view=" . $view . "&iframe=1&markdate=all&session_remove_request"; $duplicate = db_select_value("simple_offline", "id", array("folder=@folder@", "url=@url@"), array("url" => $url, "folder" => $offline_folder)); if (!empty($duplicate)) { return; } $id = sql_genID("simple_offline") * 100; $data = array("id" => $id, "url" => $url, "folder" => $offline_folder, "bookmarkname" => $folder_name); $error_sql = db_insert("simple_offline", $data); if ($error_sql == "") { db_update_treesize("simple_offline", $offline_folder); db_search_update("simple_offline", $id, array(), array("url" => "text", "bookmarkname" => "text")); sys_log_stat("new_records", 1); } }
private static function _createlocation($name) { $row_id = db_select_value("simple_locations", "id", "locationname=@name@", array("name" => $name)); $folder = folder_from_path("^locations"); if (empty($row_id) and !empty($folder)) { $id = sql_genID("simple_locations") * 100; $data = array("id" => $id, "locationname" => $name, "folder" => $folder); $error_sql = db_insert("simple_locations", $data); if ($error_sql == "") { db_update_treesize("simple_locations", $folder); db_search_update("simple_locations", $id, array(), array("locationname" => "text")); sys_log_stat("new_records", 1); } else { return $error_sql; } } return ""; }
private function _save(array &$data, $id = -1) { $insert = ($id > 0 or !is_numeric($id)) ? false : true; if (count($data) == 0) { return array(); } if (!empty($this->att["DEFAULT_SQL"]) and $this->att["DEFAULT_SQL"] == "no_select") { return self::_error("{t}Module{/t}", "{t}Access denied.{/t}"); } if (!empty($data["folder"])) { // check permissions if (!db_get_right($data["folder"], "write", $this->view)) { return self::_error("{t}Folder{/t}", "{t}Access denied.{/t}", "folder"); } $this->folder = $data["folder"]; } else { $data["folder"] = $this->folder; } // fill data array list($rdata, $data_row, $error) = $this->_complete_data($data, $id); if ($error) { return $error; } // validate if ($result = $this->_validate($rdata, $id)) { return $result; } if ($insert) { $id = sql_genID($this->tname) * 100; $sql_data = array("id" => $id, "dsize" => 0, "history" => sprintf("{t}Item created by %s at %s{/t}\n", $_SESSION["username"], sys_date("{t}m/d/y g:i:s a{/t}"))); } else { $sql_data = array("dsize" => 0, "history" => sprintf("{t}Item edited (%s) by %s at %s{/t}\n", "@fields@", $_SESSION["username"], sys_date("{t}m/d/y g:i:s a{/t}"))); } // count sizes, move files to store, delete old files foreach ($this->current_fields as $field_name => $field) { if ($field["SIMPLE_TYPE"] == "id") { continue; } if ($field["SIMPLE_TYPE"] == "files" and !empty($rdata[$field_name])) { foreach ($rdata[$field_name] as $val) { if (file_exists($val)) { $sql_data["dsize"] += filesize($val); } } // TODO 2 store handler? if (!empty($data_row[$field_name])) { $data_old = explode("|", trim($data_row[$field_name], "|")); foreach ($data_old as $filekey => $file) { if (in_array($file, $rdata[$field_name])) { continue; } if (ARCHIVE_DELETED_FILES and file_exists($file)) { $i = 1; $m = ""; $trash_name = SIMPLE_STORE . "/trash/" . $this->folder . "_" . $id . "_"; $trash_file = modify::basename($file); while (file_exists($trash_name . $m . $trash_file)) { $m = $i++ . "_"; } rename($file, $trash_name . $m . $trash_file); touch($trash_name . $m . $trash_file); } else { @unlink($file); } } } foreach ($rdata[$field_name] as $filekey => $file) { if ($file == "") { unset($rdata[$field_name][$filekey]); $data[$field_name] = implode("|", $rdata[$field_name]); continue; } if (file_exists(SIMPLE_CACHE . "/upload/" . basename($file))) { $filebase = modify::basename(basename($file)); list($target, $filename) = sys_build_filename($filebase, $this->tname); dirs_checkdir($target); $target .= sys_get_pathnum($id) . "/"; dirs_checkdir($target); $target .= md5($id) . $filename; rename(SIMPLE_CACHE . "/upload/" . basename($file), $target); $rdata[$field_name][$filekey] = $target; $data[$field_name] = implode("|", $rdata[$field_name]); } } $basenames = array(); foreach (array_reverse($rdata[$field_name]) as $filekey => $file) { $basename = modify::basename($file); if (isset($basenames[$basename])) { $old_filekey = $basenames[$basename]; $basename = preg_replace("|_rev\\d+|", "", $basename); $base = $basename; $i = 1; while (isset($basenames[$basename])) { if ($pos = strrpos($base, ".")) { $basename = substr($base, 0, $pos) . "_rev" . $i++ . substr($base, $pos); } else { $basename = $base . "_rev" . $i++; } } $target = str_replace(modify::basename($file), $basename, $file); if (rename($file, $target)) { // swap $rdata[$field_name][$filekey] = $rdata[$field_name][$old_filekey]; $rdata[$field_name][$old_filekey] = $target; $data[$field_name] = implode("|", $rdata[$field_name]); } } $basenames[$basename] = $filekey; } } if (!empty($field["STORE"]) and is_array($field["STORE"])) { foreach ($field["STORE"] as $store) { list($class, $function, $params) = sys_find_callback("modify", $store["FUNCTION"]); $rdata[$field_name] = call_user_func(array($class, $function), $rdata[$field_name], $rdata, $params); } } if (!isset($sql_data[$field_name]) and !is_null($rdata[$field_name])) { $sql_data[$field_name] = $rdata[$field_name]; } } // transform foreach ($sql_data as $key => $value) { $sql_data[$key] = self::scalarize($value, $this->fields[$key]); } // reduce to new values $sys_fields = array("history" => "", "dsize" => "", "seen" => ""); foreach ($sql_data as $data_key => $data_value) { if (isset($sys_fields[$data_key])) { continue; } $addfield = true; $field = $this->fields[$data_key]; if (!isset($this->current_fields[$data_key])) { $addfield = false; } if (isset($field["NOTINALL"])) { $addfield = false; } if (isset($field["NOTIN"]) and in_array($this->view, $field["NOTIN"])) { $addfield = false; } if (isset($field["READONLYIN"]) and (in_array($this->view, $field["READONLYIN"]) or in_array("all", $field["READONLYIN"]))) { $addfield = false; } if (isset($field["ONLYIN"])) { if (in_array($this->view, $field["ONLYIN"])) { $addfield = true; } else { $addfield = false; } } if (!$addfield) { unset($sql_data[$data_key]); } } // build history $sql_data = $this->build_history($sql_data, $data_row); if (!array_diff(array_keys($sql_data), array("history", "seen"))) { $sql_data = array(); } // save in db if ($insert) { $error_sql = db_insert($this->tname, $sql_data, array("handler" => $this->handler)); if ($error_sql != "") { return self::_error("{t}SQL failed.{/t}", $error_sql); } if ($this->notification) { sys_notification("{t}Item successfully created.{/t} (" . $id . ")"); } } else { if (count($sql_data) == 0) { return $id; } $error_sql = db_update($this->tname, $sql_data, array("id=@id@"), array("id" => $id, "folder" => $this->folder), array("handler" => $this->handler)); if ($error_sql != "") { return self::_error("{t}SQL failed.{/t}", $error_sql); } if ($this->notification) { sys_notification("{t}Item successfully updated.{/t} (" . (is_numeric($id) ? $id : 1) . ")"); } } if (empty($this->handler)) { db_update("simple_sys_tree", array("history" => "[" . $id . "/details] " . $sql_data["history"]), array("id=@id@"), array("id" => $this->folder)); db_update_treesize($this->tname, $this->folder); if (!$insert and $this->folder != $data_row["folder"]) { db_update("simple_sys_tree", array("history" => "[" . $id . "/details] " . $sql_data["history"]), array("id=@id@"), array("id" => $data_row["folder"])); db_update_treesize($this->tname, $data_row["folder"]); db_search_delete($this->tname, $id, $data_row["folder"]); } if (empty($this->att["NO_SEARCH_INDEX"])) { db_search_update($this->tname, $id, $this->fields); } sys_log_stat($insert ? "new_records" : "changed_records", 1); } // call triggers $trigger = ""; if ($insert and !empty($this->att["TRIGGER_NEW"])) { $trigger = $this->att["TRIGGER_NEW"]; } if (!$insert and !empty($this->att["TRIGGER_EDIT"])) { $trigger = $this->att["TRIGGER_EDIT"]; } if ($trigger and $result = asset_process_trigger($trigger, $id, $rdata, $this->tname)) { return self::_error("{t}Trigger failed{/t}", $result); } // send notification $tree_notification = db_select_value("simple_sys_tree", "notification", "id=@id@", array("id" => $this->folder)); if ($tree_notification != "") { $rdata["notification"] .= "," . $tree_notification; } if (!$insert and $this->folder != $data_row["folder"]) { $tree_notification = db_select_value("simple_sys_tree", "notification", "id=@id@", array("id" => $data_row["folder"])); if ($tree_notification != "") { $rdata["notification"] .= "," . $tree_notification; } } if (!empty($rdata["notification"])) { $rdata["notification"] = trim($rdata["notification"], ","); $smtp_data = asset::build_notification($this->att["NAME"], $this->current_fields, $rdata, $sql_data, $id, $data_row); if ($result = asset_process_trigger("sendmail", $id, $smtp_data)) { return self::_error("{t}Trigger failed{/t}", $result); } } // update stats if (!empty($this->handler)) { foreach ($sql_data as $data_key => $data_value) { $field = $this->fields[$data_key]; if ($field["SIMPLE_TYPE"] != "files") { continue; } foreach (explode("|", $data_value) as $file) { if (sys_strbegins($file, SIMPLE_CACHE . "/upload/")) { @unlink($file); } } } } return $id; }
function _upload_create_file($db_path, $target_lnk, $path, $filename) { list($id, $left, $unused) = _upload_process_folder_string($db_path . "/"); if ($left != 0 or $id == 0) { sys_error("path not found", "409 Conflict"); } $ftype = db_select_value("simple_sys_tree", "ftype", "id=@id@", array("id" => $id)); if (db_get_right($id, "write") and !empty($ftype) and $ftype == "files") { list($target, $a_filename) = sys_build_filename($filename, "simple_files"); dirs_checkdir($target); $target .= sys_get_pathnum($id) . "/"; dirs_checkdir($target); $target .= md5($id) . $a_filename; if ($fp = fopen("php://input", "r") and $ft = fopen($target, "wb")) { while (!feof($fp)) { fwrite($ft, fread($fp, 8192)); } fclose($fp); fclose($ft); $a_id = sql_genID("simple_files") * 100; $data = array("id" => $a_id, "folder" => $id, "dsize" => filesize($target), "filedata" => "|" . $target . "|", "filename" => $filename, "rread_users" => "|anonymous|", "rwrite_users" => "|anonymous|", "history" => t("{t}Item created by %s at %s{/t}", $_SESSION["username"], sys_date(t("{t}m/d/y g:i:s a{/t}"))) . "\n"); $error_sql = db_insert("simple_files", $data); if ($error_sql == "") { db_update_treesize("simple_files", $id); $fields = array("filename" => "text", "filedata" => "files", "folder" => "id", "id" => "id"); db_search_update("simple_files", $a_id, array(), $fields); sys_log_stat("new_records", 1); file_put_contents($target_lnk, $path . "/" . $a_id . "_0__" . $filename . "\n" . $target, LOCK_EX); _upload_success(); } } } sys_error("cant write new", "403 Forbidden"); }