Please read the posts in the Announcements section about the current status of Cumulus development now that I have retired

Please read this post before posting

Latest Cumulus release v1.9.4 (build 1099) - Nov 28 2014
Latest Cumulus MX release - v3.0.0 build 3043 Jan 20 2017. See this post for download

PHP Source listing suggestion

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderators: daj, TNETWeather

Posts: 378
Joined: Fri Jul 27, 2012 11:29 am
Weather Station: Maplin based, but customised
Operating System: off-line Vista and W10 PCs
Location: UK

PHP Source listing suggestion

Postby sfws » Fri Aug 11, 2017 4:25 am

A number of programmers have contributed PHP scripts to this support forum, and some include a self downloader.

Code: Select all

if ( isset($_REQUEST['source']) && strtolower($_REQUEST['source']) == 'view' ) {
 header('Pragma: public');
 header('Cache-Control: private');
 header('Cache-Control: no-cache, must-revalidate');
 header('Content-type: text/plain');
 header('Accept-Ranges: bytes');
 header("Content-Length: $download_size");
 header('Connection: close');

There does not appear to be consistency over the query-string used to initiate the source list, that example uses "&source=view", but I have seen the variants "&sce=view", "&view=sce", "&view=src" and "&src=view" used amongst a handful of others, with even "?view=getorfmiland" on a specific script.

Most people who implement PHP on their web-sites do so because you can use several 'require' or 'include' to include several bits of common content on as many of their web pages as appropriate. The widely used self-downloader only lists the PHP source of a single file, not all the PHP sources involved in creating that web page. If you try each of the query-strings I have just quoted on a page you are looking at on the internet, it may be ignored, or it might list an included file rather than the main script producing the web page. It depends which script file includes the above code, and if more than one of the php script files includes the above code, I suspect it is the first file parsed that is actually listed.

I have implemented an alternative, a way to view the source of any of the scripts making up any web page, and thought I would share it, in the vain(?) hope that others might consider it as 'best practice' to copy.
* I wanted to be able to decide script by script whether its source could be viewed (to exclude those containing passwords or other material that you would expect to remain hidden).
* I wanted to be able to choose which of the various scripts in any web page got listed by selecting the relevant script using a query-string.
* I wanted a way to optionally list the range of possible query-strings (for when I could not remember which scripts were included in a particular web page).
* Finally I needed the downloader to know the path names for each file as my individual scripts are not all in the same directory.

In those individual scripts that offer viewing its source, I include something equivalent to this (you can tailor it according to which query-string selector you prefer):

Code: Select all

   include_once 'sourceView.php';
   $this_file = "xxxx.php";
   add_source(__FILE__, $this_file); // Tells included file that it can display the source of the current file
 # __FILE__ represents the full path name, $this_file is the final part of the file-name and is what you specify in the query-string to request the source of this file
   if (
      isset($_GET['view']) && $_GET['view'] == $this_file or
      isset($_GET['sce']) && $_GET['sce'] == $this_file or
      isset($_REQUEST['src']) && $_REQUEST['src'] == $this_file)
            display_source ($this_file); // Tells included file that it is the source of this file that you want now

The two functions add_source(x,y) and display_source(y) are defined in the included file 'sourceView.php' (mine is slightly more complicated, because I add some formatting), that also defines a global array and can use an optional global variable (that I assign an arbitrary value to, in one of the scripts making up the page, when I want to be prompted with list of query-strings available):

Code: Select all

function display_source($filenameRequest)   {
   global $fileNameArray;
    // see other function for how this array set up
   $pathNameArray = array_flip ($fileNameArray);    // interchanges keys and values
   $filenameShow = array_search("'" . $filenameRequest . "'",$pathNameArray);
    // search for full path corrsponding with supplied file name
   $pathnameShow = substr($filenameShow,(-1 * strlen($filenameRequest)));
    // strip file name from path to get directory to choose
        $download_size = filesize($filenameRequest);
        header('Pragma: public');
        header('Cache-Control: private');
        header('Cache-Control: no-cache, must-revalidate');
        header("Content-type: text/plain");
        header("Accept-Ranges: bytes");
        header("Content-Length: $download_size");
        header('Connection: close');
        readfile("$filenameRequest",true); // outputs source

function add_source($pathName,$fileName)
   global $fileNameArray; $list_sources_available;
// Note shared array holding path-names and file-names, and note optional variable which if included in calling script will produce output in next line
   if(isset($list_sources_available))   echo "<small>Use query-string <span class='blue'>?src=" . $fileName ."</span> to view source &nbsp; &nbsp; </small>";
 // Tailor the 'src' to whatever you prefer as query-string parameter key
   if (!isset($fileNameArray) or !in_array($fileName, $fileNameArray)) $fileNameArray["'" . $fileName . "'"] = "'" . $pathName . "'";
 // If current script not already in array, add it

I am not quoting any licence with my code, as it basically just uses standard PHP (with very little interllectual design content), and I would be pleased if this was used more widely to make it easier for amateurs experimenting with programming like myself to share code. However, it did need an eureka moment for me to work out that I could deal with my path-name uncertainty by storing the path name in an array together with a shorter-name appropriate to use in a query-string, so I would not want anybody else to use that little bit that is my idea for their own profit!

Return to “Web site - General”

Who is online

Users browsing this forum: No registered users and 5 guests