source: branches/comu-euc/data/lib/glib.php @ 16927

Revision 16927, 7.2 KB checked in by adachi, 18 years ago (diff)

#213 gfDebugLog()修正

Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8/*----------------------------------------------------------------------
9 * [̾¾Î] gfDownloadCsv
10 * [³µÍ×] °ú¿ô¥Ç¡¼¥¿¤òCSV¤È¤·¤Æ¡¢¥¯¥é¥¤¥¢¥ó¥È¤Ë¥À¥¦¥ó¥í¡¼¥É¤µ¤»¤ë
11 * [°ú¿ô] 1:¥Ø¥Ã¥Àʸ»úÎó 2:CSV¥Ç¡¼¥¿
12 * [ÌáÃÍ] -
13 * [°Í¸] -
14 * [Ãí¼á] °ú¿ô¤Ï£±¡¤£²¤È¤â¥«¥ó¥Þ¶èÀÚ¤ê¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤³¤È
15 *----------------------------------------------------------------------*/
16function gfDownloadCsv($header, $contents){
17
18    $fiest_name = date("YmdHis") .".csv";
19
20    /* HTTP¥Ø¥Ã¥À¤Î½ÐÎÏ */
21    Header("Content-disposition: attachment; filename=${fiest_name}");
22    Header("Content-type: application/octet-stream; name=${fiest_name}");
23
24    $return = $header.$contents;
25    if (mb_detect_encoding($return) == CHAR_CODE){                      //ʸ»ú¥³¡¼¥ÉÊÑ´¹
26        $return = mb_convert_encoding($return,'SJIS',CHAR_CODE);
27        $return = str_replace( array( "\r\n", "\r" ), "\n", $return);   // ²þ¹ÔÊýË¡¤ÎÅý°ì
28    }
29    echo $return;
30}
31
32/*----------------------------------------------------------------------
33 * [̾¾Î] gfSetCsv
34 * [³µÍ×] °ú¿ô¤ÎÇÛÎó¤òCSV·Á¼°¤ËÊÑ´¹¤¹¤ë
35 * [°ú¿ô] 1:CSV¤Ë¤¹¤ëÇÛÎó 2:°ú¿ô1¤¬Ï¢ÁÛÇÛÎó»þ¤Îꤍ»ú¤ò»ØÄꤷ¤¿ÇÛÎó
36 * [ÌáÃÍ] CSV¥Ç¡¼¥¿
37 * [°Í¸] -
38 * [Ãí¼á] -
39 *----------------------------------------------------------------------*/
40function gfSetCsv( $array, $arrayIndex = "" ){
41    //°ú¿ô$arrayIndex¤Ï¡¢$array¤¬Ï¢ÁÛÇÛÎó¤Î¤È¤­¤Ëꤍ»ú¤ò»ØÄꤷ¤Æ¤ä¤ë¤¿¤á¤Ë»ÈÍѤ¹¤ë
42
43    $return = "";
44    for ($i=0; $i<count($array); $i++){
45
46        for ($j=0; $j<count($array[$i]); $j++ ){
47            if ( $j > 0 ) $return .= ",";
48            $return .= "\"";
49            if ( $arrayIndex ){
50                $return .= mb_ereg_replace("<","¡ã",mb_ereg_replace( "\"","\"\"",$array[$i][$arrayIndex[$j]] )) ."\"";
51            } else {
52                $return .= mb_ereg_replace("<","¡ã",mb_ereg_replace( "\"","\"\"",$array[$i][$j] )) ."\"";
53            }
54        }
55        $return .= "\n";
56    }
57
58    return $return;
59}
60
61/*----------------------------------------------------------------------
62 * [̾¾Î] gfGetAge
63 * [³µÍ×] ÆüÉÕ¤è¤êǯÎð¤ò·×»»¤¹¤ë¡£
64 * [°ú¿ô] 1:ÆüÉÕʸ»úÎó(yyyy/mm/dd¡¢yyyy-mm-dd hh:mm:ssÅù)
65 * [ÌáÃÍ] ǯÎð¤Î¿ôÃÍ
66 * [°Í¸] -
67 * [Ãí¼á] -
68 *----------------------------------------------------------------------*/
69function gfGetAge($dbdate)
70{
71    $ty = date("Y");
72    $tm = date("m");
73    $td = date("d");
74    list($by, $bm, $bd) = split("[-/ ]", $dbdate);
75    $age = $ty - $by;
76    if($tm * 100 + $td < $bm * 100 + $bd) $age--;
77    return $age;
78}
79
80/*----------------------------------------------------------------------
81 * [̾¾Î] gfDebugLog
82 * [³µÍ×] ¥í¥°¥Õ¥¡¥¤¥ë¤ËÊÑ¿ô¤Î¾ÜºÙ¤ò½ÐÎϤ¹¤ë¡£
83 * [°ú¿ô] ÂоݤȤʤëÊÑ¿ô
84 * [ÌáÃÍ] ¤Ê¤·
85 * [°Í¸] gfPrintLog
86 * [Ãí¼á] -
87 *----------------------------------------------------------------------*/
88function gfDebugLog($obj, $path = DEBUG_LOG_PATH){
89        $debugStr = print_r($obj, true);
90        gfPrintLog("*** start Debug ***", $path);
91        gfPrintLog($debugStr, $path);
92        gfPrintLog("*** end Debug ***", $path);
93
94        // ¥í¥°¥Æ¡¼¥·¥ç¥ó
95        gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
96}
97
98/*----------------------------------------------------------------------
99 * [̾¾Î] gfPrintLog
100 * [³µÍ×] ¥í¥°¥Õ¥¡¥¤¥ë¤ËÆü»þ¡¢½èÍý¥Õ¥¡¥¤¥ë̾¡¢¥á¥Ã¥»¡¼¥¸¤ò½ÐÎÏ
101 * [°ú¿ô] ɽ¼¨¤·¤¿¤¤¥á¥Ã¥»¡¼¥¸
102 * [ÌáÃÍ] ¤Ê¤·
103 * [°Í¸] ¤Ê¤·
104 * [Ãí¼á] -
105 *----------------------------------------------------------------------*/
106function gfPrintLog($mess, $path = '') {
107    // ÆüÉդμèÆÀ
108    $today = date("Y/m/d H:i:s");
109    // ½ÐÎϥѥ¹¤ÎºîÀ®
110    if ($path == "") {
111        $path = LOG_PATH;
112    }
113
114    // ¥¨¥¹¥±¡¼¥×¤µ¤ì¤Æ¤¤¤ëʸ»ú¤ò¤â¤È¤ËÌ᤹
115    $trans_tbl = get_html_translation_table (HTML_ENTITIES);
116    $trans_tbl = array_flip ($trans_tbl);
117    $mess = strtr($mess, $trans_tbl);
118
119    $fp = fopen($path, "a+");
120    if($fp) {
121        fwrite( $fp, $today." [".$_SERVER['PHP_SELF']."] ".$mess." from ". $_SERVER['REMOTE_ADDR']. "\n" );
122        fclose( $fp );
123    }
124
125    // ¥í¥°¥Æ¡¼¥·¥ç¥ó
126    gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
127}
128
129/**
130 * ¥í¥°¥í¡¼¥Æ¡¼¥·¥ç¥óµ¡Ç½
131 *
132 * @param integer $max_log ºÇÂç¥Õ¥¡¥¤¥ë¿ô
133 * @param integer $max_size ºÇÂ祵¥¤¥º
134 * @param string  $path ¥Õ¥¡¥¤¥ë¥Ñ¥¹
135 * @return void ¤Ê¤·
136 */
137function gfLogRotation($max_log, $max_size, $path) {
138
139    // ¥Ç¥£¥ì¥¯¥È¥ê̾¤ò¼èÆÀ
140    $dirname = dirname($path);
141    // ¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ
142    $basename = basename($path);
143
144    // ¥Õ¥¡¥¤¥ë¤¬ºÇÂ祵¥¤¥º¤òͤ¨¤Æ¤¤¤Ê¤¤¤«¥Á¥§¥Ã¥¯
145    if(filesize($path) > $max_size) {
146        if ($dh = opendir($dirname)) {
147            while (($file = readdir($dh)) !== false) {
148                // ¥í¥°¥í¡¼¥Æ¡¼¥·¥ç¥ó¤Ë¤ÆºîÀ®¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤ò¼èÆÀ
149                if(ereg("^". $basename . "\." , $file)) {
150                    $arrLog[] = $file;
151                }
152            }
153
154            // ¥Õ¥¡¥¤¥ë¥í¥°¤¬ºÇÂç¸Ä¿ô¤Ê¤é°Ê¾å¤Ê¤é¸Å¤¤¥Õ¥¡¥¤¥ë¤«¤éºï½ü¤¹¤ë
155            $count = count($arrLog);
156            if($count >= $max_log) {
157                $diff = $count - $max_log;
158                for($i = 0; $diff >= $i ; $i++) {
159                    unlink($dirname. "/" .array_pop($arrLog));
160                }
161            }
162
163            // ¥í¥°¥Õ¥¡¥¤¥ë¤Îꤍ»ú¤ò¤º¤é¤¹
164            $count = count($arrLog);
165            for($i = $count; 1 <= $i; $i--) {
166                $move_number = $i + 1;
167                @copy("$dirname/" . $arrLog[$i - 1], "$path.$move_number");
168            }
169            $ret = copy($path, "$path.1");
170
171            // ¿·µ¬¥í¥°¥Õ¥¡¥¤¥ë¤òºîÀ®
172            if($ret) {
173                unlink($path);
174                touch($path);
175                chmod($path, 0666);
176            }
177        }
178    }
179}
180
181/*----------------------------------------------------------------------
182 * [̾¾Î] gfMakePassword
183 * [³µÍ×] ¥é¥ó¥À¥à¥Ñ¥¹¥ï¡¼¥ÉÀ¸À®¡Ê±Ñ¿ô»ú¡Ë
184 * [°ú¿ô] ¥Ñ¥¹¥ï¡¼¥É¤Î·å¿ô
185 * [ÌáÃÍ] ¥é¥ó¥À¥àÀ¸À®¤µ¤ì¤¿¥Ñ¥¹¥ï¡¼¥É
186 * [°Í¸] ¤Ê¤·
187 * [Ãí¼á] -
188 *----------------------------------------------------------------------*/
189function gfMakePassword($pwLength) {
190
191    // Íð¿ôɽ¤Î¥·¡¼¥É¤ò·èÄê
192    srand((double)microtime() * 54234853);
193
194    // ¥Ñ¥¹¥ï¡¼¥Éʸ»úÎó¤ÎÇÛÎó¤òºîÀ®
195    $character = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679";
196    $pw = preg_split("//", $character, 0, PREG_SPLIT_NO_EMPTY);
197
198    $password = "";
199    for($i = 0; $i<$pwLength; $i++ ) {
200        $password .= $pw[array_rand($pw, 1)];
201    }
202
203    return $password;
204}
205
206/*----------------------------------------------------------------------
207 * [̾¾Î] sf_explodeExt
208 * [³µÍ×] ¥Õ¥¡¥¤¥ë¤Î³ÈÄ¥»Ò¼èÆÀ
209 * [°ú¿ô] ¥Õ¥¡¥¤¥ë̾
210 * [ÌáÃÍ] ³ÈÄ¥»Ò
211 * [°Í¸] ¤Ê¤·
212 * [Ãí¼á] -
213 *----------------------------------------------------------------------*/
214function gf_explodeExt($fileName) {
215    $ext1 = explode(".", $fileName);
216    $ext2 = $ext1[count($ext1) - 1];
217    $ext2 = strtolower($ext2);
218    return $ext2;
219}
220
221
222/*----------------------------------------------------------------------------------------------------------------------
223 * [̾¾Î] gfMailHeaderAddr
224 * [³µÍ×] ÆþÎϤµ¤ì¤¿¥á¡¼¥ë¥¢¥É¥ì¥¹¤ò¥á¡¼¥ë´Ø¿ôÍѤΰ¸Àè¤ËÊÑ´¹
225 * [°ú¿ô] ¡Ö¥á¡¼¥ë¥¢¥É¥ì¥¹¡×¤Þ¤¿¤Ï¡Ö̾Á°<¥á¡¼¥ë¥¢¥É¥ì¥¹>¡×¡¢Ê£¿ô¥¢¥É¥ì¥¹»ØÄê»þ¤Ï¥«¥ó¥Þ¶èÀÚ¤ê¤Ç»ØÄꤹ¤ë¡£
226 * [ÌáÃÍ] ¡Ö¥á¡¼¥ë¥¢¥É¥ì¥¹¡×¤Þ¤¿¤Ï¡ÖJIS_MIME¤Ë¥³¡¼¥ÉÊÑ´¹¤·¤¿Ì¾Á° <¥á¡¼¥ë¥¢¥É¥ì¥¹>¡×¡¢Ê£¿ô¥¢¥É¥ì¥¹»ØÄê»þ¤Ï¥«¥ó¥Þ¶èÀÚ¤ê¤ÇÊֵѤ¹¤ë¡£
227 * [°Í¸] ¤Ê¤·
228 * [Ãí¼á] -
229 *----------------------------------------------------------------------------------------------------------------------*/
230
231function gfMailHeaderAddr($str) {
232    $addrs = explode(",", $str); //¥¢¥É¥ì¥¹¤òÇÛÎó¤ËÆþ¤ì¤ë
233    foreach ($addrs as $addr) {
234        if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) {
235            //°ú¿ô¤¬¡Ö̾Á°<¥á¡¼¥ë¥¢¥É¥ì¥¹>¡×¤Î¾ì¹ç
236            $mailaddrs[] = mb_encode_mimeheader(trim($matches[1]))." <".trim($matches[2]).">";
237        } else {
238            //¥á¡¼¥ë¥¢¥É¥ì¥¹¤Î¤ß¤Î¾ì¹ç
239            $mailaddrs[] =  trim($addr);
240        }
241    }
242    return implode(", ", $mailaddrs); //Ê£¿ô¥¢¥É¥ì¥¹¤Ï¥«¥ó¥Þ¶èÀÚ¤ê¤Ë¤¹¤ë
243}
244?>
Note: See TracBrowser for help on using the repository browser.