summaryrefslogtreecommitdiffstats
path: root/application/modules/user/controllers/BootisoController.php
blob: cec9e8ab3e0f2c72dd5bd9e8e84fd1742138c7e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
* This program is free software distributed under the GPL version 2.
* See http://gpl.openslx.org/
*
* If you have any feedback please consult http://feedback.openslx.org/ and
* send your suggestions, praise, or complaints to feedback@openslx.org
*
* General information about OpenSLX can be found at http://openslx.org/
*/

class user_BootisoController extends Zend_Controller_Action {

  protected $bootisoMapper;
  protected $membershipMapper;
  protected $membership;
  protected $page;

  public function init() {
    if (Zend_Auth::getInstance()->hasIdentity()) {
      $userIDsNamespace = Zend_Session::namespaceGet('userIDs');
      if($userIDsNamespace['membershipID'] == '') {
        $this->_redirect('/user/index');
      }

      $this->bootisoMapper = new Application_Model_BootIsoMapper();

      $this->membershipMapper = new Application_Model_MembershipMapper();
      $this->membership = new Application_Model_Membership();
      $this->membershipMapper->find($userIDsNamespace['membershipID'], $this->membership);

      $this->db = Zend_Db_Table::getDefaultAdapter();
    } else {
      $this->_helper->redirector('login', 'auth');
    }
    $this->page = $this->_request->getParam('page');
  }

  public function indexAction() {
    $result = $this->_request->getParam('addresult');
    if($result != "") {
      
      $this->view->notification = Pbs_Notifier::notify('create', $result);
    }
    $result = $this->_request->getParam('deleteresult');
    if($result != "") {
      
      $this->view->notification = Pbs_Notifier::notify('delete', $result);
    }
    $result = $this->_request->getParam('modifyresult');
    if($result != "") {
      
      $this->view->notification = Pbs_Notifier::notify('modify', $result);
    }
    $result = $this->_request->getParam('downloadresult');
    if($result != "") {
      
      $this->view->notification = Pbs_Notifier::notify('download', $result);
    }


    $groupMapper = new Application_Model_GroupMapper();

    $groupID = $this->membership->getGroupID();

    //ACL Darf er BootISOMenu sehen?
    if(!Pbs_Acl::checkRight('bai') && !Pbs_Acl::checkRight('bui'))
      { $this->_redirect('/user/index'); }

    $groupgroupsMapper = new Application_Model_GroupGroupsMapper();

    $parents = array();
    $this->view->bootisolist = array();
    $parents = $groupgroupsMapper->getParentGroups($groupID);

    foreach($parents as $k => $parent) {
      foreach($parent as  $p) {
        $bootiso = $this->bootisoMapper->findBy(array("groupID" => $p));
        foreach ($bootiso as $b)
        if($b->getPublic() - $k >= 0 )
          { $this->view->bootisolist[] = $b; }
        elseif($k == 0 && $b->getPublic() == -1 && Pbs_Acl::checkRight('bc'))
        $this->view->bootisolist[] = $b;
      }
    }

    $this->view->bootisolist = array_reverse($this->view->bootisolist);

    $preboottypeMapper = new Application_Model_PreBootTypeMapper();
    
    foreach ($this->view->bootisolist as $bootiso) {
      $bootiso->setGroupID("[".$bootiso->getGroupID()."] ".$groupMapper->find($bootiso->getGroupID())->getTitle());
      $bootiso->setPreBootTypeID($preboottypeMapper->find($bootiso->getPreBootTypeID())->getName());
      $bootiso->setCreated(date(Zend_Registry::get('dateformat'), $bootiso->getCreated()));
      if($bootiso->getExpires() == "") {
        $bootiso->setExpires(0);
      }
      $bootiso->setExpires(date(Zend_Registry::get('dateformat'), $bootiso->getExpires()));
    }

    // Search
    $search = $this->_request->getParam('search');
    $mySearch = new Pbs_Search();
    $mySearch->setSearchTerm($search);
    $mySearch->setModule('bootiso');
    if($search != '') {
      $this->view->search = $mySearch->getSearchTerm();
      $this->view->bootisolist = $mySearch->search($this->view->bootisolist);
    }
    $this->view->searchform = $mySearch->searchForm();

    // Pagination
    $pagination = new Pbs_Pagination();
    $pagination->setPerPage(10);
    $pagination->setElement($this->view->bootisolist);
    $pagination->setRequestPage($this->_request->getParam('page'));
    $pagination->setPageUrl('/user/bootiso/index'.((isset($this->view->search)) ? '/search/'.$this->view->search : ''));
    $this->view->bootisolist = $pagination->getElements();

    $this->view->pagination = $pagination->pagination();
    $this->view->page     = $pagination->getRequestPage();
  }

  public function searchAction() {
    $this->_redirect('/user/bootiso/index/search/'.($_GET['search']));
  }

  public function downloadbootisoAction() {
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    //ACL Darf er BootISOs downloaden?
    if(!Pbs_Acl::checkRight('bdld'))
      { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/forbidden'); }

    $bootisoID = $this->_request->getParam('bootisoID');

    if(!is_numeric($bootisoID))
      { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/forbidden'); }

    $bootiso = $this->bootisoMapper->find($bootisoID);
    $prebootID = $bootiso->getPrebootID();

    if(!is_dir("../resources/bootmedium/$prebootID/"))
      { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/404'); }

    $zip = new ZipArchive();
    $res = $zip->open("../resources/bootmedium/$prebootID/preboot.zip");
    if($res == true) {
      $rootdir = $zip->getNameIndex(0);
      $zip->addFromString($rootdir."build/rootfs/tmp/serial", $bootiso->getSerialnumber());
      $zip->close();
    }

    chdir("../resources/bootmedium/$prebootID/");

    header("X-Sendfile: preboot.zip");
    header('Content-Type: application/x-gzip');
    $content_disp = ( ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']) == 'IE') ? 'inline' : 'attachment';
    header('Content-Disposition:  ' . $content_disp . '; filename="preboot.zip"');
    header('Pragma: no-cache');
    header('Expires: 0');

    $handle = fopen("preboot.zip", 'r');
    $chunk_size = 8192;
    while ($chunk = fread($handle, $chunk_size)) {
      echo $chunk;
      ob_flush();
    }

  }

  public function createbootisoAction() {
    //ACL Darf er BootISOs erstellen?
    if(!Pbs_Acl::checkRight('bc'))
      { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/addresult/forbidden'); }

    $groupgroupsMapper = new Application_Model_GroupGroupsMapper();
    $groupID = $this->membership->getGroupID();

    $groupgroupsMapper = new Application_Model_GroupGroupsMapper();
    $childgroups = count($groupgroupsMapper->getChildGroups($groupID));
    
    $preboottypeMapper = new Application_Model_PreBootTypeMapper();
    $preboottype = $preboottypeMapper->fetchAll();
    
    if (!isset($_POST["createbootiso"])) {
      $bootisoForm = new user_Form_Bootiso(array(
                                             'action' => 'createbootiso',
      										 'preboottype' => $preboottype,
                                             'groupdepth' => $childgroups,
                                             'page' => $this->page));
    } else {

      $bootisoForm = new user_Form_Bootiso(array(
                                             'action' => 'createbootiso',
      										 'preboottype' => $preboottype,
                                             'groupdepth' => $childgroups,
                                             'page' => $this->page), $_POST);

      if ($bootisoForm->isValid($_POST)) {

        $bootiso = new Application_Model_BootIso($_POST);

        $bootiso->setGroupID($this->membership->getGroupID());
        $bootiso->setCreated(time());

        try {
          $bootisoID = $this->bootisoMapper->save($bootiso);
        } catch(Zend_Exception $e) {
          echo "Caught exception: " . get_class($e) . "<br/>";
          echo "Message: " . $e->getMessage() . "<br/>";
          $this->_redirect('/user/bootiso/index/page/'.$this->page.'/addresult/error');
          //TODO Delete File & delete bootiso from DB

        }
        $this->_redirect('/user/bootiso/index/page/'.$this->page.'/addresult/ok');
      }
    }

    $this->view->bootisoForm = $bootisoForm;
  }

  public function editbootisoAction() {
    //ACL Darf er BootISOs editieren?
    if(!Pbs_Acl::checkRight('be') && !Pbs_Acl::checkRight('bem'))
      { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/forbidden'); }

    $bootisoID = $this->_request->getParam('bootisoID');
    if (!is_numeric($bootisoID))
      { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/forbidden'); }

    $groupID = $this->membership->getGroupID();

    $preboottypeMapper = new Application_Model_PreBootTypeMapper();
    $preboottype = $preboottypeMapper->fetchAll();

    $groupgroupsMapper = new Application_Model_GroupGroupsMapper();
    $childgroups = count($groupgroupsMapper->getChildGroups($groupID));

    $bootiso = new Application_Model_BootIso();
    $this->bootisoMapper->find($bootisoID, $bootiso);

    if($this->membership->getGroupID() != $bootiso->getGroupID())
      { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/forbidden'); }

    if (!isset($_POST["editbootiso"])) {

      $bootisoForm = new user_Form_Bootiso(array(
                                             'action' => 'editbootiso',
      										 'preboottype' => $preboottype,
                                             'groupdepth' => $childgroups,
                                             'page' => $this->page));
      $bootisoForm->populate($bootiso->toArray());

    } else {
      $bootisoForm = new user_Form_Bootiso(array(
                                             'action' => 'editbootiso',
      										 'preboottype' => $preboottype,
                                             'groupdepth' => $childgroups,
                                             'page' => $this->page), $_POST);

      if ($bootisoForm->isValid($_POST)) {

        $bootisoold = $bootiso;

        $bootiso = new Application_Model_BootIso($_POST);
        $bootiso->setGroupID($this->membership->getGroupID());
        $bootiso->setCreated(time());
        $bootiso->setID($bootisoID);

        if( $bootiso->getPreboottypeID() != $bootisoold->getPreboottypeID() ||
            $bootiso->getExpires() != $bootisoold->getExpires() ||
            $bootiso->getPublic() != $bootisoold->getPublic() ||
            $bootiso->getSerialnumber() != $bootisoold->getSerialnumber()) {
          //ACL Is he allowed to edit other than Metadata?
          if(!Pbs_Acl::checkRight('be'))
            { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/forbidden'); }
        }

        try {

          $this->bootisoMapper->save($bootiso);

        } catch(Zend_Exception $e) {
          echo "Caught exception: " . get_class($e) . "<br/>";
          echo "Message: " . $e->getMessage() . "<br/>";
          $this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/error');
          //TODO Redo Serial in Files...
        }

        $this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/ok');
      }

    }

    $this->view->bootisoForm = $bootisoForm;
  }

  public function deletebootisoAction() {
    //ACL Darf er BootISOs löschen?
    if(!Pbs_Acl::checkRight('bd'))
      { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/forbidden'); }

    try {

      $bootisoID = $this->_request->getParam('bootisoID');
      if (!is_numeric($bootisoID))
        { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/forbidden'); }

      $bootiso = new Application_Model_BootIso();
      $this->bootisoMapper->find($bootisoID, $bootiso);

      if($this->membership->getGroupID() != $bootiso->getGroupID())
        { $this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/forbidden'); }

      $this->bootisoMapper->delete($bootiso);

    } catch(Zend_Exception $e) {
      echo "Caught exception: " . get_class($e) . "<br/>";
      echo "Message: " . $e->getMessage() . "<br/>";
      $this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/error');
    }
    $this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/ok');
  }

}