root/downloadDatabase/files/lib/acp/form/DownloadDBDataAddForm.class.php @ 668

Revision 668, 14.3 kB (checked in by Tatzelwurm, 5 years ago)

New Filenames for Uploads.
Some Eventlistener Points added.

Line 
1<?php
2// wcf imports
3require_once(WCF_DIR.'lib/acp/form/WysiwygCacheloaderForm.class.php');
4require_once(WCF_DIR.'lib/data/downloadDB/DownloadDBDataEditor.class.php');
5require_once(WCF_DIR.'lib/system/io/Tar.class.php');
6
7
8/**
9 * Download Database Admin Control Panel.
10 * Hinzufgen eines Datensatzes.
11 *
12 * @author              Robert "Tatzelwurm" Hempel
13 * @copyright   2007/2008 INSIDE das Hörspiel
14 * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package             de.inside.wcf.downloaddb
16 */
17
18class DownloadDBDataAddForm extends WysiwygCacheloaderForm {
19        public $templateName = 'downloadDBDataAdd';
20        public $activeMenuItem = 'wcf.acp.menu.link.content.dldb';
21        public $neededPermissions = 'admin.dldb.canAddData';
22       
23        // Datensatz Variablen
24        public $dataID;
25        public $katID = 0;
26        public $katOptions = array();
27        public $name = '';
28        public $description = '';
29        public $previewIMG = NULL;
30        public $previewFile = NULL;
31        public $previewArray = array();
32        public $fileName = NULL;
33        public $fileExtension = NULL;
34        public $mimeType = NULL;
35        public $link = '';
36        public $groesse = 0;
37        public $downloads = 0;
38        public $dldbUserID;
39        public $datum;
40        public $sortOrder = 1;
41        public $supportThread = '';
42        public $activ = 0;
43        public $isExtern = false;
44        public $languageID = '1';
45        public $additionalFields = array();
46       
47        public $showSettings = true;
48        public $showSmilies = true;
49        public $canUseSmilies = true;
50        public $showSignatureSetting = false;
51       
52        public $action;
53        public $upload;
54       
55        private $valid = false;
56       
57        /**
58         * @see Form::readFormParameters()
59         */
60        public function readFormParameters() {
61                parent::readFormParameters();
62                               
63                if (isset($_POST['katID']))                     $this->katID            = intval($_REQUEST['katID']);
64                if (isset($_POST['name']))                      $this->name                     = StringUtil::trim($_REQUEST['name']);
65                if (isset($_POST['description']))       $this->description      = StringUtil::trim($_POST['description']);
66                if (isset($_POST['previewIMG']))        $this->previewIMG       = StringUtil::encodeHTML($_POST['previewIMG']);
67                if (isset($_POST['previewFile']))       $this->previewFile      = StringUtil::encodeHTML($_POST['previewFile']);
68                if (isset($_POST['link']))                      $this->link                     = escapeString($_POST['link']);
69                if (isset($_POST['groesse']))           $this->groesse          = intval($_POST['groesse']);
70                if (isset($_POST['downloads']))         $this->downloads        = intval($_POST['downloads']);
71                if (isset($_POST['dldbUserID']))        $this->dldbUserID       = intval($_POST['dldbUserID']);
72                if (isset($_POST['dldbUserName']))      $this->dldbUserName     = escapeString($_POST['dldbUserName']);
73                if (isset($_POST['datum']))             $this->datum            = intval($_POST['datum']);
74                if (isset($_POST['sortOrder']))         $this->sortOrder        = intval($_POST['sortOrder']);
75                if (isset($_POST['supportThread']))     $this->supportThread = escapeString($_POST['supportThread']);
76                if (isset($_POST['activ']))                     $this->activ            = intval($_POST['activ']);
77                if (isset($_POST['languageID']))        $this->languageID       = intval($_POST['languageID']);
78                // for File Upload
79                if (isset($_FILES['upload']))           $this->upload           = $_FILES['upload'];
80                // for Preview Upload
81                if (isset($_FILES['previewArray']))     $this->previewArray = $_FILES['previewArray'];
82                // Action
83                if (isset($_POST['action']))            $this->action           = StringUtil::toLowerCase(escapeString($_POST['action']));
84        }
85       
86        /**
87         * @see Form::validate()
88         */
89        public function validate() {
90                // prÃŒfen ob ein Titel eingegeben wurde
91                if (empty($this->name)) {
92                        throw new UserInputException('name', 'empty');
93                }
94                // prÃŒfen ob eine Kategorie eingegeben wurde
95                if (empty($this->katID)) {
96                        throw new UserInputException('katID', 'invalid');
97                }
98                // prÃŒfen ob ein Link eingegeben wurde
99                if (empty($this->upload) && empty($this->link)) {
100                        throw new UserInputException('link', 'empty');
101                }
102                // prÃŒfen ob ein relativer Pfad eingegeben wurde
103                if ($this->link && (FileUtil::isURL($this->link) == false)) {
104                        if ($this->link{0} == "/"){
105                                if (@fopen($_SERVER['DOCUMENT_ROOT'].$this->link,'rb') == false) {
106                                        throw new UserInputException('link', 'badFile');
107                                }
108                        }
109                } 
110                // Only if Action = add
111                if ($this->action == 'add') {
112                        // prÃŒfen ob ein Link UND File angegeben wurden
113                        if ($this->link && ($this->upload && $this->upload['error'] != 4)) {
114                                throw new UserInputException('link', 'invalid');
115                        }
116                }
117                $this->valid = true;
118        }
119
120        /**
121         * Create an new data set.
122         */
123        public function save() {
124                //Create new Dataset with FileUpload
125                if ($this->valid && $this->action == 'add'){
126                        // upload file if given
127                        if (!$this->link) {
128                                if ($this->upload && $this->upload['error'] != 4) {
129                                        if ($this->upload['error'] != 0) {
130                                                throw new UserInputException('upload', 'uploadFailed');
131                                        }
132                                        // import file
133                                        $this->dataID = DownloadDBDataEditor::createFile($this->upload['tmp_name'], $this->upload['name'], $this->upload['type'], 'upload');
134                                        $this->groesse = intval($this->upload['size']);
135                                } else {
136                                        throw new UserInputException('upload');
137                                }
138                        } else {
139                                if (FileUtil::isURL($this->link) == false) {
140                                        if ($this->link{0} == "/"){
141                                                if (FileUtil::isURL($_SERVER['DOCUMENT_ROOT'].$this->link) == false) {
142                                                        if (StringUtil::indexOf($_SERVER['DOCUMENT_ROOT'].$this->link, '/') !== false) {
143                                                                $this->fileName = StringUtil::substring($_SERVER['DOCUMENT_ROOT'].$this->link, StringUtil::lastIndexOf($_SERVER['DOCUMENT_ROOT'].$this->link, '/') + 1);
144                                                        }
145                                                        if (StringUtil::indexOf($this->fileName, '.') !== false) {
146                                                                $this->fileExtension = StringUtil::toLowerCase(StringUtil::substring($this->fileName, StringUtil::lastIndexOf($this->fileName, '.') + 1));
147                                                        }
148                                                        $this->groesse = @filesize($_SERVER['DOCUMENT_ROOT'].$this->link);
149// Funktioniert nur wenn "fileinfo" in php aktiviert ist!!
150//                                                      $finfo = @finfo_open(FILEINFO_MIME); // return mime type ala mimetype extension
151//                                                      $this->mimeType = @finfo_file($finfo, $_SERVER['DOCUMENT_ROOT'].$this->link);
152                                                        $this->mimeType = ''; /*#### Entfernen wenn fileinfo aktiv ist ###*/
153// daher ist $mimeType = '';
154                                                }
155                                        } else {
156                                                if (FileUtil::isURL($this->link) == false) {
157                                                        if (StringUtil::indexOf($this->link, '/') !== false) {
158                                                                $this->fileName = StringUtil::substring($this->link, StringUtil::lastIndexOf($this->link, '/') + 1);
159                                                        }
160                                                        if (StringUtil::indexOf($this->fileName, '.') !== false) {
161                                                                $this->fileExtension = StringUtil::toLowerCase(StringUtil::substring($this->fileName, StringUtil::lastIndexOf($this->fileName, '.') + 1));
162                                                        }
163                                                        $this->groesse  = @filesize($this->link);
164                                                        $this->mimeType = '';
165// Funktioniert nur wenn "fileinfo" in php aktiviert ist!!
166//                                                      $finfo = @finfo_open(FILEINFO_MIME); // return mime type ala mimetype extension
167//                                                      $this->mimeType = @finfo_file($finfo, $_SERVER['DOCUMENT_ROOT'].$this->link);
168                                                        $this->mimeType = ''; /*#### Entfernen wenn fileinfo aktiv ist ###*/
169// daher ist $mimeType = '';
170                                                }
171                                        }
172                                } else {
173                                        $this->fileName = $this->fileExtension = NULL;
174                                }
175                        }
176                        // check for Preview File
177                        if ($this->previewArray && $this->previewArray['error'] != 4) {
178                                if ($this->previewArray['error'] != 0) {
179                                        throw new UserInputException('previewIMG', 'uploadFailed');
180                                }
181                        } else {
182                                $this->previewFile = $this->previewIMG = NULL;
183                        }
184                        if ($this->dataID ) { //DataID durch File Upload erhalten
185                                $this->dldbUserID = WCF::getUser()->userID;
186                                $this->datum = TIME_NOW;
187                                $dataset = new downloadDBDataEditor($this->dataID);
188                                if ($this->previewArray && ($this->previewArray['error'] == 0)) {
189                                        // Vorschaubild anlegen und Variablen setzen
190                                        $this->previewIMG = $dataset->createPreview($this->previewArray['tmp_name'], $this->previewArray['name'], $this->previewArray['type'], 'previewIMG');
191                                        $this->previewFile = $this->previewArray['name'];
192                                }
193                                if ($this->upload['error'] != 4) {
194                                        // File Upload: File anlegen und in DB schreiben
195                                        $this->fileName = $this->upload['name'];
196                                        $this->mimeType = $this->upload['type'];
197                                        if (StringUtil::indexOf($this->fileName, '.') !== false) {
198                                                $this->fileExtension = StringUtil::toLowerCase(StringUtil::substring($this->fileName, StringUtil::lastIndexOf($this->fileName, '.') + 1));
199                                        }
200                                        // Link anlegen
201                                        $this->link = $dataset->getURL();
202                                        // Datensatz speichern  mit File
203                                        $dataset->updateDataset(array_merge($this->additionalFields, array(
204                                                        'katID'                 => intval($this->katID),
205                                                        'name'                  => escapeString($this->name),
206                                                        'description'   => escapeString($this->description),
207                                                        'previewIMG'    => escapeString($this->previewIMG),
208                                                        'previewFile'   => StringUtil::encodeHTML($this->previewFile),
209                                                        'fileName'              => escapeString($this->fileName),
210                                                        'fileExtension' => escapeString($this->fileExtension),
211                                                        'mimeType'              => escapeString($this->mimeType),
212                                                        'link'                  => StringUtil::encodeHTML($this->link),
213                                                        'groesse'               => intval($this->groesse),
214                                                        'downloads'             => intval($this->downloads),
215                                                        'userID'                => intval($this->dldbUserID),
216                                                        'datum'                 => $this->datum,
217                                                        'sortOrder'             => intval($this->sortOrder),
218                                                        'thread'                => $this->supportThread,
219                                                        'activ'                 => true,
220                                                        'parseURL'              => intval($this->parseURL),
221                                                        'enableBBCodes' => intval($this->enableBBCodes),
222                                                        'enableHtml'    => intval($this->enableHtml),
223                                                        'enableSmilies' => intval($this->enableSmilies),
224                                                        'languageID'    => intval($this->languageID))));
225                                }
226                        } else {
227                                // dataID = NULL
228                                //Create new Dataset if no file is given
229                                $dataset = DownloadDBDataEditor::createDataset(intval($this->katID), escapeString($this->name), escapeString($this->description) ,array_merge($this->additionalFields, array(
230                                                        'link'                  => $this->link,
231                                                        'fileName'              => escapeString($this->fileName),
232                                                        'fileExtension' => escapeString($this->fileExtension),
233                                                        'mimeType'              => escapeString($this->mimeType),
234                                                        'groesse'               => intval($this->groesse),
235                                                        'downloads'             => intval($this->downloads),
236                                                        'userID'                => WCF::getUser()->userID,
237                                                        'datum'                 => TIME_NOW,
238                                                        'sortOrder'             => intval($this->sortOrder),
239                                                        'thread'                => $this->supportThread,
240                                                        'activ'                 => true,
241                                                        'parseURL'              => intval($this->parseURL),
242                                                        'enableBBCodes' => intval($this->enableBBCodes),
243                                                        'enableHtml'    => intval($this->enableHtml),
244                                                        'enableSmilies' => intval($this->enableSmilies),
245                                                        'languageID'    => intval($this->languageID))));
246                                // Vorschaubild anlegen und Variablen setzen
247                                if ($this->previewArray && ($this->previewArray['error'] == 0)) {
248                                        // Vorschaubild anlegen und Variablen setzen
249                                        $this->previewIMG  = StringUtil::encodeHTML(DownloadDBDataEditor::createPreview($this->previewArray['tmp_name'], $this->previewArray['name'], $this->previewArray['type'], 'previewIMG'));
250                                        $this->previewFile = StringUtil::encodeHTML($this->previewArray['name']);
251                                        $dataset->updateDataset(array('previewIMG' => $this->previewIMG, 'previewFile' => $this->previewFile));
252                                }
253                        }
254                }
255                parent::save();
256                // reset values
257                $this->name = $this->description = $this->link = $this->supportThread = NULL;
258                $this->fileName = $this->fileExtension = $this->mimeType = NULL;
259                $this->previewFile = $this->previewIMG = NULL;
260                $this->groesse = $this->downloads = $this->katID = 0;
261                $this->sortOrder = $this->languageID = 1;
262                // reset cache
263                WCF::getCache()->clear(WCF_DIR.'cache/', 'cache.dldbKat.php');
264                WCF::getCache()->clear(WCF_DIR.'cache/', 'cache.dldbData.php');
265                $this->saved();
266                // show success message
267                WCF::getTPL()->assign('success', true);
268        }
269       
270        /**
271         * @see Page::readData()
272         */
273        public function readData() {
274                parent::readData();
275                $this->languageID = WCF::getUser()->languageID;
276                // read Category Cache
277                WCF::getCache()->addResource('dldbKat',
278                        WCF_DIR.'cache/cache.dldbKat.php',
279                        WCF_DIR.'lib/system/cache/CacheBuilderDLDBKat.class.php');
280                $this->katCache = WCF::getCache()->get('dldbKat');
281                $this->readCategorys();
282        }
283
284        /**
285         * @see Page::assignVariables()
286         */
287        public function assignVariables() {
288                parent::assignVariables();
289
290                WCFACP::getMenu()->setActiveMenuItem($this->activeMenuItem);
291               
292                $languagesArray = WCF::getLanguage()->getAvailableLanguages();
293                       
294                for ($i = 0; $i < count($languagesArray); $i++) {
295                        $languageItems = $languagesArray[$i];
296                               
297                        $languageArray[] = array(
298                                        'languageID' => $languageItems['languageID'],
299                                        'languageName' => WCF::getLanguage()->get('wcf.global.language.'.$languageItems['languageCode']),
300                        );
301                }
302               
303                $maxFileSize = WCF::getUser()->getPermission('user.dldb.maxFileSize');
304               
305                WCF::getTPL()->assign(array(
306                        'katOptions'    => $this->katOptions,
307                        'katID'                 => $this->katID,
308                        'name'                  => $this->name,
309                        'description'   => $this->description,
310                        'previewIMG'    => $this->previewIMG,
311                        'previewFile'   => $this->previewFile,
312                        'previewArray'  => $this->previewArray,
313                        'fileName'              => $this->fileName,
314                        'fileExtension' => $this->fileExtension,
315                        'mimeType'              => $this->mimeType,
316                        'link'                  => $this->link,
317                        'groesse'               => $this->groesse,
318                        'downloads'             => $this->downloads,
319                        'dldbUserID'    => $this->dldbUserID,
320                        'datum'                 => $this->datum,
321                        'sortOrder'             => $this->sortOrder,
322                        'supportThread' => $this->supportThread,
323                        'activ'                 => $this->activ,
324                        'action'                => 'add',
325                        'previewExtensions' => DOWNLOADDB_ALLOWED_PREVIEW_EXT,
326                        'fileExtensions' => DOWNLOADDB_ALLOWED_FILE_EXT,
327                        'maxFileSize'   => $maxFileSize,
328                        'isExtern'              => $this->isExtern,
329                        'languageID'    => $this->languageID,
330                        'language'              => $languageArray
331                ));
332        }
333
334        /**
335         * Liste mglicher Unterkategorien. (erste Ebene)
336         */
337        protected function readSubKat($katID) {
338                foreach ($this->katCache as $ID => $kategorie) {
339                        if ($kategorie['topID'] == $katID) {
340                                $this->katOptions[$kategorie['katID']] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".StringUtil::unescape($kategorie['name']);
341                                // weitere Unterkategorien suchen
342                                $this->readSubKat($kategorie['katID']);
343                        }
344                }
345        }
346       
347        protected function readCategorys() {
348                // format options
349                foreach ($this->katCache as $ID => $kategorie) {
350                        if ($kategorie['topID'] == NULL) {
351                                $this->katOptions[$kategorie['katID']] = "&nbsp;&nbsp;&nbsp;&nbsp;".StringUtil::unescape($kategorie['name']);
352                                $this->readSubKat($kategorie['katID']);
353                        } 
354                }
355        }
356}
357?>
Note: See TracBrowser for help on using the browser.