|
|
折腾了一晚上搞定了,
目前仅在chez.com空间测试可用,
其他空间没有测试,
功能特点如下:
单页文件,
支持注册,
支持改密,
图片链接有四种,
支持全选复制图片链接
禁止注册成员,
删除注册成员,
注册开启和关闭,
新建相册,
编辑相册名称等。
目前为了安全仅支持注册上传。
演示:http://sotu.chez.com
喜欢折腾的可以二次开发并分享出来。
复制源码保存到index.php
默认密码:admin123
- <?php
- error_reporting(0);
- $base = dirname(__FILE__);
- $s_path = $base . '/sessions';
- if (!is_dir($s_path)) @mkdir($s_path, 0777, true);
- @session_save_path($s_path);
- @session_start();
- // 1. 登录校验与状态实时检查
- if (!isset($_SESSION['uid']) && isset($_COOKIE['app_auth'])) {
- $c = explode('|', base64_decode($_COOKIE['app_auth']));
- if (count($c) == 3) { $_SESSION['uid'] = $c[0]; $_SESSION['user'] = $c[1]; $_SESSION['role'] = $c[2]; }
- }
- // 2. 数据库初始化
- try {
- $db = new PDO('sqlite:' . $base . '/data_store.db');
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $db->exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT UNIQUE, pass TEXT, status INTEGER DEFAULT 1, role TEXT DEFAULT 'user')");
- $db->exec("CREATE TABLE IF NOT EXISTS albums (id INTEGER PRIMARY KEY AUTOINCREMENT, uid INTEGER, name TEXT, time DATETIME)");
- $db->exec("CREATE TABLE IF NOT EXISTS imgs (id INTEGER PRIMARY KEY AUTOINCREMENT, uid INTEGER, aid INTEGER DEFAULT 0, path TEXT, name TEXT, time DATETIME)");
- $db->exec("CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, val TEXT)");
- if (!$db->query("SELECT count(*) FROM config WHERE key='reg_open'")->fetchColumn()) $db->exec("INSERT INTO config (key, val) VALUES ('reg_open', '1')");
- if (!$db->query("SELECT count(*) FROM users WHERE user='admin'")->fetchColumn()) $db->prepare("INSERT INTO users (user, pass, role) VALUES ('admin', ?, 'admin')")->execute(array(md5('admin123')));
- } catch (Exception $e) { die("Database Error"); }
- // 实时校验用户是否被禁用
- if (isset($_SESSION['uid'])) {
- $u_check = $db->prepare("SELECT status FROM users WHERE id=?");
- $u_check->execute(array($_SESSION['uid']));
- $u_stat = $u_check->fetchColumn();
- if ($u_stat === '0') { session_destroy(); setcookie('app_auth', '', 0, "/"); header("Location: index.php?msg=".urlencode("您的账号已被管理员禁用")); exit; }
- }
- $msg = isset($_GET['msg']) ? $_GET['msg'] : "";
- $act = isset($_GET['action']) ? $_GET['action'] : '';
- $aid = isset($_GET['aid']) ? $_GET['aid'] : 'none';
- $view_uid = isset($_GET['view_uid']) ? (int)$_GET['view_uid'] : 0;
- $is_adm = (isset($_SESSION['role']) && $_SESSION['role'] == 'admin');
- $reg_open = $db->query("SELECT val FROM config WHERE key='reg_open'")->fetchColumn();
- // 3. POST 逻辑
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- if ($act == 'login') {
- $st = $db->prepare("SELECT * FROM users WHERE user=? AND pass=?");
- $st->execute(array($_POST['u'], md5($_POST['p'])));
- if ($r = $st->fetch()) {
- if ($r['status'] == 0) { header("Location: index.php?msg=".urlencode("账号已被禁用")); exit; }
- $_SESSION['uid']=$r['id']; $_SESSION['user']=$r['user']; $_SESSION['role']=$r['role'];
- setcookie('app_auth', base64_encode($r['id'].'|'.$r['user'].'|'.$r['role']), time()+86400*7, "/");
- header("Location: index.php"); exit;
- } else { header("Location: index.php?msg=".urlencode("账号或密码错误")); exit; }
- }
- if ($act == 'reg' && $reg_open == '1') {
- try {
- $db->prepare("INSERT INTO users (user, pass) VALUES (?, ?)")->execute(array($_POST['u'], md5($_POST['p'])));
- header("Location: index.php?msg=".urlencode("注册成功")); exit;
- } catch(Exception $e) { header("Location: index.php?action=to_reg&msg=".urlencode("账号已存在")); exit; }
- }
- if (isset($_SESSION['uid'])) {
- if ($act == 'edit_album') {
- $db->prepare("UPDATE albums SET name=? WHERE id=? AND uid=?")->execute(array($_POST['new_name'], $_POST['album_id'], $_SESSION['uid']));
- header("Location: index.php"); exit;
- }
- if ($act == 'my_pass') {
- $db->prepare("UPDATE users SET pass=? WHERE id=?")->execute(array(md5($_POST['newp']), $_SESSION['uid']));
- header("Location: index.php?action=profile&msg=".urlencode("密码修改成功")); exit;
- }
- if ($is_adm) {
- if ($act == 'toggle_reg') {
- $db->prepare("UPDATE config SET val=? WHERE key='reg_open'")->execute(array($reg_open == '1' ? '0' : '1'));
- header("Location: index.php?action=users"); exit;
- }
- if ($act == 'toggle_user_stat') {
- $target_uid = (int)$_POST['uid'];
- if ($target_uid > 1 && $target_uid != $_SESSION['uid']) {
- $db->prepare("UPDATE users SET status = 1 - status WHERE id = ?")->execute(array($target_uid));
- }
- header("Location: index.php?action=users"); exit;
- }
- if ($act == 'adm_edit_user') {
- $db->prepare("UPDATE users SET pass=? WHERE id=?")->execute(array(md5($_POST['p']), $_POST['uid']));
- header("Location: index.php?action=users&msg=".urlencode("重置成功")); exit;
- }
- if ($act == 'del_user') {
- $target_uid = (int)$_POST['uid'];
- if ($target_uid > 1 && $target_uid != $_SESSION['uid']) {
- $st = $db->prepare("SELECT path FROM imgs WHERE uid=?");
- $st->execute(array($target_uid));
- while($im = $st->fetch()) { @unlink($base.'/'.$im['path']); }
- $db->prepare("DELETE FROM imgs WHERE uid=?")->execute(array($target_uid));
- $db->prepare("DELETE FROM albums WHERE uid=?")->execute(array($target_uid));
- $db->prepare("DELETE FROM users WHERE id=?")->execute(array($target_uid));
- header("Location: index.php?action=users&msg=".urlencode("已彻底删除")); exit;
- }
- }
- }
- if ($act == 'add_album') {
- $db->prepare("INSERT INTO albums (uid, name, time) VALUES (?, ?, datetime('now'))")->execute(array($_SESSION['uid'], $_POST['aname']));
- header("Location: index.php"); exit;
- }
- if (isset($_FILES['imgs']) && $aid !== 'none') {
- $up = $base . '/uploads'; if (!is_dir($up)) @mkdir($up, 0777);
- foreach ($_FILES['imgs']['name'] as $i => $n) {
- if ($_FILES['imgs']['error'][$i] === 0) {
- $sn = 'img_'.time().'_'.rand(100,999).'.'.strtolower(pathinfo($n, PATHINFO_EXTENSION));
- if (move_uploaded_file($_FILES['imgs']['tmp_name'][$i], $up.'/'.$sn))
- $db->prepare("INSERT INTO imgs (uid, aid, path, name, time) VALUES (?, ?, ?, ?, datetime('now'))")->execute(array($_SESSION['uid'], ($aid=='uncategorized'?0:(int)$aid), 'uploads/'.$sn, $n));
- }
- }
- header("Location: ".$_SERVER['HTTP_REFERER']); exit;
- }
- if ($act == 'batch_del' && isset($_POST['ids'])) {
- foreach ($_POST['ids'] as $id) { del_img_db($db, $id, $base, $is_adm); }
- header("Location: ".$_SERVER['HTTP_REFERER']); exit;
- }
- }
- }
- if ($act == 'del_one' && isset($_GET['id'])) {
- del_img_db($db, (int)$_GET['id'], $base, $is_adm);
- header("Location: ".$_SERVER['HTTP_REFERER']); exit;
- }
- function del_img_db($db, $id, $base, $is_adm) {
- $sql = $is_adm ? "SELECT path FROM imgs WHERE id=?" : "SELECT path FROM imgs WHERE id=? AND uid=".$_SESSION['uid'];
- $st = $db->prepare($sql); $st->execute(array($id));
- if ($im = $st->fetch()) { @unlink($base.'/'.$im['path']); $db->prepare("DELETE FROM imgs WHERE id=?")->execute(array($id)); }
- }
- if ($act == 'out') { session_destroy(); setcookie('app_auth', '', 0, "/"); header("Location: index.php"); exit; }
- ?>
- <!DOCTYPE html><html><head><meta charset="UTF-8"><title>CloudGallery Pro</title>
- <style>
- :root { --p: #1890ff; --d: #ff4d4f; --s: #52c41a; }
- body{font-family:sans-serif;background:#f0f2f5;margin:0;color:#333}
- .bg-full{position:fixed;top:0;left:0;width:100%;height:100%;background:linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)), url('https://picsum.photos/1920/1080?nature');background-size:cover;z-index:-1}
- .nav{background:#fff;height:60px;display:flex;align-items:center;justify-content:space-between;padding:0 30px;box-shadow:0 1px 3px rgba(0,0,0,0.1)}
- .container{max-width:1100px;margin:30px auto;padding:0 20px}
- .btn{padding:7px 14px;border-radius:6px;cursor:pointer;border:none;font-size:12px;font-weight:bold;text-decoration:none;display:inline-block}
- .b-blue{background:var(--p);color:#fff} .b-red{background:var(--d);color:#fff} .b-white{background:#fff;border:1px solid #ddd;color:#666} .b-green{background:var(--s);color:#fff}
- .card{background:#fff;border-radius:12px;padding:25px;box-shadow:0 2px 8px rgba(0,0,0,0.05);position:relative}
- .img-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:15px}
- .img-item{background:#fff;border-radius:10px;overflow:hidden;border:1px solid #eee;position:relative}
- .del-one{position:absolute;top:5px;right:5px;background:rgba(255,0,0,0.7);color:#fff;width:20px;height:20px;line-height:20px;text-align:center;border-radius:50%;display:none;text-decoration:none}
- .img-item:hover .del-one{display:block}
- .msg-tips{position:fixed;top:20px;left:50%;transform:translateX(-50%);background:rgba(0,0,0,0.8);color:#fff;padding:10px 25px;border-radius:30px;font-size:13px;display:none;z-index:9999}
- input{padding:10px;margin-bottom:10px;border:1px solid #ddd;border-radius:6px;box-sizing:border-box}
- .edit-tag{position:absolute;top:10px;right:10px;font-size:11px;cursor:pointer;color:#bbb;background:#f8f9fa;padding:2px 5px;border-radius:4px}
- </style></head><body>
- <div id="tips" class="msg-tips"></div>
- <?php if(!isset($_SESSION['uid'])) { ?>
- <div class="bg-full"></div>
- <div style="width:320px;margin:120px auto;background:#fff;padding:40px;border-radius:20px;box-shadow:0 20px 40px rgba(0,0,0,0.2)">
- <h2 style="text-align:center"><?php echo ($act=='to_reg'?'注册账号':'登录'); ?></h2>
- <?php if($msg){ echo "<p style='color:red;font-size:12px;text-align:center'>$msg</p>"; } ?>
- <form method="post" action="?action=<?php echo ($act=='to_reg'?'reg':'login'); ?>">
- <input name="u" placeholder="用户名" style="width:100%" required>
- <input name="p" type="password" placeholder="密码" style="width:100%" required>
- <button class="btn b-blue" style="width:100%;padding:12px">确认</button>
- <?php if($reg_open=='1'||$act=='to_reg'){ ?><div style="text-align:center;margin-top:20px"><a href="?action=<?php echo ($act=='to_reg'?'':'to_reg'); ?>" style="color:#999;font-size:12px;text-decoration:none"><?php echo ($act=='to_reg'?'返回登录':'注册账号'); ?></a></div><?php } ?>
- </form>
- </div>
- <?php } else { ?>
- <div class="nav">
- <b style="color:var(--p);font-size:18px">CloudGallery Pro</b>
- <div>
- <a href="?action=profile" style="text-decoration:none;color:var(--p);font-weight:bold">👤 <?php echo $_SESSION['user']; ?></a>
- <a href="index.php" style="margin-left:15px;text-decoration:none;color:#666">首页</a>
- <?php if($is_adm){ ?><a href="?action=users" style="margin-left:15px;color:orange;text-decoration:none">管理</a><?php } ?>
- <a href="?action=out" style="margin-left:15px;color:#999;text-decoration:none">退出</a>
- </div>
- </div>
- <div class="container">
- <?php if($msg){ echo "<div class='card' style='margin-bottom:15px;padding:10px;background:#e6f7ff;color:var(--p);text-align:center'>$msg</div>"; } ?>
- <?php if($act == 'profile') { ?>
- <div class="card" style="max-width:400px;margin:0 auto">
- <h3>修改个人密码</h3>
- <form method="post" action="?action=my_pass"><input type="password" name="newp" placeholder="新密码" style="width:100%" required><button class="btn b-blue" style="width:100%">确认保存</button></form>
- </div>
- <?php } else if($act == 'users' && $is_adm) { ?>
- <div class="card">
- <div style="display:flex;justify-content:space-between;margin-bottom:20px"><h3>会员管理</h3><form method="post" action="?action=toggle_reg"><button class="btn <?php echo ($reg_open=='1'?'b-blue':'b-white'); ?>">注册状态: <?php echo ($reg_open=='1'?'ON':'OFF'); ?></button></form></div>
- <table width="100%" cellpadding="10" cellspacing="0">
- <tr style="text-align:left;background:#f8f9fa"><th>用户名</th><th>状态</th><th>操作</th></tr>
- <?php $us=$db->query("SELECT * FROM users"); while($u=$us->fetch()){ ?>
- <tr style="border-bottom:1px solid #eee">
- <td><b><?php echo $u['user']; ?></b> (<?php echo $u['role']; ?>)</td>
- <td><?php echo ($u['status']==1?'<span style="color:var(--s)">正常</span>':'<span style="color:var(--d)">已禁用</span>'); ?></td>
- <td>
- <a href="index.php?view_uid=<?php echo $u['id']; ?>" class="btn b-white">查看</a>
- <?php if($u['id'] > 1 && $u['id'] != $_SESSION['uid']){ ?>
- <form method="post" action="?action=toggle_user_stat" style="display:inline"><input type="hidden" name="uid" value="<?php echo $u['id']; ?>"><button class="btn <?php echo ($u['status']==1?'b-red':'b-green'); ?>"><?php echo ($u['status']==1?'禁用':'启用'); ?></button></form>
- <form method="post" action="?action=adm_edit_user" style="display:inline"><input type="hidden" name="uid" value="<?php echo $u['id']; ?>"><input type="text" name="p" placeholder="重置密码" style="width:70px;padding:5px;margin:0"><button class="btn b-white">重置</button></form>
- <form method="post" action="?action=del_user" style="display:inline" onsubmit="return confirm('彻底物理删除该用户及所有图片?')"><input type="hidden" name="uid" value="<?php echo $u['id']; ?>"><button class="btn b-red">删除</button></form>
- <?php } else { echo "<small style='color:#ccc;margin-left:10px'>锁定保护</small>"; } ?>
- </td>
- </tr><?php } ?>
- </table>
- </div>
- <?php } else if($aid !== 'none') { ?>
- <div style="margin-bottom:20px;display:flex;justify-content:space-between">
- <a href="index.php<?php echo ($view_uid?"?view_uid=$view_uid":""); ?>" style="text-decoration:none;color:var(--p);font-weight:bold">⬅ 返回</a>
- <?php if(!$view_uid){ ?><form method="post" enctype="multipart/form-data"><input name="imgs[]" type="file" multiple required><button class="btn b-blue">上传</button></form><?php } ?>
- </div>
- <div class="card" style="margin-bottom:20px;display:flex;align-items:center;flex-wrap:wrap;gap:8px">
- <label><input type="checkbox" onclick="var c=document.querySelectorAll('.ic');for(var i=0;i<c.length;i++)c[i].checked=this.checked"> 全选</label>
- <button class="btn b-red" onclick="document.getElementById('b-form').submit()">删除选中</button>
- <div style="margin-left:auto">
- <button class="btn b-white" onclick="cp('url')">URL</button>
- <button class="btn b-white" onclick="cp('html')">HTML</button>
- <button class="btn b-white" onclick="cp('md')">Markdown</button>
- <button class="btn b-white" onclick="cp('bb')">BBCode</button>
- </div>
- </div>
- <form id="b-form" method="post" action="?action=batch_del&aid=<?php echo $aid; ?>">
- <div class="img-grid">
- <?php
- $v_aid = ($aid=='uncategorized'?0:(int)$aid);
- $target_uid = ($is_adm && $view_uid ? $view_uid : $_SESSION['uid']);
- $st = $db->prepare("SELECT * FROM imgs WHERE aid=? AND uid=? ORDER BY id DESC");
- $st->execute(array($v_aid, $target_uid));
- while($im=$st->fetch()){
- $u_url='http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.$im['path']; ?>
- <div class="img-item">
- <a href="?action=del_one&id=<?php echo $im['id']; ?>" class="del-one" onclick="return confirm('彻底物理删除?')">×</a>
- <img src="<?php echo $im['path']; ?>" style="width:100%;height:150px;object-fit:cover;cursor:pointer" onclick="window.open(this.src)">
- <div style="padding:10px"><input type="checkbox" name="ids[]" class="ic" value="<?php echo $im['id']; ?>" data-url="<?php echo $u_url; ?>"></div>
- </div>
- <?php } ?>
- </div>
- </form>
- <?php } else { ?>
- <div style="margin-bottom:25px">
- <?php if($is_adm && $view_uid){ echo "<div class='card'>预览用户 $view_uid 的空间</div>"; } else { ?>
- <form method="post" action="?action=add_album" class="card"><input name="aname" placeholder="新相册名" style="margin:0;width:200px" required> <button class="btn b-blue">创建</button></form>
- <?php } ?>
- </div>
- <div class="img-grid">
- <div class="card" onclick="location.href='?aid=uncategorized<?php echo ($view_uid?"&view_uid=$view_uid":""); ?>'" style="text-align:center;cursor:pointer;border:1px dashed #ddd">⌛<br><b>未分类</b></div>
- <?php
- $target_uid = ($is_adm && $view_uid ? $view_uid : $_SESSION['uid']);
- $st = $db->prepare("SELECT * FROM albums WHERE uid=? ORDER BY id DESC");
- $st->execute(array($target_uid));
- while($al=$st->fetch()){ ?>
- <div class="card" style="text-align:center">
- <?php if(!$view_uid){ ?><span class="edit-tag" onclick="var n=prompt('修改名称','<?php echo $al['name']; ?>');if(n){var f=document.createElement('form');f.method='post';f.action='?action=edit_album';var i1=document.createElement('input');i1.name='new_name';i1.value=n;var i2=document.createElement('input');i2.name='album_id';i2.value='<?php echo $al['id']; ?>';f.appendChild(i1);f.appendChild(i2);document.body.appendChild(f);f.submit();}">编辑</span><?php } ?>
- <div onclick="location.href='?aid=<?php echo $al['id']; ?><?php echo ($view_uid?'&view_uid='.$view_uid:''); ?>'" style="cursor:pointer"><span style="font-size:30px">📂</span><br><b><?php echo $al['name']; ?></b></div>
- </div>
- <?php } ?>
- </div>
- <?php } ?>
- </div>
- <?php } ?>
- <script>
- function showTips(t){ var x=document.getElementById('tips'); x.innerText=t; x.style.display='block'; setTimeout(function(){x.style.display='none'},2000); }
- function cp(type){
- var s=[]; var c=document.querySelectorAll('.ic:checked');
- if(!c.length) return showTips('未勾选');
- for(var i=0;i<c.length;i++){
- var u=c[i].getAttribute('data-url');
- if(type=='md') s.push(''); else if(type=='html') s.push('<img src="'+u+'" />'); else if(type=='bb') s.push('[img]'+u+'[/img]'); else s.push(u);
- }
- var ta=document.createElement('textarea'); ta.value=s.join('\n'); document.body.appendChild(ta); ta.select(); document.execCommand('copy'); document.body.removeChild(ta);
- showTips('成功复制 '+c.length+' 条');
- }
- </script></body></html>
复制代码 |
评分
-
查看全部评分
|