File: valchecks.php

package info (click to toggle)
gallery 1.5.4-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 26,712 kB
  • ctags: 6,567
  • sloc: php: 33,824; sh: 446; xml: 96; makefile: 88; perl: 61
file content (150 lines) | stat: -rw-r--r-- 4,887 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
<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2006 Bharat Mediratta
 * 
 * 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.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * $Id: valchecks.php 13579 2006-05-02 09:59:13Z jenst $
 */
?>
<?php

/**
 * This is a wrapper around different valchecks
 * The input is the value, the type its tested against and optional a default
 * The return is given by the valcheck function
*/
function sanityCheck($var, $type, $default = NULL, $choices = array()) {
      switch ($type) {
        case 'int':
            return isValidInteger($var, true, NULL, true);
            break;
        case 'int_notnull':
            return isValidInteger($var, true, $default, false);
            break;
        case 'int_empty':
            return isValidInteger($var, true, $default, true);
            break;
        case 'pictureFrame':
            if(array_key_exists($var, available_frames())) {
                return array(0, $var, '');
            }
            else {
                return array(2, $var, _("The given frame is not valid."));
            }
            break;
        case 'inChoice':
            if(in_array($var, $choices)) {
                return array(0, $var, '');
            }
            elseif (isset($default)) {
                return array(1, $default, _("Value was set to given default. Because the original value is not a in the allowed amount of choices."));
            }
            else {
                return array(2, $var, _("The given value is not in the allowed amount of choices."));
            }
            break;
        default:
        case 'text':
            return isValidText($var, $default);
            break;
	}
}

/**
 * This function checks if a given value is a valid integer.
 * Valid means:
 * --- its a numeric value
 * --- is not lower a given minum (can be 1 or 0)
 * You can give a default to correct an invalid input
 *
 * Return is an array that contains:
 * --- Status, can be 0 for OK, 1 for set to default, 2 failure and no default
 * --- Original or default value
 * --- Debug message
*/
function isValidInteger($mixed, $includingZero = false, $default = NULL, $emptyAllowed = false) {
    $minimum = ($includingZero == true) ? 0 : 1;

    if ( $mixed == '' && $emptyAllowed) {
	return array(0, $mixed, '');
    }
	
    if (! is_numeric($mixed)) {
	if (isset($default)) {
	    return array(1,$default, _("Value was set to given default. Because the original value is not numeric."));
	} else {
	    return array(2, false, _("The given Value is not numeric."));
	}
    }

    if($mixed < $minimum) {
	if (isset($default)) {
            return array(1, $default, _("Value was set to given default. Because the original value is not a valid Integer"));
        } else {
            return array(2, false, _("The given Value not a valid Integer."));
        }
    }

    return array(0, $mixed, '');
}

function isValidText($text, $default = NULL) {
    $sanitized = sanitizeInput($text);
    if($sanitized != $text) {
        if(isset($default)) {
            return array(1, $default, _("Value was set to given default. Because the original value is not a valid text"));
        }
        else {
            return array(1, $sanitized, _("Value was corrected, because the original value is not a valid text"));
        }
    }
    else {
        return array(0, $text, '');
    }
}

function sanitizeInput($value) {
    if(!is_array($value) && strip_tags($value) == $value) {
        return $value;
    }

    require_once(dirname(dirname(__FILE__)) .'/classes/HTML_Safe/Safe.php');
    static $safehtml;

    if (empty($safehtml)) {
        $safehtml =& new HTML_Safe();
    }

    if(is_array($value)) {
        //echo "\n -> Array";
        //echo "\n<ul>";
        foreach($value as $subkey => $subvalue) {
            //printf("\n<li>Checking SubValue: %s", htmlspecialchars($subkey));
            $sanitized[$subkey] = sanitizeInput($subvalue);
        }
        //echo "\n</ul>";
    }
    else {
        //echo " === ". htmlspecialchars($value);
        $sanitized = $safehtml->parse($value);
        if($sanitized != $value) {
            //echo "--->". $sanitized;
        }
    }
    return $sanitized;
}
?>