~itmages/itmages/itmages-windows-extension

« back to all changes in this revision

Viewing changes to itmagescmd.c

  • Committer: Dmitry
  • Date: 2012-03-11 07:56:24 UTC
  • Revision ID: git-v1:d5a30bf1fe74b46641b08d0151d39dba15b67f0e
Migrate from launchpad

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    ITmages  upload client for Windows
 
3
    Copyright (C) 2011 Dmitriy Simbiriatin <slpiv@itmages.ru>
 
4
 
 
5
    This program is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU General Public License
 
7
    as published by the Free Software Foundation; either version 2
 
8
    of the License, or (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include <stdio.h>
 
21
#include <stdlib.h>
 
22
#include <windows.h>
 
23
#include <commctrl.h>
 
24
#include <curl.h>
 
25
#include <mxml.h>
 
26
#include "itmagescmd.h"
 
27
#include "itmagesgui.h"
 
28
#include "errorshandler.h"
 
29
 
 
30
char g_ErrorBuffer[CURL_ERROR_SIZE];
 
31
 
 
32
size_t ItmagesCallbackWrite(
 
33
        char *data,
 
34
        size_t size,
 
35
        size_t nmemb,
 
36
        FILE *write_file)
 
37
{
 
38
    return fwrite(data, size, nmemb, write_file);
 
39
}
 
40
 
 
41
size_t ItmagesCallbackRead(
 
42
        char *data,
 
43
        size_t size,
 
44
        size_t nmemb,
 
45
        FILE *read_file)
 
46
{
 
47
    return fread(data, size, nmemb, read_file);
 
48
}
 
49
 
 
50
int CallbackProgressbar(
 
51
        HWND upload_progress,
 
52
        double total_down,
 
53
        double down,
 
54
        double total_up,
 
55
        double up)
 
56
{
 
57
    SendMessage(upload_progress, PBM_SETRANGE, 0, (LPARAM) MAKELONG(0, 10));
 
58
    SendMessage(upload_progress, PBM_SETPOS, (WPARAM) (up / total_up * 10), 0);
 
59
    SendMessage(upload_progress, PBM_SETSTEP, 0, 0);
 
60
    return 0;
 
61
}
 
62
 
 
63
int ItmagesCheckConnection(
 
64
        char *cookies_path,
 
65
        char *proxy_adrs,
 
66
        char *proxy_port)
 
67
{
 
68
    CURL *curl;
 
69
    FILE *write_file;
 
70
    CURLcode result;
 
71
    long resp_code = 0;
 
72
 
 
73
    write_file = fopen("check.xml", "w");
 
74
    if (write_file == NULL) {
 
75
        ErrorsHandler(ERROR_WRITEFILE);
 
76
        return -1;
 
77
    } else {
 
78
        curl = curl_easy_init();
 
79
        if (curl) {
 
80
            curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, g_ErrorBuffer);
 
81
            curl_easy_setopt(curl, CURLOPT_URL, ITMAGES_CHECK_URL);
 
82
            curl_easy_setopt(curl, CURLOPT_USERAGENT, ITMAGES_USER_AGENT);
 
83
            curl_easy_setopt(curl, CURLOPT_REFERER, ITMAGES_REFERER);
 
84
            curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip,deflate");
 
85
            curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookies_path);
 
86
            curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies_path);
 
87
            curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
 
88
            if (proxy_adrs != NULL && proxy_port != NULL) {
 
89
                size_t port_num = atoi(proxy_port);
 
90
                curl_easy_setopt(curl, CURLOPT_PROXY, proxy_adrs);
 
91
                curl_easy_setopt(curl, CURLOPT_PROXYPORT, port_num);
 
92
            }
 
93
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ItmagesCallbackWrite);
 
94
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_file);
 
95
            result = curl_easy_perform(curl);
 
96
 
 
97
            if (result != CURLE_OK) {
 
98
                curl_easy_cleanup(curl);
 
99
                fclose(write_file);
 
100
                remove("check.xml");
 
101
                ErrorsHandler(ERROR_CURL);
 
102
                return -1;
 
103
            }
 
104
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &resp_code);
 
105
        }
 
106
        curl_easy_cleanup(curl);
 
107
        fclose(write_file);
 
108
        remove("check.xml");
 
109
        if (resp_code == 404) {
 
110
            return 1;
 
111
        } else {
 
112
            return 0;
 
113
        }
 
114
    }
 
115
}
 
116
 
 
117
int ItmagesLogin(
 
118
        char* usr_login,
 
119
        char* usr_passwd,
 
120
        char *cookies_path,
 
121
        char *proxy_adrs,
 
122
        char *proxy_port)
 
123
{
 
124
    CURL *curl;
 
125
    FILE *write_file;
 
126
    CURLcode result;
 
127
    struct curl_httppost *post = NULL;
 
128
    struct curl_httppost *last = NULL;
 
129
 
 
130
    write_file = fopen("login.xml", "w");
 
131
    if (write_file == NULL) {
 
132
        ErrorsHandler(ERROR_WRITEFILE);
 
133
        return -1;
 
134
    } else {
 
135
        curl = curl_easy_init();
 
136
        if (curl) {
 
137
            curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, g_ErrorBuffer);
 
138
            curl_easy_setopt(curl, CURLOPT_URL, ITMAGES_LOGIN_URL);
 
139
            curl_easy_setopt(curl, CURLOPT_USERAGENT, ITMAGES_USER_AGENT);
 
140
            curl_easy_setopt(curl, CURLOPT_REFERER, ITMAGES_REFERER);
 
141
            curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip,deflate");
 
142
            curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookies_path);
 
143
            curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies_path);
 
144
            curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
 
145
            if (proxy_adrs != NULL && proxy_port != NULL) {
 
146
                size_t port_num = atoi(proxy_port);
 
147
                curl_easy_setopt(curl, CURLOPT_PROXY, proxy_adrs);
 
148
                curl_easy_setopt(curl, CURLOPT_PROXYPORT, port_num);
 
149
            }
 
150
            curl_easy_setopt(curl, CURLOPT_POST, 1);
 
151
            curl_formadd(
 
152
                    &post, &last,
 
153
                    CURLFORM_COPYNAME, "name",
 
154
                    CURLFORM_COPYCONTENTS, usr_login,
 
155
                    CURLFORM_END);
 
156
            curl_formadd(
 
157
                    &post, &last,
 
158
                    CURLFORM_COPYNAME, "passwd",
 
159
                    CURLFORM_COPYCONTENTS, usr_passwd,
 
160
                    CURLFORM_END);
 
161
            curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
 
162
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ItmagesCallbackWrite);
 
163
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_file);
 
164
            result = curl_easy_perform(curl);
 
165
        }
 
166
        curl_easy_cleanup(curl);
 
167
        if (result != CURLE_OK) {
 
168
            fclose(write_file);
 
169
            remove("login.xml");
 
170
            ErrorsHandler(ERROR_CURL);
 
171
            return -1;
 
172
        }
 
173
        fclose(write_file);
 
174
    }
 
175
 
 
176
    FILE *read_file;
 
177
    char *status;
 
178
 
 
179
    read_file = fopen("login.xml", "r");
 
180
    if (read_file == NULL) {
 
181
        ErrorsHandler(ERROR_READFILE);
 
182
        return -1;
 
183
    } else {
 
184
        mxml_node_t *response_tree;
 
185
        response_tree = mxmlLoadFile(NULL, read_file, MXML_OPAQUE_CALLBACK);
 
186
        fclose(read_file);
 
187
        remove("login.xml");
 
188
        mxml_node_t *response_node;
 
189
        response_node = mxmlFindElement(response_tree, response_tree,
 
190
                "response", NULL, NULL, MXML_DESCEND);
 
191
        if (response_node == NULL) {
 
192
            ErrorsHandler(ERROR_PARSERESPONSE);
 
193
            return -1;
 
194
        }
 
195
        status = response_node->child->child->value.opaque;
 
196
        if (memcmp(status, "ok", 3) == 0) {
 
197
            mxmlDelete(response_tree);
 
198
            return 0;
 
199
        } else {
 
200
            mxmlDelete(response_tree);
 
201
            return 1;
 
202
        }
 
203
    }
 
204
}
 
205
 
 
206
int ItmagesLogout(
 
207
        char *cookies_path,
 
208
        char *proxy_adrs,
 
209
        char *proxy_port)
 
210
{
 
211
    CURL *curl;
 
212
    CURLcode result;
 
213
    FILE *write_file;
 
214
    struct curl_httppost *post = NULL;
 
215
    struct curl_httppost *last = NULL;
 
216
 
 
217
    write_file = fopen("logout.xml", "w");
 
218
    if (write_file == NULL) {
 
219
        ErrorsHandler(ERROR_WRITEFILE);
 
220
        return -1;
 
221
    } else {
 
222
        curl = curl_easy_init();
 
223
        if (curl) {
 
224
            curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, g_ErrorBuffer);
 
225
            curl_easy_setopt(curl, CURLOPT_URL, ITMAGES_LOGOUT_URL);
 
226
            curl_easy_setopt(curl, CURLOPT_USERAGENT, ITMAGES_USER_AGENT);
 
227
            curl_easy_setopt(curl, CURLOPT_REFERER, ITMAGES_REFERER);
 
228
            curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip,deflate");
 
229
            curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies_path);
 
230
            curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookies_path);
 
231
            curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
 
232
            if (proxy_adrs != NULL && proxy_port != NULL) {
 
233
                size_t port_num = atoi(proxy_port);
 
234
                curl_easy_setopt(curl, CURLOPT_PROXY, proxy_adrs);
 
235
                curl_easy_setopt(curl, CURLOPT_PROXYPORT, port_num);
 
236
            }
 
237
            curl_easy_setopt(curl, CURLOPT_POST, 1);
 
238
            curl_formadd(
 
239
                    &post, &last,
 
240
                    CURLFORM_COPYNAME, "submit",
 
241
                    CURLFORM_COPYCONTENTS, "Logout",
 
242
                    CURLFORM_END);
 
243
            curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
 
244
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ItmagesCallbackWrite);
 
245
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_file);
 
246
            result = curl_easy_perform(curl);
 
247
        }
 
248
        curl_easy_cleanup(curl);
 
249
        if (result != CURLE_OK) {
 
250
            fclose(write_file);
 
251
            remove("logout.xml");
 
252
            ErrorsHandler(ERROR_CURL);
 
253
            return -1;
 
254
        }
 
255
        fclose(write_file);
 
256
    }
 
257
 
 
258
    FILE *read_file;
 
259
    char *status;
 
260
 
 
261
    read_file = fopen("logout.xml", "r");
 
262
    if (read_file == NULL) {
 
263
        ErrorsHandler(ERROR_READFILE);
 
264
        return -1;
 
265
    } else {
 
266
        mxml_node_t *response_tree;
 
267
        response_tree = mxmlLoadFile(NULL, read_file, MXML_OPAQUE_CALLBACK);
 
268
        fclose(read_file);
 
269
        remove("logout.xml");
 
270
        mxml_node_t *response_node;
 
271
        response_node = mxmlFindElement(response_tree, response_tree,
 
272
                "response", NULL, NULL, MXML_DESCEND);
 
273
        if (response_node == NULL) {
 
274
            ErrorsHandler(ERROR_PARSERESPONSE);
 
275
            return -1;
 
276
        }
 
277
        status = response_node->child->child->value.opaque;
 
278
        if (memcmp(status, "ok", 3) == 0) {
 
279
            mxmlDelete(response_tree);
 
280
            return 0;
 
281
        } else {
 
282
            mxmlDelete(response_tree);
 
283
            return 1;
 
284
        }
 
285
    }
 
286
}
 
287
 
 
288
int ItmagesUpload(
 
289
        HWND upload_progress,
 
290
        wchar_t *image_path,
 
291
        char *name,
 
292
        char *mime,
 
293
        char *cookies_path,
 
294
        char *proxy_adrs,
 
295
        char *proxy_port,
 
296
        size_t size,
 
297
        ItmagesImageLinks *imageLinks)
 
298
{
 
299
    CURL *curl;
 
300
    CURLcode result;
 
301
    FILE *write_file, *read_file;
 
302
    struct curl_httppost *post = NULL;
 
303
    struct curl_httppost *last = NULL;
 
304
 
 
305
    write_file = fopen("upload.xml", "w");
 
306
    if (write_file == NULL) {
 
307
        ErrorsHandler(ERROR_WRITEFILE);
 
308
        return -1;
 
309
    } else {
 
310
        read_file = _wfopen(image_path, L"rb");
 
311
        if (read_file == NULL) {
 
312
            ErrorsHandler(ERROR_READFILE);
 
313
            return -1;
 
314
        } else {
 
315
            curl = curl_easy_init();
 
316
            if (curl) {
 
317
                curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, g_ErrorBuffer);
 
318
                curl_easy_setopt(curl, CURLOPT_URL, ITMAGES_ADD_URL);
 
319
                curl_easy_setopt(curl, CURLOPT_USERAGENT, ITMAGES_USER_AGENT);
 
320
                curl_easy_setopt(curl, CURLOPT_REFERER, ITMAGES_REFERER);
 
321
                curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip, default");
 
322
                curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookies_path);
 
323
                curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies_path);
 
324
                curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
 
325
                if (proxy_adrs != NULL && proxy_port != NULL) {
 
326
                    size_t port_num = atoi(proxy_port);
 
327
                    curl_easy_setopt(curl, CURLOPT_PROXY, proxy_adrs);
 
328
                    curl_easy_setopt(curl, CURLOPT_PROXYPORT, port_num);
 
329
                }
 
330
                curl_easy_setopt(curl, CURLOPT_POST, 1);
 
331
                curl_formadd(
 
332
                        &post, &last,
 
333
                        CURLFORM_COPYNAME, "UFileManager[picture]",
 
334
                        CURLFORM_FILENAME, name,
 
335
                        CURLFORM_STREAM, read_file,
 
336
                        CURLFORM_CONTENTSLENGTH, size,
 
337
                        CURLFORM_CONTENTTYPE, mime,
 
338
                        CURLFORM_END);
 
339
                curl_formadd(
 
340
                        &post, &last,
 
341
                        CURLFORM_COPYNAME, "UFileManager[picture]",
 
342
                        CURLFORM_COPYCONTENTS, "",
 
343
                        CURLFORM_END);
 
344
                curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
 
345
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ItmagesCallbackWrite);
 
346
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_file);
 
347
                curl_easy_setopt(curl, CURLOPT_READFUNCTION, ItmagesCallbackRead);
 
348
                curl_easy_setopt(curl, CURLOPT_READDATA, read_file);
 
349
                curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
 
350
                curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, CallbackProgressbar);
 
351
                curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, upload_progress);
 
352
                result = curl_easy_perform(curl);
 
353
            }
 
354
            curl_easy_cleanup(curl);
 
355
            if (result != CURLE_OK) {
 
356
                fclose(write_file);
 
357
                fclose(read_file);
 
358
                remove("upload.xml");
 
359
                ErrorsHandler(ERROR_CURL);
 
360
                return -1;
 
361
            }
 
362
            fclose(write_file);
 
363
            fclose(read_file);
 
364
        }
 
365
 
 
366
        FILE *read_file;
 
367
                size_t node_len = 0;
 
368
 
 
369
        read_file = fopen("upload.xml", "r");
 
370
        if (read_file == NULL) {
 
371
            ErrorsHandler(ERROR_READFILE);
 
372
            return -1;
 
373
        } else {
 
374
            mxml_node_t *response_tree;
 
375
            response_tree = mxmlLoadFile(NULL, read_file, MXML_OPAQUE_CALLBACK);
 
376
            fclose(read_file);
 
377
            remove("upload.xml");
 
378
            mxml_node_t *response_node;
 
379
            response_node = mxmlFindElement(response_tree, response_tree,
 
380
                    "obj", NULL, NULL, MXML_DESCEND);
 
381
            if (response_node == NULL) {
 
382
                ErrorsHandler(ERROR_PARSERESPONSE);
 
383
                return -1;
 
384
            }
 
385
            response_node = mxmlFindElement(response_node, response_tree,
 
386
                    "full", NULL, NULL, MXML_DESCEND);
 
387
            if (response_node == NULL) {
 
388
                ErrorsHandler(ERROR_PARSERESPONSE);
 
389
                return -1;
 
390
            }
 
391
 
 
392
            node_len = strlen(response_node->child->value.opaque);
 
393
            imageLinks->full = (char*) malloc((node_len + 1) * sizeof(char));
 
394
            if (imageLinks->full == NULL) {
 
395
                ErrorsHandler(ERROR_MEMALLOC);
 
396
                exit(EXIT_FAILURE);
 
397
            }
 
398
            memcpy(imageLinks->full, response_node->child->value.opaque, node_len + 1);
 
399
 
 
400
            node_len = strlen(response_node->next->child->value.opaque);
 
401
            imageLinks->thumb = (char*) malloc((node_len + 1) * sizeof(char));
 
402
            if (imageLinks->thumb == NULL) {
 
403
                ErrorsHandler(ERROR_MEMALLOC);
 
404
                exit(EXIT_FAILURE);
 
405
            }
 
406
            memcpy(imageLinks->thumb, response_node->next->child->value.opaque, node_len + 1);
 
407
 
 
408
            response_node = mxmlFindElement(response_node, response_tree,
 
409
                    "id", NULL, NULL, MXML_DESCEND);
 
410
            if (response_node == NULL) {
 
411
                ErrorsHandler(ERROR_PARSERESPONSE);
 
412
                return -1;
 
413
            }
 
414
 
 
415
            node_len = strlen(response_node->child->value.opaque);
 
416
            imageLinks->id = (char*) malloc((node_len + 1) * sizeof(char));
 
417
            if (imageLinks->id == NULL) {
 
418
                ErrorsHandler(ERROR_MEMALLOC);
 
419
                exit(EXIT_FAILURE);
 
420
            }
 
421
            memcpy(imageLinks->id, response_node->child->value.opaque, node_len + 1);
 
422
 
 
423
            node_len = strlen(response_node->next->child->value.opaque);
 
424
            imageLinks->server = (char*) malloc((node_len + 1) * sizeof(char));
 
425
            if (imageLinks->server == NULL) {
 
426
                ErrorsHandler(ERROR_MEMALLOC);
 
427
                exit(EXIT_FAILURE);
 
428
            }
 
429
            memcpy(imageLinks->server, response_node->next->child->value.opaque, node_len + 1);
 
430
 
 
431
            node_len = strlen(response_node->next->next->child->value.opaque);
 
432
            imageLinks->key = (char*) malloc((node_len + 1) * sizeof(char));
 
433
            if (imageLinks->key == NULL) {
 
434
                ErrorsHandler(ERROR_MEMALLOC);
 
435
                exit(EXIT_FAILURE);
 
436
            }
 
437
            memcpy(imageLinks->key, response_node->next->next->child->value.opaque, node_len + 1);
 
438
 
 
439
            mxmlDelete(response_tree);
 
440
            return 0;
 
441
        }
 
442
    }
 
443
}
 
444
 
 
445
void FreeImageLinks(ItmagesImageLinks *imageLinks)
 
446
{
 
447
    if (imageLinks->full != NULL) {
 
448
        free(imageLinks->full);
 
449
        imageLinks->full = NULL;
 
450
    }
 
451
    if (imageLinks->thumb != NULL) {
 
452
        free(imageLinks->thumb);
 
453
        imageLinks->thumb = NULL;
 
454
    }
 
455
    if (imageLinks->id != NULL) {
 
456
        free(imageLinks->id);
 
457
        imageLinks->id = NULL;
 
458
    }
 
459
    if (imageLinks->server != NULL) {
 
460
        free(imageLinks->server);
 
461
        imageLinks->server = NULL;
 
462
    }
 
463
    if (imageLinks->key != NULL) {
 
464
        free(imageLinks->key);
 
465
        imageLinks->key = NULL;
 
466
    }
 
467
}