continued :)
[phpgitweb.git] / htdocs / lib / Validation.class.php
1 <?php
2 /* Validation.class.php - phpgitweb
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License 
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
17  */
18
19 class Validation {
20         
21         public static function validate_path($path) {
22                 /* Path validation #1
23                 * no '.' or '..' as elements of path, i.e. no '.' nor '..'
24                 * at the beginning, at the end, and between slashes.
25                 * also this catches doubled slashes
26                 */
27                 if(preg_match('#(^|/)(|\.|\.\.)(/|$)#', $path))
28                         return false;
29                 
30                 /* Path validation #2
31                 * no null characters
32                 */
33                 if(preg_match('#\0#', $path))
34                         return false;
35                 
36                 return true;
37         }
38         
39         public static function validate_hash($hash) {
40                 /* Hash validation #1
41                 * regular hashes [a-f0-9] are always ok
42                 */
43                 if(preg_match('#^[a-f0-9]{1,40}$#i', $hash))
44                         return true;
45                 
46                 /* Hash validation #2
47                 * must be a valid path
48                 */
49                 if(!self::validate_path($hash))
50                         return false;
51                 
52                 /* Hash validation #3
53                 * restrictions on ref name according to git-check-ref-format
54                 */
55                 if(preg_match('#(\.|\.\.|[\000-\040\177 ~^:?*\[\]]|/$)#', $hash))
56                         return false;
57                 
58                 return true;
59         }
60         
61 }
62
63 ?>