1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/*
* TiersdeConfianceAuto.cpp
*
* Created by titou34 on 08/09/2015.
* Copyright (c) 2015 titou34. GPLv3.
* DR
* « Tous droits réservés », « All rights reserved »
*/
#include "stdafx.h"
#include "TiersdeConfianceAuto.h"
#include <comdef.h>
#include <locale.h>
#include <iostream>
using namespace std;
#include <openssl\applink.c>
#include <openssl\pem.h>
#include <openssl\err.h>
#include <openssl\ssl.h>
#include <openssl\evp.h>
#include <openssl\pkcs12.h>
#include <libxml\tree.h>
#include <libxml\parser.h>
#include <libxml\xpath.h>
#include <libxml\xpathInternals.h>
#include <libxml\xmlschemas.h>
#include <curl\curlver.h>
#include <curl\curl.h>
#include <curl\stdcheaders.h>
#include <curl\types.h>
#include <curl\mprintf.h>
#include <curl\multi.h>
#include <curl\easy.h>
#include "TedetisEnvelop.h"
#include "Constant.h"
#include "SessionTab.h"
#include "TiersdeConfiance.h"
char* CTiersdeConfianceAuto::m_tdt_url;
CLog* CTiersdeConfianceAuto::m_logger;
char* CTiersdeConfianceAuto::m_certificate_path;
char* CTiersdeConfianceAuto::m_repository_path;
char* CTiersdeConfianceAuto::m_tdt_passphrase;
int CTiersdeConfianceAuto::isinit;
char* CTiersdeConfianceAuto::m_tdt_trigramme;
CSessionTab CTiersdeConfianceAuto::m_session_tab;
IMPLEMENT_DYNCREATE(CTiersdeConfianceAuto, CCmdTarget)
CTiersdeConfianceAuto::CTiersdeConfianceAuto()
{
EnableAutomation();
AfxOleLockApp();
isinit = 0;
m_repository_path = NULL;
m_tdt_passphrase = NULL;
m_certificate_path = NULL;
m_logger = NULL;
}
/////////////////////////////////////////////////////////////////////////////
// CTiersdeConfianceAuto message handlers
int CTiersdeConfianceAuto::monwrite1(char* buffer, size_t tailleBloc, size_t nombreBlocs, char** flot)
{
*flot = (char*)malloc(tailleBloc*(nombreBlocs + 1));
memcpy(*flot, buffer, tailleBloc*nombreBlocs);
(*flot)[tailleBloc*nombreBlocs] = '\0';
return (tailleBloc*nombreBlocs);
}
int CTiersdeConfianceAuto::sslconnect(char* page, struct curl_httppost* post, char** retour)
{
char* url = NULL;
CURLcode curl_code;
CURL* handle = NULL;
/*Calcul de l'URL */
url = (char*)malloc(strlen(m_tdt_url) + strlen(page) + 1);
sprintf(url, "%s%s", m_tdt_url, page);
/*Création du handle CURL */
handle = curl_easy_init();
if (handle != NULL)
{
curl_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 1);
curl_code = curl_easy_setopt(handle, CURLOPT_SSLCERT, "certificat");
//curl_code = curl_easy_setopt(handle, CURLOPT_SSLCERTPASSWD, m_tdt_passphrase);
curl_code = curl_easy_setopt(handle, CURLOPT_SSLKEY, "pkey");
//curl_code = curl_easy_setopt(handle, CURLOPT_SSLKEYPASSWD, m_tdt_passphrase);
/*curl_code = curl_easy_setopt(handle, CURLOPT_SSLCERT, m_certificate_path);
curl_code = curl_easy_setopt(handle, CURLOPT_SSLCERTTYPE, "P12");
curl_code = curl_easy_setopt(handle, CURLOPT_SSLKEYPASSWD, m_tdt_passphrase); */
curl_code = curl_easy_setopt(handle, CURLOPT_URL, url);
curl_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &monwrite1);
curl_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, retour);
if (post != NULL)
{
curl_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, post);
}
curl_code = curl_easy_perform(handle);
curl_easy_cleanup(handle);
}
/*Libération resssource locale*/
free(url);
return (curl_code);
}
/******************************************************************************************************************
* *
* Fonction permettant d'envoyer une enveloppe FAST contenant une enveloppe métier *
* *
******************************************************************************************************************/
int CTiersdeConfianceAuto::ENVOI(char INFO_SESSION[], char TYPE_TRANSMISSION[], char CHEMIN_FLUX[], char FLUX_RETOUR[])
{
CTime theTime;
CString s;
char* id = NULL;
struct curl_httppost* post = NULL;
struct curl_httppost* last = NULL;
char* drive = NULL; /* non CUtilisé cf MSDN */
char* dir = NULL; /* non CUtilisé cf MSDN */
char* fname = NULL;
char* ext = NULL;
char* final_name = NULL;
char* retour = NULL;
char* ok = NULL;
char* next_ok = NULL;
char* nb = NULL;
int ok_ok = 0;
int curl_code = 0;
id = INFO_SESSION;
if (!m_session_tab.getSession(atoi(id)))
{
m_logger->logger(SESSION_INVALID_CODE_ERR);
return SESSION_INVALID_CODE_ERR;
}
/*Calcul du champs POST */
fname = (char*)malloc(strlen(CHEMIN_FLUX) + 1);
ext = (char*)malloc(strlen(CHEMIN_FLUX) + 1);
_splitpath((char*)CHEMIN_FLUX, drive, dir, fname, ext);
final_name = (char *)malloc(strlen(CHEMIN_FLUX) + 4);
sprintf(final_name, "%s%s", fname, ext);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "enveloppe", CURLFORM_FILE, CHEMIN_FLUX, CURLFORM_FILENAME, final_name, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "api", CURLFORM_PTRCONTENTS, "1", CURLFORM_END);
free(fname);
free(ext);
curl_code = sslconnect(PAGE_TRANSACTION, post, &retour);
if (curl_code != CURLE_OK)
{
return COM_NO_REMOTE_HOST; // on est pas arrivé à programmer correctement libcurl
}
ok = strtok(retour, "\n");
if (!strcmp(ok, "OK"))
{
ok_ok = 1;
}
else
{
ok_ok = 0;
}
if (ok_ok == 1)
{
ok = strtok(NULL, "\n");
ok = strtok(NULL, "\n");
nb = (char *)malloc(strlen(ok) + 1);
strcpy(nb, ok);
free(retour);
retour = (char *)malloc(2048);
setlocale(LC_ALL, "");
theTime = CTime::GetCurrentTime();
s = theTime.Format("%d/%m/%Y %H:%M:%S");
sprintf(retour, XML_RETOUR, nb, nb, final_name, s);
free(nb);
}
FLUX_RETOUR = retour;
free(retour);
if (ok_ok == 1)
{
return SUCCESS_CODE_RET;
}
else
{
return COM_ENVELOPPE_KO;
}
free(final_name);
} |