PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/docroot/pgadmin/dataimport.php

https://github.com/mterenzio/FollowThis
PHP | 298 lines | 231 code | 25 blank | 42 comment | 43 complexity | e5cd5727b029e340e58fba31f460e77e MD5 | raw file
  1. <?php
  2. /**
  3. * Does an import to a particular table from a text file
  4. *
  5. * $Id: dataimport.php,v 1.11 2007/01/22 16:33:01 soranzo Exp $
  6. */
  7. // Prevent timeouts on large exports (non-safe mode only)
  8. if (!ini_get('safe_mode')) set_time_limit(0);
  9. // Include application functions
  10. include_once('./libraries/lib.inc.php');
  11. // Default state for XML parser
  12. $state = 'XML';
  13. $curr_col_name = null;
  14. $curr_col_val = null;
  15. $curr_col_null = false;
  16. $curr_row = array();
  17. /**
  18. * Open tag handler for XML import feature
  19. */
  20. function _startElement($parser, $name, $attrs) {
  21. global $data, $misc, $lang;
  22. global $state, $curr_row, $curr_col_name, $curr_col_val, $curr_col_null;
  23. switch ($name) {
  24. case 'DATA':
  25. if ($state != 'XML') {
  26. $data->rollbackTransaction();
  27. $misc->printMsg($lang['strimporterror']);
  28. exit;
  29. }
  30. $state = 'DATA';
  31. break;
  32. case 'HEADER':
  33. if ($state != 'DATA') {
  34. $data->rollbackTransaction();
  35. $misc->printMsg($lang['strimporterror']);
  36. exit;
  37. }
  38. $state = 'HEADER';
  39. break;
  40. case 'RECORDS':
  41. if ($state != 'READ_HEADER') {
  42. $data->rollbackTransaction();
  43. $misc->printMsg($lang['strimporterror']);
  44. exit;
  45. }
  46. $state = 'RECORDS';
  47. break;
  48. case 'ROW':
  49. if ($state != 'RECORDS') {
  50. $data->rollbackTransaction();
  51. $misc->printMsg($lang['strimporterror']);
  52. exit;
  53. }
  54. $state = 'ROW';
  55. $curr_row = array();
  56. break;
  57. case 'COLUMN':
  58. // We handle columns in rows
  59. if ($state == 'ROW') {
  60. $state = 'COLUMN';
  61. $curr_col_name = $attrs['NAME'];
  62. $curr_col_null = isset($attrs['NULL']);
  63. }
  64. // And we ignore columns in headers and fail in any other context
  65. elseif ($state != 'HEADER') {
  66. $data->rollbackTransaction();
  67. $misc->printMsg($lang['strimporterror']);
  68. exit;
  69. }
  70. break;
  71. default:
  72. // An unrecognised tag means failure
  73. $data->rollbackTransaction();
  74. $misc->printMsg($lang['strimporterror']);
  75. exit;
  76. }
  77. }
  78. /**
  79. * Close tag handler for XML import feature
  80. */
  81. function _endElement($parser, $name) {
  82. global $data, $misc, $lang;
  83. global $state, $curr_row, $curr_col_name, $curr_col_val, $curr_col_null;
  84. switch ($name) {
  85. case 'DATA':
  86. $state = 'READ_DATA';
  87. break;
  88. case 'HEADER':
  89. $state = 'READ_HEADER';
  90. break;
  91. case 'RECORDS':
  92. $state = 'READ_RECORDS';
  93. break;
  94. case 'ROW':
  95. // Build value map in order to insert row into table
  96. $fields = array();
  97. $vars = array();
  98. $nulls = array();
  99. $format = array();
  100. $types = array();
  101. $i = 0;
  102. foreach ($curr_row as $k => $v) {
  103. $fields[$i] = $k;
  104. // Check for nulls
  105. if ($v === null) $nulls[$i] = 'on';
  106. // Add to value array
  107. $vars[$i] = $v;
  108. // Format is always VALUE
  109. $format[$i] = 'VALUE';
  110. // Type is always text
  111. $types[$i] = 'text';
  112. $i++;
  113. }
  114. $status = $data->insertRow($_REQUEST['table'], $fields, $vars, $nulls, $format, $types);
  115. if ($status != 0) {
  116. $data->rollbackTransaction();
  117. $misc->printMsg($lang['strimporterror']);
  118. exit;
  119. }
  120. $curr_row = array();
  121. $state = 'RECORDS';
  122. break;
  123. case 'COLUMN':
  124. $curr_row[$curr_col_name] = ($curr_col_null ? null : $curr_col_val);
  125. $curr_col_name = null;
  126. $curr_col_val = null;
  127. $curr_col_null = false;
  128. $state = 'ROW';
  129. break;
  130. default:
  131. // An unrecognised tag means failure
  132. $data->rollbackTransaction();
  133. $misc->printMsg($lang['strimporterror']);
  134. exit;
  135. }
  136. }
  137. /**
  138. * Character data handler for XML import feature
  139. */
  140. function _charHandler($parser, $cdata) {
  141. global $data, $misc, $lang;
  142. global $state, $curr_col_val;
  143. if ($state == 'COLUMN') {
  144. $curr_col_val .= $cdata;
  145. }
  146. }
  147. function loadNULLArray() {
  148. $array = array();
  149. if (isset($_POST['allowednulls'])) {
  150. foreach ($_POST['allowednulls'] as $null_char)
  151. $array[] = $null_char;
  152. }
  153. return $array;
  154. }
  155. function determineNull($field, $null_array) {
  156. return in_array($field, $null_array);
  157. }
  158. $misc->printHeader($lang['strimport']);
  159. $misc->printTrail('table');
  160. $misc->printTabs('table','import');
  161. // Check that file is specified and is an uploaded file
  162. if (isset($_FILES['source']) && is_uploaded_file($_FILES['source']['tmp_name']) && is_readable($_FILES['source']['tmp_name'])) {
  163. $fd = fopen($_FILES['source']['tmp_name'], 'r');
  164. // Check that file was opened successfully
  165. if ($fd !== false) {
  166. $null_array = loadNULLArray();
  167. $status = $data->beginTransaction();
  168. if ($status != 0) {
  169. $misc->printMsg($lang['strimporterror']);
  170. exit;
  171. }
  172. // If format is set to 'auto', then determine format automatically from file name
  173. if ($_REQUEST['format'] == 'auto') {
  174. $extension = substr(strrchr($_FILES['source']['name'], '.'), 1);
  175. switch ($extension) {
  176. case 'csv':
  177. $_REQUEST['format'] = 'csv';
  178. break;
  179. case 'txt':
  180. $_REQUEST['format'] = 'tab';
  181. break;
  182. case 'xml':
  183. $_REQUEST['format'] = 'xml';
  184. break;
  185. default:
  186. $data->rollbackTransaction();
  187. $misc->printMsg($lang['strimporterror-fileformat']);
  188. exit;
  189. }
  190. }
  191. // Do different import technique depending on file format
  192. switch ($_REQUEST['format']) {
  193. case 'csv':
  194. case 'tab':
  195. // XXX: Length of CSV lines limited to 100k
  196. $csv_max_line = 100000;
  197. // Set delimiter to tabs or commas
  198. if ($_REQUEST['format'] == 'csv') $csv_delimiter = ',';
  199. else $csv_delimiter = "\t";
  200. // Get first line of field names
  201. $fields = fgetcsv($fd, $csv_max_line, $csv_delimiter);
  202. $row = 2; //We start on the line AFTER the field names
  203. while ($line = fgetcsv($fd, $csv_max_line, $csv_delimiter)) {
  204. // Build value map
  205. $t_fields = array();
  206. $vars = array();
  207. $nulls = array();
  208. $format = array();
  209. $types = array();
  210. $i = 0;
  211. foreach ($fields as $f) {
  212. // Check that there is a column
  213. if (!isset($line[$i])) {
  214. $misc->printMsg(sprintf($lang['strimporterrorline-badcolumnnum'], $row));
  215. exit;
  216. }
  217. $t_fields[$i] = $f;
  218. // Check for nulls
  219. if (determineNull($line[$i], $null_array)) {
  220. $nulls[$i] = 'on';
  221. }
  222. // Add to value array
  223. $vars[$i] = $line[$i];
  224. // Format is always VALUE
  225. $format[$i] = 'VALUE';
  226. // Type is always text
  227. $types[$i] = 'text';
  228. $i++;
  229. }
  230. $status = $data->insertRow($_REQUEST['table'], $t_fields, $vars, $nulls, $format, $types);
  231. if ($status != 0) {
  232. $data->rollbackTransaction();
  233. $misc->printMsg(sprintf($lang['strimporterrorline'], $row));
  234. exit;
  235. }
  236. $row++;
  237. }
  238. break;
  239. case 'xml':
  240. $parser = xml_parser_create();
  241. xml_set_element_handler($parser, '_startElement', '_endElement');
  242. xml_set_character_data_handler($parser, '_charHandler');
  243. while (!feof($fd)) {
  244. $line = fgets($fd, 4096);
  245. xml_parse($parser, $line);
  246. }
  247. xml_parser_free($parser);
  248. break;
  249. default:
  250. // Unknown type
  251. $data->rollbackTransaction();
  252. $misc->printMsg($lang['strinvalidparam']);
  253. exit;
  254. }
  255. $status = $data->endTransaction();
  256. if ($status != 0) {
  257. $misc->printMsg($lang['strimporterror']);
  258. exit;
  259. }
  260. fclose($fd);
  261. $misc->printMsg($lang['strfileimported']);
  262. }
  263. else {
  264. // File could not be opened
  265. $misc->printMsg($lang['strimporterror']);
  266. }
  267. }
  268. else {
  269. // Upload went wrong
  270. $misc->printMsg($lang['strimporterror-uploadedfile']);
  271. }
  272. $misc->printFooter();
  273. ?>