tbrehm
2009-09-03 5421462b39a5b41014569af59d3861b9ffe8a44f
commit | author | age
d77ca6 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
542146 29
T 30 --UPDATED 08.2009--
31 Full SOAP support for ISPConfig 3.1.4 b
32 Updated by Arkadiusz Roch & Artur Edelman
33 Copyright (c) Tri-Plex technology
34
d77ca6 35 */
T 36
37 /**
38 * Formularbehandlung
39 *
40 * Funktionen zur Umwandlung von Formulardaten
41 * sowie zum vorbereiten von HTML und SQL
42 * Ausgaben
43 *
44 *        Tabellendefinition
45 *
46 *        Datentypen:
47 *        - INTEGER (Wandelt Ausdrücke in Int um)
48 *        - DOUBLE
49 *        - CURRENCY (Formatiert Zahlen nach Währungsnotation)
50 *        - VARCHAR (kein weiterer Format Check)
51 *        - DATE (Datumsformat, Timestamp Umwandlung)
52 *
53 *        Formtype:
54 *        - TEXT (normales Textfeld)
55 *        - PASSWORD (Feldinhalt wird nicht angezeigt)
56 *        - SELECT (Gibt Werte als option Feld aus)
57 *        - MULTIPLE (Select-Feld mit nehreren Werten)
58 *
59 *        VALUE:
60 *        - Wert oder Array
61 *
62 *        SEPARATOR
63 *        - Trennzeichen für multiple Felder
64 *
65 *        Hinweis:
66 *        Das ID-Feld ist nicht bei den Table Values einzufügen.
67 */
68
69 class remoting_lib {
542146 70     
d77ca6 71         /**
T 72         * Definition of the database atble (array)
73         * @var tableDef
74         */
75         private $tableDef;
76
77         /**
78         * Private
79         * @var action
80         */
81         private $action;
82
83         /**
84         * Table name (String)
85         * @var table_name
86         */
87         private $table_name;
88
89         /**
90         * Debug Variable
91         * @var debug
92         */
93         private $debug = 0;
94
95         /**
96         * name of the primary field of the database table (string)
97         * @var table_index
98         */
99         var $table_index;
100
101         /**
102         * contains the error messages
103         * @var errorMessage
104         */
105         var $errorMessage = '';
106
107         var $dateformat = "d.m.Y";
108         var $formDef = array();
109         var $wordbook;
110         var $module;
111         var $primary_id;
112         var $diffrec = array();
113         
114         var $sys_username;
115         var $sys_userid;
116         var $sys_default_group;
117         var $sys_groups;
118
119         
120         //* Load the form definition from file.
121         function loadFormDef($file) {
122             global $app,$conf;
123             
124             include_once($file);
125                 
126             $this->formDef = $form;
127             unset($this->formDef['tabs']);
128                 
129             //* Copy all fields from all tabs into one form definition
130             foreach($form['tabs'] as $tab) {
131                 foreach($tab['fields'] as $key => $value) {
132                     $this->formDef['fields'][$key] = $value;
133                 }
134             }
135             unset($form);
136                 
137             return true;
138         }
139         
140         //* Load the user profile
141         function loadUserProfile($client_id = 0) {
142             global $app,$conf;
143             
144             $client_id = intval($client_id);
145             
146             if($client_id == 0) {
147                 $this->sys_username         = 'admin';
148                 $this->sys_userid            = 1;
149                 $this->sys_default_group     = 1;
150                 $this->sys_groups            = 1;
151             } else {
152                 //* Load the client data
153                 $client = $app->db->queryOneRecord("SELECT username FROM client WHERE client_id = $client_id");
154                 if($client["username"] == '') {
155                     $this->errorMessage .= 'No client with ID $client_id found.';
156                     return false;
157                 }
158                 //* load system user
159                 $user = $app->db->queryOneRecord("SELECT * FROM sys_user WHERE username = '".$app->db->quote($client["username"])."'");
160                 if(empty($user["userid"])) {
161                     $this->errorMessage .= 'No user with the username '.$client['username'].' found.';
162                     return false;
163                 }
164                 $this->sys_username         = $user['username'];
165                 $this->sys_userid            = $user['userid'];
166                 $this->sys_default_group     = $user['default_group'];
167                 $this->sys_groups             = $user['groups'];
168             }
169             
170             return true;
171             
172         }
173
174
175         /**
176         * Converts data in human readable form
177         *
178         * @param record
179         * @return record
180         */
181         function decode($record) {
182                 $new_record = '';
183                 if(is_array($record)) {
184                         foreach($this->formDef['fields'] as $key => $field) {
185                                 switch ($field['datatype']) {
186                                 case 'VARCHAR':
187                                         $new_record[$key] = stripslashes($record[$key]);
188                                 break;
189
190                                 case 'TEXT':
191                                         $new_record[$key] = stripslashes($record[$key]);
192                                 break;
193
194                                 case 'DATE':
195                                         if($record[$key] > 0) {
196                                                 $new_record[$key] = date($this->dateformat,$record[$key]);
197                                         }
198                                 break;
199
200                                 case 'INTEGER':
201                                         $new_record[$key] = intval($record[$key]);
202                                 break;
203
204                                 case 'DOUBLE':
205                                         $new_record[$key] = $record[$key];
206                                 break;
207
208                                 case 'CURRENCY':
209                                         $new_record[$key] = number_format($record[$key], 2, ',', '');
210                                 break;
211
212                                 default:
213                                         $new_record[$key] = stripslashes($record[$key]);
214                                 }
215                         }
216
217                 }
218                 
219         return $new_record;
220         }
221
222         /**
223         * Get the key => value array of a form filled from a datasource definitiom
224         *
225         * @param field = array with field definition
226         * @param record = Dataset as array
227         * @return key => value array for the value field of a form
228         */
229
230         function getDatasourceData($field, $record) {
231                 global $app;
232
233                 $values = array();
234
235                 if($field["datasource"]["type"] == 'SQL') {
236
237                         // Preparing SQL string. We will replace some
238                         // common placeholders
239                         $querystring = $field["datasource"]["querystring"];
240                         $querystring = str_replace("{USERID}",$this->sys_userid,$querystring);
241                         $querystring = str_replace("{GROUPID}",$this->sys_default_group,$querystring);
242                         $querystring = str_replace("{GROUPS}",$this->sys_groups,$querystring);
243                         $table_idx = $this->formDef['db_table_idx'];
244                         
245                         $tmp_recordid = (isset($record[$table_idx]))?$record[$table_idx]:0;
246                         $querystring = str_replace("{RECORDID}",$tmp_recordid,$querystring);
247                         unset($tmp_recordid);
248                         
249                         $querystring = str_replace("{AUTHSQL}",$this->getAuthSQL('r'),$querystring);
250
251                         // Getting the records
252                         $tmp_records = $app->db->queryAllRecords($querystring);
253                         if($app->db->errorMessage != '') die($app->db->errorMessage);
254                         if(is_array($tmp_records)) {
255                                 $key_field = $field["datasource"]["keyfield"];
256                                 $value_field = $field["datasource"]["valuefield"];
257                                 foreach($tmp_records as $tmp_rec) {
258                                         $tmp_id = $tmp_rec[$key_field];
259                                         $values[$tmp_id] = $tmp_rec[$value_field];
260                                 }
261                         }
262                 }
263
264                 if($field["datasource"]["type"] == 'CUSTOM') {
265                         // Calls a custom class to validate this record
266                         if($field["datasource"]['class'] != '' and $field["datasource"]['function'] != '') {
267                                 $datasource_class = $field["datasource"]['class'];
268                                 $datasource_function = $field["datasource"]['function'];
269                                 $app->uses($datasource_class);
270                                 $values = $app->$datasource_class->$datasource_function($field, $record);
271                         } else {
272                                 $this->errorMessage .= "Custom datasource class or function is empty<br>\r\n";
273                         }
274                 }
275
276                 return $values;
277
278         }
279
280         /**
281         * Converts the data in a format to store it in the database table
282         *
283         * @param record = Datensatz als Array
284         * @return record
285         */
286         function encode($record) {
287
288                 if(is_array($record)) {
289                         foreach($this->formDef['fields'] as $key => $field) {
290
291                                 if(isset($field['validators']) && is_array($field['validators'])) $this->validateField($key, (isset($record[$key]))?$record[$key]:'', $field['validators']);
292
293                                 switch ($field['datatype']) {
294                                 case 'VARCHAR':
295                                         if(!@is_array($record[$key])) {
8500be 296                                                 $new_record[$key] = (isset($record[$key]))?mysql_real_escape_string($record[$key]):'';
d77ca6 297                                         } else {
T 298                                                 $new_record[$key] = implode($field['separator'],$record[$key]);
299                                         }
300                                 break;
301                                 case 'TEXT':
302                                         if(!is_array($record[$key])) {
8500be 303                                                 $new_record[$key] = mysql_real_escape_string($record[$key]);
d77ca6 304                                         } else {
T 305                                                 $new_record[$key] = implode($field['separator'],$record[$key]);
306                                         }
307                                 break;
308                                 case 'DATE':
309                                         if($record[$key] > 0) {
310                                                 list($tag,$monat,$jahr) = explode('.',$record[$key]);
311                                                 $new_record[$key] = mktime(0,0,0,$monat,$tag,$jahr);
312                                         } else {
313                                             $new_record[$key] = 0;
314                                         }
315                                 break;
316                                 case 'INTEGER':
317                                         $new_record[$key] = (isset($record[$key]))?intval($record[$key]):0;
318                                         //if($new_record[$key] != $record[$key]) $new_record[$key] = $field['default'];
319                                         //if($key == 'refresh') die($record[$key]);
320                                 break;
321                                 case 'DOUBLE':
8500be 322                                         $new_record[$key] = mysql_real_escape_string($record[$key]);
d77ca6 323                                 break;
T 324                                 case 'CURRENCY':
325                                         $new_record[$key] = str_replace(",",".",$record[$key]);
326                                 break;
327                                 }
328
329                                 // The use of the field value is deprecated, use validators instead
330                                 if(isset($field['regex']) && $field['regex'] != '') {
331                                         // Enable that "." matches also newlines
332                                         $field['regex'] .= 's';
333                                         if(!preg_match($field['regex'], $record[$key])) {
334                                                 $errmsg = $field['errmsg'];
335                                                 $this->errorMessage .= $errmsg."\r\n";
336                                         }
337                                 }
338
339
340                         }
341                 }
342                 return $new_record;
343         }
344
345         /**
346         * process the validators for a given field.
347         *
348         * @param field_name = Name of the field
349         * @param field_value = value of the field
350         * @param validatoors = Array of validators
351         * @return record
352         */
353
354         function validateField($field_name, $field_value, $validators) {
355
356                 global $app;
357                 
358                 $escape = '`';
359                 
360                 // loop trough the validators
361                 foreach($validators as $validator) {
362
363                         switch ($validator['type']) {
364                                 case 'REGEX':
365                                         $validator['regex'] .= 's';
366                                         if(!preg_match($validator['regex'], $field_value)) {
367                                                 $errmsg = $validator['errmsg'];
368                                                 if(isset($this->wordbook[$errmsg])) {
369                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br>\r\n";
370                                                 } else {
371                                                     $this->errorMessage .= $errmsg."<br>\r\n";
372                                                 }
373                                         }
374                                 break;
375                                 case 'UNIQUE':
376                                         if($this->action == 'NEW') {
377                                                 $num_rec = $app->db->queryOneRecord("SELECT count(*) as number FROM ".$escape.$this->formDef['db_table'].$escape. " WHERE $field_name = '".$app->db->quote($field_value)."'");
378                                                 if($num_rec["number"] > 0) {
379                                                         $errmsg = $validator['errmsg'];
380                                                         if(isset($this->wordbook[$errmsg])) {
381                                                             $this->errorMessage .= $this->wordbook[$errmsg]."<br>\r\n";
382                                                         } else {
383                                                             $this->errorMessage .= $errmsg."<br>\r\n";
384                                                         }
385                                                 }
386                                         } else {
387                                                 $num_rec = $app->db->queryOneRecord("SELECT count(*) as number FROM ".$escape.$this->formDef['db_table'].$escape. " WHERE $field_name = '".$app->db->quote($field_value)."' AND ".$this->formDef['db_table_idx']." != ".$this->primary_id);
388                                                 if($num_rec["number"] > 0) {
389                                                         $errmsg = $validator['errmsg'];
390                                                         if(isset($this->wordbook[$errmsg])) {
391                                                             $this->errorMessage .= $this->wordbook[$errmsg]."<br>\r\n";
392                                                         } else {
393                                                             $this->errorMessage .= $errmsg."<br>\r\n";
394                                                         }
395                                                 }
396                                         }
397                                 break;
398                                 case 'NOTEMPTY':
399                                         if(empty($field_value)) {
400                                                 $errmsg = $validator['errmsg'];
401                                                 if(isset($this->wordbook[$errmsg])) {
402                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br>\r\n";
403                                                 } else {
404                                                     $this->errorMessage .= $errmsg."<br>\r\n";
405                                                 }
406                                         }
407                                 break;
408                                 case 'ISEMAIL':
409                                         if(!preg_match("/^\w+[\w.-]*\w+@\w+[\w.-]*\w+\.[a-z]{2,10}$/i", $field_value)) {
410                                                 $errmsg = $validator['errmsg'];
411                                                 if(isset($this->wordbook[$errmsg])) {
412                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br>\r\n";
413                                                 } else {
414                                                     $this->errorMessage .= $errmsg."<br>\r\n";
415                                                 }
416                                         }
417                                 break;
418                                 case 'ISINT':
419                                         $tmpval = intval($field_value);
420                                         if($tmpval === 0 and !empty($field_value)) {
421                                                 $errmsg = $validator['errmsg'];
422                                                 if(isset($this->wordbook[$errmsg])) {
423                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br>\r\n";
424                                                 } else {
425                                                     $this->errorMessage .= $errmsg."<br>\r\n";
426                                                 }
427                                         }
428                                 break;
429                                 case 'ISPOSITIVE':
430                                         if(!is_numeric($field_value) || $field_value <= 0){
431                                           $errmsg = $validator['errmsg'];
432                                           if(isset($this->wordbook[$errmsg])) {
433                                              $this->errorMessage .= $this->wordbook[$errmsg]."<br>\r\n";
434                                           } else {
435                                              $this->errorMessage .= $errmsg."<br>\r\n";
436                                           }
437                                         }
438                                 break;
439                                 case 'CUSTOM':
440                                         // Calls a custom class to validate this record
441                                         if($validator['class'] != '' and $validator['function'] != '') {
442                                                 $validator_class = $validator['class'];
443                                                 $validator_function = $validator['function'];
444                                                 $app->uses($validator_class);
445                                                 $this->errorMessage .= $app->$validator_class->$validator_function($field_name, $field_value, $validator);
446                                         } else {
447                                                 $this->errorMessage .= "Custom validator class or function is empty<br>\r\n";
448                                         }
449                                 break;
450                                 default:
451                                     $this->errorMessage .= "Unknown Validator: ".$validator['type'];
452                                 break;
453                         }
454
455
456                 }
457
458                 return true;
459         }
460
461         /**
462         * Create SQL statement
463         *
464         * @param record = Datensatz als Array
465         * @param action = INSERT oder UPDATE
466         * @param primary_id
467         * @return record
468         */
469         function getSQL($record, $action = 'INSERT', $primary_id = 0, $sql_ext_where = '') {
470
471                 global $app;
472
473                 $this->action = $action;
474                 $this->primary_id = $primary_id;
475
476                 $record = $this->encode($record,$tab);
477                 $sql_insert_key = '';
478                 $sql_insert_val = '';
479                 $sql_update = '';
480
481                 if(!is_array($this->formDef)) $app->error("No form definition found.");
482
483                 // gehe durch alle Felder des Tabs
484                 if(is_array($record)) {
485                 foreach($this->formDef['fields'] as $key => $field) {
486                                 // Wenn es kein leeres Passwortfeld ist
487                                 if (!($field['formtype'] == 'PASSWORD' and $record[$key] == '')) {
488                                         // Erzeuge Insert oder Update Quelltext
489                                         if($action == "INSERT") {
490                                                 if($field['formtype'] == 'PASSWORD') {
491                                                         $sql_insert_key .= "`$key`, ";
492                                                         if($field['encryption'] == 'CRYPT') {
493                                                                 $salt="$1$";
494                                                                 for ($n=0;$n<8;$n++) {
495                                                                     $salt.=chr(mt_rand(64,126));
496                                                                 }
497                                                                 $salt.="$";
498                                                                 // $salt = substr(md5(time()),0,2);
499                                                                 $record[$key] = crypt($record[$key],$salt);
500                                                         } else {
501                                                                 $record[$key] = md5($record[$key]);
502                                                         }
503                                                         $sql_insert_val .= "'".$record[$key]."', ";
504                                                 } elseif ($field['formtype'] == 'CHECKBOX') {
505                                                         $sql_insert_key .= "`$key`, ";
506                                                         if($record[$key] == '') {
507                                                             // if a checkbox is not set, we set it to the unchecked value
508                                                             $sql_insert_val .= "'".$field['value'][0]."', ";
509                                                             $record[$key] = $field['value'][0];
510                                                         } else {
511                                                             $sql_insert_val .= "'".$record[$key]."', ";
512                                                         }
513                                                 } else {
514                                                         $sql_insert_key .= "`$key`, ";
515                                                         $sql_insert_val .= "'".$record[$key]."', ";
516                                                 }
517                                         } else {
518                                                 if($field['formtype'] == 'PASSWORD') {
519                                                         if($field['encryption'] == 'CRYPT') {
520                                                                 $salt="$1$";
521                                                                 for ($n=0;$n<8;$n++) {
522                                                                     $salt.=chr(mt_rand(64,126));
523                                                                 }
524                                                                 $salt.="$";
525                                                                 // $salt = substr(md5(time()),0,2);
526                                                                 $record[$key] = crypt($record[$key],$salt);
527                                                         } else {
528                                                                 $record[$key] = md5($record[$key]);
529                                                         }
530                                                         $sql_update .= "`$key` = '".$record[$key]."', ";
531                                                 } elseif ($field['formtype'] == 'CHECKBOX') {
532                                                         if($record[$key] == '') {
533                                                             // if a checkbox is not set, we set it to the unchecked value
534                                                             $sql_update .= "`$key` = '".$field['value'][0]."', ";
535                                                             $record[$key] = $field['value'][0];
536                                                         } else {
537                                                             $sql_update .= "`$key` = '".$record[$key]."', ";
538                                                         }
539                                                 } else {
540                                                         $sql_update .= "`$key` = '".$record[$key]."', ";
541                                                 }
542                                         }
543                                 } else {
544                                     // we unset the password filed, if empty to tell the datalog function 
545                                     // that the password has not been changed
546                                     unset($record[$key]);
547                                 }
548                         }
549         }
550
551
552
553                 if(stristr($this->formDef['db_table'],'.')) {
554                         $escape = '';
555                 } else {
556                         $escape = '`';
557                 }
558
559
560                 if($action == "INSERT") {
561                         if($this->formDef['auth'] == 'yes') {
562                                 // Setze User und Gruppe
563                                 $sql_insert_key .= "`sys_userid`, ";
564                                 $sql_insert_val .= ($this->formDef["auth_preset"]["userid"] > 0)?"'".$this->formDef["auth_preset"]["userid"]."', ":"'".$this->sys_userid."', ";
565                                 $sql_insert_key .= "`sys_groupid`, ";
566                                 $sql_insert_val .= ($this->formDef["auth_preset"]["groupid"] > 0)?"'".$this->formDef["auth_preset"]["groupid"]."', ":"'".$this->sys_default_group."', ";
567                                 $sql_insert_key .= "`sys_perm_user`, ";
568                                 $sql_insert_val .= "'".$this->formDef["auth_preset"]["perm_user"]."', ";
569                                 $sql_insert_key .= "`sys_perm_group`, ";
570                                 $sql_insert_val .= "'".$this->formDef["auth_preset"]["perm_group"]."', ";
571                                 $sql_insert_key .= "`sys_perm_other`, ";
572                                 $sql_insert_val .= "'".$this->formDef["auth_preset"]["perm_other"]."', ";
573                         }
574                         $sql_insert_key = substr($sql_insert_key,0,-2);
575                         $sql_insert_val = substr($sql_insert_val,0,-2);
576                         $sql = "INSERT INTO ".$escape.$this->formDef['db_table'].$escape." ($sql_insert_key) VALUES ($sql_insert_val)";
577                 } else {
578                         if($primary_id != 0) {
579                                 $sql_update = substr($sql_update,0,-2);
580                                 $sql = "UPDATE ".$escape.$this->formDef['db_table'].$escape." SET ".$sql_update." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
581                                 if($sql_ext_where != '') $sql .= " and ".$sql_ext_where;
582                         } else {
583                                 $app->error("Primary ID fehlt!");
584                         }
585                 }
586                 
587                 return $sql;
588         }
92bea0 589         
T 590         function getDeleteSQL($primary_id) {
591             
592             if(stristr($this->formDef['db_table'],'.')) {
593                 $escape = '';
594             } else {
595                 $escape = '`';
596             }
597             
598             $sql = "DELETE FROM ".$escape.$this->formDef['db_table'].$escape." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
599             return $sql;
600         }
d77ca6 601
T 602
603         function getDataRecord($primary_id) {
604             global $app;
605             $escape = '`';
606             $sql = "SELECT * FROM ".$escape.$this->formDef['db_table'].$escape." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
607             return $app->db->queryOneRecord($sql);
608         }
542146 609
T 610         function dodaj_usera($params,$insert_id){
611             global $app,$sql1;
612             $username = $params["username"];
613             $password = $params["password"];
614             $modules = 'mail,sites,dns,tools';
615             $startmodule = 'mail';
616             $usertheme = $params["usertheme"];
617             $type = 'user';
618             $active = 1;
619             $language = $params["language"];
620             $groupid = $app->db->datalogInsert('sys_group', "(name,description,client_id) VALUES ('$username','','$insert_id')", 'groupid');
621             $groups = $groupid;
622             $sql1 = "INSERT INTO sys_user (username,passwort,modules,startmodule,app_theme,typ,active,language,groups,default_group,client_id)
623             VALUES ('$username',md5('$password'),'$modules','$startmodule','$usertheme','$type','$active','$language',$groups,$groupid,$insert_id)";
624             $app->db->query($sql1);
625         }
d77ca6 626
T 627         function datalogSave($action,$primary_id, $record_old, $record_new) {
628                 global $app,$conf;
629
630                 if(stristr($this->formDef['db_table'],'.')) {
631                         $escape = '';
632                 } else {
633                         $escape = '`';
634                 }
635
636                 $diffrec = array();
637                 
638                 if(is_array($record_new) && count($record_new) > 0) {
639                         foreach($record_new as $key => $val) {
640                                 if($record_old[$key] != $val) {
641                                         // Record has changed
642                                         $diffrec[$key] = array('old' => $record_old[$key],
643                                                                'new' => $val);
644                                 }
645                         }
646                 } elseif(is_array($record_old)) {
647                         foreach($record_old as $key => $val) {
648                                 if($record_new[$key] != $val) {
649                                         // Record has changed
650                                         $diffrec[$key] = array('new' => $record_new[$key],
651                                                                'old' => $val);
652                                 }
653                         }
654                 }
655                 $this->diffrec = $diffrec;
656                 
657                 
658                 // Full diff records for ISPConfig, they have a different format then the simple diffrec
659                 $diffrec_full = array();
660
661                 if(is_array($record_old) && count($record_old) > 0) {
662                         foreach($record_old as $key => $val) {
663                                 if(isset($record_new[$key]) && $record_new[$key] != $val) {
664                                     // Record has changed
665                                     $diffrec_full['old'][$key] = $val;
666                                     $diffrec_full['new'][$key] = $record_new[$key];
667                                 } else {
668                                     $diffrec_full['old'][$key] = $val;
669                                     $diffrec_full['new'][$key] = $val;
670                                 }
671                         }
672                 } elseif(is_array($record_new)) {
673                         foreach($record_new as $key => $val) {
674                                 if(isset($record_new[$key]) && $record_old[$key] != $val) {
675                                     // Record has changed
676                                     $diffrec_full['new'][$key] = $val;
677                                     $diffrec_full['old'][$key] = $record_old[$key];
678                                 } else {
679                                     $diffrec_full['new'][$key] = $val;
680                                     $diffrec_full['old'][$key] = $val;
681                                 }
682                         }
683                 }
684                 
685                 /*
686                 echo "<pre>";
687                 print_r($diffrec_full);
688                 echo "</pre>";
689                 */
690                 
691                 // Insert the server_id, if the record has a server_id
692                 $server_id = (isset($record_old["server_id"]) && $record_old["server_id"] > 0)?$record_old["server_id"]:0;
693                 if(isset($record_new["server_id"])) $server_id = $record_new["server_id"];
694
695                 if(count($this->diffrec) > 0) {
696                         $diffstr = $app->db->quote(serialize($diffrec_full));
697                         $username = $app->db->quote($this->sys_username);
698                         $dbidx = $this->formDef['db_table_idx'].":".$primary_id;
699                         // $action = ($action == 'INSERT')?'i':'u';
700                         
701                         if($action == 'INSERT') $action = 'i';
702                         if($action == 'UPDATE') $action = 'u';
703                         if($action == 'DELETE') $action = 'd';
704                         $sql = "INSERT INTO sys_datalog (dbtable,dbidx,server_id,action,tstamp,user,data) VALUES ('".$this->formDef['db_table']."','$dbidx','$server_id','$action','".time()."','$username','$diffstr')";
705                         $app->db->query($sql);
706                 }
707
708                 return true;
709
710         }
711
712 }
713
542146 714 ?>