source: branches/beta/data/lib/glib.php @ 15120

Revision 15120, 7.2 KB checked in by adati, 13 years ago (diff)

1.4.2betaのマージ

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        gfPrintLog("*** start Debug ***");
90        ob_start();
91        print_r($obj);
92        $buffer = ob_get_contents();
93        ob_end_clean();
94        $fp = fopen($path, "a+");
95        fwrite( $fp, $buffer."\n" );
96        fclose( $fp );
97        gfPrintLog("*** end Debug ***");
98
99        // ¥í¥°¥Æ¡¼¥·¥ç¥ó
100        gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
101}
102
103/*----------------------------------------------------------------------
104 * [̾¾Î] gfPrintLog
105 * [³µÍ×] ¥í¥°¥Õ¥¡¥¤¥ë¤ËÆü»þ¡¢½èÍý¥Õ¥¡¥¤¥ë̾¡¢¥á¥Ã¥»¡¼¥¸¤ò½ÐÎÏ
106 * [°ú¿ô] ɽ¼¨¤·¤¿¤¤¥á¥Ã¥»¡¼¥¸
107 * [ÌáÃÍ] ¤Ê¤·
108 * [°Í¸] ¤Ê¤·
109 * [Ãí¼á] -
110 *----------------------------------------------------------------------*/
111function gfPrintLog($mess, $path = '') {
112    // ÆüÉդμèÆÀ
113    $today = date("Y/m/d H:i:s");
114    // ½ÐÎϥѥ¹¤ÎºîÀ®
115    if ($path == "") {
116        $path = LOG_PATH;
117    }
118
119    // ¥¨¥¹¥±¡¼¥×¤µ¤ì¤Æ¤¤¤ëʸ»ú¤ò¤â¤È¤ËÌ᤹
120    $trans_tbl = get_html_translation_table (HTML_ENTITIES);
121    $trans_tbl = array_flip ($trans_tbl);
122    $mess = strtr($mess, $trans_tbl);
123
124    $fp = fopen($path, "a+");
125    if($fp) {
126        fwrite( $fp, $today." [".$_SERVER['PHP_SELF']."] ".$mess." from ". $_SERVER['REMOTE_ADDR']. "\n" );
127        fclose( $fp );
128    }
129   
130    // ¥í¥°¥Æ¡¼¥·¥ç¥ó
131    gfLogRotation(MAX_LOG_QUANTITY, MAX_LOG_SIZE, $path);
132}
133
134/**         
135 * ¥í¥°¥í¡¼¥Æ¡¼¥·¥ç¥óµ¡Ç½           
136 *         
137 * @param integer $max_log ºÇÂç¥Õ¥¡¥¤¥ë¿ô
138 * @param integer $max_size ºÇÂ祵¥¤¥º
139 * @param string  $path ¥Õ¥¡¥¤¥ë¥Ñ¥¹
140 * @return void ¤Ê¤·
141 */         
142function gfLogRotation($max_log, $max_size, $path) {
143   
144    // ¥Ç¥£¥ì¥¯¥È¥ê̾¤ò¼èÆÀ
145    $dirname = dirname($path);
146    // ¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ
147    $basename = basename($path);
148   
149    // ¥Õ¥¡¥¤¥ë¤¬ºÇÂ祵¥¤¥º¤òĶ¤¨¤Æ¤¤¤Ê¤¤¤«¥Á¥§¥Ã¥¯
150    if(filesize($path) > $max_size) {
151        if ($dh = opendir($dirname)) {
152            while (($file = readdir($dh)) !== false) {
153                // ¥í¥°¥í¡¼¥Æ¡¼¥·¥ç¥ó¤Ë¤ÆºîÀ®¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤ò¼èÆÀ
154                if(ereg("^". $basename . "\." , $file)) {
155                    $arrLog[] = $file;
156                }
157            }
158           
159            // ¥Õ¥¡¥¤¥ë¥í¥°¤¬ºÇÂç¸Ä¿ô¤Ê¤é°Ê¾å¤Ê¤é¸Å¤¤¥Õ¥¡¥¤¥ë¤«¤éºï½ü¤¹¤ë
160            $count = count($arrLog);
161            if($count >= $max_log) {
162                $diff = $count - $max_log;
163                for($i = 0; $diff >= $i ; $i++) {
164                    unlink($dirname. "/" .array_pop($arrLog));
165                }   
166            }
167           
168            // ¥í¥°¥Õ¥¡¥¤¥ë¤Îź¤¨»ú¤ò¤º¤é¤¹
169            $count = count($arrLog);
170            for($i = $count; 1 <= $i; $i--) {
171                $move_number = $i + 1;
172                @copy("$dirname/" . $arrLog[$i - 1], "$path.$move_number");
173            }
174            $ret = copy($path, "$path.1");
175           
176            // ¿·µ¬¥í¥°¥Õ¥¡¥¤¥ë¤òºîÀ®
177            if($ret) {
178                unlink($path);         
179                touch($path);
180            }
181        }
182    }
183}
184
185/*----------------------------------------------------------------------
186 * [̾¾Î] gfMakePassword
187 * [³µÍ×] ¥é¥ó¥À¥à¥Ñ¥¹¥ï¡¼¥ÉÀ¸À®¡Ê±Ñ¿ô»ú¡Ë
188 * [°ú¿ô] ¥Ñ¥¹¥ï¡¼¥É¤Î·å¿ô
189 * [ÌáÃÍ] ¥é¥ó¥À¥àÀ¸À®¤µ¤ì¤¿¥Ñ¥¹¥ï¡¼¥É
190 * [°Í¸] ¤Ê¤·
191 * [Ãí¼á] -
192 *----------------------------------------------------------------------*/
193function gfMakePassword($pwLength) {
194   
195    // Íð¿ôɽ¤Î¥·¡¼¥É¤ò·èÄê
196    srand((double)microtime() * 54234853);
197   
198    // ¥Ñ¥¹¥ï¡¼¥Éʸ»úÎó¤ÎÇÛÎó¤òºîÀ®
199    $character = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679";
200    $pw = preg_split("//", $character, 0, PREG_SPLIT_NO_EMPTY);
201   
202    $password = "";
203    for($i = 0; $i<$pwLength; $i++ ) {
204        $password .= $pw[array_rand($pw, 1)];
205    }
206
207    return $password;
208}
209
210/*----------------------------------------------------------------------
211 * [̾¾Î] sf_explodeExt
212 * [³µÍ×] ¥Õ¥¡¥¤¥ë¤Î³ÈÄ¥»Ò¼èÆÀ
213 * [°ú¿ô] ¥Õ¥¡¥¤¥ë̾
214 * [ÌáÃÍ] ³ÈÄ¥»Ò
215 * [°Í¸] ¤Ê¤·
216 * [Ãí¼á] -
217 *----------------------------------------------------------------------*/
218function gf_explodeExt($fileName) {
219    $ext1 = explode(".", $fileName);
220    $ext2 = $ext1[count($ext1) - 1];
221    $ext2 = strtolower($ext2);
222    return $ext2;
223}
224
225
226/*----------------------------------------------------------------------------------------------------------------------
227 * [̾¾Î] gfMailHeaderAddr
228 * [³µÍ×] ÆþÎϤµ¤ì¤¿¥á¡¼¥ë¥¢¥É¥ì¥¹¤ò¥á¡¼¥ë´Ø¿ôÍѤΰ¸Àè¤ËÊÑ´¹
229 * [°ú¿ô] ¡Ö¥á¡¼¥ë¥¢¥É¥ì¥¹¡×¤Þ¤¿¤Ï¡Ö̾Á°<¥á¡¼¥ë¥¢¥É¥ì¥¹>¡×¡¢Ê£¿ô¥¢¥É¥ì¥¹»ØÄê»þ¤Ï¥«¥ó¥Þ¶èÀÚ¤ê¤Ç»ØÄꤹ¤ë¡£
230 * [ÌáÃÍ] ¡Ö¥á¡¼¥ë¥¢¥É¥ì¥¹¡×¤Þ¤¿¤Ï¡ÖJIS_MIME¤Ë¥³¡¼¥ÉÊÑ´¹¤·¤¿Ì¾Á° <¥á¡¼¥ë¥¢¥É¥ì¥¹>¡×¡¢Ê£¿ô¥¢¥É¥ì¥¹»ØÄê»þ¤Ï¥«¥ó¥Þ¶èÀÚ¤ê¤ÇÊֵѤ¹¤ë¡£
231 * [°Í¸] ¤Ê¤·
232 * [Ãí¼á] -
233 *----------------------------------------------------------------------------------------------------------------------*/
234
235function gfMailHeaderAddr($str) {
236    $addrs = explode(",", $str); //¥¢¥É¥ì¥¹¤òÇÛÎó¤ËÆþ¤ì¤ë
237    foreach ($addrs as $addr) {
238        if (preg_match("/^(.+)<(.+)>$/", $addr, $matches)) {
239            //°ú¿ô¤¬¡Ö̾Á°<¥á¡¼¥ë¥¢¥É¥ì¥¹>¡×¤Î¾ì¹ç
240            $mailaddrs[] = mb_encode_mimeheader(trim($matches[1]))." <".trim($matches[2]).">";
241        } else {
242            //¥á¡¼¥ë¥¢¥É¥ì¥¹¤Î¤ß¤Î¾ì¹ç
243            $mailaddrs[] =  trim($addr);
244        }
245    }
246    return implode(", ", $mailaddrs); //Ê£¿ô¥¢¥É¥ì¥¹¤Ï¥«¥ó¥Þ¶èÀÚ¤ê¤Ë¤¹¤ë
247}
248?>
Note: See TracBrowser for help on using the repository browser.