-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrendingCourses.php
192 lines (160 loc) · 6.81 KB
/
TrendingCourses.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
<<<<<<< HEAD
<?php
/***********************************************************************************
*
* TrendingCourses.php increments count for a clicked link in trendingcourses table
* This script is used to count hits for each course
*
* @Author Manzoor Ahmed
************************************************************************************/
require_once 'connection.php';
require_once 'ParserFactory.php';
#race condition
ignore_user_abort(true);
#get url
$url = $_GET['url'];
$hostInfo = parse_url($url);
#make sure the url to be parsed, which validates that its probably not some junk value
if($hostInfo !== false){
#make sure host name is known
if(
((strcasecmp($hostInfo["host"],"www.coursera.org") == 0))
|| ((strcasecmp($hostInfo["host"],"www.edx.org") ==0))
|| ((strcasecmp($hostInfo["host"],"www.canvas.net") == 0))
|| ((strcasecmp($hostInfo["host"],"www.udacity.com") ==0))
|| ((strcasecmp($hostInfo["host"],"ocw.mit.edu") == 0))
|| ((strcasecmp($hostInfo["host"],"see.stanford.edu") == 0))
){
#check for '/course/'
$hostpath = $hostInfo['path'];
$coursepath = explode('/',$hostpath);
#this is one of the known url, ie www.coursera.org/course/.. or www.edx.org/courses/...
if(
((strcasecmp($hostInfo["host"],"www.coursera.org")==0) && (strcasecmp($coursepath[1],"course" )==0))
|| ((strcasecmp($hostInfo["host"],"www.edx.org" )==0) && (strcasecmp($coursepath[1],"courses")==0))
|| ((strcasecmp($hostInfo["host"],"www.canvas.net" )==0) && (strcasecmp($coursepath[1],"courses")==0))
|| ((strcasecmp($hostInfo["host"],"www.udacity.com" )==0) && (strcasecmp($coursepath[1],"course" )==0))
|| ((strcasecmp($hostInfo["host"],"ocw.mit.edu" )==0) && (strcasecmp($coursepath[1],"courses")==0))
|| ((strcasecmp($hostInfo["host"],"see.stanford.edu")==0) && (strcasecmp($coursepath[1],"see")==0))
){
/************
* redirect to the url while incrementing hits for this url
* but only after verifying it's an edx or coursera url
* we reject other stuff, espescially if parsing failed
*/
#redirect user to $url
header("Location: $url",true) ;
#find id of given url
$idQuery ="SELECT id FROM `course_data` WHERE `course_link` LIKE '%{$url}%';";
$row = $dbc->query($idQuery) or die($dbc->error);
#returned column
$singleColumn = $row->fetch_row();
#get id number from column
$id = $singleColumn[0];
#find hits in course_hits table with given id
$getHits = "SELECT hits FROM `trendingcourses` WHERE `id` = '$id';";
#run query, or report error
$result = $dbc->query($getHits) or die($dbc->error);
if($result){
#get returned column
$oldRow = $result->fetch_row();
#get id
$oldNum = $oldRow[0];
#update hits
$newNum = $oldNum+ 1;
#increment count in course_hits table with given id
$increment ="UPDATE `trendingcourses` SET `hits` = $newNum WHERE `trendingcourses`.`id` ='$id';";
$dbc->query($increment) or die($dbc->error);
}
}
}
else{
//don't need to update database, wrong url was passed, or someone changed the url
header("Location: index.php",true) ;
}
}
else{
#echo "Unknown URL ";
}
?>
=======
<?php
/**
* TrendingCourses.php increments count for a clicked link in trendingcourses table
* This script is used to count hits for each course
* @Author Manzoor Ahmed
**/
require_once 'connection.php';
require_once 'ParserFactory.php';
#race condition
ignore_user_abort(true);
#get url
$url = $_GET['url'];
$hostInfo = parse_url($url);
#make sure the url to be parsed, which validates that its probably not some junk value
if($hostInfo !== false){
//var_dump($hostInfo);
#make sure host name is known
if(
((strcasecmp($hostInfo["host"],"www.coursera.org") == 0))
|| ((strcasecmp($hostInfo["host"],"www.edx.org") ==0))
|| ((strcasecmp($hostInfo["host"],"www.canvas.net") == 0))
|| ((strcasecmp($hostInfo["host"],"www.udacity.com") ==0))
|| ((strcasecmp($hostInfo["host"],"ocw.mit.edu") == 0))
|| ((strcasecmp($hostInfo["host"],"see.stanford.edu") == 0))
){
#check for '/course/'
$hostpath = $hostInfo['path'];
#'course' is subpath for coursera , and 'courses' is subpath for edx
$coursepath = explode('/',$hostpath);
#this is one of the known url, ie www.coursera.org/course/.. or www.edx.org/courses/...
if(
((strcasecmp($hostInfo["host"],"www.coursera.org")==0) && (strcasecmp($coursepath[1],"course" )==0))
|| ((strcasecmp($hostInfo["host"],"www.edx.org" )==0) && (strcasecmp($coursepath[1],"courses")==0))
|| ((strcasecmp($hostInfo["host"],"www.canvas.net" )==0) && (strcasecmp($coursepath[1],"courses")==0))
|| ((strcasecmp($hostInfo["host"],"www.udacity.com" )==0) && (strcasecmp($coursepath[1],"course" )==0))
|| ((strcasecmp($hostInfo["host"],"ocw.mit.edu" )==0) && (strcasecmp($coursepath[1],"courses")==0))
|| ((strcasecmp($hostInfo["host"],"see.stanford.edu")==0) && (strcasecmp($coursepath[1],"see")==0))
){
/**
* redirect to the url while incrementing hits for this url
* but only after verifying it's an edx or coursera url
* we reject other stuff, espescially if parsing failed
*/
#redirect user to $url
header("Location: $url",true) ;
#find id of given url
$idQuery ="SELECT id FROM `course_data` WHERE `course_link` LIKE '%{$url}%';";
//$idQuery = $dbc->real_escape_string(...) gives an erro
$row = $dbc->query($idQuery) or die($dbc->error);
#returned column
$singleColumn = $row->fetch_row();
#get id number from column
$id = $singleColumn[0];
#find hits in course_hits table with given id
$getHits = "SELECT hits FROM `trendingcourses` WHERE `id` = '$id';";
#run query, or report error
$result = $dbc->query($getHits) or die($dbc->error);
if($result){
#get returned column
$oldRow = $result->fetch_row();
#get id
$oldNum = $oldRow[0];
#update hits
$newNum = $oldNum+ 1;
#increment count in course_hits table with given id
$increment ="UPDATE `trendingcourses` SET `hits` = $newNum WHERE `trendingcourses`.`id` ='$id';";
$dbc->query($increment) or die($dbc->error);
}//if
//}//else
}//if course
}//if hostname
else{
//don't need to update database, wrong url was passed, or someone changed the url
}
}//hostinfo
else{
#echo "Unknown URL ";
}
?>
>>>>>>> c90a6c3feb477ad2334df22e1462ab875a4ce2ac