PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/applications/dashboard/controllers/class.importcontroller.php

https://github.com/dkobia/Garden
PHP | 210 lines | 156 code | 32 blank | 22 comment | 30 complexity | fdc0ec35ca12bdce14482dbc383cd6c4 MD5 | raw file
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. class ImportController extends DashboardController {
  11. public function Export() {
  12. $this->Permission('Garden.Export'); // This permission doesn't exist, so only users with Admin == '1' will succeed.
  13. set_time_limit(60*2);
  14. $Ex = new ExportModel();
  15. $Ex->PDO(Gdn::Database()->Connection());
  16. $Ex->Prefix = Gdn::Database()->DatabasePrefix;
  17. /// 2. Do the export. ///
  18. $Ex->UseCompression = TRUE;
  19. $Ex->BeginExport(PATH_ROOT.DS.'uploads'.DS.'export '.date('Y-m-d His').'.txt.gz', 'Vanilla 2.0');
  20. $Ex->ExportTable('User', 'select * from :_User'); // ":_" will be replace by database prefix
  21. $Ex->ExportTable('Role', 'select * from :_Role');
  22. $Ex->ExportTable('UserRole', 'select * from :_UserRole');
  23. $Ex->ExportTable('Category', 'select * from :_Category');
  24. $Ex->ExportTable('Discussion', 'select * from :_Discussion');
  25. $Ex->ExportTable('Comment', 'select * from :_Comment');
  26. $Ex->ExportTable('Conversation', 'select * from :_Conversation');
  27. $Ex->ExportTable('UserConversation', 'select * from :_UserConversation');
  28. $Ex->ExportTable('ConversationMessage', 'select * from :_ConversationMessage');
  29. $Ex->EndExport();
  30. }
  31. public function Go() {
  32. $this->Permission('Garden.Import');
  33. $Imp = new ImportModel();
  34. $Imp->LoadState();
  35. $this->SetData('Steps', $Imp->Steps());
  36. $this->Form = new Gdn_Form();
  37. if($Imp->CurrentStep < 1) {
  38. // Check for the import file.
  39. if($Imp->ImportPath)
  40. $Imp->CurrentStep = 1;
  41. else
  42. Redirect(strtolower($this->Application).'/import');
  43. }
  44. if($Imp->CurrentStep >= 1) {
  45. if($this->Form->IsPostBack())
  46. $Imp->FromPost($this->Form->FormValues());
  47. try {
  48. $Result = $Imp->RunStep($Imp->CurrentStep);
  49. } catch(Exception $Ex) {
  50. $Result = FALSE;
  51. $this->Form->AddError($Ex);
  52. $this->SetJson('Error', TRUE);
  53. }
  54. if($Result === TRUE) {
  55. $Imp->CurrentStep++;
  56. } elseif($Result === 'COMPLETE') {
  57. $this->SetJson('Complete', TRUE);
  58. }
  59. /*elseif(is_array($Result)) {
  60. SaveToConfig(array(
  61. 'Garden.Import.CurrentStep' => $CurrentStep,
  62. 'Garden.Import.CurrentStepData' => ArrayValue('Data', $Result)));
  63. $this->SetData('CurrentStepMessage', ArrayValue('Message', $Result));
  64. }*/
  65. }
  66. $Imp->SaveState();
  67. $this->Form->SetValidationResults($Imp->Validation->Results());
  68. $this->SetData('Stats', GetValue('Stats', $Imp->Data, array()));
  69. $this->SetData('CurrentStep', $Imp->CurrentStep);
  70. $this->SetData('CurrentStepMessage', GetValue('CurrentStepMessage', $Imp->Data, ''));
  71. $this->SetData('ErrorType', GetValue('ErrorType', $Imp));
  72. if ($this->Data('ErrorType'))
  73. $this->SetJson('Error', TRUE);
  74. $Imp->ToPost($Post);
  75. $this->Form->FormValues($Post);
  76. $this->AddJsFile('import.js');
  77. $this->Render();
  78. }
  79. public function Index() {
  80. $this->Permission('Garden.Import'); // This permission doesn't exist, so only users with Admin == '1' will succeed.
  81. $Timer = new Gdn_Timer();
  82. // Determine the current step.
  83. $this->Form = new Gdn_Form();
  84. $Imp = new ImportModel();
  85. $Imp->LoadState();
  86. // Search for the list of acceptable imports.
  87. $ImportPaths = array();
  88. $ExistingPaths = SafeGlob(PATH_ROOT.'/uploads/export*', array('gz', 'txt'));
  89. foreach ($ExistingPaths as $Path)
  90. $ImportPaths[$Path] = basename($Path);
  91. if($Imp->CurrentStep < 1) {
  92. // Check to see if there is a file.
  93. $ImportPath = Gdn::Config('Garden.Import.ImportPath');
  94. $Validation = new Gdn_Validation();
  95. if (strcasecmp(Gdn::Request()->RequestMethod(), 'post') == 0) {
  96. $Upload = new Gdn_Upload();
  97. $Validation = new Gdn_Validation();
  98. if (count($ImportPaths) > 0)
  99. $Validation->ApplyRule('PathSelect', 'Required', T('You must select a file to import.'));
  100. if (count($ImportPaths) == 0 || $this->Form->GetFormValue('PathSelect') == 'NEW')
  101. $TmpFile = $Upload->ValidateUpload('ImportFile', FALSE);
  102. else
  103. $TmpFile = '';
  104. if ($TmpFile) {
  105. $Filename = $_FILES['ImportFile']['name'];
  106. $Extension = pathinfo($Filename, PATHINFO_EXTENSION);
  107. $TargetFolder = PATH_ROOT . DS . 'uploads' . DS . 'import';
  108. if(!file_exists($TargetFolder)) {
  109. mkdir($TargetFolder, 0777, TRUE);
  110. }
  111. $ImportPath = $Upload->GenerateTargetName(PATH_ROOT . DS . 'uploads' . DS . 'import', $Extension);
  112. $Upload->SaveAs($TmpFile, $ImportPath);
  113. $Imp->ImportPath = $ImportPath;
  114. $this->Form->SetFormValue('PathSelect', $ImportPath);
  115. $UploadedFiles = GetValue('UploadedFiles', $Imp->Data);
  116. $UploadedFiles[$ImportPath] = basename($Filename);
  117. $Imp->Data['UploadedFiles'] = $UploadedFiles;
  118. } elseif (($PathSelect = $this->Form->GetFormValue('PathSelect'))) {
  119. if ($PathSelect == 'NEW')
  120. $Validation->AddValidationResult('ImportFile', 'ValidateRequired');
  121. else
  122. $Imp->ImportPath = $PathSelect;
  123. } elseif (!$Imp->ImportPath && count($ImportPaths) == 0) {
  124. // There was no file uploaded this request or before.
  125. $Validation->AddValidationResult('ImportFile', $Upload->Exception);
  126. }
  127. // Validate the overwrite.
  128. if(TRUE || strcasecmp($this->Form->GetFormValue('Overwrite'), 'Overwrite') == 0) {
  129. $Validation->ApplyRule('Email', 'Required');
  130. $Validation->ApplyRule('Password', 'Required');
  131. }
  132. if($Validation->Validate($this->Form->FormValues())) {
  133. $this->Form->SetFormValue('Overwrite', 'overwrite');
  134. $Imp->FromPost($this->Form->FormValues());
  135. $this->View = 'Info';
  136. } else {
  137. $this->Form->SetValidationResults($Validation->Results());
  138. }
  139. } else {
  140. $this->Form->SetFormValue('PathSelect', $Imp->ImportPath);
  141. }
  142. $Imp->SaveState();
  143. } else {
  144. $this->SetData('Steps', $Imp->Steps());
  145. $this->View = 'Info';
  146. }
  147. if(!file_exists($Imp->ImportPath))
  148. $Imp->DeleteState();
  149. try {
  150. $UploadedFiles = GetValue('UploadedFiles', $Imp->Data, array());
  151. $ImportPaths = array_merge($ImportPaths, $UploadedFiles);
  152. $this->SetData('ImportPaths', $ImportPaths);
  153. $this->SetData('Header', $Imp->GetImportHeader());
  154. $this->SetData('Stats', GetValue('Stats', $Imp->Data, array()));
  155. $this->SetData('ImportPath', $Imp->ImportPath);
  156. $this->SetData('OriginalFilename', GetValue('OriginalFilename', $Imp->Data));
  157. $this->SetData('CurrentStep', $Imp->CurrentStep);
  158. $this->SetData('LoadSpeedWarning', $Imp->LoadTableType(FALSE) == 'LoadTableWithInsert');
  159. } catch(Gdn_UserException $Ex) {
  160. $this->Form->AddError($Ex);
  161. $Imp->SaveState();
  162. $this->View = 'Index';
  163. }
  164. $this->Render();
  165. }
  166. public function Restart() {
  167. $this->Permission('Garden.Import'); // This permission doesn't exist, so only users with Admin == '1' will succeed.
  168. // Delete the individual table files.
  169. $Imp = new ImportModel();
  170. try {
  171. $Imp->LoadState();
  172. $Imp->DeleteFiles();
  173. } catch(Exception $Ex) {
  174. }
  175. $Imp->DeleteState();
  176. Redirect(strtolower($this->Application).'/import');
  177. }
  178. }