www
/
wwwroot
/
codedabster.com
/
workspace
/
admin
➕ New
📤 Upload
✎ Editing:
profile_actions.php
← Back
<?php declare(strict_types=1); header('Content-Type: application/json; charset=utf-8'); include('../includes/config.php'); include('../includes/config.inc.php'); if (empty($_SESSION['admin']) || empty($_SESSION['admin']['id'])) { http_response_code(401); echo json_encode(['ok'=>false,'error'=>'not_authenticated']); exit; } $userId = (int)$_SESSION['admin']['id']; $action = $_POST['action'] ?? ''; function firstRow($arr){ return (is_array($arr) && isset($arr[0]) && is_array($arr[0])) ? $arr[0] : null; } function fetchAll($res){ return is_array($res) ? $res : []; } function json_ok($data=[]) { echo json_encode(['ok'=>true]+$data); exit; } function json_err($msg, $code=400){ http_response_code($code); echo json_encode(['ok'=>false,'error'=>$msg]); exit; } switch ($action) { case 'create_post': { $content = trim((string)($_POST['content'] ?? '')); if ($content === '') json_err('empty_content'); $database->execute( "INSERT INTO user_posts (up_user_id, up_content) VALUES (?, ?)", [$userId, $content] ); $row = firstRow($database->execute( "SELECT * FROM user_posts WHERE up_user_id=? ORDER BY up_id DESC LIMIT 1", [$userId] )) ?? ['up_id'=>0,'up_user_id'=>$userId,'up_content'=>$content,'up_created_at'=>date('Y-m-d H:i:s')]; json_ok(['post' => $row]); } break; case 'follow': { $target = (int)($_POST['target_user_id'] ?? 0); if (!$target || $target === $userId) json_err('invalid_target'); $exists = $database->execute( "SELECT 1 AS x FROM user_follow WHERE uf_follower_id=? AND uf_followed_id=?", [$userId, $target] ); if (!is_array($exists) || !isset($exists[0])) { $database->execute( "INSERT INTO user_follow (uf_follower_id, uf_followed_id) VALUES (?,?)", [$userId, $target] ); } $row = firstRow($database->execute("SELECT COUNT(*) c FROM user_follow WHERE uf_followed_id=?", [$target])); $target_followers = (int)($row['c'] ?? 0); $row = firstRow($database->execute("SELECT COUNT(*) c FROM user_follow WHERE uf_follower_id=?", [$userId])); $following = (int)($row['c'] ?? 0); json_ok(['following'=>$following,'target_followers'=>$target_followers]); } break; case 'unfollow': { $target = (int)($_POST['target_user_id'] ?? 0); if (!$target || $target === $userId) json_err('invalid_target'); $database->execute( "DELETE FROM user_follow WHERE uf_follower_id=? AND uf_followed_id=?", [$userId, $target] ); $row = firstRow($database->execute("SELECT COUNT(*) c FROM user_follow WHERE uf_followed_id=?", [$target])); $target_followers = (int)($row['c'] ?? 0); $row = firstRow($database->execute("SELECT COUNT(*) c FROM user_follow WHERE uf_follower_id=?", [$userId])); $following = (int)($row['c'] ?? 0); json_ok(['following'=>$following,'target_followers'=>$target_followers]); } break; case 'like': { $postId = (int)($_POST['post_id'] ?? 0); if (!$postId) json_err('invalid_post'); $exists = $database->execute( "SELECT 1 AS x FROM user_post_like WHERE upl_user_id=? AND upl_post_id=?", [$userId, $postId] ); if (is_array($exists) && isset($exists[0])) { $database->execute( "DELETE FROM user_post_like WHERE upl_user_id=? AND upl_post_id=?", [$userId, $postId] ); $liked = false; } else { $database->execute( "INSERT INTO user_post_like (upl_user_id, upl_post_id) VALUES (?,?)", [$userId, $postId] ); $liked = true; } $row = firstRow($database->execute("SELECT COUNT(*) c FROM user_post_like WHERE upl_post_id=?", [$postId])); $likes = (int)($row['c'] ?? 0); json_ok(['liked'=>$liked,'likes'=>$likes]); } break; case 'comment': { $postId = (int)($_POST['post_id'] ?? 0); $content = trim((string)($_POST['content'] ?? '')); if (!$postId || $content === '') json_err('invalid_input'); $database->execute( "INSERT INTO user_post_comment (upc_post_id, upc_user_id, upc_content) VALUES (?,?,?)", [$postId, $userId, $content] ); $row = firstRow($database->execute("SELECT COUNT(*) c FROM user_post_comment WHERE upc_post_id=?", [$postId])); $comments = (int)($row['c'] ?? 0); json_ok(['comments'=>$comments]); } break; case 'load_more_posts': { $page = max(1, (int)($_POST['page'] ?? 1)); $pp = max(1, min(20, (int)($_POST['pp'] ?? 5))); $offset = ($page - 1) * $pp; $rowsRaw = fetchAll($database->execute( "SELECT p.* FROM user_posts p WHERE p.up_user_id = ? ORDER BY p.up_created_at DESC LIMIT ? OFFSET ?", [$userId, $pp, $offset] )); // sanitize rows $rows = []; foreach ($rowsRaw as $r) { if (is_array($r) && isset($r['up_id'])) $rows[] = $r; } // atașează media doar dacă avem IDs if (!empty($rows)) { $postIds = array_values(array_filter(array_map( fn($p)=> isset($p['up_id']) ? (int)$p['up_id'] : null, $rows ), fn($v)=> !is_null($v) && $v>0)); if (count($postIds) > 0) { $in = implode(',', array_fill(0, count($postIds), '?')); $mediaRows = fetchAll($database->execute( "SELECT * FROM user_media WHERE um_post_id IN ($in) ORDER BY um_created_at ASC", $postIds )); $mediaByPost = []; foreach ($mediaRows as $m) { if (!is_array($m) || !isset($m['um_post_id'])) continue; $mediaByPost[(int)$m['um_post_id']][] = $m; } foreach ($rows as &$r) { $pid = (int)$r['up_id']; $r['_media'] = $mediaByPost[$pid] ?? []; } unset($r); } else { foreach ($rows as &$r) { $r['_media'] = []; } unset($r); } } echo json_encode(['ok'=>true,'posts'=>$rows]); exit; } break; default: json_err('unknown_action', 404); }
💾 Save Changes
Cancel
📤 Upload File
×
Select File
Upload
Cancel
➕ Create New
×
Type
📄 File
📁 Folder
Name
Create
Cancel
✎ Rename Item
×
Current Name
New Name
Rename
Cancel
🔐 Change Permissions
×
Target File
Permission (e.g., 0755, 0644)
0755
0644
0777
Apply
Cancel