php/googleClientLib/cloudTst.php

<?php
require_once 'env.php';
class MyXml extends XMLReader /* implements Stringable */ {
    public function __toString() {
        return '{' . __CLASS__ . "->$this->nodeType: $this->name}";
    }

   function walk($pp) {
        $nm = $this->name;
        $pn = "$pp/$this->name";
        if ($this->isEmptyElement) {
            out("Emy $pn");
            return $this->read();
        }
        $txt = '';
        out("beg $pn ($this)");
        for($i=0; $this->moveToNextAttribute();$i++)
            out("  att $i: $this->name => $this->value");
        $this->read();
        while (self::END_ELEMENT !== $ty = $this->nodeType) {
            # out("     ty $this->nodeType name $this->name");
            if ($ty === self::TEXT) {
                $txt .= $this->value;
                $this->read();
            } elseif ($ty === self::ELEMENT) {
                $this->walk($pn);
            } else {
                err("unsported xml node", $this);
            }
        }
        out("end $pn, txt=$txt");
        if ($nm === $this->name)
            return $this->read();
        err("end $pn expected but $this");
    }

  function walkMS($pp) {
        $nm = $this->name;
        $pn = "$pp/$this->name";
        if ($this->isEmptyElement) {
            out("Emy $pn");
            return $this->read();
        }
        $txt = '';
        out("beg $pn");
        for($i=0; $this->moveToNextAttribute();$i++)
            out("  att $i: $this->name => $this->value");
        $this->read();
        while (self::END_ELEMENT !== $ty = $this->nodeType) {
            # out("     ty $this->nodeType name $this->name");
            if ($ty === self::TEXT) {
                $txt .= $this->value;
                $this->read();
            } elseif ($ty === self::ELEMENT) {
                if ($this->name === 'd:propstat') 
                    $this->propStat($pn, isset($gotPst) ? ++$gotPst : $gotPst = 0);
                else 
                    $this->walkMS($pn);
            } else {
                err("unsported xml node", $this);
            }
        }
        out("end $pn, txt=$txt");
        if ($nm === $this->name)
            return $this->read();
        err("end $pn expected but $this");
    }

    function propStat($pa, $num) {
        $nm = $this->name;
        $pn = "$pa/$this->name";
        out("ps0 $this");
        $this->read();
        out("ps1 $this");
        if ($num) { # skip all d:prop nodes
            while ($this->nodeType === self::ELEMENT and $this->name === 'd:prop') 
                $this->next();
        } else { # process all d:prop nodes
            while (self::ELEMENT === $ty = $this->nodeType and $this->name === 'd:prop')
                $this->walkMS($pn);
        }
        out("ps1 $this");
        if ($this->nodeType === self::ELEMENT and $this->name === 'd:status') {
            $sta = ($this->read() and $this->nodeType === self::TEXT) ? $this->value : err("text expected $this->nodeType $this->name");
            ($this->read() and $this->nodeType === self::END_ELEMENT and $this->name === 'd:status') or err("end $pn expected but $this");
            $this->read();
            $num === 0 and $sta === 'HTTP/1.1 200 OK' or $num === 1 and $sta === 'HTTP/1.1 404 Not Found' or err("$pn num $num bad status $sta");
            out("end $pn num $num status ok $sta");
        } else {
            err("status missing $this");
        }
        if ($this->nodeType === self::END_ELEMENT and $nm === $this->name)
            return $this->read();
        err("end $pn expected but $this");
    }

   public function nodeText($pp) {
        $nm = $this->name;
        $pn = "$pp/$this->name";
        $txt = '';
        while ($this->read() and $this->nodeType === self::TEXT)
            $txt .= $this->value;
        ($this->nodeType === self::END_ELEMENT and $this->name === $nm) or err("end $pn expected but $this");
        $this->read();
        return $txt;
    }    
     
   public function eResponse($pp, $mm) {
        $this->propAA = [];
        $this->propC = -1;
        $this->e2mWalk($pp);
        out("end $pp, cPS $this->propC", $this->propAA);
        unset($this->aa, $this->propC);
    }

    public function eProp($pp, $mm) {
        $pn = "$pp/$this->name";
        $c = ++$this->propC;
        out("beg $pn eProp #$c $this");
        if ($this->propC)
            $this->next();
        else
            $this->e2mWalk($pp);
        out("eProp1 $this");
        ($this->nodeType === self::ELEMENT and $this->name === 'd:status') or err("d:status after $pn expected, but $this");
        $sta = $this->nodeText($pn);
        $c === 0 and $sta === 'HTTP/1.1 200 OK' or $c === 1 and $sta === 'HTTP/1.1 404 Not Found' or err("after $pn #$c bad status $sta");
        out("end $pn #$c status ok $sta");
    }

    public function eText($pp, $mm) { $this->propAA[$mm[1]] = $this->nodeText("$pp/$this->name"); }

    public function eLastMod($pp, $mm) { $this->propAA[$mm[1]] = toLocalTst($this->nodeText("$pp/$this->name")); }

    function e2mWalk($pp) {
        $nm = $this->name;
        $pn = "$pp/$this->name";
        if ($this->isEmptyElement) {
            out("Emy $pn");
            return $this->read();
        }
        $txt = '';
        out("beg $pn");
        for($i=0; $this->moveToNextAttribute();$i++)
            out("  att $i: $this->name => $this->value");
        $this->read();
        while (self::END_ELEMENT !== $ty = $this->nodeType) {
            # out("     ty $this->nodeType name $this->name");
            if ($ty === self::TEXT) {
                $txt .= $this->value;
                $this->read();
            } elseif ($ty === self::ELEMENT) {
                $mm = $this->e2m[$this->name] ?? null;
                $this->{$mm[2] ?? 'e2mWalk'}($pn, $mm);
            } else {
                err("unsported xml node", $this);
            }
        }
        out("end $pn, txt=$txt");
        if ($nm === $this->name)
            return $this->read();
        err("end $pn expected but $this");
    }

}  # end class MyXml 
require_once 'cloud.php';

$x = new MyXml();
$x->open('o.xml');
$x->read();
#$x->walkMS('');
$x->e2m =   ['d:response' => ['d:response', '', 'eResponse']
            , 'd:prop' => ['d:prop', '', 'eProp']
            , 'd:href' => ['d:href', 'path', 'eText']
            , 'd:displayname' => ['d:displayname', 'name', 'eText']
            , 'd:getlastmodified' => ['d:getlastmodified', 'lastMod', 'eLastMod']
            ];
$x->e2mWalk('');
exit(outEnd());
/*
require_once 'cloud.php';
$f = new CloudFactory;
$cc = $f->make('wlklNC');
out($cc->ff2xml('nf'));
#out('get', $cc->getHTTP('1OI9ebyLj9r6LsfFlEq9x8JdFWfEgT2Jq6ofUI4IW1PQ'));
exit(outEnd());
    $cc->ff('', fn ($n) => outTRD(...$n)
            , fn($m) => [out(']]tbh meta'), outTb(), outTRH(...$m[0]), outTRD(...$m[1]), outTbEnd(), out(']]tbh ffid'), outTb(), outTRH(...$m[2])]);
    outTbEnd();
    out("$cc->ffFo folders, $cc->ffFi files, " . sprintf('%7.2e', $cc->ffSz) . ' Bytes (file) at ' . toLocalTst('now'));
    $e = fopen('php://temp', 'w+');
    $o = fn ($n) => fputcsv($e, $n);
    $cc->ff('', $o
            , fn($m) => [$o([']]tbh meta']), $o($m[0]), $o($m[1]), $o([']]tbh ffid']), $o($m[2])]);
    out("$one $cc->ffFo folders, $cc->ffFi files, " . sprintf('%7.2e', $cc->ffSz) . ' Bytes (file) at ' . toLocalTst('now'));
    $sz = ftell($e);
    rewind($e);
    out("$one writing $sz bytes to $one:ffid-$one.csv, at " . toLocalTst('now'));
    $cc->upload("ffid-$one.csv" ,'text/csv', $sz < 1 ? '' : fread($e, $sz));
    fclose($e);

outEnd();
exit();

if (0) {
    $cc->ff('', fn ($n) => outTRD(...$n)
            , fn($m) => [out(']]tbh meta'), outTb(), outTRH(...$m[0]), outTRD(...$m[1]), outTbEnd(), out(']]tbh ffid'), outTb(), outTRH(...$m[2])]);
    outTbEnd();
    out("$cc->ffFo folders, $cc->ffFi files, " . sprintf('%7.2e', $cc->ffSz) . ' Bytes (file) at ' . toLocalTst('now'));
} else {
    # $cc->create('1Ff0f9XZx_K9o20hk0DqBnP-kENaspLbY', 'testCreate.txt', 'text/plain', 'text in testCreate.txt');
    # $cc->uploadVersion('1XkaWCp8lz5PrCx_MNd5Gby68Hrl5Wjmi', 'text in testCreate.txt from uploadVersion' );
    $cc->upload('testUploa2.txt' ,'text/plain', 'text in testUpload.txt from upload at ' . toLocalTst('now') ."\n und so oder");
}
exit();
*/

/*    $nc->rename($nc->ff(), 'fn2valid', function($nx, $n, $fP, $fM, $tP) use($nc) {
            out("$nc->cloudId, $n[0], fP", $fP, ", tP", $tP, ", fM", $fM );
            echo "$nx path $n[0] invalid, renaming " . implode('/', $fM) . " ==> " . implode('/', $tP) . "\n";
            $nc->mv($fM, $tP);
        }); */
exit();
require_once 'google-api-small/vendor/autoload.php';
# require '/wkData/install/google-api-services/autoload.php';
// include_once __DIR__ . '/google-api/templates/base.php';

# echo "rrurl '" . RRURL . "' $_SERVER[REQUEST_SCHEME]<br>";

# echo "start " . __FILE__ . "\n";



const SCOPES = 
            [ 'https://www.googleapis.com/auth/drive.metadata.readonly' # drive readonly
            , 'https://www.googleapis.com/auth/drive'                   # drive file update
            , 'https://www.googleapis.com/auth/documents.readonly'      # docs readonly
            #, 'https://www.googleapis.com/auth/documents'               # docs readWrite
            ];
define('KYPR', str_starts_with($rp = realpath(__FILE__), '/wkData/www/') ? '/wkData/wk/extr2/'  # path prefix for locallly stored keys
             : (str_starts_with($rp, '/home/ch45859/web/') ? '/home/ch45859/web/files/extr2/' : err("realpath(__FILE__) $rp not supported")));
       # , RRURL = 'http://localhost/home/inf/php/e05GetServer.php' 
const RRCPA = '/wkData/pub/oauth2.log'; # redirect URL writes code to this path
;

if (php_sapi_name() === 'cli') {
    $tokenH = 'goRorAuthTokenFile';
    $codeH  = 'goRorAuthCodeCli';
    define('RRURL', 'https://localhost/home/inf/php/goauth2callback.php'); # redirect URL, authorization server sends token to this URL
} else {
    $tokenH = 'goRorAuthTokenSess';
    $codeH  = 'goRorAuthCodeWeb';
    define('RRURL', "$_SERVER[REQUEST_SCHEME]://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]") ; # redirect URL, authorization server sends token to this URL
    goAuthBeginWeb();
}
$c = goClient('QuW', 'Wlklxy', $tokenH, $codeH);
$drv = new Google\Service\Drive($c);
if (isset($_GET['csv'])) {
    header('content-type: text/csv'); 
    $out = fopen('php://temp', 'w+');
    $r = goDrList($drv, 'root', '', fn (...$a) => fputcsv($out, $a));
    rewind($out);
    fpassthru($out);
    fclose($out);
} else {
    outBegin(__FILE__);
    outTb();
    $r = goDrList($drv, 'root', '', 'outTRD', 'outTRH');
    outTbEnd();
    outEnd(__file__);
}