PageRenderTime 67ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/php-intro/crud-videos.php

https://github.com/arwhyte/tsugi
PHP | 285 lines | 228 code | 48 blank | 9 comment | 46 complexity | 0b3c56e5f328adf6480b811e51d6b455 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 Video Application");
  6. $url = getUrl('http://www.php-intro.com/exam/mid-w14-videos');
  7. if ( $url === false ) return;
  8. $grade = 0;
  9. error_log("Videos ".$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.");
  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("-- this autograder expects the form field names to be:");
  37. line_out("-- url, email, length, and rating");
  38. line_out("-- if your fields do not match these, the next tests will fail.");
  39. line_out("Causing Add error, leaving length and rating blank.");
  40. $form->setValues(array("url" => "Sarah", "email" => "Anytown", "length" => "", "rating" => ""));
  41. $crawler = $client->submit($form);
  42. $passed++;
  43. $html = $crawler->html();
  44. $OUTPUT->togglePre("Show retrieved page",$html);
  45. checkPostRedirect($client);
  46. line_out("Expecting 'All values are required'");
  47. if ( strpos(strtolower($html), 'are required') !== false ) {
  48. $passed++;
  49. } else {
  50. error_out("Could not find 'All values are required'");
  51. }
  52. line_out("Looking in add.php for an 'Add New' submit button");
  53. $form = $crawler->selectButton('Add New')->form();
  54. line_out("Causing Add error, putting in bad email address.");
  55. $form->setValues(array("url" => "http://www.php-intro.com", "email" => "PO Box 123", "length" => "12", "rating" => "6"));
  56. $crawler = $client->submit($form);
  57. $passed++;
  58. $html = $crawler->html();
  59. $OUTPUT->togglePre("Show retrieved page",$html);
  60. checkPostRedirect($client);
  61. line_out("Expecting 'Error in input data'");
  62. if ( strpos(strtolower($html), 'error in') !== false ) {
  63. $passed++;
  64. } else {
  65. error_out("Could not find 'Error in input data'");
  66. }
  67. line_out("Looking for the form with a 'Add New' submit button");
  68. $form = $crawler->selectButton('Add New')->form();
  69. $url = 'http://www.php-intro.com/x.php?data='.sprintf("%03d",rand(1,100));
  70. $email = "sarah@php-intro.com";
  71. $length = rand(1,100);
  72. $rating = rand(1,100);
  73. line_out("Entering url=$url, email=$email, length=$length");
  74. $form->setValues(array("url" => $url, "email" => $email, "length" => $length, "rating" => "12345"));
  75. $crawler = $client->submit($form);
  76. $passed++;
  77. $html = $crawler->html();
  78. $OUTPUT->togglePre("Show retrieved page",$html);
  79. checkPostRedirect($client);
  80. line_out("Looking '$url' entry");
  81. $pos = strpos($html, $url);
  82. $pos2 = strpos($html, "edit.php", $pos);
  83. $body = substr($html,$pos,$pos2-$pos);
  84. # echo "body=",htmlentities($body);
  85. line_out("Looking for email=$email and length=$length");
  86. if ( strpos($body,''.$email) < 1 || strpos($body,''.$length) < 1 ) {
  87. error_out("Could not find email=$email and length=$length");
  88. } else {
  89. $passed++;
  90. }
  91. line_out("Looking for edit.php link associated with '$url' entry");
  92. $pos3 = strpos($html, '"', $pos2);
  93. $editlink = substr($html,$pos2,$pos3-$pos2);
  94. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  95. $crawler = $client->request('GET', $editlink);
  96. $html = $crawler->html();
  97. $OUTPUT->togglePre("Show retrieved page",$html);
  98. $passed++;
  99. line_out("Looking for the form with a 'Update' submit button");
  100. $form = $crawler->selectButton('Update')->form();
  101. $length = rand(1,100);
  102. $rating = rand(1,100);
  103. line_out("Editing url=$url, length=$length, rating=$rating");
  104. $form->setValues(array("url" => $url, "email" => $email, "length" => $length, "rating" => "12345"));
  105. $crawler = $client->submit($form);
  106. $html = $crawler->html();
  107. $OUTPUT->togglePre("Show retrieved page",$html);
  108. $passed++;
  109. checkPostRedirect($client);
  110. // Delete...
  111. line_out("Looking '$url' entry");
  112. $pos = strpos($html, $url);
  113. $pos2 = strpos($html, "delete.php", $pos);
  114. $body = substr($html,$pos,$pos2-$pos);
  115. # echo "body=",htmlentities($body);
  116. line_out("Looking for email=$email and length=$length");
  117. if ( strpos($body,''.$email) < 1 || strpos($body,''.$length) < 1 ) {
  118. error_out("Could not find email=$email and length=$length");
  119. } else {
  120. $passed++;
  121. }
  122. line_out("Looking for delete.php link associated with '$url' entry");
  123. $pos3 = strpos($html, '"', $pos2);
  124. $editlink = substr($html,$pos2,$pos3-$pos2);
  125. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  126. $crawler = $client->request('GET', $editlink);
  127. $html = $crawler->html();
  128. $OUTPUT->togglePre("Show retrieved page",$html);
  129. $passed++;
  130. // Do the Delete
  131. line_out("Looking for the form with a 'Delete' submit button");
  132. $form = $crawler->selectButton('Delete')->form();
  133. $crawler = $client->submit($form);
  134. $html = $crawler->html();
  135. $OUTPUT->togglePre("Show retrieved page",$html);
  136. $passed++;
  137. checkPostRedirect($client);
  138. line_out("Making sure '$url' has been deleted");
  139. if ( strpos($html,$url) > 0 ) {
  140. error_out("Entry '$url' not deleted");
  141. } else {
  142. $passed++;
  143. }
  144. line_out("Cleaning up old records...");
  145. while (True ) {
  146. $pos2 = strpos($html, "delete.php");
  147. if ( $pos2 < 1 ) break;
  148. $pos3 = strpos($html, '"', $pos2);
  149. if ( $pos3 < 1 ) break;
  150. $editlink = substr($html,$pos2,$pos3-$pos2);
  151. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  152. $crawler = $client->request('GET', $editlink);
  153. $html = $crawler->html();
  154. $OUTPUT->togglePre("Show retrieved page",$html);
  155. // Do the Delete
  156. line_out("Looking for the form with a 'Delete' submit button");
  157. $form = $crawler->selectButton('Delete')->form();
  158. $crawler = $client->submit($form);
  159. $html = $crawler->html();
  160. $OUTPUT->togglePre("Show retrieved page",$html);
  161. checkPostRedirect($client);
  162. $passed--; // Undo post redirect
  163. }
  164. line_out("Testing for HTML injection (proper use of htmlentities)...");
  165. line_out("Looking for Add New link.");
  166. $link = $crawler->selectLink('Add New')->link();
  167. $url = $link->getURI();
  168. line_out("Retrieving ".htmlent_utf8($url)."...");
  169. $crawler = $client->request('GET', $url);
  170. $html = $crawler->html();
  171. $OUTPUT->togglePre("Show retrieved page",$html);
  172. $passed++;
  173. line_out("Looking for the form with a 'Add New' submit button");
  174. $form = $crawler->selectButton('Add New')->form();
  175. $url = 'http://www.php-intro.com/x.php?>data='.sprintf("%03d",rand(1,100));
  176. $email = "Sarah_is_so_>@php-intro.com";
  177. $length = rand(1,100);
  178. line_out("Entering url=$url, email=$email, length=$length");
  179. $form->setValues(array("url" => $url, "email" => $email, "length" => $length, "rating" => "12345"));
  180. $crawler = $client->submit($form);
  181. $passed++;
  182. $html = $crawler->html();
  183. $OUTPUT->togglePre("Show retrieved page",$html);
  184. checkPostRedirect($client);
  185. if ( strpos($html, "_>@php") > 0 ) {
  186. error_out("Found HTML Injection");
  187. throw new Exception("Found HTML Injection");
  188. } else if ( strpos($html, "_&gt;@php") > 0 ) {
  189. $passed+=2;
  190. line_out("Passed HTML Injection test");
  191. } else {
  192. error_out("Cannot find email address on page");
  193. }
  194. $pos = strpos($html,"Sarah");
  195. $pos2 = strpos($html, "delete.php", $pos);
  196. line_out("Looking for delete.php link associated with 'Sarah' entry");
  197. $pos3 = strpos($html, '"', $pos2);
  198. $editlink = substr($html,$pos2,$pos3-$pos2);
  199. line_out("Retrieving ".htmlent_utf8($editlink)."...");
  200. $crawler = $client->request('GET', $editlink);
  201. $html = $crawler->html();
  202. $OUTPUT->togglePre("Show retrieved page",$html);
  203. $passed++;
  204. if ( strpos($html, "x.php?>data") > 0 ) {
  205. error_out("Found HTML Injection");
  206. throw new Exception("Found HTML Injection");
  207. } else if ( strpos($html, "x.php?&gt;data") > 0 ) {
  208. $passed+=2;
  209. line_out("Passed HTML Injection test");
  210. } else {
  211. error_out("Cannot find email address on page");
  212. }
  213. // $passed+=2;
  214. line_out("Looking for the form with a 'Delete' submit button");
  215. $form = $crawler->selectButton('Delete')->form();
  216. $crawler = $client->submit($form);
  217. $html = $crawler->html();
  218. $OUTPUT->togglePre("Show retrieved page",$html);
  219. $passed++;
  220. checkPostRedirect($client);
  221. } catch (Exception $ex) {
  222. error_out("The autograder did not find something it was looking for in your HTML - test ended.");
  223. error_log($ex->getMessage());
  224. error_log($ex->getTraceAsString());
  225. $detail = "This indicates the source code line where the test stopped.\n" .
  226. "It may not make any sense without looking at the source code for the test.\n".
  227. 'Caught exception: '.$ex->getMessage()."\n".$ex->getTraceAsString()."\n";
  228. $OUTPUT->togglePre("Internal error detail.",$detail);
  229. }
  230. // There is a maximum of 28 passes for this test
  231. $perfect = 28;
  232. $score = webauto_compute_effective_score($perfect, $passed, $penalty);
  233. if ( ! $titlefound ) {
  234. error_out("These pages do not have proper titles so this grade is not official");
  235. return;
  236. }
  237. if ( $score > 0.0 ) webauto_test_passed($score, $url);