-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.php
522 lines (486 loc) · 20 KB
/
setup.php
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Set Up Your Account</title>
<link type="text/css" href="script/css/common.css" rel="stylesheet" >
<script src="script/js/setup.js"></script>
</head>
<body class="bodyMain">
<?php
require 'script/php/universal.php';
require 'script/php/header.php';
printHeader();
$sitedb=new db("site");
$userdb=new db("user");
common($sitedb);
//$cookie=new cookies($userdb);
//$usrverify=$cookie->valid;
echo '</div>';
?>
<br/>
<div class="maincontent">
<?php
printmaincontent($sitedb,$userdb);
?>
</div>
<?php
//some global variables
$fail=FALSE;
$addnodes_count=0;
/**
*
* @param dbconnect $sitedb database connection to site
* @param string $token token passed
* @return mixed boolean.string
*/
function verifySource($sitedb,$token=NULL){
$tstatus=FALSE;
$uid='.';
if($token){
$con=$sitedb->start();
$token= mysqli_real_escape_string($con,$token);
if (!mysqli_connect_errno()){
$ip=$_SERVER['REMOTE_ADDR'];//filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
$query="SELECT * FROM tracking WHERE (token='$token' AND ip='$ip')";
if($result=mysqli_query($con, $query)){
while ($row = mysqli_fetch_array($result)){
$tstatus=TRUE;
$uid=$row['error'];
}
}
}
if(!$tstatus) {
header("Location:".getRoot());
exit();
}
}
return $tstatus.'.'.$uid;
}
/**
*
* @global boolean $fail
* @global db $userdb
* @param bigint $userid the userid of user
*/
function initiateupload($keyid,$mail,$uid){ //starts uploload script
global $fail;
$photo= uploadit("profilepic");
if($photo) {
// $photo= getRoot()."/".$photo;
$cid=getcontentid("profile_pic");
if($cid==0){
$fail=TRUE;
}
// $con= $userdb->start(); //Initialising Connection
if(!$fail){ //If Connection Established Sucessfully
$photoinfo=array('link'=>$photo,'id'=>$cid,'type'=>"profile_pic",'date'=>date('j F Y'),'time'=>date('g:i A'),'keyid'=>$keyid);
writecontentid($photoinfo,$uid);
startSession($keyid,$mail);
echo '<script type="text/javascript">
window.location.replace("'.getRoot().'/home.php");
</script><noscript><a href="/home.php">Click here</a> to continue</noscript>';
}
else if($fail){
echo "<a>Upload File Transfer Failed!";
}
}
else{
echo 'upload fail'; // echo "<a>Uncomplete Form</a>";
}
}
/**
* write the contrent id of user to XML file
* @param array $photo contentid to be written
*/
function writecontentid($photo,$uid){
global $userdb;
$query='INSERT INTO `photos`(`url`, `time`, `type`, `id`, `owner`) VALUES ("'.$photo["link"].'",null,"'.$photo["type"].'",'.$photo["id"].',"'.$photo["keyid"].'.'.$uid.'")';
mysqli_query($userdb->start(), $query);
/* $filename="usr/content/post/".$photo['keyid']."_photo.xml";
if (!file_exists($filename))
{
//create the file
$handle=fopen($filename, 'w');
fwrite($handle,'<?xml version="1.0" encoding="UTF-8"?>
<BASE><PHOTO id="'.$photo['id'].'" type="'.$photo['type'].'" sno="1"><URL>'.$photo['link'].'</URL><DATE>'.$photo['date'].'</DATE><TIME>'.$photo['time'].'</TIME></PHOTO></BASE>');
fclose($handle);
//initialise xml writer
/* $xmlwriter=new XMLWriter();
$xmlwriter->openUri("usr/info/s.xml");
//start document
$xmlwriter->startDocument($version='1.0', $encoding='UTF-8');
$xmlwriter->setIndent(FALSE);
$xmlwriter->startElement("BASE");
$xmlwriter->startElement("PHOTO");
//attributes
$xmlwriter->writeAttribute("id",$photo['id']);
$xmlwriter->writeAttribute("type",$photo['type']);
$xmlwriter->writeAttribute("sno", 1);
//content
$xmlwriter->startElement("URL");
$xmlwriter->writeRaw($photo['link']);
$xmlwriter->endElement();
$xmlwriter->startElement("DATE");
$xmlwriter->writeRaw($photo['date']);
$xmlwriter->endElement();
$xmlwriter->startElement("TIME");
$xmlwriter->writeRaw($photo['time']);
$xmlwriter->endElement();
//end document
$xmlwriter->endElement();
$xmlwriter->endElement();
$xmlwriter->endDocument();
$xmlwriter->flush();*/
}
function startsession($key,$mail){
global $userdb;
$con=$userdb->start();
$status=FALSE;
if($userdb->status){
$query="SELECT * FROM user WHERE email='$mail'";
if($result=mysqli_query($con, $query)){
while ($row = mysqli_fetch_array($result)) {
$grp=$row['groupId'];
$status=TRUE;
}
if($status){
$status=FALSE;
$query="SELECT * FROM user_$grp WHERE email='$mail'";
if($result= mysqli_query($con, $query)){
while ($row = mysqli_fetch_array($result)) {
$status=TRUE;
$userid=$row['userId'];
}
if($status){
$cookies=generateRandomString(20);
insertcookie($cookies,"login.".$userid,$grp,$key,$mail,$userdb);
$day=60;
setcookie("lemon", $cookies, time()+$day*24*60*60);
}
}
}
}
}
}
/**
* add the cookie to database for further use
* @param type $cookies cookie genrated
* @param type $userId user id
* @param type $gid group id
* @param type $key secret key of user
* @param type $mail user mail
*/
function insertcookie($cookies,$userId,$gid,$key,$mail,$db) {
$con= $db->start();
$ip= filter_input(INPUT_SERVER, "REMOTE-ADDR",FILTER_VALIDATE_IP);
$userId=$ip.".".$userId;
if(!mysqli_connect_errno()){
$query="DELETE FROM `cookies` WHERE userId='$userId'";
mysqli_query($con, $query);
$query="INSERT INTO `cookies`(`id`, `userId`, `groupId`, `key`,`time`) VALUES ('$cookies','$userId','$gid','$key',null)";
mysqli_query($con, $query);
if(mysqli_errno($con)==1062){
startSession($gid, $mail,$db);
}
if(mysqli_errno($con)) {
error(mysqli_error($con),"l.php",180);
}
}
}
/**
*
* @global boolean $fail
* @param string $id the id/ name of the file
* @return string
*/
function uploadit($id)
{
global $fail;
$allowedExts = array( "jpeg", "jpg", "png","JPEG","JPG","PNG");
$temp = explode(".", $_FILES[$id]["name"]);
$extension = end($temp);
if (( ($_FILES[$id]["type"] == "image/jpeg")|| ($_FILES[$id]["type"] == "image/jpg")|| ($_FILES[$id]["type"] == "image/pjpeg")|| ($_FILES[$id]["type"] == "image/x-png")|| ($_FILES[$id]["type"] == "image/png"))&& ($_FILES[$id]["size"] <= 512000)&& in_array($extension, $allowedExts))
{
if ($_FILES[$id]["error"] > 0)
{
$fail=true;
}
else
{
$folder=printDetails($id);
return $folder;
}
}
else
{
echo ' <span style="font-size: 20px;color: black;">
Upload your profile picture
</span><br/>
<div class="dialog">
<form action="" method="post" enctype="multipart/form-data" class="dialogform">
<span style="color:red;">Invalid File Type</span><br/>
<label>Choose a profile pic:</label>
<input type="file" name="profilepic" id="profilepic" class="highlightfield"/><br/>
<span class="specialtext">Maximum size 500 Kb<br/>Allowed file type:JPEG,PNG,JPG </span>
<input type="text" value="'.$token.'" name="red" style="visibility:hidden;"/>
<input type="number" style="visibility: hidden;" name="pie" value="108" /><br/>
<input type="text" value="0x000000" style="visibility: hidden;" name="temp"/><br/>
<input type="submit" value="Upload" class="button"/>
</form>
</div>
<hr/>
<form action="setup.php" method="post" style="float: right;">
<input type="number" value="109" name="pie" style="visibility:hidden;"/>
<input type="submit" value="Skip" class="button"/>
</form>';
$fail=true;
}
return NULL;
}
/**
* deside the file name and copy the file to the specified folder
* @global boolean $fail
* @param string $id
* @param int $count
* @return string
*/
function printDetails($id,$count=0)
{
global $fail;
$temp = explode(".", $_FILES[$id]["name"]);
$extension = end($temp);
$fname= generateRandomString(15).'_' .rand(1000000000, 100000000000);
//echo "Upload: " . $_FILES[$id]["name"] . "<br>";
//echo "Type: " . $_FILES[$id]["type"] . "<br>";
//echo "Size: " . ($_FILES[$id]["size"] / 1024) . " kB<br>";
//echo "File Name: " . $fname.'.'.$extension. "<br>";
if (file_exists("usr/upload/" . $fname.'.'.$extension) )
{
if($count==0)
{
printDetails($id,++$count);
}
else
{
$fail=TRUE;
}
//echo $_FILES[$id]["name"] . " already exists. ";
}
else
{
if( move_uploaded_file($_FILES[$id]["tmp_name"],"usr/content/photo/" .$fname.'.'.$extension))
{
$folder= "usr/content/photo/" . $fname.'.'.$extension;
return $folder;
}
}
}
/**
* echo the main contents according to POST and GET requests
* @param db $sitedb
* @param db $userdb
*/
function printmaincontent($sitedb,$userdb){
$request= filter_input(INPUT_POST, "pie", FILTER_VALIDATE_INT);
if($request>10)
{
$token= filter_input(INPUT_POST,"red", FILTER_SANITIZE_STRING);
}
else
{
$token= filter_input(INPUT_GET,"red", FILTER_SANITIZE_STRING);
}
$treply= verifySource($sitedb,$token);
list($tstatus,$key,$uid,$mail)= explode(".", $treply,4); //Databse status,keyId,Email,UserId. The first parameter shows the request sucess and tthe rest are the database pipe.
$uname= filter_input(INPUT_POST, "uname",FILTER_SANITIZE_STRING);
if($request==102&&isset($uname)&&$uname&&$tstatus)
{
$reply=addusername($sitedb,$userdb,$token,$uname);
if($reply)
{
echo ' <span style="font-size: 20px;color: black;">
Upload your profile picture
</span><br/>
<div class="dialog">
<form action="" method="post" enctype="multipart/form-data" class="dialogform">
<label>Choose a profile pic:</label>
<input type="file" name="profilepic" id="profilepic" class="highlightfield"/><br/>
<span class="specialtext">Maximum size 500 Kb<br/>Allowed file type:JPEG,PNG,JPG </span>
<input type="text" value="'.$token.'" name="red" style="visibility:hidden;"/>
<input type="number" style="visibility: hidden;" name="pie" value="108" /><br/>
<input type="submit" value="Upload" class="button"/>
</form>
</div>
<hr/>
<form action="setup.php" method="post" style="float: right;">
<input type="text" value="'.$token.'" name="red" style="visibility:hidden;"/>
<input type="number" value="109" name="pie" style="visibility:hidden;"/>
<input type="submit" value="Skip" class="button"/>
</form>';
}
else
{
error("username adding failed".mysqli_errno($userdb->start()), 'setup.php', 202);
}
}
elseif ($request==108&&$tstatus)
{
initiateupload($key,$mail,$uid);
}
elseif($request==109&&$tstatus){
startSession($key,$mail);
echo '<script type="text/javascript">
window.location.replace("'.getRoot().'/home.php");
</script><noscript><a href="/home.php">Click here</a> to continue</noscript>';
}
elseif (!$uname&&$request==102&&$tstatus)
{
echo ' <span style="font-size: 20px;color: black;">
Set Your User Name
</span><br/>
<div class="dialog">
<form action="setup.php" method="post" class="dialogform">
<span style="color:red;">Please enter a valid Username</span><br/>
<label>Username:</label>
<input type="text" name="uname" class="text" placeholder="Username"/><br/>
<span class="specialtext" >Please note that you cannot change this in future</span><br/>
<input type="number" value="102" name="pie" style="visibility:hidden;"/>
<input type="submit" class="button" value="Save" style="float: right;"/>
</form>';
}
else if($tstatus)
{
echo ' <span style="font-size: 20px;color: black;">
Set Your User Name
</span><br/>
<div class="dialog">
<form action="setup.php" method="post" class="dialogform">
<label>Username:</label>
<input type="text" name="uname" class="text" placeholder="Username"/><br/>
<span class="specialtext" >Please note that you cannot change this in future</span><br/>
<input type="number" value="102" name="pie" style="visibility:hidden;"/>
<input type="text" value="'.$token.'" name="red" style="visibility:hidden;"/>
<input type="submit" class="button" value="Save" style="float: right;"/>
</form>';
}
else
{
echo 'Invalid Request!';
}
}
printFooter();//prints the footer of page func located@ universal.php
/**
* adds the user name to table user
* @param db $sitedb
* @param db $userdb
* @param string $token
* @param string $name
* @return boolean true on sucess
*/
function addusername($sitedb,$userdb,$token,$name){
$con=$sitedb->start();
$conusr=$userdb->start();
$status=FALSE;
$ip=$_SERVER['REMOTE_ADDR'];
if($sitedb->status){
$query="SELECT * FROM tracking WHERE (token='$token' AND ip='$ip')";
if($result=mysqli_query($con, $query)){
while ($row = mysqli_fetch_array($result)){
$keyid=$row['error'];
list( $keyid,$temp)= explode(".", $keyid,2);
$status=TRUE;
}
if($status){
$status=FALSE;
$query="UPDATE user SET username='$name' WHERE username='$keyid'";
if(mysqli_query($conusr, $query)){
$status=TRUE;
}
elseif(mysqli_errno($conusr)==1062){
echo ' <span style="font-size: 20px;color: black;">
Set Your User Name
</span><br/>
<div class="dialog">
<form action="setup.php" method="post" class="dialogform">
<span style="color:red;">Sorry! The username you entered already exist</span><br/>
<label>Username:</label>
<input type="text" name="uname" class="text" placeholder="Username" value="'.getvalidusername($conusr,$name).'"/><br/>
<span class="specialtext" >Please note that you cannot change this in future</span><br/>
<input type="number" value="102" name="pie" style="visibility:hidden;"/>
<input type="text" value="'.$token.'" name="red" style="visibility:hidden;"/>
<input type="submit" class="button" value="Save" style="float: right;"/>
</form>';
}
}
}
}
return $status;
}
/**
*
* @param dbconnection $con
* @param string $name
* @param int $count
* @return string
*/
function getvalidusername($con,$name,$count=0)
{
$number=rand(0,999);
$query="SELECT username from user where username='$name$number'";
if($result=mysqli_query($con, $query))
{
if($row= mysqli_fetch_array($result))
{
if($count<3)
{
getvalidusername($con, $name, ++$count);
}
else
{
return '';
}
}
else
{
return $name.$number;
}
}
}
/**
* generate a valid content id as per the previous requests
* @global db $userdb
* @param string $type
* @return int
*/
function getcontentid($type){
global $userdb;
$con=$userdb->start();
$query="SELECT * FROM content_id WHERE (`group_id`='lkjfndjrcs' AND `type`='$type')";
if($result= mysqli_query($con, $query))
{
$cno;
while ($row = mysqli_fetch_array($result)) {
$cno= $row['id'];
}
if($cno>0)
{
$cid= rand(1000, 10000);
$cno=$cno+$cid;
$query="UPDATE content_id SET id=$cno WHERE (`group_id`='lkjfndjrcs' AND `type`='$type')";
if(mysqli_query($con, $query))
{
return $cno;
}
}
}
return 0;
}
//terminates initialisations
$userdb->close();
$sitedb->close();
?>
</body>
</html>