-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditcourse.php
More file actions
238 lines (190 loc) · 6.47 KB
/
editcourse.php
File metadata and controls
238 lines (190 loc) · 6.47 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/*
Runner's Medium
http://www.runnersmedium.com/
editcourse.php
view & update course info and map route
copyright 2009 Mark Baltrusaitis <http://josieprogramme.com>
*/
require('lib/base.php');
$user->signinCheck(signin());
$stop = false;
$prompt = null;
// user
$theid = mysql_real_escape_string($user->ID());
$name = $city = $distance = $comments = $params = $location = null;
if (!isset($_GET['id']) || !notempty($_GET['id'])) :
$error = 'course ID not provided';
$stop = true;
elseif (!is_numeric($_GET['id'])) :
$error = 'invalid course ID';
$stop = true;
else :
// id provided
$courseid = $_GET['id'];
if (isset($_POST['action']) && $_POST['action'] == 'Save') {
// get post data
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
// give the course a generic name
if (strlen($name) < 1) {
$name = 'My Course';
}
if (isset($_POST['city'])) {
$city = $_POST['city'];
}
if (isset($_POST['distance'])) {
$distance = $_POST['distance'];
}
if (isset($_POST['comments'])) {
$comments = str_replace("\r\n", "\n",$_POST['comments']);
}
if (isset($_POST['params'])) {
$params = $_POST['params'];
}
// validate
if (strlen($name) > 32) {
$error = 'name must be less than 32 characters';
} elseif (strlen($city) < 1) {
$error = 'Enter the city where your course is located';
} elseif (strlen($city) > 55) {
$error = 'city must be less than 55 characters';
} elseif (!is_numeric($distance)) {
$error = 'you must enter the distance for your course';
} elseif ($distance < 0 || $distance > 10000) {
$error = 'invalid distance';
} elseif (strlen($comments) > 90) {
$error = 'comments cannot be more than 90 characters';
} elseif (strlen($params) > 65535) {
$error = 'course error';
} else {
// escape
$mysql['userid'] = mysql_real_escape_string($user->ID());
$mysql['courseid'] = mysql_real_escape_string($courseid);
$mysql['name'] = mysql_real_escape_string($name);
$mysql['dist'] = mysql_real_escape_string($distance);
$mysql['city'] = mysql_real_escape_string($city);
$mysql['comments'] = (notempty($comments)) ? '\''.mysql_real_escape_string($comments).'\'' : 'NULL';
$mysql['params'] = mysql_real_escape_string($params);
// update
$conn->query("UPDATE courses SET name = '{$mysql['name']}', distance = {$mysql['dist']}, city = '{$mysql['city']}', comments = {$mysql['comments']}, params = '{$mysql['params']}'
WHERE user = {$mysql['userid']} AND id = {$mysql['courseid']}");
// run it?
$message = '<a href="'.root().'newrun?course='.format($courseid).'">Successfully updated course. Click here to run it.</a>';
}
} else {
// select contest name
$result = $conn->query('SELECT name, distance, city, comments, params FROM courses
WHERE id = '.mysql_real_escape_string($courseid).' AND user = '.mysql_real_escape_string($user->ID()));
if ($conn->rowCount($result) > 0) {
// grab data
$line = $conn->fetchAssoc($result);
$name = $line['name'];
$distance = $line['distance'];
$city = $line['city'];
$comments = $line['comments'];
$params = $line['params'];
} else {
$error = 'course not found';
$stop = true;
}
}
endif; // id provided
if (!$stop) {
$root = root();
$gmapKey = GMAP_KEY;
$scripts = <<<EOD
<script type="text/javascript" src="{$root}js/tabs.min.js"></script>
<script src="http://maps.google.com/maps?file=api&v=2&key={$gmapKey}" type="text/javascript"></script>
<script src="{$root}js/gmap.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(
function (){
jQuery('input').keypress(function (event){ return event.keyCode == 13 ? false : true; });
jQuery("#location").keyup(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) {
showAddress($('location').value);
}
});
}
);
</script>
EOD;
$body = 'onload="initialize()" onunload="GUnload()"';
}
$title = 'Runner\'s Medium - Edit ';
if (is_null($name)) {
$title .= 'course';
} else {
$title .= format($name);
}
require('header.php');
?>
<div id="content">
<?php if (!$stop) : ?>
<ul id="tabnav">
<li><a class="select" href="#gmapform">Map</a></li>
<li><a href="#infoform">Course Info</a></li>
</ul>
<br class="clear">
<?php endif; // dont show tabs ?>
<?php
messages($error, $message);
if (!$stop) :
?>
<form action="" method="post" onsubmit="return saveRoute();">
<fieldset>
<div id="infoform" style="display:none">
<label for="name">Name</label>
<input name="name" id="name" type="text" value="<?php echo format($name); ?>" />
<label for="city">City and/or State</label>
<input name="city" id="city" type="text" value="<?php echo format($city); ?>" />
<label for="distance">Distance</label>
<input name="distance" id="distance" type="text" value="<?php echo format($distance); ?>" />
<span id="units"><?php echo format($user->getUnits(true)); ?></span>
<label for="comments">Comments <span id="chars"><?php
$chars = 90-strlen(str_replace("\r\n", "\n", $comments));
if ($chars < 90) {
if ($chars < 0 ) {
echo '<span class="error">'.$chars.'</span> characters remaining';
} else {
echo $chars.' characters remaining';
}
}
?></span></label>
<textarea name="comments" id="comments" cols="100%" rows="10" onkeydown="text('comments')"><?php echo format_a($comments); ?></textarea>
</div>
<div id="gmapform">
<div id="gmap-wrapper">
<div id="overlay">
<label for="location">Enter a location, then click at every interval of your route.</label>
<input name="location" id="location" type="text" value="<?php echo format($location); ?>" />
<br />
<span id="status">
<?php if (is_null($distance)) {
echo '0.0';
} else {
echo format($distance);
} ?> <?php echo format($user->getUnits(true)); ?></span>
<span class="actions">
<a onclick="startOver()">Start over</a> / <a onclick="stepBack()">Step back</a>
</span>
</div>
<div id="gmap"></div>
</div>
</div>
<input type="hidden" name="params" id="params" value="<?php echo $params; ?>" />
<input name="action" type="submit" value="Save" class="button" />
</fieldset>
</form>
<?php
elseif (!is_null($prompt)) :
echo $prompt;
endif; // no errors
?>
</div>
<?php
require('footer.php');
?>