ajax_get_json.php 7.7 KB
Newer Older
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
<?php

/*
Copyright (c) 2012, ISPConfig UG
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

31
32
require_once '../../lib/config.inc.php';
require_once '../../lib/app.inc.php';
33
34
35
36
37
38
39
40
41
42

//* Check permissions for module
$app->auth->check_module_permissions('dashboard');

$app->uses('tform');

$type = $_GET["type"];

//if($_SESSION["s"]["user"]["typ"] == 'admin') {

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

if($type == 'globalsearch'){
	$q = $app->db->quote(trim($_GET["q"]));
	$authsql = " AND ".$app->tform->getAuthSQL('r');
	$modules = explode(',', $_SESSION['s']['user']['modules']);

	$result = array();

	// clients
	$result[] = _search('client', 'client', "AND limit_client = 0");

	// resellers
	$result[] = _search('client', 'reseller', "AND limit_client != 0");

	// web sites
	$result[] = _search('sites', 'web_domain', "AND type = 'vhost'");

	// subdomains
	$result[] = _search('sites', 'web_subdomain', "AND type = 'subdomain'");

	// web site aliases
	$result[] = _search('sites', 'web_aliasdomain', "AND type = 'alias'");

	// vhostsubdomains
	$result[] = _search('sites', 'web_vhost_subdomain', "AND type = 'vhostsubdomain'");

Dominik's avatar
Dominik committed
69
70
71
	// vhostaliasdomains
	$result[] = _search('sites', 'web_vhost_aliasdomain', "AND type = 'vhostalias'");

72
73
74
75
76
77
78
79
	// FTP users
	$result[] = _search('sites', 'ftp_user');

	// shell users
	$result[] = _search('sites', 'shell_user');

	// databases
	/*
80
81
82
83
84
		$result_databases = array('cheader' => array(), 'cdata' => array());
		if(in_array('sites', $modules)){
			$sql = "SELECT * FROM web_database WHERE database_name LIKE '%".$q."%' OR database_user LIKE '%".$q."%' OR remote_ips LIKE '%".$q."%'".$authsql." ORDER BY database_name";
			$results = $app->db->queryAllRecords($sql);

85
			if(is_array($results) && !empty($results)){
86
87
88
89
90
91
92
93
				$result_databases['cheader'] = array('title' => 'Databases',
														'total' => count($results),
														'limit' => count($results)
													);
				foreach($results as $result){
					$description = 'Database User: '.$result['database_user'].' - Remote IPs: '.$result['remote_ips'];
					$result_databases['cdata'][] = array('title' => $result['database_name'],
												'description' => $description,
94
												'onclick' => 'ISPConfig.capp(\'sites\',\'sites/database_edit.php?id='.$result['database_id'].'\');',
95
96
												'fill_text' => strtolower($result['database_name'])
												);
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
	$result[] = _search('sites', 'database');

	// database users
	$result[] = _search('sites', 'database_user');

	// email domains
	$result[] = _search('mail', 'mail_domain');

	// email alias domains
	$result[] = _search('mail', 'mail_aliasdomain', "AND type = 'aliasdomain'");

	// email mailboxes
	$result[] = _search('mail', 'mail_user');

	// email aliases
	$result[] = _search('mail', 'mail_alias', "AND type = 'alias'");

	// email forwards
	$result[] = _search('mail', 'mail_forward', "AND type = 'forward'");

	// email catchalls
	$result[] = _search('mail', 'mail_domain_catchall', "AND type = 'catchall'");

	// email transports
	$result[] = _search('mail', 'mail_transport');

	// mailinglists
	$result[] = _search('mail', 'mail_mailinglist');

	// getmails
	$result[] = _search('mail', 'mail_get');

	// dns zones
	$result[] = _search('dns', 'dns_soa');

	// secondary dns zones
	$result[] = _search('dns', 'dns_slave');

	// virtual machines
	$result[] = _search('vm', 'openvz_vm');

	// virtual machines os templates
	$result[] = _search('vm', 'openvz_ostemplate');

	// virtual machines vm templates
	$result[] = _search('vm', 'openvz_template');

	// virtual machines ip addresses
	$result[] = _search('vm', 'openvz_ip');

	// directive snippets
	$result[] = _search('admin', 'directive_snippets');

	$json = $app->functions->json_encode($result);
}
156
157
158

//}

159
function _search($module, $section, $additional_sql = ''){
160
161
162
163
164
165
166
	global $app, $q, $authsql, $modules;

	$result_array = array('cheader' => array(), 'cdata' => array());
	if(in_array($module, $modules)){
		$search_fields = array();
		$desc_fields = array();
		if(is_file('../'.$module.'/form/'.$section.'.tform.php')){
167
168
			include_once '../'.$module.'/form/'.$section.'.tform.php';

169
170
171
172
173
			$category_title = $form["title"];
			$form_file = $form["action"];
			$db_table = $form["db_table"];
			$db_table_idx = $form["db_table_idx"];
			$order_by = $db_table_idx;
174

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
			if(is_array($form["tabs"]) && !empty($form["tabs"])){
				foreach($form["tabs"] as $tab){
					if(is_array($tab['fields']) && !empty($tab['fields'])){
						foreach($tab['fields'] as $key => $val){
							if(isset($val['searchable']) && $val['searchable'] > 0){
								$search_fields[] = $key." LIKE '%".$q."%'";
								if($val['searchable'] == 1){
									$order_by = $key;
									$title_key = $key;
								}
								if($val['searchable'] == 2){
									$desc_fields[] = $key;
								}
							}
						}
					}
				}
			}
		}
		unset($form);
195

196
197
198
199
200
201
202
		$where_clause = '';
		if(!empty($search_fields)){
			$where_clause = implode(' OR ', $search_fields);
		} else {
			// valid SQL query which returns an empty result set
			$where_clause = '1 = 0';
		}
203
204
		if($where_clause != '') $where_clause = '('.$where_clause.')';
		if($additional_sql != '') $where_clause .= ' '.$additional_sql.' ';
205
206
		$order_clause = '';
		if($order_by != '') $order_clause = ' ORDER BY '.$order_by;
207

208
209
		$sql = "SELECT * FROM ".$db_table." WHERE ".$where_clause.$authsql.$order_clause." LIMIT 0,10";
		$results = $app->db->queryAllRecords($sql);
210
211

		if(is_array($results) && !empty($results)){
212
			$lng_file = '../'.$module.'/lib/lang/'.$_SESSION['s']['language'].'_'.$section.'.lng';
213
			if(is_file($lng_file)) include $lng_file;
214
			$result_array['cheader'] = array('title' => $category_title,
215
216
217
				'total' => count($results),
				'limit' => count($results)
			);
218
219
220
221
222
223
224
225
226
			foreach($results as $result){
				$description = '';
				if(!empty($desc_fields)){
					$desc_items = array();
					foreach($desc_fields as $desc_field){
						if($result[$desc_field] != '') $desc_items[] = $wb[$desc_field.'_txt'].': '.$result[$desc_field];
					}
					if(!empty($desc_items)) $description = implode(' - ', $desc_items);
				}
227

228
				$result_array['cdata'][] = array('title' => $wb[$title_key.'_txt'].': '.$result[$title_key],
229
					'description' => $description,
230
					'onclick' => "ISPConfig.capp('".$module."','".$module."/".$form_file."?id=".$result[$db_table_idx]."');",
231
232
233
					'fill_text' => strtolower($result[$title_key])
				);
			}
234
235
236
237
		}
	}
	return $result_array;
}
238

239
240
header('Content-type: application/json');
echo $json;
241
?>