public function build()
 {
     \yii\base\Event::on(\denoll\filekit\Storage::className(), \denoll\filekit\Storage::EVENT_BEFORE_SAVE, function ($event) {
         /** @var \denoll\filekit\Storage $storage */
         $storage = $event->sender;
         if (!$storage->getFilesystem()->has('.dirindex')) {
             $storage->getFilesystem()->write('.dirindex', 1);
             $dirindex = 1;
         } else {
             $dirindex = $storage->getFilesystem()->read('.dirindex');
         }
         if ($storage->maxDirFiles !== -1) {
             if ($storage->getFilesystem()->has($dirindex)) {
                 $filesCount = count($storage->getFilesystem()->listContents($dirindex));
                 if ($filesCount > $storage->maxDirFiles) {
                     $dirindex++;
                     $storage->getFilesystem()->createDir($dirindex);
                 }
             } else {
                 $storage->getFilesystem()->createDir($dirindex);
             }
         }
     });
     $client = new \Sabre\DAV\Client(['baseUri' => 'https://webdav.yandex.ru']);
     $client->addCurlSetting(CURLOPT_SSL_VERIFYPEER, false);
     $client->addCurlSetting(CURLOPT_HTTPHEADER, ['Authorization: OAuth TOKENTOKENTOKEN', 'Accept: */*', 'Host: webdav.yandex.ru']);
     $adapter = new WebDAVAdapter($client, '/');
     $flysystem = new Filesystem($adapter);
     if (!$flysystem->has($this->pathPrefix)) {
         $flysystem->createDir($this->pathPrefix);
     }
     $adapter->setPathPrefix($this->pathPrefix);
     return $flysystem;
 }
Esempio n. 2
0
function WebDAVFetch($URL, $enable_cache, $cache_time, $verify_peer)
{
    $parts = parse_url($URL);
    $location = $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
    if (!array_key_exists('host', $parts)) {
        $parts['host'] = $parts['path'];
        $parts['path'] = '/';
    }
    if (!array_key_exists('scheme', $parts)) {
        $parts['scheme'] = 'http';
    }
    if (!array_key_exists('path', $parts)) {
        $parts['path'] = '/';
    }
    $location = $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
    $settings = array('baseUri' => $location);
    if (array_key_exists('user', $parts)) {
        $settings['userName'] = $parts['user'];
    }
    if (array_key_exists('pass', $parts)) {
        $settings['password'] = $parts['pass'];
    }
    $client = new Sabre\DAV\Client($settings);
    if (!$verify_peer) {
        $client->addCurlSetting(CURLOPT_SSL_VERIFYPEER, FALSE);
    }
    $client->addCurlSetting(CURLOPT_USERAGENT, 'Extcal');
    $entries = $client->propfind('', array('{DAV:}getetag', '{DAV:}getcontenttype', '{DAV:}getlastmodified'), 1);
    $RET = "";
    foreach ($entries as $ICS => $properties) {
        $cachefile = preg_replace('/\\.ics\\.ics/', '.ics', dirname(__FILE__) . "/cache/" . preg_replace('/\\//', '_', $ICS) . ".ics");
        if (array_key_exists('{DAV:}getcontenttype', $properties) && preg_match('/calendar/i', $properties['{DAV:}getcontenttype']) > 0) {
            if ($enable_cache && $cache_time > 0 && file_exists($cachefile) && strtotime($properties['{DAV:}getlastmodified']) < filemtime($cachefile) && time() - filemtime($cachefile) < $cache_time) {
                $entry = file_get_contents($cachefile);
            } else {
                $entry = $client->request('GET', $ICS)['body'];
                if ($enable_cache) {
                    file_put_contents($cachefile, $entry);
                }
            }
            $RET .= $entry . "\n\n";
        }
    }
    // workaround for start dates before 1970
    // $RET = preg_replace("/DTSTART;VALUE=DATE:19[0-6]/","DTSTART;VALUE=DATE:197",$RET);
    return $RET;
}