/** * @param $api_id : l'identifiant de l'API utilisée à 8 chiffres. Doit exister dans la BDD et être marqué comme actif. * @param $user_id : l'identifiant utilisateur. Le compte doit être activé. */ function getTracksAPI($api_id, $user_id) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include_once PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/user/checkUserKey.php'; // check User ID // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid user if (checkUserKey($user_id) == 1) { // Search for all user's finished tracks $req = $bdd->prepare("SELECT * FROM `wdidy-track` WHERE (`IDuser` = ? AND `end` != '0000-00-00 00:00:00')"); $req->execute(array($user_id)); $data = $req->fetchAll(); $req->closeCursor(); $done = 1; } else { $cause = 'Utilisateur inexistant ou profil non activé'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * Created by PhpStorm. * User: Rascafr * Date: 02/12/2015 * Time: 01:39 */ function getPointsAPI($api_id, $track_id) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include_once PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/track/checkTrackID.php'; // check Track ID // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid track if (checkTrackID($track_id) == 1) { // Search for all user tracks $req = $bdd->prepare("SELECT * FROM `wdidy-point` WHERE (`IDtrack` = ?)"); $req->execute(array($track_id)); $data = $req->fetchAll(); $req->closeCursor(); $done = 1; } else { $cause = 'Track inexistante'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * @param $api_id : l'identifiant de l'API à utiliser * @param $email : l'email de l'utilisateur * @param $password : password le mot de passe utilisateur hashé en sha256 + salt */ function loginUserAPI($api_id, $email, $password) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for auser $req = $bdd->prepare('SELECT * FROM `wdidy-user` WHERE (`email` = ? AND `password` = ? AND `active` = 1)'); $req->execute(array($email, $password)); $data = $req->fetch(); $req->closeCursor(); if (count($data) != 1) { $done = 1; } else { $cause = 'Utilisateur inexistant ou profil non activé.'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * Retrourne la liste des messages pour une conversation entre deux personnes * @param $api_id : l'identifiant de l'API utilisée à 8 chiffres. Doit exister dans la BDD et être marqué comme actif. * @param $user_id : l'identifiant utilisateur. Le compte doit être activé. * @param $friend_id : l'identifiant de l'ami de l'utilisateur. Le compte doit être activé. */ function getConversationAPI($api_id, $user_id, $friend_id) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include_once PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/user/checkUserKey.php'; // check User ID // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid user if (checkUserKey($user_id) == 1 and checkUserKey($friend_id) == 1) { // Search for all messages between user and his friend $req = $bdd->prepare("\n SELECT * FROM `wdidy-messages` WHERE\n ((`IDsender` = ? AND `IDfriend` = ?) OR (`IDfriend` = ? AND `IDsender` = ?))\n ORDER BY `date` DESC"); $req->execute(array($user_id, $friend_id, $user_id, $friend_id)); $data = $req->fetchAll(); $req->closeCursor(); $done = 1; } else { $cause = 'Utilisateur inexistant ou profil non activé'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * Permet d'ajouter un token device à la liste de l'utilisateur (enregistrement appareil) * @param $api_id : l'identifiant de l'API à utiliser * @param $user_id : l'identifiant de l'utilisateur concerné * @param $device_id : le token du device concerné */ function registerPushAPI($api_id, $user_id, $device_id) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/user/checkUserKey.php'; // check User ID // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid user if (checkUserKey($user_id) == 1) { // Get device list $req = $bdd->prepare('SELECT `devices` FROM `wdidy-user` WHERE IDuser = ? AND active = 1'); $req->execute(array($user_id)); $data = $req->fetch(); $req->closeCursor(); // Add device identifier to existing list (if not already inside) $devices = $data['devices']; if (strpos($devices, $device_id) === false) { if ($devices != '') { $devices .= ','; } $devices .= $device_id; } // Insert updated string into database $req = $bdd->prepare('UPDATE `wdidy-user` SET `devices` = ? WHERE `IDuser` = ?'); $req->execute(array($devices, $user_id)); $req->closeCursor(); $done = 1; } else { $cause = 'Utilisateur inexistant ou profil non activé'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * Retrourne la liste des amis de l'utilisateur * @param $api_id : l'identifiant de l'API utilisée à 8 chiffres. Doit exister dans la BDD et être marqué comme actif. * @param $user_id : l'identifiant utilisateur. Le compte doit être activé. */ function listFriendsAPI($api_id, $user_id) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include_once PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/user/checkUserKey.php'; // check User ID // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid user if (checkUserKey($user_id) == 1) { // Search for all user's friends with accepted request // The current user could be the sender (IDsender = IDuser) or the friend that accepted a request from buddy (IDfriend = IDuser) // We check all the friends the user has REQUESTED (asker) → IDuser = IDsender $req = $bdd->prepare("\n SELECT friend.IDfriend,friend.date,user.firstname,user.lastname,user.city\n FROM `wdidy-friends` friend, `wdidy-user` user\n WHERE (friend.IDsender = ? AND user.IDuser = friend.IDfriend) AND friend.accepted = 1\n ORDER BY friend.date DESC"); $req->execute(array($user_id)); $asUser = $req->fetchAll(); $req->closeCursor(); // Then we check all the users's friend request by another buddies (receiver) → IDuser = IDfriend // IDsender as IDfriend cause we need to get information with same name for the two arrays $req = $bdd->prepare("\n SELECT friend.IDsender AS IDfriend,friend.date,user.firstname,user.lastname,user.city\n FROM `wdidy-friends` friend, `wdidy-user` user\n WHERE (friend.IDfriend = ? AND user.IDuser = friend.IDsender) AND friend.accepted = 1\n ORDER BY friend.date DESC"); $req->execute(array($user_id)); $asFriend = $req->fetchAll(); $req->closeCursor(); // Finally, concats the two data arrays $data = array_merge($asUser, $asFriend); $done = 1; } else { $cause = 'Utilisateur inexistant ou profil non activé'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * Tente la création d'une track pour l'utilisateur concerné * @param $api_id : la clé de l'API à utiliser * @param $user_id : l'identifiant de l'utilisateur concerné * @param $track_name : le nom de la track à créer */ function createTrackAPI($api_id, $user_id, $track_name) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); // Get database include PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/user/checkUserKey.php'; // check User ID include_once PATH . 'api/model/track/checkTrackName.php'; // check Track Name // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid user if (checkUserKey($user_id) == 1) { // Verify if there is no same named track if (checkTrackName($user_id, $track_name) == -1) { // Get current date (database not in correct timestamp, we cannont use NOW() function) date_default_timezone_set('Europe/Paris'); $date = new DateTime(); $timestamp = $date->getTimestamp(); $currentDate = date('Y-m-d H:i:s', $timestamp); // Insert track into database $req = $bdd->prepare("INSERT INTO `wdidy-track`(`IDuser`, `start`, `name`) VALUES (?,?,?)"); $req->execute(array($user_id, $currentDate, $track_name)); $req->closeCursor(); // Get track ID $data['track_id'] = checkTrackName($user_id, $track_name); $done = 1; } else { $cause = 'Ce nom de track existe déjà : veuillez le modifier'; } } else { $cause = 'Utilisateur inexistant ou profil non activé'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * Permet d'ajouter un point dans la track sélectionnée * @param $api_id : la clé d'API à utiliser * @param $track_id : track_id : l'identifiant de la track concernée * @param $latitude : la longitude en format double : 47.142563 * @param $longitude : la latitude en format double : -0.2547 * @param $date_point : la date du point en format YYYY-MM-JJ HH:MM:SS * @param $address : l'adresse du point en tant que chaîne de caractère */ function addPointAPI($api_id, $track_id, $latitude, $longitude, $date_point, $address) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include_once PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/track/checkTrackID.php'; // check Track ID // Skip return line $address = str_replace("\n", ", ", $address); // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid track if (checkTrackID($track_id) == 1) { // Add the point into database $req = $bdd->prepare("INSERT INTO `wdidy-point`(`IDtrack`, `lat`, `lon`, `datetime`, `address`) VALUES (?,?,?,?,?)"); $req->execute(array($track_id, $latitude, $longitude, $date_point, $address)); $req->closeCursor(); // TODO Check whether the point has been added to the database // End date $req = $bdd->prepare("UPDATE `wdidy-track` SET `end` = ? WHERE `IDtrack` = ?"); $req->execute(array($date_point, $track_id)); $req->closeCursor(); $done = 1; } else { $cause = 'Track inexistante'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * Retourne la liste des personnes correspondant à la recherche * @param $api_id : l'identifiant de l'API à utiliser * @param $user_id : l'identifiant de l'utilisateur qui effectue la recherche (pour pouvoir indiquer qui est ami ou non avec lui) * @param $needle : la chaîne de caractères à rechercher dans le nom ou prénom. Ex : "Gu" peut renvoyer "GUy Plantier" ou "Jean GUy" */ function searchFriendsAPI($api_id, $user_id, $needle) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include_once PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/user/checkUserKey.php'; // check User ID // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid user if (checkUserKey($user_id) == 1) { // Search for user where needle : // Is the start of the firstname // Is the start of the lastname // Is the start of fisrtname + lastname $need = $needle . '%'; // TODO : simple way here, check if MySQL is enought powerful to do that → reverse keywords (explode by space) // Note : MySQL already search ignoring case with LIKE keyword $req = $bdd->prepare("\n SELECT * FROM `wdidy-user`\n WHERE ((`firstname` LIKE ?) OR (`lastname` LIKE ?) OR (CONCAT(`firstname`, ' ', `lastname`) LIKE ?)) AND `active` = 1\n "); $req->execute(array($need, $need, $need)); $data = $req->fetchAll(); $req->closeCursor(); $done = 1; } else { $cause = 'Utilisateur inexistant ou profil non activé'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }
/** * Poste un message dans une conversation entre deux personnes * @param $api_id : l'identifiant de l'API utilisée à 8 chiffres. Doit exister dans la BDD et être marqué comme actif. * @param $user_id : l'identifiant utilisateur qui poste le message. Le compte doit être activé. * @param $friend_id : l'identifiant de l'ami de l'utilisateur. Le compte doit être activé. * @param $text : Le message à poster */ function postMessageAPI($api_id, $user_id, $friend_id, $text) { define("PATH", "/home/sites/francoisle.fr/public_html/wdidy/"); $cause = ''; $done = 0; $data = array(); // Get database include_once PATH . 'include/sql.php'; // Get other models include_once PATH . 'api/model/api/checkAPIKey.php'; // check API key include_once PATH . 'api/model/user/checkUserKey.php'; // check User ID include_once PATH . 'include/push.php'; // device push function include_once PATH . 'api/model/push/sendPushAPI.php'; // user push API // Search for a valid API ID if (checkAPIKey($api_id) == 1) { // Search for a valid user if (checkUserKey($user_id) == 1 and checkUserKey($friend_id) == 1) { // Get true server date date_default_timezone_set('Europe/Paris'); $date = new DateTime(); $timestamp = $date->getTimestamp(); $messDate = date('Y-m-d H:i:s', $timestamp); // Insert message between user and his friend $req = $bdd->prepare("\n INSERT INTO `wdidy-messages`(`IDsender`, `IDfriend`, `text`, `date`)\n VALUES (?,?,?,?)\n "); $req->execute(array($user_id, $friend_id, $text, $messDate)); $req->closeCursor(); // Get username TODO API for that action $req = $bdd->prepare('SELECT * FROM `wdidy-user` WHERE (`IDuser` = ? AND `active` = 1)'); $req->execute(array($user_id)); $data = $req->fetch(); $req->closeCursor(); $userName = $data['firstname'] . ' ' . $data['lastname']; // Friend → User ? Point of view of push / message receiver // Prepare a notification for the receiver $pushTitle = 'Nouveau message WDIDY'; $pushMessage = $data['firstname'] . ' : ' . substr(base64_decode($text), 0, 250); $pushIntent = array('app_action' => 'intent.com.wdidy.app.push.conversation.new', 'extra' => array('friend_id' => $user_id, 'friend_name' => $userName)); // PUUUUUUSSSSSH $cause = sendPushAPI($bdd, $friend_id, $pushTitle, $pushMessage, $pushIntent); // balek un petit peu de la réponse // Search for all messages between user and his friend $req = $bdd->prepare("\n SELECT * FROM `wdidy-messages` WHERE\n ((`IDsender` = ? AND `IDfriend` = ?) OR (`IDfriend` = ? AND `IDsender` = ?))\n ORDER BY `date` DESC"); $req->execute(array($user_id, $friend_id, $user_id, $friend_id)); $data = $req->fetchAll(); $req->closeCursor(); $done = 1; } else { $cause = 'Utilisateur inexistant ou profil non activé'; } } else { $cause = 'Clé API inexistante ou désactivée'; } // Resulting array $resp['success'] = $done; $resp['cause'] = $cause; $resp['data'] = $data; return $resp; }