// Add a "Log out of Google Drive" button.
        $C .= '<a class="btn" title="Disconnect your Pika CMS account from Google Drive." href="' . $base_url . '/pm.php/google_drive_connector/auth.php?action=unauthorize&username='******'username']);
        $C .= '" target="_blank">Disconnect Drive</a>';
        /* TODO:  Use this button once Pika CMS is using a version of Bootstrap 
        		that includes Font Awesome <i class="icon-signout"></i>
        		*/
        $C .= '
		</div>
	</div>
	</form>';
    } else {
        $clean_username = htmlspecialchars($auth_row['username']);
        $C .= '<p>To upload and view case-related files, you need to connect your Pika CMS account to your Google account. To do so, <a class=""';
        $C .= ' onClick=\'window.open("' . $base_url . '/pm.php/google_drive_connector/auth.php?action=authenticate", "Request for Authorization", "width=450, height=500, scrollbars=yes");\'';
        $C .= '><strong>click here to log into Google Drive</strong></a>.</p>';
        $C .= '<p><em>Please reload or refresh this page once you have logged in</em>.</p>';
    }
    // End Drive Upload Form
    require_once getcwd() . '-custom/extensions/google_drive_connector/file_list.php';
    $a['file_list'] = file_list($case1->google_drive_folder_id, null, $case1->google_drive_folder_id);
    $template = new pikaTempLib('subtemplates/case-drive.html', $a);
    $C .= $template->draw();
    // Handle Drive uploads
    // TODO Move this elsewhere
    if (array_key_exists('file_upload', $_FILES)) {
        $rest = new PikaDrive($auth_row['username']);
        $y = $rest->uploadFile($_FILES['file_upload']['tmp_name'], $_FILES['file_upload']['name'], htmlspecialchars($_POST['folder_id']));
        // $_POST['title'], $_POST['folder_id'])
    }
}
                 echo "authorized";
             }
         } else {
             switch ($api_request[1]) {
                 case 'auth':
                     if (isset($_GET['code'])) {
                         $rest->setToken($_SESSION['username'], $_GET['code']);
                         unset($_SESSION['username']);
                         echo "<script>window.close();</script>";
                     } else {
                         $_SESSION['username'] = get_value('username');
                         $rest->authenticate();
                     }
                     break;
                 case 'upload':
                     echo json_encode($rest->uploadFile($_FILES['upfile']['tmp_name'], $_POST['file_name'], $_POST['folder_id']));
                     break;
                 case 'new_folder':
                     echo json_encode($rest->createFolder($_POST['folder_name']));
                     break;
                 case 'unauthorize':
                     $rest->unauthorize(htmlspecialchars($_GET['username']));
                     break;
                 default:
                     break;
             }
         }
     }
 } else {
     http_response_code(400);
     // BAD REQUEST
    $result = mysql_query($sql);
    if (mysql_num_rows($result) == 0) {
        echo "No more documents found in {$plSettings['db_name']}.\n";
        break;
    }
    $row = mysql_fetch_assoc($result);
    if ($row['folder_ptr'] > 0) {
        $sql0 = "SELECT google_drive_path FROM doc_storage WHERE doc_id = '{$row['folder_ptr']}'";
        $result0 = mysql_query($sql0) or print_diagnostic($row);
        $row0 = mysql_fetch_assoc($result0);
        $x = $row0['google_drive_path'];
    } else {
        $x = $row['google_drive_folder_id'];
    }
    file_put_contents("/tmp/{$row['doc_name']}", gzuncompress(stripslashes($row['doc_data'])));
    $upload_results = $drive->uploadFile("/tmp/{$row['doc_name']}", $row['doc_name'], $x);
    unlink("/tmp/{$row['doc_name']}");
    if (array_key_exists('kind', $upload_results) && $upload_results['kind'] == 'drive#file') {
        $sql1 = "DELETE FROM doc_storage WHERE doc_id = '{$row['doc_id']}'";
        $result1 = mysql_query($sql1) or die('DELETE Query failed: ' . mysql_error());
        echo "Moved " . $row['doc_id'] . " to Google Drive.\n";
    } else {
        echo "Upload of doc_id {$row['doc_id']} failed.\n";
        print_r($upload_results);
        exit;
    }
}
$sql = "SELECT COUNT(*) AS a FROM doc_storage WHERE doc_type='C' AND case_id IS NOT NULL";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row['a'] == 0) {