PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/chat/lib/oc/functions.inc.php

https://gitlab.com/bechemm/opencaching.pl
PHP | 310 lines | 210 code | 32 blank | 68 comment | 49 complexity | d6dc68c12d47ebb0adc812eac29ec349 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. // called if mysql_query faild, sends email to sysadmin
  3. function sql_failed($sql)
  4. {
  5. sql_error();
  6. }
  7. function sqlValue($sql, $default)
  8. {
  9. $rs = sql($sql);
  10. if ($r = sql_fetch_row($rs)) {
  11. if ($r[0] == null)
  12. return $default;
  13. else
  14. return $r[0];
  15. } else
  16. return $default;
  17. }
  18. function sql($sql)
  19. {
  20. global $rootpath;
  21. global $sql_debug, $sql_warntime;
  22. global $sql_replacements;
  23. global $dblink, $sqlcommands;
  24. $args = func_get_args();
  25. unset($args[0]);
  26. $sqlpos = 0;
  27. $filtered_sql = '';
  28. // $sql von vorne bis hinten durchlaufen und alle &x ersetzen
  29. $nextarg = mb_strpos($sql, '&');
  30. while ($nextarg !== false) {
  31. // muss dieses & ersetzt werden, oder ist es escaped?
  32. $escapesCount = 0;
  33. while ((($nextarg - $escapesCount - 1) > 0) && (mb_substr($sql, $nextarg - $escapesCount - 1, 1) == '\\'))
  34. $escapesCount++;
  35. if (($escapesCount % 2) == 1)
  36. $nextarg++;
  37. else {
  38. $nextchar = mb_substr($sql, $nextarg + 1, 1);
  39. if (is_numeric($nextchar)) {
  40. $arglength = 0;
  41. $arg = '';
  42. // nächstes Zeichen das keine Zahl ist herausfinden
  43. while (mb_ereg_match('^[0-9]{1}', $nextchar) == 1) {
  44. $arg .= $nextchar;
  45. $arglength++;
  46. $nextchar = mb_substr($sql, $nextarg + $arglength + 1, 1);
  47. }
  48. // ok ... ersetzen
  49. $filtered_sql .= mb_substr($sql, $sqlpos, $nextarg - $sqlpos);
  50. $sqlpos = $nextarg + $arglength;
  51. if (isset($args[$arg])) {
  52. if (is_numeric($args[$arg]))
  53. $filtered_sql .= $args[$arg];
  54. else {
  55. if ((mb_substr($sql, $sqlpos - $arglength - 1, 1) == '\'') && (mb_substr($sql, $sqlpos + 1, 1) == '\''))
  56. $filtered_sql .= sql_escape($args[$arg]);
  57. else if ((mb_substr($sql, $sqlpos - $arglength - 1, 1) == '`') && (mb_substr($sql, $sqlpos + 1, 1) == '`'))
  58. $filtered_sql .= sql_escape($args[$arg]);
  59. else
  60. sql_error();
  61. }
  62. }
  63. else {
  64. // NULL
  65. if ((mb_substr($sql, $sqlpos - $arglength - 1, 1) == '\'') && (mb_substr($sql, $sqlpos + 1, 1) == '\'')) {
  66. // Anführungszeichen weg machen und NULL einsetzen
  67. $filtered_sql = mb_substr($filtered_sql, 0, mb_strlen($filtered_sql) - 1);
  68. $filtered_sql .= 'NULL';
  69. $sqlpos++;
  70. } else
  71. $filtered_sql .= 'NULL';
  72. }
  73. $sqlpos++;
  74. }
  75. else {
  76. $arglength = 0;
  77. $arg = '';
  78. // nächstes Zeichen das kein Buchstabe/Zahl ist herausfinden
  79. while (mb_ereg_match('^[a-zA-Z0-9]{1}', $nextchar) == 1) {
  80. $arg .= $nextchar;
  81. $arglength++;
  82. $nextchar = mb_substr($sql, $nextarg + $arglength + 1, 1);
  83. }
  84. // ok ... ersetzen
  85. $filtered_sql .= mb_substr($sql, $sqlpos, $nextarg - $sqlpos);
  86. if (isset($sql_replacements[$arg])) {
  87. $filtered_sql .= $sql_replacements[$arg];
  88. } else
  89. sql_error();
  90. $sqlpos = $nextarg + $arglength + 1;
  91. }
  92. }
  93. $nextarg = mb_strpos($sql, '&', $nextarg + 1);
  94. }
  95. // rest anhängen
  96. $filtered_sql .= mb_substr($sql, $sqlpos);
  97. // durch & ersetzen
  98. $nextarg = mb_strpos($filtered_sql, '\&');
  99. while ($nextarg !== false) {
  100. $escapesCount = 0;
  101. while ((($nextarg - $escapesCount - 1) > 0) && (mb_substr($filtered_sql, $nextarg - $escapesCount - 1, 1) == '\\'))
  102. $escapesCount++;
  103. if (($escapesCount % 2) == 0) {
  104. // \& ersetzen durch &
  105. $filtered_sql = mb_substr($filtered_sql, 0, $nextarg) . '&' . mb_substr($filtered_sql, $nextarg + 2);
  106. $nextarg--;
  107. }
  108. $nextarg = mb_strpos($filtered_sql, '\&', $nextarg + 2);
  109. }
  110. //
  111. // ok ... hier ist filtered_sql fertig
  112. //
  113. /* todo:
  114. - errorlogging
  115. - LIMIT
  116. - DROP/DELETE ggf. blocken
  117. */
  118. if (isset($sql_debug) && ($sql_debug == true)) {
  119. require_once($rootpath . 'lib/sqldebugger.inc.php');
  120. $result = sqldbg_execute($filtered_sql);
  121. if ($result === false)
  122. sql_error();
  123. }
  124. else {
  125. // Zeitmessung für die Ausführung
  126. // require_once($rootpath . 'lib/bench.inc.php');
  127. // $cSqlExecution = new Cbench;
  128. // $cSqlExecution->start();
  129. $result = mysql_query($filtered_sql, $dblink);
  130. if ($result === false)
  131. sql_error();
  132. // $cSqlExecution->stop();
  133. // if ($cSqlExecution->diff() > $sql_warntime)
  134. // sql_warn('execution took ' . $cSqlExecution->diff() . ' seconds');
  135. }
  136. return $result;
  137. }
  138. function sql_escape($value)
  139. {
  140. global $dblink;
  141. $value = mysql_real_escape_string($value, $dblink);
  142. $value = mb_ereg_replace('&', '\&', $value);
  143. return $value;
  144. }
  145. function sql_error()
  146. {
  147. if (class_exists('\okapi\Okapi')) {
  148. throw new Exception("SQL Error " . mysql_errno() . ": " . mysql_error());
  149. }
  150. global $sql_errormail;
  151. global $emailheaders;
  152. global $absolute_server_URI;
  153. global $interface_output;
  154. global $dberrormsg;
  155. // sendout email
  156. $email_content = mysql_errno() . ": " . mysql_error();
  157. $email_content .= "\n--------------------\n";
  158. $email_content .= print_r(debug_backtrace(), true);
  159. echo $sql_errormail . ' sql_error: ' . $absolute_server_URI . " " . $email_content;
  160. if ($interface_output == 'html') {
  161. // display errorpage
  162. tpl_errorMsg('sql_error', $dberrormsg);
  163. exit;
  164. } else if ($interface_output == 'plain') {
  165. echo "\n";
  166. echo 'sql_error' . "\n";
  167. echo '---------' . "\n";
  168. echo print_r(debug_backtrace(), true) . "\n";
  169. exit;
  170. }
  171. die('sql_error');
  172. }
  173. function sql_warn($warnmessage)
  174. {
  175. global $sql_errormail;
  176. global $emailheaders;
  177. global $absolute_server_URI;
  178. $email_content = $warnmessage;
  179. $email_content .= "\n--------------------\n";
  180. $email_content .= print_r(debug_backtrace(), true);
  181. //mb_send_mail($sql_errormail, 'sql_warn: ' . $absolute_server_URI, $email_content, $emailheaders);
  182. }
  183. /*
  184. Ersatz für die in Mysql eingebauten Funktionen
  185. */
  186. function sql_fetch_array($rs)
  187. {
  188. return mysql_fetch_array($rs);
  189. }
  190. function sql_fetch_assoc($rs)
  191. {
  192. return mysql_fetch_assoc($rs);
  193. }
  194. function sql_fetch_row($rs)
  195. {
  196. return mysql_fetch_row($rs);
  197. }
  198. function sql_free_result($rs)
  199. {
  200. return mysql_free_result($rs);
  201. }
  202. function mb_trim($str)
  203. {
  204. $bLoop = true;
  205. while ($bLoop == true) {
  206. $sPos = mb_substr($str, 0, 1);
  207. if ($sPos == ' ' || $sPos == "\r" || $sPos == "\n" || $sPos == "\t" || $sPos == "\x0B" || $sPos == "\0")
  208. $str = mb_substr($str, 1, mb_strlen($str) - 1);
  209. else
  210. $bLoop = false;
  211. }
  212. $bLoop = true;
  213. while ($bLoop == true) {
  214. $sPos = mb_substr($str, -1, 1);
  215. if ($sPos == ' ' || $sPos == "\r" || $sPos == "\n" || $sPos == "\t" || $sPos == "\x0B" || $sPos == "\0")
  216. $str = mb_substr($str, 0, mb_strlen($str) - 1);
  217. else
  218. $bLoop = false;
  219. }
  220. return $str;
  221. }
  222. /*
  223. //disconnect the databse
  224. function db_disconnect()
  225. {
  226. global $dbpconnect, $dblink;
  227. //is connected and no persistent connect used?
  228. if (($dbpconnect == false) && ($dblink !== false))
  229. {
  230. mysql_close($dblink);
  231. $dblink = false;
  232. }
  233. }
  234. //database handling
  235. function db_connect()
  236. {
  237. global $dblink, $dbpconnect, $dbusername, $dbname, $dbserver, $dbpasswd, $dbpconnect;
  238. //connect to the database by the given method - no php error reporting!
  239. if ($dbpconnect == true)
  240. {
  241. $dblink = @mysql_pconnect($dbserver, $dbusername, $dbpasswd);
  242. }
  243. else
  244. {
  245. $dblink = @mysql_connect($dbserver, $dbusername, $dbpasswd);
  246. }
  247. if ($dblink != false)
  248. {
  249. mysql_query("SET NAMES 'utf8'", $dblink);
  250. //database connection established ... set the used database
  251. if (@mysql_select_db($dbname, $dblink) == false)
  252. {
  253. //error while setting the database ... disconnect
  254. db_disconnect();
  255. $dblink = false;
  256. }
  257. }
  258. }
  259. */
  260. ?>