Advertisement
alexgrist

Untitled

Feb 11th, 2012
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.37 KB | None | 0 0
  1. <?php session_start();
  2. class API
  3. {
  4.     // A function to make FP API calls.
  5.     public function call($actions = array())
  6.     {
  7.         if ($actions)
  8.         {
  9.             $firstAction = false;
  10.             $url = "http://api.facepun.ch/?";
  11.            
  12.             $curl = curl_init();
  13.            
  14.             foreach($actions as $k => $v)
  15.             {
  16.                 if (!$firstAction)
  17.                 {
  18.                     $url = $url."$k=$v";
  19.                
  20.                     $firstAction = true;
  21.                 }
  22.                 else
  23.                 {
  24.                     $url = $url."&$k=$v";
  25.                 }
  26.             }
  27.            
  28.             if ($actions['action'] != "authenticate" && $this->isLoggedin())
  29.             {
  30.                 $url = $url."&username=".$_SESSION['username']."&password=".$_SESSION['password'];
  31.             }
  32.            
  33.             curl_setopt($curl, CURLOPT_URL, $url);
  34.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  35.             $result = curl_exec($curl);
  36.             curl_close($curl);
  37.            
  38.             return json_decode($result);
  39.         }
  40.     }
  41.    
  42.     // A function to check if the user is logged in.
  43.     public function isLoggedin()
  44.     {
  45.         if (isset($_SESSION['loggedin']) && isset($_SESSION['username']) && isset($_SESSION['password']) && $_SESSION['loggedin'])
  46.         {
  47.             return true;
  48.         }
  49.     }
  50.    
  51.     // A function to login the user.
  52.     public function login($username = false, $password = false)
  53.     {
  54.         if ($username && $password)
  55.         {
  56.             $password = md5($password);
  57.             $result = $this->call(array(
  58.                 "action" => "authenticate",
  59.                 "username" => $username,
  60.                 "password" => $password
  61.             ));
  62.            
  63.             if ($result && $result->login == "Login OK")
  64.             {
  65.                 // REWRITE THIS SHIT, IT'S INSECURE.
  66.                 $_SESSION['loggedin'] = true;
  67.                 $_SESSION['username'] = $username;
  68.                 $_SESSION['password'] = $password;
  69.                
  70.                 return true;
  71.             }
  72.             else
  73.             {
  74.                 return false;
  75.             }
  76.         }
  77.     }
  78.    
  79.     // A function to get forum layout.
  80.     public function getforums()
  81.     {
  82.         $result = $this->call(array("action" => "getforums"));
  83.        
  84.         return $result->categories;
  85.     }
  86.    
  87.     // A function to get all the threads in a forum.
  88.     public function getthreads($id = false, $page = false)
  89.     {
  90.         if ($id)
  91.         {
  92.             $params = array("action" => "getthreads", "forum_id" => $id);
  93.            
  94.             if ($page)
  95.             {
  96.                 $params["page"] = $page;
  97.             }
  98.    
  99.             $result = $this->call($params);
  100.            
  101.             return $result;
  102.         }
  103.     }
  104.    
  105.     // A function to get all the posts in a thread.
  106.     public function getposts($id = false, $page = false)
  107.     {
  108.         if ($id)
  109.         {
  110.             $params = array("action" => "getposts", "thread_id" => $id);
  111.            
  112.             if ($page)
  113.             {
  114.                 $params["page"] = $page;
  115.             }
  116.    
  117.             $result = $this->call($params);
  118.            
  119.             return $result;
  120.         }
  121.     }
  122.    
  123.     // A function to post a reply to a thread.
  124.     public function newreply($id = false, $message = false)
  125.     {
  126.         if ($id && $message)
  127.         {
  128.             $result = $this->call(array("action" => "new reply", "thread_id" => $id, "message" => $message));
  129.            
  130.             return true;
  131.         }
  132.     }
  133.    
  134.     // A function to get the popular threads.
  135.     public function getpopularthreads()
  136.     {
  137.         $result = $this->call(array("action" => "getpopularthreads"));
  138.        
  139.         return $result;
  140.     }
  141.    
  142.     // A function to get the read threads.
  143.     public function getreadthreads()
  144.     {
  145.         $result = $this->call(array("action" => "getreadthreads"));
  146.        
  147.         return $result->threads;
  148.     }
  149.    
  150.     public function getpms($page = 1)
  151.     {
  152.         $result = $this->call(array("action" => "getpms", "page" => $page));
  153.        
  154.         return $result;
  155.     }
  156.    
  157.     public function getpm($id = false)
  158.     {
  159.         if ($id)
  160.         {
  161.             $result = $this->call(array("action" => "getpm", "pm_id" => $id));
  162.            
  163.             return $result;
  164.         }
  165.     }
  166. }
  167. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement