function actionDefault() { // Create the URL object $url = new YDUrl('http://www.yellowduck.be/index.xml'); // The different parts echo '<br>URL: ' . $url->getUrl(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Query: ' . $url->getQuery(); echo '<br>Fragment: ' . $url->getFragment(); // Get the contents YDDebugUtil::dump($url->getContents(), 'URL contents'); }
/** * This function registers a named database instance. * * @param $name The name of the database instance. * @param $driver Name of the database driver, DNS string or array containing drivername, file name and class name. * @param $db (optional) Database name to use for the connection. If empty, we are using a DSN string in driver. * @param $user (optional) User name to use for the connection. * @param $pass (optional) Password to use for the connection. * @param $host (optional) Host name to use for the connection. * * @static */ function registerInstance($name, $driver, $db = '', $user = '', $pass = '', $host = '') { // Check if the global array exists YDDatabase::_initNamedInstances(); // Check if we are using a DSN string, e.g. "mysql://*****:*****@host/database" if ($db == '') { $dsn = new YDUrl($driver); $driver = $dsn->getScheme(); $db = substr($dsn->getPath(), 1); $user = $dsn->getUser(); $pass = $dsn->getPassword(); $host = $dsn->getHost(); } // Register the instance $GLOBALS['YD_DB_INSTANCES'][strtolower($name)] = array($driver, $db, $user, $pass, $host); }
function actionDefault() { // Create the URL object $url = new YDUrl('http://www.yellowduck.be/directory/test/index.xml'); // The different parts echo '<br>Original URL: ' . $url->_url; echo '<br>URL: ' . $url->getUrl(); echo '<br>URI: ' . $url->getUri(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Path segments: ' . implode(', ', $url->getPathSegments()); echo '<br>Path directories: ' . implode(', ', $url->getPathDirectories()); echo '<br>isDirectory( test ): ' . var_export($url->isDirectory('test'), 1); echo '<br>isDirectory( xx ): ' . var_export($url->isDirectory('xx'), 1); echo '<br>Query: ' . var_export($url->getQuery(), 1); echo '<br>Fragment: ' . $url->getFragment(); // Create the URL object $url = new YDUrl('http://pieter@www.yellowduck.be/directory/test/index?x[]=22&x[]=23#22'); // The different parts echo '<br><br>Original URL: ' . $url->_url; echo '<br>URL: ' . $url->getUrl(); echo '<br>URI: ' . $url->getUri(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Path segments: ' . implode(', ', $url->getPathSegments()); echo '<br>Path directories: ' . implode(', ', $url->getPathDirectories()); echo '<br>Query: ' . var_export($url->getQuery(), 1); echo '<br>Fragment: ' . $url->getFragment(); // Create the URL object $url = new YDUrl('http://*****:*****@www.yellowduck.be:8080/directory/test/?do=x&id=1#10'); // The different parts echo '<br><br>Original URL: ' . $url->_url; echo '<br>URL: ' . $url->getUrl(); echo '<br>URI: ' . $url->getUri(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Path segments: ' . implode(', ', $url->getPathSegments()); echo '<br>Path directories: ' . implode(', ', $url->getPathDirectories()); echo '<br>Query: ' . var_export($url->getQuery(), 1); echo '<br>Fragment: ' . $url->getFragment(); echo '<br>Getting query variable do: ' . $url->getQueryVar('do'); echo '<br>Setting query variable do to y: ' . $url->setQueryVar('do', 'y'); echo '<br>New URL: ' . $url->getUrl(); echo '<br>Deleting query variable do: ' . $url->deleteQueryVar('do'); echo '<br>New URL: ' . $url->getUrl(); // Create the URL object $url = new YDUrl('http://ydframework.berlios.de/ydf2_changelog_summary.xml'); // The different parts echo '<br><br>Original URL: ' . $url->_url; echo '<br>URL: ' . $url->getUrl(); echo '<br>URI: ' . $url->getUri(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Path segments: ' . implode(', ', $url->getPathSegments()); echo '<br>Path directories: ' . implode(', ', $url->getPathDirectories()); echo '<br>Query: ' . var_export($url->getQuery(), 1); echo '<br>Fragment: ' . $url->getFragment(); // Test the getPathSubdirectories function $url = new YDUrl('http://www.yellowduck.be/ydf2/forum/cool.html'); YDDebugUtil::dump($url->getUrl()); YDDebugUtil::dump($url->getPathSubdirectories('ydf2'), "getPathSubdirectories('ydf2')"); YDDebugUtil::dump($url->getPathSubdirectories('forum'), "getPathSubdirectories('forum')"); YDDebugUtil::dump($url->getPathSubdirectories('test'), "getPathSubdirectories('test')"); $url = new YDUrl('http://www.yellowduck.be/ydf2/forum/ydf2/forum/cool.html'); YDDebugUtil::dump($url->getUrl()); YDDebugUtil::dump($url->getPathSubdirectories('ydf2'), "getPathSubdirectories('ydf2')"); YDDebugUtil::dump($url->getPathSubdirectories('forum'), "getPathSubdirectories('forum')"); YDDebugUtil::dump($url->getPathSubdirectories('test'), "getPathSubdirectories('test')"); // Test the getPathSubsegments function $url = new YDUrl('http://www.yellowduck.be/ydf2/forum/cool.html'); YDDebugUtil::dump($url->getUrl()); YDDebugUtil::dump($url->getPathSubsegments('ydf2'), "getPathSubsegments('ydf2')"); YDDebugUtil::dump($url->getPathSubsegments('forum'), "getPathSubsegments('forum')"); YDDebugUtil::dump($url->getPathSubsegments('test'), "getPathSubsegments('test')"); $url = new YDUrl('http://www.yellowduck.be/ydf2/forum/ydf2/forum/cool.html'); YDDebugUtil::dump($url->getUrl()); YDDebugUtil::dump($url->getPathSubsegments('ydf2'), "getPathSubsegments('ydf2')"); YDDebugUtil::dump($url->getPathSubsegments('forum'), "getPathSubsegments('forum')"); YDDebugUtil::dump($url->getPathSubsegments('test'), "getPathSubsegments('test')"); // Do some more testing $x = 'http://localhostexamples/form2.php?form2_tmp%5Bday%5D=1&form2_tmp%5Bmonth%5D=1&form2_tmp%5Byear%5D=2000&do=test'; $url = new YDUrl($x); YDDebugUtil::dump($url, $x); YDDebugUtil::dump($url->getUrl(), 'Original url'); // Do some more testing $x = 'http://localhostexamples/form2.php?form2_tmp%5Bday%5D=1&form2_tmp%5Bmonth%5D=1&form2_tmp%5Byear%5D=2000&do=test'; $url = new YDUrl($x); YDDebugUtil::dump($url, $x); YDDebugUtil::dump($url->getUrl(), 'Original url'); // Get the contents $url = new YDUrl('http://ydframework.berlios.de/ydf2_changelog_summary.xml'); YDDebugUtil::dump($url->getContents(), 'URL contents'); }
function actionDefault() { // Create the URL object $url = new YDUrl('http://www.yellowduck.be/directory/test/index.xml'); // The different parts echo '<br>Original URL: ' . $url->_url; echo '<br>URL: ' . $url->getUrl(); echo '<br>URI: ' . $url->getUri(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Path segments: ' . implode(', ', $url->getPathSegments()); echo '<br>Path directories: ' . implode(', ', $url->getPathDirectories()); echo '<br>isDirectory( test ): ' . var_export($url->isDirectory('test'), 1); echo '<br>isDirectory( xx ): ' . var_export($url->isDirectory('xx'), 1); echo '<br>Query: ' . var_export($url->getQuery(), 1); echo '<br>Fragment: ' . $url->getFragment(); // Create the URL object $url = new YDUrl('http://pieter@www.yellowduck.be/directory/test/index?x[]=22&x[]=23#22'); // The different parts echo '<br><br>Original URL: ' . $url->_url; echo '<br>URL: ' . $url->getUrl(); echo '<br>URI: ' . $url->getUri(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Path segments: ' . implode(', ', $url->getPathSegments()); echo '<br>Path directories: ' . implode(', ', $url->getPathDirectories()); echo '<br>Query: ' . var_export($url->getQuery(), 1); echo '<br>Fragment: ' . $url->getFragment(); // Create the URL object $url = new YDUrl('http://*****:*****@www.yellowduck.be:8080/directory/test/?do=x&id=1#10'); // The different parts echo '<br><br>Original URL: ' . $url->_url; echo '<br>URL: ' . $url->getUrl(); echo '<br>URI: ' . $url->getUri(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Path segments: ' . implode(', ', $url->getPathSegments()); echo '<br>Path directories: ' . implode(', ', $url->getPathDirectories()); echo '<br>Query: ' . var_export($url->getQuery(), 1); echo '<br>Fragment: ' . $url->getFragment(); echo '<br>Getting query variable do: ' . $url->getQueryVar('do'); echo '<br>Setting query variable do to y: ' . $url->setQueryVar('do', 'y'); echo '<br>New URL: ' . $url->getUrl(); echo '<br>Deleting query variable do: ' . $url->deleteQueryVar('do'); echo '<br>New URL: ' . $url->getUrl(); // Create the URL object $url = new YDUrl('http://www.yellowduck.be/rss.xml'); // The different parts echo '<br><br>Original URL: ' . $url->_url; echo '<br>URL: ' . $url->getUrl(); echo '<br>URI: ' . $url->getUri(); echo '<br>Scheme: ' . $url->getScheme(); echo '<br>Host: ' . $url->getHost(); echo '<br>Port: ' . $url->getPort(); echo '<br>User: '******'<br>Password: '******'<br>Path: ' . $url->getPath(); echo '<br>Path segments: ' . implode(', ', $url->getPathSegments()); echo '<br>Path directories: ' . implode(', ', $url->getPathDirectories()); echo '<br>Query: ' . var_export($url->getQuery(), 1); echo '<br>Fragment: ' . $url->getFragment(); // Test the getPathSubdirectories function $url = new YDUrl('http://www.yellowduck.be/ydf2/forum/cool.html'); YDDebugUtil::dump($url->getUrl()); YDDebugUtil::dump($url->getPathSubdirectories('ydf2'), "getPathSubdirectories('ydf2')"); YDDebugUtil::dump($url->getPathSubdirectories('forum'), "getPathSubdirectories('forum')"); YDDebugUtil::dump($url->getPathSubdirectories('test'), "getPathSubdirectories('test')"); $url = new YDUrl('http://www.yellowduck.be/ydf2/forum/ydf2/forum/cool.html'); YDDebugUtil::dump($url->getUrl()); YDDebugUtil::dump($url->getPathSubdirectories('ydf2'), "getPathSubdirectories('ydf2')"); YDDebugUtil::dump($url->getPathSubdirectories('forum'), "getPathSubdirectories('forum')"); YDDebugUtil::dump($url->getPathSubdirectories('test'), "getPathSubdirectories('test')"); // Test the getPathSubsegments function $url = new YDUrl('http://www.yellowduck.be/ydf2/forum/cool.html'); YDDebugUtil::dump($url->getUrl()); YDDebugUtil::dump($url->getPathSubsegments('ydf2'), "getPathSubsegments('ydf2')"); YDDebugUtil::dump($url->getPathSubsegments('forum'), "getPathSubsegments('forum')"); YDDebugUtil::dump($url->getPathSubsegments('test'), "getPathSubsegments('test')"); $url = new YDUrl('http://www.yellowduck.be/ydf2/forum/ydf2/forum/cool.html'); YDDebugUtil::dump($url->getUrl()); YDDebugUtil::dump($url->getPathSubsegments('ydf2'), "getPathSubsegments('ydf2')"); YDDebugUtil::dump($url->getPathSubsegments('forum'), "getPathSubsegments('forum')"); YDDebugUtil::dump($url->getPathSubsegments('test'), "getPathSubsegments('test')"); // Get the contents $url = new YDUrl('http://www.yellowduck.be/rss.xml'); YDDebugUtil::dump($url->getContents(), 'URL contents'); }
function doRequest() { // Performs the actual HTTP request, returning true or false depending on outcome if (!($fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout))) { // Set error message switch ($errno) { case -3: $this->errormsg = 'Socket creation failed (-3)'; case -4: $this->errormsg = 'DNS lookup failure (-4)'; case -5: $this->errormsg = 'Connection refused or timed out (-5)'; default: $this->errormsg = 'Connection failed (' . $errno . ')'; $this->errormsg .= ' ' . $errstr; $this->debug($this->errormsg); } return false; } socket_set_timeout($fp, $this->timeout); $request = $this->buildRequest(); $this->debug('Request', $request); fwrite($fp, $request); // Reset all the variables that should not persist between requests $this->headers = array(); $this->content = ''; $this->errormsg = ''; // Set a couple of flags $inHeaders = true; $atStart = true; // Now start reading back the response while (!feof($fp)) { $line = fgets($fp, 4096); if ($atStart) { // Deal with first line of returned data $atStart = false; if (!preg_match('/HTTP\\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) { $this->errormsg = "Status code line invalid: " . htmlentities($line); $this->debug($this->errormsg); return false; } $http_version = $m[1]; // not used $this->status = $m[2]; $status_string = $m[3]; // not used $this->debug(trim($line)); continue; } if ($inHeaders) { if (trim($line) == '') { $inHeaders = false; $this->debug('Received Headers', $this->headers); if ($this->headers_only) { break; // Skip the rest of the input } continue; } if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) { // Skip to the next header continue; } $key = strtolower(trim($m[1])); $val = trim($m[2]); // Deal with the possibility of multiple headers of same name if (isset($this->headers[$key])) { if (is_array($this->headers[$key])) { $this->headers[$key][] = $val; } else { $this->headers[$key] = array($this->headers[$key], $val); } } else { $this->headers[$key] = $val; } continue; } // We're not in the headers, so append the line to the contents $this->content .= $line; } fclose($fp); // If data is compressed, uncompress it if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') { $this->debug('Content is gzip encoded, unzipping it'); $this->content = substr($this->content, 10); // See http://www.php.net/manual/en/function.gzencode.php $this->content = gzinflate($this->content); } // If $persist_cookies, deal with any cookies if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) { $cookies = $this->headers['set-cookie']; if (!is_array($cookies)) { $cookies = array($cookies); } foreach ($cookies as $cookie) { if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) { $this->cookies[$m[1]] = $m[2]; } } // Record domain of cookies for security reasons $this->cookie_host = $this->host; } // If $persist_referers, set the referer ready for the next request if ($this->persist_referers) { $this->debug('Persisting referer: ' . $this->getRequestURL()); $this->referer = $this->getRequestURL(); } // Finally, if handle_redirects and a redirect is sent, do that if ($this->handle_redirects) { if (++$this->redirect_count >= $this->max_redirects) { $this->errormsg = 'Number of redirects exceeded maximum (' . $this->max_redirects . ')'; $this->debug($this->errormsg); $this->redirect_count = 0; return false; } $location = isset($this->headers['location']) ? $this->headers['location'] : ''; $uri = isset($this->headers['uri']) ? $this->headers['uri'] : ''; if ($location || $uri) { $url = parse_url($location . $uri); // This will FAIL if redirect is to a different site if (!empty($url['path'])) { return $this->get($url['path']); } else { // Note from Rene (rene@fsfe.org): Now it won't fail anymore. // Quick&Dirty hack... don't blame me... :) $u = new YDUrl($this->headers['location']); $c = new YDHttpClient($u->getHost()); $c->setUserAgent($this->user_agent); $c->setAuthorization($this->username, $this->password); $c->setCookies($this->cookies); $c->useGzip($this->use_gzip); $c->setPersistCookies($this->persist_cookies); $c->setPersistReferers($this->persist_referers); $c->setHandleRedirects($this->handle_redirects); $c->setMaxRedirects($this->max_redirects); $c->setHeadersOnly($this->headers_only); $c->setDebug($this->debug); $r = $c->get('/' . $u->getPath()); $this->status = $c->getStatus(); $this->content = $c->getContent(); $this->headers = $c->getHeaders(); $this->errormsg = $c->getError(); $this->cookies = $c->getCookies(); return $r; } } } return true; }