PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/adodb/drivers/adodb-mssql_n.inc.php

https://github.com/mackensen/moodle
PHP | 226 lines | 119 code | 33 blank | 74 comment | 22 complexity | baaff14252b7344a017b8976e026cabb MD5 | raw file
  1. <?php
  2. /**
  3. * MSSQL Driver with auto-prepended "N" for correct unicode storage of SQL literal strings.
  4. *
  5. * Intended to be used with MSSQL drivers that are sending UCS-2 data to MSSQL
  6. * (FreeTDS and ODBTP) in order to get true cross-db compatibility from the
  7. * application point of view.
  8. *
  9. * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
  10. *
  11. * @package ADOdb
  12. * @link https://adodb.org Project's web site and documentation
  13. * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  14. *
  15. * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  16. * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  17. * any later version. This means you can use it in proprietary products.
  18. * See the LICENSE.md file distributed with this source code for details.
  19. * @license BSD-3-Clause
  20. * @license LGPL-2.1-or-later
  21. *
  22. * @copyright 2000-2013 John Lim
  23. * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  24. */
  25. // security - hide paths
  26. if (!defined('ADODB_DIR')) die();
  27. // one useful constant
  28. if (!defined('SINGLEQUOTE')) define('SINGLEQUOTE', "'");
  29. include_once(ADODB_DIR.'/drivers/adodb-mssql.inc.php');
  30. class ADODB_mssql_n extends ADODB_mssql {
  31. var $databaseType = "mssql_n";
  32. function _query($sql,$inputarr=false)
  33. {
  34. $sql = $this->_appendN($sql);
  35. return ADODB_mssql::_query($sql,$inputarr);
  36. }
  37. /**
  38. * This function will intercept all the literals used in the SQL, prepending the "N" char to them
  39. * in order to allow mssql to store properly data sent in the correct UCS-2 encoding (by freeTDS
  40. * and ODBTP) keeping SQL compatibility at ADOdb level (instead of hacking every project to add
  41. * the "N" notation when working against MSSQL.
  42. *
  43. * The original note indicated that this hack should only be used if ALL the char-based columns
  44. * in your DB are of type nchar, nvarchar and ntext, but testing seems to indicate that SQL server
  45. * doesn't seem to care if the statement is used against char etc fields.
  46. *
  47. * @todo This function should raise an ADOdb error if one of the transformations fail
  48. *
  49. * @param mixed $inboundData Either a string containing an SQL statement
  50. * or an array with resources from prepared statements
  51. *
  52. * @return mixed
  53. */
  54. function _appendN($inboundData) {
  55. $inboundIsArray = false;
  56. if (is_array($inboundData))
  57. {
  58. $inboundIsArray = true;
  59. $inboundArray = $inboundData;
  60. } else
  61. $inboundArray = (array)$inboundData;
  62. /*
  63. * All changes will be placed here
  64. */
  65. $outboundArray = $inboundArray;
  66. foreach($inboundArray as $inboundKey=>$inboundValue)
  67. {
  68. if (is_resource($inboundValue))
  69. {
  70. /*
  71. * Prepared statement resource
  72. */
  73. if ($this->debug)
  74. ADOConnection::outp("{$this->databaseType} index $inboundKey value is resource, continue");
  75. continue;
  76. }
  77. if (strpos($inboundValue, SINGLEQUOTE) === false)
  78. {
  79. /*
  80. * Check we have something to manipulate
  81. */
  82. if ($this->debug)
  83. ADOConnection::outp("{$this->databaseType} index $inboundKey value $inboundValue has no single quotes, continue");
  84. continue;
  85. }
  86. /*
  87. * Check we haven't an odd number of single quotes (this can cause problems below
  88. * and should be considered one wrong SQL). Exit with debug info.
  89. */
  90. if ((substr_count($inboundValue, SINGLEQUOTE) & 1))
  91. {
  92. if ($this->debug)
  93. ADOConnection::outp("{$this->databaseType} internal transformation: not converted. Wrong number of quotes (odd)");
  94. break;
  95. }
  96. /*
  97. * Check we haven't any backslash + single quote combination. It should mean wrong
  98. * backslashes use (bad magic_quotes_sybase?). Exit with debug info.
  99. */
  100. $regexp = '/(\\\\' . SINGLEQUOTE . '[^' . SINGLEQUOTE . '])/';
  101. if (preg_match($regexp, $inboundValue))
  102. {
  103. if ($this->debug)
  104. ADOConnection::outp("{$this->databaseType} internal transformation: not converted. Found bad use of backslash + single quote");
  105. break;
  106. }
  107. /*
  108. * Remove pairs of single-quotes
  109. */
  110. $pairs = array();
  111. $regexp = '/(' . SINGLEQUOTE . SINGLEQUOTE . ')/';
  112. preg_match_all($regexp, $inboundValue, $list_of_pairs);
  113. if ($list_of_pairs)
  114. {
  115. foreach (array_unique($list_of_pairs[0]) as $key=>$value)
  116. $pairs['<@#@#@PAIR-'.$key.'@#@#@>'] = $value;
  117. if (!empty($pairs))
  118. $inboundValue = str_replace($pairs, array_keys($pairs), $inboundValue);
  119. }
  120. /*
  121. * Remove the rest of literals present in the query
  122. */
  123. $literals = array();
  124. $regexp = '/(N?' . SINGLEQUOTE . '.*?' . SINGLEQUOTE . ')/is';
  125. preg_match_all($regexp, $inboundValue, $list_of_literals);
  126. if ($list_of_literals)
  127. {
  128. foreach (array_unique($list_of_literals[0]) as $key=>$value)
  129. $literals['<#@#@#LITERAL-'.$key.'#@#@#>'] = $value;
  130. if (!empty($literals))
  131. $inboundValue = str_replace($literals, array_keys($literals), $inboundValue);
  132. }
  133. /*
  134. * Analyse literals to prepend the N char to them if their contents aren't numeric
  135. */
  136. if (!empty($literals))
  137. {
  138. foreach ($literals as $key=>$value) {
  139. if (!is_numeric(trim($value, SINGLEQUOTE)))
  140. /*
  141. * Non numeric string, prepend our dear N, whilst
  142. * Trimming potentially existing previous "N"
  143. */
  144. $literals[$key] = 'N' . trim($value, 'N');
  145. }
  146. }
  147. /*
  148. * Re-apply literals to the text
  149. */
  150. if (!empty($literals))
  151. $inboundValue = str_replace(array_keys($literals), $literals, $inboundValue);
  152. /*
  153. * Any pairs followed by N' must be switched to N' followed by those pairs
  154. * (or strings beginning with single quotes will fail)
  155. */
  156. $inboundValue = preg_replace("/((<@#@#@PAIR-(\d+)@#@#@>)+)N'/", "N'$1", $inboundValue);
  157. /*
  158. * Re-apply pairs of single-quotes to the text
  159. */
  160. if (!empty($pairs))
  161. $inboundValue = str_replace(array_keys($pairs), $pairs, $inboundValue);
  162. /*
  163. * Print transformation if debug = on
  164. */
  165. if (strcmp($inboundValue,$inboundArray[$inboundKey]) <> 0 && $this->debug)
  166. ADOConnection::outp("{$this->databaseType} internal transformation: {$inboundArray[$inboundKey]} to {$inboundValue}");
  167. if (strcmp($inboundValue,$inboundArray[$inboundKey]) <> 0)
  168. /*
  169. * Place the transformed value into the outbound array
  170. */
  171. $outboundArray[$inboundKey] = $inboundValue;
  172. }
  173. /*
  174. * Any transformations are in the $outboundArray
  175. */
  176. if ($inboundIsArray)
  177. return $outboundArray;
  178. /*
  179. * We passed a string in originally
  180. */
  181. return $outboundArray[0];
  182. }
  183. }
  184. class ADORecordset_mssql_n extends ADORecordset_mssql {
  185. var $databaseType = "mssql_n";
  186. }