1: <?php
2:
3: if (!defined('XOOPS_ROOT_PATH')) exit();
4:
5: require_once XOOPS_MODULE_PATH . "/user/class/AbstractEditAction.class.php";
6: require_once XOOPS_MODULE_PATH . "/user/admin/forms/AvatarUploadForm.class.php";
7:
8: class User_AvatarUploadAction extends User_Action
9: {
10: var $mActionForm = null;
11: var $mErrorMessages = array();
12: var $mAllowedExts = array('gif'=>'image/gif', 'jpg'=>'image/jpeg', 'jpeg'=>'image/jpeg', 'png' =>'image/png') ;
13:
14: function prepare(&$controller, &$xoopsUser)
15: {
16: $this->mActionForm =new User_AvatarUploadForm();
17: $this->mActionForm->prepare();
18: }
19:
20: function getDefaultView(&$controller, &$xoopsUser)
21: {
22: return USER_FRAME_VIEW_INPUT;
23: }
24:
25: function _addErrorMessage($msg)
26: {
27: $this->mErrorMessages[] = $msg;
28: }
29:
30: function execute(&$controller, &$xoopsUser)
31: {
32: $form_cancel = $controller->mRoot->mContext->mRequest->getRequest('_form_control_cancel');
33: if ($form_cancel != null) {
34: return USER_FRAME_VIEW_CANCEL;
35: }
36:
37: $this->mActionForm->fetch();
38: $this->mActionForm->validate();
39:
40: if ($this->mActionForm->hasError()) {
41: return $this->getDefaultView($controller, $xoopsUser);
42: }
43:
44: $formFile = $this->mActionForm->get('upload');
45: $formFileExt = $formFile->getExtension();
46: $files = array();
47: $avatarimages = array();
48:
49: if ( strtolower($formFileExt) == "zip" ) {
50: If ( !file_exists(XOOPS_ROOT_PATH . "/class/Archive_Zip.php") ) {
51: return USER_FRAME_VIEW_ERROR;
52: }
53: require_once XOOPS_ROOT_PATH . "/class/Archive_Zip.php" ;
54: $zip = new Archive_Zip($formFile->_mTmpFileName) ;
55: $files = $zip->extract( array( 'extract_as_string' => true ) ) ;
56: if( ! is_array( @$files ) ) {
57: return USER_FRAME_VIEW_ERROR;
58: }
59: if (!$this->_fetchZipAvatarImages($files, $avatarimages)) {
60: return USER_FRAME_VIEW_ERROR;
61: }
62: }
63: else {
64: require_once XOOPS_ROOT_PATH . "/class/class.tar.php";
65: $tar =new tar();
66: $tar->openTar($formFile->_mTmpFileName);
67: if (!is_array( @$tar->files)) {
68: return USER_FRAME_VIEW_ERROR;
69: }
70: if (!$this->_fetchTarAvatarImages($tar->files, $avatarimages)) {
71: return USER_FRAME_VIEW_ERROR;
72: }
73: }
74:
75: if (!$this->_saveAvatarImages($avatarimages)) {
76: return USER_FRAME_VIEW_ERROR;
77: }
78: return USER_FRAME_VIEW_SUCCESS;
79:
80: }
81:
82: function _fetchZipAvatarImages(&$files, &$avatarimages)
83: {
84: foreach ($files as $file) {
85: $file_pos = strrpos( $file['filename'] , '/' ) ;
86: if ( $file_pos !== false ) {
87: $file['filename'] = substr( $file['filename'] , $file_pos+1 );
88: }
89: if ( !empty($file['filename']) && preg_match("/(.*)\.(gif|jpg|jpeg|png)$/i", $file['filename'], $match) && !preg_match('/[' . preg_quote('\/:*?"<>|','/') . ']/', $file['filename'])) {
90: $avatarimages[] = array('name' => $file['filename'], 'content' => $file['content']);
91: }
92: unset($file);
93: }
94: return true;
95: }
96:
97: function _fetchTarAvatarImages(&$files, &$avatarimages)
98: {
99: foreach ($files as $id => $info) {
100: $file_pos = strrpos( $info['name'] , '/' ) ;
101: if ( $file_pos !== false ) {
102: $info['name'] = substr( $info['name'] , $file_pos+1 );
103: }
104: if ( !empty($info['name']) && preg_match("/(.*)\.(gif|jpg|jpeg|png)$/i", $info['name'], $match) && !preg_match('/[' . preg_quote('\/:*?"<>|','/') . ']/', $info['name'])) {
105: $avatarimages[] = array('name' => $info['name'], 'content' => $info['file']);
106: }
107: unset($info);
108: }
109: return true;
110: }
111:
112: function _saveAvatarImages(&$avatarimages)
113: {
114: if (count($avatarimages) == 0) {
115: return true;
116: }
117:
118: $avatarhandler =& xoops_getmodulehandler('avatar');
119:
120: for ($i = 0; $i < count($avatarimages); $i++) {
121: $ext_pos = strrpos( $avatarimages[$i]['name'] , '.' ) ;
122: if( $ext_pos === false ) continue ;
123: $ext = strtolower( substr( $avatarimages[$i]['name'] , $ext_pos + 1 ) ) ;
124: if( empty( $this->mAllowedExts[$ext] ) ) continue ;
125: $file_name = substr( $avatarimages[$i]['name'] , 0 , $ext_pos ) ;
126: $save_file_name = uniqid( 'savt' ) . '.' . $ext ;
127: $filehandle = fopen( XOOPS_UPLOAD_PATH.'/'.$save_file_name , "w" ) ;
128: if( ! $filehandle ) {
129: $this->_addErrorMessage(XCube_Utils::formatMessage(_AD_USER_ERROR_COULD_NOT_SAVE_AVATAR_FILE, $file_name));
130: continue ;
131: }
132: if ( !@fwrite($filehandle, $avatarimages[$i]['content']) ) {
133: $this->_addErrorMessage(XCube_Utils::formatMessage(_AD_USER_ERROR_COULD_NOT_SAVE_AVATAR_FILE, $file_name));
134: @fclose( $filehandle ) ;
135: continue;
136: };
137: @fclose( $filehandle ) ;
138:
139: $avatar =& $avatarhandler->create();
140: $avatar->set('avatar_name', $file_name);
141: $avatar->set('avatar_file', $save_file_name);
142: $avatar->set('avatar_display', 1);
143: $avatar->set('avatar_weight', 0);
144: $avatar->set('avatar_type', 'S');
145: $avatar->set('avatar_mimetype', $this->mAllowedExts[$ext]);
146:
147: if (!$avatarhandler->insert($avatar)) {
148: $this->_addErrorMessage(XCube_Utils::formatMessage(_AD_USER_ERROR_COULD_NOT_SAVE_AVATAR_FILE, $file_name));
149: }
150: unset($avatar);
151: }
152:
153: return true;
154: }
155:
156: function executeViewInput(&$controller, &$xoopsUser, &$render)
157: {
158: $render->setTemplateName("avatar_upload.html");
159: $render->setAttribute('actionForm', $this->mActionForm);
160: }
161:
162: function executeViewSuccess(&$controller, &$xoopsUser, &$render)
163: {
164: $controller->executeForward("./index.php?action=AvatarList");
165: }
166:
167: function executeViewError(&$controller, &$xoopsUser, &$render)
168: {
169: if (count($this->mErrorMessages) == 0) {
170: $controller->executeRedirect("./index.php?action=AvatarList", 1, _AD_USER_ERROR_DBUPDATE_FAILED);
171: }
172: else {
173: $render->setTemplateName("avatar_upload_error.html");
174: $render->setAttribute('errorMessages', $this->mErrorMessages);
175: }
176: }
177:
178: function executeViewCancel(&$controller, &$xoopsUser, &$render)
179: {
180: $controller->executeForward("./index.php?action=AvatarList");
181: }
182: }
183:
184: ?>
185: