$sql = "CREATE TABLE vtubers (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
+ login VARCHAR(255) NOT NULL,
+ img VARCHAR(512) NOT NULL,
link VARCHAR(512) NOT NULL
) ENGINE=INNODB;";
$db->pdo->exec($sql);
public function down()
{
$db = Application::$app->db;
- $sql = "DROP TABLE vtuber";
+ $sql = "DROP TABLE vtubers";
$db->pdo->exec($sql);
}
}
<?php
+
namespace app\controllers;
+use app\core\Application;
use app\core\BaseController;
use app\core\Request;
use app\models\Vtubers;
-class SiteController extends BaseController {
- public function home() {
+class SiteController extends BaseController
+{
+ public function home()
+ {
$params = [
"name" => "Leonardo"
];
return $this->render("home", $params);
}
- public function live(Request $req) {
+ public function live(Request $req)
+ {
$errors = [];
+ $params = [];
$vtuberModel = new Vtubers;
if ($req->getMethod() == "post") {
if ($vtuberModel->validate() && $vtuberModel->register()) {
return "Success";
}
+ } else if ($req->getMethod() == "get") {
+ $statement = Application::$app->db->pdo->prepare("SELECT * FROM vtubers;");
+ $statement->execute();
+
+ $params = $statement->fetchAll();
}
- return $this->render("live", [ "model" => $vtuberModel ]);
+
+ if (isset($_GET["id"]))
+ $this->setLayout("live");
+ return $this->render("live", ["model" => $vtuberModel, $params]);
}
}
public Request $req;
public Response $res;
public Database $db;
+ public array $config;
public static Application $app;
public static string $ROOT_DIR;
self::$ROOT_DIR = $root;
self::$app = $this;
+ $this->config = $config;
$this->req = new Request();
$this->res = new Response();
$this->router = new Router($this->req, $this->res);
}
}
- echo " <script>alert('validate fatto!')</script>";
return empty($this->errors);
}
$method = $this->req->getMethod();
$callback = $this->routes[$method][$path] ?? false;
- if (is_string($callback)) {
+ if (is_callable($callback)) {
+ return call_user_func($callback);
+ } else if (is_string($callback)) {
return $this->renderView($callback);
} else if (is_array($callback)) {
Application::$app->setController(new $callback[0]);
$callback[0] = Application::$app->getController();
+
+ return call_user_func($callback, $this->req);
} else {
$this->res->setStatusCode(404);
return $this->renderView("404");
}
-
- return call_user_func($callback, $this->req);
}
public function renderView(string $view, array $params = [])
return str_replace("{{content}}", $viewContent, $layoutContent);
}
- private function loadLayoutContent() {
+ private function loadLayoutContent()
+ {
$layout = Application::$app->getController()->layout;
ob_start();
- include_once Application::$ROOT_DIR."/views/layouts/$layout.php";
+ include_once Application::$ROOT_DIR . "/views/layouts/$layout.php";
return ob_get_clean();
}
- private function loadViewContent(string $view, array $params) {
+ private function loadViewContent(string $view, array $params)
+ {
# modo epico per creare variabili con lo stesso nome assegnato nell'array!!
foreach ($params as $key => $value) {
$$key = $value;
}
ob_start();
- include_once Application::$ROOT_DIR."/views/$view.php";
+ include_once Application::$ROOT_DIR . "/views/$view.php";
return ob_get_clean();
}
}
-?>
namespace app\models;
+use app\core\Application;
use app\core\DbModel;
class Vtubers extends DbModel
{
public string $username = "";
+ public string $login = "";
+ public string $img = "";
public string $link = "";
public function tableName(): string
return "vtubers";
}
- public function attributes(): array {
- return [ "username", "link" ];
+ public function attributes(): array
+ {
+ return ["username", "login", "img", "link"];
}
public function register()
{
return [
"username" => [self::RULE_REQUIRED],
- "link" => [self::RULE_REQUIRED, [self::RULE_UNIQUE, "class" => self::class ]],
+ "login" => [self::RULE_REQUIRED],
+ "img" => [self::RULE_REQUIRED],
+ "link" => [self::RULE_REQUIRED, [self::RULE_UNIQUE, "class" => self::class]],
];
}
- public function getVtuberName() {
+ public function getVtuberName()
+ {
+ $clientID = Application::$app->config["twitch"]["clientid"] ?? "";
+ $token = Application::$app->config["twitch"]["token"] ?? "";
+
+ if (str_contains($this->link, "twitch.tv")) {
+ $idol = str_replace("https://www.twitch.tv/", "", $this->link);
+ $url = "https://api.twitch.tv/helix/users?login=$idol";
+
+ $ch = curl_init($url);
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array("Client-ID: $clientID", "Authorization: Bearer $token"));
+
+ $result = get_object_vars(json_decode(curl_exec($ch)));
+ curl_close($ch);
+
+ $result = get_object_vars($result["data"][0]);
+
+ $this->username = ucfirst($result["display_name"]);
+ $this->login= $result["login"];
+ $this->img = rtrim($result["profile_image_url"], "/ ");
+ return;
+ }
$this->username = "i got you bro";
+ $this->login = "i got you bro";
+ $this->img = "i got you bro";
}
}
"dsn" => $_ENV["DB_DSN"],
"user" => $_ENV["DB_USER"],
"password" => $_ENV["DB_PASSWORD"]
+ ],
+ "twitch" => [
+ "clientid" => $_ENV["TWITCH_CLIENTID"],
+ "token" => $_ENV["TWITCH_TOKEN"]
]
];
$app->router->post("/register", [AuthController::class, "register"]);
$app->run();
-?>
+++ /dev/null
-<h1>Contact page</h1>
-
-<div class="container">
-<form action="" method="post">
- <div class="mb-3">
- <label for="exampleInputEmail1" class="form-label">Email address</label>
- <input name="email" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
- <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
- </div>
- <div class="mb-3">
- <label for="exampleInputPassword1" class="form-label">Password</label>
- <input name="pass" type="password" class="form-control" id="exampleInputPassword1">
- </div>
- <div class="mb-3 form-check">
- <input name="check" type="checkbox" class="form-check-input" id="exampleCheck1">
- <label class="form-check-label" for="exampleCheck1">Check me out</label>
- </div>
- <button type="submit" class="btn btn-primary">Submit</button>
-</form>
-</div>
--- /dev/null
+<html lang="en">
+
+<head>
+ <meta charset="UTF-8" />
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
+ <style>
+ img {
+ width: 5%;
+ }
+ </style>
+ <title>WeebSite - Live!</title>
+</head>
+
+<body>
+ {{content}}
+</body>
+
+</html>
-<h1>Live page</h1>
+<?php
+if (!isset($_GET["id"])) {
+ echo "<h1>Live page</h1>";
-<div class="container">
- <?php $form = app\core\forms\Form::begin("", "post"); ?>
- <?php echo $form->field($model, "Link"); ?>
- <button type="submit" class="btn btn-primary">Submit</button>
- <?php app\core\forms\Form::end(); ?>
+ $form = app\core\forms\Form::begin("", "post");
+ echo $form->field($model, "Link");
+ echo '<button type="submit" class="btn btn-primary">Submit</button>';
+ app\core\forms\Form::end();
+
+ echo "<ul>";
+ foreach ($params[0] as $idol) {
+ echo "<li><a href='/live?id=" . $idol["id"] . "'>" . ucfirst($idol["username"]) . "</a></li>";
+ }
+ echo "</ul>";
+} else {
+ foreach ($params[0] as $vtuber) {
+ if ($_GET["id"] == $vtuber["id"]) {
+ echo "
+<div id=\"parent\">
+ <div>
+ <nav class=\"navbar bg-dark text-white\">
+ <div class=\"container-fluid\">
+ <h1>" . ucfirst($vtuber["username"]) . "</h1><img src='".$vtuber["img"]."'/>
+ </div>
+ </nav>
+ </div>
+</div>
+
+<div id=\"child\">
+<iframe src=\"https://player.twitch.tv/?channel=" . $vtuber["login"] . "&parent=localhost\" frameborder=\"0\" allowfullscreen=\"true\" scrolling=\"no\"></iframe>
</div>
+";
+ }
+ }
+}