Advertisement
SirSpamModz

api

Aug 2nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.30 KB | None | 0 0
  1. <?php
  2.  
  3. if(!defined('IS_INTERNAL')) { die('Direct file connect attempt.'); }
  4.  
  5. class api
  6. {
  7.     /**
  8.     * A place to store variables linked with the api key.
  9.     */
  10.     private static $token_variables = array();
  11.  
  12.     /**
  13.     * Adds to the list of variables linked with the token.
  14.     *
  15.     * @param string $name The name of the new variable
  16.     * @param mixed $data The data of the new variable
  17.     */
  18.     private static function AddTokenVariable($name, $data)
  19.     {
  20.         if(!is_string($name)) {
  21.             return false;
  22.         }
  23.  
  24.         api::$token_variables[$name] = $data;
  25.     }
  26.  
  27.     /**
  28.     * Gets a variable that was fetched when you authenticate token.
  29.     *
  30.     * @param string $name The name of the variable.
  31.     */
  32.     public static function GetVariable($name)
  33.     {
  34.         if(!is_string($name)) {
  35.             return false;
  36.         }
  37.  
  38.         if(isset(api::$token_variables[$name])) {
  39.             return api::$token_variables[$name];
  40.         }
  41.  
  42.         return false;
  43.     }
  44.  
  45.  
  46.     /**
  47.     * Authenticates an API token and gets all data linked with the token.
  48.     *
  49.     * @param string $token The token that we'll be using.
  50.     */
  51.     public static function AuthenticateToken($token)
  52.     {
  53.         if(!config::GetConfig("IS_API_ENABLED")) {
  54.             return array(
  55.                 'success' => false,
  56.                 'response' => 'API is disabled.'
  57.             );
  58.         }
  59.  
  60.         if(!sql::Instance()) {
  61.             return array(
  62.                 'success' => false,
  63.                 'response' => 'Failed to initialize connection with mysql database.'
  64.             );
  65.         }
  66.  
  67.         $is_ok = true;
  68.  
  69.         /* Getting information linked with token */
  70.         if($stmt = sql::Prepare("SELECT `user_id`, `enabled`, 'can_upload', 'can_delete', 'can_view' FROM `api_keys` WHERE `token` = ?")) {
  71.             $stmt->bind_param('s', $token);
  72.             $is_ok = $stmt->execute();
  73.             $stmt->store_result();
  74.  
  75.             if($stmt->num_rows == 0) {
  76.                 return array(
  77.                     'success' => false,
  78.                     'response' => 'Invalid authentication key.'
  79.                 );
  80.             }
  81.  
  82.             $stmt->bind_param($user_id, $enabled, $can_upload, $can_delete, $can_view);
  83.             $is_ok = $stmt->fetch();
  84.             $stmt->close();
  85.         }
  86.         else {
  87.             $is_ok = false;
  88.         }
  89.  
  90.         if($is_ok) {
  91.             api::AddTokenVariable("HAS_AUTHENTICATED", true);
  92.             api::AddTokenVariable("USER_ID", $user_id);
  93.             api::AddTokenVariable("IS_ENABLED", misc::IsTrue($enabled));
  94.  
  95.             //Permissions variables
  96.             api::AddTokenVariable("CAN_UPLOAD", misc::IsTrue($can_upload));
  97.             api::AddTokenVariable("CAN_DELETE", misc::IsTrue($can_delete));
  98.             api::AddTokenVariable("CAN_VIEW", misc::IsTrue($can_view));
  99.  
  100.             if(api::GetVariable("IS_ENABLED")) {
  101.                 return array(
  102.                     'success' => true,
  103.                     'response' => 'Fetched token successfully'
  104.                 );
  105.             }
  106.             else {
  107.                 return array(
  108.                     'success' => false,
  109.                     'response' => 'Token has been disabled'
  110.                 );
  111.             }
  112.         }
  113.         else {
  114.             return array(
  115.                 'success' => false,
  116.                 'response' => 'Something wen\'t wrong!'
  117.             );
  118.         }
  119.     }
  120.  
  121.     /**
  122.     * Generates a token for a specific user.
  123.     *
  124.     * @param integer $user_id The user id that'll get the API token
  125.     * @param boolean
  126.     */
  127.     public static function GenerateToken($user_id, $can_upload, $can_delete, $can_view)
  128.     {
  129.         if(!config::GetConfig("IS_API_ENABLED")) {
  130.             return array(
  131.                 'success' => false,
  132.                 'response' => 'API is disabled.'
  133.             );
  134.         }
  135.  
  136.         if(!is_integer($user_id)) {
  137.             return array(
  138.                 'success' => false,
  139.                 'response' => 'User id is an invalid type.'
  140.             );
  141.         }
  142.  
  143.         if(!sql::Instance()) {
  144.             return array(
  145.                 'success' => false,
  146.                 'response' => 'Failed to initialize connection with mysql database.'
  147.             );
  148.         }
  149.  
  150.         //Converting the parameters to sql booleans
  151.         misc::BooleanToString($can_upload);
  152.         misc::BooleanToString($can_delete);
  153.         misc::BooleanToString($can_view);
  154.  
  155.         $is_ok = true;
  156.  
  157.         /* Generates a token. Format: sha512(rnd(100) + user_id) + rnd(12) */
  158.         $token = hash("sha512", cryptography::RandomString(100, true) . (string)$user_id) . cryptography::RandomString(12, false, false);
  159.  
  160.         /* Inserting the newly made token */
  161.         if($stmt = sql::Prepare("INSERT INTO `api_keys` (`user_id`, `token`, `enabled`, `can_upload`, `can_delete`, `can_view`) VALUES (?, ?, 'true', ?, ?, ?)")) {
  162.             $stmt->bind_param('is', $user_id, $token, $can_upload, $can_delete, $can_view);
  163.             $is_ok = $stmt->execute();
  164.             $stmt->close();
  165.         }
  166.         else {
  167.             $is_ok = false;
  168.         }
  169.  
  170.         if($is_ok) {
  171.             return array(
  172.                 'success' => true,
  173.                 'response' => 'Token generated successfully',
  174.                 'token' => $token
  175.             );
  176.         }
  177.         else {
  178.             return array(
  179.                 'success' => false,
  180.                 'response' => 'Failed  to generate token'
  181.             );
  182.         }
  183.     }
  184.  
  185.  
  186.     /**
  187.     * This will change the permissions of the token.
  188.     *
  189.     * @param integer $user_id The owner of the token.
  190.     * @param string $token The token you wish to edit.
  191.     * @param boolean $can_upload The new can_upload permission.
  192.     * @param boolean $can_delete The new $can_delete permission.
  193.     * @param boolean $can_view The new $can_view permission.
  194.     */
  195.     public static function EditToken($user_id, $token, $can_upload, $can_delete, $can_view)
  196.     {
  197.         /* Checking that the API is enabled*/
  198.         if(!config::GetConfig("IS_API_ENABLED")) {
  199.             return array(
  200.                 'success' => false,
  201.                 'response' => 'API is disabled.'
  202.             );
  203.         }
  204.  
  205.         /* Connecting to mysql */
  206.         if(!sql::Instance()) {
  207.             return array(
  208.                 'success' => false,
  209.                 'response' => 'Failed to initialize connection with mysql database.'
  210.             );
  211.         }
  212.  
  213.         /* Converting the parameters to sql booleans. */
  214.         misc::BooleanToString($can_upload);
  215.         misc::BooleanToString($can_delete);
  216.         misc::BooleanToString($can_view);
  217.  
  218.         /* Variable to say whether everything wen't okay. */
  219.         $is_ok = true;
  220.  
  221.         /* Updating the token detials */
  222.         if($stmt = sql::Prepare("UPDATE `api_keys` SET `can_upload` = ?, `can_delete` = ?, `can_view` = ? WHERE `user_id` =? AND `token` = ? AND `enabled` = 'true'")) {
  223.             $stmt->bind_param('sssis', $can_upload, $can_delete, $can_view, $user_id, $token);
  224.             $is_ok = $stmt->execute();
  225.             $stmt->close();
  226.         }
  227.         else {
  228.             $is_ok = false;
  229.         }
  230.  
  231.         if($is_ok) {
  232.             /*Everything wen't as planned, so returning with a success message. */
  233.             return array(
  234.                 'success' => true,
  235.                 'response' => 'Edited token successfully.'
  236.             );
  237.         }
  238.         else {
  239.             /* Something wen't wrong. Return with a message that will be shown to user */
  240.             return array(
  241.                 'success' => false,
  242.                 'response' => 'Something wen\'t wrong.'
  243.             );
  244.         }
  245.     }
  246.  
  247.     /**
  248.     * This will 'soft delete' a token. Soft deleting will just disable it and prev-
  249.     * ent user from viewing it..
  250.     *
  251.     * @param integer $user_id The owner of the token.
  252.     * @param string $token The token you wish to delete.
  253.     */
  254.     public static function DeleteToken($user_id, $token)
  255.     {
  256.         /* Checking that the API is enabled*/
  257.         if(!config::GetConfig("IS_API_ENABLED")) {
  258.             return array(
  259.                 'success' => false,
  260.                 'response' => 'API is disabled.'
  261.             );
  262.         }
  263.  
  264.         /* Connecting to mysql */
  265.         if(!sql::Instance()) {
  266.             return array(
  267.                 'success' => false,
  268.                 'response' => 'Failed to initialize connection with mysql database.'
  269.             );
  270.         }
  271.  
  272.         /* Variable to say whether everything wen't okay. */
  273.         $is_ok = true;
  274.  
  275.         /* Disabling the token, which is pretty much the same as deleting it. */
  276.         if($stmt = sql::Prepare("UPDATE `api_keys` SET `enabled` = 'false' WHERE `user_id` = ? AND `token` = ?")) {
  277.             $stmt->bind_param('is', $user_id, $token);
  278.             $is_ok = $stmt->execute();
  279.             $stmt->close();
  280.         }
  281.         else {
  282.             $is_ok = false;
  283.         }
  284.  
  285.         if($is_ok) {
  286.             /*Everything wen't as planned, so returning with a success message. */
  287.             return array(
  288.                 'success' => true,
  289.                 'response' => 'Edited token successfully.'
  290.             );
  291.         }
  292.         else {
  293.             /* Something wen't wrong. Return with a message that will be shown to user */
  294.             return array(
  295.                 'success' => false,
  296.                 'response' => 'Something wen\'t wrong.'
  297.             );
  298.         }
  299.     }
  300.  
  301.     /**
  302.     * Gets all tokens a user has linked to his account.
  303.     *
  304.     *
  305.     */
  306.     public static function GetTokens($user_id)
  307.     {
  308.         /* Checking that the API is enabled*/
  309.         if(!config::GetConfig("IS_API_ENABLED")) {
  310.             return array(
  311.                 'success' => false,
  312.                 'response' => 'API is disabled.'
  313.             );
  314.         }
  315.  
  316.         /* Connecting to mysql */
  317.         if(!sql::Instance()) {
  318.             return array(
  319.                 'success' => false,
  320.                 'response' => 'Failed to initialize connection with mysql database.'
  321.             );
  322.         }
  323.  
  324.         $is_ok = true;
  325.  
  326.         /* Updating the token detials */
  327.         if($stmt = sql::Prepare("SELECT `token`, `can_upload`, `can_delete`, `can_view` FROM `api_keys` WHERE `user_id` = ? AND `enabled` = 'true'")) {
  328.             $stmt->bind_param('i', $user_id);
  329.             $is_ok = $stmt->execute();
  330.             $stmt->bind_result($token, $can_upload, $can_delete, $can_view);
  331.  
  332.             $i = 0;
  333.             while($stmt->fetch()) {
  334.                 $result[$i] = array(
  335.                     'token' => $token,
  336.                     'can_upload' => misc::IsTrue($can_upload),
  337.                     'can_delete' => misc::IsTrue($can_delete),
  338.                     'can_view' => misc::IsTrue($can_view),
  339.                 );
  340.  
  341.                 $i++;
  342.             }
  343.  
  344.             $stmt->close();
  345.         }
  346.         else {
  347.             $is_ok = false;
  348.         }
  349.  
  350.         if($is_ok) {
  351.             if(isset($result)) {
  352.                 return array(
  353.                     'success' => true,
  354.                     'response' => 'success',
  355.                     'data' => $result
  356.                 );
  357.             }
  358.             else {
  359.                 return array(
  360.                     'success' => false,
  361.                     'response' => 'No entries found',
  362.                 );
  363.             }
  364.         }
  365.         else {
  366.             return array(
  367.                 'success' => false,
  368.                 'response' => 'Something went wrong',
  369.             );
  370.         }
  371.     }
  372.  
  373.     /**
  374.     * Gets all information linked with a specific token.
  375.     *
  376.     * @param integer $user_id The owner of the token.
  377.     * @param string $token The token itself.
  378.     */
  379.     public static function GetTokenDetails($user_id, $token)
  380.     {
  381.         /* Checking that the API is enabled*/
  382.         if(!config::GetConfig("IS_API_ENABLED")) {
  383.             return array(
  384.                 'success' => false,
  385.                 'response' => 'API is disabled.'
  386.             );
  387.         }
  388.  
  389.         /* Connecting to mysql */
  390.         if(!sql::Instance()) {
  391.             return array(
  392.                 'success' => false,
  393.                 'response' => 'Failed to initialize connection with mysql database.'
  394.             );
  395.         }
  396.  
  397.         /* Variable to say whether everything wen't okay. */
  398.         $is_ok = true;
  399.  
  400.         /* Getting the information linked with the token */
  401.         if($stmt = sql::Prepare("SELECT `enabled`, `can_upload`, `can_delete`, `can_view` FROM `api_keys` WHERE `user_id` = ? AND `token` = ?")) {
  402.             $stmt->bind_param('is', $user_id, $token);
  403.             $is_ok = $stmt->execute();
  404.             $stmt->store_result();
  405.  
  406.             $stmt->bind_result($enabled, $can_upload, $can_delete, $can_view);
  407.             $stmt->fetch();
  408.  
  409.             if($stmt->num_rows > 0) {
  410.                 $result = array(
  411.                     'is_enabled' => misc::IsTrue($enabled),
  412.                     'can_upload' => misc::IsTrue($can_upload),
  413.                     'can_delete' => misc::IsTrue($can_delete),
  414.                     'can_view' => misc::IsTrue($can_view),
  415.                 );
  416.             }
  417.  
  418.             $stmt->close();
  419.         }
  420.         else {
  421.             $is_ok = false;
  422.         }
  423.  
  424.         if($is_ok) {
  425.             if(isset($result)) {
  426.                 return array(
  427.                     'success' => true,
  428.                     'response' => 'successful',
  429.                     'data' => $result,
  430.                 );
  431.             }
  432.             else {
  433.                 return array(
  434.                     'success' => false,
  435.                     'response' => 'No token found',
  436.                 );
  437.             }
  438.         }
  439.         else {
  440.             return array(
  441.                 'success' => false,
  442.                 'response' => 'Something wen\'t wrong',
  443.             );
  444.         }
  445.     }
  446.  
  447.  
  448.  
  449.  
  450.  
  451.     //
  452.     // All the functions below this are functions that the API will make use of, and
  453.     // all the above functions are functions to authenticate, generate and edit
  454.     // authentication tokens.
  455.     //
  456.  
  457.     /**
  458.     * Uploads a file using the API key.
  459.     *
  460.     * NOTE: The user_id this will be linked this after api::AuthenticateToken has
  461.     *   been invoked
  462.     *
  463.     * @param string $name The name of the file that's being uploaded.
  464.     * @param string $temp_location The temp file that the file's located.
  465.     */
  466.     public static function Upload($name, $temp_location)
  467.     {
  468.         if(!config::GetConfig("IS_API_ENABLED")) {
  469.             return array(
  470.                 'success' => false,
  471.                 'response' => 'API is disabled.'
  472.             );
  473.         }
  474.  
  475.         if(!api::GetVariable("HAS_AUTHENTICATED")) {
  476.             return array(
  477.                 'success' => false,
  478.                 'response' => 'API key has not been authenticated.'
  479.             );
  480.         }
  481.  
  482.         if(!api::GetVariable("IS_ENABLED")) {
  483.             return array(
  484.                 'success' => false,
  485.                 'response' => 'API key is disabled.'
  486.             );
  487.         }
  488.  
  489.         if(!api::GetVariable("CAN_UPLOAD")) {
  490.             return array(
  491.                 'success' => false,
  492.                 'response' => 'API key is unable to upload files.'
  493.             );
  494.         }
  495.  
  496.         if(!sql::Instance()) {
  497.             return array(
  498.                 'success' => false,
  499.                 'response' => 'Failed to initialize connection with mysql database.'
  500.             );
  501.         }
  502.  
  503.         $size = filesize($temp_location);
  504.  
  505.         return uploads::NewUpload(
  506.             api::GetVariable("USER_ID"),
  507.             $name,
  508.             $temp_location,
  509.             $size,
  510.             false
  511.         );
  512.     }
  513. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement