Commit | Line | Data |
---|---|---|
d215cdb0 AE |
1 | <?php |
2 | namespace wcf\system; | |
3 | use wcf\util\StringUtil; | |
4 | ||
5 | /** | |
6 | * Handles meta tags. | |
7 | * | |
8 | * @author Alexander Ebert | |
9 | * @copyright 2001-2013 WoltLab GmbH | |
10 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
11 | * @package com.woltlab.wcf.message | |
12 | * @subpackage system.message | |
13 | * @category Community Framework | |
14 | */ | |
15 | class MetaTagHandler extends SingletonFactory implements \Countable, \Iterator { | |
16 | /** | |
17 | * current iterator index | |
18 | * @var integer | |
19 | */ | |
20 | protected $index = 0; | |
21 | ||
22 | /** | |
23 | * list of index to object relation | |
24 | * @var array<integer> | |
25 | */ | |
26 | protected $indexToObject = null; | |
27 | ||
28 | /** | |
29 | * regex object | |
30 | * @var wcf\system\Regex; | |
31 | */ | |
32 | protected $regex = null; | |
33 | ||
34 | /** | |
35 | * list of meta tags | |
36 | * @var array | |
37 | */ | |
38 | protected $objects = array(); | |
39 | ||
40 | /** | |
41 | * @see wcf\system\SingletonFactory::init() | |
42 | */ | |
43 | protected function init() { | |
44 | $this->regex = new Regex('^https?://'); | |
45 | ||
46 | // set default tags | |
47 | $this->addTag('description', 'description', WCF::getLanguage()->get(META_DESCRIPTION)); | |
48 | $this->addTag('keywords', 'keywords', WCF::getLanguage()->get(META_KEYWORDS)); | |
49 | $this->addTag('og:site_name', 'og:site_name', WCF::getLanguage()->get(PAGE_TITLE)); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Adds or replaces a meta tag. | |
54 | * | |
55 | * @param string $identifier | |
56 | * @param string $name | |
57 | * @param string $value | |
58 | * @param boolean $isProperty | |
59 | */ | |
60 | public function addTag($identifier, $name, $value, $isProperty = false) { | |
61 | if (!$this->regex->match($value)) { | |
62 | $value = StringUtil::encodeHTML($value); | |
63 | } | |
64 | ||
65 | $this->objects[$identifier] = array( | |
66 | 'isProperty' => $isProperty, | |
67 | 'name' => $name, | |
68 | 'value' => $value | |
69 | ); | |
70 | } | |
71 | ||
72 | /** | |
73 | * Removes a meta tag. | |
74 | * | |
75 | * @param string $identifier | |
76 | */ | |
77 | public function removeTag($identifier) { | |
78 | if (isset($this->objects[$identifier])) { | |
79 | unset($this->objects[$identifier]); | |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
84 | * @see \Countable::count() | |
85 | */ | |
86 | public function count() { | |
87 | return count($this->objects); | |
88 | } | |
89 | ||
90 | /** | |
91 | * @see \Iterator::current() | |
92 | */ | |
93 | public function current() { | |
94 | $tag = $this->objects[$this->indexToObject[$this->index]]; | |
95 | ||
96 | return '<meta ' . ($tag['isProperty'] ? 'property' : 'name') . '="' . $tag['name'] . '" content="' + $tag['value'] + '" />'; | |
97 | } | |
98 | ||
99 | /** | |
100 | * CAUTION: This methods does not return the current iterator index, | |
101 | * rather than the object key which maps to that index. | |
102 | * | |
103 | * @see \Iterator::key() | |
104 | */ | |
105 | public function key() { | |
106 | return $this->indexToObject[$this->index]; | |
107 | } | |
108 | ||
109 | /** | |
110 | * @see \Iterator::next() | |
111 | */ | |
112 | public function next() { | |
113 | ++$this->index; | |
114 | } | |
115 | ||
116 | /** | |
117 | * @see \Iterator::rewind() | |
118 | */ | |
119 | public function rewind() { | |
120 | $this->index = 0; | |
121 | } | |
122 | ||
123 | /** | |
124 | * @see \Iterator::valid() | |
125 | */ | |
126 | public function valid() { | |
127 | return isset($this->indexToObject[$this->index]); | |
128 | } | |
129 | } |