1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - Commononly used functions * |
---|
4 | * This file written by Dan Kuykendall <seek3r@phpgroupware.org> * |
---|
5 | * and Joseph Engo <jengo@phpgroupware.org> * |
---|
6 | * and Mark Peters <skeeter@phpgroupware.org> * |
---|
7 | * Commononly used functions by phpGroupWare developers * |
---|
8 | * Copyright (C) 2000, 2001 Dan Kuykendall * |
---|
9 | * -------------------------------------------------------------------------* |
---|
10 | * This library is part of the eGroupWare API * |
---|
11 | * http://www.egroupware.org * |
---|
12 | * ------------------------------------------------------------------------ * |
---|
13 | * This library is free software; you can redistribute it and/or modify it * |
---|
14 | * under the terms of the GNU Lesser General Public License as published by * |
---|
15 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
16 | * or any later version. * |
---|
17 | * This library is distributed in the hope that it will be useful, but * |
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
20 | * See the GNU Lesser General Public License for more details. * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License * |
---|
22 | * along with this library; if not, write to the Free Software Foundation, * |
---|
23 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
24 | \**************************************************************************/ |
---|
25 | |
---|
26 | |
---|
27 | $d1 = strtolower(@substr(PHPGW_API_INC,0,3)); |
---|
28 | $d2 = strtolower(@substr(PHPGW_SERVER_ROOT,0,3)); |
---|
29 | $d3 = strtolower(@substr(PHPGW_APP_INC,0,3)); |
---|
30 | if($d1 == 'htt' || $d1 == 'ftp' || $d2 == 'htt' || $d2 == 'ftp' || $d3 == 'htt' || $d3 == 'ftp') |
---|
31 | { |
---|
32 | echo 'Failed attempt to break in via an old Security Hole!<br>'."\n"; |
---|
33 | exit; |
---|
34 | } |
---|
35 | unset($d1); |
---|
36 | unset($d2); |
---|
37 | unset($d3); |
---|
38 | |
---|
39 | /*! |
---|
40 | @class datetime |
---|
41 | @abstract datetime class that contains common date/time functions |
---|
42 | */ |
---|
43 | class date_time |
---|
44 | { |
---|
45 | var $tz_offset; |
---|
46 | var $days = Array(); |
---|
47 | var $days_short = Array(); |
---|
48 | var $gmtnow = 0; |
---|
49 | var $users_localtime; |
---|
50 | var $cv_gmtdate; |
---|
51 | |
---|
52 | function date_time() |
---|
53 | { |
---|
54 | $this->tz_offset = 3600 * (int)@$GLOBALS['phpgw_info']['user']['preferences']['common']['tz_offset']; |
---|
55 | print_debug('datetime::datetime::gmtnow',$this->gmtnow,'api'); |
---|
56 | |
---|
57 | $error_occured = True; |
---|
58 | // If we already have a GMT time, no need to do this again. |
---|
59 | if(!$this->gmtnow) |
---|
60 | { |
---|
61 | if(isset($GLOBALS['phpgw_info']['server']['tz_offset'])) |
---|
62 | { |
---|
63 | $this->gmtnow = time() - ((int)$GLOBALS['phpgw_info']['server']['tz_offset'] * 3600); |
---|
64 | print_debug('datetime::datetime::tz_offset',"set via tz_offset=".$GLOBALS['phpgw_info']['server']['tz_offset'].": gmtnow=".date('Y/m/d H:i',$this->gmtnow),'api'); |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | $this->gmtnow = time() - ($this->getbestguess() * 3600); |
---|
69 | print_debug('datetime::datetime::bestguess',"set via bestguess=".$this->getbestguess().": gmtnow=".date('Y/m/d H:i',$this->gmtnow),'api'); |
---|
70 | } |
---|
71 | } |
---|
72 | $this->users_localtime = time() + $this->tz_offset; |
---|
73 | } |
---|
74 | |
---|
75 | function getntpoffset() |
---|
76 | { |
---|
77 | $error_occured = False; |
---|
78 | if(!@is_object($GLOBALS['phpgw']->network)) |
---|
79 | { |
---|
80 | $GLOBALS['phpgw']->network = createobject('phpgwapi.network'); |
---|
81 | } |
---|
82 | $server_time = time(); |
---|
83 | |
---|
84 | if($GLOBALS['phpgw']->network->open_port('129.6.15.28',13,5)) |
---|
85 | { |
---|
86 | $line = $GLOBALS['phpgw']->network->bs_read_port(64); |
---|
87 | $GLOBALS['phpgw']->network->close_port(); |
---|
88 | |
---|
89 | $array = explode(' ',$line); |
---|
90 | // host: 129.6.15.28 |
---|
91 | // Value returned is 52384 02-04-20 13:55:29 50 0 0 9.2 UTC(NIST) * |
---|
92 | print_debug('Server datetime',time(),'api'); |
---|
93 | print_debug('Temporary NTP datetime',$line,'api'); |
---|
94 | if ($array[5] == 4) |
---|
95 | { |
---|
96 | $error_occured = True; |
---|
97 | } |
---|
98 | else |
---|
99 | { |
---|
100 | $date = explode('-',$array[1]); |
---|
101 | $time = explode(':',$array[2]); |
---|
102 | $this->gmtnow = mktime((int)$time[0],(int)$time[1],(int)$time[2],(int)$date[1],(int)$date[2],(int)$date[0] + 2000); |
---|
103 | print_debug('Temporary RFC epoch',$this->gmtnow,'api'); |
---|
104 | print_debug('GMT',date('Ymd H:i:s',$this->gmtnow),'api'); |
---|
105 | } |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | $error_occured = True; |
---|
110 | } |
---|
111 | |
---|
112 | if($error_occured == True) |
---|
113 | { |
---|
114 | return $this->getbestguess(); |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | return (int)(($server_time - $this->gmtnow) / 3600); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | function gethttpoffset() |
---|
123 | { |
---|
124 | $error_occured = False; |
---|
125 | if(!@is_object($GLOBALS['phpgw']->network)) |
---|
126 | { |
---|
127 | $GLOBALS['phpgw']->network = createobject('phpgwapi.network'); |
---|
128 | } |
---|
129 | $server_time = time(); |
---|
130 | |
---|
131 | $filename = 'http://132.163.4.213/timezone.cgi?GMT'; |
---|
132 | $file = $GLOBALS['phpgw']->network->gethttpsocketfile($filename); |
---|
133 | if(!$file) |
---|
134 | { |
---|
135 | return $this->getbestguess(); |
---|
136 | } |
---|
137 | $time = strip_tags($file[55]); |
---|
138 | $date = strip_tags($file[56]); |
---|
139 | |
---|
140 | print_debug('GMT DateTime',$date.' '.$time,'api'); |
---|
141 | $dt_array = explode(' ',$date); |
---|
142 | $temp_datetime = $dt_array[0].' '.substr($dt_array[2],0,-1).' '.substr($dt_array[1],0,3).' '.$dt_array[3].' '.$time.' GMT'; |
---|
143 | print_debug('Reformulated GMT DateTime',$temp_datetime,'api'); |
---|
144 | $this->gmtnow = $this->convert_rfc_to_epoch($temp_datetime); |
---|
145 | print_debug('this->gmtnow',$this->gmtnow,'api'); |
---|
146 | print_debug('server time',$server_time,'api'); |
---|
147 | print_debug('server DateTime',date('D, d M Y H:i:s',$server_time),'api'); |
---|
148 | return (int)(($server_time - $this->gmtnow) / 3600); |
---|
149 | } |
---|
150 | |
---|
151 | function getbestguess() |
---|
152 | { |
---|
153 | print_debug('datetime::datetime::debug: Inside getting from local server','api'); |
---|
154 | $server_time = time(); |
---|
155 | // Calculate GMT time... |
---|
156 | // If DST, add 1 hour... |
---|
157 | // - (date('I') == 1?3600:0) |
---|
158 | $this->gmtnow = $this->convert_rfc_to_epoch(gmdate('D, d M Y H:i:s',$server_time).' GMT'); |
---|
159 | return (int)(($server_time - $this->gmtnow) / 3600); |
---|
160 | } |
---|
161 | |
---|
162 | function convert_rfc_to_epoch($date_str) |
---|
163 | { |
---|
164 | $comma_pos = strpos($date_str,','); |
---|
165 | if($comma_pos) |
---|
166 | { |
---|
167 | $date_str = substr($date_str,$comma_pos+1); |
---|
168 | } |
---|
169 | |
---|
170 | // This may need to be a reference to the different months in native tongue.... |
---|
171 | $month= array( |
---|
172 | 'Jan' => 1, |
---|
173 | 'Feb' => 2, |
---|
174 | 'Mar' => 3, |
---|
175 | 'Apr' => 4, |
---|
176 | 'May' => 5, |
---|
177 | 'Jun' => 6, |
---|
178 | 'Jul' => 7, |
---|
179 | 'Aug' => 8, |
---|
180 | 'Sep' => 9, |
---|
181 | 'Oct' => 10, |
---|
182 | 'Nov' => 11, |
---|
183 | 'Dec' => 12 |
---|
184 | ); |
---|
185 | $dta = array(); |
---|
186 | $ta = array(); |
---|
187 | |
---|
188 | // Convert "15 Jul 2000 20:50:22 +0200" to unixtime |
---|
189 | $dta = explode(' ',$date_str); |
---|
190 | $ta = explode(':',$dta[4]); |
---|
191 | |
---|
192 | if(substr($dta[5],0,3) <> 'GMT') |
---|
193 | { |
---|
194 | $tzoffset = substr($dta[5],0,1); |
---|
195 | $tzhours = (int)substr($dta[5],1,2); |
---|
196 | $tzmins = (int)substr($dta[5],3,2); |
---|
197 | switch ($tzoffset) |
---|
198 | { |
---|
199 | case '-': |
---|
200 | (int)$ta[0] += $tzhours; |
---|
201 | (int)$ta[1] += $tzmins; |
---|
202 | break; |
---|
203 | case '+': |
---|
204 | (int)$ta[0] -= $tzhours; |
---|
205 | (int)$ta[1] -= $tzmins; |
---|
206 | break; |
---|
207 | } |
---|
208 | } |
---|
209 | return mktime($ta[0],$ta[1],$ta[2],$month[$dta[2]],$dta[1],$dta[3]); |
---|
210 | } |
---|
211 | |
---|
212 | function get_weekday_start($year,$month,$day) |
---|
213 | { |
---|
214 | $weekday = $this->day_of_week($year,$month,$day); |
---|
215 | switch($GLOBALS['phpgw_info']['user']['preferences']['calendar']['weekdaystarts']) |
---|
216 | { |
---|
217 | // Saturday is for arabic support |
---|
218 | case 'Saturday': |
---|
219 | $this->days = Array( |
---|
220 | 0 => 'Sat', |
---|
221 | 1 => 'Sun', |
---|
222 | 2 => 'Mon', |
---|
223 | 3 => 'Tue', |
---|
224 | 4 => 'Wed', |
---|
225 | 5 => 'Thu', |
---|
226 | 6 => 'Fri' |
---|
227 | ); |
---|
228 | $this->days_short = Array( |
---|
229 | 0 => 'Sa', |
---|
230 | 1 => 'Su', |
---|
231 | 2 => 'Mo', |
---|
232 | 3 => 'Tu', |
---|
233 | 4 => 'We', |
---|
234 | 5 => 'Th', |
---|
235 | 6 => 'Fr' |
---|
236 | ); |
---|
237 | switch($weekday) |
---|
238 | { |
---|
239 | case 0: |
---|
240 | $sday = mktime(2,0,0,$month,$day - 1,$year); |
---|
241 | break; |
---|
242 | case 6: |
---|
243 | $sday = mktime(2,0,0,$month,$day,$year); |
---|
244 | break; |
---|
245 | default: |
---|
246 | $sday = mktime(2,0,0,$month,$day - ($weekday + 1),$year); |
---|
247 | break; |
---|
248 | } |
---|
249 | break; |
---|
250 | case 'Monday': |
---|
251 | $this->days = Array( |
---|
252 | 0 => 'Mon', |
---|
253 | 1 => 'Tue', |
---|
254 | 2 => 'Wed', |
---|
255 | 3 => 'Thu', |
---|
256 | 4 => 'Fri', |
---|
257 | 5 => 'Sat', |
---|
258 | 6 => 'Sun' |
---|
259 | ); |
---|
260 | $this->days_short = Array( |
---|
261 | 0 => 'Mo', |
---|
262 | 1 => 'Tu', |
---|
263 | 2 => 'We', |
---|
264 | 3 => 'Th', |
---|
265 | 4 => 'Fr', |
---|
266 | 5 => 'Sa', |
---|
267 | 6 => 'Su' |
---|
268 | ); |
---|
269 | switch($weekday) |
---|
270 | { |
---|
271 | case 0: |
---|
272 | $sday = mktime(2,0,0,$month,$day - 6,$year); |
---|
273 | break; |
---|
274 | case 1: |
---|
275 | $sday = mktime(2,0,0,$month,$day,$year); |
---|
276 | break; |
---|
277 | default: |
---|
278 | $sday = mktime(2,0,0,$month,$day - ($weekday - 1),$year); |
---|
279 | break; |
---|
280 | } |
---|
281 | break; |
---|
282 | case 'Sunday': |
---|
283 | default: |
---|
284 | $this->days = Array( |
---|
285 | 0 => 'Sun', |
---|
286 | 1 => 'Mon', |
---|
287 | 2 => 'Tue', |
---|
288 | 3 => 'Wed', |
---|
289 | 4 => 'Thu', |
---|
290 | 5 => 'Fri', |
---|
291 | 6 => 'Sat' |
---|
292 | ); |
---|
293 | $this->days_short = Array( |
---|
294 | 0 => 'Su', |
---|
295 | 1 => 'Mo', |
---|
296 | 2 => 'Tu', |
---|
297 | 3 => 'We', |
---|
298 | 4 => 'Th', |
---|
299 | 5 => 'Fr', |
---|
300 | 6 => 'Sa' |
---|
301 | ); |
---|
302 | $sday = mktime(2,0,0,$month,$day - $weekday,$year); |
---|
303 | break; |
---|
304 | } |
---|
305 | return $sday - 7200; |
---|
306 | } |
---|
307 | |
---|
308 | function is_leap_year($year) |
---|
309 | { |
---|
310 | if (((int)$year % 4 == 0) && ((int)$year % 100 != 0) || ((int)$year % 400 == 0)) |
---|
311 | { |
---|
312 | return 1; |
---|
313 | } |
---|
314 | else |
---|
315 | { |
---|
316 | return 0; |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | function days_in_month($month,$year) |
---|
321 | { |
---|
322 | $days = Array( |
---|
323 | 1 => 31, |
---|
324 | 2 => 28 + $this->is_leap_year((int)$year), |
---|
325 | 3 => 31, |
---|
326 | 4 => 30, |
---|
327 | 5 => 31, |
---|
328 | 6 => 30, |
---|
329 | 7 => 31, |
---|
330 | 8 => 31, |
---|
331 | 9 => 30, |
---|
332 | 10 => 31, |
---|
333 | 11 => 30, |
---|
334 | 12 => 31 |
---|
335 | ); |
---|
336 | return $days[(int)$month]; |
---|
337 | } |
---|
338 | |
---|
339 | function date_valid($year,$month,$day) |
---|
340 | { |
---|
341 | return checkdate((int)$month,(int)$day,(int)$year); |
---|
342 | } |
---|
343 | |
---|
344 | function time_valid($hour,$minutes,$seconds) |
---|
345 | { |
---|
346 | if((int)$hour < 0 || (int)$hour > 24) |
---|
347 | { |
---|
348 | return False; |
---|
349 | } |
---|
350 | if((int)$minutes < 0 || (int)$minutes > 59) |
---|
351 | { |
---|
352 | return False; |
---|
353 | } |
---|
354 | if((int)$seconds < 0 || (int)$seconds > 59) |
---|
355 | { |
---|
356 | return False; |
---|
357 | } |
---|
358 | |
---|
359 | return True; |
---|
360 | } |
---|
361 | |
---|
362 | function day_of_week($year,$month,$day) |
---|
363 | { |
---|
364 | if($month > 2) |
---|
365 | { |
---|
366 | $month -= 2; |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | $month += 10; |
---|
371 | $year--; |
---|
372 | } |
---|
373 | $day = (floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4) + floor(($year / 100) / 4) - 2 * floor($year / 100) + 77); |
---|
374 | return (($day - 7 * floor($day / 7))); |
---|
375 | } |
---|
376 | |
---|
377 | function day_of_year($year,$month,$day) |
---|
378 | { |
---|
379 | $days = array(0,31,59,90,120,151,181,212,243,273,304,334); |
---|
380 | |
---|
381 | $julian = ($days[$month - 1] + $day); |
---|
382 | |
---|
383 | if($month > 2 && $this->is_leap_year($year)) |
---|
384 | { |
---|
385 | $julian++; |
---|
386 | } |
---|
387 | return($julian); |
---|
388 | } |
---|
389 | |
---|
390 | /*! |
---|
391 | @function days_between |
---|
392 | @abstract Get the number of days between two dates |
---|
393 | @author Steven Cramer/Ralf Becker |
---|
394 | @param $m1 - Month_1, $d1 - Day_1, $y1 - Year_1, $m2 - Month_2, $d2 - Day_2, $y2 - Year_2 |
---|
395 | @note the last param == 0, ensures that the calculation is always done without daylight-saveing |
---|
396 | */ |
---|
397 | function days_between($m1,$d1,$y1,$m2,$d2,$y2) |
---|
398 | { |
---|
399 | return (int)((mktime(0,0,0,$m2,$d2,$y2,0) - mktime(0,0,0,$m1,$d1,$y1,0)) / 86400); |
---|
400 | } |
---|
401 | |
---|
402 | function date_compare($a_year,$a_month,$a_day,$b_year,$b_month,$b_day) |
---|
403 | { |
---|
404 | $a_date = mktime(0,0,0,(int)$a_month,(int)$a_day,(int)$a_year); |
---|
405 | $b_date = mktime(0,0,0,(int)$b_month,(int)$b_day,(int)$b_year); |
---|
406 | if($a_date == $b_date) |
---|
407 | { |
---|
408 | return 0; |
---|
409 | } |
---|
410 | elseif($a_date > $b_date) |
---|
411 | { |
---|
412 | return 1; |
---|
413 | } |
---|
414 | elseif($a_date < $b_date) |
---|
415 | { |
---|
416 | return -1; |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | function time_compare($a_hour,$a_minute,$a_second,$b_hour,$b_minute,$b_second) |
---|
421 | { |
---|
422 | // I use the 1970/1/2 to compare the times, as the 1. can get via TZ-offest still |
---|
423 | // before 1970/1/1, which is the earliest date allowed on windows |
---|
424 | $a_time = mktime((int)$a_hour,(int)$a_minute,(int)$a_second,1,2,1970); |
---|
425 | $b_time = mktime((int)$b_hour,(int)$b_minute,(int)$b_second,1,2,1970); |
---|
426 | if($a_time == $b_time) |
---|
427 | { |
---|
428 | return 0; |
---|
429 | } |
---|
430 | elseif($a_time > $b_time) |
---|
431 | { |
---|
432 | return 1; |
---|
433 | } |
---|
434 | elseif($a_time < $b_time) |
---|
435 | { |
---|
436 | return -1; |
---|
437 | } |
---|
438 | } |
---|
439 | |
---|
440 | function makegmttime($hour,$minute,$second,$month,$day,$year) |
---|
441 | { |
---|
442 | return $this->gmtdate(mktime($hour, $minute, $second, $month, $day, $year)); |
---|
443 | } |
---|
444 | |
---|
445 | // Note common:show_date converts server- to user-time, before it returns the requested format !!! |
---|
446 | function localdates($localtime) |
---|
447 | { |
---|
448 | $date = Array('raw','day','month','year','full','dow','dm','bd'); |
---|
449 | $date['raw'] = $localtime; |
---|
450 | $date['year'] = (int)$GLOBALS['phpgw']->common->show_date($date['raw'],'Y'); |
---|
451 | $date['month'] = (int)$GLOBALS['phpgw']->common->show_date($date['raw'],'m'); |
---|
452 | $date['day'] = (int)$GLOBALS['phpgw']->common->show_date($date['raw'],'d'); |
---|
453 | $date['full'] = (int)$GLOBALS['phpgw']->common->show_date($date['raw'],'Ymd'); |
---|
454 | $date['bd'] = mktime(0,0,0,$date['month'],$date['day'],$date['year']); |
---|
455 | $date['dm'] = (int)$GLOBALS['phpgw']->common->show_date($date['raw'],'dm'); |
---|
456 | $date['dow'] = $this->day_of_week($date['year'],$date['month'],$date['day']); |
---|
457 | $date['hour'] = (int)$GLOBALS['phpgw']->common->show_date($date['raw'],'H'); |
---|
458 | $date['minute'] = (int)$GLOBALS['phpgw']->common->show_date($date['raw'],'i'); |
---|
459 | $date['second'] = (int)$GLOBALS['phpgw']->common->show_date($date['raw'],'s'); |
---|
460 | |
---|
461 | return $date; |
---|
462 | } |
---|
463 | |
---|
464 | function gmtdate($localtime) |
---|
465 | { |
---|
466 | return $this->localdates($localtime - $this->tz_offset); |
---|
467 | } |
---|
468 | } |
---|
469 | ?> |
---|