tbrehm
2013-08-02 2332b2279d8a8599b4f041370315edc9544b1560
commit | author | age
2dadd2 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
2332b2 10     * Redistributions of source code must retain the above copyright notice,
T 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.
2dadd2 18
T 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.
29 */
30
31 /**
32 * Formularbehandlung
33 *
34 * Functions to validate, display and save form values
35 *
36 *        Database table field definitions
37 *
38 *        Datatypes:
39 *        - INTEGER (Converts data to int automatically)
40 *        - DOUBLE
41 *        - CURRENCY (Formats digits in currency notation)
42 *        - VARCHAR (No format check)
d7ef36 43 *        - DATE (Date format, converts from and to UNIX timestamps automatically)
2dadd2 44 *
T 45 *        Formtype:
46 *        - TEXT (Normal text field)
47 *        - PASSWORD (password field, the content will not be displayed again to the user)
48 *        - SELECT (Option fiield)
49 *        - MULTIPLE (Allows selection of multiple values)
50 *
51 *        VALUE:
52 *        - Value or array
53 *
54 *        SEPARATOR
55 *        - separator char used for fileds with multiple values
56 *
65ea2e 57 *        Hint: The auto increment (ID) filed of the table has not be be definied separately.
2dadd2 58 *
T 59 */
60
61 class tform {
62
2332b2 63         /**
T 64         * Definition of the database table (array)
65         * @var tableDef
66         */
67         var $tableDef;
2dadd2 68
2332b2 69         /**
T 70         * Private
71         * @var action
72         */
73         var $action;
2dadd2 74
2332b2 75         /**
T 76         * Table name (String)
77         * @var table_name
78         */
79         var $table_name;
2dadd2 80
2332b2 81         /**
T 82         * Debug Variable
83         * @var debug
84         */
85         var $debug = 0;
2dadd2 86
2332b2 87         /**
T 88         * name of the primary field of the database table (string)
89         * @var table_index
90         */
91         var $table_index;
2dadd2 92
2332b2 93         /**
T 94         * contains the error messages
95         * @var errorMessage
96         */
97         var $errorMessage = '';
2dadd2 98
2332b2 99         var $dateformat = "d.m.Y";
T 100         var $formDef = array();
101         var $wordbook;
102         var $module;
103         var $primary_id;
2dadd2 104         var $diffrec = array();
T 105
2332b2 106         /**
T 107         * Loading of the table definition
108         *
109         * @param file: path to the form definition file
110         * @return true
111         */
112         /*
113         function loadTableDef($file) {
114                 global $app,$conf;
2dadd2 115
2332b2 116                 include_once($file);
T 117                 $this->tableDef = $table;
118                 $this->table_name = $table_name;
119                 $this->table_index = $table_index;
120                 return true;
121         }
122         */
2dadd2 123
2332b2 124     function loadFormDef($file,$module = '') {
T 125                 global $app,$conf;
2dadd2 126
2332b2 127                 include($file);
T 128                 $this->formDef = $form;
2dadd2 129
2332b2 130                 $this->module = $module;
2dadd2 131                 $wb = array();
2332b2 132
2dadd2 133                 include_once(ISPC_ROOT_PATH.'/lib/lang/'.$_SESSION['s']['language'].'.lng');
2332b2 134
40dd9f 135                 if(is_array($wb)) $wb_global = $wb;
2332b2 136
T 137                 if($module == '') {
2dadd2 138                     $lng_file = "lib/lang/".$_SESSION["s"]["language"]."_".$this->formDef["name"].".lng";
T 139                     if(!file_exists($lng_file)) $lng_file = "lib/lang/en_".$this->formDef["name"].".lng";
140                     include($lng_file);
2332b2 141                 } else {
2dadd2 142                     $lng_file = "../$module/lib/lang/".$_SESSION["s"]["language"]."_".$this->formDef["name"].".lng";
T 143                     if(!file_exists($lng_file)) $lng_file = "../$module/lib/lang/en_".$this->formDef["name"].".lng";
144                     include($lng_file);
2332b2 145                 }
b5a0dc 146
40dd9f 147                 if(is_array($wb_global)) {
5bbfc1 148                     $wb = $app->functions->array_merge($wb_global,$wb);
40dd9f 149                 }
T 150                 if(isset($wb_global)) unset($wb_global);
2332b2 151
T 152                 $this->wordbook = $wb;
153
b2673a 154                 $this->dateformat = $app->lng('conf_format_dateshort');
2dadd2 155
2332b2 156                 return true;
T 157         }
2dadd2 158
T 159
2332b2 160         /**
T 161         * Converts the data in the array to human readable format
162         * Datatype conversion e.g. to show the data in lists
163         *
164         * @param record
165         * @return record
166         */
167         function decode($record,$tab) {
168                 global $conf, $app;
1ca823 169                 if(!is_array($this->formDef['tabs'][$tab])) $app->error("Tab does not exist or the tab is empty (TAB: $tab).");
2332b2 170                 $new_record = '';
f9c7f3 171                 $table_idx = $this->formDef['db_table_idx'];
65ea2e 172                 if(isset($record[$table_idx])) $new_record[$table_idx] = $app->functions->intval($record[$table_idx ]);
2332b2 173
2dadd2 174                 if(is_array($record)) {
d370bb 175                         foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) {
2332b2 176
d370bb 177                                 //* Apply filter to record value.
T 178                                 if(isset($field['filters']) && is_array($field['filters'])) {
179                                     $record[$key] = $this->filterField($key, (isset($record[$key]))?$record[$key]:'', $field['filters'], 'SHOW');
180                                 }
2332b2 181
d370bb 182                                 switch ($field['datatype']) {
2332b2 183                                 case 'VARCHAR':
T 184                                         $new_record[$key] = $record[$key];
185                                 break;
2dadd2 186
2332b2 187                                 case 'TEXT':
T 188                                         $new_record[$key] = $record[$key];
189                                 break;
2dadd2 190
2332b2 191                                 case 'DATETSTAMP':
T 192                                         if($record[$key] > 0) {
193                                                 $new_record[$key] = date($this->dateformat,$record[$key]);
194                                         }
195                                 break;
196
57540e 197                                 case 'DATE':
2332b2 198                                         if($record[$key] != '' && $record[$key] != '0000-00-00') {
57540e 199                                                 $tmp = explode('-',$record[$key]);
2332b2 200                                                 $new_record[$key] = date($this->dateformat,mktime(0, 0, 0, $tmp[1]  , $tmp[2], $tmp[0]));
T 201                                         }
202                                 break;
2dadd2 203
2332b2 204                                 case 'INTEGER':
T 205                                         $new_record[$key] = $app->functions->intval($record[$key]);
206                                 break;
2dadd2 207
2332b2 208                                 case 'DOUBLE':
T 209                                         $new_record[$key] = $record[$key];
210                                 break;
2dadd2 211
2332b2 212                                 case 'CURRENCY':
T 213                                         $new_record[$key] = $app->functions->currency_format($record[$key]);
214                                 break;
2dadd2 215
2332b2 216                                 default:
T 217                                         $new_record[$key] = $record[$key];
218                                 }
219                         }
2dadd2 220
2332b2 221                 }
2dadd2 222
2332b2 223         return $new_record;
T 224         }
2dadd2 225
2332b2 226         /**
T 227         * Get the key => value array of a form filled from a datasource definitiom
228         *
229         * @param field = array with field definition
230         * @param record = Dataset as array
231         * @return key => value array for the value field of a form
232         */
2dadd2 233
2332b2 234         function getDatasourceData($field, $record) {
T 235                 global $app;
2dadd2 236
2332b2 237                 $values = array();
2dadd2 238
2332b2 239                 if($field["datasource"]["type"] == 'SQL') {
T 240
241                         // Preparing SQL string. We will replace some
242                         // common placeholders
243                         $querystring = $field["datasource"]["querystring"];
244                         $querystring = str_replace("{USERID}",$_SESSION["s"]["user"]["userid"],$querystring);
245                         $querystring = str_replace("{GROUPID}",$_SESSION["s"]["user"]["default_group"],$querystring);
246                         $querystring = str_replace("{GROUPS}",$_SESSION["s"]["user"]["groups"],$querystring);
247                         $table_idx = $this->formDef['db_table_idx'];
248
2dadd2 249                         $tmp_recordid = (isset($record[$table_idx]))?$record[$table_idx]:0;
2332b2 250                         $querystring = str_replace("{RECORDID}",$tmp_recordid,$querystring);
2dadd2 251                         unset($tmp_recordid);
T 252
2332b2 253                         $querystring = str_replace("{AUTHSQL}",$this->getAuthSQL('r'),$querystring);
2dadd2 254
2332b2 255                         // Getting the records
T 256                         $tmp_records = $app->db->queryAllRecords($querystring);
257                         if($app->db->errorMessage != '') die($app->db->errorMessage);
258                         if(is_array($tmp_records)) {
259                                 $key_field = $field["datasource"]["keyfield"];
260                                 $value_field = $field["datasource"]["valuefield"];
261                                 foreach($tmp_records as $tmp_rec) {
262                                         $tmp_id = $tmp_rec[$key_field];
263                                         $values[$tmp_id] = $tmp_rec[$value_field];
264                                 }
265                         }
266                 }
2dadd2 267
2332b2 268                 if($field["datasource"]["type"] == 'CUSTOM') {
T 269                         // Calls a custom class to validate this record
270                         if($field["datasource"]['class'] != '' and $field["datasource"]['function'] != '') {
271                                 $datasource_class = $field["datasource"]['class'];
272                                 $datasource_function = $field["datasource"]['function'];
273                                 $app->uses($datasource_class);
274                                 $values = $app->$datasource_class->$datasource_function($field, $record);
275                         } else {
276                                 $this->errorMessage .= "Custom datasource class or function is empty<br />\r\n";
277                         }
278                 }
279
280                 if(isset($field['filters']) && is_array($field['filters'])) {
281                     $new_values = array();
282                     foreach($values as $index => $value) {
283                         $new_index = $this->filterField($index, $index, $field['filters'], 'SHOW');
284                         $new_values[$new_index] = $this->filterField($index, (isset($values[$index]))?$values[$index]:'', $field['filters'], 'SHOW');
285                     }
286                     $values = $new_values;
287                     unset($new_values);
288                     unset($new_index);
289                 }
290
291                 return $values;
292
293         }
294
2dadd2 295         //* If the parameter 'valuelimit' is set
T 296         function applyValueLimit($limit,$values) {
2332b2 297
2dadd2 298             global $app;
2332b2 299
2dadd2 300             $limit_parts = explode(':',$limit);
2332b2 301
2dadd2 302             //* values are limited to a comma separated list
T 303             if($limit_parts[0] == 'list') {
304                 $allowed = explode(',',$limit_parts[1]);
305             }
2332b2 306
2dadd2 307             //* values are limited to a field in the client settings
T 308             if($limit_parts[0] == 'client') {
309                 if($_SESSION["s"]["user"]["typ"] == 'admin') {
310                     return $values;
311                 } else {
312                     $client_group_id = $_SESSION["s"]["user"]["default_group"];
313                     $client = $app->db->queryOneRecord("SELECT ".$limit_parts[1]." as lm FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
314                     $allowed = explode(',',$client['lm']);
315                 }
316             }
2332b2 317
67c4b3 318             //* values are limited to a field in the reseller settings
T 319             if($limit_parts[0] == 'reseller') {
320                 if($_SESSION["s"]["user"]["typ"] == 'admin') {
321                     return $values;
322                 } else {
323                     //* Get the limits of the client that is currently logged in
324                     $client_group_id = $_SESSION["s"]["user"]["default_group"];
325                     $client = $app->db->queryOneRecord("SELECT parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
326                     //echo "SELECT parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id";
327                     //* If the client belongs to a reseller, we will check against the reseller Limit too
328                     if($client['parent_client_id'] != 0) {
2332b2 329
67c4b3 330                         //* first we need to know the groups of this reseller
T 331                         $tmp = $app->db->queryOneRecord("SELECT userid, groups FROM sys_user WHERE client_id = ".$client['parent_client_id']);
332                         $reseller_groups = $tmp["groups"];
333                         $reseller_userid = $tmp["userid"];
2332b2 334
67c4b3 335                         // Get the limits of the reseller of the logged in client
T 336                         $client_group_id = $_SESSION["s"]["user"]["default_group"];
337                         $reseller = $app->db->queryOneRecord("SELECT ".$limit_parts[1]." as lm FROM client WHERE client_id = ".$client['parent_client_id']);
338                         $allowed = explode(',',$reseller['lm']);
339                     } else {
340                         return $values;
341                     }
342                 } // end if admin
343             } // end if reseller
2332b2 344
2dadd2 345             //* values are limited to a field in the system settings
T 346             if($limit_parts[0] == 'system') {
347                 $app->uses('getconf');
348                 $tmp_conf = $app->getconf->get_global_config($limit_parts[1]);
349                 $tmp_key = $limit_parts[2];
350                 $allowed = $tmp_conf[$tmp_key];
351             }
2332b2 352
2dadd2 353             $values_new = array();
T 354             foreach($values as $key => $val) {
355                 if(in_array($key,$allowed)) $values_new[$key] = $val;
356             }
2332b2 357
2dadd2 358             return $values_new;
T 359         }
360
361
2332b2 362         /**
T 363         * Prepare the data record to show the data in a form.
364         *
365         * @param record = Datensatz als Array
366         * @param action = NEW oder EDIT
367         * @return record
368         */
369         function getHTML($record, $tab, $action = 'NEW') {
2dadd2 370
2332b2 371                 global $app;
2dadd2 372
2332b2 373                 $this->action = $action;
2dadd2 374
2332b2 375                 if(!is_array($this->formDef)) $app->error("No form definition found.");
T 376                 if(!is_array($this->formDef['tabs'][$tab])) $app->error("The tab is empty or does not exist (TAB: $tab).");
2dadd2 377
2332b2 378                 $new_record = array();
T 379                 if($action == 'EDIT') {
380                         $record = $this->decode($record,$tab);
381                         if(is_array($record)) {
382                                 foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) {
383
384                                         if(isset($record[$key])) {
8cf78b 385                                             $val = $record[$key];
T 386                                         } else {
387                                             $val = '';
388                                         }
2dadd2 389
2332b2 390                                         // If Datasource is set, get the data from there
T 391                                         if(isset($field['datasource']) && is_array($field['datasource'])) {
418623 392                                                 if(is_array($field["value"])) {
9f56bd 393                                                     //$field["value"] = array_merge($field["value"],$this->getDatasourceData($field, $record));
5bbfc1 394                                                     $field["value"] = $app->functions->array_merge($field["value"],$this->getDatasourceData($field, $record));
418623 395                                                 } else {
2332b2 396                                                     $field["value"] = $this->getDatasourceData($field, $record);
418623 397                                                 }
2332b2 398                                         }
T 399
2dadd2 400                                         // If a limitation for the values is set
T 401                                         if(isset($field['valuelimit']) && is_array($field["value"])) {
402                                             $field["value"] = $this->applyValueLimit($field['valuelimit'],$field["value"]);
403                                         }
404
2332b2 405                                         switch ($field['formtype']) {
T 406                                         case 'SELECT':
2dadd2 407                                                 $out = '';
2332b2 408                                                 if(is_array($field['value'])) {
T 409                                                         foreach($field['value'] as $k => $v) {
410                                                                 $selected = ($k == $val)?' SELECTED':'';
9539ce 411                                                                 if(!empty($this->wordbook[$v]))
F 412                                                                     $v = $this->wordbook[$v];
2332b2 413                                                                 $out .= "<option value='$k'$selected>".$this->lng($v)."</option>\r\n";
T 414                                                         }
415                                                 }
416                                                 $new_record[$key] = $out;
417                                         break;
418                                         case 'MULTIPLE':
419                                                 if(is_array($field['value'])) {
2dadd2 420
2332b2 421                                                         // Split
T 422                                                         $vals = explode($field['separator'],$val);
2dadd2 423
2332b2 424                                                         // write HTML
T 425                                                         $out = '';
426                                                         foreach($field['value'] as $k => $v) {
2dadd2 427
2332b2 428                                                                 $selected = '';
T 429                                                                 foreach($vals as $tvl) {
430                                                                         if(trim($tvl) == trim($k)) $selected = ' SELECTED';
431                                                                 }
2dadd2 432
2332b2 433                                                                 $out .= "<option value='$k'$selected>$v</option>\r\n";
T 434                                                         }
435                                                 }
436                                                 $new_record[$key] = $out;
437                                         break;
2dadd2 438
2332b2 439                                         case 'PASSWORD':
T 440                                                 $new_record[$key] = '';
441                                         break;
2dadd2 442
2332b2 443                                         case 'CHECKBOX':
T 444                                                 $checked = ($val == $field['value'][1])?' CHECKED':'';
445                                                 $new_record[$key] = "<input name=\"".$key."\" id=\"".$key."\" value=\"".$field['value'][1]."\" type=\"checkbox\" $checked />\r\n";
446                                         break;
2dadd2 447
2332b2 448                                         case 'CHECKBOXARRAY':
T 449                                                 if(is_array($field['value'])) {
2dadd2 450
2332b2 451                                                         // aufsplitten ergebnisse
T 452                                                         $vals = explode($field['separator'],$val);
2dadd2 453
2332b2 454                                                         // HTML schreiben
T 455                                                         $out = '';
456                                                         $elementNo = 0;
457                                                         foreach($field['value'] as $k => $v) {
2dadd2 458
2332b2 459                                                                 $checked = '';
T 460                                                                 foreach($vals as $tvl) {
461                                                                         if(trim($tvl) == trim($k)) $checked = ' CHECKED';
462                                                                 }
463                                                                 // $out .= "<label for=\"".$key."[]\" class=\"inlineLabel\"><input name=\"".$key."[]\" id=\"".$key."[]\" value=\"$k\" type=\"checkbox\" $checked /> $v</label>\r\n";
5d0a76 464                                                                 $out .= "<label for=\"".$key.$elementNo."\" class=\"inlineLabel\"><input name=\"".$key."[]\" id=\"".$key.$elementNo."\" value=\"$k\" type=\"checkbox\" $checked /> $v</label><br/>\r\n";
2332b2 465                                                                 $elementNo++;
T 466                                                         }
467                                                 }
468                                                 $new_record[$key] = $out;
469                                         break;
2dadd2 470
2332b2 471                                         case 'RADIO':
T 472                                                 if(is_array($field['value'])) {
2dadd2 473
2332b2 474                                                         // HTML schreiben
T 475                                                         $out = '';
476                                                         $elementNo = 0;
477                                                         foreach($field['value'] as $k => $v) {
478                                                                 $checked = ($k == $val)?' CHECKED':'';
479                                                                 //$out .= "<label for=\"".$key."[]\" class=\"inlineLabel\"><input name=\"".$key."[]\" id=\"".$key."[]\" value=\"$k\" type=\"radio\" $checked/> $v</label>\r\n";
5d0a76 480                                                                 $out .= "<label for=\"".$key.$elementNo."\" class=\"inlineLabel\"><input name=\"".$key."[]\" id=\"".$key.$elementNo."\" value=\"$k\" type=\"radio\" $checked/> $v </label>\r\n";
2332b2 481                                                                 $elementNo++;
T 482                                                         }
483                                                 }
484                                                 $new_record[$key] = $out;
485                                         break;
2dadd2 486
2332b2 487                                         case 'DATETIME':
T 488                                                 if (strtotime($val) !== false) {
489                                                     $dt_value = $val;
490                                                 } elseif ( isset($field['default']) && (strtotime($field['default']) !== false) ) {
491                                                     $dt_value = $field['default'];
492                                                 } else {
493                                                     $dt_value = 0;
494                                                 }
495
496                                                 $display_seconds = (isset($field['display_seconds']) && $field['display_seconds'] == true) ? true : false;
497
498                                                 $new_record[$key] = $this->_getDateTimeHTML($key, $dt_value, $display_seconds);
499                                         break;
500
501                                         default:
8cf78b 502                                             if(isset($record[$key])) {
2332b2 503                                                 $new_record[$key] = htmlspecialchars($record[$key]);
8cf78b 504                                             } else {
T 505                                                 $new_record[$key] = '';
506                                             }
2332b2 507                                         }
T 508                                 }
509                         }
510                 } else {
511                         // Action: NEW
512                         foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) {
2dadd2 513
2332b2 514                                 // If Datasource is set, get the data from there
T 515                                 if(@is_array($field['datasource'])) {
516                                     if(is_array($field["value"])) {
5bbfc1 517                                         $field["value"] = $app->functions->array_merge($field["value"],$this->getDatasourceData($field, $record));
418623 518                                     } else {
2332b2 519                                         $field["value"] = $this->getDatasourceData($field, $record);
418623 520                                     }
2332b2 521                                 }
T 522
2dadd2 523                                 // If a limitation for the values is set
T 524                                 if(isset($field['valuelimit']) && is_array($field["value"])) {
525                                     $field["value"] = $this->applyValueLimit($field['valuelimit'],$field["value"]);
526                                 }
527
2332b2 528                                 switch ($field['formtype']) {
T 529                                 case 'SELECT':
530                                         if(is_array($field['value'])) {
531                                                 $out = '';
532                                                 foreach($field['value'] as $k => $v) {
533                                                     $selected = ($k == $field["default"])?' SELECTED':'';
534                                                     $out .= "<option value='$k'$selected>".$this->lng($v)."</option>\r\n";
535                                                 }
536                                         }
537                                         if(isset($out)) $new_record[$key] = $out;
538                                 break;
539                                 case 'MULTIPLE':
540                                                 if(is_array($field['value'])) {
2dadd2 541
2332b2 542                                                         // aufsplitten ergebnisse
T 543                                                         $vals = explode($field['separator'],$val);
2dadd2 544
2332b2 545                                                         // HTML schreiben
T 546                                                         $out = '';
547                                                         foreach($field['value'] as $k => $v) {
2dadd2 548
2332b2 549                                                                 $out .= "<option value='$k'>$v</option>\r\n";
T 550                                                         }
551                                                 }
552                                                 $new_record[$key] = $out;
553                                         break;
2dadd2 554
2332b2 555                                 case 'PASSWORD':
T 556                                         $new_record[$key] = '';
557                                 break;
2dadd2 558
2332b2 559                                 case 'CHECKBOX':
T 560                                         // $checked = (empty($field["default"]))?'':' CHECKED';
561                                                             $checked = ($field["default"] == $field['value'][1])?' CHECKED':'';
562                                         $new_record[$key] = "<input name=\"".$key."\" id=\"".$key."\" value=\"".$field['value'][1]."\" type=\"checkbox\" $checked />\r\n";
563                                 break;
2dadd2 564
2332b2 565                                 case 'CHECKBOXARRAY':
T 566                                         if(is_array($field['value'])) {
2dadd2 567
2332b2 568                                                 // aufsplitten ergebnisse
T 569                                                 $vals = explode($field['separator'],$field["default"]);
2dadd2 570
2332b2 571                                                 // HTML schreiben
T 572                                                 $out = '';
573                                                 $elementNo = 0;
574                                                 foreach($field['value'] as $k => $v) {
2dadd2 575
2332b2 576                                                         $checked = '';
T 577                                                         foreach($vals as $tvl) {
578                                                                 if(trim($tvl) == trim($k)) $checked = ' CHECKED';
579                                                         }
580                                                         // $out .= "<label for=\"".$key."[]\" class=\"inlineLabel\"><input name=\"".$key."[]\" id=\"".$key."[]\" value=\"$k\" type=\"checkbox\" $checked /> $v</label>\r\n";
5d0a76 581                                                         $out .= "<label for=\"".$key.$elementNo."\" class=\"inlineLabel\"><input name=\"".$key."[]\" id=\"".$key.$elementNo."\" value=\"$k\" type=\"checkbox\" $checked /> $v</label> &nbsp;\r\n";
2332b2 582                                                         $elementNo++;
T 583                                                 }
584                                         }
585                                         $new_record[$key] = $out;
586                                 break;
2dadd2 587
2332b2 588                                 case 'RADIO':
T 589                                         if(is_array($field['value'])) {
2dadd2 590
2332b2 591                                                 // HTML schreiben
T 592                                                 $out = '';
593                                                 $elementNo = 0;
594                                                 foreach($field['value'] as $k => $v) {
595                                                         $checked = ($k == $field["default"])?' CHECKED':'';
596                                                         //$out .= "<label for=\"".$key."[]\" class=\"inlineLabel\"><input name=\"".$key."[]\" id=\"".$key."[]\" value=\"$k\" type=\"radio\" $checked/> $v</label>\r\n";
5d0a76 597                                                         $out .= "<label for=\"".$key.$elementNo."\" class=\"inlineLabel\"><input name=\"".$key."[]\" id=\"".$key.$elementNo."\" value=\"$k\" type=\"radio\" $checked/> $v</label>\r\n";
2332b2 598                                                         $elementNo++;
T 599                                                 }
600                                         }
601                                         $new_record[$key] = $out;
602                                 break;
2dadd2 603
2332b2 604                                 case 'DATETIME':
T 605                                         $dt_value = (isset($field['default'])) ? $field['default'] : 0;
606                                         $display_seconds = (isset($field['display_seconds']) && $field['display_seconds'] == true) ? true : false;
2dadd2 607
2332b2 608                                         $new_record[$key] = $this->_getDateTimeHTML($key, $dt_value, $display_seconds);
T 609                                 break;
2dadd2 610
2332b2 611                                 default:
T 612                                         $new_record[$key] = htmlspecialchars($field['default']);
613                                 }
614                         }
2dadd2 615
2332b2 616                 }
2dadd2 617
2332b2 618                 if($this->debug == 1) $this->dbg($new_record);
T 619
620                 return $new_record;
621         }
622
623         /**
624         * Rewrite the record data to be stored in the database
625         * and check values with regular expressions.
626         *
627         * @param record = Datensatz als Array
628         * @return record
629         */
630         function encode($record,$tab,$dbencode = true) {
2dadd2 631             global $app;
T 632
2332b2 633                 if(!is_array($this->formDef['tabs'][$tab])) $app->error("Tab is empty or does not exist (TAB: $tab).");
T 634                 //$this->errorMessage = '';
635
636                 if(is_array($record)) {
637                         foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) {
638
d370bb 639                                 //* Apply filter to record value
2332b2 640                                 if(isset($field['filters']) && is_array($field['filters'])) {
d370bb 641                                     $record[$key] = $this->filterField($key, (isset($record[$key]))?$record[$key]:'', $field['filters'], 'SAVE');
T 642                                 }
643                                 //* Validate record value
644                                 if(isset($field['validators']) && is_array($field['validators'])) {
645                                     $this->validateField($key, (isset($record[$key]))?$record[$key]:'', $field['validators']);
646                                 }
2dadd2 647
2332b2 648                                 switch ($field['datatype']) {
T 649                                 case 'VARCHAR':
650                                         if(!@is_array($record[$key])) {
2b81b8 651                                                 $new_record[$key] = (isset($record[$key]))?$record[$key]:'';
2332b2 652                                         } else {
T 653                                                 $new_record[$key] = implode($field['separator'],$record[$key]);
654                                         }
655                                 break;
656                                 case 'TEXT':
657                                         if(!is_array($record[$key])) {
658                                                 $new_record[$key] = $record[$key];
659                                         } else {
660                                                 $new_record[$key] = implode($field['separator'],$record[$key]);
661                                         }
662                                 break;
663                                 case 'DATETSTAMP':
664                                         if($record[$key] > 0) {
665                                                 list($tag,$monat,$jahr) = explode('.',$record[$key]);
666                                                 $new_record[$key] = mktime(0,0,0,$monat,$tag,$jahr);
667                                         } else {
2dadd2 668                                             $new_record[$key] = 0;
T 669                                         }
2332b2 670                                 break;
57540e 671                                 case 'DATE':
2332b2 672                                         if($record[$key] != '' && $record[$key] != '0000-00-00') {
1ca823 673                                                 if(function_exists('date_parse_from_format')) {
T 674                                                     $date_parts = date_parse_from_format($this->dateformat,$record[$key]);
675                                                     //list($tag,$monat,$jahr) = explode('.',$record[$key]);
676                                                     $new_record[$key] = $date_parts['year'].'-'.$date_parts['month'].'-'.$date_parts['day'];
677                                                     //$tmp = strptime($record[$key],$this->dateformat);
678                                                     //$new_record[$key] = ($tmp['tm_year']+1900).'-'.($tmp['tm_mon']+1).'-'.$tmp['tm_mday'];
679                                                 } else {
680                                                     //$tmp = strptime($record[$key],$this->dateformat);
681                                                     //$new_record[$key] = ($tmp['tm_year']+1900).'-'.($tmp['tm_mon']+1).'-'.$tmp['tm_mday'];
682                                                     $tmp = strtotime($record[$key]);
683                                                     $new_record[$key] = date('Y-m-d',$tmp);
684                                                 }
2332b2 685                                         } else {
57540e 686                                             $new_record[$key] = '0000-00-00';
T 687                                         }
2332b2 688                                 break;
T 689                                 case 'INTEGER':
10b4c8 690                                         $new_record[$key] = (isset($record[$key]))?$app->functions->intval($record[$key]):0;
2332b2 691                                         //if($new_record[$key] != $record[$key]) $new_record[$key] = $field['default'];
T 692                                         //if($key == 'refresh') die($record[$key]);
693                                 break;
694                                 case 'DOUBLE':
695                                         $new_record[$key] = $record[$key];
696                                 break;
697                                 case 'CURRENCY':
698                                         $new_record[$key] = str_replace(",",".",$record[$key]);
699                                 break;
2dadd2 700
2332b2 701                                 case 'DATETIME':
T 702                                         if (is_array($record[$key]))
703                                         {
704                                             $filtered_values = array_map(create_function('$item','return (int)$item;'), $record[$key]);
705                                             extract($filtered_values, EXTR_PREFIX_ALL, '_dt');
706
707                                             if ($_dt_day != 0 && $_dt_month != 0 && $_dt_year != 0) {
708                                                 $new_record[$key] = date( 'Y-m-d H:i:s', mktime($_dt_hour, $_dt_minute, $_dt_second, $_dt_month, $_dt_day, $_dt_year) );
709                                             }
710                                         }
711                                 break;
712                                 }
713
714                                 // The use of the field value is deprecated, use validators instead
715                                 if(isset($field['regex']) && $field['regex'] != '') {
716                                         // Enable that "." matches also newlines
717                                         $field['regex'] .= 's';
718                                         if(!preg_match($field['regex'], $record[$key])) {
719                                                 $errmsg = $field['errmsg'];
720                                                 $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
721                                         }
722                                 }
723
2b81b8 724                                 //* Add slashes to all records, when we encode data which shall be inserted into mysql.
T 725                                 if($dbencode == true) $new_record[$key] = $app->db->quote($new_record[$key]);
2332b2 726                         }
T 727                 }
728                 return $new_record;
729         }
d370bb 730
2332b2 731         /**
T 732         * process the filters for a given field.
733         *
734         * @param field_name = Name of the field
735         * @param field_value = value of the field
736         * @param filters = Array of filters
737         * @param filter_event = 'SAVE'or 'SHOW'
738         * @return record
739         */
740
741         function filterField($field_name, $field_value, $filters, $filter_event) {
d370bb 742
T 743             global $app;
f1ee3a 744             $returnval = $field_value;
2332b2 745
d370bb 746             //* Loop trough all filters
T 747             foreach($filters as $filter) {
748                 if($filter['event'] == $filter_event) {
749                     switch ($filter['type']) {
750                         case 'TOLOWER':
8c1761 751                             $returnval = strtolower($returnval);
d370bb 752                         break;
T 753                         case 'TOUPPER':
8c1761 754                             $returnval = strtoupper($returnval);
d370bb 755                         break;
f1ee3a 756                         case 'IDNTOASCII':
8c1761 757                             $returnval = $app->functions->idn_encode($returnval);
f1ee3a 758                         break;
T 759                         case 'IDNTOUTF8':
8c1761 760                             $returnval = $app->functions->idn_decode($returnval);
f1ee3a 761                         break;
d370bb 762                         default:
T 763                             $this->errorMessage .= "Unknown Filter: ".$filter['type'];
764                         break;
765                     }
766                 }
767             }
2332b2 768             return $returnval;
T 769         }
2dadd2 770
2332b2 771         /**
T 772         * process the validators for a given field.
773         *
774         * @param field_name = Name of the field
775         * @param field_value = value of the field
776         * @param validatoors = Array of validators
777         * @return record
778         */
2dadd2 779
2332b2 780         function validateField($field_name, $field_value, $validators) {
2dadd2 781
2332b2 782                 global $app;
T 783
2dadd2 784                 $escape = '`';
T 785
2332b2 786                 // loop trough the validators
T 787                 foreach($validators as $validator) {
788
789                         switch ($validator['type']) {
790                                 case 'REGEX':
791                                         $validator['regex'] .= 's';
792                                         if(!preg_match($validator['regex'], $field_value)) {
793                                                 $errmsg = $validator['errmsg'];
794                                                 if(isset($this->wordbook[$errmsg])) {
795                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
2dadd2 796                                                 } else {
T 797                                                     $this->errorMessage .= $errmsg."<br />\r\n";
798                                                 }
2332b2 799                                         }
T 800                                 break;
801                                 case 'UNIQUE':
4bd960 802                                         if($validator['allowempty'] != 'y') $validator['allowempty'] = 'n';
T 803                                         if($validator['allowempty'] == 'n' || ($validator['allowempty'] == 'y' && $field_value != '')){
804                                             if($this->action == 'NEW') {
2332b2 805                                                 $num_rec = $app->db->queryOneRecord("SELECT count(*) as number FROM ".$escape.$this->formDef['db_table'].$escape. " WHERE $field_name = '".$app->db->quote($field_value)."'");
T 806                                                 if($num_rec["number"] > 0) {
807                                                         $errmsg = $validator['errmsg'];
2dadd2 808                                                         if(isset($this->wordbook[$errmsg])) {
2332b2 809                                                             $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
2dadd2 810                                                         } else {
T 811                                                             $this->errorMessage .= $errmsg."<br />\r\n";
812                                                         }
2332b2 813                                                 }
4bd960 814                                             } else {
2332b2 815                                                 $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);
T 816                                                 if($num_rec["number"] > 0) {
817                                                         $errmsg = $validator['errmsg'];
818                                                         if(isset($this->wordbook[$errmsg])) {
819                                                             $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
2dadd2 820                                                         } else {
T 821                                                             $this->errorMessage .= $errmsg."<br />\r\n";
822                                                         }
2332b2 823                                                 }
4bd960 824                                             }
T 825                                         }
2332b2 826                                 break;
T 827                                 case 'NOTEMPTY':
828                                         if(empty($field_value)) {
829                                                 $errmsg = $validator['errmsg'];
830                                                 if(isset($this->wordbook[$errmsg])) {
831                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
2dadd2 832                                                 } else {
T 833                                                     $this->errorMessage .= $errmsg."<br />\r\n";
834                                                 }
2332b2 835                                         }
T 836                                 break;
837                                 case 'ISEMAIL':
838                                     if(function_exists('filter_var')) {
55e220 839                                         if(filter_var($field_value, FILTER_VALIDATE_EMAIL) === false) {
61211e 840                                             $errmsg = $validator['errmsg'];
2332b2 841                                             if(isset($this->wordbook[$errmsg])) {
T 842                                                 $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
61211e 843                                             } else {
T 844                                                 $this->errorMessage .= $errmsg."<br />\r\n";
845                                             }
2332b2 846                                         }
61211e 847                                     } else {
T 848                                         if(!preg_match("/^\w+[\w\.\-\+]*\w{0,}@\w+[\w.-]*\w+\.[a-zA-Z0-9\-]{2,30}$/i", $field_value)) {
2332b2 849                                                 $errmsg = $validator['errmsg'];
T 850                                                 if(isset($this->wordbook[$errmsg])) {
851                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
2dadd2 852                                                 } else {
T 853                                                     $this->errorMessage .= $errmsg."<br />\r\n";
854                                                 }
2332b2 855                                         }
61211e 856                                     }
2332b2 857                                 break;
T 858                                 case 'ISINT':
10b4c8 859                                     if(function_exists('filter_var') && $field_value < 2147483647) {
55e220 860                                         if($field_value != '' && filter_var($field_value, FILTER_VALIDATE_INT) === false) {
F 861                                             $errmsg = $validator['errmsg'];
862                                             if(isset($this->wordbook[$errmsg])) {
2332b2 863                                                 $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
55e220 864                                             } else {
F 865                                                 $this->errorMessage .= $errmsg."<br />\r\n";
866                                             }
2332b2 867                                         }
55e220 868                                     } else {
2332b2 869                                         $tmpval = $app->functions->intval($field_value);
T 870                                         if($tmpval === 0 and !empty($field_value)) {
871                                                 $errmsg = $validator['errmsg'];
872                                                 if(isset($this->wordbook[$errmsg])) {
873                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
2dadd2 874                                                 } else {
T 875                                                     $this->errorMessage .= $errmsg."<br />\r\n";
876                                                 }
2332b2 877                                         }
55e220 878                                     }
2332b2 879                                 break;
T 880                                 case 'ISPOSITIVE':
881                                         if(!is_numeric($field_value) || $field_value <= 0){
882                                           $errmsg = $validator['errmsg'];
883                                           if(isset($this->wordbook[$errmsg])) {
884                                              $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
2dadd2 885                                           } else {
T 886                                              $this->errorMessage .= $errmsg."<br />\r\n";
30fc1b 887                                           }
2332b2 888                                         }
T 889                                 break;
30fc1b 890                 case 'ISV6PREFIX':
L 891                     $v6_prefix_ok = 0;
892                     $explode_field_value = explode(':',$field_value);
893                     if ($explode_field_value[count($explode_field_value)-1]=='' && $explode_field_value[count($explode_field_value)-2]=='' ){
2332b2 894                             if ( count($explode_field_value) <= 9 ) {
T 895                                     if(filter_var(substr($field_value,0,strlen($field_value)-2),FILTER_VALIDATE_IP,FILTER_FLAG_IPV6) or filter_var(substr($field_value,0,strlen($field_value)-2).'::0',FILTER_VALIDATE_IP,FILTER_FLAG_IPV6) or filter_var(substr($field_value,0,strlen($field_value)-2).':0',FILTER_VALIDATE_IP,FILTER_FLAG_IPV6) ) {
896                                             $v6_prefix_ok = 1;
897                                     }
898                             }
899                     } else {
900                         $v6_prefix_ok = 2;
30fc1b 901                     }
L 902                     // check subnet against defined server-ipv6
903                     $sql_v6 = $app->db->queryOneRecord("SELECT ip_address FROM server_ip WHERE ip_type = 'IPv6' AND virtualhost = 'y' LIMIT 0,1");
904                     $sql_v6_explode=explode(':',$sql_v6['ip_address']);
2332b2 905                     if ( count($sql_v6_explode) < count($explode_field_value) && isset($sql_v6['ip_address']) )  {
T 906                         $v6_prefix_ok = 3;
30fc1b 907                     }
2332b2 908                     if($v6_prefix_ok == 0) {
T 909                         $errmsg = $validator['errmsg'];
30fc1b 910                     }
2332b2 911                     if($v6_prefix_ok == 2) {
T 912                         $errmsg = 'IPv6 Prefix must end with ::';
30fc1b 913                     }
2332b2 914                     if($v6_prefix_ok == 3) {
T 915                         $errmsg = 'IPv6 Prefix too long (according to Server IP Addresses)';
30fc1b 916                     }
L 917                     if($v6_prefix_ok <> 1){
2332b2 918                         $this->errorMessage .= $errmsg."<br />\r\n";
30fc1b 919                     }
L 920                 break;
921                                 case 'ISIPV4':
922                                 $vip=1;
923                                 if(preg_match("/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/", $field_value)){
2dadd2 924                                 $groups=explode(".",$field_value);
T 925                                 foreach($groups as $group){
926                                     if($group<0 OR $group>255)
927                                     $vip=0;
928                                 }
929                                 }else{$vip=0;}
2332b2 930                                         if($vip==0) {
2dadd2 931                                         $errmsg = $validator['errmsg'];
2332b2 932                                           if(isset($this->wordbook[$errmsg])) {
T 933                                              $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
2dadd2 934                                           } else {
T 935                                              $this->errorMessage .= $errmsg."<br />\r\n";
936                                           }
937                                         }
2332b2 938                                 break;
a70698 939                                 case 'ISIP':
72695f 940                                 if($validator['allowempty'] != 'y') $validator['allowempty'] = 'n';
T 941                                 if($validator['allowempty'] == 'y' && $field_value == '') {
942                                     //* Do nothing
a70698 943                                 } else {
72695f 944                                 //* Check if its a IPv4 or IPv6 address
T 945                                     if(isset($validator['separator']) && $validator['separator'] != '') {
946                                         //* When the field may contain several IP addresses, split them by the char defined as separator
947                                         $field_value_array = explode($validator['separator'],$field_value);
948                                     } else {
949                                         $field_value_array[] = $field_value;
a70698 950                                     }
72695f 951                                     foreach($field_value_array as $field_value) {
615a0a 952                                         $field_value = trim($field_value);
72695f 953                                         if(function_exists('filter_var')) {
T 954                                             if(!filter_var($field_value,FILTER_VALIDATE_IP)) {
955                                                 $errmsg = $validator['errmsg'];
956                                                 if(isset($this->wordbook[$errmsg])) {
957                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
958                                                 } else {
959                                                     $this->errorMessage .= $errmsg."<br />\r\n";
960                                                 }
961                                             }
a70698 962                                         } else {
72695f 963                                             //* Check content with regex, if we use php < 5.2
T 964                                             $ip_ok = 0;
965                                             if(preg_match("/^(\:\:([a-f0-9]{1,4}\:){0,6}?[a-f0-9]{0,4}|[a-f0-9]{1,4}(\:[a-f0-9]{1,4}){0,6}?\:\:|[a-f0-9]{1,4}(\:[a-f0-9]{1,4}){1,6}?\:\:([a-f0-9]{1,4}\:){1,6}?[a-f0-9]{1,4})(\/\d{1,3})?$/i", $field_value)){
966                                                 $ip_ok = 1;
967                                             }
968                                             if(preg_match("/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/", $field_value)){
969                                                 $ip_ok = 1;
970                                             }
971                                             if($ip_ok == 0) {
972                                                 $errmsg = $validator['errmsg'];
973                                                 if(isset($this->wordbook[$errmsg])) {
974                                                     $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
975                                                 } else {
976                                                     $this->errorMessage .= $errmsg."<br />\r\n";
977                                                 }
978                                             }
a70698 979                                         }
T 980                                     }
981                                 }
2332b2 982                                 break;
b8fe56 983                                 case 'RANGE':
2332b2 984                                         //* Checks if the value is within the given range or above / below a value
b8fe56 985                                         //* Range examples: < 10 = ":10", between 2 and 10 = "2:10", above 5 = "5:".
T 986                                         $range_parts = explode(':',trim($validator['range']));
987                                         $ok = true;
2332b2 988                                         if($range_parts[0] != '' && $field_value < $range_parts[0]) {
b8fe56 989                                             $ok = false;
T 990                                         }
991                                         if($range_parts[1] != '' && $field_value > $range_parts[1]) {
992                                             $ok = false;
993                                         }
994                                         if($ok != true) {
995                                             $errmsg = $validator['errmsg'];
996                                             if(isset($this->wordbook[$errmsg])) {
997                                                 $this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
998                                             } else {
999                                                 $this->errorMessage .= $errmsg."<br />\r\n";
1000                                             }
2332b2 1001                                         }
b8fe56 1002                                         unset($range_parts);
2332b2 1003                                 break;
T 1004                                 case 'CUSTOM':
1005                                         // Calls a custom class to validate this record
1006                                         if($validator['class'] != '' and $validator['function'] != '') {
1007                                                 $validator_class = $validator['class'];
1008                                                 $validator_function = $validator['function'];
1009                                                 $app->uses($validator_class);
1010                                                 $this->errorMessage .= $app->$validator_class->$validator_function($field_name, $field_value, $validator);
1011                                         } else {
1012                                                 $this->errorMessage .= "Custom validator class or function is empty<br />\r\n";
1013                                         }
1014                                 break;
2dadd2 1015                                 default:
T 1016                                     $this->errorMessage .= "Unknown Validator: ".$validator['type'];
1017                                 break;
2332b2 1018                         }
2dadd2 1019
T 1020
2332b2 1021                 }
2dadd2 1022
2332b2 1023                 return true;
T 1024         }
2dadd2 1025
2332b2 1026         /**
T 1027         * Create SQL statement
1028         *
1029         * @param record = Datensatz als Array
1030         * @param action = INSERT oder UPDATE
1031         * @param primary_id
1032         * @return record
1033         */
1034         function getSQL($record, $tab, $action = 'INSERT', $primary_id = 0, $sql_ext_where = '') {
2dadd2 1035
2332b2 1036                 global $app;
2dadd2 1037
2332b2 1038                 // If there are no data records on the tab, return empty sql string
T 1039                 if(count($this->formDef['tabs'][$tab]['fields']) == 0) return '';
2dadd2 1040
2332b2 1041                 // checking permissions
T 1042                 if($this->formDef['auth'] == 'yes' && $_SESSION["s"]["user"]["typ"] != 'admin') {
1043                         if($action == "INSERT") {
1044                                 if(!$this->checkPerm($primary_id,'i')) $this->errorMessage .= "Insert denied.<br />\r\n";
1045                         } else {
1046                                 if(!$this->checkPerm($primary_id,'u')) $this->errorMessage .= "Update denied.<br />\r\n";
1047                         }
1048                 }
2dadd2 1049
2332b2 1050                 $this->action = $action;
T 1051                 $this->primary_id = $primary_id;
2dadd2 1052
2332b2 1053                 $record = $this->encode($record,$tab,true);
T 1054                 $sql_insert_key = '';
1055                 $sql_insert_val = '';
1056                 $sql_update = '';
2dadd2 1057
2332b2 1058                 if(!is_array($this->formDef)) $app->error("Form definition not found.");
T 1059                 if(!is_array($this->formDef['tabs'][$tab])) $app->error("The tab is empty or does not exist (TAB: $tab).");
2dadd2 1060
2332b2 1061                 // go trough all fields of the tab
T 1062                 if(is_array($record)) {
1063                 foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) {
1064                                 // Wenn es kein leeres Passwortfeld ist
1065                                 if (!($field['formtype'] == 'PASSWORD' and $record[$key] == '')) {
1066                                         // Erzeuge Insert oder Update Quelltext
1067                                         if($action == "INSERT") {
1068                                                 if($field['formtype'] == 'PASSWORD') {
1069                                                         $sql_insert_key .= "`$key`, ";
1070                                                         if($field['encryption'] == 'CRYPT') {
c614f1 1071                                                                 $record[$key] = $app->auth->crypt_password(stripslashes($record[$key]));
2dadd2 1072                                                                 $sql_insert_val .= "'".$app->db->quote($record[$key])."', ";
T 1073                                                         } elseif ($field['encryption'] == 'MYSQL') {
43b345 1074                                                                 $tmp = $app->db->queryOneRecord("SELECT PASSWORD('".$app->db->quote(stripslashes($record[$key]))."') as `crypted`");
T 1075                                                                 $record[$key] = $tmp['crypted'];
1076                                                                 $sql_insert_val .= "'".$app->db->quote($record[$key])."', ";
2dadd2 1077                                                         } elseif ($field['encryption'] == 'CLEARTEXT') {
T 1078                                                                 $sql_insert_val .= "'".$app->db->quote($record[$key])."', ";
2332b2 1079                                                         } else {
T 1080                                                                 $record[$key] = md5(stripslashes($record[$key]));
2dadd2 1081                                                                 $sql_insert_val .= "'".$app->db->quote($record[$key])."', ";
2332b2 1082                                                         }
T 1083
1084                                                 } elseif ($field['formtype'] == 'CHECKBOX') {
1085                                                         $sql_insert_key .= "`$key`, ";
2dadd2 1086                                                         if($record[$key] == '') {
T 1087                                                             // if a checkbox is not set, we set it to the unchecked value
1088                                                             $sql_insert_val .= "'".$field['value'][0]."', ";
1089                                                             $record[$key] = $field['value'][0];
1090                                                         } else {
1091                                                             $sql_insert_val .= "'".$record[$key]."', ";
1092                                                         }
2332b2 1093                                                 } else {
T 1094                                                         $sql_insert_key .= "`$key`, ";
1095                                                         $sql_insert_val .= "'".$record[$key]."', ";
1096                                                 }
1097                                         } else {
1098                                                 if($field['formtype'] == 'PASSWORD') {
2dadd2 1099                                                         if(isset($field['encryption']) && $field['encryption'] == 'CRYPT') {
2332b2 1100                                                                 $record[$key] = $app->auth->crypt_password(stripslashes($record[$key]));
2dadd2 1101                                                                 $sql_update .= "`$key` = '".$app->db->quote($record[$key])."', ";
T 1102                                                         } elseif (isset($field['encryption']) && $field['encryption'] == 'MYSQL') {
43b345 1103                                                                 $tmp = $app->db->queryOneRecord("SELECT PASSWORD('".$app->db->quote(stripslashes($record[$key]))."') as `crypted`");
T 1104                                                                 $record[$key] = $tmp['crypted'];
1105                                                                 $sql_update .= "`$key` = '".$app->db->quote($record[$key])."', ";
2dadd2 1106                                                         } elseif (isset($field['encryption']) && $field['encryption'] == 'CLEARTEXT') {
T 1107                                                                 $sql_update .= "`$key` = '".$app->db->quote($record[$key])."', ";
2332b2 1108                                                         } else {
T 1109                                                                 $record[$key] = md5(stripslashes($record[$key]));
2dadd2 1110                                                                 $sql_update .= "`$key` = '".$app->db->quote($record[$key])."', ";
2332b2 1111                                                         }
T 1112
1113                                                 } elseif ($field['formtype'] == 'CHECKBOX') {
2dadd2 1114                                                         if($record[$key] == '') {
T 1115                                                             // if a checkbox is not set, we set it to the unchecked value
1116                                                             $sql_update .= "`$key` = '".$field['value'][0]."', ";
1117                                                             $record[$key] = $field['value'][0];
1118                                                         } else {
1119                                                             $sql_update .= "`$key` = '".$record[$key]."', ";
1120                                                         }
2332b2 1121                                                 } else {
T 1122                                                         $sql_update .= "`$key` = '".$record[$key]."', ";
1123                                                 }
1124                                         }
1125                                 } else {
1126                                     // we unset the password filed, if empty to tell the datalog function
2dadd2 1127                                     // that the password has not been changed
2332b2 1128                                     unset($record[$key]);
2dadd2 1129                                 }
2332b2 1130                         }
T 1131         }
2dadd2 1132
T 1133
2332b2 1134                 // Add backticks for incomplete table names
T 1135                 if(stristr($this->formDef['db_table'],'.')) {
1136                         $escape = '';
1137                 } else {
1138                         $escape = '`';
1139                 }
2dadd2 1140
T 1141
2332b2 1142                 if($action == "INSERT") {
T 1143                         if($this->formDef['auth'] == 'yes') {
1144                                 // Set user and group
1145                                 $sql_insert_key .= "`sys_userid`, ";
1146                                 $sql_insert_val .= ($this->formDef["auth_preset"]["userid"] > 0)?"'".$this->formDef["auth_preset"]["userid"]."', ":"'".$_SESSION["s"]["user"]["userid"]."', ";
1147                                 $sql_insert_key .= "`sys_groupid`, ";
1148                                 $sql_insert_val .= ($this->formDef["auth_preset"]["groupid"] > 0)?"'".$this->formDef["auth_preset"]["groupid"]."', ":"'".$_SESSION["s"]["user"]["default_group"]."', ";
1149                                 $sql_insert_key .= "`sys_perm_user`, ";
1150                                 $sql_insert_val .= "'".$this->formDef["auth_preset"]["perm_user"]."', ";
1151                                 $sql_insert_key .= "`sys_perm_group`, ";
1152                                 $sql_insert_val .= "'".$this->formDef["auth_preset"]["perm_group"]."', ";
1153                                 $sql_insert_key .= "`sys_perm_other`, ";
1154                                 $sql_insert_val .= "'".$this->formDef["auth_preset"]["perm_other"]."', ";
1155                         }
1156                         $sql_insert_key = substr($sql_insert_key,0,-2);
1157                         $sql_insert_val = substr($sql_insert_val,0,-2);
1158                         $sql = "INSERT INTO ".$escape.$this->formDef['db_table'].$escape." ($sql_insert_key) VALUES ($sql_insert_val)";
1159                 } else {
2dadd2 1160                     if($this->formDef['auth'] == 'yes') {
2332b2 1161                         if($primary_id != 0) {
T 1162                                 $sql_update = substr($sql_update,0,-2);
1163                                 $sql = "UPDATE ".$escape.$this->formDef['db_table'].$escape." SET ".$sql_update." WHERE ".$this->getAuthSQL('u')." AND ".$this->formDef['db_table_idx']." = ".$primary_id;
1164                                 if($sql_ext_where != '') $sql .= " and ".$sql_ext_where;
1165                         } else {
1166                                 $app->error("Primary ID fehlt!");
1167                         }
2dadd2 1168                     } else {
T 1169                         if($primary_id != 0) {
2332b2 1170                                 $sql_update = substr($sql_update,0,-2);
T 1171                                 $sql = "UPDATE ".$escape.$this->formDef['db_table'].$escape." SET ".$sql_update." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
1172                                 if($sql_ext_where != '') $sql .= " and ".$sql_ext_where;
1173                         } else {
1174                                 $app->error("Primary ID fehlt!");
1175                         }
2dadd2 1176                     }
T 1177                     //* return a empty string if there is nothing to update
1178                     if(trim($sql_update) == '') $sql = '';
2332b2 1179                 }
26c0fc 1180
2332b2 1181                 return $sql;
T 1182         }
2dadd2 1183
2332b2 1184         /**
T 1185         * Debugging arrays.
1186         *
1187         * @param array_data
1188         */
1189         function dbg($array_data) {
2dadd2 1190
2332b2 1191                 echo "<pre>";
T 1192                 print_r($array_data);
1193                 echo "</pre>";
2dadd2 1194
2332b2 1195         }
2dadd2 1196
T 1197
2332b2 1198     function showForm() {
T 1199             global $app,$conf;
2dadd2 1200
2332b2 1201         if(!is_array($this->formDef)) die("Form Definition wurde nicht geladen.");
2dadd2 1202
2332b2 1203                 $active_tab = $this->getNextTab();
2dadd2 1204
2332b2 1205         // go trough the tabs
T 1206         foreach( $this->formDef["tabs"] as $key => $tab) {
2dadd2 1207
2332b2 1208             $tab['name'] = $key;
40dd9f 1209             // Translate the title of the tab
T 1210             $tab['title'] = $this->lng($tab['title']);
2dadd2 1211
2332b2 1212             if($tab['name'] == $active_tab) {
2dadd2 1213
2332b2 1214                 // If module is set, then set the template path relative to the module..
T 1215                 if($this->module != '') $tab["template"] = "../".$this->module."/".$tab["template"];
2dadd2 1216
2332b2 1217                 // Generate the template if it does not exist yet.
2dadd2 1218
T 1219
1220
2332b2 1221                 if(!is_file($tab["template"])) {
T 1222                      $app->uses('tform_tpl_generator');
1223                      $app->tform_tpl_generator->buildHTML($this->formDef,$tab['name']);
1224                 }
1225                 $app->tpl->setVar('readonly_tab', (isset($tab['readonly']) && $tab['readonly'] == true));
1226                 $app->tpl->setInclude('content_tpl',$tab["template"]);
1227                 $tab["active"] = 1;
1228                 $_SESSION["s"]["form"]["tab"] = $tab['name'];
1229             } else {
1230                     $tab["active"] = 0;
1231             }
2dadd2 1232
2332b2 1233                         // Unset unused variables.
T 1234                         unset($tab["fields"]);
1235                         unset($tab["plugins"]);
2dadd2 1236
2332b2 1237             $frmTab[] = $tab;
T 1238         }
2dadd2 1239
2332b2 1240         // setting form tabs
T 1241         $app->tpl->setLoop("formTab", $frmTab);
1242
1243                 // Set form action
1244                 $app->tpl->setVar('form_action',$this->formDef["action"]);
1245                 $app->tpl->setVar('form_active_tab',$active_tab);
1246
1247                 // Set form title
1248                 $form_hint = $this->lng($this->formDef["title"]);
1249                 if($this->formDef["description"] != '') $form_hint .= '<div class="pageForm_description">'.$this->lng($this->formDef["description"]).'</div>';
1250                 $app->tpl->setVar('form_hint',$form_hint);
1251
1252                 // Set Wordbook for this form
1253
1254                 $app->tpl->setVar($this->wordbook);
1255         }
2dadd2 1256
T 1257         function getDataRecord($primary_id) {
1258             global $app;
1259             $escape = '`';
1260             $sql = "SELECT * FROM ".$escape.$this->formDef['db_table'].$escape." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
2332b2 1261             return $app->db->queryOneRecord($sql);
2dadd2 1262         }
T 1263
2332b2 1264
T 1265         function datalogSave($action,$primary_id, $record_old, $record_new) {
1266                 global $app,$conf;
1267
74829e 1268                 $app->db->datalogSave($this->formDef['db_table'], $action, $this->formDef['db_table_idx'], $primary_id, $record_old, $record_new);
T 1269                 return true;
2dadd2 1270
2332b2 1271                 /*
T 1272                 // Add backticks for incomplete table names.
1273                 if(stristr($this->formDef['db_table'],'.')) {
1274                         $escape = '';
1275                 } else {
1276                         $escape = '`';
1277                 }
1278
1279                 $this->diffrec = array();
1280
2dadd2 1281                 // Full diff records for ISPConfig, they have a different format then the simple diffrec
T 1282                 $diffrec_full = array();
1283
2332b2 1284                 if(is_array($record_old) && count($record_old) > 0) {
T 1285                         foreach($record_old as $key => $val) {
1286                                 //if(isset($record_new[$key]) && $record_new[$key] != $val) {
2dadd2 1287                                 if(!isset($record_new[$key]) || $record_new[$key] != $val) {
2332b2 1288                                     // Record has changed
2dadd2 1289                                     $diffrec_full['old'][$key] = $val;
T 1290                                     $diffrec_full['new'][$key] = $record_new[$key];
1291                                     $this->diffrec[$key] = array(    'new' => $record_new[$key],
2332b2 1292                                                                        'old' => $val);
T 1293                                 } else {
2dadd2 1294                                     $diffrec_full['old'][$key] = $val;
T 1295                                     $diffrec_full['new'][$key] = $val;
1296                                 }
2332b2 1297                         }
T 1298                 } elseif(is_array($record_new)) {
1299                         foreach($record_new as $key => $val) {
1300                                 if(isset($record_new[$key]) && $record_old[$key] != $val) {
1301                                     // Record has changed
2dadd2 1302                                     $diffrec_full['new'][$key] = $val;
T 1303                                     $diffrec_full['old'][$key] = $record_old[$key];
1304                                     $this->diffrec[$key] = array(    'old' => @$record_old[$key],
2332b2 1305                                                                        'new' => $val);
T 1306                                 } else {
2dadd2 1307                                     $diffrec_full['new'][$key] = $val;
T 1308                                     $diffrec_full['old'][$key] = $val;
1309                                 }
2332b2 1310                         }
T 1311                 }
1312
2dadd2 1313                 //$this->diffrec = $diffrec;
T 1314                 // Insert the server_id, if the record has a server_id
1315                 $server_id = (isset($record_old["server_id"]) && $record_old["server_id"] > 0)?$record_old["server_id"]:0;
1316                 if(isset($record_new["server_id"])) $server_id = $record_new["server_id"];
1317
2332b2 1318                 if(count($this->diffrec) > 0) {
2dadd2 1319                         $diffstr = addslashes(serialize($diffrec_full));
2332b2 1320                         $username = $app->db->quote($_SESSION["s"]["user"]["username"]);
T 1321                         $dbidx = $this->formDef['db_table_idx'].":".$primary_id;
1322                         // $action = ($action == 'INSERT')?'i':'u';
1323
2dadd2 1324                         if($action == 'INSERT') $action = 'i';
T 1325                         if($action == 'UPDATE') $action = 'u';
1326                         if($action == 'DELETE') $action = 'd';
2332b2 1327                         $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')";
2dadd2 1328                         $app->db->query($sql);
2332b2 1329                 }
2dadd2 1330
2332b2 1331                 return true;
74829e 1332                 */
2dadd2 1333
2332b2 1334         }
2dadd2 1335
2332b2 1336         function getAuthSQL($perm, $table = '') {
2dadd2 1337                 if($_SESSION["s"]["user"]["typ"] == 'admin') {
T 1338                     return '1';
1339                 } else {
24f38b 1340                     if ($table != ''){
V 1341                         $table = ' ' . $table . '.';
1342                     }
2332b2 1343                     $groups = ( $_SESSION["s"]["user"]["groups"] ) ? $_SESSION["s"]["user"]["groups"] : 0;
2dadd2 1344                     $sql = '(';
2332b2 1345                     $sql .= "(" . $table . "sys_userid = ".$_SESSION["s"]["user"]["userid"]." AND " . $table . "sys_perm_user like '%$perm%') OR  ";
T 1346                     $sql .= "(" . $table . "sys_groupid IN (".$groups.") AND " . $table ."sys_perm_group like '%$perm%') OR ";
1347                     $sql .= $table . "sys_perm_other like '%$perm%'";
1348                     $sql .= ')';
2dadd2 1349
2332b2 1350                     return $sql;
2dadd2 1351                 }
2332b2 1352         }
2dadd2 1353
2332b2 1354         /*
T 1355         This function checks if a user has the parmissions $perm for the data record with the ID $record_id
1356         If record_id = 0, the the permissions are tested against the defaults of the form file.
1357         */
1358         function checkPerm($record_id,$perm) {
1359                 global $app;
2dadd2 1360
2332b2 1361                 if($record_id > 0) {
T 1362                         // Add backticks for incomplete table names.
1363                         if(stristr($this->formDef['db_table'],'.')) {
1364                                 $escape = '';
1365                         } else {
1366                                 $escape = '`';
1367                         }
2dadd2 1368
2332b2 1369                         $sql = "SELECT ".$this->formDef['db_table_idx']." FROM ".$escape.$this->formDef['db_table'].$escape." WHERE ".$this->formDef['db_table_idx']." = ".$record_id." AND ".$this->getAuthSQL($perm);
T 1370                         if($record = $app->db->queryOneRecord($sql)) {
1371                                 return true;
1372                         } else {
1373                                 return false;
1374                         }
1375                 } else {
1376                         $result = false;
1377                         if(@$this->formDef["auth_preset"]["userid"] == $_SESSION["s"]["user"]["userid"] && stristr($perm,$this->formDef["auth_preset"]["perm_user"])) $result = true;
1378                         if(@$this->formDef["auth_preset"]["groupid"] == $_SESSION["s"]["user"]["groupid"] && stristr($perm,$this->formDef["auth_preset"]["perm_group"])) $result = true;
1379                         if(@stristr($this->formDef["auth_preset"]["perm_other"],$perm)) $result = true;
2dadd2 1380
2332b2 1381                         // if preset == 0, everyone can insert a record of this type
T 1382                         if($this->formDef["auth_preset"]["userid"] == 0 AND $this->formDef["auth_preset"]["groupid"] == 0 AND (@stristr($this->formDef["auth_preset"]["perm_user"],$perm) OR @stristr($this->formDef["auth_preset"]["perm_group"],$perm))) $result = true;
2dadd2 1383
2332b2 1384                         return $result;
2dadd2 1385
2332b2 1386                 }
2dadd2 1387
2332b2 1388         }
2dadd2 1389
2332b2 1390         function getNextTab() {
T 1391                 // Which tab is shown
1392                 if($this->errorMessage == '') {
1393                     // If there is no error
1394                     if(isset($_REQUEST["next_tab"]) && $_REQUEST["next_tab"] != '') {
1395                                 // If the next tab is known
1396                                 $active_tab = $_REQUEST["next_tab"];
1397                     } else {
1398                         // else use the default tab
1399                         $active_tab = $this->formDef['tab_default'];
1400                     }
1401                 } else {
1402                     // Show the same tab again in case of an error
1403                     $active_tab = $_SESSION["s"]["form"]["tab"];
1404                 }
2dadd2 1405
2332b2 1406                 return $active_tab;
T 1407         }
2dadd2 1408
2332b2 1409         function getCurrentTab() {
T 1410                 return $_SESSION["s"]["form"]["tab"];
1411         }
1412
2dadd2 1413         function isReadonlyTab($tab, $primary_id) {
T 1414             global $app, $conf;
2332b2 1415
2dadd2 1416             // Add backticks for incomplete table names.
2332b2 1417             if(stristr($this->formDef['db_table'],'.')) {
T 1418                 $escape = '';
1419             } else {
1420                 $escape = '`';
1421             }
1422
2dadd2 1423             $sql = "SELECT sys_userid FROM ".$escape.$this->formDef['db_table'].$escape." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
2332b2 1424             $record = $app->db->queryOneRecord($sql);
T 1425
2dadd2 1426             // return true if the readonly flag of the form is set and the current loggedin user is not the owner of the record.
T 1427             if(isset($this->formDef['tabs'][$tab]['readonly']) && $this->formDef['tabs'][$tab]['readonly'] == true && $record['sys_userid'] != $_SESSION["s"]["user"]["userid"]) {
1428                 return true;
1429             } else {
1430                 return false;
1431             }
2332b2 1432         }
T 1433
1434
2dadd2 1435         // translation function for forms, tries the form wordbook first and if this fails, it tries the global wordbook
T 1436         function lng($msg) {
8d0c6b 1437             global $app,$conf;
2332b2 1438
2dadd2 1439             if(isset($this->wordbook[$msg])) {
T 1440                 return $this->wordbook[$msg];
1441             } else {
1442                 return $app->lng($msg);
1443             }
2332b2 1444
2dadd2 1445         }
2332b2 1446
3cebc3 1447         function checkClientLimit($limit_name,$sql_where = '') {
T 1448             global $app;
2332b2 1449
3cebc3 1450             $check_passed = true;
T 1451             $limit_name = $app->db->quote($limit_name);
1452             if($limit_name == '') $app->error('Limit name missing in function checkClientLimit.');
2332b2 1453
3cebc3 1454             // Get the limits of the client that is currently logged in
T 1455             $client_group_id = $_SESSION["s"]["user"]["default_group"];
1456             $client = $app->db->queryOneRecord("SELECT $limit_name as number, parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
2332b2 1457
3cebc3 1458             // Check if the user may add another item
T 1459             if($client["number"] >= 0) {
1460                 $sql = "SELECT count(".$this->formDef['db_table_idx'].") as number FROM ".$this->formDef['db_table']." WHERE ".$this->getAuthSQL('u');
1461                 if($sql_where != '') $sql .= ' and '.$sql_where;
1462                 $tmp = $app->db->queryOneRecord($sql);
1463                 if($tmp["number"] >= $client["number"]) $check_passed = false;
1464             }
2332b2 1465
3cebc3 1466             return $check_passed;
T 1467         }
2332b2 1468
3cebc3 1469         function checkResellerLimit($limit_name,$sql_where = '') {
T 1470             global $app;
2332b2 1471
3cebc3 1472             $check_passed = true;
T 1473             $limit_name = $app->db->quote($limit_name);
1474             if($limit_name == '') $app->error('Limit name missing in function checkClientLimit.');
2332b2 1475
3cebc3 1476             // Get the limits of the client that is currently logged in
T 1477             $client_group_id = $_SESSION["s"]["user"]["default_group"];
1478             $client = $app->db->queryOneRecord("SELECT parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
2332b2 1479
3cebc3 1480             //* If the client belongs to a reseller, we will check against the reseller Limit too
T 1481             if($client['parent_client_id'] != 0) {
2332b2 1482
3cebc3 1483                 //* first we need to know the groups of this reseller
T 1484                 $tmp = $app->db->queryOneRecord("SELECT userid, groups FROM sys_user WHERE client_id = ".$client['parent_client_id']);
1485                 $reseller_groups = $tmp["groups"];
1486                 $reseller_userid = $tmp["userid"];
2332b2 1487
3cebc3 1488                 // Get the limits of the reseller of the logged in client
T 1489                 $client_group_id = $_SESSION["s"]["user"]["default_group"];
1490                 $reseller = $app->db->queryOneRecord("SELECT $limit_name as number FROM client WHERE client_id = ".$client['parent_client_id']);
2332b2 1491
3cebc3 1492                 // Check if the user may add another item
T 1493                 if($reseller["number"] >= 0) {
1494                     $sql = "SELECT count(".$this->formDef['db_table_idx'].") as number FROM ".$this->formDef['db_table']." WHERE (sys_groupid IN (".$reseller_groups.") or sys_userid = ".$reseller_userid.")";
1495                     if($sql_where != '') $sql .= ' and '.$sql_where;
1496                     $tmp = $app->db->queryOneRecord($sql);
1497                     if($tmp["number"] >= $reseller["number"]) $check_passed = false;
1498                 }
1499             }
2332b2 1500
3cebc3 1501             return $check_passed;
T 1502         }
2332b2 1503
11b3da 1504         //* get the difference record of two arrays
T 1505         function getDiffRecord($record_old,$record_new) {
2332b2 1506
11b3da 1507             if(is_array($record_new) && count($record_new) > 0) {
T 1508             foreach($record_new as $key => $val) {
1509                 if(@$record_old[$key] != $val) {
1510                     // Record has changed
1511                     $diffrec[$key] = array(    'old' => @$record_old[$key],
1512                                             'new' => $val);
1513                     }
1514                 }
1515             } elseif(is_array($record_old)) {
1516                 foreach($record_old as $key => $val) {
1517                     if($record_new[$key] != $val) {
1518                         // Record has changed
1519                         $diffrec[$key] = array(    'new' => $record_new[$key],
1520                                                 'old' => $val);
1521                         }
1522                     }
1523                 }
1524             return $diffrec;
2332b2 1525
11b3da 1526         }
2332b2 1527
75ae20 1528         /**
W 1529          * Generate HTML for DATETIME fields.
2332b2 1530          *
75ae20 1531          * @access private
W 1532          * @param string $form_element Name of the form element.
1533          * @param string $default_value Selected value for fields.
1534          * @param bool $display_secons Include seconds selection.
2332b2 1535          * @return string HTML
75ae20 1536          */
W 1537         function _getDateTimeHTML($form_element, $default_value, $display_seconds=false)
1538         {
1539             $_datetime = strtotime($default_value);
1540             $_showdate = ($_datetime === false) ? false : true;
2dadd2 1541
75ae20 1542             $dselect = array('day','month','year','hour','minute');
2332b2 1543             if ($display_seconds === true) {
75ae20 1544                  $dselect[] = 'second';
W 1545             }
2332b2 1546
75ae20 1547             $out = '';
2332b2 1548
75ae20 1549             foreach ($dselect as $dt_element)
W 1550             {
1551                  $dt_options = array();
1552                  $dt_space = 1;
2332b2 1553
75ae20 1554                  switch ($dt_element) {
W 1555                      case 'day':
1556                          for ($i = 1; $i <= 31; $i++) {
2332b2 1557                             $dt_options[] = array('name' =>  sprintf('%02d', $i),
T 1558                                                   'value' => sprintf('%d', $i));
1559                         }
1560                         $selected_value = date('d', $_datetime);
75ae20 1561                          break;
2332b2 1562
75ae20 1563                      case 'month':
W 1564                          for ($i = 1; $i <= 12; $i++) {
2332b2 1565                             $dt_options[] = array('name' => strftime('%b', mktime(0, 0, 0, $i, 1, 2000)),
T 1566                                                   'value' => strftime('%m', mktime(0, 0, 0, $i, 1, 2000)));
1567                         }
1568                         $selected_value = date('n', $_datetime);
75ae20 1569                          break;
2332b2 1570
75ae20 1571                      case 'year':
W 1572                          $start_year = strftime("%Y");
1573                         $years = range((int)$start_year, (int)($start_year+3));
2332b2 1574
T 1575                         foreach ($years as $year) {
1576                             $dt_options[] = array('name' => $year,
1577                                                  'value' => $year);
1578                         }
1579                         $selected_value = date('Y', $_datetime);
1580                         $dt_space = 2;
75ae20 1581                          break;
2332b2 1582
75ae20 1583                      case 'hour':
W 1584                          foreach(range(0, 23) as $hour) {
1585                              $dt_options[] = array('name' =>  sprintf('%02d', $hour),
2332b2 1586                                                   'value' => sprintf('%d', $hour));
75ae20 1587                          }
W 1588                          $selected_value = date('G', $_datetime);
1589                          break;
2332b2 1590
75ae20 1591                      case 'minute':
W 1592                          foreach(range(0, 59) as $minute) {
1593                              if (($minute % 5) == 0) {
1594                                  $dt_options[] = array('name' =>  sprintf('%02d', $minute),
1595                                                       'value' => sprintf('%d', $minute));
1596                              }
1597                          }
1598                          $selected_value = (int)floor(date('i', $_datetime));
1599                          break;
2332b2 1600
T 1601                      case 'second':
75ae20 1602                          foreach(range(0, 59) as $second) {
W 1603                              $dt_options[] = array('name' =>  sprintf('%02d', $second),
2332b2 1604                                                     'value' => sprintf('%d', $second));
75ae20 1605                          }
W 1606                          $selected_value = (int)floor(date('s', $_datetime));
1607                          break;
1608                  }
2332b2 1609
75ae20 1610                 $out .= "<select name=\"".$form_element."[$dt_element]\" id=\"".$form_element."_$dt_element\" class=\"selectInput\" style=\"width: auto; float: none;\">";
W 1611                 if (!$_showdate) {
1612                     $out .= "<option value=\"-\" selected=\"selected\">--</option>" . PHP_EOL;
1613                 } else {
1614                     $out .= "<option value=\"-\">--</option>" . PHP_EOL;
1615                 }
2332b2 1616
75ae20 1617                 foreach ($dt_options as $dt_opt) {
W 1618                     if ( $_showdate && ($selected_value == $dt_opt['value']) ) {
1619                         $out .= "<option value=\"{$dt_opt['value']}\" selected=\"selected\">{$dt_opt['name']}</option>" . PHP_EOL;
1620                     } else {
1621                         $out .= "<option value=\"{$dt_opt['value']}\">{$dt_opt['name']}</option>" . PHP_EOL;
1622                     }
1623                 }
2332b2 1624
75ae20 1625                 $out .= '</select>' . str_repeat('&nbsp;', $dt_space);
W 1626             }
2332b2 1627
75ae20 1628             return $out;
W 1629         }
2dadd2 1630 }
T 1631
d7ef36 1632 ?>