/htdocs/application/controllers/Api.php

https://gitlab.com/billyprice1/Stikked · PHP · 198 lines · 154 code · 28 blank · 16 comment · 21 complexity · 8b7ce7a35db6873c427c8f593e41109c MD5 · raw file

  1. <?php
  2. /**
  3. * Class and Function List:
  4. * Function list:
  5. * - __construct()
  6. * - index()
  7. * - create()
  8. * - paste()
  9. * - random()
  10. * - recent()
  11. * - trending()
  12. * - langs()
  13. * Classes list:
  14. * - Api extends Main
  15. */
  16. include_once ('application/controllers/Main.php');
  17. class Api extends Main
  18. {
  19. function __construct()
  20. {
  21. parent::__construct();
  22. if (config_item('disable_api'))
  23. {
  24. die("The API has been disabled\n");
  25. }
  26. }
  27. function index()
  28. {
  29. $languages = $this->languages->get_languages();
  30. $languages = array_keys($languages);
  31. $languages = implode(', ', $languages);
  32. $data['languages'] = $languages;
  33. $this->load->view('api_help', $data);
  34. }
  35. function create()
  36. {
  37. if (config_item('apikey') != $this->input->get('apikey'))
  38. {
  39. die("Invalid API key\n");
  40. }
  41. $this->load->model('pastes');
  42. $this->load->library('form_validation'); //needed by parent class
  43. if (!$this->input->post('text'))
  44. {
  45. $data['msg'] = 'Error: Missing paste text';
  46. $this->load->view('view/api', $data);
  47. }
  48. else
  49. {
  50. if (!$this->input->post('lang'))
  51. {
  52. $_POST['lang'] = 'text';
  53. }
  54. $_POST['code'] = $this->input->post('text');
  55. if ($this->config->item('private_only'))
  56. {
  57. $_POST['private'] = 1;
  58. }
  59. //validations
  60. if (!$this->_valid_ip())
  61. {
  62. die("You are not allowed to paste\n");
  63. }
  64. if (!$this->_blockwords_check())
  65. {
  66. die("Your paste contains blocked words\n");
  67. }
  68. //create paste
  69. $paste_url = $this->pastes->createPaste();
  70. $data['msg'] = base_url() . $paste_url;
  71. $this->load->view('view/api', $data);
  72. }
  73. }
  74. function paste()
  75. {
  76. if (config_item('apikey') != $this->input->get('apikey'))
  77. {
  78. die("Invalid API key\n");
  79. }
  80. if (config_item('private_only'))
  81. {
  82. show_404();
  83. }
  84. $this->load->model('pastes');
  85. $check = $this->pastes->checkPaste(3);
  86. if ($check)
  87. {
  88. $data = $this->pastes->getPaste(3);
  89. }
  90. else
  91. {
  92. $data = array(
  93. 'message' => 'Not found',
  94. );
  95. }
  96. echo json_encode($data);
  97. }
  98. function random()
  99. {
  100. if (config_item('apikey') != $this->input->get('apikey'))
  101. {
  102. die("Invalid API key\n");
  103. }
  104. if (config_item('private_only'))
  105. {
  106. show_404();
  107. }
  108. $this->load->model('pastes');
  109. $data = $this->pastes->random_paste();
  110. echo json_encode($data);
  111. }
  112. function recent()
  113. {
  114. if (config_item('apikey') != $this->input->get('apikey'))
  115. {
  116. die("Invalid API key\n");
  117. }
  118. if (config_item('private_only'))
  119. {
  120. show_404();
  121. }
  122. $this->load->model('pastes');
  123. $pastes = $this->pastes->getLists('api/recent');
  124. $pastes = $pastes['pastes'];
  125. $data = array();
  126. foreach ($pastes as $paste)
  127. {
  128. $data[] = array(
  129. 'pid' => $paste['pid'],
  130. 'title' => $paste['title'],
  131. 'name' => $paste['name'],
  132. 'created' => $paste['created'],
  133. 'lang' => $paste['lang'],
  134. );
  135. }
  136. echo json_encode($data);
  137. }
  138. function trending()
  139. {
  140. if (config_item('apikey') != $this->input->get('apikey'))
  141. {
  142. die("Invalid API key\n");
  143. }
  144. if (config_item('private_only'))
  145. {
  146. show_404();
  147. }
  148. $this->load->model('pastes');
  149. $pastes = $this->pastes->getTrends('api/trending', 2);
  150. $pastes = $pastes['pastes'];
  151. $data = array();
  152. foreach ($pastes as $paste)
  153. {
  154. $data[] = array(
  155. 'pid' => $paste['pid'],
  156. 'title' => $paste['title'],
  157. 'name' => $paste['name'],
  158. 'created' => $paste['created'],
  159. 'lang' => $paste['lang'],
  160. 'hits' => $paste['hits'],
  161. );
  162. }
  163. echo json_encode($data);
  164. }
  165. function langs()
  166. {
  167. $languages = $this->languages->get_languages();
  168. echo json_encode($languages);
  169. }
  170. }