Source for file Ethna_Util.php
Documentation is available at Ethna_Util.php
// vim: foldmethod=marker
* @author Masaki Fujimoto <fujimoto@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* グローバルユーティリティ関数: スカラー値を要素数1の配列として返す
* @param mixed $v 配列として扱う値
* @return array 配列に変換された値
* グローバルユーティリティ関数: 指定されたフォーム項目にエラーがあるかどうかを返す
* @param string $name フォーム項目名
* @return bool true:エラー有り false:エラー無し
$action_error =
& $c->getActionError();
return $action_error->isError($name);
return $action_error->count() >
0;
* グローバルユーティリティ関数: include_pathを検索しつつfile_exists()する
* @param string $path ファイル名
* @param bool $use_include_path include_pathをチェックするかどうか
* @return bool true:有り false:無し
if ($use_include_path ==
false) {
if (is_array($include_path_list) ==
false) {
foreach ($include_path_list as $include_path) {
* グローバルユーティリティ関数: 絶対パスかどうかを返す
* @param string $path ファイル名
* @return bool true:絶対 false:相対
* @author Masaki Fujimoto <fujimoto@php.net>
* @return bool true:2回目以降のPOST false:1回目のPOST
if (isset
($_POST['uniqid'])) {
$uniqid =
$_POST['uniqid'];
} else if (isset
($_GET['uniqid'])) {
$uniqid =
$_GET['uniqid'];
$filename =
sprintf("%s/uniqid_%s_%s",
if ($st[9] +
60*
60*
1 <
time()) {
// {{{ clearDuplicatePost
* @return mixed 0:正常終了 Ethna_Error:エラー
if (isset
($_POST['uniqid'])) {
$uniqid =
$_POST['uniqid'];
$filename =
sprintf("%s/uniqid_%s_%s",
if (unlink($filename) ==
false) {
* @return bool true:正常なPOST false:不正なPOST
$name =
$c->config->get('csrf');
$plugin =
& $c->getPlugin('Csrf', $name);
$csrf =
& $plugin->getPlugin('Csrf', $name);
$name =
$c->config->get('csrf');
$plugin =
& $c->getPlugin('Csrf', $name);
$csrf =
& $plugin->getPlugin('Csrf', $name);
* @param string $mailaddress チェックするメールアドレス
* @return bool true: 正しいメールアドレス false: 不正な形式
if (preg_match('/^([a-z0-9_]|\-|\.|\+)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,6}$/i',
* @param string $csv CSV形式の文字列(1行分)
* @param string $delimiter フィールドの区切り文字
* @return mixed (array):分割結果 Ethna_Error:エラー(行継続)
foreach (array(" ", "\t", "\r", "\n") as $c) {
if (preg_match("/([$space_list]+)\$/sS", $csv, $match)) {
// 1. skip leading spaces
if ($index >=
$csv_len) {
if ($csv{$index} ==
'"') {
// 2A. handle quote delimited field
while ($index <
$csv_len) {
if ($csv{$index} ==
'"') {
if ($csv{$index+
1} ==
'"') {
while ($csv{$index} !=
$delimiter &&
$index <
$csv_len) {
if ($csv{$index} ==
$delimiter) {
if ($index ==
$csv_len) {
// 2B. handle non-quoted field
// remove trailing spaces
if ($csv{$index} ==
$delimiter) {
} while ($index <
$csv_len);
* @param string $csv エスケープ対象の文字列(CSVの各要素)
* @param bool $escape_nl 改行文字(\r/\n)のエスケープフラグ
* @return string CSVエスケープされた文字列
* @param array $target HTMLエスケープ対象となる配列
* @return array エスケープされた配列
* @param mixed $vars HTMLエスケープ対象となる配列
* @param mixed $retval HTMLエスケープ対象となる子要素
$retval[$name] =
array();
* @param string $string MIMEエンコードする文字列
* @param int $total 検索総件数
* @param int $offset 表示オフセット
* @return array リンク情報を格納した配列
$direct_link_list =
array();
$current =
$offset -
$count;
if ($offset !=
0 &&
$current <=
0) {
$backward_count =
count($direct_link_list);
$current =
$offset +
$count;
for ($i =
0; $i <
10; $i++
) {
if ($current >=
$total) {
$forward_count =
count($direct_link_list) -
$backward_count -
1;
if ($forward_count <
5) {
$backward_count -=
5 -
$forward_count;
if ($backward_count <
0) {
foreach ($direct_link_list as $direct_link) {
$v =
array('offset' =>
$direct_link, 'index' =>
$n);
* @param int $t unix time
* @return string 元号(不明な場合はnull)
$year =
$tm['tm_year'] +
1900;
$heisei_str =
_et('Heisei');
return array($heisei_str, $year -
1988);
} else if ($year >=
1926) {
$showa_str =
_et('Showa');
return array($showa_str, $year -
1925);
* getimagesize()の返すイメージタイプに対応する拡張子を返す
* @param int $type getimagesize()関数の返すイメージタイプ
* @return string $typeに対応する拡張子
return @$ext_list[$type];
* @param int $length ハッシュ値の長さ(〜64)
list
($usec, $sec) =
explode(' ', microtime());
// open_basedir がオンで、かつ /proc が許可されているか?
// open_basedir が空なら許可されていると看做す
$devfile =
'/proc/net/dev';
$open_basedir_conf =
ini_get('open_basedir');
$devfile_enabled =
(empty($open_basedir_conf)
||
(preg_match('#:/proc#', $open_basedir_conf) >
0
||
preg_match('#^/proc#', $open_basedir_conf) >
0));
for ($i =
0; $i <
2; $i++
) {
if ($devfile_enabled &&
file_exists($devfile)) {
$fp =
fopen($devfile, 'r');
while (feof($fp) ===
false) {
$v =
$now .
$time['usec'] .
$platform_value .
mt_rand(0, time());
$value =
substr($value, 0, $length);
* @param array $array 処理対象の1次元配列
* @param int $order $mをX軸と見做すかY軸と見做すか(0:X軸 1:Y軸)
* @return array m x nに再構成された配列
if ((count($array) %
$m) >
0) {
for ($i =
0; $i <
$n; $i++
) {
for ($j =
0; $j <
$m; $j++
) {
* port from File in PEAR (for BC)
* @return bool true:絶対パス false:相対パス
&&
(substr($path, 0, 1) ==
'/' ||
substr($path, 0, 1) ==
'~')) {
if (is_dir($path) ===
false) {
* @param string $dir 作成するディレクトリ
* @param int $mode パーミッション
* @return bool true:成功 false:失敗
function mkdir($dir, $mode)
if (is_dir($parent) ===
false) {
function chmod($file, $mode)
if (($st[2] & 0777) ==
$mode) {
return chmod($file, $mode);
* (途中で失敗しても中断せず、削除できるものはすべて消す)
* @param string $file 削除するファイルまたはディレクトリ
* @return bool true:成功 false:失敗
while (($entry =
readdir($dh)) !==
false) {
if ($entry ===
'.' ||
$entry ===
'..') {
* @param string $prefix ファイルのプレフィクス
* @param int $timeout 削除対象閾値(秒−60*60*1なら1時間)
$dh =
opendir($c->getDirectory('tmp'));
while (($file =
readdir($dh)) !==
false) {
if ($file ==
'.' ||
$file ==
'..') {
} else if (is_dir($c->getDirectory('tmp') .
'/' .
$file)) {
$f =
$c->getDirectory('tmp') .
"/" .
$file;
if ($st[9] +
$timeout <
time()) {
* @param string $file ロックするファイル名
* @param int $mode ロックモード('r', 'rw')
* @param int $timeout ロック待ちタイムアウト(秒−0なら無限)
* @return int ロックハンドル(falseならエラー)
function lockFile($file, $mode, $timeout =
0)
$lh =
fopen($file, $mode);
$lock_mode =
$mode ==
'r' ?
LOCK_SH :
LOCK_EX;
for ($i =
0; $i <
$timeout ||
$timeout ==
0; $i++
) {
$r =
flock($lh, $lock_mode |
LOCK_NB);
if ($timeout >
0 &&
$i ==
$timeout) {
* @param array $bt debug_backtrace()関数で取得したバックトレース
* @return string 文字列にフォーマットされたバックトレース
$r .=
sprintf("[%02d] %s:%d:%s.%s\n", $i,
isset
($elt['file']) ?
$elt['file'] :
'unknown file',
isset
($elt['line']) ?
$elt['line'] :
'unknown line',
isset
($elt['class']) ?
$elt['class'] :
'global',
if (isset
($elt['args']) ==
false ||
is_array($elt['args']) ==
false) {
foreach ($elt['args'] as $arg) {
* @param string $arg バックトレースの引数
* @param int $level バックトレースのネストレベル
* @return string 文字列にフォーマットされたバックトレース
function _formatBacktrace($arg, $level =
0, $wrap =
true)
$r =
sprintf(" %s[array] => (\n", $pad);
$r .=
sprintf(" %s *too deep*\n", $pad);
foreach ($arg as $key =>
$elt) {
$r .=
Ethna_Util::_formatBacktrace($key, $level, false);
$r .=
Ethna_Util::_formatBacktrace($elt, $level+
1);
$r =
sprintf(" %s[%s]%s%s", $pad, gettype($arg), $arg, $wrap ?
"\n" :
"");
Documentation generated on Fri, 11 Nov 2011 04:01:06 +0900 by phpDocumentor 1.4.3