1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19:
20: final class NTools
21: {
22:
23: const MINUTE = 60;
24:
25:
26: const HOUR = 3600;
27:
28:
29: const DAY = 86400;
30:
31:
32: const WEEK = 604800;
33:
34:
35: const MONTH = 2629800;
36:
37:
38: const YEAR = 31557600;
39:
40:
41: private static $criticalSections;
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 DateTime53(date('Y-m-d H:i:s', $time));
70:
71: } else { 72: return new DateTime53($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' || (int) $status;
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 compare($l, $operator, $r)
111: {
112: switch ($operator) {
113: case '>':
114: return $l > $r;
115: case '>=':
116: return $l >= $r;
117: case '<':
118: return $l < $r;
119: case '<=':
120: return $l <= $r;
121: case '=':
122: case '==':
123: return $l == $r;
124: case '!':
125: case '!=':
126: case '<>':
127: return $l != $r;
128: }
129: throw new InvalidArgumentException("Unknown operator $operator.");
130: }
131:
132:
133:
134: 135: 136: 137: 138:
139: public static function detectMimeType($file)
140: {
141: if (!is_file($file)) {
142: throw new FileNotFoundException("File '$file' not found.");
143: }
144:
145: $info = @getimagesize($file); 146: if (isset($info['mime'])) {
147: return $info['mime'];
148:
149: } elseif (extension_loaded('fileinfo')) {
150: $type = preg_replace('#[\s;].*$#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
151:
152: } elseif (function_exists('mime_content_type')) {
153: $type = mime_content_type($file);
154: }
155:
156: return isset($type) && preg_match('#^\S+/\S+$#', $type) ? $type : 'application/octet-stream';
157: }
158:
159:
160:
161:
162:
163:
164:
165: 166: 167: 168:
169: public static function enterCriticalSection()
170: {
171: if (self::$criticalSections) {
172: throw new InvalidStateException('Critical section has already been entered.');
173: }
174: $handle = fopen((defined('TEMP_DIR') ? TEMP_DIR : dirname(__FILE__)) . '/criticalSection.lock', 'w');
175: if (!$handle) {
176: throw new InvalidStateException('Unable initialize critical section.');
177: }
178: flock(self::$criticalSections = $handle, LOCK_EX);
179: }
180:
181:
182:
183: 184: 185: 186:
187: public static function leaveCriticalSection()
188: {
189: if (!self::$criticalSections) {
190: throw new InvalidStateException('Critical section has not been initialized.');
191: }
192: fclose(self::$criticalSections);
193: self::$criticalSections = NULL;
194: }
195:
196: }