1 <?php
3 class InstallRecipe extends plugin
4 {
6 public $rpcErrorMessage = "";
7 public $uuid = "";
9 // Template options
10 public $installTemplate = "";
11 public $installationTemplateNames = array();
12 public $templateToRelease = array();
13 public $installationTemplates = array();
15 // The selected installation release
16 public $installRelease = "";
18 // Selected config management variant
19 public $installConfigManagement = '';
20 public $installConfigManagementList = array();
22 // Bootstrap method
23 public $installBootstrapMethod = '';
24 public $installBootstrapMethodList = array();
26 // Timezone settings
27 public $installTimezone = "";
28 public $installTimeUTC = "";
30 // Kernel packages
31 public $installKernelPackage = "";
32 public $installKernelPackageList = array();
34 // System Locale
35 public $installSystemLocale = "";
36 public $installSystemLocaleList = array();
38 // Selected NTP Server
39 public $installNTPServer = array();
40 public $installNTPServerList = NULL;
42 // Root password
43 public $installRootEnabled = "";
44 public $installRootPasswordHash = "";
45 public $setPasswordHash = FALSE;
47 // Partition table variables
48 public $installPartitionTable = "";
50 // Keyboard-layout attribute
51 public $installKeyboardlayout = "";
52 public $installKeyboardlayoutList = array();
54 // Mirror related attribute
55 public $installMirror = "";
56 public $installMirrorList = array();
58 public $objectclasses = array();
59 public $attributes = array("installRelease", "installTemplate","installKeyboardlayout","installSystemLocale",
60 "installTimezone","installTimeUTC","installNTPServer","installMirror",
61 "installRootEnabled","installRootPasswordHash","installKernelPackage",
62 "installPartitionTable","installConfigManagement","installBootstrapMethod");
65 /* Some plugin management related attributes
66 * See class plugin for details.
67 */
68 public $view_logged = FALSE;
69 public $ignore_account = FALSE;
72 /*! \brief Initialize the plugin and its widgets.
73 * Finally call init() to load values from the backend.
74 */
75 function __construct(&$config, $dn)
76 {
77 plugin::plugin($config, $dn);
79 // Set default password hash
80 $this->hash = $this->config->get_cfg_value("core","passwordDefaultHash");
82 // Get list of password hashes
83 $tmp = passwordMethod::get_available_methods();
84 $this->hashes = array();
85 foreach($tmp['name'] as $name){
86 $this->hashes[$name] = $name;
87 }
89 print "Hard coded uuid!";
90 $this->uuid = "a0d17918-cfb2-11df-acbd-5452005f1250";
92 // Init the object values.
93 $this->init_static_info();
94 $this->init_client_info();
96 // Prepare NTP servers list
97 $this->installNTPServerList= new sortableListing($this->installNTPServer);
98 $this->installNTPServerList->setDeleteable(true);
99 $this->installNTPServerList->setEditable(false);
100 $this->installNTPServerList->setColspecs(array('*'));
101 $this->installNTPServerList->setWidth("100%");
102 $this->installNTPServerList->setHeight("70px");
104 // Preset values for new devices
105 if(!$this->is_account){
106 $this->installTemplate = key($this->installationTemplates);
107 $this->installRelease = key($this->templateToRelease[$this->installTemplate]);
108 }else{
110 if(!isset($this->installationTemplates[$this->installTemplate])){
112 // The selected template is no longer available, select the next selectable template.
113 $new = key($this->installationTemplates);
114 $message = sprintf(_("The selected template '%s' is no longer available. Selecting '%s' as replacement!"),
115 $this->installTemplate, $new);
116 msg_dialog::display(_("Warning"), $message, WARNING_DIALOG);
117 $this->installTemplate = $new;
118 $installRelease = key($this->templateToRelease[$this->installTemplate]);
120 }elseif(!isset($this->installationTemplates[$this->installTemplate]['method'])){
122 // Not necessary to warn the user here, the input fields will be empty and thus
123 // avoid saving.
125 }elseif(!in_array($this->installRelease, $this->templateToRelease[$this->installTemplate])){
127 // The selected release is no longer available for the current template
128 // Warn the user and select the next selectable release.
129 $new = key($this->templateToRelease[$this->installTemplate]);
130 $message = sprintf(_("The selected release '%s' is no longer available. Selecting '%s' as replacement!"),
131 $this->installRelease, $new);
132 $this->installRelease = $new;
133 msg_dialog::display(_("Warning"), $message, WARNING_DIALOG);
134 }
135 }
137 // Update selectable items like kernels and mirrors depending on the current setup (relase)
138 $this->installBootstrapMethod = $this->installationTemplates[$this->installTemplate]['method'];
139 $this->reloadInstallationKernelPackages();
140 $this->reloadInstallationMirrors();
141 }
144 /*! \brief Loads client-information values from the GOsa-backend using jsonRPC
145 */
146 function init_client_info()
147 {
148 $rpc = $this->config->getRpcHandle();
149 $res = $rpc->systemGetBaseInstallParameters($this->uuid);
151 // Failed to load backend values, keep reported error and return with FALSE;
152 $this->rpcError = !$rpc->success();
153 if(!$rpc->success()){
154 $this->rpcErrorMessage = $rpc->get_error();
155 return(NULL);
156 }
158 // Apply received options
159 $this->is_account = $res == True;
160 if($this->is_account){
161 $this->installNTPServer = $res['ntp-servers'];
162 $this->installTemplate = $res['template'];
163 $this->installRelease = $res['release'][0];
164 $this->installTimezone = array_search($res['timezone'][0], $this->timezones);
165 $this->installTimeUTC = $res['utc'][0];
166 $this->installKernelPackage = $res['kernel'][0];;
167 $this->installSystemLocale = $res['system-locale'][0];
168 $this->installRootEnabled = $res['root-user'][0];
169 $this->installRootPasswordHash = $res['root-hash'][0];
170 $this->installPartitionTable = $res['disk-setup'][0];
171 $this->installKeyboardlayout = $res['keyboard-layout'];
172 }
174 return;
175 }
178 function init_static_info()
179 {
180 // Load static values.
181 foreach(array(
182 "timeozones" => "getTimezones",
183 "distributions" => "loadDistributions",
184 "keyboardLayouts" => "loadInstallationKeyboardLayouts",
185 "installationTemplates" => "loadInstallationTemplates",
186 "installationMethods" => "loadInstallationMethods",
187 "locales" => "getSystemLocales" ) as $target => $func){
188 $$target = $this->$func();
189 if($$target == NULL){
190 $this->initFailed = TRUE;
191 msg_dialog::display(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
192 return;
193 }
194 }
196 // Assign fetchted values
197 $this->timezones = $timeozones;
198 $this->installBootstrapMethodList = $installationMethods['installBootstrapMethodList'];
199 $this->installConfigManagementList = $installationMethods['installConfigManagementList'];
200 $this->installSystemLocaleList = $locales;
201 $this->installKeyboardlayoutList = $keyboardLayouts;
202 $this->distributions = $distributions;
204 # Create template to relase map
205 $this->templateToRelease = array();
207 // Walk through installation templates and use the 'repositories' attribute
208 // to supported releases.
209 foreach($installationTemplates as $name => $tdata){
211 $mName = isset($this->installBootstrapMethodList[$tdata['method']]) ?
212 $this->installBootstrapMethodList[$tdata['method']]: $tdata['method'];
214 $this->installationTemplateNames[$name] = $tdata['description']." (".$mName.")";
215 $this->installationTemplates[$name] = $tdata;
216 $this->templateToRelease[$name] = array();
218 // Check if the 'repository'-category of the template can be found in any
219 // received release list.
220 if(isset($installationMethods[$tdata['method']]['repositories'])){
221 $repos = $installationMethods[$tdata['method']]['repositories'];
222 foreach($distributions as $dist){
223 if(in_array($dist['type']['name'],$repos) && isset($dist['releases'])){
224 foreach($dist['releases'] as $release){
225 $rname = $dist['name'].'/'.$release['name'];
226 $this->templateToRelease[$name][$rname] = $rname;
227 }
228 }
229 }
230 }
231 }
232 }
235 function execute()
236 {
237 // Log account access
238 if($this->is_account && !$this->view_logged){
239 $this->view_logged = TRUE;
240 new log("view","Device/".get_class($this),$this->dn);
241 }
243 /***************
244 Handle account state
245 ***************/
247 // Allow to add or remove the distribution list extension
248 if(isset($_POST['modify_state'])){
249 if($this->is_account && $this->acl_is_removeable()){
250 $this->is_account= FALSE;
251 }elseif(!$this->is_account && $this->acl_is_createable()){
252 $this->is_account= TRUE;
253 }
254 }
256 // Show account status-changer
257 $display = "";
258 if ($this->parent !== NULL){
259 if ($this->is_account){
260 $display= $this->show_disable_header(_("Remove install profile"),
261 msgPool::featuresEnabled(_("Install profile")));
262 } else {
263 $display= $this->show_enable_header(_("Add install profile"),
264 msgPool::featuresDisabled(_("Install profile")));
265 return ($display);
266 }
267 }
269 /***************
270 Root password hash dialog
271 ***************/
273 if($this->setPasswordHash){
274 $this->dialog = TRUE;
275 $smarty = get_smarty();
276 $smarty->assign('hashes', set_post($this->hashes));
277 $smarty->assign('hash', set_post($this->hash));
278 return($smarty->fetch(get_template_path('goto/Device/SetPassword.tpl', TRUE)));
279 }
281 /***************
282 Generate HTML content
283 ***************/
285 $this->installNTPServerList->setAcl($this->getacl('installNTPServer'));
286 $this->installNTPServerList->update();
288 plugin::execute();
289 $smarty = get_smarty();
290 $smarty->assign('installTemplateList', $this->installationTemplateNames);
291 $smarty->assign('installReleaseList', $this->templateToRelease[$this->installTemplate]);
294 $smarty->assign('timezones', $this->timezones);
295 $smarty->assign('installKeyboardlayoutList', $this->installKeyboardlayoutList);
296 $smarty->assign('installKernelPackageList', $this->installKernelPackageList);
297 $smarty->assign('installMirrorList', $this->installMirrorList);
298 $smarty->assign('installSystemLocaleList', $this->installSystemLocaleList);
299 $smarty->assign('installNTPServerList', $this->installNTPServerList->render());
301 $smarty->assign('installBootstrapMethod', $this->installBootstrapMethod);
302 $smarty->assign('installBootstrapMethodList', $this->installBootstrapMethodList);
303 $smarty->assign('installConfigManagement', $this->installConfigManagement);
305 // Assign list of available config-management types.
306 if(isset($this->installConfigManagementList[$this->installBootstrapMethod])){
307 $smarty->assign('installConfigManagementList', $this->installConfigManagementList[$this->installBootstrapMethod]);
308 }else{
309 $smarty->assign('installConfigManagementList', array());
310 }
312 foreach($this->attributes as $attr){
313 $smarty->assign($attr, $this->$attr);
314 }
315 $this->dialog = false;
316 return($display.$smarty->fetch(get_template_path('goto/Device/InstallRecipe.tpl', TRUE)));
317 }
320 function save_object()
321 {
322 if(isset($_POST['InstallRecipePosted'])){
324 $currentInstallMethod = $this->installConfigManagement;
326 $oldRelease = $this->installRelease;
327 plugin::save_object();
328 $this->installRootEnabled = isset($_POST['installRootEnabled']);
329 $this->installTimeUTC = isset($_POST['installTimeUTC']);
332 if(isset($_POST['setPasswordHash'])){
333 $this->setPasswordHash = true;
334 }
336 $this->installNTPServerList->save_object();
337 $this->installNTPServer = $this->installNTPServerList->getMaintainedData();
338 if(isset($_POST['installNTPServer_Input']) && isset($_POST['installNTPServer_Add'])){
339 $add = get_post('installNTPServer_Input');
340 if(!in_array($add, $this->installNTPServer) && !empty($add)){
341 $this->installNTPServer[] = $add;
342 }
343 }
344 $this->installNTPServerList->setListData($this->installNTPServer);
345 $this->installBootstrapMethod = $this->installationTemplates[$this->installTemplate]['method'];
347 // Reload list of kernel packages and mirrors
348 if($oldRelease != $this->installRelease){
349 $this->reloadInstallationKernelPackages();
350 $this->reloadInstallationMirrors();
351 }
352 }
354 if(isset($_POST['cancelPassword'])) $this->setPasswordHash =false;
355 if(isset($_POST['setPassword'])) {
356 $this->setPasswordHash =false;
357 $hash = get_post('passwordHash');
359 // Not sure, why this is here, but maybe some encryption methods require it.
360 mt_srand((double) microtime()*1000000);
362 // Generate the requested hash
363 $methods = new passwordMethod($this->config, $this->dn);
364 $available = $methods->get_available_methods();
365 $test = new $available[$hash]($this->config,$this->dn);
366 $this->installRootPasswordHash = @$test->generate_hash(get_post('rootPassword'));
367 }
368 }
371 function save()
372 {
373 if(!$this->installRootEnabled) $this->installRootPasswordHash = "";
375 print "Save missing";
376 return;
377 $rpc = $this->config->getRpcHandle();
378 $res = $rpc->systemSetBaseInstallParameters($uuid, $data);
380 // Failed to load backend values, keep reported error and return with FALSE;
381 $this->rpcError = !$rpc->success();
382 if(!$rpc->success()){
383 $this->rpcErrorMessage = $rpc->get_error();
384 return(NULL);
385 }
386 return($res);
388 }
391 function remove_from_parent()
392 {
393 print "Missing";
394 }
397 /*! \brief Refreshes the list of selectable kernel packages.
398 */
399 function reloadInstallationKernelPackages()
400 {
401 $rpc = $this->config->getRpcHandle();
402 $res = $rpc->getKernelPackages($this->installRelease);
404 // Failed to load backend values, keep reported error and return with FALSE;
405 $this->rpcError = !$rpc->success();
406 if(!$rpc->success()){
407 $this->rpcErrorMessage = $rpc->get_error();
408 msg_dialog::display(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
409 }else{
410 $this->installKernelPackageList = array();
411 foreach($res as $name){
412 $this->installKernelPackageList[$name] = $name;
413 }
415 if(!isset($this->installKernelPackageList[$this->installKernelPackage])){
416 $new = key($this->installKernelPackageList);
417 msg_dialog::display(_("Warning"), sprintf(_("The selected kernel package '%s' is no longer available! The kernel package '%s' was selected as replacement!"), $this->installKernelPackage, $new), WARNING_DIALOG);
418 $this->installKernelPackage = $new;
419 }
420 }
421 }
424 /*! \brief Returns a list of distributions on success.
425 * In case of an error, it returns NULL.
426 */
427 function loadDistributions()
428 {
429 $rpc = $this->config->getRpcHandle();
430 $res = $rpc->getDistributions();
432 // Failed to load backend values, keep reported error and return with FALSE;
433 $this->rpcError = !$rpc->success();
434 if(!$rpc->success()){
435 $this->rpcErrorMessage = $rpc->get_error();
436 return(NULL);
437 }
438 return($res);
439 }
442 /*! \brief Returns a list of timezones on success.
443 * In case of an error, it returns NULL.
444 */
445 function getTimezones()
446 {
447 $rpc = $this->config->getRpcHandle();
448 $res = $rpc->getTimezones();
450 // Failed to load backend values, keep reported error and return with FALSE;
451 $this->rpcError = !$rpc->success();
452 if(!$rpc->success()){
453 $this->rpcErrorMessage = $rpc->get_error();
454 return(NULL);
455 }
456 return($res);
457 }
460 /*! \brief Returns a list of installation mirrors on success.
461 * In case of an error, it returns NULL.
462 */
463 function reloadInstallationMirrors()
464 {
465 $res = array(
466 'dc=intranet,dc=gonicus,dc=de' => 'Intranet',
467 'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System');
468 $this->installMirrorList =$res;
469 }
472 /*! \brief Returns a list of installation templates on success.
473 * In case of an error, it returns NULL.
474 */
475 function loadInstallationTemplates()
476 {
477 $rpc = $this->config->getRpcHandle();
478 $res = $rpc->installListTemplates();
480 // Failed to load backend values, keep reported error and return with FALSE;
481 $this->rpcError = !$rpc->success();
482 if(!$rpc->success()){
483 $this->rpcErrorMessage = $rpc->get_error();
484 return(NULL);
485 }
486 return($res);
487 }
490 /*! \brief Returns a list of keyboard layouts on success.
491 * In case of an error, it returns NULL.
492 */
493 function loadInstallationKeyboardLayouts()
494 {
495 $rpc = $this->config->getRpcHandle();
496 $res = $rpc->getKeyboardModels();
498 // Failed to load backend values, keep reported error and return with FALSE;
499 $this->rpcError = !$rpc->success();
500 if(!$rpc->success()){
501 $this->rpcErrorMessage = $rpc->get_error();
502 return(NULL);
503 }
504 $lay = array();
505 foreach($res as $key => $data){
506 $lay[$key] = "{$data[0]} - {$data[2]}";
507 }
508 asort($lay);
509 return($lay);
510 }
513 /*! \brief Returns a list of system locales on success.
514 * In case of an error, it returns NULL.
515 */
516 function getSystemLocales()
517 {
518 $rpc = $this->config->getRpcHandle();
519 $res = $rpc->getSystemLocales();
521 // Failed to load backend values, keep reported error and return with FALSE;
522 $this->rpcError = !$rpc->success();
523 if(!$rpc->success()){
524 $this->rpcErrorMessage = $rpc->get_error();
525 return(NULL);
526 }
528 return($res);
529 }
532 /*! \brief Try to load install and bootstrap methods from the GOsa-backend
533 * This is done via jsonRPC, if calling the backend method fails,
534 * this method returns FALSE, in case of success it return TRUE;
535 */
536 function loadInstallationMethods()
537 {
538 $rpc = $this->config->getRpcHandle();
539 $res = $rpc->getSupportedBaseInstallMethods();
541 // Failed to load backend values, keep reported error and return with FALSE;
542 $this->rpcError = !$rpc->success();
543 if(!$rpc->success()){
544 $this->rpcErrorMessage = $rpc->get_error();
545 return(NULL);
546 }
548 // Walk through result and set bootstrap methods.
549 foreach($res as $name => $method){
550 $res['installBootstrapMethodList'][$name] = $method['name'];
551 foreach($method['methods'] as $m){
552 $res['installConfigManagementList'][$name][$m] = $m;
553 }
554 }
555 return($res);
556 }
559 static function plInfo()
560 {
561 return (array(
562 "plShortName" => _("Device"),
563 "plDescription" => _("Registered device"),
564 "plSelfModify" => FALSE,
565 "plDepends" => array(),
566 "plPriority" => 1,
567 "plSection" => array("administration"),
568 "plCategory" => array("Device"),
569 "plProvidedAcls" => array(
570 "member" => _("Member"),
571 "installTemplate" => _("Installation template"),
572 "installRelease" => _("Installation release"),
574 "installBootstrapMethod" => _("Bootstrap method"),
575 "installConfigManagement" => _("Config management"),
576 "installKeyboardlayout" => _("Keyboard layout"),
577 "installSystemLocale" => _("System locale"),
578 "installTimezone" => _("Timezone"),
579 "installTimeUTC" => _("Time"),
580 "installNTPServer" => _("NTP-Server"),
581 "installMirror" => _("Mirror"),
582 "installRootEnabled" => _("Root login enabled"),
583 "installRootPasswordHash" => _("Root password hash"),
584 "installKernelPackage" => _("Kernel package"),
585 "installPartitionTable" => _("Partition table")
586 )
587 )
588 );
589 }
590 }
591 ?>