00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Sitemap_Model_Sitemap extends Mage_Core_Model_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_filePath;
00043
00044
00045
00046
00047 protected function _construct()
00048 {
00049 $this->_init('sitemap/sitemap');
00050 }
00051
00052 protected function _beforeSave()
00053 {
00054 $io = new Varien_Io_File();
00055 $realPath = $io->getCleanPath(Mage::getBaseDir() . '/' . $this->getSitemapPath());
00056
00057
00058
00059
00060 if (!$io->allowedPath($realPath, Mage::getBaseDir())) {
00061 Mage::throwException(Mage::helper('sitemap')->__('Please define correct path'));
00062 }
00063
00064
00065
00066 if (!$io->fileExists($realPath, false)) {
00067 Mage::throwException(Mage::helper('sitemap')->__('Please create the specified folder "%s" before saving the sitemap.', $this->getSitemapPath()));
00068 }
00069
00070 if (!$io->isWriteable($realPath)) {
00071 Mage::throwException(Mage::helper('sitemap')->__('Please make sure that "%s" is writable by web-server.', $this->getSitemapPath()));
00072 }
00073
00074
00075
00076 if (!preg_match('#^[a-zA-Z0-9_\.]+$#', $this->getSitemapFilename())) {
00077 Mage::throwException(Mage::helper('sitemap')->__('Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in the filename. No spaces or other characters are allowed.'));
00078 }
00079 if (!preg_match('#\.xml$#', $this->getSitemapFilename())) {
00080 $this->setSitemapFilename($this->getSitemapFilename() . '.xml');
00081 }
00082
00083 $this->setSitemapPath(rtrim(str_replace(str_replace('\\', '/', Mage::getBaseDir()), '', $realPath), '/') . '/');
00084
00085 return parent::_beforeSave();
00086 }
00087
00088
00089
00090
00091
00092
00093 protected function getPath()
00094 {
00095 if (is_null($this->_filePath)) {
00096 $this->_filePath = str_replace('//', '/', Mage::getBaseDir() .
00097 $this->getSitemapPath());
00098 }
00099 return $this->_filePath;
00100 }
00101
00102
00103
00104
00105
00106
00107 public function generateXml()
00108 {
00109 $io = new Varien_Io_File();
00110 $io->setAllowCreateFolders(true);
00111 $io->open(array('path' => $this->getPath()));
00112
00113 if ($io->fileExists($this->getSitemapFilename()) && !$io->isWriteable($this->getSitemapFilename())) {
00114 Mage::throwException(Mage::helper('sitemap')->__('File "%s" cannot be saved. Please, make sure the directory "%s" is writeable by web server.', $this->getSitemapFilename(), $this->getPath()));
00115 }
00116
00117 $io->streamOpen($this->getSitemapFilename());
00118
00119 $io->streamWrite('<?xml version="1.0" encoding="UTF-8"?>' . "\n");
00120 $io->streamWrite('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
00121
00122 $storeId = $this->getStoreId();
00123 $date = Mage::getSingleton('core/date')->gmtDate('Y-m-d');
00124 $baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
00125
00126
00127
00128
00129 $changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq');
00130 $priority = (string)Mage::getStoreConfig('sitemap/category/priority');
00131 $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
00132 foreach ($collection as $item) {
00133 $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
00134 htmlspecialchars($baseUrl . $item->getUrl()),
00135 $date,
00136 $changefreq,
00137 $priority
00138 );
00139 $io->streamWrite($xml);
00140 }
00141 unset($collection);
00142
00143
00144
00145
00146 $changefreq = (string)Mage::getStoreConfig('sitemap/product/changefreq');
00147 $priority = (string)Mage::getStoreConfig('sitemap/product/priority');
00148 $collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);
00149 foreach ($collection as $item) {
00150 $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
00151 htmlspecialchars($baseUrl . $item->getUrl()),
00152 $date,
00153 $changefreq,
00154 $priority
00155 );
00156 $io->streamWrite($xml);
00157 }
00158 unset($collection);
00159
00160
00161
00162
00163 $changefreq = (string)Mage::getStoreConfig('sitemap/page/changefreq');
00164 $priority = (string)Mage::getStoreConfig('sitemap/page/priority');
00165 $collection = Mage::getResourceModel('sitemap/cms_page')->getCollection($storeId);
00166 foreach ($collection as $item) {
00167 $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
00168 htmlspecialchars($baseUrl . $item->getUrl()),
00169 $date,
00170 $changefreq,
00171 $priority
00172 );
00173 $io->streamWrite($xml);
00174 }
00175 unset($collection);
00176
00177 $io->streamWrite('</urlset>');
00178 $io->streamClose();
00179
00180 $this->setSitemapTime(Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s'));
00181 $this->save();
00182
00183 return $this;
00184 }
00185 }