Projet

Général

Profil

Paste
Télécharger (2,18 ko) Statistiques
| Branche: | Révision:

root / class / utils.class.php @ afe20e9f

1
<?php
2

    
3
function __autoload($class) {
4
  $class_loc = 'class/'.$class.'.class.php';
5
  if (is_readable($class_loc)) {
6
          require_once($class_loc);
7
  }
8
}
9

    
10
function errorToException($code, $msg, $file, $line) {
11
  throw new Exception($msg);
12
}
13
set_error_handler('errorToException');
14

    
15

    
16
abstract class utils {
17
  static public function list_available_panos($base_dir) {
18
          /** Lists all that can be turned into a panorama
19
           */
20
          $dir = opendir($base_dir);
21
          $ret = array();
22
          $finfo = finfo_open(FILEINFO_MIME_TYPE); // Retourne le type mime du fichier
23

    
24
          while(false !== ($filename = readdir($dir))) {
25
                  if (!preg_match('/^\.\.?$/', $filename)) {
26
                          $ftype = finfo_file($finfo, $base_dir.'/'.$filename);
27
                          if (isset($ftype)) {
28
                                  $pano = array(
29
                                    'comment' =>  $filename,
30
                                    'title' => sprintf('fichier de type %s', $ftype)
31
                                  );
32
                          } else {
33
                                  $pano = array(
34
                                    'comment' =>  sprintf('<samp>%s</samp>', $filename),
35
                                    'title' => ''
36
                                  );
37
                          }
38
                          $pano['filename'] = $filename;
39
                          $ret[] = $pano;
40
                  }
41
          }
42
          return $ret;
43
  }
44
  public static function strip_extension($filename) {
45
          /** Removes the extension from a file name
46
           * @return the stripped name
47
           */
48
          return preg_replace('/\.[^.]+$/', '', $filename);
49
  }
50

    
51
  public static function php2ini($v) {
52
          /** convert php var to a string representing it in an ini file.
53
           * @return a string, ready to be inserted into a ini file.
54
           */
55
    if (is_numeric($v)) {
56
      return $v;
57
    }
58
    $type = gettype($v);
59
    switch($type) {
60
      case 'boolean': return $v ? "true" : "false";
61
      default: return '"'.$v.'"';
62
    }
63
    return $v;
64
  }
65

    
66
  public static function get_unique_filepath($path) {
67
    /** To handle uploads with same name : for a given path, suffix it with _<n>
68
    (keeping trailing extension)
69
    * till it find a non-preexistant_path and returns it.
70
    */
71
    if (file_exists($path)) {
72
      $info = pathinfo($path);
73
      $extension = $info['extension'];
74
      $remain = self::strip_extension($path);
75
      $n = 0;
76
      do {
77
        $n++;
78
        $fn = sprintf('%s_%d.%s', $remain, $n, $extension);
79
      } while (file_exists($fn));
80
      return $fn;
81

    
82
    } else {
83
      return $path;
84
    }
85
  }
86

    
87
}
88

    
89
?>