/*
sourceer_README.txt
Sourceer 1.3.2, 5 July 2022
Source code viewer
Copyright Santosh Patnaik
GPL v3 license
A PHP Labware internal utility - https://bioinformatics.org/phplabware/internal_utilities
*/
Download latest version from https://bioinformatics.org/phplabware/downloads/sourceer.zip
== Content ==========================================================
1 About
2 Usage
2.1 Basic
2.2 Advanced
2.2.1 Authentication and security
2.2.2 Parameters
2.2.3 Returned values
2.2.4 Layout and style
2.2.5 Non-Sourceer file security
3 Other
3.1 Upgrade
3.2 Change-log
3.3 Support
3.4 Known issues
3.5 Donate
== 1 About =========================================================
Sourceer is a highly configurable, single-file PHP software for web-based browsing of directory contents, with options to view source code of specified file-types or download them. File-sizes and last-modification dates are indicated if so optioned. Directory contents are listed alphabetically, with files grouped by file-types, but can be sorted by size or age. Source code display, by default, uses PHP's syntax highlighting function. However, other means such as the server-side, PHP-based 'Geshi', or the client-side, Javascript-based 'dpSyntaxHighlighter' scripts can easily be used.
Among other things, Sourceer is useful for presenting contents of software projects (a much simple alternative to 'Trac', 'PHPDoc', 'Doxygen', SVN/CVS systems, etc.). Sourceer was developed at PHP Labware (https://bioinformatics.org/phplabware) for this purpose. Sourceer's original code was based on Stuart Montgomery's 'DirPHP' v1.0 code.
Easy integrability and high customizability set Sourceer apart from other such software.
== 2 Usage ========================================================o
-- 2.1 Basic -------------------------------------------------------
Sourceer works with *PHP versions 5 and above*. Put 'sourceer.php' in the directory that is to be browsed. Change parameter values at top if desired -- see section:- #2.2.2. Change layout and style if desired -- see section:- #2.2.4. Also see section:- #2.2.1 for authentication setup, and section:- #2.2.5 for file-security measures.
-- 2.2 Advanced ---------------------------------------------------o
For formatting of source code (*syntax highlighting*, etc.) using 'Geshi', etc., instead of Sourceer (which uses PHP's 'highlight_string' function) set the 'hiliter' parameter of '$cfg' to the name of your custom PHP function to use. The function is passed the raw file source and some other parameters by Sourceer, and it is expected to return the formatted code along with optional CSS, Javascript or HTML footer content to Sourceer. Refer to the description for 'hiliter' in section:- #2.2.2.
An example of such `external` highlighting using this:- http://mihai.bazon.net/projects/javascript-syntax-highlighting-engine Javascript-based syntax highlighter:
...
$cfg['hiliter'] = 'myHl';
...
function myHl($in, $type, $path, $enc){
// Let Sourceer handle .txt file-type.
// Also we want only .css and .js handled this way.
if($type == 'txt' or ($type != 'css' and $type != 'js)){
return 0;
}
// Long code is too resource-intensive to syntax-highlight.
// Just return as plain-formatted, preserving white-spaces.
if(isset($in[30000])){
return array(str_replace(array(' ', ' ', ' ', "\n ", ' '), array("\n \n", "\n \n", ' ', "\n ", ' '), "\n". nl2br(htmlspecialchars($in, ENT_COMPAT, $enc))));
}
// Return array with formatted code, style and script declarations, etc.
return array(
// Formatted code
0 => '
'. htmlspecialchars($in, ENT_COMPAT, $enc). '
',
// Additional CSS style declaration for HTML head
1 => '',
// Additional Javascript declaration for HTML head
2 => '',
// Additional HTML declaration for HTML foot
3 => ''
);
}
You can copy the 'Sourceer' class and use it like using 'sourceer.php'. Also, the 'sourceer.php' file itself can be included in another script. Output of 'sourceer.php' can be obtained by including the file -- 'include("path/to/sourceer.php")' -- and then using such two lines of code either in the parent script or in 'sourceer.php' itself:
$var = new Sourceer($sec_dirs, $sec_files, $src_filetypes, $cfg, $presrc);
$var->work();
For displaying the Sourceer output `later`, use output buffering functions. E.g.:
$sourceer = new Sourceer($a, $b, $c, $d);
ob_start();
$sourceer->work();
$sourceer_out = ob_get_contents;
ob_end_clean();
...
echo $sourceer_out;
The arguments '$sec_dirs', '$sec_files', '$src_filetypes', and '$cfg' should be in the form of arrays (the arrays themselves can be empty; one or more elements of the '$cfg' array may not be defined -- see note on 'parameter values' below).
All code in sourceer.php that is between the ''../pics/nat', 'head'=>'
', 'foot'=>'
'), 0);
$out->work();
$cfg parameters can also be passed using 'set_cfg()' once the Sourceer object is created. Thus, e.g.,
$out = new Sourceer(array(), array('.htaccess'), array('php'), array('root'=>'../pics/nat', 'head'=>'
', 'foot'=>'
'), 0);
$sourceer->set_cfg('dl', 0);
$out->work();
.. 2.2.1 Authentication and security ...............................
There is only one user level for Sourceer. That is, all parameters are applied similarly to all. However, access can be allowed to only those providing the correct password, or, optionally, those at a permitted IP address. See section:- #2.2.2 for more.
To secure (hide or limit access to) certain files/directories, set the appropriate parameters (section:- #2.2.2).
Sourceer filters the URL query string ('$_GET') values for filenames, so one cannot attempt to move up-root using strings like '/..' or '//'. If up-root traversal is permitted (section:- #2.2.2), then one may use the string '/..' in the 'Sl' parameter of the query string.
Also see section:- #2.2.5.
.. 2.2.2 Parameters ...............................................o
Valid PHP code can be used for the parameter values.
*$src_filetypes*
Specifies file-types (extensions) whose source code is viewable. Such files are optionally downloadable as well. It is an array of key-value pairs where the keys are lower-cased extension names and the values are file-types, like 'array("php"=>"php", "php4"=>"php", "htaccess"=>"txt", "txt"=>"txt", "js"=>"js")'. Thus, as per this example, an '.htaccess' file and a 'hello.txt' file both have the 'txt' file-type (but different file-extensions -- '.htaccess' and '.txt'). An empty key '""' can be used to refer to files that have no extension ('""=>"txt"').
Note that other file-types (e.g., 'jpg') are `downloadable` as well (but not through Sourceer/PHP) as links shown for such files can be clicked on for the browser to receive.
*$sec_files*
Specifies `secured` files that, depending on '$cfg', may not be listed or looked into. Elements are paths to such files relative to the root ('$cfg["root"]') directory. E.g., 'array("./.htaccess", "../.htaccess", "./pics/.HTACCESS")'.
You can use PHP PCRE-compatible regular expressions to indicate many filenames in a simpler way. To do so, put this in the array: 'array("x", "y", ...)' where 'x', 'y', etc., are the expression patterns with delimiters. E.g., 'array("./.htaccess", "../.htaccess", "./pics/.HTACCESS", array("!\.htaccess$!i", "!\.ini$!i"))'. The expression patterns should be PHP/PCRE-compatible and should use the exclamation mark ('!') as the delimiter.
To secure the 'sourceer.php' file itself, either set the root directory below 'sourceer.php' and turn off up-root browsing (see '$cfg' below), or put the relative path to 'sourceer.php' in '$sec_files'.
*$sec_dir*
Similar to '$sec_files' but for directories
Consider this file structure:
+---------------------------------------+
__________dir___www___pics___.HTACCESS
| | |
|___x.ini | |___.htaccess
| |
| |___sourceer.php
|
|___.htaccess
+---------------------------------------+
Here, sourceer.php is in 'dir/www/' and if the '$cfg' 'root' parameter (more below) is set to '.', then the effective root is 'www'. With the '$sec_files' set to 'array("./.htaccess", "../.htaccess", "./pics/.HTACCESS", array("!\.htaccess$!i", "!\.ini$!i"))', 'www/.htaccess' and 'www/pics/.HTACCESS' are both secured. Even if there is no './../.htaccess' in the array, the file 'dir/.htaccess' is secure, like all files in 'dir/' if the 'up_root' parameter is set to '0'.
However, if 'up_root' is '1', then unlike other files in 'dir/', the '.htaccess' file gets secured. With '$sec_dirs' set to 'array("./pics")', or to 'array(array("\pics$\"))', 'www/pics/' is secured.
The file 'x.ini' at './../..' can be secured by having 'up_root' set to '0' (most restrictive), or the element 'array("`(^|\.)/../..(/|$)`")' in '$sec_dirs' (less restrictive), or the element './../../x.ini' in '$sec_files' (least restrictive). There are obviously many other possibilities with different levels of restrictiveness.
*$cfg*
Specifies settings. It is an array with none, one or more of following elements with user-settable values. When the elements are not specified, Sourceer uses certain default values.
'auth' - '1' if a password is needed unless the user's IP address is in the 'ok_ips' array. The MD5 hash of the password prefixed with 'sourceer' is specified in 'hash'. Default: '0'
'base' - base URL. E.g., 'http://domain.com/sourceer.php', 'sourceer.php', 'https://localhost', etc. It can have a query string. E.g., 'http://domain.com/index.php?page=codes&guest=1' (note the '&' is not '&' -- don't use entities). Default: code to auto-determine correct URL
'charset' - character encoding, for HTML rendering purposes only. Probably best if set to encoding used the server's filesystem. Default: 'utf-8'
'compress' - gzip-compress output to reduce bandwidth. Sourceer will auto-check other parameters (e.g., if browser accepts compressed content). To turn off, set to '0'. Default: '1'
'css' - style declarations. If not set, or set to '0', default is used. If set as an array, like 'array("div.p {color: red;}")', will get appended to default CSS. Default: (complete declarations)
'cookie' - cookie name to use; needed when using password for authentication; used for auto-login for password-authentication if the user revisits within an hour. Default: 'sourceer'
'date_type' - specifies the date format; should be compatible with PHP's 'date()' function (like 'm-d-y'). Default: 'm/d/y'
'dl' - '1' if '$src_filetypes' files can be downloaded. Default: '1'
'file_info' - '0' if file-sizes and modification times are not to be shown. Default: '1'
'foot' - HTML string to append to the output. Can be empty, '