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: private static $criticalSections;
45:
46:
47:
48: 49: 50:
51: final public function __construct()
52: {
53: throw new \LogicException("Cannot instantiate static class " . get_class($this));
54: }
55:
56:
57:
58: 59: 60: 61: 62:
63: public static function createDateTime($time)
64: {
65: if ($time instanceof \DateTime) {
66: return clone $time;
67:
68: } elseif (is_numeric($time)) {
69: if ($time <= self::YEAR) {
70: $time += time();
71: }
72: return new \DateTime(date('Y-m-d H:i:s', $time));
73:
74: } else { 75: return new \DateTime($time);
76: }
77: }
78:
79:
80:
81: 82: 83: 84: 85:
86: public static function iniFlag($var)
87: {
88: $status = strtolower(ini_get($var));
89: return $status === 'on' || $status === 'true' || $status === 'yes' || (int) $status;
90: }
91:
92:
93:
94: 95: 96: 97: 98: 99:
100: public static function defaultize(&$var, $default)
101: {
102: if ($var === NULL) $var = $default;
103: }
104:
105:
106:
107: 108: 109: 110: 111: 112:
113: public static function compare($l, $operator, $r)
114: {
115: switch ($operator) {
116: case '>':
117: return $l > $r;
118: case '>=':
119: return $l >= $r;
120: case '<':
121: return $l < $r;
122: case '<=':
123: return $l <= $r;
124: case '=':
125: case '==':
126: return $l == $r;
127: case '!':
128: case '!=':
129: case '<>':
130: return $l != $r;
131: }
132: throw new \InvalidArgumentException("Unknown operator $operator.");
133: }
134:
135:
136:
137: 138: 139: 140: 141:
142: public static function detectMimeType($file)
143: {
144: if (!is_file($file)) {
145: throw new \FileNotFoundException("File '$file' not found.");
146: }
147:
148: $info = @getimagesize($file); 149: if (isset($info['mime'])) {
150: return $info['mime'];
151:
152: } elseif (extension_loaded('fileinfo')) {
153: $type = preg_replace('#[\s;].*$#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
154:
155: } elseif (function_exists('mime_content_type')) {
156: $type = mime_content_type($file);
157: }
158:
159: return isset($type) && preg_match('#^\S+/\S+$#', $type) ? $type : 'application/octet-stream';
160: }
161:
162:
163:
164: 165: 166: 167: 168:
169: public static function detectMimeTypeFromString($data)
170: {
171: if (extension_loaded('fileinfo') && preg_match('#^(\S+/[^\s;]+)#', finfo_buffer(finfo_open(FILEINFO_MIME), $data), $m)) {
172: return $m[1];
173:
174: } elseif (strncmp($data, "\xff\xd8", 2) === 0) {
175: return 'image/jpeg';
176:
177: } elseif (strncmp($data, "\x89PNG", 4) === 0) {
178: return 'image/png';
179:
180: } elseif (strncmp($data, "GIF", 3) === 0) {
181: return 'image/gif';
182:
183: } else {
184: return 'application/octet-stream';
185: }
186: }
187:
188:
189:
190:
191:
192:
193:
194: 195: 196: 197:
198: public static function enterCriticalSection()
199: {
200: if (self::$criticalSections) {
201: throw new \InvalidStateException('Critical section has already been entered.');
202: }
203: 204: $handle = substr(PHP_OS, 0, 3) === 'WIN' ? @fopen(NETTE_DIR . '/lockfile', 'w') : @fopen(__FILE__, 'r'); 205: if (!$handle) {
206: throw new \InvalidStateException("Unable initialize critical section.");
207: }
208: flock(self::$criticalSections = $handle, LOCK_EX);
209: }
210:
211:
212:
213: 214: 215: 216:
217: public static function leaveCriticalSection()
218: {
219: if (!self::$criticalSections) {
220: throw new \InvalidStateException('Critical section has not been initialized.');
221: }
222: flock(self::$criticalSections, LOCK_UN);
223: fclose(self::$criticalSections);
224: self::$criticalSections = NULL;
225: }
226:
227: }