]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/php/avatar-upload.php
Merge branch '3.6-release'
[friendica-addons.git] / jappixmini / jappix / php / avatar-upload.php
1 <?php
2
3 /*
4
5 Jappix - An open social platform
6 This is the avatar upload PHP script for Jappix
7
8 -------------------------------------------------
9
10 License: AGPL
11 Author: Vanaryon
12 Last revision: 27/05/11
13
14 */
15
16 // PHP base
17 define('JAPPIX_BASE', '..');
18
19 // Get the needed files
20 require_once('./functions.php');
21 require_once('./read-main.php');
22 require_once('./read-hosts.php');
23
24 // Optimize the page rendering
25 hideErrors();
26 compressThis();
27
28 // Not allowed for a special node
29 if(isStatic() || isUpload())
30         exit;
31
32 // Set a special XML header
33 header('Content-Type: text/xml; charset=utf-8');
34
35 // No file uploaded?
36 if((!isset($_FILES['file']) || empty($_FILES['file'])) || (!isset($_POST['id']) || empty($_POST['id'])))
37         exit(
38 '<jappix xmlns=\'jappix:avatar:post\' id=\'0\'>
39         <error>bad-request</error>
40 </jappix>'
41         );
42
43 // Get the POST vars
44 $id = $_POST['id'];
45 $tmp_filename = $_FILES['file']['tmp_name'];
46 $old_filename = $_FILES['file']['name'];
47
48 // Get the file extension
49 $ext = getFileExt($old_filename);
50
51 // Hash it!
52 $filename = md5($old_filename.time()).$ext;
53
54 // Define some vars
55 $path = JAPPIX_BASE.'/store/avatars/'.$filename;
56
57 // Define MIME type
58 if($ext == 'jpg')
59         $ext = 'jpeg';
60
61 $mime = 'image/'.$ext;
62
63 // Unsupported file extension?
64 if(!preg_match('/^(jpeg|png|gif)$/i', $ext))
65         exit(
66 '<jappix xmlns=\'jappix:avatar:post\' id=\''.$id.'\'>
67         <error>forbidden-type</error>
68 </jappix>'
69         );
70
71 // File upload error?
72 if(!is_uploaded_file($tmp_filename) || !move_uploaded_file($tmp_filename, $path))
73         exit(
74 '<jappix xmlns=\'jappix:file:post\' id=\''.$id.'\'>
75         <error>move-error</error>
76 </jappix>'
77         );
78
79 // Resize the image?
80 if(!function_exists('gd_info') || resizeImage($path, $ext, 96, 96)) {
81         try {
82                 // Encode the file
83                 $binval = base64_encode(file_get_contents($path));
84                 
85                 // Remove the file
86                 unlink($path);
87                 
88                 exit(
89 '<jappix xmlns=\'jappix:file:post\' id=\''.$id.'\'>
90         <type>'.$mime.'</type>
91         <binval>'.$binval.'</binval>
92 </jappix>'
93                 );
94         }
95         
96         catch(Exception $e) {
97                 // Remove the file
98                 unlink($path);
99                 
100                 exit(
101 '<jappix xmlns=\'jappix:file:post\' id=\''.$id.'\'>
102         <error>server-error</error>
103 </jappix>'
104                 );
105         }
106 }
107
108 // Remove the file
109 unlink($path);
110
111 // Something went wrong!
112 exit(
113 '<jappix xmlns=\'jappix:file:post\' id=\''.$id.'\'>
114         <error>service-unavailable</error>
115 </jappix>'
116 );
117
118 ?>