Linux websever 5.15.0-153-generic #163-Ubuntu SMP Thu Aug 7 16:37:18 UTC 2025 x86_64
Apache/2.4.52 (Ubuntu)
: 192.168.3.70 | : 192.168.1.99
Cant Read [ /etc/named.conf ]
8.1.2-1ubuntu2.23
urlab
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
CREATE WP USER
README
+ Create Folder
+ Create File
/
var /
www /
html /
cqt /
server /
routes /
[ HOME SHELL ]
Name
Size
Permission
Action
auth.js
2.8
KB
-rw-r--r--
courses.js
5.97
KB
-rw-r--r--
labs.js
9.38
KB
-rw-r--r--
news.js
6.1
KB
-rw-r--r--
publications.js
8.21
KB
-rw-r--r--
rebuild.js
1.09
KB
-rw-r--r--
research.js
6.83
KB
-rw-r--r--
team.js
6.52
KB
-rw-r--r--
workshops.js
6.01
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : publications.js
const express = require('express'); const router = express.Router(); const fs = require('fs').promises; const path = require('path'); const authenticateToken = require('../middleware/auth'); const { validateId, validatePublication } = require('../middleware/validation'); const multer = require('multer'); const crypto = require('crypto'); // Helper function to check if two files are identical async function filesAreIdentical(path1, path2) { try { const [file1, file2] = await Promise.all([ fs.readFile(path1), fs.readFile(path2) ]); const hash1 = crypto.createHash('md5').update(file1).digest('hex'); const hash2 = crypto.createHash('md5').update(file2).digest('hex'); return hash1 === hash2; } catch (error) { return false; } } // Multer storage for publications images const publicationsStorage = multer.diskStorage({ destination: (req, file, cb) => { const dest = path.join(__dirname, '../../public/images/publications'); require('fs').mkdirSync(dest, { recursive: true }); cb(null, dest); }, filename: (req, file, cb) => { const ext = path.extname(file.originalname) || ''; const name = `${Date.now()}-${Math.random().toString(36).slice(2,8)}${ext}`; cb(null, name); } }); const pdfFilter = (req, file, cb) => { const allowedMimes = ['application/pdf']; if (allowedMimes.includes(file.mimetype)) { cb(null, true); } else { cb(new Error('Invalid file type. Only PDF files are allowed.'), false); } }; const upload = multer({ storage: publicationsStorage, fileFilter: pdfFilter, limits: { fileSize: 10 * 1024 * 1024, files: 1 } }); const DATA_DIR = path.join(__dirname, '../../public/data'); const FILENAME = 'publications.json'; const FILEPATH = path.join(DATA_DIR, FILENAME); // Backup function async function createBackup() { try { const data = await fs.readFile(FILEPATH, 'utf8'); const timestamp = new Date().toISOString().replace(/:/g, '-'); const backupPath = path.join(DATA_DIR, `backups/${FILENAME.replace('.json', '')}_${timestamp}.json`); // Ensure backups directory exists await fs.mkdir(path.join(DATA_DIR, 'backups'), { recursive: true }); await fs.writeFile(backupPath, data); console.log(`Backup created: ${backupPath}`); } catch (error) { console.error('Error creating backup:', error); } } // Read data async function readData() { try { const data = await fs.readFile(FILEPATH, 'utf8'); return JSON.parse(data); } catch (error) { console.error('Error reading publications:', error); return []; } } // Write data async function writeData(data) { await createBackup(); await fs.writeFile(FILEPATH, JSON.stringify(data, null, 2)); } // GET all publications router.get('/', async (req, res) => { try { const publications = await readData(); res.json(publications); } catch (error) { res.status(500).json({ error: 'Failed to fetch publications' }); } }); // GET single publication by ID router.get('/:id', validateId(), async (req, res) => { try { const publications = await readData(); const publication = publications.find(p => p.id === req.params.id); if (!publication) { return res.status(404).json({ error: 'Publication not found' }); } res.json(publication); } catch (error) { res.status(500).json({ error: 'Failed to fetch publication' }); } }); // POST new publication (protected) router.post('/', authenticateToken, upload.single('pdf'), validatePublication(), async (req, res) => { try { const publications = await readData(); const newPublication = { id: req.body.id || `pub${Date.now()}`, ...req.body, authors: Array.isArray(req.body.authors) ? req.body.authors : typeof req.body.authors === 'string' ? req.body.authors.split(',').map(a => a.trim()) : [] }; // If a PDF was uploaded, store its public path if (req.file) { newPublication.pdf = `/images/publications/${req.file.filename}`; } publications.push(newPublication); await writeData(publications); res.status(201).json(newPublication); } catch (error) { console.error('Error creating publication:', error); res.status(500).json({ error: 'Failed to create publication' }); } }); // PUT update publication (protected) router.put('/:id', authenticateToken, validateId(), upload.single('pdf'), validatePublication(), async (req, res) => { try { const publications = await readData(); const index = publications.findIndex(p => p.id === req.params.id); if (index === -1) { return res.status(404).json({ error: 'Publication not found' }); } // Remove pdf from body to prevent overwriting uploaded file const { pdf: _, ...bodyWithoutPdf } = req.body; // If a new PDF was uploaded, check if it's identical to existing one if (req.file) { const newFilePath = path.join(__dirname, '../../public/images/publications', req.file.filename); const oldPdf = publications[index].pdf; let shouldUseNewFile = true; // Check if old PDF exists and compare with new upload if (oldPdf && typeof oldPdf === 'string' && oldPdf.startsWith('/images/publications/')) { const oldPath = path.join(__dirname, '../../public', oldPdf.replace(/^\//, '')); const areIdentical = await filesAreIdentical(oldPath, newFilePath); if (areIdentical) { // Files are identical, remove the newly uploaded file and keep the old one await fs.unlink(newFilePath).catch(() => {}); shouldUseNewFile = false; console.log('ℹ️ Identical file detected, keeping existing PDF'); } else { // Files are different, remove old file await fs.unlink(oldPath).catch(() => {}); } } if (shouldUseNewFile) { publications[index] = { ...publications[index], ...bodyWithoutPdf, pdf: `/images/publications/${req.file.filename}`, id: req.params.id, authors: Array.isArray(bodyWithoutPdf.authors) ? bodyWithoutPdf.authors : typeof bodyWithoutPdf.authors === 'string' ? bodyWithoutPdf.authors.split(',').map(a => a.trim()) : publications[index].authors }; } else { publications[index] = { ...publications[index], ...bodyWithoutPdf, id: req.params.id, authors: Array.isArray(bodyWithoutPdf.authors) ? bodyWithoutPdf.authors : typeof bodyWithoutPdf.authors === 'string' ? bodyWithoutPdf.authors.split(',').map(a => a.trim()) : publications[index].authors }; } } else { publications[index] = { ...publications[index], ...bodyWithoutPdf, id: req.params.id, authors: Array.isArray(bodyWithoutPdf.authors) ? bodyWithoutPdf.authors : typeof bodyWithoutPdf.authors === 'string' ? bodyWithoutPdf.authors.split(',').map(a => a.trim()) : publications[index].authors }; } await writeData(publications); res.json(publications[index]); } catch (error) { console.error('Error updating publication:', error); res.status(500).json({ error: 'Failed to update publication' }); } }); // DELETE publication (protected) router.delete('/:id', authenticateToken, validateId(), async (req, res) => { try { const publications = await readData(); const publication = publications.find(p => p.id === req.params.id); if (!publication) { return res.status(404).json({ error: 'Publication not found' }); } // Remove associated PDF file if exists const oldPdf = publication.pdf; if (oldPdf && typeof oldPdf === 'string' && oldPdf.startsWith('/images/publications/')) { const oldPath = path.join(__dirname, '../../public', oldPdf.replace(/^\//, '')); await fs.unlink(oldPath).catch(() => {}); } const filteredPublications = publications.filter(p => p.id !== req.params.id); await writeData(filteredPublications); res.json({ message: 'Publication deleted successfully' }); } catch (error) { console.error('Error deleting publication:', error); res.status(500).json({ error: 'Failed to delete publication' }); } }); module.exports = router;
Close