Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // This file is for Ponychan's JSON API. Its syntax will be somewhat similar to 4chan's
- // at https://github.com/4chan/4chan-API but rather than static JSON files, this will be dynamic
- // written by Ly
- require_once 'inc/functions.php';
- require_once 'inc/anti-bot.php';
- require_once 'inc/database.php';
- class apireq
- {
- static $req_types = array(
- "boards" => "listBoards",
- "threads" => "listThreads",
- "thread" => "listPosts",
- "catalog" => "listCatalog"
- );
- private $req_type = "";
- private $board = "";
- private $page = 0;
- private $thread = 0;
- public function __construct()
- {
- if (isset($_GET["req"]) && strlen($_GET["req"]) > 0 && array_key_exists($_GET["req"], self::$req_types))
- $this->req_type = $_GET["req"];
- else
- $this->err("Unknown request type. Allowed request types: ".implode(" ,", array_keys(self::$req_types)));
- if (isset($_GET["board"]) && strlen($_GET["board"]) > 0)
- $this->board = addslashes($_GET["board"]);
- if (isset($_GET["thread"]) && strlen($_GET["thread"]) > 0 && is_numeric($_GET["thread"]))
- $this->thread = (int)$_GET["thread"];
- if (isset($_GET["page"]) && strlen($_GET["page"]) > 0 && is_numeric($_GET["page"]))
- $this->page = ((int)$_GET["page"] > 0 ? (int)$_GET["page"] - 1 : 0);
- $this->{self::$req_types[$this->req_type]}();
- }
- private function listCatalog()
- {
- $errors = array();
- if (strlen($this->board) === 0)
- $errors[] = "Board not specified.";
- if (count($errors))
- $this->err($errors);
- global $config;
- $board = "posts_".$this->board;
- $limit = $config["threads_per_page"] * $config["max_pages"];
- $data = query("SELECT main.*, (SELECT COUNT(*) FROM `$board` rc WHERE rc.`thread` = main.`id`) as replyCount, (SELECT COUNT(*) FROM `$board` ic WHERE ic.`thread` = main.`id` AND `file` != 'deleted' AND `file` IS NOT NULL) as imageCount FROM `$board` main ".
- "WHERE `thread` IS NULL ".
- "ORDER BY `sticky` DESC, `bump` DESC ".
- "LIMIT $limit");
- if (!$data)
- $this->err("Invalid board or database error.");
- $results = array();
- $dlFor = array();
- while ($result = $data->fetch(PDO::FETCH_OBJ))
- {
- $results[] = $result;
- $dlFor[] = $result->id;
- }
- $data = query($this->generateUnionSQL($dlFor, $board, 1));
- if (!$data)
- $this->err("Invalid board or database error.");
- $endResults = array();
- $repliesTo = array();
- while ($result = $data->fetch(PDO::FETCH_OBJ))
- $repliesTo[(int)$result->thread][] = $result;
- $pageNum = 0;
- foreach ($results as $thread)
- {
- $thrPosts = array();
- $imagesInPosts = 0;
- if (isset($repliesTo[(int)$thread->id]))
- {
- foreach ($repliesTo[(int)$thread->id] as $reply)
- {
- $thrPosts[] = $this->getPost($reply);
- if (isset($thrPosts[count($thrPosts)-1]->filename))
- $imagesInPosts++;
- }
- }
- if (!isset($endResults[$pageNum]))
- $endResults[$pageNum] = array(
- "page" => $pageNum+1,
- "threads" => array()
- );
- $OP = $this->getOP($thread);
- $endResults[$pageNum]["threads"][] = array(
- "posts" => array_merge(
- $OP,
- array(
- "last_replies" => $thrPosts,
- "omitted_posts" => $OP["replies"] - (isset($repliesTo[(int)$thread->id]) ? count($repliesTo[(int)$thread->id]) : 0),
- "ommited_images"=> $OP["images"] - $imagesInPosts
- )
- )
- );
- if (count($endResults[$pageNum]["threads"]) >= $config["threads_per_page"])
- $pageNum++;
- }
- $this->success($endResults);
- }
- private function listBoards()
- {
- $data = query("SELECT * FROM `boards`");
- if (!$data)
- $this->err("Couldn't fetch boards.");
- $boards = array();
- global $config;
- while ($board = $data->fetch(PDO::FETCH_OBJ))
- {
- $boards[] = array(
- "board" => $board->uri,
- "title" => $board->title,
- "per_page" => $config["threads_per_page"],
- "pages" => $config["max_pages"],
- "max_filesize" => $config["max_filesize"],
- "max_webm_filesize" => $config["max_video_filesize"],
- "max_comment_chars" => $config["max_body"],
- "max_webm_duration" => $config["max_video_length"],
- "bump_limit" => $config["reply_limit"],
- "image_limit" => $config["image_hard_limit"],
- "cooldowns" => array(
- "replies" => $config["flood_time"],
- "images" => $config["flood_time"],
- "threads" => $config["flood_time_op"][0][1]
- ),
- "meta_description" => $board->subtitle,
- );
- }
- $this->success(array("boards" => $boards));
- }
- private function listThreads()
- {
- $errors = array();
- if (strlen($this->board) === 0)
- $errors[] = "Board not specified.";
- if (count($errors))
- $this->err($errors);
- global $config;
- $board = "posts_".$this->board;
- $offset = $config["threads_per_page"] * $this->page;
- $data = query("SELECT main.*, (SELECT COUNT(*) FROM `$board` rc WHERE rc.`thread` = main.`id`) as replyCount, (SELECT COUNT(*) FROM `$board` ic WHERE ic.`thread` = main.`id` AND `file` != 'deleted' AND `file` IS NOT NULL) as imageCount FROM `$board` main ".
- "WHERE `thread` IS NULL ".
- "ORDER BY `sticky` DESC, `bump` DESC ".
- "LIMIT ".$config["threads_per_page"]." OFFSET $offset");
- if (!$data)
- $this->err("Invalid board or database error.");
- $results = array();
- $dlFor = array();
- while ($result = $data->fetch(PDO::FETCH_OBJ))
- {
- $results[] = $result;
- $dlFor[] = $result->id;
- }
- $data = query($this->generateUnionSQL($dlFor, $board, 3));
- if (!$data)
- $this->err("Invalid board or database error.");
- $endResults = array();
- $repliesTo = array();
- while ($result = $data->fetch(PDO::FETCH_OBJ))
- $repliesTo[(int)$result->thread][] = $result;
- foreach ($results as $thread)
- {
- $thrPosts = array();
- $imagesInPosts = 0;
- if (isset($repliesTo[(int)$thread->id]))
- {
- foreach ($repliesTo[(int)$thread->id] as $reply)
- {
- $thrPosts[] = $this->getPost($reply);
- if (isset($thrPosts[count($thrPosts)-1]->filename))
- $imagesInPosts++;
- }
- }
- $OP = $this->getOP($thread);
- $endResults[] = array(
- "posts" => array_merge(array(
- array_merge(
- $OP,
- array(
- "omitted_posts" => $OP["replies"] - (isset($repliesTo[(int)$thread->id]) ? count($repliesTo[(int)$thread->id]) : 0),
- "ommited_images"=> $OP["images"] - $imagesInPosts
- )
- )
- ), $thrPosts)
- );
- }
- $this->success(array("threads" => $endResults));
- }
- private function listPosts()
- {
- $errors = array();
- if (strlen($this->board) === 0)
- $errors[] = "Board not specified.";
- if ($this->thread === 0)
- $errors[] = "Thread not specified.";
- if (count($errors))
- $this->err($errors);
- global $config;
- $board = "posts_".$this->board;
- $data = query("SELECT main.*, (SELECT COUNT(*) FROM `$board` rc WHERE rc.`thread` = main.`id`) as replyCount, (SELECT COUNT(*) FROM `$board` ic WHERE ic.`thread` = main.`id` AND `file` != 'deleted' AND `file` IS NOT NULL) as imageCount FROM `$board` main ".
- "WHERE `id` = '{$this->thread}' OR `thread` = '{$this->thread}'".
- "ORDER BY `ID` ASC ");
- if (!$data)
- $this->err("Invalid thread, board or database error.");
- $results = array();
- $op = true;
- while ($result = $data->fetch(PDO::FETCH_OBJ))
- {
- if ($op)
- {
- $op = false;
- $results[] = $this->getOP($result);
- }
- else
- $results[] = $this->getPost($result);
- }
- $this->success(array("posts" => $results));
- }
- private function generateUnionSQL($parentIDs, $board, $limit)
- {
- if (count($parentIDs) === 0)
- return "";
- if (count($parentIDs) === 1)
- return "SELECT * FROM `$board` WHERE `thread` = '".$parentIDs[0]."' ORDER BY `id` ASC LIMIT $limit";
- $union = "";
- foreach ($parentIDs as $key => $ID)
- {
- $union .= "(SELECT * FROM `$board` WHERE `thread` = '".$ID."' ORDER BY `id` ASC LIMIT $limit)";
- if (count($parentIDs)-1 !== $key)
- $union .= " UNION ALL ";
- }
- return $union;
- }
- private function getOP($post)
- {
- return array_merge(array(
- "sticky" => (int)$post->sticky,
- "closed" => (int)$post->locked,
- "mature" => (int)$post->mature,
- "anon" => (int)$post->anon_thread,
- "replies" => (int)$post->replyCount,
- "images" => (int)$post->imageCount
- ), $this->getPost($post));
- }
- private function getPost($post)
- {
- return array_merge(array(
- "no" => (int)$post->id,
- "now" => date("m/d/y(D)H:i:s"),
- "name" => $post->name,
- "email" => $post->email,
- "com" => (in_array($this->req_type, array("catalog", "threads")) ? (strlen($post->body_nomarkup) > 100 ? substr($post->body_nomarkup, 0, 100)."..." : $post->body_nomarkup) : $post->body_nomarkup),
- "time" => (int)$post->time,
- "resto" => (int)$post->thread,
- "capcode" => $post->capcode
- ), $this->getFileInfo($post));
- }
- private function getFileInfo($post)
- {
- if (!isset($post->filename) || !strlen($post->filename))
- return array();
- return array(
- "filename" => $post->filename,
- "file" => $post->file,
- "thumb" => $post->thumb,
- "ext" => substr($post->file, strrpos($post->file, ".")),
- "w" => (int)$post->filewidth,
- "h" => (int)$post->fileheight,
- "tn_w" => (int)$post->thumbwidth,
- "tn_h" => (int)$post->thumbheight,
- "md5" => $post->filehash,
- "fsize" => (int)$post->filesize,
- );
- }
- private function success($data)
- {
- header("Content-Type: application/json");
- $encoded = json_encode(
- array_merge(array(
- "status" => "success"),
- $data)
- );
- header("Content-Lengtj: ".strlen($encoded));
- die(encoded);
- }
- private function err($error)
- {
- header("Content-Type: application/json");
- die(json_encode(array(
- "status" => "error",
- "messages" => (is_array($error) ? $error : array($error))
- )));
- }
- }
- $request = new apireq();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement