function verifyUser($authCookieValue) { global $authCheckURL; global $authSuccessHTML; global $authCookieName; $result = false; $error = ''; // create a new cURL resource $ch = curl_init(); if ($ch !== false) { // set URL and other appropriate options $header = array(); $header[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; $header[] = 'Accept-Language: en-US,en;q=0.5'; $header[] = 'Connection: close'; $header[] = 'DNT: 1'; curl_setopt($ch, CURLOPT_URL, $authCheckURL); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0'); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIE, $authCookieName.'='.$authCookieValue); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // grab URL and pass it to the browser $response = curl_exec($ch); if($response === false) { $error = curl_error($ch); } else { $result = strpos($response, $authSuccessHTML) !== false; } curl_close($ch); } else { $error = 'cannot initialize cURL'; } return array( 'succeeded' => $result, 'error' => $error ); }
// proxy config var PROXY_URL = 'http://university.org/~user/proxy.php?'; var PROXY_URL_QUERY = 'urlForProxy='; var PROXY_ID_QUERY = 'idForProxy='; // page search and modification const var ACM_PDF_LINK_NAME = 'FullTextPDF'; var ACM_ARTICLE_ID_NAME = 'id'; var ACM_PURCHASE_LINK = 'https://dl.acm.org/purchase.cfm'; var ACM_QUERY_URL = 'http://dl.acm.org/ft_gateway.cfm'; var ACM_QUERY = 'id={0}'; var ACM_LINK = '<a name="' + ACM_PDF_LINK_NAME + '" title="FullText PDF" href="{0}" target="_blank"><img src="imagetypes/pdf_logo.gif" alt="PDF" class="fulltext_lnk" border="0">PDF</a> [proxy]'; // requests to the background page var REQUEST_AUTH = 'auth'; function setACMLink() { var pdfLink = document.getElementsByName(ACM_PDF_LINK_NAME)[0]; if (!pdfLink) { var i, id, param; var params = window.location.search.substr(1).split('&'); for (i = 0; i < params.length; i++) { param = params[i].split('='); if (param[0] === ACM_ARTICLE_ID_NAME) { id = param[1].indexOf('.') > 0 ? param[1].split('.')[1] : param[1]; break; } } if (id) { var link = PROXY_URL + ACM_QUERY.format(id) + '&' + PROXY_URL_QUERY + encodeURIComponent(ACM_QUERY_URL); // purchase link is a placeholder for a link to PDF var a, container; var links = document.getElementsByTagName('a'); for (i = 0; i < links.length; i++) { a = links[i]; if (a.href.indexOf(ACM_PURCHASE_LINK) === 0) { container = a.parentNode; container.innerHTML = ACM_LINK.format('#'); setClick(container.childNodes[0], link); break; } } } } } function setClick(elem, link) { elem.addEventListener('click', function (e) { commPort.postMessage({name: REQUEST_AUTH, href: link}); e.preventDefault(); return false; }); }
// config var AUTH_URL = 'https://university.org/intranet'; var AUTH_COOKIE = 'JSESSIONID'; // const var REQUEST_AUTH = 'auth'; chrome.runtime.onConnect.addListener(function (port) { port.onMessage.addListener(function (request) { var answer = {toRequest: request.name}; if (request.name === REQUEST_AUTH) { // check the authorization on the select web-site answer.href = request.href; answer.result = false; answer.id = ''; chrome.cookies.get({url: AUTH_URL, name: AUTH_COOKIE}, function (cookie) { if (cookie) { answer.result = true; answer.id = cookie.value; } port.postMessage(answer); }); } }); });
var commPort = chrome.runtime.connect(); commPort.onMessage.addListener(function (answer) { if (answer.toRequest === REQUEST_AUTH) { // add an authorization id, and send the request to to the proxy window.location = answer.href + '&' + PROXY_ID_QUERY + answer.id; } });
// config $cookieDomain = '.university.org'; $cookiePath = '/~user'; $headerArray = explode("\r\n", $response['header']); $js = ''; foreach ($headerArray as $headerLine) { if (strpos($destinationURL, 'ieee.org') !== false) { if (strpos($headerLine, 'Set-Cookie: ') !== false) { $cookieArray = explode(': ', $headerLine, 2); $headerLine = $cookieArray[0].': '; $cookieDataArray = explode('; ', $cookieArray[1]); $isFirstKey = true; $js .= ' document.cookie = "'; foreach ($cookieDataArray as $cdKey => $cookieData) { list($cname, $cvalue) = array_merge(explode('=', $cookieData), array('')); if ($cname === 'domain') { $cvalue = $cookieDomain; $cookieDataArray[$cdKey] = $cname.'='.$cvalue; } if ($cname === 'path') { $cvalue = $cookiePath; $cookieDataArray[$cdKey] = $cname.'='.$cvalue; } $headerLine .= ($isFirstKey ? '' : '; ').$cookieDataArray[$cdKey]; $js .= ($isFirstKey ? '' : '; ').$cookieDataArray[$cdKey]; $isFirstKey = false; } $js .= "\";\r\n"; } header($headerLine); if (strlen($js) > 0) { echo "\r\n<script>\r\n".$js.'</script>'; // insert JS code into the page to set IEEE session and identification cookies } } else { foreach ($headerArray as $headerLine) { header($headerLine); } } }
Source: https://habr.com/ru/post/192276/
All Articles