BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sphinxql.class.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. if (!extension_loaded('mysqli')) {
  3. error('Mysqli Extension not loaded.');
  4. }
  5. class Sphinxql extends mysqli
  6. {
  7. private static $Connections = [];
  8. private $Server;
  9. private $Port;
  10. private $Socket;
  11. private $Ident;
  12. private $Connected = false;
  13. public static $Queries = [];
  14. public static $Time = 0.0;
  15. /**
  16. * Initialize Sphinxql object
  17. *
  18. * @param string $Server server address or hostname
  19. * @param int $Port listening port
  20. * @param string $Socket Unix socket address, overrides $Server:$Port
  21. */
  22. public function __construct($Server, $Port, $Socket)
  23. {
  24. $this->Server = $Server;
  25. $this->Port = $Port;
  26. $this->Socket = $Socket;
  27. $this->Ident = self::get_ident($Server, $Port, $Socket);
  28. }
  29. /**
  30. * Create server ident based on connection information
  31. *
  32. * @param string $Server server address or hostname
  33. * @param int $Port listening port
  34. * @param string $Socket Unix socket address, overrides $Server:$Port
  35. * @return identification string
  36. */
  37. private static function get_ident($Server, $Port, $Socket)
  38. {
  39. if ($Socket) {
  40. return $Socket;
  41. } else {
  42. return "$Server:$Port";
  43. }
  44. }
  45. /**
  46. * Create Sphinxql object or return existing one
  47. *
  48. * @param string $Server server address or hostname
  49. * @param int $Port listening port
  50. * @param string $Socket Unix socket address, overrides $Server:$Port
  51. * @return Sphinxql object
  52. */
  53. public static function init_connection($Server, $Port, $Socket)
  54. {
  55. $Ident = self::get_ident($Server, $Port, $Socket);
  56. if (!isset(self::$Connections[$Ident])) {
  57. self::$Connections[$Ident] = new Sphinxql($Server, $Port, $Socket);
  58. }
  59. return self::$Connections[$Ident];
  60. }
  61. /**
  62. * Connect the Sphinxql object to the Sphinx server
  63. */
  64. public function sph_connect()
  65. {
  66. if ($this->Connected || $this->connect_errno) {
  67. return;
  68. }
  69. global $Debug;
  70. $Debug->set_flag("Connecting to Sphinx server $this->Ident");
  71. for ($Attempt = 0; $Attempt < 3; $Attempt++) {
  72. parent::__construct($this->Server, '', '', '', $this->Port, $this->Socket);
  73. if (!$this->connect_errno) {
  74. $this->Connected = true;
  75. break;
  76. }
  77. sleep(1);
  78. }
  79. if ($this->connect_errno) {
  80. $Errno = $this->connect_errno;
  81. $Error = $this->connect_error;
  82. $this->error("Connection failed. (".strval($Errno).": ".strval($Error).")");
  83. $Debug->set_flag("Could not connect to Sphinx server $this->Ident. (".strval($Errno).": ".strval($Error).")");
  84. } else {
  85. $Debug->set_flag("Connected to Sphinx server $this->Ident");
  86. }
  87. }
  88. /**
  89. * Print a message to privileged users and optionally halt page processing
  90. *
  91. * @param string $Msg message to display
  92. * @param bool $Halt halt page processing. Default is to continue processing the page
  93. * @return Sphinxql object
  94. */
  95. public function error($Msg, $Halt = false)
  96. {
  97. global $Debug;
  98. $ErrorMsg = 'SphinxQL ('.$this->Ident.'): '.strval($Msg);
  99. $Debug->analysis('SphinxQL Error', $ErrorMsg, 3600*24);
  100. if ($Halt === true && (DEBUG_MODE || check_perms('site_debug'))) {
  101. echo '<pre>'.display_str($ErrorMsg).'</pre>';
  102. error();
  103. } elseif ($Halt === true) {
  104. error(-1);
  105. }
  106. }
  107. /**
  108. * Escape special characters before sending them to the Sphinx server.
  109. * Two escapes needed because the first one is eaten up by the mysql driver.
  110. *
  111. * @param string $String string to escape
  112. * @return escaped string
  113. */
  114. public static function sph_escape_string($String)
  115. {
  116. return strtr(
  117. strtolower($String),
  118. array(
  119. '('=>'\\\\(',
  120. ')'=>'\\\\)',
  121. '|'=>'\\\\|',
  122. '-'=>'\\\\-',
  123. '@'=>'\\\\@',
  124. '~'=>'\\\\~',
  125. '&'=>'\\\\&',
  126. '\''=>'\\\'',
  127. '<'=>'\\\\<',
  128. '!'=>'\\\\!',
  129. '"'=>'\\\\"',
  130. '/'=>'\\\\/',
  131. '*'=>'\\\\*',
  132. '$'=>'\\\\$',
  133. '^'=>'\\\\^',
  134. '\\'=>'\\\\\\\\')
  135. );
  136. }
  137. /**
  138. * Register sent queries globally for later retrieval by debug functions
  139. *
  140. * @param string $QueryString query text
  141. * @param param $QueryProcessTime time building and processing the query
  142. */
  143. public static function register_query($QueryString, $QueryProcessTime)
  144. {
  145. self::$Queries[] = array($QueryString, $QueryProcessTime);
  146. self::$Time += $QueryProcessTime;
  147. }
  148. }