is there any php functions, to sanitize link+path?
i.e.
http://example.com/fold1/fold2/fold3/../../././MyFile.HTML
to
http://example.com/fold1/MyFile.HTML
so, i want remove dots,but maintain the suitable(relative) correct path.
I've found so far, is :
echo ConvertDotedPathToNormalUrl('http://example.com/directory/.././pageee.html');
code:
function ConvertDotedPathToNormalUrl($url){
$firstType = '/(.*)\/((?:(?!\.\.).)+)\/\.\.\//si';
preg_match($firstType,$url,$result);
if (!empty($result[2])){
$url = str_replace('/'.$result[2].'/..','',$url);
if ( strstr($url,'../')){$url= ConvertDotedPathToNormalUrl($url);}
}
$url = str_replace('/./','/',$url); $url = str_replace('://','|||',$url);$url = str_replace('//','/',$url);$url = str_replace('|||','://',$url);
return $url;
}
p.s. but not, it converts
You can
1) get the $path using parse_url(..).
2) get the $webroot = $_SERVER['DOCUMENT_ROOT'];
3) get the $zrealpath = realpath($webroot . $path);
<?php
define ('CRLF', "<br />\n");
$url = 'http://example.com/fold1/fold2/fold3/../../././MyFile.HTML';
$parsed = parse_url($url);
echo '---- vardump($parsed):', CRLF; // for education
zvardump($parsed);
$webroot = $_SERVER['DOCUMENT_ROOT'];
echo 'webroot = ', $webroot, CRLF;
$path = $parsed['path'];
echo 'path = ', $path, CRLF;
$zrealpath = realpath($webroot . $path);
echo 'realpath = ', $zrealpath, CRLF;
function zvardump($var1) {
ob_start();
echo "<pre style=\"margin:0;\">\n";
var_dump($var1);
echo "</pre>\n";
$zoutput = ob_get_contents();
ob_end_clean();
echo str_replace("=>\n ", " => ", $zoutput);
}
?>
Related
Here is my code which is a partially based on a few different codes that you can find easily in various places if googled. I'm trying to count the internal and external links, all links and ( TO DO .nofollow ) links on a any webpage. This is what I have till now. Most of the results are correct, some generic calls gives me a weird results though, and I still need to do .nofollow and perhaps _blank as well. If you care to comment or add/change anything with bit of logic explanation then please do so, it will be very appreciated.
<?php
// transform to absolute path function...
function path_to_absolute($rel, $base)
{
/* return if already absolute URL */
if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
/* queries and anchors */
if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
/* parse base URL and convert to local variables:
$scheme, $host, $path */
extract(parse_url($base));
/* remove non-directory element from path */
$path = preg_replace('#/[^/]*$#', '', $path);
/* destroy path if relative url points to root */
if ($rel[0] == '/') $path = '';
/* dirty absolute URL */
$abs = "$host$path/$rel";
/* replace '//' or '/./' or '/foo/../' with '/' */
$re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
/* absolute URL is ready! */
return $scheme.'://'.$abs;
}
// count zero begins
$intnumLinks = 0;
$extnumLinks = 0;
$nfnumLinks = 0;
$allnumLinks = 0;
// get url file
$url = $_REQUEST['url'];
// get contents of url file
$html = file_get_contents($url);
// http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php
// loading DOM document
$doc=new DOMDocument();
#$doc->loadHTML($html);
$xml=simplexml_import_dom($doc); // just to make xpath more simple
$strings=$xml->xpath('//a');
foreach ($strings as $string) {
$aa = path_to_absolute( $string[href], $url, true );
$a = parse_url($aa, PHP_URL_HOST);
$a = str_replace("www.", "", $a);
$b = parse_url($url, PHP_URL_HOST);
if($a == $b){
echo 'call-host: ' . $b . '<br>';
echo 'type: int </br>';
echo 'title: ' . $string[0] . '<br>';
echo 'url: ' . $string['href'] . '<br>';
echo 'host: ' . $a . '<br><br>';
$intnumLinks++;
}else{
echo 'call-host: ' . $b . '<br>';
echo 'type: ext </br>';
echo 'title: ' . $string[0] . '<br>';
echo 'url: ' . $string['href'] . '<br>';
echo 'host: ' . $a . '<br><br>';
$extnumLinks++;
}
$allnumLinks++;
}
// count results
echo "<br>";
echo "Count int: $intnumLinks <br>";
echo "Count ext: $extnumLinks <br>";
echo "Count nf: $nfnumLinks <br>";
echo "Count all: $allnumLinks <br>";
?>
Consider this post as closed. At first I wanted to delete this post but then again someone might use this code for his work.
I'm looking at trying to get a file's contents and preview the first 50 words, excluding titles, and I've hit a snag.
$getPage = $_SERVER['QUERY_STRING'];
$page = "news/" . $getPage . ".php";
$directory = 'news/';
$scanned_directory = array_diff(scandir($directory, 1), array('..', '.'));
if (file_exists($page)) {
include $page;
} else {
foreach ($scanned_directory as $value) {
$file = file_get_contents('news/' . $value);
$less_words = implode(' ', array_slice(explode(' ', $file), 0, 50));
$result = preg_replace('/\<.*?\>|\s*/', '', $less_words);
echo '<p>$result ...<br> Read more</p>';
/* TO ADD: URL variable */
}
}
The issue I am having with this is when echoing $less_words the text outputted is correct, however the <h1></h1> tags in the 2 example files I have show, and are formatted. But when echoing $result the text outputted on the page is $result Read more.
The php function http://php.net/manual/en/function.strip-tags.php would work...
$file = file_get_contents('news/' . $value);
$file = strip_tags($file);
$less_words = implode(' ', array_slice(explode(' ', $file), 0, 50));
echo '<p>'.$less_words.'...<br> Read more</p>';
Change
echo '<p>$result ...<br> Read more</p>';
/* TO ADD: URL variable */
to
echo '<p>'.$result.' ...<br> Read more</p>';
/* TO ADD: URL variable */
I have the following code :
function removeFilename($url)
{
$file_info = pathinfo($url);
return isset($file_info['extension'])
? str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url)
: $url;
}
$url1 = "http://website.com/folder/filename.php";
$url2 = "http://website.com/folder/";
$url3 = "http://website.com/";
echo removeFilename($url1); //outputs http://website.com/folder/
echo removeFilename($url2);//outputs http://website.com/folder/
echo removeFilename($url3);//outputs http:///
Now my problem is that when there is only only a domain without folders or filenames my function removes website.com too.
My idea is there is any way on php to tell my function to do the work only after third slash or any other solutions you think useful.
UPDATED : ( working and tested )
<?php
function removeFilename($url)
{
$parse_file = parse_url($url);
$file_info = pathinfo($parse_file['path']);
return isset($file_info['extension'])
? str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url)
: $url;
}
$url1 = "http://website.com/folder/filename.com";
$url2 = "http://website.org/folder/";
$url3 = "http://website.com/";
echo removeFilename($url1); echo '<br/>';
echo removeFilename($url2); echo '<br/>';
echo removeFilename($url3);
?>
Output:
http://website.com/folder/
http://website.org/folder/
http://website.com/
Sounds like you are wanting to replace a substring and not the whole thing. This function might help you:
http://php.net/manual/en/function.substr-replace.php
Since filename is at last slash you can use substr and str_replace to remove file name from path.
$PATH = "http://website.com/folder/filename.php";
$file = substr( strrchr( $PATH, "/" ), 1) ;
echo $dir = str_replace( $file, '', $PATH ) ;
OUTPUT
http://website.com/folder/
pathinfo cant recognize only domain and file name. But if without filename url is ended by slash
$a = array(
"http://website.com/folder/filename.php",
"http://website.com/folder/",
"http://website.com",
);
foreach ($a as $item) {
$item = explode('/', $item);
if (count($item) > 3)
$item[count($item)-1] ='';;
echo implode('/', $item) . "\n";
}
result
http://website.com/folder/
http://website.com/folder/
http://website.com
Close to the answer of splash58
function getPath($url) {
$item = explode('/', $url);
if (count($item) > 3) {
if (strpos($item[count($item) - 1], ".") === false) {
return $url;
}
$item[count($item)-1] ='';
return implode('/', $item);
}
return $url;
}
i am uploading the image into the server , need to place the _ in place of gap in the image. Like if the name of image is Stack Flow.jpg, i need to send it as Stack_Flow.jpg in the directory as well in the email. HOw could be possible with the following code. i have tried but no success.. I am sending the 4 files in one form, code as ---
$filea = $_FILES['FILE1']['name'];
$fileb = $_FILES['FILE2']['name'];
$filec = $_FILES['FILE3']['name'];
$filed = $_FILES['FILE4']['name'];
$order_image_a='order_'.$orderId.'_'.$filea;
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
$order_image_b='order_'.$orderId.'_'.$fileb;
if(!empty($fileb)) move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b");
$order_image_c='order_'.$orderId.'_'.$filec;
if(!empty($filec)) move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c");
$order_image_d='order_'.$orderId.'_'.$filed;
if(!empty($filed)) move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d");
i am using below function, how could i apply it for all four files--
<script>
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
}
</script>
here is a quick example in php:
<?php
$name = "Stack Flow.jpg";
echo preg_replace('/[\s\-]+/', '_', $name );
?>
returns Stack_Flow.jpg
http://codepad.org/MQoEZ2wv
This is not a script but PHP..
<?
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
?>
//do the same for all other images..
$filea = str_replace(' ', '_', $filea;
$order_image_a='order_'.$orderId.'_'.$filea;
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
Using:
<?php
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
}
?>
And then your code:
$filea = $_FILES['FILE1']['name'];
$fileb = $_FILES['FILE2']['name'];
$filec = $_FILES['FILE3']['name'];
$filed = $_FILES['FILE4']['name'];
$order_image_a='order_'.$orderId.'_'.convertSpecialChars($filea);
if(!empty($filea))
move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
$order_image_b='order_'.$orderId.'_'.convertSpecialChars($fileb);
if(!empty($fileb))
move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b");
$order_image_c='order_'.$orderId.'_'.convertSpecialChars($filec);
if(!empty($filec))
move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c");
$order_image_d='order_'.$orderId.'_'.convertSpecialChars($filed);
if(!empty($filed))
move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d");
Or if possible, you could do it in a loop (less duplicate code):
for ($i = 1; $i <= 4; $i++)
{
$file = $_FILES['FILE' . $i]['name'];
$order_image = 'order_' . $orderId . '_' . convertSpecialChars($file);
if(!empty($file))
move_uploaded_file($_FILES['FILE' . $i]['tmp_name'], "../files/$order_image");
}
In your code change $order_image_a='order_'.$orderId.'_'.$filea; and other similar lines to
$order_image_a='order_'.$orderId.'_'.convertSpecialChars($filea);
But will better if you will know how work your code.
I need to find a string within a string ("foo", for example) and then replace it with a PHP code. Is it possible?
I'm creating a Joomla Plugin. When my article has something like "{foo}", i will replace it with an include (require, or whatever).
I've made something like:
public function onContentBeforeDisplay($context, &$row, &$params, $page=0)
{
// $row->text has the article text ...
if (preg_match('/{contact-form}/', $row->text))
{
require_once(dirname(__FILE__) . DS . 'tmpl' . DS . 'default.php');
}
}
But this code will insert default.php at the beginning. I want to replace it in {contact-form} tag.
Thank you.
You can use the PHP function "substr_replace()", whose details are given here.
\n";
/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 0) . "<br />\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";
/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . "<br />\n";
/**
* Your food & search is here
*/
/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";
/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . "<br />\n";
?>
You can create a PHP file that matches the keyword (like: foo.php) and simply extract the keyword from the text and use it to include that file.
For example:
<?php
$str = "This is {foo} text";
$includePath = "/path/to/my/files/";
// Careful with the Regular Expression. If you allow chars like . in the
// keyword this could create a security problem. (Dots allow you to go backwards
// which would allow people to execute files outside your path.)
if (preg_match_all('/\{([\w]+)\}/', $str, $matches))
{
foreach ($matches[1] as $_match)
{
$filePath = $includePath . $_match . ".php";
if (file_exists($filePath))
{
include $filePath;
}
else
{
trigger_error("File does not exist '$filePath'.", E_USER_WARNING);
}
}
}
?>
I think you just want this, don't you?
<?php
$replacement = file_get_contents('myfile.php');
$content = str_replace('{foo}', eval($replacement), $content);
?>
If you need to replace any string on series of files on your remote server and well you don't have time for it! , here is your solution , I hope it can help someone.
////
//PHP 5.3 + Class find and replace string in files
//
//by Bruce Afruz
//
//2013
//
//example usage for single file:
//
//$new = new fileReplacement('./');
//$new->setExt("check.php");
//$new->changeContents("hello", "goodbye");
//
//example usage for multiple files:
//
//$new = new fileReplacement('./test');
//$new->setExt("*.html");
//$new->changeContents("hello", "goodbye");
//
//to change directory:
//
//$new = new fileReplacement('./test');
//$new->setDir("./test2");
//$new->setExt("*.html");
//$new->changeContents("hello", "goodbye");
////
class fileReplacement
{
private $ext , $dir ;
public function getDir() {
return $this->dir;
}
public function setDir($dir) {
$this->dir = $dir;
}
public function getExt() {
return $this->ext;
}
public function setExt($ext) {
$this->ext = $ext;
}
function __construct($dir) {
$this->dir = $dir;
}
public function rglob($pattern = '*', $flags = 0, $path = '') {
chdir($this->getDir());
$paths = glob($path . '*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
$files = glob($path . $pattern, $flags);
foreach ($paths as $path) {
$files = array_merge($files, $this->rglob($pattern, $flags, $path));
}
return $files;
}
public function changeContents($replace , $sentence , $flags = 0, $path = '') {
$all = $this->rglob($this->getExt() , $flags, $path);
foreach ($all as $file) {
$filename = $file;
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents = str_replace($replace , $sentence, $contents);
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w+')) {
echo "Cannot open file ($filename)
";
exit;
}
// Write $contents to our opened file.
if (fwrite($handle, $contents) === FALSE) {
echo "Cannot write to file ($filename)
";
exit;
}
echo "Success, wrote content to file ($filename)
";
fclose($handle);
} else {
echo "The file $filename is not writable
";
}
}
}}