1<?php
2
3/**
4 *
5 * bareos-webui - Bareos Web-Frontend
6 *
7 * @link      https://github.com/bareos/bareos for the canonical source repository
8 * @copyright Copyright (c) 2013-2017 Bareos GmbH & Co. KG (http://www.bareos.org/)
9 * @license   GNU Affero General Public License (http://www.gnu.org/licenses/)
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26namespace Pool\Model;
27
28class PoolModel
29{
30
31   /**
32    * Get all Pools by llist command
33    *
34    * @param $bsock
35    *
36    * @return array
37    */
38   public function getPools(&$bsock=null)
39   {
40      if(isset($bsock)) {
41         $cmd = 'llist pools';
42         $result = $bsock->send_command($cmd, 2);
43         $pools = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
44         return $pools['result']['pools'];
45      }
46      else {
47         throw new \Exception('Missing argument.');
48      }
49   }
50
51   /**
52    * Get all Pools by .pools command
53    *
54    * @param $bsock
55    * @param $type
56    *
57    * @return array
58    */
59   public function getDotPools(&$bsock=null, $type=null)
60   {
61      if(isset($bsock)) {
62         if($type == null) {
63            $cmd = '.pools';
64         }
65         else {
66            $cmd = '.pools type="'.$type.'"';
67         }
68         $pools = $bsock->send_command($cmd, 2);
69         $result = \Zend\Json\Json::decode($pools, \Zend\Json\Json::TYPE_ARRAY);
70         return $result['result']['pools'];
71      }
72      else {
73         throw new \Exception('Missing argument.');
74      }
75   }
76
77   /**
78    * Get a single Pool
79    *
80    * @param $bsock
81    * @param $pool
82    *
83    * @return array
84    */
85   public function getPool(&$bsock=null, $pool=null)
86   {
87      if(isset($bsock, $pool)) {
88         $cmd = 'llist pool="'.$pool.'"';
89         $result = $bsock->send_command($cmd, 2);
90         $pool = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
91         return $pool['result']['pools'];
92      }
93      else {
94         throw new \Exception('Missing argument.');
95      }
96   }
97
98   /**
99    * Get Pool Media by llist media command
100    *
101    * @param $bsock
102    * @param $pool
103    *
104    * @return array
105    */
106   public function getPoolMedia(&$bsock=null, $pool=null)
107   {
108      if(isset($bsock, $pool)) {
109         $cmd = 'llist media pool="'.$pool.'"';
110         $limit = 1000;
111         $offset = 0;
112         $retval = array();
113         while (true) {
114            $result = $bsock->send_command($cmd . ' limit=' . $limit . ' offset=' . $offset, 2);
115            if (preg_match('/Failed to send result as json. Maybe result message to long?/', $result)) {
116               $error = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
117               return $error['result']['error'];
118            } else {
119               $media = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
120               if ( empty($media['result']) ) {
121                  return false; // No matching records found
122               }
123               if ( empty($media['result']['volumes']) && $media['result']['meta']['range']['filtered'] === 0 ) {
124                  return $retval;
125               } else {
126                  $retval = array_merge($retval, $media['result']['volumes']);
127               }
128            }
129            $offset = $offset + $limit;
130         }
131      } else {
132         throw new \Exception('Missing argument.');
133      }
134   }
135}
136