1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: final class NTools
22: {
23:
24: const MINUTE = 60;
25:
26:
27: const HOUR = 3600;
28:
29:
30: const DAY = 86400;
31:
32:
33: const WEEK = 604800;
34:
35:
36: const MONTH = 2629800;
37:
38:
39: const YEAR = 31557600;
40:
41:
42:
43: 44: 45:
46: final public function __construct()
47: {
48: throw new LogicException("Cannot instantiate static class " . get_class($this));
49: }
50:
51:
52:
53: 54: 55: 56: 57:
58: public static function createDateTime($time)
59: {
60: if ($time instanceof DateTime) {
61: return clone $time;
62:
63: } elseif (is_numeric($time)) {
64: if ($time <= self::YEAR) {
65: $time += time();
66: }
67: return new DateTime(date('Y-m-d H:i:s', $time));
68:
69: } else {
70: return new DateTime($time);
71: }
72: }
73:
74:
75:
76: 77: 78: 79: 80:
81: public static function iniFlag($var)
82: {
83: $status = strtolower(ini_get($var));
84: return $status === 'on' || $status === 'true' || $status === 'yes' || $status % 256;
85: }
86:
87:
88:
89: 90: 91: 92: 93: 94:
95: public static function defaultize(&$var, $default)
96: {
97: if ($var === NULL) $var = $default;
98: }
99:
100:
101:
102: 103: 104: 105: 106: 107:
108: public static function glob($pattern, $flags = 0)
109: {
110:
111: $files = glob($pattern, $flags);
112: if (!is_array($files)) {
113: $files = array();
114: }
115:
116: $dirs = glob(dirname($pattern) . '/*', $flags | GLOB_ONLYDIR);
117: if (is_array($dirs)) {
118: $mask = basename($pattern);
119: foreach ($dirs as $dir) {
120: $files = array_merge($files, self::glob($dir . '/' . $mask, $flags));
121: }
122: }
123:
124: return $files;
125: }
126:
127:
128:
129: 130: 131: 132: 133:
134: public static function detectMimeType($file)
135: {
136: if (!is_file($file)) {
137: throw new FileNotFoundException("File '$file' not found.");
138: }
139:
140: $info = @getimagesize($file);
141: if (isset($info['mime'])) {
142: return $info['mime'];
143:
144: } elseif (extension_loaded('fileinfo')) {
145: $type = preg_replace('#[\s;].*$#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
146:
147: } elseif (function_exists('mime_content_type')) {
148: $type = mime_content_type($file);
149: }
150:
151: return isset($type) && preg_match('#^\S+/\S+$#', $type) ? $type : 'application/octet-stream';
152: }
153:
154:
155:
156:
157:
158:
159:
160:
161: private static $errorMsg;
162:
163:
164:
165: 166: 167: 168:
169: public static function tryError($level = E_ALL)
170: {
171: set_error_handler(array(__CLASS__, '_errorHandler'), $level);
172: self::$errorMsg = NULL;
173: }
174:
175:
176:
177: 178: 179: 180: 181:
182: public static function catchError(& $message)
183: {
184: restore_error_handler();
185: $message = self::$errorMsg;
186: self::$errorMsg = NULL;
187: return $message !== NULL;
188: }
189:
190:
191:
192: 193: 194: 195:
196: public static function _errorHandler($severity, $message)
197: {
198: if (($severity & error_reporting()) !== $severity) {
199: return NULL;
200: }
201:
202: if (ini_get('html_errors')) {
203: $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
204: }
205:
206: if (($a = strpos($message, ': ')) !== FALSE) {
207: $message = substr($message, $a + 2);
208: }
209:
210: self::$errorMsg = $message;
211: }
212:
213: }
214: