Source for file SC_Utils.php

Documentation is available at SC_Utils.php

  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) 2000-2007 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.      * データが存在する場合、必ず1以上の数値が設定されている。
  40.      * 0を返した場合は、呼び出し元で対応すること。
  41.      *
  42.      * @param $control_id 管理ID
  43.      * @param $dsn DataSource
  44.      * @return $control_flg フラグ
  45.      */
  46.     function sfGetSiteControlFlg($control_id, $dsn = "") {
  47.  
  48.         // データソース
  49.         if($dsn == "") {
  50.             if(defined('DEFAULT_DSN')) {
  51.                 $dsn = DEFAULT_DSN;
  52.             } else {
  53.                 return;
  54.             }
  55.         }
  56.  
  57.         // クエリ生成
  58.         $target_column = "control_flg";
  59.         $table_name = "dtb_site_control";
  60.         $where = "control_id = ?";
  61.         $arrval = array($control_id);
  62.         $control_flg = 0;
  63.  
  64.         // クエリ発行
  65.         $objQuery = new SC_Query($dsn, true, true);
  66.         $arrSiteControl = $objQuery->select($target_column, $table_name, $where, $arrval);
  67.  
  68.         // データが存在すればフラグを取得する
  69.         if (count($arrSiteControl) > 0) {
  70.             $control_flg = $arrSiteControl[0]["control_flg"];
  71.         }
  72.  
  73.         return $control_flg;
  74.     }
  75.  
  76.     // インストール初期処理
  77.     function sfInitInstall() {
  78.         // インストール済みが定義されていない。
  79.         if(!defined('ECCUBE_INSTALL')) {
  80.             if(!ereg("/install/", $_SERVER['PHP_SELF'])) {
  81.                 header("Location: ./install/"); // TODO 絶対URL にする
  82.             }
  83.         } else {
  84.             $path = HTML_PATH . "install/index.php";
  85.             if(file_exists($path)) {
  86.                 SC_Utils::sfErrorHeader("&gt;&gt; /install/index.phpは、インストール完了後にファイルを削除してください。");
  87.             }
  88.         }
  89.     }
  90.  
  91.     // 装飾付きエラーメッセージの表示
  92.     function sfErrorHeader($mess, $print = false) {
  93.         global $GLOBAL_ERR;
  94.         $GLOBAL_ERR.="<div style='color: #F00; font-weight: bold; font-size: 12px;"
  95.             . "background-color: #FEB; text-align: center; padding: 5px;'>";
  96.         $GLOBAL_ERR.= $mess;
  97.         $GLOBAL_ERR.= "</div>";
  98.         if($print) {
  99.             print($GLOBAL_ERR);
  100.         }
  101.     }
  102.  
  103.     /* エラーページの表示 */
  104.     function sfDispError($type) {
  105.  
  106.         require_once(CLASS_EX_PATH . "page_extends/error/LC_Page_Error_DispError_Ex.php");
  107.  
  108.         $objPage = new LC_Page_Error_DispError_Ex();
  109.         register_shutdown_function(array($objPage, "destroy"));
  110.         $objPage->init();
  111.         $objPage->type = $type;
  112.         $objPage->process();
  113.         exit;
  114.     }
  115.  
  116.     /* サイトエラーページの表示 */
  117.     function sfDispSiteError($type, $objSiteSess = "", $return_top = false, $err_msg = "", $is_mobile = false) {
  118.         global $objCampaignSess;
  119.  
  120.         require_once(CLASS_EX_PATH . "page_extends/error/LC_Page_Error_Ex.php");
  121.  
  122.         $objPage = new LC_Page_Error_Ex();
  123.         register_shutdown_function(array($objPage, "destroy"));
  124.         $objPage->init();
  125.         $objPage->type = $type;
  126.         $objPage->objSiteSess = $objSiteSess;
  127.         $objPage->return_top = $return_top;
  128.         $objPage->err_msg = $err_msg;
  129.         $objPage->is_mobile = (defined('MOBILE_SITE')) ? true : false;
  130.         $objPage->process();
  131.         exit;
  132.     }
  133.  
  134.     /* 認証の可否判定 */
  135.     function sfIsSuccess($objSess, $disp_error = true) {
  136.         $ret = $objSess->IsSuccess();
  137.         if($ret != SUCCESS) {
  138.             if($disp_error) {
  139.                 // エラーページの表示
  140.                 SC_Utils::sfDispError($ret);
  141.             }
  142.             return false;
  143.         }
  144.         // リファラーチェック(CSRFの暫定的な対策)
  145.         // 「リファラ無」 の場合はスルー
  146.         // 「リファラ有」 かつ 「管理画面からの遷移でない」 場合にエラー画面を表示する
  147.         if ( empty($_SERVER['HTTP_REFERER']) ) {
  148.             // TODO 警告表示させる?
  149.             // sfErrorHeader('>> referrerが無効になっています。');
  150.         } else {
  151.             $domain  = SC_Utils::sfIsHTTPS() ? SSL_URL : SITE_URL;
  152.             $pattern = sprintf('|^%s.*|', $domain);
  153.             $referer = $_SERVER['HTTP_REFERER'];
  154.  
  155.             // 管理画面から以外の遷移の場合はエラー画面を表示
  156.             if (!preg_match($pattern, $referer)) {
  157.                 if ($disp_error) SC_Utils::sfDispError(INVALID_MOVE_ERRORR);
  158.                 return false;
  159.             }
  160.         }
  161.         return true;
  162.     }
  163.  
  164.     /**
  165.      * 文字列をアスタリスクへ変換する.
  166.      *
  167.      * @param string $passlen 変換する文字列
  168.      * @return string アスタリスクへ変換した文字列
  169.      */
  170.     function lfPassLen($passlen){
  171.         $ret = "";
  172.         for ($i=0;$i<$passlen;true){
  173.             $ret.="*";
  174.             $i++;
  175.         }
  176.         return $ret;
  177.     }
  178.  
  179.     /**
  180.      * HTTPSかどうかを判定
  181.      *
  182.      * @return bool 
  183.      */
  184.     function sfIsHTTPS () {
  185.         // HTTPS時には$_SERVER['HTTPS']には空でない値が入る
  186.         // $_SERVER['HTTPS'] != 'off' はIIS用
  187.         if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  188.             return true;
  189.         } else {
  190.             return false;
  191.         }
  192.     }
  193.  
  194.     /**
  195.      *  正規の遷移がされているかを判定
  196.      *  前画面でuniqidを埋め込んでおく必要がある
  197.      *  @param  obj  SC_Session, SC_SiteSession
  198.      *  @return bool 
  199.      */
  200.     function sfIsValidTransition($objSess) {
  201.         // 前画面からPOSTされるuniqidが正しいものかどうかをチェック
  202.         $uniqid = $objSess->getUniqId();
  203.         if ( !empty($_POST['uniqid']) && ($_POST['uniqid'] === $uniqid) ) {
  204.             return true;
  205.         } else {
  206.             return false;
  207.         }
  208.     }
  209.  
  210.     /* 前のページで正しく登録が行われたか判定 */
  211.     function sfIsPrePage(&$objSiteSess, $is_mobile = false) {
  212.         $ret = $objSiteSess->isPrePage();
  213.         if($ret != true) {
  214.             // エラーページの表示
  215.             SC_Utils::sfDispSiteError(PAGE_ERROR, $objSiteSess, false, "", $is_mobile);
  216.         }
  217.     }
  218.  
  219.     function sfCheckNormalAccess(&$objSiteSess, &$objCartSess) {
  220.         // ユーザユニークIDの取得
  221.         $uniqid = $objSiteSess->getUniqId();
  222.         // 購入ボタンを押した時のカート内容がコピーされていない場合のみコピーする。
  223.         $objCartSess->saveCurrentCart($uniqid);
  224.         // POSTのユニークIDとセッションのユニークIDを比較(ユニークIDがPOSTされていない場合はスルー)
  225.         $ret = $objSiteSess->checkUniqId();
  226.         if($ret != true) {
  227.             // エラーページの表示
  228.             SC_Utils_Ex::sfDispSiteError(CANCEL_PURCHASE, $objSiteSess);
  229.         }
  230.  
  231.         // カート内が空でないか || 購入ボタンを押してから変化がないか
  232.         $quantity = $objCartSess->getTotalQuantity();
  233.         $ret = $objCartSess->checkChangeCart();
  234.         if($ret == true || !($quantity > 0)) {
  235.             // カート情報表示に強制移動する
  236.             // FIXME false を返して, Page クラスで遷移させるべき...
  237.             if (defined("MOBILE_SITE")) {
  238.                 header("Location: ". MOBILE_URL_CART_TOP
  239.                        . "?" . session_name() . "=" . session_id());
  240.             } else {
  241.                 header("Location: ".URL_CART_TOP);
  242.             }
  243.             exit;
  244.         }
  245.         return $uniqid;
  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.     // INT型の数値チェック
  264.     function sfIsInt($value) {
  265.         if($value != "" && strlen($value) <= INT_LEN && is_numeric($value)) {
  266.             return true;
  267.         }
  268.         return false;
  269.     }
  270.  
  271.     function sfCSVDownload($data, $prefix = ""){
  272.  
  273.         if($prefix == "") {
  274.             $dir_name = SC_Utils::sfUpDirName();
  275.             $file_name = $dir_name . date("ymdHis") .".csv";
  276.         } else {
  277.             $file_name = $prefix . date("ymdHis") .".csv";
  278.         }
  279.  
  280.         /* HTTPヘッダの出力 */
  281.         Header("Content-disposition: attachment; filename=${file_name}");
  282.         Header("Content-type: application/octet-stream; name=${file_name}");
  283.         Header("Cache-Control: ");
  284.         Header("Pragma: ");
  285.  
  286.         if (mb_internal_encoding() == CHAR_CODE){
  287.             $data = mb_convert_encoding($data,'SJIS-Win',CHAR_CODE);
  288.         }
  289.  
  290.         /* データを出力 */
  291.         echo $data;
  292.     }
  293.  
  294.     /* 1階層上のディレクトリ名を取得する */
  295.     function sfUpDirName() {
  296.         $path = $_SERVER['PHP_SELF'];
  297.         $arrVal = split("/", $path);
  298.         $cnt = count($arrVal);
  299.         return $arrVal[($cnt - 2)];
  300.     }
  301.  
  302.  
  303.  
  304.  
  305.     /**
  306.      * 現在のサイトを更新(ただしポストは行わない)
  307.      *
  308.      * @deprecated LC_Page::reload() を使用して下さい.
  309.      */
  310.     function sfReload($get = "") {
  311.         if ($_SERVER["SERVER_PORT"] == "443" ){
  312.             $url = ereg_replace(URL_DIR . "$", "", SSL_URL);
  313.         } else {
  314.             $url = ereg_replace(URL_DIR . "$", "", SITE_URL);
  315.         }
  316.  
  317.         if($get != "") {
  318.             header("Location: ". $url . $_SERVER['PHP_SELF'] . "?" . $get);
  319.         } else {
  320.             header("Location: ". $url . $_SERVER['PHP_SELF']);
  321.         }
  322.         exit;
  323.     }
  324.  
  325.     // チェックボックスの値をマージ
  326.     function sfMergeCBValue($keyname, $max) {
  327.         $conv = "";
  328.         $cnt = 1;
  329.         for($cnt = 1; $cnt <= $max; $cnt++) {
  330.             if ($_POST[$keyname . $cnt] == "1") {
  331.                 $conv.= "1";
  332.             } else {
  333.                 $conv.= "0";
  334.             }
  335.         }
  336.         return $conv;
  337.     }
  338.  
  339.     // html_checkboxesの値をマージして2進数形式に変更する。
  340.     function sfMergeCheckBoxes($array, $max) {
  341.         $ret = "";
  342.         if(is_array($array)) {
  343.             foreach($array as $val) {
  344.                 $arrTmp[$val] = "1";
  345.             }
  346.         }
  347.         for($i = 1; $i <= $max; $i++) {
  348.             if(isset($arrTmp[$i]) && $arrTmp[$i] == "1") {
  349.                 $ret.= "1";
  350.             } else {
  351.                 $ret.= "0";
  352.             }
  353.         }
  354.         return $ret;
  355.     }
  356.  
  357.  
  358.     // html_checkboxesの値をマージして「-」でつなげる。
  359.     function sfMergeParamCheckBoxes($array) {
  360.         $ret = '';
  361.         if(is_array($array)) {
  362.             foreach($array as $val) {
  363.                 if($ret != "") {
  364.                     $ret.= "-$val";
  365.                 } else {
  366.                     $ret = $val;
  367.                 }
  368.             }
  369.         } else {
  370.             $ret = $array;
  371.         }
  372.         return $ret;
  373.     }
  374.  
  375.     // html_checkboxesの値をマージしてSQL検索用に変更する。
  376.     function sfSearchCheckBoxes($array) {
  377.         $max = 0;
  378.         $ret = "";
  379.         foreach($array as $val) {
  380.             $arrTmp[$val] = "1";
  381.             if($val > $max) {
  382.                 $max = $val;
  383.             }
  384.         }
  385.         for($i = 1; $i <= $max; $i++) {
  386.             if($arrTmp[$i] == "1") {
  387.                 $ret.= "1";
  388.             } else {
  389.                 $ret.= "_";
  390.             }
  391.         }
  392.  
  393.         if($ret != "") {
  394.             $ret.= "%";
  395.         }
  396.         return $ret;
  397.     }
  398.  
  399.     // 2進数形式の値をhtml_checkboxes対応の値に切り替える
  400.     function sfSplitCheckBoxes($val) {
  401.         $arrRet = array();
  402.         $len = strlen($val);
  403.         for($i = 0; $i < $len; $i++) {
  404.             if(substr($val, $i, 1) == "1") {
  405.                 $arrRet[] = ($i + 1);
  406.             }
  407.         }
  408.         return $arrRet;
  409.     }
  410.  
  411.     // チェックボックスの値をマージ
  412.     function sfMergeCBSearchValue($keyname, $max) {
  413.         $conv = "";
  414.         $cnt = 1;
  415.         for($cnt = 1; $cnt <= $max; $cnt++) {
  416.             if ($_POST[$keyname . $cnt] == "1") {
  417.                 $conv.= "1";
  418.             } else {
  419.                 $conv.= "_";
  420.             }
  421.         }
  422.         return $conv;
  423.     }
  424.  
  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::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.         for($cnt = 0; $cnt < $max; $cnt++) {
  476.             if($keysize != "") {
  477.                 $key = SC_Utils::sfCutString($arrList[$cnt][$keyname], $keysize);
  478.             } else {
  479.                 $key = $arrList[$cnt][$keyname];
  480.             }
  481.             $val = $arrList[$cnt][$valname];
  482.  
  483.             if($connect != "") {
  484.                 $arrRet[$key].= "$val".$connect;
  485.             } else {
  486.                 $arrRet[$key][] = $val;
  487.             }
  488.         }
  489.         return $arrRet;
  490.     }
  491.  
  492.     // 配列の値をカンマ区切りで返す。
  493.     function sfGetCommaList($array, $space=true, $arrPop = array()) {
  494.         if (count($array) > 0) {
  495.             $line = "";
  496.             foreach($array as $val) {
  497.                 if (!in_array($val, $arrPop)) {
  498.                     if ($space) {
  499.                         $line .= $val . ", ";
  500.                     } else {
  501.                         $line .= $val . ",";
  502.                     }
  503.                 }
  504.             }
  505.             if ($space) {
  506.                 $line = ereg_replace(", $", "", $line);
  507.             } else {
  508.                 $line = ereg_replace(",$", "", $line);
  509.             }
  510.             return $line;
  511.         } else {
  512.             return false;
  513.         }
  514.  
  515.     }
  516.  
  517.     /* 配列の要素をCSVフォーマットで出力する。*/
  518.     function sfGetCSVList($array) {
  519.         $line = "";
  520.         if (count($array) > 0) {
  521.             foreach($array as $key => $val) {
  522.                 $val = mb_convert_encoding($val, CHAR_CODE, CHAR_CODE);
  523.                 $line .= "\"".$val."\",";
  524.             }
  525.             $line = ereg_replace(",$", "\r\n", $line);
  526.         }else{
  527.             return false;
  528.         }
  529.         return $line;
  530.     }
  531.  
  532.     /* 配列の要素をPDFフォーマットで出力する。*/
  533.     function sfGetPDFList($array) {
  534.         foreach($array as $key => $val) {
  535.             $line .= "\t".$val;
  536.         }
  537.         $line.="\n";
  538.         return $line;
  539.     }
  540.  
  541.  
  542.  
  543.     /*-----------------------------------------------------------------*/
  544.     /*    check_set_term
  545.     /*    年月日に別れた2つの期間の妥当性をチェックし、整合性と期間を返す
  546.     /* 引数 (開始年,開始月,開始日,終了年,終了月,終了日)
  547.     /* 戻値 array(1,2,3)
  548.     /*          1.開始年月日 (YYYY/MM/DD 000000)
  549.     /*            2.終了年月日 (YYYY/MM/DD 235959)
  550.     /*            3.エラー ( 0 = OK, 1 = NG )
  551.     /*-----------------------------------------------------------------*/
  552.     function sfCheckSetTerm ( $start_year, $start_month, $start_day, $end_year, $end_month, $end_day ) {
  553.  
  554.         // 期間指定
  555.         $error = 0;
  556.         if ( $start_month || $start_day || $start_year){
  557.             if ( ! checkdate($start_month, $start_day , $start_year) ) $error = 1;
  558.         } else {
  559.             $error = 1;
  560.         }
  561.         if ( $end_month || $end_day || $end_year){
  562.             if ( ! checkdate($end_month ,$end_day ,$end_year) ) $error = 2;
  563.         }
  564.         if ( ! $error ){
  565.             $date1 = $start_year ."/".sprintf("%02d",$start_month) ."/".sprintf("%02d",$start_day) ." 000000";
  566.             $date2 = $end_year   ."/".sprintf("%02d",$end_month)   ."/".sprintf("%02d",$end_day)   ." 235959";
  567.             if ($date1 > $date2) $error = 3;
  568.         } else {
  569.             $error = 1;
  570.         }
  571.         return array($date1, $date2, $error);
  572.     }
  573.  
  574.     // エラー箇所の背景色を変更するためのfunction SC_Viewで読み込む
  575.     function sfSetErrorStyle(){
  576.         return 'style="background-color:'.ERR_COLOR.'"';
  577.     }
  578.  
  579.     /* DBに渡す数値のチェック
  580.      * 10桁以上はオーバーフローエラーを起こすので。
  581.      */
  582.     function sfCheckNumLength( $value ){
  583.         if ( ! is_numeric($value)  ){
  584.             return false;
  585.         }
  586.  
  587.         if ( strlen($value) > 9 ) {
  588.             return false;
  589.         }
  590.  
  591.         return true;
  592.     }
  593.  
  594.     // 一致した値のキー名を取得
  595.     function sfSearchKey($array, $word, $default) {
  596.         foreach($array as $key => $val) {
  597.             if($val == $word) {
  598.                 return $key;
  599.             }
  600.         }
  601.         return $default;
  602.     }
  603.  
  604.     function sfGetErrorColor($val) {
  605.         if($val != "") {
  606.             return "background-color:" . ERR_COLOR;
  607.         }
  608.         return "";
  609.     }
  610.  
  611.     function sfGetEnabled($val) {
  612.         if( ! $val ) {
  613.             return " disabled=\"disabled\"";
  614.         }
  615.         return "";
  616.     }
  617.  
  618.     function sfGetChecked($param, $value) {
  619.         if($param == $value) {
  620.             return "checked=\"checked\"";
  621.         }
  622.         return "";
  623.     }
  624.  
  625.     function sfTrim($str) {
  626.         $ret = ereg_replace("^[  \n\r]*", "", $str);
  627.         $ret = ereg_replace("[  \n\r]*$", "", $ret);
  628.         return $ret;
  629.     }
  630.  
  631.     /* 税金計算 */
  632.     function sfTax($price, $tax = null, $tax_rule = null) {
  633.         // 店舗基本情報を取得
  634.         static $CONF;
  635.         if (
  636.                is_null($CONF)
  637.             && (is_null($tax) || is_null($tax_rule))
  638.         ) {
  639.             $CONF = SC_Helper_DB_Ex::sf_getBasisData();
  640.         }
  641.         
  642.         if (is_null($tax)) {
  643.             $tax = $CONF['tax'];
  644.         }
  645.         
  646.         if (is_null($tax_rule)) {
  647.             $tax_rule = $CONF['tax_rule'];
  648.         }
  649.         
  650.         $real_tax = $tax / 100;
  651.         $ret = $price * $real_tax;
  652.         switch($tax_rule) {
  653.         // 四捨五入
  654.         case 1:
  655.             $ret = round($ret);
  656.             break;
  657.         // 切り捨て
  658.         case 2:
  659.             $ret = floor($ret);
  660.             break;
  661.         // 切り上げ
  662.         case 3:
  663.             $ret = ceil($ret);
  664.             break;
  665.         // デフォルト:切り上げ
  666.         default:
  667.             $ret = ceil($ret);
  668.             break;
  669.         }
  670.         return $ret;
  671.     }
  672.  
  673.     /* 税金付与 */
  674.     function sfPreTax($price, $tax = null, $tax_rule = null) {
  675.         return $price + SC_Utils_Ex::sfTax($price, $tax, $tax_rule);
  676.     }
  677.  
  678.     // 桁数を指定して四捨五入
  679.     function sfRound($value, $pow = 0){
  680.         $adjust = pow(10 ,$pow-1);
  681.  
  682.         // 整数且つ0出なければ桁数指定を行う
  683.         if(SC_Utils::sfIsInt($adjust) and $pow > 1){
  684.             $ret = (round($value * $adjust)/$adjust);
  685.         }
  686.  
  687.         $ret = round($ret);
  688.  
  689.         return $ret;
  690.     }
  691.  
  692.     /* ポイント付与 */
  693.     function sfPrePoint($price, $point_rate, $rule = POINT_RULE, $product_id = "") {
  694.         if(SC_Utils::sfIsInt($product_id)) {
  695.             $objQuery = new SC_Query();
  696.             $where = "now() >= cast(start_date as date) AND ";
  697.             $where .= "now() < cast(end_date as date) AND ";
  698.  
  699.             $where .= "del_flg = 0 AND campaign_id IN (SELECT campaign_id FROM dtb_campaign_detail where product_id = ? )";
  700.             //登録(更新)日付順
  701.             $objQuery->setorder('update_date DESC');
  702.             //キャンペーンポイントの取得
  703.             $arrRet = $objQuery->select("campaign_name, campaign_point_rate", "dtb_campaign", $where, array($product_id));
  704.         }
  705.         //複数のキャンペーンに登録されている商品は、最新のキャンペーンからポイントを取得
  706.         if(isset($arrRet[0]['campaign_point_rate'])
  707.            && $arrRet[0]['campaign_point_rate'] != "") {
  708.  
  709.             $campaign_point_rate = $arrRet[0]['campaign_point_rate'];
  710.             $real_point = $campaign_point_rate / 100;
  711.         } else {
  712.             $real_point = $point_rate / 100;
  713.         }
  714.         $ret = $price * $real_point;
  715.         switch($rule) {
  716.         // 四捨五入
  717.         case 1:
  718.             $ret = round($ret);
  719.             break;
  720.         // 切り捨て
  721.         case 2:
  722.             $ret = floor($ret);
  723.             break;
  724.         // 切り上げ
  725.         case 3:
  726.             $ret = ceil($ret);
  727.             break;
  728.         // デフォルト:切り上げ
  729.         default:
  730.             $ret = ceil($ret);
  731.             break;
  732.         }
  733.         //キャンペーン商品の場合
  734.         if(isset($campaign_point_rate) && $campaign_point_rate != "") {
  735.             $ret = "(".$arrRet[0]['campaign_name']."ポイント率".$campaign_point_rate."%)".$ret;
  736.         }
  737.         return $ret;
  738.     }
  739.  
  740.     /* 規格分類の件数取得 */
  741.     function sfGetClassCatCount() {
  742.         $sql = "select count(dtb_class.class_id) as count, dtb_class.class_id ";
  743.         $sql.= "from dtb_class inner join dtb_classcategory on dtb_class.class_id = dtb_classcategory.class_id ";
  744.         $sql.= "where dtb_class.del_flg = 0 AND dtb_classcategory.del_flg = 0 ";
  745.         $sql.= "group by dtb_class.class_id, dtb_class.name";
  746.         $objQuery = new SC_Query();
  747.         $arrList = $objQuery->getall($sql);
  748.         // キーと値をセットした配列を取得
  749.         $arrRet = SC_Utils::sfArrKeyValue($arrList, 'class_id', 'count');
  750.  
  751.         return $arrRet;
  752.     }
  753.  
  754.     /* 規格の登録 */
  755.     function sfInsertProductClass($objQuery, $arrList, $product_id , $product_class_id = "") {
  756.         // すでに規格登録があるかどうかをチェックする。
  757.         $where = "product_id = ? AND classcategory_id1 <> 0 AND classcategory_id1 <> 0";
  758.         $count = $objQuery->count("dtb_products_class", $where,  array($product_id));
  759.  
  760.         // すでに規格登録がない場合
  761.         if($count == 0) {
  762.             // 既存規格の削除
  763.             $where = "product_id = ?";
  764.             $objQuery->delete("dtb_products_class", $where, array($product_id));
  765.  
  766.             // 配列の添字を定義
  767.             $checkArray = array("product_code", "stock", "stock_unlimited", "price01", "price02");
  768.             $arrList = SC_Utils_Ex::arrayDefineIndexes($arrList, $checkArray);
  769.  
  770.             $sqlval['product_id'] = $product_id;
  771.             if(strlen($product_class_id ) > 0 ){
  772.                 $sqlval['product_class_id'] = $product_class_id;
  773.             }
  774.             $sqlval['classcategory_id1'] = '0';
  775.             $sqlval['classcategory_id2'] = '0';
  776.             $sqlval['product_code'] = $arrList["product_code"];
  777.             $sqlval['stock'] = $arrList["stock"];
  778.             $sqlval['stock_unlimited'] = ($arrList["stock_unlimited"]) ? '1' : '0';
  779.             $sqlval['price01'] = $arrList['price01'];
  780.             $sqlval['price02'] = $arrList['price02'];
  781.             $sqlval['creator_id'] = $_SESSION['member_id'];
  782.             $sqlval['create_date'] = "now()";
  783.  
  784.             if($_SESSION['member_id'] == "") {
  785.                 $sqlval['creator_id'] = '0';
  786.             }
  787.  
  788.             // INSERTの実行
  789.             $objQuery->insert("dtb_products_class", $sqlval);
  790.         }
  791.     }
  792.  
  793.     function sfGetProductClassId($product_id, $classcategory_id1, $classcategory_id2) {
  794.         $where = "product_id = ? AND classcategory_id1 = ? AND classcategory_id2 = ?";
  795.         $objQuery = new SC_Query();
  796.         $ret = $objQuery->get("dtb_products_class", "product_class_id", $where, Array($product_id, $classcategory_id1, $classcategory_id2));
  797.         return $ret;
  798.     }
  799.  
  800.     /* 文末の「/」をなくす */
  801.     function sfTrimURL($url) {
  802.         $ret = ereg_replace("[/]+$", "", $url);
  803.         return $ret;
  804.     }
  805.  
  806.     /* DBから取り出した日付の文字列を調整する。*/
  807.     function sfDispDBDate($dbdate, $time = true) {
  808.         list($y, $m, $d, $H, $M) = split("[- :]", $dbdate);
  809.  
  810.         if(strlen($y) > 0 && strlen($m) > 0 && strlen($d) > 0) {
  811.             if ($time) {
  812.                 $str = sprintf("%04d/%02d/%02d %02d:%02d", $y, $m, $d, $H, $M);
  813.             } else {
  814.                 $str = sprintf("%04d/%02d/%02d", $y, $m, $d, $H, $M);
  815.             }
  816.         } else {
  817.             $str = "";
  818.         }
  819.         return $str;
  820.     }
  821.  
  822.     /* 配列をキー名ごとの配列に変更する */
  823.     function sfSwapArray($array, $isColumnName = true) {
  824.         $arrRet = array();
  825.         $max = count($array);
  826.         for($i = 0; $i < $max; $i++) {
  827.             $j = 0;
  828.             foreach($array[$i] as $key => $val) {
  829.                 if ($isColumnName) {
  830.                     $arrRet[$key][] = $val;
  831.                 } else {
  832.                     $arrRet[$j][] = $val;
  833.                 }
  834.                 $j++;
  835.             }
  836.         }
  837.         return $arrRet;
  838.     }
  839.  
  840.     /**
  841.      * 連想配列から新たな配列を生成して返す.
  842.      *
  843.      * $requires が指定された場合, $requires に含まれるキーの値のみを返す.
  844.      *
  845.      * @param array 連想配列
  846.      * @param array 必須キーの配列
  847.      * @return array 連想配列の値のみの配列
  848.      */
  849.     function getHash2Array($hash, $requires = array()) {
  850.         $array = array();
  851.         $i = 0;
  852.         foreach ($hash as $key => $val) {
  853.             if (!empty($requires)) {
  854.                 if (in_array($key, $requires)) {
  855.                     $array[$i] = $val;
  856.                     $i++;
  857.                 }
  858.             } else {
  859.                 $array[$i] = $val;
  860.                 $i++;
  861.             }
  862.         }
  863.         return $array;
  864.     }
  865.  
  866.     /* かけ算をする(Smarty用) */
  867.     function sfMultiply($num1, $num2) {
  868.         return ($num1 * $num2);
  869.     }
  870.  
  871.     // カードの処理結果を返す
  872.     function sfGetAuthonlyResult($dir, $file_name, $name01, $name02, $card_no, $card_exp, $amount, $order_id, $jpo_info = "10"){
  873.  
  874.         $path = $dir .$file_name;        // cgiファイルのフルパス生成
  875.         $now_dir = getcwd();            // requireがうまくいかないので、cgi実行ディレクトリに移動する
  876.         chdir($dir);
  877.  
  878.         // パイプ渡しでコマンドラインからcgi起動
  879.         $cmd = "$path card_no=$card_no name01=$name01 name02=$name02 card_exp=$card_exp amount=$amount order_id=$order_id jpo_info=$jpo_info";
  880.  
  881.         $tmpResult = popen($cmd, "r");
  882.  
  883.         // 結果取得
  884.         while( ! FEOF ( $tmpResult ) ) {
  885.             $result .= FGETS($tmpResult);
  886.         }
  887.         pclose($tmpResult);                //     パイプを閉じる
  888.         chdir($now_dir);                // 元にいたディレクトリに帰る
  889.  
  890.         // 結果を連想配列へ格納
  891.         $result = ereg_replace("&$", "", $result);
  892.         foreach (explode("&",$result) as $data) {
  893.             list($key, $val) = explode("=", $data, 2);
  894.             $return[$key] = $val;
  895.         }
  896.  
  897.         return $return;
  898.     }
  899.  
  900.     /* 加算ポイントの計算式 */
  901.     function sfGetAddPoint($totalpoint, $use_point, $arrInfo) {
  902.         // 購入商品の合計ポイントから利用したポイントのポイント換算価値を引く方式
  903.         $add_point = $totalpoint - intval($use_point * ($arrInfo['point_rate'] / 100));
  904.  
  905.         if($add_point < 0) {
  906.             $add_point = '0';
  907.         }
  908.         return $add_point;
  909.     }
  910.  
  911.     /* 一意かつ予測されにくいID */
  912.     function sfGetUniqRandomId($head = "") {
  913.         // 予測されないようにランダム文字列を付与する。
  914.         $random = GC_Utils_Ex::gfMakePassword(8);
  915.         // 同一ホスト内で一意なIDを生成
  916.         $id = uniqid($head);
  917.         return ($id . $random);
  918.     }
  919.  
  920.     // カテゴリ別おすすめ商品の取得
  921.     function sfGetBestProducts( $conn, $category_id = 0){
  922.         $sql = '';
  923.         // 既に登録されている内容を取得する
  924.         $sql .= "SELECT name, main_image, main_list_image, price01_min, price01_max, price02_min, price02_max, point_rate,
  925.                  A.product_id, A.comment FROM dtb_best_products as A LEFT JOIN vw_products_allclass AS allcls
  926.                 USING (product_id) WHERE A.category_id = ? AND A.del_flg = 0 AND allcls.del_flg = 0 AND allcls.status = 1";
  927.         
  928.         // 在庫無し商品の非表示
  929.         if (NOSTOCK_HIDDEN === true) {
  930.             $sql .= ' AND (allcls.stock_max >= 1 OR allcls.stock_unlimited_max = 1)';
  931.         }
  932.         
  933.         $sql .= " ORDER BY A.rank";
  934.         $arrItems = $conn->getAll($sql, array($category_id));
  935.  
  936.         return $arrItems;
  937.     }
  938.  
  939.     // 特殊制御文字の手動エスケープ
  940.     function sfManualEscape($data) {
  941.         // 配列でない場合
  942.         if(!is_array($data)) {
  943.             if (DB_TYPE == "pgsql") {
  944.                 $ret = pg_escape_string($data);
  945.             }else if(DB_TYPE == "mysql"){
  946.                 $ret = mysql_real_escape_string($data);
  947.             }
  948.             $ret = ereg_replace("%", "\\%", $ret);
  949.             $ret = ereg_replace("_", "\\_", $ret);
  950.             return $ret;
  951.         }
  952.  
  953.         // 配列の場合
  954.         foreach($data as $val) {
  955.             if (DB_TYPE == "pgsql") {
  956.                 $ret = pg_escape_string($val);
  957.             }else if(DB_TYPE == "mysql"){
  958.                 $ret = mysql_real_escape_string($val);
  959.             }
  960.  
  961.             $ret = ereg_replace("%", "\\%", $ret);
  962.             $ret = ereg_replace("_", "\\_", $ret);
  963.             $arrRet[] = $ret;
  964.         }
  965.  
  966.         return $arrRet;
  967.     }
  968.  
  969.     /**
  970.      * ドメイン間で有効なセッションのスタート
  971.      * 共有SSL対応のための修正により、この関数は廃止します。
  972.      * セッションはrequire.phpを読み込んだ際に開始されます。
  973.      */
  974.     function sfDomainSessionStart() {
  975.         /**
  976.          * 2.1.1ベータからはSC_SessionFactory_UseCookie::initSession()で処理するため、
  977.          * ここでは何も処理しない
  978.          */
  979.         if (defined('SESSION_KEEP_METHOD')) {
  980.             return;
  981.         }
  982.  
  983.         if (session_id() === "") {
  984.  
  985.             session_set_cookie_params(0, "/", DOMAIN_NAME);
  986.  
  987.             if (!ini_get("session.auto_start")) {
  988.                 // セッション開始
  989.                 session_start();
  990.             }
  991.         }
  992.     }
  993.  
  994.     /* 文字列に強制的に改行を入れる */
  995.     function sfPutBR($str, $size) {
  996.         $i = 0;
  997.         $cnt = 0;
  998.         $line = array();
  999.         $ret = "";
  1000.  
  1001.         while($str[$i] != "") {
  1002.             $line[$cnt].=$str[$i];
  1003.             $i++;
  1004.             if(strlen($line[$cnt]) > $size) {
  1005.                 $line[$cnt].="<br />";
  1006.                 $cnt++;
  1007.             }
  1008.         }
  1009.  
  1010.         foreach($line as $val) {
  1011.             $ret.=$val;
  1012.         }
  1013.         return $ret;
  1014.     }
  1015.  
  1016.     // 二回以上繰り返されているスラッシュ[/]を一つに変換する。
  1017.     function sfRmDupSlash($istr){
  1018.         if(ereg("^http://", $istr)) {
  1019.             $str = substr($istr, 7);
  1020.             $head = "http://";
  1021.         } else if(ereg("^https://", $istr)) {
  1022.             $str = substr($istr, 8);
  1023.             $head = "https://";
  1024.         } else {
  1025.             $str = $istr;
  1026.         }
  1027.         $str = ereg_replace("[/]+", "/", $str);
  1028.         $ret = $head . $str;
  1029.         return $ret;
  1030.     }
  1031.  
  1032.     /**
  1033.      * テキストファイルの文字エンコーディングを変換する.
  1034.      *
  1035.      * $filepath に存在するテキストファイルの文字エンコーディングを変換する.
  1036.      * 変換前の文字エンコーディングは, mb_detect_order で設定した順序で自動検出する.
  1037.      * 変換後は, 変換前のファイル名に「enc_」というプレフィクスを付与し,
  1038.      * $out_dir で指定したディレクトリへ出力する
  1039.      *
  1040.      * TODO $filepath のファイルがバイナリだった場合の扱い
  1041.      * TODO fwrite などでのエラーハンドリング
  1042.      *
  1043.      * @access public
  1044.      * @param string $filepath 変換するテキストファイルのパス
  1045.      * @param string $enc_type 変換後のファイルエンコーディングの種類を表す文字列
  1046.      * @param string $out_dir 変換後のファイルを出力するディレクトリを表す文字列
  1047.      * @return string 変換後のテキストファイルのパス
  1048.      */
  1049.     function sfEncodeFile($filepath, $enc_type, $out_dir) {
  1050.         $ifp = fopen($filepath, "r");
  1051.  
  1052.         // 正常にファイルオープンした場合
  1053.         if ($ifp !== false) {
  1054.  
  1055.             $basename = basename($filepath);
  1056.             $outpath = $out_dir . "enc_" . $basename;
  1057.  
  1058.             $ofp = fopen($outpath, "w+");
  1059.  
  1060.             while(!feof($ifp)) {
  1061.                 $line = fgets($ifp);
  1062.                 $line = mb_convert_encoding($line, $enc_type, "auto");
  1063.                 fwrite($ofp,  $line);
  1064.             }
  1065.  
  1066.             fclose($ofp);
  1067.             fclose($ifp);
  1068.         }
  1069.         // ファイルが開けなかった場合はエラーページを表示
  1070.           else {
  1071.               SC_Utils::sfDispError('');
  1072.               exit;
  1073.         }
  1074.         return     $outpath;
  1075.     }
  1076.  
  1077.     function sfCutString($str, $len, $byte = true, $commadisp = true) {
  1078.         if($byte) {
  1079.             if(strlen($str) > ($len + 2)) {
  1080.                 $ret =substr($str, 0, $len);
  1081.                 $cut = substr($str, $len);
  1082.             } else {
  1083.                 $ret = $str;
  1084.                 $commadisp = false;
  1085.             }
  1086.         } else {
  1087.             if(mb_strlen($str) > ($len + 1)) {
  1088.                 $ret = mb_substr($str, 0, $len);
  1089.                 $cut = mb_substr($str, $len);
  1090.             } else {
  1091.                 $ret = $str;
  1092.                 $commadisp = false;
  1093.             }
  1094.         }
  1095.  
  1096.         // 絵文字タグの途中で分断されないようにする。
  1097.         if (isset($cut)) {
  1098.             // 分割位置より前の最後の [ 以降を取得する。
  1099.             $head = strrchr($ret, '[');
  1100.  
  1101.             // 分割位置より後の最初の ] 以前を取得する。
  1102.             $tail_pos = strpos($cut, ']');
  1103.             if ($tail_pos !== false) {
  1104.                 $tail = substr($cut, 0, $tail_pos + 1);
  1105.             }
  1106.  
  1107.             // 分割位置より前に [、後に ] が見つかった場合は、[ から ] までを
  1108.             // 接続して絵文字タグ1個分になるかどうかをチェックする。
  1109.             if ($head !== false && $tail_pos !== false) {
  1110.                 $subject = $head . $tail;
  1111.                 if (preg_match('/^\[emoji:e?\d+\]$/', $subject)) {
  1112.                     // 絵文字タグが見つかったので削除する。
  1113.                     $ret = substr($ret, 0, -strlen($head));
  1114.                 }
  1115.             }
  1116.         }
  1117.  
  1118.         if($commadisp){
  1119.             $ret = $ret . "...";
  1120.         }
  1121.         return $ret;
  1122.     }
  1123.  
  1124.     // 年、月、締め日から、先月の締め日+1、今月の締め日を求める。
  1125.     function sfTermMonth($year, $month, $close_day) {
  1126.         $end_year = $year;
  1127.         $end_month = $month;
  1128.  
  1129.         // 開始月が終了月と同じか否か
  1130.         $same_month = false;
  1131.  
  1132.         // 該当月の末日を求める。
  1133.         $end_last_day = date("d", mktime(0, 0, 0, $month + 1, 0, $year));
  1134.  
  1135.         // 月の末日が締め日より少ない場合
  1136.         if($end_last_day < $close_day) {
  1137.             // 締め日を月末日に合わせる
  1138.             $end_day = $end_last_day;
  1139.         } else {
  1140.             $end_day = $close_day;
  1141.         }
  1142.  
  1143.         // 前月の取得
  1144.         $tmp_year = date("Y", mktime(0, 0, 0, $month, 0, $year));
  1145.         $tmp_month = date("m", mktime(0, 0, 0, $month, 0, $year));
  1146.         // 前月の末日を求める。
  1147.         $start_last_day = date("d", mktime(0, 0, 0, $month, 0, $year));
  1148.  
  1149.         // 前月の末日が締め日より少ない場合
  1150.         if ($start_last_day < $close_day) {
  1151.             // 月末日に合わせる
  1152.             $tmp_day = $start_last_day;
  1153.         } else {
  1154.             $tmp_day = $close_day;
  1155.         }
  1156.  
  1157.         // 先月の末日の翌日を取得する
  1158.         $start_year = date("Y", mktime(0, 0, 0, $tmp_month, $tmp_day + 1, $tmp_year));
  1159.         $start_month = date("m", mktime(0, 0, 0, $tmp_month, $tmp_day + 1, $tmp_year));
  1160.         $start_day = date("d", mktime(0, 0, 0, $tmp_month, $tmp_day + 1, $tmp_year));
  1161.  
  1162.         // 日付の作成
  1163.         $start_date = sprintf("%d/%d/%d 00:00:00", $start_year, $start_month, $start_day);
  1164.         $end_date = sprintf("%d/%d/%d 23:59:59", $end_year, $end_month, $end_day);
  1165.  
  1166.         return array($start_date, $end_date);
  1167.     }
  1168.  
  1169.     // PDF用のRGBカラーを返す
  1170.     function sfGetPdfRgb($hexrgb) {
  1171.         $hex = substr($hexrgb, 0, 2);
  1172.         $r = hexdec($hex) / 255;
  1173.  
  1174.         $hex = substr($hexrgb, 2, 2);
  1175.         $g = hexdec($hex) / 255;
  1176.  
  1177.         $hex = substr($hexrgb, 4, 2);
  1178.         $b = hexdec($hex) / 255;
  1179.  
  1180.         return array($r, $g, $b);
  1181.     }
  1182.  
  1183.     //メルマガ仮登録とメール配信
  1184.     /*
  1185.      * FIXME
  1186.      */
  1187.     function sfRegistTmpMailData($mail_flag, $email){
  1188.         $objQuery = new SC_Query();
  1189.         $objConn = new SC_DBConn();
  1190.         $objPage = new LC_Page();
  1191.  
  1192.         $random_id = sfGetUniqRandomId();
  1193.         $arrRegistMailMagazine["mail_flag"] = $mail_flag;
  1194.         $arrRegistMailMagazine["email"] = $email;
  1195.         $arrRegistMailMagazine["temp_id"] =$random_id;
  1196.         $arrRegistMailMagazine["end_flag"]='0';
  1197.         $arrRegistMailMagazine["update_date"] = 'now()';
  1198.  
  1199.         //メルマガ仮登録用フラグ
  1200.         $flag = $objQuery->count("dtb_customer_mail_temp", "email=?", array($email));
  1201.         $objConn->query("BEGIN");
  1202.         switch ($flag){
  1203.             case '0':
  1204.             $objConn->autoExecute("dtb_customer_mail_temp",$arrRegistMailMagazine);
  1205.             break;
  1206.  
  1207.             case '1':
  1208.                 $objConn->autoExecute("dtb_customer_mail_temp",$arrRegistMailMagazine, "email = " .SC_Utils::sfQuoteSmart($email));
  1209.             break;
  1210.         }
  1211.         $objConn->query("COMMIT");
  1212.         $subject = 'メルマガ仮登録が完了しました';
  1213.         $objPage->tpl_url = SSL_URL."mailmagazine/regist.php?temp_id=".$arrRegistMailMagazine['temp_id'];
  1214.         switch ($mail_flag){
  1215.             case '1':
  1216.             $objPage->tpl_name = "登録";
  1217.             $objPage->tpl_kindname = "HTML";
  1218.             break;
  1219.  
  1220.             case '2':
  1221.             $objPage->tpl_name = "登録";
  1222.             $objPage->tpl_kindname = "テキスト";
  1223.             break;
  1224.  
  1225.             case '3':
  1226.             $objPage->tpl_name = "解除";
  1227.             break;
  1228.         }
  1229.         $objPage->tpl_email = $email;
  1230.         sfSendTplMail($email, $subject, 'mail_templates/mailmagazine_temp.tpl', $objPage);
  1231.     }
  1232.  
  1233.     // 再帰的に多段配列を検索して一次元配列(Hidden引渡し用配列)に変換する。
  1234.     function sfMakeHiddenArray($arrSrc, $arrDst = array(), $parent_key = "") {
  1235.         if(is_array($arrSrc)) {
  1236.             foreach($arrSrc as $key => $val) {
  1237.                 if($parent_key != "") {
  1238.                     $keyname = $parent_key . "[". $key . "]";
  1239.                 } else {
  1240.                     $keyname = $key;
  1241.                 }
  1242.                 if(is_array($val)) {
  1243.                     $arrDst = SC_Utils::sfMakeHiddenArray($val, $arrDst, $keyname);
  1244.                 } else {
  1245.                     $arrDst[$keyname] = $val;
  1246.                 }
  1247.             }
  1248.         }
  1249.         return $arrDst;
  1250.     }
  1251.  
  1252.     // DB取得日時をタイムに変換
  1253.     function sfDBDatetoTime($db_date) {
  1254.         $date = ereg_replace("\..*$","",$db_date);
  1255.         $time = strtotime($date);
  1256.         return $time;
  1257.     }
  1258.  
  1259.     /**
  1260.      * テンプレートを切り替えて出力する
  1261.      *
  1262.      * @deprecated 2008/04/02以降使用不可
  1263.      */
  1264.     function sfCustomDisplay(&$objPage, $is_mobile = false) {
  1265.         $basename = basename($_SERVER["REQUEST_URI"]);
  1266.  
  1267.         if($basename == "") {
  1268.             $path = $_SERVER["REQUEST_URI"] . DIR_INDEX_URL;
  1269.         } else {
  1270.             $path = $_SERVER["REQUEST_URI"];
  1271.         }
  1272.  
  1273.         if(isset($_GET['tpl']) && $_GET['tpl'] != "") {
  1274.             $tpl_name = $_GET['tpl'];
  1275.         } else {
  1276.             $tpl_name = ereg_replace("^/", "", $path);
  1277.             $tpl_name = ereg_replace("/", "_", $tpl_name);
  1278.             $tpl_name = ereg_replace("(\.php$|\.html$)", ".tpl", $tpl_name);
  1279.         }
  1280.  
  1281.         $template_path = TEMPLATE_FTP_DIR . $tpl_name;
  1282. echo $template_path;
  1283.         if($is_mobile === true) {
  1284.             $objView = new SC_MobileView();
  1285.             $objView->assignobj($objPage);
  1286.             $objView->display(SITE_FRAME);
  1287.         } else if(file_exists($template_path)) {
  1288.             $objView = new SC_UserView(TEMPLATE_FTP_DIR, COMPILE_FTP_DIR);
  1289.             $objView->assignobj($objPage);
  1290.             $objView->display($tpl_name);
  1291.         } else {
  1292.             $objView = new SC_SiteView();
  1293.             $objView->assignobj($objPage);
  1294.             $objView->display(SITE_FRAME);
  1295.         }
  1296.     }
  1297.  
  1298.     // PHPのmb_convert_encoding関数をSmartyでも使えるようにする
  1299.     function sf_mb_convert_encoding($str, $encode = 'CHAR_CODE') {
  1300.         return  mb_convert_encoding($str, $encode);
  1301.     }
  1302.  
  1303.     // PHPのmktime関数をSmartyでも使えるようにする
  1304.     function sf_mktime($format, $hour=0, $minute=0, $second=0, $month=1, $day=1, $year=1999) {
  1305.         return  date($format,mktime($hour, $minute, $second, $month, $day, $year));
  1306.     }
  1307.  
  1308.     // PHPのdate関数をSmartyでも使えるようにする
  1309.     function sf_date($format, $timestamp = '') {
  1310.         return  date( $format, $timestamp);
  1311.     }
  1312.  
  1313.     // チェックボックスの型を変換する
  1314.     function sfChangeCheckBox($data , $tpl = false){
  1315.         if ($tpl) {
  1316.             if ($data == 1){
  1317.                 return 'checked';
  1318.             }else{
  1319.                 return "";
  1320.             }
  1321.         }else{
  1322.             if ($data == "on"){
  1323.                 return 1;
  1324.             }else{
  1325.                 return 2;
  1326.             }
  1327.         }
  1328.     }
  1329.  
  1330.     // 2つの配列を用いて連想配列を作成する
  1331.     function sfarrCombine($arrKeys, $arrValues) {
  1332.  
  1333.         if(count($arrKeys) <= 0 and count($arrValues) <= 0) return array();
  1334.  
  1335.         $keys = array_values($arrKeys);
  1336.         $vals = array_values($arrValues);
  1337.  
  1338.         $max = max( count( $keys ), count( $vals ) );
  1339.         $combine_ary = array();
  1340.         for($i=0; $i<$max; $i++) {
  1341.             $combine_ary[$keys[$i]] = $vals[$i];
  1342.         }
  1343.         if(is_array($combine_ary)) return $combine_ary;
  1344.  
  1345.         return false;
  1346.     }
  1347.  
  1348.     /* 子ID所属する親IDを取得する */
  1349.     function sfGetParentsArraySub($arrData, $pid_name, $id_name, $child) {
  1350.         $max = count($arrData);
  1351.         $parent = "";
  1352.         for($i = 0; $i < $max; $i++) {
  1353.             if($arrData[$i][$id_name] == $child) {
  1354.                 $parent = $arrData[$i][$pid_name];
  1355.                 break;
  1356.             }
  1357.         }
  1358.         return $parent;
  1359.     }
  1360.  
  1361.     /* 階層構造のテーブルから与えられたIDの兄弟を取得する */
  1362.     function sfGetBrothersArray($arrData, $pid_name, $id_name, $arrPID) {
  1363.         $max = count($arrData);
  1364.  
  1365.         $arrBrothers = array();
  1366.         foreach($arrPID as $id) {
  1367.             // 親IDを検索する
  1368.             for($i = 0; $i < $max; $i++) {
  1369.                 if($arrData[$i][$id_name] == $id) {
  1370.                     $parent = $arrData[$i][$pid_name];
  1371.                     break;
  1372.                 }
  1373.             }
  1374.             // 兄弟IDを検索する
  1375.             for($i = 0; $i < $max; $i++) {
  1376.                 if($arrData[$i][$pid_name] == $parent) {
  1377.                     $arrBrothers[] = $arrData[$i][$id_name];
  1378.                 }
  1379.             }
  1380.         }
  1381.         return $arrBrothers;
  1382.     }
  1383.  
  1384.     /* 階層構造のテーブルから与えられたIDの直属の子を取得する */
  1385.     function sfGetUnderChildrenArray($arrData, $pid_name, $id_name, $parent) {
  1386.         $max = count($arrData);
  1387.  
  1388.         $arrChildren = array();
  1389.         // 子IDを検索する
  1390.         for($i = 0; $i < $max; $i++) {
  1391.             if($arrData[$i][$pid_name] == $parent) {
  1392.                 $arrChildren[] = $arrData[$i][$id_name];
  1393.             }
  1394.         }
  1395.         return $arrChildren;
  1396.     }
  1397.  
  1398.     // SQLシングルクォート対応
  1399.     function sfQuoteSmart($in){
  1400.  
  1401.         if (is_int($in) || is_double($in)) {
  1402.             return $in;
  1403.         } elseif (is_bool($in)) {
  1404.             return $in ? 1 : 0;
  1405.         } elseif (is_null($in)) {
  1406.             return 'NULL';
  1407.         } else {
  1408.             return "'" . str_replace("'", "''", $in) . "'";
  1409.         }
  1410.     }
  1411.  
  1412.     // ディレクトリを再帰的に生成する
  1413.     function sfMakeDir($path) {
  1414.         static $count = 0;
  1415.         $count++;  // 無限ループ回避
  1416.         $dir = dirname($path);
  1417.         if(ereg("^[/]$", $dir) || ereg("^[A-Z]:[\\]$", $dir) || $count > 256) {
  1418.             // ルートディレクトリで終了
  1419.             return;
  1420.         } else {
  1421.             if(is_writable(dirname($dir))) {
  1422.                 if(!file_exists($dir)) {
  1423.                     mkdir($dir);
  1424.                     GC_Utils::gfPrintLog("mkdir $dir");
  1425.                 }
  1426.             } else {
  1427.                 SC_Utils::sfMakeDir($dir);
  1428.                 if(is_writable(dirname($dir))) {
  1429.                     if(!file_exists($dir)) {
  1430.                         mkdir($dir);
  1431.                         GC_Utils::gfPrintLog("mkdir $dir");
  1432.                     }
  1433.                 }
  1434.            }
  1435.         }
  1436.         return;
  1437.     }
  1438.  
  1439.     // ディレクトリ以下のファイルを再帰的にコピー
  1440.     function sfCopyDir($src, $des, $mess = "", $override = false){
  1441.         if(!is_dir($src)){
  1442.             return false;
  1443.         }
  1444.  
  1445.         $oldmask = umask(0);
  1446.         $mod= stat($src);
  1447.  
  1448.         // ディレクトリがなければ作成する
  1449.         if(!file_exists($des)) {
  1450.             if(!mkdir($des, $mod[2])) {
  1451.                 print("path:" . $des);
  1452.             }
  1453.         }
  1454.  
  1455.         $fileArray=glob( $src."*" );
  1456.         if (is_array($fileArray)) {
  1457.             foreach( $fileArray as $key => $data_ ){
  1458.                 // CVS管理ファイルはコピーしない
  1459.                 if(ereg("/CVS/Entries", $data_)) {
  1460.                     break;
  1461.                 }
  1462.                 if(ereg("/CVS/Repository", $data_)) {
  1463.                     break;
  1464.                 }
  1465.                 if(ereg("/CVS/Root", $data_)) {
  1466.                     break;
  1467.                 }
  1468.  
  1469.                 mb_ereg("^(.*[\/])(.*)",$data_, $matches);
  1470.                 $data=$matches[2];
  1471.                 if( is_dir( $data_ ) ){
  1472.                     $mess = SC_Utils::sfCopyDir( $data_.'/', $des.$data.'/', $mess);
  1473.                 }else{
  1474.                     if(!$override && file_exists($des.$data)) {
  1475.                         $mess.= $des.$data . ":ファイルが存在します\n";
  1476.                     } else {
  1477.                         if(@copy( $data_, $des.$data)) {
  1478.                             $mess.= $des.$data . ":コピー成功\n";
  1479.                         } else {
  1480.                             $mess.= $des.$data . ":コピー失敗\n";
  1481.                         }
  1482.                     }
  1483.                     $mod=stat($data_ );
  1484.                 }
  1485.             }
  1486.         }
  1487.         umask($oldmask);
  1488.         return $mess;
  1489.     }
  1490.  
  1491.     // 指定したフォルダ内のファイルを全て削除する
  1492.     function sfDelFile($dir){
  1493.         if(file_exists($dir)) {
  1494.             $dh = opendir($dir);
  1495.             // フォルダ内のファイルを削除
  1496.             while($file = readdir($dh)){
  1497.                 if ($file == "." or $file == "..") continue;
  1498.                 $del_file = $dir . "/" . $file;
  1499.                 if(is_file($del_file)){
  1500.                     $ret = unlink($dir . "/" . $file);
  1501.                 }else if (is_dir($del_file)){
  1502.                     $ret = SC_Utils::sfDelFile($del_file);
  1503.                 }
  1504.  
  1505.                 if(!$ret){
  1506.                     return $ret;
  1507.                 }
  1508.             }
  1509.  
  1510.             // 閉じる
  1511.             closedir($dh);
  1512.  
  1513.             // フォルダを削除
  1514.             return rmdir($dir);
  1515.         }
  1516.     }
  1517.  
  1518.     /*
  1519.      * 関数名:sfWriteFile
  1520.      * 引数1 :書き込むデータ
  1521.      * 引数2 :ファイルパス
  1522.      * 引数3 :書き込みタイプ
  1523.      * 引数4 :パーミッション
  1524.      * 戻り値:結果フラグ 成功なら true 失敗なら false
  1525.      * 説明 :ファイル書き出し
  1526.      */
  1527.     function sfWriteFile($str, $path, $type, $permission = "") {
  1528.         //ファイルを開く
  1529.         if (!($file = fopen ($path, $type))) {
  1530.             return false;
  1531.         }
  1532.  
  1533.         //ファイルロック
  1534.         flock ($file, LOCK_EX);
  1535.         //ファイルの書き込み
  1536.         fputs ($file, $str);
  1537.         //ファイルロックの解除
  1538.         flock ($file, LOCK_UN);
  1539.         //ファイルを閉じる
  1540.         fclose ($file);
  1541.         // 権限を指定
  1542.         if($permission != "") {
  1543.             chmod($path, $permission);
  1544.         }
  1545.  
  1546.         return true;
  1547.     }
  1548.  
  1549.     /**
  1550.      * ブラウザに強制的に送出する
  1551.      *
  1552.      * @param boolean|string$output 半角スペース256文字+改行を出力するか。または、送信する文字列を指定。
  1553.      * @return void 
  1554.      */
  1555.     function sfFlush($output = false, $sleep = 0){
  1556.         // 出力をバッファリングしない(==日本語自動変換もしない)
  1557.         while (@ob_end_flush());
  1558.  
  1559.         if ($output === true) {
  1560.             // IEのために半角スペース256文字+改行を出力
  1561.             //echo str_repeat(' ', 256) . "\n";
  1562.             echo str_pad('', 256) . "\n";
  1563.         } else if ($output !== false) {
  1564.             echo $output;
  1565.         }
  1566.  
  1567.         // 出力をフラッシュする
  1568.         flush();
  1569.  
  1570.         ob_start();
  1571.  
  1572.         // 時間のかかる処理
  1573.         sleep($sleep);
  1574.     }
  1575.  
  1576.     // @versionの記載があるファイルからバージョンを取得する。
  1577.     function sfGetFileVersion($path) {
  1578.         if(file_exists($path)) {
  1579.             $src_fp = fopen($path, "rb");
  1580.             if($src_fp) {
  1581.                 while (!feof($src_fp)) {
  1582.                     $line = fgets($src_fp);
  1583.                     if(ereg("@version", $line)) {
  1584.                         $arrLine = split(" ", $line);
  1585.                         $version = $arrLine[5];
  1586.                     }
  1587.                 }
  1588.                 fclose($src_fp);
  1589.             }
  1590.         }
  1591.         return $version;
  1592.     }
  1593.  
  1594.     // 指定したURLに対してPOSTでデータを送信する
  1595.     function sfSendPostData($url, $arrData, $arrOkCode = array()){
  1596.         require_once(DATA_PATH . "module/Request.php");
  1597.  
  1598.         // 送信インスタンス生成
  1599.         $req = new HTTP_Request($url);
  1600.  
  1601.         $req->addHeader('User-Agent', 'DoCoMo/2.0 P2101V(c100)');
  1602.         $req->setMethod(HTTP_REQUEST_METHOD_POST);
  1603.  
  1604.         // POSTデータ送信
  1605.         $req->addPostDataArray($arrData);
  1606.  
  1607.         // エラーが無ければ、応答情報を取得する
  1608.         if (!PEAR::isError($req->sendRequest())) {
  1609.  
  1610.             // レスポンスコードがエラー判定なら、空を返す
  1611.             $res_code = $req->getResponseCode();
  1612.  
  1613.             if(!in_array($res_code, $arrOkCode)){
  1614.                 $response = "";
  1615.             }else{
  1616.                 $response = $req->getResponseBody();
  1617.             }
  1618.  
  1619.         } else {
  1620.             $response = "";
  1621.         }
  1622.  
  1623.         // POSTデータクリア
  1624.         $req->clearPostData();
  1625.  
  1626.         return $response;
  1627.     }
  1628.  
  1629.     /**
  1630.      * $array の要素を $arrConvList で指定した方式で mb_convert_kana を適用する.
  1631.      *
  1632.      * @param array $array 変換する文字列の配列
  1633.      * @param array $arrConvList mb_convert_kana の適用ルール
  1634.      * @return array 変換後の配列
  1635.      * @see mb_convert_kana
  1636.      */
  1637.     function mbConvertKanaWithArray($array, $arrConvList) {
  1638.         foreach ($arrConvList as $key => $val) {
  1639.             if(isset($array[$key])) {
  1640.                 $array[$key] = mb_convert_kana($array[$key] ,$val);
  1641.             }
  1642.         }
  1643.         return $array;
  1644.     }
  1645.  
  1646.     /**
  1647.      * 配列の添字が未定義の場合は空文字を代入して定義する.
  1648.      *
  1649.      * @param array $array 添字をチェックする配列
  1650.      * @param array $defineIndexes チェックする添字
  1651.      * @return array 添字を定義した配列
  1652.      */
  1653.     function arrayDefineIndexes($array, $defineIndexes) {
  1654.         foreach ($defineIndexes as $key) {
  1655.             if (!isset($array[$key])) $array[$key] = "";
  1656.         }
  1657.         return $array;
  1658.     }
  1659.  
  1660.     /**
  1661.      * XML宣言を出力する.
  1662.      *
  1663.      * XML宣言があると問題が発生する UA は出力しない.
  1664.      *
  1665.      * @return string XML宣言の文字列
  1666.      */
  1667.     function printXMLDeclaration() {
  1668.         $ua = $_SERVER['HTTP_USER_AGENT'];
  1669.         if (!preg_match("/MSIE/", $ua) || preg_match("/MSIE 7/", $ua)) {
  1670.             print("<?xml version='1.0' encoding='" . CHAR_CODE . "'?>\n");
  1671.         }
  1672.     }
  1673.  
  1674.     /*
  1675.      * 関数名:sfGetFileList()
  1676.      * 説明 :指定パス配下のディレクトリ取得
  1677.      * 引数1 :取得するディレクトリパス
  1678.      */
  1679.     function sfGetFileList($dir) {
  1680.         $arrFileList = array();
  1681.         $arrDirList = array();
  1682.  
  1683.         if (is_dir($dir)) {
  1684.             if ($dh = opendir($dir)) {
  1685.                 $cnt = 0;
  1686.                 // 行末の/を取り除く
  1687.                 while (($file = readdir($dh)) !== false) $arrDir[] = $file;
  1688.                 $dir = ereg_replace("/$", "", $dir);
  1689.                 // アルファベットと数字でソート
  1690.                 natcasesort($arrDir);
  1691.                 foreach($arrDir as $file) {
  1692.                     // ./ と ../を除くファイルのみを取得
  1693.                     if($file != "." && $file != "..") {
  1694.  
  1695.                         $path = $dir."/".$file;
  1696.                         // SELECT内の見た目を整えるため指定文字数で切る
  1697.                         $file_name = SC_Utils::sfCutString($file, FILE_NAME_LEN);
  1698.                         $file_size = SC_Utils::sfCutString(SC_Utils::sfGetDirSize($path), FILE_NAME_LEN);
  1699.                         $file_time = date("Y/m/d", filemtime($path));
  1700.  
  1701.                         // ディレクトリとファイルで格納配列を変える
  1702.                         if(is_dir($path)) {
  1703.                             $arrDirList[$cnt]['file_name'] = $file;
  1704.                             $arrDirList[$cnt]['file_path'] = $path;
  1705.                             $arrDirList[$cnt]['file_size'] = $file_size;
  1706.                             $arrDirList[$cnt]['file_time'] = $file_time;
  1707.                             $arrDirList[$cnt]['is_dir'] = true;
  1708.                         } else {
  1709.                             $arrFileList[$cnt]['file_name'] = $file;
  1710.                             $arrFileList[$cnt]['file_path'] = $path;
  1711.                             $arrFileList[$cnt]['file_size'] = $file_size;
  1712.                             $arrFileList[$cnt]['file_time'] = $file_time;
  1713.                             $arrFileList[$cnt]['is_dir'] = false;
  1714.                         }
  1715.                         $cnt++;
  1716.                     }
  1717.                 }
  1718.                 closedir($dh);
  1719.             }
  1720.         }
  1721.  
  1722.         // フォルダを先頭にしてマージ
  1723.         return array_merge($arrDirList, $arrFileList);
  1724.     }
  1725.  
  1726.     /*
  1727.      * 関数名:sfGetDirSize()
  1728.      * 説明 :指定したディレクトリのバイト数を取得
  1729.      * 引数1 :ディレクトリ
  1730.      */
  1731.     function sfGetDirSize($dir) {
  1732.         if(file_exists($dir)) {
  1733.             // ディレクトリの場合下層ファイルの総量を取得
  1734.             if (is_dir($dir)) {
  1735.                 $handle = opendir($dir);
  1736.                 while ($file = readdir($handle)) {
  1737.                     // 行末の/を取り除く
  1738.                     $dir = ereg_replace("/$", "", $dir);
  1739.                     $path = $dir."/".$file;
  1740.                     if ($file != '..' && $file != '.' && !is_dir($path)) {
  1741.                         $bytes += filesize($path);
  1742.                     } else if (is_dir($path) && $file != '..' && $file != '.') {
  1743.                         // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。
  1744.                         $bytes += SC_Utils::sfGetDirSize($path);
  1745.                     }
  1746.                 }
  1747.             } else {
  1748.                 // ファイルの場合
  1749.                 $bytes = filesize($dir);
  1750.             }
  1751.         }
  1752.         // ディレクトリ(ファイル)が存在しない場合は0byteを返す
  1753.         if($bytes == "") $bytes = 0;
  1754.  
  1755.         return $bytes;
  1756.     }
  1757.  
  1758.     /*
  1759.      * 関数名:sfDeleteDir()
  1760.      * 説明 :指定したディレクトリを削除
  1761.      * 引数1 :削除ファイル
  1762.      */
  1763.     function sfDeleteDir($dir) {
  1764.         $arrResult = array();
  1765.         if(file_exists($dir)) {
  1766.             // ディレクトリかチェック
  1767.             if (is_dir($dir)) {
  1768.                 if ($handle = opendir("$dir")) {
  1769.                     $cnt = 0;
  1770.                     while (false !== ($item = readdir($handle))) {
  1771.                         if ($item != "." && $item != "..") {
  1772.                             if (is_dir("$dir/$item")) {
  1773.                                 sfDeleteDir("$dir/$item");
  1774.                             } else {
  1775.                                 $arrResult[$cnt]['result'] = @unlink("$dir/$item");
  1776.                                 $arrResult[$cnt]['file_name'] = "$dir/$item";
  1777.                             }
  1778.                         }
  1779.                         $cnt++;
  1780.                     }
  1781.                 }
  1782.                 closedir($handle);
  1783.                 $arrResult[$cnt]['result'] = @rmdir($dir);
  1784.                 $arrResult[$cnt]['file_name'] = "$dir/$item";
  1785.             } else {
  1786.                 // ファイル削除
  1787.                 $arrResult[0]['result'] = @unlink("$dir");
  1788.                 $arrResult[0]['file_name'] = "$dir";
  1789.             }
  1790.         }
  1791.  
  1792.         return $arrResult;
  1793.     }
  1794.  
  1795.     /*
  1796.      * 関数名:sfGetFileTree()
  1797.      * 説明 :ツリー生成用配列取得(javascriptに渡す用)
  1798.      * 引数1 :ディレクトリ
  1799.      * 引数2 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納
  1800.      */
  1801.     function sfGetFileTree($dir, $tree_status) {
  1802.  
  1803.         $cnt = 0;
  1804.         $arrTree = array();
  1805.         $default_rank = count(split('/', $dir));
  1806.  
  1807.         // 文末の/を取り除く
  1808.         $dir = ereg_replace("/$", "", $dir);
  1809.         // 最上位層を格納(user_data/)
  1810.         if(sfDirChildExists($dir)) {
  1811.             $arrTree[$cnt]['type'] = "_parent";
  1812.         } else {
  1813.             $arrTree[$cnt]['type'] = "_child";
  1814.         }
  1815.         $arrTree[$cnt]['path'] = $dir;
  1816.         $arrTree[$cnt]['rank'] = 0;
  1817.         $arrTree[$cnt]['count'] = $cnt;
  1818.         // 初期表示はオープン
  1819.         if($_POST['mode'] != '') {
  1820.             $arrTree[$cnt]['open'] = lfIsFileOpen($dir, $tree_status);
  1821.         } else {
  1822.             $arrTree[$cnt]['open'] = true;
  1823.         }
  1824.         $cnt++;
  1825.  
  1826.         sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status);
  1827.  
  1828.         return $arrTree;
  1829.     }
  1830.  
  1831.     /*
  1832.      * 関数名:sfGetFileTree()
  1833.      * 説明 :ツリー生成用配列取得(javascriptに渡す用)
  1834.      * 引数1 :ディレクトリ
  1835.      * 引数2 :デフォルトの階層(/区切りで 0,1,2・・・とカウント)
  1836.      * 引数3 :連番
  1837.      * 引数4 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納
  1838.      */
  1839.     function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) {
  1840.  
  1841.         if(file_exists($dir)) {
  1842.             if ($handle = opendir("$dir")) {
  1843.                 while (false !== ($item = readdir($handle))) $arrDir[] = $item;
  1844.                 // アルファベットと数字でソート
  1845.                 natcasesort($arrDir);
  1846.                 foreach($arrDir as $item) {
  1847.                     if ($item != "." && $item != "..") {
  1848.                         // 文末の/を取り除く
  1849.                         $dir = ereg_replace("/$", "", $dir);
  1850.                         $path = $dir."/".$item;
  1851.                         // ディレクトリのみ取得
  1852.                         if (is_dir($path)) {
  1853.                             $arrTree[$cnt]['path'] = $path;
  1854.                             if(sfDirChildExists($path)) {
  1855.                                 $arrTree[$cnt]['type'] = "_parent";
  1856.                             } else {
  1857.                                 $arrTree[$cnt]['type'] = "_child";
  1858.                             }
  1859.  
  1860.                             // 階層を割り出す
  1861.                             $arrCnt = split('/', $path);
  1862.                             $rank = count($arrCnt);
  1863.                             $arrTree[$cnt]['rank'] = $rank - $default_rank + 1;
  1864.                             $arrTree[$cnt]['count'] = $cnt;
  1865.                             // フォルダが開いているか
  1866.                             $arrTree[$cnt]['open'] = lfIsFileOpen($path, $tree_status);
  1867.                             $cnt++;
  1868.                             // 下層ディレクトリ取得の為、再帰的に呼び出す
  1869.                             sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status);
  1870.                         }
  1871.                     }
  1872.                 }
  1873.             }
  1874.             closedir($handle);
  1875.         }
  1876.     }
  1877.  
  1878.     /*
  1879.      * 関数名:sfDirChildExists()
  1880.      * 説明 :指定したディレクトリ配下にファイルがあるか
  1881.      * 引数1 :ディレクトリ
  1882.      */
  1883.     function sfDirChildExists($dir) {
  1884.         if(file_exists($dir)) {
  1885.             if (is_dir($dir)) {
  1886.                 $handle = opendir($dir);
  1887.                 while ($file = readdir($handle)) {
  1888.                     // 行末の/を取り除く
  1889.                     $dir = ereg_replace("/$", "", $dir);
  1890.                     $path = $dir."/".$file;
  1891.                     if ($file != '..' && $file != '.' && is_dir($path)) {
  1892.                         return true;
  1893.                     }
  1894.                 }
  1895.             }
  1896.         }
  1897.  
  1898.         return false;
  1899.     }
  1900.  
  1901.     /*
  1902.      * 関数名:lfIsFileOpen()
  1903.      * 説明 :指定したファイルが前回開かれた状態にあったかチェック
  1904.      * 引数1 :ディレクトリ
  1905.      * 引数2 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納
  1906.      */
  1907.     function lfIsFileOpen($dir, $tree_status) {
  1908.         $arrTreeStatus = split('\|', $tree_status);
  1909.         if(in_array($dir, $arrTreeStatus)) {
  1910.             return true;
  1911.         }
  1912.  
  1913.         return false;
  1914.     }
  1915.  
  1916.     /*
  1917.      * 関数名:sfDownloadFile()
  1918.      * 引数1 :ファイルパス
  1919.      * 説明 :ファイルのダウンロード
  1920.      */
  1921.     function sfDownloadFile($file) {
  1922.          // ファイルの場合はダウンロードさせる
  1923.         Header("Content-disposition: attachment; filename=".basename($file));
  1924.         Header("Content-type: application/octet-stream; name=".basename($file));
  1925.         Header("Cache-Control: ");
  1926.         Header("Pragma: ");
  1927.         echo (sfReadFile($file));
  1928.     }
  1929.  
  1930.     /*
  1931.      * 関数名:sfCreateFile()
  1932.      * 引数1 :ファイルパス
  1933.      * 引数2 :パーミッション
  1934.      * 説明 :ファイル作成
  1935.      */
  1936.     function sfCreateFile($file, $mode = "") {
  1937.         // 行末の/を取り除く
  1938.         if($mode != "") {
  1939.             $ret = @mkdir($file, $mode);
  1940.         } else {
  1941.             $ret = @mkdir($file);
  1942.         }
  1943.  
  1944.         return $ret;
  1945.     }
  1946.  
  1947.     /*
  1948.      * 関数名:sfReadFile()
  1949.      * 引数1 :ファイルパス
  1950.      * 説明 :ファイル読込
  1951.      */
  1952.     function sfReadFile($filename) {
  1953.         $str = "";
  1954.         // バイナリモードでオープン
  1955.         $fp = @fopen($filename, "rb" );
  1956.         //ファイル内容を全て変数に読み込む
  1957.         if($fp) {
  1958.             $str = @fread($fp, filesize($filename)+1);
  1959.         }
  1960.         @fclose($fp);
  1961.  
  1962.         return $str;
  1963.     }
  1964.  
  1965.    /**
  1966.      * CSV出力用データ取得
  1967.      *
  1968.      * @return string 
  1969.      */
  1970.     function getCSVData($array, $arrayIndex) {
  1971.         for ($i = 0; $i < count($array); $i++){
  1972.             // インデックスが設定されている場合
  1973.             if (is_array($arrayIndex) && 0 < count($arrayIndex)){
  1974.                 for ($j = 0; $j < count($arrayIndex); $j++ ){
  1975.                     if ( $j > 0 ) $return .= ",";
  1976.                     $return .= "\"";
  1977.                     $return .= mb_ereg_replace("<","<",mb_ereg_replace( "\"","\"\"",$array[$i][$arrayIndex[$j]] )) ."\"";
  1978.                 }
  1979.             } else {
  1980.                 for ($j = 0; $j < count($array[$i]); $j++ ){
  1981.                     if ( $j > 0 ) $return .= ",";
  1982.                     $return .= "\"";
  1983.                     $return .= mb_ereg_replace("<","<",mb_ereg_replace( "\"","\"\"",$array[$i][$j] )) ."\"";
  1984.                 }
  1985.             }
  1986.             $return .= "\n";
  1987.         }
  1988.         return $return;
  1989.     }
  1990.  
  1991.    /**
  1992.      * 配列をテーブルタグで出力する。
  1993.      *
  1994.      * @return string 
  1995.      */
  1996.     function getTableTag($array) {
  1997.         $html = "<table>";
  1998.         $html.= "<tr>";
  1999.         foreach($array[0] as $key => $val) {
  2000.             $html.="<th>$key</th>";
  2001.         }
  2002.         $html.= "</tr>";
  2003.  
  2004.         $cnt = count($array);
  2005.  
  2006.         for($i = 0; $i < $cnt; $i++) {
  2007.             $html.= "<tr>";
  2008.             foreach($array[$i] as $val) {
  2009.                 $html.="<td>$val</td>";
  2010.             }
  2011.             $html.= "</tr>";
  2012.         }
  2013.         return $html;
  2014.     }
  2015.  
  2016.    /**
  2017.      * 一覧-メイン画像のファイル指定がない場合、専用の画像ファイルに書き換える。
  2018.      *
  2019.      * @param string &$filename ファイル名
  2020.      * @return string 
  2021.      */
  2022.     function sfNoImageMainList($filename = '') {
  2023.         if (strlen($filename) == 0 || substr($filename, -1, 1) == '/') {
  2024.             $filename .= 'noimage_main_list.jpg';
  2025.         }
  2026.         return $filename;
  2027.     }
  2028.  
  2029.    /**
  2030.      * 詳細-メイン画像のファイル指定がない場合、専用の画像ファイルに書き換える。
  2031.      *
  2032.      * @param string &$filename ファイル名
  2033.      * @return string 
  2034.      */
  2035.     function sfNoImageMain($filename = '') {
  2036.         if (strlen($filename) == 0 || substr($filename, -1, 1) == '/') {
  2037.             $filename .= 'noimage_main.png';
  2038.         }
  2039.         return $filename;
  2040.     }
  2041.  
  2042.     /* デバッグ用 ------------------------------------------------------------------------------------------------*/
  2043.     function sfPrintR($obj) {
  2044.         print("<div style='font-size: 12px;color: #00FF00;'>\n");
  2045.         print("<strong>**デバッグ中**</strong><br />\n");
  2046.         print("<pre>\n");
  2047.         //print_r($obj);
  2048.         var_dump($obj);
  2049.         print("</pre>\n");
  2050.         print("<strong>**デバッグ中**</strong></div>\n");
  2051.     }
  2052. }
  2053. ?>

Documentation generated on Tue, 28 Apr 2009 18:13:47 +0900 by phpDocumentor 1.4.2