SourceViewer 1.1 beta

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I made SourceViewer a while ago to view the source on my site (maxalbert.com). I'm working on the next version, and have run into a glitch.

I'm linking to files that are included into a PHP file, so that visitors can see the source to those included files. I have a very beta version of this working. The glitch comes into play when trying to evaluate the path to these included files.

The beta I'm working with is in the root web directory. Most (if not all) included files are using absolute paths. I'm racking my brain trying to come up with something that will accomodate relative paths. I also want allow the SourceViewer file to be placed somewhere other than the root web directory. And my biggest worry, paths to included files may contain variables.

I am trying to make this script as flexible as possible, so those who may desire to use the script, won't have to go through and edit any include() or require() functions to work with my script.

Examples:
Example 1 - /index.php, contains 2 included files, also both at root web
Example 2 - about_me/photos.php, contains 2 included files, both at root web
SourceViewer 1.1 beta - Please do not steal my code, observe the copywrite.

Any help appreciated.

Mark Hensler
If there is no answer on Google, then there is no question.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

I wish I could help you Mark, you know more about PHP than me! Does PHP have an equivalent method to Server.MapPath in ASP? That way everything could be relative and dynamic, including the SourceViewer file.

PJ | Are we there yet?
pjboettcher.com

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

realpath() is the PHP equivalent to ASP's Server.MapPath

The more I think about this, the more my head hurts. Problems I see...
1) included files being relative to the parent process
2) variables in include path
3) included files residing outside the public_html, but in the include_path

Just struck me, I can evaluate the path as best I can, then do an is_file() to see if I have the correct path. If I do, create the link. If not, don't link it.

Mark Hensler
If there is no answer on Google, then there is no question.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'll give you an outline (psuedo-code) of what I've got now, then see if anyone can come up with something better.

1) pattern match for all included files, load into array1

2) loop through array built in #1
3) if filename starts with "/" (using an absolute path)
4) check if file exists, if so, store in array2
5) else (using relative path)
6) change to directory of file whose source is being printed
7) change to directory of found included file (line 1)
Cool create var containing the current working directory appended by filename of found included file
9) check if file (in line 10) exists, if so store it in array2
10) end if
11) end loop

12) extract unique paths which were stored in array2 on lines 5 and 11

13) loop through array created on line 13
14) create link to file, and store in array3
15) end loop

16) print array of included files

If your familiar with PHP, you may prefer looking at the code here. Lines 303 to 403.

My feeble little mind thinks that this will accomodate files that are included using absolute paths or relative paths. I'm thinking that it's near impossible to handle files with vars in the path.

Any thoughts?

Mark Hensler
If there is no answer on Google, then there is no question.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

Logic looks good, as for the variable in the path problem, if PHP is like ASP then it will be impossible, since include files (and their vars) are processed before any ASP code.

That's some neat code!

PJ | Are we there yet?
pjboettcher.com

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.