Old Keyo.co API


SUBMITTED BY: Guest

DATE: April 9, 2013, 2:23 p.m.

FORMAT: PHP

SIZE: 14.0 kB

HITS: 1276

  1. <?php
  2. /* This should not be used in production! */
  3. //ini_set('display_errors', 1);
  4. //error_reporting(E_ALL);
  5. include 'includes/functions.php';
  6. /* Simplified API Version 4 ~ no crap */
  7. /* Declare our API Classes */
  8. $api = new apiFunctions();
  9. $error = new errorReport();
  10. define('ADDR', 'http://keyo.co'); //Main Website
  11. define('STRE', 'keyUploads'); //Where to store the files
  12. $whiteList = array('jpg','png','gif','jpeg','txt','zip','rar','doc','xls','md'); //Extension whitelist
  13. define('HOST', 'localhost'); //Database Host
  14. define('USER', 'root'); //Database User
  15. define('NAME', 'KeyoApp'); //Database Name
  16. define('PASS', ''); //Database Pass
  17. mysql_connect(HOST, USER, PASS) or die("MySQL Error: " . mysql_error());
  18. mysql_select_db(NAME) or die("MySQL Error: " . mysql_error());
  19. //Make the URLs cleaner
  20. $getParams = array();
  21. $getParts = explode('/', $_SERVER['REQUEST_URI']);
  22. //Skip through the segments by 2
  23. for($i = 0; $i < count($getParts); $i = $i + 2){
  24. //First segment is the param name, second is the value
  25. $getParams[$getParts[$i]] = $getParts[$i+1];
  26. }
  27. /* Make it work with all existing code */
  28. $_GET = $getParams;
  29. /* Collect API credentials if they are there */
  30. if (!empty($_GET['user']) && !empty($_GET['key']))
  31. {
  32. $apiUser = $api->simpleSanitize($_GET['user']);
  33. $apiKey = $_GET['key'];
  34. $getUser = mysql_query("SELECT * FROM users WHERE Username = '$apiUser'");
  35. $userExist = mysql_num_rows($getUser);
  36. /* Include the graphing code, a lot cleaner! */
  37. include 'graph.php';
  38. /* User doesn't exist in the database */
  39. if ($userExist == 0)
  40. {
  41. $noExist = $error->returnError(1);
  42. echo $noExist;
  43. die;
  44. }
  45. /* Get all the users info */
  46. $userInfo = mysql_fetch_assoc($getUser);
  47. /* See what their API key should be */
  48. $checkAPI = $api->apiKey($apiUser, $userInfo['UserID']);
  49. $oldAPI = $api->apiSuperseded($apiUser);
  50. /* API is not correct, try the old key */
  51. if ($apiKey != $checkAPI)
  52. {
  53. if ($apiKey != $oldAPI)
  54. {
  55. /* API key is still not correct, return an error */
  56. $keyCheck = $error->returnError(2);
  57. echo $keyCheck;
  58. die;
  59. }
  60. }
  61. /* Just for debugging, nothing important in here */
  62. if (!empty($_GET['debug']))
  63. {
  64. echo 'Currently using: ' . $apiKey . '<br />';
  65. echo 'Old API: ' . $oldAPI . '<br />';
  66. echo 'New API: ' . $checkAPI . '<br />';
  67. die;
  68. }
  69. /* Check for login, with the old key too! */
  70. if (!empty($_GET['login']))
  71. {
  72. if ($apiKey == $checkAPI || $apiKey == $oldAPI)
  73. {
  74. echo 'Correct';
  75. die;
  76. }
  77. else
  78. {
  79. echo 'Incorrect';
  80. die;
  81. }
  82. }
  83. /* Getting the views of a file */
  84. if (!empty($_GET['views']))
  85. {
  86. $file = $api->simpleSanitize($_GET['views']);
  87. $views = $api->getViews($file);
  88. echo $views;
  89. die;
  90. }
  91. /* Come here to remove an image */
  92. if (!empty($_GET['remove']))
  93. {
  94. $file = $api->simpleSanitize($_GET['remove']);
  95. $remove = $api->removeFile($apiUser, $file);
  96. echo $remove;
  97. die;
  98. }
  99. /* Adding a new category */
  100. if (!empty($_GET['add']) && !empty($_GET['category']))
  101. {
  102. $file = $api->simpleSanitize($_GET['add']);
  103. $fileExist = $api->fileExists($file, $apiUser);
  104. if ($fileExist)
  105. {
  106. $cat = $api->simpleSanitize($_GET['category']);
  107. $catExist = $api->catExists($cat, $apiUser);
  108. if ($catExist)
  109. {
  110. $sqlMain = mysql_query("UPDATE files SET category = '$cat' WHERE newfilename='$file' AND user='$apiUser'");
  111. echo 'File ' . $file . ' is now in the ' . $cat . ' category.';
  112. die;
  113. }
  114. else
  115. {
  116. $catCheck = $error->returnError(3);
  117. echo $catCheck;
  118. die;
  119. }
  120. }
  121. else
  122. {
  123. $fileCheck = $error->returnError(4);
  124. echo $fileCheck;
  125. die;
  126. }
  127. }
  128. if (empty($_FILES))
  129. {
  130. $postCheck = $error->returnError(5);
  131. echo $postCheck;
  132. die;
  133. }
  134. else
  135. {
  136. /* Get the time and date */
  137. $theDateTime = date('Y-m-d H:i:s');
  138. /* Just the date */
  139. $theDate = date('Y-m-d');
  140. $YearMonthDay = explode("-", $theDate);
  141. $theTime = date('H:i:s');
  142. $HourMinuteSecond = explode(":", $theTime);
  143. //IP Address
  144. $ipAddress = $_SERVER['REMOTE_ADDR'];
  145. /* The old files name */
  146. $oldFile = basename($_FILES['file']['name']);
  147. if (empty($oldFile))
  148. {
  149. $oldFile = basename($_FILES['uploaded']['name']);
  150. $android = 1;
  151. }
  152. /* Gets the extension */
  153. $getExt = pathinfo($oldFile);
  154. $checkExt = strtolower($getExt['extension']);
  155. /* This checks what type of file it is, and provides the correct link (hopefully) */
  156. $l = $api->getLink($checkExt);
  157. /* Check if the files extension is okay */
  158. if (!in_array($checkExt, $whiteList))
  159. {
  160. $typeCheck = $error->returnError(6);
  161. echo $typeCheck;
  162. die;
  163. }
  164. /* Check the size of the file */
  165. $fileSize = $_SERVER['CONTENT_LENGTH'];
  166. $sizeKB = $fileSize / 1024; //In KB
  167. /* Using MB */
  168. if ($sizeKB > 999)
  169. {
  170. $sizeKB = $sizeKB / 1024; //mb
  171. $sizeType = ' MB';
  172. if ($sizeFinalBlank > 10.0)
  173. {
  174. $sizeCheck = $error->returnError(7);
  175. echo $sizeCheck;
  176. die;
  177. }
  178. }
  179. else
  180. {
  181. $sizeType = ' KB';
  182. }
  183. /* Round the file up to 2 decimals */
  184. $sizeFinalBlank = round($sizeKB, 2);
  185. $sizeFinal = round($sizeKB, 2) . $sizeType;
  186. /* Create the new file name */
  187. $newFile = stripslashes($api->fileName() . "." . $checkExt);
  188. /* Check if the file exists, rename it if it does (needs work) */
  189. if (file_exists($newFile))
  190. {
  191. $newFile = stripslashes($api->fileName() . "." . $checkExt);
  192. }
  193. /* Full Upload Path */
  194. $fullUpload = STRE . '/' . $newFile;
  195. /* The current time */
  196. $theTimeStamp = time();
  197. /* Is it being uploaded through the app? */
  198. if (!$android)
  199. {
  200. if (is_uploaded_file($_FILES['file']['tmp_name']))
  201. {
  202. if (move_uploaded_file($_FILES['file']['tmp_name'], $fullUpload))
  203. {
  204. //Main SQL
  205. $sqlMain = mysql_query("INSERT INTO files (origfilename, newfilename, uploaderip, size, user, timedate, time, date, timestamp) VALUES ('$oldFile', '$newFile', '$ipAddress', '$sizeFinal', '$apiUser', '$theDateTime', '$theTime', '$theDate', '$theTimeStamp')");
  206. mysql_close();
  207. echo "http://keyo.co/?" . $l . "=" . $newFile;
  208. }
  209. }
  210. }
  211. else
  212. {
  213. if (is_uploaded_file($_FILES['uploaded']['tmp_name']))
  214. {
  215. if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $fullUpload))
  216. {
  217. //Main SQL
  218. $sqlMain = mysql_query("INSERT INTO files (origfilename, newfilename, uploaderip, size, user, timedate, time, date, timestamp) VALUES ('$oldFile', '$newFile', '$ipAddress', '$sizeFinal', '$apiUser', '$theDateTime', '$theTime', '$theDate', '$theTimeStamp')");
  219. mysql_close();
  220. echo "http://keyo.co/?" . $l . "=" . $newFile;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. class apiFunctions
  227. {
  228. function apiKey($keyoUser, $keyoID)
  229. {
  230. $uniqueSalt = sha1('PKoj7YgVBHV)(!!jk?/<');
  231. $keyoHash = sha1($keyoUser . $uniqueSalt . $keyoID);
  232. for ($i = 0; $i < 20; $i++) {
  233. $keyoHash = sha1($keyoHash);
  234. }
  235. return sha1($keyoUser . $keyoHash . $uniqueSalt . $keyoID);
  236. }
  237. function apiSuperseded($apiName)
  238. {
  239. $hashValue = "sQfYKDTXvOApaFYC";
  240. $apiChecked = sha1($apiName . $hashValue);
  241. return $apiChecked;
  242. }
  243. function fileName($nameLength = 5, $charChoice = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-$_.!(),')
  244. {
  245. $numChars = strlen($charChoice);
  246. $returnFile = '';
  247. for($i = 0; $i < $nameLength; ++$i)
  248. {
  249. $returnFile .= $charChoice[mt_rand(0, $numChars)];
  250. }
  251. return $returnFile;
  252. }
  253. function fileExists($file, $username)
  254. {
  255. $getFile = mysql_query("SELECT * FROM files WHERE newfilename = '$file' AND user = '$username'");
  256. $fileExist = mysql_num_rows($getFile);
  257. /* File doesn't exist in the database */
  258. if ($fileExist == 0)
  259. {
  260. return 0;
  261. }
  262. else
  263. {
  264. return 1;
  265. }
  266. }
  267. function catExists($cat, $username)
  268. {
  269. $getCat = mysql_query("SELECT * FROM categories WHERE name = '$cat' AND username = '$username'");
  270. $catExist = mysql_num_rows($getCat);
  271. /* Category doesn't exist in the database */
  272. if ($catExist == 0)
  273. {
  274. return 0;
  275. }
  276. else
  277. {
  278. return 1;
  279. }
  280. }
  281. function getLink($ext)
  282. {
  283. if ($ext == 'jpg' || $ext == 'png' || $ext == 'gif' || $ext == 'jpeg')
  284. {
  285. return 'i';
  286. }
  287. elseif ($ext == 'txt' || $ext == 'md')
  288. {
  289. return 't';
  290. }
  291. elseif ($ext == 'zip' || $ext == 'rar')
  292. {
  293. return 'c';
  294. }
  295. elseif ($ext == 'doc' || $ext == 'xls')
  296. {
  297. return 'd';
  298. }
  299. }
  300. function apiRequestsCount($apiUsername, $uploadsPerMin)
  301. {
  302. $callTime = time();
  303. $timeMinusMin = $callTime - 60;
  304. $getmyInfo = mysql_query("SELECT *, COUNT(newfilename) FROM files WHERE user = '$apiUsername' AND timestamp BETWEEN $timeMinusMin AND $callTime");
  305. $myInfo = mysql_fetch_assoc($getmyInfo);
  306. $myInfo = $myInfo['COUNT(newfilename)'];
  307. if ($myInfo <= $uploadsPerMin)
  308. {
  309. $apiStatus = 1;
  310. }
  311. else
  312. {
  313. $apiStatus = 0;
  314. }
  315. return $apiStatus;
  316. }
  317. function upgradeUser($upgradeUser, $userLevel)
  318. {
  319. $upgradeQuery = mysql_query("SELECT * FROM users WHERE Username = '$upgradeUser'");
  320. $upgradeDetails = mysql_fetch_assoc($upgradeQuery);
  321. $userExist = mysql_num_rows($upgradeQuery);
  322. if ($userExist == 0)
  323. {
  324. $upgradeResult = 'The user does not exist';
  325. }
  326. else
  327. {
  328. if ($upgradeDetails['AccountType'] > 1)
  329. {
  330. $upgradeResult = $upgradeUser . ' is already premium';
  331. }
  332. else
  333. {
  334. $finishUpgrade = mysql_query("UPDATE users SET AccountType='$userLevel' WHERE Username='$upgradeUser'");
  335. $upgradeResult = $upgradeUser . ' has been upgraded';
  336. }
  337. }
  338. return $upgradeResult;
  339. }
  340. function getViews($viewedFile)
  341. {
  342. $apiViews = mysql_query("SELECT * FROM files WHERE newfilename = '$viewedFile'");
  343. $viewsResult = mysql_fetch_assoc($apiViews);
  344. $viewsResult = $viewsResult['views'];
  345. return $viewsResult;
  346. }
  347. function removeFile($userCheck, $removedFile)
  348. {
  349. $apiRemove = mysql_query("SELECT * FROM files WHERE newfilename = '$removedFile'");
  350. $removeResult = mysql_fetch_assoc($apiRemove);
  351. $removeUser = $removeResult['user'];
  352. $removeCheck = mysql_num_rows($apiRemove); //Check if it exists
  353. if ($removeCheck == 0)
  354. {
  355. $remResult = "Doesn't exist";
  356. return $remResult;
  357. }
  358. if ($userCheck != $removeUser)
  359. {
  360. $remResult = "You do not own this image";
  361. return $remResult;
  362. }
  363. //Use this for the account page, so it forwards you back to the correct page!
  364. if ($_GET['forward'] == 'true')
  365. {
  366. $pastPage = $_GET['page'];
  367. $deleteMe = mysql_query("DELETE FROM files WHERE newfilename='$removedFile'");
  368. unlink("keyUploads/" . $removedFile);
  369. header("Location: http://keyo.co/gallery/page/$pastPage");
  370. }
  371. else
  372. {
  373. //Removing files from the api only, not the account page!
  374. $deleteMe = mysql_query("DELETE FROM files WHERE newfilename='$removedFile'");
  375. unlink("keyUploads/" . $removedFile);
  376. $remResult = "Success";
  377. return $remResult;
  378. }
  379. }
  380. function simpleSanitize($inputData)
  381. {
  382. $inputData = strip_tags(mysql_real_escape_string($inputData));
  383. return $inputData;
  384. }
  385. }
  386. class errorReport
  387. {
  388. function returnError($i)
  389. {
  390. switch ($i)
  391. {
  392. case 0:
  393. $msg = "No credentials given";
  394. return $msg;
  395. break;
  396. case 1:
  397. $msg = "User does not exist";
  398. return $msg;
  399. break;
  400. case 2:
  401. $msg = "Incorrect API credentials";
  402. return $msg;
  403. break;
  404. case 3:
  405. $msg = "Category does not exist";
  406. return $msg;
  407. break;
  408. case 4:
  409. $msg = "File does not exist";
  410. return $msg;
  411. break;
  412. case 5:
  413. $msg = "A POSTed file is needed";
  414. return $msg;
  415. break;
  416. case 6:
  417. $msg = "This file type is not allowed";
  418. return $msg;
  419. break;
  420. case 7:
  421. $msg = "This file is too large";
  422. return $msg;
  423. break;
  424. case 8:
  425. $msg = "";
  426. return $msg;
  427. break;
  428. case 9:
  429. $msg = "";
  430. return $msg;
  431. break;
  432. case 10:
  433. $msg = "";
  434. return $msg;
  435. break;
  436. }
  437. }
  438. }

comments powered by Disqus