18thstreetmike / codaclientphp

The PHP drivers for connecting to CodaServer.

This URL has Read+Write access

codaclientphp / tester.php
100644 174 lines (167 sloc) 4.865 kb
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
165
166
167
168
169
170
171
172
173
174
<?php
 
include_once('codaserver.php');
session_start();
//unset($_SESSION['database_connection']);
// if the form is posted and no connection is present, make one
if ($_POST && (!isset($_SESSION['database_connection']) || !$_SESSION['database_connection'])) {
if (empty($_POST['hostname']) || empty($_POST['port'])) {
echo "Please enter a valid hostname and port. Hit back and try again!";
die;
}
$_SESSION['database_connection'] = codaserver_connect($_POST['hostname'], $_POST['port'], $_POST['username'], $_POST['password']);
if ($_SESSION['database_connection'] === false) {
unset($_SESSION['database_connection']);
echo "You didn't enter a valid username and password for the desired host. Please hit back and try again!";
die;
}
}
// var_dump($_SESSION);
// check to see if there is an incoming query
if (isset($_POST['coda_query'])) {
// try to run it
$result = codaserver_query($_SESSION['database_connection'], $_POST['coda_query']);
 
// if it's false, check to see if the error code is about this session timing out. if so, reconnect and rerun.
if ($result === false) {
//echo 'here';
$errors = codaserver_errors();
if (count($errors) == 1) {
if ($errors[0]['errorcode'] == '1005') {
if (empty($_POST['hostname']) || empty($_POST['port'])) {
echo "Please enter a valid hostname and port. Hit back and try again!";
die;
}
$_SESSION['database_connection'] = codaserver_connect($_POST['hostname'], $_POST['port'], $_POST['username'], $_POST['password']);
if ($_SESSION['database_connection'] === false) {
unset($_SESSION['database_connection']);
echo "You didn't enter a valid username and password for the desired host. Please hit back and try again!";
die;
}
$result = codaserver_query($_SESSION['database_connection'], $_POST['coda_query']);
}
}
}
}
//var_dump($result);
 
?>
<html>
<head>
<title>CodaServer Driver Tester</title>
</head>
<style>
body {
font-family: Helvetica, Arial, san-serif;
font-size: 10pt;
}
#content {
width: 600pt;
}
#connection-info div {
margin: 3px;
float: left;
}
table {
font-size: 10pt;
border-left: 2px solid black;
border-top: 2px solid black;
border-bottom: 1px solid black;
border-right: 1px solid black;
}
table thead {
background-color: #0A59AF;
color: white;
}
table th {
padding: 4px;
border: solid 0px black;
border-right-width: 1px;
border-bottom-width: 1px;
}
table td {
padding: 4px;
border: solid 0px black;
border-right-width: 1px;
border-bottom-width: 1px;
}
</style>
<body>
<div id="content">
<h1>CodaServer Driver Tester</h1>
<p>
You can use this page to connect to and run queries against a CodaServer instance. It is a proof of concept and helps to let
you know if your installation is set up correctly.
</p>
<form method="post">
<fieldset>
<legend>Connection Information</legend>
<div id="connection-info">
<div>
<label for="hostname">Hostname</label><br />
<input name="hostname" id="hostname" size="20" value="<?= $_POST['hostname'] ? $_POST['hostname'] : 'localhost' ?>" />
</div>
<div>
<label for="port">Port</label><br />
<input name="port" id="port" size="5" value="<?= $_POST ? $_POST['port'] : '3407' ?>" />
</div>
<div>
<label for="username">Username</label><br />
<input name="username" id="username" size="20" value="<?= $_POST['username'] ?>" />
</div>
<div>
<label for="password">Password</label><br />
<input type="password" name="password" id="password" size="20" value="<?= $_POST['password'] ?>" />
</div>
</div>
</fieldset>
<br />
<fieldset>
<legend>Coda Query/Command</legend>
<textarea name="coda_query" rows="6" cols="95"><?= $_POST['coda_query'] ?></textarea>
<input type="submit" style="float: right; margin-top: 5px;" value="Submit" />
</fieldset>
</form>
<?php
 
if ($_POST && is_array($result)) {
$fields = codaserver_fetch_fields($result);
?>
<h2>Results</h2>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<?php
foreach ($fields as $field) {
echo '<th>'.$field['columnname'].'</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
while ($row = codaserver_fetch_row($result)) {
echo "<tr>";
foreach($row as $column) {
echo "<td>&nbsp;".$column."&nbsp;</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
<?php
} else if ($_POST && $result === true) {
?>
<h2>Success!</h2>
<?php
} else if ($_POST) {
?>
<h2>The Following Errors Occurred</h2>
<ul>
<?php
$errors = codaserver_errors();
foreach($errors as $error) {
echo '<li><strong>ERR'.$error['errorcode'].':</strong> '.$error['errormessage'].'</li>';
}
?>
</ul>
<?php
}
 
?>