Jump to content

how to code into .inc?


PlagueInfected

Recommended Posts

This might be the wrong forum to post this in, but when I do my sitemap, my PHP includes appear onto it, and was thinking of using .inc files or .tpl files for my includes instead.

 

the only problem is, I don't know how to write it, I tried .tpl with using this code below

 

{php}
echo "hello, world!";
{/php}

 

and when I uploaded it to my site, I was able to download it, so I'm assuming I have to turn on a file extension somewhere on my php.ini file?

 

Any ideas on how to code it correctly for .inc or .tpl?

Link to comment
Share on other sites

This might be the wrong forum to post this in, but when I do my sitemap, my PHP includes appear onto it, and was thinking of using .inc files or .tpl files for my includes instead.

 

the only problem is, I don't know how to write it, I tried .tpl with using this code below

 

{php}
echo "hello, world!";
{/php}

 

and when I uploaded it to my site, I was able to download it, so I'm assuming I have to turn on a file extension somewhere on my php.ini file?

 

Any ideas on how to code it correctly for .inc or .tpl?

 

Add a .htaccess file to your web root, and add the following line:

AddType application/x-httpd-php5 .phtml .php .tpl .inc

 

This will force .inc and .tpl to be parsed as PHP code, and will display the same as a (.php) document.

Link to comment
Share on other sites

Uh...a php forum...php...yup.

 

How do you build your site map? How do you traverse folders? How do you select the files?

 

If its boiler plate code

 

&& $file != *.inc // will not include all .inc files

&& $file != includes // will not traverse the includes folder

 

HTH

Teamatomic

Link to comment
Share on other sites

here is the sitemap code

 

<?php 
// starting directory. Dot means current directory
$basedir = "./";

// function to count depth of directory 
function getdepth($fn){
  return (($p = strpos($fn, "/")) === false) ? 0 : (1 + getdepth(substr($fn, $p+1)));
}

// function to print a line of html for the indented hyperlink
function printlink($fn){
  $indent = getdepth($fn); // get indent value
  echo "<li class=\"$indent\"><a href=\"$fn\">"; //print url
$handle = fopen($fn, "r"); //open web page file
$filestr = fread($handle, 1024); //read top part of html
fclose($handle); //clos web page file
if (preg_match("/<title>.+<\/title>/i",$filestr,$title)) { //get page title
    echo substr($title[0], 7, strpos($title[0], '/')-; //print title
} else {
    echo "No title";
}
  echo "</a></li><br>\n"; //finish html
}

// main function that scans the directory tree for web pages 
function listdir($basedir){
    if ($handle = @opendir($basedir)) { 
        while (false !== ($fn = readdir($handle))){ 
            if ($fn != '.' && $fn != '..'){ // ignore these
                $dir = $basedir."/".$fn; 
                if (is_dir($dir)){ 
                    listdir($dir); // recursive call to this function
                } else { //only consider .html etc. files
                    if (preg_match("/[^.\/].+\.(htm|html|php)$/",$dir,$fname)) {
                       printlink($fname[0]); //generate the html code
                    }
							} 
            } 
        } 
        closedir($handle); 
    } 
} 
// function call 
listdir($basedir); //this line starts the ball rolling
?>

 

I'll definitely give a try that comment you sent me

 

&& !file = includes

Link to comment
Share on other sites

I got to thinking and this is what I came up with. It's fully recursive and you can change where it searches, what it searches for and what it ignores using function arguments. Try this instead of your function.

 

<?php
function recursive_scan($basedir,$extension='php',$ignore=array('.','..')) {
	if(file_exists($basedir) && is_dir($basedir)) {
		$basedir = substr($basedir,-1) != '/' ? $basedir . '/' : $basedir;
		$list_tmp = glob($basedir . '*');
		foreach($list_tmp as $key => $value) {
			if(!in_array(pathinfo($value,PATHINFO_FILENAME),$ignore)) {
				if(is_dir($value)) {
					$files = recursive_scan($value,$extension,$ignore);
					if(count($files) > 0) {
						$list[$key]['name'] = $value;
						$list[$key]['files'] = $files;
					}
				}
				elseif(pathinfo($value,PATHINFO_EXTENSION) == $extension) {
					$list[$key]['name'] = $value;
				}
			}
		}
		return $list;
	}
	else {
		return FALSE;
	}
}
print_r(recursive_scan('./','php',array('.','..','include')));
?>

Link to comment
Share on other sites

try this on for size. Now it will return an array filled with url's to php files outside of your ignored directories and files and inside of your search area.

 

[/code]

<?php

function recursive_scan($basedir='./',$extension='php',$ignore=array('.','..','include')) {

if(file_exists($basedir) && is_dir($basedir)) {

$basedir = substr($basedir,-1) != '/' ? $basedir . '/' : $basedir;

$list_tmp = glob($basedir . '*');

foreach($list_tmp as $key => $value) {

if(!in_array(pathinfo($value,PATHINFO_FILENAME),$ignore)) {

if(is_dir($value)) {

$files = recursive_scan($value,$extension,$ignore);

if(count($files) > 0) {

$list[$key]['name'] = $value;

$list[$key]['files'] = $files;

}

}

elseif(pathinfo($value,PATHINFO_EXTENSION) == $extension) {

$list[$key]['name'] = $value;

}

}

}

return $list;

}

else {

return FALSE;

}

}

function get_links($baselink,$file_array=NULL) {

if($file_array === NULL) {

$file_array = recursive_scan();

}

foreach($file_array as $key => $value) {

if(!isset($value['files'])) {

$file_name = substr($value['name'],0,1) == '.' ? substr($value['name'],1) : $value['name'];

$file_name = substr($file_name,0,1) == '/' ? $file_name : '/' . $file_name;

$links[] = $baselink . $file_name . "\r\n";

}

else {

foreach(get_links($value['files']) as $file) {

$links[] = $baselink . $file;

}

}

}

return $links;

}

print_r(get_links('http://www.example.com'));

?>

[/code]

Link to comment
Share on other sites

Here try this.

 

function get_links($file_array,$baselink) {
	foreach($file_array as $key => $value) {
		if(!isset($value['files'])) {
			$file_name = substr($value['name'],0,1) == '.' ? substr($value['name'],1) : $value['name'];
			$file_name = substr($file_name,0,1) == '/' ? $file_name : '/' . $file_name;
			$links[] = $baselink . $file_name . "\r\n";
			print_r($links);
		}
		else {
			foreach(get_links($value['files']) as $file) {
				$links[] = $baselink . $file;
			}
		}
	}
	return $links;
}
print_r(get_links(recursive_scan(),'http://www.plagueinfected.com'));

Link to comment
Share on other sites

I worked on some code as well, as it was an interesting project.

As with crabfingers, I used a recursive function, with a structured array.

But a very completly different code, I tried to stick with your <li> directives, but also added headers (folder names). as well as some indentation so you can see in html the levels it retrived. Neat little project :)

Its not bad.

<?php 

function sitemap($basedir='./',$includes=array('html','htm','php'))
{
    $pages=array();
    if($handle=@opendir($basedir))
    {
        while(false!==($fn=readdir($handle)))
        {
            if(substr($fn,0,1)=='.')
                continue;
            if(is_dir($basedir.$fn))
            {
                $subpages=sitemap("{$basedir}{$fn}/");
                if(!empty($subpages))
                {
                    $pages[]=$fn;
                    $pages[]=$subpages;
                }
            } else {
                $fileinfo=pathinfo($fn);
                if(in_array($fileinfo['extension'],$includes))
                {
                    $page=file_get_contents($basedir.$fn);
                    $title=preg_match('@<title>(.+)</title>@i',$page,$title)?$title[1]:$fileinfo['basename'];
                    $pages[]="{$title}\t{$fileinfo['dirname']}/{$fn}";
                }
            }
        }
        closedir($handle);
    }
    return $pages;
}

function displaysitemap($sitemap=array(),$level=0)
{
    $indent=str_pad(' ',($level+1*4));
    echo "{$indent}<ul>\n";
    foreach($sitemap as $entry)
    {
        if(is_array($entry))
            displaysitemap($entry,$level+1);
        else
        {
            $info=explode("\t",$entry);
            echo "{$indent}<li>". (isset($info[1])?"<a href=\"{$info[1]}\">":'') . $info[0] . (isset($info[1])?'</a>':'') ."</li>\n";
        }
    }
    echo "{$indent}</ul>";
}


displaysitemap(sitemap())

?>

Link to comment
Share on other sites

it's missing 2 arguments, I don't know what I'm missing

 

http://plagueinfected.com/sitemap.php

 

<?php
   function recursive_scan($basedir='./',$extension='php',$ignore=array('.','..','inc', 'services', 'editor', 'monavie', 'music')) {
      if(file_exists($basedir) && is_dir($basedir)) {
         $basedir = substr($basedir,-1) != '/' ? $basedir . '/' : $basedir;
         $list_tmp = glob($basedir . '*');
         foreach($list_tmp as $key => $value) {
            if(!in_array(pathinfo($value,PATHINFO_FILENAME),$ignore)) {
               if(is_dir($value)) {
                  $files = recursive_scan($value,$extension,$ignore);
                  if(count($files) > 0) {
                     $list[$key]['name'] = $value;
                     $list[$key]['files'] = $files;
                  }
               }
               elseif(pathinfo($value,PATHINFO_EXTENSION) == $extension) {
                  $list[$key]['name'] = $value;
               }
            }
         }
         return $list;
      }
      else {
         return FALSE;
      }
   }
function get_links($file_array,$baselink) {
	foreach($file_array as $key => $value) {
		if(!isset($value['files'])) {
			$file_name = substr($value['name'],0,1) == '.' ? substr($value['name'],1) : $value['name'];
			$file_name = substr($file_name,0,1) == '/' ? $file_name : '/' . $file_name;
			$links[] = $baselink . $file_name . "\r\n";
			print_r($links);
		}
		else {
			foreach(get_links($value['files']) as $file) {
				$links[] = $baselink . $file;
			}
		}
	}
	return $links;
}
print_r(get_links(recursive_scan(),'http://www.plagueinfected.com'));
?>

Link to comment
Share on other sites

If this doesn't work I'm going to shit myself.

 

<?php
function recursive_scan($basedir='.',$extension='php',$ignore=array('.','..','inc', 'services', 'editor', 'monavie', 'music')) {
	if(file_exists($basedir) && is_dir($basedir)) {
		$basedir = substr($basedir,-1) != '/' ? $basedir . '/' : $basedir;
		$list['dir'] = $basedir;
		$list['tmp']['files'] = glob($basedir . '*');
		foreach($list['tmp']['files'] as $key => $value) {
			if(!in_array(pathinfo($value,PATHINFO_FILENAME),$ignore)) {
				if(is_dir($value)) {
					$file_array = recursive_scan($value,$extension,$ignore);
					if(count($file_array['files']) > 0) {
						$list['files'][$key]['dir'] = $value;
						$list['files'][$key]['files'] = $file_array['files'];
					}
				}
				elseif(pathinfo($value,PATHINFO_EXTENSION) == $extension) {
					$list['files'][$key]['name'] = $value;
				}
			}
		}
		unset($list['tmp']);
		return $list;
	}
	else {
		return FALSE;
	}
}
function get_links($file_array=NULL,$baselink=NULL) {
	$file_array = $file_array === NULL ? recursive_scan() : $file_array;
	$baselink = $baselink === NULL ? 'http://www.plagueinfected.com' : $baselink;
	$link_array['dir'] = $file_array['dir'];
	$link_array['tmp']['files'] = $file_array['files'];
	foreach($link_array['tmp']['files'] as $key => $value) {
		if(isset($value['files'])) {
			$link_array['links'][$key] = get_links($value,$baselink);
		}
		else {
			$file_name = substr($value['name'],0,1) == '.' ? substr($value['name'],1) : $value['name'];
			$file_name = substr($file_name,0,1) == '/' ? $file_name : '/' . $file_name;
			$link_array['links'][$key]['file'] = $value['name'];
			$link_array['links'][$key]['link'] = $baselink . $file_name;
		}
	}
	unset($link_array['tmp']);
	return $link_array;
}
function sitemap($link_array=NULL,$baselink=NULL) {
	$link_array = $link_array === NULL ? get_links($file_array,$baselink) : $link_array;
	$sitemap = '<strong>' . $link_array['dir'] . '</strong><div class="indent">';
	foreach($link_array['links'] as $key => $value) {
		if(isset($value['dir'])) {
			$sitemap .= sitemap($value,$baselink);
		}
		else {
			$file_content = file_get_contents($value['file']);
			if(preg_match("/<title>(.+)<\/title>/i",$file_content,$title)) {
				$link_name = $title['1'];
			}
			else {
				$link_name = $value['link'];
			}
			$sitemap .= '<a href="' . $value['link'] . '">' . $link_name . '</a><br />' . "\r\n";
		}
	}
	$sitemap .= '</div>';
	return $sitemap;
}
//print_r(recursive_scan());
//print_r(get_links());
print sitemap();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.