OSDN Git Service

Modify the way to deal with i18n::strftime. i18n::mail should still be checked in...
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / i18n.php
1 <?php
2 /*
3  * i18n class
4  * written by Takashi Sakamoto as of Jun.5, 2011
5  * This is wrapper functions of iconv and mbstring
6  * for multibyte processing.
7  */
8
9 class i18n {
10         private static $charset = '';
11         private static $iconv = FALSE;
12         private static $mbstring = FALSE;
13         
14         public static function init ($charset) {
15                 if (extension_loaded('iconv')) {
16                         self::$iconv = TRUE;
17                         if (iconv_set_encoding ('output_encoding', $charset)
18                          && iconv_set_encoding ('internal_encoding', $charset)){
19                                 $return =TRUE;
20                         } else {
21                                 $return =FALSE;
22                         }
23                 } else if (extension_loaded('mbstring')) {
24                         self::$mbstring = TRUE;
25                         if (mb_http_output($charset)
26                          && mb_internal_encoding($charset)
27                          && mb_regex_encoding($charset)) {
28                                 $return = TRUE;
29                          } else {
30                                 $return =FALSE;
31                          }
32                 } else {
33                         $return =FALSE;
34                 }
35                 self::$charset = $charset;
36                 return (boolean) $return;
37         }
38         
39         public static function hsc ($string, $quotation=ENT_QUOTES) {
40                 return (string) htmlspecialchars($string, $quotation, self::$charset);
41         }
42         
43         public static function convert ($target, $from, $to='') {
44                 $string = '';
45                 if ($to == '') {
46                         $to = self::$charset;
47                 }
48                 if (self::$iconv) {
49                         $string = iconv($from, $to.'//TRANSLIT', $string);
50                 } else if (self::$mbstring) {
51                         $string = mb_convert_encoding($string, $to, $from);
52                 }
53                 return (string) $string;
54         }
55         
56         public static function strlen ($string) {
57                 $length = 0;
58                 if (self::$iconv) {
59                         $length = iconv_strlen ($string, self::$charset);
60                 } else if (self::$mbstring) {
61                         $length = mb_strlen ($string, self::$charset);
62                 } else {
63                         $length = strlen ($string);
64                 }
65                 return (integer) $length;
66         }
67         
68         public static function strpos ($haystack, $needle, $offset=0) {
69                 $position = 0;
70                 if (self::$iconv) {
71                         $position = iconv_strpos ($haystack, $needle, $offset, self::$charset);
72                 } else if (self::$mbstring) {
73                         $position = mb_strpos ($haystack, $needle, $offset, self::$charset);
74                 } else {
75                         $position = strpos ($haystack, $needle, $offset);
76                 }
77                 return (integer) $position;
78         }
79         
80         public static function strrpos ($haystack, $needle) {
81                 $position = 0;
82                 if (self::$iconv) {
83                         $position = iconv_strrpos ($haystack, $needle, self::$charset);
84                 } else if (self::$mbstring) {
85                         $position = mb_strrpos ($haystack, $needle, 0, self::$charset);
86                 } else {
87                         $position = strrpos ($haystack, $needle, 0);
88                 }
89                 return (integer) $position;
90         }
91         
92         public static function substr ($string, $start, $length=0) {
93                 $return = '';
94                 if (self::$iconv) {
95                         $return = iconv_substr ($string, $start, $length, self::$charset);
96                 } else if (self::$mbstring) {
97                         $return = mb_substr ($string, $start, $length, self::$charset);
98                 } else {
99                         $return = strrpos ($string, $start, $length);
100                 }
101                 return (string) $return;
102         }
103         
104         public static function explode ($delimiter, $target, $limit=0) {
105                 $array = array();
106                 if (preg_match("#$delimiter#", $target) === 0) {
107                         return $target;
108                 }
109                 for ($count=0; $limit == 0 || $count < $limit; $count++) {
110                         $offset = self::strpos($target, $delimiter);
111                         if ($array != array() && $offset == 0) {
112                                 $array[] = $target;
113                                 break;
114                         }
115                         $array[] = self::substr($target, 0, $offset);
116                         $length = self::strlen($target) - $offset;
117                         $target = self::substr($target, $offset+1, $length);
118                         continue;
119                 }
120                 return (array) $array;
121         }
122         
123         public static function strftime ($format, $timestamp='') {
124                 $formatted = '';
125                 $elements = array();
126                 if (preg_match('#%#', $format) === 0) {
127                         return $format;
128                 }
129                 $format = trim(preg_replace('#(%.)#', ',$1,', $format), ',');
130                 $elements = self::explode(',', $format);
131                 
132                 foreach ($elements as $element) {
133                         if (preg_match('#^%.$#', $element)) {
134                                 $formatted .= strftime($element, $timestamp);
135                         } else {
136                                 $formatted .= $element;
137                         }
138                 }
139                 
140                 return (string) $formatted;
141         }
142         
143         /*
144          * This function is experimental, need to be tested in various PHP environment
145          */
146         public static function mail($to, $subject, $message, $from) {
147                 if (self::$iconv) {
148                         $options = array(
149                          'scheme' => 'B',
150                          'input-charset' => self::$charset,
151                          'output-charset' => self::$charset,
152                          'line-length' => 76,
153                          'line-break-chars' => "\r\n");
154                         
155                         $subject = iconv_mime_encode('Subject', $subject, $options);
156                         $addition = iconv_mime_encode('From', $from, $options);
157                         
158                         $result = mail($to, $subject, $message, $addition);
159                 } else if (self::$mbstring) {
160                         $result = mb_send_mail ($to, $subject, $message, "From: $from");
161                 } else {
162                         $result = mail ($to, $subject, $message, "From: $from");
163                 }
164                 return (boolean) $result;
165         }
166 }