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