Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. <?php
  2. error_reporting(-1);
  3. ini_set('display_errors', 'On');
  4. require_once '../include/Config.php';
  5. require_once '../include/DbHandler.php';
  6. require_once '../include/AppPassHash.php';
  7. require_once '../include/DBParsers.php';
  8. require '.././libs/Slim/Slim.php';
  9.  
  10. SlimSlim::registerAutoloader();
  11. $app = new SlimSlim();
  12.  
  13. //Slim routes for each db tables
  14. require 'login_router.php';
  15. require 'sync_router.php';
  16. require 'user_router.php';
  17. require 'notes_router.php';
  18.  
  19. // User id from db - Global Variable
  20. $user_id = NULL;
  21.  
  22. function getConnection() {
  23. $host = 'localhost';
  24. $db = 'myproject';
  25. $user = 'myproject';
  26. $pass = 'myproject';
  27. $charset = 'utf8';
  28.  
  29. $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
  30. $opt = [
  31. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  32. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  33. PDO::ATTR_EMULATE_PREPARES => false,
  34. ];
  35. $pdo = new PDO($dsn, DB_USERNAME, DB_PASSWORD, $opt);
  36. return $pdo;
  37. }
  38.  
  39. /**
  40. * Adding Middle Layer to authenticate every request
  41. * Checking if the request has valid api key in the 'Authorization' header
  42. */
  43. function authenticate(SlimRoute $route) {
  44. // Getting request headers
  45. $headers = apache_request_headers();
  46. $response = array();
  47. $app = SlimSlim::getInstance();
  48.  
  49. // Verifying Authorization Header
  50. if (isset($headers['Authorization'])) {
  51. $db = new DbHandler();
  52.  
  53. // get the api key
  54. $api_key = $headers['Authorization'];
  55. // validating api key
  56. if (!$db->isValidApiKey($api_key)) {
  57. // api key is not present in users table
  58. $response["error"] = true;
  59. $response["message"] = "Access Denied. Invalid Api key";
  60. echoRespnse(401, $response);
  61. $app->stop();
  62. } else {
  63. global $user_id;
  64. // get user primary key id
  65. $user_id = $db->getUserId($api_key);
  66. }
  67. } else {
  68. // api key is missing in header
  69. $response["error"] = true;
  70. $response["message"] = "Api key is misssing";
  71. echoRespnse(400, $response);
  72. $app->stop();
  73. }
  74. }
  75.  
  76. /**
  77. * Verifying required params posted or not
  78. */
  79. function verifyRequiredParams($required_fields) {
  80. $error = false;
  81. $error_fields = "";
  82. $request_params = array();
  83. $request_params = $_REQUEST;
  84. // Handling PUT request params
  85. if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
  86. $app = SlimSlim::getInstance();
  87. parse_str($app->request()->getBody(), $request_params);
  88. }
  89. foreach ($required_fields as $field) {
  90. if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
  91. $error = true;
  92. $error_fields .= $field . ', ';
  93. }
  94. }
  95.  
  96. if ($error) {
  97. $response["error"] = true;
  98. $response["message"] = 'Required field empty';
  99. echoRespnse(400, $response);
  100. $app->stop();
  101. }
  102. }
  103.  
  104.  
  105. $app->run();
  106. ?>
  107.  
  108. <form id="loginForm" method="post" action="../MyAPIRoot/v1/login">
  109. <input name="email" type="text" class="form-control input-lg" />
  110. <input name="email" type="text" class="form-control input-lg" />
  111. </form>
  112.  
  113. <script type="text/javascript">
  114. $(document).ready(function(){
  115. $("#loginForm").submit(function(event){
  116. event.preventDefault();
  117. // Serialize the form data.
  118. var form = $('#loginForm');
  119. var formData = $(form).serialize();
  120. //alert("formData: "+formData);
  121. // Submit the form using AJAX.
  122. $.ajax({
  123. type: 'POST',
  124. dataType: 'json',
  125. url: $(form).attr('action'),
  126. data: formData
  127. }).done(function(response) {
  128. alert("Res: ");
  129. // NO RESPONSE HERE
  130. $("#resultMsg").html(response.message);
  131. })
  132. });
  133. });
  134.  
  135. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement