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.8KB

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