summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfignew/page.inc.php
blob: c8cdb3cd473baaf9acb7d9cefbedc96e0a2d6c5a (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
<?php

class Page_SysConfigNew extends Page
{
	// private $tmpath = '/srv/openslx/www/';
	private $tmpath = '/home/raul/tm-scripts/server';
	private $tmconfigs;
	private $tmmodules;

	protected function doPreprocess(){
		User::load();
		if (!User::hasPermission('baseconfig_local')) {
			Message::addError('main.no-permission');
			Util::redirect('?do=Main');
		}

		$this->tmconfigs = $this->tmpath . '/configs';
		$this->tmmodules = $this->tmpath . '/modules';
	}


	protected function doRender(){
		$module = $_GET['module'];
		if(isset($module)){
			Render::addTemplate('module-editor',array(
				"module" => $module
			));
		}else{
			if(is_dir($this->tmpath)){
				$configs = array();
				$modules = array();

				foreach($this->listDirectory($this->tmconfigs) as $key => $value)
					$configs[] = array(
						"name" => $value
					);

				foreach($this->listDirectory($this->tmmodules) as $key => $value)
					$modules[] = array(
						"name" => $value
					);

				$data = array(
					"configs"  => $configs,
					"modules" => $modules
				);
				Render::addTemplate('_pagenew',$data);
			}else{
				Message::addError('no-tm-scripts');
			}
		}
	}

	protected function doAjax(){
		$request = $_GET['request'];
		switch($request){
			case "module-contents":
				$path = $this->tmpath . '/modules/' . Request::get('module');
				$data = $this->getDirContents($path);
				$json = json_encode($data);
				print_r($json);
				break;
			case "configs":
				$this->tmconfigs = $this->tmpath . '/configs';
				$this->tmmodules = $this->tmpath . '/modules';
				$userModules = $this->listDirectory($this->tmconfigs . '/' . Request::get('config'));
				$modules = array();
				foreach($this->listDirectory($this->tmmodules) as $key => $value){
					$chosen = (in_array($value, $userModules)) ? true : false;
					$modules[] = array(
						"name" => $value,
						"chosen" => $chosen
					);
				}

				$ret = '';
				foreach ($modules as $module) {
					$class = ($module['chosen']) ? "select-item select-item-selected" : "select-item";
					$ret .= "<button type='button' class='" . $class . "' onclick='select(this)' >";
					$ret .= $module['name'];
					$ret .= "</button>";
				}

				echo $ret;
				break;
		}

	}

	private function getDirContents($path){
		$ret = array();
		foreach ($this->listDirectory($path) as $key => $value) {
			if(is_dir($path . "/" . $value)){
				$ret["dir_" . $value] = $this->getDirContents($path . "/" . $value);
			}else{
				if(is_link($path . "/" . $value)){
					$ret["link_" . $value] = readlink($path . "/" . $value);
				}else{
					if(mime_content_type($path . "/" . $value) == "text/plain"){
						$ret["file_" . $value] = file_get_contents($path . "/" . $value);
					}else{
						$ret["lock_" . $value] = " oops";
					}
				}
			}
		}
		return $ret;
	}

	private function listDirectory($path){
		return array_diff(scandir($path), array('..', '.'));
	}

}