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