* */ set_time_limit(600); list($usec, $sec) = explode(" ", microtime()); $start = (double) $usec + (double) $sec; require_once './wurfl_config.php'; define('FORCED_UPDATE', true); require_once WURFL_PARSER_FILE; list($usec, $sec) = explode(" ", microtime()); $load_parser = (double) $usec + (double) $sec; wurfl_log('update_cache', "Forced cache update started"); if (WURFL_USE_CACHE === true) { parse(); if (WURFL_USE_MULTICACHE === true) { wurfl_log('update_cache', "Updating multicache dir"); touch(MULTICACHE_TOUCH); if (is_dir(MULTICACHE_DIR)) { rename(substr(MULTICACHE_DIR, 0, -1), substr(MULTICACHE_DIR, 0, -1) . '.' . time()); } rename(substr(MULTICACHE_TMP_DIR, 0, -1), substr(MULTICACHE_DIR, 0, -1)); unlink(MULTICACHE_TOUCH); } wurfl_log('update_cache', "Done updating cache"); } else { wurfl_log('update_cache', "Why update cache if WURFL_USE_CACHE is not set to true?"); } list($usec, $sec) = explode(" ", microtime()); $parse = (double) $usec + (double) $sec; echo "Parser load time:" . ($load_parser - $start) . "<br>\n"; echo "Parsing time:" . ($parse - $load_parser) . "<br>\n"; echo "Total:" . ($parse - $start) . "<br>\n";
function parse() { global $wurfl, $wurfl_stat, $check_patch_params, $checkpatch_result; $wurfl = array(); $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if (!file_exists(WURFL_FILE)) { wurfl_log('parse', WURFL_FILE . " does not exist"); die(WURFL_FILE . " does not exist"); } if (!($fp = fopen(WURFL_FILE, "r"))) { wurfl_log('parse', "could not open XML input"); die("could not open XML input"); } //$count = 0; while ($data = fread($fp, 4096)) { //$count++; if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } //if ( $count > 30 ) //return; } fclose($fp); xml_parser_free($xml_parser); $check_patch_params = false; if (defined('WURFL_PATCH_FILE') && file_exists(WURFL_PATCH_FILE)) { wurfl_log('parse', "Trying to load XML patch file: " . WURFL_PATCH_FILE); $check_patch_params = true; $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = fopen(WURFL_PATCH_FILE, "r"))) { wurfl_log('parse', "could not open XML patch file: " . WURFL_PATCH_FILE); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } fclose($fp); xml_parser_free($xml_parser); // logging? $checkpatch_result['device']['id'] } else { if (defined('WURFL_PATCH_FILE') && !file_exists(WURFL_PATCH_FILE)) { wurfl_log('parse', WURFL_PATCH_FILE . " does not exist"); } else { wurfl_log('parse', "No XML patch file defined"); } } //reset($wurfl); //echo "<pre>"; //print_r($wurfl); //echo "</pre>"; reset($wurfl); $devices = $wurfl["devices"]; // I check if var_export loses any empty key, in this case I force the generic // device. if (var_export_bug()) { $wurfl_agents['generic'] = 'generic'; } foreach ($devices as $one) { $wurfl_agents[$one['user_agent']] = $one['id']; } reset($wurfl); reset($wurfl_agents); if (WURFL_USE_CACHE) { if (defined("WURFL_AGENT2ID_FILE") && file_exists(WURFL_AGENT2ID_FILE) && !is_writeable(WURFL_AGENT2ID_FILE)) { wurfl_log('parse', "ERROR: Unable to remove " . WURFL_AGENT2ID_FILE); //die ('Unable to remove '.WURFL_AGENT2ID_FILE); return; } if (isset($wurfl_stat)) { $cache_stat = $wurfl_stat; } else { $cache_stat = $wurfl_stat = filemtime(WURFL_FILE); if (defined('WURFL_PATCH_FILE') && file_exists(WURFL_PATCH_FILE)) { $patch_stat = filemtime(WURFL_PATCH_FILE); if ($patch_stat > $wurfl_stat) { // if the patch file is newer than the WURFL I set wurfl_stat to that time $wurfl_stat = $patch_stat; } } } if (WURFL_USE_MULTICACHE) { // If using Multicache remove old cache files $wurfl_temp_devices = $wurfl['devices']; $wurfl['devices'] = array(); //Attempt to remove all existing multicache files if (defined("MULTICACHE_DIR") && is_dir(MULTICACHE_DIR) && !is_writeable(MULTICACHE_DIR)) { wurfl_log('parse', "ERROR: Unable to remove files from" . MULTICACHE_DIR); return; } // Get all the agent file names in the multicache directory. Use // glob if available if (function_exists('glob')) { $filelist = glob(MULTICACHE_DIR . "/*" . MULTICACHE_SUFFIX); } else { if ($dh = @opendir(MULTICACHE_DIR)) { $filelist = array(); while (false !== ($file = @readdir($dh))) { $filename = MULTICACHE_DIR . "/{$file}"; if (is_file($filename)) { $filelist[] = $filename; } } @closedir($dh); } } foreach ($filelist as $filename) { @unlink($filename); } } $php_version = PHP_VERSION; list($php_main_version, $php_subversion, $php_subsubversion) = explode('.', $php_version); $fp_cache = fopen(CACHE_FILE, "w"); fwrite($fp_cache, "<?php\n"); // it seems until PHP 4.3.2 var_export had a problem with apostrophes in array keys if ($php_main_version == 4 && $php_subversion > 2 && $php_subsubversion > 2 || $php_main_version > 4) { if (!WURFL_USE_MULTICACHE) { $wurfl_to_file = var_export($wurfl, true); } $wurfl_agents_to_file = var_export($wurfl_agents, true); $cache_stat_to_file = var_export($cache_stat, true); fwrite($fp_cache, "\$cache_stat={$cache_stat_to_file};\n"); if (!WURFL_USE_MULTICACHE) { fwrite($fp_cache, "\$wurfl={$wurfl_to_file};\n"); } fwrite($fp_cache, "\$wurfl_agents={$wurfl_agents_to_file};\n"); } else { if (!WURFL_USE_MULTICACHE) { $wurfl_to_file = urlencode(serialize($wurfl)); } $wurfl_agents_to_file = urlencode(serialize($wurfl_agents)); $cache_stat_to_file = urlencode(serialize($cache_stat)); fwrite($fp_cache, "\$cache_stat=unserialize(urldecode(\"" . $cache_stat_to_file . "\"));\n"); if (!WURFL_USE_MULTICACHE) { fwrite($fp_cache, "\$wurfl=unserialize(urldecode(\"" . $wurfl_to_file . "\"));\n"); } fwrite($fp_cache, "\$wurfl_agents=unserialize(urldecode(\"" . $wurfl_agents_to_file . "\"));\n"); } fwrite($fp_cache, "?>\n"); fclose($fp_cache); if (defined("WURFL_AGENT2ID_FILE") && file_exists(WURFL_AGENT2ID_FILE)) { @unlink(WURFL_AGENT2ID_FILE); } if (WURFL_USE_MULTICACHE) { // Return the capabilities to the wurfl structure $wurfl['devices'] =& $wurfl_temp_devices; // Write multicache files if (FORCED_UPDATE === true) { $path = MULTICACHE_TMP_DIR; } else { $path = MULTICACHE_DIR; } if (!is_dir($path)) { @mkdir($path); } foreach ($wurfl_temp_devices as $id => $capabilities) { $fname = urlencode($id); $varname = addcslashes($id, "'\\"); $fp_cache = fopen($path . "/{$fname}" . MULTICACHE_SUFFIX, 'w'); fwrite($fp_cache, "<?php\n"); if ($php_main_version == 4 && $php_subversion > 2 || $php_main_version > 4) { $wurfl_to_file = var_export($capabilities, true); fwrite($fp_cache, "\$_cached_devices['{$varname}']={$wurfl_to_file};\n"); } else { $wurfl_to_file = urlencode(serialize($capabilities)); fwrite($fp_cache, "\$_cached_devices['{$varname}']=unserialize(urldecode(\"" . $wurfl_to_file . "\"));\n"); } fwrite($fp_cache, "?>\n"); fclose($fp_cache); } } // It's probably not really worth encoding cache.php if you're using Multicache if (0 && function_exists('mmcache_encode')) { $empty = ''; set_time_limit(60); $to_file = mmcache_encode(CACHE_FILE, $empty); $to_file = '<?php if (!is_callable("mmcache_load") && !@dl((PHP_OS=="WINNT"||PHP_OS=="WIN32")?"TurckLoader.dll":"TurckLoader.so")) { die("This PHP script has been encoded with Turck MMcache, to run it you must install <a href=\\"http://turck-mmcache.sourceforge.net/\\">Turck MMCache or Turck Loader</a>");} return mmcache_load(\'' . $to_file . "');?>\n"; $fp_cache = fopen(CACHE_FILE, "wb"); fwrite($fp_cache, $to_file); fclose($fp_cache); } //echo "cache written"; } else { // not using WURFL cache $cache_stat = 0; } return array($cache_stat, $wurfl, $wurfl_agents); }