aboutsummaryrefslogtreecommitdiff
path: root/cgi-bin/guest_book.py
blob: 97a7403afdc3dc02d25b6b52e6b54902160ac2a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! /usr/bin/env python3

import cgi
import json
import datetime
import html

def handle_fields():
    form = cgi.FieldStorage()

    name = form.getvalue("name")
    url = form.getvalue("url")
    message = form.getvalue("message")

    # Nothign was submitted.
    if name is None and url is None and message is None:
        return ""

    if name is None:
        return "<p>name is required</p>"
    elif message is None:
        return "<p>please write a silly something (:</p>"

    url = "" if url is None else url

    # Make the names safe.
    name = html.escape(name)
    url = html.escape(url)
    message = html.escape(message)

    guest_book = []

    # Open data if already there.
    try:
        with open("guest_book.json", "r")  as fp:
            guest_book = json.load(fp)
    except FileNotFoundError:
        pass

    # Already in list.
    for guest in guest_book:
        if guest["name"] == name and guest["url"] == url and guest["message"] == message:
            return "<p>You already been added</p>"

    date = datetime.datetime.now()
    guest_entry = {"name": name, "url": url, "message": message, "date": date.strftime("%B, %d %Y")}

    # Dump guest to file.
    with open("guest_book.json", "w") as fp:
        guest_book.append(guest_entry)
        json.dump(guest_book, fp, indent=4)

    return "<p>You been added to the guest book yippe (:</p><img src=\"../images/yippee.gif\" alt=\"yippee!\"/>"

def get_guest_html_from_list():
    guest_html = ""

    try:
        with open("guest_book.json", "r")  as fp:
            for guest in json.load(fp)[::-1]:
                guest_table = """
                    <table border="1" width="60%">
                        <tr>
                            <td>
                                <b>{name}</b> <a href="{url}" target="_blank">{url}</a> --- signed {date}
                            </td> 
                        </tr>

                        <tr>
                            <td><p>{message}</p></td>
                        </tr>
                    </table>
                """

                guest_table = guest_table.format(name=guest["name"], url=guest["url"],
                    date=guest["date"], message=guest["message"])
                guest_html += guest_table
    except FileNotFoundError:
        guest_html =  """
            <table border="1" width="60%">
                <tr>
                    <td>
                        <p>No guest have been added ): But you can be the first!</p>
                    </td>
                </tr>
            </table>
        """

    return guest_html

def display_html(fields_reponse):
    print("Content-Type: text/html")
    
    html_text = """
        <!DOCTYPE html>
        <html>
        
        <head>
            <title>Guest Book</title>

        <style>
            
        body {{
            color: black;
            background-image: url('../images/guest_book_background.png');
        }}

        table {{
            color: black;
            background-color: #bebebe;
            margin-top: 10px;
            margin-bottom: 10px;
            margin-left: 10px;
            margin-right: 10px;
        }}

        </style>
        </head>

        <body>
            <a href="../index.html"><img src="../images/back_home.png" alt="Back to home page"/></a>
            
            <center>
                <table border="1" width="60%">
                    <tr>
                        <td>
                            <h3>Sign my fucking guest book</h3>
                            <form action = "guest_book.cgi" method = "get">
                                <lable for = "name" maxlength="100">Name</lable>
                                <input type = "text" name = "name"/>
                                <br/>
                                <lable for = "url" maxlength="256">Website (Optional)</lable>
                                <input type = "text" name = "url"/>
                                <br/><br/>
                                <lable for = "message">Write a little silly something</lable>
                                <br/>
                                <textarea type = "text" name = "message" rows = "4" cols = "40" maxlength="512"></textarea>
                                <br/>
                                <input type = "submit" value = "Fucking submit"> <b>You can't delete/edit it afterwards</b>
                            </form>
                            <p>
                                <b>Be nice! Dont be a fucking ass.</b>
                            </p>
                            {fields_reponse}
                        </td>
                    </tr>
                </table>

            {guest_html}
            </center>
        </body>
        </html>
    """
    
    html_text = html_text.format(
        fields_reponse=fields_reponse,
        guest_html=get_guest_html_from_list()
    )

    print(html_text)

fields_reponse = handle_fields()
display_html(fields_reponse)