XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
ImageUploadAction.class.php
1<?php
10
11if (!defined('XOOPS_ROOT_PATH')) {
12 exit();
13}
14
15require_once XOOPS_MODULE_PATH . '/legacy/class/AbstractEditAction.class.php';
16require_once XOOPS_MODULE_PATH . '/legacy/admin/forms/ImageUploadForm.class.php';
17
19{
20 public $mActionForm = null;
21 public $mCategory = null;
22 public $mErrorMessages = [];
23 public $mAllowedExts = ['gif' =>'image/gif', 'jpg' =>'image/jpeg', 'jpeg' =>'image/jpeg', 'png' =>'image/png'];
24
25 public function prepare(&$controller, &$xoopsUser)
26 {
27 $this->mActionForm =new Legacy_ImageUploadForm();
28 $this->mActionForm->prepare();
29 }
30
31 public function getDefaultView(&$controller, &$xoopsUser)
32 {
33 return LEGACY_FRAME_VIEW_INPUT;
34 }
35
36 public function _addErrorMessage($msg)
37 {
38 $this->mErrorMessages[] = $msg;
39 }
40
41 public function execute(&$controller, &$xoopsUser)
42 {
43 $form_cancel = $controller->mRoot->mContext->mRequest->getRequest('_form_control_cancel');
44 if (null !== $form_cancel) {
45 return LEGACY_FRAME_VIEW_CANCEL;
46 }
47
48 $this->mActionForm->fetch();
49 $this->mActionForm->validate();
50
51 if ($this->mActionForm->hasError()) {
52 return $this->getDefaultView($controller, $xoopsUser);
53 }
54
55 $t_imgcat_id = $this->mActionForm->get('imgcat_id');
56
57 $formFile = $this->mActionForm->get('upload');
58 $formFileExt = $formFile->getExtension();
59 $files = [];
60 $targetimages = [];
61
62 if ('zip' === strtolower($formFileExt)) {
63 if (!file_exists(XOOPS_ROOT_PATH . '/class/Archive_Zip.php')) {
64 return LEGACY_FRAME_VIEW_ERROR;
65 }
66 require_once XOOPS_ROOT_PATH . '/class/Archive_Zip.php';
67 $zip = new Archive_Zip($formFile->_mTmpFileName) ;
68 $files = $zip->extract(['extract_as_string' => true]) ;
69 if (! is_array(@$files)) {
70 return LEGACY_FRAME_VIEW_ERROR;
71 }
72 if (!$this->_fetchZipTargetImages($files, $targetimages)) {
73 return LEGACY_FRAME_VIEW_ERROR;
74 }
75 }//if zip end
76 else {
77 require_once XOOPS_ROOT_PATH . '/class/class.tar.php';
78 $tar =new tar();
79 $tar->openTar($formFile->_mTmpFileName);
80 if (!is_array(@$tar->files)) {
81 return LEGACY_FRAME_VIEW_ERROR;
82 }
83 if (!$this->_fetchTarTargetImages($tar->files, $targetimages)) {
84 return LEGACY_FRAME_VIEW_ERROR;
85 }
86 }//end tar
87
88 if (!$this->_saveTargetImages($targetimages, $t_imgcat_id)) {
89 return LEGACY_FRAME_VIEW_ERROR;
90 }
91 return LEGACY_FRAME_VIEW_SUCCESS;
92 }
93
94 public function _fetchZipTargetImages(&$files, &$targetimages)
95 {
96 foreach ($files as $file) {
97 $file_pos = strrpos($file['filename'], '/') ;
98 if (false !== $file_pos) {
99 $file['filename'] = substr($file['filename'], $file_pos+1);
100 }
101 if (!empty($file['filename']) && preg_match("/(.*)\.(gif|jpg|jpeg|png)$/i", $file['filename'], $match) && !preg_match('/[' . preg_quote('\/:*?"<>|', '/') . ']/', $file['filename'])) {
102 $targetimages[] = ['name' => $file['filename'], 'content' => $file['content']];
103 }
104 unset($file);
105 }
106 return true;
107 }
108
109 public function _fetchTarTargetImages(&$files, &$targetimages)
110 {
111 foreach ($files as $id => $info) {
112 $file_pos = strrpos($info['name'], '/') ;
113 if (false !== $file_pos) {
114 $info['name'] = substr($info['name'], $file_pos+1);
115 }
116 if (!empty($info['name']) && preg_match("/(.*)\.(gif|jpg|jpeg|png)$/i", $info['name'], $match) && !preg_match('/[' . preg_quote('\/:*?"<>|', '/') . ']/', $info['name'])) {
117 $targetimages[] = ['name' => $info['name'], 'content' => $info['file']];
118 }
119 unset($info);
120 }
121 return true;
122 }
123
124 public function _saveTargetImages(&$targetimages, $t_imgcat_id)
125 {
126 if (0 === (is_countable($targetimages) ? count($targetimages) : 0)) {
127 return true;
128 }
129
130 $imgcathandler =& xoops_getmodulehandler('imagecategory', 'legacy');
131 $t_category = & $imgcathandler->get($t_imgcat_id);
132 $t_category_type = $t_category->get('imgcat_storetype');
133 $imagehandler =& xoops_getmodulehandler('image');
134
135 if ('file' === strtolower($t_category_type)) {
136 for ($i = 0; $i < (is_countable($targetimages) ? count($targetimages) : 0); $i++) {
137 $ext_pos = strrpos($targetimages[$i]['name'], '.') ;
138 if (false === $ext_pos) {
139 continue ;
140 }
141 $ext = strtolower(substr($targetimages[$i]['name'], $ext_pos + 1)) ;
142 if (empty($this->mAllowedExts[$ext])) {
143 continue ;
144 }
145 $file_name = substr($targetimages[$i]['name'], 0, $ext_pos) ;
146 $save_file_name = uniqid('img') . '.' . $ext ;
147 $filehandle = fopen(XOOPS_UPLOAD_PATH.'/'.$save_file_name, 'wb') ;
148 if (! $filehandle) {
149 $this->_addErrorMessage(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_SAVE_IMAGE_FILE, $file_name));
150 continue ;
151 }
152 if (!@fwrite($filehandle, $targetimages[$i]['content'])) {
153 $this->_addErrorMessage(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_SAVE_IMAGE_FILE, $file_name));
154 @fclose($filehandle) ;
155 continue;
156 };
157 @fclose($filehandle) ;
158
159 $image =& $imagehandler->create();
160 $image->set('image_nicename', $file_name);
161 $image->set('image_name', $save_file_name);
162 $image->set('image_mimetype', $this->mAllowedExts[$ext]);
163 $image->set('image_display', 1);
164 $image->set('imgcat_id', $t_imgcat_id);
165
166 if (!$imagehandler->insert($image)) {
167 $this->_addErrorMessage(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_SAVE_IMAGE_FILE, $file_name));
168 }
169 unset($image);
170 } //end of for
171 } //end of if
172 elseif ('db' === strtolower($t_category_type)) {
173 foreach ($targetimages as $iValue) {
174 $ext_pos = strrpos($iValue['name'], '.') ;
175 if ($ext_pos === false) {
176 continue ;
177 }
178 $ext = strtolower(substr($iValue['name'], $ext_pos + 1)) ;
179 if (empty($this->mAllowedExts[$ext])) {
180 continue ;
181 }
182 $file_name = substr($iValue['name'], 0, $ext_pos) ;
183 $save_file_name = uniqid('img', true) . '.' . $ext ;
184 //
185 $image =& $imagehandler->create();
186 $image->set('image_nicename', $file_name);
187 $image->set('image_name', $save_file_name);
188 $image->set('image_mimetype', $this->mAllowedExts[$ext]);
189 $image->set('image_display', 1);
190 $image->set('imgcat_id', $t_imgcat_id);
191 $image->loadImageBody();
192 if (!is_object($image->mImageBody)) {
193 $image->mImageBody =& $image->createImageBody();
194 }
195 $image->mImageBody->set('image_body', $iValue['content']);
196
197 if (!$imagehandler->insert($image)) {
198 $this->_addErrorMessage(XCube_Utils::formatString(_AD_LEGACY_ERROR_COULD_NOT_SAVE_IMAGE_FILE, $file_name));
199 }
200 unset($image);
201 } //end of for
202 } //end of elseif
203 return true;
204 }
205
206 public function executeViewInput(&$controller, &$xoopsUser, &$render)
207 {
208 $render->setTemplateName('image_upload.html');
209 $render->setAttribute('actionForm', $this->mActionForm);
210 //image category
211 $handler =& xoops_getmodulehandler('imagecategory', 'legacy');
212 $cat_id = $controller->mRoot->mContext->mRequest->getRequest('imgcat_id');
213 if (isset($cat_id)) {
214 $this->mCategory =& $handler->get($cat_id);
215 $render->setAttribute('category', $this->mCategory);
216 }
217 $categoryArr =& $handler->getObjects();
218 $render->setAttribute('categoryArr', $categoryArr);
219 }
220
221 public function executeViewSuccess(&$controller, &$xoopsUser, &$render)
222 {
223 $controller->executeForward('./index.php?action=ImageList&imgcat_id=' . $this->mActionForm->get('imgcat_id'));
224 }
225
226 public function executeViewError(&$controller, &$xoopsUser, &$render)
227 {
228 if (count($this->mErrorMessages) == 0) {
229 $controller->executeRedirect('./index.php?action=ImageList&imgcat_id=' . $this->mActionForm->get('imgcat_id'), 1, _AD_LEGACY_ERROR_DBUPDATE_FAILED);
230 } else {
231 $render->setTemplateName('image_upload_error.html');
232 $render->setAttribute('errorMessages', $this->mErrorMessages);
233 }
234 }
235
236 public function executeViewCancel(&$controller, &$xoopsUser, &$render)
237 {
238 if ($this->mCategory) {
239 $controller->executeForward('./index.php?action=ImageList&imgcat_id=' . $this->mCategory->get('imgcat_id'));
240 } else {
241 $controller->executeForward('./index.php?action=ImagecategoryList');
242 }
243 }
244}
static formatString()
[Static] Formats string with special care for international.