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