| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-13 11:46:03 -04:00
										 |  |  | define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | define('LACONICA', true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once(INSTALLDIR . '/lib/common.php'); | 
					
						
							|  |  |  | require_once(INSTALLDIR . '/lib/util.php'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $output_paths = parse_args(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | standard_map(); | 
					
						
							|  |  |  | notices_map(); | 
					
						
							|  |  |  | user_map(); | 
					
						
							|  |  |  | index_map(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | # Main functions: get data out and turn them into sitemaps
 | 
					
						
							|  |  |  | # ------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Generate index sitemap of all other sitemaps.
 | 
					
						
							|  |  |  | function index_map() { | 
					
						
							|  |  |  | 	global $output_paths; | 
					
						
							|  |  |  | 	$output_dir = $output_paths['output_dir']; | 
					
						
							|  |  |  | 	$output_url = $output_paths['output_url']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	foreach (glob("$output_dir*.xml") as $file_name) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		# Just the file name please.
 | 
					
						
							|  |  |  | 		$file_name = preg_replace("|$output_dir|", '', $file_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 16:00:58 -04:00
										 |  |  | 		$index_urls .= sitemap( | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 						   array( | 
					
						
							|  |  |  | 								 'url' => $output_url . $file_name, | 
					
						
							| 
									
										
										
										
											2008-07-17 11:36:57 -04:00
										 |  |  | 								 'changefreq' => 'daily' | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 								 ) | 
					
						
							|  |  |  | 						   ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 16:00:58 -04:00
										 |  |  | 	write_file($output_paths['index_file'], sitemapindex($index_urls)); | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Generate sitemap of standard site elements.
 | 
					
						
							|  |  |  | function standard_map() { | 
					
						
							|  |  |  | 	global $output_paths; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$standard_map_urls .= url( | 
					
						
							|  |  |  | 							  array( | 
					
						
							|  |  |  | 									'url' => common_local_url('public'), | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 									'changefreq' => 'daily', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 									'priority' => '1', | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 							  ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$standard_map_urls .= url( | 
					
						
							|  |  |  | 							  array( | 
					
						
							|  |  |  | 									'url' => common_local_url('publicrss'), | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 									'changefreq' => 'daily', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 									'priority' => '0.3', | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 							  ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	foreach($docs as $title) { | 
					
						
							|  |  |  | 		$standard_map_urls .= url( | 
					
						
							|  |  |  | 								  array( | 
					
						
							|  |  |  | 										'url' => common_local_url('doc', array('title' => $title)), | 
					
						
							|  |  |  | 										'changefreq' => 'monthly', | 
					
						
							|  |  |  | 										'priority'   => '0.2', | 
					
						
							|  |  |  | 										) | 
					
						
							|  |  |  | 								  ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$urlset_path = $output_paths['output_dir'] . 'standard.xml'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	write_file($urlset_path, urlset($standard_map_urls)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Generate sitemaps of all notices.
 | 
					
						
							|  |  |  | function notices_map() { | 
					
						
							|  |  |  | 	global $output_paths; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$notices = DB_DataObject::factory('notice'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-12 08:18:32 -04:00
										 |  |  | 	$notices->query('SELECT id, uri, url, modified FROM notice'); | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	$notice_count = 0; | 
					
						
							|  |  |  | 	$map_count = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while ($notices->fetch()) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		# Maximum 50,000 URLs per sitemap file.
 | 
					
						
							|  |  |  | 		if ($notice_count == 50000) { | 
					
						
							|  |  |  | 			$notice_count = 0; | 
					
						
							|  |  |  | 			$map_count++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 21:16:00 -04:00
										 |  |  | 		# remote notices have an URL
 | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2008-08-12 08:18:32 -04:00
										 |  |  | 		if (!$notices->url && $notices->uri) { | 
					
						
							| 
									
										
										
										
											2008-08-11 21:16:00 -04:00
										 |  |  | 			$notice = array( | 
					
						
							| 
									
										
										
										
											2008-08-12 08:18:32 -04:00
										 |  |  | 						'url'        => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)), | 
					
						
							| 
									
										
										
										
											2008-07-17 11:52:12 -04:00
										 |  |  | 						'lastmod'    => common_date_w3dtf($notices->modified), | 
					
						
							| 
									
										
										
										
											2008-08-12 08:18:32 -04:00
										 |  |  | 						'changefreq' => 'never', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 						'priority'   => '1', | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 21:16:00 -04:00
										 |  |  | 			$notice_list[$map_count] .= url($notice); | 
					
						
							|  |  |  | 			$notice_count++; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# Make full sitemaps from the lists and save them.
 | 
					
						
							|  |  |  | 	array_to_map($notice_list, 'notice'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Generate sitemaps of all users.
 | 
					
						
							|  |  |  | function user_map() { | 
					
						
							|  |  |  | 	global $output_paths; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$users = DB_DataObject::factory('user'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$users->query('SELECT id, nickname FROM user'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$user_count = 0; | 
					
						
							|  |  |  | 	$map_count = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while ($users->fetch()) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		# Maximum 50,000 URLs per sitemap file.
 | 
					
						
							|  |  |  | 		if ($user_count == 50000) { | 
					
						
							|  |  |  | 			$user_count = 0; | 
					
						
							|  |  |  | 			$map_count++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$user_args = array('nickname' => $users->nickname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		# Define parameters for generating <url></url> elements.
 | 
					
						
							|  |  |  | 		$user = array( | 
					
						
							|  |  |  | 					  'url'        => common_local_url('showstream', $user_args), | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 					  'changefreq' => 'daily', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 					  'priority'   => '1', | 
					
						
							|  |  |  | 					  ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$user_rss = array( | 
					
						
							|  |  |  | 						  'url'        => common_local_url('userrss', $user_args), | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 						  'changefreq' => 'daily', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 						  'priority'   => '0.3', | 
					
						
							|  |  |  | 						  ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$all = array( | 
					
						
							|  |  |  | 					 'url'        => common_local_url('all', $user_args), | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 					 'changefreq' => 'daily', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 					 'priority'   => '1', | 
					
						
							|  |  |  | 					 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$all_rss = array( | 
					
						
							|  |  |  | 						 'url'        => common_local_url('allrss', $user_args), | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 						 'changefreq' => 'daily', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 						 'priority'   => '0.3', | 
					
						
							|  |  |  | 						 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$replies = array( | 
					
						
							|  |  |  | 						 'url'        => common_local_url('replies', $user_args), | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 						 'changefreq' => 'daily', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 						 'priority'   => '1', | 
					
						
							|  |  |  | 						 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$replies_rss = array( | 
					
						
							|  |  |  | 							 'url'        => common_local_url('repliesrss', $user_args), | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 							 'changefreq' => 'daily', | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 							 'priority'   => '0.3', | 
					
						
							|  |  |  | 							 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$foaf = array( | 
					
						
							|  |  |  | 					  'url'        => common_local_url('foaf', $user_args), | 
					
						
							|  |  |  | 					  'changefreq' => 'weekly', | 
					
						
							|  |  |  | 					  'priority'   => '0.5', | 
					
						
							|  |  |  | 					  ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		# Construct a <url></url> element for each user facet and add it
 | 
					
						
							|  |  |  | 		# to our existing list of those.
 | 
					
						
							|  |  |  | 		$user_list[$map_count]        .= url($user); | 
					
						
							|  |  |  | 		$user_rss_list[$map_count]    .= url($user_rss); | 
					
						
							|  |  |  | 		$all_list[$map_count]         .= url($all); | 
					
						
							|  |  |  | 		$all_rss_list[$map_count]     .= url($all_rss); | 
					
						
							|  |  |  | 		$replies_list[$map_count]     .= url($replies); | 
					
						
							|  |  |  | 		$replies_rss_list[$map_count] .= url($replies_rss); | 
					
						
							|  |  |  | 		$foaf_list[$map_count]        .= url($foaf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$user_count++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# Make full sitemaps from the lists and save them.
 | 
					
						
							|  |  |  | 	# Possible factoring: put all the lists into a master array, thus allowing
 | 
					
						
							|  |  |  | 	# calling with single argument (i.e., array_to_map('user')).
 | 
					
						
							|  |  |  | 	array_to_map($user_list, 'user'); | 
					
						
							|  |  |  | 	array_to_map($user_rss_list, 'user_rss'); | 
					
						
							|  |  |  | 	array_to_map($all_list, 'all'); | 
					
						
							|  |  |  | 	array_to_map($all_rss_list, 'all_rss'); | 
					
						
							|  |  |  | 	array_to_map($replies_list, 'replies'); | 
					
						
							|  |  |  | 	array_to_map($replies_rss_list, 'replies_rss'); | 
					
						
							|  |  |  | 	array_to_map($foaf_list, 'foaf'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | # XML generation functions
 | 
					
						
							|  |  |  | # ------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Generate a <url></url> element.
 | 
					
						
							|  |  |  | function url($url_args) { | 
					
						
							|  |  |  | 	$url        = preg_replace('/&/', '&', $url_args['url']); # escape ampersands for XML
 | 
					
						
							|  |  |  | 	$lastmod    = $url_args['lastmod']; | 
					
						
							|  |  |  | 	$changefreq = $url_args['changefreq']; | 
					
						
							|  |  |  | 	$priority   = $url_args['priority']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (is_null($url)) { | 
					
						
							|  |  |  | 		error("url() arguments require 'url' value."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$url_out = "\t<url>\n"; | 
					
						
							|  |  |  | 	$url_out .= "\t\t<loc>$url</loc>\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ($changefreq) { | 
					
						
							|  |  |  | 		$url_out .= "\t\t<changefreq>$changefreq</changefreq>\n"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ($lastmod) { | 
					
						
							|  |  |  | 		$url_out .= "\t\t<lastmod>$lastmod</lastmod>\n"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ($priority) { | 
					
						
							|  |  |  | 		$url_out .= "\t\t<priority>$priority</priority>\n"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$url_out .= "\t</url>\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return $url_out; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 16:00:58 -04:00
										 |  |  | function sitemap($sitemap_args) { | 
					
						
							|  |  |  | 	$url        = preg_replace('/&/', '&', $sitemap_args['url']); # escape ampersands for XML
 | 
					
						
							|  |  |  | 	$lastmod    = $sitemap_args['lastmod']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (is_null($url)) { | 
					
						
							|  |  |  | 		error("url() arguments require 'url' value."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$sitemap_out = "\t<sitemap>\n"; | 
					
						
							|  |  |  | 	$sitemap_out .= "\t\t<loc>$url</loc>\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ($lastmod) { | 
					
						
							|  |  |  | 		$sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$sitemap_out .= "\t</sitemap>\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return $sitemap_out; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | # Generate a <urlset></urlset> element.
 | 
					
						
							|  |  |  | function urlset($urlset_text) { | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 	$urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . | 
					
						
							|  |  |  | 	  '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" . | 
					
						
							|  |  |  | 	  $urlset_text . | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 	  '</urlset>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return $urlset; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 16:00:58 -04:00
										 |  |  | # Generate a <urlset></urlset> element.
 | 
					
						
							|  |  |  | function sitemapindex($sitemapindex_text) { | 
					
						
							|  |  |  | 	$sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . | 
					
						
							|  |  |  | 	  '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" . | 
					
						
							|  |  |  | 	  $sitemapindex_text . | 
					
						
							|  |  |  | 	  '</sitemapindex>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return $sitemapindex; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | # Generate a sitemap from an array containing <url></url> elements and write it to a file.
 | 
					
						
							|  |  |  | function array_to_map($url_list, $filename_prefix) { | 
					
						
							|  |  |  | 	global $output_paths; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-15 20:44:14 -04:00
										 |  |  | 	if ($url_list) { | 
					
						
							|  |  |  | 		# $map_urls is a long string containing concatenated <url></url> elements.
 | 
					
						
							|  |  |  | 		while (list($map_idx, $map_urls) = each($url_list)) { | 
					
						
							|  |  |  | 			$urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml"; | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			write_file($urlset_path, urlset($map_urls)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | # Internal functions
 | 
					
						
							|  |  |  | # ------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Parse command line arguments.
 | 
					
						
							|  |  |  | function parse_args() { | 
					
						
							|  |  |  | 	$args = getopt('f:d:u:'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) { | 
					
						
							| 
									
										
										
										
											2008-07-13 20:08:33 -04:00
										 |  |  | 		error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>'); | 
					
						
							| 
									
										
										
										
											2008-07-12 20:49:36 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (is_null($args[f])) { | 
					
						
							|  |  |  | 		error('You must specify an index file name with the -f option.'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (is_null($args[d])) { | 
					
						
							|  |  |  | 		error('You must specify a directory for the output file with the -d option.'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (is_null($args[u])) { | 
					
						
							|  |  |  | 		error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$index_file = $args[f]; | 
					
						
							|  |  |  | 	$output_dir = $args[d]; | 
					
						
							|  |  |  | 	$output_url = $args[u]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (file_exists($output_dir)) { | 
					
						
							|  |  |  | 		if (is_writable($output_dir) === FALSE) { | 
					
						
							|  |  |  | 			error("$output_dir is not writable."); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}	 else { | 
					
						
							|  |  |  | 		error("output directory $output_dir does not exist."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	$paths = array( | 
					
						
							|  |  |  | 				   'index_file' => $index_file, | 
					
						
							|  |  |  | 				   'output_dir' => trailing_slash($output_dir), | 
					
						
							|  |  |  | 				   'output_url' => trailing_slash($output_url), | 
					
						
							|  |  |  | 				   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return $paths; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Ensure paths end with a "/".
 | 
					
						
							|  |  |  | function trailing_slash($path) { | 
					
						
							|  |  |  | 	if (preg_match('/\/$/', $path) == 0) { | 
					
						
							|  |  |  | 		$path .= '/'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return $path; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Write data to disk.
 | 
					
						
							|  |  |  | function write_file($path, $data) { | 
					
						
							|  |  |  | 	if (is_null($path)) { | 
					
						
							|  |  |  | 		error('No path specified for writing to.'); | 
					
						
							|  |  |  | 	}	 elseif (is_null($data)) { | 
					
						
							|  |  |  | 		error('No data specified for writing.'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (($fh_out = fopen($path,'w')) === FALSE) { | 
					
						
							|  |  |  | 		error("couldn't open $path for writing."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (fwrite($fh_out, $data) === FALSE) { | 
					
						
							|  |  |  | 		error("couldn't write to $path."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Display an error message and exit.
 | 
					
						
							|  |  |  | function error ($error_msg) { | 
					
						
							|  |  |  | 	if (is_null($error_msg)) { | 
					
						
							|  |  |  | 		$error_msg = 'error() was called without any explanation!'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	echo "Error: $error_msg\n"; | 
					
						
							|  |  |  | 	exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?>
 |