| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | $args = getopt( 'u:p:f:s:h:' ); |
|---|
| 4 | |
|---|
| 5 | $sizelimit = 2*1024*1024; // 2MB |
|---|
| 6 | |
|---|
| 7 | if( empty( $args ) ) { |
|---|
| 8 | echo<<<END |
|---|
| 9 | This script requires the following options: |
|---|
| 10 | -u: Username |
|---|
| 11 | -p: Password |
|---|
| 12 | -f: File to upload |
|---|
| 13 | -s: Size of the chunks to upload (1-$sizelimit) |
|---|
| 14 | -h: The host to upload to |
|---|
| 15 | |
|---|
| 16 | END; |
|---|
| 17 | exit; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | if ( ! file_exists( $args['f'] ) ) { |
|---|
| 21 | echo "Filename doesn't exist.\n"; |
|---|
| 22 | exit; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | $args['s'] = (int) $args['s']; |
|---|
| 26 | if( $args['s'] < 1 || $args['s'] > $sizelimit ) { |
|---|
| 27 | echo "Size must be between 1 and $sizelimit\n"; |
|---|
| 28 | exit; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | $filesize = filesize( $args['f'] ); |
|---|
| 32 | |
|---|
| 33 | $values = array( |
|---|
| 34 | 0 => 0, |
|---|
| 35 | 1 => $args['u'], |
|---|
| 36 | 2 => $args['p'], |
|---|
| 37 | 3 => array( |
|---|
| 38 | 'name' => basename( $args['f'] ), |
|---|
| 39 | 'type' => mime_content_type( $args['f'] ), |
|---|
| 40 | 'filesize' => $filesize, |
|---|
| 41 | 'resume' => true |
|---|
| 42 | ) |
|---|
| 43 | ); |
|---|
| 44 | |
|---|
| 45 | $handle = fopen( $args['f'], 'r' ); |
|---|
| 46 | |
|---|
| 47 | for( $ii = 0; $ii <= $filesize; $ii += $args['s'] ) { |
|---|
| 48 | $values[3]['bits'] = base64_encode( fread( $handle, $args['s'] ) ); |
|---|
| 49 | $values[3]['location'] = $ii; |
|---|
| 50 | |
|---|
| 51 | $xml = xmlrpc_encode_request( 'wp.uploadFile', $values ); |
|---|
| 52 | |
|---|
| 53 | echo $response = do_post_request( $args['h'], $xml ); |
|---|
| 54 | $data = xmlrpc_decode( $response ); |
|---|
| 55 | //var_dump( $data ); |
|---|
| 56 | $values['3']['path'] = $data['path']; |
|---|
| 57 | $values['3']['url'] = $data['url']; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | function do_post_request($url, $data, $optional_headers = null) |
|---|
| 61 | { |
|---|
| 62 | $params = array('http' => array( |
|---|
| 63 | 'method' => 'POST', |
|---|
| 64 | 'content' => $data |
|---|
| 65 | )); |
|---|
| 66 | if ($optional_headers !== null) { |
|---|
| 67 | $params['http']['header'] = $optional_headers; |
|---|
| 68 | } |
|---|
| 69 | $ctx = stream_context_create($params); |
|---|
| 70 | $fp = @fopen($url, 'rb', false, $ctx); |
|---|
| 71 | if (!$fp) { |
|---|
| 72 | throw new Exception("Problem with $url, $php_errormsg"); |
|---|
| 73 | } |
|---|
| 74 | $response = @stream_get_contents($fp); |
|---|
| 75 | if ($response === false) { |
|---|
| 76 | throw new Exception("Problem reading data from $url, $php_errormsg"); |
|---|
| 77 | } |
|---|
| 78 | return $response; |
|---|
| 79 | } |
|---|
| 80 | ?> |
|---|