Subtitles::upload PHP Method

upload() public method

public upload ( $file, $post )
    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");
    }

Usage Example

コード例 #1
0
ファイル: api-v1.php プロジェクト: lordgabber/rartracker
     httpResponse($bookmarks->create($postdata));
     break;
 case validateRoute('DELETE', 'bookmarks/\\d+'):
     $bookmarks = new Bookmarks($db, $user);
     httpResponse($bookmarks->delete((int) $params[1]));
     break;
 case validateRoute('GET', 'subtitles'):
     $subtitles = new Subtitles($db, $user);
     httpResponse($subtitles->fetch($_GET["torrentid"]));
     break;
 case validateRoute('POST', 'subtitles'):
     $torrent = new Torrent($db, $user);
     $log = new Logs($db);
     $mailbox = new Mailbox($db, $user);
     $subtitles = new Subtitles($db, $user, $log, $torrent, $mailbox);
     httpResponse($subtitles->upload($_FILES["file"], $_POST));
     break;
 case validateRoute('DELETE', 'subtitles/\\d+'):
     $log = new Logs($db);
     $torrent = new Torrent($db, $user);
     $mailbox = new Mailbox($db, $user);
     $subtitles = new Subtitles($db, $user, $log, $torrent, $mailbox);
     httpResponse($subtitles->delete((int) $params[1], $_GET["reason"]));
     break;
 case validateRoute('GET', 'donations'):
     $donations = new Donations($db, $user);
     list($result, $totalCount) = $donations->query(array("limit" => $_GET["limit"], "index" => $_GET["index"]));
     httpResponse($result, $totalCount);
     break;
 case validateRoute('POST', 'donations'):
     $donate = new Donations($db, $user);