PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/phpMyAdmin/libraries/tbl_select.lib.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 385 lines | 255 code | 37 blank | 93 comment | 84 complexity | 1f5ed8ac81e1337090f019805a8417ef MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functions for the table-search page and zoom-search page
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. require_once 'url_generating.lib.php';
  9. /**
  10. * Gets all the fields of a table along with their types, collations
  11. * and whether null or not.
  12. *
  13. * @param string $db Selected database
  14. * @param string $table Selected table
  15. *
  16. * @return array Array containing the field list, field types, collations
  17. * and null constraint
  18. */
  19. function PMA_tbl_getFields($db, $table)
  20. {
  21. // Gets the list and number of fields
  22. $fields = PMA_DBI_get_columns($db, $table, null, true);
  23. $fields_list = $fields_null = $fields_type = $fields_collation = array();
  24. $geom_column_present = false;
  25. $geom_types = PMA_getGISDatatypes();
  26. foreach ($fields as $key => $row) {
  27. $fields_list[] = $row['Field'];
  28. $type = $row['Type'];
  29. // check whether table contains geometric columns
  30. if (in_array($type, $geom_types)) {
  31. $geom_column_present = true;
  32. }
  33. // reformat mysql query output
  34. if (strncasecmp($type, 'set', 3) == 0
  35. || strncasecmp($type, 'enum', 4) == 0
  36. ) {
  37. $type = str_replace(',', ', ', $type);
  38. } else {
  39. // strip the "BINARY" attribute, except if we find "BINARY(" because
  40. // this would be a BINARY or VARBINARY field type
  41. if (!preg_match('@BINARY[\(]@i', $type)) {
  42. $type = preg_replace('@BINARY@i', '', $type);
  43. }
  44. $type = preg_replace('@ZEROFILL@i', '', $type);
  45. $type = preg_replace('@UNSIGNED@i', '', $type);
  46. $type = strtolower($type);
  47. }
  48. if (empty($type)) {
  49. $type = '&nbsp;';
  50. }
  51. $fields_null[] = $row['Null'];
  52. $fields_type[] = $type;
  53. $fields_collation[] = ! empty($row['Collation']) && $row['Collation'] != 'NULL'
  54. ? $row['Collation']
  55. : '';
  56. } // end while
  57. return array($fields_list, $fields_type, $fields_collation, $fields_null, $geom_column_present);
  58. }
  59. /**
  60. * Sets the table header for displaying a table in query-by-example format.
  61. *
  62. * @param bool $geom_column_present whether a geometry column is present
  63. *
  64. * @return HTML content, the tags and content for table header
  65. */
  66. function PMA_tbl_setTableHeader($geom_column_present = false)
  67. {
  68. // Display the Function column only if there is alteast one geomety colum
  69. $func = '';
  70. if ($geom_column_present) {
  71. $func = '<th>' . __('Function') . '</th>';
  72. }
  73. return '<thead>
  74. <tr>' . $func . '<th>' . __('Column') . '</th>
  75. <th>' . __('Type') . '</th>
  76. <th>' . __('Collation') . '</th>
  77. <th>' . __('Operator') . '</th>
  78. <th>' . __('Value') . '</th>
  79. </tr>
  80. </thead>';
  81. }
  82. /**
  83. * Returns an array with necessary configrations to create
  84. * sub-tabs(Table Search and Zoom Search) in the table_select page.
  85. *
  86. * @return array Array containing configuration (icon, text, link, id, args)
  87. * of sub-tabs for Table Search and Zoom search
  88. */
  89. function PMA_tbl_getSubTabs()
  90. {
  91. $subtabs = array();
  92. $subtabs['search']['icon'] = 'b_search.png';
  93. $subtabs['search']['text'] = __('Table Search');
  94. $subtabs['search']['link'] = 'tbl_select.php';
  95. $subtabs['search']['id'] = 'tbl_search_id';
  96. $subtabs['search']['args']['pos'] = 0;
  97. $subtabs['zoom']['icon'] = 'b_props.png';
  98. $subtabs['zoom']['link'] = 'tbl_zoom_select.php';
  99. $subtabs['zoom']['text'] = __('Zoom Search');
  100. $subtabs['zoom']['id'] = 'zoom_search_id';
  101. return $subtabs;
  102. }
  103. /**
  104. * Creates the HTML content for:
  105. * 1) Browsing foreign data for a field.
  106. * 2) Creating elements for search criteria input on fields.
  107. *
  108. * @param array $foreigners Array of foreign keys
  109. * @param array $foreignData Foreign keys data
  110. * @param string $field Column name
  111. * @param string $tbl_fields_type Column type
  112. * @param int $i Column index
  113. * @param string $db Selected database
  114. * @param string $table Selected table
  115. * @param array $titles Selected title
  116. * @param int $foreignMaxLimit Max limit of displaying foreign elements
  117. * @param array $fields Array of search criteria inputs
  118. * @param bool $in_fbs Whether we are in 'function based search'
  119. * @param bool $in_zoom_search_edit Whether we are in zoom search edit
  120. *
  121. * @return string HTML content for viewing foreing data and elements
  122. * for search criteria input.
  123. */
  124. function PMA_getForeignFields_Values($foreigners, $foreignData, $field, $tbl_fields_type, $i, $db, $table, $titles, $foreignMaxLimit, $fields, $in_fbs = false, $in_zoom_search_edit = false)
  125. {
  126. $str = '';
  127. if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
  128. // f o r e i g n k e y s
  129. $str .= '<select name="fields[' . $i . ']" id="fieldID_' . $i .'">' . "\n";
  130. // go back to first row
  131. // here, the 4th parameter is empty because there is no current
  132. // value of data for the dropdown (the search page initial values
  133. // are displayed empty)
  134. $str .= PMA_foreignDropdown(
  135. $foreignData['disp_row'], $foreignData['foreign_field'],
  136. $foreignData['foreign_display'], '', $foreignMaxLimit
  137. );
  138. $str .= '</select>' . "\n";
  139. } elseif ($foreignData['foreign_link'] == true) {
  140. if (isset($fields[$i]) && is_string($fields[$i])) {
  141. $str .= '<input type="text" id="fieldID_' . $i . '" name="fields[' . $i . ']" value="' . $fields[$i] . '" id="field_' . md5($field) . '[' . $i .']" class="textfield" />' ;
  142. } else {
  143. $str .= '<input type="text" id="fieldID_' . $i . '" name="fields[' . $i . ']" id="field_' . md5($field) . '[' . $i .']" class="textfield" />' ;
  144. }
  145. $str .= <<<EOT
  146. <a target="_blank" onclick="window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes'); return false" href="browse_foreigners.php?
  147. EOT;
  148. $str .= '' . PMA_generate_common_url($db, $table) . '&amp;field=' . urlencode($field) . '&amp;fieldkey=' . $i . '"';
  149. if ($in_zoom_search_edit) {
  150. $str .= ' class="browse_foreign"';
  151. }
  152. $str .= '>' . str_replace("'", "\'", $titles['Browse']) . '</a>';
  153. } elseif (in_array($tbl_fields_type[$i], PMA_getGISDatatypes())) {
  154. // g e o m e t r y
  155. $str .= '<input type="text" name="fields[' . $i . ']"'
  156. .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
  157. if ($in_fbs) {
  158. $edit_url = 'gis_data_editor.php?' . PMA_generate_common_url();
  159. $edit_str = PMA_getIcon('b_edit.png', __('Edit/Insert'));
  160. $str .= '<span class="open_search_gis_editor">';
  161. $str .= PMA_linkOrButton($edit_url, $edit_str, array(), false, false, '_blank');
  162. $str .= '</span>';
  163. }
  164. } elseif (strncasecmp($tbl_fields_type[$i], 'enum', 4) == 0
  165. || (strncasecmp($tbl_fields_type[$i], 'set', 3) == 0 && $in_zoom_search_edit)
  166. ) {
  167. // e n u m s a n d s e t s
  168. // Enum in edit mode --> dropdown
  169. // Enum in search mode --> multiselect
  170. // Set in edit mode --> multiselect
  171. // Set in search mode --> input (skipped here, so the 'else'
  172. // section would handle it)
  173. $value = explode(', ', str_replace("'", '', substr($tbl_fields_type[$i], 5, -1)));
  174. $cnt_value = count($value);
  175. if ((strncasecmp($tbl_fields_type[$i], 'enum', 4) && ! $in_zoom_search_edit)
  176. || (strncasecmp($tbl_fields_type[$i], 'set', 3) && $in_zoom_search_edit)
  177. ) {
  178. $str .= '<select name="fields[' . ($i) . '][]" id="fieldID_' . $i .'">' . "\n";
  179. } else {
  180. $str .= '<select name="fields[' . ($i) . '][]" id="fieldID_' . $i .'"'
  181. . ' multiple="multiple" size="' . min(3, $cnt_value) . '">' . "\n";
  182. }
  183. for ($j = 0; $j < $cnt_value; $j++) {
  184. if (isset($fields[$i])
  185. && is_array($fields[$i])
  186. && in_array($value[$j], $fields[$i])
  187. ) {
  188. $str .= '<option value="' . $value[$j] . '" Selected>'
  189. . $value[$j] . '</option>';
  190. } else {
  191. $str .= '<option value="' . $value[$j] . '">'
  192. . $value[$j] . '</option>';
  193. }
  194. } // end for
  195. $str .= '</select>' . "\n";
  196. } else {
  197. // o t h e r c a s e s
  198. $the_class = 'textfield';
  199. $type = $tbl_fields_type[$i];
  200. if ($type == 'date') {
  201. $the_class .= ' datefield';
  202. } elseif ($type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
  203. $the_class .= ' datetimefield';
  204. } elseif (substr($type, 0, 3) == 'bit') {
  205. $the_class .= ' bit';
  206. }
  207. if (isset($fields[$i]) && is_string($fields[$i])) {
  208. $str .= '<input type="text" name="fields[' . $i . ']"'
  209. .' size="40" class="' . $the_class . '" id="fieldID_'
  210. . $i .'" value = "' . $fields[$i] . '"/>' . "\n";
  211. } else {
  212. $str .= '<input type="text" name="fields[' . $i . ']"'
  213. .' size="40" class="' . $the_class . '" id="fieldID_'
  214. . $i .'" />' . "\n";
  215. }
  216. }
  217. return $str;
  218. }
  219. /**
  220. * Return the where clause for query generation based on the inputs provided.
  221. *
  222. * @param mixed $fields Search criteria input
  223. * @param string $names Name of the column on which search is submitted
  224. * @param string $types Type of the field
  225. * @param string $collations Field collation
  226. * @param string $func_type Search fucntion/operator
  227. * @param bool $unaryFlag Whether operator unary or not
  228. * @param bool $geom_func Whether geometry functions should be applied
  229. *
  230. * @return string HTML content for viewing foreing data and elements
  231. * for search criteria input.
  232. */
  233. function PMA_tbl_search_getWhereClause($fields, $names, $types, $collations, $func_type, $unaryFlag, $geom_func = null)
  234. {
  235. /**
  236. * @todo move this to a more apropriate place
  237. */
  238. $geom_unary_functions = array(
  239. 'IsEmpty' => 1,
  240. 'IsSimple' => 1,
  241. 'IsRing' => 1,
  242. 'IsClosed' => 1,
  243. );
  244. $w = '';
  245. // If geometry function is set apply it to the field name
  246. if ($geom_func != null && trim($geom_func) != '') {
  247. // Get details about the geometry fucntions
  248. $geom_funcs = PMA_getGISFunctions($types, true, false);
  249. // If the function takes a single parameter
  250. if ($geom_funcs[$geom_func]['params'] == 1) {
  251. $backquoted_name = $geom_func . '(' . PMA_backquote($names) . ')';
  252. } else {
  253. // If the function takes two parameters
  254. // create gis data from the string
  255. $gis_data = PMA_createGISData($fields);
  256. $w = $geom_func . '(' . PMA_backquote($names) . ',' . $gis_data . ')';
  257. return $w;
  258. }
  259. // New output type is the output type of the function being applied
  260. $types = $geom_funcs[$geom_func]['type'];
  261. // If the where clause is something like 'IsEmpty(`spatial_col_name`)'
  262. if (isset($geom_unary_functions[$geom_func]) && trim($fields) == '') {
  263. $w = $backquoted_name;
  264. return $w;
  265. }
  266. } else {
  267. $backquoted_name = PMA_backquote($names);
  268. }
  269. if ($unaryFlag) {
  270. $fields = '';
  271. $w = $backquoted_name . ' ' . $func_type;
  272. } elseif (in_array($types, PMA_getGISDatatypes()) && ! empty($fields)) {
  273. // create gis data from the string
  274. $gis_data = PMA_createGISData($fields);
  275. $w = $backquoted_name . ' ' . $func_type . ' ' . $gis_data;
  276. } elseif (strncasecmp($types, 'enum', 4) == 0) {
  277. if (!empty($fields)) {
  278. if (! is_array($fields)) {
  279. $fields = explode(',', $fields);
  280. }
  281. $enum_selected_count = count($fields);
  282. if ($func_type == '=' && $enum_selected_count > 1) {
  283. $func_type = 'IN';
  284. $parens_open = '(';
  285. $parens_close = ')';
  286. } elseif ($func_type == '!=' && $enum_selected_count > 1) {
  287. $func_type = 'NOT IN';
  288. $parens_open = '(';
  289. $parens_close = ')';
  290. } else {
  291. $parens_open = '';
  292. $parens_close = '';
  293. }
  294. $enum_where = '\'' . PMA_sqlAddslashes($fields[0]) . '\'';
  295. for ($e = 1; $e < $enum_selected_count; $e++) {
  296. $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$e]) . '\'';
  297. }
  298. $w = $backquoted_name . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
  299. }
  300. } elseif ($fields != '') {
  301. // For these types we quote the value. Even if it's another type (like INT),
  302. // for a LIKE we always quote the value. MySQL converts strings to numbers
  303. // and numbers to strings as necessary during the comparison
  304. if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types)
  305. || strpos(' ' . $func_type, 'LIKE')
  306. ) {
  307. $quot = '\'';
  308. } else {
  309. $quot = '';
  310. }
  311. // LIKE %...%
  312. if ($func_type == 'LIKE %...%') {
  313. $func_type = 'LIKE';
  314. $fields = '%' . $fields . '%';
  315. }
  316. if ($func_type == 'REGEXP ^...$') {
  317. $func_type = 'REGEXP';
  318. $fields = '^' . $fields . '$';
  319. }
  320. if ($func_type == 'IN (...)'
  321. || $func_type == 'NOT IN (...)'
  322. || $func_type == 'BETWEEN'
  323. || $func_type == 'NOT BETWEEN'
  324. ) {
  325. $func_type = str_replace(' (...)', '', $func_type);
  326. // quote values one by one
  327. $values = explode(',', $fields);
  328. foreach ($values as &$value) {
  329. $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
  330. }
  331. if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
  332. $w = $backquoted_name . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '')
  333. . ' AND ' . (isset($values[1]) ? $values[1] : '');
  334. } else {
  335. $w = $backquoted_name . ' ' . $func_type . ' (' . implode(',', $values) . ')';
  336. }
  337. } else {
  338. $w = $backquoted_name . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields) . $quot;;
  339. }
  340. } // end if
  341. return $w;
  342. }
  343. ?>