Created
July 8, 2012 11:10
-
-
Save masihyeganeh/3070562 to your computer and use it in GitHub Desktop.
Functions to login to University website and select cources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function LoginAndGetCookie($server='82.99.229.141', $user, $pass) | |
{ | |
$status = 'Error'; | |
$data = 'مشکلی در انجام عملیات بوجود آمد. شاید به دلیل شلوغی سایت، این مشکل بوجود آمده. لطفاً بعداً دوباره امتحان کنید'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://' . $server . '/'); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0'); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_NOBODY, 0); | |
$page = curl_exec($ch); | |
curl_close($ch); | |
preg_match('/ASP.NET_SessionId=[^;]*;/i', $page, $match); | |
$aspxcookie = str_replace(array('ASP.NET_SessionId=', ';'), array('', ''), $match[0]); | |
preg_match('/__VIEWSTATE" value="[^"]*"/i', $page, $match); | |
$viewstate = str_replace(array('__VIEWSTATE" value="', '"'), array('', ''), $match[0]); | |
preg_match('/__EVENTVALIDATION" value="[^"]*"/i', $page, $match); | |
$eventvalidation = str_replace(array('__EVENTVALIDATION" value="', '"'), array('', ''), $match[0]); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://' . $server . '/login.aspx'); | |
curl_setopt($ch, CURLOPT_REFERER, 'http://' . $server . '/'); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0'); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_NOBODY, 0); | |
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Cookie: ASP.NET_SessionId='.$aspxcookie)); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, '__EVENTTARGET=&__EVENTARGUMENT=&__EVENTVALIDATION=' . urlencode($eventvalidation) . '&__VIEWSTATE=' . urlencode($viewstate) . '&txtUserName=' . urlencode($user) . '&txtPassword=' . urlencode($pass) . '&grop1=RadioButton1&LoginButton=%D9%88%D8%B1%D9%88%D8%AF+%D8%A8%D9%87+%D8%B3%DB%8C%D8%B3%D8%AA%D9%85'); | |
$page = curl_exec($ch); | |
curl_close($ch); | |
$page = str_replace("HTTP/1.1 100 Continue\r\n\r\n", '', $page); | |
$page = ltrim(substr($page, strpos($page, "\r\n\r\n")), "\r\n"); | |
if (strpos($page, '<script> alert ("') === 0) | |
{ | |
$data = substr($page, 18, strpos($page, '"', 18) - 18); | |
} | |
else | |
{ | |
$status = 'OK'; | |
$data = $aspxcookie; | |
} | |
return array("status" => $status, "data" => $data); | |
} | |
function SelectCourse($server='82.99.229.141', $cookie, $code) | |
{ | |
$status = 'Error'; | |
$data = 'مشکلی در انجام عملیات بوجود آمد. لطفاً دوباره لاگین کنید'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://' . $server . '/SelectCourse.aspx'); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0'); | |
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Cookie: ASP.NET_SessionId='.$cookie)); | |
curl_setopt($ch, CURLOPT_REFERER, 'http://' . $server . '/SelectCourse.aspx'); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_NOBODY, 0); | |
$page = curl_exec($ch); | |
curl_close($ch); | |
preg_match('/__VIEWSTATE" value="[^"]*"/i', $page, $match); | |
$viewstate = str_replace(array('__VIEWSTATE" value="', '"'), array('', ''), $match[0]); | |
preg_match('/__EVENTVALIDATION" value="[^"]*"/i', $page, $match); | |
$eventvalidation = str_replace(array('__EVENTVALIDATION" value="', '"'), array('', ''), $match[0]); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://' . $server . '/SelectCourse.aspx'); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0'); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_REFERER, 'http://' . $server . '/SelectCourse.aspx'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_NOBODY, 0); | |
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Cookie: ASP.NET_SessionId='.$cookie)); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, '__EVENTTARGET=ctl00%24ContentPlaceHolder1%24lnkBtnSelectCourse&__EVENTARGUMENT=&__VIEWSTATE=' . urlencode($viewstate) . '&__VIEWSTATEENCRYPTED=&__PREVIOUSPAGE=tCJC0IzlpZ5yX2jv_ZL8qTztaKbNGgHV8SwF7FEm5b01&__EVENTVALIDATION=' . urlencode($eventvalidation) . '&ctl00%24ContentPlaceHolder1%24txtCourseId='.$code); | |
$page = curl_exec($ch); | |
curl_close($ch); | |
$pos = 0; | |
if (($pos = strpos($page, 'alert(')) !== false) | |
{ | |
$pos += 7; | |
$data = substr($page, $pos, strpos($page, "'", $pos) - $pos); | |
} | |
elseif (strpos($page, '>'.$code.'<') !== false) | |
{ | |
$status = 'OK'; | |
$data = $code; | |
} | |
else | |
{ | |
$status = 'Error'; | |
$data = 'مشخصه ' . $code . ' ثبت نشد. لطفاً با سرور دیگری امتحان کنید'; | |
} | |
return array("status" => $status, "data" => $data); | |
} | |
function multiSelectCourse($server='82.99.229.141', $cookie, $codes) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://' . $server . '/SelectCourse.aspx'); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cookie: ASP.NET_SessionId='.$cookie)); | |
curl_setopt($ch, CURLOPT_REFERER, 'http://' . $server . '/SelectCourse.aspx'); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_NOBODY, 0); | |
$page = curl_exec($ch); | |
curl_close($ch); | |
preg_match('/__VIEWSTATE" value="[^"]*"/i', $page, $match); | |
$viewstate = str_replace(array('__VIEWSTATE" value="', '"'), array('', ''), $match[0]); | |
preg_match('/__EVENTVALIDATION" value="[^"]*"/i', $page, $match); | |
$eventvalidation = str_replace(array('__EVENTVALIDATION" value="', '"'), array('', ''), $match[0]); | |
$curly = array(); | |
$result = array(); | |
$mh = curl_multi_init(); | |
$codes = array_values(array_diff($codes, array(''))); | |
foreach ($codes as $code) { | |
$id = $code; | |
$curly[$id] = curl_init(); | |
curl_setopt($curly[$id], CURLOPT_URL, 'http://' . $server . '/SelectCourse.aspx'); | |
curl_setopt($curly[$id], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0'); | |
curl_setopt($curly[$id], CURLOPT_HEADER, 1); | |
curl_setopt($curly[$id], CURLOPT_REFERER, 'http://' . $server . '/SelectCourse.aspx'); | |
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curly[$id], CURLOPT_NOBODY, 0); | |
curl_setopt($curly[$id], CURLOPT_HTTPHEADER, array('Cookie: ASP.NET_SessionId='.$cookie)); | |
curl_setopt($curly[$id], CURLOPT_POST, 1); | |
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, '__EVENTTARGET=ctl00%24ContentPlaceHolder1%24lnkBtnSelectCourse&__EVENTARGUMENT=&__VIEWSTATE=' . urlencode($viewstate) . '&__VIEWSTATEENCRYPTED=&__PREVIOUSPAGE=tCJC0IzlpZ5yX2jv_ZL8qTztaKbNGgHV8SwF7FEm5b01&__EVENTVALIDATION=' . urlencode($eventvalidation) . '&ctl00%24ContentPlaceHolder1%24txtCourseId='.$code); | |
curl_multi_add_handle($mh, $curly[$id]); | |
} | |
// execute the handles | |
$running = null; | |
do { | |
curl_multi_exec($mh, $running); | |
} while($running > 0); | |
// get content and remove handles | |
foreach($curly as $code => $c) { | |
$status = 'Error'; | |
$data = 'مشکلی در انجام عملیات بوجود آمد. لطفاً دوباره لاگین کنید'; | |
$page = curl_multi_getcontent($c); | |
$pos = 0; | |
if (($pos = strpos($page, 'alert(')) !== false) | |
{ | |
$pos += 7; | |
$data = substr($page, $pos, strpos($page, "'", $pos) - $pos); | |
} | |
elseif (strpos($page, '>'.$code.'<') !== false) | |
{ | |
$status = 'OK'; | |
$data = $code; | |
} | |
else | |
{ | |
$status = 'Error'; | |
$data = 'مشخصه ' . $code . ' ثبت نشد. لطفاً با سرور دیگری امتحان کنید'; | |
} | |
if (@$code) | |
$data = (strpos($data, " $code " /* this is weird! really weird! */) === false ? ($code . ': ') : '') . $data; | |
array_push($result, array("status" => $status, "data" => $data)); | |
curl_multi_remove_handle($mh, $c); | |
} | |
// all done | |
curl_multi_close($mh); | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment