File: validation.inc.php

package info (click to toggle)
webissues-server 0.8.3-2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 392 kB
  • ctags: 610
  • sloc: php: 2,206; sql: 458; sh: 44; makefile: 9
file content (334 lines) | stat: -rw-r--r-- 9,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**************************************************************************
* This file is part of the WebIssues Server program
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007-2008 WebIssues Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

function wi_verify_definition( $definition )
{
    $info = wi_parse_definition( $definition );

    wi_verify_definition_info( $info );
}

function wi_verify_value( $definition, $value, $project_id )
{
    $info = wi_parse_definition( $definition );

    return wi_verify_value_info( $info, $value, $project_id );
}

function wi_get_default_value( $definition )
{
    $info = wi_parse_definition( $definition );

    return $info[ 'default' ];
}

function wi_parse_definition( $definition )
{
    $reg_num = '-?\d+';
    $reg_str = '"(?:\\\\["\\\\]|[^"\\\\\\n])*"';
    $reg_arr = "\{$reg_str(?:,$reg_str)*}";

    $reg_def = "/^([A-Z]+)(( ([-a-z]+)=($reg_num|$reg_str|$reg_arr))*)$/";

    if ( !preg_match( $reg_def, $definition, $parts ) )
        wi_error( 343 );

    $info[ 'type' ] = $parts[ 1 ];
    $meta = array();

    $count = preg_match_all( "/([-a-z]+)=($reg_num|$reg_str|$reg_arr)/", $parts[ 2 ], $matches );

    for ( $i = 0; $i < $count; $i++ ) {
        $value = $matches[ 2 ][ $i ];
        $letter = $value{ 0 };
        if ( $letter == '"' ) {
            $value = wi_string( wi_unquote( $value ), 80, false );
        } else if ( $letter == '{' ) {
            preg_match_all( "/$reg_str/", $value, $items );
            $value = array();
            foreach ( $items[ 0 ] as $item )
                $value[] = wi_string( wi_unquote( $item ), 80, false );
        } else {
            if ( !wi_check_integer( $value ) )
                wi_error( 343 );
            $value = (int)$value;
        }
        $meta[ $matches[ 1 ][ $i ] ] = $value;
    }

    if ( isset( $meta[ 'default' ] ) ) {
        $info[ 'default' ] = $meta[ 'default' ];
        unset( $meta[ 'default' ] );
    } else {
        $info[ 'default' ] = '';
    }

    if ( isset( $meta[ 'required' ] ) ) {
        $info[ 'required' ] = $meta[ 'required' ];
        unset( $meta[ 'required' ] );
    } else {
        $info[ 'required' ] = 0;
    }

    $info[ 'metadata' ] = $meta;

    return $info;
}

function wi_verify_definition_info( $info )
{
    switch ( $info[ 'type' ] ) {
        case 'TEXT':
            wi_verify_text_definition_info( $info );
            break;
        case 'ENUM':
            wi_verify_enum_definition_info( $info );
            break;
        case 'NUMERIC':
            wi_verify_numeric_definition_info( $info );
            break;
        case 'DATETIME':
            wi_verify_datetime_definition_info( $info );
            break;
        case 'USER':
            wi_verify_user_definition_info( $info );
            break;
        default:
            wi_error( 343 );
    }

    $required = $info[ 'required' ];
    if ( !is_int( $required ) || $required < 0 || $required > 1 )
        wi_error( 343 );

    wi_verify_value_info( $info, $info[ 'default' ], 0 );
}

function wi_verify_text_definition_info( $info )
{
    foreach ( $info[ 'metadata' ] as $meta => $value ) {
        switch ( $meta ) {
            case 'max-length':
                if ( !is_int( $value ) || $value < 1 || $value > 80 )
                    wi_error( 343 );
                break;
            default:
                wi_error( 343 );
        }
    }
}

function wi_verify_enum_definition_info( $info )
{
    if ( !wi_metadata( $info, 'items', false ) )
        wi_error( 343 );

    foreach ( $info[ 'metadata' ] as $meta => $value ) {
        switch ( $meta ) {
            case 'items':
                if ( !is_array( $value ) )
                    wi_error( 343 );
                if ( array_search( '', $value ) !== false )
                    wi_error( 343 );
                if ( count( array_unique( $value ) ) != count( $value ) )
                    wi_error( 343 );
                break;
            case 'editable':
                if ( !is_int( $value ) || $value < 0 || $value > 1 )
                    wi_error( 343 );
                break;
            default:
                wi_error( 343 );
        }
    }
}

function wi_verify_numeric_definition_info( $info )
{
    foreach ( $info[ 'metadata' ] as $meta => $value ) {
        switch ( $meta ) {
            case 'decimal':
                if ( !is_int( $value ) || $value < 0 || $value > 6 )
                    wi_error( 343 );
                break;
            case 'min-value':
            case 'max-value':
                if ( !is_string( $value ) )
                    wi_error( 343 );
                break;
            default:
                wi_error( 343 );
        }
    }

    $decimal = wi_metadata( $info, 'decimal', 0 );
    $min = wi_metadata( $info, 'min-value', false );
    $max = wi_metadata( $info, 'max-value', false );

    if ( $min !== false && !wi_check_decimal( $min, $decimal ) )
        wi_error( 342 );
    if ( $max !== false && !wi_check_decimal( $max, $decimal ) )
        wi_error( 342 );

    if ( $min !== false && $max !== false && (float)$min > (float)$max )
        wi_error( 342 );
}

function wi_verify_datetime_definition_info( $info )
{
    foreach ( $info[ 'metadata' ] as $meta => $value ) {
        switch ( $meta ) {
            case 'time':
                if ( !is_int( $value ) || $value < 0 || $value > 1 )
                    wi_error( 343 );
                break;
            default:
                wi_error( 343 );
        }
    }
}

function wi_verify_user_definition_info( $info )
{
    foreach ( $info[ 'metadata' ] as $meta => $value ) {
        switch ( $meta ) {
            case 'members':
                if ( !is_int( $value ) || $value < 0 || $value > 1 )
                    wi_error( 343 );
                break;
            default:
                wi_error( 343 );
        }
    }
}

function wi_verify_value_info( $info, $value, $project_id )
{
    $value = wi_string( $value, 80, $info[ 'required' ] );

    if ( $value == '' )
        return $value;

    switch ( $info[ 'type' ] ) {
        case 'TEXT':
            return wi_verify_text_value_info( $info, $value );
        case 'ENUM':
            return wi_verify_enum_value_info( $info, $value );
        case 'NUMERIC':
            return wi_verify_numeric_value_info( $info, $value );
        case 'DATETIME':
            return wi_verify_datetime_value_info( $info, $value );
        case 'USER':
            return wi_verify_user_value_info( $info, $value, $project_id );
        default:
            wi_error( 343 );
    }
}

function wi_verify_text_value_info( $info, $value )
{
    if ( wi_metadata( $info, 'max-length', 0 ) ) {
        if ( wi_length( $value ) > $info[ 'metadata' ][ 'max-length' ] )
            wi_error( 342 );
    }

    return $value;
}

function wi_verify_enum_value_info( $info, $value )
{
    if ( !wi_metadata( $info, 'editable', 0 ) ) {
        $items = wi_metadata( $info, 'items', array() );
        if ( array_search( $value, $items ) === false )
           wi_error( 342 );
    }

    return $value;
}

function wi_verify_numeric_value_info( $info, $value )
{
    $decimal = wi_metadata( $info, 'decimal', 0 );

    if ( !wi_check_decimal( $value, $decimal ) )
        wi_error( 342 );

    $numeric = (float)$value;

    $min = wi_metadata( $info, 'min-value', false );
    if ( $min !== false && $numeric < (float)$min )
        wi_error( 342 );

    $max = wi_metadata( $info, 'max-value', false );
    if ( $max !== false && $numeric > (float)$max )
        wi_error( 342 );

    return number_format( $numeric, $decimal, '.', '' );
}

function wi_verify_datetime_value_info( $info, $value )
{
    $reg_date = '(\d\d\d\d)-(\d\d)-(\d\d)';
    $reg_time = '(\d\d):(\d\d)';

    $time = wi_metadata( $info, 'time', 0 );
    if ( $time )
        $reg = "/^$reg_date $reg_time$/";
    else
        $reg = "/^$reg_date$/";

    if ( !preg_match( $reg, $value, $matches ) )
        wi_error( 342 );

    if ( !checkdate( $matches[ 2 ], $matches[ 3 ], $matches[ 1 ] ) )
        wi_error( 342 );

    if ( $time ) {
        if ( $matches[ 4 ] < 0 || $matches[ 4 ] > 23 || $matches[ 5 ] < 0 || $matches[ 5 ] > 59 )
            wi_error( 342 );
    }

    return $value;
}

function wi_verify_user_value_info( $info, $value, $project_id )
{
    $query = 'SELECT u.user_id FROM {users} AS u';
    if ( wi_metadata( $info, 'members', 0 ) && $project_id != 0 )
        $query .= ' JOIN {rights} AS r ON r.user_id = u.user_id AND r.project_id = %2$d';
    $query .= ' WHERE u.user_name = %1$s';

    if ( !wi_query_row( $query, $value, $project_id ) )
        wi_error( 342 );

    return $value;
}

function wi_metadata( $info, $name, $default )
{
    if ( isset( $info[ 'metadata' ][ $name ] ) )
        return $info[ 'metadata' ][ $name ];
    return $default;
}

function wi_check_decimal( $value, $decimal )
{
    if ( $decimal == 0 )
        $reg = '/^-?\d+$/';
    else
        $reg = '/^-?\d+(?:\.(\d+))?$/';
    if ( !preg_match( $reg, $value, $matches ) )
        return false;
    if ( $decimal > 0 && isset( $matches[ 1 ] ) && strlen( $matches[ 1 ] ) > $decimal )
        return false;
    return true;
}