Guest User

Untitled

a guest
May 29th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.53 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * TODO
  5.  *  Better logging instantiation/customization
  6.  *  Config handling class
  7.  *  Make it easier to set response code and output error
  8.  *  Check user<->game permissions
  9.  *  should login care about roles? or is that job for getUserInfo?
  10.  */
  11.  
  12. require_once 'Outspark/3rdparty/Restafarian/restafarian_common.php';
  13. require_once '../OSKSignedRestResource.inc';
  14. require_once '../OSKUserModel.inc';
  15. require_once 'Outspark/Common/KeyMaster/KeyMaster.php';
  16. require_once 'Outspark/Platform/OutsparkUser/OSKUser.php';
  17. require_once 'Outspark/Platform/Ruxpin/Ruxpin.php';
  18. require_once 'Outspark/Platform/AvatarProfile/AvatarProfile.php';
  19.  
  20. define('MEEBO_AUTH_USERNAME', 1001);
  21. define('MEEBO_AUTH_UID', 1002);
  22.  
  23. // Set up logging
  24. if (!class_exists('Log')) {
  25.     include 'Log.php';
  26. }
  27. $GLOBALS['logger'] = Log::factory('file', '', 'UserResource');
  28. if (is_null($GLOBALS['logger'])) {
  29.     exit("Unable to initiate Logging");
  30. }
  31. $GLOBALS['logger']->setMask(PEAR_LOG_ALL);
  32.  
  33. $GLOBALS['conf'] = parse_ini_file('/var/www/conf/restconf.ini', true);
  34.  
  35. class MeeboResource extends OSKRestResource
  36. {
  37.     public function dispatch ($req)
  38.     {
  39.         $next = $this;
  40.         $version = $this->getNextRawSegment();
  41.  
  42.         /*
  43.          * If version doesn't match, return /user resource
  44.          */
  45.         if ($version != 'v1') {
  46.             return $this;
  47.         }
  48.  
  49.         /*
  50.          * Get the portion after version
  51.          */
  52.         $this->incrementNextIndex();
  53.         $segment = $this->getNextRawSegment();
  54.  
  55.         switch ($segment) {
  56.             case 'authenticateusername':
  57.                 $next = new MeeboAuthenticationResource(MEEBO_AUTH_USERNAME);
  58.                 break;
  59.             case 'authenticateuid':
  60.                 $next = new MeeboAuthenticationResource(MEEBO_AUTH_UID);
  61.                 break;
  62.             case 'getfriends':
  63.                 $next = new MeeboFriendsListResource();
  64.                 break;
  65.             case 'getinfo':
  66.                 $next = new MeeboUserInfoResource();
  67.                 break;
  68.             case 'usernameexists':
  69.                 $next = new MeeboUserExistsResource(MEEBO_AUTH_USERNAME);
  70.                 break;
  71.             case 'uidexists':
  72.                 $next = new MeeboUserExistsResource(MEEBO_AUTH_UID);
  73.                 break;
  74.             case 'authenticatetoken':
  75.                 $next = new MeeboAuthenticationTokenResource();
  76.                 break;
  77.             case 'statuschange':
  78.                 $next = new MeeboStatusChangeResource();
  79.                 break;
  80.             case 'notificationsread':
  81.             case 'addfriendrequest':
  82.             case 'blocked':
  83.             case 'abusereported':
  84.                 $next = new MeeboStatusOKResource();
  85.                 break;        
  86.             default:
  87.                 // no default action
  88.                 break;
  89.         }
  90.  
  91.         return $next;
  92.     }
  93.  
  94.     public function GET ($req, $resp)
  95.     {
  96.         /*
  97.          * Anything not handled under /user is an error.
  98.          */
  99.         $resp->setStatusCode(REST_STATUS_BAD_REQUEST);
  100.         $resp->makeError($this, $resp->getStatusCode(), "Bad Request");
  101.     }
  102. }
  103.  
  104. class MeeboAuthenticationTokenResource extends OSKRestResource {
  105.     public function GET($req, $resp){
  106.         return $this->POST($req, $resp);
  107.     }
  108.    
  109.     public function POST($req, $resp){
  110.  
  111.         $conf = $GLOBALS['conf'];
  112.  
  113.         $params = $req->getUri()->getQueryParams();
  114.         //$user = $params->getFirst('uid');
  115.         //$token = $params->getFirst('token');
  116.         $user = $_REQUEST['uid'];
  117.         $token = $_REQUEST['token'];
  118.  
  119.         $userInfo = false;
  120.  
  121.         $output = isset($_REQUEST['output'])? $_REQUEST['output']:'json';
  122.  
  123.         $result = array();
  124.         $result["stat"] = "fail";
  125.         if (!(empty($user) || empty($token))) {
  126.             try {
  127.                 $model = new OSKUserModel();
  128.                 $userInfo = $model->getUserInfoByUID($user);
  129.                 if ($userInfo) {
  130.                     // grab the time from the end of the token
  131.                     list($hash, $time) = explode(".", $token, 2);
  132.                     if(time() <= $time + (60 * 60 * 24 * 14)){ // two weeks
  133.                         // lookup the token secret
  134.                         $S1 = OSKKeyMaster::getInstance()->getKey('meebo.token');
  135.                         // generate token as: H(S1 + H(P + T))
  136.                         $local_hash = hash("sha256", $S1.hash("sha256", $userInfo['password'].$time));
  137.                         // compare tokens. if they match, we're golden
  138.                         if($local_hash == $hash){
  139.                             $result["stat"] = "ok";
  140.                             $result["data"] = array("authenticated" => true, "uid" => $userInfo["userid"], "name" => $userInfo["sparkid"]);
  141.                         }else{
  142.                             $result["stat"] = "ok";
  143.                             $result["data"] = array("authenticated" => false);
  144.                         }
  145.                     }else{
  146.                         $result["stat"] = "ok";
  147.                         $result["data"] = array("authenticated" => false);
  148.                     }
  149.                 }else{
  150.                     $result["errorcode"] = 400;
  151.                     $result["msg"] = "User id not found";
  152.                 }
  153.             } catch (Exception $e) {
  154.                 $result["errorcode"] = 500;
  155.                 $result["msg"] = "Unable to look up user";
  156.             }
  157.         }else{
  158.             $result["errorcode"] = 400;
  159.             $result["msg"] = "Missing required parameter.";            
  160.         }
  161.        
  162.         $json = json_encode($result);
  163.  
  164.         if ($output == 'text') {
  165.             $resp->setContent(CTYPE_TEXT, $json);
  166.         } else {
  167.             $resp->setContent(CTYPE_JSON, $json);
  168.         }
  169.        
  170.         $resp->setStatusCode(REST_STATUS_OK);
  171.     }
  172. }
  173.  
  174. class MeeboStatusOKResource extends OSKRestResource {
  175.     public function GET($req, $resp){
  176.         return $this->POST($req, $resp);
  177.     }
  178.    
  179.     public function POST($req, $resp){
  180.         $response = array();
  181.         $response["stat"]= "ok";
  182.        
  183.         $json = json_encode($response);
  184.        
  185.         $output = isset($_REQUEST['output'])?$_REQUEST['output']:'text';
  186.         if ($output == 'text') {
  187.             $resp->setContent(CTYPE_TEXT, $json);
  188.         } else {
  189.             $resp->setContent(CTYPE_JSON, $json);
  190.         }
  191.        
  192.         $resp->setStatusCode(REST_STATUS_OK);
  193.        
  194.     }
  195. }
  196. class MeeboUserInfoResource extends OSKRestResource {
  197.    
  198.     public function GET($req, $resp){
  199.         $response = array();
  200.         $response["stat"]= "fail";
  201.        
  202.         $params = $req->getUri()->getQueryParams();
  203.         $uid = $_REQUEST['uid'];//$params->getFirst('uid');
  204.        
  205.         $user = OSKUser::getByUserID($uid);
  206.         $profile = $user->getProfile();
  207.        
  208.         $avatarprofile = new AvatarProfile();
  209.        
  210.         $response["stat"] = "ok";
  211.         $response["data"] = array();
  212.        
  213.         $image = $avatarprofile->getProfileImage($uid, AvatarProfile::PROF_IMAGE_THUMB, $junk);
  214.         if($image){
  215.             $response["data"]["imageurl"] = $image;
  216.         }
  217.        
  218.         $status = $avatarprofile->getStatusMessage($uid, $junk);
  219.         if($status){
  220.             $response["data"]["status"] = $status;
  221.         }
  222.        
  223.         $gender = $profile["gender"];
  224.         if($gender){
  225.             $response["data"]["gender"] = $gender;
  226.         }
  227.        
  228.         $json = json_encode($response);
  229.        
  230.         $output = $params->getFirst('output');
  231.         if ($output == 'text') {
  232.             $resp->setContent(CTYPE_TEXT, $json);
  233.         } else {
  234.             $resp->setContent(CTYPE_JSON, $json);
  235.         }
  236.        
  237.         $resp->setStatusCode(REST_STATUS_OK);
  238.        
  239.     }
  240.    
  241. }
  242.  
  243. class MeeboFriendsListResource extends OSKRestResource {
  244.     public function GET($req, $resp){
  245.         $response = array();
  246.         $response["stat"]= "fail";
  247.        
  248.         $params = $req->getUri()->getQueryParams();
  249.         $user = $_REQUEST['uid'];// $params->getFirst('uid');
  250.        
  251.         $ruxpin = new Ruxpin($user);
  252.         $output = $params->getFirst('output');
  253.         if (empty($output)) {
  254.             $output = 'json';
  255.         }
  256.  
  257.         $result = array();
  258.         $result["stat"] = "ok";
  259.        
  260.         $buddies = $ruxpin->getBuddies();
  261.         $result["data"]["friendresponse"] = "full";
  262.     $result["data"]["friends"] = array();
  263.         foreach($buddies as $buddy){
  264.             $result["data"]["friends"][] = array("uid" => $buddy->uid, "name" => $buddy->sparkid);
  265.         }
  266.        
  267.                 $json = json_encode($result);
  268.  
  269.         if ($output == 'text') {
  270.             $resp->setContent(CTYPE_TEXT, $json);
  271.         } else {
  272.             $resp->setContent(CTYPE_JSON, $json);
  273.         }
  274.        
  275.         $resp->setStatusCode(REST_STATUS_OK);
  276.        
  277.     }
  278. }
  279.  
  280. class MeeboAuthenticationResource extends OSKRestResource {
  281.    
  282.     private $authtype;
  283.    
  284.     public function __construct($auth_type){
  285.         $this->authtype = $auth_type;
  286.         parent::__construct();
  287.     }
  288.    
  289.     public function GET ($req, $resp){
  290.         return $this->POST($req, $resp);
  291.     }    
  292.     public function POST ($req, $resp)
  293.     {
  294.         $conf = $GLOBALS['conf'];
  295.        
  296.         $response = array();
  297.         $response["stat"]= "fail";
  298.        
  299.         $user = false;
  300.        
  301.         $params = $req->getUri()->getQueryParams();
  302.         if($this->authtype == MEEBO_AUTH_UID){
  303.             $user = is_numeric($_REQUEST['uid'])?$_REQUEST['uid'] : -1;//$params->getFirst('uid');
  304.         }else{
  305.             $user = $_REQUEST['username'];//$params->getFirst('username');
  306.         }
  307.         $pass = $_REQUEST['password'];//$params->getFirst('password');
  308.  
  309.         $userInfo = false;
  310.  
  311.         $realm = "meebo";
  312.  
  313.         $output = $params->getFirst('output');
  314.         if (empty($output)) {
  315.             $output = 'json';
  316.         }
  317.  
  318.         $result = array();
  319.         $result["stat"] = "fail";
  320.         if ( !empty($user) && !empty($pass) ) {
  321.             try {
  322.                 $model = new OSKUserModel();
  323.                 if($this->authtype == MEEBO_AUTH_UID){
  324.                     $userInfo = $model->getUserInfoByUID($user);
  325.                 }else{
  326.                     $userInfo = $model->getUserInfo($user);
  327.                 }
  328.                 if ($userInfo) {
  329.                     // Deny access to users who are unverified or banned
  330.                     if (!empty($userInfo['status'])) {
  331.                         if (!empty($userInfo['verified'])) {
  332.                             $result["stat"] = "ok";    
  333.                             if ( ($userInfo['password'] != $pass) && ($userInfo['password'] != md5($pass)) ) { // check to work around reg issue where 54k users had their passwords double hashed
  334.                                 $result["data"] = array("authenticated" => false);
  335.                             }else{
  336.                                 $result["data"] = array("authenticated" => true, "uid"=> $userInfo["userid"], "name" => $userInfo["sparkid"]);
  337.                             }
  338.                         }else{
  339.                             // not verified
  340.                             $result["stat"] = "ok";
  341.                             $result["data"] = array("authenticated" => false);
  342.                         }
  343.                     }else{
  344.                         $result["stat"] = "ok";
  345.                         $result["data"] = array("authenticated" => false);          
  346.                     }
  347.                 }else{
  348.                     $result["stat"] = "ok";
  349.                     $result["data"] = array("authenticated" => false);          
  350.                 }
  351.             } catch (Exception $e) {
  352.                 $result["stat"] = "ok";
  353.                 $result["data"] = array("authenticated" => false);
  354.             }
  355.         }else{
  356.             $result["errorcode"] = 400;
  357.             $result["msg"] = "Missing required parameters. Check your username and password";            
  358.         }
  359.        
  360.         $json = json_encode($result);
  361.  
  362.         if ($output == 'text') {
  363.             $resp->setContent(CTYPE_TEXT, $json);
  364.         } else {
  365.             $resp->setContent(CTYPE_JSON, $json);
  366.         }
  367.        
  368.         $resp->setStatusCode(REST_STATUS_OK);
  369.     }
  370.  
  371. }
  372.  
  373. class MeeboUserExistsResource extends OSKRestResource {
  374.    
  375.     private $authtype;
  376.    
  377.     public function __construct($auth_type){
  378.         $this->authtype = $auth_type;
  379.         parent::__construct();
  380.     }
  381.    
  382.     public function GET ($req, $resp){
  383.         return $this->POST($req, $resp);
  384.     }
  385.    
  386.     public function POST ($req, $resp)
  387.     {
  388.         $conf = $GLOBALS['conf'];
  389.        
  390.         $response = array();
  391.         $response["stat"]= "fail";
  392.        
  393.         $user = false;
  394.        
  395.         $params = $req->getUri()->getQueryParams();
  396.         if($this->authtype == MEEBO_AUTH_UID){
  397.             $user = is_numeric($_REQUEST['uid'])?$_REQUEST['uid']: false;//$params->getFirst('uid');
  398.         }else{
  399.             $user = $_REQUEST['username'];//$params->getFirst('username');
  400.         }
  401.  
  402.         $userInfo = false;
  403.  
  404.         $output = $params->getFirst('output');
  405.         if (empty($output)) {
  406.             $output = 'json';
  407.         }
  408.  
  409.         $result = array();
  410.         $result["stat"] = "fail";
  411.         if (!(empty($user))) {
  412.             try {
  413.                 $model = new OSKUserModel();
  414.                 if($this->authtype == MEEBO_AUTH_UID){
  415.                     $userInfo = $model->getUserInfoByUID($user);
  416.                 }else{
  417.                     $userInfo = $model->getUserInfo($user);
  418.                 }
  419.                 if ($userInfo) {
  420.                     // Deny access to users who are unverified or banned
  421.                     $result["stat"] = "ok";    
  422.                     $result["data"] = array("exists" => true, "uid"=> $userInfo["userid"], "name" => $userInfo["sparkid"]);
  423.                 }else{
  424.                     $result["stat"] = "ok";
  425.                     $result["exists"] = false;
  426.                 }
  427.             } catch (Exception $e) {
  428.                     $result["stat"] = "ok";
  429.                 $result["exists"] = false;            
  430.             }
  431.         }else{
  432.             $result["errorcode"] = 400;
  433.             $result["msg"] = "Missing required parameter.";            
  434.         }
  435.        
  436.         $json = json_encode($result);
  437.  
  438.         if ($output == 'text') {
  439.             $resp->setContent(CTYPE_TEXT, $json);
  440.         } else {
  441.             $resp->setContent(CTYPE_JSON, $json);
  442.         }
  443.        
  444.         $resp->setStatusCode(REST_STATUS_OK);
  445.     }
  446.  
  447. }
  448.  
  449.  
  450.  
  451.  
  452. class MeeboStatusChangeResource extends OSKRestResource {
  453.    
  454.     public function POST($req, $resp){
  455.         $response = array();
  456.         $response["stat"]= "ok";
  457.        
  458.         $params = $req->getUri()->getQueryParams();
  459.         $uid = $params->getFirst('uid');
  460.         $status = $params->getFirst('status');
  461.        
  462.         if( isset($uid, $status) ){
  463.             $response["stat"]= "ok";
  464.         }else{
  465.             $response["stat"]= "fail";
  466.             $response["errorcode"] = 400;
  467.             $response["msg"] = "Missing required parameters.";
  468.         }
  469.  
  470.         $json = json_encode($response);
  471.        
  472.         $output = isset($_REQUEST['output'])?$_REQUEST['output']:'text';
  473.         if ($output == 'text') {
  474.             $resp->setContent(CTYPE_TEXT, $json);
  475.         } else {
  476.             $resp->setContent(CTYPE_JSON, $json);
  477.         }
  478.        
  479.         $resp->setStatusCode(REST_STATUS_OK);
  480.        
  481.     }
  482. }
  483.  
  484. ?>
Add Comment
Please, Sign In to add comment