Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-type: application/json; charset=utf-8');
- use Psr\Log\LogLevel;
- require 'vendor/autoload.php';
- /**
- *
- * Author - Olcay Ertaş
- *
- */
- class ArendiWebServices {
- private $db;
- private $host = "localhost";
- private $userName = "yourUserName";
- private $password = "yourPassword";
- private $database = "yourDatabaseName";
- private $logger;
- // Constructor - open DB connection
- function __construct() {
- $this->db = new mysqli($this->host, $this->userName, $this->password, $this->database);
- $this->db->autocommit(FALSE);
- $stmt1 = $this->db->prepare("SET NAMES 'utf8'");
- $stmt1->execute();
- $stmt2 = $this->db->prepare('SET CHARACTER SET "utf8"');
- $stmt2->execute();
- $this->logger = new Katzgrau\KLogger\Logger("log.txt", LogLevel::DEBUG);
- }
- // Destructor - close DB connection
- function __destruct() {
- $this->db->close();
- }
- /**
- * Proje listesini veren method.
- */
- function getProjects() {
- $this->logger->debug("GetProjects: Entrance\n");
- $stmt = $this->db->prepare('SELECT * FROM yourProjectsTable');
- $id = $no = $title = $subject = $description = $attachment = $status = NULL;
- $stmt->bind_result($id, $no, $title, $subject, $description, $attachment, $status);
- if ($stmt->execute()) {
- $this->logger->debug("GetProjects: If\n");
- $result = array();
- while ($stmt->fetch()) {
- $row = array("id" => $id, "no" => $no, "title" => $title,
- "subject" => $subject, "description" => $description,
- "attachment" => $attachment, "status" => $status);
- $result[] = $row;
- }
- echo json_encode($result);
- }
- $stmt->close();
- $this->logger->debug("GetProjects: Exit\n");
- }
- /**
- * Method for login, returns user information if user name and password are correct.
- * @param type $email User email address
- * @param type $password User account password.
- */
- function getUser($email, $password) {
- $this->logger->debug("GetUser: Entrance\n");
- $stmt = $this->db->prepare("SELECT * FROM yourUsersTable WHERE email=? && password=?");
- $stmt->bind_param("ss", $email, $password);
- $id = $companyid = $name = $surname = $email2 = $password2 = NULL;
- $stmt->bind_result($id, $companyid, $name, $surname, $email2, $password2);
- if ($stmt->execute()) {
- if ($stmt->fetch()) {
- $stmt->close();
- $log = $id . " - "
- . $companyid . " - "
- . $name . " - "
- . $surname . " - "
- . $email2 . " - "
- . $password2 . "\n";
- $this->logger->debug("GetUser: " . $log);
- $stmt2 = $this->db->prepare("SELECT * FROM yourCompanyTable WHERE id=" . $companyid);
- $cid = $cname = $caddress = $cphone = $cwebpage = NULL;
- $stmt2->bind_result($cid, $cname, $caddress, $cphone, $cwebpage);
- $stmt2->execute();
- $stmt2->fetch();
- $result = "{\"success\":\"1\",";
- $result = $result . "\"id\":\"" . $id . "\",";
- $result = $result . "\"companyid\":\"" . $cid . "\",";
- $result = $result . "\"companyname\":\"" . $cname . "\",";
- $result = $result . "\"name\":\"" . $name . "\",";
- $result = $result . "\"surname\":\"" . $surname . "\",";
- $result = $result . "\"email\":\"" . $email2 . "\",";
- $result = $result . "\"password\":\"" . $password2 . "\"}";
- $stmt2->close();
- echo $result;
- $log = $cid . " - "
- . $cname . " - "
- . $caddress . " - "
- . $cphone . " - "
- . $cwebpage . "\n";
- $this->logger->debug("GetUser: " . $log);
- } else {
- echo "{\"success\":\"0\",\"errorMessage\":\"User name or password is wrong.\",\"email\":\"" . $email . "\",\"password\":\"" . $password . "\"}";
- }
- } else {
- echo "{\"success\":\"0\",\"errorMessage\":\"User name or password is wrong.\"}";
- }
- }
- /**
- * Saves new idea.
- * @param 1 - companyid - User's company id.
- * @param 2 - employeeid - User id
- * @param 3 - title - Idea title
- * @param 4 - description - Idea description.
- * @param 5 - privacy - Idea visibility: 0 everbody can see,
- * 1 Only people in same company can see.
- * @param 6 - attachment - Atachment url
- */
- function saveIdea($ideaData) {
- $this->logger->debug("SaveIdea: Entrance");
- $data = $ideaData;
- $company = $data['company'];
- $employeeName = $data['employeeName'];
- $employeeSurname = $data['employeeSurname'];
- $title = $data['title'];
- $description = $data['description'];
- $privacy = intval($data['privacy']);
- $vote = 0;
- $attachment = $data['attachment'];
- $stmt = $this->db->prepare("INSERT INTO yourIdeaTable VALUES ('', ?, ?, ?, ?, ?, ?, ?, ?)");
- if ($stmt) {
- $stmt->bind_param("sssssiis", $company, $employeeName, $employeeSurname, $title, $description, $privacy, $vote, $attachment);
- } else {
- echo "{\"success\":\"0\",\"error_message\":\"Error while generating query!\"}";
- return;
- }
- if ($stmt === FALSE) {
- $code = $stmt->errorCode();
- $errInfo = $stmt->errorInfo();
- error_log("PREPARE of INSERT query, FAILED\nCode: $code ErrorMessage: $errInfo\n", 3, "php.log");
- } else {
- if ($stmt->execute()) {
- $this->logger->debug("GetUser: Idea saved!");
- echo "{\"success\":\"1\",\"success_message\":\"Idea saved.\"}";
- } else {
- $code = $stmt->errorCode();
- $errInfo = $stmt->errorInfo();
- $this->logger->error("GetUser: Failed to save idea!");
- $this->logger->error("GetUser: Code: $code ErrorMessage: $errInfo\n");
- echo "{\"success\":\"0\",\"error_message\":\"Failed to save idea!\"}";
- }
- }
- }
- }
- $arendiService = new ArendiWebServices();
- $post = file_get_contents('php://input');
- $post = json_decode($post, true);
- $logger = new Katzgrau\KLogger\Logger("log", LogLevel::DEBUG);
- $logger->debug("Arendi Web Services");
- $logger->debug("Post data loaded");
- $logger->debug("Service instance created");
- if (isset($post['request'])) {
- if ($post['request'] == 'login') {
- $logger->debug("Requested service - login");
- $logger->debug($post['userName'] . " - " . $post['password']);
- $arendiService->getUser($post['userName'], $post['password']);
- } else if ($post['request'] == 'getprojects') {
- $logger->debug("Requested service - getprojects");
- $arendiService->getProjects();
- } else if ($post['request'] == 'getMenu') {
- $logger->debug("Requested service - getMenu");
- $arendiService->getMenu();
- } else if ($post['request'] == 'saveIdea') {
- $logger->debug("Requested service - saveIdea");
- //error_log("Incoming data: ".$post."\n", 3, "php.log");
- $arendiService->saveIdea($post);
- } else {
- $logger->debug("Requested service - undefined\n");
- echo '{"success":0,"error_message":"Undefined service!"}';
- }
- } else {
- $logger->error("Invalid service request!");
- $logger->error("Request must have a 'request' field!");
- $response = '{"success":0,"error_message":"Invalid service request! Request must have a \'request\' field!"}';
- $jsonResponse = json_encode($response);
- echo $jsonResponse;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement