1 | <?php |
---|
2 | /** |
---|
3 | * |
---|
4 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
5 | * |
---|
6 | * http://www.lockon.co.jp/ |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Sitemapプロトコル ファイル生成モジュール. |
---|
12 | * PHP versions 4 and 5 |
---|
13 | * |
---|
14 | * <pre> |
---|
15 | * このモジュールは Sitemapプロトコルに対応した XMLファイルを出力する. |
---|
16 | * EC-CUBE インストールディレクトリの htmlディレクトリへ配置することにより動作する. |
---|
17 | * |
---|
18 | * このモジュールにより, 以下のページのサイトマップが生成される. |
---|
19 | * 1. $staticURL で指定したページ |
---|
20 | * 2. 管理画面のデザイン管理から生成したページ |
---|
21 | * 3. 公開されているすべての商品一覧ページ |
---|
22 | * 4. 公開されているすべての商品詳細ページ |
---|
23 | * 5. html/mobile 以下の上記ページ |
---|
24 | * |
---|
25 | * このモジュールを設置後, 各検索エンジンにサイトマップを登録することにより, 検索エンジンの |
---|
26 | * インデックス化が促進される. |
---|
27 | * </pre> |
---|
28 | * @see https://www.google.com/webmasters/tools/siteoverview?hl=ja |
---|
29 | * @see https://siteexplorer.search.yahoo.com/mysites |
---|
30 | * |
---|
31 | * @author Kentaro Ohkouchi |
---|
32 | * @version $Id$ |
---|
33 | */ |
---|
34 | require_once("require.php"); |
---|
35 | // --------------------------------------------------------------------- 初期設定 |
---|
36 | // :TODO: filemtime 関数を使えば、静的なページの更新時間も取得するようにできそう |
---|
37 | // |
---|
38 | // 動的に生成されないページを配列で指定 |
---|
39 | $staticURL = array(SITE_URL, MOBILE_SITE_URL, SITE_URL . "rss/index.php"); |
---|
40 | // :TODO: 各ページの changefreq や priority を指定できるようにする |
---|
41 | // ---------------------------------------------------------------------------- |
---|
42 | |
---|
43 | // ---------------------------------------------------------------------------- |
---|
44 | // }}} |
---|
45 | // {{{ View Logic |
---|
46 | |
---|
47 | /** |
---|
48 | * Sitemap の <url /> を生成する. |
---|
49 | * |
---|
50 | * @param string $loc ページの URL ※必須 |
---|
51 | * @param string $lastmod ファイルの最終更新日 YYYY-MM-DD or W3C Datetime 形式 |
---|
52 | * @param string $changefreq ページの更新頻度 |
---|
53 | * @param double $priority URL の優先度 |
---|
54 | * @return Sitemap 形式の <url /> |
---|
55 | * @see https://www.google.com/webmasters/tools/docs/ja/protocol.html#xmlTagDefinitions |
---|
56 | */ |
---|
57 | function createSitemap($loc, $lastmod = "", $changefreq = "", $priority = "") { |
---|
58 | printf("\t<url>\n"); |
---|
59 | printf("\t\t<loc>%s</loc>\n", htmlentities($loc, ENT_QUOTES, "UTF-8")); |
---|
60 | if (!empty($lastmod)) { |
---|
61 | printf("\t\t<lastmod>%s</lastmod>\n", $lastmod); |
---|
62 | } |
---|
63 | if (!empty($changefreq)) { |
---|
64 | printf("\t\t<changefreq>%s</changefreq>\n", $changefreq); |
---|
65 | } |
---|
66 | if(!empty($priority)) { |
---|
67 | printf("\t\t<priority>%01.1f</priority>\n", $priority); |
---|
68 | } |
---|
69 | printf("\t</url>\n"); |
---|
70 | } |
---|
71 | |
---|
72 | $objQuery = new SC_Query(); |
---|
73 | |
---|
74 | //キャッシュしない(念のため) |
---|
75 | header("Paragrama: no-cache"); |
---|
76 | |
---|
77 | //XMLテキスト |
---|
78 | header("Content-type: application/xml; charset=utf-8"); |
---|
79 | |
---|
80 | // 必ず UTF-8 として出力 |
---|
81 | mb_http_output("UTF-8"); |
---|
82 | ob_start('mb_output_handler'); |
---|
83 | |
---|
84 | print("<?xml version='1.0' encoding='UTF-8'?>\n"); |
---|
85 | print("<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n"); |
---|
86 | |
---|
87 | // ---------------------------------------------------------------------------- |
---|
88 | // }}} |
---|
89 | // {{{ Controller Logic |
---|
90 | |
---|
91 | // 静的なページを処理 |
---|
92 | foreach($staticURL as $url) { |
---|
93 | createSitemap($url, '', 'daily', 1.0); |
---|
94 | } |
---|
95 | // ページのデータを取得 |
---|
96 | $objPageData = new LC_PageLayout; |
---|
97 | |
---|
98 | // TOPページを処理 |
---|
99 | $topPage = getTopPage($objPageData->arrPageList); |
---|
100 | createSitemap($topPage[0]['url'], date2W3CDatetime($topPage[0]['update_date']), |
---|
101 | 'daily', 1.0); |
---|
102 | |
---|
103 | // 編集可能ページを処理 |
---|
104 | $editablePages = getEditablePage($objPageData->arrPageList); |
---|
105 | foreach($editablePages as $editablePage) { |
---|
106 | createSitemap($editablePage['url'], date2W3CDatetime($editablePage['update_date'])); |
---|
107 | } |
---|
108 | |
---|
109 | // 商品一覧ページを処理 |
---|
110 | $products = getAllProducts(); |
---|
111 | foreach($products as $product) { |
---|
112 | createSitemap($product['url'], '', 'daily'); |
---|
113 | } |
---|
114 | $mobileProducts = getAllProducts(true); |
---|
115 | foreach($mobileProducts as $mobileProduct) { |
---|
116 | createSitemap($mobileProduct['url'], '', 'daily'); |
---|
117 | } |
---|
118 | |
---|
119 | // 商品詳細ページを処理 |
---|
120 | $details = getAllDetail(); |
---|
121 | foreach($details as $detail) { |
---|
122 | createSitemap($detail['url'], date2W3CDatetime($detail['update_date'])); |
---|
123 | } |
---|
124 | $mobileDetails = getAllDetail(true); |
---|
125 | foreach($mobileDetails as $mobileDetail) { |
---|
126 | createSitemap($mobileDetail['url'], date2W3CDatetime($mobileDetail['update_date'])); |
---|
127 | } |
---|
128 | |
---|
129 | print("</urlset>\n"); |
---|
130 | |
---|
131 | // ---------------------------------------------------------------------------- |
---|
132 | // }}} |
---|
133 | // {{{ Model Logic |
---|
134 | |
---|
135 | /** |
---|
136 | * TOPページの情報を取得 |
---|
137 | * |
---|
138 | * @param array $pageData すべてのページ情報の配列 |
---|
139 | * @return TOPページの情報 |
---|
140 | */ |
---|
141 | function getTopPage($pageData) { |
---|
142 | $arrRet = array(); |
---|
143 | foreach($pageData as $page) { |
---|
144 | if ($page['page_id'] == "1") { |
---|
145 | $page['url'] = SITE_URL . $page['url']; |
---|
146 | $arrRet[0] = $page; |
---|
147 | return $arrRet; |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * すべての編集可能ページの情報を取得する. |
---|
154 | * |
---|
155 | * @param array $pageData すべてのページ情報の配列 |
---|
156 | * @return 編集可能ページ |
---|
157 | */ |
---|
158 | function getEditablePage($pageData) { |
---|
159 | $arrRet = array(); |
---|
160 | $i = 0; |
---|
161 | foreach($pageData as $page) { |
---|
162 | if ($page['page_id'] > 4) { |
---|
163 | $arrRet[$i] = $page; |
---|
164 | $i++; |
---|
165 | } |
---|
166 | } |
---|
167 | return $arrRet; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * date形式の文字列を W3C Datetime 形式に変換して出力する. |
---|
172 | * |
---|
173 | * @param date $date 変換する日付 |
---|
174 | */ |
---|
175 | function date2W3CDatetime($date) { |
---|
176 | $arr = array(); |
---|
177 | // 正規表現で文字列を抽出 |
---|
178 | ereg("^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})", |
---|
179 | $date, $arr); |
---|
180 | // :TODO: time zone も取得するべき... |
---|
181 | return sprintf("%04d-%02d-%02dT%02d:%02d:%02d+09:00", |
---|
182 | $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6]); |
---|
183 | } |
---|
184 | |
---|
185 | // ---------------------------------------------------------------------------- |
---|
186 | // }}} |
---|
187 | // {{{ DB Access Objects |
---|
188 | |
---|
189 | /** |
---|
190 | * ページデータを扱うクラス. |
---|
191 | */ |
---|
192 | class LC_PageLayout { |
---|
193 | |
---|
194 | var $arrPageData; // ページデータ格納用 |
---|
195 | var $arrPageList; // ページデータ格納用 |
---|
196 | |
---|
197 | /** |
---|
198 | * コンストラクタ. |
---|
199 | */ |
---|
200 | function LC_PageLayout() { |
---|
201 | $this->arrPageList = $this->getPageData(); |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * ブロック情報を取得する. |
---|
206 | * |
---|
207 | * @param string $where WHERE句 |
---|
208 | * @param array $arrVal WHERE句の値を格納した配列 |
---|
209 | * @return ブロック情報 |
---|
210 | */ |
---|
211 | function getPageData($where = '', $arrVal = ''){ |
---|
212 | $objDBConn = new SC_DbConn; // DB操作オブジェクト |
---|
213 | $sql = ""; // データ取得SQL生成用 |
---|
214 | $arrRet = array(); // データ取得用 |
---|
215 | |
---|
216 | // SQL生成(url と update_date 以外は不要?) |
---|
217 | $sql .= " SELECT"; |
---|
218 | $sql .= " page_id"; // ページID |
---|
219 | $sql .= " ,page_name"; // 名称 |
---|
220 | $sql .= " ,url"; // URL |
---|
221 | $sql .= " ,php_dir"; // php保存先ディレクトリ |
---|
222 | $sql .= " ,tpl_dir"; // tpl保存先ディdレクトリ |
---|
223 | $sql .= " ,filename"; // ファイル名称 |
---|
224 | $sql .= " ,header_chk "; // ヘッダー使用FLG |
---|
225 | $sql .= " ,footer_chk "; // フッター使用FLG |
---|
226 | $sql .= " ,author"; // authorタグ |
---|
227 | $sql .= " ,description"; // descriptionタグ |
---|
228 | $sql .= " ,keyword"; // keywordタグ |
---|
229 | $sql .= " ,update_url"; // 更新URL |
---|
230 | $sql .= " ,create_date"; // データ作成日 |
---|
231 | $sql .= " ,update_date"; // データ更新日 |
---|
232 | $sql .= " FROM "; |
---|
233 | $sql .= " dtb_pagelayout"; |
---|
234 | |
---|
235 | // where句の指定があれば追加 |
---|
236 | if ($where != '') { |
---|
237 | $sql .= " WHERE " . $where; |
---|
238 | } |
---|
239 | |
---|
240 | $sql .= " ORDER BY page_id"; |
---|
241 | |
---|
242 | $arrRet = $objDBConn->getAll($sql, $arrVal); |
---|
243 | |
---|
244 | $this->arrPageData = $arrRet; |
---|
245 | |
---|
246 | return $arrRet; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * すべての商品一覧ページを取得する. |
---|
252 | * |
---|
253 | * @param boolean $isMobile モバイルページを取得する場合 true |
---|
254 | * @return 検索エンジンからアクセス可能な商品一覧ページの情報 |
---|
255 | */ |
---|
256 | function getAllProducts($isMobile = false) { |
---|
257 | $conn = new SC_DBConn(); |
---|
258 | $sql = "SELECT category_id FROM dtb_category WHERE del_flg = 0"; |
---|
259 | $result = $conn->getAll($sql); |
---|
260 | |
---|
261 | $mobile = ""; |
---|
262 | if ($isMobile) { |
---|
263 | $mobile = "mobile/"; |
---|
264 | } |
---|
265 | |
---|
266 | $arrRet = array(); |
---|
267 | for ($i = 0; $i < count($result); $i++) { |
---|
268 | // :TODO: カテゴリの最終更新日を取得できるようにする |
---|
269 | $page = array("url" => SITE_URL . sprintf("%sproducts/list.php?category_id=%d", $mobile, $result[$i]['category_id'])); |
---|
270 | $arrRet[$i] = $page; |
---|
271 | } |
---|
272 | return $arrRet; |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * すべての商品詳細ページを取得する. |
---|
277 | * |
---|
278 | * @param boolean $isMobile モバイルページを取得する場合 true |
---|
279 | * @return 検索エンジンからアクセス可能な商品詳細ページの情報 |
---|
280 | */ |
---|
281 | function getAllDetail($isMobile = false) { |
---|
282 | $conn = new SC_DBConn(); |
---|
283 | $sql = "SELECT product_id, update_date FROM dtb_products WHERE del_flg = 0 AND status = 1"; |
---|
284 | $result = $conn->getAll($sql); |
---|
285 | |
---|
286 | $mobile = ""; |
---|
287 | if ($isMobile) { |
---|
288 | $mobile = "mobile/"; |
---|
289 | } |
---|
290 | |
---|
291 | $arrRet = array(); |
---|
292 | for ($i = 0; $i < count($result); $i++) { |
---|
293 | $page = array("url" => SITE_URL. sprintf("%sproducts/detail.php?product_id=%d", $mobile, $result[$i]['product_id']), |
---|
294 | "update_date" => $result[$i]['update_date']); |
---|
295 | $arrRet[$i] = $page; |
---|
296 | } |
---|
297 | return $arrRet; |
---|
298 | } |
---|
299 | ?> |
---|