123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- /**
- * Sphinx database abstraction layer
- */
- class SphinxDB {
- /**
- * Placeholder for db link
- *
- * @var object
- */
- protected $db = '';
- /**
- * Contains name of db driver is correct extension exists
- *
- * @var string
- */
- protected $dbDriver = 'none';
- /**
- * Contains global configuration
- *
- * @var array
- */
- protected $altCfg = array();
- /**
- * Contains list of indexes to search in
- *
- * @var string
- */
- protected $searchIndexes = 'ip,mac,realname,login,fulladdress,mobile,phone';
- /**
- * Contains additional sorting parameters
- *
- * @var string
- */
- protected $queryOptions = ' ';
- /**
- * Limit number of search results
- */
- const SEARCHLIMIT = 100;
- public function __construct() {
- $this->LoadAlter();
- $this->dbConnect();
- }
- /**
- * load alter.ini config
- *
- * @return void
- */
- protected function LoadAlter() {
- global $ubillingConfig;
- $this->altCfg = $ubillingConfig->getAlter();
- }
- /**
- * Connect to Sphinx DB if possible and needed extenstions are loaded
- *
- * @return boolean or object
- */
- protected function dbConnect() {
- $params = array(
- 'SPHINX_SEARCH_HOST',
- 'SPHINX_SEARCH_SQL_PORT',
- 'SPHINX_SEARCH_USER',
- 'SPHINX_SEARCH_PASSWORD',
- 'SPHINX_SEARCH_DB'
- );
- foreach ($params as $param) {
- if (!isset($this->altCfg[$param])) {
- return false;
- }
- }
- $host = $this->altCfg['SPHINX_SEARCH_HOST'];
- $port = $this->altCfg['SPHINX_SEARCH_SQL_PORT'];
- $user = $this->altCfg['SPHINX_SEARCH_USER'];
- $password = $this->altCfg['SPHINX_SEARCH_PASSWORD'];
- $db = $this->altCfg['SPHINX_SEARCH_DB'];
- if (!extension_loaded('mysql')) {
- $this->db = new mysqli($host, $user, $password, $db, $port);
- if ($this->db->connect_error) {
- die('Connection error (' . $this->db->connect_errno . ') ' . $this->db->connect_error);
- }
- $this->dbDriver = 'mysqli';
- return true;
- } else {
- $this->db = mysql_connect($host . ':' . $port, $user, $password);
- if (empty($this->db)) {
- die('Unable to connect to database server!');
- }
- $this->dbDriver = 'legacy';
- return true;
- }
- }
- /**
- * Query search in our fulltextsearch engine
- *
- * @param string $searchString
- * @return json array
- */
- public function searchQuery($searchString) {
- $search = array();
- if (isset($this->altCfg['SPHINX_SEARCH_INDEXES'])) {
- $this->searchIndexes = $this->altCfg['SPHINX_SEARCH_INDEXES'];
- }
- if (isset($this->altCfg['SPHINX_SEARCH_SORT'])) {
- $this->queryOptions .= $this->altCfg['SPHINX_SEARCH_SORT'];
- }
- if (isset($this->altCfg['SPHINX_SEARCH_LIMIT'])) {
- $this->queryOptions .= ' LIMIT ' . $this->altCfg['SPHINX_SEARCH_LIMIT'];
- } else {
- $this->queryOptions .= ' LIMIT ' . self::SEARCHLIMIT;
- }
- if (!empty($searchString)) {
- if ($this->dbDriver == 'none') {
- return $search;
- }
- $query = "SELECT * FROM " . $this->searchIndexes . " WHERE MATCH ('" . $searchString . "') " . $this->queryOptions;
- if ($this->dbDriver == 'mysqli') {
- if ($result = $this->db->query($query, MYSQLI_USE_RESULT)) {
- while ($row = $result->fetch_assoc()) {
- $search[] = $row;
- }
- }
- }
- if ($this->dbDriver == 'legacy') {
- $queried = mysql_query($query, $this->db);
- while ($row = mysql_fetch_assoc($queried)) {
- $search[] = $row;
- }
- }
- $search = json_encode($search);
- return $search;
- }
- }
- }
- /**
- * Sphinx user-search implementation
- */
- class SphinxSearch {
- /**
- * Placeholder for Sphinx DB connection
- *
- * @var object
- */
- protected $db = '';
- public function __construct($searchString = '') {
- $this->db = new SphinxDB;
- if (!empty($searchString)) {
- $this->returnSearchResult($searchString);
- }
- }
- /**
- * Escape unwanted characters
- *
- * @param string $string
- * @return string
- */
- protected function escapeString($string) {
- $from = array('\\', '(', ')', '|', '-', '!', '@', '~', '"', '&', '/', '^', '$', '=', '<');
- $to = array('\\\\', '\\\(', '\\\)', '\\\|', '\\\-', '\\\!', '\\\@', '\\\~', '\\\"', '\\\&', '\\\/', '\\\^', '\\\$', '\\\=', '\\\<');
- return str_replace($from, $to, $string);
- }
- /**
- * Send our string info search query and return json result.
- *
- * @param string $searchString
- * @return json array
- */
- protected function returnSearchResult($searchString) {
- $escapedSearchString = $this->escapeString($searchString);
- die($this->db->searchQuery($escapedSearchString));
- }
- }
|