Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. include('../includes/config.php');
  3. include('../paste/includes/libraries/geshi.php');
  4. include('../paste/includes/paste.php');
  5. header('Content-Type: application/json');
  6. $jsonArray = array();
  7. if (isset($_GET['component']))
  8. {
  9. $component = strtolower($_GET['component']);
  10. switch ($component)
  11. {
  12. case 'upload':
  13. if (isset($_GET['action']))
  14. {
  15. $action = strtolower($_GET['action']);
  16. switch ($action)
  17. {
  18. case "post":
  19. $results = upload($_FILES, $CONF, $db);
  20. if (isset($results))
  21. {
  22. if (isset($_POST['get_delete_key']))
  23. {
  24. $name = $results['results']['file']['name'];
  25. $delete_key = generate_code($name, $CONF);
  26. $data = array(
  27. "delete_key" => $delete_key
  28. );
  29. $post_id = $db->update($data, 'uploads', 'url=?', array($name));
  30. $results['results']['file'] = $results['results']['file'] + $data;
  31. }
  32. array_push($jsonArray, $results);
  33. }
  34. else
  35. {
  36. array_push($jsonArray, array('error' => $CONF['errors']['InvRequest']));
  37. }
  38. break;
  39. default:
  40. array_push($jsonArray, array('error' => $CONF['errors']['InvRequest']));
  41. break;
  42. }
  43. }
  44. else
  45. {
  46. array_push($jsonArray, array('error' => $CONF['errors']['InvRequest']));
  47. }
  48. break;
  49. case 'paste':
  50. if (isset($_POST['code']))
  51. {
  52. // Create our pastebin object
  53. $pastebin = new Pastebin($CONF, $db);
  54. /// Clean up older posts
  55. $pastebin->doGarbageCollection();
  56. $id = $pastebin->doPost($_POST);
  57. $post = $pastebin->getPaste($id);
  58. array_push($jsonArray, array('results' =>
  59. array('paste' =>
  60. array(
  61. 'id' => $id,
  62. 'url' => get_page_url("p", $CONF).'/'.$id,
  63. 'title' => $post['title'],
  64. 'format' => $post['format'],
  65. 'expiration' => $post['expires'],
  66. 'password' => $post['password']
  67. )
  68. )
  69. )
  70. );
  71. }
  72. else
  73. {
  74. array_push($jsonArray, array('error' => $CONF['errors']['NoPaste']));
  75. }
  76. break;
  77. case 'ricehalla':
  78. if (isset($_GET['action']))
  79. {
  80. $action = strtolower($_GET['action']);
  81. switch ($action)
  82. {
  83. case "post":
  84. if (isset($_POST['username']))
  85. {
  86. if (isset($_POST['password']))
  87. {
  88. $username = $_POST['username'];
  89. $password = hashPassword($_POST['password'], $CONF);
  90. if ($userTools->login($username, $password, false))
  91. {
  92. $user = unserialize($_SESSION['user']);
  93. $results = upload($_FILES, $CONF, $db);
  94. if (isset($results))
  95. {
  96. $filename = $results['results']['file']['name'];
  97. $file_path = $CONF['upload_dir'] . $filename;
  98. $thumbnail_path = $CONF['upload_dir'] . 'thumbnails/150_150_' . $filename;
  99. $date_added = date("Y-m-d H:i:s",time());
  100. $file_db = $db->select('uploads', "filename=? LIMIT 1", array($filename));
  101. if (file_exists($file_path) && $file_db)
  102. {
  103. $file_type = $file_db['type'];
  104. $pattern = "/^(image)\/(.*)$/";
  105. if(preg_match($pattern, $file_type))
  106. {
  107. $resizeObj = new resize($file_path);
  108. // *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
  109. $resizeObj->resizeImage(150, 150, 'auto');
  110. $resizeObj->saveImage($thumbnail_path, 70);
  111. $data = array(
  112. "url" => $filename,
  113. "user_id" => $user->id,
  114. "date_added" => $date_added
  115. );
  116. $row_id = $db->insert($data, 'ricehalla');
  117. $data = array(
  118. "table_name" => 'ricehalla',
  119. "row_id" => $row_id,
  120. "user_id" => $user->id,
  121. "points" => 1
  122. );
  123. $db->insert($data, 'votes');
  124. array_push($jsonArray, array('image' =>
  125. array(
  126. 'id' => $row_id,
  127. 'url' => get_page_url("ricehalla", $CONF).'/'.$row_id,
  128. 'image_src' => get_page_url("u", $CONF).'/'.$filename,
  129. 'votes' => 1,
  130. 'owner' => $user->username,
  131. 'date_posted' => $date_added,
  132. 'tags' => array()
  133. )
  134. )
  135. );
  136. }
  137. else
  138. {
  139. array_push($jsonArray, array('error' => $CONF['errors']['InvFile']));
  140. }
  141. }
  142. else
  143. {
  144. array_push($jsonArray, array('error' => $CONF['errors']['NoFile']));
  145. }
  146. }
  147. else
  148. {
  149. array_push($jsonArray, array('error' => $CONF['errors']['InvRequest']));
  150. }
  151. }
  152. else
  153. {
  154. array_push($jsonArray, array('error' => $CONF['errors']['InvCred']));
  155. }
  156. }
  157. else
  158. {
  159. array_push($jsonArray, array('error' => $CONF['errors']['NoPass']));
  160. }
  161. }
  162. else
  163. {
  164. array_push($jsonArray, array('error' => $CONF['errors']['NoUser']));
  165. }
  166. break;
  167. case "get":
  168. $filter = "votes.table_name=?";
  169. $filter_content = array("ricehalla");
  170. $order_by = "TotalRank";
  171. $order = "DESC";
  172. $limit = "";
  173. if (isset($_POST['id']))
  174. {
  175. $filter .= " AND ricehalla.id=?";
  176. array_push($filter_content, $_POST['id']);
  177. }
  178. if (isset($_POST['owner']))
  179. {
  180. $user_id = $userTools->getUser($_POST['owner'])->id;
  181. $filter .= " AND ricehalla.user_id=?";
  182. array_push($filter_content, $user_id);
  183. }
  184. if (isset($_POST['order']))
  185. {
  186. if (strtolower($_POST['order']) == "asc")
  187. {
  188. $order = "ASC";
  189. }
  190. }
  191. if (isset($_POST['order_by']))
  192. {
  193. switch ($_POST['order_by'])
  194. {
  195. case 'id':
  196. $order_by = "ricehalla.id";
  197. break;
  198. case 'owner':
  199. $order_by = "ricehalla.user_id";
  200. break;
  201. case 'date':
  202. $order_by = "ricehalla.date_added";
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. if (isset($_POST['limit']))
  209. {
  210. if (is_numeric($_POST['limit']))
  211. {
  212. $limit = " LIMIT ".$_POST['limit'];
  213. }
  214. }
  215. $Results = $db->select_raw('ricehalla', "INNER JOIN votes ON ricehalla.id=votes.row_id WHERE ".$filter." GROUP BY votes.row_id ORDER BY ".$order_by." ".$order.$limit, $filter_content, 'ricehalla.url, ricehalla.user_id, ricehalla.id, ricehalla.tags, votes.points, sum(votes.points) TotalPoints, COUNT(votes.id) as TotalVotes, (sum(votes.points) / COUNT(votes.id)) * abs(sum(votes.points)) as TotalRank, ricehalla.date_added');
  216. if ($Results)
  217. {
  218. $result_list = array();
  219. foreach ($Results as $result)
  220. {
  221. if (!is_array($result))
  222. {
  223. $result_list = array($Results);
  224. break;
  225. }
  226. array_push($result_list, $result);
  227. }
  228. // Generate Ranking List
  229. $rankResults = $db->select_raw('ricehalla', "INNER JOIN votes ON ricehalla.id=votes.row_id WHERE votes.table_name=? GROUP BY votes.row_id ORDER BY TotalRank DESC, TotalVotes DESC, TotalPoints DESC", array("ricehalla"), 'ricehalla.id, sum(votes.points) as TotalPoints, COUNT(votes.id) as TotalVotes, (sum(votes.points) / COUNT(votes.id)) * abs(sum(votes.points)) as TotalRank');
  230. $rank_list = array();
  231. foreach ($rankResults as $rank_result)
  232. {
  233. if (!is_array($rank_result))
  234. {
  235. $result_list = array($rankResults);
  236. break;
  237. }
  238. array_push($rank_list, $rank_result);
  239. }
  240. $result_array = array();
  241. // Generate object for each result
  242. foreach ($result_list as $result)
  243. {
  244. $id = $result['id'];
  245. $rank = multi_array_search($rank_list, array('id' => $id))[0] + 1;
  246. $username = $userTools->get($result['user_id'])->username;
  247. $image_src = $result['url'];
  248. $date_posted = $result['date_added'];
  249. $user_vote = $result['TotalPoints'];
  250. $tags = explode(',', $result['tags']);
  251. array_push($result_array, array('image' =>
  252. array(
  253. 'id' => $id,
  254. 'url' => get_page_url("ricehalla", $CONF).'/'.$id,
  255. 'image_src' => get_page_url("u", $CONF).'/'.$image_src,
  256. 'rank' => $rank,
  257. 'votes' => $user_vote,
  258. 'owner' => $username,
  259. 'date_posted' => $date_posted,
  260. 'tags' => $tags
  261. )
  262. )
  263. );
  264. }
  265. array_push($jsonArray, array('results' => $result_array));
  266. }
  267. else
  268. {
  269. array_push($jsonArray, array('error' => $CONF['errors']['NoImages']));
  270. }
  271. break;
  272. default:
  273. array_push($jsonArray, array('error' => $CONF['errors']['InvRequest']));
  274. break;
  275. }
  276. }
  277. else
  278. {
  279. array_push($jsonArray, array('error' => $CONF['errors']['InvRequest']));
  280. }
  281. break;
  282. default:
  283. array_push($jsonArray, array('error' => $CONF['errors']['InvRequest']));
  284. break;
  285. }
  286. }
  287. else
  288. {
  289. array_push($jsonArray, array('error' => $CONF['errors']['InvRequest']));
  290. }
  291. echo json_encode($jsonArray);
  292. ?>