Fix #263 gallery system using imgur

setup tutorial: https://github.com/Znote/ZnoteAAC/wiki/IMGUR-powered-Gallery-page
This commit is contained in:
Znote 2020-12-13 22:20:54 +00:00
parent 8128804021
commit 48d1dd5712
5 changed files with 66 additions and 22 deletions

View File

@ -15,6 +15,21 @@ if (isset($_POST['remove'])) {
$data = explode(":", $_POST['remove']);
$did = (int)$data[0];
echo 'Image '. $did .' removed.';
$delhash = $_POST['delhash'];
$imgurClientID = $config['gallery']['Client ID'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.imgur.com/3/image/{$delhash}");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Client-ID {$imgurClientID}"
));
$response = json_decode(curl_exec($ch));
mysql_delete("DELETE FROM `znote_images` WHERE `id`='$did' LIMIT 1;");
}
@ -106,6 +121,7 @@ if ($images != false) {
<tr class="yellow">
<td><h2><?php echo $image['title']; ?><form action="" method="post">
<input type="submit" name="accept" value="<?php echo $image['id']; ?>:Recover Image"/>
<input type="hidden" name="delhash" value="<?php echo $image['delhash']; ?>">
<input type="submit" name="remove" value="<?php echo $image['id']; ?>:Remove Image"/>
</form></h2></td>
</tr>

View File

@ -703,6 +703,16 @@
'debug' => false,
);
// website.com/gallery.php
// website.com/admin_gallery.php
// we use imgur as image host, and need to register app with them and add client/secret id.
// https://github.com/Znote/ZnoteAAC/wiki/IMGUR-powered-Gallery-page
$config['gallery'] = array(
'Client Name' => 'ZnoteAAC-Gallery',
'Client ID' => '4dfcdc4f2cabca6',
'Client Secret' => '697af737777c99a8c0be07c2f4419aebb2c48ac5'
);
// Email Server configurations (SMTP)
/* Please consider using a released stable version of PHPMailer or you may run into issues.
Download PHPMailer: https://github.com/PHPMailer/PHPMailer/releases

View File

@ -40,7 +40,8 @@ CREATE TABLE IF NOT EXISTS `znote_images` (
`desc` text NOT NULL,
`date` int NOT NULL,
`status` int NOT NULL,
`image` varchar(30) NOT NULL,
`image` varchar(50) NOT NULL,
`delhash` varchar(30) NOT NULL,
`account_id` int NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

View File

@ -6,21 +6,22 @@
// Fetch Images
function fetchImages($status) {
$status = (int)$status;
return mysql_select_multi("SELECT `id`, `title`, `desc`, `date`, `status`, `image`, `account_id` FROM znote_images WHERE `status`='$status' ORDER BY `date` DESC;");
return mysql_select_multi("SELECT `id`, `title`, `desc`, `date`, `status`, `image`, `delhash`, `account_id` FROM znote_images WHERE `status`='$status' ORDER BY `date` DESC;");
}
// Insert image data
function insertImage($account_id, $title, $desc, $image) {
function insertImage($account_id, $title, $desc, $image, $image_delete) {
$title = sanitize($title);
$desc = sanitize($desc);
$image = sanitize($image);
$image_delete = sanitize($image_delete);
$account_id = (int)$account_id;
$time = time();
// Insert only if image dosn't already exist there
$exist = mysql_select_single("SELECT `id` FROM `znote_images` WHERE `image`='$image' LIMIT 1;");
if ($exist === false) {
mysql_insert("INSERT INTO `znote_images` (`title`, `desc`, `date`, `status`, `image`, `account_id`) VALUES ('$title', '$desc', '$time', '1', '$image', '$account_id');");
mysql_insert("INSERT INTO `znote_images` (`title`, `desc`, `date`, `status`, `image`, `delhash`, `account_id`) VALUES ('$title', '$desc', '$time', '1', '$image', '$image_delete', '$account_id');");
return true;
}
return false;

View File

@ -1,35 +1,51 @@
<?php require_once 'engine/init.php'; include 'layout/overall/header.php';
/* SETUP INSTALLATION
- See comments above $config['gallery'] in config.php */
$logged_in = user_logged_in();
if ($logged_in === true) {
if (!empty($_POST['new'])) {
?>
<h1>Create image article</h1>
<p>Works with "Image Code" text from <a href="http://www.freeimagehosting.net/" target="_BLANK">www.freeimagehosting.net</a></p>
<form action="" method="post">
Image Code:<br /><input type="text" name="image" size="70"><br />
<p>This gallery is powered by IMGUR image host.</p>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:<br><input type="file" name="imagefile" id="imagefile"><br>
Image Title:<br /><input type="text" name="title" size="70"><br />
Image Description:<br /><textarea name="desc" cols="55" rows="15"></textarea><br />
<input type="submit" name="Submit" value="Post Image Article">
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
}
if (!empty($_POST['image']) && !empty($_POST['title']) && !empty($_POST['desc'])) {
$imageDom = $_POST['image'];
$imageSrc = false;
$doc=new DOMDocument();
$doc->loadHTML($imageDom);
$xml=simplexml_import_dom($doc); // just to make xpath more simple
$images=$xml->xpath('//img');
foreach ($images as $img) {
$imageSrc = (string)$img['src'];
}
if (isset($_FILES['imagefile']) && !empty($_FILES['imagefile'])) {
$image = file_get_contents($_FILES['imagefile']['tmp_name']);
$imgurClientID = $config['gallery']['Client ID'];
// Post image to imgur
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.imgur.com/3/image/");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
"type" => "file",
"name" => $_FILES['imagefile']['name'],
"image" => $image
]);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Client-ID {$imgurClientID}"
));
$response = json_decode(curl_exec($ch));
$image_url = $response->data->link;
$image_delete = $response->data->deletehash;
$title = $_POST['title'];
$desc = $_POST['desc'];
if ($imageSrc !== false) {
if ($image_url !== false) {
// Insert to database
$inserted = insertImage((int)$session_user_id, $title, $desc, $imageSrc);
$inserted = insertImage((int)$session_user_id, $title, $desc, $image_url, $image_delete);
if ($inserted === true) {
?>
<h1>Image Posted</h1>
@ -43,7 +59,7 @@ if ($logged_in === true) {
</tr>
<tr>
<td>
<a href="<?php echo $imageSrc; ?>" target="_BLANK"><img class="galleryImage" src="<?php echo $imageSrc; ?>" alt="<?php echo $title; ?>"/></a>
<a href="<?php echo $image_url; ?>" target="_BLANK"><img class="galleryImage" style="max-width: 100%;" src="<?php echo $image_url; ?>" alt="<?php echo $title; ?>"/></a>
</td>
</tr>
<tr>
@ -93,7 +109,7 @@ if (empty($_POST)) {
</tr>
<tr>
<td>
<a href="<?php echo $image['image']; ?>" target="_BLANK"><img class="galleryImage" src="<?php echo $image['image']; ?>" alt="<?php echo $image['title']; ?>"/></a>
<a href="<?php echo $image['image']; ?>" target="_BLANK"><img class="galleryImage" style="max-width: 100%;" src="<?php echo $image['image']; ?>" alt="<?php echo $image['title']; ?>"/></a>
</td>
</tr>
<tr>