source: branches/feature-module-zeus/data/lib/glib.php @ 15455

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