public function upload($file, $post)
{
if (!preg_match("/\\.(srt|zip|rar)\$/", $file["name"], $match)) {
throw new Exception(L::get("SUBTITLE_FILE_EXTENSION_REQUIREMENT"), 412);
}
if (!is_uploaded_file($file["tmp_name"])) {
throw new Exception(L::get("SUBTITLE_FILE_UPLOAD_ERROR"));
}
if (!filesize($file["tmp_name"])) {
throw new Exception(L::get("SUBTITLE_FILE_EMPTY_ERROR"));
}
$sth = $this->db->prepare("SELECT COUNT(*) FROM subs WHERE filnamn = ?");
$sth->bindParam(1, $file["name"], PDO::PARAM_STR);
$sth->execute();
$res = $sth->fetch();
if ($res[0] > 0) {
throw new Exception(L::get("SUBTITLE_CONFLICT_ERROR"), 409);
}
$sth = $this->db->prepare("INSERT INTO subs(torrentid, filnamn, datum, quality, userid) VALUES(?, ?, NOW(), ?, ?)");
$sth->bindParam(1, $post["torrentid"], PDO::PARAM_INT);
$sth->bindParam(2, $file["name"], PDO::PARAM_STR);
$sth->bindValue(3, $post["quality"] ?: '', PDO::PARAM_STR);
$sth->bindValue(4, $this->user->getId(), PDO::PARAM_INT);
$sth->execute();
move_uploaded_file($file["tmp_name"], $this->subsDir . $file["name"]);
$torrent = $this->torrent->get($post["torrentid"]);
$this->db->query("UPDATE torrents SET swesub = 1 WHERE id = " . $torrent["id"]);
$this->log->log(1, L::get("SUBTITLE_UPLOAD_SITE_LOG", [$torrent["id"], $torrent["name"], $torrent["name"]], Config::DEFAULT_LANGUAGE), $this->user->getId(), true);
// Inform users watching for subtitles
$sth = $this->db->prepare("SELECT * FROM bevakasubs WHERE torrentid = ? AND userid != ?");
$sth->bindParam(1, $torrent["id"], PDO::PARAM_INT);
$sth->bindValue(2, $this->user->getId(), PDO::PARAM_INT);
$sth->execute();
$subject = L::get("SUBTITLE_UPLOAD_PM_SUBJECT", [$torrent["name"]]);
$message = L::get("SUBTITLE_UPLOAD_PM_BODY", [$file["name"], $torrent["id"], $torrent["name"], $torrent["name"]]);
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$this->mailbox->sendSystemMessage($row["userid"], $subject, $message);
}
return array("status" => "ok");
}