12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374 |
- <?php
- namespace KD2\WebDAV
- {
-
- class Exception extends \RuntimeException {}
- class Server
- {
- // List of basic DAV properties that you should return if $requested_properties is NULL
- const BASIC_PROPERTIES = [
- 'DAV::resourcetype', // should be empty for files, and 'collection' for directories
- 'DAV::getcontenttype', // MIME type
- 'DAV::getlastmodified', // File modification date (must be \DateTimeInterface)
- 'DAV::getcontentlength', // file size
- 'DAV::displayname', // File name for display
- ];
- const EXTENDED_PROPERTIES = [
- 'DAV::getetag',
- 'DAV::creationdate',
- 'DAV::lastaccessed',
- 'DAV::ishidden',
- 'DAV::quota-used-bytes',
- 'DAV::quota-available-bytes',
- ];
-
- const PROP_DIGEST_MD5 = 'urn:karadav:digest_md5';
- const EMPTY_PROP_VALUE = 'DAV::empty';
- const SHARED_LOCK = 'shared';
- const EXCLUSIVE_LOCK = 'exclusive';
- protected bool $enable_gzip = true;
- protected string $base_uri;
- public string $original_uri;
- public string $prefix = '';
- protected AbstractStorage $storage;
- public function setStorage(AbstractStorage $storage)
- {
- $this->storage = $storage;
- }
- public function getStorage(): AbstractStorage
- {
- return $this->storage;
- }
- public function setBaseURI(string $uri): void
- {
- $this->base_uri = rtrim($uri, '/') . '/';
- }
- protected function extendExecutionTime(): void
- {
- if (false === strpos(@ini_get('disable_functions'), 'set_time_limit')) {
- @set_time_limit(3600);
- }
- @ini_set('max_execution_time', '3600');
- @ini_set('max_input_time', '3600');
- }
- protected function _prefix(string $uri): string
- {
- if (!$this->prefix) {
- return $uri;
- }
- return rtrim(rtrim($this->prefix, '/') . '/' . ltrim($uri, '/'), '/');
- }
- protected function html_directory(string $uri, iterable $list): ?string
- {
-
- if (substr($this->original_uri, -1) != '/') {
- http_response_code(301);
- header(sprintf('Location: /%s/', trim($this->base_uri . $uri, '/')), true);
- return null;
- }
- $out = sprintf('<!DOCTYPE html><html data-webdav-url="%s"><head><meta name="viewport" content="width=device-width, initial-scale=1.0, target-densitydpi=device-dpi" /><style>
- body { font-size: 1.1em; font-family: Arial, Helvetica, sans-serif; }
- table { border-collapse: collapse; }
- th, td { padding: .5em; text-align: left; border: 2px solid #ccc; }
- span { font-size: 40px; line-height: 40px; }
- </style>', '/' . ltrim($this->base_uri, '/'));
- $out .= sprintf('<title>%s</title></head><body><h1>%1$s</h1><table>', htmlspecialchars($uri ? str_replace('/', ' / ', $uri) . ' - Files' : 'Files'));
- if (trim($uri)) {
- $out .= '<tr><th colspan=3><a href="../"><b>Back</b></a></th></tr>';
- }
- $props = null;
- foreach ($list as $file => $props) {
- if (null === $props) {
- $props = $this->storage->properties(trim($uri . '/' . $file, '/'), self::BASIC_PROPERTIES, 0);
- }
- $collection = !empty($props['DAV::resourcetype']) && $props['DAV::resourcetype'] == 'collection';
- if ($collection) {
- $out .= sprintf('<tr><td>[DIR]</td><th><a href="%s/"><b>%s</b></a></th></tr>', rawurlencode($file), htmlspecialchars($file));
- }
- else {
- $size = $props['DAV::getcontentlength'];
- if ($size > 1024*1024) {
- $size = sprintf('%d MB', $size / 1024 / 1024);
- }
- elseif ($size) {
- $size = sprintf('%d KB', $size / 1024);
- }
- $date = $props['DAV::getlastmodified'];
- if ($date instanceof \DateTimeInterface) {
- $date = $date->format('d/m/Y H:i');
- }
- $out .= sprintf('<tr><td></td><th><a href="%s">%s</a></th><td>%s</td><td>%s</td></tr>',
- rawurlencode($file),
- htmlspecialchars($file),
- $size,
- $date
- );
- }
- }
- $out .= '</table>';
- if (null === $props) {
- $out .= '<p>This directory is empty.</p>';
- }
- $out .= '</body></html>';
- return $out;
- }
- public function http_delete(string $uri): ?string
- {
-
- if (isset($_SERVER['HTTP_DEPTH']) && $_SERVER['HTTP_DEPTH'] != 'infinity') {
- throw new Exception('We can only delete to infinity', 400);
- }
- $uri = $this->_prefix($uri);
- $this->checkLock($uri);
- $this->storage->delete($uri);
- if ($token = $this->getLockToken()) {
- $this->storage->unlock($uri, $token);
- }
- http_response_code(204);
- header('Content-Length: 0', true);
- return null;
- }
- public function http_put(string $uri): ?string
- {
- if (!empty($_SERVER['HTTP_CONTENT_TYPE']) && !strncmp($_SERVER['HTTP_CONTENT_TYPE'], 'multipart/', 10)) {
- throw new Exception('Multipart PUT requests are not supported', 501);
- }
- if (!empty($_SERVER['HTTP_CONTENT_ENCODING'])) {
- if (false !== strpos($_SERVER['HTTP_CONTENT_ENCODING'], 'gzip')) {
-
- throw new Exception('Content Encoding is not supported', 501);
- }
- else {
- throw new Exception('Content Encoding is not supported', 501);
- }
- }
- if (!empty($_SERVER['HTTP_CONTENT_RANGE'])) {
- throw new Exception('Content Range is not supported', 501);
- }
-
- if (isset($_SERVER['HTTP_X_EXPECTED_ENTITY_LENGTH'])) {
- throw new Exception('This server is not compatible with OS/X finder. Consider using a different WebDAV client or webserver.', 403);
- }
- $hash = null;
- $hash_algo = null;
-
-
- if (!empty($_SERVER['HTTP_CONTENT_MD5'])) {
- $hash = bin2hex(base64_decode($_SERVER['HTTP_CONTENT_MD5']));
- $hash_algo = 'MD5';
- }
-
-
- elseif (!empty($_SERVER['HTTP_OC_CHECKSUM'])
- && preg_match('/MD5:[a-f0-9]{32}|SHA1:[a-f0-9]{40}/', $_SERVER['HTTP_OC_CHECKSUM'], $match)) {
- $hash_algo = strtok($match[0], ':');
- $hash = strtok(false);
- }
- $uri = $this->_prefix($uri);
- $this->checkLock($uri);
- if (!empty($_SERVER['HTTP_IF_MATCH'])) {
- $etag = trim($_SERVER['HTTP_IF_MATCH'], '" ');
- $prop = $this->storage->properties($uri, ['DAV::getetag'], 0);
- if (!empty($prop['DAV::getetag']) && $prop['DAV::getetag'] != $etag) {
- throw new Exception('ETag did not match condition', 412);
- }
- }
-
-
- $mtime = (int)($_SERVER['HTTP_X_OC_MTIME'] ?? 0) ?: null;
- if ($mtime) {
- header('X-OC-MTime: accepted');
- }
- $this->extendExecutionTime();
- $stream = fopen('php://input', 'r');
-
-
- if (strstr($_SERVER['HTTP_TRANSFER_ENCODING'] ?? '', 'chunked') && PHP_SAPI == 'fpm-fcgi') {
-
-
- $l = strlen(fread($stream, 1));
- if ($l === 0) {
- throw new Exception('This server cannot accept "Transfer-Encoding: chunked" uploads (please upgrade to mod_fcgid >= 2.3.10).', 500);
- }
-
- fseek($stream, 0, SEEK_SET);
- }
- $created = $this->storage->put($uri, $stream, $hash_algo, $hash, $mtime);
- $prop = $this->storage->properties($uri, ['DAV::getetag'], 0);
- if (!empty($prop['DAV::getetag'])) {
- $value = $prop['DAV::getetag'];
- if (substr($value, 0, 1) != '"') {
- $value = '"' . $value . '"';
- }
- header(sprintf('ETag: %s', $value));
- }
- http_response_code($created ? 201 : 204);
- return null;
- }
- public function http_head(string $uri, array &$props = []): ?string
- {
- $uri = $this->_prefix($uri);
- $requested_props = self::BASIC_PROPERTIES;
- $requested_props[] = 'DAV::getetag';
-
- if (!empty($_SERVER['HTTP_WANT_DIGEST'])) {
- $requested_props[] = self::PROP_DIGEST_MD5;
- }
- $props = $this->storage->properties($uri, $requested_props, 0);
- if (!$props) {
- throw new Exception('Resource Not Found', 404);
- }
- http_response_code(200);
- if (isset($props['DAV::getlastmodified'])
- && $props['DAV::getlastmodified'] instanceof \DateTimeInterface) {
- header(sprintf('Last-Modified: %s', $props['DAV::getlastmodified']->format(\DATE_RFC7231)));
- }
- if (!empty($props['DAV::getetag'])) {
- $value = $props['DAV::getetag'];
- if (substr($value, 0, 1) != '"') {
- $value = '"' . $value . '"';
- }
- header(sprintf('ETag: %s', $value));
- }
- if (empty($props['DAV::resourcetype']) || $props['DAV::resourcetype'] != 'collection') {
- if (!empty($props['DAV::getcontenttype'])) {
- header(sprintf('Content-Type: %s', $props['DAV::getcontenttype']));
- }
- if (!empty($props['DAV::getcontentlength'])) {
- header(sprintf('Content-Length: %d', $props['DAV::getcontentlength']));
- header('Accept-Ranges: bytes');
- }
- }
- if (!empty($props[self::PROP_DIGEST_MD5])) {
- header(sprintf('Digest: md5=%s', base64_encode(hex2bin($props[self::PROP_DIGEST_MD5]))));
- }
- return null;
- }
- public function http_get(string $uri): ?string
- {
- $props = [];
- $this->http_head($uri, $props);
- $uri = $this->_prefix($uri);
- $is_collection = !empty($props['DAV::resourcetype']) && $props['DAV::resourcetype'] == 'collection';
- $out = '';
- if ($is_collection) {
- $list = $this->storage->list($uri, self::BASIC_PROPERTIES);
- if (!isset($_SERVER['HTTP_ACCEPT']) || false === strpos($_SERVER['HTTP_ACCEPT'], 'html')) {
- $list = is_array($list) ? $list : iterator_to_array($list);
- if (!count($list)) {
- return "Nothing in this collection\n";
- }
- return implode("\n", array_keys($list));
- }
- header('Content-Type: text/html; charset=utf-8', true);
- return $this->html_directory($uri, $list);
- }
- $file = $this->storage->get($uri);
- if (!$file) {
- throw new Exception('File Not Found', 404);
- }
-
- if (!empty($file['stop'])) {
- return null;
- }
- if (!isset($file['content']) && !isset($file['resource']) && !isset($file['path'])) {
- throw new \RuntimeException('Invalid file array returned by ::get()');
- }
- $this->extendExecutionTime();
- $length = $start = $end = null;
- $gzip = false;
- if (isset($_SERVER['HTTP_RANGE'])
- && preg_match('/^bytes=(\d*)-(\d*)$/i', $_SERVER['HTTP_RANGE'], $match)
- && $match[1] . $match[2] !== '') {
- $start = $match[1] === '' ? null : (int) $match[1];
- $end = $match[2] === '' ? null : (int) $match[2];
- if (null !== $start && $start < 0) {
- throw new Exception('Start range cannot be satisfied', 416);
- }
- if (isset($props['DAV::getcontentlength']) && $start > $props['DAV::getcontentlength']) {
- throw new Exception('End range cannot be satisfied', 416);
- }
- $this->log('HTTP Range requested: %s-%s', $start, $end);
- }
- elseif ($this->enable_gzip
- && isset($_SERVER['HTTP_ACCEPT_ENCODING'])
- && false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')
- && isset($props['DAV::getcontentlength'])
-
- && $props['DAV::getcontentlength'] < 8*1024*1024
-
- && !preg_match('/\.(?:cbz|cbr|cb7|mp4|m4a|zip|docx|xlsx|pptx|ods|odt|odp|7z|gz|bz2|lzma|lz|xz|apk|dmg|jar|rar|webm|ogg|mp3|ogm|flac|ogv|mkv|avi)$/i', $uri)) {
- $gzip = true;
- header('Content-Encoding: gzip', true);
- }
-
- if (function_exists('apache_setenv')) {
- @apache_setenv('no-gzip', 1);
- }
- @ini_set('zlib.output_compression', 'Off');
- if (@ob_get_length()) {
- @ob_clean();
- }
- if (isset($file['content'])) {
- $length = strlen($file['content']);
- if ($start || $end) {
- if (null !== $end && $end > $length) {
- header('Content-Range: bytes */' . $length, true);
- throw new Exception('End range cannot be satisfied', 416);
- }
- if ($start === null) {
- $start = $length - $end;
- $end = $start + $end;
- }
- elseif ($end === null) {
- $end = $length;
- }
- http_response_code(206);
- header(sprintf('Content-Range: bytes %s-%s/%s', $start, $end - 1, $length));
- $file['content'] = substr($file['content'], $start, $end - $start);
- $length = $end - $start;
- }
- if ($gzip) {
- $file['content'] = gzencode($file['content'], 9);
- $length = strlen($file['content']);
- }
- header('Content-Length: ' . $length, true);
- echo $file['content'];
- return null;
- }
- if (isset($file['path'])) {
- $file['resource'] = fopen($file['path'], 'rb');
- }
- $seek = fseek($file['resource'], 0, SEEK_END);
- if ($seek === 0) {
- $length = ftell($file['resource']);
- fseek($file['resource'], 0, SEEK_SET);
- }
- if (($start || $end) && $seek === 0) {
- if (null !== $end && $end > $length) {
- header('Content-Range: bytes */' . $length, true);
- throw new Exception('End range cannot be satisfied', 416);
- }
- if ($start === null) {
- $start = $length - $end;
- $end = $start + $end;
- }
- elseif ($end === null) {
- $end = $length;
- }
- fseek($file['resource'], $start, SEEK_SET);
- http_response_code(206);
- header(sprintf('Content-Range: bytes %s-%s/%s', $start, $end - 1, $length), true);
- $length = $end - $start;
- $end -= $start;
- }
- elseif (null === $length && isset($file['path'])) {
- $end = $length = filesize($file['path']);
- }
- if ($gzip) {
- $this->log('Using gzip output compression');
- $gzip = deflate_init(ZLIB_ENCODING_GZIP);
- $fp = fopen('php://temp', 'wb');
- while (!feof($file['resource'])) {
- fwrite($fp, deflate_add($gzip, fread($file['resource'], 8192), ZLIB_NO_FLUSH));
- }
- fwrite($fp, deflate_add($gzip, '', ZLIB_FINISH));
- $length = ftell($fp);
- rewind($fp);
- fclose($file['resource']);
- $file['resource'] = $fp;
- unset($fp);
- }
- if (null !== $length) {
- $this->log('Length: %s', $length);
- header('Content-Length: ' . $length, true);
- }
- $block_size = 8192*4;
- while (!feof($file['resource']) && ($end === null || $end > 0)) {
- $l = $end !== null ? min($block_size, $end) : $block_size;
- echo fread($file['resource'], $l);
- flush();
- if (null !== $end) {
- $end -= $block_size;
- }
- }
- fclose($file['resource']);
- return null;
- }
- public function http_copy(string $uri): ?string
- {
- return $this->_http_copymove($uri, 'copy');
- }
- public function http_move(string $uri): ?string
- {
- return $this->_http_copymove($uri, 'move');
- }
- protected function _http_copymove(string $uri, string $method): ?string
- {
- $uri = $this->_prefix($uri);
- $destination = $_SERVER['HTTP_DESTINATION'] ?? null;
- $depth = $_SERVER['HTTP_DEPTH'] ?? 1;
- if (!$destination) {
- throw new Exception('Destination not supplied', 400);
- }
- $destination = $this->getURI($destination);
- if (trim($destination, '/') == trim($uri, '/')) {
- throw new Exception('Cannot move file to itself', 403);
- }
- $overwrite = ($_SERVER['HTTP_OVERWRITE'] ?? null) == 'T';
-
- if (empty($destination)) {
- $destination = basename($uri);
- }
- $this->log('<= Destination: %s', $destination);
- $this->log('<= Overwrite: %s (%s)', $overwrite ? 'Yes' : 'No', $_SERVER['HTTP_OVERWRITE'] ?? null);
- if (!$overwrite && $this->storage->exists($destination)) {
- throw new Exception('File already exists and overwriting is disabled', 412);
- }
- if ($method == 'move') {
- $this->checkLock($uri);
- }
- $this->checkLock($destination);
-
-
- if ($depth == 0
- && $this->storage->exists($destination)
- && current($this->storage->properties($destination, ['DAV::resourcetype'], 0)) == 'collection') {
- $overwritten = $this->storage->exists($uri);
- }
- else {
- $overwritten = $this->storage->$method($uri, $destination);
- }
- if ($method == 'move' && ($token = $this->getLockToken())) {
- $this->storage->unlock($uri, $token);
- }
- http_response_code($overwritten ? 204 : 201);
- return null;
- }
- public function http_mkcol(string $uri): ?string
- {
- if (!empty($_SERVER['CONTENT_LENGTH'])) {
- throw new Exception('Unsupported body for MKCOL', 415);
- }
- $uri = $this->_prefix($uri);
- $this->storage->mkcol($uri);
- http_response_code(201);
- return null;
- }
- protected function extractRequestedProperties(string $body): ?array
- {
-
-
- if (!preg_match('!<(?:\w+:)?propfind!', $body)) {
- return null;
- }
- $ns = [];
- $dav_ns = null;
- $default_ns = null;
- if (preg_match('/<propfind[^>]+xmlns="DAV:"/', $body)) {
- $default_ns = 'DAV:';
- }
- preg_match_all('!xmlns:(\w+)\s*=\s*"([^"]+)"!', $body, $match, PREG_SET_ORDER);
-
- foreach ($match as $found) {
- $ns[$found[2]] = $found[1];
- }
- if (isset($ns['DAV:'])) {
- $dav_ns = $ns['DAV:'] . ':';
- }
- $regexp = '/<(' . $dav_ns . 'prop(?!find))[^>]*?>(.*?)<\/\1\s*>/s';
- if (!preg_match($regexp, $body, $match)) {
- return null;
- }
-
-
-
- preg_match_all('!<([\w-]+)[^>]*xmlns="([^"]*)"|<(?:([\w-]+):)?([\w-]+)!', $match[2], $match, PREG_SET_ORDER);
- $properties = [];
- foreach ($match as $found) {
- if (isset($found[4])) {
- $url = array_search($found[3], $ns) ?: $default_ns;
- $name = $found[4];
- }
- else {
- $url = $found[2];
- $name = $found[1];
- }
- $properties[$url . ':' . $name] = [
- 'name' => $name,
- 'ns_alias' => $found[3] ?? null,
- 'ns_url' => $url,
- ];
- }
- return $properties;
- }
- public function http_propfind(string $uri): ?string
- {
-
- $depth = isset($_SERVER['HTTP_DEPTH']) && empty($_SERVER['HTTP_DEPTH']) ? 0 : 1;
- $uri = $this->_prefix($uri);
- $body = file_get_contents('php://input');
- if (false !== strpos($body, '<!DOCTYPE ')) {
- throw new Exception('Invalid XML', 400);
- }
- $this->log('Requested depth: %s', $depth);
-
-
- if (isset($_SERVER['HTTP_X_LITMUS'])) {
- if (false !== strpos($body, '<!DOCTYPE ')) {
- throw new Exception('Invalid XML', 400);
- }
- $xml = @simplexml_load_string($body);
- if ($e = libxml_get_last_error()) {
- throw new Exception('Invalid XML', 400);
- }
- }
- $requested = $this->extractRequestedProperties($body);
- $requested_keys = $requested ? array_keys($requested) : null;
-
- $properties = $this->storage->properties($uri, $requested_keys, $depth);
- if (null === $properties) {
- throw new Exception('This does not exist', 404);
- }
- $items = [$uri => $properties];
- if ($depth) {
- foreach ($this->storage->list($uri, $requested) as $file => $properties) {
- $path = trim($uri . '/' . $file, '/');
- $properties = $properties ?? $this->storage->properties($path, $requested_keys, 0);
- if (!$properties) {
- $this->log('!!! Cannot find "%s"', $path);
- continue;
- }
- $items[$path] = $properties;
- }
- }
-
- header('HTTP/1.1 207 Multi-Status', true);
- $this->dav_header();
- header('Content-Type: application/xml; charset=utf-8');
- $root_namespaces = [
- 'DAV:' => 'd',
-
- 'urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/' => 'ns0',
- ];
- $i = 0;
- $requested ??= [];
- foreach ($requested as $prop) {
- if ($prop['ns_url'] == 'DAV:' || !$prop['ns_url']) {
- continue;
- }
- if (!array_key_exists($prop['ns_url'], $root_namespaces)) {
- $root_namespaces[$prop['ns_url']] = $prop['ns_alias'] ?: 'rns' . $i++;
- }
- }
- foreach ($items as $properties) {
- foreach ($properties as $name => $value) {
- $pos = strrpos($name, ':');
- $ns = substr($name, 0, strrpos($name, ':'));
-
- if (!$ns) {
- continue;
- }
- if (!array_key_exists($ns, $root_namespaces)) {
- $root_namespaces[$ns] = 'rns' . $i++;
- }
- }
- }
- $out = '<?xml version="1.0" encoding="utf-8"?>';
- $out .= '<d:multistatus';
- foreach ($root_namespaces as $url => $alias) {
- $out .= sprintf(' xmlns:%s="%s"', $alias, $url);
- }
- $out .= '>';
- foreach ($items as $uri => $item) {
- $e = '<d:response>';
- if ($this->prefix) {
- $uri = substr($uri, strlen($this->prefix));
- }
- $uri = trim(rtrim($this->base_uri, '/') . '/' . ltrim($uri, '/'), '/');
- $path = '/' . str_replace('%2F', '/', rawurlencode($uri));
- if (($item['DAV::resourcetype'] ?? null) == 'collection' && $path != '/') {
- $path .= '/';
- }
- $e .= sprintf('<d:href>%s</d:href>', htmlspecialchars($path, ENT_XML1));
- $e .= '<d:propstat><d:prop>';
- foreach ($item as $name => $value) {
- if (null === $value) {
- continue;
- }
- $pos = strrpos($name, ':');
- $ns = substr($name, 0, strrpos($name, ':'));
- $tag_name = substr($name, strrpos($name, ':') + 1);
- $alias = $root_namespaces[$ns] ?? null;
- $attributes = '';
-
-
- if ($name == 'DAV::creationdate'
- && ($value instanceof \DateTimeInterface)
- && false !== stripos($_SERVER['HTTP_USER_AGENT'] ?? '', 'owncloud')) {
- $value = $value->getTimestamp();
- }
-
-
- elseif ($name == 'DAV::getcontenttype'
- && ($item['DAV::resourcetype'] ?? null) == 'collection') {
- $value = null;
- }
- if ($name == 'DAV::resourcetype' && $value == 'collection') {
- $value = '<d:collection />';
- }
- elseif ($name == 'DAV::getetag' && strlen($value) && $value[0] != '"') {
- $value = '"' . $value . '"';
- }
- elseif ($value instanceof \DateTimeInterface) {
-
- $value = clone $value;
- $value->setTimezone(new \DateTimeZone('GMT'));
- $value = $value->format(DATE_RFC7231);
- }
- elseif (is_array($value)) {
- $attributes = $value['attributes'] ?? '';
- $value = $value['xml'] ?? null;
- }
- else {
- $value = htmlspecialchars($value, ENT_XML1);
- }
-
- if (!$ns) {
- $attributes .= ' xmlns=""';
- }
- else {
- $tag_name = $alias . ':' . $tag_name;
- }
- if (null === $value || self::EMPTY_PROP_VALUE === $value) {
- $e .= sprintf('<%s%s />', $tag_name, $attributes ? ' ' . $attributes : '');
- }
- else {
- $e .= sprintf('<%s%s>%s</%1$s>', $tag_name, $attributes ? ' ' . $attributes : '', $value);
- }
- }
- $e .= '</d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat>' . "\n";
-
- if (!empty($requested)) {
- $missing_properties = array_diff($requested_keys, array_keys($item));
- if (count($missing_properties)) {
- $e .= '<d:propstat><d:prop>';
- foreach ($missing_properties as $name) {
- $pos = strrpos($name, ':');
- $ns = substr($name, 0, strrpos($name, ':'));
- $name = substr($name, strrpos($name, ':') + 1);
- $alias = $root_namespaces[$ns] ?? null;
-
- if (!$alias) {
- $e .= sprintf('<%s xmlns="" />', $name);
- }
- else {
- $e .= sprintf('<%s:%s />', $alias, $name);
- }
- }
- $e .= '</d:prop><d:status>HTTP/1.1 404 Not Found</d:status></d:propstat>';
- }
- }
- $e .= '</d:response>' . "\n";
- $out .= $e;
- }
- $out .= '</d:multistatus>';
- return $out;
- }
- static public function parsePropPatch(string $body): array
- {
- if (false !== strpos($body, '<!DOCTYPE ')) {
- throw new Exception('Invalid XML', 400);
- }
- $xml = @simplexml_load_string($body);
- if (false === $xml) {
- throw new WebDAV_Exception('Invalid XML', 400);
- }
- $_ns = null;
-
- if (!empty(key($xml->getDocNameSpaces()))) {
- $_ns = 'DAV:';
- }
- $out = [];
-
- foreach ($xml->children($_ns) as $child) {
- foreach ($child->children($_ns) as $prop) {
- $prop = $prop->children();
- if ($child->getName() == 'set') {
- $ns = $prop->getNamespaces(true);
- $ns = array_flip($ns);
- $name = key($ns) . ':' . $prop->getName();
- $attributes = $prop->attributes();
- $attributes = $attributes === null ? null : iterator_to_array($attributes);
- foreach ($ns as $xmlns => $alias) {
- foreach (iterator_to_array($prop->attributes($alias)) as $key => $v) {
- $attributes[$xmlns . ':' . $key] = $value;
- }
- }
- if ($prop->count() > 1) {
- $text = '';
- foreach ($prop->children() as $c) {
- $text .= $c->asXML();
- }
- }
- else {
- $text = (string)$prop;
- }
- $out[$name] = ['action' => 'set', 'attributes' => $attributes ?: null, 'content' => $text ?: null];
- }
- else {
- $ns = $prop->getNamespaces();
- $name = current($ns) . ':' . $prop->getName();
- $out[$name] = ['action' => 'remove'];
- }
- }
- }
- return $out;
- }
- public function http_proppatch(string $uri): ?string
- {
- $uri = $this->_prefix($uri);
- $this->checkLock($uri);
- $body = file_get_contents('php://input');
- $this->storage->setProperties($uri, $body);
-
- header('HTTP/1.1 207 Multi-Status', true);
- header('Content-Type: application/xml; charset=utf-8');
- $out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
- $out .= '<d:multistatus xmlns:d="DAV:">';
- $out .= '</d:multistatus>';
- return $out;
- }
- public function http_lock(string $uri): ?string
- {
- $uri = $this->_prefix($uri);
-
-
-
-
- if (empty($_SERVER['CONTENT_LENGTH']) && !empty($_SERVER['HTTP_IF'])) {
- $token = $this->getLockToken();
- if (!$token) {
- throw new Exception('Invalid If header', 400);
- }
- $info = null;
- $ns = 'D';
- $scope = self::EXCLUSIVE_LOCK;
- $this->checkLock($uri, $token);
- $this->log('Requesting LOCK refresh: %s = %s', $uri, $scope);
- }
- else {
- $locked_scope = $this->storage->getLock($uri);
- if ($locked_scope == self::EXCLUSIVE_LOCK) {
- throw new Exception('Cannot acquire another lock, resource is locked for exclusive use', 423);
- }
- if ($locked_scope && $token = $this->getLockToken()) {
- $token = $this->getLockToken();
- if (!$token) {
- throw new Exception('Missing lock token', 423);
- }
- $this->checkLock($uri, $token);
- }
- $xml = file_get_contents('php://input');
- if (!preg_match('!<((?:(\w+):)?lockinfo)[^>]*>(.*?)</\1>!is', $xml, $match)) {
- throw new Exception('Invalid XML', 400);
- }
- $ns = $match[2];
- $info = $match[3];
-
- $uuid = random_bytes(16);
- $uuid[6] = chr(ord($uuid[6]) & 0x0f | 0x40);
- $uuid[8] = chr(ord($uuid[8]) & 0x3f | 0x80);
- $uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($uuid), 4));
- $token = 'opaquelocktoken:' . $uuid;
- $scope = false !== stripos($info, sprintf('<%sexclusive', $ns ? $ns . ':' : '')) ? self::EXCLUSIVE_LOCK : self::SHARED_LOCK;
- $this->log('Requesting LOCK: %s = %s', $uri, $scope);
- }
- $this->storage->lock($uri, $token, $scope);
- $timeout = 60*5;
- $info = sprintf('
- <d:lockscope><d:%s /></d:lockscope>
- <d:locktype><d:write /></d:locktype>
- <d:owner>unknown</d:owner>
- <d:depth>%d</d:depth>
- <d:timeout>Second-%d</d:timeout>
- <d:locktoken><d:href>%s</d:href></d:locktoken>
- ', $scope, 1, $timeout, $token);
- http_response_code(200);
- header('Content-Type: application/xml; charset=utf-8');
- header(sprintf('Lock-Token: <%s>', $token));
- $out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
- $out .= '<d:prop xmlns:d="DAV:">';
- $out .= '<d:lockdiscovery><d:activelock>';
- $out .= $info;
- $out .= '</d:activelock></d:lockdiscovery></d:prop>';
- if ($ns != 'D') {
- $out = str_replace('D:', $ns ? $ns . ':' : '', $out);
- $out = str_replace('xmlns:D', $ns ? 'xmlns:' . $ns : 'xmlns', $out);
- }
- return $out;
- }
- public function http_unlock(string $uri): ?string
- {
- $uri = $this->_prefix($uri);
- $token = $this->getLockToken();
- if (!$token) {
- throw new Exception('Invalid Lock-Token header', 400);
- }
- $this->log('<= Lock Token: %s', $token);
- $this->checkLock($uri, $token);
- $this->storage->unlock($uri, $token);
- http_response_code(204);
- return null;
- }
- protected function getLockToken(): ?string
- {
- if (isset($_SERVER['HTTP_LOCK_TOKEN'])
- && preg_match('/<(.*?)>/', trim($_SERVER['HTTP_LOCK_TOKEN']), $match)) {
- return $match[1];
- }
- elseif (isset($_SERVER['HTTP_IF'])
- && preg_match('/\(<(.*?)>\)/', trim($_SERVER['HTTP_IF']), $match)) {
- return $match[1];
- }
- else {
- return null;
- }
- }
- protected function checkLock(string $uri, ?string $token = null): void
- {
- if ($token === null) {
- $token = $this->getLockToken();
- }
-
- if (isset($_SERVER['HTTP_IF'])
- && preg_match('/<([^>]+)>\s*\(<[^>]*>\)/', $_SERVER['HTTP_IF'], $match)) {
- $root = $this->getURI($match[1]);
- if (0 !== strpos($uri, $root)) {
- throw new Exception('Invalid "If" header path: ' . $root, 400);
- }
- $uri = $root;
- }
-
- elseif (isset($_SERVER['HTTP_IF'])
- && preg_match('/\(<([^>]*)>\s+\["([^""]+)"\]\)/', $_SERVER['HTTP_IF'], $match)) {
- $token = $match[1];
- $request_etag = $match[2];
- $etag = current($this->storage->properties($uri, ['DAV::getetag'], 0));
- if ($request_etag != $etag) {
- throw new Exception('Resource is locked and etag does not match', 412);
- }
- }
- if ($token == 'DAV:no-lock') {
- throw new Exception('Resource is locked', 412);
- }
-
- if ($token && $this->storage->getLock($uri, $token)) {
- return;
- }
- elseif ($token) {
- throw new Exception('Invalid token', 400);
- }
-
- elseif ($this->storage->getLock($uri)) {
- throw new Exception('Resource is locked', 423);
- }
- }
- protected function dav_header()
- {
- header('DAV: 1, 2, 3');
- }
- public function http_options(): void
- {
- http_response_code(200);
- $methods = 'GET HEAD PUT DELETE COPY MOVE PROPFIND MKCOL LOCK UNLOCK';
- $this->dav_header();
- header('Allow: ' . $methods);
- header('Content-length: 0');
- header('Accept-Ranges: bytes');
- header('MS-Author-Via: DAV');
- }
- public function log(string $message, ...$params)
- {
- if (PHP_SAPI == 'cli-server') {
- file_put_contents('php://stderr', vsprintf($message, $params) . "\n");
- }
- }
- protected function getURI(string $source): string
- {
- $uri = parse_url($source, PHP_URL_PATH);
- $uri = rawurldecode($uri);
- $uri = rtrim($uri, '/');
- if ($uri . '/' == $this->base_uri) {
- $uri .= '/';
- }
- if (strpos($uri, $this->base_uri) !== 0) {
- throw new Exception(sprintf('Invalid URI, "%s" is outside of scope "%s"', $uri, $this->base_uri), 400);
- }
- $uri = preg_replace('!/{2,}!', '/', $uri);
- if (false !== strpos($uri, '..')) {
- throw new Exception(sprintf('Invalid URI: "%s"', $uri), 403);
- }
- $uri = substr($uri, strlen($this->base_uri));
- $uri = $this->_prefix($uri);
- return $uri;
- }
- public function route(?string $uri = null): bool
- {
- if (null === $uri) {
- $uri = $_SERVER['REQUEST_URI'] ?? '/';
- }
- $this->original_uri = $uri;
- if ($uri . '/' == $this->base_uri) {
- $uri .= '/';
- }
- if (0 === strpos($uri, $this->base_uri)) {
- $uri = substr($uri, strlen($this->base_uri));
- }
- else {
- $this->log('<= %s is not a managed URL', $uri);
- return false;
- }
-
- if (isset($_SERVER['HTTP_X_LITMUS']) || isset($_SERVER['HTTP_X_LITMUS_SECOND'])) {
- $this->log('X-Litmus: %s', $_SERVER['HTTP_X_LITMUS'] ?? $_SERVER['HTTP_X_LITMUS_SECOND']);
- }
- $method = $_SERVER['REQUEST_METHOD'] ?? null;
- header_remove('Expires');
- header_remove('Pragma');
- header_remove('Cache-Control');
- header('X-Server: KD2', true);
-
- if ($method == 'OPTIONS') {
- $this->log('<= OPTIONS');
- $this->http_options();
- return true;
- }
- $uri = rawurldecode($uri);
- $uri = trim($uri, '/');
- $uri = preg_replace('!/{2,}!', '/', $uri);
- $this->log('<= %s /%s', $method, $uri);
- try {
- if (false !== strpos($uri, '..')) {
- throw new Exception(sprintf('Invalid URI: "%s"', $uri), 403);
- }
-
- $method = 'http_' . strtolower($method);
- if (!method_exists($this, $method)) {
- throw new Exception('Invalid request method', 405);
- }
- $out = $this->$method($uri);
- $this->log('=> %d', http_response_code());
- if (null !== $out) {
- $this->log('=> %s', $out);
- }
- echo $out;
- }
- catch (Exception $e) {
- $this->error($e);
- }
- return true;
- }
- function error(Exception $e)
- {
- $this->log('=> %d - %s', $e->getCode(), $e->getMessage());
- if ($e->getCode() == 423) {
-
- header('HTTP/1.1 423 Locked');
- }
- else {
- http_response_code($e->getCode());
- }
- header('Content-Type: application/xml; charset=utf-8', true);
- printf('<?xml version="1.0" encoding="utf-8"?><d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"><s:message>%s</s:message></d:error>', htmlspecialchars($e->getMessage(), ENT_XML1));
- }
- static public function hmac(array $data, string $key = '')
- {
-
- $data = array_map('sha1', $data);
- $data = implode(':', $data);
- return hash_hmac('sha1', $data, sha1($key));
- }
- }
- abstract class AbstractStorage
- {
- abstract public function get(string $uri): ?array;
- abstract public function exists(string $uri): bool;
- abstract public function properties(string $uri, ?array $requested_properties, int $depth): ?array;
- public function setProperties(string $uri, string $body): void
- {
-
- }
- abstract public function put(string $uri, $pointer, ?string $hash_algo, ?string $hash, ?int $mtime): bool;
- abstract public function delete(string $uri): void;
- abstract public function copy(string $uri, string $destination): bool;
- abstract public function move(string $uri, string $destination): bool;
- abstract public function mkcol(string $uri): void;
- abstract public function list(string $uri, array $properties): iterable;
- public function lock(string $uri, string $token, string $scope): void
- {
-
- }
- public function unlock(string $uri, string $token): void
- {
-
- }
- public function getLock(string $uri, ?string $token = null): ?string
- {
-
- return null;
- }
- }
- }
- namespace PicoDAV
- {
- use KD2\WebDAV\AbstractStorage;
- use KD2\WebDAV\Exception as WebDAV_Exception;
- class Storage extends AbstractStorage
- {
-
- const PUT_IGNORE_PATTERN = '!^~(?:lock\.|^\._)|^(?:\.DS_Store|Thumbs\.db|desktop\.ini)$!';
- protected string $path;
- protected ?string $user = null;
- public array $users = [];
- public function __construct(string $path)
- {
- $this->path = $path . '/';
- }
- public function auth(): bool
- {
- if (ANONYMOUS_WRITE && ANONYMOUS_READ) {
- return true;
- }
- if ($this->user) {
- return true;
- }
- $user = $_SERVER['PHP_AUTH_USER'] ?? null;
- $password = $_SERVER['PHP_AUTH_PW'] ?? null;
- $hash = $this->users[$user]['password'] ?? null;
- if (!$hash) {
- return false;
- }
- if (!password_verify($password, $hash)) {
- return false;
- }
- $this->user = $user;
- return true;
- }
- static protected function glob(string $path, string $pattern = '', int $flags = 0): array
- {
- $path = preg_replace('/[\*\?\[\]]/', '\\\\$0', $path);
- return glob($path . $pattern, $flags);
- }
- public function canRead(string $uri): bool
- {
- if (in_array($uri, INTERNAL_FILES)) {
- return false;
- }
- if (preg_match('/\.(?:php\d?|phtml|phps)$|^\./i', $uri)) {
- return false;
- }
- if (ANONYMOUS_READ) {
- return true;
- }
- if (!$this->auth()) {
- return false;
- }
- $restrict = $this->users[$this->user]['restrict'] ?? [];
- if (!is_array($restrict) || empty($restrict)) {
- return true;
- }
- foreach ($restrict as $match) {
- if (0 === strpos($uri, $match)) {
- return true;
- }
- }
- return false;
- }
- public function canWrite(string $uri): bool
- {
- if (!$this->auth() && !ANONYMOUS_WRITE) {
- return false;
- }
- if (!$this->canRead($uri)) {
- return false;
- }
- if (ANONYMOUS_WRITE) {
- return true;
- }
- if (!$this->auth() || empty($this->users[$this->user]['write'])) {
- return false;
- }
- $restrict = $this->users[$this->user]['restrict_write'] ?? [];
- if (!is_array($restrict) || empty($restrict)) {
- return true;
- }
- foreach ($restrict as $match) {
- if (0 === strpos($uri, $match)) {
- return true;
- }
- }
- return false;
- }
- public function canOnlyCreate(string $uri): bool
- {
- $restrict = $this->users[$this->user]['restrict_write'] ?? [];
- if (in_array($uri, $restrict, true)) {
- return true;
- }
- $restrict = $this->users[$this->user]['restrict'] ?? [];
- if (in_array($uri, $restrict, true)) {
- return true;
- }
- return false;
- }
- public function list(string $uri, ?array $properties): iterable
- {
- if (!$this->canRead($uri . '/')) {
-
- }
- $dirs = self::glob($this->path . $uri, '/*', \GLOB_ONLYDIR);
- $dirs = array_map('basename', $dirs);
- $dirs = array_filter($dirs, fn($a) => $this->canRead(ltrim($uri . '/' . $a, '/') . '/'));
- natcasesort($dirs);
- $files = self::glob($this->path . $uri, '/*');
- $files = array_map('basename', $files);
- $files = array_diff($files, $dirs);
-
- $files = array_filter($files, fn($a) => $this->canRead(ltrim($uri . '/' . $a, '/')));
- natcasesort($files);
- $files = array_flip(array_merge($dirs, $files));
- $files = array_map(fn($a) => null, $files);
- return $files;
- }
- public function get(string $uri): ?array
- {
- if (!$this->canRead($uri)) {
- throw new WebDAV_Exception('Access forbidden', 403);
- }
- $path = $this->path . $uri;
- if (!file_exists($path)) {
- return null;
- }
- return ['path' => $path];
- }
- public function exists(string $uri): bool
- {
- return file_exists($this->path . $uri);
- }
- public function get_file_property(string $uri, string $name, int $depth)
- {
- $target = $this->path . $uri;
- switch ($name) {
- case 'DAV::displayname':
- return basename($uri);
- case 'DAV::getcontentlength':
- return is_dir($target) ? null : filesize($target);
- case 'DAV::getcontenttype':
-
-
- return is_dir($target) ? null : mime_content_type($target);
- case 'DAV::resourcetype':
- return is_dir($target) ? 'collection' : '';
- case 'DAV::getlastmodified':
- if (!$uri && $depth == 0 && is_dir($target)) {
- $mtime = self::getDirectoryMTime($target);
- }
- else {
- $mtime = filemtime($target);
- }
- if (!$mtime) {
- return null;
- }
- return new \DateTime('@' . $mtime);
- case 'DAV::ishidden':
- return basename($target)[0] == '.';
- case 'DAV::getetag':
- $hash = filemtime($target) . filesize($target);
- return md5($hash . $target);
- case 'DAV::lastaccessed':
- return new \DateTime('@' . fileatime($target));
- case 'DAV::creationdate':
- return new \DateTime('@' . filectime($target));
- case 'http://owncloud.org/ns:permissions':
- $permissions = 'G';
- if (is_dir($target)) {
- $uri .= '/';
- }
- if (is_writeable($target) && $this->canWrite($uri)) {
-
-
- if ($this->canOnlyCreate($uri)) {
- $permissions .= 'CK';
- }
- else {
- $permissions .= 'DNVWCK';
- }
- }
- return $permissions;
- case Server::PROP_DIGEST_MD5:
- if (!is_file($target) || is_dir($target) || !is_readable($target)) {
- return null;
- }
- return md5_file($target);
- default:
- break;
- }
- return null;
- }
- public function properties(string $uri, ?array $properties, int $depth): ?array
- {
- $target = $this->path . $uri;
- if (!file_exists($target)) {
- return null;
- }
- if (null === $properties) {
- $properties = Server::BASIC_PROPERTIES;
- }
- $out = [];
- foreach ($properties as $name) {
- $v = $this->get_file_property($uri, $name, $depth);
- if (null !== $v) {
- $out[$name] = $v;
- }
- }
- return $out;
- }
- public function put(string $uri, $pointer, ?string $hash_algo, ?string $hash, ?int $mtime): bool
- {
- if (preg_match(self::PUT_IGNORE_PATTERN, basename($uri))) {
- return false;
- }
- if (!$this->canWrite($uri)) {
- throw new WebDAV_Exception('Access forbidden', 403);
- }
- $target = $this->path . $uri;
- $parent = dirname($target);
- if (is_dir($target)) {
- throw new WebDAV_Exception('Target is a directory', 409);
- }
- if (!file_exists($parent)) {
- mkdir($parent, 0770, true);
- }
- $new = !file_exists($target);
- $delete = false;
- $size = 0;
- $quota = disk_free_space($this->path);
- $tmp_file = $this->path . '.tmp.' . sha1($target);
- $out = fopen($tmp_file, 'w');
- while (!feof($pointer)) {
- $bytes = fread($pointer, 8192);
- $size += strlen($bytes);
- if ($size > $quota) {
- $delete = true;
- break;
- }
- fwrite($out, $bytes);
- }
- fclose($out);
- fclose($pointer);
- if ($delete) {
- @unlink($tmp_file);
- throw new WebDAV_Exception('Your quota is exhausted', 507);
- }
- elseif ($hash && $hash_algo == 'MD5' && md5_file($tmp_file) != $hash) {
- @unlink($tmp_file);
- throw new WebDAV_Exception('The data sent does not match the supplied MD5 hash', 400);
- }
- elseif ($hash && $hash_algo == 'SHA1' && sha1_file($tmp_file) != $hash) {
- @unlink($tmp_file);
- throw new WebDAV_Exception('The data sent does not match the supplied SHA1 hash', 400);
- }
- else {
- rename($tmp_file, $target);
- }
- if ($mtime) {
- @touch($target, $mtime);
- }
- return $new;
- }
- public function delete(string $uri): void
- {
- if (!$this->canWrite($uri)) {
- throw new WebDAV_Exception('Access forbidden', 403);
- }
- if ($this->canOnlyCreate($uri)) {
- throw new WebDAV_Exception('Access forbidden', 403);
- }
- $target = $this->path . $uri;
- if (!file_exists($target)) {
- throw new WebDAV_Exception('Target does not exist', 404);
- }
- if (!is_writeable($target)) {
- throw new WebDAV_Exception('File permissions says that you cannot delete this, sorry.', 403);
- }
- if (is_dir($target)) {
- foreach (self::glob($target, '/*') as $file) {
- $this->delete(substr($file, strlen($this->path)));
- }
- rmdir($target);
- }
- else {
- unlink($target);
- }
- }
- public function copymove(bool $move, string $uri, string $destination): bool
- {
- if (!$this->canWrite($uri)
- || !$this->canWrite($destination)
- || $this->canOnlyCreate($uri)) {
- throw new WebDAV_Exception('Access forbidden', 403);
- }
- $source = $this->path . $uri;
- $target = $this->path . $destination;
- $parent = dirname($target);
- if (!file_exists($source)) {
- throw new WebDAV_Exception('File not found', 404);
- }
- $overwritten = file_exists($target);
- if (!is_dir($parent)) {
- throw new WebDAV_Exception('Target parent directory does not exist', 409);
- }
- if (false === $move) {
- $quota = disk_free_space($this->path);
- if (filesize($source) > $quota) {
- throw new WebDAV_Exception('Your quota is exhausted', 507);
- }
- }
- if ($overwritten) {
- $this->delete($destination);
- }
- $method = $move ? 'rename' : 'copy';
- if ($method == 'copy' && is_dir($source)) {
- @mkdir($target, 0770, true);
- foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST) as $item)
- {
- if ($item->isDir()) {
- @mkdir($target . DIRECTORY_SEPARATOR . $iterator->getSubPathname());
- } else {
- copy($item, $target . DIRECTORY_SEPARATOR . $iterator->getSubPathname());
- }
- }
- }
- else {
- $method($source, $target);
- $this->getResourceProperties($uri)->move($destination);
- }
- return $overwritten;
- }
- public function copy(string $uri, string $destination): bool
- {
- return $this->copymove(false, $uri, $destination);
- }
- public function move(string $uri, string $destination): bool
- {
- return $this->copymove(true, $uri, $destination);
- }
- public function mkcol(string $uri): void
- {
- if (!$this->canWrite($uri)) {
- throw new WebDAV_Exception('Access forbidden', 403);
- }
- if (!disk_free_space($this->path)) {
- throw new WebDAV_Exception('Your quota is exhausted', 507);
- }
- $target = $this->path . $uri;
- $parent = dirname($target);
- if (file_exists($target)) {
- throw new WebDAV_Exception('There is already a file with that name', 405);
- }
- if (!file_exists($parent)) {
- throw new WebDAV_Exception('The parent directory does not exist', 409);
- }
- mkdir($target, 0770);
- }
- static public function getDirectoryMTime(string $path): int
- {
- $last = 0;
- $path = rtrim($path, '/');
- foreach (self::glob($path, '/*', GLOB_NOSORT) as $f) {
- if (is_dir($f)) {
- $m = self::getDirectoryMTime($f);
- if ($m > $last) {
- $last = $m;
- }
- }
- $m = filemtime($f);
- if ($m > $last) {
- $last = $m;
- }
- }
- return $last;
- }
- }
- class Server extends \KD2\WebDAV\Server
- {
- protected function html_directory(string $uri, iterable $list): ?string
- {
- $out = parent::html_directory($uri, $list);
- if (null !== $out) {
- $out = str_replace('<body>', sprintf('<body style="opacity: 0"><script type="text/javascript" src="%s/.webdav/webdav.js"></script>', rtrim($this->base_uri, '/')), $out);
- }
- return $out;
- }
- public function route(?string $uri = null): bool
- {
- if (!ANONYMOUS_WRITE && !ANONYMOUS_READ && !$this->storage->auth()) {
- $this->requireAuth();
- return true;
- }
- return parent::route($uri);
- }
- protected function requireAuth(): void
- {
- http_response_code(401);
- header('WWW-Authenticate: Basic realm="Please login"');
- echo '<h2>Error 401</h2><h1>You need to login to access this.</h1>';
- }
- public function error(WebDAV_Exception $e)
- {
- if ($e->getCode() == 403 && !$this->storage->auth() && count($this->storage->users)) {
- return;
- }
- parent::error($e);
- }
- protected string $_log = '';
- public function log(string $message, ...$params): void
- {
- if (!HTTP_LOG_FILE) {
- return;
- }
- $this->_log .= vsprintf($message, $params) . "\n";
- }
- public function __destruct()
- {
- if (!$this->_log) {
- return;
- }
- file_put_contents(HTTP_LOG_FILE, $this->_log, \FILE_APPEND);
- }
- }
- }
- namespace {
- use PicoDAV\Server;
- use PicoDAV\Storage;
- $uri = strtok($_SERVER['REQUEST_URI'], '?');
- $self = $_SERVER['SCRIPT_FILENAME'];
- $self_dir = dirname($self);
- $root = substr(dirname($_SERVER['SCRIPT_FILENAME']), strlen($_SERVER['DOCUMENT_ROOT']));
- $root = '/' . ltrim($root, '/');
- if (false !== strpos($uri, '..')) {
- http_response_code(404);
- die('Invalid URL');
- }
- $relative_uri = ltrim(substr($uri, strlen($root)), '/');
- if (!empty($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'apache') && !file_exists($self_dir . '/.htaccess')) {
- file_put_contents($self_dir . '/.htaccess', str_replace('index.php', basename($self), 'DirectoryIndex disabled
- RedirectMatch 404 \\.picodav\\.ini
- RewriteEngine On
- RewriteBase /
- # Uncomment the following 2 lignes to make things a bit faster for
- # downloading files, AND you don\'t use PicoDAV users to manage access,
- # but a regular .htpasswd file and config for your web server.
- #RewriteCond %{REQUEST_FILENAME} !-f [OR]
- #RewriteCond %{REQUEST_METHOD} !GET
- RewriteRule ^.*$ /index.php [END]
- '));
- }
- if ($relative_uri == '.webdav/webdav.js' || $relative_uri == '.webdav/webdav.css') {
- http_response_code(200);
- if ($relative_uri == '.webdav/webdav.js') {
- header('Content-Type: text/javascript', true);
- }
- else {
- header('Content-Type: text/css', true);
- }
- $seconds_to_cache = 3600 * 24 * 5;
- $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
- header("Expires: " . $ts);
- header("Pragma: cache");
- header("Cache-Control: max-age=" . $seconds_to_cache);
- $fp = fopen(__FILE__, 'r');
- if ($relative_uri == '.webdav/webdav.js') {
- fseek($fp, 52608, SEEK_SET);
- echo fread($fp, 27789);
- }
- else {
- fseek($fp, 52608 + 27789, SEEK_SET);
- echo fread($fp, 7004);
- }
- fclose($fp);
- exit;
- }
- $config_file = $self_dir . '/.picodav.ini';
- define('PicoDAV\INTERNAL_FILES', ['.picodav.ini', $self_dir, '.webdav/webdav.js', '.webdav/webdav.css']);
- const DEFAULT_CONFIG = [
- 'ANONYMOUS_READ' => true,
- 'ANONYMOUS_WRITE' => false,
- 'HTTP_LOG_FILE' => null,
- ];
- $config = [];
- $storage = new Storage($self_dir);
- if (file_exists($config_file)) {
- $config = parse_ini_file($config_file, true);
- $users = array_filter($config, 'is_array');
- $config = array_diff_key($config, $users);
- $config = array_change_key_case($config, \CASE_UPPER);
- $replace = [];
-
- foreach ($users as $name => $properties) {
- if (isset($properties['password']) && substr($properties['password'], 0, 1) != '$') {
- $users[$name]['password'] = $replace[$name] = password_hash($properties['password'], null);
- }
- }
- if (count($replace)) {
- $lines = file($config_file);
- $current = null;
- foreach ($lines as &$line) {
- if (preg_match('/^\s*\[(\w+)\]\s*$/', $line, $match)) {
- $current = $match[1];
- continue;
- }
- if ($current && isset($replace[$current]) && preg_match('/^\s*password\s*=/', $line)) {
- $line = sprintf("password = %s\n", var_export($replace[$current], true));
- }
- }
- unset($line, $current);
- file_put_contents($config_file, implode('', $lines));
- }
- $storage->users = $users;
- }
- foreach (DEFAULT_CONFIG as $key => $value) {
- if (array_key_exists($key, $config)) {
- $value = $config[$key];
- }
- if (is_bool(DEFAULT_CONFIG[$key])) {
- $value = boolval($value);
- }
- define('PicoDAV\\' . $key, $value);
- }
- $dav = new Server();
- $dav->setStorage($storage);
- $dav->setBaseURI($root);
- if (!$dav->route($uri)) {
- http_response_code(404);
- die('Unknown URL, sorry.');
- }
- exit;
- ?>
- var css_url = document.currentScript.src.replace(/\/[^\/]+$/, '') + '/webdav.css';
- const WebDAVNavigator = (url, options) => {
-
-
- const microdown=function(){function l(n,e,r){return"<"+n+(r?" "+Object.keys(r).map(function(n){return r[n]?n+'="'+(a(r[n])||"")+'"':""}).join(" "):"")+">"+e+"</"+n+">"}function c(n,e){return e=n.match(/^[+-]/m)?"ul":"ol",n?"<"+e+">"+n.replace(/(?:[+-]|\d+\.) +(.*)\n?(([ \t].*\n?)*)/g,function(n,e,r){return"<li>"+g(e+"\n"+(t=r||"").replace(new RegExp("^"+(t.match(/^\s+/)||"")[0],"gm"),"").replace(o,c))+"</li>";var t})+"</"+e+">":""}function e(r,t,u,c){return function(n,e){return n=n.replace(t,u),l(r,c?c(n):n)}}function t(n,u){return f(n,[/<!--((.|\n)*?)-->/g,"\x3c!--$1--\x3e",/^("""|```)(.*)\n((.*\n)*?)\1/gm,function(n,e,r,t){return'"""'===e?l("div",p(t,u),{class:r}):u&&u.preCode?l("pre",l("code",a(t),{class:r})):l("pre",a(t),{class:r})},/(^>.*\n?)+/gm,e("blockquote",/^> ?(.*)$/gm,"$1",r),/((^|\n)\|.+)+/g,e("table",/^.*(\n\|---.*?)?$/gm,function(n,t){return e("tr",/\|(-?)([^|]*)\1(\|$)?/gm,function(n,e,r){return l(e||t?"th":"td",g(r))})(n.slice(0,n.length-(t||"").length))}),o,c,/#\[([^\]]+?)]/g,'<a name="$1"></a>',/^(#+) +(.*)(?:$)/gm,function(n,e,r){return l("h"+e.length,g(r))},/^(===+|---+)(?=\s*$)/gm,"<hr>"],p,u)}var i=this,a=function(n){return n?n.replace(/"/g,""").replace(/</g,"<").replace(/>/g,">"):""},o=/(?:(^|\n)([+-]|\d+\.) +(.*(\n[ \t]+.*)*))+/g,g=function c(n,i){var o=[];return n=(n||"").trim().replace(/`([^`]*)`/g,function(n,e){return"\\"+o.push(l("code",a(e)))}).replace(/[!&]?\[([!&]?\[.*?\)|[^\]]*?)]\((.*?)( .*?)?\)|(\w+:\/\/[$\-.+!*'()/,\w]+)/g,function(n,e,r,t,u){return u?i?n:"\\"+o.push(l("a",u,{href:u})):"&"==n[0]?(e=e.match(/^(.+),(.+),([^ \]]+)( ?.+?)?$/),"\\"+o.push(l("iframe","",{width:e[1],height:e[2],frameborder:e[3],class:e[4],src:r,title:t}))):"\\"+o.push("!"==n[0]?l("img","",{src:r,alt:e,title:t}):l("a",c(e,1),{href:r,title:t}))}),n=function r(n){return n.replace(/\\(\d+)/g,function(n,e){return r(o[Number.parseInt(e)-1])})}(i?n:r(n))},r=function t(n){return f(n,[/([*_]{1,3})((.|\n)+?)\1/g,function(n,e,r){return e=e.length,r=t(r),1<e&&(r=l("strong",r)),e%2&&(r=l("em",r)),r},/(~{1,3})((.|\n)+?)\1/g,function(n,e,r){return l([,"u","s","del"][e.length],t(r))},/ \n|\n /g,"<br>"],t)},f=function(n,e,r,t){for(var u,c=0;c<e.length;){if(u=e[c++].exec(n))return r(n.slice(0,u.index),t)+("string"==typeof e[c]?e[c].replace(/\$(\d)/g,function(n,e){return u[e]}):e[c].apply(i,u))+r(n.slice(u.index+u[0].length),t);c++}return n},p=function(n,e){n=n.replace(/[\r\v\b\f]/g,"").replace(/\\./g,function(n){return"&#"+n.charCodeAt(1)+";"});var r=t(n,e);return r!==n||r.match(/^[\s\n]*$/i)||(r=g(r).replace(/((.|\n)+?)(\n\n+|$)/g,function(n,e){return l("p",e)})),r.replace(/&
- const PREVIEW_TYPES = /^image\/(png|webp|svg|jpeg|jpg|gif|png)|^application\/pdf|^text\/|^audio\/|^video\/|application\/x-empty/;
- const _ = key => typeof lang_strings != 'undefined' && key in lang_strings ? lang_strings[key] : key;
- const rename_button = `<input class="rename" type="button" value="${_('Rename')}" />`;
- const delete_button = `<input class="delete" type="button" value="${_('Delete')}" />`;
- const edit_button = `<input class="edit" type="button" value="${_('Edit')}" />`;
- const mkdir_dialog = `<input type="text" name="mkdir" placeholder="${_('Directory name')}" />`;
- const mkfile_dialog = `<input type="text" name="mkfile" placeholder="${_('File name')}" />`;
- const rename_dialog = `<input type="text" name="rename" placeholder="${_('New file name')}" />`;
- const paste_upload_dialog = `<h3>Upload this file?</h3><input type="text" name="paste_name" placeholder="${_('New file name')}" />`;
- const edit_dialog = `<textarea name="edit" cols="70" rows="30"></textarea>`;
- const markdown_dialog = `<div id="mdp"><textarea name="edit" cols="70" rows="30"></textarea><div id="md"></div></div>`;
- const delete_dialog = `<h3>${_('Confirm delete?')}</h3>`;
- const wopi_dialog = `<iframe id="wopi_frame" name="wopi_frame" allowfullscreen="true" allow="autoplay camera microphone display-capture"
- sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-top-navigation allow-popups-to-escape-sandbox allow-downloads allow-modals">
- </iframe>`;
- const dialog_tpl = `<dialog open><p class="close"><input type="button" value="&
- const html_tpl = `<!DOCTYPE html><html>
- <head><title>Files</title><link rel="stylesheet" type="text/css" href="${css_url}" /></head>
- <body><main></main><div class="bg"></div></body></html>`;
- const body_tpl = `<h1>%title%</h1>
- <div class="upload">
- <select class="sortorder btn">
- <option value="name">${_('Sort by name')}</option>
- <option value="date">${_('Sort by date')}</option>
- <option value="size">${_('Sort by size')}</option>
- </select>
- <input type="button" class="download_all" value="${_('Download all files')}" />
- </div>
- <table>%table%</table>`;
- const create_buttons = `<input class="mkdir" type="button" value="${_('New directory')}" />
- <input type="file" style="display: none;" />
- <input class="mkfile" type="button" value="${_('New text file')}" />
- <input class="uploadfile" type="button" value="${_('Upload file')}" />`;
- const dir_row_tpl = `<tr data-permissions="%permissions%"><td class="thumb"><span class="icon dir"><b>%icon%</b></span></td><th colspan="2"><a href="%uri%">%name%</a></th><td>%modified%</td><td class="buttons"><div></div></td></tr>`;
- const file_row_tpl = `<tr data-permissions="%permissions%" data-mime="%mime%" data-size="%size%"><td class="thumb"><span class="icon %icon%"><b>%icon%</b></span></td><th><a href="%uri%">%name%</a></th><td class="size">%size_bytes%</td><td>%modified%</td><td class="buttons"><div><a href="%uri%" download class="btn">${_('Download')}</a></div></td></tr>`;
- const propfind_tpl = `<?xml version="1.0" encoding="UTF-8"?>
- <D:propfind xmlns:D="DAV:" xmlns:oc="http:
- <D:prop>
- <D:getlastmodified/><D:getcontenttype/><D:getcontentlength/><D:resourcetype/><D:displayname/><oc:permissions/>
- </D:prop>
- </D:propfind>`;
- const wopi_propfind_tpl = `<?xml version="1.0" encoding="UTF-8"?>
- <D:propfind xmlns:D="DAV:" xmlns:W="https://interoperability.blob.core.windows.net/files/MS-WOPI/">
- <D:prop>
- <W:file-url/><W:token/><W:token-ttl/>
- </D:prop>
- </D:propfind>`;
- const html = (unsafe) => {
- return unsafe.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "&
- };
- const reqXML = (method, url, body, headers) => {
- return req(method, url, body, headers).then((r) => {
- if (!r.ok) {
- throw new Error(r.status + ' ' + r.statusText);
- }
- return r.text();
- }).then(str => new window.DOMParser().parseFromString(str, "text/xml"));
- };
- const reqAndReload = (method, url, body, headers) => {
- animateLoading();
- req(method, url, body, headers).then(r => {
- stopLoading();
- if (!r.ok) {
- return r.text().then(t => {
- var message;
- if (a = t.match(/<((?:\w+:)?message)>(.*)<\/\1>/)) {
- message = "\n" + a[2];
- }
- throw new Error(r.status + ' ' + r.statusText + message); });
- }
- reloadListing();
- }).catch(e => {
- console.error(e);
- alert(e);
- });
- return false;
- };
- const req = (method, url, body, headers) => {
- if (!headers) {
- headers = {};
- }
- if (auth_header) {
- headers.Authorization = auth_header;
- }
- return fetch(url, {method, body, headers});
- };
- const xhr = (method, url, progress_callback) => {
- var xhr = new XMLHttpRequest();
- current_xhr = xhr;
- xhr.responseType = 'blob';
- var p = new Promise((resolve, reject) => {
- xhr.open(method, url);
- xhr.onload = function () {
- if (this.status >= 200 && this.status < 300) {
- resolve(xhr.response);
- } else {
- reject({
- status: this.status,
- statusText: xhr.statusText
- });
- }
- };
- xhr.onerror = function () {
- reject({
- status: this.status,
- statusText: xhr.statusText
- });
- };
- xhr.onprogress = progress_callback;
- xhr.send();
- });
- return p;
- };
- const get_url = async (url) => {
- var progress = (e) => {
- var p = $('progress');
- if (!p || e.loaded <= 0) return;
- p.value = e.loaded;
- $('.progress_bytes').innerHTML = formatBytes(e.loaded);
- };
- if (temp_object_url) {
- window.URL.revokeObjectURL(temp_object_url);
- }
- return await xhr('GET', url, progress).then(blob => {
- temp_object_url = window.URL.createObjectURL(blob);
- return temp_object_url;
- });
- };
- const wopi_init = async () => {
- if (!wopi_discovery_url) {
- return;
- }
- var d = await reqXML('GET', wopi_discovery_url);
- d.querySelectorAll('app').forEach(app => {
- var mime = (a = app.getAttribute('name').match(/^.*\/.*$/)) ? a[0] : null;
- wopi_mimes[mime] = {};
- app.querySelectorAll('action').forEach(action => {
- var ext = action.getAttribute('ext').toUpperCase();
- var url = action.getAttribute('urlsrc').replace(/<[^>]*&>/g, '');
- var name = action.getAttribute('name');
- if (mime) {
- wopi_mimes[mime][name] = url;
- }
- else {
- if (!wopi_extensions.hasOwnProperty(ext)) {
- wopi_extensions[ext] = {};
- }
- wopi_extensions[ext][name] = url;
- }
- });
- });
- reloadListing();
- };
- const wopi_getEditURL = (name, mime) => {
- var file_ext = name.replace(/^.*\.(\w+)$/, '$1').toUpperCase();
- if (wopi_mimes.hasOwnProperty(mime) && wopi_mimes[mime].hasOwnProperty('edit')) {
- return wopi_mimes[mime].edit;
- }
- else if (wopi_extensions.hasOwnProperty(file_ext) && wopi_extensions[file_ext].hasOwnProperty('edit')) {
- return wopi_extensions[file_ext].edit;
- }
- return null;
- };
- const wopi_getViewURL = (name, mime) => {
- var file_ext = name.replace(/^.*\.(\w+)$/, '$1').toUpperCase();
- if (wopi_mimes.hasOwnProperty(mime) && wopi_mimes[mime].hasOwnProperty('view')) {
- return wopi_mimes[mime].view;
- }
- else if (wopi_extensions.hasOwnProperty(file_ext) && wopi_extensions[file_ext].hasOwnProperty('view')) {
- return wopi_extensions[file_ext].view;
- }
- return wopi_getEditURL(name, mime);
- };
- const wopi_open = async (document_url, wopi_url) => {
- var properties = await reqXML('PROPFIND', document_url, wopi_propfind_tpl, {'Depth': '0'});
- var src = (a = properties.querySelector('file-url')) ? a.textContent : null;
- var token = (a = properties.querySelector('token')) ? a.textContent : null;
- var token_ttl = (a = properties.querySelector('token-ttl')) ? a.textContent : +(new Date(Date.now() + 3600 * 1000));
- if (!src || !token) {
- alert('Cannot open document: WebDAV server did not return WOPI properties');
- }
- wopi_url += '&WOPISrc=' + encodeURIComponent(src);
- openDialog(wopi_dialog, false);
- $('dialog').className = 'preview';
- var f = $('dialog form');
- f.target = 'wopi_frame';
- f.action = wopi_url;
- f.method = 'post';
- f.insertAdjacentHTML('beforeend', `<input name="access_token" value="${token}" type="hidden" /><input name="access_token_ttl" value="${token_ttl}" type="hidden" />`);
- f.submit();
- };
- const template = (tpl, params) => {
- return tpl.replace(/%(\w+)%/g, (a, b) => {
- return params[b];
- });
- };
- const openDialog = (html, ok_btn = true) => {
- var tpl = dialog_tpl.replace(/%b/, ok_btn ? `<p><input type="submit" value="${_('OK')}" /></p>` : '');
- $('body').classList.add('dialog');
- $('body').insertAdjacentHTML('beforeend', tpl.replace(/%s/, html));
- $('.close input').onclick = closeDialog;
- evt = window.addEventListener('keyup', (e) => {
- if (e.key != 'Escape') return;
- closeDialog();
- return false;
- });
- if (a = $('dialog form input, dialog form textarea')) a.focus();
- };
- const closeDialog = (e) => {
- if (!$('body').classList.contains('dialog')) {
- return;
- }
- if (current_xhr) {
- current_xhr.abort();
- current_xhr = null;
- }
- window.onbeforeunload = null;
- $('body').classList.remove('dialog');
- if (!$('dialog')) return;
- $('dialog').remove();
- window.removeEventListener('keyup', evt);
- evt = null;
- };
- const download = async (name, size, url) => {
- window.onbeforeunload = () => {
- if (current_xhr) {
- current_xhr.abort();
- }
- return true;
- };
- openDialog(`<p class="spinner"><span></span></p>
- <h3>${html(name)}</h3>
- <progress max="${size}"></progress>
- <p><span class="progress_bytes"></span> / ${formatBytes(size)}</p>`, false);
- await get_url(url);
- const a = document.createElement('a');
- a.style.display = 'none';
- a.href = temp_object_url;
- a.download = name;
- document.body.appendChild(a);
- a.click();
- window.URL.revokeObjectURL(temp_object_url);
- a.remove();
- closeDialog();
- window.onbeforeunload = null;
- };
- const download_all = async () => {
- for (var i = 0; i < items.length; i++) {
- var item = items[i];
- if (item.is_dir) {
- continue;
- }
- await download(item.name, item.size, item.uri)
- }
- };
- const preview = (type, url) => {
- if (type.match(/^image\
- openDialog(`<img src="${url}" />`, false);
- }
- else if (type.match(/^audio\
- openDialog(`<audio controls="true" autoplay="true" src="${url}" />`, false);
- }
- else if (type.match(/^video\
- openDialog(`<video controls="true" autoplay="true" src="${url}" />`, false);
- }
- else {
- openDialog(`<iframe src="${url}" />`, false);
- }
- $('dialog').className = 'preview';
- };
- const $ = (a) => document.querySelector(a);
- const formatBytes = (bytes) => {
- const unit = _('B');
- if (bytes >= 1024*1024*1024) {
- return Math.round(bytes / (1024*1024*1024)) + ' G' + unit;
- }
- else if (bytes >= 1024*1024) {
- return Math.round(bytes / (1024*1024)) + ' M' + unit;
- }
- else if (bytes >= 1024) {
- return Math.round(bytes / 1024) + ' K' + unit;
- }
- else {
- return bytes + ' ' + unit;
- }
- };
- const formatDate = (date) => {
- var now = new Date;
- var nb_hours = (+(now) - +(date)) / 3600 / 1000;
- if (date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth() && date.getDate() == now.getDate()) {
- if (nb_hours <= 1) {
- return _('%d minutes ago').replace(/%d/, Math.round(nb_hours * 60));
- }
- else {
- return _('%d hours ago').replace(/%d/, Math.round(nb_hours));
- }
- }
- else if (nb_hours <= 24) {
- return _('Yesterday, %s').replace(/%s/, date.toLocaleTimeString());
- }
- return date.toLocaleString();
- };
- const openListing = (uri, push) => {
- closeDialog();
- reqXML('PROPFIND', uri, propfind_tpl, {'Depth': 1}).then((xml) => {
- buildListing(uri, xml)
- current_url = uri;
- changeURL(uri, push);
- }).catch((e) => {
- console.error(e);
- alert(e);
- });
- };
- const reloadListing = () => {
- stopLoading();
- openListing(current_url, false);
- };
- const normalizeURL = (url) => {
- if (!url.match(/^https?:\/\
- url = base_url.replace(/^(https?:\/\/[^\/]+\/).*$/, '$1') + url.replace(/^\/+/, '');
- }
- return url;
- };
- const changeURL = (uri, push) => {
- try {
- if (push) {
- history.pushState(1, null, uri);
- }
- else {
- history.replaceState(1, null, uri);
- }
- if (popstate_evt) return;
- popstate_evt = window.addEventListener('popstate', (e) => {
- var url = location.pathname;
- openListing(url, false);
- });
- }
- catch (e) {
-
- location.hash = uri;
- }
- };
- const animateLoading = () => {
- document.body.classList.add('loading');
- };
- const stopLoading = () => {
- document.body.classList.remove('loading');
- };
- const buildListing = (uri, xml) => {
- uri = normalizeURL(uri);
- items = [[], []];
- var title = null;
- var root_permissions = null;
- xml.querySelectorAll('response').forEach((node) => {
- var item_uri = normalizeURL(node.querySelector('href').textContent);
- var props = null;
- node.querySelectorAll('propstat').forEach((propstat) => {
- if (propstat.querySelector('status').textContent.match(/200/)) {
- props = propstat;
- }
- });
-
- if (!props) {
- console.error('Cannot find properties for: ' + item_uri);
- return;
- }
- var name = item_uri.replace(/\/$/, '').split('/').pop();
- name = decodeURIComponent(name);
- var permissions = (prop = node.querySelector('permissions')) ? prop.textContent : null;
- if (item_uri == uri) {
- title = name;
- root_permissions = permissions;
- return;
- }
- var is_dir = node.querySelector('resourcetype collection') ? true : false;
- var index = sort_order == 'name' && is_dir ? 0 : 1;
- items[index].push({
- 'uri': item_uri,
- 'name': name,
- 'size': !is_dir && (prop = node.querySelector('getcontentlength')) ? parseInt(prop.textContent, 10) : null,
- 'mime': !is_dir && (prop = node.querySelector('getcontenttype')) ? prop.textContent : null,
- 'modified': (prop = node.querySelector('getlastmodified')) ? new Date(prop.textContent) : null,
- 'is_dir': is_dir,
- 'permissions': permissions,
- });
- });
- if (sort_order == 'name') {
- items[0].sort((a, b) => a.name.localeCompare(b.name));
- }
- items[1].sort((a, b) => {
- if (sort_order == 'date') {
- return b.modified - a.modified;
- }
- else if (sort_order == 'size') {
- return b.size - a.size;
- }
- else {
- return a.name.localeCompare(b.name);
- }
- });
- if (sort_order == 'name') {
-
- items = items[0].concat(items[1]);
- }
- else {
- items = items[1];
- }
- var table = '';
- var parent = uri.replace(/\/+$/, '').split('/').slice(0, -1).join('/') + '/';
- if (parent.length >= base_url.length) {
- table += template(dir_row_tpl, {'name': _('Back'), 'uri': parent, 'icon': '↲'});
- }
- else {
- title = 'My files';
- }
- items.forEach(item => {
-
- if (item.permissions !== null && item.permissions.indexOf('G') == -1) {
- console.error('OC permissions deny read access to this file: ' + item.name, 'Permissions: ', item.permissions);
- return;
- }
- var row = item.is_dir ? dir_row_tpl : file_row_tpl;
- item.size_bytes = item.size !== null ? formatBytes(item.size).replace(/ /g, ' ') : null;
- item.icon = item.is_dir ? '📁' : (item.uri.indexOf('.') > 0 ? item.uri.replace(/^.*\.(\w+)$/, '$1').toUpperCase() : '');
- item.modified = item.modified !== null ? formatDate(item.modified) : null;
- item.name = html(item.name);
- table += template(row, item);
- });
- document.title = title;
- document.querySelector('main').innerHTML = template(body_tpl, {'title': html(document.title), 'base_url': base_url, 'table': table});
- var select = $('.sortorder');
- select.value = sort_order;
- select.onchange = () => {
- sort_order = select.value;
- window.localStorage.setItem('sort_order', sort_order);
- reloadListing();
- };
- if (!items.length) {
- $('.download_all').disabled = true;
- }
- else {
- $('.download_all').onclick = download_all;
- }
- if (!root_permissions || root_permissions.indexOf('CK') != -1) {
- $('.upload').insertAdjacentHTML('afterbegin', create_buttons);
- $('.mkdir').onclick = () => {
- openDialog(mkdir_dialog);
- document.forms[0].onsubmit = () => {
- var name = $('input[name=mkdir]').value;
- if (!name) return false;
- name = encodeURIComponent(name);
- req('MKCOL', current_url + name).then(() => openListing(current_url + name + '/'));
- return false;
- };
- };
- $('.mkfile').onclick = () => {
- openDialog(mkfile_dialog);
- var t = $('input[name=mkfile]');
- t.value = '.md';
- t.focus();
- t.selectionStart = t.selectionEnd = 0;
- document.forms[0].onsubmit = () => {
- var name = t.value;
- if (!name) return false;
- name = encodeURIComponent(name);
- return reqAndReload('PUT', current_url + name, '');
- };
- };
- var fi = $('input[type=file]');
- $('.uploadfile').onclick = () => fi.click();
- fi.onchange = () => {
- if (!fi.files.length) return;
- var body = new Blob(fi.files);
- var name = fi.files[0].name;
- name = encodeURIComponent(name);
- return reqAndReload('PUT', current_url + name, body);
- };
- }
- Array.from($('table').rows).forEach((tr) => {
- var $$ = (a) => tr.querySelector(a);
- var file_url = $$('a').href;
- var file_name = $$('a').innerText;
- var dir = $$('[colspan]');
- var mime = !dir ? tr.getAttribute('data-mime') : 'dir';
- var buttons = $$('td.buttons div');
- var permissions = tr.getAttribute('data-permissions');
- var size = tr.getAttribute('data-size');
- if (permissions == 'null') {
- permissions = null;
- }
- if (dir) {
- $$('a').onclick = () => {
- openListing(file_url, true);
- return false;
- };
- }
-
- if (dir && $$('a').getAttribute('href').length < uri.length) {
- dir.setAttribute('colspan', 4);
- tr.querySelector('td:last-child').remove();
- tr.querySelector('td:last-child').remove();
- return;
- }
-
- if (user && password && (a = tr.querySelector('a[download]'))) {
- a.onclick = () => {
- download(file_name, size, url);
- return false;
- };
- }
-
- if (!permissions || permissions.indexOf('NV') != -1) {
- buttons.insertAdjacentHTML('afterbegin', rename_button);
- $$('.rename').onclick = () => {
- openDialog(rename_dialog);
- let t = $('input[name=rename]');
- t.value = file_name;
- t.focus();
- t.selectionStart = 0;
- t.selectionEnd = file_name.lastIndexOf('.');
- document.forms[0].onsubmit = () => {
- var name = t.value;
- if (!name) return false;
- name = encodeURIComponent(name);
- name = name.replace(/%2F/, '/');
- var dest = current_url + name;
- dest = normalizeURL(dest);
- return reqAndReload('MOVE', file_url, '', {'Destination': dest});
- };
- };
- }
- if (!permissions || permissions.indexOf('D') != -1) {
- buttons.insertAdjacentHTML('afterbegin', delete_button);
- $$('.delete').onclick = (e) => {
- openDialog(delete_dialog);
- document.forms[0].onsubmit = () => {
- return reqAndReload('DELETE', file_url);
- };
- };
- }
- var view_url, edit_url;
-
- if (mime.match(PREVIEW_TYPES)
- && !(mime == 'application/pdf' && window.navigator.userAgent.match(/Mobi|Tablet|Android|iPad|iPhone/))) {
- $$('a').onclick = () => {
- if (file_url.match(/\.md$/)) {
- openDialog('<div class="md_preview"></div>', false);
- $('dialog').className = 'preview';
- req('GET', file_url).then(r => r.text()).then(t => {
- $('.md_preview').innerHTML = microdown.parse(html(t));
- });
- return false;
- }
- if (user && password) {
- (async () => { preview(mime, await get_url(file_url)); })();
- }
- else {
- preview(mime, file_url);
- }
- return false;
- };
- }
- else if (view_url = wopi_getViewURL(file_url, mime)) {
- $$('.icon').classList.add('document');
- $$('a').onclick = () => { wopi_open(file_url, view_url); return false; };
- }
- else if (user && password && !dir) {
- $$('a').onclick = () => { download(file_name, size, file_url); return false; };
- }
- else {
- $$('a').download = file_name;
- }
- if (!permissions || permissions.indexOf('W') != -1) {
- if (mime.match(/^text\/|application\/x-empty/)) {
- buttons.insertAdjacentHTML('beforeend', edit_button);
- $$('.edit').onclick = (e) => {
- req('GET', file_url).then((r) => r.text().then((t) => {
- let md = file_url.match(/\.md$/);
- openDialog(md ? markdown_dialog : edit_dialog);
- var txt = $('textarea[name=edit]');
- txt.value = t;
-
- if (md) {
- let pre = $('#md');
- txt.oninput = () => {
- pre.innerHTML = microdown.parse(html(txt.value));
- };
- txt.oninput();
-
- txt.onscroll = (e) => {
- var p = e.target.scrollTop / (e.target.scrollHeight - e.target.offsetHeight);
- var target = e.target == pre ? txt : pre;
- target.scrollTop = p * (target.scrollHeight - target.offsetHeight);
- e.preventDefault();
- return false;
- };
- }
- document.forms[0].onsubmit = () => {
- var content = txt.value;
- return reqAndReload('PUT', file_url, content);
- };
- }));
- };
- }
- else if (edit_url = wopi_getEditURL(file_url, mime)) {
- buttons.insertAdjacentHTML('beforeend', edit_button);
- $$('.icon').classList.add('document');
- $$('.edit').onclick = () => { wopi_open(file_url, edit_url); return false; };
- }
- }
- });
- };
- var items = [[], []];
- var current_xhr = null;
- var current_url = url;
- var base_url = url;
- const user = options.user || null;
- const password = options.password || null;
- var auth_header = (user && password) ? 'Basic ' + btoa(user + ':' + password) : null;
- if (location.pathname.indexOf(base_url) === 0) {
- current_url = location.pathname;
- }
- if (!base_url.match(/^https?:/)) {
- base_url = location.href.replace(/^(https?:\/\/[^\/]+\/).*$/, '$1') + base_url.replace(/^\/+/, '');
- }
- var evt, paste_upload, popstate_evt, temp_object_url;
- var sort_order = window.localStorage.getItem('sort_order') || 'name';
- var wopi_mimes = {}, wopi_extensions = {};
- const wopi_discovery_url = options.wopi_discovery_url || null;
- document.querySelector('html').innerHTML = html_tpl;
- if (wopi_discovery_url) {
-
- wopi_init();
- }
- else {
- reloadListing();
- }
- window.addEventListener('paste', (e) => {
- let items = e.clipboardData.items;
- const IMAGE_MIME_REGEX = /^image\/(p?jpeg|gif|png)$/i;
- for (var i = 0; i < items.length; i++) {
- if (items[i].kind === 'file' || IMAGE_MIME_REGEX.test(items[i].type)) {
- e.preventDefault();
- let f = items[i].getAsFile();
- let name = f.name == 'image.png' ? f.name.replace(/\./, '-' + (+(new Date)) + '.') : f.name;
- paste_upload = f;
- openDialog(paste_upload_dialog);
- let t = $('input[name=paste_name]');
- t.value = name;
- t.focus();
- t.selectionStart = 0;
- t.selectionEnd = name.lastIndexOf('.');
- document.forms[0].onsubmit = () => {
- name = encodeURIComponent(t.value);
- return reqAndReload('PUT', current_url + name, paste_upload);
- };
- return;
- }
- }
- });
- var dragcounter = 0;
- window.addEventListener('dragover', (e) => {
- e.preventDefault();
- e.stopPropagation();
- });
- window.addEventListener('dragenter', (e) => {
- e.preventDefault();
- e.stopPropagation();
- if (!dragcounter) {
- document.body.classList.add('dragging');
- }
- dragcounter++;
- });
- window.addEventListener('dragleave', (e) => {
- e.preventDefault();
- e.stopPropagation();
- dragcounter--;
- if (!dragcounter) {
- document.body.classList.remove('dragging');
- }
- });
- window.addEventListener('drop', (e) => {
- e.preventDefault();
- e.stopPropagation();
- document.body.classList.remove('dragging');
- dragcounter = 0;
- const files = [...e.dataTransfer.items].map(item => item.getAsFile());
- if (!files.length) return;
- animateLoading();
- (async () => {
- for (var i = 0; i < files.length; i++) {
- var f = files[i]
- await req('PUT', current_url + encodeURIComponent(f.name), f);
- }
- window.setTimeout(() => {
- stopLoading();
- reloadListing();
- }, 500);
- })();
- });
- };
- if (url = document.querySelector('html').getAttribute('data-webdav-url')) {
- WebDAVNavigator(url, {
- 'wopi_discovery_url': document.querySelector('html').getAttribute('data-wopi-discovery-url'),
- });
- }
- :root {
- --bg-color:
- --fg-color:
- --g1-color:
- --g2-color:
- --g3-color:
- --link-color: blue;
- --visited-color: purple;
- --active-color: darkred;
- }
- body {
- text-align: center;
- font-size: 1.1em;
- font-family: Arial, Helvetica, sans-serif;
- background: var(--bg-color);
- color: var(--fg-color);
- }
- a:link {
- color: var(--link-color);
- }
- a:visited {
- color: var(--visited-color);
- }
- a:hover {
- color: var(--active-color);
- }
- table {
- margin: 2em auto;
- border-collapse: collapse;
- width: 90%;
- }
- th, td {
- padding: .5em;
- text-align: left;
- border: 2px solid var(--g2-color);
- }
- th {
- word-break: break-all;
- }
- td.thumb {
- width: 5%;
- }
- td.buttons {
- text-align: right;
- width: 20em;
- }
- td.buttons div {
- display: flex;
- flex-direction: row-reverse;
- }
- table tr:nth-child(even) {
- background: var(--g1-color);
- }
- .icon {
- width: 2.6em;
- height: 2.6em;
- display: block;
- border-radius: .2em;
- background:var(--g3-color);
- overflow: hidden;
- color: var(--bg-color);
- text-align: center;
- }
- .icon b {
- font-weight: normal;
- display: inline-block;
- transform: rotate(-30deg);
- line-height: 2.6rem;
- }
- .icon.JPEG, .icon.PNG, .icon.JPG, .icon.GIF, .icon.SVG, .icon.WEBP {
- background:
- }
- .icon.TXT, .icon.MD {
- background: var(--fg-color);
- }
- .icon.MP4, .icon.MKV, .icon.MP3, .icon.M4A, .icon.WAV, .icon.FLAC, .icon.OGG, .icon.OGV, .icon.AAC, .icon.WEBM {
- background:
- }
- .icon.document {
- background:
- }
- .icon.PDF {
- background:
- }
- .icon.dir {
- background: var(--g2-color);
- color: var(--fg-color);
- }
- .icon.dir b {
- font-size: 2em;
- transform: none;
- }
- .size {
- text-align: right;
- }
- input[type=button], input[type=submit], .btn {
- font-size: 1.2em;
- padding: .3em .5em;
- margin: .2em .3em;
- border: none;
- background: var(--g2-color);
- border-radius: .2em;
- cursor: pointer;
- text-decoration: none;
- color: var(--fg-color) !important;
- font-family: inherit;
- }
- td input[type=button], td input[type=submit], td .btn {
- font-size: 1em;
- }
- input[type=text], textarea {
- font-size: 1.2em;
- padding: .3em .5em;
- border: none;
- background: var(--bg-color);
- border-radius: .2em;
- width: calc(100% - 1em);
- color: var(--fg-color);
- }
- input:focus, textarea:focus {
- box-shadow: 0px 0px 5px var(--active-color);
- outline: 1px solid var(--active-color);
- }
- input[type=button]:hover, input[type=submit]:hover, .btn:hover {
- color: var(--active-color);
- text-decoration: underline;
- background: var(--bg-color);
- box-shadow: 0px 0px 5px var(--fg-color);
- }
- .close {
- text-align: right;
- margin: 0;
- }
- .close input {
- font-size: .8em;
- }
- input[type=submit] {
- float: right;
- }
- dialog {
- position: fixed;
- top: 1em;
- right: 1em;
- bottom: 1em;
- left: 1em;
- box-shadow: 0px 0px 5px var(--fg-color);
- background: var(--g1-color);
- color: var(--fg-color);
- border: none;
- border-radius: .5em;
- }
- dialog form div {
- clear: both;
- margin: 2em 0;
- text-align: center;
- }
- .upload {
- margin: 1em 0;
- }
- width: calc(100% - 1em);
- padding: .5em;
- font-size: 1em;
- height: calc(100% - 1em);
- text-align: left;
- margin: 0;
- }
- overflow: hidden;
- overflow-x: auto;
- }
- display: grid;
- grid-template-columns: 1fr 1fr;
- grid-gap: .2em;
- background: var(--g1-color);
- height: 82vh;
- }
- dialog.preview {
- height: calc(100%);
- width: calc(100%);
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- padding: 0;
- border-radius: 0;
- background: var(--g1-color);
- overflow: hidden;
- }
- iframe, .md_preview {
- overflow: auto;
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- padding: 0;
- margin: 0;
- width: 100%;
- height: 100%;
- border: none;
- }
- iframe, iframe body, .md_preview {
- background-color:
- color:
- }
- .preview form {
- height: calc(100% - 2em);
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .preview form > div {
- width: calc(100vw);
- height: 100%;
- position: relative;
- margin: 0;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .preview div video {
- max-width: 100%;
- max-height: 100%;
- }
- .md_preview {
- width: calc(100vw - 2em);
- height: calc(100vh - 2em);
- padding: 1em;
- text-align: left;
- }
- .preview .close {
- height: 2em;
- text-align: center;
- font-size: 1em;
- display: block;
- width: 100%;
- margin: 0;
- padding: 0;
- border-radius: 0;
- background: var(--g2-color);
- color: var(--fg-color);
- box-shadow: 0px 0px 5px var(--g2-color);
- }
- .preview img {
- max-width: 95%;
- max-height: 95%;
- }
- input[name=rename], input[name=paste_name] {
- width: 30em;
- }
- .bg {
- align-items: center;
- justify-content: center;
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- margin: 0;
- padding: 0;
- width: 0;
- height: 0;
- border: none;
- align-items: center;
- justify-content: center;
- opacity: 0;
- display: flex;
- }
- .loading .bg::after, .spinner span::after {
- display: block;
- content: " ";
- width: 70px;
- height: 70px;
- border: 5px solid var(--g2-color);
- border-radius: 50%;
- border-top-color: var(--fg-color);
- animation: spin 1s ease-in-out infinite;
- filter: none;
- }
- .loading .bg::before {
- display: block;
- content: " ";
- width: 70px;
- height: 70px;
- border: 20px solid var(--bg-color);
- border-radius: 50%;
- background: var(--bg-color);
- position: absolute;
- }
- .spinner {
- align-items: center;
- justify-content: center;
- display: flex;
- }
- .spinner span::after {
- width: 30px;
- height: 30px;
- }
- .loading .bg, .dragging .bg, .dialog .bg {
- backdrop-filter: blur(5px);
- background: rgba(0, 0, 0, 0.5);
- opacity: 1;
- width: 100%;
- height: 100%;
- }
- dialog {
- transition: all .3s;
- }
- progress {
- height: 2em;
- width: 90%;
- }
- @keyframes spin { to { transform: rotate(360deg); } }
- @media screen and (max-width: 800px) {
- .upload {
- display: flex;
- flex-direction: row;
- justify-content: center;
- flex-wrap: wrap;
- }
- body {
- margin: 0;
- font-size: 1em;
- }
- table {
- margin: 2em 0;
- width: 100%;
- display: flex;
- flex-direction: column;
- }
- table tr {
- display: block;
- border-top: 5px solid var(--bg-color);
- padding: 0;
- padding-left: 2em;
- position: relative;
- text-align: left;
- min-height: 2.5em;
- }
- table td, table th {
- border: none;
- display: inline-block;
- padding: .2em .5em;
- }
- table td.buttons {
- display: block;
- width: auto;
- text-align: left;
- }
- td.buttons div {
- display: inline-block;
- }
- table td.thumb {
- padding: 0;
- width: 2em;
- position: absolute;
- left: 0;
- top: 0;
- bottom: 0;
- }
- table th {
- display: block;
- }
- .icon {
- font-size: 12px;
- height: 100%;
- border-radius: 0;
- }
- .icon:not(.dir) b {
- line-height: 3em;
- display: block;
- transform: translateX(-50%) translateY(-50%) rotate(-90deg);
- font-size: 2em;
- height: 3em;
- position: absolute;
- top: 50%;
- left: 50%;
- }
- table th a {
- font-size: 1.2em;
- }
- input[name=rename], input[name=paste_name] {
- width: auto;
- }
- }
- @media (prefers-color-scheme: dark) {
- :root {
- --bg-color:
- --fg-color:
- --g1-color:
- --g2-color:
- --g3-color:
- --link-color:
- --visited-color:
- --active-color: orange;
- }
- }
- <?php } ?>
|