Files
selima-perl/lib/php/monica/callform.inc.php
2026-03-10 21:31:43 +08:00

432 lines
12 KiB
PHP

<?php
// File name: callform.inc.php
// Description: PHP subroutine to do API between scripts
// Date: 2002-11-11
// Author: imacat <imacat@pristine.com.tw>
// Copyright: Copyright (C) 2002-2007 Pristine Communications
// Set the include path
if (!defined("INCPATH_SET")) {
require_once dirname(__FILE__) . "/incpath.inc.php";
}
// Referenced subroutines
require_once "monica/addget.inc.php";
require_once "monica/cgiemu.inc.php";
require_once "monica/formfunc.inc.php";
require_once "monica/formreg.inc.php";
require_once "monica/http.inc.php";
require_once "monica/newsn.inc.php";
require_once "monica/requri.inc.php";
require_once "monica/userhome.inc.php";
// Settings
define("FORMID_LEN", 9);
register_form("FORM_THIS", _callform_form_this());
register_form("FORM_USERS", "/admin/users.php");
register_form("FORM_GROUPS", "/admin/groups.php");
register_form("FORM_USERMEM", "/admin/usermem.php");
register_form("FORM_GROUPMEM", "/admin/groupmem.php");
register_form("FORM_USERPREF", "/admin/userpref.php");
register_form("FORM_SCPTPRIV", "/admin/scpptpriv.php");
register_form("FORM_PIC", "/admin/pic.php");
register_form("FORM_PAGES", "/admin/pages.php");
register_form("FORM_NEWS", "/admin/news.php");
register_form("FORM_LINKCAT", "/admin/linkcat.php");
register_form("FORM_LINKS", "/admin/links.php");
if (!defined("USE_SUSPEND_FORM")) {
define("USE_SUSPEND_FORM", true);
}
// call_form: Suspend the current form and call another selection form
function call_form($form, $args, $import_func)
{
// Only work for defined forms
if (is_null(get_registered_form($form))) {
trigger_error("Calling undefined form \"$form\".\n", E_USER_ERROR);
}
// Record the selection import function
if (!is_null($import_func)) {
// Obtain the form
$FORM =& get_or_post();
$FORM["import_func"] = $import_func;
}
// Suspend the current form
$formid = suspend_form();
// Base URL
$url = get_registered_form($form);
// Initialize as an empty array
if (is_null($args)) {
$args = array();
}
// Add the caller
$args[] = "caller=" . urlencode(REQUEST_PATH);
// Add the caller form ID
$args[] = "cformid=" . urlencode($formid);
// Redirect
http_303($url . "?" . implode("&", $args));
// No need to return
exit;
}
// proc_status_redirect: Save the form status, suspend and redirect
// the form
function proc_status_redirect(&$status)
{
// Obtain the form
$FORM =& get_or_post();
// Set the status
if (!is_null($status)) {
$FORM["status"] = $status;
}
// No more form
if ( is_array($status)
&& array_key_exists("isform", $status)
&& !$status["isform"]) {
// Find the referring script
// Back to the page that refer to this form
if (array_key_exists("referer2", $FORM)) {
$url = $FORM["referer2"];
// Back to the user's home on the calling site
} elseif (array_key_exists("hostport", $FORM)) {
$url = $FORM["hostport"] . userhome();
// Default to the current URL
} else {
$url = REQUEST_FILE;
}
// Do not affect the previous form
unset($status["isform"]);
// Do not suspend the form in this case
// Default to this same script
} else {
$url = _callform_form_this();
// Suspend the current form
$formid = suspend_form();
$url = add_get_arg($url, "formid", $formid);
}
if (!is_null($status)) {
$statid = suspend_status($status);
$url = add_get_arg($url, "statid", $statid);
}
// Redirect
if ($_SERVER["REQUEST_METHOD"] == "POST") {
http_303($url);
} else {
http_307($url);
}
// No need to return
exit;
}
// error_redirect: Shortcut to redirect the error form
function error_redirect(&$status)
{
if (!is_null($status)) {
$status["status"] = "error";
}
proc_status_redirect($status);
// No need to return
exit;
}
// success_redirect: Shortcut to redirect the success form
function success_redirect(&$status)
{
if (!is_null($status)) {
$status["status"] = "success";
}
proc_status_redirect($status);
// No need to return
exit;
}
// goto_form: Goto a specific form
function goto_form(&$FORM, $url = null)
{
// Add the caller infomation
if (is_called_form()) {
// Obtain the current form
$CURFORM =& curform();
// Add the caller infomation
$FORM["caller"] = $CURFORM["caller"];
$FORM["cformid"] = $CURFORM["cformid"];
}
// Suspend the supplied form
$formid = suspend_form($FORM);
// Base URL
// Default to this same script
if (is_null($url)) {
$url = REQUEST_FILE;
}
// Add the form ID
$url = add_get_arg($url, "formid", $formid);
// Redirect
if ($_SERVER["REQUEST_METHOD"] == "POST") {
http_303($url);
} else {
http_307($url);
}
// No need to return
exit;
}
// suspend_form: Suspend the current form to $_SESSION
function suspend_form($FORM = null)
{
// Obtain the form
if (is_null($FORM)) {
$FORM =& get_or_post();
}
// Initialize the form deposit
if (!array_key_exists("saveforms", $_SESSION)) {
$_SESSION["saveforms"] = array();
}
// Generate a new random form ID
$formid = new_sn_assoc($_SESSION["saveforms"]);
// Save the current form
$_SESSION["saveforms"][$formid] = $FORM;
// Save the form ID
$_SESSION["saveforms"][$formid]["formid"] = $formid;
$_SESSION["saveforms"][$formid]["ownerscript"] = REQUEST_PATH;
// Return the form ID
return $formid;
}
// retrieve_form: Retrieve a previously suspended form from $_SESSION
// Return empty array instead of empty, to be merged directly
function &retrieve_form($formid = null)
{
// Cache the result
static $cache = array();
// Return the cache
if (is_null($formid)) {
// $cache[0] is the default (previous form)
if (array_key_exists(0, $cache)) {
return $cache[0];
}
} else {
if (array_key_exists($formid, $cache)) {
return $cache[$formid];
}
}
// Session not in use, return null in all cases
if (!isset($_SESSION)) {
if (is_null($formid)) {
$cache[0] = array();
return $cache[0];
} else {
$cache[$formid] = array();
return $cache[$formid];
}
}
// Initialize the form deposit
if (!array_key_exists("saveforms", $_SESSION)) {
$_SESSION["saveforms"] = array();
if (is_null($formid)) {
$cache[0] = array();
return $cache[0];
} else {
$cache[$formid] = array();
return $cache[$formid];
}
}
// Obtain the previous suspended form if form not specified
if (is_null($formid)) {
// Obtain the form
$FORM =& get_or_post();
// Return empty if not set
if (!array_key_exists("formid", $FORM)) {
$cache[0] = array();
return $cache[0];
}
$formid = $FORM["formid"];
// There is no such previously suspended form
if (!array_key_exists($formid, $_SESSION["saveforms"])) {
$cache[$formid] = array();
$cache[0] = array();
return $cache[0];
}
$cache[$formid] = $_SESSION["saveforms"][$formid];
$cache[0] =& $cache[$formid];
} else {
// There is no such previously suspended form
if (!array_key_exists($formid, $_SESSION["saveforms"])) {
$cache[$formid] = array();
return $cache[$formid];
}
$cache[$formid] = $_SESSION["saveforms"][$formid];
}
// Import the selection if needed
if ( array_key_exists("import_func", $cache[$formid])
&& function_exists($cache[$formid]["import_func"])) {
$cache[$formid] = call_user_func($cache[$formid]["import_func"],
$cache[$formid]);
}
// Return the form
return $cache[$formid];
}
// is_first_form: If it is a first form
function is_first_form()
{
// Obtain the form
$FORM =& get_or_post();
// Return empty if not set
return !array_key_exists("formid", $FORM);
}
// is_called_form: If this is a called form
function is_called_form($FORM = null)
{
if (!USE_SUSPEND_FORM) {
return false;
}
// Obtain the current form
if (is_null($FORM)) {
$FORM =& curform();
}
// Full caller infomation provided
return array_key_exists("caller", $FORM)
&& array_key_exists("cformid", $FORM);
}
// args_addcaller: Add the caller infomation to the GET argument list
function args_addcaller(&$args)
{
if (is_called_form()) {
// Obtain the current form
$FORM =& curform();
// Add the caller infomation
$args[] = "caller=" . urlencode($FORM["caller"]);
$args[] = "cformid=" . urlencode($FORM["cformid"]);
}
return;
}
// suspend_status: Suspend the current status to $_SESSION
function suspend_status($status)
{
// Bounce
if (is_null($status)) {
return null;
}
// Initialize the status deposit
if (!array_key_exists("savestats", $_SESSION)) {
$_SESSION["savestats"] = array();
}
// Generate a new random status ID
$id = new_sn_assoc($_SESSION["savestats"]);
// Save the current status
$_SESSION["savestats"][$id] = $status;
// Save the status ID
$_SESSION["savestats"][$id]["id"] = $id;
// Return the status ID
return $id;
}
// retrieve_status: Retrieve a previously suspended status from $_SESSION
function &retrieve_status($statid = null)
{
// Cache the result
static $cache = array();
// Return the cache
if (is_null($statid)) {
// $cache[0] is the default (previous form)
if (array_key_exists(0, $cache)) {
return $cache[0];
}
} else {
if (array_key_exists($statid, $cache)) {
return $cache[$statid];
}
}
// Session not in use, return null in all cases
if (!isset($_SESSION)) {
if (is_null($statid)) {
$cache[0] = null;
return $cache[0];
} else {
$cache[$statid] = null;
return $cache[$statid];
}
}
// Initialize the status deposit
if (!array_key_exists("savestats", $_SESSION)) {
$_SESSION["savestats"] = array();
if (is_null($statid)) {
$cache[0] = null;
return $cache[0];
} else {
$cache[$statid] = null;
return $cache[$statid];
}
}
// Obtain the previous suspended status if status not specified
if (is_null($statid)) {
// Obtain the form
$FORM =& get_or_post();
// Return empty if not set
if (!array_key_exists("statid", $FORM)) {
$cache[0] = null;
return $cache[0];
}
$statid = $FORM["statid"];
// There is no such previously suspended status
if (!array_key_exists($statid, $_SESSION["savestats"])) {
$cache[$statid] = null;
$cache[0] = null;
return $cache[0];
}
$cache[$statid] = $_SESSION["savestats"][$statid];
$cache[0] =& $cache[$statid];
} else {
// There is no such previously suspended status
if (!array_key_exists($statid, $_SESSION["savestats"])) {
$cache[$statid] = null;
return $cache[$statid];
}
$cache[$statid] = $_SESSION["savestats"][$statid];
}
// Return the status
return $cache[$statid];
}
// _callform_form_this: Find the processing script
function _callform_form_this()
{
static $cache;
if (isset($cache)) {
return $cache;
}
$FORM =& get_or_post();
$cache = (array_key_exists("referer", $FORM)? $FORM["referer"]:
REQUEST_FILE);
return $cache;
}
?>