Changeset 164970
- Timestamp:
- 10/19/2009 07:49:04 PM (16 years ago)
- Location:
- cdn-tools
- Files:
-
- 12 added
- 10 edited
- 1 copied
-
tags/0.94 (copied) (copied from cdn-tools/trunk)
-
tags/0.94/cdn_classes/cloudfiles/cloudfiles.php (modified) (29 diffs)
-
tags/0.94/cdn_classes/cloudfiles/cloudfiles_exceptions.php (modified) (1 diff)
-
tags/0.94/cdn_classes/cloudfiles/cloudfiles_http.php (modified) (30 diffs)
-
tags/0.94/cdn_classes/cloudfiles/share (added)
-
tags/0.94/cdn_classes/cloudfiles/share/cacert.pem (added)
-
tags/0.94/cdn_classes/cloudfiles/share/magic (added)
-
tags/0.94/cdn_classes/cloudfiles/share/magic.mgc (added)
-
tags/0.94/cdn_classes/cloudfiles/share/magic.mime (added)
-
tags/0.94/cdn_classes/cloudfiles/share/magic.mime.mgc (added)
-
tags/0.94/cdntools.php (modified) (4 diffs)
-
tags/0.94/readme.txt (modified) (4 diffs)
-
trunk/cdn_classes/cloudfiles/cloudfiles.php (modified) (29 diffs)
-
trunk/cdn_classes/cloudfiles/cloudfiles_exceptions.php (modified) (1 diff)
-
trunk/cdn_classes/cloudfiles/cloudfiles_http.php (modified) (30 diffs)
-
trunk/cdn_classes/cloudfiles/share (added)
-
trunk/cdn_classes/cloudfiles/share/cacert.pem (added)
-
trunk/cdn_classes/cloudfiles/share/magic (added)
-
trunk/cdn_classes/cloudfiles/share/magic.mgc (added)
-
trunk/cdn_classes/cloudfiles/share/magic.mime (added)
-
trunk/cdn_classes/cloudfiles/share/magic.mime.mgc (added)
-
trunk/cdntools.php (modified) (4 diffs)
-
trunk/readme.txt (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cdn-tools/tags/0.94/cdn_classes/cloudfiles/cloudfiles.php
r118371 r164970 31 31 * $bday = $images->create_object("first_birthday.jpg"); 32 32 * 33 * # Upload content from a local file by streaming it 33 * # Upload content from a local file by streaming it. Note that we use 34 * # a "float" for the file size to overcome PHP's 32-bit integer limit for 35 * # very large files. 34 36 * # 35 37 * $fname = "/home/user/photos/birthdays/birthday1.jpg"; # filename to upload … … 328 330 * Cloud Files account information 329 331 * 330 * Return an array of two integers (or possibly floats if the value 331 * overflows PHP's 32-bit integer); number of containers on the account 332 * and total bytes used for the account. 332 * Return an array of two floats (since PHP only supports 32-bit integers); 333 * number of containers on the account and total bytes used for the account. 333 334 * 334 335 * Example: … … 390 391 throw new SyntaxException($r); 391 392 } 392 if (strpos($container_name, "?") !== False) { 393 $r = "Container name '".$container_name; 394 $r .= "' cannot contain a '?' character."; 395 throw new SyntaxException($r); 396 } 397 if (mb_strlen($container_name, "UTF-8") > MAX_CONTAINER_NAME_LEN) { 393 if (strlen($container_name) > MAX_CONTAINER_NAME_LEN) { 398 394 throw new SyntaxException(sprintf( 399 "Container name exeeds %d characters.",395 "Container name exeeds %d bytes.", 400 396 MAX_CONTAINER_NAME_LEN)); 401 397 } … … 441 437 function delete_container($container=NULL) 442 438 { 439 $container_name = NULL; 440 443 441 if (is_object($container)) { 444 442 if (get_class($container) == "CF_Container") { … … 541 539 * @throws InvalidResponseException unexpected response 542 540 */ 543 function get_containers( )541 function get_containers($limit=0, $marker=NULL) 544 542 { 545 543 list($status, $reason, $container_info) = 546 $this->cfs_http->list_containers_info( );544 $this->cfs_http->list_containers_info($limit, $marker); 547 545 #if ($status == 401 && $this->_re_auth()) { 548 546 # return $this->get_containers(); … … 555 553 foreach ($container_info as $name => $info) { 556 554 $containers[] = new CF_Container($this->cfs_auth, $this->cfs_http, 557 $ name, $info["count"], $info["bytes"], False);555 $info['name'], $info["count"], $info["bytes"], False); 558 556 } 559 557 return $containers; … … 816 814 * Objects stored in the Container are publicly available via the CDN. 817 815 * 818 * NOTE: Due to the possible overflow of PHP's 32-bit integer, the Container's819 * object_count and bytes_used instance variables may either be a820 * integer or float.821 *822 816 * @package php-cloudfiles 823 817 */ … … 833 827 public $cdn_uri; 834 828 public $cdn_ttl; 829 public $cdn_log_retention; 835 830 836 831 /** … … 849 844 $bytes=0, $docdn=True) 850 845 { 851 if ( mb_strlen($name, "UTF-8") > MAX_CONTAINER_NAME_LEN) {846 if (strlen($name) > MAX_CONTAINER_NAME_LEN) { 852 847 throw new SyntaxException("Container name exceeds " 853 848 . "maximum allowed length."); … … 865 860 $this->cdn_uri = NULL; 866 861 $this->cdn_ttl = NULL; 862 $this->cdn_log_retention = NULL; 867 863 if ($this->cfs_http->getCDNMUrl() != NULL && $docdn) { 868 864 $this->_cdn_initialize(); … … 882 878 $this->name, $this->object_count, $this->bytes_used); 883 879 if ($this->cfs_http->getCDNMUrl() != NULL) { 884 $me .= sprintf(", cdn: %s, cdn uri: %s, cdn ttl: %.0f ",880 $me .= sprintf(", cdn: %s, cdn uri: %s, cdn ttl: %.0f logs retention: %s", 885 881 $this->is_public() ? "Yes" : "No", 886 $this->cdn_uri, $this->cdn_ttl); 882 $this->cdn_uri, $this->cdn_ttl, 883 $this->cdn_log_retention ? "Yes" : "No" 884 ); 887 885 } 888 886 return $me; … … 906 904 * # CDN-enable the container and set it's TTL for a month 907 905 * # 908 * $public_container->make_public(86400 *30); # 30 days (86400 seconds/day)906 * $public_container->make_public(86400/2); # 12 hours (86400 seconds/day) 909 907 * </code> 910 908 * … … 924 922 # previously published, assume we're setting new attributes 925 923 list($status, $reason, $cdn_uri) = 926 $this->cfs_http->update_cdn_container($this->name,$ttl );924 $this->cfs_http->update_cdn_container($this->name,$ttl,$this->cdn_log_retention); 927 925 #if ($status == 401 && $this->_re_auth()) { 928 926 # return $this->make_public($ttl); … … 950 948 $this->cdn_ttl = $ttl; 951 949 $this->cdn_uri = $cdn_uri; 950 $this->cdn_log_retention = False; 952 951 return $this->cdn_uri; 953 952 } 954 953 954 function log_retention($cdn_log_retention=False) { 955 if ($this->cfs_http->getCDNMUrl() == NULL) { 956 throw new CDNNotEnabledException( 957 "Authentication response did not indicate CDN availability"); 958 } 959 list($status,$reason) = 960 $this->cfs_http->update_cdn_container($this->name, 961 $this->cdn_ttl, 962 $cdn_log_retention); 963 if (!in_array($status, array(202,404))) { 964 throw new InvalidResponseException( 965 "Invalid response (".$status."): ".$this->cfs_http->get_error()); 966 } 967 $this->cdn_log_retention = $cdn_log_retention; 968 } 969 955 970 /** 956 971 * Disable the CDN sharing for this container … … 1000 1015 $this->cdn_ttl = NULL; 1001 1016 $this->cdn_uri = NULL; 1017 $this->cdn_log_retention = NULL; 1002 1018 return True; 1003 1019 } … … 1104 1120 * # 1105 1121 * $first_ten = $images->list_objects(10); 1106 * $next_ten = $images->list_objects(10,10); 1122 * 1123 * # Note the use of the previous result's last object name being 1124 * # used as the 'marker' parameter to fetch the next 10 objects 1125 * # 1126 * $next_ten = $images->list_objects(10, $first_ten[count($first_ten)-1]); 1107 1127 * 1108 1128 * # Grab images starting with "birthday_party" and default limit/marker … … 1159 1179 * # 1160 1180 * $first_ten = $images->get_objects(10); 1161 * $next_ten = $images->get_objects(10,10); 1181 * 1182 * # Note the use of the previous result's last object name being 1183 * # used as the 'marker' parameter to fetch the next 10 objects 1184 * # 1185 * $next_ten = $images->list_objects(10, $first_ten[count($first_ten)-1]); 1162 1186 * 1163 1187 * # Grab images starting with "birthday_party" and default limit/marker … … 1197 1221 $tmp = new CF_Object($this, $obj["name"], False, False); 1198 1222 $tmp->content_type = $obj["content_type"]; 1199 $tmp->content_length = $obj["bytes"];1223 $tmp->content_length = (float) $obj["bytes"]; 1200 1224 $tmp->set_etag($obj["hash"]); 1225 $tmp->last_modified = $obj["last_modified"]; 1201 1226 $objects[] = $tmp; 1202 1227 } … … 1272 1297 { 1273 1298 if ($path_name[0] == '/') { 1274 $path_name = mb_substr($path_name, 1);1299 $path_name = mb_substr($path_name, 0, 1); 1275 1300 } 1276 1301 $elements = explode('/', $path_name, -1); … … 1295 1320 private function _cdn_initialize() 1296 1321 { 1297 list($status, $reason, $cdn_enabled, $cdn_uri, $cdn_ttl ) =1322 list($status, $reason, $cdn_enabled, $cdn_uri, $cdn_ttl, $cdn_log_retention) = 1298 1323 $this->cfs_http->head_cdn_container($this->name); 1299 1324 #if ($status == 401 && $this->_re_auth()) { … … 1307 1332 $this->cdn_uri = $cdn_uri; 1308 1333 $this->cdn_ttl = $cdn_ttl; 1334 $this->cdn_log_retention = $cdn_log_retention; 1309 1335 } 1310 1336 … … 1357 1383 throw new SyntaxException($r); 1358 1384 } 1359 if ( mb_strlen($name, "UTF-8") > MAX_OBJECT_NAME_LEN) {1385 if (strlen($name) > MAX_OBJECT_NAME_LEN) { 1360 1386 throw new SyntaxException("Object name exceeds " 1361 1387 . "maximum allowed length."); … … 1389 1415 1390 1416 /** 1417 * Internal check to get the proper mimetype. 1418 * 1419 * This function would go over the available PHP methods to get 1420 * the MIME type. 1421 * 1422 * By default it will try to use the PHP fileinfo library which is 1423 * available from PHP 5.3 or as an PECL extension 1424 * (http://pecl.php.net/package/Fileinfo). 1425 * 1426 * It will get the magic file by default from the system wide file 1427 * which is usually available in /usr/share/magic on Unix or try 1428 * to use the file specified in the source directory of the API 1429 * (share directory). 1430 * 1431 * if fileinfo is not available it will try to use the internal 1432 * mime_content_type function. 1433 * 1434 * @param string $handle name of file or buffer to guess the type from 1435 * @return boolean <kbd>True</kbd> if successful 1436 * @throws BadContentTypeException 1437 */ 1438 function _guess_content_type($handle) { 1439 if ($this->content_type) 1440 return; 1441 1442 if (function_exists("finfo_open")) { 1443 $local_magic = dirname(__FILE__) . "/share/magic"; 1444 $finfo = @finfo_open(FILEINFO_MIME, $local_magic); 1445 1446 if (!$finfo) 1447 $finfo = @finfo_open(FILEINFO_MIME); 1448 1449 if ($finfo) { 1450 1451 if (is_file((string)$handle)) 1452 $ct = @finfo_file($finfo, $handle); 1453 else 1454 $ct = @finfo_buffer($finfo, $handle); 1455 1456 /* PHP 5.3 fileinfo display extra information like 1457 charset so we remove everything after the ; since 1458 we are not into that stuff */ 1459 if ($ct) { 1460 $extra_content_type_info = strpos($ct, "; "); 1461 if ($extra_content_type_info) 1462 $ct = substr($ct, 0, $extra_content_type_info); 1463 } 1464 1465 if ($ct && $ct != 'application/octet-stream') 1466 $this->content_type = $ct; 1467 1468 @finfo_close($finfo); 1469 } 1470 } 1471 1472 if (!$this->content_type && (string)is_file($handle) && function_exists("mime_content_type")) { 1473 $this->content_type = @mime_content_type($handle); 1474 } 1475 1476 if (!$this->content_type) { 1477 throw new BadContentTypeException("Required Content-Type not set"); 1478 } 1479 return True; 1480 } 1481 1482 /** 1391 1483 * String representation of the Object's public URI 1392 1484 * … … 1586 1678 * @return boolean <kbd>True</kbd> when data uploaded successfully 1587 1679 * @throws SyntaxException missing required parameters 1680 * @throws BadContentTypeException if no Content-Type was/could be set 1588 1681 * @throws MisMatchedChecksumException $verify is set and checksums unequal 1589 1682 * @throws InvalidResponseException unexpected response … … 1611 1704 # a temporary file instead. 1612 1705 # 1613 #$fp = fopen("php://temp", "rb+");1614 $fp = fopen("php://memory", "rb+");1615 fwrite($fp, $data );1706 $fp = fopen("php://temp", "wb+"); 1707 #$fp = fopen("php://memory", "wb+"); 1708 fwrite($fp, $data, strlen($data)); 1616 1709 rewind($fp); 1617 1710 $close_fh = True; 1618 $this->content_length = (float) mb_strlen($data);1711 $this->content_length = (float) strlen($data); 1619 1712 if ($this->content_length > MAX_OBJECT_SIZE) { 1620 1713 throw new SyntaxException("Data exceeds maximum object size"); 1621 1714 } 1715 $ct_data = substr($data, 0, 64); 1622 1716 } else { 1623 1717 $this->content_length = $bytes; 1624 1718 $fp = $data; 1625 } 1626 1627 /* FIXME: test if this works with string AND resource (or string only) */ 1628 if (!$this->content_type) { 1629 if (function_exists("finfo_open")) { 1630 $finfo = False; 1631 try { 1632 $finfo = @finfo_open(FILEINFO_MIME); 1633 } catch (Exception $e) { 1634 try { 1635 $my_mime = dirname(__FILE__) . "/magic"; 1636 $finfo = @finfo_open(FILEINFO_MIME, $my_mime); 1637 } catch (Exception $se) { 1638 $finfo = False; 1639 } 1640 } 1641 if (!$finfo) { 1642 $this->content_type = "application/octet-stream"; 1643 } else { 1644 $ct = finfo_buffer($finfo, $data); 1645 finfo_close($finfo); 1646 if (!$ct) { 1647 $this->content_type = "application/octet-stream"; 1648 } else { 1649 $this->content_type = $ct; 1650 } 1651 } 1652 } else { 1653 $this->content_type = "application/octet-stream"; 1654 } 1655 } 1719 $ct_data = fread($data, 64); 1720 rewind($data); 1721 } 1722 1723 $this->_guess_content_type($ct_data); 1656 1724 1657 1725 list($status, $reason, $etag) = … … 1705 1773 * @return boolean <kbd>True</kbd> if data uploaded successfully 1706 1774 * @throws SyntaxException missing required parameters 1775 * @throws BadContentTypeException if no Content-Type was/could be set 1707 1776 * @throws MisMatchedChecksumException $verify is set and checksums unequal 1708 1777 * @throws InvalidResponseException unexpected response … … 1719 1788 throw new SyntaxException("File size exceeds maximum object size."); 1720 1789 } 1721 $auto_ct = False; 1722 if (function_exists("finfo_open")) { 1723 $finfo = False; 1724 try { 1725 $finfo = @finfo_open(FILEINFO_MIME); 1726 } catch (Exception $e) { 1727 try { 1728 $my_mime = dirname(__FILE__) . "/magic"; 1729 $finfo = @finfo_open(FILEINFO_MIME, $my_mime); 1730 } catch (Exception $se) { 1731 $finfo = False; 1732 } 1733 } 1734 if ($finfo) { 1735 $ct = finfo_file($finfo, $filename); 1736 finfo_close($finfo); 1737 if ($ct) { 1738 $this->content_type = $ct; 1739 $auto_ct = True; 1740 } 1741 } 1742 } 1743 if (!$auto_ct && function_exists("mime_content_type")) { 1744 $this->content_type = @mime_content_type($filename); 1745 } else { 1746 $this->content_type = "application/octet-stream"; 1747 } 1748 //ghetto png fix 1749 $ext = strtolower(array_pop(explode('.',$filename))); 1750 if($ext == 'png') { 1751 $this->content_type = 'image/png'; 1752 } 1753 //ghetto png fix 1790 1791 $this->_guess_content_type($filename); 1792 1754 1793 $this->write($fp, $size, $verify); 1755 1794 fclose($fp); … … 1825 1864 * may either be a local filename, open resource for reading, or a string. 1826 1865 * 1827 * <b>WARNING:</b> If $data is a resource, the entire contents are read 1828 * into a local variable (memory) before computing the checksum! You 1829 * should ensure that you have enough RAM to support this. 1866 * <b>WARNING:</b> if you are uploading a big file over a stream 1867 * it could get very slow to compute the md5 you probably want to 1868 * set the $verify parameter to False in the write() method and 1869 * compute yourself the md5 before if you have it. 1830 1870 * 1831 1871 * @param filename|obj|string $data filename, open resource, or string … … 1835 1875 { 1836 1876 1837 if (is_file($data)) { 1877 if (function_exists("hash_init") && is_resource($data)) { 1878 $ctx = hash_init('md5'); 1879 while (!feof($data)) { 1880 $buffer = fgets($data, 65536); 1881 hash_update($ctx, $buffer); 1882 } 1883 $md5 = hash_final($ctx, false); 1884 rewind($data); 1885 } elseif ((string)is_file($data)) { 1838 1886 $md5 = md5_file($data); 1839 } elseif (is_resource($data)) {1840 # let's hope this isn't a BIG file1841 $contents = stream_get_contents($data); # PHP 5 and up1842 $md5 = md5($contents);1843 rewind($data);1844 1887 } else { 1845 1888 $md5 = md5($data); -
cdn-tools/tags/0.94/cdn_classes/cloudfiles/cloudfiles_exceptions.php
r118371 r164970 26 26 class IOException extends Exception { } 27 27 class CDNNotEnabledException extends Exception { } 28 class BadContentTypeException extends Exception { } 29 class InvalidUTF8Exception extends Exception { } 28 30 29 31 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ -
cdn-tools/tags/0.94/cdn_classes/cloudfiles/cloudfiles_http.php
r118371 r164970 30 30 require_once("cloudfiles_exceptions.php"); 31 31 32 define("PHP_CF_VERSION", "1. 3.0");32 define("PHP_CF_VERSION", "1.4.0"); 33 33 define("USER_AGENT", sprintf("PHP-CloudFiles/%s", PHP_CF_VERSION)); 34 34 define("ACCOUNT_CONTAINER_COUNT", "X-Account-Container-Count"); … … 39 39 define("CDN_URI", "X-CDN-URI"); 40 40 define("CDN_ENABLED", "X-CDN-Enabled"); 41 define("CDN_LOG_RETENTION", "X-Log-Retention"); 41 42 define("CDN_TTL", "X-TTL"); 42 43 define("CDNM_URL", "X-CDN-Management-Url"); … … 96 97 private $_cdn_uri; 97 98 private $_cdn_ttl; 99 private $_cdn_log_retention; 98 100 99 101 function __construct($api_version) … … 142 144 $this->_cdn_uri = NULL; 143 145 $this->_cdn_ttl = NULL; 146 $this->_cdn_log_retention = NULL; 147 148 # The OS list with a PHP without an updated CA File for CURL to 149 # connect to SSL Websites. It is the first 3 letters of the PHP_OS 150 # variable. 151 $OS_CAFILE_NONUPDATED=array( 152 "win", 153 ); 154 155 if (in_array((strtolower (substr(PHP_OS, 0,3))), $OS_CAFILE_NONUPDATED)) 156 $this->ssl_use_cabundle(); 157 144 158 } 145 159 … … 154 168 throw new IOException("Could not use CA bundle: " 155 169 . $this->cabundle_path); 156 }157 return;158 }159 160 private function _set_connections_cainfo()161 {162 if ($this->cabundle_path) {163 foreach ($this->connections as $k => $v) {164 if (!is_null($v)) {165 curl_setopt($this->connections[$k], CURLOPT_SSL_VERIFYPEER,166 True);167 curl_setopt($this->connections[$k], CURLOPT_CAINFO,168 $this->cabundle_path);169 }170 }171 170 } 172 171 return; … … 197 196 198 197 $curl_ch = curl_init(); 199 if ( $this->cabundle_path) {198 if (!is_null($this->cabundle_path)) { 200 199 curl_setopt($curl_ch, CURLOPT_SSL_VERIFYPEER, True); 201 curl_setopt($curl_ch, CURLOPT_CAINFO, 202 $this->cabundle_path); 200 curl_setopt($curl_ch, CURLOPT_CAINFO, $this->cabundle_path); 203 201 } 204 202 curl_setopt($curl_ch, CURLOPT_VERBOSE, $this->dbug); … … 228 226 229 227 if (!$return_code) { 230 $this->error_str = "Failed to obtain http response";228 $this->error_str .= ": Failed to obtain valid HTTP response."; 231 229 array(0,$this->error_str,array()); 232 230 } … … 250 248 # (CDN) POST /v1/Account/Container 251 249 # 252 function update_cdn_container($container_name, $ttl=86400 )250 function update_cdn_container($container_name, $ttl=86400, $cdn_log_retention=False) 253 251 { 254 252 if (!$container_name) { 255 253 throw new SyntaxException("Container name not set."); 256 254 } 255 257 256 $url_path = $this->_make_path("CDN", $container_name); 258 257 $hdrs = array( 259 258 CDN_ENABLED => "True", 260 259 CDN_TTL => $ttl, 260 CDN_LOG_RETENTION => $cdn_log_retention ? "True" : "False", 261 261 ); 262 262 $return_code = $this->_send_request("DEL_POST",$url_path,$hdrs,"POST"); … … 338 338 339 339 if (!$return_code) { 340 $this->error_str = "Failed to obtain http response";341 array(0,$this->error_str,NULL,NULL,NULL);340 $this->error_str .= ": Failed to obtain valid HTTP response."; 341 return array(0,$this->error_str,NULL,NULL,NULL,NULL); 342 342 } 343 343 if ($return_code == 401) { 344 return array($return_code,"Unauthorized",NULL,NULL,NULL );345 } 346 if ($return_code == 404) { 347 return array($return_code,"Account not found.",NULL,NULL,NULL );344 return array($return_code,"Unauthorized",NULL,NULL,NULL,NULL); 345 } 346 if ($return_code == 404) { 347 return array($return_code,"Account not found.",NULL,NULL,NULL,NULL); 348 348 } 349 349 if ($return_code == 204) { 350 350 return array($return_code,$this->response_reason, 351 $this->_cdn_enabled, $this->_cdn_uri, $this->_cdn_ttl); 352 } 353 return array($return_code,$this->response_reason,NULL,NULL,NULL); 351 $this->_cdn_enabled, $this->_cdn_uri, $this->_cdn_ttl, 352 $this->_cdn_log_retention 353 ); 354 } 355 return array($return_code,$this->response_reason,NULL,NULL,NULL,$this->_cdn_log_retention); 354 356 } 355 357 … … 377 379 378 380 if (!$return_code) { 379 $this->error_str = "Failed to obtain valid HTTP response.";381 $this->error_str .= ": Failed to obtain valid HTTP response."; 380 382 return array(0,$this->error_str,array()); 381 383 } … … 417 419 418 420 if (!$return_code) { 419 $this->error_str = "Failed to obtain valid HTTP response.";421 $this->error_str .= ": Failed to obtain valid HTTP response."; 420 422 return array(0,$this->error_str,array()); 421 423 } … … 445 447 446 448 if (!$return_code) { 447 $this->error_str = "Failed to obtain http response";449 $this->error_str .= ": Failed to obtain valid HTTP response."; 448 450 array(0,$this->error_str,0,0); 449 451 } … … 470 472 471 473 if (!$return_code) { 472 $this->error_str = "Failed to obtain http response";474 $this->error_str .= ": Failed to obtain valid HTTP response."; 473 475 return False; 474 476 } … … 488 490 489 491 if (!$return_code) { 490 $this->error_str = "Failed to obtain http response";492 $this->error_str .= ": Failed to obtain valid HTTP response."; 491 493 } 492 494 if ($return_code == 409) { … … 536 538 537 539 if (!$return_code) { 538 $this->error_str = "Failed to obtain http response";540 $this->error_str .= ": Failed to obtain valid HTTP response."; 539 541 return array(0,$this->error_str,array()); 540 542 } … … 589 591 590 592 if (!$return_code) { 591 $this->error_str = "Failed to obtain http response";593 $this->error_str .= ": Failed to obtain valid HTTP response."; 592 594 return array(0,$this->error_str,array()); 593 595 } … … 624 626 625 627 if (!$return_code) { 626 $this->error_str = "Failed to obtain http response";628 $this->error_str .= ": Failed to obtain valid HTTP response."; 627 629 array(0,$this->error_str,0,0); 628 630 } … … 653 655 654 656 if (!$return_code) { 655 $this->error_str = "Failed to obtain http response";657 $this->error_str .= ": Failed to obtain valid HTTP response."; 656 658 return array($return_code0,$this->error_str,NULL); 657 659 } … … 689 691 690 692 if (!$return_code) { 691 $this->error_str = "Failed to obtain http response";693 $this->error_str .= ": Failed to obtain valid HTTP response."; 692 694 return array($return_code,$this->error_str); 693 695 } … … 747 749 $return_code = $this->_send_request($conn_type,$url_path,$hdrs); 748 750 if (!$return_code) { 749 $this->error_str = "Failed to obtain http response";751 $this->error_str .= ": Failed to obtain valid HTTP response."; 750 752 return array(0,$this->error_str,NULL); 751 753 } … … 784 786 $return_code = $this->_send_request("DEL_POST",$url_path,$hdrs,"POST"); 785 787 if (!$return_code) { 786 $this->error_str = "Failed to obtain http response";788 $this->error_str .= ": Failed to obtain valid HTTP response."; 787 789 return 0; 788 790 } … … 811 813 812 814 if (!$return_code) { 813 $this->error_str = "Failed to obtain http response";815 $this->error_str .= ": Failed to obtain valid HTTP response."; 814 816 return array(0, $this->error_str." ".$this->response_reason, 815 817 NULL, NULL, NULL, NULL, array()); … … 845 847 $return_code = $this->_send_request("DEL_POST",$url_path,NULL,"DELETE"); 846 848 if (!$return_code) { 847 $this->error_str = "Failed to obtain http response";849 $this->error_str .= ": Failed to obtain valid HTTP response."; 848 850 return 0; 849 851 } … … 932 934 return strlen($header); 933 935 } 936 if (stripos($header, CDN_LOG_RETENTION) === 0) { 937 $this->_cdn_log_retention = 938 trim(substr($header, strlen(CDN_LOG_RETENTION)+1)) == "True" ? True : False; 939 return strlen($header); 940 } 934 941 if (stripos($header, ACCOUNT_CONTAINER_COUNT) === 0) { 935 $this->_account_container_count = trim(substr($header,942 $this->_account_container_count = (float) trim(substr($header, 936 943 strlen(ACCOUNT_CONTAINER_COUNT)+1))+0; 937 944 return strlen($header); 938 945 } 939 946 if (stripos($header, ACCOUNT_BYTES_USED) === 0) { 940 $this->_account_bytes_used = trim(substr($header,947 $this->_account_bytes_used = (float) trim(substr($header, 941 948 strlen(ACCOUNT_BYTES_USED)+1))+0; 942 949 return strlen($header); 943 950 } 944 951 if (stripos($header, CONTAINER_OBJ_COUNT) === 0) { 945 $this->_container_object_count = trim(substr($header,952 $this->_container_object_count = (float) trim(substr($header, 946 953 strlen(CONTAINER_OBJ_COUNT)+1))+0; 947 954 return strlen($header); 948 955 } 949 956 if (stripos($header, CONTAINER_BYTES_USED) === 0) { 950 $this->_container_bytes_used = trim(substr($header,957 $this->_container_bytes_used = (float) trim(substr($header, 951 958 strlen(CONTAINER_BYTES_USED)+1))+0; 952 959 return strlen($header); 953 960 } 954 961 if (stripos($header, METADATA_HEADER) === 0) { 962 # $header => X-Object-Meta-Foo: bar baz 955 963 $temp = substr($header, strlen(METADATA_HEADER)); 964 # $temp => Foo: bar baz 956 965 $parts = explode(":", $temp); 957 $this->_obj_metadata[strtolower($parts[0])] = trim($parts[1]); 966 # $parts[0] => Foo 967 $val = substr(strstr($temp, ":"), 1); 968 # $val => bar baz 969 $this->_obj_metadata[$parts[0]] = trim($val); 958 970 return strlen($header); 959 971 } 960 if (stripos($header, "ETag") === 0) { 961 $parts = explode(":", $header); 962 $this->_obj_etag = trim($parts[1]); 972 if (stripos($header, "ETag:") === 0) { 973 # $header => ETag: abc123def456... 974 $val = substr(strstr($header, ":"), 1); 975 # $val => abc123def456... 976 $this->_obj_etag = trim($val); 963 977 return strlen($header); 964 978 } 965 if (stripos($header, "Last-Modified ") === 0) {966 $ parts = explode(":", $header);967 $this->_obj_last_modified = trim($ parts[1]);979 if (stripos($header, "Last-Modified:") === 0) { 980 $val = substr(strstr($header, ":"), 1); 981 $this->_obj_last_modified = trim($val); 968 982 return strlen($header); 969 983 } 970 if (stripos($header, "Content-Type ") === 0) {971 $ parts = explode(":", $header);972 $this->_obj_content_type = trim($ parts[1]);984 if (stripos($header, "Content-Type:") === 0) { 985 $val = substr(strstr($header, ":"), 1); 986 $this->_obj_content_type = trim($val); 973 987 return strlen($header); 974 988 } 975 if (stripos($header, "Content-Length ") === 0) {976 $ parts = explode(":", $header);977 $this->_obj_content_length = trim($parts[1])+0;989 if (stripos($header, "Content-Length:") === 0) { 990 $val = substr(strstr($header, ":"), 1); 991 $this->_obj_content_length = (float) trim($val)+0; 978 992 return strlen($header); 979 993 } … … 993 1007 private function _write_cb($ch, $data) 994 1008 { 995 $ amount= strlen($data);1009 $dlen = strlen($data); 996 1010 switch ($this->_write_callback_type) { 997 1011 case "TEXT_LIST": … … 999 1013 break; 1000 1014 case "OBJECT_STREAM": 1001 $written = 0; 1002 while ($written < strlen($data)) { 1003 $written += fwrite($this->_obj_write_resource, $data-$written); 1004 } 1005 if ($written > strlen($data)) { 1006 throw new IOException( 1007 "Wrote more data to client than we should have?!"); 1008 } 1015 fwrite($this->_obj_write_resource, $data, $dlen); 1009 1016 break; 1010 1017 case "OBJECT_STRING": … … 1013 1020 } 1014 1021 if (isset($this->_user_read_progress_callback_func)) { 1015 call_user_func($this->_user_read_progress_callback_func, strlen($data));1016 } 1017 return strlen($data);1022 call_user_func($this->_user_read_progress_callback_func, $dlen); 1023 } 1024 return $dlen; 1018 1025 } 1019 1026 … … 1052 1059 $parts = explode(":", $v); 1053 1060 $header = $parts[0]; 1054 $value = trim( $parts[1]);1061 $value = trim(substr(strstr($v, ":"), 1)); 1055 1062 } else { 1056 1063 $header = $h; … … 1089 1096 } 1090 1097 1091 $this->_set_connections_cainfo();1092 1093 1098 if ($this->dbug) { curl_setopt($ch, CURLOPT_VERBOSE, 1); } 1099 1100 if (!is_null($this->cabundle_path)) { 1101 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, True); 1102 curl_setopt($ch, CURLOPT_CAINFO, $this->cabundle_path); 1103 } 1094 1104 1095 1105 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); … … 1165 1175 $hdrs = array(); 1166 1176 foreach ($obj->metadata as $k => $v) { 1167 if (strpos($k,":") !== False || strpos($v,":") !== False) {1177 if (strpos($k,":") !== False) { 1168 1178 throw new SyntaxException( 1169 "Metadata cannot contain a ':' character.");1179 "Metadata keys cannot contain a ':' character."); 1170 1180 } 1171 $k = strtolower(trim($k));1181 $k = trim($k); 1172 1182 $key = sprintf("%s%s", METADATA_HEADER, $k); 1173 1183 if (!array_key_exists($key, $hdrs)) { -
cdn-tools/tags/0.94/cdntools.php
r161722 r164970 5 5 Description: CDN Tools is a plugin designed to help you drastically speed up your blog's load time by loading content onto a distribution network. You can use a commercial CDN or just load some of your larger JS libraries for free from Google's servers! At this time Cloud Files is the only supported CDN. 6 6 Author: Paul Kehrer 7 Version: 0.9 37 Version: 0.94 8 8 Author URI: http://langui.sh/ 9 9 */ … … 28 28 set_time_limit(300); //5 minutes max... 29 29 30 define('CDNTOOLS_VERSION','0.9 3');30 define('CDNTOOLS_VERSION','0.94'); 31 31 32 32 $dir_array = explode('/',dirname(__FILE__)); … … 121 121 122 122 function bind_filters() { 123 add_filter('init',array($this,'disable_script_concatenation')); 123 124 add_filter( 'print_scripts_array',array($this,"jquery_noconflict"),100); 124 125 add_filter('wp_handle_upload', array($this,'handle_upload_filter')); //grab data about uploads … … 127 128 add_filter('the_content', array($this,'cdn_media_url_rewrite'), 1000); //this needs to run after any other filter 128 129 add_filter('wp_generate_attachment_metadata', array($this,'cdn_upload_resized_images')); 130 } 131 132 //wp 2.8 concatenates scripts in the admin panel and this messes up the google ajax rewriting. hook init and disable it for now. revisit for 2.9 133 function disable_script_concatenation() { 134 global $concatenate_scripts; 135 $concatenate_scripts = false; 129 136 } 130 137 -
cdn-tools/tags/0.94/readme.txt
r161722 r164970 5 5 Requires at least: 2.7 6 6 Tested up to: 2.8.4 7 Stable tag: 0.9 37 Stable tag: 0.94 8 8 9 9 CDN Tools is a plugin designed to help you drastically speed up your blog's load time by loading data onto a content distribution network (CDN). … … 11 11 == Description == 12 12 13 [CDN Tools](http:// swearingscience.com/cdn-tools/ "CDN Tools Home") is a WordPress plugin that allows you to load javascript and media files to an external server to drastically speed page loads. You can sideload data to a commercial CDN or just load your larger JS libraries (prototype, jquery) for **free** from Google's servers. CDN Tools has been designed to be as easy to use as possible, so give it a shot! Your blog's readers will thank you. At this time the only commercial CDN supported is Cloud Files. Check out the plugin homepage to view a screencast.13 [CDN Tools](http://langui.sh/cdn-tools/ "CDN Tools Home") is a WordPress plugin that allows you to load javascript and media files to an external server to drastically speed page loads. You can sideload data to a commercial CDN or just load your larger JS libraries (prototype, jquery) for **free** from Google's servers. CDN Tools has been designed to be as easy to use as possible, so give it a shot! Your blog's readers will thank you. At this time the only commercial CDN supported is Cloud Files. Check out the plugin homepage to view a screencast. 14 14 15 = New in 0.9 2/0.93=16 * A partial fix for issues with customers who have full file paths stored in their postmeta table. This is not a complete fix, but should help some (most?)users.17 * Major upgrade to support WP 2.8. If you are a previous user of CDN Tools you _MUST_ unload files and then load them again. Please let me know if you have issues because several major changes were made. See http://langui.sh/cdn-tools for more details.15 = New in 0.94 = 16 * Upgraded to CF API 1.4. This should resolve some cURL issues users have experienced and removes the dependence on a png hack for RHEL4 users. 17 * Fixed major issue with Google AJAX CDN. Previously users were unable to switch between visual and HTML mode on the edit post page. 18 18 19 [View complete changelog](http:// swearingscience.com/cdn-tools/ "CDN Tools Home").19 [View complete changelog](http://langui.sh/cdn-tools/ "CDN Tools Home"). 20 20 21 21 == Installation == … … 33 33 = Could you add XXXX CDN compatibility? = 34 34 35 I would love to, but I am not currently willing to buy accounts and pay to develop for every possible CDN out there. If you'd like to supply me with a test account so I can support your favorite CDN I'd be happy to. Drop me a comment on the CDN Tools home page.35 Unfortunately I'm likely too busy to do this for your CDN, but I'd be happy to answer questions if you need some assistance in developing your own! If you write a plugin, please let me know so I can merge it into the main distribution. 36 36 37 37 = I found a bug. What do I do? = … … 46 46 47 47 == Changelog == 48 = 0.94 - 10/19/2009 = 49 * Upgraded to CF API 1.4. This should resolve some cURL issues users have experienced and removes the dependence on a png hack for RHEL4 users. 50 * Fixed major issue with Google AJAX CDN. Previously users were unable to switch between visual and HTML mode on the edit post page. 51 48 52 = 0.93 - 10/8/2009 = 49 53 * A partial fix for issues with customers who have full file paths stored in their postmeta table. This is not a complete fix, but should help some (most?) users. -
cdn-tools/trunk/cdn_classes/cloudfiles/cloudfiles.php
r118371 r164970 31 31 * $bday = $images->create_object("first_birthday.jpg"); 32 32 * 33 * # Upload content from a local file by streaming it 33 * # Upload content from a local file by streaming it. Note that we use 34 * # a "float" for the file size to overcome PHP's 32-bit integer limit for 35 * # very large files. 34 36 * # 35 37 * $fname = "/home/user/photos/birthdays/birthday1.jpg"; # filename to upload … … 328 330 * Cloud Files account information 329 331 * 330 * Return an array of two integers (or possibly floats if the value 331 * overflows PHP's 32-bit integer); number of containers on the account 332 * and total bytes used for the account. 332 * Return an array of two floats (since PHP only supports 32-bit integers); 333 * number of containers on the account and total bytes used for the account. 333 334 * 334 335 * Example: … … 390 391 throw new SyntaxException($r); 391 392 } 392 if (strpos($container_name, "?") !== False) { 393 $r = "Container name '".$container_name; 394 $r .= "' cannot contain a '?' character."; 395 throw new SyntaxException($r); 396 } 397 if (mb_strlen($container_name, "UTF-8") > MAX_CONTAINER_NAME_LEN) { 393 if (strlen($container_name) > MAX_CONTAINER_NAME_LEN) { 398 394 throw new SyntaxException(sprintf( 399 "Container name exeeds %d characters.",395 "Container name exeeds %d bytes.", 400 396 MAX_CONTAINER_NAME_LEN)); 401 397 } … … 441 437 function delete_container($container=NULL) 442 438 { 439 $container_name = NULL; 440 443 441 if (is_object($container)) { 444 442 if (get_class($container) == "CF_Container") { … … 541 539 * @throws InvalidResponseException unexpected response 542 540 */ 543 function get_containers( )541 function get_containers($limit=0, $marker=NULL) 544 542 { 545 543 list($status, $reason, $container_info) = 546 $this->cfs_http->list_containers_info( );544 $this->cfs_http->list_containers_info($limit, $marker); 547 545 #if ($status == 401 && $this->_re_auth()) { 548 546 # return $this->get_containers(); … … 555 553 foreach ($container_info as $name => $info) { 556 554 $containers[] = new CF_Container($this->cfs_auth, $this->cfs_http, 557 $ name, $info["count"], $info["bytes"], False);555 $info['name'], $info["count"], $info["bytes"], False); 558 556 } 559 557 return $containers; … … 816 814 * Objects stored in the Container are publicly available via the CDN. 817 815 * 818 * NOTE: Due to the possible overflow of PHP's 32-bit integer, the Container's819 * object_count and bytes_used instance variables may either be a820 * integer or float.821 *822 816 * @package php-cloudfiles 823 817 */ … … 833 827 public $cdn_uri; 834 828 public $cdn_ttl; 829 public $cdn_log_retention; 835 830 836 831 /** … … 849 844 $bytes=0, $docdn=True) 850 845 { 851 if ( mb_strlen($name, "UTF-8") > MAX_CONTAINER_NAME_LEN) {846 if (strlen($name) > MAX_CONTAINER_NAME_LEN) { 852 847 throw new SyntaxException("Container name exceeds " 853 848 . "maximum allowed length."); … … 865 860 $this->cdn_uri = NULL; 866 861 $this->cdn_ttl = NULL; 862 $this->cdn_log_retention = NULL; 867 863 if ($this->cfs_http->getCDNMUrl() != NULL && $docdn) { 868 864 $this->_cdn_initialize(); … … 882 878 $this->name, $this->object_count, $this->bytes_used); 883 879 if ($this->cfs_http->getCDNMUrl() != NULL) { 884 $me .= sprintf(", cdn: %s, cdn uri: %s, cdn ttl: %.0f ",880 $me .= sprintf(", cdn: %s, cdn uri: %s, cdn ttl: %.0f logs retention: %s", 885 881 $this->is_public() ? "Yes" : "No", 886 $this->cdn_uri, $this->cdn_ttl); 882 $this->cdn_uri, $this->cdn_ttl, 883 $this->cdn_log_retention ? "Yes" : "No" 884 ); 887 885 } 888 886 return $me; … … 906 904 * # CDN-enable the container and set it's TTL for a month 907 905 * # 908 * $public_container->make_public(86400 *30); # 30 days (86400 seconds/day)906 * $public_container->make_public(86400/2); # 12 hours (86400 seconds/day) 909 907 * </code> 910 908 * … … 924 922 # previously published, assume we're setting new attributes 925 923 list($status, $reason, $cdn_uri) = 926 $this->cfs_http->update_cdn_container($this->name,$ttl );924 $this->cfs_http->update_cdn_container($this->name,$ttl,$this->cdn_log_retention); 927 925 #if ($status == 401 && $this->_re_auth()) { 928 926 # return $this->make_public($ttl); … … 950 948 $this->cdn_ttl = $ttl; 951 949 $this->cdn_uri = $cdn_uri; 950 $this->cdn_log_retention = False; 952 951 return $this->cdn_uri; 953 952 } 954 953 954 function log_retention($cdn_log_retention=False) { 955 if ($this->cfs_http->getCDNMUrl() == NULL) { 956 throw new CDNNotEnabledException( 957 "Authentication response did not indicate CDN availability"); 958 } 959 list($status,$reason) = 960 $this->cfs_http->update_cdn_container($this->name, 961 $this->cdn_ttl, 962 $cdn_log_retention); 963 if (!in_array($status, array(202,404))) { 964 throw new InvalidResponseException( 965 "Invalid response (".$status."): ".$this->cfs_http->get_error()); 966 } 967 $this->cdn_log_retention = $cdn_log_retention; 968 } 969 955 970 /** 956 971 * Disable the CDN sharing for this container … … 1000 1015 $this->cdn_ttl = NULL; 1001 1016 $this->cdn_uri = NULL; 1017 $this->cdn_log_retention = NULL; 1002 1018 return True; 1003 1019 } … … 1104 1120 * # 1105 1121 * $first_ten = $images->list_objects(10); 1106 * $next_ten = $images->list_objects(10,10); 1122 * 1123 * # Note the use of the previous result's last object name being 1124 * # used as the 'marker' parameter to fetch the next 10 objects 1125 * # 1126 * $next_ten = $images->list_objects(10, $first_ten[count($first_ten)-1]); 1107 1127 * 1108 1128 * # Grab images starting with "birthday_party" and default limit/marker … … 1159 1179 * # 1160 1180 * $first_ten = $images->get_objects(10); 1161 * $next_ten = $images->get_objects(10,10); 1181 * 1182 * # Note the use of the previous result's last object name being 1183 * # used as the 'marker' parameter to fetch the next 10 objects 1184 * # 1185 * $next_ten = $images->list_objects(10, $first_ten[count($first_ten)-1]); 1162 1186 * 1163 1187 * # Grab images starting with "birthday_party" and default limit/marker … … 1197 1221 $tmp = new CF_Object($this, $obj["name"], False, False); 1198 1222 $tmp->content_type = $obj["content_type"]; 1199 $tmp->content_length = $obj["bytes"];1223 $tmp->content_length = (float) $obj["bytes"]; 1200 1224 $tmp->set_etag($obj["hash"]); 1225 $tmp->last_modified = $obj["last_modified"]; 1201 1226 $objects[] = $tmp; 1202 1227 } … … 1272 1297 { 1273 1298 if ($path_name[0] == '/') { 1274 $path_name = mb_substr($path_name, 1);1299 $path_name = mb_substr($path_name, 0, 1); 1275 1300 } 1276 1301 $elements = explode('/', $path_name, -1); … … 1295 1320 private function _cdn_initialize() 1296 1321 { 1297 list($status, $reason, $cdn_enabled, $cdn_uri, $cdn_ttl ) =1322 list($status, $reason, $cdn_enabled, $cdn_uri, $cdn_ttl, $cdn_log_retention) = 1298 1323 $this->cfs_http->head_cdn_container($this->name); 1299 1324 #if ($status == 401 && $this->_re_auth()) { … … 1307 1332 $this->cdn_uri = $cdn_uri; 1308 1333 $this->cdn_ttl = $cdn_ttl; 1334 $this->cdn_log_retention = $cdn_log_retention; 1309 1335 } 1310 1336 … … 1357 1383 throw new SyntaxException($r); 1358 1384 } 1359 if ( mb_strlen($name, "UTF-8") > MAX_OBJECT_NAME_LEN) {1385 if (strlen($name) > MAX_OBJECT_NAME_LEN) { 1360 1386 throw new SyntaxException("Object name exceeds " 1361 1387 . "maximum allowed length."); … … 1389 1415 1390 1416 /** 1417 * Internal check to get the proper mimetype. 1418 * 1419 * This function would go over the available PHP methods to get 1420 * the MIME type. 1421 * 1422 * By default it will try to use the PHP fileinfo library which is 1423 * available from PHP 5.3 or as an PECL extension 1424 * (http://pecl.php.net/package/Fileinfo). 1425 * 1426 * It will get the magic file by default from the system wide file 1427 * which is usually available in /usr/share/magic on Unix or try 1428 * to use the file specified in the source directory of the API 1429 * (share directory). 1430 * 1431 * if fileinfo is not available it will try to use the internal 1432 * mime_content_type function. 1433 * 1434 * @param string $handle name of file or buffer to guess the type from 1435 * @return boolean <kbd>True</kbd> if successful 1436 * @throws BadContentTypeException 1437 */ 1438 function _guess_content_type($handle) { 1439 if ($this->content_type) 1440 return; 1441 1442 if (function_exists("finfo_open")) { 1443 $local_magic = dirname(__FILE__) . "/share/magic"; 1444 $finfo = @finfo_open(FILEINFO_MIME, $local_magic); 1445 1446 if (!$finfo) 1447 $finfo = @finfo_open(FILEINFO_MIME); 1448 1449 if ($finfo) { 1450 1451 if (is_file((string)$handle)) 1452 $ct = @finfo_file($finfo, $handle); 1453 else 1454 $ct = @finfo_buffer($finfo, $handle); 1455 1456 /* PHP 5.3 fileinfo display extra information like 1457 charset so we remove everything after the ; since 1458 we are not into that stuff */ 1459 if ($ct) { 1460 $extra_content_type_info = strpos($ct, "; "); 1461 if ($extra_content_type_info) 1462 $ct = substr($ct, 0, $extra_content_type_info); 1463 } 1464 1465 if ($ct && $ct != 'application/octet-stream') 1466 $this->content_type = $ct; 1467 1468 @finfo_close($finfo); 1469 } 1470 } 1471 1472 if (!$this->content_type && (string)is_file($handle) && function_exists("mime_content_type")) { 1473 $this->content_type = @mime_content_type($handle); 1474 } 1475 1476 if (!$this->content_type) { 1477 throw new BadContentTypeException("Required Content-Type not set"); 1478 } 1479 return True; 1480 } 1481 1482 /** 1391 1483 * String representation of the Object's public URI 1392 1484 * … … 1586 1678 * @return boolean <kbd>True</kbd> when data uploaded successfully 1587 1679 * @throws SyntaxException missing required parameters 1680 * @throws BadContentTypeException if no Content-Type was/could be set 1588 1681 * @throws MisMatchedChecksumException $verify is set and checksums unequal 1589 1682 * @throws InvalidResponseException unexpected response … … 1611 1704 # a temporary file instead. 1612 1705 # 1613 #$fp = fopen("php://temp", "rb+");1614 $fp = fopen("php://memory", "rb+");1615 fwrite($fp, $data );1706 $fp = fopen("php://temp", "wb+"); 1707 #$fp = fopen("php://memory", "wb+"); 1708 fwrite($fp, $data, strlen($data)); 1616 1709 rewind($fp); 1617 1710 $close_fh = True; 1618 $this->content_length = (float) mb_strlen($data);1711 $this->content_length = (float) strlen($data); 1619 1712 if ($this->content_length > MAX_OBJECT_SIZE) { 1620 1713 throw new SyntaxException("Data exceeds maximum object size"); 1621 1714 } 1715 $ct_data = substr($data, 0, 64); 1622 1716 } else { 1623 1717 $this->content_length = $bytes; 1624 1718 $fp = $data; 1625 } 1626 1627 /* FIXME: test if this works with string AND resource (or string only) */ 1628 if (!$this->content_type) { 1629 if (function_exists("finfo_open")) { 1630 $finfo = False; 1631 try { 1632 $finfo = @finfo_open(FILEINFO_MIME); 1633 } catch (Exception $e) { 1634 try { 1635 $my_mime = dirname(__FILE__) . "/magic"; 1636 $finfo = @finfo_open(FILEINFO_MIME, $my_mime); 1637 } catch (Exception $se) { 1638 $finfo = False; 1639 } 1640 } 1641 if (!$finfo) { 1642 $this->content_type = "application/octet-stream"; 1643 } else { 1644 $ct = finfo_buffer($finfo, $data); 1645 finfo_close($finfo); 1646 if (!$ct) { 1647 $this->content_type = "application/octet-stream"; 1648 } else { 1649 $this->content_type = $ct; 1650 } 1651 } 1652 } else { 1653 $this->content_type = "application/octet-stream"; 1654 } 1655 } 1719 $ct_data = fread($data, 64); 1720 rewind($data); 1721 } 1722 1723 $this->_guess_content_type($ct_data); 1656 1724 1657 1725 list($status, $reason, $etag) = … … 1705 1773 * @return boolean <kbd>True</kbd> if data uploaded successfully 1706 1774 * @throws SyntaxException missing required parameters 1775 * @throws BadContentTypeException if no Content-Type was/could be set 1707 1776 * @throws MisMatchedChecksumException $verify is set and checksums unequal 1708 1777 * @throws InvalidResponseException unexpected response … … 1719 1788 throw new SyntaxException("File size exceeds maximum object size."); 1720 1789 } 1721 $auto_ct = False; 1722 if (function_exists("finfo_open")) { 1723 $finfo = False; 1724 try { 1725 $finfo = @finfo_open(FILEINFO_MIME); 1726 } catch (Exception $e) { 1727 try { 1728 $my_mime = dirname(__FILE__) . "/magic"; 1729 $finfo = @finfo_open(FILEINFO_MIME, $my_mime); 1730 } catch (Exception $se) { 1731 $finfo = False; 1732 } 1733 } 1734 if ($finfo) { 1735 $ct = finfo_file($finfo, $filename); 1736 finfo_close($finfo); 1737 if ($ct) { 1738 $this->content_type = $ct; 1739 $auto_ct = True; 1740 } 1741 } 1742 } 1743 if (!$auto_ct && function_exists("mime_content_type")) { 1744 $this->content_type = @mime_content_type($filename); 1745 } else { 1746 $this->content_type = "application/octet-stream"; 1747 } 1748 //ghetto png fix 1749 $ext = strtolower(array_pop(explode('.',$filename))); 1750 if($ext == 'png') { 1751 $this->content_type = 'image/png'; 1752 } 1753 //ghetto png fix 1790 1791 $this->_guess_content_type($filename); 1792 1754 1793 $this->write($fp, $size, $verify); 1755 1794 fclose($fp); … … 1825 1864 * may either be a local filename, open resource for reading, or a string. 1826 1865 * 1827 * <b>WARNING:</b> If $data is a resource, the entire contents are read 1828 * into a local variable (memory) before computing the checksum! You 1829 * should ensure that you have enough RAM to support this. 1866 * <b>WARNING:</b> if you are uploading a big file over a stream 1867 * it could get very slow to compute the md5 you probably want to 1868 * set the $verify parameter to False in the write() method and 1869 * compute yourself the md5 before if you have it. 1830 1870 * 1831 1871 * @param filename|obj|string $data filename, open resource, or string … … 1835 1875 { 1836 1876 1837 if (is_file($data)) { 1877 if (function_exists("hash_init") && is_resource($data)) { 1878 $ctx = hash_init('md5'); 1879 while (!feof($data)) { 1880 $buffer = fgets($data, 65536); 1881 hash_update($ctx, $buffer); 1882 } 1883 $md5 = hash_final($ctx, false); 1884 rewind($data); 1885 } elseif ((string)is_file($data)) { 1838 1886 $md5 = md5_file($data); 1839 } elseif (is_resource($data)) {1840 # let's hope this isn't a BIG file1841 $contents = stream_get_contents($data); # PHP 5 and up1842 $md5 = md5($contents);1843 rewind($data);1844 1887 } else { 1845 1888 $md5 = md5($data); -
cdn-tools/trunk/cdn_classes/cloudfiles/cloudfiles_exceptions.php
r118371 r164970 26 26 class IOException extends Exception { } 27 27 class CDNNotEnabledException extends Exception { } 28 class BadContentTypeException extends Exception { } 29 class InvalidUTF8Exception extends Exception { } 28 30 29 31 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ -
cdn-tools/trunk/cdn_classes/cloudfiles/cloudfiles_http.php
r118371 r164970 30 30 require_once("cloudfiles_exceptions.php"); 31 31 32 define("PHP_CF_VERSION", "1. 3.0");32 define("PHP_CF_VERSION", "1.4.0"); 33 33 define("USER_AGENT", sprintf("PHP-CloudFiles/%s", PHP_CF_VERSION)); 34 34 define("ACCOUNT_CONTAINER_COUNT", "X-Account-Container-Count"); … … 39 39 define("CDN_URI", "X-CDN-URI"); 40 40 define("CDN_ENABLED", "X-CDN-Enabled"); 41 define("CDN_LOG_RETENTION", "X-Log-Retention"); 41 42 define("CDN_TTL", "X-TTL"); 42 43 define("CDNM_URL", "X-CDN-Management-Url"); … … 96 97 private $_cdn_uri; 97 98 private $_cdn_ttl; 99 private $_cdn_log_retention; 98 100 99 101 function __construct($api_version) … … 142 144 $this->_cdn_uri = NULL; 143 145 $this->_cdn_ttl = NULL; 146 $this->_cdn_log_retention = NULL; 147 148 # The OS list with a PHP without an updated CA File for CURL to 149 # connect to SSL Websites. It is the first 3 letters of the PHP_OS 150 # variable. 151 $OS_CAFILE_NONUPDATED=array( 152 "win", 153 ); 154 155 if (in_array((strtolower (substr(PHP_OS, 0,3))), $OS_CAFILE_NONUPDATED)) 156 $this->ssl_use_cabundle(); 157 144 158 } 145 159 … … 154 168 throw new IOException("Could not use CA bundle: " 155 169 . $this->cabundle_path); 156 }157 return;158 }159 160 private function _set_connections_cainfo()161 {162 if ($this->cabundle_path) {163 foreach ($this->connections as $k => $v) {164 if (!is_null($v)) {165 curl_setopt($this->connections[$k], CURLOPT_SSL_VERIFYPEER,166 True);167 curl_setopt($this->connections[$k], CURLOPT_CAINFO,168 $this->cabundle_path);169 }170 }171 170 } 172 171 return; … … 197 196 198 197 $curl_ch = curl_init(); 199 if ( $this->cabundle_path) {198 if (!is_null($this->cabundle_path)) { 200 199 curl_setopt($curl_ch, CURLOPT_SSL_VERIFYPEER, True); 201 curl_setopt($curl_ch, CURLOPT_CAINFO, 202 $this->cabundle_path); 200 curl_setopt($curl_ch, CURLOPT_CAINFO, $this->cabundle_path); 203 201 } 204 202 curl_setopt($curl_ch, CURLOPT_VERBOSE, $this->dbug); … … 228 226 229 227 if (!$return_code) { 230 $this->error_str = "Failed to obtain http response";228 $this->error_str .= ": Failed to obtain valid HTTP response."; 231 229 array(0,$this->error_str,array()); 232 230 } … … 250 248 # (CDN) POST /v1/Account/Container 251 249 # 252 function update_cdn_container($container_name, $ttl=86400 )250 function update_cdn_container($container_name, $ttl=86400, $cdn_log_retention=False) 253 251 { 254 252 if (!$container_name) { 255 253 throw new SyntaxException("Container name not set."); 256 254 } 255 257 256 $url_path = $this->_make_path("CDN", $container_name); 258 257 $hdrs = array( 259 258 CDN_ENABLED => "True", 260 259 CDN_TTL => $ttl, 260 CDN_LOG_RETENTION => $cdn_log_retention ? "True" : "False", 261 261 ); 262 262 $return_code = $this->_send_request("DEL_POST",$url_path,$hdrs,"POST"); … … 338 338 339 339 if (!$return_code) { 340 $this->error_str = "Failed to obtain http response";341 array(0,$this->error_str,NULL,NULL,NULL);340 $this->error_str .= ": Failed to obtain valid HTTP response."; 341 return array(0,$this->error_str,NULL,NULL,NULL,NULL); 342 342 } 343 343 if ($return_code == 401) { 344 return array($return_code,"Unauthorized",NULL,NULL,NULL );345 } 346 if ($return_code == 404) { 347 return array($return_code,"Account not found.",NULL,NULL,NULL );344 return array($return_code,"Unauthorized",NULL,NULL,NULL,NULL); 345 } 346 if ($return_code == 404) { 347 return array($return_code,"Account not found.",NULL,NULL,NULL,NULL); 348 348 } 349 349 if ($return_code == 204) { 350 350 return array($return_code,$this->response_reason, 351 $this->_cdn_enabled, $this->_cdn_uri, $this->_cdn_ttl); 352 } 353 return array($return_code,$this->response_reason,NULL,NULL,NULL); 351 $this->_cdn_enabled, $this->_cdn_uri, $this->_cdn_ttl, 352 $this->_cdn_log_retention 353 ); 354 } 355 return array($return_code,$this->response_reason,NULL,NULL,NULL,$this->_cdn_log_retention); 354 356 } 355 357 … … 377 379 378 380 if (!$return_code) { 379 $this->error_str = "Failed to obtain valid HTTP response.";381 $this->error_str .= ": Failed to obtain valid HTTP response."; 380 382 return array(0,$this->error_str,array()); 381 383 } … … 417 419 418 420 if (!$return_code) { 419 $this->error_str = "Failed to obtain valid HTTP response.";421 $this->error_str .= ": Failed to obtain valid HTTP response."; 420 422 return array(0,$this->error_str,array()); 421 423 } … … 445 447 446 448 if (!$return_code) { 447 $this->error_str = "Failed to obtain http response";449 $this->error_str .= ": Failed to obtain valid HTTP response."; 448 450 array(0,$this->error_str,0,0); 449 451 } … … 470 472 471 473 if (!$return_code) { 472 $this->error_str = "Failed to obtain http response";474 $this->error_str .= ": Failed to obtain valid HTTP response."; 473 475 return False; 474 476 } … … 488 490 489 491 if (!$return_code) { 490 $this->error_str = "Failed to obtain http response";492 $this->error_str .= ": Failed to obtain valid HTTP response."; 491 493 } 492 494 if ($return_code == 409) { … … 536 538 537 539 if (!$return_code) { 538 $this->error_str = "Failed to obtain http response";540 $this->error_str .= ": Failed to obtain valid HTTP response."; 539 541 return array(0,$this->error_str,array()); 540 542 } … … 589 591 590 592 if (!$return_code) { 591 $this->error_str = "Failed to obtain http response";593 $this->error_str .= ": Failed to obtain valid HTTP response."; 592 594 return array(0,$this->error_str,array()); 593 595 } … … 624 626 625 627 if (!$return_code) { 626 $this->error_str = "Failed to obtain http response";628 $this->error_str .= ": Failed to obtain valid HTTP response."; 627 629 array(0,$this->error_str,0,0); 628 630 } … … 653 655 654 656 if (!$return_code) { 655 $this->error_str = "Failed to obtain http response";657 $this->error_str .= ": Failed to obtain valid HTTP response."; 656 658 return array($return_code0,$this->error_str,NULL); 657 659 } … … 689 691 690 692 if (!$return_code) { 691 $this->error_str = "Failed to obtain http response";693 $this->error_str .= ": Failed to obtain valid HTTP response."; 692 694 return array($return_code,$this->error_str); 693 695 } … … 747 749 $return_code = $this->_send_request($conn_type,$url_path,$hdrs); 748 750 if (!$return_code) { 749 $this->error_str = "Failed to obtain http response";751 $this->error_str .= ": Failed to obtain valid HTTP response."; 750 752 return array(0,$this->error_str,NULL); 751 753 } … … 784 786 $return_code = $this->_send_request("DEL_POST",$url_path,$hdrs,"POST"); 785 787 if (!$return_code) { 786 $this->error_str = "Failed to obtain http response";788 $this->error_str .= ": Failed to obtain valid HTTP response."; 787 789 return 0; 788 790 } … … 811 813 812 814 if (!$return_code) { 813 $this->error_str = "Failed to obtain http response";815 $this->error_str .= ": Failed to obtain valid HTTP response."; 814 816 return array(0, $this->error_str." ".$this->response_reason, 815 817 NULL, NULL, NULL, NULL, array()); … … 845 847 $return_code = $this->_send_request("DEL_POST",$url_path,NULL,"DELETE"); 846 848 if (!$return_code) { 847 $this->error_str = "Failed to obtain http response";849 $this->error_str .= ": Failed to obtain valid HTTP response."; 848 850 return 0; 849 851 } … … 932 934 return strlen($header); 933 935 } 936 if (stripos($header, CDN_LOG_RETENTION) === 0) { 937 $this->_cdn_log_retention = 938 trim(substr($header, strlen(CDN_LOG_RETENTION)+1)) == "True" ? True : False; 939 return strlen($header); 940 } 934 941 if (stripos($header, ACCOUNT_CONTAINER_COUNT) === 0) { 935 $this->_account_container_count = trim(substr($header,942 $this->_account_container_count = (float) trim(substr($header, 936 943 strlen(ACCOUNT_CONTAINER_COUNT)+1))+0; 937 944 return strlen($header); 938 945 } 939 946 if (stripos($header, ACCOUNT_BYTES_USED) === 0) { 940 $this->_account_bytes_used = trim(substr($header,947 $this->_account_bytes_used = (float) trim(substr($header, 941 948 strlen(ACCOUNT_BYTES_USED)+1))+0; 942 949 return strlen($header); 943 950 } 944 951 if (stripos($header, CONTAINER_OBJ_COUNT) === 0) { 945 $this->_container_object_count = trim(substr($header,952 $this->_container_object_count = (float) trim(substr($header, 946 953 strlen(CONTAINER_OBJ_COUNT)+1))+0; 947 954 return strlen($header); 948 955 } 949 956 if (stripos($header, CONTAINER_BYTES_USED) === 0) { 950 $this->_container_bytes_used = trim(substr($header,957 $this->_container_bytes_used = (float) trim(substr($header, 951 958 strlen(CONTAINER_BYTES_USED)+1))+0; 952 959 return strlen($header); 953 960 } 954 961 if (stripos($header, METADATA_HEADER) === 0) { 962 # $header => X-Object-Meta-Foo: bar baz 955 963 $temp = substr($header, strlen(METADATA_HEADER)); 964 # $temp => Foo: bar baz 956 965 $parts = explode(":", $temp); 957 $this->_obj_metadata[strtolower($parts[0])] = trim($parts[1]); 966 # $parts[0] => Foo 967 $val = substr(strstr($temp, ":"), 1); 968 # $val => bar baz 969 $this->_obj_metadata[$parts[0]] = trim($val); 958 970 return strlen($header); 959 971 } 960 if (stripos($header, "ETag") === 0) { 961 $parts = explode(":", $header); 962 $this->_obj_etag = trim($parts[1]); 972 if (stripos($header, "ETag:") === 0) { 973 # $header => ETag: abc123def456... 974 $val = substr(strstr($header, ":"), 1); 975 # $val => abc123def456... 976 $this->_obj_etag = trim($val); 963 977 return strlen($header); 964 978 } 965 if (stripos($header, "Last-Modified ") === 0) {966 $ parts = explode(":", $header);967 $this->_obj_last_modified = trim($ parts[1]);979 if (stripos($header, "Last-Modified:") === 0) { 980 $val = substr(strstr($header, ":"), 1); 981 $this->_obj_last_modified = trim($val); 968 982 return strlen($header); 969 983 } 970 if (stripos($header, "Content-Type ") === 0) {971 $ parts = explode(":", $header);972 $this->_obj_content_type = trim($ parts[1]);984 if (stripos($header, "Content-Type:") === 0) { 985 $val = substr(strstr($header, ":"), 1); 986 $this->_obj_content_type = trim($val); 973 987 return strlen($header); 974 988 } 975 if (stripos($header, "Content-Length ") === 0) {976 $ parts = explode(":", $header);977 $this->_obj_content_length = trim($parts[1])+0;989 if (stripos($header, "Content-Length:") === 0) { 990 $val = substr(strstr($header, ":"), 1); 991 $this->_obj_content_length = (float) trim($val)+0; 978 992 return strlen($header); 979 993 } … … 993 1007 private function _write_cb($ch, $data) 994 1008 { 995 $ amount= strlen($data);1009 $dlen = strlen($data); 996 1010 switch ($this->_write_callback_type) { 997 1011 case "TEXT_LIST": … … 999 1013 break; 1000 1014 case "OBJECT_STREAM": 1001 $written = 0; 1002 while ($written < strlen($data)) { 1003 $written += fwrite($this->_obj_write_resource, $data-$written); 1004 } 1005 if ($written > strlen($data)) { 1006 throw new IOException( 1007 "Wrote more data to client than we should have?!"); 1008 } 1015 fwrite($this->_obj_write_resource, $data, $dlen); 1009 1016 break; 1010 1017 case "OBJECT_STRING": … … 1013 1020 } 1014 1021 if (isset($this->_user_read_progress_callback_func)) { 1015 call_user_func($this->_user_read_progress_callback_func, strlen($data));1016 } 1017 return strlen($data);1022 call_user_func($this->_user_read_progress_callback_func, $dlen); 1023 } 1024 return $dlen; 1018 1025 } 1019 1026 … … 1052 1059 $parts = explode(":", $v); 1053 1060 $header = $parts[0]; 1054 $value = trim( $parts[1]);1061 $value = trim(substr(strstr($v, ":"), 1)); 1055 1062 } else { 1056 1063 $header = $h; … … 1089 1096 } 1090 1097 1091 $this->_set_connections_cainfo();1092 1093 1098 if ($this->dbug) { curl_setopt($ch, CURLOPT_VERBOSE, 1); } 1099 1100 if (!is_null($this->cabundle_path)) { 1101 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, True); 1102 curl_setopt($ch, CURLOPT_CAINFO, $this->cabundle_path); 1103 } 1094 1104 1095 1105 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); … … 1165 1175 $hdrs = array(); 1166 1176 foreach ($obj->metadata as $k => $v) { 1167 if (strpos($k,":") !== False || strpos($v,":") !== False) {1177 if (strpos($k,":") !== False) { 1168 1178 throw new SyntaxException( 1169 "Metadata cannot contain a ':' character.");1179 "Metadata keys cannot contain a ':' character."); 1170 1180 } 1171 $k = strtolower(trim($k));1181 $k = trim($k); 1172 1182 $key = sprintf("%s%s", METADATA_HEADER, $k); 1173 1183 if (!array_key_exists($key, $hdrs)) { -
cdn-tools/trunk/cdntools.php
r161722 r164970 5 5 Description: CDN Tools is a plugin designed to help you drastically speed up your blog's load time by loading content onto a distribution network. You can use a commercial CDN or just load some of your larger JS libraries for free from Google's servers! At this time Cloud Files is the only supported CDN. 6 6 Author: Paul Kehrer 7 Version: 0.9 37 Version: 0.94 8 8 Author URI: http://langui.sh/ 9 9 */ … … 28 28 set_time_limit(300); //5 minutes max... 29 29 30 define('CDNTOOLS_VERSION','0.9 3');30 define('CDNTOOLS_VERSION','0.94'); 31 31 32 32 $dir_array = explode('/',dirname(__FILE__)); … … 121 121 122 122 function bind_filters() { 123 add_filter('init',array($this,'disable_script_concatenation')); 123 124 add_filter( 'print_scripts_array',array($this,"jquery_noconflict"),100); 124 125 add_filter('wp_handle_upload', array($this,'handle_upload_filter')); //grab data about uploads … … 127 128 add_filter('the_content', array($this,'cdn_media_url_rewrite'), 1000); //this needs to run after any other filter 128 129 add_filter('wp_generate_attachment_metadata', array($this,'cdn_upload_resized_images')); 130 } 131 132 //wp 2.8 concatenates scripts in the admin panel and this messes up the google ajax rewriting. hook init and disable it for now. revisit for 2.9 133 function disable_script_concatenation() { 134 global $concatenate_scripts; 135 $concatenate_scripts = false; 129 136 } 130 137 -
cdn-tools/trunk/readme.txt
r161722 r164970 5 5 Requires at least: 2.7 6 6 Tested up to: 2.8.4 7 Stable tag: 0.9 37 Stable tag: 0.94 8 8 9 9 CDN Tools is a plugin designed to help you drastically speed up your blog's load time by loading data onto a content distribution network (CDN). … … 11 11 == Description == 12 12 13 [CDN Tools](http:// swearingscience.com/cdn-tools/ "CDN Tools Home") is a WordPress plugin that allows you to load javascript and media files to an external server to drastically speed page loads. You can sideload data to a commercial CDN or just load your larger JS libraries (prototype, jquery) for **free** from Google's servers. CDN Tools has been designed to be as easy to use as possible, so give it a shot! Your blog's readers will thank you. At this time the only commercial CDN supported is Cloud Files. Check out the plugin homepage to view a screencast.13 [CDN Tools](http://langui.sh/cdn-tools/ "CDN Tools Home") is a WordPress plugin that allows you to load javascript and media files to an external server to drastically speed page loads. You can sideload data to a commercial CDN or just load your larger JS libraries (prototype, jquery) for **free** from Google's servers. CDN Tools has been designed to be as easy to use as possible, so give it a shot! Your blog's readers will thank you. At this time the only commercial CDN supported is Cloud Files. Check out the plugin homepage to view a screencast. 14 14 15 = New in 0.9 2/0.93=16 * A partial fix for issues with customers who have full file paths stored in their postmeta table. This is not a complete fix, but should help some (most?)users.17 * Major upgrade to support WP 2.8. If you are a previous user of CDN Tools you _MUST_ unload files and then load them again. Please let me know if you have issues because several major changes were made. See http://langui.sh/cdn-tools for more details.15 = New in 0.94 = 16 * Upgraded to CF API 1.4. This should resolve some cURL issues users have experienced and removes the dependence on a png hack for RHEL4 users. 17 * Fixed major issue with Google AJAX CDN. Previously users were unable to switch between visual and HTML mode on the edit post page. 18 18 19 [View complete changelog](http:// swearingscience.com/cdn-tools/ "CDN Tools Home").19 [View complete changelog](http://langui.sh/cdn-tools/ "CDN Tools Home"). 20 20 21 21 == Installation == … … 33 33 = Could you add XXXX CDN compatibility? = 34 34 35 I would love to, but I am not currently willing to buy accounts and pay to develop for every possible CDN out there. If you'd like to supply me with a test account so I can support your favorite CDN I'd be happy to. Drop me a comment on the CDN Tools home page.35 Unfortunately I'm likely too busy to do this for your CDN, but I'd be happy to answer questions if you need some assistance in developing your own! If you write a plugin, please let me know so I can merge it into the main distribution. 36 36 37 37 = I found a bug. What do I do? = … … 46 46 47 47 == Changelog == 48 = 0.94 - 10/19/2009 = 49 * Upgraded to CF API 1.4. This should resolve some cURL issues users have experienced and removes the dependence on a png hack for RHEL4 users. 50 * Fixed major issue with Google AJAX CDN. Previously users were unable to switch between visual and HTML mode on the edit post page. 51 48 52 = 0.93 - 10/8/2009 = 49 53 * A partial fix for issues with customers who have full file paths stored in their postmeta table. This is not a complete fix, but should help some (most?) users.
Note: See TracChangeset
for help on using the changeset viewer.