6f28961d8d1612e9c11df2c074e386807b07283d
[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         const S_IFMT      = 170000;
21         const S_IFGITLINK = 160000;
22         const S_IFLNK     = 120000;
23         const S_IFREG     = 100000;
24         const S_IFDIR     =  40000;
25         const S_IFINVALID =  30000;
26         const S_IXUSR     =    100;
27         
28         public static function get_filetype($mode, $exec = false) {
29                 if(!preg_match('/^[0-7]+$/', $mode))
30                         return $mode;
31                 
32                 if(($mode & self::S_IFMT) == S_IFGITLINK)
33                         return 'submodule';
34                 else if(($mode & self::S_IFMT) == S_IFDIR)
35                         return 'directory';
36                 else if(($mode & self::S_IFMT) == S_IFLNK)
37                         return 'symlink';
38                 else if(($mode & self::S_IFMT) == S_IFREG) {
39                         if($exec && ($mode & self::S_IXUSR))
40                                 return 'executable';
41                         return 'file';
42                 } else
43                         return 'unknown';
44         }
45         
46         public static function age_calculate($last_change) {
47                 $result = array();
48                 $now = time();
49                 $age = ($last_change > 0 ? ($now - $last_change) : 0);
50                 
51                 if ($age > 60*60*24*365*2) {
52                         $age_str = floor($age/60/60/24/365);
53                         $age_str .= " years ago";
54                         $max_cache = (60*60*24*365) - ($age % (60*60*24*365));
55                 } else if ($age > 60*60*24*(365/12)*2) {
56                         $age_str = floor($age/60/60/24/(365/12));
57                         $age_str .= " months ago";
58                         $max_cache = min((60*60*24*(365/12)) - ($age % (60*60*24*(365/12))), (60*60*24*365*2) - $age);
59                 } else if ($age > 60*60*24*7*2) {
60                         $age_str = floor($age/60/60/24/7);
61                         $age_str .= " weeks ago";
62                         $max_cache = min((60*60*24*7) - ($age % (60*60*24*7)), (60*60*24*(365/12)*2) - $age);
63                 } else if ($age > 60*60*24*2) {
64                         $age_str = floor($age/60/60/24);
65                         $age_str .= " days ago";
66                         $max_cache = min((60*60*24) - ($age % (60*60*24)), (60*60*24*7*2) - $age);
67                 } else if ($age > 60*60*2) {
68                         $age_str = floor($age/60/60);
69                         $age_str .= " hours ago";
70                         $max_cache = min((60*60) - ($age % (60*60)), (60*60*24*2) - $age);
71                 } else if ($age > 60*2) {
72                         $age_str = floor($age/60);
73                         $age_str .= " min ago";
74                         $max_cache = min(60 - ($age % 60), (60*60*2) - $age);
75                 } else if ($age > 2) {
76                         $age_str = $age;
77                         $age_str .= " sec ago";
78                         $max_cache = 1;
79                 } else if ($age >= 0) {
80                         $age_str = "right now";
81                         $max_cache = 1;
82                 } else {
83                         $max_cache = -1;
84                 }
85                 if($age == 0) {
86                         $age_class = "noage";
87                 } else if ($age < 60*60*2) {
88                         $age_class = "age0";
89                 } else if ($age < 60*60*24*2) {
90                         $age_class = "age1";
91                 } else {
92                         $age_class = "age2";
93                 }
94                 
95                 return array("age_str" => $age_str, "age_class" => $age_class, "max_cache" => $max_cache);
96         }
97         
98         public static function chop_text($text, $len, $add_len) {
99                 if(strlen($text) <= $len + $add_len)
100                         return $text;
101                 $ctext = substr($text, 0, $len);
102                 for($i = 0; $i < $add_len; $i++) {
103                         if($text[$len+$i] == ' ') 
104                                 break;
105                         $ctext .= $text[$len+$i];
106                 }
107                 $ctext .= "...";
108                 return $ctext;
109         }
110         
111         public static function parseTimeZone($timezone) {
112                 if(!preg_match("/^([+-])([0-9]{2})([0-9]{2})$/i", $timezone, $treffer))
113                         return 0;
114                 $offset = ($treffer[1] == '-' ? -1 : 1) * (($treffer[2] * 3600) + ($treffer[3] * 60));
115                 return $offset;
116         }
117         
118 }
119
120 ?>