-
Notifications
You must be signed in to change notification settings - Fork 96
/
cloudfiles.php
2113 lines (2018 loc) · 71.4 KB
/
cloudfiles.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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This is the PHP Cloud Files API.
*
* <code>
* # Authenticate to Cloud Files. The default is to automatically try
* # to re-authenticate if an authentication token expires.
* #
* # NOTE: Some versions of cURL include an outdated certificate authority (CA)
* # file. This API ships with a newer version obtained directly from
* # cURL's web site (http://curl.haxx.se). To use the newer CA bundle,
* # call the CF_Authentication instance's 'ssl_use_cabundle()' method.
* #
* $auth = new CF_Authentication($username, $api_key);
* # $auth->ssl_use_cabundle(); # bypass cURL's old CA bundle
* $auth->authenticate();
*
* # Establish a connection to the storage system
* #
* # NOTE: Some versions of cURL include an outdated certificate authority (CA)
* # file. This API ships with a newer version obtained directly from
* # cURL's web site (http://curl.haxx.se). To use the newer CA bundle,
* # call the CF_Connection instance's 'ssl_use_cabundle()' method.
* #
* $conn = new CF_Connection($auth);
* # $conn->ssl_use_cabundle(); # bypass cURL's old CA bundle
*
* # Create a remote Container and storage Object
* #
* $images = $conn->create_container("photos");
* $bday = $images->create_object("first_birthday.jpg");
*
* # Upload content from a local file by streaming it. Note that we use
* # a "float" for the file size to overcome PHP's 32-bit integer limit for
* # very large files.
* #
* $fname = "/home/user/photos/birthdays/birthday1.jpg"; # filename to upload
* $size = (float) sprintf("%u", filesize($fname));
* $fp = open($fname, "r");
* $bday->write($fp, $size);
*
* # Or... use a convenience function instead
* #
* $bday->load_from_filename("/home/user/photos/birthdays/birthday1.jpg");
*
* # Now, publish the "photos" container to serve the images by CDN.
* # Use the "$uri" value to put in your web pages or send the link in an
* # email message, etc.
* #
* $uri = $images->make_public();
*
* # Or... print out the Object's public URI
* #
* print $bday->public_uri();
* </code>
*
* See the included tests directory for additional sample code.
*
* Requres PHP 5.x (for Exceptions and OO syntax) and PHP's cURL module.
*
* It uses the supporting "cloudfiles_http.php" module for HTTP(s) support and
* allows for connection re-use and streaming of content into/out of Cloud Files
* via PHP's cURL module.
*
* See COPYING for license information.
*
* @author Eric "EJ" Johnson <[email protected]>
* @copyright Copyright (c) 2008, Rackspace US, Inc.
* @package php-cloudfiles
*/
/**
*/
require_once("cloudfiles_exceptions.php");
require("cloudfiles_http.php");
define("DEFAULT_CF_API_VERSION", 1);
define("MAX_CONTAINER_NAME_LEN", 256);
define("MAX_OBJECT_NAME_LEN", 1024);
define("MAX_OBJECT_SIZE", 5*1024*1024*1024+1); # bigger than S3! ;-)
/**
* Class for handling Cloud Files Authentication, call it's {@link authenticate()}
* method to obtain authorized service urls and an authentication token.
*
* Example:
* <code>
* # Create the authentication instance
* #
* $auth = new CF_Authentication("username", "api_key");
*
* # NOTE: Some versions of cURL include an outdated certificate authority (CA)
* # file. This API ships with a newer version obtained directly from
* # cURL's web site (http://curl.haxx.se). To use the newer CA bundle,
* # call the CF_Authentication instance's 'ssl_use_cabundle()' method.
* #
* # $auth->ssl_use_cabundle(); # bypass cURL's old CA bundle
*
* # Perform authentication request
* #
* $auth->authenticate();
* </code>
*
* @package php-cloudfiles
*/
class CF_Authentication
{
public $dbug;
public $username;
public $api_key;
public $auth_host;
public $account;
/**
* Instance variables that are set after successful authentication
*/
public $storage_url;
public $cdnm_url;
public $auth_token;
/**
* Class constructor (PHP 5 syntax)
*
* @param string $username Mosso username
* @param string $api_key Mosso API Access Key
* @param string $account <b>Deprecated</b> <i>Account name</i>
* @param string $auth_host <b>Deprecated</b> <i>Authentication service URI</i>
*/
function __construct($username, $api_key, $account=NULL, $auth_host=NULL)
{
if (!$username || !$api_key) {
throw new SyntaxException("Missing required constructor arguments.");
}
$this->dbug = False;
$this->username = $username;
$this->api_key = $api_key;
$this->account_name = $account;
$this->auth_host = $auth_host;
$this->storage_url = NULL;
$this->cdnm_url = NULL;
$this->auth_token = NULL;
$this->cfs_http = new CF_Http(DEFAULT_CF_API_VERSION);
}
/**
* Use the Certificate Authority bundle included with this API
*
* Most versions of PHP with cURL support include an outdated Certificate
* Authority (CA) bundle (the file that lists all valid certificate
* signing authorities). The SSL certificates used by the Cloud Files
* storage system are perfectly valid but have been created/signed by
* a CA not listed in these outdated cURL distributions.
*
* As a work-around, we've included an updated CA bundle obtained
* directly from cURL's web site (http://curl.haxx.se). You can direct
* the API to use this CA bundle by calling this method prior to making
* any remote calls. The best place to use this method is right after
* the CF_Authentication instance has been instantiated.
*
* You can specify your own CA bundle by passing in the full pathname
* to the bundle. You can use the included CA bundle by leaving the
* argument blank.
*
* @param string $path Specify path to CA bundle (default to included)
*/
function ssl_use_cabundle($path=NULL)
{
$this->cfs_http->ssl_use_cabundle($path);
}
/**
* Attempt to validate Username/API Access Key
*
* Attempts to validate credentials with the authentication service. It
* either returns <kbd>True</kbd> or throws an Exception. Accepts a single
* (optional) argument for the storage system API version.
*
* Example:
* <code>
* # Create the authentication instance
* #
* $auth = new CF_Authentication("username", "api_key");
*
* # Perform authentication request
* #
* $auth->authenticate();
* </code>
*
* @param string $version API version for Auth service (optional)
* @return boolean <kbd>True</kbd> if successfully authenticated
* @throws AuthenticationException invalid credentials
* @throws InvalidResponseException invalid response
*/
function authenticate($version=DEFAULT_CF_API_VERSION)
{
list($status,$reason,$surl,$curl,$atoken) =
$this->cfs_http->authenticate($this->username, $this->api_key,
$this->account_name, $this->auth_host);
if ($status == 401) {
throw new AuthenticationException("Invalid username or access key.");
}
if ($status != 204) {
throw new InvalidResponseException(
"Unexpected response (".$status."): ".$reason);
}
if (!($surl || $curl) || !$atoken) {
throw new InvalidResponseException(
"Expected headers missing from auth service.");
}
$this->storage_url = $surl;
$this->cdnm_url = $curl;
$this->auth_token = $atoken;
return True;
}
/**
* Make sure the CF_Authentication instance has authenticated.
*
* Ensures that the instance variables necessary to communicate with
* Cloud Files have been set from a previous authenticate() call.
*
* @return boolean <kbd>True</kbd> if successfully authenticated
*/
function authenticated()
{
if (!($this->storage_url || $this->cdnm_url) || !$this->auth_token) {
return False;
}
return True;
}
/**
* Toggle debugging - set cURL verbose flag
*/
function setDebug($bool)
{
$this->dbug = $bool;
$this->cfs_http->setDebug($bool);
}
}
/**
* Class for establishing connections to the Cloud Files storage system.
* Connection instances are used to communicate with the storage system at
* the account level; listing and deleting Containers and returning Container
* instances.
*
* Example:
* <code>
* # Create the authentication instance
* #
* $auth = new CF_Authentication("username", "api_key");
*
* # Perform authentication request
* #
* $auth->authenticate();
*
* # Create a connection to the storage/cdn system(s) and pass in the
* # validated CF_Authentication instance.
* #
* $conn = new CF_Connection($auth);
*
* # NOTE: Some versions of cURL include an outdated certificate authority (CA)
* # file. This API ships with a newer version obtained directly from
* # cURL's web site (http://curl.haxx.se). To use the newer CA bundle,
* # call the CF_Authentication instance's 'ssl_use_cabundle()' method.
* #
* # $conn->ssl_use_cabundle(); # bypass cURL's old CA bundle
* </code>
*
* @package php-cloudfiles
*/
class CF_Connection
{
public $dbug;
public $cfs_http;
public $cfs_auth;
/**
* Pass in a previously authenticated CF_Authentication instance.
*
* Example:
* <code>
* # Create the authentication instance
* #
* $auth = new CF_Authentication("username", "api_key");
*
* # Perform authentication request
* #
* $auth->authenticate();
*
* # Create a connection to the storage/cdn system(s) and pass in the
* # validated CF_Authentication instance.
* #
* $conn = new CF_Connection($auth);
*
* # If you are connecting via Rackspace servers and have access
* # to the servicenet network you can set the $servicenet to True
* # like this.
*
* $conn = new CF_Connection($auth, $servicenet=True);
*
* </code>
*
* If the environement variable RACKSPACE_SERVICENET is defined it will
* force to connect via the servicenet.
*
* @param obj $cfs_auth previously authenticated CF_Authentication instance
* @param boolean $servicenet enable/disable access via Rackspace servicenet.
* @throws AuthenticationException not authenticated
*/
function __construct($cfs_auth, $servicenet=False)
{
if (isset($_ENV['RACKSPACE_SERVICENET']))
$servicenet=True;
$this->cfs_http = new CF_Http(DEFAULT_CF_API_VERSION);
$this->cfs_auth = $cfs_auth;
if (!$this->cfs_auth->authenticated()) {
$e = "Need to pass in a previously authenticated ";
$e .= "CF_Authentication instance.";
throw new AuthenticationException($e);
}
$this->cfs_http->setCFAuth($this->cfs_auth, $servicenet=$servicenet);
$this->dbug = False;
}
/**
* Toggle debugging of instance and back-end HTTP module
*
* @param boolean $bool enable/disable cURL debugging
*/
function setDebug($bool)
{
$this->dbug = (boolean) $bool;
$this->cfs_http->setDebug($this->dbug);
}
/**
* Close a connection
*
* Example:
* <code>
*
* $conn->close();
*
* </code>
*
* Will close all current cUrl active connections.
*
*/
public function close()
{
$this->cfs_http->close();
}
/**
* Cloud Files account information
*
* Return an array of two floats (since PHP only supports 32-bit integers);
* number of containers on the account and total bytes used for the account.
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
* $conn = new CF_Authentication($auth);
*
* list($quantity, $bytes) = $conn->get_info();
* print "Number of containers: " . $quantity . "\n";
* print "Bytes stored in container: " . $bytes . "\n";
* </code>
*
* @return array (number of containers, total bytes stored)
* @throws InvalidResponseException unexpected response
*/
function get_info()
{
list($status, $reason, $container_count, $total_bytes) =
$this->cfs_http->head_account();
#if ($status == 401 && $this->_re_auth()) {
# return $this->get_info();
#}
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
}
return array($container_count, $total_bytes);
}
/**
* Create a Container
*
* Given a Container name, return a Container instance, creating a new
* remote Container if it does not exit.
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
* $conn = new CF_Authentication($auth);
*
* $images = $conn->create_container("my photos");
* </code>
*
* @param string $container_name container name
* @return CF_Container
* @throws SyntaxException invalid name
* @throws InvalidResponseException unexpected response
*/
function create_container($container_name=NULL)
{
if ($container_name != "0" and !isset($container_name))
throw new SyntaxException("Container name not set.");
if (!isset($container_name) or $container_name == "")
throw new SyntaxException("Container name not set.");
if (strpos($container_name, "/") !== False) {
$r = "Container name '".$container_name;
$r .= "' cannot contain a '/' character.";
throw new SyntaxException($r);
}
if (strlen($container_name) > MAX_CONTAINER_NAME_LEN) {
throw new SyntaxException(sprintf(
"Container name exeeds %d bytes.",
MAX_CONTAINER_NAME_LEN));
}
$return_code = $this->cfs_http->create_container($container_name);
if (!$return_code) {
throw new InvalidResponseException("Invalid response ("
. $return_code. "): " . $this->cfs_http->get_error());
}
#if ($status == 401 && $this->_re_auth()) {
# return $this->create_container($container_name);
#}
if ($return_code != 201 && $return_code != 202) {
throw new InvalidResponseException(
"Invalid response (".$return_code."): "
. $this->cfs_http->get_error());
}
return new CF_Container($this->cfs_auth, $this->cfs_http, $container_name);
}
/**
* Delete a Container
*
* Given either a Container instance or name, remove the remote Container.
* The Container must be empty prior to removing it.
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
* $conn = new CF_Authentication($auth);
*
* $conn->delete_container("my photos");
* </code>
*
* @param string|obj $container container name or instance
* @return boolean <kbd>True</kbd> if successfully deleted
* @throws SyntaxException missing proper argument
* @throws InvalidResponseException invalid response
* @throws NonEmptyContainerException container not empty
* @throws NoSuchContainerException remote container does not exist
*/
function delete_container($container=NULL)
{
$container_name = NULL;
if (is_object($container)) {
if (get_class($container) == "CF_Container") {
$container_name = $container->name;
}
}
if (is_string($container)) {
$container_name = $container;
}
if ($container_name != "0" and !isset($container_name))
throw new SyntaxException("Must specify container object or name.");
$return_code = $this->cfs_http->delete_container($container_name);
if (!$return_code) {
throw new InvalidResponseException("Failed to obtain http response");
}
#if ($status == 401 && $this->_re_auth()) {
# return $this->delete_container($container);
#}
if ($return_code == 409) {
throw new NonEmptyContainerException(
"Container must be empty prior to removing it.");
}
if ($return_code == 404) {
throw new NoSuchContainerException(
"Specified container did not exist to delete.");
}
if ($return_code != 204) {
throw new InvalidResponseException(
"Invalid response (".$return_code."): "
. $this->cfs_http->get_error());
}
return True;
}
/**
* Return a Container instance
*
* For the given name, return a Container instance if the remote Container
* exists, otherwise throw a Not Found exception.
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
* $conn = new CF_Authentication($auth);
*
* $images = $conn->get_container("my photos");
* print "Number of Objects: " . $images->count . "\n";
* print "Bytes stored in container: " . $images->bytes . "\n";
* </code>
*
* @param string $container_name name of the remote Container
* @return container CF_Container instance
* @throws NoSuchContainerException thrown if no remote Container
* @throws InvalidResponseException unexpected response
*/
function get_container($container_name=NULL)
{
list($status, $reason, $count, $bytes) =
$this->cfs_http->head_container($container_name);
#if ($status == 401 && $this->_re_auth()) {
# return $this->get_container($container_name);
#}
if ($status == 404) {
throw new NoSuchContainerException("Container not found.");
}
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response: ".$this->cfs_http->get_error());
}
return new CF_Container($this->cfs_auth, $this->cfs_http,
$container_name, $count, $bytes);
}
/**
* Return array of Container instances
*
* Return an array of CF_Container instances on the account. The instances
* will be fully populated with Container attributes (bytes stored and
* Object count)
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
* $conn = new CF_Authentication($auth);
*
* $clist = $conn->get_containers();
* foreach ($clist as $cont) {
* print "Container name: " . $cont->name . "\n";
* print "Number of Objects: " . $cont->count . "\n";
* print "Bytes stored in container: " . $cont->bytes . "\n";
* }
* </code>
*
* @return array An array of CF_Container instances
* @throws InvalidResponseException unexpected response
*/
function get_containers($limit=0, $marker=NULL)
{
list($status, $reason, $container_info) =
$this->cfs_http->list_containers_info($limit, $marker);
#if ($status == 401 && $this->_re_auth()) {
# return $this->get_containers();
#}
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response: ".$this->cfs_http->get_error());
}
$containers = array();
foreach ($container_info as $name => $info) {
$containers[] = new CF_Container($this->cfs_auth, $this->cfs_http,
$info['name'], $info["count"], $info["bytes"], False);
}
return $containers;
}
/**
* Return list of remote Containers
*
* Return an array of strings containing the names of all remote Containers.
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
* $conn = new CF_Authentication($auth);
*
* $container_list = $conn->list_containers();
* print_r($container_list);
* Array
* (
* [0] => "my photos",
* [1] => "my docs"
* )
* </code>
*
* @param integer $limit restrict results to $limit Containers
* @param string $marker return results greater than $marker
* @return array list of remote Containers
* @throws InvalidResponseException unexpected response
*/
function list_containers($limit=0, $marker=NULL)
{
list($status, $reason, $containers) =
$this->cfs_http->list_containers($limit, $marker);
#if ($status == 401 && $this->_re_auth()) {
# return $this->list_containers($limit, $marker);
#}
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
}
return $containers;
}
/**
* Return array of information about remote Containers
*
* Return a nested array structure of Container info.
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
*
* $container_info = $conn->list_containers_info();
* print_r($container_info);
* Array
* (
* ["my photos"] =>
* Array
* (
* ["bytes"] => 78,
* ["count"] => 2
* )
* ["docs"] =>
* Array
* (
* ["bytes"] => 37323,
* ["count"] => 12
* )
* )
* </code>
*
* @param integer $limit restrict results to $limit Containers
* @param string $marker return results greater than $marker
* @return array nested array structure of Container info
* @throws InvalidResponseException unexpected response
*/
function list_containers_info($limit=0, $marker=NULL)
{
list($status, $reason, $container_info) =
$this->cfs_http->list_containers_info($limit, $marker);
#if ($status == 401 && $this->_re_auth()) {
# return $this->list_containers_info($limit, $marker);
#}
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
}
return $container_info;
}
/**
* Return list of Containers that have been published to the CDN.
*
* Return an array of strings containing the names of published Containers.
* Note that this function returns the list of any Container that has
* ever been CDN-enabled regardless of it's existence in the storage
* system.
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
* $conn = new CF_Authentication($auth);
*
* $public_containers = $conn->list_public_containers();
* print_r($public_containers);
* Array
* (
* [0] => "images",
* [1] => "css",
* [2] => "javascript"
* )
* </code>
*
* @return array list of published Container names
* @throws InvalidResponseException unexpected response
*/
function list_public_containers()
{
list($status, $reason, $containers) =
$this->cfs_http->list_cdn_containers();
#if ($status == 401 && $this->_re_auth()) {
# return $this->list_public_containers();
#}
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
}
return $containers;
}
/**
* Set a user-supplied callback function to report download progress
*
* The callback function is used to report incremental progress of a data
* download functions (e.g. $container->list_objects(), $obj->read(), etc).
* The specified function will be periodically called with the number of
* bytes transferred until the entire download is complete. This callback
* function can be useful for implementing "progress bars" for large
* downloads.
*
* The specified callback function should take a single integer parameter.
*
* <code>
* function read_callback($bytes_transferred) {
* print ">> downloaded " . $bytes_transferred . " bytes.\n";
* # ... do other things ...
* return;
* }
*
* $conn = new CF_Connection($auth_obj);
* $conn->set_read_progress_function("read_callback");
* print_r($conn->list_containers());
*
* # output would look like this:
* #
* >> downloaded 10 bytes.
* >> downloaded 11 bytes.
* Array
* (
* [0] => fuzzy.txt
* [1] => space name
* )
* </code>
*
* @param string $func_name the name of the user callback function
*/
function set_read_progress_function($func_name)
{
$this->cfs_http->setReadProgressFunc($func_name);
}
/**
* Set a user-supplied callback function to report upload progress
*
* The callback function is used to report incremental progress of a data
* upload functions (e.g. $obj->write() call). The specified function will
* be periodically called with the number of bytes transferred until the
* entire upload is complete. This callback function can be useful
* for implementing "progress bars" for large uploads/downloads.
*
* The specified callback function should take a single integer parameter.
*
* <code>
* function write_callback($bytes_transferred) {
* print ">> uploaded " . $bytes_transferred . " bytes.\n";
* # ... do other things ...
* return;
* }
*
* $conn = new CF_Connection($auth_obj);
* $conn->set_write_progress_function("write_callback");
* $container = $conn->create_container("stuff");
* $obj = $container->create_object("foo");
* $obj->write("The callback function will be called during upload.");
*
* # output would look like this:
* # >> uploaded 51 bytes.
* #
* </code>
*
* @param string $func_name the name of the user callback function
*/
function set_write_progress_function($func_name)
{
$this->cfs_http->setWriteProgressFunc($func_name);
}
/**
* Use the Certificate Authority bundle included with this API
*
* Most versions of PHP with cURL support include an outdated Certificate
* Authority (CA) bundle (the file that lists all valid certificate
* signing authorities). The SSL certificates used by the Cloud Files
* storage system are perfectly valid but have been created/signed by
* a CA not listed in these outdated cURL distributions.
*
* As a work-around, we've included an updated CA bundle obtained
* directly from cURL's web site (http://curl.haxx.se). You can direct
* the API to use this CA bundle by calling this method prior to making
* any remote calls. The best place to use this method is right after
* the CF_Authentication instance has been instantiated.
*
* You can specify your own CA bundle by passing in the full pathname
* to the bundle. You can use the included CA bundle by leaving the
* argument blank.
*
* @param string $path Specify path to CA bundle (default to included)
*/
function ssl_use_cabundle($path=NULL)
{
$this->cfs_http->ssl_use_cabundle($path);
}
#private function _re_auth()
#{
# $new_auth = new CF_Authentication(
# $this->cfs_auth->username,
# $this->cfs_auth->api_key,
# $this->cfs_auth->auth_host,
# $this->cfs_auth->account);
# $new_auth->authenticate();
# $this->cfs_auth = $new_auth;
# $this->cfs_http->setCFAuth($this->cfs_auth);
# return True;
#}
}
/**
* Container operations
*
* Containers are storage compartments where you put your data (objects).
* A container is similar to a directory or folder on a conventional filesystem
* with the exception that they exist in a flat namespace, you can not create
* containers inside of containers.
*
* You also have the option of marking a Container as "public" so that the
* Objects stored in the Container are publicly available via the CDN.
*
* @package php-cloudfiles
*/
class CF_Container
{
public $cfs_auth;
public $cfs_http;
public $name;
public $object_count;
public $bytes_used;
public $cdn_enabled;
public $cdn_uri;
public $cdn_ttl;
public $cdn_log_retention;
public $cdn_acl_user_agent;
public $cdn_acl_referrer;
/**
* Class constructor
*
* Constructor for Container
*
* @param obj $cfs_auth CF_Authentication instance
* @param obj $cfs_http HTTP connection manager
* @param string $name name of Container
* @param int $count number of Objects stored in this Container
* @param int $bytes number of bytes stored in this Container
* @throws SyntaxException invalid Container name
*/
function __construct(&$cfs_auth, &$cfs_http, $name, $count=0,
$bytes=0, $docdn=True)
{
if (strlen($name) > MAX_CONTAINER_NAME_LEN) {
throw new SyntaxException("Container name exceeds "
. "maximum allowed length.");
}
if (strpos($name, "/") !== False) {
throw new SyntaxException(
"Container names cannot contain a '/' character.");
}
$this->cfs_auth = $cfs_auth;
$this->cfs_http = $cfs_http;
$this->name = $name;
$this->object_count = $count;
$this->bytes_used = $bytes;
$this->cdn_enabled = NULL;
$this->cdn_uri = NULL;
$this->cdn_ttl = NULL;
$this->cdn_log_retention = NULL;
$this->cdn_acl_user_agent = NULL;
$this->cdn_acl_referrer = NULL;
if ($this->cfs_http->getCDNMUrl() != NULL && $docdn) {
$this->_cdn_initialize();
}
}
/**
* String representation of Container
*
* Pretty print the Container instance.
*
* @return string Container details
*/
function __toString()
{
$me = sprintf("name: %s, count: %.0f, bytes: %.0f",
$this->name, $this->object_count, $this->bytes_used);
if ($this->cfs_http->getCDNMUrl() != NULL) {
$me .= sprintf(", cdn: %s, cdn uri: %s, cdn ttl: %.0f, logs retention: %s",
$this->is_public() ? "Yes" : "No",
$this->cdn_uri, $this->cdn_ttl,
$this->cdn_log_retention ? "Yes" : "No"
);
if ($this->cdn_acl_user_agent != NULL) {
$me .= ", cdn acl user agent: " . $this->cdn_acl_user_agent;
}
if ($this->cdn_acl_referrer != NULL) {
$me .= ", cdn acl referrer: " . $this->cdn_acl_referrer;
}
}
return $me;
}
/**
* Enable Container content to be served via CDN or modify CDN attributes
*
* Either enable this Container's content to be served via CDN or
* adjust its CDN attributes. This Container will always return the
* same CDN-enabled URI each time it is toggled public/private/public.
*
* Example:
* <code>
* # ... authentication code excluded (see previous examples) ...
* #
* $conn = new CF_Authentication($auth);
*
* $public_container = $conn->create_container("public");
*
* # CDN-enable the container and set it's TTL for a month
* #
* $public_container->make_public(86400/2); # 12 hours (86400 seconds/day)
* </code>
*
* @param int $ttl the time in seconds content will be cached in the CDN
* @returns string the CDN enabled Container's URI
* @throws CDNNotEnabledException CDN functionality not returned during auth
* @throws AuthenticationException if auth token is not valid/expired
* @throws InvalidResponseException unexpected response
*/
function make_public($ttl=86400)
{
if ($this->cfs_http->getCDNMUrl() == NULL) {
throw new CDNNotEnabledException(
"Authentication response did not indicate CDN availability");
}
if ($this->cdn_uri != NULL) {
# previously published, assume we're setting new attributes
list($status, $reason, $cdn_uri) =
$this->cfs_http->update_cdn_container($this->name,$ttl,
$this->cdn_log_retention,
$this->cdn_acl_user_agent,
$this->cdn_acl_referrer);
#if ($status == 401 && $this->_re_auth()) {
# return $this->make_public($ttl);
#}
if ($status == 404) {
# this instance _thinks_ the container was published, but the
# cdn management system thinks otherwise - try again with a PUT
list($status, $reason, $cdn_uri) =
$this->cfs_http->add_cdn_container($this->name,$ttl);
}
} else {
# publish it for first time
list($status, $reason, $cdn_uri) =
$this->cfs_http->add_cdn_container($this->name,$ttl);
}
#if ($status == 401 && $this->_re_auth()) {
# return $this->make_public($ttl);
#}
if (!in_array($status, array(201,202))) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
}
$this->cdn_enabled = True;
$this->cdn_ttl = $ttl;
$this->cdn_uri = $cdn_uri;