Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $username = 'user';
- $password = 'pass';
- $file_name = 'test_image.png';
- list($page, $cookies) = login($username, $password);
- $token = imageuploadpage($cookies);
- $result = upload($token, $cookies, $file_name);
- echo $result;
- function login($username, $password)
- {
- $url = 'http://codequiz.saudqq.com/quiz1/login.php';
- $post_data = [
- 'user' => $username,
- 'pass' => $password
- ];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8']);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
- curl_setopt($ch, CURLOPT_TIMEOUT, 15);
- $data = curl_exec($ch);
- preg_match('/Set-Cookie: (.*)\b/', $data, $cookie_match);
- $cookies = (array)http_parse_cookie($cookie_match[1]); // would be a problem if there is 2 headers set-cookie
- return [$data, $cookies];
- }
- function imageuploadpage($cookies)
- {
- $url = 'http://codequiz.saudqq.com/quiz1/';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8']);
- curl_setopt($ch, CURLOPT_COOKIE, http_build_cookie($cookies));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
- curl_setopt($ch, CURLOPT_TIMEOUT, 15);
- $page = curl_exec($ch);
- $doc = new DOMDocument();
- $doc->loadHTML($page);
- $xpath = new DOMXPath($doc);
- $nodes = $xpath->query('//head/meta');
- foreach($nodes as $node) {
- if($node->getAttribute('name') == 'token') {
- return $node->getAttribute('content');
- }
- }
- }
- function upload($token, $cookies, $file_name)
- {
- $url = 'http://codequiz.saudqq.com/quiz1/upload.php';
- $curl_image_file = new CURLFile($file_name, image_type_to_mime_type(exif_imagetype($file_name)), $file_name);
- $post_data = [
- 'upl' => $curl_image_file,
- 't' => $token
- ];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Referer: http://codequiz.saudqq.com/quiz1/', 'Content-Type: multipart/form-data']);
- curl_setopt($ch, CURLOPT_COOKIE, http_build_cookie($cookies));
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
- curl_setopt($ch, CURLOPT_TIMEOUT, 15);
- $data = curl_exec($ch);
- return $data;
- }
- // I got trouble with pecl_http module for centos7/php7 so it is my workaround
- function http_parse_cookie($szHeader, $object = true)
- {
- $obj = new stdClass;
- $arrCookie = array();
- $arrObj = array();
- $arrCookie = explode("\n", $szHeader);
- for($i = 0; $i<count($arrCookie); $i++){
- $cookie = $arrCookie[$i];
- $attributes = explode(';', $cookie);
- $arrCookie[$i] = array();
- foreach($attributes as $attrEl){
- $tmp = explode('=', $attrEl, 2);
- if(count($tmp)<2){
- continue;
- }
- $key = trim($tmp[0]);
- $value = trim($tmp[1]);
- if($key=='version'||$key=='path'||$key=='expires'||$key=='domain'||$key=='comment'){
- if(!isset($arrObj[$key])){
- $arrObj[$key] = $value;
- }
- }else{
- $arrObj['cookies'][$key] = $value;
- }
- }
- }
- if($object===true){
- $obj = (object)$arrObj;
- $return = $obj;
- }else{
- $return = $arrObj;
- }
- return $return;
- }
- function http_build_cookie($array)
- {
- $result = [];
- foreach($array['cookies'] as $key => $value) {
- $result[] = "$key=$value";
- }
- return implode(";", $result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement