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

Revision 402, 7.2 kB (checked in by Tatzelwurm, 5 years ago)
Line 
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               
47                // get file extension
48                $fileExtension = self::getFileExtension($name, $field, DOWNLOADDB_ALLOWED_PREVIEW_EXT);
49                // check for image
50                if(!ImageUtil::checkImageContent($tmpName))     {
51                        // no or fake image
52                        // delete file
53                        @unlink($tmpName);
54                        throw new UserInputException($field, 'notAllowedExtension');
55                }
56                // check size again
57                $size = self::getFileSize($tmpName, $field);
58                // copy file to download folder
59                if (!@copy($tmpName, WCF_DIR.DOWNLOADDB_PREVIEW_DIR.$this->dataID.'_'.StringUtil::encodeHTML($name))) {
60                        // copy failed
61                        // delete file
62                        @unlink($tmpName);
63                        throw new UserInputException($field, 'copyFailed');
64                }
65                // make thumbnail
66                $thumb = new Thumbnail(WCF_DIR.DOWNLOADDB_PREVIEW_DIR.$this->dataID.'_'.StringUtil::encodeHTML($name), DOWNLOADDB_PREVIEW_WIDTH, DOWNLOADDB_PREVIEW_HIGHT);
67                // get thumbnail
68                if (($thumbnailData = $thumb->makeThumbnail(true))) {
69                        // save thumbnail
70                        $file = new File(WCF_DIR.DOWNLOADDB_PREVIEW_DIR.'thumbnail-'.$this->dataID.'_'.StringUtil::encodeHTML($name));
71                        $file->write($thumbnailData);
72                        unset($thumbnailData);
73                        $file->close();
74                        @chmod(WCF_DIR.DOWNLOADDB_PREVIEW_DIR.'thumbnail-'.$this->dataID.'_'.StringUtil::encodeHTML($name), 0666);
75                }               
76                // set permissions
77                @chmod(WCF_DIR.DOWNLOADDB_PREVIEW_DIR.$this->dataID.'_'.StringUtil::encodeHTML($name), 0666);
78                // creat Link
79                $link = DOWNLOADDB_PREVIEW_DIR.$this->dataID.'_'.StringUtil::encodeHTML($name);
80
81                // reset cache
82                parent::resetCache();
83
84                return $link;
85        }
86       
87        /**
88         * Returns the dataset object
89         *
90         * @param       integer $katID          // katID for data
91         * @param       array   $moreData       // complete entry
92         * @return      new data object
93         */
94        public static function createDataset($katID, $name, $description, $moreData) {
95                $keys = $values = '';
96                foreach ($moreData as $key => $value) {
97                        $keys .= ','.$key;
98                        $values .= ",'".escapeString($value)."'";
99                }
100                $sql = "INSERT INTO     wcf".WCF_N."_dldb_data
101                                        (`katID`, `name`, `description`".$keys.")
102                                VALUES         
103                                        ('".intval($katID)."','".escapeString($name)."','".escapeString($description)."'".$values.")";
104                WCF::getDB()->sendQuery($sql);
105                $dataID = WCF::getDB()->getInsertID();
106                $dataset = new DownloadDBDataEditor($dataID);
107
108                // reset cache
109                parent::resetCache();
110
111                return $dataset;
112        }
113       
114        /**
115         * Creates the file row in database table.
116         *
117         * @param       string          $fileName
118         * @param       string          $fileExtension
119         * @param       string          $fileType
120         * @return      integer         new data id
121         */
122        public static function insert($fileName, $fileExtension, $fileType){ 
123                $sql = "INSERT INTO     wcf".WCF_N."_dldb_data
124                                        (`fileName`, `fileExtension`, `mimeType`)
125                                VALUES         
126                                        ('".escapeString($fileName)."','".escapeString($fileExtension)."','".escapeString($fileType)."')";
127                WCF::getDB()->sendQuery($sql);
128                return WCF::getDB()->getInsertID();
129        }
130
131        /**
132         * Creates  a new file
133         *
134         * @param       integer         $dataID
135         * @param       string          $tmpName
136         * @param       string          $name
137         * @param       string          $mimeType
138         * @param       string          $field
139         */
140        public static function createFile($tmpName, $name, $mimeType, $field) {
141               
142                // get file extension
143                $fileExtension = self::getFileExtension($name, $field, DOWNLOADDB_ALLOWED_FILE_EXT);
144               
145                // check size again
146                $size = self::getFileSize($tmpName, $field);
147                               
148                // creat file in database
149                $dataID = self::insert($name, $fileExtension, $mimeType);
150
151                // copy file to download folder
152                if (!@copy($tmpName, WCF_DIR.DOWNLOADDB_FILE_DIR.$dataID.'_'.$name)) {
153                        // copy failed
154                        // delete file
155                        @unlink($tmpName);
156                        // delete dataset
157                        $sql = "DELETE FROM     wcf".WCF_N."_dldb_data
158                                        WHERE dataID = ".$dataID;
159                        WCF::getDB()->sendQuery($sql);
160                        throw new UserInputException($field, 'copyFailed');
161                }
162                // set permissions
163                @chmod(WCF_DIR.DOWNLOADDB_FILE_DIR.$dataID.'_'.StringUtil::encodeHTML($name), 0666);
164
165                return $dataID;
166        }
167       
168        /**
169         * Updates the file and database data
170         * @param       integer         $dataID
171         * @param       string          $fileOld
172         * @param       string          $tmpName
173         * @param       string          $name
174         * @param       string          $mimeType
175         * @param       string          $field
176         */
177        public function updateFile($fileOld, $tmpName, $name, $mimeType, $field) {
178
179                // get file extension
180                $fileExtension = self::getFileExtension($name, $field, DOWNLOADDB_ALLOWED_FILE_EXT);
181               
182                // check size again
183                $size = self::getFileSize($tmpName, $field);
184                               
185                // copy file to download folder
186                if (!@copy($tmpName, WCF_DIR.DOWNLOADDB_FILE_DIR.$this->dataID.'_'.$name)) {
187                        // copy failed
188                        // delete file
189                        @unlink($tmpName);
190                        throw new UserInputException($field, 'copyFailed');
191                }
192                // set permissions
193                @chmod(WCF_DIR.DOWNLOADDB_FILE_DIR.$this->dataID.'_'.$name, 0666);
194                // delete old file
195                @unlink(WCF_DIR.DOWNLOADDB_FILE_DIR.$this->dataID.'_'.$fileOld);
196
197                // edit file in database
198                self::updateDataset(array(
199                        'fileName'              => $name,
200                        'fileExtension' => $fileExtension,
201                        'mimeType'              => $mimeType,
202                ));
203
204                // reset cache
205                parent::resetCache();
206        }
207
208        /**
209         * Gets the $fileExtension and check permission.
210         *
211         * @param       string          $name
212         * @param       string          $field (Error field)
213         * @return      string          $fileExtension
214         */
215        public static function getFileExtension($name, $field, $permiss) {
216               
217                // get file extension
218                $fileExtension = '';
219                if (!empty($name) && StringUtil::indexOf($name, '.') !== false) {
220                        $fileExtension = StringUtil::toLowerCase(StringUtil::substring($name, StringUtil::lastIndexOf($name, '.') + 1));
221                }
222               
223                // check file extension
224                if (!in_array($fileExtension, explode(',', $permiss))) {
225                        throw new UserInputException($field, 'notAllowedExtension');
226                }
227                return $fileExtension;
228        }
229
230        /**
231         * Gets the fileSize and check permission.
232         *
233         * @param       string          $tmpName
234         * @param       string          $field (Error field)
235         * @return      float           $size
236         */
237
238        public static function getFileSize($tmpName, $field) {
239               
240                $size = @filesize($tmpName);
241               
242                // check size again
243                if ($size > WCF::getUser()->getPermission('user.dldb.maxFileSize')) {
244                        throw new UserInputException($field, 'tooLarge');
245                }
246                return $size;
247        }
248
249}
250?>
Note: See TracBrowser for help on using the browser.