root/downloadDatabase/files/lib/data/downloadDB/DownloadDBDataEditor.class.php @ 523

Revision 523, 7.2 kB (checked in by Tatzelwurm, 5 years ago)

Einige Änderungen wegen dem "/r/n Problem"
WYSIWYG im ACP hinzugefügt
Auswahl der Sprache hinzugefügt
Anzeige der Sprach-Flaggen im ACP und auf den Seiten (noch nicht fertig).

RevLine 
[402]1<?php
2require_once(WCF_DIR.'lib/data/downloadDB/DownloadDBData.class.php');
3require_once(WCF_DIR.'lib/data/image/Thumbnail.class.php');
4require_once(WCF_DIR.'lib/system/exception/UserInputException.class.php');
5require_once(WCF_DIR.'lib/system/io/File.class.php');
6
7/**
8 * DownloadBDDataEditor creates, updates or deletes an Dataset.
9 *
10 * @author              Robert "Tatzelwurm" Hempel
11 * @copyright   2007/2008 INSIDE das Hrspiel
12 * @license             GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package             de.inside.wcf.downloaddb
14 */
15class DownloadDBDataEditor extends DownloadDBData {
16               
17        /**
18         * Updates the Dataset in Database.
19         * @param       array           $moreData       // some data to be updated
20         */
21        public function updateDataset($moreData) {
22                $updates = '';
23                foreach ($moreData as $key => $value) {
24                        if (!empty($updates)) $updates .= ',';
25                        $updates .= $key . " = '" . $value . "'";
26                }
27                if (!empty($updates)) {
28                        $sql = "UPDATE  wcf".WCF_N."_dldb_data
29                                SET     ".$updates."
30                                WHERE   dataID = ".$this->dataID;
31                        WCF::getDB()->registerShutdownUpdate($sql);
32                        // reset cache
33                        parent::resetCache();
34                }
35        }
36       
37        /**
38         * Creates a new preview image and thumbnail.
39         *
40         * @param       string          $tmpName
41         * @param       string          $name
42         * @param       string          $field
43         * @return      string          Link
44         */
45        public function createPreview($tmpName, $name, $mimeType, $field) {
46                // get file extension
47                $fileExtension = self::getFileExtension($name, $field, DOWNLOADDB_ALLOWED_PREVIEW_EXT);
48                // check for image
49                if(!ImageUtil::checkImageContent($tmpName))     {
50                        // no or fake image
51                        // delete file
52                        @unlink($tmpName);
53                        throw new UserInputException($field, 'notAllowedExtension');
54                }
55                // check size again
56                $size = self::getFileSize($tmpName, $field);
57                // copy file to download folder
58                if (!@copy($tmpName, WCF_DIR.DOWNLOADDB_PREVIEW_DIR.$this->dataID.'_'.StringUtil::encodeHTML($name))) {
59                        // copy failed
60                        // delete file
61                        @unlink($tmpName);
62                        throw new UserInputException($field, 'copyFailed');
63                }
64                // make thumbnail
65                $thumb = new Thumbnail(WCF_DIR.DOWNLOADDB_PREVIEW_DIR.$this->dataID.'_'.StringUtil::encodeHTML($name), DOWNLOADDB_PREVIEW_WIDTH, DOWNLOADDB_PREVIEW_HIGHT);
66                // get thumbnail
67                if (($thumbnailData = $thumb->makeThumbnail(true))) {
68                        // save thumbnail
69                        $file = new File(WCF_DIR.DOWNLOADDB_PREVIEW_DIR.'thumbnail-'.$this->dataID.'_'.StringUtil::encodeHTML($name));
70                        $file->write($thumbnailData);
71                        unset($thumbnailData);
72                        $file->close();
73                        @chmod(WCF_DIR.DOWNLOADDB_PREVIEW_DIR.'thumbnail-'.$this->dataID.'_'.StringUtil::encodeHTML($name), 0666);
74                }               
75                // set permissions
76                @chmod(WCF_DIR.DOWNLOADDB_PREVIEW_DIR.$this->dataID.'_'.StringUtil::encodeHTML($name), 0666);
77                // creat Link
78                $link = DOWNLOADDB_PREVIEW_DIR.$this->dataID.'_'.StringUtil::encodeHTML($name);
79
80                // reset cache
81                parent::resetCache();
82
83                return $link;
84        }
85       
86        /**
87         * Returns the dataset object
88         *
89         * @param       integer $katID          // katID for data
90         * @param       array   $moreData       // complete entry
91         * @return      new data object
92         */
93        public static function createDataset($katID, $name, $description, $moreData) {
94                $keys = $values = '';
95                foreach ($moreData as $key => $value) {
96                        $keys .= ','.$key;
[523]97                        $values .= ",'".$value."'";
[402]98                }
99                $sql = "INSERT INTO     wcf".WCF_N."_dldb_data
100                                        (`katID`, `name`, `description`".$keys.")
101                                VALUES         
[523]102                                        ('".intval($katID)."','".$name."','".$description."'".$values.")";
[402]103                WCF::getDB()->sendQuery($sql);
104                $dataID = WCF::getDB()->getInsertID();
105                $dataset = new DownloadDBDataEditor($dataID);
106
107                // reset cache
108                parent::resetCache();
109
110                return $dataset;
111        }
112       
113        /**
114         * Creates the file row in database table.
115         *
116         * @param       string          $fileName
117         * @param       string          $fileExtension
118         * @param       string          $fileType
119         * @return      integer         new data id
120         */
121        public static function insert($fileName, $fileExtension, $fileType){ 
122                $sql = "INSERT INTO     wcf".WCF_N."_dldb_data
123                                        (`fileName`, `fileExtension`, `mimeType`)
124                                VALUES         
125                                        ('".escapeString($fileName)."','".escapeString($fileExtension)."','".escapeString($fileType)."')";
126                WCF::getDB()->sendQuery($sql);
127                return WCF::getDB()->getInsertID();
128        }
129
130        /**
131         * Creates  a new file
132         *
133         * @param       integer         $dataID
134         * @param       string          $tmpName
135         * @param       string          $name
136         * @param       string          $mimeType
137         * @param       string          $field
138         */
139        public static function createFile($tmpName, $name, $mimeType, $field) {
140               
141                // get file extension
142                $fileExtension = self::getFileExtension($name, $field, DOWNLOADDB_ALLOWED_FILE_EXT);
143               
144                // check size again
145                $size = self::getFileSize($tmpName, $field);
146                               
147                // creat file in database
148                $dataID = self::insert($name, $fileExtension, $mimeType);
149
150                // copy file to download folder
151                if (!@copy($tmpName, WCF_DIR.DOWNLOADDB_FILE_DIR.$dataID.'_'.$name)) {
152                        // copy failed
153                        // delete file
154                        @unlink($tmpName);
155                        // delete dataset
156                        $sql = "DELETE FROM     wcf".WCF_N."_dldb_data
157                                        WHERE dataID = ".$dataID;
158                        WCF::getDB()->sendQuery($sql);
159                        throw new UserInputException($field, 'copyFailed');
160                }
161                // set permissions
162                @chmod(WCF_DIR.DOWNLOADDB_FILE_DIR.$dataID.'_'.StringUtil::encodeHTML($name), 0666);
163
164                return $dataID;
165        }
166       
167        /**
168         * Updates the file and database data
169         * @param       integer         $dataID
170         * @param       string          $fileOld
171         * @param       string          $tmpName
172         * @param       string          $name
173         * @param       string          $mimeType
174         * @param       string          $field
175         */
176        public function updateFile($fileOld, $tmpName, $name, $mimeType, $field) {
177
178                // get file extension
179                $fileExtension = self::getFileExtension($name, $field, DOWNLOADDB_ALLOWED_FILE_EXT);
180               
181                // check size again
182                $size = self::getFileSize($tmpName, $field);
183                               
184                // copy file to download folder
185                if (!@copy($tmpName, WCF_DIR.DOWNLOADDB_FILE_DIR.$this->dataID.'_'.$name)) {
186                        // copy failed
187                        // delete file
188                        @unlink($tmpName);
189                        throw new UserInputException($field, 'copyFailed');
190                }
191                // set permissions
192                @chmod(WCF_DIR.DOWNLOADDB_FILE_DIR.$this->dataID.'_'.$name, 0666);
193                // delete old file
194                @unlink(WCF_DIR.DOWNLOADDB_FILE_DIR.$this->dataID.'_'.$fileOld);
195
196                // edit file in database
197                self::updateDataset(array(
198                        'fileName'              => $name,
199                        'fileExtension' => $fileExtension,
200                        'mimeType'              => $mimeType,
201                ));
202
203                // reset cache
204                parent::resetCache();
205        }
206
207        /**
208         * Gets the $fileExtension and check permission.
209         *
210         * @param       string          $name
211         * @param       string          $field (Error field)
212         * @return      string          $fileExtension
213         */
214        public static function getFileExtension($name, $field, $permiss) {
215                // get file extension
216                $fileExtension = '';
217                if (!empty($name) && StringUtil::indexOf($name, '.') !== false) {
218                        $fileExtension = StringUtil::toLowerCase(StringUtil::substring($name, StringUtil::lastIndexOf($name, '.') + 1));
219                }
220               
221                // check file extension
222                if (!in_array($fileExtension, explode(',', $permiss))) {
223                        throw new UserInputException($field, 'notAllowedExtension');
224                }
225                return $fileExtension;
226        }
227
228        /**
229         * Gets the fileSize and check permission.
230         *
231         * @param       string          $tmpName
232         * @param       string          $field (Error field)
233         * @return      float           $size
234         */
235
236        public static function getFileSize($tmpName, $field) {
237               
238                $size = @filesize($tmpName);
239               
240                // check size again
241                if ($size > WCF::getUser()->getPermission('user.dldb.maxFileSize')) {
242                        throw new UserInputException($field, 'tooLarge');
243                }
244                return $size;
245        }
246
247}
248?>
Note: See TracBrowser for help on using the browser.