$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>';
} else {
    if (strlen($case1->google_drive_folder_id) == 0) {
        // Reminder: It's not possible to search Google Drive if the current Pika CMS user
        // account doesn't have a token.
        // Examples:  '98765' becomes '765', '12' becomes '012'.
        $case_sub_folder_name = str_pad(substr($case1->case_id, -3), 3, '0', STR_PAD_LEFT);
        // Look for the sub_folder where this case's folder will go.  If it doesn't
        // exist, create it.
        $case_sub_folder_id = folder_search_or_create($case_sub_folder_name, UNIQUE_FOLDER_ID);
        // Now that the sub_folder_id has either been found or created, we can find
        // (if the folder has been orphaned somehow) or create the case's folder
        // inside the case_sub_folder (which is inside the root folder).
        $case1->google_drive_folder_id = folder_search_or_create($case1->case_id, $case_sub_folder_id);
        $case1->save();
    }
    $a = array('google_drive_folder_id' => $case1->google_drive_folder_id);
    // End Drive Section
    // Drive Upload Form
    // Only display this if the user has logged in using the API /drive/auth URL.
    // I don't have permission to edit pikaDrive on dev server at the moment, this
    // is a work around.
    //if (pikaDrive::isAuthenticated($auth_row["username"]))
    if (file_exists(getcwd() . "-custom/extensions/google_drive_connector/tokens/{$auth_row['username']}")) {
        $C .= '
	<form method="post" enctype="multipart/form-data" action="">
	<div class="row file-uploads">
		<div class="span5">
		<input class="choose-file" type="file" name="file_upload">
*/
/* create drive folder for all case_id's in SELECT case_id FROM doc_storage GROUP by case_id;
Save the unique_id
*/
$sql = "SELECT case_id FROM doc_storage LEFT JOIN cases USING(case_id) " . "WHERE case_id IS NOT NULL AND google_drive_folder_id IS NULL GROUP BY case_id";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    echo "Starting folder for case_id {$row['case_id']}... ";
    $case_sub_folder_name = str_pad(substr($row['case_id'], -3), 3, '0', STR_PAD_LEFT);
    // Look for the sub_folder where this case's folder will go.  If it doesn't
    // exist, create it.
    $case_sub_folder_id = folder_search_or_create($case_sub_folder_name, UNIQUE_FOLDER_ID, $pika_cms_username);
    // Now that the sub_folder_id has either been found or created, we can find
    // (if the folder has been orphaned somehow) or create the case's folder
    // inside the case_sub_folder (which is inside the root folder).
    $case_folder_id = folder_search_or_create($row['case_id'], $case_sub_folder_id, $pika_cms_username);
    mysql_query("UPDATE cases SET google_drive_folder_id = '{$case_folder_id}' WHERE case_id = '{$row['case_id']}'");
    echo "Created folder for case_id {$row['case_id']}\n";
}
/*  create drive folder for all subfolders
*/
$sql = "SELECT doc_id, doc_name, google_drive_folder_id, folder_ptr FROM doc_storage " . "LEFT JOIN cases USING(case_id) " . "WHERE doc_storage.case_id IS NOT NULL AND google_drive_path IS NULL AND doc_type='C' AND folder = '1'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    echo "Starting subfolder id {$row['doc_id']}... ";
    if ($row['folder_ptr'] > 0) {
        $sql0 = "SELECT google_drive_path FROM doc_storage WHERE doc_id = '{$row['folder_ptr']}'";
        $result0 = mysql_query($sql0);
        $row0 = mysql_fetch_assoc($result0);
        $x = $row0['google_drive_path'];
    } else {