-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilefieldelement.go
149 lines (125 loc) · 4.26 KB
/
filefieldelement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package wajaf
type FileFieldElement NodeDef
func NewFileFieldElement(id string) FileFieldElement {
e := NewNode("element", "filefieldElement")
e.SetID(id)
e.RegisterKnownAttributes([]string{"display", "style", "classname", "left", "width", "right", "top", "height", "bottom"})
return e
}
/*
namespace wajaf;
class filefieldElement extends Xelement
{
private $ExtensionsImages = array('.gif', '.jpg', '.png');
private $ExtensionsAudios = array('.mp3', '.wma');
private $ExtensionsVideos = array('.mp4', '.avi', '.wmv', '.mov');
private $ExtensionsDocuments = array('.pdf', '.doc', '.xls', '.ppt', '.zip', '.txt', '.csv');
private $ExtensionsAuth = null;
private $ExtensionsDir = '/skins/clean/extensions/'; // directory where the extensions gifs are
private $ExtensionsOther = 'other.png'; // put a gif link if we accept other extensions, or NULL if not
public function __construct($id = null)
{
parent::__construct('filefieldElement');
// $this->registerAttributes( array('classnameover', 'classnamedown', 'classnamedisabled', 'action', 'link') );
$this->setId($id);
// $this->registerData($text);
}
public function setExtensions($auth, $dir, $other = null)
{
if ( is_string($auth) )
{
switch($auth)
{
case 'images': $this->ExtensionsAuth = $this->ExtensionsImages; break;
case 'audios': $this->ExtensionsAuth = $this->ExtensionsAudios; break;
case 'videos': $this->ExtensionsAuth = $this->ExtensionsVideos; break;
case 'documents': $this->ExtensionsAuth = $this->ExtensionsDocuments; break;
default: $this->ExtensionsAuth = null; break;
}
}
else
$this->ExtensionsAuth = $auth;
$this->ExtensionsDir = $dir;
$this->ExtensionsOther = $other;
}
public function getExtension($name)
{
if ($this->ExtensionsAuth)
{
foreach ($this->ExtensionsAuth as $ext)
{
if (strtolower(substr($name, -strlen($ext))) == $ext)
{
return $ext;
}
}
}
if ($this->ExtensionsOther)
{
$pos = strrpos($name,'.');
if ($pos === false)
return null;
return substr($name, $pos);
}
return null;
}
// returns the gif of the extensionm or null
public function getExtensionImage($extension)
{
if ($this->ExtensionsAuth && in_array($extension, $this->ExtensionsAuth))
return substr($extension,1).'.png';
return substr($extension,1).'.png';
return $this->ExtensionsOther;
}
public function processFile($temporarydir, $temporarypath)
{
$Context = $this->base->HTTPRequest->getParameter('ApplicationContext');
$tempname = $this->base->createKey(10);
$extension = $this->getExtension(strtolower($_FILES[$this->getId()]['name']));
$id = $this->getId();
if ($extension)
{
// 1. save the file in a temporary public directory
DB_File::createDirectory($temporarydir, $temporarypath);
move_uploaded_file($_FILES[$this->getId()]['tmp_name'], $temporarydir . $temporarypath . $tempname . $extension);
// 2. return the javascript to show this file, and keep the name in a temporary field
$gif = $this->getExtensionImage($extension);
$tempfullname = $tempname.$extension;
$truefullname = $_FILES[$this->getId()]['name'];
return <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
<!--
window.parent.\$E('$Context{$id}').setFile('{$this->ExtensionsDir}', '$gif', '$tempfullname', '$truefullname');
// -->
</script>
</head>
</html>
EOF;
}
else
{
// 2. return the javascript to notify error
return <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
<!--
window.parent.\$E('$Context{$id}').setFile('/pics/', 'dot.gif', null, null);
alert('Error: el archivo que subió no es un archivo autorizado.');
// -->
</script>
</head>
</html>
EOF;
}
}
}
*/