<?php /* vim: set expandtab sw=4 ts=4 sts=4: */ /** * Saved searches managing * * @package PhpMyAdmin */ namespace PhpMyAdmin; use PhpMyAdmin\Message; use PhpMyAdmin\Relation; use PhpMyAdmin\Response; use PhpMyAdmin\Util; /** * Saved searches managing * * @package PhpMyAdmin */ class SavedSearches { /** * Global configuration * @var array */ private $_config = null; /** * Id * @var int|null */ private $_id = null; /** * Username * @var string */ private $_username = null; /** * DB name * @var string */ private $_dbname = null; /** * Saved search name * @var string */ private $_searchName = null; /** * Criterias * @var array */ private $_criterias = null; /** * @var Relation $relation */ private $relation; /** * Public constructor * * @param array $config Global configuration */ public function __construct(array $config) { $this->setConfig($config); $this->relation = new Relation(); } /** * Setter of id * * @param int|null $searchId Id of search * * @return static */ public function setId($searchId) { $searchId = (int)$searchId; if (empty($searchId)) { $searchId = null; } $this->_id = $searchId; return $this; } /** * Getter of id * * @return int|null */ public function getId() { return $this->_id; } /** * Setter of searchName * * @param string $searchName Saved search name * * @return static */ public function setSearchName($searchName) { $this->_searchName = $searchName; return $this; } /** * Getter of searchName * * @return string */ public function getSearchName() { return $this->_searchName; } /** * Setter of config * * @param array $config Global configuration * * @return static */ public function setConfig(array $config) { $this->_config = $config; return $this; } /** * Getter of config * * @return array */ public function getConfig() { return $this->_config; } /** * Setter for criterias * * @param array|string $criterias Criterias of saved searches * @param bool $json Criterias are in JSON format * * @return static */ public function setCriterias($criterias, $json = false) { if (true === $json && is_string($criterias)) { $this->_criterias = json_decode($criterias, true); return $this; } $aListFieldsToGet = array( 'criteriaColumn', 'criteriaSort', 'criteriaShow', 'criteria', 'criteriaAndOrRow', 'criteriaAndOrColumn', 'rows', 'TableList' ); $data = array(); $data['criteriaColumnCount'] = count($criterias['criteriaColumn']); foreach ($aListFieldsToGet as $field) { if (isset($criterias[$field])) { $data[$field] = $criterias[$field]; } } /* Limit amount of rows */ if (!isset($data['rows'])) { $data['rows'] = 0; } else { $data['rows'] = min( max(0, intval($data['rows'])), 100 ); } for ($i = 0; $i <= $data['rows']; $i++) { $data['Or' . $i] = $criterias['Or' . $i]; } $this->_criterias = $data; return $this; } /** * Getter for criterias * * @return array */ public function getCriterias() { return $this->_criterias; } /** * Setter for username * * @param string $username Username * * @return static */ public function setUsername($username) { $this->_username = $username; return $this; } /** * Getter for username * * @return string */ public function getUsername() { return $this->_username; } /** * Setter for DB name * * @param string $dbname DB name * * @return static */ public function setDbname($dbname) { $this->_dbname = $dbname; return $this; } /** * Getter for DB name * * @return string */ public function getDbname() { return $this->_dbname; } /** * Save the search * * @return boolean */ public function save() { if (null == $this->getSearchName()) { $message = Message::error( __('Please provide a name for this bookmarked search.') ); $response = Response::getInstance(); $response->setRequestStatus($message->isSuccess()); $response->addJSON('fieldWithError', 'searchName'); $response->addJSON('message', $message); exit; } if (null == $this->getUsername() || null == $this->getDbname() || null == $this->getSearchName() || null == $this->getCriterias() ) { $message = Message::error( __('Missing information to save the bookmarked search.') ); $response = Response::getInstance(); $response->setRequestStatus($message->isSuccess()); $response->addJSON('message', $message); exit; } $savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db']) . "." . Util::backquote($this->_config['cfgRelation']['savedsearches']); //If it's an insert. if (null === $this->getId()) { $wheres = array( "search_name = '" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "'" ); $existingSearches = $this->getList($wheres); if (!empty($existingSearches)) { $message = Message::error( __('An entry with this name already exists.') ); $response = Response::getInstance(); $response->setRequestStatus($message->isSuccess()); $response->addJSON('fieldWithError', 'searchName'); $response->addJSON('message', $message); exit; } $sqlQuery = "INSERT INTO " . $savedSearchesTbl . "(`username`, `db_name`, `search_name`, `search_data`)" . " VALUES (" . "'" . $GLOBALS['dbi']->escapeString($this->getUsername()) . "'," . "'" . $GLOBALS['dbi']->escapeString($this->getDbname()) . "'," . "'" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "'," . "'" . $GLOBALS['dbi']->escapeString(json_encode($this->getCriterias())) . "')"; $result = (bool) $this->relation->queryAsControlUser($sqlQuery); if (!$result) { return false; } $this->setId($GLOBALS['dbi']->insertId()); return true; } //Else, it's an update. $wheres = array( "id != " . $this->getId(), "search_name = '" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "'" ); $existingSearches = $this->getList($wheres); if (!empty($existingSearches)) { $message = Message::error( __('An entry with this name already exists.') ); $response = Response::getInstance(); $response->setRequestStatus($message->isSuccess()); $response->addJSON('fieldWithError', 'searchName'); $response->addJSON('message', $message); exit; } $sqlQuery = "UPDATE " . $savedSearchesTbl . "SET `search_name` = '" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "', " . "`search_data` = '" . $GLOBALS['dbi']->escapeString(json_encode($this->getCriterias())) . "' " . "WHERE id = " . $this->getId(); return (bool) $this->relation->queryAsControlUser($sqlQuery); } /** * Delete the search * * @return boolean */ public function delete() { if (null == $this->getId()) { $message = Message::error( __('Missing information to delete the search.') ); $response = Response::getInstance(); $response->setRequestStatus($message->isSuccess()); $response->addJSON('fieldWithError', 'searchId'); $response->addJSON('message', $message); exit; } $savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db']) . "." . Util::backquote($this->_config['cfgRelation']['savedsearches']); $sqlQuery = "DELETE FROM " . $savedSearchesTbl . "WHERE id = '" . $GLOBALS['dbi']->escapeString($this->getId()) . "'"; return (bool) $this->relation->queryAsControlUser($sqlQuery); } /** * Load the current search from an id. * * @return bool Success */ public function load() { if (null == $this->getId()) { $message = Message::error( __('Missing information to load the search.') ); $response = Response::getInstance(); $response->setRequestStatus($message->isSuccess()); $response->addJSON('fieldWithError', 'searchId'); $response->addJSON('message', $message); exit; } $savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db']) . "." . Util::backquote($this->_config['cfgRelation']['savedsearches']); $sqlQuery = "SELECT id, search_name, search_data " . "FROM " . $savedSearchesTbl . " " . "WHERE id = '" . $GLOBALS['dbi']->escapeString($this->getId()) . "' "; $resList = $this->relation->queryAsControlUser($sqlQuery); if (false === ($oneResult = $GLOBALS['dbi']->fetchArray($resList))) { $message = Message::error(__('Error while loading the search.')); $response = Response::getInstance(); $response->setRequestStatus($message->isSuccess()); $response->addJSON('fieldWithError', 'searchId'); $response->addJSON('message', $message); exit; } $this->setSearchName($oneResult['search_name']) ->setCriterias($oneResult['search_data'], true); return true; } /** * Get the list of saved searches of a user on a DB * * @param string[] $wheres List of filters * * @return array List of saved searches or empty array on failure */ public function getList(array $wheres = array()) { if (null == $this->getUsername() || null == $this->getDbname() ) { return array(); } $savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db']) . "." . Util::backquote($this->_config['cfgRelation']['savedsearches']); $sqlQuery = "SELECT id, search_name " . "FROM " . $savedSearchesTbl . " " . "WHERE " . "username = '" . $GLOBALS['dbi']->escapeString($this->getUsername()) . "' " . "AND db_name = '" . $GLOBALS['dbi']->escapeString($this->getDbname()) . "' "; foreach ($wheres as $where) { $sqlQuery .= "AND " . $where . " "; } $sqlQuery .= "order by search_name ASC "; $resList = $this->relation->queryAsControlUser($sqlQuery); $list = array(); while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) { $list[$oneResult['id']] = $oneResult['search_name']; } return $list; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
Config | Folder | 0755 |
|
|
Controllers | Folder | 0755 |
|
|
Database | Folder | 0755 |
|
|
Dbi | Folder | 0755 |
|
|
Di | Folder | 0755 |
|
|
Display | Folder | 0755 |
|
|
Engines | Folder | 0755 |
|
|
Gis | Folder | 0755 |
|
|
Navigation | Folder | 0755 |
|
|
Plugins | Folder | 0755 |
|
|
Properties | Folder | 0755 |
|
|
Rte | Folder | 0755 |
|
|
Server | Folder | 0755 |
|
|
Twig | Folder | 0755 |
|
|
Utils | Folder | 0755 |
|
|
Advisor.php | File | 18.79 KB | 0644 |
|
Bookmark.php | File | 10.37 KB | 0644 |
|
BrowseForeigners.php | File | 10.73 KB | 0644 |
|
CentralColumns.php | File | 53.12 KB | 0644 |
|
Charsets.php | File | 24.92 KB | 0644 |
|
CheckUserPrivileges.php | File | 11.58 KB | 0644 |
|
Config.php | File | 59.69 KB | 0644 |
|
Console.php | File | 3.58 KB | 0644 |
|
Core.php | File | 38.98 KB | 0644 |
|
CreateAddField.php | File | 17.97 KB | 0644 |
|
DatabaseInterface.php | File | 103.86 KB | 0644 |
|
Encoding.php | File | 8.25 KB | 0644 |
|
Error.php | File | 13.05 KB | 0644 |
|
ErrorHandler.php | File | 16.68 KB | 0644 |
|
ErrorReport.php | File | 8.37 KB | 0644 |
|
Export.php | File | 40.32 KB | 0644 |
|
File.php | File | 20.53 KB | 0644 |
|
FileListing.php | File | 2.83 KB | 0644 |
|
Font.php | File | 4.25 KB | 0644 |
|
Footer.php | File | 10.54 KB | 0644 |
|
Header.php | File | 25.81 KB | 0644 |
|
Import.php | File | 55.59 KB | 0644 |
|
Index.php | File | 24.63 KB | 0644 |
|
IndexColumn.php | File | 4.43 KB | 0644 |
|
InsertEdit.php | File | 129.29 KB | 0644 |
|
IpAllowDeny.php | File | 9.21 KB | 0644 |
|
Language.php | File | 4.3 KB | 0644 |
|
LanguageManager.php | File | 23.42 KB | 0644 |
|
Linter.php | File | 5.1 KB | 0644 |
|
ListAbstract.php | File | 3.15 KB | 0644 |
|
ListDatabase.php | File | 4.22 KB | 0644 |
|
Logging.php | File | 2.56 KB | 0644 |
|
Menu.php | File | 22.34 KB | 0644 |
|
Message.php | File | 19.19 KB | 0644 |
|
Mime.php | File | 891 B | 0644 |
|
MultSubmits.php | File | 23.19 KB | 0644 |
|
Normalization.php | File | 39.03 KB | 0644 |
|
OpenDocument.php | File | 8.5 KB | 0644 |
|
Operations.php | File | 79.06 KB | 0644 |
|
OutputBuffering.php | File | 3.63 KB | 0644 |
|
ParseAnalyze.php | File | 2.46 KB | 0644 |
|
Partition.php | File | 7.26 KB | 0644 |
|
Pdf.php | File | 4.07 KB | 0644 |
|
Plugins.php | File | 21.42 KB | 0644 |
|
RecentFavoriteTable.php | File | 12.13 KB | 0644 |
|
Relation.php | File | 78.19 KB | 0644 |
|
RelationCleanup.php | File | 14.7 KB | 0644 |
|
Replication.php | File | 5.37 KB | 0644 |
|
ReplicationGui.php | File | 41.79 KB | 0644 |
|
Response.php | File | 16.31 KB | 0644 |
|
Sanitize.php | File | 14.15 KB | 0644 |
|
SavedSearches.php | File | 11.95 KB | 0644 |
|
Scripts.php | File | 5.33 KB | 0644 |
|
Session.php | File | 7.82 KB | 0644 |
|
Sql.php | File | 88.22 KB | 0644 |
|
SqlQueryForm.php | File | 17.19 KB | 0644 |
|
StorageEngine.php | File | 13.47 KB | 0644 |
|
SubPartition.php | File | 3.53 KB | 0644 |
|
SysInfo.php | File | 1.54 KB | 0644 |
|
SysInfoBase.php | File | 801 B | 0644 |
|
SysInfoLinux.php | File | 1.96 KB | 0644 |
|
SysInfoSunOS.php | File | 1.87 KB | 0644 |
|
SysInfoWINNT.php | File | 3.25 KB | 0644 |
|
SystemDatabase.php | File | 3.84 KB | 0644 |
|
Table.php | File | 92.59 KB | 0644 |
|
Template.php | File | 3.91 KB | 0644 |
|
Theme.php | File | 10.53 KB | 0644 |
|
ThemeManager.php | File | 10.73 KB | 0644 |
|
Tracker.php | File | 29.72 KB | 0644 |
|
Tracking.php | File | 41.99 KB | 0644 |
|
Transformations.php | File | 16.12 KB | 0644 |
|
TwoFactor.php | File | 7.1 KB | 0644 |
|
Types.php | File | 22.75 KB | 0644 |
|
Url.php | File | 8.17 KB | 0644 |
|
UserPassword.php | File | 8.47 KB | 0644 |
|
UserPreferences.php | File | 8.52 KB | 0644 |
|
Util.php | File | 162.99 KB | 0644 |
|
VersionInformation.php | File | 6.34 KB | 0644 |
|
ZipExtension.php | File | 9.98 KB | 0644 |
|