-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_snippet.php
More file actions
136 lines (126 loc) · 4.16 KB
/
save_snippet.php
File metadata and controls
136 lines (126 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
// Set headers
header('Content-Type: application/json');
// Get POST data
$input = json_decode(file_get_contents('php://input'), true);
// Validate input
if (!$input || !isset($input['title']) || !isset($input['language']) || !isset($input['code'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid snippet data']);
exit;
}
// Generate unique ID and filename
$snippetId = uniqid();
$filename = "snippets/{$snippetId}.php";
// Create snippet data
$snippetData = [
'id' => $snippetId,
'title' => $input['title'],
'description' => $input['description'] ?? '',
'language' => $input['language'],
'framework' => $input['framework'] ?? null,
'code' => $input['code'],
'tags' => $input['tags'] ?? [],
'visibility' => $input['visibility'] ?? 'public',
'license' => $input['license'] ?? 'mit',
'gistUrl' => $input['gistUrl'] ?? null,
'isFavorite' => $input['isFavorite'] ?? false,
'created' => $input['created'] ?? date('Y-m-d H:i:s'),
'stars' => $input['stars'] ?? 0
];
// Create snippet file content
$fileContent = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$snippetData['title']} - Code Snippet</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.snippet-header {
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 1px solid #eee;
}
.snippet-title {
font-size: 24px;
margin: 0 0 10px;
}
.snippet-meta {
color: #666;
font-size: 14px;
}
.code-block {
background: #f8f8f8;
border-radius: 4px;
overflow: hidden;
margin: 20px 0;
}
.code-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 15px;
background: #e8e8e8;
border-bottom: 1px solid #ddd;
}
.code-language {
font-weight: bold;
text-transform: capitalize;
}
.code-content {
padding: 15px;
overflow-x: auto;
}
pre {
margin: 0;
border: 2px solid crimson;
}
</style>
</head>
<body>
<div class="snippet-header">
<h1 class="snippet-title">{$snippetData['title']}</h1>
<div class="snippet-meta">
<span>Language: {$snippetData['language']}</span>
<span> | </span>
<span>Created: {$snippetData['created']}</span>
</div>
</div>
<div class="snippet-description">
<p>{$snippetData['description']}</p>
</div>
<div class="code-block">
<div class="code-header">
<span class="code-language">{$snippetData['language']}</span>
</div>
<div class="code-content">
<pre><code class="language-{$snippetData['language']}"><?php echo htmlspecialchars(\$snippetData['code']); ?></code></pre>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
</body>
</html>
HTML;
// Save the snippet file
if (file_put_contents($filename, $fileContent) {
// Also save to a central database file (optional)
$databaseFile = 'snippets/database.json';
$database = file_exists($databaseFile) ? json_decode(file_get_contents($databaseFile), true) : [];
$database[$snippetId] = $snippetData;
file_put_contents($databaseFile, json_encode($database, JSON_PRETTY_PRINT));
echo json_encode(['success' => true, 'id' => $snippetId]);
} else {
http_response_code(500);
echo json_encode(['error' => 'Could not save snippet file']);
}