added commit difftree
[phpgitweb.git] / htdocs / lib / Tools.class.php
1 <?php
2 /* Tools.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 Tools {
20         /* octal constants (sys/stat.h) */
21         const S_IFMT      = 0170000;
22         const S_IFGITLINK = 0160000; /* GIT defined */
23         const S_IFLNK     = 0120000;
24         const S_IFREG     = 0100000;
25         const S_IFDIR     = 0040000;
26         const S_IPERMS    = 0000777;
27         const S_IXUSR     = 0000100;
28         
29         public static function get_filetype($mode, $exec = false) {
30                 if(!preg_match('/^[0-7]+$/', $mode))
31                         return $mode;
32                 
33                 $mode = octdec($mode);
34                 
35                 if(($mode & self::S_IFMT) == self::S_IFGITLINK)
36                         return 'submodule';
37                 else if(($mode & self::S_IFMT) == self::S_IFDIR)
38                         return 'directory';
39                 else if(($mode & self::S_IFMT) == self::S_IFLNK)
40                         return 'symlink';
41                 else if(($mode & self::S_IFMT) == self::S_IFREG) {
42                         if($exec && ($mode & self::S_IXUSR))
43                                 return 'executable';
44                         return 'file';
45                 } else
46                         return 'unknown';
47         }
48         
49         public static function is_regular_file($mode) {
50                 $mode = octdec($mode);
51                 return (($mode & self::S_IFMT) == self::S_IFREG);
52         }
53         
54         public static function get_file_permissions($mode) {
55                 $mode = octdec($mode);
56                 return sprintf("%04o",($mode & self::S_IPERMS));
57         }
58         
59         public static function age_calculate($last_change) {
60                 $result = array();
61                 $now = time();
62                 $age = ($last_change > 0 ? ($now - $last_change) : 0);
63                 
64                 if ($age > 60*60*24*365*2) {
65                         $age_str = floor($age/60/60/24/365);
66                         $age_str .= " years ago";
67                         $max_cache = (60*60*24*365) - ($age % (60*60*24*365));
68                 } else if ($age > 60*60*24*(365/12)*2) {
69                         $age_str = floor($age/60/60/24/(365/12));
70                         $age_str .= " months ago";
71                         $max_cache = min((60*60*24*(365/12)) - ($age % (60*60*24*(365/12))), (60*60*24*365*2) - $age);
72                 } else if ($age > 60*60*24*7*2) {
73                         $age_str = floor($age/60/60/24/7);
74                         $age_str .= " weeks ago";
75                         $max_cache = min((60*60*24*7) - ($age % (60*60*24*7)), (60*60*24*(365/12)*2) - $age);
76                 } else if ($age > 60*60*24*2) {
77                         $age_str = floor($age/60/60/24);
78                         $age_str .= " days ago";
79                         $max_cache = min((60*60*24) - ($age % (60*60*24)), (60*60*24*7*2) - $age);
80                 } else if ($age > 60*60*2) {
81                         $age_str = floor($age/60/60);
82                         $age_str .= " hours ago";
83                         $max_cache = min((60*60) - ($age % (60*60)), (60*60*24*2) - $age);
84                 } else if ($age > 60*2) {
85                         $age_str = floor($age/60);
86                         $age_str .= " min ago";
87                         $max_cache = min(60 - ($age % 60), (60*60*2) - $age);
88                 } else if ($age > 2) {
89                         $age_str = $age;
90                         $age_str .= " sec ago";
91                         $max_cache = 1;
92                 } else if ($age >= 0) {
93                         $age_str = "right now";
94                         $max_cache = 1;
95                 } else {
96                         $max_cache = -1;
97                 }
98                 if($age == 0) {
99                         $age_class = "noage";
100                 } else if ($age < 60*60*2) {
101                         $age_class = "age0";
102                 } else if ($age < 60*60*24*2) {
103                         $age_class = "age1";
104                 } else {
105                         $age_class = "age2";
106                 }
107                 
108                 return array("age_str" => $age_str, "age_class" => $age_class, "max_cache" => $max_cache);
109         }
110         
111         public static function chop_text($text, $len, $add_len) {
112                 if(strlen($text) <= $len + $add_len)
113                         return $text;
114                 $ctext = substr($text, 0, $len);
115                 for($i = 0; $i < $add_len; $i++) {
116                         if($text[$len+$i] == ' ') 
117                                 break;
118                         $ctext .= $text[$len+$i];
119                 }
120                 $ctext .= "...";
121                 return $ctext;
122         }
123         
124         public static function parseTimeZone($timezone) {
125                 if(!preg_match("/^([+-])([0-9]{2})([0-9]{2})$/i", $timezone, $treffer))
126                         return 0;
127                 $offset = ($treffer[1] == '-' ? -1 : 1) * (($treffer[2] * 3600) + ($treffer[3] * 60));
128                 return $offset;
129         }
130         
131 }
132
133 ?>