Commit 3f93766c authored by Vincent Dubois's avatar Vincent Dubois Committed by Vincent Dubois

Commentaire dans le php + Suppression de code inutilisé

parent 9fde0047
......@@ -187,7 +187,6 @@ class Geocropping {
$osm.on('connected', () => {
$(this).trigger('userConnected');
//console.trace('myGeocropping', 'connected');
this.notify.ConnectionOK();
this.updateUserDetails();
});
......
......@@ -16,6 +16,8 @@ class Geocropping
function __construct($type = 'PROD')
{
// récuper les tokens de l'application
global $consumer;
if ($type === 'DEV') {
// DEV
......@@ -42,31 +44,27 @@ class Geocropping
$this->oauthAccessToken = new OAuthToken('', '');
}
// non utilisé avec les session
public function encrypteTokenValue($tokenValue)
{
return openssl_encrypt($tokenValue, "AES-128-ECB", $this->oauthConsumer->key);
}
// non utilisé avec les session
public function decrypteTokenValue($tokenValue)
{
$tokenValue = str_replace(' ', '+', $tokenValue);
return openssl_decrypt($tokenValue, "AES-128-ECB", $this->oauthConsumer->key);
}
// Oauth 1 : récupérer un token pour identifier une session pour la connexion
public function requestToken($params = [])
{
$endpoint = $this->authUrl . '/oauth/request_token';
$req = OAuthRequest::from_consumer_and_token($this->oauthConsumer, null, "GET", $endpoint, $params);
$url = $this->authUrl . '/oauth/request_token';
// préconfiguration de la requete
$req = OAuthRequest::from_consumer_and_token($this->oauthConsumer, null, "GET", $url, $params);
// signer la requete avec le type de signature en utilisant seulement le consumer comme clé
$req->sign_request(new OAuthSignatureMethod_HMAC_SHA1, $this->oauthConsumer, null);
$str = file_get_contents($req->to_url());
//var_dump(json_encode($http_response_header));
if ($str != false) {
parse_str($str, $data);
......@@ -77,40 +75,48 @@ class Geocropping
return [$http_response_header, $req->to_url()];
}
public function getAuthorizeUrl($token = null)
// Oauth 2 : récupérer l'adresse pour la connexion de l'utilisateur (sur OSM)
// le callback par défaut est l'url actuel avec l'action access_token
public function getAuthorizeUrl($token = null, $callback = "")
{
// récupérer le token de la class si aucun n'est passé en paramètre
$token = empty($token) ? $this->oauthRequestToken : $token;
$url = $this->authUrl . '/oauth/authorize';
$callback = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_URI'] . '/auth/?' . http_build_query([action => 'access_token', request_secret => $this->encrypteTokenValue($this->oauthRequestToken->secret)], '', '&');
// callback = url vers le getAccessToken
if(!is_string($callback) || empty($callback)){
$callback = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_URI'] . '/auth/?' . http_build_query([action => 'access_token'], '', '&');
}
return ($url . '?' . http_build_query([oauth_token => $token->key, oauth_callback => $callback], '', '&'));
}
public function getAccessToken($token = null, $callback = null)
// Oauth 3 : récupérer la session OSM de l'utilisateur
public function getAccessToken($token = null)
{
$consumer = $this->oauthConsumer;
$token = empty($token) ? $this->oauthRequestToken : $token;
// parametres ajouté a la signature
$params = ['oauth_token' => $token->key];
$endpoint = $this->authUrl . '/oauth/access_token';
// préconfiguration de la requete
$req = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $endpoint, $params);
// signer la requete avec le type de signature en utilisant le consumer et le request_token comme clé
$req->sign_request(new OAuthSignatureMethod_HMAC_SHA1, $consumer, $token);
$str = file_get_contents($req->to_url());
$data = null;
parse_str($str, $data);
$this->oauthAccessToken->key = $data['oauth_token'];
$this->oauthAccessToken->secret = $data['oauth_token_secret'];
if ($str != false) {
parse_str($str, $data);
$this->oauthAccessToken->key = $data['oauth_token'];
$this->oauthAccessToken->secret = $data['oauth_token_secret'];
}
return $this->oauthAccessToken;
}
///print_r(openssl_get_cipher_methods());
function __toString()
{
$req_secret = $this->oauthRequestToken->secret;// ? $this->encrypteTokenValue($this->oauthRequestToken->secret) : NULL;
$access_secret = $this->oauthAccessToken->secret;// ? $this->encrypteTokenValue($this->oauthAccessToken->secret) : NULL;
$req_secret = $this->oauthRequestToken->secret;;
$access_secret = $this->oauthAccessToken->secret;;
return json_encode([
'oauth_request_token' => [
......@@ -124,14 +130,20 @@ class Geocropping
]);
}
// faire des requete auprès de OSM
private function requestAPI($endpoint, $token = null, $params = array(), $method = 'GET', $content = '')
{
$consumer = $this->oauthConsumer;
$token = empty($token) ? $this->oauthAccessToken : $token;
// parametres ajouté a la signature
$params['oauth_token'] = $token->key;
$endpoint = $this->apiUrl . $endpoint;
// préconfiguration de la requete
$req = OAuthRequest::from_consumer_and_token($consumer, $token, $method, $endpoint, $params);
// signer la requete avec le type de signature en utilisant le consumer et le request_token comme clé
$req->sign_request(new OAuthSignatureMethod_HMAC_SHA1, $consumer, $token);
// informations nécessaire pour le requete
$options = [
'http' => [
'method' => $method,
......@@ -141,6 +153,7 @@ class Geocropping
];
$url = $req->to_url();
// ajouter le contenu a la requete
if (!empty($content)) {
$options['http']['content'] = $content;
$options['http']['header'] .= 'Content-Type: ' . self::getMime($content) . "\r\n";
......@@ -151,6 +164,8 @@ class Geocropping
return $this->parseHeaders($http_response_header);
}*/
$headers = $this->parseHeaders($http_response_header);
// si la réponse est en xml, transformer en json (plus facile a traiter en javascript)
if ($headers['Content-Type'] && (strpos($headers['Content-Type'], 'application/xml') !== false || strpos($headers['Content-Type'], 'text/xml') !== false)) {
$headers['Content-Type'] = 'application/json';
$str = simplexml_load_string($str);
......@@ -182,6 +197,7 @@ class Geocropping
return $head;
}
// gérer les différente requete (peut être transformé en plusieur fonction)
public function handleRequestAPI($command, $options)
{
if (!is_array($options)) {
......@@ -195,13 +211,14 @@ class Geocropping
/// GET
case 'GetServerCapabilities' :
$url = '/api/capabilities';
return $this->requestAPI($url, null, $options, 'GET');
case 'GetPermissions' :
$url = '/api/0.6/permissions';
return $this->requestAPI($url, null, $options, 'GET');
case 'GetDataBoundsBox' :
$left = $options['left'] ? $options['left'] : '0';
$bottom = $options['bottom'] ? $options['bottom'] : '0';
......@@ -209,21 +226,26 @@ class Geocropping
$top = $options['top'] ? $options['top'] : '0.5';
$url = '/api/0.6/map?bbox=' . $left . ',' . $bottom . ',' . $right . ',' . $top;
return $this->requestAPI($url, null, $options, 'GET');
case 'GetUserDetails' :
$url = '/api/0.6/user/details';
return $this->requestAPI($url, null, $options, 'GET');
case 'GetUserPreferences' :
$url = '/api/0.6/user/preferences';
return $this->requestAPI($url, null, $options, 'GET');
case 'GetUserId' :
$id = $options['id'] ? $options['id'] : '1';
$url = '/api/0.6/user/' . $id;
return $this->requestAPI($url, null, $options, 'GET');
case 'GetNoteId' :
$id = $options['id'] ? $options['id'] : '1';
$format = $options['format'] ? '.' . $options['format'] : '';
$url = '/api/0.6/notes/' . $id . $format;
return $this->requestAPI($url, null, $options, 'GET');
case 'GetNotesBoundsBox' :
$left = $options['left'] ? $options['left'] : '0';
$bottom = $options['bottom'] ? $options['bottom'] : '0';
......@@ -232,29 +254,31 @@ class Geocropping
$format = $options['format'] ? '.' . $options['format'] : '';
$url = '/api/0.6/notes' . $format . '?bbox=' . $left . ',' . $bottom . ',' . $right . ',' . $top;
return $this->requestAPI($url, null, $options, 'GET');
case 'SearchNotes' :
$query = $options['query'] ? $options['query'] : 'hello';
$limit = $options['limit'] ? $options['limit'] : '100';// 1 -> 10000
$closed = $options['closed'] ? $options['closed'] : '7';// Specifies the number of days a bug needs to be closed to no longer be returned
// A value of 0 means only open bugs are returned.
// A value of -1 means all bugs are returned.
$format = $options['format'] ? '.' . $options['format'] : '';
$url = '/api/0.6/notes/search' . $format . '?q=' . $query . '&limit=' . $limit . '&closed=' . $closed;
return $this->requestAPI($url, null, $options, 'GET');
case 'GetChangesetId' :
$id = $options['id'] ? $options['id'] : '1';
$discussion = $options['discussion'] ? $options['discussion'] : 'true';
$url = '/api/0.6/changeset/' . $id . '?include_discussion=' . $discussion;
return $this->requestAPI($url, null, $options, 'GET');
case 'GetChangeOfChangesetId' :
$id = $options['id'] ? $options['id'] : '1';
$url = '/api/0.6/changeset/' . $id . '/download';
return $this->requestAPI($url, null, $options, 'GET');
case 'GetElementId' :
$id = $options['id'] ? $options['id'] : '1';
$type = strtolower($options['type']);// [node|way|relation]
......
<?php
require_once('geocropping.php');
$useSession = true;
require_once ('geocropping.php');
if ($useSession) {
// d * h * min * s
$timeoutSession = 30 * 24 * 60 * 60; // ~1 mois
ini_set("session.gc_maxlifetime", $timeoutSession);
session_set_cookie_params($timeoutSession);
session_name('OSM');
$timeoutSession = 30 * 24 * 60 * 60; // ~1 mois
ini_set("session.gc_maxlifetime", $timeoutSession);
session_set_cookie_params($timeoutSession);
session_name('OSM');
session_start();
}
session_start();
if ($_REQUEST['devMode']) {
$_SESSION['devMode'] = $_REQUEST['devMode'];
}
// gère les differentes requetes utilisant OAuth
$geocropping = new Geocropping( $_SESSION['devMode'] === 'true' ? 'DEV' : 'PROD');
$geocropping = new Geocropping($_SESSION['devMode'] === 'true' ? 'DEV' : 'PROD');
switch ($_REQUEST['action']) {
case null:
case '':
case null :
case '' :
// REQUEST TOKEN
if (($err = $geocropping->requestToken()) === true) { // 1
if ( ($err = $geocropping->requestToken()) === true) { // 1
// garder le token pour le réustiliser dans le access token
$_SESSION['request_token'] = $geocropping->oauthRequestToken;
$location = $geocropping->getAuthorizeUrl();
header('Location: '.$location); // 2
} else {
header('Location: ' . $location); // 2
}
else {
// afficher tout en cas d'erreur
header('Content-Type: application/json');
die(json_encode($err));
}
break;
case 'access_token':
case 'access_token' :
// ACCESS TOKEN
if (!empty($_REQUEST['oauth_token']) && !empty($_REQUEST['request_secret'])) {
$geocropping->oauthRequestToken = new OAuthToken($_REQUEST['oauth_token'], $geocropping->decrypteTokenValue($_REQUEST['request_secret']));
if (!empty($_REQUEST['oauth_token']) && !empty($_SESSION['request_token'])) {
//TODO: vérifier si le token de la requete et le même que celui de la session ?
$geocropping->oauthRequestToken = $_SESSION['request_token'];
unset($_SESSION['request_token']);
$tokenAccess = $geocropping->getAccessToken(); // 3
$callback;
if (!empty($tokenAccess->key) && !empty($tokenAccess->secret)) {
if ($useSession) {
$_SESSION['access_token'] = $tokenAccess;
$callback = '../';
} else {
// aller sur la page de callback avec le token ecrypté
$callback = 'callback.html?'.http_build_query([oauth_token=>$tokenAccess->key,access_secret=>$geocropping->encrypteTokenValue($tokenAccess->secret),callback=>'../'], '', '&');
}
header('Location: '.$callback);
//sauvegarde le token de l'utilisateur et retourner sur la carte
$_SESSION['access_token'] = $tokenAccess;
$callback = '../';
header('Location: ' . $callback);
}
} else {
}
else {
http_response_code(400);
}
break;
case 'request_api':
case 'request_api' :
// API
$data;
if ($useSession /*&& !empty($_SESSION['access_token'])*/) {
// with session
$geocropping->oauthAccessToken = $_SESSION['access_token'];
$data = $geocropping->handleRequestAPI($_REQUEST['command'], $_REQUEST['options']); // 4
} elseif (!empty($_REQUEST['oauth_token'] && !empty($_REQUEST['access_secret']))) {
// without session
$geocropping->oauthAccessToken->key = $_REQUEST['oauth_token'];
$geocropping->oauthAccessToken->secret = $geocropping->decrypteTokenValue($_REQUEST['access_secret']);
$data = $geocropping->handleRequestAPI($_REQUEST['command'], $_REQUEST['options']); // 4
}/* else {
//error
// Not connected
http_response_code(401);
die();
}*/
// faire la requete avec les paramètres associer
$geocropping->oauthAccessToken = $_SESSION['access_token'];
$data = $geocropping->handleRequestAPI($_REQUEST['command'], $_REQUEST['options']); // 4
/*if ($_SESSION['devMode'] && $data['res']['headers']['response_code'] == 401) {
$data['res']['headers']['response_code'] = 400;
}*/
// définir le status de retour comme celui de la requete OSM
http_response_code($data['res']['headers']['response_code']);
// réponse ok
if ($data['res']['headers']['response_code'] >= 200 && $data['res']['headers']['response_code'] <= 299) {
// afficher les donné du retour si pas d'erreur
header('Content-Type: '.$data['res']['headers']['Content-Type']);
//définir le content-type de retour comme celui de la requete OSM
header('Content-Type: ' . $data['res']['headers']['Content-Type']);
if (strpos($data['res']['headers']['Content-Type'], 'application/json') !== false) {
// le contenu est du json, l'encoder
die(json_encode($data['res']['body']));
} elseif (!empty($data['res']['body'])) {
}
elseif (!empty($data['res']['body'])) {
die($data['res']['body']);
} else {
}
else {
// si aucun contenu, afficher tout les données ?
header('Content-Type: application/json');
die(json_encode($data));
}
} else {
}
else {
// afficher tout en cas d'erreur
header('Content-Type: application/json');
die(json_encode($data));
}
break;
case 'is_connected':
$connected = empty($_SESSION['access_token'])?'false':'true';
case 'is_connected' :
// vérifier si un token est présent
$connected = empty($_SESSION['access_token']) ? 'false' : 'true';
header('Content-Type: application/json');
die(json_encode([connected=>$connected]));
die(json_encode([connected => $connected]));
break;
case 'logout':
case 'logout' :
// supprimer la session
if (!empty($_SESSION['access_token'])) {
session_destroy();
$params = session_get_cookie_params();
......@@ -107,6 +108,6 @@ switch ($_REQUEST['action']) {
}
http_response_code(204);
break;
default:
default :
http_response_code(400);
}
{
// The tab key will cycle through the settings when first created
// Visit http://wbond.net/sublime_packages/sftp/settings for help
// sftp, ftp or ftps
"type": "sftp",
"save_before_upload": true,
"upload_on_save": false,
"sync_down_on_open": false,
"sync_skip_deletes": false,
"sync_same_age": true,
"confirm_downloads": false,
"confirm_sync": true,
"confirm_overwrite_newer": false,
"host": "web01",
"user": "devvdubois",
"password": "ohmudohDo6",
//"port": "22",
"remote_path": "/var/www/restricted/ssh/devvdubois/www/OSM/geocropping-dev/",
"ignore_regexes": [
"\\.sublime-(project|workspace)", "sftp-config(-alt\\d?)?\\.json",
"sftp-settings\\.json", "/venv/", "\\.svn/", "\\.hg/", "\\.git/",
"\\.bzr", "_darcs", "CVS", "\\.DS_Store", "Thumbs\\.db", "desktop\\.ini"
],
//"file_permissions": "664",
//"dir_permissions": "775",
//"extra_list_connections": 0,
"connect_timeout": 30,
//"keepalive": 120,
//"ftp_passive_mode": true,
//"ftp_obey_passive_host": false,
//"ssh_key_file": "~/.ssh/id_rsa",
//"sftp_flags": ["-F", "/path/to/ssh_config"],
//"preserve_modification_times": false,
//"remote_time_offset_in_hours": 0,
//"remote_encoding": "utf-8",
//"remote_locale": "C",
//"allow_config_upload": false,
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment