Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit ae2383e

Browse files
committed
ENH: refs #0324. More robust fix the warnings on upload of some files
We subclassed the Zend http adapter to fix the bug in their mime type detection method.
1 parent 44997dc commit ae2383e

File tree

2 files changed

+74
-26
lines changed

2 files changed

+74
-26
lines changed

core/controllers/UploadController.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class UploadController extends AppController
2424
*/
2525
function init()
2626
{
27-
$maxFile = str_replace("M", "", ini_get('upload_max_filesize'));
28-
$maxPost = str_replace("M", "", ini_get('post_max_size'));
27+
$maxFile = str_replace('M', '', ini_get('upload_max_filesize'));
28+
$maxPost = str_replace('M', '', ini_get('post_max_size'));
2929
if($maxFile < $maxPost)
3030
{
3131
$this->view->maxSizeFile = $maxFile * 1024 * 1024;
@@ -62,11 +62,11 @@ public function simpleuploadAction()
6262
{
6363
if(!$this->logged)
6464
{
65-
throw new Zend_Exception("You have to be logged in to do that");
65+
throw new Zend_Exception('You have to be logged in to do that');
6666
}
6767
if(!$this->getRequest()->isXmlHttpRequest() && !$this->isTestingEnv())
6868
{
69-
throw new Zend_Exception("Error, should be an ajax action.");
69+
throw new Zend_Exception('Error, should be an ajax action.');
7070
}
7171
$this->disableLayout();
7272
$this->view->form = $this->getFormAsArray($this->Form->Upload->createUploadLinkForm());
@@ -94,23 +94,23 @@ public function revisionAction()
9494
{
9595
if(!$this->logged)
9696
{
97-
throw new Zend_Exception("You have to be logged in to do that");
97+
throw new Zend_Exception('You have to be logged in to do that');
9898
}
9999
if(!$this->getRequest()->isXmlHttpRequest() && !$this->isTestingEnv())
100100
{
101-
throw new Zend_Exception("Error, should be an ajax action.");
101+
throw new Zend_Exception('Error, should be an ajax action.');
102102
}
103103
$this->disableLayout();
104104
$itemId = $this->_getParam('itemId');
105105
$item = $this->Item->load($itemId);
106106

107107
if($item == false)
108108
{
109-
throw new Zend_Exception("Unable to load item.");
109+
throw new Zend_Exception('Unable to load item.');
110110
}
111111
if(!$this->Item->policyCheck($item, $this->userSession->Dao, MIDAS_POLICY_WRITE))
112112
{
113-
throw new Zend_Exception("Error policies.");
113+
throw new Zend_Exception('Error policies.');
114114
}
115115
$this->view->item = $item;
116116
$itemRevision = $this->Item->getLastRevision($item);
@@ -123,20 +123,20 @@ public function savelinkAction()
123123
{
124124
if(!$this->logged)
125125
{
126-
throw new Zend_Exception("You have to be logged in to do that");
126+
throw new Zend_Exception('You have to be logged in to do that');
127127
}
128128
if(!$this->getRequest()->isXmlHttpRequest() && !$this->isTestingEnv())
129129
{
130-
throw new Zend_Exception("Error, should be an ajax action.");
130+
throw new Zend_Exception('Error, should be an ajax action.');
131131
}
132132

133133
$this->disableLayout();
134134
$this->disableView();
135-
$parent = $this->_getParam("parent");
136-
$name = $this->_getParam("name");
137-
$url = $this->_getParam("url");
138-
$parent = $this->_getParam("parent");
139-
$license = $this->_getParam("license");
135+
$parent = $this->_getParam('parent');
136+
$name = $this->_getParam('name');
137+
$url = $this->_getParam('url');
138+
$parent = $this->_getParam('parent');
139+
$license = $this->_getParam('license');
140140
if(!empty($url) && !empty($name))
141141
{
142142
$item = $this->Component->Upload->createLinkItem($this->userSession->Dao, $name, $url, $parent);
@@ -149,14 +149,14 @@ public function javauploadAction()
149149
{
150150
if(!$this->logged)
151151
{
152-
throw new Zend_Exception("You have to be logged in to do that");
152+
throw new Zend_Exception('You have to be logged in to do that');
153153
}
154154
if(!$this->getRequest()->isXmlHttpRequest())
155155
{
156-
throw new Zend_Exception("Error, should be an ajax action.");
156+
throw new Zend_Exception('Error, should be an ajax action.');
157157
}
158158
$this->_helper->layout->disableLayout();
159-
$this->view->protocol = "http";
159+
$this->view->protocol = 'http';
160160
$this->view->host = empty($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['HTTP_X_FORWARDED_HOST'];
161161
$this->view->selectedLicense = Zend_Registry::get('configGlobal')->defaultlicense;
162162

@@ -184,7 +184,7 @@ function gethttpuploadoffsetAction()
184184
$this->Component->Httpupload->get_http_upload_offset($params);
185185
} //end get_http_upload_offset
186186

187-
/** java upload function, didn 't check what it does :-) */
187+
/** java upload function, didn't check what it does :-) */
188188
function gethttpuploaduniqueidentifierAction()
189189
{
190190
$this->disableLayout();
@@ -200,7 +200,7 @@ function processjavauploadAction()
200200
$params = $this->_getAllParams();
201201
if(!$this->logged)
202202
{
203-
throw new Zend_Exception("You have to be logged in to do that");
203+
throw new Zend_Exception('You have to be logged in to do that');
204204
}
205205
$this->disableLayout();
206206
$this->disableView();
@@ -237,7 +237,7 @@ public function saveuploadedAction()
237237
{
238238
if(!$this->logged)
239239
{
240-
throw new Zend_Exception("You have to be logged in to do that");
240+
throw new Zend_Exception('You have to be logged in to do that');
241241
}
242242

243243
$this->disableLayout();
@@ -252,23 +252,24 @@ public function saveuploadedAction()
252252
}
253253
else
254254
{
255+
// bugfix: We added an adapter class (see issue 324) under Zend/File/Transfer/Adapter
255256
ob_start();
256-
$upload = new Zend_File_Transfer();
257+
$upload = new Zend_File_Transfer('HttpFixed');
257258
$upload->receive();
258259
$path = $upload->getFileName();
259260
$file_size = filesize($path);
260261
$filename = $upload->getFilename(null, false);
261262
ob_end_clean();
262263
}
263264

264-
$parent = $this->_getParam("parent");
265-
$license = $this->_getParam("license");
265+
$parent = $this->_getParam('parent');
266+
$license = $this->_getParam('license');
266267
if(!empty($path) && file_exists($path) && $file_size > 0)
267268
{
268269
$tmp = explode('-', $parent);
269270
if(count($tmp) == 2) //means we upload a new revision
270271
{
271-
$changes = $this->_getParam("changes");
272+
$changes = $this->_getParam('changes');
272273
$this->Component->Upload->createNewRevision($this->userSession->Dao, $filename, $path, $tmp, $changes, $license);
273274
}
274275
else
@@ -283,4 +284,4 @@ public function saveuploadedAction()
283284
echo json_encode($info);
284285
}
285286
}//end saveuploaded
286-
}//end class
287+
}//end class
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
require_once 'Zend/File/Transfer/Adapter/Http.php';
4+
5+
/**
6+
* We override the Zend http transfer adapter class to fix the
7+
* _detectMimeType method from the abstract adapter. See issue 324.
8+
*/
9+
class Zend_File_Transfer_Adapter_HttpFixed extends Zend_File_Transfer_Adapter_Http
10+
{
11+
protected function _detectMimeType($value)
12+
{
13+
if (file_exists($value['tmp_name'])) {
14+
$file = $value['tmp_name'];
15+
} else {
16+
return null;
17+
}
18+
19+
if (class_exists('finfo', false)) {
20+
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
21+
if (!empty($value['options']['magicFile'])) {
22+
$mime = @finfo_open($const, $value['options']['magicFile']);
23+
}
24+
25+
if (empty($mime)) {
26+
$mime = @finfo_open($const);
27+
}
28+
29+
if (!empty($mime)) {
30+
$result = finfo_file($mime, $file);
31+
}
32+
33+
unset($mime);
34+
}
35+
36+
if (empty($result) && (function_exists('mime_content_type')
37+
&& ini_get('mime_magic.magicfile'))) {
38+
$result = mime_content_type($file);
39+
}
40+
41+
if (empty($result)) {
42+
$result = 'application/octet-stream';
43+
}
44+
45+
return $result;
46+
}
47+
}

0 commit comments

Comments
 (0)