| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 6 | * |
|---|
| 7 | * http://www.lockon.co.jp/ |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or |
|---|
| 10 | * modify it under the terms of the GNU General Public License |
|---|
| 11 | * as published by the Free Software Foundation; either version 2 |
|---|
| 12 | * of the License, or (at your option) any later version. |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * 各種ユーティリティクラス. |
|---|
| 26 | * |
|---|
| 27 | * 主に static 参照するユーティリティ系の関数群 |
|---|
| 28 | * |
|---|
| 29 | * :XXX: 内部でインスタンスを生成している関数は, Helper クラスへ移動するべき... |
|---|
| 30 | * |
|---|
| 31 | * @package Util |
|---|
| 32 | * @author LOCKON CO.,LTD. |
|---|
| 33 | * @version $Id:SC_Utils.php 15532 2007-08-31 14:39:46Z nanasess $ |
|---|
| 34 | */ |
|---|
| 35 | class SC_Utils { |
|---|
| 36 | |
|---|
| 37 | // インストール初期処理 |
|---|
| 38 | function sfInitInstall() { |
|---|
| 39 | // インストール済みが定義されていない。 |
|---|
| 40 | if (!defined('ECCUBE_INSTALL')) { |
|---|
| 41 | $phpself = $_SERVER['PHP_SELF']; |
|---|
| 42 | if (strpos('/install/', $phpself) === false) { |
|---|
| 43 | $path = substr($phpself, 0, strpos($phpself, basename($phpself))); |
|---|
| 44 | $install_url = SC_Utils_Ex::searchInstallerPath($path); |
|---|
| 45 | header('Location: ' . $install_url); |
|---|
| 46 | exit; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | $path = HTML_REALDIR . 'install/' . DIR_INDEX_FILE; |
|---|
| 50 | if (file_exists($path)) { |
|---|
| 51 | SC_Utils_Ex::sfErrorHeader('>> /install/' . DIR_INDEX_FILE . ' は、インストール完了後にファイルを削除してください。'); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * インストーラのパスを検索し, URL を返す. |
|---|
| 57 | * |
|---|
| 58 | * $path と同階層に install/index.php があるか検索する. |
|---|
| 59 | * 存在しない場合は上位階層を再帰的に検索する. |
|---|
| 60 | * インストーラのパスが見つかった場合は, その URL を返す. |
|---|
| 61 | * DocumentRoot まで検索しても見つからない場合は /install/index.php を返す. |
|---|
| 62 | * |
|---|
| 63 | * @param string $path 検索対象のパス |
|---|
| 64 | * @return string インストーラの URL |
|---|
| 65 | */ |
|---|
| 66 | function searchInstallerPath($path) { |
|---|
| 67 | $installer = 'install/' . DIR_INDEX_PATH; |
|---|
| 68 | |
|---|
| 69 | if (SC_Utils_Ex::sfIsHTTPS()) { |
|---|
| 70 | $proto = "https://"; |
|---|
| 71 | } else { |
|---|
| 72 | $proto = "http://"; |
|---|
| 73 | } |
|---|
| 74 | $host = $proto . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']; |
|---|
| 75 | if ($path == '/') { |
|---|
| 76 | return $host . $path . $installer; |
|---|
| 77 | } |
|---|
| 78 | if (substr($path, -1, 1) != '/') { |
|---|
| 79 | $path .= $path . '/'; |
|---|
| 80 | } |
|---|
| 81 | $installer_url = $host . $path . $installer; |
|---|
| 82 | $resources = fopen(SC_Utils_Ex::getRealURL($installer_url), 'r'); |
|---|
| 83 | if ($resources === false) { |
|---|
| 84 | $installer_url = SC_Utils_Ex::searchInstallerPath($path . '../'); |
|---|
| 85 | } |
|---|
| 86 | return $installer_url; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * 相対パスで記述された URL から絶対パスの URL を取得する. |
|---|
| 91 | * |
|---|
| 92 | * この関数は, http(s):// から始まる URL を解析し, 相対パスで記述されていた |
|---|
| 93 | * 場合, 絶対パスに変換して返す |
|---|
| 94 | * |
|---|
| 95 | * 例) |
|---|
| 96 | * http://www.example.jp/aaa/../index.php |
|---|
| 97 | * ↓ |
|---|
| 98 | * http://www.example.jp/index.php |
|---|
| 99 | * |
|---|
| 100 | * @param string $url http(s):// から始まる URL |
|---|
| 101 | * @return string $url を絶対パスに変換した URL |
|---|
| 102 | */ |
|---|
| 103 | function getRealURL($url) { |
|---|
| 104 | $parse = parse_url($url); |
|---|
| 105 | $tmp = explode('/', $parse['path']); |
|---|
| 106 | $results = array(); |
|---|
| 107 | foreach ($tmp as $v) { |
|---|
| 108 | if ($v == '' || $v == '.') { |
|---|
| 109 | // queit. |
|---|
| 110 | } elseif ($v == '..') { |
|---|
| 111 | array_pop($results); |
|---|
| 112 | } else { |
|---|
| 113 | array_push($results, $v); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | $path = join('/', $results); |
|---|
| 118 | return $parse['scheme'] . '://' . $parse['host'] . ':' . $parse['port'] .'/' . $path; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | // 装飾付きエラーメッセージの表示 |
|---|
| 122 | function sfErrorHeader($mess, $print = false) { |
|---|
| 123 | global $GLOBAL_ERR; |
|---|
| 124 | $GLOBAL_ERR.= '<div id="errorHeader">'; |
|---|
| 125 | $GLOBAL_ERR.= $mess; |
|---|
| 126 | $GLOBAL_ERR.= "</div>"; |
|---|
| 127 | if ($print) { |
|---|
| 128 | echo $GLOBAL_ERR; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /* エラーページの表示 */ |
|---|
| 133 | function sfDispError($type) { |
|---|
| 134 | |
|---|
| 135 | require_once CLASS_EX_REALDIR . 'page_extends/error/LC_Page_Error_DispError_Ex.php'; |
|---|
| 136 | |
|---|
| 137 | $objPage = new LC_Page_Error_DispError_Ex(); |
|---|
| 138 | register_shutdown_function(array($objPage, 'destroy')); |
|---|
| 139 | $objPage->init(); |
|---|
| 140 | $objPage->type = $type; |
|---|
| 141 | $objPage->process(); |
|---|
| 142 | exit; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /* サイトエラーページの表示 */ |
|---|
| 146 | function sfDispSiteError($type, $objSiteSess = "", $return_top = false, $err_msg = "") { |
|---|
| 147 | |
|---|
| 148 | require_once CLASS_EX_REALDIR . 'page_extends/error/LC_Page_Error_Ex.php'; |
|---|
| 149 | |
|---|
| 150 | $objPage = new LC_Page_Error_Ex(); |
|---|
| 151 | register_shutdown_function(array($objPage, 'destroy')); |
|---|
| 152 | $objPage->init(); |
|---|
| 153 | $objPage->type = $type; |
|---|
| 154 | $objPage->objSiteSess = $objSiteSess; |
|---|
| 155 | $objPage->return_top = $return_top; |
|---|
| 156 | $objPage->err_msg = $err_msg; |
|---|
| 157 | $objPage->is_mobile = SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE; |
|---|
| 158 | $objPage->process(); |
|---|
| 159 | exit; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * 例外エラーページの表示 |
|---|
| 164 | * |
|---|
| 165 | * @param string $debugMsg デバッグ用のメッセージ |
|---|
| 166 | * @return void |
|---|
| 167 | */ |
|---|
| 168 | function sfDispException($debugMsg = null) { |
|---|
| 169 | require_once CLASS_EX_REALDIR . 'page_extends/error/LC_Page_Error_SystemError_Ex.php'; |
|---|
| 170 | |
|---|
| 171 | $objPage = new LC_Page_Error_SystemError_Ex(); |
|---|
| 172 | register_shutdown_function(array($objPage, 'destroy')); |
|---|
| 173 | $objPage->init(); |
|---|
| 174 | if (!is_null($debugMsg)) { |
|---|
| 175 | $objPage->addDebugMsg($debugMsg); |
|---|
| 176 | } |
|---|
| 177 | if (function_exists("debug_backtrace")) { |
|---|
| 178 | $objPage->backtrace = debug_backtrace(); |
|---|
| 179 | } |
|---|
| 180 | GC_Utils_Ex::gfPrintLog($objPage->sfGetErrMsg()); |
|---|
| 181 | $objPage->process(); |
|---|
| 182 | |
|---|
| 183 | exit(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /* 認証の可否判定 */ |
|---|
| 187 | function sfIsSuccess($objSess, $disp_error = true) { |
|---|
| 188 | $ret = $objSess->IsSuccess(); |
|---|
| 189 | if ($ret != SUCCESS) { |
|---|
| 190 | if ($disp_error) { |
|---|
| 191 | // エラーページの表示 |
|---|
| 192 | SC_Utils_Ex::sfDispError($ret); |
|---|
| 193 | } |
|---|
| 194 | return false; |
|---|
| 195 | } |
|---|
| 196 | // リファラーチェック(CSRFの暫定的な対策) |
|---|
| 197 | // 「リファラ無」 の場合はスルー |
|---|
| 198 | // 「リファラ有」 かつ 「管理画面からの遷移でない」 場合にエラー画面を表示する |
|---|
| 199 | if (empty($_SERVER['HTTP_REFERER'])) { |
|---|
| 200 | // TODO 警告表示させる? |
|---|
| 201 | // sfErrorHeader('>> referrerが無効になっています。'); |
|---|
| 202 | } else { |
|---|
| 203 | $domain = SC_Utils_Ex::sfIsHTTPS() ? HTTPS_URL : HTTP_URL; |
|---|
| 204 | $pattern = sprintf('|^%s.*|', $domain); |
|---|
| 205 | $referer = $_SERVER['HTTP_REFERER']; |
|---|
| 206 | |
|---|
| 207 | // 管理画面から以外の遷移の場合はエラー画面を表示 |
|---|
| 208 | if (!preg_match($pattern, $referer)) { |
|---|
| 209 | if ($disp_error) SC_Utils_Ex::sfDispError(INVALID_MOVE_ERRORR); |
|---|
| 210 | return false; |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | return true; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * 文字列をアスタリスクへ変換する. |
|---|
| 218 | * |
|---|
| 219 | * @param string $passlen 変換する文字列 |
|---|
| 220 | * @return string アスタリスクへ変換した文字列 |
|---|
| 221 | */ |
|---|
| 222 | function sfPassLen($passlen) { |
|---|
| 223 | $ret = ""; |
|---|
| 224 | for ($i=0;$i<$passlen;true) { |
|---|
| 225 | $ret.="*"; |
|---|
| 226 | $i++; |
|---|
| 227 | } |
|---|
| 228 | return $ret; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | * HTTPSかどうかを判定 |
|---|
| 233 | * |
|---|
| 234 | * @return bool |
|---|
| 235 | */ |
|---|
| 236 | function sfIsHTTPS() { |
|---|
| 237 | // HTTPS時には$_SERVER['HTTPS']には空でない値が入る |
|---|
| 238 | // $_SERVER['HTTPS'] != 'off' はIIS用 |
|---|
| 239 | if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { |
|---|
| 240 | return true; |
|---|
| 241 | } else { |
|---|
| 242 | return false; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | /** |
|---|
| 247 | * 正規の遷移がされているかを判定 |
|---|
| 248 | * 前画面でuniqidを埋め込んでおく必要がある |
|---|
| 249 | * @param obj SC_Session, SC_SiteSession |
|---|
| 250 | * @return bool |
|---|
| 251 | */ |
|---|
| 252 | function sfIsValidTransition($objSess) { |
|---|
| 253 | // 前画面からPOSTされるuniqidが正しいものかどうかをチェック |
|---|
| 254 | $uniqid = $objSess->getUniqId(); |
|---|
| 255 | if (!empty($_POST['uniqid']) && ($_POST['uniqid'] === $uniqid)) { |
|---|
| 256 | return true; |
|---|
| 257 | } else { |
|---|
| 258 | return false; |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | /* DB用日付文字列取得 */ |
|---|
| 263 | function sfGetTimestamp($year, $month, $day, $last = false) { |
|---|
| 264 | if ($year != "" && $month != "" && $day != "") { |
|---|
| 265 | if ($last) { |
|---|
| 266 | $time = "23:59:59"; |
|---|
| 267 | } else { |
|---|
| 268 | $time = "00:00:00"; |
|---|
| 269 | } |
|---|
| 270 | $date = $year."-".$month."-".$day." ".$time; |
|---|
| 271 | } else { |
|---|
| 272 | $date = ""; |
|---|
| 273 | } |
|---|
| 274 | return $date; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /** |
|---|
| 278 | * INT型の数値チェック |
|---|
| 279 | * ・FIXME: マイナス値の扱いが不明確 |
|---|
| 280 | * ・XXX: INT_LENには収まるが、INT型の範囲を超えるケースに対応できないのでは? |
|---|
| 281 | * |
|---|
| 282 | * @param mixed $value |
|---|
| 283 | * @return bool |
|---|
| 284 | */ |
|---|
| 285 | // |
|---|
| 286 | function sfIsInt($value) { |
|---|
| 287 | if (strlen($value) >= 1 && strlen($value) <= INT_LEN && is_numeric($value)) { |
|---|
| 288 | return true; |
|---|
| 289 | } |
|---|
| 290 | return false; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | /* |
|---|
| 294 | * 桁が0で埋められているかを判定する |
|---|
| 295 | * |
|---|
| 296 | * @param string $value 検査対象 |
|---|
| 297 | * @return boolean 0で埋められている |
|---|
| 298 | */ |
|---|
| 299 | function sfIsZeroFilling($value) { |
|---|
| 300 | if (strlen($value) > 1 && $value{0} === '0') |
|---|
| 301 | return true; |
|---|
| 302 | return false; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | function sfGetCSVData($data, $prefix = "") { |
|---|
| 306 | if ($prefix == "") { |
|---|
| 307 | $dir_name = SC_Utils_Ex::sfUpDirName(); |
|---|
| 308 | $file_name = $dir_name . date('ymdHis') .".csv"; |
|---|
| 309 | } else { |
|---|
| 310 | $file_name = $prefix . date('ymdHis') .".csv"; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | if (mb_internal_encoding() == CHAR_CODE) { |
|---|
| 314 | $data = mb_convert_encoding($data,'SJIS-Win',CHAR_CODE); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | /* データを出力 */ |
|---|
| 318 | return array($file_name, $data); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | /* 1階層上のディレクトリ名を取得する */ |
|---|
| 322 | function sfUpDirName() { |
|---|
| 323 | $path = $_SERVER['PHP_SELF']; |
|---|
| 324 | $arrVal = explode("/", $path); |
|---|
| 325 | $cnt = count($arrVal); |
|---|
| 326 | return $arrVal[($cnt - 2)]; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | // チェックボックスの値をマージ |
|---|
| 330 | /** |
|---|
| 331 | * @deprecated |
|---|
| 332 | */ |
|---|
| 333 | function sfMergeCBValue($keyname, $max) { |
|---|
| 334 | $conv = ""; |
|---|
| 335 | $cnt = 1; |
|---|
| 336 | for ($cnt = 1; $cnt <= $max; $cnt++) { |
|---|
| 337 | if ($_POST[$keyname . $cnt] == "1") { |
|---|
| 338 | $conv.= "1"; |
|---|
| 339 | } else { |
|---|
| 340 | $conv.= "0"; |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | return $conv; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | // html_checkboxesの値をマージして2進数形式に変更する。 |
|---|
| 347 | /** |
|---|
| 348 | * @deprecated |
|---|
| 349 | */ |
|---|
| 350 | function sfMergeCheckBoxes($array, $max) { |
|---|
| 351 | $ret = ""; |
|---|
| 352 | if (is_array($array)) { |
|---|
| 353 | foreach ($array as $val) { |
|---|
| 354 | $arrTmp[$val] = "1"; |
|---|
| 355 | } |
|---|
| 356 | } |
|---|
| 357 | for ($i = 1; $i <= $max; $i++) { |
|---|
| 358 | if (isset($arrTmp[$i]) && $arrTmp[$i] == "1") { |
|---|
| 359 | $ret.= "1"; |
|---|
| 360 | } else { |
|---|
| 361 | $ret.= "0"; |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | return $ret; |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | // html_checkboxesの値をマージして「-」でつなげる。 |
|---|
| 368 | /** |
|---|
| 369 | * @deprecated |
|---|
| 370 | */ |
|---|
| 371 | function sfMergeParamCheckBoxes($array) { |
|---|
| 372 | $ret = ''; |
|---|
| 373 | if (is_array($array)) { |
|---|
| 374 | foreach ($array as $val) { |
|---|
| 375 | if ($ret != "") { |
|---|
| 376 | $ret.= "-$val"; |
|---|
| 377 | } else { |
|---|
| 378 | $ret = $val; |
|---|
| 379 | } |
|---|
| 380 | } |
|---|
| 381 | } else { |
|---|
| 382 | $ret = $array; |
|---|
| 383 | } |
|---|
| 384 | return $ret; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | // html_checkboxesの値をマージしてSQL検索用に変更する。 |
|---|
| 388 | /** |
|---|
| 389 | * @deprecated |
|---|
| 390 | */ |
|---|
| 391 | function sfSearchCheckBoxes($array) { |
|---|
| 392 | $max = max($array); |
|---|
| 393 | $ret = ''; |
|---|
| 394 | for ($i = 1; $i <= $max; $i++) { |
|---|
| 395 | $ret .= in_array($i, $array) ? '1' : '_'; |
|---|
| 396 | } |
|---|
| 397 | if (strlen($ret) != 0) { |
|---|
| 398 | $ret .= '%'; |
|---|
| 399 | } |
|---|
| 400 | return $ret; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | // 2進数形式の値をhtml_checkboxes対応の値に切り替える |
|---|
| 404 | /** |
|---|
| 405 | * @deprecated |
|---|
| 406 | */ |
|---|
| 407 | function sfSplitCheckBoxes($val) { |
|---|
| 408 | $arrRet = array(); |
|---|
| 409 | $len = strlen($val); |
|---|
| 410 | for ($i = 0; $i < $len; $i++) { |
|---|
| 411 | if (substr($val, $i, 1) == "1") { |
|---|
| 412 | $arrRet[] = ($i + 1); |
|---|
| 413 | } |
|---|
| 414 | } |
|---|
| 415 | return $arrRet; |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | // チェックボックスの値をマージ |
|---|
| 419 | /** |
|---|
| 420 | * @deprecated |
|---|
| 421 | */ |
|---|
| 422 | function sfMergeCBSearchValue($keyname, $max) { |
|---|
| 423 | $conv = ""; |
|---|
| 424 | $cnt = 1; |
|---|
| 425 | for ($cnt = 1; $cnt <= $max; $cnt++) { |
|---|
| 426 | if ($_POST[$keyname . $cnt] == "1") { |
|---|
| 427 | $conv.= "1"; |
|---|
| 428 | } else { |
|---|
| 429 | $conv.= '_'; |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | return $conv; |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | // チェックボックスの値を分解 |
|---|
| 436 | /** |
|---|
| 437 | * @deprecated |
|---|
| 438 | */ |
|---|
| 439 | function sfSplitCBValue($val, $keyname = "") { |
|---|
| 440 | $arr = array(); |
|---|
| 441 | $len = strlen($val); |
|---|
| 442 | $no = 1; |
|---|
| 443 | for ($cnt = 0; $cnt < $len; $cnt++) { |
|---|
| 444 | if ($keyname != "") { |
|---|
| 445 | $arr[$keyname . $no] = substr($val, $cnt, 1); |
|---|
| 446 | } else { |
|---|
| 447 | $arr[] = substr($val, $cnt, 1); |
|---|
| 448 | } |
|---|
| 449 | $no++; |
|---|
| 450 | } |
|---|
| 451 | return $arr; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | // キーと値をセットした配列を取得 |
|---|
| 455 | function sfArrKeyValue($arrList, $keyname, $valname, $len_max = "", $keysize = "") { |
|---|
| 456 | $arrRet = array(); |
|---|
| 457 | $max = count($arrList); |
|---|
| 458 | |
|---|
| 459 | if ($len_max != "" && $max > $len_max) { |
|---|
| 460 | $max = $len_max; |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | for ($cnt = 0; $cnt < $max; $cnt++) { |
|---|
| 464 | if ($keysize != "") { |
|---|
| 465 | $key = SC_Utils_Ex::sfCutString($arrList[$cnt][$keyname], $keysize); |
|---|
| 466 | } else { |
|---|
| 467 | $key = $arrList[$cnt][$keyname]; |
|---|
| 468 | } |
|---|
| 469 | $val = $arrList[$cnt][$valname]; |
|---|
| 470 | |
|---|
| 471 | if (!isset($arrRet[$key])) { |
|---|
| 472 | $arrRet[$key] = $val; |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | } |
|---|
| 476 | return $arrRet; |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | // キーと値をセットした配列を取得(値が複数の場合) |
|---|
| 480 | function sfArrKeyValues($arrList, $keyname, $valname, $len_max = "", $keysize = "", $connect = "") { |
|---|
| 481 | |
|---|
| 482 | $max = count($arrList); |
|---|
| 483 | |
|---|
| 484 | if ($len_max != "" && $max > $len_max) { |
|---|
| 485 | $max = $len_max; |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | for ($cnt = 0; $cnt < $max; $cnt++) { |
|---|
| 489 | if ($keysize != "") { |
|---|
| 490 | $key = SC_Utils_Ex::sfCutString($arrList[$cnt][$keyname], $keysize); |
|---|
| 491 | } else { |
|---|
| 492 | $key = $arrList[$cnt][$keyname]; |
|---|
| 493 | } |
|---|
| 494 | $val = $arrList[$cnt][$valname]; |
|---|
| 495 | |
|---|
| 496 | if ($connect != "") { |
|---|
| 497 | $arrRet[$key].= "$val".$connect; |
|---|
| 498 | } else { |
|---|
| 499 | $arrRet[$key][] = $val; |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | return $arrRet; |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | // 配列の値をカンマ区切りで返す。 |
|---|
| 506 | function sfGetCommaList($array, $space=true, $arrPop = array()) { |
|---|
| 507 | if (count($array) > 0) { |
|---|
| 508 | $line = ""; |
|---|
| 509 | foreach ($array as $val) { |
|---|
| 510 | if (!in_array($val, $arrPop)) { |
|---|
| 511 | if ($space) { |
|---|
| 512 | $line .= $val . ", "; |
|---|
| 513 | } else { |
|---|
| 514 | $line .= $val . ","; |
|---|
| 515 | } |
|---|
| 516 | } |
|---|
| 517 | } |
|---|
| 518 | if ($space) { |
|---|
| 519 | $line = ereg_replace(", $", "", $line); |
|---|
| 520 | } else { |
|---|
| 521 | $line = ereg_replace(",$", "", $line); |
|---|
| 522 | } |
|---|
| 523 | return $line; |
|---|
| 524 | } else { |
|---|
| 525 | return false; |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | /* 配列の要素をCSVフォーマットで出力する。*/ |
|---|
| 531 | function sfGetCSVList($array) { |
|---|
| 532 | $line = ""; |
|---|
| 533 | if (count($array) > 0) { |
|---|
| 534 | foreach ($array as $key => $val) { |
|---|
| 535 | $val = mb_convert_encoding($val, CHAR_CODE, CHAR_CODE); |
|---|
| 536 | $line .= "\"".$val."\","; |
|---|
| 537 | } |
|---|
| 538 | $line = ereg_replace(",$", "\r\n", $line); |
|---|
| 539 | } else { |
|---|
| 540 | return false; |
|---|
| 541 | } |
|---|
| 542 | return $line; |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | /*-----------------------------------------------------------------*/ |
|---|
| 546 | /* check_set_term |
|---|
| 547 | /* 年月日に別れた2つの期間の妥当性をチェックし、整合性と期間を返す |
|---|
| 548 | /* 引数 (開始年,開始月,開始日,終了年,終了月,終了日) |
|---|
| 549 | /* 戻値 array(1,2,3) |
|---|
| 550 | /* 1.開始年月日 (YYYY/MM/DD 000000) |
|---|
| 551 | /* 2.終了年月日 (YYYY/MM/DD 235959) |
|---|
| 552 | /* 3.エラー (0 = OK, 1 = NG) |
|---|
| 553 | /*-----------------------------------------------------------------*/ |
|---|
| 554 | function sfCheckSetTerm($start_year, $start_month, $start_day, $end_year, $end_month, $end_day) { |
|---|
| 555 | |
|---|
| 556 | // 期間指定 |
|---|
| 557 | $error = 0; |
|---|
| 558 | if ($start_month || $start_day || $start_year) { |
|---|
| 559 | if (! checkdate($start_month, $start_day , $start_year)) $error = 1; |
|---|
| 560 | } else { |
|---|
| 561 | $error = 1; |
|---|
| 562 | } |
|---|
| 563 | if ($end_month || $end_day || $end_year) { |
|---|
| 564 | if (! checkdate($end_month ,$end_day ,$end_year)) $error = 2; |
|---|
| 565 | } |
|---|
| 566 | if (! $error) { |
|---|
| 567 | $date1 = $start_year ."/".sprintf("%02d",$start_month) ."/".sprintf("%02d",$start_day) ." 000000"; |
|---|
| 568 | $date2 = $end_year ."/".sprintf("%02d",$end_month) ."/".sprintf("%02d",$end_day) ." 235959"; |
|---|
| 569 | if ($date1 > $date2) $error = 3; |
|---|
| 570 | } else { |
|---|
| 571 | $error = 1; |
|---|
| 572 | } |
|---|
| 573 | return array($date1, $date2, $error); |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | // エラー箇所の背景色を変更するためのfunction SC_Viewで読み込む |
|---|
| 577 | function sfSetErrorStyle() { |
|---|
| 578 | return 'style="background-color:'.ERR_COLOR.'"'; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | // 一致した値のキー名を取得 |
|---|
| 582 | function sfSearchKey($array, $word, $default) { |
|---|
| 583 | foreach ($array as $key => $val) { |
|---|
| 584 | if ($val == $word) { |
|---|
| 585 | return $key; |
|---|
| 586 | } |
|---|
| 587 | } |
|---|
| 588 | return $default; |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | function sfGetErrorColor($val) { |
|---|
| 592 | if ($val != "") { |
|---|
| 593 | return "background-color:" . ERR_COLOR; |
|---|
| 594 | } |
|---|
| 595 | return ""; |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | function sfGetEnabled($val) { |
|---|
| 599 | if (! $val) { |
|---|
| 600 | return " disabled=\"disabled\""; |
|---|
| 601 | } |
|---|
| 602 | return ""; |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | function sfGetChecked($param, $value) { |
|---|
| 606 | if ((string)$param === (string)$value) { |
|---|
| 607 | return "checked=\"checked\""; |
|---|
| 608 | } |
|---|
| 609 | return ""; |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | function sfTrim($str) { |
|---|
| 613 | $ret = mb_ereg_replace("^[ \n\r]*", "", $str); |
|---|
| 614 | $ret = mb_ereg_replace("[ \n\r]*$", "", $ret); |
|---|
| 615 | return $ret; |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | /** |
|---|
| 619 | * 税金額を返す |
|---|
| 620 | * |
|---|
| 621 | * ・店舗基本情報に基づいた計算は SC_Helper_DB::sfTax() を使用する |
|---|
| 622 | * |
|---|
| 623 | * @param integer $price 計算対象の金額 |
|---|
| 624 | * @param integer $tax 税率(%単位) |
|---|
| 625 | * XXX integer のみか不明 |
|---|
| 626 | * @param integer $tax_rule 端数処理 |
|---|
| 627 | * @return integer 税金額 |
|---|
| 628 | */ |
|---|
| 629 | function sfTax($price, $tax, $tax_rule) { |
|---|
| 630 | $real_tax = $tax / 100; |
|---|
| 631 | $ret = $price * $real_tax; |
|---|
| 632 | switch ($tax_rule) { |
|---|
| 633 | // 四捨五入 |
|---|
| 634 | case 1: |
|---|
| 635 | $ret = round($ret); |
|---|
| 636 | break; |
|---|
| 637 | // 切り捨て |
|---|
| 638 | case 2: |
|---|
| 639 | $ret = floor($ret); |
|---|
| 640 | break; |
|---|
| 641 | // 切り上げ |
|---|
| 642 | case 3: |
|---|
| 643 | $ret = ceil($ret); |
|---|
| 644 | break; |
|---|
| 645 | // デフォルト:切り上げ |
|---|
| 646 | default: |
|---|
| 647 | $ret = ceil($ret); |
|---|
| 648 | break; |
|---|
| 649 | } |
|---|
| 650 | return $ret; |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | /** |
|---|
| 654 | * 税金付与した金額を返す |
|---|
| 655 | * |
|---|
| 656 | * ・店舗基本情報に基づいた計算は SC_Helper_DB::sfTax() を使用する |
|---|
| 657 | * |
|---|
| 658 | * @param integer $price 計算対象の金額 |
|---|
| 659 | * @param integer $tax 税率(%単位) |
|---|
| 660 | * XXX integer のみか不明 |
|---|
| 661 | * @param integer $tax_rule 端数処理 |
|---|
| 662 | * @return integer 税金付与した金額 |
|---|
| 663 | */ |
|---|
| 664 | function sfCalcIncTax($price, $tax, $tax_rule) { |
|---|
| 665 | return $price + SC_Utils_Ex::sfTax($price, $tax, $tax_rule); |
|---|
| 666 | } |
|---|
| 667 | |
|---|
| 668 | // 桁数を指定して四捨五入 |
|---|
| 669 | function sfRound($value, $pow = 0) { |
|---|
| 670 | $adjust = pow(10 ,$pow-1); |
|---|
| 671 | |
|---|
| 672 | // 整数且つ0出なければ桁数指定を行う |
|---|
| 673 | if (SC_Utils_Ex::sfIsInt($adjust) and $pow > 1) { |
|---|
| 674 | $ret = (round($value * $adjust)/$adjust); |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | $ret = round($ret); |
|---|
| 678 | |
|---|
| 679 | return $ret; |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | /* ポイント付与 */ |
|---|
| 683 | function sfPrePoint($price, $point_rate, $rule = POINT_RULE, $product_id = "") { |
|---|
| 684 | $real_point = $point_rate / 100; |
|---|
| 685 | $ret = $price * $real_point; |
|---|
| 686 | switch ($rule) { |
|---|
| 687 | // 四捨五入 |
|---|
| 688 | case 1: |
|---|
| 689 | $ret = round($ret); |
|---|
| 690 | break; |
|---|
| 691 | // 切り捨て |
|---|
| 692 | case 2: |
|---|
| 693 | $ret = floor($ret); |
|---|
| 694 | break; |
|---|
| 695 | // 切り上げ |
|---|
| 696 | case 3: |
|---|
| 697 | $ret = ceil($ret); |
|---|
| 698 | break; |
|---|
| 699 | // デフォルト:切り上げ |
|---|
| 700 | default: |
|---|
| 701 | $ret = ceil($ret); |
|---|
| 702 | break; |
|---|
| 703 | } |
|---|
| 704 | return $ret; |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | /* 規格分類の件数取得 */ |
|---|
| 708 | function sfGetClassCatCount() { |
|---|
| 709 | $sql = "select count(dtb_class.class_id) as count, dtb_class.class_id "; |
|---|
| 710 | $sql.= "from dtb_class inner join dtb_classcategory on dtb_class.class_id = dtb_classcategory.class_id "; |
|---|
| 711 | $sql.= "where dtb_class.del_flg = 0 AND dtb_classcategory.del_flg = 0 "; |
|---|
| 712 | $sql.= "group by dtb_class.class_id, dtb_class.name"; |
|---|
| 713 | $objQuery = new SC_Query_Ex(); |
|---|
| 714 | $arrList = $objQuery->getAll($sql); |
|---|
| 715 | // キーと値をセットした配列を取得 |
|---|
| 716 | $arrRet = SC_Utils_Ex::sfArrKeyValue($arrList, 'class_id', 'count'); |
|---|
| 717 | |
|---|
| 718 | return $arrRet; |
|---|
| 719 | } |
|---|
| 720 | |
|---|
| 721 | function sfGetProductClassId($product_id, $classcategory_id1, $classcategory_id2) { |
|---|
| 722 | $where = "product_id = ?"; |
|---|
| 723 | $objQuery = new SC_Query_Ex(); |
|---|
| 724 | $ret = $objQuery->get("product_class_id", "dtb_products_class", $where, Array($product_id)); |
|---|
| 725 | return $ret; |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | /* 文末の「/」をなくす */ |
|---|
| 729 | function sfTrimURL($url) { |
|---|
| 730 | $ret = ereg_replace("[/]+$", "", $url); |
|---|
| 731 | return $ret; |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| 734 | /* DBから取り出した日付の文字列を調整する。*/ |
|---|
| 735 | function sfDispDBDate($dbdate, $time = true) { |
|---|
| 736 | list($y, $m, $d, $H, $M) = preg_split("/[- :]/", $dbdate); |
|---|
| 737 | |
|---|
| 738 | if (strlen($y) > 0 && strlen($m) > 0 && strlen($d) > 0) { |
|---|
| 739 | if ($time) { |
|---|
| 740 | $str = sprintf("%04d/%02d/%02d %02d:%02d", $y, $m, $d, $H, $M); |
|---|
| 741 | } else { |
|---|
| 742 | $str = sprintf("%04d/%02d/%02d", $y, $m, $d, $H, $M); |
|---|
| 743 | } |
|---|
| 744 | } else { |
|---|
| 745 | $str = ""; |
|---|
| 746 | } |
|---|
| 747 | return $str; |
|---|
| 748 | } |
|---|
| 749 | |
|---|
| 750 | /* 配列をキー名ごとの配列に変更する */ |
|---|
| 751 | function sfSwapArray($array, $isColumnName = true) { |
|---|
| 752 | $arrRet = array(); |
|---|
| 753 | foreach ($array as $key1 => $arr1) { |
|---|
| 754 | $index = 0; |
|---|
| 755 | foreach ($arr1 as $key2 => $val) { |
|---|
| 756 | if ($isColumnName) { |
|---|
| 757 | $arrRet[$key2][$key1] = $val; |
|---|
| 758 | } else { |
|---|
| 759 | $arrRet[$index++][$key1] = $val; |
|---|
| 760 | } |
|---|
| 761 | } |
|---|
| 762 | } |
|---|
| 763 | return $arrRet; |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | /** |
|---|
| 767 | * 連想配列から新たな配列を生成して返す. |
|---|
| 768 | * |
|---|
| 769 | * $requires が指定された場合, $requires に含まれるキーの値のみを返す. |
|---|
| 770 | * |
|---|
| 771 | * @param array 連想配列 |
|---|
| 772 | * @param array 必須キーの配列 |
|---|
| 773 | * @return array 連想配列の値のみの配列 |
|---|
| 774 | */ |
|---|
| 775 | function getHash2Array($hash, $requires = array()) { |
|---|
| 776 | $array = array(); |
|---|
| 777 | $i = 0; |
|---|
| 778 | foreach ($hash as $key => $val) { |
|---|
| 779 | if (!empty($requires)) { |
|---|
| 780 | if (in_array($key, $requires)) { |
|---|
| 781 | $array[$i] = $val; |
|---|
| 782 | $i++; |
|---|
| 783 | } |
|---|
| 784 | } else { |
|---|
| 785 | $array[$i] = $val; |
|---|
| 786 | $i++; |
|---|
| 787 | } |
|---|
| 788 | } |
|---|
| 789 | return $array; |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | /* かけ算をする(Smarty用) */ |
|---|
| 793 | function sfMultiply($num1, $num2) { |
|---|
| 794 | return $num1 * $num2; |
|---|
| 795 | } |
|---|
| 796 | |
|---|
| 797 | /** |
|---|
| 798 | * 加算ポイントの計算 |
|---|
| 799 | * |
|---|
| 800 | * ・店舗基本情報に基づいた計算は SC_Helper_DB::sfGetAddPoint() を使用する |
|---|
| 801 | * |
|---|
| 802 | * @param integer $totalpoint |
|---|
| 803 | * @param integer $use_point |
|---|
| 804 | * @param integer $point_rate |
|---|
| 805 | * @return integer 加算ポイント |
|---|
| 806 | */ |
|---|
| 807 | function sfGetAddPoint($totalpoint, $use_point, $point_rate) { |
|---|
| 808 | // 購入商品の合計ポイントから利用したポイントのポイント換算価値を引く方式 |
|---|
| 809 | $add_point = $totalpoint - intval($use_point * ($point_rate / 100)); |
|---|
| 810 | |
|---|
| 811 | if ($add_point < 0) { |
|---|
| 812 | $add_point = '0'; |
|---|
| 813 | } |
|---|
| 814 | return $add_point; |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | /* 一意かつ予測されにくいID */ |
|---|
| 818 | function sfGetUniqRandomId($head = "") { |
|---|
| 819 | // 予測されないようにランダム文字列を付与する。 |
|---|
| 820 | $random = GC_Utils_Ex::gfMakePassword(8); |
|---|
| 821 | // 同一ホスト内で一意なIDを生成 |
|---|
| 822 | $id = uniqid($head); |
|---|
| 823 | return $id . $random; |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | // 二回以上繰り返されているスラッシュ[/]を一つに変換する。 |
|---|
| 827 | function sfRmDupSlash($istr) { |
|---|
| 828 | if (ereg("^http://", $istr)) { |
|---|
| 829 | $str = substr($istr, 7); |
|---|
| 830 | $head = "http://"; |
|---|
| 831 | } else if (ereg("^https://", $istr)) { |
|---|
| 832 | $str = substr($istr, 8); |
|---|
| 833 | $head = "https://"; |
|---|
| 834 | } else { |
|---|
| 835 | $str = $istr; |
|---|
| 836 | } |
|---|
| 837 | $str = ereg_replace("[/]+", "/", $str); |
|---|
| 838 | $ret = $head . $str; |
|---|
| 839 | return $ret; |
|---|
| 840 | } |
|---|
| 841 | |
|---|
| 842 | /** |
|---|
| 843 | * テキストファイルの文字エンコーディングを変換する. |
|---|
| 844 | * |
|---|
| 845 | * $filepath に存在するテキストファイルの文字エンコーディングを変換する. |
|---|
| 846 | * 変換前の文字エンコーディングは, mb_detect_order で設定した順序で自動検出する. |
|---|
| 847 | * 変換後は, 変換前のファイル名に「enc_」というプレフィクスを付与し, |
|---|
| 848 | * $out_dir で指定したディレクトリへ出力する |
|---|
| 849 | * |
|---|
| 850 | * TODO $filepath のファイルがバイナリだった場合の扱い |
|---|
| 851 | * TODO fwrite などでのエラーハンドリング |
|---|
| 852 | * |
|---|
| 853 | * @access public |
|---|
| 854 | * @param string $filepath 変換するテキストファイルのパス |
|---|
| 855 | * @param string $enc_type 変換後のファイルエンコーディングの種類を表す文字列 |
|---|
| 856 | * @param string $out_dir 変換後のファイルを出力するディレクトリを表す文字列 |
|---|
| 857 | * @return string 変換後のテキストファイルのパス |
|---|
| 858 | */ |
|---|
| 859 | function sfEncodeFile($filepath, $enc_type, $out_dir) { |
|---|
| 860 | $ifp = fopen($filepath, 'r'); |
|---|
| 861 | |
|---|
| 862 | // 正常にファイルオープンした場合 |
|---|
| 863 | if ($ifp !== false) { |
|---|
| 864 | |
|---|
| 865 | $basename = basename($filepath); |
|---|
| 866 | $outpath = $out_dir . "enc_" . $basename; |
|---|
| 867 | |
|---|
| 868 | $ofp = fopen($outpath, "w+"); |
|---|
| 869 | |
|---|
| 870 | while (!feof($ifp)) { |
|---|
| 871 | $line = fgets($ifp); |
|---|
| 872 | $line = mb_convert_encoding($line, $enc_type, 'auto'); |
|---|
| 873 | fwrite($ofp, $line); |
|---|
| 874 | } |
|---|
| 875 | |
|---|
| 876 | fclose($ofp); |
|---|
| 877 | fclose($ifp); |
|---|
| 878 | } |
|---|
| 879 | // ファイルが開けなかった場合はエラーページを表示 |
|---|
| 880 | else { |
|---|
| 881 | SC_Utils_Ex::sfDispError(''); |
|---|
| 882 | exit; |
|---|
| 883 | } |
|---|
| 884 | return $outpath; |
|---|
| 885 | } |
|---|
| 886 | |
|---|
| 887 | function sfCutString($str, $len, $byte = true, $commadisp = true) { |
|---|
| 888 | if ($byte) { |
|---|
| 889 | if (strlen($str) > ($len + 2)) { |
|---|
| 890 | $ret =substr($str, 0, $len); |
|---|
| 891 | $cut = substr($str, $len); |
|---|
| 892 | } else { |
|---|
| 893 | $ret = $str; |
|---|
| 894 | $commadisp = false; |
|---|
| 895 | } |
|---|
| 896 | } else { |
|---|
| 897 | if (mb_strlen($str) > ($len + 1)) { |
|---|
| 898 | $ret = mb_substr($str, 0, $len); |
|---|
| 899 | $cut = mb_substr($str, $len); |
|---|
| 900 | } else { |
|---|
| 901 | $ret = $str; |
|---|
| 902 | $commadisp = false; |
|---|
| 903 | } |
|---|
| 904 | } |
|---|
| 905 | |
|---|
| 906 | // 絵文字タグの途中で分断されないようにする。 |
|---|
| 907 | if (isset($cut)) { |
|---|
| 908 | // 分割位置より前の最後の [ 以降を取得する。 |
|---|
| 909 | $head = strrchr($ret, '['); |
|---|
| 910 | |
|---|
| 911 | // 分割位置より後の最初の ] 以前を取得する。 |
|---|
| 912 | $tail_pos = strpos($cut, ']'); |
|---|
| 913 | if ($tail_pos !== false) { |
|---|
| 914 | $tail = substr($cut, 0, $tail_pos + 1); |
|---|
| 915 | } |
|---|
| 916 | |
|---|
| 917 | // 分割位置より前に [、後に ] が見つかった場合は、[ から ] までを |
|---|
| 918 | // 接続して絵文字タグ1個分になるかどうかをチェックする。 |
|---|
| 919 | if ($head !== false && $tail_pos !== false) { |
|---|
| 920 | $subject = $head . $tail; |
|---|
| 921 | if (preg_match('/^\[emoji:e?\d+\]$/', $subject)) { |
|---|
| 922 | // 絵文字タグが見つかったので削除する。 |
|---|
| 923 | $ret = substr($ret, 0, -strlen($head)); |
|---|
| 924 | } |
|---|
| 925 | } |
|---|
| 926 | } |
|---|
| 927 | |
|---|
| 928 | if ($commadisp) { |
|---|
| 929 | $ret = $ret . "..."; |
|---|
| 930 | } |
|---|
| 931 | return $ret; |
|---|
| 932 | } |
|---|
| 933 | |
|---|
| 934 | // 年、月、締め日から、先月の締め日+1、今月の締め日を求める。 |
|---|
| 935 | function sfTermMonth($year, $month, $close_day) { |
|---|
| 936 | $end_year = $year; |
|---|
| 937 | $end_month = $month; |
|---|
| 938 | |
|---|
| 939 | // 開始月が終了月と同じか否か |
|---|
| 940 | $same_month = false; |
|---|
| 941 | |
|---|
| 942 | // 該当月の末日を求める。 |
|---|
| 943 | $end_last_day = date('d', mktime(0, 0, 0, $month + 1, 0, $year)); |
|---|
| 944 | |
|---|
| 945 | // 月の末日が締め日より少ない場合 |
|---|
| 946 | if ($end_last_day < $close_day) { |
|---|
| 947 | // 締め日を月末日に合わせる |
|---|
| 948 | $end_day = $end_last_day; |
|---|
| 949 | } else { |
|---|
| 950 | $end_day = $close_day; |
|---|
| 951 | } |
|---|
| 952 | |
|---|
| 953 | // 前月の取得 |
|---|
| 954 | $tmp_year = date('Y', mktime(0, 0, 0, $month, 0, $year)); |
|---|
| 955 | $tmp_month = date('m', mktime(0, 0, 0, $month, 0, $year)); |
|---|
| 956 | // 前月の末日を求める。 |
|---|
| 957 | $start_last_day = date('d', mktime(0, 0, 0, $month, 0, $year)); |
|---|
| 958 | |
|---|
| 959 | // 前月の末日が締め日より少ない場合 |
|---|
| 960 | if ($start_last_day < $close_day) { |
|---|
| 961 | // 月末日に合わせる |
|---|
| 962 | $tmp_day = $start_last_day; |
|---|
| 963 | } else { |
|---|
| 964 | $tmp_day = $close_day; |
|---|
| 965 | } |
|---|
| 966 | |
|---|
| 967 | // 先月の末日の翌日を取得する |
|---|
| 968 | $start_year = date('Y', mktime(0, 0, 0, $tmp_month, $tmp_day + 1, $tmp_year)); |
|---|
| 969 | $start_month = date('m', mktime(0, 0, 0, $tmp_month, $tmp_day + 1, $tmp_year)); |
|---|
| 970 | $start_day = date('d', mktime(0, 0, 0, $tmp_month, $tmp_day + 1, $tmp_year)); |
|---|
| 971 | |
|---|
| 972 | // 日付の作成 |
|---|
| 973 | $start_date = sprintf("%d/%d/%d 00:00:00", $start_year, $start_month, $start_day); |
|---|
| 974 | $end_date = sprintf("%d/%d/%d 23:59:59", $end_year, $end_month, $end_day); |
|---|
| 975 | |
|---|
| 976 | return array($start_date, $end_date); |
|---|
| 977 | } |
|---|
| 978 | |
|---|
| 979 | // 再帰的に多段配列を検索して一次元配列(Hidden引渡し用配列)に変換する。 |
|---|
| 980 | function sfMakeHiddenArray($arrSrc, $arrDst = array(), $parent_key = "") { |
|---|
| 981 | if (is_array($arrSrc)) { |
|---|
| 982 | foreach ($arrSrc as $key => $val) { |
|---|
| 983 | if ($parent_key != "") { |
|---|
| 984 | $keyname = $parent_key . "[". $key . "]"; |
|---|
| 985 | } else { |
|---|
| 986 | $keyname = $key; |
|---|
| 987 | } |
|---|
| 988 | if (is_array($val)) { |
|---|
| 989 | $arrDst = SC_Utils_Ex::sfMakeHiddenArray($val, $arrDst, $keyname); |
|---|
| 990 | } else { |
|---|
| 991 | $arrDst[$keyname] = $val; |
|---|
| 992 | } |
|---|
| 993 | } |
|---|
| 994 | } |
|---|
| 995 | return $arrDst; |
|---|
| 996 | } |
|---|
| 997 | |
|---|
| 998 | // DB取得日時をタイムに変換 |
|---|
| 999 | function sfDBDatetoTime($db_date) { |
|---|
| 1000 | $date = ereg_replace("\..*$","",$db_date); |
|---|
| 1001 | $time = strtotime($date); |
|---|
| 1002 | return $time; |
|---|
| 1003 | } |
|---|
| 1004 | |
|---|
| 1005 | // PHPのmb_convert_encoding関数をSmartyでも使えるようにする |
|---|
| 1006 | function sfMbConvertEncoding($str, $encode = 'CHAR_CODE') { |
|---|
| 1007 | return mb_convert_encoding($str, $encode); |
|---|
| 1008 | } |
|---|
| 1009 | |
|---|
| 1010 | // 2つの配列を用いて連想配列を作成する |
|---|
| 1011 | function sfArrCombine($arrKeys, $arrValues) { |
|---|
| 1012 | |
|---|
| 1013 | if(count($arrKeys) <= 0 and count($arrValues) <= 0) return array(); |
|---|
| 1014 | |
|---|
| 1015 | $keys = array_values($arrKeys); |
|---|
| 1016 | $vals = array_values($arrValues); |
|---|
| 1017 | |
|---|
| 1018 | $max = max( count($keys), count($vals)); |
|---|
| 1019 | $combine_ary = array(); |
|---|
| 1020 | for ($i=0; $i<$max; $i++) { |
|---|
| 1021 | $combine_ary[$keys[$i]] = $vals[$i]; |
|---|
| 1022 | } |
|---|
| 1023 | if(is_array($combine_ary)) return $combine_ary; |
|---|
| 1024 | |
|---|
| 1025 | return false; |
|---|
| 1026 | } |
|---|
| 1027 | |
|---|
| 1028 | /* 階層構造のテーブルから与えられたIDの兄弟を取得する */ |
|---|
| 1029 | function sfGetBrothersArray($arrData, $pid_name, $id_name, $arrPID) { |
|---|
| 1030 | $max = count($arrData); |
|---|
| 1031 | |
|---|
| 1032 | $arrBrothers = array(); |
|---|
| 1033 | foreach ($arrPID as $id) { |
|---|
| 1034 | // 親IDを検索する |
|---|
| 1035 | for ($i = 0; $i < $max; $i++) { |
|---|
| 1036 | if ($arrData[$i][$id_name] == $id) { |
|---|
| 1037 | $parent = $arrData[$i][$pid_name]; |
|---|
| 1038 | break; |
|---|
| 1039 | } |
|---|
| 1040 | } |
|---|
| 1041 | // 兄弟IDを検索する |
|---|
| 1042 | for ($i = 0; $i < $max; $i++) { |
|---|
| 1043 | if ($arrData[$i][$pid_name] == $parent) { |
|---|
| 1044 | $arrBrothers[] = $arrData[$i][$id_name]; |
|---|
| 1045 | } |
|---|
| 1046 | } |
|---|
| 1047 | } |
|---|
| 1048 | return $arrBrothers; |
|---|
| 1049 | } |
|---|
| 1050 | |
|---|
| 1051 | /* 階層構造のテーブルから与えられたIDの直属の子を取得する */ |
|---|
| 1052 | function sfGetUnderChildrenArray($arrData, $pid_name, $id_name, $parent) { |
|---|
| 1053 | $max = count($arrData); |
|---|
| 1054 | |
|---|
| 1055 | $arrChildren = array(); |
|---|
| 1056 | // 子IDを検索する |
|---|
| 1057 | for ($i = 0; $i < $max; $i++) { |
|---|
| 1058 | if ($arrData[$i][$pid_name] == $parent) { |
|---|
| 1059 | $arrChildren[] = $arrData[$i][$id_name]; |
|---|
| 1060 | } |
|---|
| 1061 | } |
|---|
| 1062 | return $arrChildren; |
|---|
| 1063 | } |
|---|
| 1064 | |
|---|
| 1065 | /** |
|---|
| 1066 | * SQLシングルクォート対応 |
|---|
| 1067 | * @deprecated SC_Query::quote() を使用すること |
|---|
| 1068 | */ |
|---|
| 1069 | function sfQuoteSmart($in) { |
|---|
| 1070 | |
|---|
| 1071 | if (is_int($in) || is_double($in)) { |
|---|
| 1072 | return $in; |
|---|
| 1073 | } elseif (is_bool($in)) { |
|---|
| 1074 | return $in ? 1 : 0; |
|---|
| 1075 | } elseif (is_null($in)) { |
|---|
| 1076 | return 'NULL'; |
|---|
| 1077 | } else { |
|---|
| 1078 | return "'" . str_replace("'", "''", $in) . "'"; |
|---|
| 1079 | } |
|---|
| 1080 | } |
|---|
| 1081 | |
|---|
| 1082 | // ディレクトリを再帰的に生成する |
|---|
| 1083 | function sfMakeDir($path) { |
|---|
| 1084 | static $count = 0; |
|---|
| 1085 | $count++; // 無限ループ回避 |
|---|
| 1086 | $dir = dirname($path); |
|---|
| 1087 | if (ereg("^[/]$", $dir) || ereg("^[A-Z]:[\\]$", $dir) || $count > 256) { |
|---|
| 1088 | // ルートディレクトリで終了 |
|---|
| 1089 | return; |
|---|
| 1090 | } else { |
|---|
| 1091 | if (is_writable(dirname($dir))) { |
|---|
| 1092 | if (!file_exists($dir)) { |
|---|
| 1093 | mkdir($dir); |
|---|
| 1094 | GC_Utils_Ex::gfPrintLog("mkdir $dir"); |
|---|
| 1095 | } |
|---|
| 1096 | } else { |
|---|
| 1097 | SC_Utils_Ex::sfMakeDir($dir); |
|---|
| 1098 | if (is_writable(dirname($dir))) { |
|---|
| 1099 | if (!file_exists($dir)) { |
|---|
| 1100 | mkdir($dir); |
|---|
| 1101 | GC_Utils_Ex::gfPrintLog("mkdir $dir"); |
|---|
| 1102 | } |
|---|
| 1103 | } |
|---|
| 1104 | } |
|---|
| 1105 | } |
|---|
| 1106 | return; |
|---|
| 1107 | } |
|---|
| 1108 | |
|---|
| 1109 | // ディレクトリ以下のファイルを再帰的にコピー |
|---|
| 1110 | function sfCopyDir($src, $des, $mess = "", $override = false) { |
|---|
| 1111 | if (!is_dir($src)) { |
|---|
| 1112 | return false; |
|---|
| 1113 | } |
|---|
| 1114 | |
|---|
| 1115 | $oldmask = umask(0); |
|---|
| 1116 | $mod= stat($src); |
|---|
| 1117 | |
|---|
| 1118 | // ディレクトリがなければ作成する |
|---|
| 1119 | if (!file_exists($des)) { |
|---|
| 1120 | if (!mkdir($des, $mod[2])) { |
|---|
| 1121 | echo 'path:' . $des; |
|---|
| 1122 | } |
|---|
| 1123 | } |
|---|
| 1124 | |
|---|
| 1125 | $fileArray=glob($src."*"); |
|---|
| 1126 | if (is_array($fileArray)) { |
|---|
| 1127 | foreach ($fileArray as $key => $data_) { |
|---|
| 1128 | // CVS管理ファイルはコピーしない |
|---|
| 1129 | if (ereg("/CVS/Entries", $data_)) { |
|---|
| 1130 | break; |
|---|
| 1131 | } |
|---|
| 1132 | if (ereg("/CVS/Repository", $data_)) { |
|---|
| 1133 | break; |
|---|
| 1134 | } |
|---|
| 1135 | if (ereg("/CVS/Root", $data_)) { |
|---|
| 1136 | break; |
|---|
| 1137 | } |
|---|
| 1138 | |
|---|
| 1139 | mb_ereg("^(.*[\/])(.*)",$data_, $matches); |
|---|
| 1140 | $data=$matches[2]; |
|---|
| 1141 | if (is_dir($data_)) { |
|---|
| 1142 | $mess = SC_Utils_Ex::sfCopyDir($data_.'/', $des.$data.'/', $mess); |
|---|
| 1143 | } else { |
|---|
| 1144 | if (!$override && file_exists($des.$data)) { |
|---|
| 1145 | $mess.= $des.$data . ":ファイルが存在します\n"; |
|---|
| 1146 | } else { |
|---|
| 1147 | if (@copy($data_, $des.$data)) { |
|---|
| 1148 | $mess.= $des.$data . ":コピー成功\n"; |
|---|
| 1149 | } else { |
|---|
| 1150 | $mess.= $des.$data . ":コピー失敗\n"; |
|---|
| 1151 | } |
|---|
| 1152 | } |
|---|
| 1153 | $mod=stat($data_); |
|---|
| 1154 | } |
|---|
| 1155 | } |
|---|
| 1156 | } |
|---|
| 1157 | umask($oldmask); |
|---|
| 1158 | return $mess; |
|---|
| 1159 | } |
|---|
| 1160 | |
|---|
| 1161 | // 指定したフォルダ内のファイルを全て削除する |
|---|
| 1162 | function sfDelFile($dir) { |
|---|
| 1163 | if (file_exists($dir)) { |
|---|
| 1164 | $dh = opendir($dir); |
|---|
| 1165 | // フォルダ内のファイルを削除 |
|---|
| 1166 | while ($file = readdir($dh)) { |
|---|
| 1167 | if ($file == "." or $file == "..") continue; |
|---|
| 1168 | $del_file = $dir . "/" . $file; |
|---|
| 1169 | if (is_file($del_file)) { |
|---|
| 1170 | $ret = unlink($dir . "/" . $file); |
|---|
| 1171 | }else if (is_dir($del_file)) { |
|---|
| 1172 | $ret = SC_Utils_Ex::sfDelFile($del_file); |
|---|
| 1173 | } |
|---|
| 1174 | |
|---|
| 1175 | if (!$ret) { |
|---|
| 1176 | return $ret; |
|---|
| 1177 | } |
|---|
| 1178 | } |
|---|
| 1179 | |
|---|
| 1180 | // 閉じる |
|---|
| 1181 | closedir($dh); |
|---|
| 1182 | |
|---|
| 1183 | // フォルダを削除 |
|---|
| 1184 | return rmdir($dir); |
|---|
| 1185 | } |
|---|
| 1186 | } |
|---|
| 1187 | |
|---|
| 1188 | /* |
|---|
| 1189 | * 関数名:sfWriteFile |
|---|
| 1190 | * 引数1 :書き込むデータ |
|---|
| 1191 | * 引数2 :ファイルパス |
|---|
| 1192 | * 引数3 :書き込みタイプ |
|---|
| 1193 | * 引数4 :パーミッション |
|---|
| 1194 | * 戻り値:結果フラグ 成功なら true 失敗なら false |
|---|
| 1195 | * 説明 :ファイル書き出し |
|---|
| 1196 | */ |
|---|
| 1197 | function sfWriteFile($str, $path, $type, $permission = "") { |
|---|
| 1198 | //ファイルを開く |
|---|
| 1199 | if (!($file = fopen ($path, $type))) { |
|---|
| 1200 | return false; |
|---|
| 1201 | } |
|---|
| 1202 | |
|---|
| 1203 | //ファイルロック |
|---|
| 1204 | flock ($file, LOCK_EX); |
|---|
| 1205 | //ファイルの書き込み |
|---|
| 1206 | fputs ($file, $str); |
|---|
| 1207 | //ファイルロックの解除 |
|---|
| 1208 | flock ($file, LOCK_UN); |
|---|
| 1209 | //ファイルを閉じる |
|---|
| 1210 | fclose ($file); |
|---|
| 1211 | // 権限を指定 |
|---|
| 1212 | if ($permission != "") { |
|---|
| 1213 | chmod($path, $permission); |
|---|
| 1214 | } |
|---|
| 1215 | |
|---|
| 1216 | return true; |
|---|
| 1217 | } |
|---|
| 1218 | |
|---|
| 1219 | /** |
|---|
| 1220 | * ブラウザに強制的に送出する |
|---|
| 1221 | * |
|---|
| 1222 | * @param boolean|string $output 半角スペース256文字+改行を出力するか。または、送信する文字列を指定。 |
|---|
| 1223 | * @return void |
|---|
| 1224 | */ |
|---|
| 1225 | function sfFlush($output = false, $sleep = 0) { |
|---|
| 1226 | // 出力をバッファリングしない(==日本語自動変換もしない) |
|---|
| 1227 | while (@ob_end_flush()); |
|---|
| 1228 | |
|---|
| 1229 | if ($output === true) { |
|---|
| 1230 | // IEのために半角スペース256文字+改行を出力 |
|---|
| 1231 | //echo str_repeat(' ', 256) . "\n"; |
|---|
| 1232 | echo str_pad('', 256) . "\n"; |
|---|
| 1233 | } else if ($output !== false) { |
|---|
| 1234 | echo $output; |
|---|
| 1235 | } |
|---|
| 1236 | |
|---|
| 1237 | // 出力をフラッシュする |
|---|
| 1238 | flush(); |
|---|
| 1239 | |
|---|
| 1240 | ob_start(); |
|---|
| 1241 | |
|---|
| 1242 | // 時間のかかる処理 |
|---|
| 1243 | sleep($sleep); |
|---|
| 1244 | } |
|---|
| 1245 | |
|---|
| 1246 | // @versionの記載があるファイルからバージョンを取得する。 |
|---|
| 1247 | function sfGetFileVersion($path) { |
|---|
| 1248 | if (file_exists($path)) { |
|---|
| 1249 | $src_fp = fopen($path, 'rb'); |
|---|
| 1250 | if ($src_fp) { |
|---|
| 1251 | while (!feof($src_fp)) { |
|---|
| 1252 | $line = fgets($src_fp); |
|---|
| 1253 | if (ereg("@version", $line)) { |
|---|
| 1254 | $arrLine = explode(" ", $line); |
|---|
| 1255 | $version = $arrLine[5]; |
|---|
| 1256 | } |
|---|
| 1257 | } |
|---|
| 1258 | fclose($src_fp); |
|---|
| 1259 | } |
|---|
| 1260 | } |
|---|
| 1261 | return $version; |
|---|
| 1262 | } |
|---|
| 1263 | |
|---|
| 1264 | /** |
|---|
| 1265 | * $array の要素を $arrConvList で指定した方式で mb_convert_kana を適用する. |
|---|
| 1266 | * |
|---|
| 1267 | * @param array $array 変換する文字列の配列 |
|---|
| 1268 | * @param array $arrConvList mb_convert_kana の適用ルール |
|---|
| 1269 | * @return array 変換後の配列 |
|---|
| 1270 | * @see mb_convert_kana |
|---|
| 1271 | */ |
|---|
| 1272 | function mbConvertKanaWithArray($array, $arrConvList) { |
|---|
| 1273 | foreach ($arrConvList as $key => $val) { |
|---|
| 1274 | if (isset($array[$key])) { |
|---|
| 1275 | $array[$key] = mb_convert_kana($array[$key] ,$val); |
|---|
| 1276 | } |
|---|
| 1277 | } |
|---|
| 1278 | return $array; |
|---|
| 1279 | } |
|---|
| 1280 | |
|---|
| 1281 | /** |
|---|
| 1282 | * 配列の添字が未定義の場合は空文字を代入して定義する. |
|---|
| 1283 | * |
|---|
| 1284 | * @param array $array 添字をチェックする配列 |
|---|
| 1285 | * @param array $defineIndexes チェックする添字 |
|---|
| 1286 | * @return array 添字を定義した配列 |
|---|
| 1287 | */ |
|---|
| 1288 | function arrayDefineIndexes($array, $defineIndexes) { |
|---|
| 1289 | foreach ($defineIndexes as $key) { |
|---|
| 1290 | if (!isset($array[$key])) $array[$key] = ""; |
|---|
| 1291 | } |
|---|
| 1292 | return $array; |
|---|
| 1293 | } |
|---|
| 1294 | |
|---|
| 1295 | /** |
|---|
| 1296 | * $arrSrc のうち、キーが $arrKey に含まれるものを返す |
|---|
| 1297 | * |
|---|
| 1298 | * $arrSrc に含まない要素は返されない。 |
|---|
| 1299 | * |
|---|
| 1300 | * @param array $arrSrc |
|---|
| 1301 | * @param array $arrKey |
|---|
| 1302 | * @return array |
|---|
| 1303 | */ |
|---|
| 1304 | function sfArrayIntersectKeys($arrSrc, $arrKey) { |
|---|
| 1305 | $arrRet = array(); |
|---|
| 1306 | foreach ($arrKey as $key) { |
|---|
| 1307 | if (isset($arrSrc[$key])) $arrRet[$key] = $arrSrc[$key]; |
|---|
| 1308 | } |
|---|
| 1309 | return $arrRet; |
|---|
| 1310 | } |
|---|
| 1311 | |
|---|
| 1312 | /** |
|---|
| 1313 | * XML宣言を出力する. |
|---|
| 1314 | * |
|---|
| 1315 | * XML宣言があると問題が発生する UA は出力しない. |
|---|
| 1316 | * |
|---|
| 1317 | * @return string XML宣言の文字列 |
|---|
| 1318 | */ |
|---|
| 1319 | function printXMLDeclaration() { |
|---|
| 1320 | $ua = $_SERVER['HTTP_USER_AGENT']; |
|---|
| 1321 | if (!preg_match("/MSIE/", $ua) || preg_match("/MSIE 7/", $ua)) { |
|---|
| 1322 | echo '<?xml version="1.0" encoding="' . CHAR_CODE . '"?>' . "\n"; |
|---|
| 1323 | } |
|---|
| 1324 | } |
|---|
| 1325 | |
|---|
| 1326 | /* |
|---|
| 1327 | * 関数名:sfGetFileList() |
|---|
| 1328 | * 説明 :指定パス配下のディレクトリ取得 |
|---|
| 1329 | * 引数1 :取得するディレクトリパス |
|---|
| 1330 | */ |
|---|
| 1331 | function sfGetFileList($dir) { |
|---|
| 1332 | $arrFileList = array(); |
|---|
| 1333 | $arrDirList = array(); |
|---|
| 1334 | |
|---|
| 1335 | if (is_dir($dir)) { |
|---|
| 1336 | if ($dh = opendir($dir)) { |
|---|
| 1337 | $cnt = 0; |
|---|
| 1338 | // 行末の/を取り除く |
|---|
| 1339 | while (($file = readdir($dh)) !== false) $arrDir[] = $file; |
|---|
| 1340 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 1341 | // アルファベットと数字でソート |
|---|
| 1342 | natcasesort($arrDir); |
|---|
| 1343 | foreach ($arrDir as $file) { |
|---|
| 1344 | // ./ と ../を除くファイルのみを取得 |
|---|
| 1345 | if ($file != "." && $file != "..") { |
|---|
| 1346 | |
|---|
| 1347 | $path = $dir."/".$file; |
|---|
| 1348 | // SELECT内の見た目を整えるため指定文字数で切る |
|---|
| 1349 | $file_name = SC_Utils_Ex::sfCutString($file, FILE_NAME_LEN); |
|---|
| 1350 | $file_size = SC_Utils_Ex::sfCutString(SC_Utils_Ex::sfGetDirSize($path), FILE_NAME_LEN); |
|---|
| 1351 | $file_time = date("Y/m/d", filemtime($path)); |
|---|
| 1352 | |
|---|
| 1353 | // ディレクトリとファイルで格納配列を変える |
|---|
| 1354 | if (is_dir($path)) { |
|---|
| 1355 | $arrDirList[$cnt]['file_name'] = $file; |
|---|
| 1356 | $arrDirList[$cnt]['file_path'] = $path; |
|---|
| 1357 | $arrDirList[$cnt]['file_size'] = $file_size; |
|---|
| 1358 | $arrDirList[$cnt]['file_time'] = $file_time; |
|---|
| 1359 | $arrDirList[$cnt]['is_dir'] = true; |
|---|
| 1360 | } else { |
|---|
| 1361 | $arrFileList[$cnt]['file_name'] = $file; |
|---|
| 1362 | $arrFileList[$cnt]['file_path'] = $path; |
|---|
| 1363 | $arrFileList[$cnt]['file_size'] = $file_size; |
|---|
| 1364 | $arrFileList[$cnt]['file_time'] = $file_time; |
|---|
| 1365 | $arrFileList[$cnt]['is_dir'] = false; |
|---|
| 1366 | } |
|---|
| 1367 | $cnt++; |
|---|
| 1368 | } |
|---|
| 1369 | } |
|---|
| 1370 | closedir($dh); |
|---|
| 1371 | } |
|---|
| 1372 | } |
|---|
| 1373 | |
|---|
| 1374 | // フォルダを先頭にしてマージ |
|---|
| 1375 | return array_merge($arrDirList, $arrFileList); |
|---|
| 1376 | } |
|---|
| 1377 | |
|---|
| 1378 | /* |
|---|
| 1379 | * 関数名:sfGetDirSize() |
|---|
| 1380 | * 説明 :指定したディレクトリのバイト数を取得 |
|---|
| 1381 | * 引数1 :ディレクトリ |
|---|
| 1382 | */ |
|---|
| 1383 | function sfGetDirSize($dir) { |
|---|
| 1384 | if (file_exists($dir)) { |
|---|
| 1385 | // ディレクトリの場合下層ファイルの総量を取得 |
|---|
| 1386 | if (is_dir($dir)) { |
|---|
| 1387 | $handle = opendir($dir); |
|---|
| 1388 | while ($file = readdir($handle)) { |
|---|
| 1389 | // 行末の/を取り除く |
|---|
| 1390 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 1391 | $path = $dir."/".$file; |
|---|
| 1392 | if ($file != '..' && $file != '.' && !is_dir($path)) { |
|---|
| 1393 | $bytes += filesize($path); |
|---|
| 1394 | } else if (is_dir($path) && $file != '..' && $file != '.') { |
|---|
| 1395 | // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。 |
|---|
| 1396 | $bytes += SC_Utils_Ex::sfGetDirSize($path); |
|---|
| 1397 | } |
|---|
| 1398 | } |
|---|
| 1399 | } else { |
|---|
| 1400 | // ファイルの場合 |
|---|
| 1401 | $bytes = filesize($dir); |
|---|
| 1402 | } |
|---|
| 1403 | } |
|---|
| 1404 | // ディレクトリ(ファイル)が存在しない場合は0byteを返す |
|---|
| 1405 | if($bytes == "") $bytes = 0; |
|---|
| 1406 | |
|---|
| 1407 | return $bytes; |
|---|
| 1408 | } |
|---|
| 1409 | |
|---|
| 1410 | /* |
|---|
| 1411 | * 関数名:sfGetFileTree() |
|---|
| 1412 | * 説明 :ツリー生成用配列取得(javascriptに渡す用) |
|---|
| 1413 | * 引数1 :ディレクトリ |
|---|
| 1414 | * 引数2 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 |
|---|
| 1415 | */ |
|---|
| 1416 | function sfGetFileTree($dir, $tree_status) { |
|---|
| 1417 | |
|---|
| 1418 | $cnt = 0; |
|---|
| 1419 | $arrTree = array(); |
|---|
| 1420 | $default_rank = count(explode('/', $dir)); |
|---|
| 1421 | |
|---|
| 1422 | // 文末の/を取り除く |
|---|
| 1423 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 1424 | // 最上位層を格納(user_data/) |
|---|
| 1425 | if (sfDirChildExists($dir)) { |
|---|
| 1426 | $arrTree[$cnt]['type'] = "_parent"; |
|---|
| 1427 | } else { |
|---|
| 1428 | $arrTree[$cnt]['type'] = "_child"; |
|---|
| 1429 | } |
|---|
| 1430 | $arrTree[$cnt]['path'] = $dir; |
|---|
| 1431 | $arrTree[$cnt]['rank'] = 0; |
|---|
| 1432 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 1433 | // 初期表示はオープン |
|---|
| 1434 | if ($_POST['mode'] != '') { |
|---|
| 1435 | $arrTree[$cnt]['open'] = lfIsFileOpen($dir, $tree_status); |
|---|
| 1436 | } else { |
|---|
| 1437 | $arrTree[$cnt]['open'] = true; |
|---|
| 1438 | } |
|---|
| 1439 | $cnt++; |
|---|
| 1440 | |
|---|
| 1441 | sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 1442 | |
|---|
| 1443 | return $arrTree; |
|---|
| 1444 | } |
|---|
| 1445 | |
|---|
| 1446 | /* |
|---|
| 1447 | * 関数名:sfGetFileTree() |
|---|
| 1448 | * 説明 :ツリー生成用配列取得(javascriptに渡す用) |
|---|
| 1449 | * 引数1 :ディレクトリ |
|---|
| 1450 | * 引数2 :デフォルトの階層(/区切りで 0,1,2・・・とカウント) |
|---|
| 1451 | * 引数3 :連番 |
|---|
| 1452 | * 引数4 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 |
|---|
| 1453 | */ |
|---|
| 1454 | function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) { |
|---|
| 1455 | |
|---|
| 1456 | if (file_exists($dir)) { |
|---|
| 1457 | if ($handle = opendir("$dir")) { |
|---|
| 1458 | while (false !== ($item = readdir($handle))) $arrDir[] = $item; |
|---|
| 1459 | // アルファベットと数字でソート |
|---|
| 1460 | natcasesort($arrDir); |
|---|
| 1461 | foreach ($arrDir as $item) { |
|---|
| 1462 | if ($item != "." && $item != "..") { |
|---|
| 1463 | // 文末の/を取り除く |
|---|
| 1464 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 1465 | $path = $dir."/".$item; |
|---|
| 1466 | // ディレクトリのみ取得 |
|---|
| 1467 | if (is_dir($path)) { |
|---|
| 1468 | $arrTree[$cnt]['path'] = $path; |
|---|
| 1469 | if (sfDirChildExists($path)) { |
|---|
| 1470 | $arrTree[$cnt]['type'] = "_parent"; |
|---|
| 1471 | } else { |
|---|
| 1472 | $arrTree[$cnt]['type'] = "_child"; |
|---|
| 1473 | } |
|---|
| 1474 | |
|---|
| 1475 | // 階層を割り出す |
|---|
| 1476 | $arrCnt = explode('/', $path); |
|---|
| 1477 | $rank = count($arrCnt); |
|---|
| 1478 | $arrTree[$cnt]['rank'] = $rank - $default_rank + 1; |
|---|
| 1479 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 1480 | // フォルダが開いているか |
|---|
| 1481 | $arrTree[$cnt]['open'] = lfIsFileOpen($path, $tree_status); |
|---|
| 1482 | $cnt++; |
|---|
| 1483 | // 下層ディレクトリ取得の為、再帰的に呼び出す |
|---|
| 1484 | sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 1485 | } |
|---|
| 1486 | } |
|---|
| 1487 | } |
|---|
| 1488 | } |
|---|
| 1489 | closedir($handle); |
|---|
| 1490 | } |
|---|
| 1491 | } |
|---|
| 1492 | |
|---|
| 1493 | /* |
|---|
| 1494 | * 関数名:sfDirChildExists() |
|---|
| 1495 | * 説明 :指定したディレクトリ配下にファイルがあるか |
|---|
| 1496 | * 引数1 :ディレクトリ |
|---|
| 1497 | */ |
|---|
| 1498 | function sfDirChildExists($dir) { |
|---|
| 1499 | if (file_exists($dir)) { |
|---|
| 1500 | if (is_dir($dir)) { |
|---|
| 1501 | $handle = opendir($dir); |
|---|
| 1502 | while ($file = readdir($handle)) { |
|---|
| 1503 | // 行末の/を取り除く |
|---|
| 1504 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 1505 | $path = $dir."/".$file; |
|---|
| 1506 | if ($file != '..' && $file != '.' && is_dir($path)) { |
|---|
| 1507 | return true; |
|---|
| 1508 | } |
|---|
| 1509 | } |
|---|
| 1510 | } |
|---|
| 1511 | } |
|---|
| 1512 | |
|---|
| 1513 | return false; |
|---|
| 1514 | } |
|---|
| 1515 | |
|---|
| 1516 | /* |
|---|
| 1517 | * 関数名:lfIsFileOpen() |
|---|
| 1518 | * 説明 :指定したファイルが前回開かれた状態にあったかチェック |
|---|
| 1519 | * 引数1 :ディレクトリ |
|---|
| 1520 | * 引数2 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 |
|---|
| 1521 | */ |
|---|
| 1522 | function lfIsFileOpen($dir, $tree_status) { |
|---|
| 1523 | $arrTreeStatus = explode('\|', $tree_status); |
|---|
| 1524 | if (in_array($dir, $arrTreeStatus)) { |
|---|
| 1525 | return true; |
|---|
| 1526 | } |
|---|
| 1527 | |
|---|
| 1528 | return false; |
|---|
| 1529 | } |
|---|
| 1530 | |
|---|
| 1531 | /* |
|---|
| 1532 | * 関数名:sfDownloadFile() |
|---|
| 1533 | * 引数1 :ファイルパス |
|---|
| 1534 | * 説明 :ファイルのダウンロード |
|---|
| 1535 | */ |
|---|
| 1536 | function sfDownloadFile($file) { |
|---|
| 1537 | // ファイルの場合はダウンロードさせる |
|---|
| 1538 | Header("Content-disposition: attachment; filename=".basename($file)); |
|---|
| 1539 | Header("Content-type: application/octet-stream; name=".basename($file)); |
|---|
| 1540 | Header("Cache-Control: "); |
|---|
| 1541 | Header("Pragma: "); |
|---|
| 1542 | echo (sfReadFile($file)); |
|---|
| 1543 | } |
|---|
| 1544 | |
|---|
| 1545 | /* |
|---|
| 1546 | * 関数名:sfCreateFile() |
|---|
| 1547 | * 引数1 :ファイルパス |
|---|
| 1548 | * 引数2 :パーミッション |
|---|
| 1549 | * 説明 :ファイル作成 |
|---|
| 1550 | */ |
|---|
| 1551 | function sfCreateFile($file, $mode = "") { |
|---|
| 1552 | // 行末の/を取り除く |
|---|
| 1553 | if ($mode != "") { |
|---|
| 1554 | $ret = @mkdir($file, $mode); |
|---|
| 1555 | } else { |
|---|
| 1556 | $ret = @mkdir($file); |
|---|
| 1557 | } |
|---|
| 1558 | |
|---|
| 1559 | return $ret; |
|---|
| 1560 | } |
|---|
| 1561 | |
|---|
| 1562 | /* |
|---|
| 1563 | * 関数名:sfReadFile() |
|---|
| 1564 | * 引数1 :ファイルパス |
|---|
| 1565 | * 説明 :ファイル読込 |
|---|
| 1566 | */ |
|---|
| 1567 | function sfReadFile($filename) { |
|---|
| 1568 | $str = ""; |
|---|
| 1569 | // バイナリモードでオープン |
|---|
| 1570 | $fp = @fopen($filename, 'rb'); |
|---|
| 1571 | //ファイル内容を全て変数に読み込む |
|---|
| 1572 | if ($fp) { |
|---|
| 1573 | $str = @fread($fp, filesize($filename)+1); |
|---|
| 1574 | } |
|---|
| 1575 | @fclose($fp); |
|---|
| 1576 | |
|---|
| 1577 | return $str; |
|---|
| 1578 | } |
|---|
| 1579 | |
|---|
| 1580 | /** |
|---|
| 1581 | * CSV出力用データ取得 |
|---|
| 1582 | * |
|---|
| 1583 | * @return string |
|---|
| 1584 | */ |
|---|
| 1585 | function getCSVData($array, $arrayIndex) { |
|---|
| 1586 | for ($i = 0; $i < count($array); $i++) { |
|---|
| 1587 | // インデックスが設定されている場合 |
|---|
| 1588 | if (is_array($arrayIndex) && 0 < count($arrayIndex)) { |
|---|
| 1589 | for ($j = 0; $j < count($arrayIndex); $j++) { |
|---|
| 1590 | if ($j > 0) $return .= ","; |
|---|
| 1591 | $return .= "\""; |
|---|
| 1592 | $return .= mb_ereg_replace("<","<",mb_ereg_replace("\"","\"\"",$array[$i][$arrayIndex[$j]])) ."\""; |
|---|
| 1593 | } |
|---|
| 1594 | } else { |
|---|
| 1595 | for ($j = 0; $j < count($array[$i]); $j++) { |
|---|
| 1596 | if ($j > 0) $return .= ","; |
|---|
| 1597 | $return .= "\""; |
|---|
| 1598 | $return .= mb_ereg_replace("<","<",mb_ereg_replace("\"","\"\"",$array[$i][$j])) ."\""; |
|---|
| 1599 | } |
|---|
| 1600 | } |
|---|
| 1601 | $return .= "\n"; |
|---|
| 1602 | } |
|---|
| 1603 | return $return; |
|---|
| 1604 | } |
|---|
| 1605 | |
|---|
| 1606 | /** |
|---|
| 1607 | * 配列をテーブルタグで出力する。 |
|---|
| 1608 | * |
|---|
| 1609 | * @return string |
|---|
| 1610 | */ |
|---|
| 1611 | function getTableTag($array) { |
|---|
| 1612 | $html = "<table>"; |
|---|
| 1613 | $html.= "<tr>"; |
|---|
| 1614 | foreach ($array[0] as $key => $val) { |
|---|
| 1615 | $html.="<th>$key</th>"; |
|---|
| 1616 | } |
|---|
| 1617 | $html.= "</tr>"; |
|---|
| 1618 | |
|---|
| 1619 | $cnt = count($array); |
|---|
| 1620 | |
|---|
| 1621 | for ($i = 0; $i < $cnt; $i++) { |
|---|
| 1622 | $html.= "<tr>"; |
|---|
| 1623 | foreach ($array[$i] as $val) { |
|---|
| 1624 | $html.="<td>$val</td>"; |
|---|
| 1625 | } |
|---|
| 1626 | $html.= "</tr>"; |
|---|
| 1627 | } |
|---|
| 1628 | return $html; |
|---|
| 1629 | } |
|---|
| 1630 | |
|---|
| 1631 | /** |
|---|
| 1632 | * 一覧-メイン画像のファイル指定がない場合、専用の画像ファイルに書き換える。 |
|---|
| 1633 | * |
|---|
| 1634 | * @param string &$filename ファイル名 |
|---|
| 1635 | * @return string |
|---|
| 1636 | */ |
|---|
| 1637 | function sfNoImageMainList($filename = '') { |
|---|
| 1638 | if (strlen($filename) == 0 || substr($filename, -1, 1) == '/') { |
|---|
| 1639 | $filename .= 'noimage_main_list.jpg'; |
|---|
| 1640 | } |
|---|
| 1641 | return $filename; |
|---|
| 1642 | } |
|---|
| 1643 | |
|---|
| 1644 | /** |
|---|
| 1645 | * 詳細-メイン画像のファイル指定がない場合、専用の画像ファイルに書き換える。 |
|---|
| 1646 | * |
|---|
| 1647 | * @param string &$filename ファイル名 |
|---|
| 1648 | * @return string |
|---|
| 1649 | */ |
|---|
| 1650 | function sfNoImageMain($filename = '') { |
|---|
| 1651 | if (strlen($filename) == 0 || substr($filename, -1, 1) == '/') { |
|---|
| 1652 | $filename .= 'noimage_main.png'; |
|---|
| 1653 | } |
|---|
| 1654 | return $filename; |
|---|
| 1655 | } |
|---|
| 1656 | |
|---|
| 1657 | /* デバッグ用 ------------------------------------------------------------------------------------------------*/ |
|---|
| 1658 | function sfPrintR($obj) { |
|---|
| 1659 | echo '<div style="font-size: 12px;color: #00FF00;">' . "\n"; |
|---|
| 1660 | echo '<strong>**デバッグ中**</strong><br />' . "\n"; |
|---|
| 1661 | echo '<pre>' . "\n"; |
|---|
| 1662 | var_dump($obj); |
|---|
| 1663 | echo '</pre>' . "\n"; |
|---|
| 1664 | echo '<strong>**デバッグ中**</strong></div>' . "\n"; |
|---|
| 1665 | } |
|---|
| 1666 | |
|---|
| 1667 | /** |
|---|
| 1668 | * ランダムな文字列を取得する |
|---|
| 1669 | * |
|---|
| 1670 | * @param integer $length 文字数 |
|---|
| 1671 | * @return string ランダムな文字列 |
|---|
| 1672 | */ |
|---|
| 1673 | function sfGetRandomString($length = 1) { |
|---|
| 1674 | require_once dirname(__FILE__) . '/../../module/Text/Password.php'; |
|---|
| 1675 | return Text_Password::create($length); |
|---|
| 1676 | } |
|---|
| 1677 | |
|---|
| 1678 | /** |
|---|
| 1679 | * 現在の URL を取得する |
|---|
| 1680 | * |
|---|
| 1681 | * @return string 現在のURL |
|---|
| 1682 | */ |
|---|
| 1683 | function sfGetUrl() { |
|---|
| 1684 | $url = ''; |
|---|
| 1685 | |
|---|
| 1686 | if (SC_Utils_Ex::sfIsHTTPS()) { |
|---|
| 1687 | $url = "https://"; |
|---|
| 1688 | } else { |
|---|
| 1689 | $url = "http://"; |
|---|
| 1690 | } |
|---|
| 1691 | |
|---|
| 1692 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '?' . $_SERVER['QUERY_STRING']; |
|---|
| 1693 | |
|---|
| 1694 | return $url; |
|---|
| 1695 | } |
|---|
| 1696 | |
|---|
| 1697 | /** |
|---|
| 1698 | * バックトレースをテキスト形式で出力する |
|---|
| 1699 | * |
|---|
| 1700 | * @return string テキストで表現したバックトレース |
|---|
| 1701 | */ |
|---|
| 1702 | function sfBacktraceToString($arrBacktrace) { |
|---|
| 1703 | $string = ''; |
|---|
| 1704 | |
|---|
| 1705 | foreach (array_reverse($arrBacktrace) as $backtrace) { |
|---|
| 1706 | if (strlen($backtrace['class']) >= 1) { |
|---|
| 1707 | $func = $backtrace['class'] . $backtrace['type'] . $backtrace['function']; |
|---|
| 1708 | } else { |
|---|
| 1709 | $func = $backtrace['function']; |
|---|
| 1710 | } |
|---|
| 1711 | |
|---|
| 1712 | $string .= $backtrace['file'] . " " . $backtrace['line'] . ":" . $func . "\n"; |
|---|
| 1713 | } |
|---|
| 1714 | |
|---|
| 1715 | return $string; |
|---|
| 1716 | } |
|---|
| 1717 | |
|---|
| 1718 | /** |
|---|
| 1719 | * 管理機能かを判定 |
|---|
| 1720 | * |
|---|
| 1721 | * @return bool 管理機能か |
|---|
| 1722 | */ |
|---|
| 1723 | function sfIsAdminFunction() { |
|---|
| 1724 | return defined('ADMIN_FUNCTION') && ADMIN_FUNCTION; |
|---|
| 1725 | } |
|---|
| 1726 | |
|---|
| 1727 | /** |
|---|
| 1728 | * フロント機能かを判定 |
|---|
| 1729 | * |
|---|
| 1730 | * @return bool フロント機能か |
|---|
| 1731 | */ |
|---|
| 1732 | function sfIsFrontFunction() { |
|---|
| 1733 | return defined('FRONT_FUNCTION') && FRONT_FUNCTION; |
|---|
| 1734 | } |
|---|
| 1735 | |
|---|
| 1736 | /** |
|---|
| 1737 | * インストール機能かを判定 |
|---|
| 1738 | * |
|---|
| 1739 | * @return bool インストール機能か |
|---|
| 1740 | */ |
|---|
| 1741 | function sfIsInstallFunction() { |
|---|
| 1742 | return defined('INSTALL_FUNCTION') && INSTALL_FUNCTION; |
|---|
| 1743 | } |
|---|
| 1744 | |
|---|
| 1745 | // 郵便番号から住所の取得 |
|---|
| 1746 | function sfGetAddress($zipcode) { |
|---|
| 1747 | |
|---|
| 1748 | $objQuery = new SC_Query_Ex(ZIP_DSN); |
|---|
| 1749 | |
|---|
| 1750 | $masterData = new SC_DB_MasterData_Ex(); |
|---|
| 1751 | $arrPref = $masterData->getMasterData('mtb_pref'); |
|---|
| 1752 | // インデックスと値を反転させる。 |
|---|
| 1753 | $arrREV_PREF = array_flip($arrPref); |
|---|
| 1754 | |
|---|
| 1755 | // 郵便番号検索文作成 |
|---|
| 1756 | $zipcode = mb_convert_kana($zipcode ,'n'); |
|---|
| 1757 | $sqlse = "SELECT state, city, town FROM mtb_zip WHERE zipcode = ?"; |
|---|
| 1758 | |
|---|
| 1759 | $data_list = $objQuery->getAll($sqlse, array($zipcode)); |
|---|
| 1760 | if (empty($data_list)) return array(); |
|---|
| 1761 | |
|---|
| 1762 | /* |
|---|
| 1763 | 総務省からダウンロードしたデータをそのままインポートすると |
|---|
| 1764 | 以下のような文字列が入っているので 対策する。 |
|---|
| 1765 | ・(1・19丁目) |
|---|
| 1766 | ・以下に掲載がない場合 |
|---|
| 1767 | */ |
|---|
| 1768 | $town = $data_list[0]['town']; |
|---|
| 1769 | $town = ereg_replace("(.*)$","",$town); |
|---|
| 1770 | $town = ereg_replace("以下に掲載がない場合","",$town); |
|---|
| 1771 | $data_list[0]['town'] = $town; |
|---|
| 1772 | $data_list[0]['state'] = $arrREV_PREF[$data_list[0]['state']]; |
|---|
| 1773 | |
|---|
| 1774 | return $data_list; |
|---|
| 1775 | } |
|---|
| 1776 | |
|---|
| 1777 | /** |
|---|
| 1778 | * プラグインが配置されているディレクトリ(フルパス)を取得する |
|---|
| 1779 | * |
|---|
| 1780 | * @param string $file プラグイン情報ファイル(info.php)のパス |
|---|
| 1781 | * @return SimpleXMLElement プラグイン XML |
|---|
| 1782 | */ |
|---|
| 1783 | function sfGetPluginFullPathByRequireFilePath($file) { |
|---|
| 1784 | return str_replace('\\', '/', dirname($file)) . '/'; |
|---|
| 1785 | } |
|---|
| 1786 | |
|---|
| 1787 | /** |
|---|
| 1788 | * プラグインのパスを取得する |
|---|
| 1789 | * |
|---|
| 1790 | * @param string $pluginFullPath プラグインが配置されているディレクトリ(フルパス) |
|---|
| 1791 | * @return SimpleXMLElement プラグイン XML |
|---|
| 1792 | */ |
|---|
| 1793 | function sfGetPluginPathByPluginFullPath($pluginFullPath) { |
|---|
| 1794 | return basename(rtrim($pluginFullPath, '/')); |
|---|
| 1795 | } |
|---|
| 1796 | |
|---|
| 1797 | /** |
|---|
| 1798 | * プラグイン情報配列の基本形を作成する |
|---|
| 1799 | * |
|---|
| 1800 | * @param string $file プラグイン情報ファイル(info.php)のパス |
|---|
| 1801 | * @return array プラグイン情報配列 |
|---|
| 1802 | */ |
|---|
| 1803 | function sfMakePluginInfoArray($file) { |
|---|
| 1804 | $fullPath = SC_Utils_Ex::sfGetPluginFullPathByRequireFilePath($file); |
|---|
| 1805 | |
|---|
| 1806 | return |
|---|
| 1807 | array( |
|---|
| 1808 | // パス |
|---|
| 1809 | 'path' => SC_Utils_Ex::sfGetPluginPathByPluginFullPath($fullPath), |
|---|
| 1810 | // プラグイン名 |
|---|
| 1811 | 'name' => '未定義', |
|---|
| 1812 | // フルパス |
|---|
| 1813 | 'fullpath' => $fullPath, |
|---|
| 1814 | // バージョン |
|---|
| 1815 | 'version' => null, |
|---|
| 1816 | // 著作者 |
|---|
| 1817 | 'auther' => '未定義', |
|---|
| 1818 | ) |
|---|
| 1819 | ; |
|---|
| 1820 | } |
|---|
| 1821 | |
|---|
| 1822 | /** |
|---|
| 1823 | * プラグイン情報配列を取得する |
|---|
| 1824 | * |
|---|
| 1825 | * TODO include_once を利用することで例外対応をサボタージュしているのを改善する。 |
|---|
| 1826 | * |
|---|
| 1827 | * @param string $path プラグインのディレクトリ名 |
|---|
| 1828 | * @return array プラグイン情報配列 |
|---|
| 1829 | */ |
|---|
| 1830 | function sfGetPluginInfoArray($path) { |
|---|
| 1831 | return (array)include_once PLUGIN_REALDIR . "$path/plugin_info.php"; |
|---|
| 1832 | } |
|---|
| 1833 | |
|---|
| 1834 | /** |
|---|
| 1835 | * プラグイン XML を読み込む |
|---|
| 1836 | * |
|---|
| 1837 | * TODO 空だったときを考慮 |
|---|
| 1838 | * |
|---|
| 1839 | * @return SimpleXMLElement プラグイン XML |
|---|
| 1840 | */ |
|---|
| 1841 | function sfGetPluginsXml() { |
|---|
| 1842 | return simplexml_load_file(PLUGIN_REALDIR . 'plugins.xml'); |
|---|
| 1843 | } |
|---|
| 1844 | |
|---|
| 1845 | /** |
|---|
| 1846 | * プラグイン XML を書き込む |
|---|
| 1847 | * |
|---|
| 1848 | * @param SimpleXMLElement $pluginsXml プラグイン XML |
|---|
| 1849 | * @return integer ファイルに書き込まれたバイト数を返します。 |
|---|
| 1850 | */ |
|---|
| 1851 | function sfPutPluginsXml($pluginsXml) { |
|---|
| 1852 | if (version_compare(PHP_VERSION, '5.0.0', '>')) { |
|---|
| 1853 | return; |
|---|
| 1854 | } |
|---|
| 1855 | |
|---|
| 1856 | $xml = $pluginsXml->asXML(); |
|---|
| 1857 | if (strlen($xml) == 0) SC_Utils_Ex::sfDispException(); |
|---|
| 1858 | |
|---|
| 1859 | $return = file_put_contents(PLUGIN_REALDIR . 'plugins.xml', $pluginsXml->asXML()); |
|---|
| 1860 | if ($return === false) SC_Utils_Ex::sfDispException(); |
|---|
| 1861 | return $return; |
|---|
| 1862 | } |
|---|
| 1863 | |
|---|
| 1864 | function sfLoadPluginInfo($filenamePluginInfo) { |
|---|
| 1865 | return (array)include_once $filenamePluginInfo; |
|---|
| 1866 | } |
|---|
| 1867 | |
|---|
| 1868 | /** |
|---|
| 1869 | * 現在の Unix タイムスタンプを float (秒単位) でマイクロ秒まで返す |
|---|
| 1870 | * |
|---|
| 1871 | * PHP4の上位互換用途。 |
|---|
| 1872 | * FIXME PHP4でテストする。(現状全くテストしていない。) |
|---|
| 1873 | * @param SimpleXMLElement $pluginsXml プラグイン XML |
|---|
| 1874 | * @return integer ファイルに書き込まれたバイト数を返します。 |
|---|
| 1875 | */ |
|---|
| 1876 | function sfMicrotimeFloat() { |
|---|
| 1877 | $microtime = microtime(true); |
|---|
| 1878 | if (is_string($microtime)) { |
|---|
| 1879 | list($usec, $sec) = explode(" ", microtime()); |
|---|
| 1880 | return (float)$usec + (float)$sec; |
|---|
| 1881 | } |
|---|
| 1882 | return $microtime; |
|---|
| 1883 | } |
|---|
| 1884 | |
|---|
| 1885 | /** |
|---|
| 1886 | * 変数が空白かどうかをチェックする. |
|---|
| 1887 | * |
|---|
| 1888 | * 引数 $val が空白かどうかをチェックする. 空白の場合は true. |
|---|
| 1889 | * 以下の文字は空白と判断する. |
|---|
| 1890 | * - " " (ASCII 32 (0x20)), 通常の空白 |
|---|
| 1891 | * - "\t" (ASCII 9 (0x09)), タブ |
|---|
| 1892 | * - "\n" (ASCII 10 (0x0A)), リターン |
|---|
| 1893 | * - "\r" (ASCII 13 (0x0D)), 改行 |
|---|
| 1894 | * - "\0" (ASCII 0 (0x00)), NULバイト |
|---|
| 1895 | * - "\x0B" (ASCII 11 (0x0B)), 垂直タブ |
|---|
| 1896 | * |
|---|
| 1897 | * 引数 $val が配列の場合は, 空の配列の場合 true を返す. |
|---|
| 1898 | * |
|---|
| 1899 | * 引数 $greedy が true の場合は, 全角スペース, ネストした空の配列も |
|---|
| 1900 | * 空白と判断する. |
|---|
| 1901 | * |
|---|
| 1902 | * @param mixed $val チェック対象の変数 |
|---|
| 1903 | * @param boolean $greedy "貧欲"にチェックを行う場合 true |
|---|
| 1904 | * @return boolean $val が空白と判断された場合 true |
|---|
| 1905 | */ |
|---|
| 1906 | function isBlank($val, $greedy = true) { |
|---|
| 1907 | if (is_array($val)) { |
|---|
| 1908 | if ($greedy) { |
|---|
| 1909 | if (empty($val)) { |
|---|
| 1910 | return true; |
|---|
| 1911 | } |
|---|
| 1912 | $array_result = true; |
|---|
| 1913 | foreach ($val as $in) { |
|---|
| 1914 | /* |
|---|
| 1915 | * SC_Utils_Ex への再帰は無限ループやメモリリークの懸念 |
|---|
| 1916 | * 自クラスへ再帰する. |
|---|
| 1917 | */ |
|---|
| 1918 | $array_result = SC_Utils::isBlank($in, $greedy); |
|---|
| 1919 | if (!$array_result) { |
|---|
| 1920 | return false; |
|---|
| 1921 | } |
|---|
| 1922 | } |
|---|
| 1923 | return $array_result; |
|---|
| 1924 | } else { |
|---|
| 1925 | return empty($val); |
|---|
| 1926 | } |
|---|
| 1927 | } |
|---|
| 1928 | |
|---|
| 1929 | if ($greedy) { |
|---|
| 1930 | $val = preg_replace("/ /", "", $val); |
|---|
| 1931 | } |
|---|
| 1932 | |
|---|
| 1933 | $val = trim($val); |
|---|
| 1934 | if (strlen($val) > 0) { |
|---|
| 1935 | return false; |
|---|
| 1936 | } |
|---|
| 1937 | return true; |
|---|
| 1938 | } |
|---|
| 1939 | |
|---|
| 1940 | /** |
|---|
| 1941 | * 指定されたURLのドメインが一致するかを返す |
|---|
| 1942 | * |
|---|
| 1943 | * 戻り値:一致(true) 不一致(false) |
|---|
| 1944 | * |
|---|
| 1945 | * @param string $url |
|---|
| 1946 | * @return boolean |
|---|
| 1947 | */ |
|---|
| 1948 | function sfIsInternalDomain($url) { |
|---|
| 1949 | $netURL = new Net_URL(HTTP_URL); |
|---|
| 1950 | $host = $netURL->host; |
|---|
| 1951 | if (!$host) return false; |
|---|
| 1952 | $host = preg_quote($host, "#"); |
|---|
| 1953 | if (!preg_match("#^(http|https)://{$host}#i", $url)) return false; |
|---|
| 1954 | return true; |
|---|
| 1955 | } |
|---|
| 1956 | |
|---|
| 1957 | /** |
|---|
| 1958 | * パスワードのハッシュ化 |
|---|
| 1959 | * |
|---|
| 1960 | * @param string $str 暗号化したい文言 |
|---|
| 1961 | * @param string $salt salt |
|---|
| 1962 | * @return string ハッシュ暗号化された文字列 |
|---|
| 1963 | */ |
|---|
| 1964 | function sfGetHashString($str, $salt) { |
|---|
| 1965 | $res = ''; |
|---|
| 1966 | if ($salt == '') { |
|---|
| 1967 | $salt = AUTH_MAGIC; |
|---|
| 1968 | } |
|---|
| 1969 | if (AUTH_TYPE == 'PLAIN') { |
|---|
| 1970 | $res = $str; |
|---|
| 1971 | } else { |
|---|
| 1972 | $res = hash_hmac(PASSWORD_HASH_ALGOS, $str . ":" . AUTH_MAGIC, $salt); |
|---|
| 1973 | } |
|---|
| 1974 | return $res; |
|---|
| 1975 | } |
|---|
| 1976 | |
|---|
| 1977 | /** |
|---|
| 1978 | * パスワード文字列のハッシュ一致判定 |
|---|
| 1979 | * |
|---|
| 1980 | * @param string $pass 確認したいパスワード文字列 |
|---|
| 1981 | * @param string $hashpass 確認したいパスワードハッシュ文字列 |
|---|
| 1982 | * @param string $salt salt |
|---|
| 1983 | * @return boolean 一致判定 |
|---|
| 1984 | */ |
|---|
| 1985 | function sfIsMatchHashPassword($pass, $hashpass, $salt) { |
|---|
| 1986 | $res = false; |
|---|
| 1987 | if ($hashpass != '') { |
|---|
| 1988 | if (AUTH_TYPE == 'PLAIN') { |
|---|
| 1989 | if ($pass === $hashpass) { |
|---|
| 1990 | $res = true; |
|---|
| 1991 | } |
|---|
| 1992 | } else { |
|---|
| 1993 | if (empty($salt)) { |
|---|
| 1994 | // 旧バージョン(2.11未満)からの移行を考慮 |
|---|
| 1995 | $hash = sha1($pass . ":" . AUTH_MAGIC); |
|---|
| 1996 | } else { |
|---|
| 1997 | $hash = SC_Utils_Ex::sfGetHashString($pass, $salt); |
|---|
| 1998 | } |
|---|
| 1999 | if ($hash === $hashpass) { |
|---|
| 2000 | $res = true; |
|---|
| 2001 | } |
|---|
| 2002 | } |
|---|
| 2003 | } |
|---|
| 2004 | return $res; |
|---|
| 2005 | } |
|---|
| 2006 | |
|---|
| 2007 | /** |
|---|
| 2008 | * 検索結果の1ページあたりの最大表示件数を取得する |
|---|
| 2009 | * |
|---|
| 2010 | * フォームの入力値から最大表示件数を取得する |
|---|
| 2011 | * 取得できなかった場合は, 定数 SEARCH_PMAX の値を返す |
|---|
| 2012 | * |
|---|
| 2013 | * @param string $search_page_max 表示件数の選択値 |
|---|
| 2014 | * @return integer 1ページあたりの最大表示件数 |
|---|
| 2015 | */ |
|---|
| 2016 | function sfGetSearchPageMax($search_page_max) { |
|---|
| 2017 | if (SC_Utils_Ex::sfIsInt($search_page_max) && $search_page_max > 0) { |
|---|
| 2018 | $page_max = intval($search_page_max); |
|---|
| 2019 | } else { |
|---|
| 2020 | $page_max = SEARCH_PMAX; |
|---|
| 2021 | } |
|---|
| 2022 | return $page_max; |
|---|
| 2023 | } |
|---|
| 2024 | |
|---|
| 2025 | /** |
|---|
| 2026 | * 値を JSON 形式にして返す. |
|---|
| 2027 | * |
|---|
| 2028 | * この関数は, json_encode() 又は Services_JSON::encode() のラッパーです. |
|---|
| 2029 | * json_encode() 関数が使用可能な場合は json_encode() 関数を使用する. |
|---|
| 2030 | * 使用できない場合は, Services_JSON::encode() 関数を使用する. |
|---|
| 2031 | * |
|---|
| 2032 | * @param mixed $value JSON 形式にエンコードする値 |
|---|
| 2033 | * @return string JSON 形式にした文字列 |
|---|
| 2034 | * @see json_encode() |
|---|
| 2035 | * @see Services_JSON::encode() |
|---|
| 2036 | */ |
|---|
| 2037 | function jsonEncode($value) { |
|---|
| 2038 | if (function_exists('json_encode')) { |
|---|
| 2039 | return json_encode($value); |
|---|
| 2040 | } else { |
|---|
| 2041 | require_once dirname(__FILE__) . '/../../module/Services/JSON.php'; |
|---|
| 2042 | GC_Utils_Ex::gfPrintLog(' *use Services_JSON::encode(). faster than using the json_encode!'); |
|---|
| 2043 | $objJson = new Services_JSON(); |
|---|
| 2044 | return $objJson->encode($value); |
|---|
| 2045 | } |
|---|
| 2046 | } |
|---|
| 2047 | |
|---|
| 2048 | /** |
|---|
| 2049 | * JSON 文字列をデコードする. |
|---|
| 2050 | * |
|---|
| 2051 | * この関数は, json_decode() 又は Services_JSON::decode() のラッパーです. |
|---|
| 2052 | * json_decode() 関数が使用可能な場合は json_decode() 関数を使用する. |
|---|
| 2053 | * 使用できない場合は, Services_JSON::decode() 関数を使用する. |
|---|
| 2054 | * |
|---|
| 2055 | * @param string $json JSON 形式にエンコードされた文字列 |
|---|
| 2056 | * @return mixed デコードされた PHP の型 |
|---|
| 2057 | * @see json_decode() |
|---|
| 2058 | * @see Services_JSON::decode() |
|---|
| 2059 | */ |
|---|
| 2060 | function jsonDecode($json) { |
|---|
| 2061 | if (function_exists('json_decode')) { |
|---|
| 2062 | return json_decode($json); |
|---|
| 2063 | } else { |
|---|
| 2064 | require_once dirname(__FILE__) . '/../../module/Services/JSON.php'; |
|---|
| 2065 | GC_Utils_Ex::gfPrintLog(' *use Services_JSON::decode(). faster than using the json_decode!'); |
|---|
| 2066 | $objJson = new Services_JSON(); |
|---|
| 2067 | return $objJson->decode($json); |
|---|
| 2068 | } |
|---|
| 2069 | } |
|---|
| 2070 | |
|---|
| 2071 | /** |
|---|
| 2072 | * パスが絶対パスかどうかをチェックする. |
|---|
| 2073 | * |
|---|
| 2074 | * 引数のパスが絶対パスの場合は true を返す. |
|---|
| 2075 | * この関数は, パスの存在チェックを行なわないため注意すること. |
|---|
| 2076 | * |
|---|
| 2077 | * @param string チェック対象のパス |
|---|
| 2078 | * @return boolean 絶対パスの場合 true |
|---|
| 2079 | */ |
|---|
| 2080 | function isAbsoluteRealPath($realpath) { |
|---|
| 2081 | if (strpos(PHP_OS, 'WIN') === false) { |
|---|
| 2082 | return (substr($realpath, 0, 1) == '/'); |
|---|
| 2083 | } else { |
|---|
| 2084 | return preg_match('/^[a-zA-Z]:(\\\|\/)/', $realpath) ? true : false; |
|---|
| 2085 | } |
|---|
| 2086 | } |
|---|
| 2087 | |
|---|
| 2088 | /** |
|---|
| 2089 | * ディレクトリを再帰的に作成する. |
|---|
| 2090 | * |
|---|
| 2091 | * mkdir 関数の $recursive パラメーターを PHP4 でサポートする. |
|---|
| 2092 | * |
|---|
| 2093 | * @param string $pathname ディレクトリのパス |
|---|
| 2094 | * @param integer $mode 作成するディレクトリのパーミッション |
|---|
| 2095 | * @return boolean 作成に成功した場合 true; 失敗した場合 false |
|---|
| 2096 | * @see http://jp.php.net/mkdir |
|---|
| 2097 | */ |
|---|
| 2098 | function recursiveMkdir($pathname, $mode = 0777) { |
|---|
| 2099 | /* |
|---|
| 2100 | * SC_Utils_Ex への再帰は無限ループやメモリリークの懸念 |
|---|
| 2101 | * 自クラスへ再帰する. |
|---|
| 2102 | */ |
|---|
| 2103 | is_dir(dirname($pathname)) || SC_Utils::recursiveMkdir(dirname($pathname), $mode); |
|---|
| 2104 | return is_dir($pathname) || @mkdir($pathname, $mode); |
|---|
| 2105 | } |
|---|
| 2106 | |
|---|
| 2107 | function isAppInnerUrl($url) { |
|---|
| 2108 | $pattern = '/^(' . preg_quote(HTTP_URL, '/') . '|' . preg_quote(HTTPS_URL, '/') . ')/'; |
|---|
| 2109 | return preg_match($pattern, $url) >= 1; |
|---|
| 2110 | } |
|---|
| 2111 | |
|---|
| 2112 | /** |
|---|
| 2113 | * PHP のタイムアウトを延長する |
|---|
| 2114 | * |
|---|
| 2115 | * ループの中で呼び出すことを意図している。 |
|---|
| 2116 | * 暴走スレッドが残留する確率を軽減するため、set_time_limit(0) とはしていない。 |
|---|
| 2117 | * @param integer $seconds 最大実行時間を延長する秒数。 |
|---|
| 2118 | * @return boolean 成功=true, 失敗=false |
|---|
| 2119 | */ |
|---|
| 2120 | function extendTimeOut($seconds = null) { |
|---|
| 2121 | $safe_mode = (boolean)ini_get('safe_mode'); |
|---|
| 2122 | if ($safe_mode) return false; |
|---|
| 2123 | |
|---|
| 2124 | if (is_null($seconds)) { |
|---|
| 2125 | $seconds |
|---|
| 2126 | = is_numeric(ini_get('max_execution_time')) |
|---|
| 2127 | ? intval(ini_get('max_execution_time')) |
|---|
| 2128 | : intval(get_cfg_var('max_execution_time')) |
|---|
| 2129 | ; |
|---|
| 2130 | } |
|---|
| 2131 | |
|---|
| 2132 | // タイムアウトをリセット |
|---|
| 2133 | set_time_limit($seconds); |
|---|
| 2134 | |
|---|
| 2135 | return true; |
|---|
| 2136 | } |
|---|
| 2137 | |
|---|
| 2138 | /** |
|---|
| 2139 | * 指定されたパスの配下を再帰的に削除. |
|---|
| 2140 | * |
|---|
| 2141 | * @param string $path 削除対象のディレクトリまたはファイルのパス |
|---|
| 2142 | * @param boolean $del_myself $pathそのものを削除するか. true なら削除する. |
|---|
| 2143 | * @return void |
|---|
| 2144 | */ |
|---|
| 2145 | function deleteFile($path, $del_myself = true) { |
|---|
| 2146 | $flg = false; |
|---|
| 2147 | // 対象が存在するかを検証. |
|---|
| 2148 | if (file_exists($path) === false) { |
|---|
| 2149 | GC_Utils_Ex::gfPrintLog($path . " が存在しません."); |
|---|
| 2150 | } elseif (is_dir($path)) { |
|---|
| 2151 | // ディレクトリが指定された場合 |
|---|
| 2152 | $handle = opendir($path); |
|---|
| 2153 | if (!$handle) { |
|---|
| 2154 | GC_Utils_Ex::gfPrintLog($path . " が開けませんでした."); |
|---|
| 2155 | } |
|---|
| 2156 | while (($item = readdir($handle)) !== false) { |
|---|
| 2157 | if ($item === '.' || $item === '..') continue; |
|---|
| 2158 | $cur_path = $path . '/' . $item; |
|---|
| 2159 | if (is_dir($cur_path)) { |
|---|
| 2160 | // ディレクトリの場合、再帰処理 |
|---|
| 2161 | $flg = SC_Utils_Ex::deleteFile($cur_path); |
|---|
| 2162 | } else { |
|---|
| 2163 | // ファイルの場合、unlink |
|---|
| 2164 | $flg = @unlink($cur_path); |
|---|
| 2165 | } |
|---|
| 2166 | } |
|---|
| 2167 | closedir($handle); |
|---|
| 2168 | // ディレクトリを削除 |
|---|
| 2169 | GC_Utils_Ex::gfPrintLog($path . " を削除します."); |
|---|
| 2170 | if ($del_myself) { |
|---|
| 2171 | $flg = @rmdir($path); |
|---|
| 2172 | } |
|---|
| 2173 | } else { |
|---|
| 2174 | // ファイルが指定された場合. |
|---|
| 2175 | GC_Utils_Ex::gfPrintLog($path . " を削除します."); |
|---|
| 2176 | $flg = @unlink($path); |
|---|
| 2177 | } |
|---|
| 2178 | return $flg; |
|---|
| 2179 | } |
|---|
| 2180 | } |
|---|