]> git.sur5r.net Git - bacula/bacula/commitdiff
baculum: Reorganize estimate job code
authorMarcin Haba <marcin.haba@bacula.pl>
Sun, 28 May 2017 04:47:57 +0000 (06:47 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Sun, 28 May 2017 18:58:33 +0000 (20:58 +0200)
gui/baculum/protected/API/Pages/API/JobEstimate.php
gui/baculum/protected/Common/Class/Miscellaneous.php

index 2fce4255fc61f74176a917d5b115b1b50c90c93a..5ffb976fc2611fa5268af9c368d426410485a5b1 100644 (file)
  
 class JobEstimate extends BaculumAPIServer {
 
-       public function get() {}
-
        public function create($params) {
-               $jobid = property_exists($params, 'id') ? intval($params->id) : 0;
-               $job = $jobid > 0 ? $this->getModule('job')->getJobById($jobid)->name : $params->name;
-               $level = $params->level;
-               $fileset = $params->fileset;
-               $clientid = intval($params->clientid);
-               $client = $this->getModule('client')->getClientById($clientid);
-               $accurateJob = intval($params->accurate);
-               $accurate = $accurateJob === 1 ? 'yes' : 'no';
+               $job = null;
+               if (property_exists($params, 'id')) {
+                       $jobid = intval($params->id);
+                       $job_row = $this->getModule('job')->getJobById($jobid);
+                       $job = is_object($job_row) ? $job_row->name : null;
+               } elseif (property_exists($params, 'name') && $this->getModule('misc')->isValidName($params->name)) {
+                       $job = $params->name;
+               }
+               $level = null;
+               if (property_exists($params, 'level')) {
+                       $level = $params->level;
+               }
+               $fileset = null;
+               if (property_exists($params, 'fileset') && $this->getModule('misc')->isValidName($params->fileset)) {
+                       $fileset = $params->fileset;
+               }
+               $client = null;
+               if (property_exists($params, 'clientid')) {
+                       $clientid = intval($params->clientid);
+                       $client_row = $this->getModule('client')->getClientById($clientid);
+                       $client = is_object($client_row) ? $client_row->name : null;
+               } elseif (property_exists($params, 'client') && $this->getModule('misc')->isValidName($params->client)) {
+                       $client = $params->client;
+               }
+               $accurate = null;
+               if (property_exists($params, 'accurate')) {
+                       $accurate_job = intval($params->accurate);
+                       $accurate = $accurate_job === 1 ? 'yes' : 'no';
+               }
 
-               if(!is_null($job)) {
-                       $isValidLevel = $this->getModule('misc')->isValidJobLevel($params->level);
-                       if($isValidLevel === true) {
-                               if(!is_null($fileset)) {
-                                       if(!is_null($client)) {
+               if (!is_null($job)) {
+                       $is_valid_level = $this->getModule('misc')->isValidJobLevel($params->level);
+                       if ($is_valid_level === true) {
+                               if (!is_null($fileset)) {
+                                       if (!is_null($client)) {
                                                $joblevels  = $this->getModule('misc')->getJobLevels();
-                                               $estimation = $this->getModule('bconsole')->bconsoleCommand($this->director, array('estimate', 'job="' . $job . '"', 'level="' . $joblevels[$level] . '"', 'fileset="' . $fileset. '"', 'client="' . $client->name . '"', 'accurate="' . $accurate . '"'), $this->user);
+                                               $cmd = array(
+                                                       'estimate',
+                                                       'job="' . $job . '"',
+                                                       'level="' . $joblevels[$level] . '"',
+                                                       'fileset="' . $fileset. '"',
+                                                       'client="' . $client . '"',
+                                                       'accurate="' . $accurate . '"'
+                                               );
+                                               $estimation = $this->getModule('bconsole')->bconsoleCommand($this->director, $cmd, $this->user);
                                                $this->output = $estimation->output;
                                                $this->error = (integer)$estimation->exitcode;
                                        } else {
@@ -62,4 +89,4 @@ class JobEstimate extends BaculumAPIServer {
        }
 }
 
-?>
\ No newline at end of file
+?>
index 34820677713af43bf109e7c4a146081a50f67fce..7051d175c833d3be734bd3909ce6eed0bbbb8946 100644 (file)
@@ -181,6 +181,10 @@ class Miscellaneous extends TModule {
                return array_key_exists($jobLevel, $this->getJobLevels());
        }
 
+       public function isValidName($name) {
+               return (preg_match('/^[\w:\.\-]{1,127}$/', $name) === 1);
+       }
+
        /**
         * Writing INI-style configuration file.
         *