PlagueInfected Posted January 5, 2010 Share Posted January 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/ Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-988896 Share on other sites More sharing options...
teamatomic Posted January 5, 2010 Share Posted January 5, 2010 How do you build your site map? HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-988930 Share on other sites More sharing options...
PlagueInfected Posted January 5, 2010 Author Share Posted January 5, 2010 sweet thanks, I'll give it a try and I coded the sitemap in PHP only problem is, I try to specify it to only the main directory "/" or "." and no matter what it will still pull every file on my site in .php format Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-988997 Share on other sites More sharing options...
teamatomic Posted January 5, 2010 Share Posted January 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989048 Share on other sites More sharing options...
PlagueInfected Posted January 5, 2010 Author Share Posted January 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989116 Share on other sites More sharing options...
teamatomic Posted January 5, 2010 Share Posted January 5, 2010 In your listdir function, refer to my earlier post as to what is ignored. To ignore folders use an if statement inside this if: if (is_dir($dir)){ like so if ($dir !='includes') HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989138 Share on other sites More sharing options...
PlagueInfected Posted January 6, 2010 Author Share Posted January 6, 2010 ok, but for the statement if I want to not include the includes folder would it need to be if(if_dir=!directory) { die(); } or something like that, I had a hard time coding stuff like that to say if you pull this folder, ignore it. Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989445 Share on other sites More sharing options...
crabfinger Posted January 6, 2010 Share Posted January 6, 2010 Try this line in your listdir function if ($fn != '.' && $fn != '..' && $fn != 'includes'){ // ignore these Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989448 Share on other sites More sharing options...
PlagueInfected Posted January 6, 2010 Author Share Posted January 6, 2010 ok I will, right now I got no time to try it lol I'm redoing my website Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989450 Share on other sites More sharing options...
crabfinger Posted January 6, 2010 Share Posted January 6, 2010 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'))); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989461 Share on other sites More sharing options...
PlagueInfected Posted January 6, 2010 Author Share Posted January 6, 2010 ace! looks good Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989464 Share on other sites More sharing options...
PlagueInfected Posted January 6, 2010 Author Share Posted January 6, 2010 I tried your sitemap, and what it did was list the pages in a decoded array Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989827 Share on other sites More sharing options...
crabfinger Posted January 6, 2010 Share Posted January 6, 2010 Well what my function is supposed to return is a structured array. Tell me what it gives you. Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989829 Share on other sites More sharing options...
PlagueInfected Posted January 6, 2010 Author Share Posted January 6, 2010 here man, check it out http://plagueinfected.com/sitemap.php Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989830 Share on other sites More sharing options...
crabfinger Posted January 6, 2010 Share Posted January 6, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989846 Share on other sites More sharing options...
PlagueInfected Posted January 6, 2010 Author Share Posted January 6, 2010 didn't work =/ Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989861 Share on other sites More sharing options...
crabfinger Posted January 6, 2010 Share Posted January 6, 2010 is the current copy on your site? Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-989946 Share on other sites More sharing options...
PlagueInfected Posted January 7, 2010 Author Share Posted January 7, 2010 yup Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-990106 Share on other sites More sharing options...
crabfinger Posted January 7, 2010 Share Posted January 7, 2010 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')); Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-990108 Share on other sites More sharing options...
laffin Posted January 7, 2010 Share Posted January 7, 2010 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()) ?> Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-990456 Share on other sites More sharing options...
PlagueInfected Posted January 7, 2010 Author Share Posted January 7, 2010 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')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-990484 Share on other sites More sharing options...
crabfinger Posted January 7, 2010 Share Posted January 7, 2010 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-990664 Share on other sites More sharing options...
PlagueInfected Posted January 8, 2010 Author Share Posted January 8, 2010 better take a suppository, it worked! thanks dude, been trying to code one for some time, damn this helps. Quote Link to comment https://forums.phpfreaks.com/topic/187265-how-to-code-into-inc/#findComment-990773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.