1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.ComponentModel; |
---|
4 | using System.Data; |
---|
5 | using System.Drawing; |
---|
6 | using System.Linq; |
---|
7 | using System.Text; |
---|
8 | using System.Windows.Forms; |
---|
9 | using System.Diagnostics; |
---|
10 | using System.IO; |
---|
11 | using System.Net; |
---|
12 | using System.Xml; |
---|
13 | using System.Resources; |
---|
14 | using System.Globalization; |
---|
15 | using System.Threading; |
---|
16 | using Picasa2PiwigoConstants; |
---|
17 | |
---|
18 | namespace Picasa2Piwigo |
---|
19 | { |
---|
20 | using Picasa2PiwigoConstants; |
---|
21 | using Picasa2PiwigoCommonFunctions; |
---|
22 | |
---|
23 | public partial class StatusForm : Form |
---|
24 | { |
---|
25 | bool stop_button_pressed = false; |
---|
26 | int command_to_run; |
---|
27 | string[] local_command_line_args; |
---|
28 | int[] piwigo_person_tag_count = new int[constants.MAX_NUMBER_OF_CONTACTS]; |
---|
29 | |
---|
30 | CookieContainer cookiesjar = new CookieContainer(); |
---|
31 | public StatusForm(int command, string[] command_line_args) |
---|
32 | { |
---|
33 | CultureInfo ci; |
---|
34 | ci = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name); |
---|
35 | Thread.CurrentThread.CurrentUICulture = ci; |
---|
36 | Thread.CurrentThread.CurrentCulture = ci; |
---|
37 | if (Picasa2PiwigoConstants.constants.DEBUG_LOCALE) |
---|
38 | { |
---|
39 | #pragma warning disable 0162 |
---|
40 | Thread.CurrentThread.CurrentUICulture = new CultureInfo(Picasa2PiwigoConstants.constants.SET_LOCALE); |
---|
41 | #pragma warning restore 0162 |
---|
42 | } |
---|
43 | InitializeComponent(); |
---|
44 | command_to_run = command; |
---|
45 | local_command_line_args = command_line_args; |
---|
46 | } |
---|
47 | |
---|
48 | private void StatusForm_Load(object sender, EventArgs e) |
---|
49 | { |
---|
50 | bool step_passed; |
---|
51 | XmlDocument TagList = new XmlDocument(); |
---|
52 | XmlDocument AlbumList = new XmlDocument(); |
---|
53 | int NewPiwigoAlbumId; |
---|
54 | string PiwigoVersionNumber; |
---|
55 | |
---|
56 | this.button1.Text = WinFormStrings.SFbutton1StrRes; |
---|
57 | this.Text = WinFormStrings.SFCaptionsStrRes; |
---|
58 | this.label1.Text = WinFormStrings.SFlabel1StrRes; |
---|
59 | |
---|
60 | LabelAdjust AdjustLabelClass = new LabelAdjust(); |
---|
61 | AdjustLabelClass.AdjustLabelSize(label1); |
---|
62 | AdjustLabelClass.AdjustButtonSize(button1); |
---|
63 | |
---|
64 | DialogResult = DialogResult.Yes; |
---|
65 | this.Show(); |
---|
66 | StatusRefresh(); |
---|
67 | |
---|
68 | //4. Retrieve tags |
---|
69 | //SetStatus("Retrieving tags..."); |
---|
70 | //step_passed = UpdateStatus(SyncPiwigoGetTags(ref TagList)); |
---|
71 | //if (!step_passed) |
---|
72 | // { |
---|
73 | // return; |
---|
74 | //} |
---|
75 | |
---|
76 | switch (command_to_run) |
---|
77 | { |
---|
78 | case constants.COMMAND_START_UP: |
---|
79 | WriteLog("Start up"); |
---|
80 | |
---|
81 | //1. Reading Picasa contacts |
---|
82 | SetStatus(WinFormStrings.ReadContactsFileStrRes); |
---|
83 | step_passed = UpdateStatus((SyncPiwigoReadPicasaContacts())); |
---|
84 | if (stop_button_pressed) |
---|
85 | { |
---|
86 | return; |
---|
87 | } |
---|
88 | if (!step_passed) |
---|
89 | { |
---|
90 | return; |
---|
91 | } |
---|
92 | |
---|
93 | //2. Validate that server can be reached |
---|
94 | SetStatus(WinFormStrings.CheckServerConnStrRes); |
---|
95 | step_passed = UpdateStatus((SyncPiwigoCheckServerStatus())); |
---|
96 | if (!step_passed) |
---|
97 | { |
---|
98 | return; |
---|
99 | } |
---|
100 | if (stop_button_pressed) |
---|
101 | { |
---|
102 | return; |
---|
103 | } |
---|
104 | |
---|
105 | //3. Login |
---|
106 | SetStatus(WinFormStrings.LogInStrRes); |
---|
107 | step_passed = UpdateStatus(SyncPiwigoLogIn()); |
---|
108 | if (!step_passed) |
---|
109 | { |
---|
110 | return; |
---|
111 | } |
---|
112 | if (stop_button_pressed) |
---|
113 | { |
---|
114 | return; |
---|
115 | } |
---|
116 | |
---|
117 | //4. Get Piwigo Version |
---|
118 | PiwigoVersionNumber = ""; |
---|
119 | SetStatus(WinFormStrings.GetPiwigoVersionStrRes); |
---|
120 | step_passed = UpdateStatus(SyncPiwigoGetVersion(ref PiwigoVersionNumber)); |
---|
121 | if (stop_button_pressed) |
---|
122 | { |
---|
123 | return; |
---|
124 | } |
---|
125 | GlobalClass.PiwigoVersionNumber = PiwigoVersionNumber; |
---|
126 | |
---|
127 | //5. Get list of albums |
---|
128 | SetStatus(WinFormStrings.RetrieveAlbumListStRes); |
---|
129 | step_passed = UpdateStatus(SyncPiwigoGetAlbums(ref AlbumList)); |
---|
130 | if (step_passed) |
---|
131 | { |
---|
132 | GlobalClass.AlbumList = AlbumList; |
---|
133 | return; |
---|
134 | } |
---|
135 | if (stop_button_pressed) |
---|
136 | { |
---|
137 | return; |
---|
138 | } |
---|
139 | break; |
---|
140 | |
---|
141 | case constants.COMMAND_ADD_TO_ALBUM: |
---|
142 | WriteLog("Add to Album"); |
---|
143 | |
---|
144 | //1. Validate that server can be reached |
---|
145 | SetStatus(WinFormStrings.CheckServerConnStrRes); |
---|
146 | step_passed = UpdateStatus((SyncPiwigoCheckServerStatus())); |
---|
147 | if (!step_passed) |
---|
148 | { |
---|
149 | return; |
---|
150 | } |
---|
151 | if (stop_button_pressed) |
---|
152 | { |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|
156 | //2. Login |
---|
157 | SetStatus(WinFormStrings.LogInStrRes); |
---|
158 | step_passed = UpdateStatus(SyncPiwigoLogIn()); |
---|
159 | if (!step_passed) |
---|
160 | { |
---|
161 | return; |
---|
162 | } |
---|
163 | if (stop_button_pressed) |
---|
164 | { |
---|
165 | return; |
---|
166 | } |
---|
167 | |
---|
168 | //3. upload photos |
---|
169 | NewPiwigoAlbumId = GlobalClass.AddAlbumCategoryID; |
---|
170 | SetStatus(WinFormStrings.UploadPhotosStrRes); |
---|
171 | step_passed = UpdateStatus(SyncPiwigoAddPhoto(NewPiwigoAlbumId)); |
---|
172 | if (!step_passed) |
---|
173 | { |
---|
174 | return; |
---|
175 | } |
---|
176 | if (stop_button_pressed) |
---|
177 | { |
---|
178 | return; |
---|
179 | } |
---|
180 | |
---|
181 | break; |
---|
182 | |
---|
183 | case constants.COMMAND_NEW_ALBUM: |
---|
184 | WriteLog("New Album"); |
---|
185 | |
---|
186 | //1. Validate that server can be reached |
---|
187 | SetStatus(WinFormStrings.CheckServerConnStrRes); |
---|
188 | step_passed = UpdateStatus((SyncPiwigoCheckServerStatus())); |
---|
189 | if (!step_passed) |
---|
190 | { |
---|
191 | return; |
---|
192 | } |
---|
193 | if (stop_button_pressed) |
---|
194 | { |
---|
195 | return; |
---|
196 | } |
---|
197 | |
---|
198 | //2. Login |
---|
199 | SetStatus(WinFormStrings.LogInStrRes); |
---|
200 | step_passed = UpdateStatus(SyncPiwigoLogIn()); |
---|
201 | if (!step_passed) |
---|
202 | { |
---|
203 | return; |
---|
204 | } |
---|
205 | if (stop_button_pressed) |
---|
206 | { |
---|
207 | return; |
---|
208 | } |
---|
209 | |
---|
210 | //3. Create new album |
---|
211 | SetStatus(WinFormStrings.CreateNewAlbumRes); |
---|
212 | NewPiwigoAlbumId = -1; |
---|
213 | step_passed = UpdateStatus(SyncPiwigoCreateAlbum(ref NewPiwigoAlbumId)); |
---|
214 | if (!step_passed) |
---|
215 | { |
---|
216 | return; |
---|
217 | } |
---|
218 | if (stop_button_pressed) |
---|
219 | { |
---|
220 | return; |
---|
221 | } |
---|
222 | |
---|
223 | //4. upload photos |
---|
224 | SetStatus(WinFormStrings.UploadPhotosStrRes); |
---|
225 | step_passed = UpdateStatus(SyncPiwigoAddPhoto(NewPiwigoAlbumId)); |
---|
226 | if (!step_passed) |
---|
227 | { |
---|
228 | return; |
---|
229 | } |
---|
230 | if (stop_button_pressed) |
---|
231 | { |
---|
232 | return; |
---|
233 | } |
---|
234 | |
---|
235 | break; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | public int SearchForNameXML(byte[] filedata, byte[] name_sig, int k, int exif_length) |
---|
240 | { |
---|
241 | |
---|
242 | byte firstByte = name_sig[0]; |
---|
243 | int index = -1; |
---|
244 | int n, match_count; |
---|
245 | |
---|
246 | n = k; |
---|
247 | do |
---|
248 | { |
---|
249 | match_count = 0; |
---|
250 | if ((index = Array.IndexOf(filedata, firstByte, n)) >= 0) |
---|
251 | { |
---|
252 | for (int i = 0; i < name_sig.Length; i++) |
---|
253 | { |
---|
254 | if (index + i > exif_length - 2) |
---|
255 | { |
---|
256 | break; |
---|
257 | } |
---|
258 | if (name_sig[i] != filedata[index + i]) |
---|
259 | { |
---|
260 | break; |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | match_count++; |
---|
265 | } |
---|
266 | } |
---|
267 | if (match_count == name_sig.Length) |
---|
268 | { |
---|
269 | //found a person |
---|
270 | return index + name_sig.Length; |
---|
271 | } |
---|
272 | } |
---|
273 | n = index + 1; |
---|
274 | } while (index != -1); |
---|
275 | return index; |
---|
276 | } |
---|
277 | private static int ByteSearch(byte[] searchIn, byte[] searchBytes, int start) |
---|
278 | { |
---|
279 | int found = -1; |
---|
280 | bool matched = false; |
---|
281 | //only look at this if we have a populated search array and search bytes with a sensible start |
---|
282 | if (searchIn.Length > 0 && searchBytes.Length > 0 && start <= (searchIn.Length - searchBytes.Length) && searchIn.Length >= searchBytes.Length) |
---|
283 | { |
---|
284 | //iterate through the array to be searched |
---|
285 | for (int i = start; i <= searchIn.Length - searchBytes.Length; i++) |
---|
286 | { |
---|
287 | //if the start bytes match we will start comparing all other bytes |
---|
288 | if (searchIn[i] == searchBytes[0]) |
---|
289 | { |
---|
290 | if (searchIn.Length > 1) |
---|
291 | { |
---|
292 | //multiple bytes to be searched we have to compare byte by byte |
---|
293 | matched = true; |
---|
294 | for (int y = 1; y <= searchBytes.Length - 1; y++) |
---|
295 | { |
---|
296 | if (searchIn[i + y] != searchBytes[y]) |
---|
297 | { |
---|
298 | matched = false; |
---|
299 | break; |
---|
300 | } |
---|
301 | } |
---|
302 | //everything matched up |
---|
303 | if (matched) |
---|
304 | { |
---|
305 | found = i; |
---|
306 | break; |
---|
307 | } |
---|
308 | |
---|
309 | } |
---|
310 | else |
---|
311 | { |
---|
312 | //search byte is only one bit nothing else to do |
---|
313 | found = i; |
---|
314 | break; //stop the loop |
---|
315 | } |
---|
316 | |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | } |
---|
321 | return found; |
---|
322 | } |
---|
323 | public string GetCommentListFromXml(string xmlstring) |
---|
324 | { |
---|
325 | string tag_list, s; |
---|
326 | int j, k, l, m, n; |
---|
327 | |
---|
328 | tag_list = ""; |
---|
329 | k = xmlstring.IndexOf("<dc:description>"); |
---|
330 | if (k != -1) |
---|
331 | { |
---|
332 | l = xmlstring.IndexOf("/dc:description", k); |
---|
333 | if (l != -1) |
---|
334 | { |
---|
335 | j = k; |
---|
336 | do |
---|
337 | { |
---|
338 | j = xmlstring.IndexOf("<rdf:li", j+1); |
---|
339 | if (j != -1 && j < l) |
---|
340 | { |
---|
341 | n = xmlstring.IndexOf(">", j); |
---|
342 | if (n != -1 && n < l) |
---|
343 | { |
---|
344 | m = xmlstring.IndexOf("</rdf:li>", j); |
---|
345 | if (m != -1 && m < l) |
---|
346 | { |
---|
347 | s = xmlstring.Substring(n + 1, m - n - 1); |
---|
348 | tag_list += RemoveCommaFromContract(s); |
---|
349 | tag_list += ","; |
---|
350 | } |
---|
351 | } |
---|
352 | } |
---|
353 | } while (j != -1); |
---|
354 | } |
---|
355 | } |
---|
356 | if (tag_list.Length > 1) |
---|
357 | { |
---|
358 | tag_list = tag_list.Substring(0, tag_list.Length - 1); |
---|
359 | } |
---|
360 | |
---|
361 | return tag_list; |
---|
362 | } |
---|
363 | |
---|
364 | public string GetTagListFromXml(string xmlstring) |
---|
365 | { |
---|
366 | string tag_list, s; |
---|
367 | int j, k, l, m; |
---|
368 | |
---|
369 | tag_list = ""; |
---|
370 | k = xmlstring.IndexOf("<dc:subject>"); |
---|
371 | if (k != -1) |
---|
372 | { |
---|
373 | l = xmlstring.IndexOf("</dc:subject>",k); |
---|
374 | if (l != -1) |
---|
375 | { |
---|
376 | j = k; |
---|
377 | do |
---|
378 | { |
---|
379 | j = xmlstring.IndexOf("<rdf:li>", j+1); |
---|
380 | if (j != -1 && j < l) |
---|
381 | { |
---|
382 | m = xmlstring.IndexOf("</rdf:li>", j); |
---|
383 | if (m != -1 && m < l) |
---|
384 | { |
---|
385 | s = xmlstring.Substring(j + 8, m - j - 8); |
---|
386 | tag_list += RemoveCommaFromContract(s); |
---|
387 | tag_list += ","; |
---|
388 | } |
---|
389 | } |
---|
390 | } while (j != -1); |
---|
391 | } |
---|
392 | } |
---|
393 | if (tag_list.Length > 1) |
---|
394 | { |
---|
395 | tag_list = tag_list.Substring(0, tag_list.Length - 1); |
---|
396 | } |
---|
397 | |
---|
398 | return tag_list; |
---|
399 | } |
---|
400 | |
---|
401 | public void SyncPiwigoGetTagsandComments(ref string tag_list, ref string comment_list, string filename) |
---|
402 | { |
---|
403 | byte[] filedata = new byte[100 * 1024]; |
---|
404 | byte[] name_sig = new byte[] {0x44, 0x65, 0x73, 0x63, 0x72, 0x69, |
---|
405 | 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x6D, 0x77, 0x67, 0x2D, 0x72, 0x73, 0x3A, |
---|
406 | 0x4E, 0x61, 0x6D, 0x65, 0x3D, 0x22 }; |
---|
407 | int temp1, temp2, k, i, l, m; |
---|
408 | UInt16 signature, exif_length; |
---|
409 | byte[] temp_array = new byte[1]; |
---|
410 | byte[] xml_start = { 0x3C, 0x78, 0x3A, 0x78, 0x6D, 0x70, 0x6D, 0x65, 0x74, 0x61 }; |
---|
411 | byte[] xml_end = { 0x3C, 0x2F, 0x78, 0x3A, 0x78, 0x6D, 0x70, 0x6D, 0x65, 0x74, 0x61, 0x3E }; |
---|
412 | string xml_content; |
---|
413 | XmlDocument xmldoc = new XmlDocument(); |
---|
414 | |
---|
415 | try |
---|
416 | { |
---|
417 | BinaryReader b = new BinaryReader(File.Open(filename, FileMode.Open)); |
---|
418 | |
---|
419 | signature = b.ReadUInt16(); |
---|
420 | if (signature != 0xD8FF) |
---|
421 | { |
---|
422 | b.Close(); |
---|
423 | return; |
---|
424 | } |
---|
425 | signature = b.ReadUInt16(); |
---|
426 | if (signature != 0xE1FF && signature != 0xE0FF) |
---|
427 | { |
---|
428 | b.Close(); |
---|
429 | return; |
---|
430 | } |
---|
431 | for (i = 0; i < 2; i++) |
---|
432 | { |
---|
433 | //get length |
---|
434 | temp1 = b.ReadByte(); |
---|
435 | temp2 = b.ReadByte(); |
---|
436 | exif_length = (UInt16)((temp1 << 8) + temp2); |
---|
437 | filedata = b.ReadBytes(exif_length - 1); |
---|
438 | |
---|
439 | //search file data for xml |
---|
440 | l = -1; |
---|
441 | k = ByteSearch(filedata, xml_start, 0); |
---|
442 | if (k != -1) |
---|
443 | { |
---|
444 | l = ByteSearch(filedata, xml_end, k); |
---|
445 | } |
---|
446 | |
---|
447 | if (k != -1 && l != -1) |
---|
448 | { |
---|
449 | xml_content = System.Text.Encoding.UTF8.GetString(filedata, k, l - k + xml_end.Length); |
---|
450 | tag_list = GetTagListFromXml(xml_content); |
---|
451 | comment_list = GetCommentListFromXml(xml_content); |
---|
452 | break; |
---|
453 | } |
---|
454 | m = exif_length; |
---|
455 | if (m > filedata.Length) |
---|
456 | { |
---|
457 | m = filedata.Length + 1; |
---|
458 | } |
---|
459 | Array.Clear(filedata, 0, m - 1); |
---|
460 | } |
---|
461 | |
---|
462 | |
---|
463 | b.Close(); |
---|
464 | } |
---|
465 | catch (IOException e) |
---|
466 | { |
---|
467 | e.ToString(); |
---|
468 | return; |
---|
469 | } |
---|
470 | catch (Exception e) |
---|
471 | { |
---|
472 | e.ToString(); |
---|
473 | return; |
---|
474 | } |
---|
475 | |
---|
476 | |
---|
477 | } |
---|
478 | public void SearchForNamesInExif(ref bool[] found_contact_in_photo_array, constants.PersonIDStruct[] contact_array, string filename, int person_count) |
---|
479 | { |
---|
480 | byte[] filedata = new byte[100 * 1024]; |
---|
481 | byte[] name_sig = new byte[] {0x44, 0x65, 0x73, 0x63, 0x72, 0x69, |
---|
482 | 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x6D, 0x77, 0x67, 0x2D, 0x72, 0x73, 0x3A, |
---|
483 | 0x4E, 0x61, 0x6D, 0x65, 0x3D, 0x22 }; |
---|
484 | int temp1, temp2, k, i, m; |
---|
485 | UInt16 signature, exif_length; |
---|
486 | string name_from_photo, tempstr; |
---|
487 | byte[] temp_array = new byte[1]; |
---|
488 | |
---|
489 | try |
---|
490 | { |
---|
491 | BinaryReader b = new BinaryReader(File.Open(filename, FileMode.Open)); |
---|
492 | |
---|
493 | signature = b.ReadUInt16(); |
---|
494 | if (signature != 0xD8FF) |
---|
495 | { |
---|
496 | b.Close(); |
---|
497 | return; |
---|
498 | } |
---|
499 | signature = b.ReadUInt16(); |
---|
500 | if (signature != 0xE1FF && signature != 0xE0FF) |
---|
501 | { |
---|
502 | b.Close(); |
---|
503 | return; |
---|
504 | } |
---|
505 | for (i = 0; i < 2; i++) |
---|
506 | { |
---|
507 | //get length |
---|
508 | temp1 = b.ReadByte(); |
---|
509 | temp2 = b.ReadByte(); |
---|
510 | exif_length = (UInt16)((temp1 << 8) + temp2); |
---|
511 | filedata = b.ReadBytes(exif_length - 1); |
---|
512 | k = 0; |
---|
513 | do |
---|
514 | { |
---|
515 | k = SearchForNameXML(filedata, name_sig, k, exif_length); |
---|
516 | if (k != -1) |
---|
517 | { |
---|
518 | name_from_photo = ""; |
---|
519 | do |
---|
520 | { |
---|
521 | //found a name |
---|
522 | temp_array[0] = filedata[k]; |
---|
523 | tempstr = Encoding.Default.GetString(temp_array); |
---|
524 | if (tempstr != "\"") |
---|
525 | { |
---|
526 | name_from_photo += tempstr; |
---|
527 | } |
---|
528 | else |
---|
529 | { |
---|
530 | if (name_from_photo.Substring(0, 1) == "0") |
---|
531 | { |
---|
532 | name_from_photo = name_from_photo.Substring(1); |
---|
533 | } |
---|
534 | //have name, search for it in array |
---|
535 | for (i = 0; i < person_count; i++) |
---|
536 | { |
---|
537 | if (name_from_photo == contact_array[i].name) |
---|
538 | { |
---|
539 | found_contact_in_photo_array[i] = true; |
---|
540 | break; |
---|
541 | } |
---|
542 | } |
---|
543 | break; |
---|
544 | } |
---|
545 | k++; |
---|
546 | } while (tempstr != "\""); |
---|
547 | } |
---|
548 | k++; |
---|
549 | } while (k != 0); |
---|
550 | m = exif_length; |
---|
551 | if (m > filedata.Length) |
---|
552 | { |
---|
553 | m = filedata.Length + 1; |
---|
554 | } |
---|
555 | Array.Clear(filedata, 0, m - 1); |
---|
556 | } |
---|
557 | |
---|
558 | |
---|
559 | b.Close(); |
---|
560 | } |
---|
561 | catch (IOException e) |
---|
562 | { |
---|
563 | e.ToString(); |
---|
564 | return; |
---|
565 | } |
---|
566 | catch (Exception e) |
---|
567 | { |
---|
568 | e.ToString(); |
---|
569 | return; |
---|
570 | } |
---|
571 | } |
---|
572 | public string RemoveCommaFromContract(string contact) |
---|
573 | { |
---|
574 | string cleaned_name, s, t; |
---|
575 | int k; |
---|
576 | |
---|
577 | cleaned_name = contact; |
---|
578 | |
---|
579 | //do we have to remove commma |
---|
580 | if (HaveToRemoveComma()) |
---|
581 | { |
---|
582 | do |
---|
583 | { |
---|
584 | k = cleaned_name.IndexOf(","); |
---|
585 | if (k != -1) |
---|
586 | { |
---|
587 | s = cleaned_name.Substring(0, k); |
---|
588 | t = cleaned_name.Substring(k + 1); |
---|
589 | cleaned_name = s + t; |
---|
590 | } |
---|
591 | } while (k != -1); |
---|
592 | } |
---|
593 | else |
---|
594 | { |
---|
595 | //commas are ok, so escape them |
---|
596 | cleaned_name = cleaned_name.Replace(",", "/,"); |
---|
597 | } |
---|
598 | return cleaned_name; |
---|
599 | } |
---|
600 | public string SyncPiwigoGetFaceTags(string filename) |
---|
601 | { |
---|
602 | int k, l; |
---|
603 | string filepath, inidata, photofilename, photo_file_name, ini_unique_id; |
---|
604 | string face_tag_list; |
---|
605 | bool[] found_contact_in_photo_array = new bool[256]; |
---|
606 | bool found_photo; |
---|
607 | bool finished_faces, found_person; |
---|
608 | System.IO.StreamReader inifile; |
---|
609 | |
---|
610 | face_tag_list = ""; |
---|
611 | |
---|
612 | k = filename.LastIndexOf("\\"); |
---|
613 | filepath = filename.Substring(0, k); |
---|
614 | photofilename = filename.Substring(k + 1); |
---|
615 | photo_file_name = ""; |
---|
616 | |
---|
617 | //if it's a video file then skip it |
---|
618 | if (photofilename.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) == -1 && |
---|
619 | photofilename.IndexOf(".gif", StringComparison.OrdinalIgnoreCase) == -1 && |
---|
620 | photofilename.IndexOf(".png", StringComparison.OrdinalIgnoreCase) == -1 && |
---|
621 | photofilename.IndexOf(".tga", StringComparison.OrdinalIgnoreCase) == -1 && |
---|
622 | photofilename.IndexOf(".tif", StringComparison.OrdinalIgnoreCase) == -1 && |
---|
623 | photofilename.IndexOf(".tiff", StringComparison.OrdinalIgnoreCase) == -1 && |
---|
624 | photofilename.IndexOf(".psd", StringComparison.OrdinalIgnoreCase) == -1) |
---|
625 | { |
---|
626 | return face_tag_list; |
---|
627 | } |
---|
628 | |
---|
629 | //parse photo |
---|
630 | for (k = 0; k < GlobalClass.person_count; k++) |
---|
631 | { |
---|
632 | found_contact_in_photo_array[k] = false; |
---|
633 | } |
---|
634 | SearchForNamesInExif(ref found_contact_in_photo_array, GlobalClass.contact_array, filename, GlobalClass.person_count); |
---|
635 | for (k = 0; k < GlobalClass.person_count; k++) |
---|
636 | { |
---|
637 | if (found_contact_in_photo_array[k]) |
---|
638 | { |
---|
639 | face_tag_list += RemoveCommaFromContract(GlobalClass.contact_array[k].name) + ","; |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | inifile = null; |
---|
644 | try |
---|
645 | { |
---|
646 | inifile = new System.IO.StreamReader(filepath + "\\.picasa.ini"); |
---|
647 | } |
---|
648 | catch |
---|
649 | { |
---|
650 | if (face_tag_list.Length > 1) |
---|
651 | { |
---|
652 | face_tag_list = face_tag_list.Substring(0, face_tag_list.Length - 1); |
---|
653 | } |
---|
654 | return face_tag_list; |
---|
655 | } |
---|
656 | k = 0; |
---|
657 | found_photo = false; |
---|
658 | while ((inidata = inifile.ReadLine()) != null) |
---|
659 | { |
---|
660 | if (inidata.Length > 4) |
---|
661 | { |
---|
662 | if (inidata.Substring(0, 1) == "[") |
---|
663 | { |
---|
664 | found_photo = false; |
---|
665 | k = inidata.IndexOf("]"); |
---|
666 | if (k != -1) |
---|
667 | { |
---|
668 | photo_file_name = inidata.Substring(1, k - 1); |
---|
669 | } |
---|
670 | if (photo_file_name != photofilename) |
---|
671 | { |
---|
672 | continue; |
---|
673 | } |
---|
674 | |
---|
675 | if (photo_file_name.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) != -1 || |
---|
676 | photo_file_name.IndexOf(".gif", StringComparison.OrdinalIgnoreCase) != -1 || |
---|
677 | photo_file_name.IndexOf(".png", StringComparison.OrdinalIgnoreCase) != -1 || |
---|
678 | photo_file_name.IndexOf(".tga", StringComparison.OrdinalIgnoreCase) != -1 || |
---|
679 | photo_file_name.IndexOf(".tif", StringComparison.OrdinalIgnoreCase) != -1 || |
---|
680 | photo_file_name.IndexOf(".tiff", StringComparison.OrdinalIgnoreCase) != -1 || |
---|
681 | photo_file_name.IndexOf(".psd", StringComparison.OrdinalIgnoreCase) != -1) |
---|
682 | { |
---|
683 | found_photo = true; |
---|
684 | } |
---|
685 | } |
---|
686 | else if (inidata.Substring(0, 6) == "faces=" && found_photo) |
---|
687 | { |
---|
688 | finished_faces = false; |
---|
689 | k = 0; |
---|
690 | do |
---|
691 | { |
---|
692 | k = inidata.IndexOf("),", k); |
---|
693 | if (k != -1) |
---|
694 | { |
---|
695 | l = inidata.IndexOf(";", k); |
---|
696 | if (l == -1) |
---|
697 | { |
---|
698 | l = inidata.Length; |
---|
699 | } |
---|
700 | if (l != -1) |
---|
701 | { |
---|
702 | ini_unique_id = inidata.Substring(k + 2, l - k - 2); |
---|
703 | |
---|
704 | //found face in a picture, but who? |
---|
705 | l = 0; |
---|
706 | found_person = false; |
---|
707 | do |
---|
708 | { |
---|
709 | if (GlobalClass.picasa_unique_id_arrary[l].unique_id == ini_unique_id) |
---|
710 | { |
---|
711 | //did we already find this person? |
---|
712 | if (found_contact_in_photo_array[l] == false) |
---|
713 | { |
---|
714 | found_person = true; |
---|
715 | break; |
---|
716 | } |
---|
717 | } |
---|
718 | l++; |
---|
719 | } while (l < GlobalClass.unique_id_count); |
---|
720 | |
---|
721 | //found person |
---|
722 | if (found_person) |
---|
723 | { |
---|
724 | if (photo_file_name.Length > 3) |
---|
725 | { |
---|
726 | face_tag_list += GlobalClass.contact_array[l].name + ","; |
---|
727 | } |
---|
728 | } |
---|
729 | } |
---|
730 | k++; |
---|
731 | } |
---|
732 | else |
---|
733 | { |
---|
734 | finished_faces = true; |
---|
735 | } |
---|
736 | } while (!finished_faces); |
---|
737 | } |
---|
738 | } |
---|
739 | } |
---|
740 | //remove last comma |
---|
741 | if (face_tag_list.Length > 1) |
---|
742 | { |
---|
743 | face_tag_list = face_tag_list.Substring(0, face_tag_list.Length - 1); |
---|
744 | } |
---|
745 | return face_tag_list; |
---|
746 | } |
---|
747 | public bool SyncPiwigoReadPicasaContacts() |
---|
748 | { |
---|
749 | int i, k, l; |
---|
750 | string filedata, person_str, unique_id; |
---|
751 | bool person_exists; |
---|
752 | System.IO.StreamReader file; |
---|
753 | |
---|
754 | try |
---|
755 | { |
---|
756 | file = new System.IO.StreamReader(FormSettings.Default.PicasaContactsLocation); |
---|
757 | } |
---|
758 | catch (Exception e) |
---|
759 | { |
---|
760 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.UnableToOpenContactsStrRes + " " + |
---|
761 | e.Message; |
---|
762 | return false; |
---|
763 | } |
---|
764 | |
---|
765 | for (i = 0; i < constants.MAX_NUMBER_OF_CONTACTS; i++) |
---|
766 | { |
---|
767 | piwigo_person_tag_count[i] = 0; |
---|
768 | } |
---|
769 | GlobalClass.person_count = 0; |
---|
770 | GlobalClass.unique_id_count = 0; |
---|
771 | while ((filedata = file.ReadLine()) != null) |
---|
772 | { |
---|
773 | //parse name and unique id |
---|
774 | k = filedata.IndexOf("contact id="); |
---|
775 | if (k != -1) |
---|
776 | { |
---|
777 | unique_id = filedata.Substring(k + 12, 16); |
---|
778 | k = filedata.IndexOf("name="); |
---|
779 | l = filedata.IndexOf("\"", k + 6); |
---|
780 | person_str = filedata.Substring(k + 6, (l - k - 6)); |
---|
781 | Debug.WriteLine("Person: " + person_str + " ID: " + unique_id); |
---|
782 | //does this person already exist |
---|
783 | person_exists = false; |
---|
784 | for (i = 0; i < GlobalClass.person_count; i++) |
---|
785 | { |
---|
786 | if (person_str == GlobalClass.contact_array[i].name) |
---|
787 | { |
---|
788 | person_exists = true; |
---|
789 | break; |
---|
790 | } |
---|
791 | } |
---|
792 | |
---|
793 | if (!person_exists) |
---|
794 | { |
---|
795 | GlobalClass.contact_array[GlobalClass.person_count].name = person_str; |
---|
796 | GlobalClass.contact_array[GlobalClass.person_count].index = GlobalClass.person_count; |
---|
797 | GlobalClass.picasa_unique_id_arrary[GlobalClass.unique_id_count].unique_id = unique_id; |
---|
798 | GlobalClass.picasa_unique_id_arrary[GlobalClass.unique_id_count].index = GlobalClass.person_count; |
---|
799 | GlobalClass.person_count++; |
---|
800 | } |
---|
801 | else |
---|
802 | { |
---|
803 | GlobalClass.picasa_unique_id_arrary[GlobalClass.unique_id_count].unique_id = unique_id; |
---|
804 | GlobalClass.picasa_unique_id_arrary[GlobalClass.unique_id_count].index = i; |
---|
805 | } |
---|
806 | GlobalClass.unique_id_count++; |
---|
807 | } |
---|
808 | Debug.WriteLine("Number of contacts: " + GlobalClass.person_count.ToString()); |
---|
809 | } |
---|
810 | return true; |
---|
811 | } |
---|
812 | public bool SyncPiwigoCheckServerStatus() |
---|
813 | { |
---|
814 | string WebResponse, WebRequest, xmlResponse; |
---|
815 | |
---|
816 | WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.session.getStatus"; |
---|
817 | WriteLog(WebRequest); |
---|
818 | |
---|
819 | WebResponse = SendWebGet(WebRequest); |
---|
820 | WriteLog(WebResponse); |
---|
821 | |
---|
822 | if (WebResponse.IndexOf("An error occurred:") != -1) |
---|
823 | { |
---|
824 | GlobalClass.StatusDialogErrorMessage = WebResponse; |
---|
825 | return false; |
---|
826 | } |
---|
827 | |
---|
828 | xmlResponse = XmlGetAttribute(FilterForXML(WebResponse), "stat"); |
---|
829 | if (xmlResponse == "ok") |
---|
830 | { |
---|
831 | return true; |
---|
832 | } |
---|
833 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.UnableToContactServerStrRes; |
---|
834 | return false; |
---|
835 | } |
---|
836 | public bool SyncPiwigoLogIn() |
---|
837 | { |
---|
838 | string WebResponse, WebRequest, xmlResponse, PostData; |
---|
839 | byte[] dummy_data = new byte[1]; |
---|
840 | bool response; |
---|
841 | |
---|
842 | dummy_data[0] = 0; |
---|
843 | WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.session.login"; |
---|
844 | PostData = "username=" + FormSettings.Default.PiwigoUserName + "&password=" + FormSettings.Default.PiwigoPassword; |
---|
845 | WriteLog(WebRequest); |
---|
846 | WriteLog(PostData); |
---|
847 | response = SendWebPost(WebRequest, PostData, "application/x-www-form-urlencoded"); |
---|
848 | if (!response) |
---|
849 | { |
---|
850 | WriteLog("Log In Post Fail."); |
---|
851 | return false; |
---|
852 | } |
---|
853 | WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.session.getStatus"; |
---|
854 | WriteLog(WebRequest); |
---|
855 | WebResponse = SendWebGet(WebRequest); |
---|
856 | WriteLog(WebResponse); |
---|
857 | if (WebResponse.IndexOf("An error occurred:") != -1) |
---|
858 | { |
---|
859 | GlobalClass.StatusDialogErrorMessage = WebResponse; |
---|
860 | return false; |
---|
861 | } |
---|
862 | xmlResponse = XmlGetElement(FilterForXML(WebResponse), "username"); |
---|
863 | if (xmlResponse == FormSettings.Default.PiwigoUserName) |
---|
864 | { |
---|
865 | return true; |
---|
866 | } |
---|
867 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.UnableToLogInStrRes; |
---|
868 | return false; |
---|
869 | } |
---|
870 | |
---|
871 | public bool SyncPiwigoGetVersion(ref string PiwigoVersionNumber) |
---|
872 | { |
---|
873 | string WebResponse, WebRequest, xmlResponse; |
---|
874 | |
---|
875 | WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.getVersion"; |
---|
876 | WriteLog(WebRequest); |
---|
877 | |
---|
878 | WebResponse = SendWebGet(WebRequest); |
---|
879 | WriteLog(WebResponse); |
---|
880 | |
---|
881 | PiwigoVersionNumber = ""; |
---|
882 | if (WebResponse.IndexOf("An error occurred:") != -1) |
---|
883 | { |
---|
884 | GlobalClass.StatusDialogErrorMessage = WebResponse; |
---|
885 | return false; |
---|
886 | } |
---|
887 | |
---|
888 | xmlResponse = XmlGetAttribute(FilterForXML(WebResponse), "stat"); |
---|
889 | if (xmlResponse != "ok") |
---|
890 | { |
---|
891 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.UnableToGetVersionStrRes; |
---|
892 | return false; |
---|
893 | } |
---|
894 | |
---|
895 | PiwigoVersionNumber = XmlGetElement(FilterForXML(WebResponse), "rsp"); |
---|
896 | PiwigoVersionNumber = PiwigoVersionNumber.Substring(1); |
---|
897 | PiwigoVersionNumber = PiwigoVersionNumber.Substring(0,PiwigoVersionNumber.Length -1); |
---|
898 | return true; |
---|
899 | } |
---|
900 | public bool SyncPiwigoGetAlbums(ref XmlDocument AlbumList) |
---|
901 | { |
---|
902 | string WebResponse, WebRequest, xmlResponse; |
---|
903 | |
---|
904 | WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.categories.getList&fullname=\"true\"&recursive=\"false\""; |
---|
905 | WriteLog(WebRequest); |
---|
906 | |
---|
907 | WebResponse = SendWebGet(WebRequest); |
---|
908 | WriteLog(WebResponse); |
---|
909 | |
---|
910 | if (WebResponse.IndexOf("An error occurred:") != -1) |
---|
911 | { |
---|
912 | GlobalClass.StatusDialogErrorMessage = WebResponse; |
---|
913 | return false; |
---|
914 | } |
---|
915 | xmlResponse = XmlGetAttribute(FilterForXML(WebResponse), "stat"); |
---|
916 | if (xmlResponse == "ok") |
---|
917 | { |
---|
918 | AlbumList.LoadXml(FilterForXML(WebResponse)); |
---|
919 | return true; |
---|
920 | } |
---|
921 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.UnableToRetreiveAlbumStrRes; |
---|
922 | return false; |
---|
923 | } |
---|
924 | public bool SyncPiwigoGetTags(ref XmlDocument TagList) |
---|
925 | { |
---|
926 | string WebResponse, WebRequest, xmlResponse; |
---|
927 | |
---|
928 | WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.tags.getList"; |
---|
929 | WriteLog(WebRequest); |
---|
930 | |
---|
931 | WebResponse = SendWebGet(WebRequest); |
---|
932 | WriteLog(WebResponse); |
---|
933 | |
---|
934 | if (WebResponse.IndexOf("An error occurred:") != -1) |
---|
935 | { |
---|
936 | GlobalClass.StatusDialogErrorMessage = WebResponse; |
---|
937 | return false; |
---|
938 | } |
---|
939 | xmlResponse = XmlGetAttribute(FilterForXML(WebResponse), "stat"); |
---|
940 | if (xmlResponse == "ok") |
---|
941 | { |
---|
942 | TagList.LoadXml(FilterForXML(WebResponse)); |
---|
943 | return true; |
---|
944 | } |
---|
945 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.UnableToRetreiveTagsStrRes; |
---|
946 | return false; |
---|
947 | } |
---|
948 | public bool SyncPiwigoCreateAlbum(ref int NewPiwigoAlbumID) |
---|
949 | { |
---|
950 | string WebResponse, WebRequest, xmlResponse, tempstr; |
---|
951 | |
---|
952 | NewPiwigoAlbumID = -1; |
---|
953 | WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.categories.add&name=" |
---|
954 | + GlobalClass.NewAlbumName; |
---|
955 | if (Form1Settings.Default.AccessValue == WinFormStrings.PublicStrRes) |
---|
956 | { |
---|
957 | tempstr = "public"; |
---|
958 | } |
---|
959 | else |
---|
960 | { |
---|
961 | tempstr = "private"; |
---|
962 | } |
---|
963 | WebRequest += "&status=" + tempstr; |
---|
964 | if (GlobalClass.NewAlbumNameParentID != -1) |
---|
965 | { |
---|
966 | WebRequest += "&parent=" + GlobalClass.NewAlbumNameParentID.ToString(); |
---|
967 | } |
---|
968 | |
---|
969 | if (GlobalClass.NewAlbumDescription.Length != 0) |
---|
970 | { |
---|
971 | WebRequest += "&comment=" + GlobalClass.NewAlbumDescription; |
---|
972 | } |
---|
973 | |
---|
974 | WriteLog(WebRequest); |
---|
975 | |
---|
976 | WebResponse = SendWebGet(WebRequest); |
---|
977 | WriteLog(WebResponse); |
---|
978 | |
---|
979 | if (WebResponse.IndexOf("An error occurred:") != -1) |
---|
980 | { |
---|
981 | GlobalClass.StatusDialogErrorMessage = WebResponse; |
---|
982 | return false; |
---|
983 | } |
---|
984 | xmlResponse = XmlGetAttribute(FilterForXML(WebResponse), "stat"); |
---|
985 | if (xmlResponse == "ok") |
---|
986 | { |
---|
987 | NewPiwigoAlbumID = Convert.ToInt32(XmlGetElement(FilterForXML(WebResponse), "id")); |
---|
988 | return true; |
---|
989 | } |
---|
990 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.UnableToCreateAlbumStrRes; |
---|
991 | return false; |
---|
992 | } |
---|
993 | public bool SyncPiwigoAddPhoto(int NewPiwigoAlbumId) |
---|
994 | { |
---|
995 | int i,k, di; |
---|
996 | string WebResponse, WebRequest, xmlResponse, filename; |
---|
997 | byte[] PhotoFileContents; |
---|
998 | string [] file_to_send = {local_command_line_args[0]}; |
---|
999 | string[,] post_array = new string[10,2]; |
---|
1000 | string image_id; |
---|
1001 | bool upload_failed, response; |
---|
1002 | string face_tag_list, tag_list, comment_list, all_tags; |
---|
1003 | |
---|
1004 | upload_failed = false; |
---|
1005 | for (i = 0; i < local_command_line_args.Length; i++) |
---|
1006 | { |
---|
1007 | if (stop_button_pressed) |
---|
1008 | { |
---|
1009 | return false; |
---|
1010 | } |
---|
1011 | |
---|
1012 | SetStatus(WinFormStrings.UploadingPHotoStrRes + " " + (i + 1).ToString() + " " + WinFormStrings.ofStrREf + " " + local_command_line_args.Length.ToString() + "..."); |
---|
1013 | |
---|
1014 | face_tag_list = SyncPiwigoGetFaceTags(local_command_line_args[i]); |
---|
1015 | |
---|
1016 | tag_list = ""; |
---|
1017 | comment_list = ""; |
---|
1018 | all_tags = ""; |
---|
1019 | SyncPiwigoGetTagsandComments(ref tag_list, ref comment_list, local_command_line_args[i]); |
---|
1020 | |
---|
1021 | if (face_tag_list.Length != 0) |
---|
1022 | { |
---|
1023 | all_tags = face_tag_list; |
---|
1024 | } |
---|
1025 | if (tag_list.Length != 0) |
---|
1026 | { |
---|
1027 | if (all_tags.Length != 0) |
---|
1028 | { |
---|
1029 | all_tags += ","; |
---|
1030 | } |
---|
1031 | all_tags += tag_list; |
---|
1032 | } |
---|
1033 | |
---|
1034 | k = local_command_line_args[i].LastIndexOf("\\"); |
---|
1035 | filename = local_command_line_args[i].Substring(k + 1); |
---|
1036 | PhotoFileContents = FileToByteArray(local_command_line_args[i]); |
---|
1037 | |
---|
1038 | WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.images.addSimple"; |
---|
1039 | |
---|
1040 | WebResponse = ""; |
---|
1041 | local_command_line_args[i] = local_command_line_args[i].Replace("\\","\\\\"); |
---|
1042 | post_array[0, 0] = "name=\"category\""; |
---|
1043 | post_array[0, 1] = NewPiwigoAlbumId.ToString(); |
---|
1044 | post_array[1, 0] = "name=\"name\"" ; |
---|
1045 | post_array[1, 1] = filename; |
---|
1046 | k = 2; |
---|
1047 | if (all_tags.Length != 0) |
---|
1048 | { |
---|
1049 | post_array[k, 0] = "name=\"tags\""; |
---|
1050 | post_array[k, 1] = all_tags; |
---|
1051 | k++; |
---|
1052 | } |
---|
1053 | if (comment_list.Length != 0) |
---|
1054 | { |
---|
1055 | post_array[k, 0] = "name=\"comment\""; |
---|
1056 | post_array[k, 1] = "\"" + comment_list + "\""; |
---|
1057 | k++; |
---|
1058 | } |
---|
1059 | post_array[k, 0] = "name=\"image\"" + ";filename=" + "\"" + local_command_line_args[i] + "\""; |
---|
1060 | |
---|
1061 | WriteLog(WebRequest); |
---|
1062 | for (di = 0; di < k; di++) |
---|
1063 | { |
---|
1064 | WriteLog(post_array[k, 0] + post_array[k, 1]); |
---|
1065 | } |
---|
1066 | |
---|
1067 | |
---|
1068 | response = SendWebPostMultipart(WebRequest, post_array, "multipart/form-data", PhotoFileContents, |
---|
1069 | ref WebResponse); |
---|
1070 | |
---|
1071 | WriteLog(WebResponse); |
---|
1072 | |
---|
1073 | if (!response) |
---|
1074 | { |
---|
1075 | return false; |
---|
1076 | } |
---|
1077 | xmlResponse = XmlGetAttribute(FilterForXML(WebResponse), "stat"); |
---|
1078 | if (xmlResponse == "ok") |
---|
1079 | { |
---|
1080 | image_id = XmlGetElement(FilterForXML(WebResponse), "image_id"); |
---|
1081 | } |
---|
1082 | else |
---|
1083 | { |
---|
1084 | upload_failed = true; |
---|
1085 | } |
---|
1086 | } |
---|
1087 | return !upload_failed; |
---|
1088 | } |
---|
1089 | public string SendWebGet(string webrequest) |
---|
1090 | { |
---|
1091 | string webresponse; |
---|
1092 | webresponse = ""; |
---|
1093 | |
---|
1094 | try |
---|
1095 | { |
---|
1096 | ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; |
---|
1097 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; |
---|
1098 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webrequest); |
---|
1099 | request.CookieContainer = cookiesjar; |
---|
1100 | request.Proxy = null; |
---|
1101 | request.Timeout = 60000; |
---|
1102 | request.Credentials = CredentialCache.DefaultCredentials; |
---|
1103 | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); |
---|
1104 | |
---|
1105 | Stream dataStream = response.GetResponseStream(); |
---|
1106 | StreamReader reader = new StreamReader(dataStream); |
---|
1107 | // Read the content. |
---|
1108 | webresponse = reader.ReadToEnd(); |
---|
1109 | reader.Close(); |
---|
1110 | dataStream.Close(); |
---|
1111 | response.Close(); |
---|
1112 | } |
---|
1113 | catch (Exception e) |
---|
1114 | { |
---|
1115 | webresponse = WinFormStrings.ErrorOcurredStrRes + " " + e.Message; |
---|
1116 | } |
---|
1117 | return webresponse; |
---|
1118 | } |
---|
1119 | public bool SendWebPost(string webrequest, string post_data, string content_type) |
---|
1120 | { |
---|
1121 | try |
---|
1122 | { |
---|
1123 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webrequest); |
---|
1124 | ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; |
---|
1125 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; |
---|
1126 | request.CookieContainer = cookiesjar; |
---|
1127 | request.Proxy = null; |
---|
1128 | request.Timeout = 60000; |
---|
1129 | request.Method = "POST"; |
---|
1130 | request.Credentials = CredentialCache.DefaultCredentials; |
---|
1131 | |
---|
1132 | // Create POST data and convert it to a byte array. |
---|
1133 | string postData = post_data; |
---|
1134 | byte[] byteArray = Encoding.UTF8.GetBytes(postData); |
---|
1135 | |
---|
1136 | // Set the ContentType property of the WebRequest. |
---|
1137 | request.ContentType = content_type; |
---|
1138 | |
---|
1139 | // Set the ContentLength property of the WebRequest. |
---|
1140 | request.ContentLength = byteArray.Length; |
---|
1141 | |
---|
1142 | // Get the request stream. |
---|
1143 | Stream dataStream = request.GetRequestStream(); |
---|
1144 | |
---|
1145 | // Write the data to the request stream. |
---|
1146 | dataStream.Write(byteArray, 0, byteArray.Length); |
---|
1147 | |
---|
1148 | WebResponse response = request.GetResponse(); |
---|
1149 | // Display the status. |
---|
1150 | Console.WriteLine(((HttpWebResponse)response).StatusDescription); |
---|
1151 | // Get the stream containing content returned by the server. |
---|
1152 | dataStream = response.GetResponseStream(); |
---|
1153 | // Open the stream using a StreamReader for easy access. |
---|
1154 | StreamReader reader = new StreamReader(dataStream); |
---|
1155 | // Read the content. |
---|
1156 | string responseFromServer = reader.ReadToEnd(); |
---|
1157 | // Display the content. |
---|
1158 | |
---|
1159 | // Close the Stream object. |
---|
1160 | dataStream.Close(); |
---|
1161 | } |
---|
1162 | catch (Exception e) |
---|
1163 | { |
---|
1164 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.ErrorOcurredStrRes + " " + e.Message; |
---|
1165 | return false; |
---|
1166 | } |
---|
1167 | return true; |
---|
1168 | } |
---|
1169 | public string XmlGetAttribute(string xmlstring, string element) |
---|
1170 | { |
---|
1171 | string foundattribute; |
---|
1172 | |
---|
1173 | foundattribute = ""; |
---|
1174 | XmlDocument StatusResponse = new XmlDocument(); |
---|
1175 | StatusResponse.LoadXml(xmlstring); |
---|
1176 | XmlElement ResponseElem = StatusResponse.DocumentElement; |
---|
1177 | |
---|
1178 | if (ResponseElem.HasAttribute(element)) |
---|
1179 | { |
---|
1180 | foundattribute = ResponseElem.GetAttribute(element); |
---|
1181 | } |
---|
1182 | return foundattribute; |
---|
1183 | } |
---|
1184 | public string XmlGetElement(string xmlstring, string element) |
---|
1185 | { |
---|
1186 | XmlDocument StatusResponse = new XmlDocument(); |
---|
1187 | StatusResponse.LoadXml(xmlstring); |
---|
1188 | XmlNodeList nodelist = StatusResponse.GetElementsByTagName(element); |
---|
1189 | foreach (XmlNode anode in nodelist) |
---|
1190 | { |
---|
1191 | return anode.ChildNodes[0].Value; |
---|
1192 | } |
---|
1193 | return ""; |
---|
1194 | } |
---|
1195 | public bool UpdateStatus(bool result) |
---|
1196 | { |
---|
1197 | if (result) |
---|
1198 | { |
---|
1199 | this.StatusTextBox.Text += WinFormStrings.OKStrRes; |
---|
1200 | } |
---|
1201 | else |
---|
1202 | { |
---|
1203 | this.StatusTextBox.Text += WinFormStrings.FailedStrRes; |
---|
1204 | DialogResult = DialogResult.No; |
---|
1205 | } |
---|
1206 | StatusRefresh(); |
---|
1207 | return result; |
---|
1208 | } |
---|
1209 | public void SetStatus(string status) |
---|
1210 | { |
---|
1211 | this.StatusTextBox.Text = status; |
---|
1212 | StatusRefresh(); |
---|
1213 | } |
---|
1214 | public void StatusRefresh() |
---|
1215 | { |
---|
1216 | this.Refresh(); |
---|
1217 | this.StatusTextBox.Update(); |
---|
1218 | Application.DoEvents(); |
---|
1219 | } |
---|
1220 | public byte[] FileToByteArray(string _FileName) |
---|
1221 | { |
---|
1222 | byte[] _Buffer = null; |
---|
1223 | |
---|
1224 | try |
---|
1225 | { |
---|
1226 | // Open file for reading |
---|
1227 | System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); |
---|
1228 | |
---|
1229 | // attach filestream to binary reader |
---|
1230 | System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream); |
---|
1231 | |
---|
1232 | // get total byte length of the file |
---|
1233 | long _TotalBytes = new System.IO.FileInfo(_FileName).Length; |
---|
1234 | |
---|
1235 | // read entire file into buffer |
---|
1236 | _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes); |
---|
1237 | |
---|
1238 | // close file reader |
---|
1239 | _FileStream.Close(); |
---|
1240 | _FileStream.Dispose(); |
---|
1241 | _BinaryReader.Close(); |
---|
1242 | } |
---|
1243 | catch (Exception _Exception) |
---|
1244 | { |
---|
1245 | // Error |
---|
1246 | Console.WriteLine("File Read Error: ", _Exception.ToString()); |
---|
1247 | } |
---|
1248 | |
---|
1249 | return _Buffer; |
---|
1250 | } |
---|
1251 | public bool SendWebPostMultipart(string webrequest, string[,] post_data, string content_type, byte[] binary_content, ref string response_from_server) |
---|
1252 | { |
---|
1253 | int i; |
---|
1254 | ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; |
---|
1255 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; |
---|
1256 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webrequest); |
---|
1257 | request.CookieContainer = cookiesjar; |
---|
1258 | string boundary, formdataTemplate; |
---|
1259 | Stream dataStream; |
---|
1260 | byte[] byteArray; |
---|
1261 | Stream memStream = new System.IO.MemoryStream(); |
---|
1262 | |
---|
1263 | request.Proxy = null; |
---|
1264 | request.Timeout = 10 * 60 * 1000; |
---|
1265 | request.Method = "POST"; |
---|
1266 | request.Credentials = CredentialCache.DefaultCredentials; |
---|
1267 | request.KeepAlive = true; |
---|
1268 | |
---|
1269 | try |
---|
1270 | { |
---|
1271 | // Create POST data and convert it to a byte array. |
---|
1272 | boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); |
---|
1273 | content_type = "multipart/form-data; boundary=" + boundary; |
---|
1274 | |
---|
1275 | // Set the ContentType property of the WebRequest. |
---|
1276 | request.ContentType = content_type; |
---|
1277 | |
---|
1278 | byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + |
---|
1279 | boundary + "\r\n"); |
---|
1280 | |
---|
1281 | memStream.Write(boundarybytes, 0, boundarybytes.Length); |
---|
1282 | |
---|
1283 | byteArray = Encoding.UTF8.GetBytes(""); |
---|
1284 | |
---|
1285 | for (i = 0; i < 10; i++) |
---|
1286 | { |
---|
1287 | if (post_data[i, 0] != null) |
---|
1288 | { |
---|
1289 | formdataTemplate = "Content-Disposition: form-data; " |
---|
1290 | + post_data[i, 0] + "\r\n"; |
---|
1291 | if (post_data[i, 1] != null) |
---|
1292 | { |
---|
1293 | formdataTemplate += "\r\n"; |
---|
1294 | formdataTemplate += post_data[i, 1]; |
---|
1295 | } |
---|
1296 | |
---|
1297 | if (post_data[i + 1, 0] != null) |
---|
1298 | { |
---|
1299 | formdataTemplate += "\r\n--" + boundary + "\r\n"; |
---|
1300 | } |
---|
1301 | |
---|
1302 | byte[] byteArray2 = Encoding.UTF8.GetBytes(formdataTemplate); |
---|
1303 | |
---|
1304 | // Set the ContentLength property of the WebRequest. |
---|
1305 | //request.ContentLength = byteArray.Length; |
---|
1306 | memStream.Write(byteArray2, 0, byteArray2.Length); |
---|
1307 | } |
---|
1308 | else |
---|
1309 | { |
---|
1310 | break; |
---|
1311 | } |
---|
1312 | } |
---|
1313 | formdataTemplate = "Content-Type: image/jpeg\r\n\r\n"; |
---|
1314 | byte[] byteArray3 = Encoding.UTF8.GetBytes(formdataTemplate); |
---|
1315 | memStream.Write(byteArray3, 0, byteArray3.Length); |
---|
1316 | memStream.Write(binary_content, 0, binary_content.Length); |
---|
1317 | |
---|
1318 | formdataTemplate = "\r\n--" + boundary + "--\r\n"; |
---|
1319 | byte[] byteArray4 = Encoding.UTF8.GetBytes(formdataTemplate); |
---|
1320 | memStream.Write(byteArray4, 0, byteArray4.Length); |
---|
1321 | |
---|
1322 | memStream.Position = 0; |
---|
1323 | byte[] tempBuffer = new byte[memStream.Length]; |
---|
1324 | memStream.Read(tempBuffer, 0, tempBuffer.Length); |
---|
1325 | memStream.Close(); |
---|
1326 | |
---|
1327 | request.ContentLength = tempBuffer.Length; |
---|
1328 | |
---|
1329 | dataStream = request.GetRequestStream(); |
---|
1330 | dataStream.Write(tempBuffer, 0, tempBuffer.Length); |
---|
1331 | dataStream.Close(); |
---|
1332 | |
---|
1333 | |
---|
1334 | WebResponse response = request.GetResponse(); |
---|
1335 | // Display the status. |
---|
1336 | Console.WriteLine(((HttpWebResponse)response).StatusDescription); |
---|
1337 | // Get the stream containing content returned by the server. |
---|
1338 | dataStream = response.GetResponseStream(); |
---|
1339 | // Open the stream using a StreamReader for easy access. |
---|
1340 | StreamReader reader = new StreamReader(dataStream); |
---|
1341 | // Read the content. |
---|
1342 | response_from_server = reader.ReadToEnd(); |
---|
1343 | // Display the content. |
---|
1344 | |
---|
1345 | // Close the Stream object. |
---|
1346 | dataStream.Close(); |
---|
1347 | } |
---|
1348 | catch (Exception e) |
---|
1349 | { |
---|
1350 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.ErrorOcurredStrRes + " " + e.Message; |
---|
1351 | return false; |
---|
1352 | } |
---|
1353 | return true; |
---|
1354 | } |
---|
1355 | |
---|
1356 | private void button1_Click(object sender, EventArgs e) |
---|
1357 | { |
---|
1358 | GlobalClass.StatusDialogErrorMessage = WinFormStrings.StopButtonStrRes; |
---|
1359 | DialogResult = DialogResult.No; |
---|
1360 | stop_button_pressed = true; |
---|
1361 | } |
---|
1362 | public bool HaveToRemoveComma() |
---|
1363 | { |
---|
1364 | int k,l; |
---|
1365 | int major_version, sub_version, minor_version; |
---|
1366 | |
---|
1367 | |
---|
1368 | k = GlobalClass.PiwigoVersionNumber.IndexOf("."); |
---|
1369 | if (k == -1) |
---|
1370 | { |
---|
1371 | return true; |
---|
1372 | } |
---|
1373 | |
---|
1374 | l = 0; |
---|
1375 | if (GlobalClass.PiwigoVersionNumber.Length >= k + 1) |
---|
1376 | { |
---|
1377 | l = GlobalClass.PiwigoVersionNumber.IndexOf(".", k + 1); |
---|
1378 | if (l == -1) |
---|
1379 | { |
---|
1380 | return true; |
---|
1381 | } |
---|
1382 | } |
---|
1383 | |
---|
1384 | major_version = Convert.ToInt32(GlobalClass.PiwigoVersionNumber.Substring(0, k)); |
---|
1385 | sub_version = Convert.ToInt32(GlobalClass.PiwigoVersionNumber.Substring(k+1, l-k-1)); |
---|
1386 | minor_version = Convert.ToInt32(GlobalClass.PiwigoVersionNumber.Substring(l+1)); |
---|
1387 | |
---|
1388 | return true; |
---|
1389 | } |
---|
1390 | public string FilterForXML(string input_xml) |
---|
1391 | { |
---|
1392 | int k; |
---|
1393 | string xml_only_str; |
---|
1394 | |
---|
1395 | xml_only_str = ""; |
---|
1396 | k = input_xml.IndexOf("<?xml version="); |
---|
1397 | if (k != -1) |
---|
1398 | { |
---|
1399 | xml_only_str = input_xml.Substring(k); |
---|
1400 | } |
---|
1401 | else |
---|
1402 | { |
---|
1403 | WriteLog("No xml found in string"); |
---|
1404 | } |
---|
1405 | return xml_only_str; |
---|
1406 | } |
---|
1407 | public void WriteLog(string logMessage) |
---|
1408 | { |
---|
1409 | string strLogFile; |
---|
1410 | |
---|
1411 | strLogFile = "picasa2piwigo_log.txt"; |
---|
1412 | #pragma warning disable 0162 |
---|
1413 | if (constants.DEBUG_LOG_ENABLED) |
---|
1414 | { |
---|
1415 | try |
---|
1416 | { |
---|
1417 | #pragma warning restore 0162 |
---|
1418 | StreamWriter swLog; |
---|
1419 | |
---|
1420 | logMessage = string.Format("{0}: {1}", DateTime.Now, logMessage); |
---|
1421 | |
---|
1422 | if (!File.Exists(strLogFile)) |
---|
1423 | { |
---|
1424 | swLog = new StreamWriter(strLogFile); |
---|
1425 | } |
---|
1426 | else |
---|
1427 | { |
---|
1428 | swLog = File.AppendText(strLogFile); |
---|
1429 | } |
---|
1430 | |
---|
1431 | swLog.WriteLine(logMessage); |
---|
1432 | swLog.WriteLine(); |
---|
1433 | |
---|
1434 | swLog.Close(); |
---|
1435 | |
---|
1436 | } |
---|
1437 | catch |
---|
1438 | { |
---|
1439 | } |
---|
1440 | } |
---|
1441 | } |
---|
1442 | } |
---|
1443 | } |
---|