nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Ilia Alshanetsky BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ define("GDOCS_BACKUP_DIR", "./docs/"); if (!is_dir(GDOCS_BACKUP_DIR)) { if (!mkdir(GDOCS_BACKUP_DIR, 0755)) { exit("Docs backup directory '".GDOCS_BACKUP_DIR."' does not exist and could not be created\n"); } } else if (!is_writable(GDOCS_BACKUP_DIR)) { exit("The backup dir '".GDOCS_BACKUP_DIR."' is not writable\n"); } $curl = curl_init(); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); function get_auth_token($curl, $type = "writely") { // Construct an HTTP POST request $auth = array( "accountType" => 'HOSTED_OR_GOOGLE', "Email" => 'your user name', "Passwd" => 'your password', "service" => $type, "source" => 'Document Backup App' ); curl_setopt($curl, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin"); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $auth); $response = curl_exec($curl); // Get the Auth string and save it if (preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches)) { return $matches[1]; } else { exit("No token\n"); } } $auth = get_auth_token($curl); $headers_docs = array( "Authorization: GoogleLogin auth=" . $auth, "GData-Version: 3.0", ); $auth = get_auth_token($curl, "wise"); $headers_xls = array( "Authorization: GoogleLogin auth=" . $auth, "GData-Version: 3.0", ); curl_setopt($curl, CURLOPT_URL, "https://docs.google.com/feeds/default/private/full"); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers_docs); curl_setopt($curl, CURLOPT_POST, false); $data = curl_exec($curl); if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) { $response = simplexml_load_string($data); } else { exit("ERROR: could not obtain document list\n"); } $backup_date = date("Ymd"); $start_date = time() - 86400; // Data itteration foreach($response->entry as $file) { if (!$file->updated) { $tm = strtotime($file->published); } else { $tm = strtotime($file->updated); } // file already backed up, we only backup files updated/created in the last 24 hours if ($start_date > $tm) { continue; } // establish output content type $host = strtok(parse_url($file->content['src'], PHP_URL_HOST), '.'); switch ($host) { case 'docs': if (strpos($file->content['src'], '/presentations/') !== false) { $format = 'ppt'; } else { $format = 'doc'; } break; case 'spreadsheets': $format = 'xls'; break; case 'presentations': case 'present': $format = 'ppt'; break; default: $format = 'pdf'; // safe default, since Google can export anything to PDF break; } // for Excel files we need to use a different authentication token if ($format != 'xls') { curl_setopt($curl, CURLOPT_HTTPHEADER, $headers_docs); } else { curl_setopt($curl, CURLOPT_HTTPHEADER, $headers_xls); } curl_setopt($curl, CURLOPT_URL, $file->content['src'] . '&exportFormat=' . $format); $data = curl_exec($curl); if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) { file_put_contents(GDOCS_BACKUP_DIR . $backup_date . '_' . strtr($file->title, ' ', '_'), $data); } else { echo "ERROR: could not retrieve " . $file->title . "\n"; } } curl_close($curl);