1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: class Mage_Sitemap_Model_Sitemap extends Mage_Core_Model_Abstract
49: {
50: 51: 52: 53: 54:
55: protected $_filePath;
56:
57: 58: 59:
60: protected function _construct()
61: {
62: $this->_init('sitemap/sitemap');
63: }
64:
65: protected function _beforeSave()
66: {
67: $io = new Varien_Io_File();
68: $realPath = $io->getCleanPath(Mage::getBaseDir() . '/' . $this->getSitemapPath());
69:
70: 71: 72:
73: if (!$io->allowedPath($realPath, Mage::getBaseDir())) {
74: Mage::throwException(Mage::helper('sitemap')->__('Please define correct path'));
75: }
76: 77: 78:
79: if (!$io->fileExists($realPath, false)) {
80: Mage::throwException(Mage::helper('sitemap')->__('Please create the specified folder "%s" before saving the sitemap.', Mage::helper('core')->htmlEscape($this->getSitemapPath())));
81: }
82:
83: if (!$io->isWriteable($realPath)) {
84: Mage::throwException(Mage::helper('sitemap')->__('Please make sure that "%s" is writable by web-server.', $this->getSitemapPath()));
85: }
86: 87: 88:
89: if (!preg_match('#^[a-zA-Z0-9_\.]+$#', $this->getSitemapFilename())) {
90: 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.'));
91: }
92: if (!preg_match('#\.xml$#', $this->getSitemapFilename())) {
93: $this->setSitemapFilename($this->getSitemapFilename() . '.xml');
94: }
95:
96: $this->setSitemapPath(rtrim(str_replace(str_replace('\\', '/', Mage::getBaseDir()), '', $realPath), '/') . '/');
97:
98: return parent::_beforeSave();
99: }
100:
101: 102: 103: 104: 105:
106: protected function getPath()
107: {
108: if (is_null($this->_filePath)) {
109: $this->_filePath = str_replace('//', '/', Mage::getBaseDir() .
110: $this->getSitemapPath());
111: }
112: return $this->_filePath;
113: }
114:
115: 116: 117: 118: 119:
120: public function getPreparedFilename()
121: {
122: return $this->getPath() . $this->getSitemapFilename();
123: }
124:
125: 126: 127: 128: 129:
130: public function generateXml()
131: {
132: $io = new Varien_Io_File();
133: $io->setAllowCreateFolders(true);
134: $io->open(array('path' => $this->getPath()));
135:
136: if ($io->fileExists($this->getSitemapFilename()) && !$io->isWriteable($this->getSitemapFilename())) {
137: 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()));
138: }
139:
140: $io->streamOpen($this->getSitemapFilename());
141:
142: $io->streamWrite('<?xml version="1.0" encoding="UTF-8"?>' . "\n");
143: $io->streamWrite('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
144:
145: $storeId = $this->getStoreId();
146: $date = Mage::getSingleton('core/date')->gmtDate('Y-m-d');
147: $baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
148:
149: 150: 151:
152: $changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
153: $priority = (string)Mage::getStoreConfig('sitemap/category/priority', $storeId);
154: $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
155: foreach ($collection as $item) {
156: $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
157: htmlspecialchars($baseUrl . $item->getUrl()),
158: $date,
159: $changefreq,
160: $priority
161: );
162: $io->streamWrite($xml);
163: }
164: unset($collection);
165:
166: 167: 168:
169: $changefreq = (string)Mage::getStoreConfig('sitemap/product/changefreq', $storeId);
170: $priority = (string)Mage::getStoreConfig('sitemap/product/priority', $storeId);
171: $collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);
172: foreach ($collection as $item) {
173: $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
174: htmlspecialchars($baseUrl . $item->getUrl()),
175: $date,
176: $changefreq,
177: $priority
178: );
179: $io->streamWrite($xml);
180: }
181: unset($collection);
182:
183: 184: 185:
186: $changefreq = (string)Mage::getStoreConfig('sitemap/page/changefreq', $storeId);
187: $priority = (string)Mage::getStoreConfig('sitemap/page/priority', $storeId);
188: $collection = Mage::getResourceModel('sitemap/cms_page')->getCollection($storeId);
189: foreach ($collection as $item) {
190: $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
191: htmlspecialchars($baseUrl . $item->getUrl()),
192: $date,
193: $changefreq,
194: $priority
195: );
196: $io->streamWrite($xml);
197: }
198: unset($collection);
199:
200: $io->streamWrite('</urlset>');
201: $io->streamClose();
202:
203: $this->setSitemapTime(Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s'));
204: $this->save();
205:
206: return $this;
207: }
208: }
209: