Plugin Directory

Changeset 549264


Ignore:
Timestamp:
05/26/2012 11:06:33 AM (13 years ago)
Author:
hel.io
Message:

Resumable upload media link now requested from Google

Location:
backup/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • backup/trunk/backup.php

    r548407 r549264  
    22/*
    33Plugin Name: Backup
    4 Version: 1.1.4
     4Version: 1.1.5
    55Plugin URI: http://hel.io/wordpress/backup/
    66Description: Backup your WordPress website to Google Drive.
     
    2727        }
    2828
    29         // check allow_url_fopen
     29        // check zip extension
    3030        if(!extension_loaded('zip')) {
    3131            throw new Exception(__('Please load the \'zip\' PHP extension. It is needed in order to create the archive to be backed up.', 'backup'));
     
    157157                        <div class="misc-pub-section"><?php _e( 'Most recent backup:', 'backup' ); ?><br/><?php
    158158                            if ( $options['last_backup'] ) {
    159                                 echo '<strong>' . date( __( "M j, Y \a\\t H:i:s", 'backup' ), $options['last_backup'] ) . '</strong><br/><br/>';
     159                                echo '<strong>' . date( __( "M j, Y \a\\t H:i:s", 'backup' ), $options['last_backup'] ) . '</strong>';
    160160                                if ( $options['local_number'] > 0 ) {
    161                                     ?><a class="button-secondary" title="<?php _e( 'Download most recent backup', 'backup'); ?>" href="<?php echo WP_CONTENT_URL . substr( end( $options['local_files'] ), strlen( WP_CONTENT_DIR ) ); ?>">Download backup</a><?php
     161                                    ?><br/><br/><a class="button-secondary" title="<?php _e( 'Download most recent backup', 'backup'); ?>" href="<?php echo WP_CONTENT_URL . substr( end( $options['local_files'] ), strlen( WP_CONTENT_DIR ) ); ?>">Download backup</a><?php
    162162                                }
    163163                            }
     
    320320        $options['last_backup'] = $backup_time;
    321321        $options['local_files'][] = $file_path;
    322         if ( $options['token'] && $options['client_id'] && $options['client_secret'] ) {
     322        if ( $options['drive_number'] > 0 && $options['token'] && $options['client_id'] && $options['client_secret'] ) {
    323323            if ( $access = access_token( $options['token'], $options['client_id'], $options['client_secret'] ) ) {
    324324                backup_log( 'NOTICE', 'Attempting to upload archive to Google Drive' );
     
    329329                    backup_log( 'NOTICE', 'Archive ' . $file_name . ' uploaded to Google Drive in ' . ( microtime( true ) - $timer_start ) . ' seconds' );
    330330                    $options['drive_files'][] = $id;
    331                     while ( count( $options['drive_files'] ) > $options['drive_number'] )
    332                         if ( delete_file( $r = array_shift( $options['drive_files'] ), $access ) )
    333                             backup_log( 'NOTICE', 'Deleted Google Drive file ' . $r );
    334                         else   
    335                             backup_log( 'WARNING', 'Could not delete Google Drive file ' . $r );
    336331                }
    337332            }
     
    340335            }   
    341336        }
     337       
     338        // Delete excess Drive files
     339        while ( count( $options['drive_files'] ) > $options['drive_number'] )
     340            if ( delete_file( $r = array_shift( $options['drive_files'] ), $access ) )
     341                backup_log( 'NOTICE', 'Deleted Google Drive file ' . $r );
     342            else   
     343                backup_log( 'WARNING', 'Could not delete Google Drive file ' . $r );
     344       
     345        // Delete excess local files   
    342346        while ( count( $options['local_files'] ) > $options['local_number'] )
    343347            if ( unlink( $f = array_shift( $options['local_files'] ) ) )
     
    345349            else   
    346350                backup_log( 'WARNING', 'Could not delete file ' . $f );
     351       
    347352        update_option( 'backup_options', $options );
    348353    }
     
    386391    );
    387392
    388     $url = 'https://docs.google.com/feeds/upload/create-session/default/private/full';
    389     if ( $parent )
    390         $url .= '/folder%3A' . $parent . '/contents';
    391     $url .= '?convert=false'; // needed to upload a file
     393    $url = get_resumable_create_media_link( $token, $parent );
     394    if ( $url )
     395        $url .= '?convert=false'; // needed to upload a file
     396    else {
     397        backup_log( 'ERROR', 'Could not retrieve resumable create media link.', __FILE__, __LINE__ );
     398        return false;
     399    }
    392400   
    393401    $result = @file_get_contents( $url, false, stream_context_create( $context ) );
     
    420428        }
    421429        else {
    422             backup_log( 'ERROR', 'Bad response: ' . $response . '. Request header: ' . join( "; ", $header ), __FILE__, __LINE__ );
     430            backup_log( 'ERROR', 'Bad response: ' . $response . ' Response header: ' . var_export( $response_header, true ) . ' Response body: ' . $result . ' Request URL: ' . $url, __FILE__, __LINE__ );
    423431            return false;
    424432        }
     
    512520    if ( strpos( $headers[0], '200' ) )
    513521        return true;
     522    return false;
     523}
     524/**
     525 * Get the resumable-create-media link needed to upload files
     526 *
     527 * @param  string $token  The Google Account access token
     528 * @param  string $parent The Id of the folder where the upload is to be made. Default is empty string.
     529 * @return string|boolean Returns a link on success, FALSE on failure.
     530 */
     531function get_resumable_create_media_link( $token, $parent = '' ) {
     532    $header = array(
     533        'Authorization: Bearer ' . $token,
     534        'GData-Version: 3.0'
     535    );
     536    $context = array(
     537        'http' => array(
     538            'method' => 'GET',
     539            'header' => join( "\r\n", $header )
     540        )
     541    );
     542    $url = 'https://docs.google.com/feeds/default/private/full';
     543
     544    if ( $parent )
     545        $url .= '/' . $parent;
     546
     547    $result = @file_get_contents( $url, false, stream_context_create( $context ) );
     548
     549    if ( $result !== false ) {
     550        $xml = new SimpleXMLElement( $result );
     551        error_log( $result );
     552        foreach ( $xml->link as $link )
     553            if ( $link['rel'] == 'http://schemas.google.com/g/2005#resumable-create-media' )
     554                return $link['href'];
     555    }
    514556    return false;
    515557}
  • backup/trunk/readme.txt

    r547983 r549264  
    55Requires at least: 3.0
    66Tested up to: 3.3.2
    7 Stable tag: 1.1.3
     7Stable tag: 1.1.5
    88
    99Make backups of your Wordpress site to Google Drive.
     
    2626
    2727== Changelog ==
     28= 1.1.5 =
     29* You can now chose not to upload backups to Google Drive by entering `0` in the appropriate field on the settings page.
     30* Resumable create media link is no longer hard coded.
     31
    2832= 1.1.3 =
    2933* Fixed some issues created by the `1.1.2` update
Note: See TracChangeset for help on using the changeset viewer.