From 723a6fadc982bb0df35f37f6d226d50ec327d270 Mon Sep 17 00:00:00 2001 From: xiaomlove <353856593@qq.com> Date: Wed, 3 Mar 2021 19:29:29 +0800 Subject: [PATCH] custom fields in upload.php --- include/functions.php | 4 +- nexus/Database/DB.php | 13 ++++-- nexus/Field/Field.php | 101 ++++++++++++++++++++++++++++++++++++++++-- public/attachment.php | 12 ++++- public/edit.php | 10 +++++ public/takeupload.php | 18 ++++++++ public/upload.php | 10 ++--- 7 files changed, 153 insertions(+), 15 deletions(-) diff --git a/include/functions.php b/include/functions.php index b5549e3f..3e4737ae 100644 --- a/include/functions.php +++ b/include/functions.php @@ -220,8 +220,8 @@ function formatCode($text) { return addTempCode("
".$lang_functions['text_code']."
$text

"); } -function formatImg($src, $enableImageResizer, $image_max_width, $image_max_height) { - return addTempCode("\"image\""); +function formatImg($src, $enableImageResizer, $image_max_width, $image_max_height, $imgId = "") { + return addTempCode("\"image\""); } function formatFlash($src, $width, $height) { diff --git a/nexus/Database/DB.php b/nexus/Database/DB.php index 07df4361..cc128ce0 100644 --- a/nexus/Database/DB.php +++ b/nexus/Database/DB.php @@ -152,9 +152,6 @@ class DB public static function update($table, $data, $whereStr) { - if (empty($table) || empty($data) || !is_array($data) || empty($whereStr)) { - throw new DatabaseException("require table and data(array) and whereStr."); - } $updateArr = []; foreach ($data as $field => $value) { $updateArr[] = "`$field` = " . sqlesc($value); @@ -164,6 +161,16 @@ class DB return mysql_affected_rows(); } + public static function delete($table, $whereStr, $limit = null) + { + $sql = "delete from $table where $whereStr"; + if (!is_null($limit)) { + $sql .= " limit $limit"; + } + sql_query($sql); + return mysql_affected_rows(); + } + public static function getOne($table, $whereStr, $fields = '*') { if ($fields != '*') { diff --git a/nexus/Field/Field.php b/nexus/Field/Field.php index afecb07b..26ea53d2 100644 --- a/nexus/Field/Field.php +++ b/nexus/Field/Field.php @@ -11,6 +11,7 @@ class Field const TYPE_RADIO = 'radio'; const TYPE_CHECKBOX = 'checkbox'; const TYPE_SELECT = 'select'; + const TYPE_IMAGE = 'image'; const TYPE_FILE = 'file'; public static $types = [ @@ -19,6 +20,7 @@ class Field self::TYPE_RADIO => '横向单选(radio)', self::TYPE_CHECKBOX => '横向多选(checkbox)', self::TYPE_SELECT => '下拉单选(select)', + self::TYPE_IMAGE => '图片(image)', self::TYPE_FILE => '文件(file)', ]; @@ -197,14 +199,107 @@ HEAD; } - public function renderUploadPage() + public function renderUploadPage(array $customValues = []) { $searchBoxId = get_setting('main.browsecat'); $searchBox = DB::getOne('searchbox', "id = $searchBoxId"); if (empty($searchBox)) { throw new \RuntimeException("Invalid search box: $searchBoxId"); } - $customFieldIdArr = explode(',', $searchBox['custom_fields']); - + $sql = sprintf('select * from torrents_custom_fields where id in (%s)', $searchBox['custom_fields']); + $res = sql_query($sql); + $html = ''; + while ($row = mysql_fetch_assoc($res)) { + $name = "custom_fields[{$row['id']}]"; + $currentValue = $customValues[$row['id']] ?? ''; + if ($row['type'] == self::TYPE_TEXT) { + $html .= tr($row['label'], sprintf('', $name, $currentValue), 1); + } elseif ($row['type'] == self::TYPE_TEXTAREA) { + $html .= tr($row['label'], sprintf('', $name, $currentValue), 1); + } elseif ($row['type'] == self::TYPE_RADIO || $row['type'] == self::TYPE_CHECKBOX) { + $part = ""; + foreach (preg_split('/[\r\n]+/', trim($row['options'])) as $option) { + if (empty($option) || ($pos = strpos($option, '|')) === false) { + continue; + } + $key = substr($option, 0, $pos); + $value = substr($option, $pos + 1); + $checked = ""; + if ($row['type'] == self::TYPE_RADIO && (string)$currentValue === (string)$value) { + $checked = " checked"; + } + if ($row['type'] == self::TYPE_CHECKBOX && in_array($value, (array)$currentValue)) { + $checked = " checked"; + } + $part .= sprintf( + '', + $row['type'], $name, $key, $checked, $value + ); + } + $html .= tr($row['label'], $part, 1); + } elseif ($row['type'] == self::TYPE_SELECT) { + $part = ''; + $html .= tr($row['label'], $part, 1); + } elseif ($row['type'] == self::TYPE_IMAGE) { + $callbackFunc = "preview_custom_field_image_" . $row['id']; + $iframeId = "iframe_$callbackFunc"; + $inputId = "input_$callbackFunc"; + $imgId = "attach" . $row['id']; + $previewBoxId = "preview_$callbackFunc"; + $y = ''; + $y .= sprintf('', $inputId, $name, $currentValue); + $y .= '
'; + if (!empty($currentValue)) { + if (substr($currentValue, 0, 4) == 'http') { + $y .= formatImg($currentValue, true, 700, 0, $imgId); + } else { + $y .= format_comment($currentValue); + } + } + $y .= '
'; + $y .= << + function {$callbackFunc}(delkey, url) + { + var previewBox = $('$previewBoxId') + var existsImg = $('$imgId') + var input = $('$inputId') + if (existsImg) { + previewBox.removeChild(existsImg) + input.value = '' + } + var img = document.createElement('img') + img.src=url + img.setAttribute('onload', 'Scale(this, 700, 0);') + img.setAttribute('onclick', 'Preview(this);') + input.value = '[attach]' + delkey + '[/attach]' + img.id='$imgId' + previewBox.appendChild(img) } + +JS; + $html .= tr($row['label'], $y, 1); + } elseif ($row['type'] == self::TYPE_FILE) { + $html .= tr($row['label'], sprintf('', $name), 1); + } + } + return $html; + } + } \ No newline at end of file diff --git a/public/attachment.php b/public/attachment.php index 61e52976..57379656 100644 --- a/public/attachment.php +++ b/public/attachment.php @@ -230,11 +230,19 @@ if ($Attach->enable_attachment()) $dlkey = md5($db_file_location.".".$ext); sql_query("INSERT INTO attachments (userid, width, added, filename, filetype, filesize, location, dlkey, isimage, thumb) VALUES (".$CURUSER['id'].", ".$width.", ".sqlesc(date("Y-m-d H:i:s")).", ".sqlesc($origfilename).", ".sqlesc($filetype).", ".$filesize.", ".sqlesc($db_file_location.".".$ext).", ".sqlesc($dlkey).", ".($isimage ? 1 : 0).", ".($hasthumb ? 1 : 0).")") or sqlerr(__FILE__, __LINE__); $count_left--; - echo(""); + if (!empty($_REQUEST['callback_func'])) { + $url = $httpdirectory_attachment."/".$db_file_location . ".$ext"; + if ($hasthumb) { + $url .= ".thumb.jpg"; + } + echo sprintf('', $_REQUEST['callback_func'], $dlkey, $url); + } else { + echo(""); + } } } } - print("
"); + print(""); print(""); print(""); print(" "); diff --git a/public/edit.php b/public/edit.php index a2b02c0a..4189c9fb 100644 --- a/public/edit.php +++ b/public/edit.php @@ -12,6 +12,16 @@ $res = sql_query("SELECT torrents.*, categories.mode as cat_mode FROM torrents L $row = mysql_fetch_array($res); if (!$row) die(); +/** + * custom fields + * @since v1.6 + */ +$customFieldValueRes = sql_query("select * from torrents_custom_field_values where torrent_id = $id"); +$customFieldValues = []; +while ($row = mysql_fetch_assoc($res)) { + $customFieldValues[$row['custom_field_id']] = unserialize($row['custom_field_value']); +} + if ($enablespecial == 'yes' && get_user_class() >= $movetorrent_class) $allowmove = true; //enable moving torrent to other section else $allowmove = false; diff --git a/public/takeupload.php b/public/takeupload.php index e04d7af3..f0f4843f 100644 --- a/public/takeupload.php +++ b/public/takeupload.php @@ -352,6 +352,24 @@ if (!$ret) { } $id = mysql_insert_id(); +/** + * add custom fields + * @since v1.6 + */ +if (!empty($_POST['custom_fields'])) { + $now = date('Y-m-d H:i:s'); + foreach ($_POST['custom_fields'] as $customField => $customValue) { + $customData = [ + 'torrent_id' => $id, + 'custom_field_id' => $customField, + 'custom_field_value' => serialize($customValue), + 'created_at' => $now, + 'updated_at' => $now, + ]; + \Nexus\Database\DB::insert('torrents_custom_field_values', $customData); + } +} + @sql_query("DELETE FROM files WHERE torrent = $id"); foreach ($filelist as $file) { @sql_query("INSERT INTO files (torrent, filename, size) VALUES ($id, ".sqlesc($file[0]).",".$file[1].")"); diff --git a/public/upload.php b/public/upload.php index 6a4f6188..28cd0762 100644 --- a/public/upload.php +++ b/public/upload.php @@ -64,11 +64,11 @@ stdhead($lang_upload['head_upload']); $ptGen = new \Nexus\PTGen\PTGen(); echo $ptGen->renderUploadPageFormInput(""); } - if ($enablenfo_main=='yes') - tr($lang_upload['row_nfo_file'], "
".$lang_upload['text_only_viewed_by'].get_user_class_name($viewnfo_class,false,true,true).$lang_upload['text_or_above']."", 1); - - $field = new \Nexus\Field\Field(); - + $field = new \Nexus\Field\Field(); + $field->renderUploadPage(); + if ($enablenfo_main=='yes') { + tr($lang_upload['row_nfo_file'], "
".$lang_upload['text_only_viewed_by'].get_user_class_name($viewnfo_class,false,true,true).$lang_upload['text_or_above']."", 1); + } print("".$lang_upload['row_description']."*"); textbbcode("upload","descr","",false); print("\n");