PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/admin/backup.import.php

https://github.com/cyrke/phpMyFAQ
PHP | 140 lines | 112 code | 10 blank | 18 comment | 28 complexity | 12815538325366d8849c2eca759bcb80 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * The import function to import the phpMyFAQ backups
  4. *
  5. * PHP Version 5.3
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package Administration
  13. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  14. * @copyright 2003-2012 phpMyFAQ Team
  15. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  16. * @link http://www.phpmyfaq.de
  17. * @since 2003-02-24
  18. */
  19. if (!defined('IS_VALID_PHPMYFAQ')) {
  20. header('Location: http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']));
  21. exit();
  22. }
  23. $csrfToken = PMF_Filter::filterInput(INPUT_POST, 'csrf', FILTER_SANITIZE_STRING);
  24. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  25. $permission['restore'] = false;
  26. }
  27. if ($permission['restore']) {
  28. printf("<header><h2>%s</h2></header>\n", $PMF_LANG['ad_csv_rest']);
  29. if (isset($_FILES['userfile']) && 0 == $_FILES['userfile']['error']) {
  30. $ok = 1;
  31. $finfo = new finfo(FILEINFO_MIME_ENCODING);
  32. if ('utf-8' == $finfo->file($_FILES['userfile']['tmp_name'])) {
  33. print 'This file is not UTF_8 encoded.';
  34. $ok = 0;
  35. }
  36. $handle = fopen($_FILES['userfile']['tmp_name'], 'r');
  37. $dat = fgets($handle, 65536);
  38. $versionFound = PMF_String::substr($dat, 0, 9);
  39. $versionExpected = '-- pmf' . substr($faqConfig->get('main.currentVersion'), 0, 3);
  40. if ($versionFound != $versionExpected) {
  41. printf('%s (Version check failure: "%s" found, "%s" expected)',
  42. $PMF_LANG['ad_csv_no'],
  43. $versionFound,
  44. $versionExpected
  45. );
  46. $ok = 0;
  47. } else {
  48. // @todo: Start transaction for better recovery if something really bad happens
  49. $dat = trim(PMF_String::substr($dat, 11));
  50. $tbl = explode(' ', $dat);
  51. $num = count($tbl);
  52. for ($h = 0; $h < $num; $h++) {
  53. $mquery[] = 'DELETE FROM '.$tbl[$h];
  54. }
  55. $ok = 1;
  56. }
  57. if ($ok == 1) {
  58. $table_prefix = '';
  59. printf("<p>%s</p>\n", $PMF_LANG['ad_csv_prepare']);
  60. while ($dat = fgets($handle, 65536)) {
  61. $dat = trim($dat);
  62. $backup_prefix_pattern = "-- pmftableprefix:";
  63. $backup_prefix_pattern_len = PMF_String::strlen($backup_prefix_pattern);
  64. if (PMF_String::substr($dat, 0, $backup_prefix_pattern_len) == $backup_prefix_pattern) {
  65. $table_prefix = trim(PMF_String::substr($dat, $backup_prefix_pattern_len));
  66. }
  67. if ( (PMF_String::substr($dat, 0, 2) != '--') && ($dat != '') ) {
  68. $mquery[] = trim(PMF_String::substr($dat, 0, -1));
  69. }
  70. }
  71. $k = 0;
  72. $g = 0;
  73. printf("<p>%s</p>\n", $PMF_LANG['ad_csv_process']);
  74. $num = count($mquery);
  75. $kg = '';
  76. for ($i = 0; $i < $num; $i++) {
  77. $mquery[$i] = PMF_DB_Helper::alignTablePrefix($mquery[$i], $table_prefix, PMF_Db::getTablePrefix());
  78. $kg = $faqConfig->getDb()->query($mquery[$i]);
  79. if (!$kg) {
  80. printf('<div style="alert alert-error"><strong>Query</strong>: "%s" failed (Reason: %s)</div>%s',
  81. PMF_String::htmlspecialchars($mquery[$i], ENT_QUOTES, 'utf-8'),
  82. $faqConfig->getDb()->error(),
  83. "\n");
  84. $k++;
  85. } else {
  86. printf('<!-- <div style="alert alert-success"><strong>Query</strong>: "%s" okay</div> -->%s',
  87. PMF_String::htmlspecialchars($mquery[$i], ENT_QUOTES, 'utf-8'),
  88. "\n");
  89. $g++;
  90. }
  91. }
  92. printf('<p class="alert alert-success">%d %s %d %s</p>',
  93. $g,
  94. $PMF_LANG['ad_csv_of'],
  95. $num,
  96. $PMF_LANG['ad_csv_suc']
  97. );
  98. }
  99. } else {
  100. switch ($_FILES['userfile']['error']) {
  101. case 1:
  102. $errorMessage = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
  103. break;
  104. case 2:
  105. $errorMessage = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
  106. break;
  107. case 3:
  108. $errorMessage = 'The uploaded file was only partially uploaded.';
  109. break;
  110. case 4:
  111. $errorMessage = 'No file was uploaded.';
  112. break;
  113. case 6:
  114. $errorMessage = 'Missing a temporary folder.';
  115. break;
  116. case 7:
  117. $errorMessage = 'Failed to write file to disk.';
  118. break;
  119. case 8:
  120. $errorMessage = 'A PHP extension stopped the file upload.';
  121. break;
  122. default:
  123. $errorMessage = 'Undefined error.';
  124. break;
  125. }
  126. printf('<p class="alert alert-error">%s (%s)</p>', $PMF_LANG['ad_csv_no'], $errorMessage);
  127. }
  128. } else {
  129. print $PMF_LANG['err_NotAuth'];
  130. }