Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / scripts / libraries / fonctions.php @ 4ce9f9ca

Historique | Voir | Annoter | Télécharger (14,4 ko)

1
<?php
2
error_reporting(E_ALL);
3
//debug: E_ALL
4
// ------------------------------------
5
// On rebind la fonction sql_exec pasqu'elle est vraiment lourdingue !
6
// SELECT: retourne le résultat ou le tableau de résultat
7
function sql_exec($req) {
8
  global $dbh;
9
  //sql debug mode
10
  //echo "req = $req\n";
11
  
12
  if ( DB::isError($res = $dbh->query($req)) ) {
13
    die ("***\n" . DB::errorMessage($res) . "\n***\n\n");
14
  }
15
  if ( $res === DB_OK ) {
16
    return;
17
  }
18

    
19
  if ( $row = $res->fetchRow() ) {
20
    if (DB::isError($row)) {
21
      print $row->toString() . "<br/>";
22
      continue;
23
    }
24
  }
25
  
26
  if ( $res->numRows() > 1 ) {
27
    return $res;
28
  } else {
29
    return $row;
30
  }
31
}
32

    
33
// ------------------------------------
34
// Avant d'insérer le nouvel enregistrement on essaye de voir si il n'est
35
// pas déjà présent
36
function ajoute_ou_upgrade_package($packageid, $serverid, $version, $etat) {
37
        $vi = makeintversion($version);
38
        $reqt = "SELECT id " .
39
                        "FROM packages_installed " .
40
                        "WHERE `id_package`='$packageid' and `id_server`='$serverid';";
41
        $rest = sql_exec($reqt);
42
        if ( is_array($rest) ) {
43
                $r = $rest[0];
44
        } else {
45
                $r = $rest;
46
        }
47
        if ( $r ) {
48
                $req1 = "UPDATE packages_installed " .
49
                                "SET `inst_version`='$version', `inst_version_int`='$vi' " .
50
                                "WHERE `id`='$r';";
51
                $row1 = sql_exec($req1);
52
        } else {
53
                $req1 = "INSERT INTO packages_installed (id_server, " .
54
                                                                                                "id_package, " .
55
                                                                                                "package_status, " .
56
                                                                                                "inst_version, " .
57
                                                                                                "inst_version_int ) " .
58
                                "VALUES ('$serverid', '$packageid', '$etat', '$version', $vi);";
59
                $row1 = sql_exec($req1);
60
        }
61
}
62

    
63
// ------------------------------------
64
// Vérification que ce serveur est bien enregistré dans la base
65
// ... si jamais il n'est pas dans la base on expédie un rapport
66
function get_serveruid ( $serveruid, $str="" ) {
67
        global $mailto, $mail_subject;
68
        
69
        // Détection du "format" de la chaîne SERVERUID contenue dans le mail
70
        // Les anciennes versions sont du style SERVERUID=AA:BB:CC:DD:EE:FF;AA:BB:CC:DD:EE:GG
71
        // Les nouvelles versions sont du style SERVERUID=AABBCCDDEEFF
72
        // ...donc recherche du caractère ";"
73
        if (strstr($serveruid,";")) {
74
                // On a une ancienne forme...
75
                $t = explode(";",$serveruid);
76
                if ( strstr($t[0],":") ) {
77
                        $t[0] = str_replace(":","",$t[0]);
78
                }
79
                if ( strstr($t[1], ":") ) {
80
                        $t[1] = str_replace(":", "", $t[1]);
81
                }
82
                
83
                $req = "SELECT id " .
84
                                "FROM servers " .
85
                                "WHERE (`mac_address_0`='" . $t[0] . "' " .
86
                                "   AND `mac_address_1`='" . $t[1] . "') " .
87
                                "OR    (`mac_address_0`='" . $t[1] . "' " .
88
                                "   AND `mac_address_1`='" . $t[0] . "') " .
89
                                "OR    (`mac_address_0`='" . strtolower($t[0]) . "' " .
90
                                "   AND `mac_address_1`='" . strtolower($t[1]) . "') " .
91
                                "OR    (`mac_address_0`='" . strtolower($t[1]) . "' " .
92
                                "   AND `mac_address_1`='" . strtolower($t[0]) . "');";
93
                // print "R: $req\n";
94
        } else {
95
                // Nouvelle forme ("horizon-system-tools")
96
                $req = "SELECT id " .
97
                       "FROM servers " .
98
                       "WHERE `mac_address_0` LIKE '" . $serveruid . "' ".
99
                       "OR `mac_address_1` LIKE '" . $serveruid . "' ".
100
                       "OR `mac_address_0` LIKE '" . strtoupper($serveruid) . "' ".
101
                       "OR `mac_address_1` LIKE '" . strtoupper($serveruid) . "';";
102
        }
103
        
104
        $res = sql_exec($req);
105
        
106
        if ( is_array($res) ) {
107
                $r = $res[0];
108
        }
109
        
110
        if ( isset($r) ) {
111
                return $r;
112
        } else {
113
                $message = "Tentative de get_serveruid ($serveruid)\n" .
114
                                        "STR: $str";
115
                mail($mailto,$mail_subject,$message);
116
                return -1;
117
        }
118
}
119

    
120
// ------------------------------------
121
// Récupration du serveurname
122
function get_servername($id,$str) {
123
  global $mailto, $mail_subject;
124
  $req = "SELECT name FROM servers WHERE id='" . addslashes($id) . "';";
125
  $row = sql_exec($req);
126
  if (is_array($row)) {
127
    $r = $row[0];
128
  } else {
129
    $r = $row;
130
  }
131
  
132
  if(isset($r))
133
    return $r;
134
  else {
135
    $message = "tentative de get_servername $id
136
STR:$str";
137
    mail($mailto,$mail_subject,$message);
138
    return -1;
139
  }
140
}
141

    
142
// ------------------------------
143
// transforme une date au format mysql en date "normale"
144
// 2003-10-05 en 05/10/2003 ...
145
function MysqlDateToFrDate($s,$h = 1) {
146
  $masque = "d/m/Y";
147
  if($h == 1)
148
    $masque .= " G:i:s";
149
  return date($masque,strtotime($s));
150
  //implode('/',array_reverse(split('[-]',$s)));
151
}
152

    
153
// ------------------------------
154
// On a des versions de paquets qui sont en 1:1.2.3
155
// Il faut donc virer le 1:
156
function cleanversion($s) {
157
        $masque = "/(.:)?(.*)/";
158
        $r = preg_replace($masque, "\${2}", $s);
159
        //  print "$s -> $r\n";
160
        return preg_replace($masque, "\${2}", $s);
161
}
162

    
163
// ------------------------------
164
// transforme une version en un bigint qui permet la comparaison !
165
function makeintversion($s) {
166
        $max = 6;
167
        $masque = "/(.:)?([\d\.]*)[-+]?.*/";
168
        $i = preg_replace($masque, "\${2}", $s);
169
        //  print "$s -> $i\n";
170
        $tab = @explode( ".", preg_replace("/\(\w\D\)/i", "", $i) );
171
        
172
        /*
173
         * $masque = "/[-+[:alpha:]]/";
174
         * $i = preg_split($masque,$s);
175
         * $tab = explode(".",preg_replace("/\(\w\D\)/i","",$i[0]));
176
         */
177
        for ( $i = 0; $i < count($tab) || $i < $max; $i++) {
178
                //Modifier le 03 par ce qu'on veut pour le masque de sortie
179
                $res .= @sprintf("%03d",$tab[$i]);
180
        }
181
        return $res;
182
}
183

    
184
//Update la date de dernière info sur la table servers
185
function update_datederniereinfo($id) {
186
  $r = "UPDATE servers SET datederniereinfo=now() WHERE id='$id'";
187
  // $s = sql_exec($r);
188
}
189

    
190
//Retourne le diff -u système des deux fichiers
191
function systemdiff($f1,$f2,$filename) {
192
        $t = '';
193
        $tf1 = tempnam("/tmp",basename("$filename") . "_1-");
194
        $tf2 = tempnam("/tmp",basename("$filename") . "_2-");
195
        
196
        $handle = fopen($tf1, "w");
197
        fwrite($handle, $f1);
198
        fclose($handle);
199
        
200
        $handle = fopen($tf2, "w");
201
        fwrite($handle, $f2);
202
        fclose($handle);
203
        
204
        exec("diff -u $tf1 $tf2",$t);
205
        $r = implode("\n",$t);
206
        unlink($tf1);
207
        unlink($tf2);
208
        return $r;
209
}
210

    
211

    
212
// ---------------------------------------------
213
// On vire les espaces ponctuations etc.
214
// agressif = 0 on est gentil - on ne fait rien
215
//          = 1 on vire les accents + tout en minuscules
216
//          = 2 on vire aussi les tirets et espaces (login / pass)
217
function anti_speciaux($t, $agressif=0) {
218
  if(trim($t) == "")
219
    return ;  
220
  if($agressif >= 1) {
221
    $texte = strtolower($t);
222
    $texte = remove_accents($texte);
223
    if($agressif == 2) {
224
      // On vire les espaces
225
      $texte = str_replace(" ","",$texte);
226
      $texte = str_replace("-","",$texte);
227
      $texte = str_replace("_","",$texte);
228
      //le python accepte - et _ dans le login apres une lettre
229
    }
230
  }
231
  else {
232
    $texte=$t;
233
  }  
234
  return $texte;
235
}
236

    
237
/**
238
 * Code from wordpress - wordpress.org
239
 * Converts all accent characters to ASCII characters.
240
 *
241
 * If there are no accent characters, then the string given is just returned.
242
 *
243
 * @since 1.2.1
244
 *
245
 * @param string $string Text that might have accent characters
246
 * @return string Filtered string with replaced "nice" characters.
247
 */
248
function remove_accents($string) {
249
  if ( !preg_match('/[\x80-\xff]/', $string) )
250
    return $string;
251
  
252
  if (seems_utf8($string)) {
253
    $chars = array(
254
                   // Decompositions for Latin-1 Supplement
255
                   chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
256
                   chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
257
                   chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
258
                   chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
259
                   chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
260
                   chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
261
                   chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
262
                   chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
263
                   chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
264
                   chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
265
                   chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
266
                   chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
267
                   chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
268
                   chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
269
                   chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
270
                   chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
271
                   chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
272
                   chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
273
                   chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
274
                   chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
275
                   chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
276
                   chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
277
                   chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
278
                   chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
279
                   chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
280
                   chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
281
                   chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
282
                   chr(195).chr(191) => 'y',
283
                   // Decompositions for Latin Extended-A
284
                   chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
285
                   chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
286
                   chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
287
                   chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
288
                   chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
289
                   chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
290
                   chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
291
                   chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
292
                   chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
293
                   chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
294
                   chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
295
                   chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
296
                   chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
297
                   chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
298
                   chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
299
                   chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
300
                   chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
301
                   chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
302
                   chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
303
                   chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
304
                   chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
305
                   chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
306
                   chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
307
                   chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
308
                   chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
309
                   chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
310
                   chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
311
                   chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
312
                   chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
313
                   chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
314
                   chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
315
                   chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
316
                   chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
317
                   chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
318
                   chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
319
                   chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
320
                   chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
321
                   chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
322
                   chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
323
                   chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
324
                   chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
325
                   chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
326
                   chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
327
                   chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
328
                   chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
329
                   chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
330
                   chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
331
                   chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
332
                   chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
333
                   chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
334
                   chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
335
                   chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
336
                   chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
337
                   chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
338
                   chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
339
                   chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
340
                   chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
341
                   chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
342
                   chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
343
                   chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
344
                   chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
345
                   chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
346
                   chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
347
                   chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
348
                   // Euro Sign
349
                   chr(226).chr(130).chr(172) => 'E',
350
                   // GBP (Pound) Sign
351
                   chr(194).chr(163) => '');
352
    
353
    $string = strtr($string, $chars);
354
  } else {
355
    // Assume ISO-8859-1 if not UTF-8
356
    $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
357
      .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
358
      .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
359
      .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
360
      .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
361
      .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
362
      .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
363
      .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
364
      .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
365
      .chr(252).chr(253).chr(255);
366
    
367
    $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
368
    
369
    $string = strtr($string, $chars['in'], $chars['out']);
370
    $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
371
    $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
372
    $string = str_replace($double_chars['in'], $double_chars['out'], $string);
373
  }
374
  
375
  return $string;
376
}
377

    
378
/**
379
 * Code from wordpress - wordpress.org
380
 * Checks to see if a string is utf8 encoded.
381
 *
382
 * NOTE: This function checks for 5-Byte sequences, UTF8
383
 *       has Bytes Sequences with a maximum length of 4.
384
 *
385
 * @author bmorel at ssi dot fr (modified)
386
 * @since 1.2.1
387
 *
388
 * @param string $str The string to be checked
389
 * @return bool True if $str fits a UTF-8 model, false otherwise.
390
 */
391
function seems_utf8($str) {
392
        $length = strlen($str);
393
        for ($i=0; $i < $length; $i++) {
394
                $c = ord($str[$i]);
395
                if ($c < 0x80) $n = 0; # 0bbbbbbb
396
                elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
397
                elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
398
                elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
399
                elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
400
                elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
401
                else return false; # Does not match any model
402
                for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
403
                        if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
404
                                return false;
405
                }
406
        }
407
        return true;
408
}
409

    
410

    
411
?>
Redmine Appliance - Powered by TurnKey Linux