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)); }
private static function _move($ccp, $folder, $sgsml) { if (empty($sgsml->att["DISABLE_TRIGGER_CCP"]) and (!empty($sgsml->att["TRIGGER_NEW"]) or !empty($sgsml->att["TRIGGER_DELETE"]))) { $ccp["data_fields"] = array("*"); } else { if (in_array("notification", $ccp["data_fields"])) { $ccp["data_fields"] = array("id", "folder", "notification"); foreach ($sgsml->fields as $key => $field) { if (isset($field["REQUIRED"]) and !in_array($key, $ccp["data_fields"])) { $ccp["data_fields"][] = $key; } } } else { $ccp["data_fields"] = array("id", "folder"); } } $vars = array("handler" => $ccp["handler"], "sqlvarsnoquote" => $ccp["vars_noquote"], "custom_name" => $ccp["custom_name"], "default_sql" => $ccp["default_sql"]); $rows = db_select($ccp["tname"], $ccp["data_fields"], $ccp["where"], "", "", array("item" => $ccp["items"], "folder" => $ccp["folder"], "folders" => $ccp["folders"]), $vars); if (is_array($rows) and count($rows) < count($ccp["items"])) { exit("{t}Item(s) not found or access denied.{/t}"); } $default_values = folder_get_default_values($folder); $messages = array(); foreach ($rows as $row) { $folder_source = $row["folder"]; if (empty($folder_source) or $folder_source == $folder or !db_get_right($folder_source, "write")) { continue; } $sgsml->vars["id"] = array($row["id"]); $sgsml->vars["folders"] = array($folder_source); if (!empty($sgsml->att["DISABLE_TRIGGER_CCP"])) { unset($sgsml->att["TRIGGER_EDIT"]); } $result = 0; if (!is_numeric($folder)) { $sgsml->vars["id"] = $row["id"]; $sgsml->vars["folder_source"] = $folder_source; $sql_data = array_merge($default_values, array("folder" => $folder)); $sql_data["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}")); $sql_data = $sgsml->build_history($sql_data, $row); $error_sql = db_update($sgsml->tname, $sql_data, array("id=@id@"), $sgsml->vars, array("handler" => $sgsml->handler)); if ($error_sql != "") { $messages[] = "{t}SQL failed.{/t} " . $error_sql; } if (!empty($sgsml->att["TRIGGER_EDIT"]) and empty($sgsml->att["DISABLE_TRIGGER_CCP"])) { $return = asset_process_trigger($sgsml->att["TRIGGER_EDIT"], $row["id"], $row, $sgsml->tname); if ($return != "") { $messages[] = "{t}Trigger failed{/t}: " . $return; } } if (!empty($row["notification"])) { $row["folder"] = $folder; $smtp_data = asset::build_notification($sgsml->tname, $sgsml->fields, $row, $sql_data, $row["id"]); $return = asset_process_trigger("sendmail", $row["id"], $smtp_data); if ($return != "") { $messages[] = "{t}Trigger failed{/t}: " . $return; } } } else { $sql_data = array_merge($default_values, array("folder" => $folder)); $result = $sgsml->update($sql_data, $row["id"]); } if (!is_numeric($result)) { if (is_array($result) and count($result) > 0) { $message = "{t}Error pasting asset{/t}:"; foreach ($result as $field => $errors) { foreach ($errors as $error) { $message .= "\n" . $error[0] . ": " . $error[1]; } } } else { $message = $result; } $messages[] = $message; } else { sys_log_stat("moved_records", 1); } } return $messages; }
if (empty($cron_conf["smtp"])) { $message = t("{t}Mail identities{/t}") . ": " . t("{t}SMTP not configured for %s{/t}", "cron"); sys_log_message_log("php-fail", $message); exit($message); } if (!DEBUG and file_exists($lock_file) and filemtime($lock_file) + 150 > time() and !isset($_REQUEST["debug"])) { exit("already running."); } touch($lock_file); $notifications = db_select("simple_sys_notifications", "*", array("sent=0", "delivery <= @now@", "category='email'"), "created desc", "100", array("now" => NOW)); if (!empty($notifications)) { $log = ""; out("sending mails:\n"); foreach ($notifications as $notification) { $smtp_data = array("efrom" => "", "eto" => $notification["eto"], "subject" => $notification["subject"], "message" => trim($notification["message"]), "attachment" => $notification["attachment"], "folder" => "cron"); $result = asset_process_trigger("sendmail", "", $smtp_data); if ($result != "") { echo "ERROR " . $notification["eto"] . ": " . $result . "\n"; sys_log_message_log("php-fail", $result, var_export($smtp_data, true)); db_update("simple_sys_notifications", array("error" => $result), array("id=@id@"), array("id" => $notification["id"])); } else { $data = array("error" => ""); if (!empty($notification["recurrence"])) { if ($notification["recurrence"][0] == "|") { $match = array(); preg_match("/([^\\|]+)\\|([^&]+)/", $notification["reference"], $match); if (!empty($match) and count($match) == 3) { $row = db_select_first($match[1], "*", "id=@id@", "", array("id" => $match[2])); if (!empty($row)) { trigger::notify($match[2], $row, array(), $match[1]); }
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; }