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; }
public function actionInstall($argv) { count($argv) > 0 || APP_ID != 'gini' or die("Usage: gini index install <module> <version>\n\n"); if (!class_exists('\\Sabre\\DAV\\Client')) { self::_loadGiniComposer(); } list($options, $headers) = self::_davOptionsAndHeaders(); $client = new \Sabre\DAV\Client($options); $installedModules = []; $installModule = function ($module, $versionRange, $targetDir, $isApp = false) use(&$installModule, &$installedModules, &$client, &$options, &$headers) { if (isset($installedModules[$module])) { $info = $installedModules[$module]; $v = new \Gini\Version($info->version); // if installed version is incorrect, abort the operation. if (!$v->satisfies($versionRange)) { die("Conflict detected on {$module}! Installed: {$v->fullVersion} Expecting: {$versionRange}\n"); } } else { // try to see if we've already got it somewhere if (isset(\Gini\Core::$MODULE_INFO[$module])) { $info = \Gini\Core::$MODULE_INFO[$module]; $v = new \Gini\Version($info->version); if ($v->satisfies($versionRange)) { $matched = $v; } } // fetch index.json echo "Fetching catalog of {$module}...\n"; while (true) { $response = $client->request('GET', $module . '/index.json', null, $headers); if ($response['statusCode'] == 401 && isset($response['headers']['www-authenticate'])) { // Authentication required // prompt user/password and try again if (!isset($options['userName'])) { list($options, $headers) = self::_davOptionsAndHeaders(true); $client = new \Sabre\DAV\Client($options); continue; } $matched or die("Access denied for fetch catalog of {$module} .\n"); $response = null; } elseif ($response['statusCode'] < 200 || $response['statusCode'] > 206) { $matched or die('Error: ' . $response['statusCode'] . "\n"); $response = null; } break; } if ($response) { $indexInfo = (array) json_decode($response['body'], true); // find latest match version foreach ($indexInfo as $version => $foo) { $v = new \Gini\Version($version); if ($v->satisfies($versionRange)) { if ($matched) { if ($matched->compare($v) > 0) { continue; } } $matched = $v; } } } if (!$matched) { die("Failed to locate required version!\n"); } if (!$info || $matched->fullVersion != $info->version) { $version = $matched->fullVersion; $info = (object) $indexInfo[$version]; $tarPath = "{$module}/{$version}.tgz"; echo "Downloading {$module} from {$tarPath}...\n"; while (true) { $response = $client->request('GET', $tarPath, null, $headers); if ($response['statusCode'] == 401 && isset($response['headers']['www-authenticate'])) { // Authentication required // prompt user/password and try again if (!isset($options['userName'])) { list($options, $headers) = self::_davOptionsAndHeaders(true); $client = new \Sabre\DAV\Client($options); continue; } die("Access denied for fetch catalog of {$module}.\n"); } if ($response['statusCode'] < 200 || $response['statusCode'] > 206) { die('Error: ' . $response['statusCode'] . "\n"); } break; } if ($isApp) { $modulePath = $targetDir; } else { $modulePath = "{$targetDir}/modules/{$module}"; } if (is_dir($modulePath) && file_exists($modulePath)) { \Gini\File::removeDir($modulePath); } \Gini\File::ensureDir($modulePath); echo "Extracting {$module}...\n"; $ph = popen('tar -zx -C ' . escapeshellcmd($modulePath), 'w'); if (is_resource($ph)) { fwrite($ph, $response['body']); pclose($ph); } } else { $version = $info->version; echo "Found local copy of {$module}/{$version}.\n"; } $installedModules[$module] = $info; echo "\n"; } if ($info) { foreach ((array) $info->dependencies as $m => $r) { if ($m == 'gini') { continue; } $installModule($m, $r, $targetDir, false); } } }; if (count($argv) > 0) { // e.g. gini install xxx $module = $argv[0]; if (count($argv) > 1) { $versionRange = $argv[1]; } else { $versionRange = readline('Please provide a version constraint for the ' . $module . ' requirement:'); } $installModule($module, $versionRange, $_SERVER['PWD'] . "/{$module}", true); } else { // run: gini install, then you should be in module directory if (APP_ID != 'gini') { // try to install its dependencies $app = \Gini\Core::moduleInfo(APP_ID); $installedModules[APP_ID] = $app; $installModule(APP_ID, $app->version, APP_PATH, true); } } }