/**
 * currently this splitts only sitemaps for albums and its images. Spliting the images itself requires a major rework...
 *
 * Gets links to all images for all albums (album by album)
 *
 * @return string
 */
function getSitemapImages()
{
    global $_zp_gallery, $sitemap_number;
    $data = '';
    $sitemap_locales = generateLanguageList();
    $imagechangefreq = getOption('sitemap_changefreq_images');
    $imagelastmod = getOption('sitemap_lastmod_images');
    $limit = sitemap_getDBLimit(1);
    $albums = array();
    getSitemapAlbumList($_zp_gallery, $albums, 'passImages');
    $offset = $sitemap_number - 1;
    $albums = array_slice($albums, $offset, SITEMAP_CHUNK);
    if ($albums) {
        $data .= sitemap_echonl('<?xml version="1.0" encoding="UTF-8"?>');
        if (GOOGLE_SITEMAP) {
            $data .= sitemap_echonl('<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">');
        } else {
            $data .= sitemap_echonl('<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
        }
        foreach ($albums as $album) {
            @set_time_limit(120);
            //	Extend script timeout to allow for gathering the images.
            $albumobj = newAlbum($album['folder']);
            $images = $albumobj->getImages();
            // print plain images links if available
            if ($images) {
                foreach ($images as $image) {
                    $imageobj = newImage($albumobj, $image);
                    $ext = getSuffix($imageobj->filename);
                    $date = sitemap_getDateformat($imageobj, $imagelastmod);
                    switch (SITEMAP_LOCALE_TYPE) {
                        case 1:
                            foreach ($sitemap_locales as $locale) {
                                $path = seo_locale::localePath(true, $locale) . '/' . pathurlencode($albumobj->name) . '/' . urlencode($imageobj->filename) . IM_SUFFIX;
                                $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $path . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $imagechangefreq . "</changefreq>\n\t\t<priority>0.6</priority>\n");
                                if (GOOGLE_SITEMAP) {
                                    $data .= getSitemapGoogleImageVideoExtras($albumobj, $imageobj, $locale);
                                }
                                $data .= sitemap_echonl("</url>");
                            }
                            break;
                        case 2:
                            foreach ($sitemap_locales as $locale) {
                                $path = rewrite_path(pathurlencode($albumobj->name) . '/' . urlencode($imageobj->filename) . IM_SUFFIX, '?album=' . pathurlencode($albumobj->name) . '&amp;image=' . urlencode($imageobj->filename), dynamic_locale::fullHostPath($locale));
                                $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $path . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $imagechangefreq . "</changefreq>\n\t\t<priority>0.6</priority>\n");
                                if (GOOGLE_SITEMAP) {
                                    $data .= getSitemapGoogleImageVideoExtras($albumobj, $imageobj, $locale);
                                }
                                $data .= sitemap_echonl("</url>");
                            }
                            break;
                        default:
                            $path = rewrite_path(pathurlencode($albumobj->name) . '/' . urlencode($imageobj->filename) . IM_SUFFIX, '?album=' . pathurlencode($albumobj->name) . '&amp;image=' . urlencode($imageobj->filename), FULLWEBPATH);
                            $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $path . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $imagechangefreq . "</changefreq>\n\t\t<priority>0.6</priority>\n");
                            if (GOOGLE_SITEMAP) {
                                $data .= getSitemapGoogleImageVideoExtras($albumobj, $imageobj, NULL);
                            }
                            $data .= sitemap_echonl("</url>");
                            break;
                    }
                }
            }
        }
        $data .= sitemap_echonl('</urlset>');
        // End off the <urlset> tag
    }
    return $data;
}
/**
 * Places album and all of its album pages on one sitemap
 *
 * Gets links to all albums incl. pagination and if the Google image video extension is enabled for images using this as well.
 * This is independent from the images fetched by getSitemapImages().
 *
 * NOTE: Using the Google extension is currently NOT recommended if you have a huge gallery.
 *
 * @return string
 */
function getSitemapAlbums()
{
    global $_zp_gallery, $sitemap_number;
    $data = '';
    $sitemap_locales = generateLanguageList();
    $albumchangefreq = getOption('sitemap_changefreq_albums');
    $imagechangefreq = getOption('sitemap_changefreq_images');
    $albumlastmod = getOption('sitemap_lastmod_albums');
    $albumlastmod = sanitize($albumlastmod);
    $imagelastmod = getOption('sitemap_lastmod_images');
    $albums = array();
    getSitemapAlbumList($_zp_gallery, $albums, 'passAlbums');
    $offset = $sitemap_number - 1;
    $albums = array_slice($albums, $offset, SITEMAP_CHUNK);
    if (!empty($albums)) {
        $data .= sitemap_echonl('<?xml version="1.0" encoding="UTF-8"?>');
        if (getOption('sitemap_google')) {
            $data .= sitemap_echonl('<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">');
        } else {
            $data .= sitemap_echonl('<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
        }
        foreach ($albums as $album) {
            $albumobj = new Album($_zp_gallery, $album['folder']);
            set_context(ZP_ALBUM);
            makeAlbumCurrent($albumobj);
            //getting the album pages
            /* Custom images/albums per page taken out as it is not possible to set these now anymore. Maybe later becomes an options
            			$images_per_page = getOption('images_per_page');
            			$albums_per_page = getOption('albums_per_page');
            			if(is_array($imagesperpage)) {
            				foreach($imagesperpage as $alb=>$number) {
            					if($alb == $albumobj->name) {
            						setOption('images_per_page',$number,false);
            					} else {
            						setOption('images_per_page',$images_per_page);
            					}
            				}
            			}
            			if(is_array($albumsperpage)) {
            				foreach($albumsperpage as $alb=>$number) {
            					if($alb == $albumobj->name) {
            						setOption('albums_per_page',$number,false);
            					} else {
            						setOption('albums_per_page',$albums_per_page);
            					}
            				}
            			} */
            $pageCount = getTotalPages();
            $imageCount = getNumImages();
            $images = $albumobj->getImages();
            $loop_index = getSitemapGoogleLoopIndex($imageCount, $pageCount);
            $date = sitemap_getDateformat($albumobj, $albumlastmod);
            if (sitemap_multilingual()) {
                foreach ($sitemap_locales as $locale) {
                    $url = FULLWEBPATH . '/' . rewrite_path($locale . '/' . pathurlencode($albumobj->name), '?album=' . pathurlencode($albumobj->name), false);
                    $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $url . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $albumchangefreq . "</changefreq>\n\t\t<priority>0.8</priority>\n");
                    printSitemapGoogleImageVideoExtras(1, $loop_index, $albumobj, $images);
                    $data .= sitemap_echonl("\t</url>");
                }
            } else {
                $url = FULLWEBPATH . '/' . rewrite_path(pathurlencode($albumobj->name), '?album=' . pathurlencode($albumobj->name), false);
                $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $url . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $albumchangefreq . "</changefreq>\n\t\t<priority>0.8</priority>\n");
                $data .= getSitemapGoogleImageVideoExtras(1, $loop_index, $albumobj, $images);
                $data .= sitemap_echonl("\t</url>");
            }
            // print album pages if avaiable
            if ($pageCount > 1) {
                for ($x = 2; $x <= $pageCount; $x++) {
                    if (sitemap_multilingual()) {
                        foreach ($sitemap_locales as $locale) {
                            $url = FULLWEBPATH . '/' . rewrite_path($locale . '/' . pathurlencode($albumobj->name) . '/page/' . $x, '?album=' . pathurlencode($albumobj->name) . '&amp;page=' . $x, false);
                            $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $url . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $albumchangefreq . "</changefreq>\n\t\t<priority>0.8</priority>\n");
                            $data .= getSitemapGoogleImageVideoExtras($x, $loop_index, $albumobj, $images);
                            $data .= sitemap_echonl("\t</url>");
                        }
                    } else {
                        $url = FULLWEBPATH . '/' . rewrite_path(pathurlencode($albumobj->name) . '/page/' . $x, '?album=' . pathurlencode($albumobj->name) . '&amp;page=' . $x, false);
                        $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $url . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $albumchangefreq . "</changefreq>\n\t\t<priority>0.8</priority>\n");
                        $data .= getSitemapGoogleImageVideoExtras($x, $loop_index, $albumobj, $images);
                        $data .= sitemap_echonl("\t</url>");
                    }
                }
            }
        }
        $data .= sitemap_echonl('</urlset>');
        // End off the <urlset> tag
    }
    restore_context();
    return $data;
}