-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathicon.php
53 lines (42 loc) · 1.11 KB
/
icon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
// Message d'erreur et échec.
function error ($message)
{
printf ('{"success":false,"error":"%s"}', $message);
exit (1);
}
if (! array_key_exists ('url', $_GET))
error ('url');
if (! array_key_exists ('dir', $_GET))
error ('dir');
// Enregistrement du buffer.
function w ($dir, $file, $data)
{
if (file_exists ($dir . '/' . $file))
error ('file_exists');
file_put_contents ($dir . '/' . $file, $data);
printf ('{"success":true,"result":"%s"}', $file);
exit (0);
}
// Chargement du fichier, détection du type MIME et enregistrement.
function f ($url, $dir)
{
$data = @file_get_contents ($url);
if ($data === false)
error ('file_get_contents');
$finfo = finfo_open (FILEINFO_MIME);
$mime = finfo_buffer ($finfo, $data);
if (preg_match ('/^image\/([^;]*)(;.*)?$/', $mime, $matches))
{
switch ($matches[1])
{
case 'jpeg' : w ($dir, 'icon.jpg', $data);
case 'png' : w ($dir, 'icon.png', $data);
case 'webp' : w ($dir, 'icon.webp', $data);
case 'gif' : w ($dir, 'icon.gif', $data);
}
}
error ('mime_type');
}
f ($_GET['url'], $_GET['dir']);
?>