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

/dist/tenonTest.class.php

https://gitlab.com/tenon-io/tenon-js-embed-code
PHP | 205 lines | 123 code | 36 blank | 46 comment | 23 complexity | 18ed8ff2390cfdbf162f72ddb93e187d MD5 | raw file
  1. <?php
  2. /**
  3. * This class submits a request against the Tenon API for automatic
  4. * accessibility testing.
  5. *
  6. */
  7. class tenonTest
  8. {
  9. protected $url, $opts, $hash;
  10. public $tenonResponse, $tURL;
  11. /**
  12. * Class constructor
  13. *
  14. * @param string $url the API url to post your request to
  15. * @param array $opts options for the request
  16. */
  17. public function __construct($url, $opts)
  18. {
  19. $this->url = $url;
  20. $this->opts = $opts;
  21. $this->hash = md5(serialize($opts['src']));
  22. $this->tenonResponse = '';
  23. }
  24. /**
  25. * Submits the HTML source for testing
  26. *
  27. * @param bool $printInfo whether or not to print the output from curl_getinfo (usually for debugging only)
  28. *
  29. * @return string the results, formatted as JSON
  30. */
  31. public function submit($printInfo = false)
  32. {
  33. if (true == $printInfo) {
  34. echo '<h2>Options Passed To TenonTest</h2><pre><br>';
  35. var_dump($this->opts);
  36. echo '</pre>';
  37. }
  38. //open connection
  39. $ch = curl_init();
  40. curl_setopt($ch, CURLOPT_URL, $this->url);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  42. curl_setopt($ch, CURLOPT_POST, true);
  43. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  44. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->opts);
  45. //execute post and get results
  46. $result = curl_exec($ch);
  47. if (true == $printInfo) {
  48. echo 'ERROR INFO (if any): ' . curl_error($ch) . '<br>';
  49. echo '<h2>Curl Info </h2><pre><br>';
  50. print_r(curl_getinfo($ch));
  51. echo '</pre>';
  52. }
  53. $this->tCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  54. //close connection
  55. curl_close($ch);
  56. //the test results
  57. $this->tenonResponse = $result;
  58. }
  59. /**
  60. *
  61. * @param $array
  62. * @param int $minlength
  63. *
  64. * @return array|bool
  65. */
  66. protected function cleanArray($array, $minlength = 32)
  67. {
  68. $output = array();
  69. if (!is_array($array)) {
  70. return false;
  71. }
  72. foreach ($array AS $val) {
  73. if (strlen($val) < $minlength) {
  74. continue;
  75. } else {
  76. $output[] = trim($val);
  77. }
  78. }
  79. return $output;
  80. }
  81. /**
  82. *
  83. *
  84. * @return bool
  85. */
  86. public function hashExists()
  87. {
  88. if (file_exists(HASH_FILEPATH)) {
  89. $array = file(HASH_FILEPATH);
  90. $array = $this->cleanArray($array);
  91. if (false == in_array(trim($this->hash), $array, true)) {
  92. $handle = fopen(HASH_FILEPATH, 'a+');
  93. if (false != $handle) {
  94. fwrite($handle, $this->hash . PHP_EOL);
  95. fclose($handle);
  96. }
  97. return false;
  98. } else {
  99. return true;
  100. }
  101. }
  102. }
  103. /**
  104. * @return mixed
  105. */
  106. public function decodeResponse()
  107. {
  108. if ((false !== $this->tenonResponse) && (!is_null($this->tenonResponse))) {
  109. $result = json_decode($this->tenonResponse, true);
  110. if (!is_null($result)) {
  111. $this->rspArray = $result;
  112. } else {
  113. return false;
  114. }
  115. } else {
  116. return false;
  117. }
  118. }
  119. /**
  120. *
  121. * @param $pathToFolder
  122. * @param null $name
  123. * @param string $mode
  124. *
  125. * @return bool
  126. */
  127. public function writeResultsToCSV($pathToFolder, $name = null, $mode = 'a+')
  128. {
  129. $this->decodeResponse();
  130. $issues = $this->rspArray['resultSet'];
  131. if (is_null($name)) {
  132. $name = htmlspecialchars($this->rspArray['request']['docID']) . '.csv';
  133. }
  134. $count = count($issues);
  135. if ($count < 1) {
  136. return false;
  137. }
  138. for ($x = 0; $x < $count; $x++) {
  139. $rows[$x] = array(
  140. $this->tURL,
  141. $issues[$x]['tID'],
  142. $issues[$x]['resultTitle'],
  143. $issues[$x]['errorTitle'],
  144. $issues[$x]['errorDescription'],
  145. implode(', ', $issues[$x]['standards']),
  146. html_entity_decode($issues[$x]['errorSnippet']),
  147. $issues[$x]['position']['line'],
  148. $issues[$x]['position']['column'],
  149. $issues[$x]['xpath'],
  150. $issues[$x]['certainty'],
  151. $issues[$x]['priority'],
  152. $issues[$x]['ref'],
  153. $issues[$x]['signature']
  154. );
  155. }
  156. // Put a row of headers up on the beginning
  157. array_unshift($rows, array('URL', 'testID', 'Best Practice', 'Issue Title', 'Description',
  158. 'WCAG SC', 'Issue Code', 'Line', 'Column', 'xPath', 'Certainty', 'Priority', 'Reference', 'Signature'));
  159. $fp = fopen($pathToFolder . $name, $mode);
  160. if (false != $fp) {
  161. foreach ($rows as $fields) {
  162. fputcsv($fp, $fields);
  163. }
  164. fclose($fp);
  165. return true;
  166. }
  167. return false;
  168. }
  169. }