Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Bugfix and some minor improvements
Browse files Browse the repository at this point in the history
- Fixes an ugly bug in the BareosBSock class of the Bareos vendor module
- Updates the composer.phar
- Some indentation changes for readability
- A few RestoreController improvements
- Sets the header content-type info in the filebrowser view
  • Loading branch information
fbergkemper committed Aug 18, 2015
1 parent e07d500 commit 9e1f1ba
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 88 deletions.
Binary file modified composer.phar
Binary file not shown.
66 changes: 33 additions & 33 deletions module/Application/Module.php
Expand Up @@ -22,7 +22,7 @@ public function onBootstrap(MvcEvent $e)
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this->initSession($e);
$this->initSession($e);
}

public function getConfig()
Expand Down Expand Up @@ -55,10 +55,10 @@ public function initSession($e)
if(!isset($container->init)) {

$serviceManager = $e->getApplication()->getServiceManager();
$request = $serviceManager->get('Request');
$request = $serviceManager->get('Request');

$session->regenerateId(true);
$container->init = 1;
$container->init = 1;
$container->remoteAddr = $request->getServer()->get('REMOTE_ADDR');
$container->httpUserAgent = $request->getServer()->get('HTTP_USER_AGENT');
$container->username = "";
Expand All @@ -72,22 +72,22 @@ public function initSession($e)

$sessionConfig = $config['session'];

if (isset($sessionConfig['validators'])) {
$chain = $session->getValidatorChain();
foreach ($sessionConfig['validators'] as $validator) {
switch ($validator) {
case 'Zend\Session\Validator\HttpUserAgent':
$validator = new $validator($container->httpUserAgent);
break;
case 'Zend\Session\Validator\RemoteAddr':
$validator = new $validator($container->remoteAddr);
break;
default:
$validator = new $validator();
}
$chain->attach('session.validate', array($validator, 'isValid'));
}
}
if(isset($sessionConfig['validators'])) {
$chain = $session->getValidatorChain();
foreach($sessionConfig['validators'] as $validator) {
switch ($validator) {
case 'Zend\Session\Validator\HttpUserAgent':
$validator = new $validator($container->httpUserAgent);
break;
case 'Zend\Session\Validator\RemoteAddr':
$validator = new $validator($container->remoteAddr);
break;
default:
$validator = new $validator();
}
$chain->attach('session.validate', array($validator, 'isValid'));
}
}

}

Expand All @@ -109,33 +109,33 @@ public function getServiceConfig()
if(isset($session['config'])) {
$class = isset($session['config']['class']) ? $session['config']['class'] : 'Zend\Session\Config\SessionConfig';
$options = isset($session['config']['options']) ? $session['config']['options'] : array();
$sessionConfig = new $class();
$sessionConfig->setOptions($options);
$sessionConfig = new $class();
$sessionConfig->setOptions($options);
}

$sessionStorage = null;

if (isset($session['storage'])) {
$class = $session['storage'];
$sessionStorage = new $class();
}
$class = $session['storage'];
$sessionStorage = new $class();
}

$sessionSaveHandler = null;

if (isset($session['save_handler'])) {
// class should be fetched from service manager since it will require constructor arguments
$sessionSaveHandler = $sm->get($session['save_handler']);
}
// class should be fetched from service manager since it will require constructor arguments
$sessionSaveHandler = $sm->get($session['save_handler']);
}

$sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);

} else {
$sessionManager = new SessionManager();
}
} else {
$sessionManager = new SessionManager();
}

Container::setDefaultManager($sessionManager);
return $sessionManager;
}
Container::setDefaultManager($sessionManager);
return $sessionManager;
}
),
);
}
Expand Down
112 changes: 69 additions & 43 deletions module/Restore/src/Restore/Controller/RestoreController.php
Expand Up @@ -27,7 +27,7 @@

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\ViewModel\JsonModel;
use Zend\View\Model\JsonModel;
use Restore\Model\Restore;
use Restore\Form\RestoreForm;

Expand Down Expand Up @@ -140,11 +140,11 @@ public function indexAction()
public function filebrowserAction()
{
if($_SESSION['bareos']['authenticated'] == true) {
$result = new ViewModel(array(
$this->getRestoreParams();
$this->layout('layout/json');
return new ViewModel(array(
'items' => $this->buildSubtree()
));
$this->layout('layout/json');
return $result;
}
else{
return $this->redirect()->toRoute('auth', array('action' => 'login'));
Expand All @@ -161,18 +161,34 @@ private function buildSubtree()

// Get directories
if($this->restore_params['type'] == "client") {
$directories = $this->getDirectories($this->getJobIds($this->restore_params['jobid'], $this->restore_params['mergefilesets'], $this->restore_params['mergejobs']), $this->restore_params['id']);
$directories = $this->getDirectories(
$this->getJobIds($this->restore_params['jobid'],
$this->restore_params['mergefilesets'],
$this->restore_params['mergejobs']),
$this->restore_params['id']
);
}
else {
$directories = $this->getDirectories($this->restore_params['jobid'], $this->restore_params['id']);
$directories = $this->getDirectories(
$this->restore_params['jobid'],
$this->restore_params['id']
);
}

// Get files
if($this->restore_params['type'] == "client") {
$files = $this->getFiles($this->getJobIds($this->restore_params['jobid'], $this->restore_params['mergefilesets'], $this->restore_params['mergejobs']), $this->restore_params['id']);
$files = $this->getFiles(
$this->getJobIds($this->restore_params['jobid'],
$this->restore_params['mergefilesets'],
$this->restore_params['mergejobs']),
$this->restore_params['id']
);
}
else {
$files = $this->getFiles($this->restore_params['jobid'], $this->restore_params['id']);
$files = $this->getFiles(
$this->restore_params['jobid'],
$this->restore_params['id']
);
}

$dnum = count($directories);
Expand All @@ -182,47 +198,55 @@ private function buildSubtree()
// Build Json for JStree
$items = '[';

foreach($directories as $dir) {
if($dir['name'] == ".") {
--$dnum;
next($directories);
}
elseif($dir['name'] == "..") {
--$dnum;
next($directories);
}
else {
--$dnum;
$items .= '{';
$items .= '"id":"-' . $dir['pathid'] . '"';
$items .= ',"text":"' . $dir["name"] . '"';
$items .= ',"icon":"glyphicon glyphicon-folder-close"';
$items .= ',"state":""';
$items .= ',"data":' . \Zend\Json\Json::encode($dir, \Zend\Json\Json::TYPE_OBJECT);
$items .= ',"children":true';
$items .= '}';
if($dnum > 0) {
$items .= ",";
if($dnum > 0) {

foreach($directories as $dir) {
if($dir['name'] == ".") {
--$dnum;
next($directories);
}
elseif($dir['name'] == "..") {
--$dnum;
next($directories);
}
else {
--$dnum;
$items .= '{';
$items .= '"id":"-' . $dir['pathid'] . '"';
$items .= ',"text":"' . $dir["name"] . '"';
$items .= ',"icon":"glyphicon glyphicon-folder-close"';
$items .= ',"state":""';
$items .= ',"data":' . \Zend\Json\Json::encode($dir, \Zend\Json\Json::TYPE_OBJECT);
$items .= ',"children":true';
$items .= '}';
if($dnum > 0) {
$items .= ",";
}
}
}

}

if( $tmp > 2 && $fnum > 0 ) {
$items .= ",";
}

foreach($files as $file) {
$items .= '{';
$items .= '"id":"' . $file["fileid"] . '"';
$items .= ',"text":"' . $file["name"] . '"';
$items .= ',"icon":"glyphicon glyphicon-file"';
$items .= ',"state":""';
$items .= ',"data":' . \Zend\Json\Json::encode($file, \Zend\Json\Json::TYPE_OBJECT);
$items .= '}';
--$fnum;
if($fnum > 0) {
$items .= ",";
if($fnum > 0) {

foreach($files as $file) {
$items .= '{';
$items .= '"id":"' . $file["fileid"] . '"';
$items .= ',"text":"' . $file["name"] . '"';
$items .= ',"icon":"glyphicon glyphicon-file"';
$items .= ',"state":""';
$items .= ',"data":' . \Zend\Json\Json::encode($file, \Zend\Json\Json::TYPE_OBJECT);
$items .= '}';
--$fnum;
if($fnum > 0) {
$items .= ",";
}
}

}

$items .= ']';
Expand Down Expand Up @@ -437,11 +461,11 @@ private function getJobIds($jobid=null, $mergefilesets=0, $mergejobs=0)
$result = null;
$director = $this->getServiceLocator()->get('director');

if($mergefilesets == 0) {
if($mergefilesets == 0 && $mergejobs == 0) {
$cmd = ".bvfs_get_jobids jobid=$jobid all";
$result = $director->send_command($cmd, 2);
}
elseif($mergefilesets == 1) {
elseif($mergefilesets == 1 && $mergejobs == 0) {
$cmd = ".bvfs_get_jobids jobid=$jobid";
$result = $director->send_command($cmd, 2);
}
Expand All @@ -452,7 +476,8 @@ private function getJobIds($jobid=null, $mergefilesets=0, $mergejobs=0)

$jobids = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
$result = "";
$jnum = count($jobids['result']['jobids']);

$jnum = count($jobids['result']['jobids']);

foreach($jobids['result']['jobids'] as $jobid) {
$result .= $jobid['id'];
Expand All @@ -463,6 +488,7 @@ private function getJobIds($jobid=null, $mergefilesets=0, $mergejobs=0)
}

$this->restore_params['jobids'] = $result;

return $result;
}

Expand Down
1 change: 1 addition & 0 deletions module/Restore/view/restore/restore/filebrowser.phtml
@@ -1,3 +1,4 @@
<?php
header('Content-Type: application/json');
echo $this->items;
?>
33 changes: 21 additions & 12 deletions vendor/Bareos/library/Bareos/BSock/BareosBSock.php
Expand Up @@ -125,6 +125,11 @@ public function set_user_credentials($username=null, $password=null)
{
$this->config['console_name'] = $username;
$this->config['password'] = $password;

if($this->config['debug']) {
// extended debug: print config array
var_dump($this->config);
}
}

/**
Expand All @@ -146,7 +151,7 @@ public function set_config($config)

if($this->config['debug']) {
// extended debug: print config array
//var_dump($this->config);
var_dump($this->config);
}
}

Expand Down Expand Up @@ -273,17 +278,13 @@ private function receive_message()
if ($len == 0) {
break;
}
if ($len > 0 && $len < 8192) {
$msg .= fread($this->socket, $len);
} elseif($len > 8192) {
$rlen = 8192;
while ($len > 0) {
if ($len > 0) {
$rlen = 1024;
while (floor($len / $rlen) > 0) {
$msg .= fread($this->socket, $rlen);
$len -= $rlen;
if($len < $rlen) {
$rlen = $len;
}
}
$msg .= fread($this->socket, $len);
} elseif ($len < 0) {
// signal received
switch ($len) {
Expand Down Expand Up @@ -457,7 +458,14 @@ private function connect($username, $password)
$port = $this->config['port'];
$remote = "tcp://" . $this->config['host'] . ":" . $port;

$context = stream_context_create();
// set default stream context options
$opts = array(
'socket' => array(
'bindto' => '0:0',
),
);

$context = stream_context_create($opts);

/*
* It only makes sense to setup the whole TLS context when we as client support or
Expand Down Expand Up @@ -498,8 +506,9 @@ private function connect($username, $password)
}
}

$this->socket = stream_socket_client($remote, $error, $errstr, 60,
STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $context);
//$this->socket = stream_socket_client($remote, $error, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $context);
$this->socket = stream_socket_client($remote, $error, $errstr, 60, STREAM_CLIENT_CONNECT, $context);

if (!$this->socket) {
throw new \Exception("Error Connecting Socket: " . $errstr . "\n");
}
Expand Down

0 comments on commit 9e1f1ba

Please sign in to comment.