php/cloudTst.php

<?php
require_once 'cloud.php';
require_once 'net.php';
$dbg=0;
$a2strLevel=2;
$f = new CloudFactory;

if (0) {
    out('argv', $argv, '; envArgs', envArgs());
    tstArr();
} elseif (0) {
    $n = fopen('/wkData/tmp/tst1.csv', 'r'); fgets($n); fgets($n);
    $o= fopen('/wkData/tmp/ffid-wlklGo.csv', 'r');
    for ($i=0; $i<10 and ($q = fgets($n)) and ($r = fgets($o)); $i++){
        for ($x=0; $x < min(strlen($q), strlen($r)) and $q[$x] === $r[$x] ; $x++ ) ;
        out("$i n:", strlen($q), $q===$r, $x, $q);
        out("$i o:", strlen($r), $q===$r, $x, $r);
    }
} elseif (0) {
    $cc = $f->make('wlHoNC');
    $ff = $cc->ff($ppl = $cc->pipeline('%,metaRowCsv,PHST RECUR'));
    out("  ff $cc->clId", ($ppl->ppc->rowWrite2 ?? $ppl->ppc->rowWriter)->writeC, "rows,", ftell($ff), "bytes", toLocalTst('now'));
    out('  creUpd', $cc->creUpd('%all,row1,PHST KRF WRIPA', "ffid-$cc->clId.csv", 'text/csv', fRewContClo($ff)), "{$cc->fun}d", toLocalTst('now'));
#   out("copy", $cc->copy(Net::CREPA | Net::OVERWR, 'test/tst.txt', 'test33/tstCopy.txt'));
#    out('capa', $cc->req(',string', 'ocs/v1.php/cloud/capabilities', hC: "OCS-APIRequest: true"));
#    out('ff =>', xmlPP($cc->ff('%,string', 'ab0'))); # bug report
#    out('ff =>', xmlPP($cc->ff('%,rowTb', path: "ffid-$cc->clId.csv"))); 
    # out('versions =>', $cc->versions('%vrs,RowTb', id: 1352)); #rowOut new Req2Str);
    # out('ff 12 =>', xmlPP($cc->ff('%vrs,string', id: 2124))); #https://cloud.wlkl.ch/index.php/f/12
    #out('creUpd test/tst.txt', $cc->creUpd('%all,rowTb', 'test/tst.txt', 'text/plain', "test/tst.txt creating\ngeht das um" . toLocaltst('now') 
    #            . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt created.", doBits: Cloud::RECUR, kRF: true)); #, modTst: '2024-04-01T06:17:59'));
} elseif (0) {
    $cc = $f->make('wlklNC');
    out('csv tst1.csv', strlen($cont=rr($cc->ff('name id type path modTst owner size,metaRowCsv'))));
    out('creUpd tst1.csv', $cc->creUpd('%all,row1,PHST KRF WRIPA', 'tst1.csv', 'text/csv', $cont));
    out('versions =>', $cc->versions('%vrs,RowTb', id: 2299)); #rowOut new Req2Str);
exit();
    out('propPatch do/tstNNa.txt', $cc->propPatch('do/tstNNa.txt', favorite: 1, creTst: '1981-04-01T06:17:59', type: 'text/csv'));
    out('ff 12 =>', $cc->ff('%all,metaRowTb', path: 'do/tstNNa.txt')); #https://cloud.wlkl.ch/index.php/f/12
    out('ff do =>', rr($cc->ff('%allAll,metaRowCsv', path: 'do'))); #https://cloud.wlkl.ch/index.php/f/12
    out('versions =>', $cc->versions('%vrs,RowTb', id: 1352)); #rowOut new Req2Str);
  #  out('creUpd do/tst.txt', $cc->creUpd('%vrs,rowTb', 'do/tstNNa.txt', 'text/plain', "do/tstNNa.txt creating\ngeht das um" . toLocaltst('now') 
  #              . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt created.", doBits: Cloud::RECUR, kRF: true, modTst: '2024-05-02T06:17:59'));
    out('propPatch do/tstNNa.txt', $cc->propPatch('do/tstNNa.txt', 0));
   out('creUpd do/tst.txt', $cc->creUpd('%,rowTb', 'do/tst.txt', 'text/plain', "do/tst.txt creating\ngeht das um" . toLocaltst('now') 
                . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt created.", doBits: Cloud::RECUR, kRF: true)); #, modTst: '2024-04-01T06:17:59'));
    out('versions =>', $cc->versions('%vrs,RowTb', id: 2124)); #rowOut new Req2Str);
    out('versionPatch do/tst.txt', $cc->versionPatch(2124, '2024-12-12t22:04:19', 'lbl2024-12-12t22:04:19xy'));
    out('ff 12 =>', $cc->ff('%vrs,metaRowTb', id: 2124)); #https://cloud.wlkl.ch/index.php/f/12
    out('propPatch do/tst.txt', $cc->propPatch('do/tst.txt', 0));
    out('creUpd do/tst.txt', $cc->creUpd('%,rowTb', 'do/tst.txt', 'text/plain', "do/tst.txt creating\ngeht das um" . toLocaltst('now') 
                . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt created.", doBits: Cloud::RECUR)); #, modTst: '2024-04-01T06:17:59'));
 #   out('propPatch do/tst.txt', $cc->propPatch('%,string', 'do/tst.txt', modTst: '2024-04-01T06:17:59'));
    out('creUpd do/tst.txt', $cc->create('%,rowTb', 'do/tst.txt', '%dir')); #, modTst: '2024-04-01T06:17:59'));
    out('propPatch do/tst.txt', $cc->propPatch('%,string', 'do/tst.txt', modTst: '2024-04-01T06:17:59'));
    out('ff 12 =>', $cc->ff('%vrs,metaRowTb', id: 2124)); #https://cloud.wlkl.ch/index.php/f/12
    out('creUpd do/tst.txt', $cc->creUpd('%,rowTb', 'do/tst.txt', 'text/plain', "do/tst.txt creating\ngeht das um" . toLocaltst('now') 
                . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt created.", doBits: Cloud::RECUR)); #, modTst: '2024-04-01T06:17:59'));
  #  out('ff 12 =>', $cc->ff('%vrs,metaRowTb', id: 1693)); #https://cloud.wlkl.ch/index.php/f/12
    out('versions nf =>', $cc->versions('%vrs,RowTb', id: 2124)); #rowOut new Req2Str);
#   out('creUpd do/dirB/a/b/c %dir', $cc->creUpd('%,rowTb', 'do/dirB/a/b/c', '%dir', doBits: Cloud::RECUR));
    out('creUpd do/dirB/a/b/c/tst.txt', $cc->creUpd('%,rowTb', 'do/dirB/a/b/c/tst.txt', 'text/plain', "tst.txt creating\ngeht das um" . toLocaltst('now') 
                . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt created.", doBits: Cloud::RECUR)); #, modTst: '2024-04-01T06:17:59'));
    out('update do/dirB/a/b/c/tst.txt', $cc->update('%,rowTb', 'do/dirB/a/b/c/tst.txt', 'text/plain', "tst.txt creating\ngeht das um" . toLocaltst('now') 
                . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt created.", doBits: Cloud::RECUR)); #, modTst: '2024-04-01T06:17:59'));
    err("get do/tstA", $cc->get('%,row1', id: 1718));
    err("get do/tstA", $cc->get('%,row1', path: 'do/tstA.txt'));
    out("create", $cc->create("id type path,rowOut", 'do/dirC'));
    out("get 1284", $cc->get('%,row1', id: 1284));
    out("get -123 gibt es nicht)", $cc->get('%,rows', id: 123456));
    out("get 2409mettmen", $cc->get('%,row1', path: 'nf/2409mettmen'));
    out("get gibt/es/nicht)", $cc->get('%,row1', path: 'gibt/es/nicht'));
    out('ff nf =>', $cc->ff(',metaRowTb', path: 'nf')); #rowOut new Req2Str);
    out('ff 12 =>', $cc->ff(',metaRowTb', id: 12)); #https://cloud.wlkl.ch/index.php/f/12
    outH('reuse ppl');
    out('reuseff =>', $cc->ff('Documents', $cc->ppl)); #new Req2Str);
} elseif (1) {
    $cc = $f->make('wlklGo');
  #  out($cc->getCrePa('tstDir/a/b/q/r/s/', crePa: true));
    out('ff csv', $cc->ff('%allAll,rowTb,PHST', '1XGHjhVcn-UQIMY6H2vpgdMWCgdKqiq8T' ));
#    out('ff csv', strlen($cont=rr($cc->ff('name id type path modTst owner size,metaRowCsv'))));
#    out('creUpd tst1.csv', $cc->creUpd('%all,row1,PHST KRF WRIPA', 'tst1.csv', 'text/csv', $cont), $cc->fun);
exit(); 
    out('creUpd', $cc->creUpd('%,Row1,PHST CREPA KRF', 'tstDir/a/bb/c/d/tstc1.txt', 'text/plain', "tstc1.txt updatiNNNNng\ngeht das um" . toLocaltst('now ') 
                 . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt!"), $cc->fun); #modTst: '2022-12-30T06:17:59'));
    out('---ff', $r = $cc->ff('path type modTst,metaRowTb', dirPa: 'bike/lecanne')); # bike  , "---->\n"  . rr($r)
    out('creUpd tst1.csv', $cc->creUpd('%all,row1,PHST KRF WRIPA', 'tst1.csv', 'text/csv', $cont), $cc->fun);
    out('---get', $cc->get('%allAll,rowTb', '1OI9ebyLj9r6LsfFlEq9x8JdFWfEgT2Jq6ofUI4IW1PQ')); # ausweisDiv
    out('---get', $cc->get('%allAll,rowTb', '1a1T8igwZ0PKYfGKceamhHViMhMsaZfPo')); # tstDir/a/b/tstc3.txt
    out('---get', $cc->get('%allAll,rowTb', path: 'tstDir/a/b/tstc3.txt')); # tstDir/a/b/tstc3.txt
    out('---ff', $r = $cc->ff('path type modTst,metaRowTb', dirPa: 'bike/lecanne')); # bike  , "---->\n"  . rr($r)
    out('---ff', $r = $cc->ff('path type modTst,metaRowTb', '1boOC6Tb5SY6weDFqnVlXxJUJHoI1BOoU')); # bike  , "---->\n"  . rr($r)
    out('creUpd', $cc->creUpd('%,Row1,PHST RECUR', 'tstDir/a/b/c/d/tstc1.txt', 'text/plain', "tstc1.txt updatiNNNNng\ngeht das um" . toLocaltst('now ') 
                 . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt!")); #modTst: '2022-12-30T06:17:59'));
    out('ffPath ', $cc->ff('%,row1,PHST RECUR WRIPA', dirPa: 'nf/2007bollenwees/walter/testli/Kopie von IMG_20200701_125122.jpg'));
    out($cc->create('%,row1', 'root', 'eins/zwei/dr*ei', Cloud::DIR));
#    $cc->pipeline('%')->walkRwkAdd('a', 'bc', 'parent', 'defg', 'id', 'x');
#    out('---ff sharedWithMe', $r = $cc->ff('path owner', whr: 'sharedWithMe', doBits: Cloud::WRIPA)); # sharedWithMe
    out('---get', $cc->get('%,rows', '1OI9ebyLj9r6LsfFlEq9x8JdFWfEgT2Jq6ofUI4IW1PQ'));
#    out('---get path only', $cc->get('path,rowTb', '1OI9ebyLj9r6LsfFlEq9x8JdFWfEgT2Jq6ofUI4IW1PQ'));
#    out('---get path only DIR ', $cc->get($p = $cc->pipeline('path,row1'), '1lpnkkHMYQumpT7yN36V8W27NUSknvqq5', paPa: 'paPa?'));
#    out('---get path only DIR 22 ', $cc->get($p, '1boOC6Tb5SY6weDFqnVlXxJUJHoI1BOoU', paPa: 'paPa22?'));
    out('---ff', $r = $cc->ff('path type modTst,metaRowCsv', '1boOC6Tb5SY6weDFqnVlXxJUJHoI1BOoU'), "---->\n" . rr($r)); # bike
#    out('---ff', $r = $cc->ff('path type modTst,metaRowCsv', '14Pz7b2wMO-VGddOnF-uIg72TTKbuv8vy'), "---->\n" . rr($r)); # leCanne
#    out('---ff', $r = $cc->ff('%,metaRowTb', 'root', doBits: Cloud::WRIPA)); # leCanne
#    out('---ffHTTP', $cc->ffHTTP('path,metaRowOut', '14Pz7b2wMO-VGddOnF-uIg72TTKbuv8vy')); # leCanne
#   out('---ffHTTP', $cc->ffHTTP('path,metaRowOut', '1boOC6Tb5SY6weDFqnVlXxJUJHoI1BOoU')); #bike
#    out('---ffHTTP', $cc->ffHTTP('path,metaRowOut')); #root
#   out('---ffHTTP', $cc->ffHTTP('path,metaRowOut', '1lKeqN20roh-OdyruYVUnrkJOjgppHojr')); #bike
#    out('create tstDir', $cc->create('%,RowTb', 'root', 'tstDir', Cloud::DIR));
#    out('create tstc2.txt', $cc->create('%,RowTb', '1qpwG6WEzEL3NlE7ytChUaSNrE1ya3tiH', 'tstc2.txt', 'text/plain', "tstc2.txt creating\ngeht das um" . toLocaltst('now') 
#                . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt created.", true, '1987-04-01T06:17:59'));
#   out('update', $cc->update('%,Row1', '1rsQI5VxiLLNP6-0hub-HF3WXd6x3N7LO', 'text/plain', "tstc1.txt updationg\ngeht das um" . toLocaltst('now') 
#                . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt!", true));
#   out('createMeta tst2.txt', $cc->uploadHTTP('%,RowTb', 'tst5.txt', 'text/plain', "txt.txt upload\ngeht das um" . toLocaltst('now') 
#                . "\nund weiter\nund weiter\nund weiter\nund weiter\nund weiter bis hier punkt"));
} elseif(1) {
    OutTb();
    foreach(['   a/  b  .  ', ' c *  d/e  /   f  .  /g/'] as $f)
            outTRD("<$f>", '<' . Cloud::fn2valid($f) .'>', '<' . Cloud::fn2valid($f, true) . '>');
    outTbEnd();
    $e = fopen('php://temp', 'w+');
    fputcsv($e, ['eins', 'a zbd zwei', '  e f   g  ']);
    rewind($e);
    out("csv", fread($e, 9999));
 
} else {
    $dbg=3;
    function ee($i) {
        if ($i > 0)
            return ee($i-1);
        out('ex1 empty:', (string) new NetEx());
        out('ex2 code:', (string) new NetEx(123, NetExCall: 4));
        $e = new NetNotFound('fehlter text', 'und so', drei: 333, vier: 'text4', a5: 'föif');
        out("ex3: $e");
        NetEx::errClear();
        netEx();
        throw $e;
    }
    file_get_contents('bit es nich');
    out('json', json_decode('{"wiek": "gehts", "und": {"so": 123, "abc": truxe}}', true));
    libxml_use_internal_errors(true);
    # out('simplexml file', simplexml_load_file('o.xml'));
    out('simplexml', simplexml_load_string("<?xml version='1.0' encoding='UTF-8'?>
            <d:note xmlns:d='DAV:' xmlns:s='http://sabredav.org/ns'><to>Tove</to></dnote>"));
    ee(3);
}
err('ende gut');
function rr($r) {
    if (! is_resource($r))
        return $r;
    $sz = ftell($r);
    rewind($r);
    return "file sz $sz\n" . ($sz < 1 ? '' : fread($r, $sz));
}

function tstArr() {
    $g = 'funScope g';
    $a = [ 0
            /* sdf
               wer */
         ,  function ($q) use ($g) { # start
                out("($q), g=$g");
            } # end
            /* und so
            weiter */
          ,2
    ];
    $r = new ReflectionFunction($a[1]);
    $ff ??= file($r->getFileName());
    out('#', rtrim(substr($l=$ff[-1 + $i=$r->getStartLine()], $p=strspn($l, ' \t'))));
    for ($t=min($i+1, $r->getEndLine()); $i < $t; $i++)
        out('#', rtrim(substr($ff[$i], $p)));
    out('->', $a[1]('soso'));
}

if (0) {
    $x = new MyXml(MyXml::$cfg['all'], 'outB', 'outE');
    $x->open('o.xml');
    if (0) {
        for ($i=0; $x->read() and $i <100; $i++)
            out("$i $x $x->localName depth $x->depth, uri $x->baseURI");
        exit();
    }
    $x->read();
    $x->walk();
    exit(outEnd());
}
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";

$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
libxml_use_internal_errors(true);

$xml=simplexml_load_file('o.xml',options: LIBXML_PEDANTIC | LIBXML_NONET);
if (! $xml) {
    err('xml error', libxml_get_errors());
} else {
    print_r($xml);
    out('---all', $xml);
    out('title', $xml->title);
    out('href', (string)$xml->{'d:response'}->{'d:href'});
}
exit();
    $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 'googleClientLib/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__);
}