carlwgeorge / rpms / php

Forked from rpms/php 5 years ago
Clone
5d4e5cd
5d4e5cd
Add support for use of the system timezone database, rather
5d4e5cd
than embedding a copy.  Discussed upstream but was not desired.
5d4e5cd
5d4e5cd
History:
3bd1eb2
r7: improve check for valid timezone id to exclude directories
1890efc
r6: fix fd leak in r5, fix country code/BC flag use in 
1890efc
    timezone_identifiers_list() using system db,
1890efc
    fix use of PECL timezonedb to override system db,
5d4e5cd
r5: reverts addition of "System/Localtime" fake tzname.
5d4e5cd
    updated for 5.3.0, parses zone.tab to pick up mapping between
5d4e5cd
    timezone name, country code and long/lat coords
5d4e5cd
r4: added "System/Localtime" tzname which uses /etc/localtime
5d4e5cd
r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
5d4e5cd
r2: add filesystem trawl to set up name alias index
5d4e5cd
r1: initial revision
5d4e5cd
3bd1eb2
--- php-5.3.1/ext/date/lib/parse_tz.c.systzdata
3bd1eb2
+++ php-5.3.1/ext/date/lib/parse_tz.c
5d4e5cd
@@ -20,6 +20,16 @@
5d4e5cd
 
5d4e5cd
 #include "timelib.h"
5d4e5cd
 
5d4e5cd
+#ifdef HAVE_SYSTEM_TZDATA
5d4e5cd
+#include <sys/mman.h>
5d4e5cd
+#include <sys/stat.h>
5d4e5cd
+#include <limits.h>
5d4e5cd
+#include <fcntl.h>
5d4e5cd
+#include <unistd.h>
5d4e5cd
+
5d4e5cd
+#include "php_scandir.h"
5d4e5cd
+#endif
5d4e5cd
+
5d4e5cd
 #include <stdio.h>
5d4e5cd
 
5d4e5cd
 #ifdef HAVE_LOCALE_H
5f68886
@@ -31,7 +41,12 @@
5d4e5cd
 #else
5d4e5cd
 #include <strings.h>
5d4e5cd
 #endif
5d4e5cd
+
5d4e5cd
+#ifndef HAVE_SYSTEM_TZDATA
5d4e5cd
 #include "timezonedb.h"
5d4e5cd
+#endif
1890efc
+
5f68886
+#include <ctype.h>
5f68886
 
5d4e5cd
 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
5d4e5cd
 # if defined(__LITTLE_ENDIAN__)
1890efc
@@ -51,9 +66,14 @@
1890efc
 
1890efc
 static void read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
1890efc
 {
1890efc
-	/* skip ID */
1890efc
-	*tzf += 4;
1890efc
-	
1890efc
+        if (memcmp(tzf, "TZif", 4) == 0) {
1890efc
+                *tzf += 20;
1890efc
+                return;
1890efc
+        }
1890efc
+        
1890efc
+        /* skip ID */
1890efc
+        *tzf += 4;
1890efc
+                
1890efc
 	/* read BC flag */
1890efc
 	tz->bc = (**tzf == '\1');
1890efc
 	*tzf += 1;
3bd1eb2
@@ -253,7 +273,397 @@ void timelib_dump_tzinfo(timelib_tzinfo 
5d4e5cd
 	}
5d4e5cd
 }
5d4e5cd
 
1890efc
-static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
5d4e5cd
+#ifdef HAVE_SYSTEM_TZDATA
5d4e5cd
+
5d4e5cd
+#ifdef HAVE_SYSTEM_TZDATA_PREFIX
5d4e5cd
+#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
5d4e5cd
+#else
5d4e5cd
+#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
5d4e5cd
+#endif
5d4e5cd
+
5d4e5cd
+/* System timezone database pointer. */
5d4e5cd
+static const timelib_tzdb *timezonedb_system = NULL;
5d4e5cd
+
5d4e5cd
+/* Hash table entry for the cache of the zone.tab mapping table. */
5d4e5cd
+struct location_info {
1890efc
+        char code[2];
1890efc
+        double latitude, longitude;
1890efc
+        char name[64];
1890efc
+        char *comment;
1890efc
+        struct location_info *next;
5d4e5cd
+};
5d4e5cd
+
5d4e5cd
+/* Cache of zone.tab. */
1890efc
+static struct location_info **system_location_table;
5d4e5cd
+
5d4e5cd
+/* Size of the zone.tab hash table; a random-ish prime big enough to
5d4e5cd
+ * prevent too many collisions. */
5d4e5cd
+#define LOCINFO_HASH_SIZE (1021)
5d4e5cd
+
5d4e5cd
+static uint32_t tz_hash(const char *str)
5f68886
+{
5d4e5cd
+    const unsigned char *p = (const unsigned char *)str;
5d4e5cd
+    uint32_t hash = 5381;
5d4e5cd
+    int c;
5d4e5cd
+    
5d4e5cd
+    while ((c = *p++) != '\0') {
5d4e5cd
+        hash = (hash << 5) ^ hash ^ c;
5d4e5cd
+    }
5d4e5cd
+    
5d4e5cd
+    return hash % LOCINFO_HASH_SIZE;
5d4e5cd
+}
5d4e5cd
+
5d4e5cd
+/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
5d4e5cd
+ * parsed string on success, or NULL on parse error.  On success,
5d4e5cd
+ * writes the parsed number to *result. */
5d4e5cd
+static char *parse_iso6709(char *p, double *result)
5d4e5cd
+{
5d4e5cd
+    double v, sign;
5d4e5cd
+    char *pend;
5d4e5cd
+    size_t len;
5d4e5cd
+
5d4e5cd
+    if (*p == '+')
5d4e5cd
+        sign = 1.0;
5d4e5cd
+    else if (*p == '-')
5d4e5cd
+        sign = -1.0;
5d4e5cd
+    else
5d4e5cd
+        return NULL;
5d4e5cd
+
5d4e5cd
+    p++;
5d4e5cd
+    for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
5d4e5cd
+        ;;
5d4e5cd
+
5d4e5cd
+    /* Annoying encoding used by zone.tab has no decimal point, so use
5d4e5cd
+     * the length to determine the format:
5d4e5cd
+     * 
5d4e5cd
+     * 4 = DDMM
5d4e5cd
+     * 5 = DDDMM
5d4e5cd
+     * 6 = DDMMSS
5d4e5cd
+     * 7 = DDDMMSS
5d4e5cd
+     */
5d4e5cd
+    len = pend - p;
5d4e5cd
+    if (len < 4 || len > 7) {
5d4e5cd
+        return NULL;
5d4e5cd
+    }
5d4e5cd
+
5d4e5cd
+    /* p => [D]DD */
5d4e5cd
+    v = (p[0] - '0') * 10.0 + (p[1] - '0');
5d4e5cd
+    p += 2;
5d4e5cd
+    if (len == 5 || len == 7)
5d4e5cd
+        v = v * 10.0 + (*p++ - '0');
5d4e5cd
+    /* p => MM[SS] */
5d4e5cd
+    v += (10.0 * (p[0] - '0')
5d4e5cd
+          + p[1] - '0') / 60.0;
5d4e5cd
+    p += 2;
5d4e5cd
+    /* p => [SS] */
5d4e5cd
+    if (len > 5) {
5d4e5cd
+        v += (10.0 * (p[0] - '0')
5d4e5cd
+              + p[1] - '0') / 3600.0;
5d4e5cd
+        p += 2;
5d4e5cd
+    }
5d4e5cd
+
1890efc
+    /* Round to five decimal place, not because it's a good idea,
1890efc
+     * but, because the builtin data uses rounded data, so, match
1890efc
+     * that. */
1890efc
+    *result = round(v * sign * 100000.0) / 100000.0;
5d4e5cd
+
5d4e5cd
+    return p;
5d4e5cd
+}
5d4e5cd
+
5d4e5cd
+/* This function parses the zone.tab file to build up the mapping of
5d4e5cd
+ * timezone to country code and geographic location, and returns a
5d4e5cd
+ * hash table.  The hash table is indexed by the function:
5d4e5cd
+ *
5d4e5cd
+ *   tz_hash(timezone-name)
5d4e5cd
+ */
1890efc
+static struct location_info **create_location_table(void)
5d4e5cd
+{
5d4e5cd
+    struct location_info **li, *i;
5d4e5cd
+    char zone_tab[PATH_MAX];
5d4e5cd
+    char line[512];
5d4e5cd
+    FILE *fp;
5d4e5cd
+
5d4e5cd
+    strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
5d4e5cd
+
5d4e5cd
+    fp = fopen(zone_tab, "r");
5d4e5cd
+    if (!fp) {
5d4e5cd
+        return NULL;
5d4e5cd
+    }
5d4e5cd
+
5d4e5cd
+    li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
5d4e5cd
+
5d4e5cd
+    while (fgets(line, sizeof line, fp)) {
5d4e5cd
+        char *p = line, *code, *name, *comment;
5d4e5cd
+        uint32_t hash;
5d4e5cd
+        double latitude, longitude;
5d4e5cd
+
5d4e5cd
+        while (isspace(*p))
5d4e5cd
+            p++;
5d4e5cd
+
5d4e5cd
+        if (*p == '#' || *p == '\0' || *p == '\n')
5d4e5cd
+            continue;
5d4e5cd
+        
5d4e5cd
+        if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
5d4e5cd
+            continue;
5d4e5cd
+        
5d4e5cd
+        /* code => AA */
5d4e5cd
+        code = p;
5d4e5cd
+        p[2] = 0;
5d4e5cd
+        p += 3;
5d4e5cd
+
5d4e5cd
+        /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
5d4e5cd
+        p = parse_iso6709(p, &latitude);
5d4e5cd
+        if (!p) {
5d4e5cd
+            continue;
5d4e5cd
+        }
5d4e5cd
+        p = parse_iso6709(p, &longitude);
5d4e5cd
+        if (!p) {
5d4e5cd
+            continue;
5d4e5cd
+        }
5d4e5cd
+
5d4e5cd
+        if (!p || *p != '\t') {
5d4e5cd
+            continue;
5d4e5cd
+        }
5d4e5cd
+
5d4e5cd
+        /* name = string */
5d4e5cd
+        name = ++p;
5d4e5cd
+        while (*p != '\t' && *p && *p != '\n')
5d4e5cd
+            p++;
5d4e5cd
+
5d4e5cd
+        *p++ = '\0';
5d4e5cd
+
5d4e5cd
+        /* comment = string */
5d4e5cd
+        comment = p;
5d4e5cd
+        while (*p != '\t' && *p && *p != '\n')
5d4e5cd
+            p++;
5d4e5cd
+
5d4e5cd
+        if (*p == '\n' || *p == '\t')
5d4e5cd
+            *p = '\0';
5d4e5cd
+        
5d4e5cd
+        hash = tz_hash(name);
5d4e5cd
+        i = malloc(sizeof *i);
5d4e5cd
+        memcpy(i->code, code, 2);
5d4e5cd
+        strncpy(i->name, name, sizeof i->name);
5d4e5cd
+        i->comment = strdup(comment);
5d4e5cd
+        i->longitude = longitude;
5d4e5cd
+        i->latitude = latitude;
5d4e5cd
+        i->next = li[hash];
5d4e5cd
+        li[hash] = i;
1890efc
+        /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
5d4e5cd
+    }
1890efc
+
1890efc
+    fclose(fp);
1890efc
+
5d4e5cd
+    return li;
5d4e5cd
+}
5d4e5cd
+
5d4e5cd
+/* Return location info from hash table, using given timezone name.
5d4e5cd
+ * Returns NULL if the name could not be found. */
5d4e5cd
+const struct location_info *find_zone_info(struct location_info **li, 
5d4e5cd
+                                           const char *name)
5d4e5cd
+{
5d4e5cd
+    uint32_t hash = tz_hash(name);
5d4e5cd
+    const struct location_info *l;
5d4e5cd
+
5d4e5cd
+    if (!li) {
5d4e5cd
+        return NULL;
5d4e5cd
+    }
5d4e5cd
+
5d4e5cd
+    for (l = li[hash]; l; l = l->next) {
5d4e5cd
+        if (strcasecmp(l->name, name) == 0)
5d4e5cd
+            return l;
5d4e5cd
+    }
5d4e5cd
+
5d4e5cd
+    return NULL;
5d4e5cd
+}    
5d4e5cd
+
5d4e5cd
+/* Filter out some non-tzdata files and the posix/right databases, if
5d4e5cd
+ * present. */
5d4e5cd
+static int index_filter(const struct dirent *ent)
5d4e5cd
+{
5d4e5cd
+	return strcmp(ent->d_name, ".") != 0
5d4e5cd
+		&& strcmp(ent->d_name, "..") != 0
5d4e5cd
+		&& strcmp(ent->d_name, "posix") != 0
5d4e5cd
+		&& strcmp(ent->d_name, "posixrules") != 0
5d4e5cd
+		&& strcmp(ent->d_name, "right") != 0
5d4e5cd
+		&& strstr(ent->d_name, ".tab") == NULL;
5d4e5cd
+}
5d4e5cd
+
1890efc
+static int sysdbcmp(const void *first, const void *second)
1890efc
+{
1890efc
+        const timelib_tzdb_index_entry *alpha = first, *beta = second;
1890efc
+
1890efc
+        return strcmp(alpha->id, beta->id);
1890efc
+}
1890efc
+
1890efc
+
5d4e5cd
+/* Create the zone identifier index by trawling the filesystem. */
5d4e5cd
+static void create_zone_index(timelib_tzdb *db)
5d4e5cd
+{
5d4e5cd
+	size_t dirstack_size,  dirstack_top;
5d4e5cd
+	size_t index_size, index_next;
5d4e5cd
+	timelib_tzdb_index_entry *db_index;
5d4e5cd
+	char **dirstack;
5d4e5cd
+
5d4e5cd
+	/* LIFO stack to hold directory entries to scan; each slot is a
5d4e5cd
+	 * directory name relative to the zoneinfo prefix. */
5d4e5cd
+	dirstack_size = 32;
5d4e5cd
+	dirstack = malloc(dirstack_size * sizeof *dirstack);
5d4e5cd
+	dirstack_top = 1;
5d4e5cd
+	dirstack[0] = strdup("");
5d4e5cd
+	
5d4e5cd
+	/* Index array. */
5d4e5cd
+	index_size = 64;
5d4e5cd
+	db_index = malloc(index_size * sizeof *db_index);
5d4e5cd
+	index_next = 0;
5d4e5cd
+
5d4e5cd
+	do {
5d4e5cd
+		struct dirent **ents;
5d4e5cd
+		char name[PATH_MAX], *top;
5d4e5cd
+		int count;
5d4e5cd
+
5d4e5cd
+		/* Pop the top stack entry, and iterate through its contents. */
5d4e5cd
+		top = dirstack[--dirstack_top];
5d4e5cd
+		snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
5d4e5cd
+
5d4e5cd
+		count = php_scandir(name, &ents, index_filter, php_alphasort);
5d4e5cd
+
5d4e5cd
+		while (count > 0) {
5d4e5cd
+			struct stat st;
5d4e5cd
+			const char *leaf = ents[count - 1]->d_name;
5d4e5cd
+
5d4e5cd
+			snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", 
5d4e5cd
+				 top, leaf);
5d4e5cd
+			
5d4e5cd
+			if (strlen(name) && stat(name, &st) == 0) {
5d4e5cd
+				/* Name, relative to the zoneinfo prefix. */
5d4e5cd
+				const char *root = top;
5d4e5cd
+
5d4e5cd
+				if (root[0] == '/') root++;
5d4e5cd
+
5d4e5cd
+				snprintf(name, sizeof name, "%s%s%s", root, 
5d4e5cd
+					 *root ? "/": "", leaf);
5d4e5cd
+
5d4e5cd
+				if (S_ISDIR(st.st_mode)) {
5d4e5cd
+					if (dirstack_top == dirstack_size) {
5d4e5cd
+						dirstack_size *= 2;
5d4e5cd
+						dirstack = realloc(dirstack, 
5d4e5cd
+								   dirstack_size * sizeof *dirstack);
5d4e5cd
+					}
5d4e5cd
+					dirstack[dirstack_top++] = strdup(name);
5d4e5cd
+				}
5d4e5cd
+				else {
5d4e5cd
+					if (index_next == index_size) {
5d4e5cd
+						index_size *= 2;
5d4e5cd
+						db_index = realloc(db_index,
5d4e5cd
+								   index_size * sizeof *db_index);
5d4e5cd
+					}
5d4e5cd
+
1890efc
+					db_index[index_next++].id = strdup(name);
5d4e5cd
+				}
5d4e5cd
+			}
5d4e5cd
+
5d4e5cd
+			free(ents[--count]);
5d4e5cd
+		}
5d4e5cd
+		
5d4e5cd
+		if (count != -1) free(ents);
5d4e5cd
+		free(top);
5d4e5cd
+	} while (dirstack_top);
5d4e5cd
+
1890efc
+        qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
1890efc
+
5d4e5cd
+	db->index = db_index;
5d4e5cd
+	db->index_size = index_next;
5d4e5cd
+
5d4e5cd
+	free(dirstack);
5d4e5cd
+}
5d4e5cd
+
1890efc
+#define FAKE_HEADER "1234\0??\1??"
1890efc
+#define FAKE_UTC_POS (7 - 4)
1890efc
+
1890efc
+/* Create a fake data segment for database 'sysdb'. */
1890efc
+static void fake_data_segment(timelib_tzdb *sysdb,
1890efc
+                              struct location_info **info)
1890efc
+{
1890efc
+        size_t n;
1890efc
+        char *data, *p;
1890efc
+        
1890efc
+        data = malloc(3 * sysdb->index_size + 7);
1890efc
+
1890efc
+        p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
1890efc
+
1890efc
+        for (n = 0; n < sysdb->index_size; n++) {
1890efc
+                const struct location_info *li;
1890efc
+                timelib_tzdb_index_entry *ent;
1890efc
+
1890efc
+                ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
1890efc
+
1890efc
+                /* Lookup the timezone name in the hash table. */
1890efc
+                if (strcmp(ent->id, "UTC") == 0) {
1890efc
+                        ent->pos = FAKE_UTC_POS;
1890efc
+                        continue;
1890efc
+                }
1890efc
+
1890efc
+                li = find_zone_info(info, ent->id);
1890efc
+                if (li) {
1890efc
+                        /* If found, append the BC byte and the
1890efc
+                         * country code; set the position for this
1890efc
+                         * section of timezone data.  */
1890efc
+                        ent->pos = (p - data) - 4;
1890efc
+                        *p++ = '\1';
1890efc
+                        *p++ = li->code[0];
1890efc
+                        *p++ = li->code[1];
1890efc
+                }
1890efc
+                else {
1890efc
+                        /* If not found, the timezone data can
1890efc
+                         * point at the header. */
1890efc
+                        ent->pos = 0;
1890efc
+                }
1890efc
+        }
1890efc
+        
1890efc
+        sysdb->data = (unsigned char *)data;
1890efc
+}
1890efc
+
3bd1eb2
+/* Returns true if the passed-in stat structure describes a
3bd1eb2
+ * probably-valid timezone file. */
3bd1eb2
+static int is_valid_tzfile(const struct stat *st)
3bd1eb2
+{
3bd1eb2
+	return S_ISREG(st->st_mode) && st->st_size > 20;
3bd1eb2
+}
3bd1eb2
+
5d4e5cd
+/* Return the mmap()ed tzfile if found, else NULL.  On success, the
5d4e5cd
+ * length of the mapped data is placed in *length. */
5d4e5cd
+static char *map_tzfile(const char *timezone, size_t *length)
5d4e5cd
+{
5d4e5cd
+	char fname[PATH_MAX];
5d4e5cd
+	struct stat st;
5d4e5cd
+	char *p;
5d4e5cd
+	int fd;
5d4e5cd
+	
1890efc
+	if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
5d4e5cd
+		return NULL;
5d4e5cd
+	}
5d4e5cd
+
5d4e5cd
+	snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
5d4e5cd
+	
5d4e5cd
+	fd = open(fname, O_RDONLY);
5d4e5cd
+	if (fd == -1) {
5d4e5cd
+		return NULL;
3bd1eb2
+	} else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
5d4e5cd
+		close(fd);
5d4e5cd
+		return NULL;
5d4e5cd
+	}
5d4e5cd
+
5d4e5cd
+	*length = st.st_size;
5d4e5cd
+	p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
5d4e5cd
+	close(fd);
5d4e5cd
+	
5d4e5cd
+	return p != MAP_FAILED ? p : NULL;
5d4e5cd
+}
5d4e5cd
+
1890efc
+#endif
1890efc
+
1890efc
+static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
5f68886
 {
1890efc
 	int left = 0, right = tzdb->index_size - 1;
1890efc
 #ifdef HAVE_SETLOCALE
3bd1eb2
@@ -292,36 +702,125 @@ static int seek_to_tz_position(const uns
1890efc
 	return 0;
1890efc
 }
1890efc
 
1890efc
+static int seek_to_tz_position(const unsigned char **tzf, char *timezone, 
1890efc
+			       char **map, size_t *maplen,
1890efc
+			       const timelib_tzdb *tzdb)
1890efc
+{
1890efc
+	if (tzdb == timezonedb_system) {
1890efc
+		char *orig;
1890efc
+
1890efc
+		orig = map_tzfile(timezone, maplen);
1890efc
+		if (orig == NULL) {
1890efc
+			return 0;
1890efc
+		}
1890efc
+		
1890efc
+		(*tzf) = (unsigned char *)orig ;
1890efc
+		*map = orig;
1890efc
+                
1890efc
+                return 1;
1890efc
+	}
1890efc
+	else {
1890efc
+		return inmem_seek_to_tz_position(tzf, timezone, tzdb);
1890efc
+	}
1890efc
+}
1890efc
+
1890efc
 const timelib_tzdb *timelib_builtin_db(void)
1890efc
 {
1890efc
+#ifdef HAVE_SYSTEM_TZDATA
5d4e5cd
+	if (timezonedb_system == NULL) {
5d4e5cd
+		timelib_tzdb *tmp = malloc(sizeof *tmp);
5d4e5cd
+
5d4e5cd
+		tmp->version = "0.system";
5d4e5cd
+		tmp->data = NULL;
5d4e5cd
+		create_zone_index(tmp);
1890efc
+		system_location_table = create_location_table();
1890efc
+                fake_data_segment(tmp, system_location_table);
5d4e5cd
+		timezonedb_system = tmp;
5d4e5cd
+	}
1890efc
+
5d4e5cd
+			
5d4e5cd
+	return timezonedb_system;
1890efc
+#else
1890efc
 	return &timezonedb_builtin;
1890efc
+#endif
1890efc
 }
1890efc
 
1890efc
 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
1890efc
 {
1890efc
+#ifdef HAVE_SYSTEM_TZDATA
5d4e5cd
+	*count = timezonedb_system->index_size;
5d4e5cd
+	return timezonedb_system->index;
1890efc
+#else
1890efc
 	*count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
1890efc
 	return timezonedb_idx_builtin;
1890efc
+#endif
1890efc
 }
1890efc
 
1890efc
 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
1890efc
 {
1890efc
 	const unsigned char *tzf;
1890efc
-	return (seek_to_tz_position(&tzf, timezone, tzdb));
5d4e5cd
+
1890efc
+#ifdef HAVE_SYSTEM_TZDATA
1890efc
+        if (tzdb == timezonedb_system) {
1890efc
+            char fname[PATH_MAX];
3bd1eb2
+            struct stat st;
3bd1eb2
+
1890efc
+            if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
5d4e5cd
+		return 0;
1890efc
+            }
1890efc
+            
1890efc
+            snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
1890efc
+            
3bd1eb2
+            return stat(fname, &st) == 0 && is_valid_tzfile(&st);
5d4e5cd
+        }
1890efc
+#endif
5d4e5cd
+
1890efc
+	return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
5d4e5cd
 }
5d4e5cd
 
1890efc
 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
5d4e5cd
 {
1890efc
 	const unsigned char *tzf;
1890efc
+	char *memmap = NULL;
1890efc
+	size_t maplen;
1890efc
 	timelib_tzinfo *tmp;
5d4e5cd
 
1890efc
-	if (seek_to_tz_position(&tzf, timezone, tzdb)) {
1890efc
+	if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
1890efc
 		tmp = timelib_tzinfo_ctor(timezone);
1890efc
 
1890efc
 		read_preamble(&tzf, tmp);
1890efc
 		read_header(&tzf, tmp);
1890efc
 		read_transistions(&tzf, tmp);
1890efc
 		read_types(&tzf, tmp);
1890efc
-		read_location(&tzf, tmp);
5d4e5cd
+
1890efc
+#ifdef HAVE_SYSTEM_TZDATA
1890efc
+		if (memmap) {
1890efc
+			const struct location_info *li;
1890efc
+
1890efc
+			/* TZif-style - grok the location info from the system database,
1890efc
+			 * if possible. */
1890efc
+
1890efc
+			if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
1890efc
+				tmp->location.comments = strdup(li->comment);
1890efc
+                                strncpy(tmp->location.country_code, li->code, 2);
1890efc
+				tmp->location.longitude = li->longitude;
1890efc
+				tmp->location.latitude = li->latitude;
1890efc
+				tmp->bc = 1;
1890efc
+			}
1890efc
+			else {
1890efc
+				strcpy(tmp->location.country_code, "??");
1890efc
+				tmp->bc = 0;
1890efc
+				tmp->location.comments = strdup("");
1890efc
+			}
5d4e5cd
+
1890efc
+			/* Now done with the mmap segment - discard it. */
1890efc
+			munmap(memmap, maplen);
1890efc
+#endif
1890efc
+		}
1890efc
+		else {
1890efc
+			/* PHP-style - use the embedded info. */
1890efc
+			read_location(&tzf, tmp);
1890efc
+		}
1890efc
 	} else {
1890efc
 		tmp = NULL;
1890efc
 	}
3bd1eb2
--- php-5.3.1/ext/date/lib/timelib.m4.systzdata
3bd1eb2
+++ php-5.3.1/ext/date/lib/timelib.m4
5f68886
@@ -78,3 +78,17 @@ stdlib.h
5f68886
 
5f68886
 dnl Check for strtoll, atoll
5f68886
 AC_CHECK_FUNCS(strtoll atoll strftime)
5f68886
+
5f68886
+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
5f68886
+[  --with-system-tzdata[=DIR]      to specify use of system timezone data],
5f68886
+no, no)
5f68886
+
5f68886
+if test "$PHP_SYSTEM_TZDATA" != "no"; then
5f68886
+   AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
5f68886
+
5f68886
+   if test "$PHP_SYSTEM_TZDATA" != "yes"; then
5f68886
+      AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
5f68886
+                         [Define for location of system timezone data])
5f68886
+   fi
5f68886
+fi
5f68886
+