11if (!defined(
'XOOPS_ROOT_PATH')) {
15require_once XOOPS_MODULE_PATH .
'/legacy/class/AbstractEditAction.class.php';
16require_once XOOPS_MODULE_PATH .
'/legacy/admin/forms/ImageUploadForm.class.php';
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'];
25 public function prepare(&$controller, &$xoopsUser)
27 $this->mActionForm =
new Legacy_ImageUploadForm();
28 $this->mActionForm->prepare();
31 public function getDefaultView(&$controller, &$xoopsUser)
33 return LEGACY_FRAME_VIEW_INPUT;
36 public function _addErrorMessage($msg)
38 $this->mErrorMessages[] = $msg;
41 public function execute(&$controller, &$xoopsUser)
43 $form_cancel = $controller->mRoot->mContext->mRequest->getRequest(
'_form_control_cancel');
44 if (
null !== $form_cancel) {
45 return LEGACY_FRAME_VIEW_CANCEL;
48 $this->mActionForm->fetch();
49 $this->mActionForm->validate();
51 if ($this->mActionForm->hasError()) {
52 return $this->getDefaultView($controller, $xoopsUser);
55 $t_imgcat_id = $this->mActionForm->get(
'imgcat_id');
57 $formFile = $this->mActionForm->get(
'upload');
58 $formFileExt = $formFile->getExtension();
62 if (
'zip' === strtolower($formFileExt)) {
63 if (!file_exists(XOOPS_ROOT_PATH .
'/class/Archive_Zip.php')) {
64 return LEGACY_FRAME_VIEW_ERROR;
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;
72 if (!$this->_fetchZipTargetImages($files, $targetimages)) {
73 return LEGACY_FRAME_VIEW_ERROR;
77 require_once XOOPS_ROOT_PATH .
'/class/class.tar.php';
79 $tar->openTar($formFile->_mTmpFileName);
80 if (!is_array(@$tar->files)) {
81 return LEGACY_FRAME_VIEW_ERROR;
83 if (!$this->_fetchTarTargetImages($tar->files, $targetimages)) {
84 return LEGACY_FRAME_VIEW_ERROR;
88 if (!$this->_saveTargetImages($targetimages, $t_imgcat_id)) {
89 return LEGACY_FRAME_VIEW_ERROR;
91 return LEGACY_FRAME_VIEW_SUCCESS;
94 public function _fetchZipTargetImages(&$files, &$targetimages)
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);
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']];
109 public function _fetchTarTargetImages(&$files, &$targetimages)
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);
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']];
124 public function _saveTargetImages(&$targetimages, $t_imgcat_id)
126 if (0 === (is_countable($targetimages) ? count($targetimages) : 0)) {
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');
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) {
141 $ext = strtolower(substr($targetimages[$i][
'name'], $ext_pos + 1)) ;
142 if (empty($this->mAllowedExts[$ext])) {
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') ;
152 if (!@fwrite($filehandle, $targetimages[$i][
'content'])) {
154 @fclose($filehandle) ;
157 @fclose($filehandle) ;
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);
166 if (!$imagehandler->insert($image)) {
172 elseif (
'db' === strtolower($t_category_type)) {
173 foreach ($targetimages as $iValue) {
174 $ext_pos = strrpos($iValue[
'name'],
'.') ;
175 if ($ext_pos ===
false) {
178 $ext = strtolower(substr($iValue[
'name'], $ext_pos + 1)) ;
179 if (empty($this->mAllowedExts[$ext])) {
182 $file_name = substr($iValue[
'name'], 0, $ext_pos) ;
183 $save_file_name = uniqid(
'img',
true) .
'.' . $ext ;
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();
195 $image->mImageBody->set(
'image_body', $iValue[
'content']);
197 if (!$imagehandler->insert($image)) {
206 public function executeViewInput(&$controller, &$xoopsUser, &$render)
208 $render->setTemplateName(
'image_upload.html');
209 $render->setAttribute(
'actionForm', $this->mActionForm);
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);
217 $categoryArr =& $handler->getObjects();
218 $render->setAttribute(
'categoryArr', $categoryArr);
221 public function executeViewSuccess(&$controller, &$xoopsUser, &$render)
223 $controller->executeForward(
'./index.php?action=ImageList&imgcat_id=' . $this->mActionForm->get(
'imgcat_id'));
226 public function executeViewError(&$controller, &$xoopsUser, &$render)
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);
231 $render->setTemplateName(
'image_upload_error.html');
232 $render->setAttribute(
'errorMessages', $this->mErrorMessages);
236 public function executeViewCancel(&$controller, &$xoopsUser, &$render)
238 if ($this->mCategory) {
239 $controller->executeForward(
'./index.php?action=ImageList&imgcat_id=' . $this->mCategory->get(
'imgcat_id'));
241 $controller->executeForward(
'./index.php?action=ImagecategoryList');
static formatString()
[Static] Formats string with special care for international.