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