Files
image-pichost/api-docs.php
2025-11-30 13:05:45 +00:00

152 lines
8.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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>
&nbsp;&nbsp;"success": true,<br>
&nbsp;&nbsp;"data": {<br>
&nbsp;&nbsp;&nbsp;&nbsp;"id": 123,<br>
&nbsp;&nbsp;&nbsp;&nbsp;"title": "<?php echo t('image_title'); ?>",<br>
&nbsp;&nbsp;&nbsp;&nbsp;"url": "<?php echo SITE_URL; ?>/view-image.php?id=123",<br>
&nbsp;&nbsp;&nbsp;&nbsp;"direct_url": "<?php echo SITE_URL; ?>/uploads/filename.jpg",<br>
&nbsp;&nbsp;&nbsp;&nbsp;"tags": ["<?php echo t('landscape'); ?>", "<?php echo t('nature'); ?>"]<br>
&nbsp;&nbsp;}<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>
&nbsp;&nbsp;"success": true,<br>
&nbsp;&nbsp;"data": [<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"id": 123,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"title": "<?php echo t('image_title'); ?>",<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"url": "<?php echo SITE_URL; ?>/view-image.php?id=123",<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"direct_url": "<?php echo SITE_URL; ?>/uploads/filename.jpg",<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"is_public": 1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"views": 45,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"file_size_formatted": "2.5 MB"<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;],<br>
&nbsp;&nbsp;"pagination": {<br>
&nbsp;&nbsp;&nbsp;&nbsp;"page": 1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;"limit": 20,<br>
&nbsp;&nbsp;&nbsp;&nbsp;"total": 150,<br>
&nbsp;&nbsp;&nbsp;&nbsp;"pages": 8<br>
&nbsp;&nbsp;}<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>
&nbsp;&nbsp;-F "api_key=<?php echo $user['api_key'] ?? 'YOUR_API_KEY'; ?>" \<br>
&nbsp;&nbsp;-F "title=<?php echo t('my_image'); ?>" \<br>
&nbsp;&nbsp;-F "is_public=1" \<br>
&nbsp;&nbsp;-F "image=@/path/to/your/image.jpg" \<br>
&nbsp;&nbsp;"<?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>