152 lines
8.3 KiB
PHP
152 lines
8.3 KiB
PHP
|
|
<?php
|
|||
|
|
require_once 'config.php';
|
|||
|
|
|
|||
|
|
if (!isset($_SESSION['user_id'])) {
|
|||
|
|
header('Location: login.php');
|
|||
|
|
exit;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
$stmt = $pdo->prepare("SELECT api_key FROM users WHERE id = ?");
|
|||
|
|
$stmt->execute([$_SESSION['user_id']]);
|
|||
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|||
|
|
} catch(PDOException $e) {
|
|||
|
|
$user = ['api_key' => null];
|
|||
|
|
}
|
|||
|
|
?>
|
|||
|
|
<!DOCTYPE html>
|
|||
|
|
<html lang="<?php echo $lang; ?>" data-theme="<?php echo $currentUserSettings['dark_mode'] ? 'dark' : 'light'; ?>">
|
|||
|
|
<head>
|
|||
|
|
<meta charset="UTF-8">
|
|||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|||
|
|
<title><?php echo t('api_docs'); ?> - <?php echo SITE_NAME; ?></title>
|
|||
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
|||
|
|
<link rel="stylesheet" href="css/style.css">
|
|||
|
|
</head>
|
|||
|
|
<body>
|
|||
|
|
<?php include 'components/navbar.php'; ?>
|
|||
|
|
|
|||
|
|
<div class="container">
|
|||
|
|
<h1><i class="fas fa-code"></i> <?php echo t('api_documentation'); ?></h1>
|
|||
|
|
|
|||
|
|
<div class="api-docs-container">
|
|||
|
|
<section class="api-section card">
|
|||
|
|
<h2><i class="fas fa-key"></i> <?php echo t('api_management'); ?></h2>
|
|||
|
|
<?php if(empty($user['api_key'])): ?>
|
|||
|
|
<p><?php echo t('no_api_key_message'); ?></p>
|
|||
|
|
<form method="POST" action="generate-api-key.php">
|
|||
|
|
<button type="submit" class="btn btn-primary">
|
|||
|
|
<i class="fas fa-plus"></i> <?php echo t('generate_api_key'); ?>
|
|||
|
|
</button>
|
|||
|
|
</form>
|
|||
|
|
<?php else: ?>
|
|||
|
|
<div class="api-key-display">
|
|||
|
|
<label><strong><i class="fas fa-key"></i> <?php echo t('your_api_key'); ?>:</strong></label>
|
|||
|
|
<div class="api-key-input">
|
|||
|
|
<input type="text" value="<?php echo $user['api_key']; ?>" id="apiKey" readonly>
|
|||
|
|
<button onclick="copyApiKey()" class="btn">
|
|||
|
|
<i class="fas fa-copy"></i> <?php echo t('copy'); ?>
|
|||
|
|
</button>
|
|||
|
|
<form method="POST" action="generate-api-key.php" style="display: inline;">
|
|||
|
|
<button type="submit" class="btn btn-secondary" onclick="return confirm('<?php echo t('confirm_regenerate_api_key'); ?>')">
|
|||
|
|
<i class="fas fa-sync"></i> <?php echo t('regenerate_key'); ?>
|
|||
|
|
</button>
|
|||
|
|
</form>
|
|||
|
|
</div>
|
|||
|
|
<small><i class="fas fa-shield-alt"></i> <?php echo t('keep_secret'); ?></small>
|
|||
|
|
</div>
|
|||
|
|
<?php endif; ?>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<section class="api-section card mt-3">
|
|||
|
|
<h2><i class="fas fa-plug"></i> <?php echo t('api_endpoints'); ?></h2>
|
|||
|
|
|
|||
|
|
<div class="api-endpoint">
|
|||
|
|
<h3><i class="fas fa-cloud-upload-alt"></i> 1. <?php echo t('upload_image'); ?></h3>
|
|||
|
|
<div class="code-block">
|
|||
|
|
<span class="endpoint">POST</span> <?php echo SITE_URL; ?>/api/upload.php<br><br>
|
|||
|
|
<span class="comment">// <?php echo t('headers'); ?>:</span><br>
|
|||
|
|
Content-Type: multipart/form-data<br><br>
|
|||
|
|
<span class="comment">// <?php echo t('parameters'); ?>:</span><br>
|
|||
|
|
api_key = <span class="param">string</span> (<?php echo t('required'); ?>)<br>
|
|||
|
|
image = <span class="param">file</span> (<?php echo t('required'); ?>)<br>
|
|||
|
|
title = <span class="param">string</span> (<?php echo t('optional'); ?>)<br>
|
|||
|
|
tags = <span class="param">string</span> (<?php echo t('optional'); ?>)<br>
|
|||
|
|
is_public = <span class="param">integer</span> (<?php echo t('optional'); ?>)<br><br>
|
|||
|
|
<span class="comment">// <?php echo t('success_response'); ?>:</span><br>
|
|||
|
|
{<br>
|
|||
|
|
"success": true,<br>
|
|||
|
|
"data": {<br>
|
|||
|
|
"id": 123,<br>
|
|||
|
|
"title": "<?php echo t('image_title'); ?>",<br>
|
|||
|
|
"url": "<?php echo SITE_URL; ?>/view-image.php?id=123",<br>
|
|||
|
|
"direct_url": "<?php echo SITE_URL; ?>/uploads/filename.jpg",<br>
|
|||
|
|
"tags": ["<?php echo t('landscape'); ?>", "<?php echo t('nature'); ?>"]<br>
|
|||
|
|
}<br>
|
|||
|
|
}
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="api-endpoint mt-3">
|
|||
|
|
<h3><i class="fas fa-images"></i> 2. <?php echo t('get_images'); ?></h3>
|
|||
|
|
<div class="code-block">
|
|||
|
|
<span class="endpoint">GET</span> <?php echo SITE_URL; ?>/api/images.php?api_key=<?php echo $user['api_key'] ?? 'YOUR_API_KEY'; ?>&page=1&limit=20<br><br>
|
|||
|
|
<span class="comment">// <?php echo t('parameters'); ?>:</span><br>
|
|||
|
|
api_key = <span class="param">string</span> (<?php echo t('required'); ?>)<br>
|
|||
|
|
page = <span class="param">integer</span> (<?php echo t('optional'); ?>)<br>
|
|||
|
|
limit = <span class="param">integer</span> (<?php echo t('optional'); ?>)<br><br>
|
|||
|
|
<span class="comment">// <?php echo t('success_response'); ?>:</span><br>
|
|||
|
|
{<br>
|
|||
|
|
"success": true,<br>
|
|||
|
|
"data": [<br>
|
|||
|
|
{<br>
|
|||
|
|
"id": 123,<br>
|
|||
|
|
"title": "<?php echo t('image_title'); ?>",<br>
|
|||
|
|
"url": "<?php echo SITE_URL; ?>/view-image.php?id=123",<br>
|
|||
|
|
"direct_url": "<?php echo SITE_URL; ?>/uploads/filename.jpg",<br>
|
|||
|
|
"is_public": 1,<br>
|
|||
|
|
"views": 45,<br>
|
|||
|
|
"file_size_formatted": "2.5 MB"<br>
|
|||
|
|
}<br>
|
|||
|
|
],<br>
|
|||
|
|
"pagination": {<br>
|
|||
|
|
"page": 1,<br>
|
|||
|
|
"limit": 20,<br>
|
|||
|
|
"total": 150,<br>
|
|||
|
|
"pages": 8<br>
|
|||
|
|
}<br>
|
|||
|
|
}
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="api-endpoint mt-3">
|
|||
|
|
<h3><i class="fas fa-terminal"></i> 3. <?php echo t('curl_examples'); ?></h3>
|
|||
|
|
<div class="code-block">
|
|||
|
|
<span class="comment"># <?php echo t('upload_image'); ?></span><br>
|
|||
|
|
curl -X POST \<br>
|
|||
|
|
-F "api_key=<?php echo $user['api_key'] ?? 'YOUR_API_KEY'; ?>" \<br>
|
|||
|
|
-F "title=<?php echo t('my_image'); ?>" \<br>
|
|||
|
|
-F "is_public=1" \<br>
|
|||
|
|
-F "image=@/path/to/your/image.jpg" \<br>
|
|||
|
|
"<?php echo SITE_URL; ?>/api/upload.php"
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="code-block mt-2">
|
|||
|
|
<span class="comment"># <?php echo t('get_images'); ?></span><br>
|
|||
|
|
curl "<?php echo SITE_URL; ?>/api/images.php?api_key=<?php echo $user['api_key'] ?? 'YOUR_API_KEY'; ?>&page=1&limit=10"
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<script>
|
|||
|
|
function copyApiKey() {
|
|||
|
|
const apiKey = document.getElementById('apiKey');
|
|||
|
|
apiKey.select();
|
|||
|
|
document.execCommand('copy');
|
|||
|
|
alert('<?php echo t('api_key_copied'); ?>');
|
|||
|
|
}
|
|||
|
|
</script>
|
|||
|
|
</body>
|
|||
|
|
</html>
|