Skip to content

Commit d77ca66

Browse files
committed
Improved the remoting interface.
Added a demo client to test the remoting framework.
1 parent ac89150 commit d77ca66

File tree

4 files changed

+867
-23
lines changed

4 files changed

+867
-23
lines changed

interface/lib/classes/remoting.inc.php

Lines changed: 107 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,31 @@ class remoting {
55
//* remote session timeout in seconds
66
private $session_timeout = 600;
77

8-
private $app;
8+
private $server;
9+
10+
/*
11+
These variables shall stay global.
12+
Please do not make them private variables.
13+
14+
private $app;
915
private $conf;
10-
private $server;
16+
*/
1117

1218
public function __construct()
1319
{
14-
global $app, $conf, $server;
20+
global $server;
1521
$this->server = $server;
22+
/*
1623
$this->app = $app;
1724
$this->conf = $conf;
25+
*/
1826
}
1927

2028
//* remote login function
2129
public function login($username, $password)
2230
{
31+
global $app, $conf, $server;
32+
2333
if(empty($username)) {
2434
$this->server->fault('login_username_empty', 'The login username is empty');
2535
return false;
@@ -30,11 +40,11 @@ public function login($username, $password)
3040
return false;
3141
}
3242

33-
$username = $this->app->db->quote($username);
34-
$password = $this->app->db->quote($password);
43+
$username = $app->db->quote($username);
44+
$password = $app->db->quote($password);
3545

3646
$sql = "SELECT * FROM remote_user WHERE remote_username = '$username' and remote_password = md5('$password')";
37-
$remote_user = $this->app->db->queryOneRecord($sql);
47+
$remote_user = $app->db->queryOneRecord($sql);
3848
if($remote_user['remote_userid'] > 0) {
3949
//* Create a remote user session
4050
srand ((double)microtime()*1000000);
@@ -45,7 +55,7 @@ public function login($username, $password)
4555
$sql = 'INSERT INTO remote_session (remote_session,remote_userid,remote_functions,tstamp'
4656
.') VALUES ('
4757
." '$remote_session',$remote_userid,'$remote_functions',$tstamp)";
48-
$this->app->db->query($sql);
58+
$app->db->query($sql);
4959
return $remote_session;
5060
} else {
5161
$this->server->fault('login_failed', 'The login failed. Username or password wrong.');
@@ -54,44 +64,117 @@ public function login($username, $password)
5464

5565
}
5666

57-
5867
//* remote logout function
5968
public function logout($session_id)
6069
{
70+
global $app;
71+
6172
if(empty($session_id)) {
6273
$this->server->fault('session_id_empty', 'The SessionID is empty.');
6374
return false;
6475
}
6576

66-
$session_id = $this->app->db->quote($session_id);
77+
$session_id = $app->db->quote($session_id);
6778

6879
$sql = "DELETE FROM remote_session WHERE remote_session = '$session_id'";
69-
$this->app->db->query($sql);
70-
return ($this->app->db->affectedRows() == 1);
80+
$app->db->query($sql);
81+
return ($app->db->affectedRows() == 1);
7182
}
7283

73-
public function mail_domain_add($session_id, $params)
84+
85+
public function mail_domain_add($session_id, $client_id, $params)
7486
{
7587
if(!$this->checkPerm($session_id, 'mail_domain_add')) {
7688
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
7789
return false;
7890
}
79-
80-
//* Form definition file, that is used for this table in the interafce
81-
$formdef = '../mail/form/mail_domain.tform.php';
82-
83-
//* check the variables against the form definition and build the sql query automatically.
84-
// I will use a modified version of the tform class for this.
85-
91+
$domain_id = $this->insertQuery('../mail/form/mail_domain.tform.php',$client_id,$params);
92+
return $domain_id;
93+
}
94+
95+
public function mail_domain_update($session_id, $client_id, $domain_id, $params)
96+
{
97+
if(!$this->checkPerm($session_id, 'mail_domain_update')) {
98+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
99+
return false;
100+
}
101+
$domain_id = $this->updateQuery('../mail/form/mail_domain.tform.php',$client_id,$domain_id,$params);
102+
return $domain_id;
86103
}
87104

88105

89106

90107
//** private functions -----------------------------------------------------------------------------------
91108

92-
private function updateQuery($formdef, $params)
109+
private function updateQuery($formdef_file, $client_id, $primary_id, $params)
93110
{
111+
global $app;
112+
113+
$app->uses('remoting_lib');
114+
115+
//* Load the form definition
116+
$app->remoting_lib->loadFormDef($formdef_file);
117+
118+
//* load the user profile of the client
119+
$app->remoting_lib->loadUserProfile($client_id);
120+
121+
//* Get the SQL query
122+
$sql = $app->remoting_lib->getSQL($params,'UPDATE',$primary_id);
123+
if($app->remoting_lib->errorMessage != '') {
124+
$this->server->fault('data_processing_error', $app->remoting_lib->errorMessage);
125+
return false;
126+
}
127+
128+
$app->db->query($sql);
129+
130+
if($app->db->errorMessage != '') {
131+
$this->server->fault('database_error', $app->db->errorMessage . ' '.$sql);
132+
return false;
133+
}
134+
135+
$affected_rows = $app->db->affectedRows();
136+
137+
//* TODO: Save changes to Datalog
138+
139+
140+
141+
return $affected_rows;
142+
}
94143

144+
private function insertQuery($formdef_file, $client_id, $params)
145+
{
146+
global $app;
147+
148+
$app->uses('remoting_lib');
149+
150+
//* Load the form definition
151+
$app->remoting_lib->loadFormDef($formdef_file);
152+
153+
//* load the user profile of the client
154+
$app->remoting_lib->loadUserProfile($client_id);
155+
156+
//* Get the SQL query
157+
$sql = $app->remoting_lib->getSQL($params,'INSERT',0);
158+
if($app->remoting_lib->errorMessage != '') {
159+
$this->server->fault('data_processing_error', $app->remoting_lib->errorMessage);
160+
return false;
161+
}
162+
163+
$app->db->query($sql);
164+
165+
if($app->db->errorMessage != '') {
166+
$this->server->fault('database_error', $app->db->errorMessage . ' '.$sql);
167+
return false;
168+
}
169+
170+
$insert_id = $app->db->insertID();
171+
172+
//* TODO: Save changes to Datalog
173+
174+
175+
176+
177+
return $insert_id
95178
}
96179

97180

@@ -107,16 +190,18 @@ private function checkPerm($session_id, $function_name)
107190

108191
private function getSession($session_id)
109192
{
193+
global $app;
194+
110195
if(empty($session_id)) {
111196
$this->server->fault('session_id_empty','The SessionID is empty.');
112197
return false;
113198
}
114199

115-
$session_id = $this->app->db->quote($session_id);
200+
$session_id = $app->db->quote($session_id);
116201

117202
$now = time();
118203
$sql = "SELECT * FROM remote_session WHERE remote_session = '$session_id' AND tstamp >= $now";
119-
$session = $this->app->db->queryOneRecord($sql);
204+
$session = $app->db->queryOneRecord($sql);
120205
if($session['remote_userid'] > 0) {
121206
return $session;
122207
} else {

0 commit comments

Comments
 (0)