forked from bangordailynews/Docs-to-WordPress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.php
892 lines (695 loc) · 23 KB
/
plugin.php
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<?php
/*
Plugin Name: Docs to WP
Author: William P. Davis, Bangor Daily News
Author URI: http://wpdavis.com/
Version: 0.4-beta
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
//MF
add_action('admin_menu', 'gdocs_admin_add_page');
function gdocs_admin_add_page() {
add_posts_page('Docs to Wordpress', 'Docs to Wordpress', 'edit_posts', 'gdocs-admin', 'gdocs_admin');
}
function gdocs_admin() {
//Init the Docs to WP
$docs_to_wp = new Docs_To_WP();
//Set these variables in your wp-config
$gdClient = $docs_to_wp->docs_to_wp_init( DOCSTOWP_USER, DOCSTOWP_PASS );
?>
<h2>Fetching documents from Google Docs</h2>
<?php
//We're just going to call one function:
$docs_to_wp->retrieve_docs_for_web( $gdClient, DOCSTOWP_ORIGIN, DOCSTOWP_DESTINATION);
}
function open_output_to_page($title, $contents, $error) {
?>
}
function close_output_to_page() {
?>
</div>
</div>
<?php
}
function gdocs_log($content, $type = "log") {
global $output;
$output[] = array($type, $content);
}
function echo_gdocs_log($title) {
global $output;
$errors = "";
$action = "";
foreach ($output as $item) {
if($item[0]=="error") {
$errors = $errors.$item[1];
} elseif ($item[0]=="action") {
$action = $item[1];
}
}
?>
<div id="linksubmitdiv" class="postbox closed" style="width:90%;">
<div class="handlediv" title="Click to show"><br></div>
<h3 class="handle"<?php echo strlen($errors)>0?' style="color:red;"':''; ?>><span><?php
echo $action.$title;
echo strlen($errors)>0?"Errors: ".$errors:"";
?></span></h3>
<div class="inside"><?php
foreach ($output as $item) {
echo $item[1]."\n";
}
?>
</div>
</div>
<?php
$output = array(); //empty for next log round
}
class Docs_To_WP {
public function docs_to_wp_init( $username, $password ) {
do_action( 'docs_to_wp_init' );
$gdClient = new gData( $username, $password );
return $gdClient;
}
public function purifier_init( $plugin_path ) {
//Include and set up the HTML purifier
include( $plugin_path . 'purifier/HTMLPurifier.standalone.php' );
$config = HTMLPurifier_Config::createDefault();
/* Uncomment if having problems with empty lines
$config->set('AutoFormat.RemoveEmpty', 'true');
$config->set('AutoFormat.RemoveEmpty.RemoveNbsp', 'true');
$config->set('HTML.DefinitionRev', 1);
*/
$purifier = new HTMLPurifier($config);
return $purifier;
}
public function retrieve_docs_for_web( $gdClient, $folderID, $toFolder = false ) {
//Get all the posts from the source folder in Google Docs
$contents = $gdClient->getInfoFiles( $folderID );
//Run all the posts through WordPress
$posts = $this->put_docs_in_wp( $contents, $gdClient );
if( empty( $posts ) || !is_array( $posts ) )
return false;
//If a destination folder is set, move the docs to the new folder
if( $toFolder ) {
foreach( $posts as $post ) {
$gdClient->moveFileToFolder( $post[ 'gdoc_id' ], $toFolder );
}
}
//Move the docs out of the source folder to make sure we aren't processing the same doc twice
foreach( $posts as $post ) {
$gdClient->removeFileFolder( $post[ 'gdoc_id' ], $folderID );
}
//Returns an array of post IDs and corresponding Google Docs IDs
return $posts;
}
public function put_docs_in_wp( $feed, $gdClient ) {
global $plugin_path;
//Init the purifier
$purifier = $this->purifier_init( $plugin_path );
//Start with an empty array of posts
$posts = array();
//If we didn't get a proper feed, bail
if( empty( $feed ) || !is_array( $feed ) )
return false;
//Foreach post in the feed, loop through it, clean it and publish it to WordPress
foreach ($feed as $entry) {
$cats = $entry[ 'folders' ];
$author = (string) $entry[ 'author' ];
$docID = $entry[ 'id' ];
$title = (string) $entry[ 'name' ];
$source = (string) $entry[ 'down' ];
$content = $this->get_clean_doc( $gdClient, $purifier, $source );
$post_id = $this->publish_to_WordPress( $title, $content, $author, $cats, array( '_gdocID' => $docID ) );
$posts[] = array( 'post_id' => $post_id, 'gdoc_id' => $docID );
echo_gdocs_log($title." to post id ".$post_id);
}
return $posts;
}
public function get_clean_doc( $gdClient, $purifier, $uri ) {
//We want to clean up each doc a bit
$contents = $gdClient->getFile( $uri, false, "html");
$contents = apply_filters( 'pre_docs_to_wp_strip', $contents );
//New domDocument and xPath to get the content
$dom= new DOMDocument();
$dom->loadHTML( $contents[ 'contents' ] );
$xpath = new DOMXPath($dom);
//Strip away the headers
$body = $xpath->query('/html/body');
//This is our dirty HTML
$dirty_html = $dom->saveXml($body->item(0));
$dirty_html = apply_filters( 'pre_docs_to_wp_purify', $dirty_html );
//Run that through the purifier
$clean_html = $purifier->purify( $dirty_html );
//Return that clean shit
return $clean_html;
}
//Checks if there is an earlier version of the article
public function post_exists_by_meta( $key, $value ) {
global $wpdb;
$query = "SELECT post_id FROM " . $wpdb->postmeta . " WHERE meta_key = %s AND meta_value = %s";
return $wpdb->get_var( $wpdb->prepare( $query, $key, $value ) );
}
public function publish_to_WordPress ( $title, $content, $author = false, $categories = false, $custom_fields = false ) {
//Find out if we are creating a draft or updating a doc
$post_id = $this->post_exists_by_meta( '_gdocID', $custom_fields[ '_gdocID' ] );
//Find out if the collections the doc is in matches any categories
$cats = array();
foreach ( $categories as $category ) {
$cat = term_exists( $category, 'category' );
if( !empty( $cat ) )
$cats[] = $cat['term_id'];
if( empty( $cats ) )
$cats[] = get_option('default_link_category');
}
//If the username in gdocs matches the username in WordPress, it will automatically apply the correct username
$author_data = get_userdatabylogin( $author );
$author = $author_data->ID;
$post_array = array(
'post_title' => $title,
'post_content' => $content,
'custom_fields' => $custom_fields,
);
if( empty( $post_id ) ) {
$post_array = array_merge( $post_array, array( 'post_author' => $author, 'post_category' => $cats ) );
} else {
$post_array = array_merge( $post_array, array( 'ID' => $post_id ) );
}
//If you want all posts to be auto-published, for example, you can add a filter here
$post_array = apply_filters( 'pre_docs_to_wp_insert', $post_array );
gdocs_log($post_array['post_content']);
//Add or update
if( empty( $post_id ) ) {
gdocs_log("Added: ", "action");
$post_id = wp_insert_post( $post_array );
// We need to insert images here as they require the post id to be set as attachments
if(has_filter('attach_images_docs_to_wp_insert')) {
$post_array['ID'] = $post_id;
$post_array = apply_filters( 'attach_images_docs_to_wp_insert', $post_array); //MF
$post_id = wp_update_post( $post_array ); // update again due to changes after we know the post ID
gdocs_log("Had to run update on post again to insert images");
}
} else {
gdocs_log("Updated: ", "action");
$post_array = apply_filters( 'attach_images_docs_to_wp_insert', $post_array); //MF
$post_id = wp_update_post( $post_array );
}
//Update post meta, including the _gdocID field
foreach( $post_array['custom_fields'] as $key => $value )
update_post_meta( $post_id, $key, $value );
return $post_id;
}
}
/*
NOTE: the gData class uses CURL, not the WordPress HTTP API. This will be fixed in a later release.
*/
/**
* PHP gData
*
* For support issues please contact me by email :
* or the official web site:
* http://www.boxcreation.com
* ==============================================================================
*
* @version $Id: gdata.class.php,v 0.5 2010/06/20 10:54:32 $
* @copyright Copyright (c) 2007 Yassine Oussi (http://www.boxcreation.com)
* @author Yassine Oussi <[email protected]>
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*
* ==============================================================================
*/
class gData
{
private $username;
private $password;
private $auth;
private $curl;
private $status ="not connected";
//--Public methods--//
/**
* Class constructor
* Set auth to google Docs
* @param $email, $password
* @access public
* @return void
*/
public function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
// Construct an HTTP POST request
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array("accountType" => "HOSTED_OR_GOOGLE",
"Email" => $this->username,
"Passwd" => $this->password,
"service" => "writely",
"source" => "Gdata");
// Initialize the curl object
$this->curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
// Execute
$response = curl_exec($this->curl);
// Get the Auth string and save it
if(preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches))
{
$this->auth = $matches[1];
$this->status = "connected";
}
else
{
preg_match("/Error=([a-z0-9_\-]+)/i", $response, $matches);
$this->status = "not connected : ". $matches[1] ." ". curl_error($this->curl);
}
}
/**
* getStatus
* check the status
* @access public
* @return String
*/
public function getStatus()
{
return $this->status;
}
/**
* createFolder
* Create a folder or subfolder
* @param $name, $idFolder=0
* @access public
* @return array
*/
public function createFolder($name, $idFolder = NULL)
{
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
"Content-Type: application/atom+xml");
if($idFolder)
{
$url = "https://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents";
}
else
{
$url = "https://docs.google.com/feeds/default/private/full";
}
$xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\">
<atom:category scheme=\"http://schemas.google.com/g/2005#kind\"
term=\"http://schemas.google.com/docs/2007#folder\" label=\"folder\"/>
<atom:title>". $name ."</atom:title>
</atom:entry>";
// Make the request
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $xmlstr);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
$response = simplexml_load_string($response);
if(preg_match("/folder%3A([a-z0-9_\-]+)/i",$response->id[0], $matches))
{
$arr["name"] = $response->title;
$arr["type"] = $response->content["type"];
$arr["down"] = $response->content["src"];
$arr["link"] = $response->link[2]["href"];
$arr["id"] = $matches[1];
return $arr;
}
}
/**
* getInfoFile
* Remove a folder
* @param $idFolder, $next
* @access public
* @return array
*/
public function getInfoFiles($idFolder = NULL) {
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
);
$url = "https://docs.google.com/feeds/default/private/full";
if( $idFolder )
$url .= "/folder%3A" . $idFolder . "/contents";
// Make the request
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_POST, false);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
// Parse the response
$response = simplexml_load_string($response);
// Get files
$size = sizeOf($response);
for ($i=0; $i<$size; $i++)
{
if( $response->entry[$i]->title )
{
$arr[$i]["name"] = $response->entry[$i]->title ;
$arr[$i]["type"] = $response->entry[$i]->content["type"];
$arr[$i]["down"] = $response->entry[$i]->content["src"];
list( $not_needed, $id ) = explode( "?", (string) $response->entry[$i]->content["src"] );
$arr[$i]["link"] = $response->entry[$i]->link[2]["href"];
$arr[$i]["author"] = $response->entry[$i]->author->name;
foreach( $response->entry[$i]->link as $folder ) {
$title = (string) $folder["title"];
if( !empty( $title ) )
$arr[$i]["folders"][] = $title;
}
$arr[$i]["id"] = str_replace( "id=", "", $id );
//$arr[$i]["dump"] = $response->entry[$i];
}
}
return $arr;
}
/**
* getFolders
* get a list of folder
* @access public
* @return array
*/
public function getInfoFolders() {
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
);
$url = "https://docs.google.com/feeds/default/private/full/-/folder";
// Make the request
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_POST, false);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
// Parse the response
$response = simplexml_load_string($response);
// Get folders
$size = sizeOf($response);
for ($i=0; $i<$size; $i++)
{
if($response->entry[$i]->title && preg_match("/folder%3A([a-z0-9_\-]+)/i", $response->entry[$i]->content[0]["src"], $matches))
{
$arr[$i]["name"] = $response->entry[$i]->title;
$arr[$i]["link"] = $response->entry[$i]->content["src"];
$arr[$i]["author"] = $response->entry[$i]->author->name;
$arr[$i]["id"] = $matches[1];
}
}
return $arr;
}
/**
* uploadFile
* Upload file
* @param $file, $name, $idFolder
* @access public
* @return array
*/
public function uploadFile($file, $name, $idFolder = null)
{
// Include the Auth string in the headers
// Together with the API version being used
if(!@filesize($file))
{
throw new Exception("File not found!");
}
else
{
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
"Content-Length: ". filesize($file),
"Content-Type: ". $this->returnMIMEType($file),
"Slug: ". $name);
if($idFolder)
{
$url = "https://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents";
}
else
{
$url = "https://docs.google.com/feeds/default/private/full";
}
// Make the request
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, file_get_contents($file));
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
// Parse the response
$response = simplexml_load_string($response);
if(preg_match("/%3A([a-z0-9_\-]+)/i", $response->link[2]["href"], $matches))
{
$arr["name"] = $response->title;
$arr["type"] = $response->content["type"];
$arr["down"] = $response->content["src"];
$arr["link"] = $response->link[2]["href"];
$arr["id"] = $matches[1];
return $arr;
}
else
{
return false;
}
}
}
/**
* getFile
* Download file from google docs
* @param $id, $filename, $format
* @access public
* @return array
*/
public function getFile($link, $filename = false, $format)
{
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
);
// Make the request
curl_setopt($this->curl, CURLOPT_HTTPGET, true);
curl_setopt($this->curl, CURLOPT_URL, $link ."&exportFormat=". $format . "&format=". $format);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_HEADER, false);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
if( $filename ) {
// save the file
$out = fopen($filename, 'wb');
fwrite($out, $response);
fclose($out);
} else {
$arr[ "contents" ] = $response;
}
$arr["filename"] = $filename;
//$arr["content"] = $this->returnMIMEType($filename);
return $arr;
}
/**
* removeFile
* Remove a file
* @param $idFile
* @access public
* @return void
*/
public function removeFileFolder( $idFile, $idFolder = false )
{
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"GData-Version: 3.0",
"If-Match: * ",
"Authorization: GoogleLogin auth=" . $this->auth,
);
$url = "https://docs.google.com/feeds/default/private/full/";
if( $idFolder ) {
$url .= "folder%3A" . $idFolder . "/contents/document%3A" . $idFile;
} else {
//$url .= $idFile ."?delete=true
}
// Make the request
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($this->curl, CURLOPT_URL, $url );
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, false);
curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
// Execute
curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
}
/**
* moveFileFolder
* move file to Folder
* @param $id, $idFolder, $type
* @access public
* @return void
*/
public function moveFileToFolder($id, $idFolder, $type="file")
{
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
"Content-Type: application/atom+xml");
if($type == "file")
{
$xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns=\"http://www.w3.org/2005/Atom\">
<id>https://docs.google.com/feeds/default/private/full/document%3A". $id ."</id>
</entry>";
}
elseif($type == "folder")
{
$xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns=\"http://www.w3.org/2005/Atom\">
<id>https://docs.google.com/feeds/default/private/full/folder%3A". $id ."</id>
</entry>";
}
// Make the request
curl_setopt($this->curl, CURLOPT_URL, "https://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents");
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $xmlstr);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
// Execute
curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
}
/**
* closeConnection
* Curl close
* @access public
* @return void
*/
public function closeConnection()
{
curl_close($this->curl);
}
//--Private methods--//
/**
* returnMIMEType
* Return mime type
* @param $filename
* @access private
* @return string
*/
private function returnMIMEType($filename)
{
preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
switch(strtolower($fileSuffix[1]))
{
case "js" :
return "application/x-javascript";
case "json" :
return "application/json";
case "jpg" :
case "jpeg" :
case "jpe" :
return "image/jpg";
case "png" :
case "gif" :
case "bmp" :
case "tiff" :
return "image/".strtolower($fileSuffix[1]);
case "css" :
return "text/css";
case "xml" :
return "application/xml";
case "doc" :
case "docx" :
return "application/msword";
case "xls" :
case "xlt" :
case "xlm" :
case "xld" :
case "xla" :
case "xlc" :
case "xlw" :
case "xll" :
return "application/vnd.ms-excel";
case "ppt" :
case "pps" :
return "application/vnd.ms-powerpoint";
case "rtf" :
return "application/rtf";
case "pdf" :
return "application/pdf";
case "html" :
case "htm" :
case "php" :
return "text/html";
case "txt" :
return "text/plain";
case "mpeg" :
case "mpg" :
case "mpe" :
return "video/mpeg";
case "mp3" :
return "audio/mpeg3";
case "wav" :
return "audio/wav";
case "aiff" :
case "aif" :
return "audio/aiff";
case "avi" :
return "video/msvideo";
case "wmv" :
return "video/x-ms-wmv";
case "mov" :
return "video/quicktime";
case "zip" :
return "application/zip";
case "tar" :
return "application/x-tar";
case "swf" :
return "application/x-shockwave-flash";
default :
if(function_exists("mime_content_type"))
{
$fileSuffix = mime_content_type($filename);
}
return "unknown/" . trim($fileSuffix[0], ".");
}
}
}