This repository has been archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
addr.php
275 lines (249 loc) · 7.61 KB
/
addr.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
<?php
/* One of most important file...
Comments have been added to make it more readable...
Hope you enjoy reading it, if you found any bug please inform me : skbly7@gmail.com
Finally opening it.. :P
*/
//DB Connection
include "connect.php";
//Not useful now
include "files/month.php";
//Defined a function for finding all elements within two text. Returns a array.
//Sorry wasn't knowing about advance crawling at the time of making it.. :)
function g($string,$start,$end){
preg_match_all('/' . preg_quote($start, '/') . '(.*?)'. preg_quote($end, '/').'/i', $string, $m);
$out = array();
foreach($m[1] as $key => $value){
$type = explode('::',$value);
if(sizeof($type)>1){
if(!is_array($out[$type[0]]))
$out[$type[0]] = array();
$out[$type[0]][] = $type[1];
} else {
if($value=='NA')
{
$value=0;
$out[] = $value;
$out[] = $value;
}
else
{
$out[] = $value;
}
}
}
return $out;
}
/* Recalculate function start
Please dont waste time thinking why it is named recalculate, i too dont remember what i was thinking that tym.. :/
Funtion to calculate a Codechef User Submissions is below
It was a early prototype. And now it is not in use anymore.. :)
*/
function recalculate($user,$month,$lmonth,$short,$lshort,$html)
{
// Finding 1 is finding rank for latest Long Solutions
$finding1=g($html,"".$month."</b>: <span>","</span>");
if(array_key_exists(0,$finding1))
{
$names_of_solved=g($finding1[0],'">','</a>');
$i1=1;
$problems1=$names_of_solved[0];
while(array_key_exists($i1,$names_of_solved))
{
$problems1=$problems1.", ".$names_of_solved[$i1];
$i1++;
}
}
else
{
$problems1="Not solved any";
$i1=0;
}
// echo $problems1." Total : ".$i1;
$finding2=g($html,"".$lmonth."</b>: <span>","</span>");
if(array_key_exists(0,$finding2))
{
$names_of_solved=g($finding2[0],'">','</a>');
$i2=1;
$problems2=$names_of_solved[0];
while(array_key_exists($i2,$names_of_solved))
{
$problems2=$problems2.", ".$names_of_solved[$i2];
$i2++;
}
}
else
{
$problems2="Not solved any";
$i2=0;
}
// echo $problems2." Total : ".$i2;
$finding3=g($html,"".$short."</b>: <span>","</span>");
if(array_key_exists(0,$finding3))
{
$names_of_solved=g($finding3[0],'">','</a>');
$i3=1;
$problems3=$names_of_solved[0];
while(array_key_exists($i3,$names_of_solved))
{
$problems3=$problems3.", ".$names_of_solved[$i3];
$i3++;
}
}
else
{
$problems3="Not solved any";
$i3=0;
}
// echo $problems3." Total : ".$i3;
$finding4=g($html,"".$lshort."</b>: <span>","</span>");
if(array_key_exists(0,$finding4))
{
$names_of_solved=g($finding4[0],'">','</a>');
$i4=1;
$problems4=$names_of_solved[0];
while(array_key_exists($i4,$names_of_solved))
{
$problems4=$problems4.", ".$names_of_solved[$i4];
$i4++;
}
}
else
{
$problems4="Not solved any";
$i4=0;
}
// echo $problems4." Total : ".$i4;
$sql="SELECT COUNT(*) as total FROM TABLE_NAME WHERE ************='".$user."'";
$retval=mysql_query($sql);
$data=mysql_fetch_assoc($retval);
$sql = "INSERT INTO dynamic(handle, short, lshort, longg, llong, p1, p2, p3, p4)
VALUES('".$user."',".$i1.", ".$i2.", ".$i3.", ".$i4.",'".$problems1."', '".$problems2."','".$problems3."','".$problems4."')";
echo $sql;
$retval = mysql_query($sql);
return true;
}
/*Recalculate function end */
// You wonder what it is ??
// I just befooled some, by displaying a text field like captcha code in add page, there was never a captcha.. :P
// You can select it and drag into the text field.. :D
if(($_GET['res']!=$_GET['show'])&&0)
{
header("Location: add.php?error=3");
}
else
{
$value=0;
//Check for college selection. Each college has a unique ID
if (isset($_COOKIE["College"]))
$value=mysql_real_escape_string($_COOKIE["College"]);
$user=mysql_real_escape_string($_GET['user']);
$sql='SELECT COUNT(*) as total FROM detail WHERE handle="'.$user.'"';
$retval=mysql_query($sql)or die("cannot select db");
$data=mysql_fetch_assoc($retval)or die("cannot select db");
mysql_error();
// echo $data['total'];
// If user was already added a error message would be shown. For this the above work has been done.
if($data['total']==0)
{
// HTML DOM PARSER LIBRARY
include_once "files/crawl2.php";
// ALL COLLEGE DETAILS (as a array)
include "files/college.php";
$url="http://www.codechef.com/users/".$user;
$html = file_get_html($url);
$title="";
$res = preg_match("/<title>(.*)<\/title>/siU", $html, $title_matches);
$title = preg_replace('/\s+/', ' ', $title_matches[1]);
$title = trim($title);
$pos = strpos($title, 'User');
// Check if a user exist on Codechef or not..
// Codechef displays "user" in title if url is right else not. Used this check...
if($pos==9)
{
$rank="";
$reallll=0;
// Well a lot of work to get correct ranking in all the cases of Codechef... Actually Codechef shows NA, N/A, 0, - , many things for no rank.
// Removed each bug time to time in this script
foreach($html->find('hx') as $element)
{
$reallll++;
$rank=$rank."".$element.'<br>';
}
// echo $reallll;
$rank=g($rank,'<hx>','</hx>');
// print_r($rank)."<br>";
if($rank[2]=="")
{
$rank[2]=$rank[3];
$rank[3]=$rank[4];
}
// print_r($rank);
if($reallll==2)
{
$new_rank=array($rank[0],0,$rank[2],0);
$rank=$new_rank;
}
if($reallll==4&&$rank[3]=="")
{
$rank[3]=0;
}
// print_r($rank);
// echo "Ranks are : ".$rank[0]." , ".$rank[1]." , ".$rank[2]." , ".$rank[3].$rank[4].'<br>';
// FINALLY WE ARE NOW HAVING ALL THE RANKING IN A ARRAY $rank....!!
// HUNT FOR CODECHEF USER's NAME
$name="";
foreach($html->find('h1') as $element)
$name=$name."".$element.'<br>';
$naam=g($name,'<h1>',"'s User");
if($naam[0]==NULL)
$naam[0]='Undefined';
//echo $naam[0].'<br>';
// HUNT FOR COUNTRY OF USER
$country=g($name,'/64/','.png');
if($country[0]==NULL)
$country[0]='NA';
$country[0]=strtoupper($country[0]);
foreach($html->find('body') as $element)
$test_ini=$test_ini."".$element;
// CHECK FOR CODECHEF USER's INSITUTE NAME IN PROFILE in place About Me and Insitution
$about=g($test_ini,'About Me:','</tr>');
$institute=g($test_ini,'Institution:','</tr>');
$to_check="xyz".$institute[0];
$to_check2="xyz".$about[0];
$add=0;
foreach($final[$value] as $fun)
if(stripos($to_check, $fun)>0)
$add=1;
foreach($final[$value] as $fun)
if(stripos($to_check2, $fun)>0)
$add=1;
echo $add;
if($add==0)
{
// INSIDE IT IF THE USER IS NOT OF THE INSITUTION NAME SUPPLIED IN COOKIE
header("Location: add.php?error=4");
}
else
{
// FINALLY ADDING THE USER INTO THE DATABASE
recalculate($user,$month,$lmonth,$short,$lshort,$html);
$sql = 'INSERT INTO detail (handle, name, country, rank1, rank2, rank3, rank4, college) VALUES("'.$user.'", "'.$naam[0].'", "'.$country[0].'", '.$rank[0].', '.$rank[1].', '.$rank[2].', '.$rank[3].', '.$value.')';
$retval = mysql_query($sql)or die("cannot select db");
mysql_error();
header("Location: add.php?error=9&user=".$user."");
//echo 'Thank you ';
}
}
else
{
header("Location: add.php?error=1");
}
}
else
{
header("Location: add.php?error=2");
}
mysql_close($conn);
}
?>