346 lines
9.8 KiB
PHP
346 lines
9.8 KiB
PHP
<?php
|
||
// File name: echoform.inc.php
|
||
// Description: PHP subroutines to echo parts of a form
|
||
// Date: 2002-05-17
|
||
// Author: imacat <imacat@pristine.com.tw>
|
||
// Copyright: Copyright (C) 2002-2007 Pristine Communications
|
||
|
||
// This file is in UTF-8 萬國碼
|
||
|
||
// Set the include path
|
||
if (!defined("INCPATH_SET")) {
|
||
require_once dirname(__FILE__) . "/incpath.inc.php";
|
||
}
|
||
// Referenced subroutines
|
||
require_once "monica/callform.inc.php";
|
||
require_once "monica/commtext.inc.php";
|
||
require_once "monica/formfunc.inc.php";
|
||
require_once "monica/getlang.inc.php";
|
||
require_once "monica/gettext.inc.php";
|
||
require_once "monica/htmlchar.inc.php";
|
||
require_once "monica/markabbr.inc.php";
|
||
require_once "monica/requri.inc.php";
|
||
require_once "monica/sql.inc.php";
|
||
|
||
// locale_date: Obtain a localed date representation
|
||
function locale_date($date, $lang = null)
|
||
{
|
||
if (is_null($lang)) {
|
||
$lang = getlang();
|
||
}
|
||
switch ($lang) {
|
||
case "zh-tw":
|
||
$d = localtime($date);
|
||
return sprintf("%d 年 %d 月 %d 日",
|
||
$d[5]+1900-1911, $d[4]+1, $d[3]);
|
||
case "de":
|
||
return date("F j Y", $date);
|
||
case "en":
|
||
default:
|
||
return date("F j Y", $date);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// locale_month: Obtain a localed month representation
|
||
function locale_month($date, $lang = null)
|
||
{
|
||
if (is_null($lang)) {
|
||
$lang = getlang();
|
||
}
|
||
switch ($lang) {
|
||
case "zh-tw":
|
||
$d = localtime($date);
|
||
return sprintf("%d 年 %d 月",
|
||
$d[5]+1900-1911, $d[4]+1);
|
||
case "de":
|
||
case "en":
|
||
default:
|
||
return date("F Y", $date);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// locale_year: Obtain a localed year representation
|
||
function locale_year($date, $lang = null)
|
||
{
|
||
if (is_null($lang)) {
|
||
$lang = getlang();
|
||
}
|
||
switch ($lang) {
|
||
case "zh-tw":
|
||
$d = localtime($date);
|
||
return sprintf("%d 年", $d[5]+1900-1911);
|
||
case "de":
|
||
case "en":
|
||
default:
|
||
return date("Y", $date);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// locale_amount: Obtain a localed amount representation
|
||
function locale_amount($amount, $lang = null)
|
||
{
|
||
if (is_null($lang)) {
|
||
$lang = getlang();
|
||
}
|
||
switch ($lang) {
|
||
case "zh-tw":
|
||
// 一萬以下
|
||
if (mb_strlen($amount) <= 4) {
|
||
return $amount;
|
||
// 一億以下
|
||
} elseif (mb_strlen($amount) <= 8) {
|
||
$sig = mb_substr($amount, 0, -4) . "." . mb_substr($amount, -4);
|
||
while (mb_substr($sig, -1) == "0") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
if (mb_substr($sig, -1) == ".") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
return sprintf("%s ( %s 萬)",
|
||
$amount, $sig);
|
||
// 一兆以下
|
||
} elseif (mb_strlen($amount) <= 12) {
|
||
$sig = mb_substr($amount, 0, -8) . "." . mb_substr($amount, -8);
|
||
while (mb_substr($sig, -1) == "0") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
if (mb_substr($sig, -1) == ".") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
return sprintf("%s ( %s 億)",
|
||
$amount, $sig);
|
||
// 一兆以上
|
||
} else {
|
||
$sig = mb_substr($amount, 0, -12) . "." . mb_substr($amount, -12);
|
||
while (mb_substr($sig, -1) == "0") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
if (mb_substr($sig, -1) == ".") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
return sprintf("%s ( %s 兆)",
|
||
$amount, $sig);
|
||
}
|
||
case "en":
|
||
// Less than one thousand
|
||
if (mb_strlen($amount) <= 3) {
|
||
return $amount;
|
||
// Less than one million
|
||
} elseif (mb_strlen($amount) <= 6) {
|
||
$sig = mb_substr($amount, 0, -3) . "." . mb_substr($amount, -3);
|
||
while (mb_substr($sig, -1) == "0") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
if (mb_substr($sig, -1) == ".") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
return sprintf("%s (%s thousands)",
|
||
$amount, $sig);
|
||
// Less than one billion
|
||
} elseif (mb_strlen($amount) <= 9) {
|
||
$sig = mb_substr($amount, 0, -6) . "." . mb_substr($amount, -6);
|
||
while (mb_substr($sig, -1) == "0") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
if (mb_substr($sig, -1) == ".") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
return sprintf("%s (%s millions)",
|
||
$amount, $sig);
|
||
// More than one billion
|
||
} else {
|
||
$sig = mb_substr($amount, 0, -9) . "." . mb_substr($amount, -9);
|
||
while (mb_substr($sig, -1) == "0") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
if (mb_substr($sig, -1) == ".") {
|
||
$sig = mb_substr($sig, 0, -1);
|
||
}
|
||
return sprintf("%s (%s billions)",
|
||
$amount, $sig);
|
||
}
|
||
}
|
||
return;
|
||
}
|
||
|
||
// report_size: Obtain a human readable size
|
||
function report_size($size)
|
||
{
|
||
// Get the size
|
||
$report = sprintf(C_("%s bytes"), number_format($size));
|
||
|
||
// Try to use KB as the unit
|
||
$kb = $size / 1024;
|
||
// Bounce if there are fewer than 3 digits in the rounded result
|
||
if (round($kb * 100, 0) < 100) {
|
||
return $report;
|
||
}
|
||
// Check the rounded result for each digit
|
||
for ($d = 2; $d >= 0; $d--) {
|
||
$digits = pow(10, $d);
|
||
$rounded = round($kb * $digits, 0);
|
||
// There are 3 significient digits in the rounded result
|
||
if ($rounded < 1000) {
|
||
return sprintf("%s (%.".$d."f KB)",
|
||
$report, $rounded / $digits);
|
||
}
|
||
}
|
||
|
||
// Try to use MB as the unit
|
||
$mb = $kb / 1024;
|
||
// Check each digit
|
||
for ($d = 2; $d >= 0; $d--) {
|
||
$digits = pow(10, $d);
|
||
$rounded = round($mb * $digits, 0);
|
||
// There are 3 significient digits in the rounded result
|
||
if ($rounded < 1000) {
|
||
return sprintf("%s (%.".$d."f MB)",
|
||
$report, $rounded / $digits);
|
||
}
|
||
}
|
||
|
||
// Try to use GB as the unit
|
||
$gb = $mb / 1024;
|
||
// Check each digit
|
||
for ($d = 2; $d >= 0; $d--) {
|
||
$digits = pow(10, $d);
|
||
$rounded = round($gb * $digits, 0);
|
||
// There are 3 significient digits in the rounded result
|
||
if ($rounded < 1000) {
|
||
return sprintf("%s (%.".$d."f GB)",
|
||
$report, $rounded / $digits);
|
||
}
|
||
}
|
||
|
||
// Try to use TB as the unit
|
||
$tb = $gb / 1024;
|
||
// Check each digit
|
||
for ($d = 2; $d >= 0; $d--) {
|
||
$digits = pow(10, $d);
|
||
$rounded = round($tb * $digits, 0);
|
||
// There are 3 significient digits in the rounded result
|
||
if ($rounded < 1000) {
|
||
return sprintf("%s (%.".$d."f TB)",
|
||
$report, $rounded / $digits);
|
||
}
|
||
}
|
||
|
||
// More than TB
|
||
return sprintf("%s (%s TB)",
|
||
$report, number_format(round($tb, 0)));
|
||
}
|
||
|
||
// auto_keep_referer: If we should keep the referer information
|
||
function auto_keep_referer($auto_keep_referer = null)
|
||
{
|
||
// Cache the result
|
||
static $cache;
|
||
// Set the value
|
||
if (!is_null($auto_keep_referer)) {
|
||
return ($cache = $auto_keep_referer);
|
||
}
|
||
// Return the cache
|
||
if (isset($cache)) {
|
||
return $cache;
|
||
}
|
||
|
||
// Only work for forms
|
||
if (!is_form()) {
|
||
return ($cache = false);
|
||
}
|
||
// Not for called forms
|
||
if (is_called_form()) {
|
||
return ($cache = false);
|
||
}
|
||
// Only work for the first forms
|
||
if (!is_first_form()) {
|
||
return ($cache = false);
|
||
}
|
||
// Respect the specified referer
|
||
$FORM =& curform();
|
||
if (array_key_exists("referer", $FORM)) {
|
||
return ($cache = false);
|
||
}
|
||
// Referer not specified
|
||
if (!array_key_exists("HTTP_REFERER", $_SERVER)) {
|
||
return ($cache = false);
|
||
}
|
||
|
||
// Only keep referer from the same host
|
||
// Check the prefix of the referer
|
||
$prefix = REQUEST_HOSTPORT . "/";
|
||
return ($cache = (substr($_SERVER["HTTP_REFERER"], 0, strlen($prefix)) == $prefix));
|
||
}
|
||
|
||
// opt_list: Return an options list
|
||
function opt_list($select, $curval)
|
||
{
|
||
// Cache the result
|
||
$cache = array();
|
||
|
||
// Not cached yet
|
||
if (!array_key_exists($select, $cache)) {
|
||
$result = sql_query($select);
|
||
$count = sql_num_rows($result);
|
||
for ($i = 0, $opts = array(); $i < $count; $i++) {
|
||
$row = sql_fetch_assoc($result);
|
||
$opts[] = array(
|
||
"value" => $row["value"],
|
||
"content" => $row["content"],
|
||
);
|
||
}
|
||
// Obtain the HTML
|
||
$cache[$select] = opt_list_array($opts);
|
||
}
|
||
|
||
return preselect_options($cache[$select], $curval);
|
||
}
|
||
|
||
// opt_list_array: Return an options list from an array
|
||
function opt_list_array($opts)
|
||
{
|
||
// Obtain the HTML
|
||
ob_start();
|
||
?> <option value=""><?php echo h_abbr(t_notset()); ?></option>
|
||
<?php
|
||
for ($i = 0; $i < count($opts); $i++) {
|
||
$value = $opts[$i]["value"];
|
||
?> <option value="<?php echo h($value); ?>"><?php
|
||
echo h($opts[$i]["content"]); ?></option>
|
||
<?php
|
||
}
|
||
$html = ob_get_contents();
|
||
ob_end_clean();
|
||
|
||
return $html;
|
||
}
|
||
|
||
// preselect_options: Presect an option in an option list
|
||
function preselect_options($html, $value)
|
||
{
|
||
// Not selected if value not set
|
||
if (is_null($value)) {
|
||
return $html;
|
||
}
|
||
$value = h($value);
|
||
$option = "<option value=\"$value\">";
|
||
$pos = strpos($html, $option);
|
||
|
||
// Not selected if there is no such value in the options
|
||
if ($pos === false) {
|
||
return $html;
|
||
}
|
||
|
||
$prefix = substr($html, 0, $pos);
|
||
$suffix = substr($html, $pos + strlen($option));
|
||
$selected = "<option value=\"$value\" selected=\"selected\">";
|
||
|
||
$html = $prefix . $selected . $suffix;
|
||
return $html;
|
||
}
|
||
|
||
?>
|