"Fossies" - the Fresh Open Source Software Archive 
Member "openmailadmin-1.0.1/inc/lib/InputValidatorSuite.php" (26 May 2007, 7204 Bytes) of package /linux/privat/old/openmailadmin-1.0.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "InputValidatorSuite.php" see the
Fossies "Dox" file reference documentation.
1 <?php
2 /**
3 * Here will all checks take place.
4 */
5 class InputValidatorSuite
6 {
7 protected $oma;
8 protected $cfg;
9 protected $ErrorHandler;
10 protected $initialized = false;
11
12 private $invalid = array();
13 private $missing = array();
14 /** captions and default values of given fields */
15 private $inputs = array();
16 /** array with validation instructions and corresponding error-messages */
17 private $validate = array();
18
19 public function __construct(openmailadmin $oma = null, array $cfg = array()) {
20 $this->oma = $oma;
21 $this->cfg = $cfg;
22 $this->ErrorHandler = ErrorHandler::getInstance();
23 }
24
25 private function initialize() {
26 $this->initialized = true;
27
28 // Fieldname as key, cap as its caption and def as its default value.
29 $this->inputs['mbox'] = array('cap' => txt('83'),
30 );
31 $this->inputs['pate'] = array('cap' => txt('9'),
32 'def' => $this->oma->current_user->mbox,
33 );
34 $this->inputs['person'] = array('cap' => txt('84'),
35 );
36 $this->inputs['domains'] = array('cap' => txt('86'),
37 'def' => $this->oma->current_user->domains,
38 );
39 $this->inputs['canonical'] = array('cap' => txt('7'),
40 );
41 $this->inputs['quota'] = array('cap' => txt('87'),
42 );
43 $this->inputs['max_alias'] = array('cap' => txt('88'),
44 );
45 $this->inputs['max_regexp'] = array('cap' => txt('89'),
46 'def' => 0,
47 );
48 $this->inputs['a_super'] = array('cap' => txt('68'),
49 'def' => 0,
50 );
51 $this->inputs['a_admin_domains'] = array('cap' => txt('50'),
52 'def' => 0,
53 );
54 $this->inputs['a_admin_user'] = array('cap' => txt('70'),
55 'def' => 0,
56 );
57 // domains
58 $this->inputs['domain'] = array('cap' => txt('55'),
59 );
60 $this->inputs['owner'] = array('cap' => txt('56'),
61 'def' => $this->oma->current_user->mbox,
62 );
63 $this->inputs['a_admin'] = array('cap' => txt('57'),
64 'def' => implode(',', array_unique(array($this->oma->current_user->mbox, $this->oma->authenticated_user->mbox))),
65 );
66 $this->inputs['categories'] = array('cap' => txt('58'),
67 );
68
69 // Hash with tests vor sanity and possible error-messages on failure.
70 // These will only be processed if a value is given. (I.e. not on the default values from above)
71 // If a test fails the next won't be invoked.
72 $this->validate['mbox'] = array(array( 'val' => 'strlen(~) >= $this->cfg[\'mbox\'][\'min_length\'] && strlen(~) <= $this->cfg[\'mbox\'][\'max_length\']',
73 'error' => sprintf(txt('62'), $this->cfg['mbox']['min_length'], $this->cfg['mbox']['max_length']) ),
74 array( 'val' => '$this->oma->imap->is_valid_username(~)',
75 'error' => txt('135') ),
76 );
77 $this->validate['pate'] = array(array( 'val' => '$this->oma->authenticated_user->is_superuser() || $this->oma->user_is_descendant(~, $this->oma->authenticated_user->mbox)',
78 ),
79 );
80 $this->validate['person'] = array(array( 'val' => 'strlen(~) <= 100 && strlen(~) >= 4 && preg_match(\'/^[\w\s0-9-_\.\(\)]*$/\', ~)',
81 ),
82 );
83 $this->validate['domains'] = array(array( 'val' => '(~ = trim(~)) && preg_match(\'/^((?:[\w]+|[\w]+\.[\w]+),\s*)*([\w]+|[\w]+\.[\w]+)$/i\', ~)',
84 ),
85 array( 'val' => '$this->oma->domain_check($this->oma->current_user, $this->oma->current_user->mbox, ~)',
86 'error' => txt('81')),
87 );
88 $this->validate['canonical'] = array(array( 'val' => 'preg_match(\'/\'.openmailadmin::regex_valid_email.\'/i\', ~)',
89 'error' => txt('64')),
90 );
91 $this->validate['quota'] = array(array( 'val' => 'is_numeric(~) && settype(~, \'int\') && ~ >= 0',
92 'error' => txt('63')),
93 );
94 $this->validate['max_alias'] = array(array( 'val' => 'is_numeric(~) && settype(~, \'int\') && ~ >= 0',
95 'error' => txt('63')),
96 );
97 $this->validate['max_regexp'] = array(array( 'val' => 'is_numeric(~) && settype(~, \'int\') && ~ >= 0',
98 'error' => txt('63')),
99 );
100 $this->validate['a_super'] = array(array( 'val' => 'is_numeric(~) && settype(~, \'int\') && ~ < 3 && ~ >= 0',
101 ),
102 array( 'val' => '~ == 0 || $this->oma->authenticated_user-># >= 2 || $this->oma->authenticated_user->a_super > ~ || $this->oma->authenticated_user-># > ~',
103 'error' => txt('16')),
104 );
105 $this->validate['a_admin_domains'] = $this->validate['a_super'];
106 $this->validate['a_admin_user'] = $this->validate['a_super'];
107 // domains
108 $this->validate['domain'] = array(array( 'val' => 'preg_match(\'/\'.openmailadmin::regex_valid_domain.\'/i\', ~)',
109 'error' => txt('51')),
110 );
111 $this->validate['owner'] = array(array( 'val' => 'strlen(~) >= $this->cfg[\'mbox\'][\'min_length\'] && strlen(~) <= $this->cfg[\'mbox\'][\'max_length\'] && preg_match(\'/^[a-zA-Z0-9]*$/\', ~)',
112 ),
113 );
114 $this->validate['a_admin'] = array(array( 'val' => 'preg_match(\'/^([a-z0-9]+,\s*)*[a-z0-9]+$/i\', ~)',
115 ),
116 );
117 $this->validate['categories'] = array(array( 'val' => '(~ = trim(~)) && preg_match(\'/^((?:[\w]+|[\w]+\.[\w]+),\s*)*([\w]+|[\w]+\.[\w]+)$/i\', ~)',
118 ),
119 );
120 }
121
122 /**
123 * @param oma Openmailadmin; the caller
124 * @param cfg Array with configuration options.
125 * @param data data to be tested typically $_POST
126 * @param which array of fields' names from data to be checked
127 */
128 public function validate(&$data, $which) {
129 if(!$this->initialized) {
130 $this->initialize();
131 }
132 // Now we can set error-messages.
133 $error_occured = $this->iterate_through_fields($data, $which);
134 if($error_occured) {
135 if(count($this->invalid) > 0) {
136 $this->ErrorHandler->add_error(sprintf(txt('130'), implode(', ', $this->invalid)));
137 }
138 if(count($this->missing) > 0) {
139 $this->ErrorHandler->add_error(sprintf(txt('129'), implode(', ', $this->missing)));
140 }
141 }
142 return(!$error_occured);
143 }
144
145 /**
146 * To invoke all necessary checks.
147 */
148 private function iterate_through_fields(&$data, $which) {
149 $error_occured = false;
150 $this->invalid = array();
151 $this->missing = array();
152 foreach($which as $fieldname) {
153 // Do we have to care about that field?
154 if(isset($this->inputs[$fieldname])) {
155 // Did the user provide it?
156 if(isset($data[$fieldname]) && $data[$fieldname] != '') {
157 // If so and if we have a rule to check for validity, we have to validate this field.
158 if(isset($this->validate[$fieldname])) {
159 foreach($this->validate[$fieldname] as $test) {
160 if(!eval('return ('.str_replace(array('~', '#'), array('$data[\''.$fieldname.'\']', $fieldname), $test['val']).');')) {
161 // The given value is invalid.
162 $error_occured = true;
163 if(isset($test['error'])) {
164 $this->ErrorHandler->add_error($test['error']);
165 } else {
166 $this->invalid[] = $this->inputs[$fieldname]['cap'];
167 }
168 break;
169 }
170 }
171 }
172 // $data[$fieldname] = mysql_real_escape_string($data[$fieldname]);
173 } else {
174 // Assign it a valid value, if possible.
175 if(isset($this->inputs[$fieldname]['def'])) {
176 $data[$fieldname] = $this->inputs[$fieldname]['def'];
177 } else {
178 // No value was given and we cannot assign it a default value.
179 $error_occured = true;
180 $this->missing[] = $this->inputs[$fieldname]['cap'];
181 }
182 }
183 }
184 }
185 return $error_occured;
186 }
187
188 }
189 ?>