sites.inc.php 38.3 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
31
32
33
34
35
36
37
38
39
40
41
42
<?php

/*
Copyright (c) 2007 - 2013, Till Brehm, projektfarm Gmbh
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.

--UPDATED 08.2009--
Full SOAP support for ISPConfig 3.1.4 b
Updated by Arkadiusz Roch & Artur Edelman
Copyright (c) Tri-Plex technology

--UPDATED 08.2013--
Migrated into new remote classes system
by Marius Cramer <m.cramer@pixcept.de>

*/

class remoting_sites extends remoting {
	// Website functions ---------------------------------------------------------------------------------------
43

44
45
	//* Get cron details
	public function sites_cron_get($session_id, $cron_id)
46
	{
47
		global $app;
48

49
50
51
52
53
54
55
56
		if(!$this->checkPerm($session_id, 'sites_cron_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
		$app->remoting_lib->loadFormDef('../sites/form/cron.tform.php');
		return $app->remoting_lib->getDataRecord($cron_id);
	}
57

58
59
	//* Add a cron record
	public function sites_cron_add($session_id, $client_id, $params)
60
	{
61
62
63
64
		if(!$this->checkPerm($session_id, 'sites_cron_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
65
		return $this->insertQuery('../sites/form/cron.tform.php', $client_id, $params);
66
	}
67

68
69
	//* Update cron record
	public function sites_cron_update($session_id, $client_id, $cron_id, $params)
70
	{
71
72
73
74
		if(!$this->checkPerm($session_id, 'sites_cron_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
75
		$affected_rows = $this->updateQuery('../sites/form/cron.tform.php', $client_id, $cron_id, $params);
76
77
		return $affected_rows;
	}
78

79
80
	//* Delete cron record
	public function sites_cron_delete($session_id, $cron_id)
81
	{
82
83
84
85
		if(!$this->checkPerm($session_id, 'sites_cron_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
86
		$affected_rows = $this->deleteQuery('../sites/form/cron.tform.php', $cron_id);
87
88
		return $affected_rows;
	}
89

90
	// ----------------------------------------------------------------------------------------------------------
91

92
93
	//* Get record details
	public function sites_database_get($session_id, $primary_id)
94
	{
95
		global $app;
96

97
98
99
100
101
102
103
104
		if(!$this->checkPerm($session_id, 'sites_database_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
		$app->remoting_lib->loadFormDef('../sites/form/database.tform.php');
		return $app->remoting_lib->getDataRecord($primary_id);
	}
105
106
	
	/* TODO: secure queries! */
107
108
	//* Add a record
	public function sites_database_add($session_id, $client_id, $params)
109
110
111
	{
		global $app;

112
113
114
115
		if(!$this->checkPerm($session_id, 'sites_database_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
116

117
		//* Check for duplicates
118
		$tmp = $app->db->queryOneRecord("SELECT count(database_id) as dbnum FROM web_database WHERE database_name = ? AND server_id = ?", $params['database_name'], $params["server_id"]);
119
120
121
122
123
		if($tmp['dbnum'] > 0) {
			throw new SoapFault('database_name_error_unique', 'There is already a database with that name on the same server.');
			return false;
		}

124
125
126
		$sql = $this->insertQueryPrepare('../sites/form/database.tform.php', $client_id, $params);
		if($sql !== false) {
			$app->uses('sites_database_plugin');
127

128
129
130
			$this->id = 0;
			$this->dataRecord = $params;

131
			$retval = $this->insertQueryExecute($sql, $params);
132
			$app->sites_database_plugin->processDatabaseInsert($this);
133
134
135
136
137
138
139
140
141
142
			
			// set correct values for backup_interval and backup_copies
			if(isset($params['backup_interval']) || isset($params['backup_copies'])){
				$sql_set = array();
				if(isset($params['backup_interval'])) $sql_set[] = "backup_interval = '".$app->db->quote($params['backup_interval'])."'";
				if(isset($params['backup_copies'])) $sql_set[] = "backup_copies = ".$app->functions->intval($params['backup_copies']);
				$this->updateQueryExecute("UPDATE web_database SET ".implode(', ', $sql_set)." WHERE database_id = ".$retval, $retval, $params);
			}
			
			return $retval;
143
144
145
		}

		return false;
146
	}
147

148
149
	//* Update a record
	public function sites_database_update($session_id, $client_id, $primary_id, $params)
150
151
152
	{
		global $app;

153
154
155
156
		if(!$this->checkPerm($session_id, 'sites_database_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
157

158
		$sql = $this->updateQueryPrepare('../sites/form/database.tform.php', $client_id, $primary_id, $params);
159
160
161
162
163
164
		if($sql !== false) {
			$app->uses('sites_database_plugin');

			$this->id = $primary_id;
			$this->dataRecord = $params;
			$app->sites_database_plugin->processDatabaseUpdate($this);
165
166
167
168
169
170
171
172
173
174
175
			$retval = $this->updateQueryExecute($sql, $primary_id, $params);
			
			// set correct values for backup_interval and backup_copies
			if(isset($params['backup_interval']) || isset($params['backup_copies'])){
				$sql_set = array();
				if(isset($params['backup_interval'])) $sql_set[] = "backup_interval = '".$app->db->quote($params['backup_interval'])."'";
				if(isset($params['backup_copies'])) $sql_set[] = "backup_copies = ".$app->functions->intval($params['backup_copies']);
				$this->updateQueryExecute("UPDATE web_database SET ".implode(', ', $sql_set)." WHERE database_id = ".$primary_id, $primary_id, $params);
			}
			
			return $retval;
176
177
178
179
180
		}

		return false;
	}

181
182
	//* Delete a record
	public function sites_database_delete($session_id, $primary_id)
183
184
	{
		global $app;
185
186
187
188
		if(!$this->checkPerm($session_id, 'sites_database_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
189
190
191
192
193

		$app->uses('sites_database_plugin');
		$app->sites_database_plugin->processDatabaseDelete($primary_id);

		$affected_rows = $this->deleteQuery('../sites/form/database.tform.php', $primary_id);
194
195
		return $affected_rows;
	}
196

197
	// ----------------------------------------------------------------------------------------------------------
198

199
200
	//* Get record details
	public function sites_database_user_get($session_id, $primary_id)
201
	{
202
		global $app;
203

204
205
206
207
208
209
210
211
		if(!$this->checkPerm($session_id, 'sites_database_user_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
		$app->remoting_lib->loadFormDef('../sites/form/database_user.tform.php');
		return $app->remoting_lib->getDataRecord($primary_id);
	}
212

213
214
	//* Add a record
	public function sites_database_user_add($session_id, $client_id, $params)
215
	{
216
217
218
219
220
		if(!$this->checkPerm($session_id, 'sites_database_user_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}

221
		return $this->insertQuery('../sites/form/database_user.tform.php', $client_id, $params);
222
	}
223

224
225
	//* Update a record
	public function sites_database_user_update($session_id, $client_id, $primary_id, $params)
226
227
228
	{
		global $app;

229
230
231
232
233
234
		if(!$this->checkPerm($session_id, 'sites_database_user_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
		$app->remoting_lib->loadFormDef('../sites/form/database_user.tform.php');
235
236
		$old_rec = $app->remoting_lib->getDataRecord($primary_id);

237
		$result = $this->updateQuery('../sites/form/database_user.tform.php', $client_id, $primary_id, $params);
238
239
240

		$new_rec = $app->remoting_lib->getDataRecord($primary_id);

241
		$records = $app->db->queryAllRecords("SELECT DISTINCT server_id FROM web_database WHERE database_user_id = ? UNION SELECT DISTINCT server_id FROM web_database WHERE database_ro_user_id = ?", $primary_id, $primary_id);
242
243
244
245
246
247
248
249
250
251
252
253
		foreach($records as $rec) {
			$tmp_rec = $new_rec;
			$tmp_rec['server_id'] = $rec['server_id'];
			$app->remoting_lib->datalogSave('UPDATE', $primary_id, $old_rec, $tmp_rec);
		}
		unset($new_rec);
		unset($old_rec);
		unset($records);

		return $result;
	}

254
255
	//* Delete a record
	public function sites_database_user_delete($session_id, $primary_id)
256
257
258
	{
		global $app;

259
260
261
262
		if(!$this->checkPerm($session_id, 'sites_database_user_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
263

264
		//$app->db->datalogDelete('web_database_user', 'database_user_id', $primary_id);
265
266
		$affected_rows = $this->deleteQuery('../sites/form/database_user.tform.php', $primary_id);

267
		$records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE database_user_id = ?", $primary_id);
268
		foreach($records as $rec) {
269
			$app->db->datalogUpdate('web_database', array('database_user_id' => null), 'database_id', $rec['database_id']);
270
271

		}
272
		$records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE database_ro_user_id = ?", $primary_id);
273
		foreach($records as $rec) {
274
			$app->db->datalogUpdate('web_database', array('database_ro_user_id' => null), 'database_id', $rec['database_id']);
275
276
		}

277
278
		return $affected_rows;
	}
279

280
	// ----------------------------------------------------------------------------------------------------------
281

282
283
	//* Get record details
	public function sites_ftp_user_get($session_id, $primary_id)
284
	{
285
		global $app;
286

287
288
289
290
291
292
293
294
		if(!$this->checkPerm($session_id, 'sites_ftp_user_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
		$app->remoting_lib->loadFormDef('../sites/form/ftp_user.tform.php');
		return $app->remoting_lib->getDataRecord($primary_id);
	}
295

296
297
	//* Add a record
	public function sites_ftp_user_add($session_id, $client_id, $params)
298
	{
299
300
301
302
		if(!$this->checkPerm($session_id, 'sites_ftp_user_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
303
		return $this->insertQuery('../sites/form/ftp_user.tform.php', $client_id, $params);
304
	}
305

306
307
	//* Update a record
	public function sites_ftp_user_update($session_id, $client_id, $primary_id, $params)
308
	{
309
310
311
312
		if(!$this->checkPerm($session_id, 'sites_ftp_user_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
313
		$affected_rows = $this->updateQuery('../sites/form/ftp_user.tform.php', $client_id, $primary_id, $params);
314
315
		return $affected_rows;
	}
316

317
318
	//* Delete a record
	public function sites_ftp_user_delete($session_id, $primary_id)
319
	{
320
321
322
323
		if(!$this->checkPerm($session_id, 'sites_ftp_user_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
324
		$affected_rows = $this->deleteQuery('../sites/form/ftp_user.tform.php', $primary_id);
325
326
		return $affected_rows;
	}
327

328
329
	//* Get server for an ftp user
	public function sites_ftp_user_server_get($session_id, $ftp_user)
330
	{
331
		global $app;
332

333
334
335
336
		if(!$this->checkPerm($session_id, 'sites_ftp_user_server_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
337

338
		$data = $app->db->queryOneRecord("SELECT server_id FROM ftp_user WHERE username = ?", $ftp_user);
339
		//file_put_contents('/tmp/test.txt', serialize($data));
340
341
342
343
344
		if(!isset($data['server_id'])) return false;

		$server = $this->server_get($session_id, $data['server_id'], 'server');
		//file_put_contents('/tmp/test2.txt', serialize($server));

345
346
		return $server;
	}
347

348
	// ----------------------------------------------------------------------------------------------------------
349

350
351
	//* Get record details
	public function sites_shell_user_get($session_id, $primary_id)
352
	{
353
		global $app;
354

355
356
357
358
359
360
361
362
		if(!$this->checkPerm($session_id, 'sites_shell_user_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
		$app->remoting_lib->loadFormDef('../sites/form/shell_user.tform.php');
		return $app->remoting_lib->getDataRecord($primary_id);
	}
363

364
365
	//* Add a record
	public function sites_shell_user_add($session_id, $client_id, $params)
366
	{
367
368
369
370
		if(!$this->checkPerm($session_id, 'sites_shell_user_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
371
		return $this->insertQuery('../sites/form/shell_user.tform.php', $client_id, $params);
372
	}
373

374
375
	//* Update a record
	public function sites_shell_user_update($session_id, $client_id, $primary_id, $params)
376
	{
377
378
379
380
		if(!$this->checkPerm($session_id, 'sites_shell_user_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
381
		$affected_rows = $this->updateQuery('../sites/form/shell_user.tform.php', $client_id, $primary_id, $params);
382
383
		return $affected_rows;
	}
384

385
386
	//* Delete a record
	public function sites_shell_user_delete($session_id, $primary_id)
387
	{
388
389
390
391
		if(!$this->checkPerm($session_id, 'sites_shell_user_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
392
		$affected_rows = $this->deleteQuery('../sites/form/shell_user.tform.php', $primary_id);
393
394
		return $affected_rows;
	}
395

396
	// ----------------------------------------------------------------------------------------------------------
397

398
399
	//* Get record details
	public function sites_web_domain_get($session_id, $primary_id)
400
	{
401
		global $app;
402

403
404
405
406
407
		if(!$this->checkPerm($session_id, 'sites_web_domain_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
Marius Cramer's avatar
Marius Cramer committed
408
		$app->remoting_lib->loadFormDef('../sites/form/web_vhost_domain.tform.php');
409
410
		return $app->remoting_lib->getDataRecord($primary_id);
	}
411

412
413
414
415
416
417
418
419
	//* Add a record
	public function sites_web_domain_add($session_id, $client_id, $params, $readonly = false)
	{
		global $app;
		if(!$this->checkPerm($session_id, 'sites_web_domain_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
420

421
		if(!isset($params['client_group_id']) or (isset($params['client_group_id']) && empty($params['client_group_id']))) {
422
			$rec = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ?", $client_id);
423
424
			$params['client_group_id'] = $rec['groupid'];
		}
425

426
427
428
429
		//* Set a few params to "not empty" values which get overwritten by the sites_web_domain_plugin
		if($params['document_root'] == '') $params['document_root'] = '-';
		if($params['system_user'] == '') $params['system_user'] = '-';
		if($params['system_group'] == '') $params['system_group'] = '-';
430

431
432
433
434
435
		//* Set a few defaults for nginx servers
		if($params['pm_max_children'] == '') $params['pm_max_children'] = 1;
		if($params['pm_start_servers'] == '') $params['pm_start_servers'] = 1;
		if($params['pm_min_spare_servers'] == '') $params['pm_min_spare_servers'] = 1;
		if($params['pm_max_spare_servers'] == '') $params['pm_max_spare_servers'] = 1;
436

437
		$domain_id = $this->insertQuery('../sites/form/web_vhost_domain.tform.php', $client_id, $params, 'sites:web_vhost_domain:on_after_insert');
438
		if ($readonly === true)
439
			$app->db->query("UPDATE web_domain SET `sys_userid` = '1' WHERE domain_id = ?", $domain_id);
440
441
442
		return $domain_id;
	}

443
444
	//* Update a record
	public function sites_web_domain_update($session_id, $client_id, $primary_id, $params)
445
	{
446
447
448
449
		if(!$this->checkPerm($session_id, 'sites_web_domain_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
450

451
452
453
454
455
		//* Set a few defaults for nginx servers
		if($params['pm_max_children'] == '') $params['pm_max_children'] = 1;
		if($params['pm_start_servers'] == '') $params['pm_start_servers'] = 1;
		if($params['pm_min_spare_servers'] == '') $params['pm_min_spare_servers'] = 1;
		if($params['pm_max_spare_servers'] == '') $params['pm_max_spare_servers'] = 1;
456

Marius Cramer's avatar
Marius Cramer committed
457
		$affected_rows = $this->updateQuery('../sites/form/web_vhost_domain.tform.php', $client_id, $primary_id, $params);
458
459
		return $affected_rows;
	}
460

461
462
	//* Delete a record
	public function sites_web_domain_delete($session_id, $primary_id)
463
	{
464
465
466
467
		if(!$this->checkPerm($session_id, 'sites_web_domain_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
Marius Cramer's avatar
Marius Cramer committed
468
		$affected_rows = $this->deleteQuery('../sites/form/web_vhost_domain.tform.php', $primary_id);
469
470
		return $affected_rows;
	}
471

472
	// ----------------------------------------------------------------------------------------------------------
473

Dominik's avatar
Dominik committed
474
475
476
477
478
	//* Get record details
	public function sites_web_vhost_aliasdomain_get($session_id, $primary_id)
	{
		global $app;

479
		if(!$this->checkPerm($session_id, 'sites_web_aliasdomain_get')) {
Dominik's avatar
Dominik committed
480
481
482
483
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
484
		$app->remoting_lib->loadFormDef('../sites/form/web_vhost_domain.tform.php');
Dominik's avatar
Dominik committed
485
486
487
488
489
490
491
		return $app->remoting_lib->getDataRecord($primary_id);
	}

	//* Add a record
	public function sites_web_vhost_aliasdomain_add($session_id, $client_id, $params)
	{
		global $app;
492
		if(!$this->checkPerm($session_id, 'sites_web_aliasdomain_add')) {
Dominik's avatar
Dominik committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}

		//* Set a few params to "not empty" values which get overwritten by the sites_web_domain_plugin
		if($params['document_root'] == '') $params['document_root'] = '-';
		if($params['system_user'] == '') $params['system_user'] = '-';
		if($params['system_group'] == '') $params['system_group'] = '-';

		//* Set a few defaults for nginx servers
		if($params['pm_max_children'] == '') $params['pm_max_children'] = 1;
		if($params['pm_start_servers'] == '') $params['pm_start_servers'] = 1;
		if($params['pm_min_spare_servers'] == '') $params['pm_min_spare_servers'] = 1;
		if($params['pm_max_spare_servers'] == '') $params['pm_max_spare_servers'] = 1;

508
		$domain_id = $this->insertQuery('../sites/form/web_vhost_domain.tform.php', $client_id, $params, 'sites:web_vhost_aliasdomain:on_after_insert');
Dominik's avatar
Dominik committed
509
510
511
512
513
514
		return $domain_id;
	}

	//* Update a record
	public function sites_web_vhost_aliasdomain_update($session_id, $client_id, $primary_id, $params)
	{
515
		if(!$this->checkPerm($session_id, 'sites_web_aliasdomain_update')) {
Dominik's avatar
Dominik committed
516
517
518
519
520
521
522
523
524
525
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}

		//* Set a few defaults for nginx servers
		if($params['pm_max_children'] == '') $params['pm_max_children'] = 1;
		if($params['pm_start_servers'] == '') $params['pm_start_servers'] = 1;
		if($params['pm_min_spare_servers'] == '') $params['pm_min_spare_servers'] = 1;
		if($params['pm_max_spare_servers'] == '') $params['pm_max_spare_servers'] = 1;

526
		$affected_rows = $this->updateQuery('../sites/form/web_vhost_domain.tform.php', $client_id, $primary_id, $params, 'sites:web_vhost_aliasdomain:on_after_insert');
Dominik's avatar
Dominik committed
527
528
529
530
531
532
		return $affected_rows;
	}

	//* Delete a record
	public function sites_web_vhost_aliasdomain_delete($session_id, $primary_id)
	{
533
		if(!$this->checkPerm($session_id, 'sites_web_aliasdomain_delete')) {
Dominik's avatar
Dominik committed
534
535
536
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
537
		$affected_rows = $this->deleteQuery('../sites/form/web_vhost_domain.tform.php', $primary_id);
Dominik's avatar
Dominik committed
538
539
540
541
542
		return $affected_rows;
	}

	// ----------------------------------------------------------------------------------------------------------

543
544
	//* Get record details
	public function sites_web_vhost_subdomain_get($session_id, $primary_id)
545
	{
546
		global $app;
547

548
		if(!$this->checkPerm($session_id, 'sites_web_subdomain_get')) {
549
550
551
552
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
553
		$app->remoting_lib->loadFormDef('../sites/form/web_vhost_domain.tform.php');
554
555
		return $app->remoting_lib->getDataRecord($primary_id);
	}
556

557
558
559
560
	//* Add a record
	public function sites_web_vhost_subdomain_add($session_id, $client_id, $params)
	{
		global $app;
561
		if(!$this->checkPerm($session_id, 'sites_web_subdomain_add')) {
562
563
564
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
565

566
567
568
569
		//* Set a few params to "not empty" values which get overwritten by the sites_web_domain_plugin
		if($params['document_root'] == '') $params['document_root'] = '-';
		if($params['system_user'] == '') $params['system_user'] = '-';
		if($params['system_group'] == '') $params['system_group'] = '-';
570

571
572
573
574
575
		//* Set a few defaults for nginx servers
		if($params['pm_max_children'] == '') $params['pm_max_children'] = 1;
		if($params['pm_start_servers'] == '') $params['pm_start_servers'] = 1;
		if($params['pm_min_spare_servers'] == '') $params['pm_min_spare_servers'] = 1;
		if($params['pm_max_spare_servers'] == '') $params['pm_max_spare_servers'] = 1;
576

577
		$domain_id = $this->insertQuery('../sites/form/web_vhost_domain.tform.php', $client_id, $params, 'sites:web_vhost_subdomain:on_after_insert');
578
579
580
		return $domain_id;
	}

581
582
	//* Update a record
	public function sites_web_vhost_subdomain_update($session_id, $client_id, $primary_id, $params)
583
	{
584
		if(!$this->checkPerm($session_id, 'sites_web_subdomain_update')) {
585
586
587
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
588

589
590
591
592
593
		//* Set a few defaults for nginx servers
		if($params['pm_max_children'] == '') $params['pm_max_children'] = 1;
		if($params['pm_start_servers'] == '') $params['pm_start_servers'] = 1;
		if($params['pm_min_spare_servers'] == '') $params['pm_min_spare_servers'] = 1;
		if($params['pm_max_spare_servers'] == '') $params['pm_max_spare_servers'] = 1;
594

595
		$affected_rows = $this->updateQuery('../sites/form/web_vhost_domain.tform.php', $client_id, $primary_id, $params, 'sites:web_vhost_subdomain:on_after_insert');
596
597
		return $affected_rows;
	}
598

599
600
	//* Delete a record
	public function sites_web_vhost_subdomain_delete($session_id, $primary_id)
601
	{
602
		if(!$this->checkPerm($session_id, 'sites_web_subdomain_delete')) {
603
604
605
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
606
		$affected_rows = $this->deleteQuery('../sites/form/web_vhost_domain.tform.php', $primary_id);
607
608
		return $affected_rows;
	}
609

610
	// -----------------------------------------------------------------------------------------------
611

612
613
	//* Get record details
	public function sites_web_aliasdomain_get($session_id, $primary_id)
614
	{
615
		global $app;
616

617
618
619
620
621
		if(!$this->checkPerm($session_id, 'sites_web_aliasdomain_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
622
		$app->remoting_lib->loadFormDef('../sites/form/web_childdomain.tform.php');
623
624
		return $app->remoting_lib->getDataRecord($primary_id);
	}
625

626
627
	//* Add a record
	public function sites_web_aliasdomain_add($session_id, $client_id, $params)
628
	{
629
630
631
632
		if(!$this->checkPerm($session_id, 'sites_web_aliasdomain_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
633
		return $this->insertQuery('../sites/form/web_childdomain.tform.php', $client_id, $params);
634
	}
635

636
637
	//* Update a record
	public function sites_web_aliasdomain_update($session_id, $client_id, $primary_id, $params)
638
	{
639
640
641
642
		if(!$this->checkPerm($session_id, 'sites_web_aliasdomain_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
643
		$affected_rows = $this->updateQuery('../sites/form/web_childdomain.tform.php', $client_id, $primary_id, $params);
644
645
		return $affected_rows;
	}
646

647
648
	//* Delete a record
	public function sites_web_aliasdomain_delete($session_id, $primary_id)
649
	{
650
651
652
653
		if(!$this->checkPerm($session_id, 'sites_web_aliasdomain_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
654
		$affected_rows = $this->deleteQuery('../sites/form/web_childdomain.tform.php', $primary_id);
655
656
		return $affected_rows;
	}
657

658
	// ----------------------------------------------------------------------------------------------------------
659

660
661
	//* Get record details
	public function sites_web_subdomain_get($session_id, $primary_id)
662
	{
663
		global $app;
664

665
666
667
668
669
		if(!$this->checkPerm($session_id, 'sites_web_subdomain_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
670
		$app->remoting_lib->loadFormDef('../sites/form/web_childdomain.tform.php');
671
672
		return $app->remoting_lib->getDataRecord($primary_id);
	}
673

674
675
	//* Add a record
	public function sites_web_subdomain_add($session_id, $client_id, $params)
676
	{
677
678
679
680
		if(!$this->checkPerm($session_id, 'sites_web_subdomain_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
681
		return $this->insertQuery('../sites/form/web_childdomain.tform.php', $client_id, $params);
682
	}
683

684
685
	//* Update a record
	public function sites_web_subdomain_update($session_id, $client_id, $primary_id, $params)
686
	{
687
688
689
690
		if(!$this->checkPerm($session_id, 'sites_web_subdomain_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
691
		$affected_rows = $this->updateQuery('../sites/form/web_childdomain.tform.php', $client_id, $primary_id, $params);
692
693
		return $affected_rows;
	}
694

695
696
	//* Delete a record
	public function sites_web_subdomain_delete($session_id, $primary_id)
697
	{
698
699
700
701
		if(!$this->checkPerm($session_id, 'sites_web_subdomain_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
702
		$affected_rows = $this->deleteQuery('../sites/form/web_childdomain.tform.php', $primary_id);
703
704
		return $affected_rows;
	}
705

706
	// ----------------------------------------------------------------------------------------------------------
707

708
709
	//* Get record details
	public function sites_web_folder_get($session_id, $primary_id)
710
	{
711
		global $app;
712

713
714
715
716
717
718
719
720
		if(!$this->checkPerm($session_id, 'sites_web_folder_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
		$app->remoting_lib->loadFormDef('../sites/form/web_folder.tform.php');
		return $app->remoting_lib->getDataRecord($primary_id);
	}
721

722
723
	//* Add a record
	public function sites_web_folder_add($session_id, $client_id, $params)
724
	{
725
726
727
728
		if(!$this->checkPerm($session_id, 'sites_web_folder_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
729
		return $this->insertQuery('../sites/form/web_folder.tform.php', $client_id, $params);
730
	}
731

732
733
	//* Update a record
	public function sites_web_folder_update($session_id, $client_id, $primary_id, $params)
734
	{
735
736
737
738
		if(!$this->checkPerm($session_id, 'sites_web_folder_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
739
		$affected_rows = $this->updateQuery('../sites/form/web_folder.tform.php', $client_id, $primary_id, $params);
740
741
		return $affected_rows;
	}
742

743
744
	//* Delete a record
	public function sites_web_folder_delete($session_id, $primary_id)
745
	{
746
747
748
749
750
		global $app;
		if(!$this->checkPerm($session_id, 'sites_web_folder_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
751
752

		// Delete all users that belong to this folder. - taken from web_folder_delete.php
753
		$records = $app->db->queryAllRecords("SELECT web_folder_user_id FROM web_folder_user WHERE web_folder_id = ?", $primary_id);
754
		foreach($records as $rec) {
755
			$this->deleteQuery('../sites/form/web_folder_user.tform.php', $rec['web_folder_user_id']);
756
757
758
			//$app->db->datalogDelete('web_folder_user','web_folder_user_id',$rec['web_folder_user_id']);
		}
		unset($records);
759
760

		$affected_rows = $this->deleteQuery('../sites/form/web_folder.tform.php', $primary_id);
761
762
		return $affected_rows;
	}
763

764
	// -----------------------------------------------------------------------------------------------
765

766
767
	//* Get record details
	public function sites_web_folder_user_get($session_id, $primary_id)
768
	{
769
		global $app;
770

771
772
773
774
775
776
777
778
		if(!$this->checkPerm($session_id, 'sites_web_folder_user_get')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$app->uses('remoting_lib');
		$app->remoting_lib->loadFormDef('../sites/form/web_folder_user.tform.php');
		return $app->remoting_lib->getDataRecord($primary_id);
	}
779

780
781
	//* Add a record
	public function sites_web_folder_user_add($session_id, $client_id, $params)
782
	{
783
784
785
786
		if(!$this->checkPerm($session_id, 'sites_web_folder_user_add')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
787
		return $this->insertQuery('../sites/form/web_folder_user.tform.php', $client_id, $params);
788
	}
789

790
791
	//* Update a record
	public function sites_web_folder_user_update($session_id, $client_id, $primary_id, $params)
792
	{
793
794
795
796
		if(!$this->checkPerm($session_id, 'sites_web_folder_user_update')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
797
		$affected_rows = $this->updateQuery('../sites/form/web_folder_user.tform.php', $client_id, $primary_id, $params);
798
799
		return $affected_rows;
	}
800

801
802
	//* Delete a record
	public function sites_web_folder_user_delete($session_id, $primary_id)
803
	{
804
805
806
807
		if(!$this->checkPerm($session_id, 'sites_web_folder_user_delete')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
808
		$affected_rows = $this->deleteQuery('../sites/form/web_folder_user.tform.php', $primary_id);
809
810
811
812
813
		return $affected_rows;
	}

	/**
	 * Gets sites by $sys_userid & $sys_groupid
814
815
816
817
818
	 * @param int  session id
	 * @param int  user id
	 * @param array list of groups
	 * @return mixed array with sites by user
	 * @author Julio Montoya <gugli100@gmail.com> BeezNest 2010
819
	 */
820
821


822
	public function client_get_sites_by_user($session_id, $sys_userid, $sys_groupid) {
823
824
825
826
827
828
829
830
831
		global $app;
		if(!$this->checkPerm($session_id, 'client_get_sites_by_user')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		$sys_userid  = $app->functions->intval($sys_userid);
		$sys_groupid = explode(',', $sys_groupid);
		$new_group = array();
		foreach($sys_groupid as $group_id) {
832
			$new_group[] = $app->functions->intval( $group_id);
833
834
		}
		$group_list = implode(',', $new_group);
835
		$sql ="SELECT domain, domain_id, document_root, active FROM web_domain WHERE ( (sys_userid = $sys_userid  AND sys_perm_user LIKE '%r%') OR (sys_groupid IN ($group_list) AND sys_perm_group LIKE '%r%') OR  sys_perm_other LIKE '%r%') AND type = 'vhost'";
836
837
		$result = $app->db->queryAllRecords($sql);
		if(isset($result)) {
838
			return $result;
839
		} else {
840
841
			throw new SoapFault('no_client_found', 'There is no site for this user');
			return false;
842
843
844
845
846
847
848
849
850
851
852
853
		}
	}



	/**
	 * Change domains status
	 * @param int  session id
	 * @param int  site id
	 * @param string active or inactive string
	 * @return mixed false if error
	 * @author Julio Montoya <gugli100@gmail.com> BeezNest 2010
854
	 */
855
856
857
858
859
860
861
862
863
864
865
866
	public function sites_web_domain_set_status($session_id, $primary_id, $status) {
		global $app;
		if(!$this->checkPerm($session_id, 'sites_web_domain_set_status')) {
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
		}
		if(in_array($status, array('active', 'inactive'))) {
			if ($status == 'active') {
				$status = 'y';
			} else {
				$status = 'n';
			}
Marius Cramer's avatar
Marius Cramer committed
867
			$app->remoting_lib->loadFormDef('../sites/form/web_vhost_domain.tform.php');
868
869
870
			$params = $app->remoting_lib->getDataRecord($primary_id);
			$params['active'] = $status;
			
Marius Cramer's avatar
Marius Cramer committed
871
			$affected_rows = $this->updateQuery('../sites/form/web_vhost_domain.tform.php', 0, $primary_id, $params);
872
			return $affected_rows;
873
		} else {
874
875
			throw new SoapFault('status_undefined', 'The status is not available');
			return false;
876
		}
877
878
	}

879
880
881
882
	/**
	 * Get all databases by user
	 * @author Julio Montoya <gugli100@gmail.com> BeezNest 2010
	 */
883
	public function sites_database_get_all_by_user($session_id, $client_id)
884
885
	{
		global $app;
886
		if(!$this->checkPerm($session_id, 'sites_database_get')) {
887
888
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
			return false;
889
		}
890
		$client_id = $app->functions->intval($client_id);
891
892
		$sql = "SELECT d.database_id, d.database_name, d.database_user_id, d.database_ro_user_id, du.database_user, du.database_password FROM web_database d LEFT JOIN web_database_user du ON (du.database_user_id = d.database_user_id) INNER JOIN sys_user s on(d.sys_groupid = s.default_group) WHERE client_id = ?";
		$all = $app->db->queryAllRecords($sql, $client_id);
893
		return $all;
894
	}
895
896
897
898
899
900
901
	
	//** backup functions -----------------------------------------------------------------------------------
	public function sites_web_domain_backup_list($session_id, $site_id = null)
	{
		global $app;
	
		if(!$this->checkPerm($session_id, 'sites_web_domain_backup')) {
902
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
903
904
905
			return false;
		}
		
906
		$result = $app->db->queryAllRecords("SELECT * FROM web_backup".(($site_id != null)?' WHERE parent_domain_id = ?':''), $site_id);
907
908
909
910
911
912
913
914
915
		return $result;
	}
	
	//* Backup download and restoration by Abdi Joseph
	public function sites_web_domain_backup($session_id, $primary_id, $action_type)
	{
		global $app;
	
		if(!$this->checkPerm($session_id, 'sites_web_domain_backup')) {
916
			throw new SoapFault('permission_denied', 'You do not have the permissions to access this function.');
917
918
919
920
921
922
923
924
925
926
927
928
929
			return false;
		}
	
		//*Set variables
		$backup_record = $app->db->queryOneRecord("SELECT * FROM `web_backup` WHERE `backup_id`= ?", $primary_id);
		$server_id = $backup_record['server_id'];
	
		//*Set default action state
		$action_state = "pending";
		$tstamp = time();
	
		//* Basic validation of variables
		if ($server_id <= 0) {
930
			throw new SoapFault('invalid_backup_id', "Invalid or non existant backup_id $primary_id");
931
932
933
			return false;
		}
	
Dominik's avatar
Dominik committed
934
		if ($action_type != 'backup_download' and $action_type != 'backup_restore' and $action_type != 'backup_delete') {
935
			throw new SoapFault('invalid_action', "Invalid action_type $action_type");
936
937
938
939
940
941
			return false;
		}
	
		//* Validate instance
		$instance_record = $app->db->queryOneRecord("SELECT * FROM `sys_remoteaction` WHERE `action_param`= ? and `action_type`= ? and `action_state`= ?", $primary_id, $action_type, 'pending');
		if ($instance_record['action_id'] >= 1) {
942
			throw new SoapFault('duplicate_action', "There is already a pending $action_type action");
943
944
945
946
947
948
949
950
951
952
953
			return false;
		}
	
		//* Save the record
		if ($app->db->query("INSERT INTO `sys_remoteaction` SET `server_id` = ?, `tstamp` = ?, `action_type` = ?, `action_param` = ?, `action_state` = ?", $server_id, $tstamp, $action_type, $primary_id, $action_state)) {
			return true;
		} else {
			return false;
		}
	}
	
954
	//** quota functions -----------------------------------------------------------------------------------
955
	public function quota_get_by_user($session_id, $client_id)
956
957
958
959
960
	{
		global $app;
		$app->uses('quota_lib');
	
		if(!$this->checkPerm($session_id, 'quota_get_by_user')) {