Advertisement
Guest User

Untitled

a guest
Aug 6th, 2016
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. <?php
  2. $username = 'user';
  3. $password = 'pass';
  4. $file_name = 'test_image.png';
  5.  
  6. list($page, $cookies) = login($username, $password);
  7. $token = imageuploadpage($cookies);
  8. $result = upload($token, $cookies, $file_name);
  9.  
  10. echo $result;
  11.  
  12. function login($username, $password)
  13. {
  14. $url = 'http://codequiz.saudqq.com/quiz1/login.php';
  15. $post_data = [
  16. 'user' => $username,
  17. 'pass' => $password
  18. ];
  19. $ch = curl_init();
  20. curl_setopt($ch, CURLOPT_URL, $url);
  21. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
  22. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8']);
  23. curl_setopt($ch, CURLOPT_POST, true);
  24. curl_setopt($ch, CURLOPT_HEADER, 1);
  25. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
  26. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  27. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
  28. curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  29. $data = curl_exec($ch);
  30. preg_match('/Set-Cookie: (.*)\b/', $data, $cookie_match);
  31. $cookies = (array)http_parse_cookie($cookie_match[1]); // would be a problem if there is 2 headers set-cookie
  32. return [$data, $cookies];
  33. }
  34.  
  35. function imageuploadpage($cookies)
  36. {
  37. $url = 'http://codequiz.saudqq.com/quiz1/';
  38. $ch = curl_init();
  39. curl_setopt($ch, CURLOPT_URL, $url);
  40. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
  41. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8']);
  42. curl_setopt($ch, CURLOPT_COOKIE, http_build_cookie($cookies));
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  44. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
  45. curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  46. $page = curl_exec($ch);
  47. $doc = new DOMDocument();
  48. $doc->loadHTML($page);
  49. $xpath = new DOMXPath($doc);
  50. $nodes = $xpath->query('//head/meta');
  51. foreach($nodes as $node) {
  52. if($node->getAttribute('name') == 'token') {
  53. return $node->getAttribute('content');
  54. }
  55. }
  56. }
  57.  
  58. function upload($token, $cookies, $file_name)
  59. {
  60. $url = 'http://codequiz.saudqq.com/quiz1/upload.php';
  61. $curl_image_file = new CURLFile($file_name, image_type_to_mime_type(exif_imagetype($file_name)), $file_name);
  62. $post_data = [
  63. 'upl' => $curl_image_file,
  64. 't' => $token
  65. ];
  66. $ch = curl_init();
  67. curl_setopt($ch, CURLOPT_URL, $url);
  68. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
  69. 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']);
  70. curl_setopt($ch, CURLOPT_COOKIE, http_build_cookie($cookies));
  71. curl_setopt($ch, CURLOPT_POST, true);
  72. curl_setopt($ch, CURLOPT_HEADER, 1);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  74. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  75. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
  76. curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  77. $data = curl_exec($ch);
  78. return $data;
  79. }
  80.  
  81. // I got trouble with pecl_http module for centos7/php7 so it is my workaround
  82. function http_parse_cookie($szHeader, $object = true)
  83. {
  84. $obj = new stdClass;
  85. $arrCookie = array();
  86. $arrObj = array();
  87. $arrCookie = explode("\n", $szHeader);
  88. for($i = 0; $i<count($arrCookie); $i++){
  89. $cookie = $arrCookie[$i];
  90. $attributes = explode(';', $cookie);
  91. $arrCookie[$i] = array();
  92. foreach($attributes as $attrEl){
  93. $tmp = explode('=', $attrEl, 2);
  94. if(count($tmp)<2){
  95. continue;
  96. }
  97. $key = trim($tmp[0]);
  98. $value = trim($tmp[1]);
  99. if($key=='version'||$key=='path'||$key=='expires'||$key=='domain'||$key=='comment'){
  100. if(!isset($arrObj[$key])){
  101. $arrObj[$key] = $value;
  102. }
  103. }else{
  104. $arrObj['cookies'][$key] = $value;
  105. }
  106. }
  107. }
  108. if($object===true){
  109. $obj = (object)$arrObj;
  110. $return = $obj;
  111. }else{
  112. $return = $arrObj;
  113. }
  114. return $return;
  115. }
  116.  
  117. function http_build_cookie($array)
  118. {
  119. $result = [];
  120. foreach($array['cookies'] as $key => $value) {
  121. $result[] = "$key=$value";
  122. }
  123. return implode(";", $result);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement