list & updatecheck actions are now working to load all Olson zones from files.
[davical.git] / inc / tz / updatecheck.php
blob2d0ed736dae470016dde7da197e36c03cd43f367
1 <?php
2 /**
3 * DAViCal Timezone Service handler - capabilitis
5 * @package davical
6 * @subpackage tzservice
7 * @author Andrew McMillan <andrew@morphoss.com>
8 * @copyright Morphoss Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v3 or later
12 require_once('vCalendar.php');
14 $new_zones = 0;
15 $modified_zones = 0;
17 if ( empty($c->tzsource) ) $c->tzsource = '../zonedb/vtimezones';
18 if ( preg_match('{^http}', $c->tzsource ) ) {
21 else if ( file_exists($c->tzsource) ) {
22 /**
23 * Find all files recursively within the diectory given.
24 * @param string $dirname The directory to find files in
25 * @return array of filenames with full path
27 function recursive_files( $dirname ) {
28 $d = opendir($dirname);
29 $result = array();
30 while( $fn = readdir($d) ) {
31 if ( substr($fn,0,1) == '.' ) continue;
32 $fn = $dirname.'/'.$fn;
33 if ( is_dir($fn) ) {
34 $result = array_merge($result,recursive_files($fn));
36 else {
37 $result[] = $fn;
40 return $result;
43 $qry = new AwlQuery();
44 foreach( recursive_files($c->tzsource) AS $filename ) {
45 $tzid = str_replace('.ics', '', str_replace($c->tzsource.'/', '', $filename));
46 $tzrow = null;
47 if ( $qry->QDo('SELECT * FROM timezones WHERE tzid=:tzid', array(':tzid' => $tzid)) && $qry->rows() > 0 ) {
48 $tzrow = $qry->Fetch();
50 $vtimezone = file_get_contents( $filename, false );
51 if ( $vtimezone == $tzrow->vtimezone ) {
52 dbg_error_log('tz/updatecheck', 'Skipping zone "%s" - no change', $tzid );
53 continue;
55 $vtz = new vCalendar($vtimezone);
56 $last_modified = $vtz->GetProperty('LAST-MODIFIED');
57 if ( empty($last_modified) ) {
58 $last_modified = gmdate('Ymd\THis\Z');
59 // Then it was probably that way when we last updated the data, too :-(
60 if ( !empty($tzrow) ) {
61 $old_vtz = new vCalendar($tzrow->vtimezone);
62 $old_vtz->ClearProperties('LAST-MODIFIED');
63 // We need to add & remove this property so the Render is equivalent.
64 $vtz->AddProperty('LAST-MODIFIED',$last_modified);
65 $vtz->ClearProperties('LAST-MODIFIED');
66 if ( $vtz->Render() == $old_vtz->Render() ) {
67 dbg_error_log('tz/updatecheck', 'Skipping zone "%s" - no change', $tzid );
68 continue;
71 $vtz->AddProperty('LAST-MODIFIED',$last_modified);
73 dbg_error_log('tz/updatecheck', 'Writing %s zone for "%s"', (empty($tzrow)?"new":"updated"), $tzid );
74 $params = array(
75 ':tzid' => $tzid,
76 ':olson_name' => $tzid,
77 ':vtimezone' => $vtz->Render(),
78 ':last_modified' => $last_modified,
79 ':etag' => md5($vtz->Render())
81 if ( empty($tzrow) ) {
82 $new_zones++;
83 $sql = 'INSERT INTO timezones(tzid,active,olson_name,last_modified,etag,vtimezone) ';
84 $sql .= 'VALUES(:tzid,TRUE,:olson_name,:last_modified,:etag,:vtimezone)';
86 else {
87 $modified_zones++;
88 $sql = 'UPDATE timezones SET active=TRUE, olson_name=:olson_name, last_modified=:last_modified, ';
89 $sql .= 'etag=:etag, vtimezone=:vtimezone WHERE tzid=:tzid';
91 $qry->QDo($sql,$params);
93 header('Content-type: text/plain');
94 printf('Added %d new zones and updated data for %d zones', $new_zones, $modified_zones);
96 else {
97 dbg_error_log('ERROR', '$c->tzsource is not configured to a good source of timezone data');
100 exit(0);