<?phpphp

	/*==================================================================*\
	######################################################################
	#                                                                    #
	# Copyright 2005 Arca Solutions, Inc. All Rights Reserved.           #
	#                                                                    #
	# This file may not be redistributed in whole or part.               #
	# eDirectory is licensed on a per-domain basis.                      #
	#                                                                    #
	# ---------------- eDirectory IS NOT FREE SOFTWARE ----------------- #
	#                                                                    #
	# http://www.edirectory.com | http://www.edirectory.com/license.html #
	######################################################################
	\*==================================================================*/

	# ----------------------------------------------------------------------------------------------------
	# * FILE: /cron/sitemapgen_funct.php
	# ----------------------------------------------------------------------------------------------------

	function sitemapgen_makeSitemap($path) {

		if (SITEMAP_FEATURE == "on") {

			$access = true;
			$dbObj = db_getDBObject(DEFAULT_DB,true);
			$sql = "SELECT * FROM Registration WHERE name = 'extra' AND domain = '' AND date_time = '0000-00-00 00:00:00' LIMIT 1";
			$result = $dbObj->query($sql);
			if ($result) {
				if ($row = mysql_fetch_assoc($result)) {
					if ($row["value"] == string_strtoupper(md5(VERSION."SITEMAP_FEATUREon"))) {
						$access = true;
					}
				}
			}

			if ($access) {

				$files_listinglocation = false;
				$files_listingcategory = false;
				$files_listingdetail = false;
				$files_promotionlocation = false;
				$files_promotioncategory = false;
				$files_eventlocation = false;
				$files_eventcategory = false;
				$files_eventdetail = false;
				$files_classifiedlocation = false;
				$files_classifiedcategory = false;
				$files_classifieddetail = false;
				$files_articlecategory = false;
				$files_articledetail = false;
				$files_content = false;

                if (SITEMAP_HASLISTINGCATEGORY == "y"){
                    unset($aux_listingcategory);
                    $aux_listingcategory = sitemap_createModuleCategories($path, 'listing');
                    if(is_array($aux_listingcategory)){
                        $files_listingcategory[] = $aux_listingcategory;
                    }

                }

                if (PROMOTION_FEATURE == 'on' && CUSTOM_PROMOTION_FEATURE == "on" && SITEMAP_HASPROMOTIONCATEGORY == "y"){
                    unset($aux_promotioncategory);
                    $aux_promotioncategory = sitemap_createModuleCategories($path, 'promotion');
                    if(is_array($aux_promotioncategory)){
                        $files_promotioncategory[] = $aux_promotioncategory;
                    }

                }

                if (EVENT_FEATURE == 'on' && CUSTOM_EVENT_FEATURE == "on" && SITEMAP_HASEVENTCATEGORY == "y"){
                    unset($aux_eventcategory);
                    $aux_eventcategory = sitemap_createModuleCategories($path, 'event');
                    if(is_array($aux_eventcategory)){
                        $files_eventcategory[] = $aux_eventcategory;
                    }


                }

                if (CLASSIFIED_FEATURE == 'on' && CUSTOM_CLASSIFIED_FEATURE == "on" && SITEMAP_HASCLASSIFIEDCATEGORY == "y"){
                    unset($aux_classifiedcategory);
                    $aux_classifiedcategory = sitemap_createModuleCategories($path, 'classified');
                    if(is_array($aux_classifiedcategory)){
                        $files_classifiedcategory[] = $aux_classifiedcategory;
                    }

                }

                if (ARTICLE_FEATURE == 'on' && CUSTOM_ARTICLE_FEATURE == "on" && SITEMAP_HASARTICLECATEGORY == "y"){
                    unset($aux_articlecategory);
                    $aux_articlecategory = sitemap_createModuleCategories($path, 'article');
                    if(is_array($aux_articlecategory)){
                        $files_articlecategory[] = $aux_articlecategory;
                    }

                }

                if (BLOG_FEATURE == 'on' && CUSTOM_BLOG_FEATURE == 'on' && SITEMAP_HASBLOGCATEGORY == "y"){
                    unset($aux_blogcategory);
                    $aux_blogcategory = sitemap_createModuleCategories($path, 'blog');
                    if(is_array($aux_blogcategory)){
                        $files_blogcategory[] = $aux_blogcategory;
                    }
                }

                if (SITEMAP_HASLISTINGLOCATION == "y"){
                    $files_listinglocation = sitemap_createModuleLocations($path, 'listing');
                }
                
				if (SITEMAP_HASLISTINGDETAIL == "y"){
                    $files_listingdetail = sitemap_createModuleDetails($path, 'listing');
                }

                if (PROMOTION_FEATURE == "on" && CUSTOM_PROMOTION_FEATURE == "on"){
                    if (SITEMAP_HASPROMOTIONLOCATION == "y"){
                        $files_promotionlocation = sitemap_createModuleLocations($path, 'promotion');
                    }
                    
                    if (SITEMAP_HASPROMOTIONDETAIL == "y"){
                        $files_promotiondetail = sitemap_createModuleDetails($path, 'promotion');
                    }
                }
				
				if (EVENT_FEATURE == 'on' && CUSTOM_EVENT_FEATURE == "on") {
					if (SITEMAP_HASEVENTLOCATION == "y"){
                        $files_eventlocation = sitemap_createModuleLocations($path, 'event');
                    }
					
					if (SITEMAP_HASEVENTDETAIL == "y"){
                        $files_eventdetail = sitemap_createModuleDetails($path, 'event');
                    }
				}

				if (CLASSIFIED_FEATURE == 'on' && CUSTOM_CLASSIFIED_FEATURE == "on") {
					if (SITEMAP_HASCLASSIFIEDLOCATION == "y"){
                        $files_classifiedlocation = sitemap_createModuleLocations($path, 'classified');
                    }
					
					if (SITEMAP_HASCLASSIFIEDDETAIL == "y"){
                        $files_classifieddetail = sitemap_createModuleDetails($path, 'classified');
                    }
				}

				if (ARTICLE_FEATURE == 'on' && CUSTOM_ARTICLE_FEATURE == "on") {
			
					if (SITEMAP_HASARTICLEDETAIL == "y"){
                        $files_articledetail = sitemap_createModuleDetails($path, 'article');
                    }
				}

				if (BLOG_FEATURE == 'on' && CUSTOM_BLOG_FEATURE == 'on') {
				
					if (SITEMAP_HASBLOGDETAIL == "y"){
                        $files_blogdetail = sitemap_createModuleDetails($path, 'blog');
                    }
				}

				if (SITEMAP_HASCONTENT == "y"){
                    $files_content = sitemap_createContentMap($path);
                }
                

				$sitemap_buffer = "";
				$sitemap_buffer .= sitemap_indexPrintHeader();
				if ($files_listinglocation) {
					foreach ($files_listinglocation as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				if ($files_listingcategory) {
					foreach ($files_listingcategory as $file) {
                        if(is_array($file)){
                            for($i=0;$i<count($file);$i++){
                                $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file[$i]);
                            }
                        }else{
                            $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
                        }
					}
				}
				if ($files_listingdetail) {
					foreach ($files_listingdetail as $file) {
                        
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				if ($files_promotionlocation) {
					foreach ($files_promotionlocation as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				if ($files_promotioncategory) {
					foreach ($files_promotioncategory as $file) {
						if(is_array($file)){
                            for($i=0;$i<count($file);$i++){
                                $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file[$i]);
                            }
                        }else{
                            $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
                        }
					}
				}
                if ($files_promotiondetail) {
					foreach ($files_promotiondetail as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				if ($files_eventlocation) {
					foreach ($files_eventlocation as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				if ($files_eventcategory) {
					foreach ($files_eventcategory as $file) {
						if(is_array($file)){
                            for($i=0;$i<count($file);$i++){
                                $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file[$i]);
                            }
                        }else{
                            $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
                        }
					}
				}
				if ($files_eventdetail) {
					foreach ($files_eventdetail as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				if ($files_classifiedlocation) {
					foreach ($files_classifiedlocation as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				if ($files_classifiedcategory) {
					foreach ($files_classifiedcategory as $file) {
                        if(is_array($file)){
                            for($i=0;$i<count($file);$i++){
                                $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file[$i]);
                            }
                        }else{
                            $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
                        }
					}
				}
				if ($files_classifieddetail) {
					foreach ($files_classifieddetail as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				if ($files_articlecategory) {
					foreach ($files_articlecategory as $file) {
						if(is_array($file)){
                            for($i=0;$i<count($file);$i++){
                                $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file[$i]);
                            }
                        }else{
                            $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
                        }
					}
				}
				if ($files_articledetail) {
					foreach ($files_articledetail as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}

				if ($files_blogcategory) {
					foreach ($files_blogcategory as $file) {
                        if(is_array($file)){
                            for($i=0;$i<count($file);$i++){
                                $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file[$i]);
                            }
                        }else{
                            $sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
                        }
					}
				}
				if ($files_blogdetail) {
					foreach ($files_blogdetail as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}

				if ($files_content) {
					foreach ($files_content as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				$sitemap_buffer .= sitemap_indexPrintFooter();
				if (!sitemap_writeFile($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/index.xml', $sitemap_buffer)){
                    die('ERROR WHILE SAVING THE '.$path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/index.xml!'.PHP_EOL);
                }

			} else {

				if (file_exists($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/index.xml')) {
					@unlink($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/index.xml');
				}
			}

		} else {

			if (file_exists($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/index.xml')) {
				@unlink($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/index.xml');
			}
		}

	}

	function sitemapgen_makeSitemapNews($path) {

		if (SITEMAP_FEATURE == "on") {

			$access = true;
			$dbObj = db_getDBObject(DEFAULT_DB,true);
			$sql = "SELECT * FROM Registration WHERE name = 'extra' AND domain = '' AND date_time = '0000-00-00 00:00:00' LIMIT 1";
			$result = $dbObj->query($sql);
			if ($result) {
				if ($row = mysql_fetch_assoc($result)) {
					if ($row["value"] == string_strtoupper(md5(VERSION."SITEMAP_FEATUREon"))) {
						$access = true;
					}
				}
			}

			if ($access) {

				$files_articlenews = false;

				if (ARTICLE_FEATURE == 'on' && CUSTOM_ARTICLE_FEATURE == "on") {
					if (SITEMAP_HASARTICLENEWS == "y") $files_articlenews = sitemap_createModuleNews($path, 'article');
				}

				$sitemap_buffer = "";
				$sitemap_buffer .= sitemap_indexPrintHeader();
				if ($files_articlenews) {
					foreach ($files_articlenews as $file) {
						$sitemap_buffer .= sitemap_indexPrintNodeSitemap($file);
					}
				}
				$sitemap_buffer .= sitemap_indexPrintFooter();
				if (!sitemap_writeFile($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/indexnews.xml', $sitemap_buffer)) die('ERROR WHILE SAVING THE '.$path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/indexnews.xml!'.PHP_EOL);

			} else {

				if (file_exists($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/indexnews.xml')) {
					@unlink($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/indexnews.xml');
				}

			}

		} else {

			if (file_exists($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/indexnews.xml')) {
				@unlink($path.'/custom/domain_'.SELECTED_DOMAIN_ID.'/sitemap/indexnews.xml');
			}

		}

	}

?>
