예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function doTask()
 {
     $config = ConfigProvider::getInstance();
     self::$logger = new Logger('BackupTask');
     self::$logger->setLogProviderFile($config->get('app.file.store.dir') . 'logs/tasks.log');
     if (!file_exists($config->get('backup.dir'))) {
         mkdir($config->get('backup.dir'));
     }
     $targetDir = $config->get('backup.dir') . date('Y-m-d') . '/';
     if (file_exists($targetDir)) {
         FileUtils::deleteDirectoryContents($targetDir);
     }
     if (!file_exists($targetDir)) {
         mkdir($targetDir);
     }
     $back = new BackupUtils();
     $back->backUpAttachmentsAndLogs($targetDir);
     $back->backUpDatabase($targetDir);
     $additionalDirectories = explode(',', $config->get('backup.include.dirs'));
     if (count($additionalDirectories) > 0) {
         foreach ($additionalDirectories as $additionalDirectory) {
             FileUtils::copy($additionalDirectory, $targetDir . basename($additionalDirectory));
         }
     }
     if ($config->get('backup.compress')) {
         FileUtils::zip($targetDir, $config->get('backup.dir') . date('Y-m-d') . '.zip');
         // we can safely remove the uncompressed files now to save space...
         FileUtils::deleteDirectoryContents($targetDir . 'logs');
         rmdir($targetDir . 'logs');
         FileUtils::deleteDirectoryContents($targetDir . 'attachments');
         rmdir($targetDir . 'attachments');
         unlink($targetDir . $config->get('db.name') . '_' . date('Y-m-d') . '.sql');
         if (count($additionalDirectories) > 0) {
             foreach ($additionalDirectories as $additionalDirectory) {
                 FileUtils::deleteDirectoryContents($targetDir . basename($additionalDirectory));
                 rmdir($targetDir . basename($additionalDirectory));
             }
         }
     }
 }
예제 #2
0
 /**
  * Method to handle PUT requests.
  *
  * @param Alpha\Util\Http\Request
  *
  * @return Alpha\Util\Http\Response
  *
  * @since 1.0
  */
 public function doPUT($request)
 {
     self::$logger->debug('>>doPUT($request=[' . var_export($request, true) . '])');
     $config = ConfigProvider::getInstance();
     $params = $request->getParams();
     try {
         // check the hidden security fields before accepting the form POST data
         if (!$this->checkSecurityFields()) {
             throw new SecurityException('This page cannot accept post data from remote servers!');
             self::$logger->debug('<<doPUT');
         }
         if (isset($params['markdownTextBoxRows']) && $params['markdownTextBoxRows'] != '') {
             $viewState = ViewState::getInstance();
             $viewState->set('markdownTextBoxRows', $params['markdownTextBoxRows']);
         }
         if (isset($params['title']) || isset($params['ActiveRecordOID'])) {
             if (isset($params['ActiveRecordType']) && class_exists($params['ActiveRecordType'])) {
                 $record = new $params['ActiveRecordType']();
             } else {
                 $record = new Article();
             }
             if (isset($params['title'])) {
                 $title = str_replace($config->get('cms.url.title.separator'), ' ', $params['title']);
                 $record->loadByAttribute('title', $title, false, array('OID', 'version_num', 'created_ts', 'updated_ts', 'title', 'author', 'published', 'content', 'headerContent'));
             } else {
                 $record->load($params['ActiveRecordOID']);
             }
             // uploading an article attachment
             if (isset($params['uploadBut'])) {
                 $source = $request->getFile('userfile')['tmp_name'];
                 $dest = $record->getAttachmentsLocation() . '/' . $request->getFile('userfile')['name'];
                 // upload the file to the attachments directory
                 FileUtils::copy($source, $dest);
                 if (!file_exists($dest)) {
                     throw new AlphaException('Could not move the uploaded file [' . $request->getFile('userfile')['name'] . ']');
                 }
                 // set read/write permissions on the file
                 $success = chmod($dest, 0666);
                 if (!$success) {
                     throw new AlphaException('Unable to set read/write permissions on the uploaded file [' . $dest . '].');
                 }
                 if ($success) {
                     self::$logger->action('File ' . $source . ' uploaded to ' . $dest);
                     $this->setStatusMessage(View::displayUpdateMessage('File ' . $source . ' uploaded to ' . $dest));
                 }
             } elseif (isset($params['deletefile']) && $params['deletefile'] != '') {
                 $success = unlink($record->getAttachmentsLocation() . '/' . $params['deletefile']);
                 if (!$success) {
                     throw new AlphaException('Could not delete the file [' . $params['deletefile'] . ']');
                 }
                 if ($success) {
                     self::$logger->action('File ' . $record->getAttachmentsLocation() . '/' . $params['deletefile'] . ' deleted');
                     $this->setStatusMessage(View::displayUpdateMessage('File ' . $record->getAttachmentsLocation() . '/' . $params['deletefile'] . ' deleted'));
                 }
             } else {
                 self::$logger->debug('<<doPUT');
                 return parent::doPUT($request);
             }
         } else {
             throw new IllegalArguementException('No valid article ID provided!');
         }
     } catch (SecurityException $e) {
         $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
         self::$logger->warn($e->getMessage());
     } catch (IllegalArguementException $e) {
         $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
         self::$logger->error($e->getMessage());
     } catch (RecordNotFoundException $e) {
         self::$logger->warn($e->getMessage());
         $this->setStatusMessage(View::displayErrorMessage('Failed to load the requested article from the database!'));
     } catch (AlphaException $e) {
         $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
         self::$logger->error($e->getMessage());
     }
     $response = new Response(301);
     if ($this->getNextJob() != '') {
         $response->redirect($this->getNextJob());
     } else {
         if ($this->request->isSecureURI()) {
             $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType=Alpha\\Model\\Article&ActiveRecordOID=' . $record->getOID() . '&view=edit'));
         } else {
             $title = str_replace(' ', $config->get('cms.url.title.separator'), $record->get('title'));
             $response->redirect($config->get('app.url') . '/a/' . $title . '/edit');
         }
     }
     self::$logger->debug('<<doPUT');
     return $response;
 }
예제 #3
0
 /**
  * Testing the copy() method.
  *
  * @since 2.0
  */
 public function testCopy()
 {
     FileUtils::copy('./public/images/logo-small.png', '/tmp/logo-small.png');
     $this->assertTrue(file_exists('/tmp/logo-small.png'), 'Testing the copy() method');
 }
예제 #4
0
 /**
  * Backs up the attachments and logs directories to the destination backup directory.
  *
  * @param string $backupDir
  *
  * @since 1.1
  */
 public static function backUpAttachmentsAndLogs($backupDir)
 {
     $config = ConfigProvider::getInstance();
     FileUtils::copy($config->get('app.file.store.dir') . 'attachments', $backupDir . 'attachments');
     FileUtils::copy($config->get('app.file.store.dir') . 'logs', $backupDir . 'logs');
 }