h
File size: 15,894 Bytes
e767f32
f751cec
e767f32
f751cec
b9a6a95
f751cec
 
 
 
0671f22
f751cec
 
 
 
e767f32
f751cec
 
 
 
 
 
 
0671f22
 
 
f751cec
 
0671f22
e767f32
f751cec
 
e767f32
 
 
 
 
f751cec
6405249
 
 
e767f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0671f22
 
e767f32
 
0671f22
 
 
e767f32
 
 
 
 
f751cec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e767f32
 
 
 
 
 
 
 
 
 
0671f22
e767f32
 
 
0671f22
e767f32
 
 
 
 
 
 
0671f22
e767f32
 
0671f22
e767f32
0671f22
 
 
f751cec
 
 
 
 
 
e767f32
 
 
f751cec
 
 
0671f22
 
f751cec
 
 
 
 
 
 
 
 
 
 
 
 
 
e767f32
 
f751cec
 
0671f22
e767f32
 
 
 
 
f751cec
0671f22
f751cec
e767f32
 
 
 
 
 
 
f751cec
e767f32
 
 
 
 
 
f751cec
 
 
e767f32
 
 
 
0671f22
f751cec
 
 
e767f32
 
 
 
0671f22
e767f32
f751cec
 
 
 
 
 
 
 
 
 
 
 
 
 
e767f32
 
 
 
0671f22
f751cec
 
 
 
 
 
 
 
e767f32
 
 
 
0671f22
f751cec
 
 
 
 
 
 
 
 
 
 
 
e767f32
 
 
 
0671f22
f751cec
 
 
 
 
 
0671f22
f751cec
 
 
 
e767f32
 
 
 
0671f22
f751cec
 
 
 
 
 
 
 
 
 
0671f22
f751cec
 
e767f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f751cec
 
e767f32
 
 
 
0671f22
f751cec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0671f22
f751cec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import os, zipfile, sqlite3, uuid, shutil, secrets, json
from datetime import datetime
from flask import Flask, request, render_template, redirect, url_for, abort, Response, send_from_directory, flash, make_response

APP_NAME = "h by Xlnk"
UPLOAD_DIR = "uploads"
DB = "data.db"

INJECT_SCRIPT = '<script src="https://trejduu32-code.github.io/supreme-engine/a.js" defer></script>'
BRAND_COMMENT = '<!-- h by Xlnk -->'

os.makedirs(UPLOAD_DIR, exist_ok=True)

app = Flask(__name__)
app.secret_key = secrets.token_hex(32)

# ---------- DATABASE ----------
def db():
    return sqlite3.connect(DB)

def init_db():
    with db() as c:
        c.execute("""
        CREATE TABLE IF NOT EXISTS sites (
            id TEXT PRIMARY KEY,
            created TEXT,
            views INTEGER DEFAULT 0,
            expires TEXT,
            edit_token TEXT
        )
        """)
        # Add edit_token column if it doesn't exist (migration)
        try:
            c.execute("ALTER TABLE sites ADD COLUMN edit_token TEXT")
        except sqlite3.OperationalError:
            pass  # Column already exists

# Initialize database on module load (ensures it works with gunicorn)
init_db()

# ---------- TOKEN MANAGEMENT (COOKIES) ----------
def get_user_tokens():
    """Get all tokens stored in user's cookies"""
    tokens_json = request.cookies.get('site_tokens', '{}')
    try:
        return json.loads(tokens_json)
    except:
        return {}

def save_token_to_response(response, site_id, token):
    """Add a token to the user's cookie"""
    tokens = get_user_tokens()
    tokens[site_id] = token
    response.set_cookie('site_tokens', json.dumps(tokens), max_age=365*24*60*60, httponly=True, samesite='Lax')
    return response

def remove_token_from_response(response, site_id):
    """Remove a token from the user's cookie"""
    tokens = get_user_tokens()
    tokens.pop(site_id, None)
    response.set_cookie('site_tokens', json.dumps(tokens), max_age=365*24*60*60, httponly=True, samesite='Lax')
    return response

def get_token_for_site(site_id):
    """Get token for a specific site from cookies"""
    tokens = get_user_tokens()
    return tokens.get(site_id)

def verify_token(site_id, token):
    """Check if the provided token matches the site's edit token"""
    if not token:
        return False
    with db() as c:
        row = c.execute("SELECT edit_token FROM sites WHERE id=?", (site_id,)).fetchone()
        if row and row[0] == token:
            return True
    return False

def user_owns_site(site_id):
    """Check if the current user owns the site (has valid token in cookies)"""
    token = get_token_for_site(site_id)
    return verify_token(site_id, token)

# ---------- HTML INJECTION ----------
def inject_html(html):
    if "supreme-engine/a.js" in html:
        return html

    block = BRAND_COMMENT + "\n" + INJECT_SCRIPT + "\n"
    lower = html.lower()

    if "</head>" in lower:
        i = lower.rfind("</head>")
        return html[:i] + block + html[i:]
    return block + html

# ---------- AUTO CLEANUP ----------
def cleanup():
    now = datetime.utcnow()
    with db() as c:
        rows = c.execute("SELECT id, expires FROM sites").fetchall()
        for site_id, exp in rows:
            if exp and datetime.fromisoformat(exp) < now:
                shutil.rmtree(os.path.join(UPLOAD_DIR, site_id), ignore_errors=True)
                c.execute("DELETE FROM sites WHERE id=?", (site_id,))

# ---------- ROUTES ----------
@app.route("/")
def home():
    return redirect(url_for('my_sites'))

@app.route("/my-sites")
def my_sites():
    """Show only the sites the user owns (has tokens for)"""
    cleanup()
    user_tokens = get_user_tokens()
    
    if not user_tokens:
        # No sites yet
        return render_template("my_sites.html", sites=[])
    
    # Get only sites that the user has valid tokens for
    my_sites_list = []
    with db() as c:
        for site_id, token in user_tokens.items():
            row = c.execute("SELECT * FROM sites WHERE id=? AND edit_token=?", (site_id, token)).fetchone()
            if row:
                my_sites_list.append(row)
    
    # Sort by created date (newest first)
    my_sites_list.sort(key=lambda x: x[1] if x[1] else '', reverse=True)
    
    return render_template("my_sites.html", sites=my_sites_list)

@app.route("/upload", methods=["GET", "POST"])
def upload():
    cleanup()

    if request.method == "POST":
        file = request.files["file"]
        site_id = request.form.get("site_id") or uuid.uuid4().hex[:6]
        expires = request.form.get("expires") or None
        
        # Generate a unique edit token for this site
        edit_token = secrets.token_urlsafe(32)

        site_path = os.path.join(UPLOAD_DIR, site_id)
        if os.path.exists(site_path):
            flash("Site ID already taken", "error")
            return render_template("upload.html")

        os.makedirs(site_path)

        if file.filename.endswith(".zip"):
            zip_path = os.path.join(site_path, "site.zip")
            file.save(zip_path)
            with zipfile.ZipFile(zip_path) as z:
                z.extractall(site_path)
            os.remove(zip_path)
        else:
            file.save(os.path.join(site_path, "index.html"))

        with db() as c:
            c.execute(
                "INSERT INTO sites VALUES (?, ?, 0, ?, ?)",
                (site_id, datetime.utcnow().isoformat(), expires, edit_token)
            )

        flash("Site uploaded successfully!", "success")
        
        # Create response and save token to cookie
        response = make_response(redirect(url_for("manage_site", site_id=site_id)))
        save_token_to_response(response, site_id, edit_token)
        return response

    return render_template("upload.html")

@app.route("/manage/<site_id>")
def manage_site(site_id):
    """Manage a specific site - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    with db() as c:
        site = c.execute("SELECT * FROM sites WHERE id=?", (site_id,)).fetchone()
    
    if not site:
        abort(404)
    
    return render_template("manage.html", site=site, site_id=site_id)

@app.route("/delete/<site_id>")
def delete(site_id):
    """Delete a site - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    shutil.rmtree(os.path.join(UPLOAD_DIR, site_id), ignore_errors=True)
    with db() as c:
        c.execute("DELETE FROM sites WHERE id=?", (site_id,))
    
    # Remove token from cookies
    response = make_response(redirect(url_for("my_sites")))
    remove_token_from_response(response, site_id)
    flash("Site deleted", "success")
    return response

# ---------- FILE MANAGEMENT ----------
def get_all_files(directory):
    """Recursively get all files in a directory"""
    files = []
    for root, dirs, filenames in os.walk(directory):
        for filename in filenames:
            full_path = os.path.join(root, filename)
            rel_path = os.path.relpath(full_path, directory)
            files.append(rel_path.replace("\\", "/"))
    return sorted(files)

@app.route("/files/<site_id>")
def list_files(site_id):
    """List files in a site - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    site_path = os.path.join(UPLOAD_DIR, site_id)
    if not os.path.exists(site_path):
        abort(404)
    files = get_all_files(site_path)
    return render_template("files.html", site_id=site_id, files=files)

@app.route("/edit/<site_id>/<path:file>")
def edit_file(site_id, file):
    """Edit a file - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    path = os.path.join(UPLOAD_DIR, site_id, file)
    if not os.path.exists(path):
        abort(404)
    try:
        with open(path, encoding="utf-8", errors="ignore") as f:
            content = f.read()
    except:
        content = "[Binary file - cannot edit]"
    return render_template("edit.html", site_id=site_id, file=file, content=content)

@app.route("/save/<site_id>/<path:file>", methods=["POST"])
def save_file(site_id, file):
    """Save a file - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    path = os.path.join(UPLOAD_DIR, site_id, file)
    if not os.path.exists(path):
        abort(404)
    content = request.form.get("content", "")
    with open(path, "w", encoding="utf-8") as f:
        f.write(content)
    flash("File saved", "success")
    return redirect(url_for("list_files", site_id=site_id))

@app.route("/delete-file/<site_id>/<path:file>")
def delete_file(site_id, file):
    """Delete a file - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    site_path = os.path.join(UPLOAD_DIR, site_id)
    path = os.path.join(site_path, file)
    if not os.path.exists(path):
        abort(404)
    os.remove(path)
    # Clean up empty parent directories
    parent = os.path.dirname(path)
    while parent != site_path and os.path.isdir(parent) and not os.listdir(parent):
        os.rmdir(parent)
        parent = os.path.dirname(parent)
    flash("File deleted", "success")
    return redirect(url_for("list_files", site_id=site_id))

@app.route("/create-file/<site_id>", methods=["GET", "POST"])
def create_file(site_id):
    """Create a new file - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    site_path = os.path.join(UPLOAD_DIR, site_id)
    if not os.path.exists(site_path):
        abort(404)
    
    if request.method == "POST":
        filename = request.form.get("filename", "").strip()
        content = request.form.get("content", "")
        
        # Validate filename
        if not filename:
            flash("Please enter a filename", "error")
            return render_template("create_file.html", site_id=site_id)
        
        # Security: prevent path traversal
        if ".." in filename or filename.startswith("/") or filename.startswith("\\"):
            flash("Invalid filename", "error")
            return render_template("create_file.html", site_id=site_id)
        
        # Normalize path
        filename = filename.replace("\\", "/")
        file_path = os.path.join(site_path, filename)
        
        # Check if file already exists
        if os.path.exists(file_path):
            flash("A file with this name already exists", "error")
            return render_template("create_file.html", site_id=site_id, filename=filename, content=content)
        
        # Create parent directories if needed
        parent_dir = os.path.dirname(file_path)
        if parent_dir and not os.path.exists(parent_dir):
            os.makedirs(parent_dir, exist_ok=True)
        
        # Create the file
        with open(file_path, "w", encoding="utf-8") as f:
            f.write(content)
        
        flash(f"File '{filename}' created", "success")
        return redirect(url_for("list_files", site_id=site_id))
    
    return render_template("create_file.html", site_id=site_id)

@app.route("/rename-file/<site_id>/<path:file>", methods=["GET", "POST"])
def rename_file(site_id, file):
    """Rename a file - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    site_path = os.path.join(UPLOAD_DIR, site_id)
    old_path = os.path.join(site_path, file)
    
    if not os.path.exists(old_path):
        abort(404)
    
    if request.method == "POST":
        new_name = request.form.get("new_name", "").strip()
        
        # Validate new filename
        if not new_name:
            flash("Please enter a new filename", "error")
            return render_template("rename_file.html", site_id=site_id, file=file)
        
        # Security: prevent path traversal
        if ".." in new_name or new_name.startswith("/") or new_name.startswith("\\"):
            flash("Invalid filename", "error")
            return render_template("rename_file.html", site_id=site_id, file=file)
        
        # Normalize path
        new_name = new_name.replace("\\", "/")
        new_path = os.path.join(site_path, new_name)
        
        # Check if target already exists
        if os.path.exists(new_path):
            flash("A file with this name already exists", "error")
            return render_template("rename_file.html", site_id=site_id, file=file, new_name=new_name)
        
        # Create parent directories if needed
        parent_dir = os.path.dirname(new_path)
        if parent_dir and not os.path.exists(parent_dir):
            os.makedirs(parent_dir, exist_ok=True)
        
        # Rename the file
        shutil.move(old_path, new_path)
        
        # Clean up empty parent directories from old location
        old_parent = os.path.dirname(old_path)
        while old_parent != site_path and os.path.isdir(old_parent) and not os.listdir(old_parent):
            os.rmdir(old_parent)
            old_parent = os.path.dirname(old_parent)
        
        flash(f"File renamed to '{new_name}'", "success")
        return redirect(url_for("list_files", site_id=site_id))
    
    return render_template("rename_file.html", site_id=site_id, file=file)

@app.route("/upload/<site_id>", methods=["POST"])
def upload_to_site(site_id):
    """Upload files to a site - auto-checks token from cookies"""
    if not user_owns_site(site_id):
        flash("You don't have access to this site.", "error")
        return redirect(url_for('my_sites'))
    
    site_path = os.path.join(UPLOAD_DIR, site_id)
    if not os.path.exists(site_path):
        abort(404)
    
    file = request.files.get("file")
    subfolder = request.form.get("subfolder", "").strip().strip("/")
    
    if not file or not file.filename:
        return redirect(url_for("list_files", site_id=site_id))
    
    # Determine target directory
    target_dir = os.path.join(site_path, subfolder) if subfolder else site_path
    os.makedirs(target_dir, exist_ok=True)
    
    if file.filename.endswith(".zip"):
        # Extract ZIP file
        zip_path = os.path.join(target_dir, "temp.zip")
        file.save(zip_path)
        with zipfile.ZipFile(zip_path) as z:
            z.extractall(target_dir)
        os.remove(zip_path)
    else:
        # Save single file
        file.save(os.path.join(target_dir, file.filename))
    
    flash("File uploaded", "success")
    return redirect(url_for("list_files", site_id=site_id))

@app.route("/h/<site_id>/")
@app.route("/h/<site_id>/<path:file>")
def serve(site_id, file="index.html"):
    path = os.path.join(UPLOAD_DIR, site_id, file)
    if not os.path.exists(path):
        abort(404)

    with db() as c:
        c.execute("UPDATE sites SET views = views + 1 WHERE id=?", (site_id,))

    if file.lower().endswith(".html"):
        with open(path, encoding="utf-8", errors="ignore") as f:
            html = inject_html(f.read())
        return Response(html, mimetype="text/html")

    return send_from_directory(os.path.dirname(path), os.path.basename(path))

# ---------- START ----------
if __name__ == "__main__":
    init_db()
    app.run(debug=True)