Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once("database.php");
- class User{
- private $id;
- private $username;
- private $password;
- private $salt;
- private $clanid;
- private $clanxp;
- private $clanname;
- private $rank;
- private $points;
- private $cape;
- private $rsn;
- private $email;
- private $about;
- private $reputation;
- public function __destruct(){
- $database = new Database();
- $this->update($database);
- }
- public function getFromDatabaseByUsername($username, $db){
- $username = _username($username);
- $query = "SELECT * FROM users WHERE username = :username";
- $params = array(":username"=>$username);
- try{
- $result = $db->query($query, $params);
- $this->id = _id($result["id"]);
- $this->username = _username($result["username"]);
- $this->clanid = _id($result["clanid"]);
- $this->clanxp = _uint($result["clanxp"]);
- $this->rank = _rank($result["rank"]);
- $this->points = _points($result["points"]);
- $this->cape = _cape($result["cape"]);
- $this->rsn = _username($result["rsn"]);
- $this->email = _email($result["email"]);
- $this->about = _about($result["about"]);
- $this->reputation = _points($result["reputation"]);
- $this->clanname = _clan_id($this->clanid, $db)->get_name();
- }catch(Exception $e){
- throw $e;
- }
- return $this;
- }
- public function getFromDatabaseByID($id, $db){
- $id = _id($id);
- $query = "SELECT * FROM users WHERE id = :id";
- $params = array(":id"=>$id);
- try{
- $result = $db->query($query, $params);
- $this->id = _id($result["id"]);
- $this->username = _username($result["username"]);
- $this->clanid = _id($result["clanid"]);
- $this->clanxp = _uint($result["clanxp"]);
- $this->rank = _rank($result["rank"]);
- $this->points = _points($result["points"]);
- $this->cape = _cape($result["cape"]);
- $this->rsn = _username($result["rsn"]);
- $this->email = _email($result["email"]);
- $this->about = _about($result["about"]);
- $this->reputation = _points($result["reputation"]);
- $this->clanname = _clan_id($this->clanid, $db)->get_name();
- }catch(Exception $e){
- throw $e;
- }
- return $this;
- }
- public function register($username, $password, $email, $db){
- try{
- $username = _username($username);
- if($this->user_exists($username, $db)){
- throw new Exception("User already exists");
- }
- $salt = _salt();
- $password = _password($password, $salt);
- $email = _email($email);
- $cape = _cape("none");
- $clanid = _id(0);
- $rank = _rank("free agent");
- $query = "INSERT INTO users (username, salt, password, email, cape, clanid, rank) VALUES (:username, :salt, :password, :email, :cape, :clanid, :rank)";
- $params = array(":username"=>$username, ":salt"=>$salt, ":password"=>$password, ":email"=>$email, ":cape"=>$cape, ":clanid"=>$clanid, ":rank"=>$rank);
- $db->queryNone($query, $params);
- $this->getFromDatabaseByUsername($username, $db);
- return $this;
- }catch(Exception $e){
- throw $e;
- }
- }
- public function log_in($username, $password, $db){
- try{
- if(!$this->user_exists(_username($username), $db)){
- throw new Exception("User does not exist");
- }
- if(_password($password) !== $this->get_password(_username($username), $db)){
- throw new Exception("Incorrect password");
- }
- $this->getFromDatabaseByUsername(_username($username), $db);
- $this->to_session();
- session_start();
- }catch(Exception $e){
- throw $e;
- }
- }
- public function log_out(){
- unset($_SESSION["user"]);
- }
- public function update($db){
- try{
- $query = "UPDATE users SET username = :username, email = :email, cape = :cape, clanid = :clanid, rank = :rank, points = :points, rsn = :rsn, about = :about, reputation = :reputation WHERE id = :id";
- $params = array(":username"=>$this->get_username(), ":email"=>$this->get_email(), ":cape"=>$this->get_cape(), ":clanid"=>$this->get_clanid(), ":rank"=>$this->get_rank(), ":points"=>$this->get_points(), ":rsn"=>$this->get_rsn(), ":about"=>$this->get_about(), ":reputation"=>$this->get_reputation(), ":id"=>$this->get_id());
- $db->queryNone($query, $params);
- return $this;
- }catch(Exception $e){
- throw $e;
- }
- }
- private function user_exists($username, $db){
- $query = "SELECT username FROM users WHERE username = :username";
- $params = array(":username"=>_username($username));
- $result = $db->query($query, $params);
- return isset($result["username"]);
- }
- private function get_password($username, $db){
- $query = "SELECT password FROM users WHERE username = :username";
- $params = array(":username"=>_username($username));
- $result = $db->query($query, $params);
- return $result["password"];
- }
- private function to_session(){
- $_SESSION["user"] = $this;
- }
- public function to_json(){
- $json = array();
- $json["user"]["id"] = $this->get_id();
- $json["user"]["username"] = $this->get_username();
- $json["user"]["clanid"] = $this->get_clanid();
- $json["user"]["clanxp"] = $this->get_clanxp();
- $json["user"]["clanname"] = $this->get_clanname();
- $json["user"]["rank"] = $this->get_rank();
- $json["user"]["points"] = $this->get_points();
- $json["user"]["cape"] = $this->get_cape();
- $json["user"]["rsn"] = $this->get_rsn();
- $json["user"]["email"] = $this->get_email();
- $json["user"]["about"] = $this->get_about();
- $json["user"]["reputation"] = $this->get_reputation();
- return json_encode($json["user"]);
- }
- public function get_id(){
- return $this->id;
- }
- public function get_username(){
- return $this->username;
- }
- public function get_clanid(){
- return $this->clanid;
- }
- public function get_clanxp(){
- return $this->clanxp;
- }
- public function get_clanname(){
- return $this->clanname;
- }
- public function get_rank(){
- return $this->rank;
- }
- public function get_points(){
- return $this->points;
- }
- public function get_cape(){
- return $this->cape;
- }
- public function get_rsn(){
- return $this->rsn;
- }
- public function get_email(){
- return $this->email;
- }
- public function get_about(){
- return $this->about;
- }
- public function get_reputation(){
- return $this->reputation;
- }
- public function set_id($value){
- $this->id = _id($value);
- }
- public function set_username($value){
- $this->username = _username($value);
- }
- public function set_clanid($value){
- $this->clanid = _id($value);
- }
- public function set_clanxp($value){
- $this->clanxp = _uint($value);
- }
- public function set_clanname($value){
- $this->clanname = _strict($value);
- }
- public function set_rank($value){
- $this->rank = _rank($value);
- }
- public function set_points($value){
- $this->points = _points($value);
- }
- public function set_cape($value){
- $this->cape = _cape($value);
- }
- public function set_rsn($value){
- $this->rsn = _username($value);
- }
- public function set_email($value){
- $this->email = _email($value);
- }
- public function set_about($value){
- $this->about = _about($value);
- }
- public function set_reputation($value){
- $this->reputation = _points($value);
- }
- public function rank_up(){
- $ranks = array('free agent','recruit','corporal','sergeant','lieutenant','captain','general','admin','organiser','coordinator','overseer','deputy owner','owner');
- $key = array_search($this->rank, $ranks);
- if($key < 12){
- $key++;
- $this->rank = $ranks[$key];
- }
- }
- public function rank_down(){
- $ranks = array('free agent','recruit','corporal','sergeant','lieutenant','captain','general','admin','organiser','coordinator','overseer','deputy owner','owner');
- $key = array_search($this->rank, $ranks);
- if($key > 0){
- $key--;
- $this->rank = $ranks[$key];
- }
- }
- public function remove_from_clan(){
- $this->set_clanid(0);
- $this->set_clanxp(0);
- $this->set_clanname("");
- $this->set_points(0);
- $this->set_rank("free agent");
- }
- public function add_to_clan($clan){
- $this->remove_from_clan();
- $this->set_clanid($clan->get_id());
- $this->set_clanname($clan->get_name());
- $this->rank_up();
- }
- public function award_points($count){
- $points = $this->get_points();
- $this->set_points($points + _int($count));
- }
- public function award_reputation($count){
- $reputation = $this->get_reputation();
- $this->set_reputation($reputation + _int($count));
- }
- public function upvote(){
- $this->award_reputation(1);
- }
- public function downvote(){
- $this->award_reputation(-1);
- }
- public function can_ask(){
- return ($this->get_reputation() >= 1);
- }
- public function can_edit(){
- return ($this->get_reputation() >= 10);
- }
- public function can_edit_without_approval(){
- return ($this->get_reputation() >= 50);
- }
- public function can_downvote(){
- return ($this->get_reputation() >= 1);
- }
- public function can_moderate(){
- return ($this->get_reputation() >= 25);
- }
- public function award_comp(){
- if($this->get_cape() == "none"){
- $this->set_cape("comp");
- }
- }
- public function award_trim(){
- if($this->get_cape() == "none" || $this->get_cape() == "comp"){
- $this->set_cape("trim");
- }
- }
- }
- class Clan{
- private $id;
- private $name;
- private $make_changes;
- private $about;
- public function __destruct(){
- $database = new Database();
- $this->update($database);
- }
- public function create($name, $db){
- try{
- $name = _strict($name);
- if($this->exists($name, $db)){
- throw new Exception("Clan already exists");
- }
- $query = "INSERT INTO clan_ids (name) VALUES (:name)";
- $params = array(":name"=>$name);
- $db->queryNone($query, $params);
- $query = "SELECT id FROM clan_ids WHERE name = :name";
- $params = array(":name"=>$name);
- $result = $db->query($query, $params);
- $id = _id($result["id"]);
- $makechanges = _rank("admin");
- $query = "INSERT INTO clans (id, makechanges) VALUES (:id, :makechanges)";
- $params = array(":id"=>$id, ":makechanges"=>$makechanges);
- $db->queryNone($query, $params);
- $this->getFromDatabaseByName($name, $db);
- return $this;
- }catch(Exception $e){
- throw $e;
- }
- }
- public function exists($name, $db){
- $query = "SELECT id FROM clan_ids WHERE name = :name";
- $params = array(":name"=>_strict($name));
- $result = $db->query($query, $params);
- return isset($result["id"]);
- }
- public function id_exists($id, $db){
- $query = "SELECT id FROM clan_ids WHERE id = :id";
- $params = array(":id"=>_id($id));
- $result = $db->query($query, $params);
- return isset($result["id"]);
- }
- public function getFromDatabaseByName($name, $db){
- $name = _strict($name);
- $query = "SELECT * FROM clan_ids WHERE name = :name";
- $params = array(":name"=>$name);
- try{
- $result = $db->query($query, $params);
- $this->id = _id($result["id"]);
- $this->name = _strict($result["name"]);
- }catch(Exception $e){
- throw $e;
- }
- $query = "SELECT * FROM clans WHERE id = :id";
- $params = array(":id"=>$this->id);
- try{
- $result = $db->query($query, $params);
- $this->make_changes = _rank($result["makechanges"]);
- $this->about = _about($result["about"]);
- }catch(Exception $e){
- throw $e;
- }
- return $this;
- }
- public function getFromDatabaseByID($id, $db){
- $id = _id($id);
- $query = "SELECT * FROM clan_ids WHERE id = :id";
- $params = array(":id"=>$id);
- try{
- $result = $db->query($query, $params);
- $this->id = _id($result["id"]);
- $this->name = _strict($result["name"]);
- }catch(Exception $e){
- throw $e;
- }
- $query = "SELECT * FROM clans WHERE id = :id";
- $params = array(":id"=>$this->id);
- try{
- $result = $db->query($query, $params);
- $this->make_changes = _rank($result["makechanges"]);
- $this->about = _about($result["about"]);
- }catch(Exception $e){
- throw $e;
- }
- return $this;
- }
- public function update($db){
- try{
- $query = "UPDATE clan_ids SET name = :name WHERE id = :id";
- $params = array(":name"=>$this->get_name(), ":id"=>$this->get_id());
- $db->queryNone($query, $params);
- $query = "UPDATE clans SET makechanges = :makechanges, about = :about WHERE id = :id";
- $params = array(":makechanges"=>$this->get_make_changes(), ":about"=>$this->get_about(), ":id"=>$this->get_id());
- $db->queryNone($query, $params);
- return $this;
- }catch(Exception $e){
- throw $e;
- }
- }
- public function get_name(){
- return $this->name;
- }
- public function get_id(){
- return $this->id;
- }
- public function get_about(){
- return $this->about;
- }
- public function get_make_changes(){
- return $this->make_changes;
- }
- public function can_make_changes($user){
- $ranks = array('free agent','recruit','corporal','sergeant','lieutenant','captain','general','admin','organiser','coordinator','overseer','deputy owner','owner');
- $key1 = array_search($user->rank, $ranks);
- $key2 = array_search($this->get_make_changes(), $ranks);
- return ($key1 > $key2);
- }
- public function set_about($value, $user){
- $value = _about($value);
- if($this->can_make_changes($user)){
- $this->about = $value;
- }
- }
- public function set_name($value, $user){
- $value = _strict($value);
- if($this->can_make_changes($user)){
- $this->name = $value;
- }
- }
- public function set_make_changes($value, $user){
- $value = _rank($value);
- if($this->can_make_changes($user)){
- $this->make_changes = $value;
- }
- }
- }
- class Question{
- private $id;
- private $answers = array();
- private $tags = array();
- private $user;
- private $title;
- private $content;
- private $timestamp;
- public function ask($title, $content, $user, $db){
- $user = _user_id($user, $db);
- if(!$user->can_ask()){
- throw new Exception("Your reputation is not high enough to ask a question.");
- }
- $title = _text($title, 140);
- $content = _html($content);
- $this->user = $user;
- $this->title = $title;
- $this->content = $content;
- }
- public function add_tag($tag){
- try{
- $tag = _tag($tag);
- }catch(Exception $e){
- throw $e;
- }
- $this->tags[] = $tag;
- }
- public function load($id, $db){
- $query = "SELECT * FROM questions WHERE id = :id";
- $params = array(":id"=>_id($id));
- $result = $db->query($query, $params);
- if(!isset($result["id"])){
- throw new Exception("Question does not exist");
- }
- $this->ask($result["title"], $result["content"], $result["contributor"], $db);
- $this->id = $result["id"];
- $this->timestamp = $result["date"];
- $this->load_tags($id, $db);
- $this->load_answers($id, $db);
- }
- private function load_tags($id, $db){
- $query = "SELECT * FROM tagged_items WHERE item_id = :id";
- $params = array(":id"=>_id($id));
- $results = $db->queryAll($query, $params);
- $tags = array('melee', 'defence', 'ranged', 'prayer', 'magic', 'runecrafting', 'construction', 'dungeoneering', 'constitution', 'agility', 'herblore', 'thieving', 'crafting', 'fletching', 'slayer', 'hunter', 'divination', 'mining', 'smithing', 'fishing', 'cooking', 'firemaking', 'woodcutting', 'farming', 'summoning', 'minigames', 'distractions and diversions', 'combat', 'runescape 3', 'old school runescape', 'runescape classic', 'monster hunting', 'moneymaking', 'technical', 'general', 'security', 'spreadsheets', 'meta');
- foreach($results as $tag){
- $tag_name = $tags[$tag["tag_id"]];
- $this->tags[] = $tag_name;
- }
- }
- public function submit($db){
- $query = "INSERT INTO questions (contributor, title, content) VALUES (:contributor, :title, :content)";
- $params = array(":contributor"=>$this->user->get_id(), ":title"=>$this->title, ":content"=>$this->content);
- $db->queryNone($query, $params);
- $query = "SELECT id FROM questions WHERE contributor = :contributor AND title = :title AND content = :content";
- $params = array(":contributor"=>$this->user->get_id(), ":title"=>$this->title, ":content"=>$this->content);
- $result = $db->queryAll($query, $params);
- $result = end($result);
- $this->id = $result["id"];
- foreach($this->tags as $tag){
- $this->submit_tag($tag, $this->id, $db);
- }
- }
- public function get_title(){
- return stripslashes($this->title);
- }
- public function get_content(){
- return stripslashes($this->content);
- }
- public function get_user(){
- return $this->user;
- }
- public function get_answers(){
- return $this->answers;
- }
- public function get_tags(){
- return $this->tags;
- }
- public function get_id(){
- return $this->id;
- }
- private function submit_tag($tag, $question_id, $db){
- $tags = array('melee', 'defence', 'ranged', 'prayer', 'magic', 'runecrafting', 'construction', 'dungeoneering', 'constitution', 'agility', 'herblore', 'thieving', 'crafting', 'fletching', 'slayer', 'hunter', 'divination', 'mining', 'smithing', 'fishing', 'cooking', 'firemaking', 'woodcutting', 'farming', 'summoning', 'minigames', 'distractions and diversions', 'combat', 'runescape 3', 'old school runescape', 'runescape classic', 'monster hunting', 'moneymaking', 'technical', 'general', 'security', 'spreadsheets', 'meta');
- $tag_id = array_search($tag, $tags);
- $query = "INSERT INTO tagged_items (tag_id, item_id) VALUES (:tag_id, :item_id)";
- $params = array(":tag_id"=>$tag_id, ":item_id"=>$question_id);
- $db->queryNone($query, $params);
- }
- private function load_answers($id, $db){
- $query = "SELECT * FROM answers WHERE questionid = :id";
- $params = array(":id" => $id);
- $results = $db->queryAll($query, $params);
- foreach($results as $result){
- $temp = new Answer();
- $temp->load_from_array($result, $db);
- $this->answers[] = $temp;
- }
- }
- public function to_json(){
- $json["user"] = $this->user->get_username();
- $json["reputation"] = $this->user->get_reputation();
- $json["content"] = $this->get_content();
- $json["timestamp"] = $this->timestamp;
- $json["title"] = $this->get_title();
- $json["answers"] = array();
- $json["tags"] = array();
- foreach($this->answers as $answer){
- $json["answers"][] = $answer->to_array();
- }
- foreach($this->tags as $tag){
- $json["tags"][] = $tag;
- }
- return json_encode($json);
- }
- }
- class Answer{
- private $questionid;
- private $answerid;
- private $user;
- private $content;
- private $upvotes;
- private $downvotes;
- private $comments;
- private $timestamp;
- public function __destruct(){
- $database = new Database();
- $this->update($database);
- }
- public function respond($questionid, $contributorid, $content, $db){
- $this->questionid = _id($questionid);
- $this->user = _user_id($contributorid, $db);
- $this->content = _html($content);
- }
- public function submit($db){
- $query = "INSERT INTO answers (contributor, questionid, content) VALUES (:contributor, :questionid, :content)";
- $params = array(":contributor"=>$this->user->get_id(), ":content"=>$this->content, ":questionid"=>$this->questionid);
- $db->queryNone($query, $params);
- $query = "SELECT id FROM answers WHERE contributor = :contributor AND content = :content";
- $params = array(":contributor"=>$this->user->get_id(), ":content"=>$this->content);
- $result = $db->queryAll($query, $params);
- $result = end($result);
- $this->answerid = $result["id"];
- }
- public function update($db){
- try{
- $query = "UPDATE answers SET contributor = :contributor, questionid = :questionid, content = :content, upvotes = :upvotes, downvotes = :downvotes WHERE id = :id";
- $params = array(":contributor"=>$this->user->get_id(), ":questionid"=>$this->questionid, ":content"=>$this->content, ":upvotes"=>$this->upvotes, ":downvotes"=>$this->downvotes, ":id"=>$this->answerid);
- $db->queryNone($query, $params);
- return $this;
- }catch(Exception $e){
- throw $e;
- }
- }
- public function get_id(){
- return $this->answerid;
- }
- public function get_user(){
- return $this->user;
- }
- public function get_content(){
- return stripslashes($this->content);
- }
- public function get_upvotes(){
- return $this->upvotes;
- }
- public function get_downvotes(){
- return $this->downvotes;
- }
- public function get_score(){
- return $this->get_upvotes() - $this->get_downvotes();
- }
- public function get_comments(){
- return $this->comments;
- }
- public function upvote(){
- $this->upvotes++;
- }
- public function downvote(){
- $this->downvote++;
- }
- public function load_from_array($answer, $db){
- $this->questionid = $answer["questionid"];
- $this->answerid = $answer["id"];
- $this->user = _user_id($answer["contributor"], $db);
- $this->content = $answer["content"];
- $this->upvotes = $answer["upvotes"];
- $this->downvotes = $answer["downvotes"];
- $this->timestamp = $answer["date"];
- $this->load_comments($db);
- }
- public function load_from_id($questionid, $answerid, $db){
- $query = "SELECT * FROM answers WHERE questionid = :questionid AND id = :id";
- $params = array(":questionid" => $questionid, ":id" => $id);
- $result = $db->query($query, $params);
- $this->load_from_array($result, $db);
- }
- public function load_comments($db){
- $query = "SELECT * FROM comments WHERE questionid = :questionid AND answerid = :answerid";
- $params = array(":questionid" => $this->questionid, ":answerid" => $this->answerid);
- $results = $db->queryAll($query, $params);
- foreach($results as $result){
- $temp = new Comment();
- $temp->load_from_array($result, $db);
- $this->comments[] = $temp;
- }
- }
- public function to_json(){
- $json["user"] = $this->user->get_username();
- $json["reputation"] = $this->user->get_reputation();
- $json["content"] = $this->content;
- $json["timestamp"] = $this->timestamp;
- $json["score"] = $this->get_score();
- $json["comments"] = array();
- foreach($this->comments as $comment){
- $json["comments"][] = $comment->to_json();
- }
- return stripslashes(json_encode($json));
- }
- public function to_array(){
- $json["user"] = $this->user->get_username();
- $json["reputation"] = $this->user->get_reputation();
- $json["content"] = $this->content;
- $json["timestamp"] = $this->timestamp;
- $json["score"] = $this->get_score();
- $json["comments"] = array();
- foreach($this->comments as $comment){
- $json["comments"][] = $comment->to_array();
- }
- return $json;
- }
- }
- class Comment{
- private $questionid;
- private $answerid;
- private $commentid;
- private $user;
- private $content;
- private $upvotes;
- private $timestamp;
- public function __destruct(){
- $database = new Database();
- $this->update($database);
- }
- public function respond($questionid, $answerid, $contributorid, $content, $db){
- $this->questionid = _id($questionid);
- $this->answerid = _id($answerid);
- $this->user = _user_id($contributorid, $db);
- $this->content = _html($content, 140);
- }
- public function submit($db){
- $query = "INSERT INTO comments (contributor, questionid, answerid, content) VALUES (:contributor, :questionid, :answerid, :content)";
- $params = array(":contributor"=>$this->user->get_id(), ":questionid"=>$this->questionid, ":answerid"=>$this->answerid, ":content"=>$this->content);
- $db->queryNone($query, $params);
- $query = "SELECT id FROM comments WHERE contributor = :contributor AND content = :content";
- $params = array(":contributor"=>$this->user->get_id(), ":content"=>$this->content);
- $result = $db->queryAll($query, $params);
- $result = end($result);
- $this->commentid = $result["id"];
- }
- public function update($db){
- try{
- $query = "UPDATE comments SET contributor = :contributor, questionid = :questionid, content = :content, upvotes = :upvotes, answerid = :answerid WHERE id = :id";
- $params = array(":contributor"=>$this->user->get_id(), ":questionid"=>$this->questionid, ":content"=>$this->content, ":upvotes"=>$this->upvotes, ":answerid"=>$this->answerid, ":id"=>$this->commentid);
- $db->queryNone($query, $params);
- return $this;
- }catch(Exception $e){
- throw $e;
- }
- }
- public function get_user(){
- return $this->user;
- }
- public function get_content(){
- return stripslashes($this->content);
- }
- public function get_upvotes(){
- return $this->upvotes;
- }
- public function get_score(){
- return $this->get_upvotes();
- }
- public function upvote(){
- $this->upvotes++;
- }
- public function load_from_array($comment, $db){
- $this->questionid = $comment["questionid"];
- $this->answerid = $comment["answerid"];
- $this->commentid = $comment["id"];
- $this->user = _user_id($comment["contributor"], $db);
- $this->content = $comment["content"];
- $this->upvotes = $comment["upvotes"];
- $this->timestamp = $comment["date"];
- }
- public function load_from_id($questionid, $answerid, $id, $db){
- $query = "SELECT * FROM comments WHERE questionid = :questionid AND answerid = :answerid AND id = :id";
- $params = array(":questionid" => $questionid, ":answerid" => $answerid, ":id" => $id);
- $result = $db->query($query, $params);
- $this->load_from_array($result, $db);
- }
- public function to_json(){
- $json["user"] = $this->user->get_username();
- $json["reputation"] = $this->user->get_reputation();
- $json["content"] = $this->content;
- $json["timestamp"] = $this->timestamp;
- $json["score"] = $this->get_score();
- return stripslashes(json_encode($json));
- }
- public function to_array(){
- $json["user"] = $this->user->get_username();
- $json["reputation"] = $this->user->get_reputation();
- $json["content"] = $this->content;
- $json["timestamp"] = $this->timestamp;
- $json["score"] = $this->get_score();
- return $json;
- }
- }
- class UnsignedInteger{
- private $value;
- private $min;
- private $max;
- public function __construct($value, $max=4294967295){
- $this->max = $this->toInt($max);
- $this->min = 0;
- $this->set($value);
- }
- public function set($value){
- $this->value = $this->toInt($value);
- if($this->value > $this->max){
- $this->value = $this->value%$this->max;
- }
- if($this->value < $this->min){
- $this->value = $this->max + $this->value%$this->max;
- }
- return $this;
- }
- public function get(){
- return $this->value;
- }
- private function toInt($value){
- if(is_numeric($value)){
- return floor($value + 0);
- }else{
- return 0;
- }
- }
- }
- class Integer{
- private $value;
- private $min;
- private $max;
- public function __construct($value, $max=2147483647, $min=-2147483647){
- $this->max = $this->toInt($max);
- $this->min = $this->toInt($min);
- $this->set($value);
- }
- public function set($value){
- $this->value = $this->toInt($value);
- if($this->value > $this->max){
- $this->value = $this->value%$this->max;
- }
- if($this->value < $this->min){
- $this->value = $this->max + $this->value%$this->max;
- }
- return $this;
- }
- public function get(){
- return $this->value;
- }
- private function toInt($value){
- if(is_numeric($value)){
- return floor($value + 0);
- }else{
- return 0;
- }
- }
- }
- class UnsignedFloat{
- private $value;
- private $min;
- private $max;
- public function __construct($value, $max=4294967295){
- $this->max = $this->toFloat($max);
- $this->min = 0;
- $this->set($value);
- }
- public function set($value){
- $this->value = $this->toFloat($value);
- if($this->value > $this->max){
- $this->value = $this->value%$this->max;
- }
- if($this->value < $this->min){
- $this->value = $this->max + $this->value%$this->max;
- }
- return $this;
- }
- public function get(){
- return $this->value;
- }
- private function toFloat($value){
- if(is_numeric($value)){
- return $value + 0;
- }else{
- return 0;
- }
- }
- }
- class Float{
- private $value;
- private $min;
- private $max;
- public function __construct($value, $max=2147483647, $min=-2147483647){
- $this->max = $this->toFloat($max);
- $this->min = $this->toFloat($min);
- $this->set($value);
- }
- public function set($value){
- $this->value = $this->toFloat($value);
- if($this->value > $this->max){
- $this->value = $this->value%$this->max;
- }
- if($this->value < $this->min){
- $this->value = $this->max + $this->value%$this->max;
- }
- return $this;
- }
- public function get(){
- return $this->value;
- }
- private function toInt($value){
- if(is_numeric($value)){
- return $value + 0;
- }else{
- return 0;
- }
- }
- }
- class Text{
- private $text;
- private $length;
- public function __construct($text, $length=255){
- $this->length = _uint($length);
- $this->set($text);
- }
- public function get(){
- return $this->text;
- }
- public function set($value){
- $value = (string) $value;
- $value = trim($value);
- $value = htmlspecialchars($value, ENT_QUOTES, "UTF-8");
- $value = strip_tags($value);
- //$value = escapeshellcmd($value);
- if(strlen($value) > $this->length){
- $this->text = substr($value, 0, $this->length);
- }else{
- $this->text = $value;
- }
- return $this;
- }
- }
- class HTML{
- private $text;
- private $length;
- public function __construct($text, $length=1024){
- $this->length = _uint($length);
- $this->set($text);
- }
- public function get(){
- return $this->text;
- }
- public function set($value){
- $value = (string) $value;
- $value = trim($value);
- $value = htmlspecialchars($value, ENT_QUOTES, "UTF-8");
- $value = strip_tags($value, "<p><a><img><div><span><table><tr><td><th><h2><h3><h4><blockquote><ul><ol><li>");
- //$value = escapeshellcmd($value);
- $dom = new DOMDocument;
- $dom->loadHTML($value);
- $nodes = $dom->getElementsByTagName('*');
- foreach($nodes as $node){
- if($node->hasAttributes()){
- foreach($node->attributes as $attr){
- $name = $attr->name;
- $value = $attr->value;
- switch($name){
- case "id": break;
- case "style": break;
- case "src": break;
- case "alt": break;
- case "class": break;
- case "href":
- if(strpos($value, "javascript") !== false){
- $node->removeAttribute($name);
- }
- break;
- default:
- $node->removeAttribute($name);
- break;
- }
- }
- }
- }
- $value = $dom->saveXML($dom->documentElement->firstChild->firstChild->firstChild);
- $value = strip_tags($value, "<p><a><img><div><span><table><tr><td><th><h2><h3><h4><blockquote><ul><ol><li>");
- if(strlen($value) > $this->length){
- $this->text = substr($value, 0, $this->length);
- }else{
- $this->text = $value;
- }
- return $this;
- }
- }
- class StrictText{
- private $text;
- private $length;
- public function __construct($text, $length=255){
- $this->length = _uint($length);
- $this->set($text);
- }
- public function get(){
- return $this->text;
- }
- public function set($value){
- $value = (string) $value;
- $value = trim($value);
- $value = htmlspecialchars($value, ENT_QUOTES, "UTF-8");
- $value = strip_tags($value);
- //$value = escapeshellcmd($value);
- $value = preg_replace("/[^A-Za-z0-9 ]/", '', $value);
- if(strlen($value) > $this->length){
- $this->text = substr($value, 0, $this->length);
- }else{
- $this->text = $value;
- }
- return $this;
- }
- }
- class Password{
- private $text;
- private $security;
- private $salt;
- public function __construct($text, $salt=null, $security=2){
- $this->security = _uint($security, 3);
- $this->set($text);
- if($salt == null){
- $this->salt = _salt();
- }else{
- $this->salt = $salt;
- }
- }
- public function get(){
- return $this->text;
- }
- public function set($text){
- $text = _text($text);
- if($this->security == 0){
- if(!ctype_graph($text)){
- throw new Exception("Password contains whitespace");
- }
- }
- if($this->security > 0){
- if(strlen($text) < 8){
- throw new Exception("Password cannot be less than 8 characters");
- }
- }
- if($this->security > 1){
- if(!preg_match('/[A-Za-z]/', $text) && !preg_match('/[0-9]/', $text)){
- throw new Exception("Password must a contain number");
- }
- }
- if($this->security > 2){
- if(ctype_lower($text)){
- throw new Exception("Password must contain a mix of uppercase and lowercase letters.");
- }
- }
- if($this->security > 3){
- if(ctype_alnum($text)){
- throw new Exception("Password must contain a symbol");
- }
- }
- $this->text = $this->hash($text, $this->salt);
- }
- function hash($value, $salt){
- $pepper = "[censored]";
- $loopcount = [censored];
- for($i = 0; $i < $loopcount; $i++){
- $value = hash("sha256", $value . $salt . $pepper);
- }
- return $value;
- }
- }
- class Email{
- private $text;
- public function __construct($text){
- $this->set($text);
- }
- public function get(){
- return $this->text;
- }
- public function set($email){
- if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
- throw new Exception("Invalid email");
- }
- if(strlen(_text($email)) < 6){
- throw new Exception("Invalid email");
- }
- $this->text = _text($email);
- }
- }
- class Enumerator{
- private $options;
- private $value;
- private $use_strict;
- public function __construct($options, $length=64, $use_strict=true){
- $this->use_strict = $use_strict;
- if(!is_array($options)){
- throw new Exception("Enumerator options must be in the form of an array");
- }
- foreach($options as $option){
- if($this->use_strict){
- $this->options[] = _strict($option, $length);
- }else{
- $this->options[] = _text($option, $length);
- }
- }
- }
- public function get(){
- return $this->value;
- }
- public function set($value){
- if($this->use_strict){
- $value = _strict($value);
- }else{
- $value = _text($value);
- }
- if(!in_array($value, $this->options)){
- throw new Exception("{$value} is not valid option");
- }
- $this->value = $value;
- }
- }
- function _uint($value, $max=4294967295){
- $uint = new UnsignedInteger($value, $max);
- return $uint->get();
- }
- function _int($value, $max=2147483647, $min=-2147483647){
- $int = new Integer($value, $max, $min);
- return $int->get();
- }
- function _ufloat($value, $max=4294967295){
- $ufloat = new UnsignedFloat($value, $max);
- return $ufloat->get();
- }
- function _float($value, $max=2147483647, $min=-2147483647){
- $float = new Float($value, $max, $min);
- return $float->get();
- }
- function _text($text, $length=255){
- $text = new Text($text, $length);
- return $text->get();
- }
- function _strict($text, $length=255){
- $text = new StrictText($text, $length);
- return $text->get();
- }
- function _html($text, $length=255){
- $html = new HTML($text, $length);
- return $html->get();
- }
- function _salt(){
- $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
- return $salt;
- }
- function _password($value, $salt=null, $security=2){
- try{
- $password = new Password($value, $salt, $security);
- return $password->get();
- }catch(Exception $e){
- throw $e;
- }
- }
- function _email($value){
- try{
- $email = new Email($value);
- return $email->get();
- }catch(Exception $e){
- throw $e;
- }
- }
- function _enum($value, $options, $length=64, $use_strict=true){
- try{
- $enum = new Enumerator($options, $length, $use_strict);
- $enum->set($value);
- return $enum->get();
- }catch(Exception $e){
- throw $e;
- }
- }
- function _id($value){
- return _uint($value);
- }
- function _username($value){
- return _strict($value, 12);
- }
- function _points($value){
- return _uint($value);
- }
- function _about($text){
- return _html($text, 512);
- }
- function _rank($value){
- return _enum($value, array('free agent','recruit','corporal','sergeant','lieutenant','captain','general','admin','organiser','coordinator','overseer','deputy owner','owner'));
- }
- function _cape($value){
- return _enum($value, array('none', 'comp', 'trim'));
- }
- function _user($name, $db){
- $user = new User();
- return $user->getFromDatabaseByUsername(_username($name), $db);
- }
- function _user_id($id, $db){
- $user = new User();
- return $user->getFromDatabaseByID(_id($id), $db);
- }
- function _clan($name, $db){
- $clan = new Clan();
- return $clan->getFromDatabaseByName(_strict($name), $db);
- }
- function _clan_id($id, $db){
- $clan = new Clan();
- return $clan->getFromDatabaseByID(_id($id), $db);
- }
- function _tag($value){
- return _enum($value, array('melee', 'defence', 'ranged', 'prayer', 'magic', 'runecrafting', 'construction', 'dungeoneering', 'constitution', 'agility', 'herblore', 'thieving', 'crafting', 'fletching', 'slayer', 'hunter', 'divination', 'mining', 'smithing', 'fishing', 'cooking', 'firemaking', 'woodcutting', 'farming', 'summoning', 'minigames', 'distractions and diversions', 'combat', 'runescape 3', 'old school runescape', 'runescape classic', 'monster hunting', 'moneymaking', 'technical', 'general', 'security', 'spreadsheets', 'meta'));
- }
- function _encrypt($text){
- $key = "[censored]";
- $output = "";
- for($i = 0; $i < strlen($text); $i++){
- for($j=0; $j < strlen($key); $j++, $i++){
- $output .= $text[$i] ^ $key[$j];
- }
- }
- return $output;
- }
- function _decrypt($text){
- $key = "[censored]";
- $output = "";
- for($i = 0; $i < strlen($text); $i++){
- for($j=0; $j < strlen($key); $j++, $i++){
- $output .= $key[$j] ^ $text[$i];
- }
- }
- return $output;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment