Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 21.89 KB | None | 0 0
  1. <?php
  2.  
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5.  
  6. header("Access-Control-Allow-Origin: *");
  7.  
  8. // Routes
  9.  
  10. $app->get('/', function (Request $request, Response $response, array $args) {
  11.     // Sample log message
  12.     $this->logger->info("Slim-Skeleton '/' route");
  13.  
  14.     // Render index view
  15.     return $this->renderer->render($response, 'index.phtml', $args);
  16. });
  17.  
  18. // Get data from MySQLi
  19. $app->get('/users', function(){
  20.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  21.     $query = "SELECT * FROM users ORDER BY id";
  22.     $result = $mysqli->query($query) or die($mysqli->error);
  23.  
  24.     while($row = $result->fetch_assoc()) {
  25.         $data[] = $row;
  26.         // echo "number of rows: " . $result->num_rows;
  27.     }
  28.  
  29.     if (isset($data)) {
  30.         header('Content-Type: application/json');
  31.         // echo '{"users": ' . json_encode($data) . '}';
  32.         echo '{"users": ' . json_encode($data, JSON_UNESCAPED_UNICODE) . '}';
  33.     }
  34. });
  35.  
  36. $app->get('/singleUser', function(Request $request, Response $response){
  37.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  38.    
  39.     $userid = $request->getParam('userid');
  40.  
  41.     // try it!
  42.     try {
  43.         // Might need to add some security to this..
  44.         // make query string
  45.         $sql = "SELECT * FROM users WHERE id =:userid";
  46.        
  47.         $stmt = $pdo->prepare($sql); // chaining SQL statements..
  48.  
  49.         $stmt->bindParam("userid", $userid);
  50.  
  51.         $stmt->execute();
  52.  
  53.         $userData = $stmt->fetch(PDO::FETCH_OBJ);
  54.        
  55.         return $response->withJson(['response' => 'success', 'userData' => $userData], 200);
  56.     }
  57.     // did we get any errors?
  58.     catch (PDOException $e) {
  59.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  60.     }
  61. });
  62.  
  63. $app->get('/singleCustomer', function(Request $request, Response $response){
  64.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  65.    
  66.     $koncern = $request->getParam('koncern');
  67.  
  68.     // try it!
  69.     try {
  70.         // Might need to add some security to this..
  71.         // make query string
  72.         $sql = "SELECT Kund_id FROM customers WHERE koncern =:koncern";
  73.        
  74.         $stmt = $pdo->prepare($sql); // chaining SQL statements..
  75.  
  76.         $stmt->bindParam("koncern", $koncern);
  77.  
  78.         $stmt->execute();
  79.  
  80.         $koncernData = $stmt->fetch(PDO::FETCH_OBJ);
  81.        
  82.         return $response->withJson(['response' => 'success', 'koncernData' => $koncernData], 200);
  83.     }
  84.     // did we get any errors?
  85.     catch (PDOException $e) {
  86.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  87.     }
  88. });
  89.  
  90. $app->get('/singleInsurance', function(Request $request, Response $response){
  91.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  92.    
  93.     $insuranceid = $request->getParam('insuranceid');
  94.  
  95.     // try it!
  96.     try {
  97.         // Might need to add some security to this..
  98.         // make query string
  99.         $sql = "SELECT * FROM insurances WHERE id =:insuranceid";
  100.        
  101.         $stmt = $pdo->prepare($sql); // chaining SQL statements..
  102.  
  103.         $stmt->bindParam("insuranceid", $insuranceid);
  104.  
  105.         $stmt->execute();
  106.  
  107.         $insuranceData = $stmt->fetch(PDO::FETCH_OBJ);
  108.        
  109.         return $response->withJson(['response' => 'success', 'insuranceData' => $insuranceData], 200);
  110.     }
  111.     // did we get any errors?
  112.     catch (PDOException $e) {
  113.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  114.     }
  115. });
  116.  
  117. // $data = $request->getParam('paramName');
  118. //$data = $request->getParams(); // if you want to get all params
  119.  
  120. // Get data from MySQL with PDO
  121. $app->post('/addUser', function(Request $request, Response $response){
  122.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  123.    
  124.     // get username and password
  125.     $username = $request->getParam('username');
  126.     $password = password_hash($request->getParam('password'), PASSWORD_DEFAULT);
  127.     $firstname = $request->getParam('firstname');
  128.     $lastname = $request->getParam('lastname');
  129.     $email = $request->getParam('email');
  130.     $role = $request->getParam('role');
  131.  
  132.     // try it!
  133.     try {
  134.         // Might need to add some security to this..
  135.         // make query string
  136.         $sql = "INSERT INTO users (username, password, firstname, lastname, email, role) VALUES (?,?,?,?,?,?)";
  137.         $pdo->prepare($sql)->execute([$username, $password, $firstname, $lastname, $email, $role]); // chaining SQL statements..
  138.  
  139.         return $response->withJson(['response' => 'success', 'message' => 'Added user to database'], 200);
  140.     }
  141.     // did we get any errors?
  142.     catch (PDOException $e) {
  143.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  144.     }
  145. });
  146.  
  147. $app->post('/editUser', function(Request $request, Response $response){
  148.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  149.    
  150.     // get username and password
  151.     $username = $request->getParam('username');
  152.     $password = password_hash($request->getParam('password'), PASSWORD_DEFAULT);
  153.     $firstname = $request->getParam('firstname');
  154.     $lastname = $request->getParam('lastname');
  155.     $email = $request->getParam('email');
  156.     $userid = $request->getParam('userid');
  157.     $role = $request->getParam('role');
  158.  
  159.     // try it!
  160.     try {
  161.         // Might need to add some security to this..
  162.         // make query string
  163.         $sql = "UPDATE users SET username = ?, password = ?, firstname = ?, lastname = ?, email = ?, role = ? WHERE id = ?";
  164.         $pdo->prepare($sql)->execute([$username, $password, $firstname, $lastname, $email, $role, $userid]); // chaining SQL statements..
  165.  
  166.         return $response->withJson(['response' => 'success', 'message' => 'Updated user data'], 200);
  167.     }
  168.     // did we get any errors?
  169.     catch (PDOException $e) {
  170.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  171.     }
  172. });
  173.  
  174. $app->post('/removeUser', function(Request $request, Response $response){
  175.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  176.    
  177.     // get username and password
  178.     $userid = $request->getParam('userid');
  179.    
  180.     if($userid == 20) {
  181.         return $response->withJson(['response' => 'failed', 'message' => 'User cannot be removed'], 200);
  182.     }
  183.  
  184.     // try it!
  185.     try {
  186.         // make query string
  187.         $sql = "DELETE FROM users WHERE id = ?";
  188.         $pdo->prepare($sql)->execute([$userid]); // chaining SQL statements..
  189.  
  190.         return $response->withJson(['response' => 'success', 'message' => 'User removed'], 200);
  191.     }
  192.     // did we get any errors?
  193.     catch (PDOException $e) {
  194.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  195.     }
  196. });
  197.  
  198. // Get data from MySQL with PDO
  199. $app->post('/login', function(Request $request, Response $response){
  200.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  201.     // get username and password
  202.     $username = $request->getParam('username');
  203.     $password = $request->getParam('password');
  204.    
  205.     // try it!
  206.     try {
  207.         // Might need to add some security to this..
  208.         // make query string
  209.         $sql = "SELECT id, firstname, lastname, email, username, password, role FROM users WHERE (username=:username or email=:username)";
  210.        
  211.         $stmt = $pdo->prepare($sql);
  212.  
  213.         $stmt->bindParam("username", $username, PDO::PARAM_STR);
  214.        
  215.         $stmt->execute();
  216.  
  217.         $mainCount = $stmt->rowCount();
  218.  
  219.         if($mainCount == 0) {
  220.             return $response->withJson(['response' => 'failed', 'message' => 'Fel användarnamn eller lösenord'], 200);
  221.         }
  222.         $userData = $stmt->fetch(PDO::FETCH_OBJ);
  223.  
  224.         if(!empty($userData) && password_verify($password, $userData->password)) {
  225.             return $response->withJson(['response' => 'success', 'userData' => $userData], 200);
  226.         }
  227.         else {
  228.             return $response->withJson(['response' => 'failed', 'message' => 'Fel användarnamn eller lösenord'], 200);
  229.         }
  230.     }
  231.     // did we get any errors?
  232.     catch (PDOException $e) {
  233.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  234.     }
  235. });
  236.  
  237. // Get data from MySQLi
  238. $app->get('/insurances', function(){
  239.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  240.     $query = "SELECT * FROM insurances INNER JOIN customers ON insurances.Kund_id=customers.Kund_id ORDER BY Koncern";
  241.     $result = $mysqli->query($query) or die($mysqli->error);
  242.  
  243.     while($row = $result->fetch_assoc()) {
  244.         $data[] = $row;
  245.     }
  246.  
  247.     if (isset($data)) {
  248.         header('Content-Type: application/json');
  249.  
  250.         echo '{"insurances": ' . json_encode($data, JSON_UNESCAPED_UNICODE) . '}';
  251.     }
  252. });
  253.  
  254. // Get data from MySQLi
  255. $app->get('/customers', function(){
  256.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  257.     $query = "SELECT * FROM customers ORDER BY Koncern";
  258.     $result = $mysqli->query($query) or die($mysqli->error);
  259.  
  260.     while($row = $result->fetch_assoc()) {
  261.         $data[] = $row;
  262.     }
  263.  
  264.     if (isset($data)) {
  265.         header('Content-Type: application/json');
  266.  
  267.         echo '{"customers": ' . json_encode($data, JSON_UNESCAPED_UNICODE) . '}';
  268.     }
  269. });
  270.  
  271. // Get data from MySQL with PDO
  272. $app->get('/getcustomersdata', function(Request $request, Response $response){
  273.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  274.  
  275.     try {
  276.         $sql = "SELECT * FROM customers GROUP BY Koncern";
  277.        
  278.         $stmt = $pdo->prepare($sql);
  279.  
  280.         $stmt->execute();
  281.  
  282.         $mainCount = $stmt->rowCount();
  283.  
  284.         if($mainCount == 0) {
  285.             return $response->withJson(['response' => 'failed', 'message' => 'Ingen datas'], 200);
  286.         }
  287.  
  288.         $customersData = $stmt->fetchAll(PDO::FETCH_OBJ);
  289.  
  290.         if(!empty($customersData)) {
  291.             return $response->withJson(['response' => 'success', 'customersData' => $customersData], 200);
  292.         }
  293.         else {
  294.             return $response->withJson(['response' => 'failed', 'message' => 'Ingen data'], 200);
  295.         }
  296.     }
  297.     // did we get any errors?
  298.     catch (PDOException $e) {
  299.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  300.     }
  301. });
  302.  
  303. // Get data from MySQLi
  304. $app->get('/responsible', function(){
  305.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  306.     $query = "SELECT Kundansvarig FROM insurances GROUP BY Kundansvarig";
  307.     $result = $mysqli->query($query) or die($mysqli->error);
  308.  
  309.     while($row = $result->fetch_assoc()) {
  310.         $data[] = $row;
  311.         // echo "number of rows: " . $result->num_rows;
  312.     }
  313.  
  314.     if (isset($data)) {
  315.         header('Content-Type: application/json');
  316.        
  317.         // echo json_encode($data);
  318.         echo '{"responsible": ' . json_encode($data, JSON_UNESCAPED_UNICODE) . '}';
  319.     }
  320. });
  321.  
  322. // Get data from MySQL with PDO
  323. $app->post('/addInsurance', function(Request $request, Response $response){
  324.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  325.    
  326.     // get username and password
  327.     $kundid = $request->getParam('Kund_id');
  328.     $uppdrutg = $request->getParam('Uppdragsavtalets_utgang');
  329.    
  330.     $ovriginfo = $request->getParam('Ovrig_information');
  331.     $arvode = $request->getParam('Arvode');
  332.     $rorligt = $request->getParam('Rorligt');
  333.     $arvodefakt = $request->getParam('Arvode_fakurerat');
  334.  
  335.     $forsakrform = $request->getParam('Forsakringsform');
  336.     $projektforsakring = $request->getParam('Projektforsakring');
  337.     $forsakrnr = $request->getParam('Forsakringsnr');
  338.     $fornydatum = $request->getParam('Fornyelse');
  339.     $forsakrb = $request->getParam('Forsakringsbolag');
  340.  
  341.     $premiekr = $request->getParam('Premie_kr');
  342.     $provpr = $request->getParam('Provision_procent');
  343.     $provkr = $request->getParam('Provision_kr');
  344.     $sarskildfaktrutin = $request->getParam('Sarskild_faktura_rutin');
  345.     $aps1 = $request->getParam('ARBETSPROCESS_STEG_1');
  346.    
  347.     $aps2a = $request->getParam('ARBETSPROCESS_STEG_2_A_FORNYELSE');
  348.     $aps2b = $request->getParam('ARBETSPROCESS_STEG_2_B_UPPHANDLING');
  349.     $aps3 = $request->getParam('ARBETSPROCESS_STEG_3_PLACERING');
  350.     $aps4 = $request->getParam('ARBETSPROCESS_STEG_4_ANDRING');
  351.     $arkiverad = $request->getParam('Arkiverad');
  352.  
  353.     // try it!
  354.     try {
  355.         // Might need to add some security to this..
  356.         // make query string
  357.         $sql = "INSERT INTO insurances (Kund_id,
  358.                                        Uppdragsavtalets_utgang,
  359.                                        Ovrig_information,
  360.                                        Arvode,
  361.                                        Rorligt,
  362.  
  363.                                        Arvode_fakurerat,
  364.                                        Forsakringsform,
  365.                                        Projektforsakring,
  366.                                        Forsakringsnr,
  367.  
  368.                                        Fornyelse,
  369.                                        Forsakringsbolag,
  370.                                        Premie_kr,
  371.                                        Provision_procent,
  372.                                        Provision_kr,
  373.  
  374.                                        Sarskild_faktura_rutin,
  375.                                        ARBETSPROCESS_STEG_1,
  376.                                        ARBETSPROCESS_STEG_2_A_FORNYELSE,
  377.                                        ARBETSPROCESS_STEG_2_B_UPPHANDLING,
  378.                                        ARBETSPROCESS_STEG_3_PLACERING,
  379.  
  380.                                        ARBETSPROCESS_STEG_4_ANDRING,
  381.                                        Arkiverad) VALUES (?, ?, ?, ?, ?,
  382.                                                            ?, ?, ?, ?, ?,
  383.                                                            ?, ?, ?, ?, ?,
  384.                                                            ?, ?, ?, ?, ?,
  385.                                                            ?)";
  386.                                        
  387.         $pdo->prepare($sql)->execute([$kundid,
  388.                                     $uppdrutg,
  389.                                     $ovriginfo,
  390.                                     $arvode,
  391.                                     $rorligt,
  392.  
  393.                                     $arvodefakt,
  394.                                     $forsakrform,
  395.                                     $projektforsakring,
  396.                                     $forsakrnr,
  397.  
  398.                                     $fornydatum,
  399.                                     $forsakrb,
  400.                                     $premiekr,
  401.                                     $provpr,
  402.                                     $provkr,
  403.  
  404.                                     $sarskildfaktrutin,
  405.                                     $aps1,
  406.                                     $aps2a,
  407.                                     $aps2b,
  408.                                     $aps3,
  409.  
  410.                                     $aps4,
  411.                                     $arkiverad]); // chaining SQL statements..
  412.  
  413.         return $response->withJson(['response' => 'success', 'message' => 'Added insurance to database'], 200);
  414.     }
  415.     // did we get any errors?
  416.     catch (PDOException $e) {
  417.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  418.     }
  419. });
  420.  
  421. $app->post('/addCustomer', function(Request $request, Response $response){
  422.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  423.  
  424.     $kundid = uniqid();
  425.     $orgnr = $request->getParam('Organisationsnummer');
  426.     $koncern = $request->getParam('Koncern');
  427.     $orgdottrb = $request->getParam('Organisationsnummer_Dotterbolag');
  428.     $dottrb = $request->getParam('Dotterbolag');
  429.     $kundansv = $request->getParam('Kundansvarig');
  430.  
  431.     // try it!
  432.     try {
  433.         // Might need to add some security to this..
  434.         // make query string
  435.         $sql = "INSERT INTO customers (Organisationsnummer,
  436.                                        Kund_id,
  437.                                        Koncern,
  438.                                        Organisationsnummer_Dotterbolag,
  439.                                        Dotterbolag,
  440.                                        Kundansvarig) VALUES (?, ?, ?, ?, ?, ?)";
  441.                                        
  442.         $pdo->prepare($sql)->execute([$orgnr,
  443.                                     $kundid,
  444.                                     $koncern,
  445.                                     $orgdottrb,
  446.                                     $dottrb,
  447.                                     $kundansv]);
  448.  
  449.         return $response->withJson(['response' => 'success', 'message' => 'Added insurance to database'], 200);
  450.     }
  451.     // did we get any errors?
  452.     catch (PDOException $e) {
  453.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  454.     }
  455. });
  456.  
  457. $app->post('/editInsurance', function(Request $request, Response $response){
  458.     require_once(__DIR__ . '/dbstore/dbconnect.php');
  459.    
  460.     // get username and password
  461.     $id = $request->getParam('insuranceid');
  462.     $orgnr = $request->getParam('Organisationsnummer');
  463.     $koncern = $request->getParam('Koncern');
  464.     $dottrb = $request->getParam('Dotterbolag');    
  465.     $orgdottrb = $request->getParam('Organisationsnummer_Dotterbolag');
  466.     $uppdrutg = $request->getParam('Uppdragsavtalets_utgang');
  467.    
  468.     $ovriginfo = $request->getParam('Ovrig_information');
  469.     $arvode = $request->getParam('Arvode');
  470.     $rorligt = $request->getParam('Rorligt');
  471.     $arvodefakt = $request->getParam('Arvode_fakurerat');
  472.     $kundansv = $request->getParam('Kundansvarig');
  473.  
  474.     $forsakrform = $request->getParam('Forsakringsform');
  475.     $projektforsakring = $request->getParam('Projektforsakring');
  476.     $forsakrnr = $request->getParam('Forsakringsnr');
  477.     $fornydatum = $request->getParam('Fornyelse');
  478.     $forsakrb = $request->getParam('Forsakringsbolag');
  479.  
  480.     $premiekr = $request->getParam('Premie_kr');
  481.     $provpr = $request->getParam('Provision_procent');
  482.     $provkr = $request->getParam('Provision_kr');
  483.     $sarskildfaktrutin = $request->getParam('Sarskild_faktura_rutin');
  484.     $aps1 = $request->getParam('ARBETSPROCESS_STEG_1');
  485.    
  486.     $aps2a = $request->getParam('ARBETSPROCESS_STEG_2_A_FORNYELSE');
  487.     $aps2b = $request->getParam('ARBETSPROCESS_STEG_2_B_UPPHANDLING');
  488.     $aps3 = $request->getParam('ARBETSPROCESS_STEG_3_PLACERING');
  489.     $aps4 = $request->getParam('ARBETSPROCESS_STEG_4_ANDRING');
  490.     $arkiverad = $request->getParam('Arkiverad');
  491.  
  492.     // try it!
  493.     try {
  494.         // Might need to add some security to this..
  495.         // make query string
  496.         $sql = "UPDATE insurances SET Organisationsnummer = ?,
  497.                                        Koncern = ?,
  498.                                        Organisationsnummer_Dotterbolag = ?,
  499.                                        Dotterbolag = ?,
  500.                                        Uppdragsavtalets_utgang = ?,
  501.                                        
  502.                                        Ovrig_information = ?,
  503.                                        Arvode = ?,
  504.                                        Rorligt = ?,
  505.                                        Arvode_fakurerat = ?,
  506.                                        Kundansvarig = ?,
  507.                                        
  508.                                        Forsakringsform = ?,
  509.                                        Projektforsakring = ?,
  510.                                        Forsakringsnr = ?,
  511.                                        Fornyelse = ?,
  512.                                        Forsakringsbolag = ?,
  513.                                        
  514.                                        Premie_kr = ?,
  515.                                        Provision_procent = ?,
  516.                                        Provision_kr = ?,
  517.                                        Sarskild_faktura_rutin = ?,
  518.                                        ARBETSPROCESS_STEG_1 = ?,
  519.                                        
  520.                                        ARBETSPROCESS_STEG_2_A_FORNYELSE = ?,
  521.                                        ARBETSPROCESS_STEG_2_B_UPPHANDLING = ?,
  522.                                        ARBETSPROCESS_STEG_3_PLACERING = ?,
  523.                                        ARBETSPROCESS_STEG_4_ANDRING = ?,
  524.                                        Arkiverad = ? WHERE id = ?";
  525.                                        
  526.         $pdo->prepare($sql)->execute([$orgnr,
  527.                                     $koncern,
  528.                                     $dottrb,
  529.                                     $orgdottrb,
  530.                                     $uppdrutg,
  531.  
  532.                                     $ovriginfo,
  533.                                     $arvode,
  534.                                     $rorligt,
  535.                                     $arvodefakt,
  536.                                     $kundansv,
  537.  
  538.                                     $forsakrform,
  539.                                     $projektforsakring,
  540.                                     $forsakrnr,
  541.                                     $fornydatum,
  542.                                     $forsakrb,
  543.  
  544.                                     $premiekr,
  545.                                     $provpr,
  546.                                     $provkr,
  547.                                     $sarskildfaktrutin,
  548.                                     $aps1,
  549.  
  550.                                     $aps2a,
  551.                                     $aps2b,
  552.                                     $aps3,
  553.                                     $aps4,
  554.                                     $arkiverad,
  555.                                     $id]); // chaining SQL statements..
  556.  
  557.         return $response->withJson(['response' => 'success', 'message' => 'Updated insurance to database'], 200);
  558.     }
  559.     // did we get any errors?
  560.     catch (PDOException $e) {
  561.         return $response->withJson(['response' => 'failed', 'message' => $e->getMessage()], 200);
  562.     }
  563. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement