PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/php-intro/a06.php

https://github.com/arwhyte/tsugi
PHP | 264 lines | 213 code | 43 blank | 8 comment | 43 complexity | a8ab1f5529208445ba4ecc28119bddde MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. require_once "../../config.php";
  3. require_once "webauto.php";
  4. use Goutte\Client;
  5. line_out("Grading PHP-Intro Assignment 6");
  6. $url = getUrl('http://www.php-intro.com/assn/tracks');
  7. if ( $url === false ) return;
  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. $titlefound = 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. $titlefound = false;
  24. }
  25. line_out("Looking for Add New link in index.php.");
  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' error in index.php");
  44. if ( strpos(strtolower($html), 'bad value') !== false ) {
  45. $passed++;
  46. } else {
  47. error_out("Could not find flash message with 'Bad value for title, plays, or rating'");
  48. }
  49. line_out("Looking for Add New link in index.php.");
  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 records...");
  134. while (True ) {
  135. $pos2 = strpos($html, "delete.php");
  136. if ( $pos2 < 1 ) break;
  137. $pos3 = strpos($html, '"', $pos2);
  138. if ( $pos3 < 1 ) break;
  139. $editlink = substr($html,$pos2,$pos3-$pos2);
  140. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  141. $crawler = $client->request('GET', $editlink);
  142. $html = $crawler->html();
  143. $OUTPUT->togglePre("Show retrieved page",$html);
  144. // Do the Delete
  145. line_out("Looking for the form with a 'Delete' submit button");
  146. $form = $crawler->selectButton('Delete')->form();
  147. $crawler = $client->submit($form);
  148. $html = $crawler->html();
  149. $OUTPUT->togglePre("Show retrieved page",$html);
  150. checkPostRedirect($client);
  151. $passed--; // Since checkPostRedirect() gives a pass
  152. }
  153. line_out("Testing for HTML injection (proper use of htmlentities)...");
  154. line_out("Looking for Add New link in index.php.");
  155. $link = $crawler->selectLink('Add New')->link();
  156. $url = $link->getURI();
  157. line_out("Retrieving ".htmlent_utf8($url)."...");
  158. $crawler = $client->request('GET', $url);
  159. $html = $crawler->html();
  160. $OUTPUT->togglePre("Show retrieved page",$html);
  161. $passed++;
  162. line_out("Looking for the form with a 'Add New' submit button");
  163. $form = $crawler->selectButton('Add New')->form();
  164. $title = 'AC<DC'.sprintf("%03d",rand(1,100));
  165. $plays = rand(1,100);
  166. $rating = rand(1,100);
  167. line_out("Entering title=$title, plays=$plays, rating=$rating");
  168. $form->setValues(array("title" => $title, "plays" => $plays, "rating" => $rating));
  169. $crawler = $client->submit($form);
  170. $passed++;
  171. $html = $crawler->html();
  172. $OUTPUT->togglePre("Show retrieved page",$html);
  173. checkPostRedirect($client);
  174. if ( strpos($html, "AC&lt;DC") > 2 ) {
  175. $passed+=2;
  176. } else if ( strpos($html, "&amp;lt;") > 2 ) {
  177. error_out("It looks like you have double-called htmlentities()");
  178. } else {
  179. error_out("Found HTML Injection");
  180. }
  181. $pos = strpos($html, $title);
  182. $pos2 = strpos($html, "delete.php", $pos);
  183. line_out("Looking for delete.php link associated with '$title' entry");
  184. $pos3 = strpos($html, '"', $pos2);
  185. $editlink = substr($html,$pos2,$pos3-$pos2);
  186. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  187. $crawler = $client->request('GET', $editlink);
  188. $html = $crawler->html();
  189. $OUTPUT->togglePre("Show retrieved page",$html);
  190. $passed++;
  191. if ( strpos($html, "AC&lt;DC") > 2 ) {
  192. $passed+=2;
  193. } else if ( strpos($html, "&amp;lt;") > 2 ) {
  194. error_out("It looks like you have double-called htmlentities()");
  195. } else {
  196. error_out("Found HTML Injection");
  197. }
  198. line_out("Looking for the form with a 'Delete' submit button");
  199. $form = $crawler->selectButton('Delete')->form();
  200. $crawler = $client->submit($form);
  201. $html = $crawler->html();
  202. $OUTPUT->togglePre("Show retrieved page",$html);
  203. $passed++;
  204. checkPostRedirect($client);
  205. } catch (Exception $ex) {
  206. error_out("The autograder did not find something it was looking for in your HTML - test ended.");
  207. error_log($ex->getMessage());
  208. error_log($ex->getTraceAsString());
  209. $detail = "This indicates the source code line where the test stopped.\n" .
  210. "It may not make any sense without looking at the source code for the test.\n".
  211. 'Caught exception: '.$ex->getMessage()."\n".$ex->getTraceAsString()."\n";
  212. $OUTPUT->togglePre("Internal error detail.",$detail);
  213. }
  214. // There is a maximum of 26 passes for this test
  215. $perfect = 26;
  216. $score = webauto_compute_effective_score($perfect, $passed, $penalty);
  217. if ( ! $titlefound ) {
  218. error_out("These pages do not have proper titles so this grade is not official");
  219. return;
  220. }
  221. if ( $score > 0.0 ) webauto_test_passed($score, $url);