#!/usr/bin/env python3
#=====================================================================================
# Retrieve a File from the server
#=====================================================================================
import cgi                                  #import cgi module

print("Content-Type: text/html \n")         #required http response header (w/ extra line)

elements = cgi.FieldStorage()               #obtain the http for elements

file = elements.getvalue('file') or ""      #entry field             
 
msg = ''                                    #global variable

#-------------------------------------------------------------------------------------
# Function to validate entry fields
#-------------------------------------------------------------------------------------
def validate():
    global msg                             #needed to modify global variable
                                           #no need if access to global is read only
    if file=='':
        msg = '*** Please enter a file name ***'

#-------------------------------------------------------------------------------------
# Function to read a file from the server
#-------------------------------------------------------------------------------------
def getFile(file):
    global msg                              #needed to modify global variable

    try:
        input = open(file,'r')              #Open file for reading    
        data  = input.readlines()           #read entire file into array
        input.close()                       #close the file       
    except:
       msg  = "Could not retrieve the file"
       data = ""

    return(data)
        
#-------------------------------------------------------------------------------------
# Function to display the file
#-------------------------------------------------------------------------------------
def display(data):
    global msg                             #needed to modify global variable
                                           #no need if access to blobal is read only
    print('''
        <!DOCTYPE html>
        <html>
        <head>
        <title>Get URL</title>
        <style type="text/css">
            #Layer3 {position:relative; left:60px; top:10px; width:790px; height:110px;}
            .style4 {font-family: Arial, Helvetica, sans-serif; font-size:16px; font-weight: bold;}
            span    {font-weight:bold; cursor:pointer}
        </style>
        </head>
        <body bgcolor='lightyellow'>
        <div id='Layer3'>

        <form METHOD=post ACTION=getFile.py>                
        <fieldset style='width:730'>
        <legend>Get a File</legend> 

        <table border='0'>
        <tr>
        <td> File Name </td>
    ''')
    print("<td><input type='text' id='file' name='file' size='75' value='" + file + "'>") 
    print(" use absolute path")

    print(''' 
        </tr>
        <tr>
        <td></td>
        <td><input type='submit' value='  Retrieve  '>
    ''')
    print("<font color='red'>" + msg + "</font></td>")
    print('''
        </tr>
        </table>       
        </fieldset>
        </form>
        </div>
        <i>Example:</i> 
        <span onclick=document.getElementById('file').value=this.innerHTML>/etc/passwd</span>
        <br>
        <i>Example:</i> 
        <span onclick=document.getElementById('file').value=this.innerHTML>/home/sultans/data/state</span>
        <br>
        <i>Example:</i> 
        <span onclick=document.getElementById('file').value=this.innerHTML>/home/sultans/data/cust_order.file</span>
        <hr>
    ''')

    for line in data:                           #print the retrieved file
        print(line, '<br>')          

#-------------------------------------------------------------------------------------
# Main code
#-------------------------------------------------------------------------------------

data = ''

if elements:                                    #if data was entered in the form 
    validate()                                  #validate entered data                            
    data = getFile(file)                        #retrieve the file from the server

display(data)                                   #call display function


#=== link to see the python code ================================================
import os
import sys
sys.path.insert(0,'/home/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#================================================================================