*/ class Mage_Sitemap_Model_Sitemap extends Mage_Core_Model_Abstract { /** * Real file path * * @var string */ protected $_filePath; /** * Init model */ protected function _construct() { $this->_init('sitemap/sitemap'); } protected function _beforeSave() { $io = new Varien_Io_File(); $realPath = $io->getCleanPath(Mage::getBaseDir() . '/' . $this->getSitemapPath()); /** * Check path is allow */ if (!$io->allowedPath($realPath, Mage::getBaseDir())) { Mage::throwException(Mage::helper('sitemap')->__('Please define correct path')); } /** * Check exists and writeable path */ if (!$io->fileExists($realPath, false)) { Mage::throwException(Mage::helper('sitemap')->__('Please create the specified folder "%s" before saving the sitemap.', Mage::helper('core')->escapeHtml($this->getSitemapPath()))); } if (!$io->isWriteable($realPath)) { Mage::throwException(Mage::helper('sitemap')->__('Please make sure that "%s" is writable by web-server.', $this->getSitemapPath())); } /** * Check allow filename */ if (!preg_match('#^[a-zA-Z0-9_\.]+$#', $this->getSitemapFilename())) { 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.')); } if (!preg_match('#\.xml$#', $this->getSitemapFilename())) { $this->setSitemapFilename($this->getSitemapFilename() . '.xml'); } $this->setSitemapPath(rtrim(str_replace(str_replace('\\', '/', Mage::getBaseDir()), '', $realPath), '/') . '/'); return parent::_beforeSave(); } /** * Return real file path * * @return string */ protected function getPath() { if (is_null($this->_filePath)) { $this->_filePath = str_replace('//', '/', Mage::getBaseDir() . $this->getSitemapPath()); } return $this->_filePath; } /** * Return full file name with path * * @return string */ public function getPreparedFilename() { return $this->getPath() . $this->getSitemapFilename(); } /** * Generate XML file * * @return Mage_Sitemap_Model_Sitemap */ public function generateXml() { $io = new Varien_Io_File(); $io->setAllowCreateFolders(true); $io->open(array('path' => $this->getPath())); if ($io->fileExists($this->getSitemapFilename()) && !$io->isWriteable($this->getSitemapFilename())) { 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())); } $io->streamOpen($this->getSitemapFilename()); $io->streamWrite('' . "\n"); $io->streamWrite(''); $storeId = $this->getStoreId(); $date = Mage::getSingleton('core/date')->gmtDate('Y-m-d'); $baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK); /** * Generate categories sitemap */ $changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq', $storeId); $priority = (string)Mage::getStoreConfig('sitemap/category/priority', $storeId); $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId); $categories = new Varien_Object(); $categories->setItems($collection); Mage::dispatchEvent('sitemap_categories_generating_before', array( 'collection' => $categories )); foreach ($categories->getItems() as $item) { $xml = sprintf( '%s%s%s%.1f', htmlspecialchars($baseUrl . $item->getUrl()), $date, $changefreq, $priority ); $io->streamWrite($xml); } unset($collection); /** * Generate products sitemap */ $changefreq = (string)Mage::getStoreConfig('sitemap/product/changefreq', $storeId); $priority = (string)Mage::getStoreConfig('sitemap/product/priority', $storeId); $collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId); $products = new Varien_Object(); $products->setItems($collection); Mage::dispatchEvent('sitemap_products_generating_before', array( 'collection' => $products )); foreach ($products->getItems() as $item) { $xml = sprintf( '%s%s%s%.1f', htmlspecialchars($baseUrl . $item->getUrl()), $date, $changefreq, $priority ); $io->streamWrite($xml); } unset($collection); /** * Generate cms pages sitemap */ $changefreq = (string)Mage::getStoreConfig('sitemap/page/changefreq', $storeId); $priority = (string)Mage::getStoreConfig('sitemap/page/priority', $storeId); $collection = Mage::getResourceModel('sitemap/cms_page')->getCollection($storeId); foreach ($collection as $item) { $xml = sprintf( '%s%s%s%.1f', htmlspecialchars($baseUrl . $item->getUrl()), $date, $changefreq, $priority ); $io->streamWrite($xml); } unset($collection); $io->streamWrite(''); $io->streamClose(); $this->setSitemapTime(Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s')); $this->save(); return $this; } }