XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
upload.php
Go to the documentation of this file.
1 <?php
2 // $Id: upload.php 967 2012-05-31 04:19:25Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 include '../../../mainfile.php';
12 XoopsLogger::getInstance()->activated = false;
13 XoopsLogger::getInstance()->renderingEnabled = false;
14 
15 function error($message){
16  $data['error'] = 1;
17  $data['message'] = $message;
18  echo json_encode($data);
19  die();
20 }
21 
25 $security = TextCleaner::getInstance()->decrypt(rmc_server_var($_POST, 'rmsecurity', 0), true);
26 $category = rmc_server_var($_POST, 'category', 0);
27 
28 $data = $security; //base64_decode($security);
29 $data = explode("|", $data); // [0] = referer, [1] = session_id(), [2] = user, [3] = token
30 
31 $xoopsUser = new XoopsUser($data[0]);
32 
33 if (!isset($data[1]) || strpos($data[1], RMCURL)===FALSE){
34  error(__('You are not allowed to do this action','rmcommon'));
35 }
36 
37 if (!$xoopsUser){
38  error(__('You are not allowed to do this action','rmcommon'));
39 }
40 
41 if ($category<=0){
42  error(__('Sorry, category has not been specified!','rmcommon'));
43 }
44 
46 if ($cat->isNew()){
47  error(__('Sorry, the specified category has not been found!','rmcommon'));
48 }
49 
50 if ($cat->getVar('status')!='open'){
51  error(__('Sorry, the specified category is closed!','rmcommon'));
52 }
53 
54 if (!$cat->user_allowed_toupload($xoopsUser)){
55  error(__('Sorry, you can not upload images!','rmcommon'));
56 }
57 
58 // Cargamos la imágen
59 $updir = XOOPS_UPLOAD_PATH.'/'.date('Y', time());
60 if (!file_exists($updir)){
61  mkdir($updir);
62  chmod($updir, octdec('0777'));
63 }
64 $updir .= '/'.date('m',time());
65 if (!file_exists($updir)){
66  mkdir($updir);
67  chmod($updir, octdec('0777'));
68 }
69 
70 if (!file_exists($updir.'/sizes')){
71  mkdir($updir.'/sizes');
72  chmod($updir.'/sizes', octdec('0777'));
73 }
74 
75 include RMCPATH.'/class/uploader.php';
76 
77 $uploader = new RMFileUploader($updir, $cat->max_file_size(), array('gif', 'jpg', 'jpeg', 'png'));
78 
79 $err = array();
80 if (!$uploader->fetchMedia('Filedata')){
81  error($uploader->getErrors());
82 }
83 
84 if (!$uploader->upload()){
85  error($uploader->getErrors());
86 }
87 
88 // Insertamos el archivo en la base de datos
89 $image = new RMImage();
90 $image->setVar('title', $uploader->savedFileName);
91 $image->setVar('date', time());
92 $image->setVar('file', $uploader->savedFileName);
93 $image->setVar('cat', $cat->id());
94 $image->setVar('uid', $xoopsUser->uid());
95 
96 if (!$image->save()){
97  unlink($uploader->savedDestination);
98  error(__('File could not be inserted to database!','rmcommon'));
99 }
100 
101 $ret['message'] = '1';
102 $ret['id'] = $image->id();
103 echo json_encode($ret);
104 
105 die();