PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/php-intro/old/assn05.php

https://github.com/arwhyte/tsugi
PHP | 274 lines | 224 code | 43 blank | 7 comment | 43 complexity | 9d69f4c6ce9c9267cf334d472517a435 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. require_once "../../config.php";
  3. require_once "webauto-old.php";
  4. require_once "misc.php";
  5. use Goutte\Client;
  6. line_out("Grading PHP-Intro Assignment 5");
  7. $url = getUrl('http://www.php-intro.com/assn/tracks');
  8. $grade = 0;
  9. error_log("ASSN05 ".$url);
  10. line_out("Retrieving ".htmlent_utf8($url)."...");
  11. flush();
  12. $client = new Client();
  13. $crawler = $client->request('GET', $url);
  14. // Yes, one gigantic unindented try/catch block
  15. $passed = 0;
  16. $titlepassed = true;
  17. try {
  18. $html = $crawler->html();
  19. $OUTPUT->togglePre("Show retrieved page",$html);
  20. $retval = webauto_check_title($crawler);
  21. if ( $retval !== true ) {
  22. error_out($retval);
  23. $titlepassed = false;
  24. }
  25. line_out("Looking for Add New link.");
  26. $link = $crawler->selectLink('Add New')->link();
  27. $url = $link->getURI();
  28. line_out("Retrieving ".htmlent_utf8($url)."...");
  29. $crawler = $client->request('GET', $url);
  30. $html = $crawler->html();
  31. $OUTPUT->togglePre("Show retrieved page",$html);
  32. $passed++;
  33. // Add new fail
  34. line_out("Looking for the form with a 'Add New' submit button");
  35. $form = $crawler->selectButton('Add New')->form();
  36. line_out("Setting non-integer values in the plays and rating form fields and leaving title blank");
  37. $form->setValues(array("plays" => "many", "rating" => "awesome"));
  38. $crawler = $client->submit($form);
  39. $passed++;
  40. $html = $crawler->html();
  41. $OUTPUT->togglePre("Show retrieved page",$html);
  42. checkPostRedirect($client);
  43. line_out("Expecting 'Bad value for title, plays, or rating'");
  44. if ( strpos(strtolower($html), 'bad value') !== false ) {
  45. $passed++;
  46. } else {
  47. error_out("Could not find 'Bad value for title, plays, or rating'");
  48. }
  49. line_out("Looking for Add New link.");
  50. $link = $crawler->selectLink('Add New')->link();
  51. $url = $link->getURI();
  52. line_out("Retrieving ".htmlent_utf8($url)."...");
  53. $crawler = $client->request('GET', $url);
  54. $html = $crawler->html();
  55. $OUTPUT->togglePre("Show retrieved page",$html);
  56. $passed++;
  57. line_out("Looking for the form with a 'Add New' submit button");
  58. $form = $crawler->selectButton('Add New')->form();
  59. $title = 'ACDC'.sprintf("%03d",rand(1,100));
  60. $plays = rand(1,100);
  61. $rating = rand(1,100);
  62. line_out("Entering title=$title, plays=$plays, rating=$rating");
  63. $form->setValues(array("title" => $title, "plays" => $plays, "rating" => $rating));
  64. $crawler = $client->submit($form);
  65. $passed++;
  66. $html = $crawler->html();
  67. $OUTPUT->togglePre("Show retrieved page",$html);
  68. checkPostRedirect($client);
  69. line_out("Looking '$title' entry");
  70. $pos = strpos($html, $title);
  71. $pos2 = strpos($html, "edit.php", $pos);
  72. $body = substr($html,$pos,$pos2-$pos);
  73. # echo "body=",htmlentities($body);
  74. line_out("Looking for plays=$plays and rating=$rating");
  75. if ( strpos($body,''.$plays) < 1 || strpos($body,''.$rating) < 1 ) {
  76. error_out("Could not find plays=$plays and rating=$rating");
  77. } else {
  78. $passed++;
  79. }
  80. line_out("Looking for edit.php link associated with '$title' entry");
  81. $pos3 = strpos($html, '"', $pos2);
  82. $editlink = substr($html,$pos2,$pos3-$pos2);
  83. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  84. $crawler = $client->request('GET', $editlink);
  85. $html = $crawler->html();
  86. $OUTPUT->togglePre("Show retrieved page",$html);
  87. $passed++;
  88. line_out("Looking for the form with a 'Update' submit button");
  89. $form = $crawler->selectButton('Update')->form();
  90. $plays = rand(1,100);
  91. $rating = rand(1,100);
  92. line_out("Editing title=$title, plays=$plays, rating=$rating");
  93. $form->setValues(array("title" => $title, "plays" => $plays, "rating" => $rating));
  94. $crawler = $client->submit($form);
  95. $html = $crawler->html();
  96. $OUTPUT->togglePre("Show retrieved page",$html);
  97. $passed++;
  98. checkPostRedirect($client);
  99. // Delete...
  100. line_out("Looking '$title' entry");
  101. $pos = strpos($html, $title);
  102. $pos2 = strpos($html, "delete.php", $pos);
  103. $body = substr($html,$pos,$pos2-$pos);
  104. # echo "body=",htmlentities($body);
  105. line_out("Looking for plays=$plays and rating=$rating");
  106. if ( strpos($body,''.$plays) < 1 || strpos($body,''.$rating) < 1 ) {
  107. error_out("Could not find plays=$plays and rating=$rating");
  108. } else {
  109. $passed++;
  110. }
  111. line_out("Looking for delete.php link associated with '$title' entry");
  112. $pos3 = strpos($html, '"', $pos2);
  113. $editlink = substr($html,$pos2,$pos3-$pos2);
  114. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  115. $crawler = $client->request('GET', $editlink);
  116. $html = $crawler->html();
  117. $OUTPUT->togglePre("Show retrieved page",$html);
  118. $passed++;
  119. // Do the Delete
  120. line_out("Looking for the form with a 'Delete' submit button");
  121. $form = $crawler->selectButton('Delete')->form();
  122. $crawler = $client->submit($form);
  123. $html = $crawler->html();
  124. $OUTPUT->togglePre("Show retrieved page",$html);
  125. $passed++;
  126. checkPostRedirect($client);
  127. line_out("Making sure '$title' has been deleted");
  128. if ( strpos($html,$title) > 0 ) {
  129. error_out("Entry '$title' not deleted");
  130. } else {
  131. $passed++;
  132. }
  133. line_out("Cleaning up old ACDC records...");
  134. while (True ) {
  135. $pos = strpos($html, 'ACDC');
  136. if ( $pos < 1 ) break;
  137. $pos2 = strpos($html, "delete.php", $pos);
  138. if ( $pos2 < 1 ) break;
  139. $pos3 = strpos($html, '"', $pos2);
  140. if ( $pos3 < 1 ) break;
  141. $editlink = substr($html,$pos2,$pos3-$pos2);
  142. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  143. $crawler = $client->request('GET', $editlink);
  144. $html = $crawler->html();
  145. $OUTPUT->togglePre("Show retrieved page",$html);
  146. $passed++;
  147. // Do the Delete
  148. line_out("Looking for the form with a 'Delete' submit button");
  149. $form = $crawler->selectButton('Delete')->form();
  150. $crawler = $client->submit($form);
  151. $html = $crawler->html();
  152. $OUTPUT->togglePre("Show retrieved page",$html);
  153. $passed++;
  154. checkPostRedirect($client);
  155. }
  156. line_out("Testing for HTML injection (proper use of htmlentities)...");
  157. line_out("Looking for Add New link.");
  158. $link = $crawler->selectLink('Add New')->link();
  159. $url = $link->getURI();
  160. line_out("Retrieving ".htmlent_utf8($url)."...");
  161. $crawler = $client->request('GET', $url);
  162. $html = $crawler->html();
  163. $OUTPUT->togglePre("Show retrieved page",$html);
  164. $passed++;
  165. line_out("Looking for the form with a 'Add New' submit button");
  166. $form = $crawler->selectButton('Add New')->form();
  167. $title = 'AC<DC'.sprintf("%03d",rand(1,100));
  168. $plays = rand(1,100);
  169. $rating = rand(1,100);
  170. line_out("Entering title=$title, plays=$plays, rating=$rating");
  171. $form->setValues(array("title" => $title, "plays" => $plays, "rating" => $rating));
  172. $crawler = $client->submit($form);
  173. $passed++;
  174. $html = $crawler->html();
  175. $OUTPUT->togglePre("Show retrieved page",$html);
  176. checkPostRedirect($client);
  177. $pos = strpos($html, "AC&lt;DC");
  178. if ( $pos > 0 ) {
  179. $passed+=2;
  180. } else {
  181. error_out("Found HTML Injection");
  182. throw new Exception("Found HTML Injection");
  183. }
  184. $pos2 = strpos($html, "delete.php", $pos);
  185. line_out("Looking for delete.php link associated with '$title' entry");
  186. $pos3 = strpos($html, '"', $pos2);
  187. $editlink = substr($html,$pos2,$pos3-$pos2);
  188. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  189. $crawler = $client->request('GET', $editlink);
  190. $html = $crawler->html();
  191. $OUTPUT->togglePre("Show retrieved page",$html);
  192. $passed++;
  193. $pos = strpos($html, "AC&lt;DC");
  194. if ( $pos > 0 ) {
  195. $passed+=2;
  196. } else {
  197. error_out("Found HTML Injection");
  198. throw new Exception("Found HTML Injection");
  199. }
  200. line_out("Looking for the form with a 'Delete' submit button");
  201. $form = $crawler->selectButton('Delete')->form();
  202. $crawler = $client->submit($form);
  203. $html = $crawler->html();
  204. $OUTPUT->togglePre("Show retrieved page",$html);
  205. $passed++;
  206. checkPostRedirect($client);
  207. } catch (Exception $ex) {
  208. error_out("The autograder did not find something it was looking for in your HTML - test ended.");
  209. error_log($ex->getMessage());
  210. error_log($ex->getTraceAsString());
  211. $detail = "This indicates the source code line where the test stopped.\n" .
  212. "It may not make any sense without looking at the source code for the test.\n".
  213. 'Caught exception: '.$ex->getMessage()."\n".$ex->getTraceAsString()."\n";
  214. $OUTPUT->togglePre("Internal error detail.",$detail);
  215. }
  216. $perfect = 26;
  217. $score = $passed * (1.0 / $perfect);
  218. if ( $score < 0 ) $score = 0;
  219. if ( $score > 1 ) $score = 1;
  220. $scorestr = "Score = $score ($passed/$perfect)";
  221. if ( $penalty === false ) {
  222. line_out("Score = $score ($passed/$perfect)");
  223. } else {
  224. $score = $score * (1.0 - $penalty);
  225. line_out("Score = $score ($passed/$perfect) penalty=$penalty");
  226. }
  227. if ( ! $titlepassed ) {
  228. error_out("These pages do not have proper titles so this grade is not official");
  229. return;
  230. }
  231. if ( $score > 0.0 ) webauto_test_passed($score, $url);