Example #1
0
 /**
  * Restore database.
  */
 public function showRestore()
 {
     $this->showMainMenu();
     $tables = array();
     $selectedTables = array();
     $disabledTables = array();
     foreach (Curry_Propel::getModels() as $package => $classes) {
         $tables[$package] = array();
         foreach ($classes as $table) {
             $tables[$package][$table] = $table;
             if (method_exists($table, 'save')) {
                 $selectedTables[] = $table;
             } else {
                 $disabledTables[] = $table;
             }
         }
     }
     $files = array('' => '-- Select file --', 'upload' => '[ Upload from computer ]', 'remote' => '[ From remote server ]');
     $path = Curry_Backend_DatabaseHelper::createBackupName('*.txt');
     foreach (array_reverse(glob($path)) as $file) {
         $files[$file] = basename($file) . ' (' . Curry_Util::humanReadableBytes(filesize($file)) . ')';
     }
     $form = new Curry_Form(array('action' => url('', array("module", "view", "page_id")), 'method' => 'post', 'enctype' => 'multipart/form-data', 'elements' => array('tables' => array('multiselect', array('label' => 'Tables', 'multiOptions' => $tables, 'value' => $selectedTables, 'disable' => $disabledTables, 'size' => 15)), 'file' => array('select', array('label' => 'From file', 'multiOptions' => $files, 'class' => 'trigger-change', 'onchange' => "\$('#uploadfile-label').next().andSelf()[this.value == 'upload'?'show':'hide']();" . "\$('#remote-label').next().andSelf()[this.value == 'remote'?'show':'hide']();")), 'uploadfile' => array('file', array('label' => 'Upload file', 'valueDisabled' => true)), 'remote' => array('text', array('label' => 'Remote')), 'max_execution_time' => array('text', array('label' => 'Max execution time', 'value' => '', 'description' => 'Input time in seconds to allow interruption if the time taken to restore would exceed the maximum execution time.')))));
     $form->addElement('submit', 'Go');
     if (isPost() && $form->isValid($_POST)) {
         if ($form->file->getValue() == 'upload') {
             if (!$form->uploadfile->isUploaded()) {
                 throw new Exception('No file was uploaded.');
             }
             $fileinfo = $form->uploadfile->getFileInfo();
             Curry_Backend_DatabaseHelper::restoreFromFile($fileinfo['uploadfile']['tmp_name'], $form->tables->getValue(), 0, 0, $this);
         } else {
             if ($form->file->getValue() == 'remote') {
                 if (!$form->remote->getValue()) {
                     throw new Exception('No remote URL set');
                 }
                 $url = url($form->remote->getValue());
                 $post = array('login_username' => $url->getUser(), 'login_password' => $url->getPassword(), 'tables' => '*', 'name' => 'db.txt', 'type' => 'local');
                 $url->setUser(null)->setPassword(null)->setPath('/admin.php')->add(array('module' => 'Curry_Backend_Database', 'view' => 'Backup'));
                 $context = stream_context_create(array('http' => array('method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'content' => http_build_query($post))));
                 $fp = fopen($url, 'r', false, $context);
                 Curry_Backend_DatabaseHelper::restoreFromFile($fp, $form->tables->getValue(), 0, 0, $this);
                 fclose($fp);
             } else {
                 if ($form->file->getValue()) {
                     Curry_Backend_DatabaseHelper::restoreFromFile($form->file->getValue(), $form->tables->getValue(), (int) $form->max_execution_time->getValue(), 0, $this);
                 }
             }
         }
     }
     $this->addMainContent($form);
 }
Example #2
0
 /**
  * Create an automatic backup of the database.
  */
 public function doAutoBackup()
 {
     $autoBackup = Curry_Core::$config->curry->autoBackup;
     if ($autoBackup) {
         $filename = Curry_Backend_DatabaseHelper::createBackupName("backup_%Y-%m-%d_%H-%M-%S_autobackup.txt");
         $lastModified = 0;
         foreach (new DirectoryIterator(dirname($filename)) as $entry) {
             if ($entry->isFile()) {
                 $lastModified = max($lastModified, $entry->getMTime());
             }
         }
         if (time() - $lastModified >= $autoBackup && !file_exists($filename)) {
             $status = Curry_Backend_DatabaseHelper::dumpDatabase($filename);
             if ($this->backend) {
                 if ($status) {
                     $this->backend->addMessage('An automatic backup of the database has been created successfully.', Curry_Backend::MSG_SUCCESS);
                 } else {
                     $this->backend->addMessage('There was an error when trying to create the automatic backup of the database.', Curry_Backend::MSG_ERROR);
                 }
             }
         }
     }
 }