PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/classes/SEO.php

http://acp3.googlecode.com/
PHP | 361 lines | 195 code | 26 blank | 140 comment | 25 complexity | 1029c9b9a5b4bbe344493b526a8ffdfd MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * SEO
  4. *
  5. * @author Tino Goratsch
  6. * @package ACP3
  7. * @subpackage Core
  8. */
  9. /**
  10. * Klasse zum Setzen von URI Aliases, Keywords und Beschreibungen für Seiten
  11. *
  12. * @author Tino Goratsch
  13. * @package ACP3
  14. * @subpackage Core
  15. */
  16. class ACP3_SEO
  17. {
  18. /**
  19. * Caching Variable für die URI-Aliases
  20. *
  21. * @access private
  22. * @var array
  23. */
  24. private static $aliases = array();
  25. /**
  26. * Gibt die nächste Seite an
  27. *
  28. * @var string
  29. */
  30. private static $next_page = '';
  31. /**
  32. * Gibt die vorherige Seite an
  33. *
  34. * @var string
  35. */
  36. private static $previous_page = '';
  37. /**
  38. * Kanonische URL
  39. *
  40. * @var string
  41. */
  42. private static $canonical = '';
  43. /**
  44. * Setzt den Cache für die URI-Aliase
  45. *
  46. * @return boolean
  47. */
  48. private static function setSEOCache()
  49. {
  50. $aliases = ACP3_CMS::$db2->fetchAll('SELECT uri, alias, keywords, description, robots FROM ' . DB_PRE . 'seo');
  51. $c_aliases = count($aliases);
  52. $data = array();
  53. for ($i = 0; $i < $c_aliases; ++$i) {
  54. $data[$aliases[$i]['uri']] = array(
  55. 'alias' => $aliases[$i]['alias'],
  56. 'keywords' => $aliases[$i]['keywords'],
  57. 'description' => $aliases[$i]['description'],
  58. 'robots' => $aliases[$i]['robots']
  59. );
  60. }
  61. return ACP3_Cache::create('aliases', $data, 'seo');
  62. }
  63. /**
  64. * Gibt den Cache der URI-Aliase aus
  65. *
  66. * @return array
  67. */
  68. private static function getSEOCache()
  69. {
  70. if (ACP3_Cache::check('aliases', 'seo') === false)
  71. self::setSEOCache();
  72. return ACP3_Cache::output('aliases', 'seo');
  73. }
  74. /**
  75. * Gibt die für die jeweilige Seite gesetzten Metatags aus
  76. *
  77. * @return string
  78. */
  79. public static function getMetaTags()
  80. {
  81. $meta = array(
  82. 'description' => defined('IN_ADM') === true ? '' : self::getCurrentDescription(),
  83. 'keywords' => defined('IN_ADM') === true ? '' : self::getCurrentKeywords(),
  84. 'robots' => defined('IN_ADM') === true ? 'noindex,nofollow' : self::getCurrentRobotsSetting(),
  85. 'previous_page' => self::$previous_page,
  86. 'next_page' => self::$next_page,
  87. 'canonical' => self::$canonical,
  88. );
  89. ACP3_CMS::$view->assign('meta', $meta);
  90. return ACP3_CMS::$view->fetchTemplate('system/meta.tpl');
  91. }
  92. /**
  93. * Gibt die Beschreibung der aktuell angezeigten Seite oder der
  94. * Elternseite aus
  95. *
  96. * @return string
  97. */
  98. public static function getCurrentDescription()
  99. {
  100. $description = self::getDescription(ACP3_CMS::$uri->getCleanQuery());
  101. if (empty($description))
  102. $description = self::getDescription(ACP3_CMS::$uri->mod . '/' . ACP3_CMS::$uri->file);
  103. if (empty($description))
  104. $description = self::getDescription(ACP3_CMS::$uri->mod);
  105. return !empty($description) ? $description : CONFIG_SEO_META_DESCRIPTION;
  106. }
  107. /**
  108. * Gibt die Keywords der aktuell angezeigten Seite oder der
  109. * Elternseite aus
  110. *
  111. * @return string
  112. */
  113. public static function getCurrentKeywords()
  114. {
  115. $keywords = self::getKeywords(ACP3_CMS::$uri->getCleanQuery());
  116. if (empty($keywords))
  117. $keywords = self::getKeywords(ACP3_CMS::$uri->mod . '/' . ACP3_CMS::$uri->file);
  118. if (empty($keywords))
  119. $keywords = self::getKeywords(ACP3_CMS::$uri->mod);
  120. return strtolower(!empty($keywords) ? $keywords : CONFIG_SEO_META_KEYWORDS);
  121. }
  122. /**
  123. * Gibt den Robots-Metatag der aktuell angezeigten Seite oder der
  124. * Elternseite aus
  125. *
  126. * @return string
  127. */
  128. public static function getCurrentRobotsSetting()
  129. {
  130. $robots = self::getRobotsSetting(ACP3_CMS::$uri->getCleanQuery());
  131. if (empty($robots))
  132. $robots = self::getRobotsSetting(ACP3_CMS::$uri->mod . '/' . ACP3_CMS::$uri->file);
  133. if (empty($robots))
  134. $robots = self::getRobotsSetting(ACP3_CMS::$uri->mod);
  135. return strtolower(!empty($robots) ? $robots : self::getRobotsSetting());
  136. }
  137. /**
  138. * Gibt die Beschreibung der Seite aus
  139. *
  140. * @param string $path
  141. * @return string
  142. */
  143. public static function getDescription($path)
  144. {
  145. if (empty(self::$aliases))
  146. self::$aliases = self::getSEOCache();
  147. $path.= !preg_match('/\/$/', $path) ? '/' : '';
  148. return !empty(self::$aliases[$path]['description']) ? self::$aliases[$path]['description'] : '';
  149. }
  150. /**
  151. * Gibt die Schlüsselwörter der Seite aus
  152. *
  153. * @param string $path
  154. * @return string
  155. */
  156. public static function getKeywords($path)
  157. {
  158. if (empty(self::$aliases))
  159. self::$aliases = self::getSEOCache();
  160. $path.= !preg_match('/\/$/', $path) ? '/' : '';
  161. return !empty(self::$aliases[$path]['keywords']) ? self::$aliases[$path]['keywords'] : '';
  162. }
  163. /**
  164. * Gibt die jeweilige Einstellung für den Robots-Metatag aus
  165. *
  166. * @param string $path
  167. * @return string
  168. */
  169. public static function getRobotsSetting($path = '')
  170. {
  171. $replace = array(
  172. 1 => 'index,follow',
  173. 2 => 'index,nofollow',
  174. 3 => 'noindex,follow',
  175. 4 => 'noindex,nofollow',
  176. );
  177. if ($path === '') {
  178. return strtr(CONFIG_SEO_ROBOTS, $replace);
  179. } else {
  180. if (empty(self::$aliases))
  181. self::$aliases = self::getSEOCache();
  182. $path.= !preg_match('/\/$/', $path) ? '/' : '';
  183. $robot = isset(self::$aliases[$path]) === false || self::$aliases[$path]['robots'] == 0 ? CONFIG_SEO_ROBOTS : self::$aliases[$path]['robots'];
  184. return strtr($robot, $replace);
  185. }
  186. }
  187. /**
  188. * Gibt einen URI-Alias aus
  189. *
  190. * @param string $path
  191. * @return string
  192. */
  193. public static function getUriAlias($path, $for_form = false)
  194. {
  195. if (empty(self::$aliases))
  196. self::$aliases = self::getSEOCache();
  197. $path.= !preg_match('/\/$/', $path) ? '/' : '';
  198. return !empty(self::$aliases[$path]['alias']) ? self::$aliases[$path]['alias'] : ($for_form === true ? '' : $path);
  199. }
  200. /**
  201. * Setzt die kanonische URI
  202. *
  203. * @param string $path
  204. */
  205. public static function setCanonicalUri($path)
  206. {
  207. self::$canonical = $path;
  208. }
  209. /**
  210. * Setzt die nächste Seite
  211. *
  212. * @param string $path
  213. */
  214. public static function setNextPage($path)
  215. {
  216. self::$next_page = $path;
  217. }
  218. /**
  219. * Setzt die vorherige Seite
  220. *
  221. * @param string $path
  222. */
  223. public static function setPreviousPage($path)
  224. {
  225. self::$previous_page = $path;
  226. }
  227. /**
  228. * Löscht einen URI-Alias
  229. *
  230. * @param string $alias
  231. * @param string $path
  232. * @return boolean
  233. */
  234. public static function deleteUriAlias($path)
  235. {
  236. $path.= !preg_match('/\/$/', $path) ? '/' : '';
  237. $bool = ACP3_CMS::$db2->delete(DB_PRE . 'seo', array('uri' => $path));
  238. $bool2 = self::setSEOCache();
  239. return $bool !== false && $bool2 !== false ? true : false;
  240. }
  241. /**
  242. * Trägt einen URI-Alias in die Datenbank ein bzw. aktualisiert den Eintrag
  243. *
  244. * @param string $path
  245. * @param string $alias
  246. * @param string $keywords
  247. * @param string $description
  248. * @return boolean
  249. */
  250. public static function insertUriAlias($path, $alias, $keywords = '', $description = '', $robots = 0)
  251. {
  252. $path.= !preg_match('/\/$/', $path) ? '/' : '';
  253. $keywords = str_encode($keywords);
  254. $description = str_encode($description);
  255. // Vorhandenen Alias aktualisieren
  256. if (ACP3_CMS::$db2->fetchColumn('SELECT COUNT(*) FROM ' . DB_PRE . 'seo WHERE uri = ?', array($path)) == 1) {
  257. $bool = ACP3_CMS::$db2->update(DB_PRE . 'seo', array('alias' => $alias, 'keywords' => $keywords, 'description' => $description, 'robots' => (int) $robots), array('uri' => $path));
  258. // Neuer Eintrag in DB
  259. } else {
  260. $bool = ACP3_CMS::$db2->insert(DB_PRE . 'seo', array('alias' => $alias, 'uri' => $path, 'keywords' => $keywords, 'description' => $description, 'robots' => (int) $robots));
  261. }
  262. $bool2 = self::setSEOCache();
  263. return $bool !== false && $bool2 !== false ? true : false;
  264. }
  265. /**
  266. * Gibt die Formularfelder für die Suchmaschinenoptimierung aus
  267. *
  268. * @param string $alias
  269. * @param string $keywords
  270. * @param string $description
  271. * @param string $robots
  272. * @return string
  273. */
  274. public static function formFields($path = '')
  275. {
  276. if (!empty($path)) {
  277. $path.= !preg_match('/\/$/', $path) ? '/' : '';
  278. $alias = isset($_POST['alias']) ? $_POST['alias'] : ACP3_SEO::getUriAlias($path, true);
  279. $keywords = isset($_POST['seo_keywords']) ? $_POST['seo_keywords'] : ACP3_SEO::getKeywords($path);
  280. $description = isset($_POST['seo_description']) ? $_POST['seo_description'] : ACP3_SEO::getDescription($path);
  281. $robots = isset(self::$aliases[$path]) === true ? self::$aliases[$path]['robots'] : 0;
  282. } else {
  283. $alias = $keywords = $description = '';
  284. $robots = 0;
  285. }
  286. $seo = array(
  287. 'enable_uri_aliases' => (bool) CONFIG_SEO_ALIASES,
  288. 'alias' => isset($alias) ? $alias : '',
  289. 'keywords' => $keywords,
  290. 'description' => $description,
  291. 'robots' => array(
  292. array(
  293. 'value' => 0,
  294. 'selected' => selectEntry('seo_robots', 0, $robots),
  295. 'lang' => sprintf(ACP3_CMS::$lang->t('system', 'seo_robots_use_system_default'), ACP3_SEO::getRobotsSetting())
  296. ),
  297. array(
  298. 'value' => 1,
  299. 'selected' => selectEntry('seo_robots', 1, $robots),
  300. 'lang' => ACP3_CMS::$lang->t('system', 'seo_robots_index_follow')
  301. ),
  302. array(
  303. 'value' => 2,
  304. 'selected' => selectEntry('seo_robots', 2, $robots),
  305. 'lang' => ACP3_CMS::$lang->t('system', 'seo_robots_index_nofollow')
  306. ),
  307. array(
  308. 'value' => 3,
  309. 'selected' => selectEntry('seo_robots', 3, $robots),
  310. 'lang' => ACP3_CMS::$lang->t('system', 'seo_robots_noindex_follow')
  311. ),
  312. array(
  313. 'value' => 4,
  314. 'selected' => selectEntry('seo_robots', 4, $robots),
  315. 'lang' => ACP3_CMS::$lang->t('system', 'seo_robots_noindex_nofollow')
  316. )
  317. )
  318. );
  319. ACP3_CMS::$view->assign('seo', $seo);
  320. return ACP3_CMS::$view->fetchTemplate('system/seo_fields.tpl');
  321. }
  322. /**
  323. * Überprüft, ob ein URI-Alias existiert
  324. *
  325. * @param string $path
  326. * @return boolean
  327. */
  328. public static function uriAliasExists($path)
  329. {
  330. if (empty(self::$aliases))
  331. self::$aliases = self::getSEOCache();
  332. $path.= !preg_match('/\/$/', $path) ? '/' : '';
  333. return array_key_exists($path, self::$aliases) === true && !empty(self::$aliases[$path]['alias']);
  334. }
  335. }