root/trunk/gui/include/calc-functions.php

Revision 1408, 4.3 kB (checked in by scitech, 4 days ago)

Add default user with password to statistics

Line 
1 <?php
2 /**
3  * ispCP ω (OMEGA) a Virtual Hosting Control System
4  *
5  * @copyright     2001-2006 by moleSoftware GmbH
6  * @copyright     2006-2008 by ispCP | http://isp-control.net
7  * @version     SVN: $Id$
8  * @link         http://isp-control.net
9  * @author         ispCP Team
10  *
11  * @license
12  *   This program is free software; you can redistribute it and/or modify it under
13  *   the terms of the MPL General Public License as published by the Free Software
14  *   Foundation; either version 1.1 of the License, or (at your option) any later
15  *   version.
16  *   You should have received a copy of the MPL Mozilla Public License along with
17  *   this program; if not, write to the Open Source Initiative (OSI)
18  *   http://opensource.org | osi@opensource.org
19  */
20
21 function calc_bars($crnt, $max, $bars_max) {
22     if($max != 0) {
23         $percent_usage = (100*$crnt)/$max;
24     } else {
25         $percent_usage = 0;
26     }
27
28     $bars = ($percent_usage * $bars_max)/100;
29
30     if ($bars > $bars_max) $bars = $bars_max;
31
32     return array(
33                  sprintf("%.2f", $percent_usage),
34                  sprintf("%d", $bars)
35                 );
36 }
37
38 function sizeit($bytes, $from = 'B') {
39
40     switch ($from) {
41         case 'PB':
42             $bytes = $bytes * pow(1024, 5);
43             break;
44         case 'TB':
45             $bytes = $bytes * pow(1024, 4);
46             break;
47         case 'GB':
48             $bytes = $bytes * pow(1024, 3);
49             break;
50         case 'MB':
51             $bytes = $bytes * pow(1024, 2);
52             break;
53         case 'KB':
54             $bytes = $bytes * pow(1024, 1);
55             break;
56         case 'B':
57             break;
58         default:
59             die('FIXME: ' . __FILE__ . ':' . __LINE__);
60             break;
61     }
62
63     if ($bytes == '' || $bytes < 0 ) {
64         $bytes = 0;
65     }
66
67     if ($bytes > pow(1024, 5)) {
68         $bytes = $bytes/pow(1024, 5);
69         $ret   = tr('%.2f PB', $bytes);
70     } else if ($bytes > pow(1024, 4)) {
71         $bytes = $bytes/pow(1024, 4);
72         $ret   = tr('%.2f TB', $bytes);
73     } else if ($bytes > pow(1024, 3)) {
74         $bytes = $bytes/pow(1024, 3);
75         $ret   = tr('%.2f GB', $bytes);
76     } else if ($bytes > pow(1024, 2) ) {
77         $bytes = $bytes/pow(1024, 2);
78         $ret   = tr('%.2f MB', $bytes);
79     } else if ($bytes > pow(1024, 1)) {
80         $bytes = $bytes/pow(1024, 1);
81         $ret   = tr('%.2f KB', $bytes);
82     } else {
83         $ret   = tr('%d B', $bytes);
84     }
85
86     return $ret;
87
88 }
89
90 //
91 // some password managment.
92 //
93
94 /**
95  * Generates a random salt for passwords.
96  *
97  * @param int $min minimum ASCII char
98  * @param int $max maximum ASCII char
99  * @return string Salt for password
100  */
101 function generate_rand_salt($min = 46, $max = 126) {
102     if (CRYPT_BLOWFISH == 1) {
103         $length    = 13;
104         $pre    = '$2$';
105     } elseif (CRYPT_MD5 == 1) {
106         $length    = 9;
107         $pre    = '$1$';
108     } elseif (CRYPT_EXT_DES == 1) {
109         $length    = 9;
110         $pre    = '';
111     } elseif (CRYPT_STD_DES == 1) {
112         $length    = 2;
113         $pre    = '';
114     }
115
116     $salt = $pre;
117
118     for($i = 0; $i < $length; $i++) {
119         $salt .= chr(mt_rand($min, $max));
120     }
121
122     return $salt;
123 }
124
125 function get_salt_from($data) {
126     $salt = substr($data, 0, 2);
127     return $salt;
128 }
129
130 function crypt_user_pass($data) {
131     $res = md5($data);
132     return $res;
133 }
134
135 /**
136  * Encryptes the FTP user password.
137  *
138  * @param string $data the password in clear text
139  * @return string the password encrypted with salt
140  */
141 function crypt_user_pass_with_salt($data) {
142     $res = crypt($data, generate_rand_salt());
143     return $res;
144 }
145
146 function check_user_pass($crdata, $data ) {
147     $salt = get_salt_from($crdata);
148     $udata = crypt($data, $salt);
149     return ($udata == $crdata);
150 }
151
152 function _passgen() {
153     $pw = '';
154
155     for($i = 0; $i <= Config::get('PASSWD_CHARS'); $i++) {
156         $z = 0;
157
158         do {
159             $z = mt_rand(42, 123);
160         } while($z >= 91 && $z <= 96);
161         $pw .= chr($z);
162     }
163     return $pw;
164 }
165
166 function passgen() {
167     $pw = null;
168
169     while ($pw == null || !chk_password($pw, 50, "/[<>]/")) {
170         $pw = _passgen();
171     }
172
173     return $pw;
174
175 }
176
177 function translate_limit_value($value, $autosize = false) {
178     if ($value == -1) {
179         return tr('disabled');
180     } else if ($value == 0){
181         return tr('unlimited');
182     } else {
183         if (!$autosize) {
184             return $value;
185         } else {
186             return sizeit($value, 'MB');
187         }
188     }
189 }
190
191 ?>
192
Note: See TracBrowser for help on using the browser.