root/alternc/tags/0.9.7/bureau/class/functions.php

Revision 1945, 10.7 kB (checked in by azerttyu, 1 year ago)

revert des commit [1944], [1939], [1933], [1925] : nouveau bureau.
On recommencera mais en patchant la branche /franck-desktop

Line 
1 <?php
2 /*
3  $Id: functions.php,v 1.9 2005/12/18 09:50:59 benjamin Exp $
4  ----------------------------------------------------------------------
5  AlternC - Web Hosting System
6  Copyright (C) 2002 by the AlternC Development Team.
7  http://alternc.org/
8  ----------------------------------------------------------------------
9  Based on:
10  Valentin Lacambre's web hosting softwares: http://altern.org/
11  ----------------------------------------------------------------------
12  LICENSE
13
14  This program is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License (GPL)
16  as published by the Free Software Foundation; either version 2
17  of the License, or (at your option) any later version.
18
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU General Public License for more details.
23
24  To read the license please visit http://www.gnu.org/copyleft/gpl.html
25  ----------------------------------------------------------------------
26  Original Author of file: Benjamin Sonntag, Franck Missoum
27  Purpose of file: Miscellaneous functions globally used
28  ----------------------------------------------------------------------
29 */
30
31 /* seed the random number generator : */
32 list($usec, $sec) = explode(' ', microtime());
33 mt_srand((float) $sec + ((float) $usec * 100000));
34
35 /* Format a field value for input or textarea : */
36 function fl($str) { return str_replace("<","&lt;",str_replace("\"","&quot;",$str)); }
37
38 /*
39  Check if a domain can be hosted on this server :
40  Return a negative value in case of an error,
41  or a string for the index in $tld
42 */
43 function checkhostallow($domain,$dns) {
44   global $L_NS1,$L_NS2,$db;
45   $sizefound=0;
46   $found="";
47   $db->query("SELECT tld,mode FROM tld;");
48   while ($db->next_record()) {
49     list($key,$val)=$db->Record;
50     if (substr($domain,-1-strlen($key))==".".$key) {
51       if ($sizefound<strlen($key)) {
52     $sizefound=strlen($key);
53     $found=$key;
54     $fmode=$val;
55       }
56     }
57   }
58
59   if (!$found || $fmode==0)            // TLD not allowed at all
60     return -1;
61   if (($fmode!=4) && (!is_array($dns)))    // NO dns found in the whois, and domain MUST exists
62     return -2;
63   if ($fmode>2)        // OK, in the case 3 4 5
64     return $found;
65   $n1=false;    $n2=false;
66   for ($i=0;$i<count($dns);$i++) {
67     if ($dns[$i]==$L_NS1) $n1=true;
68     if ($dns[$i]==$L_NS2) $n2=true;
69   }
70   if ($fmode==1 && $n1)        // OK
71     return $found;
72   if ($fmode==2 && $n1 && $n2)        // OK
73     return $found;
74   return -3;    // DNS incorrect in the whois
75 }
76
77 /* Check that a domain can be hosted in that server,
78 without DNS managment.
79 */
80 function checkhostallow_nodns($domain) {
81   global $db;
82   $sizefound=0;
83   $found="";
84   $db->query("SELECT tld,mode FROM tld;");
85   while ($db->next_record()) {
86     list($key,$val)=$db->Record;
87     if (substr($domain,-1-strlen($key))==".".$key) {
88       if ($sizefound<strlen($key)) {
89     $sizefound=strlen($key);
90     $found=$key;
91     $fmode=$val;
92       }
93     }
94   }
95   // If we found a correct tld, let's find how many . before ;)
96   if (!$found || $fmode==0)                       // TLD not allowed at all
97     return 1;
98   if (count(explode(".",substr($domain,0,-$sizefound)))>2) {
99     return 1;
100   }
101   return 0;
102 }
103
104 /* Check that $url is a correct url (http:// or https:// or ftp://)  */
105 function checkurl($url) {
106   // TODO : add a path/file check
107   if (substr($url,0,7)!="http://" && substr($url,0,8)!="https://" && substr($url,0,6)!="ftp://") return false;
108   if (substr($url,0,7)=="http://" ) $fq=substr($url,7);
109   if (substr($url,0,8)=="https://") $fq=substr($url,8);
110   if (substr($url,0,6)=="ftp://"  ) $fq=substr($url,6);
111   $f=explode("/",$fq);
112   if (!is_array($f)) $f=array($f);
113   $t=checkfqdn($f[0]);
114   if ($t) return false;
115   return true;
116 }
117
118 /* Check that $ip is a correct 4 Dotted ip */
119 function checkip($ip) {
120   // return true or false whether the ip is correctly formatted
121   if (!ereg("[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*",$ip)) return false;
122   $l=explode(".",$ip);
123   if ($l[0]>255 || $l[1]>255 || $l[2]>255 || $l[3]>255) return false;
124   return true;
125 }
126
127 /* Check a login mail */
128 function checkloginmail($mail) {
129   if (!preg_match("/^[a-zA-Z0-9_\.:\+\-]+$/",$mail)) {
130     return false;
131   } else {
132     return true;
133   }
134 }
135 /* " */
136
137 /* Check an email address, use checkloginmail and checkfqdn */
138 function checkmail($mail) {
139   // Retourne 0 si tout va bien, sinon retourne un code erreur...
140   // 1 s'il n'y a rien devant l'@
141   // 2 3 ou 4 si le domaine est incorrect.
142   // 5 s'il y a caractères interdits dans la partie gauche du @
143   // 6 si le mail contient aucun ou plus d'un @
144   $t=explode("@",$mail);
145   if (count($t)!=2) {
146     return 6;
147   }
148   $c=checkfqdn($t[1]);
149   if ($c)
150     return $c;
151   // Verification de la partie gauche :
152   if (!checkloginmail($t[0])) {
153     if ($t[0]=="") {
154         return 1;
155     } else {
156         return 5;
157     }
158   }
159   return 0;
160 }
161
162 /* Check that a domain name is fqdn compliant */
163 function checkfqdn($fqdn) {
164   // (RFC 1035 http://www.ietf.org/rfc/rfc1035.txt)
165   // Retourne 0 si tout va bien, sinon, retourne un code erreur...
166   // 1. Nom de domaine complet trop long.
167   // 2. L'un des membres est trop long.
168   // 3. Caractere interdit dans l'un des membres.
169   // 4. Le fqdn ne fait qu'un seul membre (il n'est donc pas fq...)
170   if (strlen($fqdn)>255)
171     return 1;
172   $members=explode(".", $fqdn);
173   if (count($members)>1) $ret=0; else $ret=4;
174   reset($members);
175   while (list ($key, $val) = each ($members)) {
176     if (strlen($val)>63)
177       return 2;
178     if (!eregi("^[a-z0-9][a-z0-9-]*[a-z0-9]$",$val)) {
179       /*"*/                  return 3;
180     }
181   }
182   return $ret;
183 }
184
185 function checkuserpath($path) {
186   /*
187     return 0 if the path is not in the user's space
188     return 1 if this is a directory
189     return 2 if this is a regular file
190   */
191   global $mem;
192   $user=$mem->user["login"];
193   $usar=substr($user,0,1);
194   if (substr($path,0,1)=="/")
195     $path="/".$path;
196
197   $rpath = realpath("/var/alternc/html/$usar/$user$path");
198   $userpath = realpath("/var/alternc/html/$usar/$user");
199   if(strpos($rpath,$userpath) === 0){
200     if (is_dir("/var/alternc/html/$usar/$user$path")) {
201         return 1;
202     }
203     if (is_file("/var/alternc/html/$usar/$user$path")) {
204       return 2;
205     }
206   }
207   return 0;
208 }
209
210 /**
211  * get the home of the user
212  *
213  * @args string $user the username, if null will use the global $mem. no
214  * security checks performed on path
215  * @returns string the actual absolute path
216  * @see $L_ALTERNC_LOC
217  */
218 function getuserpath($user = null) {
219   global $L_ALTERNC_LOC;
220   if (is_null($user)) {
221     global $mem;
222     $user = $mem->user['login'];
223   }
224   return $L_ALTERNC_LOC . "/html/".substr($user,0,1)."/".$user;
225 }
226
227 function cbox($test) {
228   if ($test) echo (" checked=\"checked\"");
229 }
230
231 function ecif($test,$tr,$fa="") {
232   if ($test)
233     echo $tr;
234   else
235     echo $fa;
236 }
237
238 function __($str) {
239   echo _($str);
240 }
241
242 function ife($test,$tr,$fa="") {
243   if ($test)
244     return $tr;
245   else
246     return $fa;
247 }
248
249 function format_size($size) {
250   // Retourne une taille formattée en Octets, Kilo-octets, Méga-octets ou Giga-Octets, avec 2 décimales.
251   if ("-" == $size) {
252     return $size;
253   }
254   $size=(float)$size;
255   if ($size<1024) {
256     $r=$size;
257     if ($size!=1) {
258       $r.=" "._("Bytes");
259     } else {
260       $r.=" "._("Byte");
261     }
262   } else {
263     $size=$size/1024;
264     if ($size<1024) {
265       $r=round($size,2)." "._("Kb");
266     } else {
267       $size=$size/1024;
268       if ($size<1024) {
269     $r=round($size,2)." "._("Mb");
270       } else {
271     $size=$size/1024;
272     if ($size<1024) {
273       $r=round($size,2)." "._("Gb");
274     } else {
275       $r=round($size/1024,2)." "._("Tb");
276     }
277       }
278     }
279   }
280   return $r;
281 }
282
283 function getlinkhelp($hid) {
284   return "(<a href=\"javascript:help($hid);\">?</a>)";
285 }
286 function linkhelp($hid) {
287   echo getlinkhelp($hid);
288 }
289
290 function format_date($format,$date) {
291   $d=substr($date,8,2);
292   $m=substr($date,5,2);
293   $y=substr($date,0,4);
294   $h=substr($date,11,2);
295   $i=substr($date,14,2);
296   if ($h>12) {
297     $hh=$h-12;
298     $am="pm";
299   } else {
300     $hh=$h;
301     $am="am";
302   }
303   return sprintf(_($format),$d,$m,$y,$h,$i,$hh,$am);
304 }
305
306 /* Strip slashes if needed : */
307 function ssla($str) {
308   if (get_magic_quotes_gpc()) {
309     return stripslashes($str);
310   } else {
311     return $str;
312   }
313 }
314
315   /* ----------------------------------------------------------------- */
316   /** Hashe un mot de passe en clair en MD5 avec un salt aléatoire
317    * @param string $pass Mot de passe à crypter (max 32 caractères)
318    * @return string Retourne le mot de passe crypté
319    * @access private
320    */
321   function _md5cr($pass,$salt="") {
322     if (!$salt) {
323       $chars="./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
324       for ($i=0;$i<12;$i++) {
325     $salt.=substr($chars,(mt_rand(0,strlen($chars))),1);
326       }
327       $salt="$1$".$salt;
328     }
329     return crypt($pass,$salt);
330   }
331
332 /** split mysql database name between username and custom database name
333  * @param string $dbname database name
334  * @return array returns username as first element, custom name as second
335  */
336 function split_mysql_database_name($dbname) {
337     $db_exploded_name = explode("_",$dbname);
338     return array($db_exploded_name[0],
339                  implode("_", array_slice($db_exploded_name, 1)));
340 }
341
342
343 /* ----------------------------------------------------------------- */
344 /** Echappe les caractères pouvant perturber un flux XML standard :
345  * @param string $string Chaine de caractère à encoder en valeur xml.
346  * @return string Retourne la chaîne modifiée si besoin.
347  * @access private
348  */
349 function xml_entities($string) {
350   return str_replace("<","&lt;",str_replace(">","&gt;",str_replace("&","&amp;",$string)));
351 }
352
353 /* ----------------------------------------------------------------- */
354 /** Converti un nombre de mois en une chaine plus lisible
355  * @param  number $months Nombre de mois
356  * @return string Chaîne représentant le nombre de mois
357  * @access private
358  */
359 function pretty_months($months) {
360   if( $months % 12 == 0 && $months > 11) {
361     $years = $months / 12;
362     return "$years " . ($years > 1 ? _("years") : _("year"));
363   } else {
364     return "$months " . ($months > 1 ? _("months") : _("month"));
365   }
366 }
367
368 /* ----------------------------------------------------------------- */
369 /** Fabrique un drop-down pour les durées de comptes
370  * @name string $name Nom pour le composasnt
371  * @selected number Option selectionée du composant
372  * @return string Code html pour le drop-down
373  * @access private
374  */
375 function duration_list($name, $selected=0) {
376   $res = "<select name=\"$name\" id=\"$name\">";
377
378   foreach(array(0, 1, 2, 3, 4, 6, 12, 24) as $dur) {
379     $res .= "<option value=\"$dur\"";
380     if($selected == $dur) {
381       $res .= ' selected';
382     }
383
384     $res .= '>';
385
386     if($dur == 0) {
387       $res .= _('Not managed');
388     } else {
389       $res .= pretty_months($dur);
390     }
391     $res .= '</option>';
392   }
393
394   $res .= '</select>';
395   return $res;
396 }
397
398
399 ?>
400
Note: See TracBrowser for help on using the browser.