1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: include_once dirname(__DIR__) . '/include/common.php';
22:
23: class PublisherMetagen
24: {
25: 26: 27: 28:
29: public $publisher = null;
30:
31: 32: 33:
34: public $_myts;
35:
36: 37: 38:
39: public $_title;
40:
41: 42: 43:
44: public $_original_title;
45:
46: 47: 48:
49: public $_keywords;
50:
51: 52: 53:
54: public $_categoryPath;
55:
56: 57: 58:
59: public $_description;
60:
61: 62: 63: 64:
65: public $_minChar = 4;
66:
67: 68: 69: 70: 71: 72:
73: public function __construct($title, $keywords = '', $description = '', $categoryPath = false)
74: {
75: $this->publisher = Publisher::getInstance();
76: $this->_myts = \Xoops\Core\Text\Sanitizer::getInstance();
77: $this->setCategoryPath($categoryPath);
78: $this->setTitle($title);
79: $this->setDescription($description);
80: if ($keywords == '') {
81: $keywords = $this->createMetaKeywords();
82: }
83: $this->setKeywords($keywords);
84: }
85:
86: 87: 88:
89: public function setTitle($title)
90: {
91: $this->_title = $this->html2text($title);
92: $this->_original_title = $this->_title;
93: $titleTag = array();
94: $titleTag['module'] = $this->publisher->getModule()->getVar('name');
95: if (isset($this->_title) && ($this->_title != '') && (strtoupper($this->_title) != strtoupper($titleTag['module']))) {
96: $titleTag['title'] = $this->_title;
97: }
98: if (isset($this->_categoryPath) && ($this->_categoryPath != '')) {
99: $titleTag['category'] = $this->_categoryPath;
100: }
101: $ret = isset($titleTag['title']) ? $titleTag['title'] : '';
102: if (isset($titleTag['category']) && $titleTag['category'] != '') {
103: if ($ret != '') {
104: $ret .= ' - ';
105: }
106: $ret .= $titleTag['category'];
107: }
108: if (isset($titleTag['module']) && $titleTag['module'] != '') {
109: if ($ret != '') {
110: $ret .= ' - ';
111: }
112: $ret .= $titleTag['module'];
113: }
114: $this->_title = $ret;
115: }
116:
117: 118: 119:
120: public function setKeywords($keywords)
121: {
122: $this->_keywords = $keywords;
123: }
124:
125: 126: 127:
128: public function setCategoryPath($categoryPath)
129: {
130: $categoryPath = $this->html2text($categoryPath);
131: $this->_categoryPath = $categoryPath;
132: }
133:
134: 135: 136:
137: public function setDescription($description)
138: {
139: $description = $this->html2text($description);
140: $description = $this->purifyText($description);
141: $this->_description = $description;
142: }
143:
144: 145: 146:
147: public function createTitleTag()
148: {
149: }
150:
151: 152: 153: 154: 155:
156: public function createMetaDescription($maxWords = 30)
157: {
158: $description = $this->purifyText($this->_description);
159: $description = $this->html2text($description);
160: $words = explode(" ", $description);
161: $ret = '';
162: $i = 1;
163: $wordCount = count($words);
164: foreach ($words as $word) {
165: $ret .= $word;
166: if ($i < $wordCount) {
167: $ret .= ' ';
168: }
169: ++$i;
170: }
171: return $ret;
172: }
173:
174: 175: 176: 177: 178: 179:
180: public function findMetaKeywords($text, $minChar)
181: {
182: $keywords = array();
183: $text = $this->purifyText($text);
184: $text = $this->html2text($text);
185: $originalKeywords = explode(" ", $text);
186: foreach ($originalKeywords as $originalKeyword) {
187: $secondRoundKeywords = explode("'", $originalKeyword);
188: foreach ($secondRoundKeywords as $secondRoundKeyword) {
189: if (strlen($secondRoundKeyword) >= $minChar) {
190: if (!in_array($secondRoundKeyword, $keywords)) {
191: $keywords[] = trim($secondRoundKeyword);
192: }
193: }
194: }
195: }
196: return $keywords;
197: }
198:
199: 200: 201:
202: public function createMetaKeywords()
203: {
204: $keywords = $this->findMetaKeywords($this->_original_title . " " . $this->_description, $this->_minChar);
205: $moduleKeywords = $this->publisher->getConfig('seo_meta_keywords');
206: if ($moduleKeywords != '') {
207: $moduleKeywords = explode(",", $moduleKeywords);
208: $keywords = array_merge($keywords, array_map('trim', $moduleKeywords));
209: }
210: $ret = implode(',', $keywords);
211: return $ret;
212: }
213:
214: 215: 216:
217: public function autoBuildMeta_keywords()
218: {
219: }
220:
221: 222: 223:
224: public function buildAutoMetaTags()
225: {
226: $this->_keywords = $this->createMetaKeywords();
227: $this->_description = $this->createMetaDescription();
228:
229: }
230:
231: 232: 233:
234: public function createMetaTags()
235: {
236: global $xoopsTpl, $xoTheme;
237: if ($this->_keywords != '') {
238: $xoTheme->addMeta('meta', 'keywords', $this->_keywords);
239: }
240: if ($this->_description != '') {
241: $xoTheme->addMeta('meta', 'description', $this->_description);
242: }
243: if ($this->_title != '') {
244: $xoopsTpl->assign('xoops_pagetitle', $this->_title);
245: }
246: }
247:
248: 249: 250: 251: 252: 253: 254:
255: static public function emptyString($var)
256: {
257: return (strlen($var) > 0);
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267: 268: 269:
270: static function generateSeoTitle($title = '', $withExt = true)
271: {
272:
273:
274: $title = rawurlencode(strtolower($title));
275:
276:
277: $pattern = array("/%09/", "/%20/", "/%21/", "/%22/", "/%23/", "/%25/", "/%26/", "/%27/", "/%28/", "/%29/", "/%2C/", "/%2F/", "/%3A/", "/%3B/", "/%3C/", "/%3D/", "/%3E/", "/%3F/", "/%40/", "/%5B/", "/%5C/", "/%5D/", "/%5E/", "/%7B/", "/%7C/", "/%7D/", "/%7E/", "/\./");
278: $rep_pat = array("-", "-", "-", "-", "-", "-100", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-at-", "-", "-", "-", "-", "-", "-", "-", "-", "-");
279: $title = preg_replace($pattern, $rep_pat, $title);
280:
281:
282: $pattern = array("/%B0/", "/%E8/", "/%E9/", "/%EA/", "/%EB/", "/%E7/", "/%E0/", "/%E2/", "/%E4/", "/%EE/", "/%EF/", "/%F9/", "/%FC/", "/%FB/", "/%F4/", "/%F6/");
283: $rep_pat = array("-", "e", "e", "e", "e", "c", "a", "a", "a", "i", "i", "u", "u", "u", "o", "o");
284: $title = preg_replace($pattern, $rep_pat, $title);
285: $tableau = explode("-", $title);
286: $tableau = array_filter($tableau, array('PublisherMetagen', 'emptyString'));
287: $title = implode("-", $tableau);
288: if (sizeof($title) > 0) {
289: if ($withExt) {
290: $title .= '.html';
291: }
292: return $title;
293: }
294: return '';
295: }
296:
297: 298: 299: 300: 301: 302:
303: public function purifyText($text, $keyword = false)
304: {
305: $text = str_replace(' ', ' ', $text);
306: $text = str_replace('<br />', ' ', $text);
307: $text = strip_tags($text);
308: $text = html_entity_decode($text);
309: $text = $this->_myts->undoHtmlSpecialChars($text);
310: $text = str_replace(')', ' ', $text);
311: $text = str_replace('(', ' ', $text);
312: $text = str_replace(':', ' ', $text);
313: $text = str_replace('&euro', ' euro ', $text);
314: $text = str_replace('&hellip', '...', $text);
315: $text = str_replace('&rsquo', ' ', $text);
316: $text = str_replace('!', ' ', $text);
317: $text = str_replace('?', ' ', $text);
318: $text = str_replace('"', ' ', $text);
319: $text = str_replace('-', ' ', $text);
320: $text = str_replace('\n', ' ', $text);
321: if ($keyword) {
322: $text = str_replace('.', ' ', $text);
323: $text = str_replace(',', ' ', $text);
324: $text = str_replace('\'', ' ', $text);
325: }
326: $text = str_replace(';', ' ', $text);
327: return $text;
328: }
329:
330: 331: 332: 333: 334:
335: public function html2text($document)
336: {
337:
338:
339:
340:
341:
342:
343: $search = array(
344: "'<script[^>]*?>.*?</script>'si",
345: "'<img.*?/>'si",
346: "'<[\/\!]*?[^<>]*?>'si",
347: "'([\r\n])[\s]+'",
348: "'&(quot|#34);'i",
349: "'&(amp|#38);'i",
350: "'&(lt|#60);'i",
351: "'&(gt|#62);'i",
352: "'&(nbsp|#160);'i",
353: "'&(iexcl|#161);'i",
354: "'&(cent|#162);'i",
355: "'&(pound|#163);'i",
356: "'&(copy|#169);'i",
357:
358: );
359:
360: $replace = array(
361: "",
362: "",
363: "",
364: "\\1",
365: "\"",
366: "&",
367: "<",
368: ">",
369: " ",
370: chr(161),
371: chr(162),
372: chr(163),
373: chr(169),
374:
375: );
376: $text = preg_replace($search, $replace, $document);
377: return $text;
378: }
379: }
380: