Fixed bug #17034: Live search also returns records which not available to the user...
[Packages/TYPO3.CMS.git] / t3lib / search / class.t3lib_search_livesearch_queryParser.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2010 Michael Klapper <michael.klapper@aoemedia.de>
6 * (c) 2010 Jeff Segars <jeff@webempoweredchurch.org>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Class for parsing query parameters in backend live search.
31 *
32 * @author Michael Klapper <michael.klapper@aoemedia.de>
33 * @author Jeff Segars <jeff@webempoweredchurch.org>
34 * @package TYPO3
35 * @subpackage t3lib
36 */
37 class t3lib_search_livesearch_queryParser {
38
39 /**
40 * @var string
41 */
42 protected $commandKey = '';
43
44 /**
45 * @var string
46 */
47 protected $tableName = '';
48
49 /**
50 * @var string
51 */
52 const COMMAND_KEY_INDICATOR = '#';
53
54 /**
55 * @var string
56 */
57 const COMMAND_SPLIT_INDICATOR = ':';
58
59 /**
60 * Retrive the validated command key
61 *
62 * @return string Command name
63 */
64 protected function extractKeyFromQuery($query) {
65 $keyAndValue = substr($query, 1);
66 $key = explode(':', $keyAndValue);
67 $this->commandKey = $key[0];
68 }
69
70 /**
71 * Extract the search value from the full search query which contains also the command part.
72 *
73 * @param string $query For example #news:weather
74 * @return string The extracted search value
75 */
76 public function getSearchQueryValue($query) {
77 $this->extractKeyFromQuery($query);
78 return str_replace(self::COMMAND_KEY_INDICATOR . $this->commandKey . self::COMMAND_SPLIT_INDICATOR, '', $query);
79 }
80
81 /**
82 * Find the registerd table command and retrieve the matching table name.
83 *
84 * @param string $query
85 * @return string Database Table name
86 */
87 public function getTableNameFromCommand($query) {
88 $tableName = '';
89 $this->extractKeyFromQuery($query);
90 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch']) && array_key_exists($this->commandKey, $GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'])) {
91 $tableName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'][$this->commandKey];
92 }
93
94 return $tableName;
95 }
96
97 /**
98 * Verify if an given query contains a page jump command.
99 *
100 * @param string $query A valid value looks like '#14'
101 * @return integer
102 */
103 public function getId($query) {
104 return str_replace(self::COMMAND_KEY_INDICATOR, '', $query);
105 }
106
107 /**
108 * Verify if a given query contains a page jump command.
109 *
110 * @param string $query A valid value looks like '#14'
111 * @return boolean
112 */
113 public function isValidPageJump($query) {
114 $isValid = FALSE;
115
116 if (preg_match('~^#(\d)+$~', $query)) {
117 $isValid = TRUE;
118 }
119
120 return $isValid;
121 }
122
123 /**
124 * Verify if an given query contains an registered command key.
125 *
126 * @param string $query
127 * @return boolean
128 */
129 public function isValidCommand($query) {
130 $isValid = FALSE;
131 if (strpos($query, self::COMMAND_KEY_INDICATOR) === 0 &&
132 strpos($query, self::COMMAND_SPLIT_INDICATOR) > 1 &&
133 $this->getTableNameFromCommand($query)) {
134 $isValid = TRUE;
135 }
136
137 return $isValid;
138 }
139
140 /**
141 * Gets the command for the given table.
142 *
143 * @param string $tableName The table to find a command for.
144 * @return string
145 */
146 public function getCommandForTable($tableName) {
147 $commandArray = array_keys($GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'], $tableName);
148 if (is_array($commandArray)) {
149 $command = $commandArray[0];
150 } else {
151 $command = FALSE;
152 }
153
154 return $command;
155 }
156
157 /**
158 * Gets the page jump command for a given query.
159 *
160 * @param string $query
161 * @return string
162 */
163 public function getCommandForPageJump($query) {
164 if ($this->isValidPageJump($query)) {
165 $command = $this->getCommandForTable('pages');
166 $id = $this->getId($query);
167
168 $resultQuery = self::COMMAND_KEY_INDICATOR . $command . self::COMMAND_SPLIT_INDICATOR . $id;
169 } else {
170 $resultQuery = FALSE;
171 }
172
173 return $resultQuery;
174 }
175 }
176
177 ?>