PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/theme-check/checkbase.php

https://gitlab.com/f3z0/tc-site
PHP | 311 lines | 51 code | 13 blank | 247 comment | 4 complexity | 3ffd8f985c420c90126fd78fbf8daeb0 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, Apache-2.0, LGPL-2.1
  1. <?php
  2. // main global to hold our checks
  3. global $themechecks;
  4. $themechecks = array();
  5. // counter for the checks
  6. global $checkcount;
  7. $checkcount = 0;
  8. // interface that all checks should implement
  9. interface themecheck
  10. {
  11. // should return true for good/okay/acceptable, false for bad/not-okay/unacceptable
  12. public function check( $php_files, $css_files, $other_files );
  13. // should return an array of strings explaining any problems found
  14. public function getError();
  15. }
  16. // load all the checks in the checks directory
  17. $dir = 'checks';
  18. foreach (glob(dirname(__FILE__). "/{$dir}/*.php") as $file) {
  19. include $file;
  20. }
  21. do_action('themecheck_checks_loaded');
  22. function run_themechecks($php, $css, $other) {
  23. global $themechecks;
  24. $pass = true;
  25. foreach($themechecks as $check) {
  26. if ($check instanceof themecheck) {
  27. $pass = $pass & $check->check($php, $css, $other);
  28. }
  29. }
  30. return $pass;
  31. }
  32. function display_themechecks() {
  33. $results = '';
  34. global $themechecks;
  35. $errors = array();
  36. foreach ($themechecks as $check) {
  37. if ($check instanceof themecheck) {
  38. $error = $check->getError();
  39. $error = (array) $error;
  40. if (!empty($error)) {
  41. $errors = array_unique( array_merge( $error, $errors ) );
  42. }
  43. }
  44. }
  45. if (!empty($errors)) {
  46. rsort($errors);
  47. foreach ($errors as $e) {
  48. if ( defined( 'TC_TRAC' ) ) {
  49. $results .= ( isset( $_POST['s_info'] ) && preg_match( '/INFO/', $e ) ) ? '' : '* ' . tc_trac( $e ) . "\r\n";
  50. } else {
  51. $results .= ( isset( $_POST['s_info'] ) && preg_match( '/INFO/', $e ) ) ? '' : '<li>' . tc_trac( $e ) . '</li>';
  52. }
  53. }
  54. }
  55. if ( defined( 'TC_TRAC' ) ) {
  56. if ( defined( 'TC_PRE' ) ) $results = TC_PRE . $results;
  57. $results = '<textarea cols=140 rows=20>' . strip_tags( $results );
  58. if ( defined( 'TC_POST' ) ) $results = $results . TC_POST;
  59. $results .= '</textarea>';
  60. }
  61. return $results;
  62. }
  63. function checkcount() {
  64. global $checkcount;
  65. $checkcount++;
  66. }
  67. // some functions theme checks use
  68. function tc_grep( $error, $file ) {
  69. $lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
  70. $line_index = 0;
  71. $bad_lines = '';
  72. foreach( $lines as $this_line ) {
  73. if ( stristr ( $this_line, $error ) ) {
  74. $error = str_replace( '"', "'", $error );
  75. $this_line = str_replace( '"', "'", $this_line );
  76. $error = ltrim( $error );
  77. $pre = ( FALSE !== ( $pos = strpos( $this_line, $error ) ) ? substr( $this_line, 0, $pos ) : FALSE );
  78. $pre = ltrim( htmlspecialchars( $pre ) );
  79. $bad_lines .= "<pre class='tc-grep'>". __("Line ", "theme-check") . ( $line_index+1 ) . ": " . $pre . htmlspecialchars( substr( stristr( $this_line, $error ), 0, 75 ) ) . "</pre>";
  80. }
  81. $line_index++;
  82. }
  83. return str_replace( $error, '<span class="tc-grep">' . $error . '</span>', $bad_lines );
  84. }
  85. function tc_preg( $preg, $file ) {
  86. $lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
  87. $line_index = 0;
  88. $bad_lines = '';
  89. $error = '';
  90. foreach( $lines as $this_line ) {
  91. if ( preg_match( $preg, $this_line, $matches ) ) {
  92. $error = $matches[0];
  93. $this_line = str_replace( '"', "'", $this_line );
  94. $error = ltrim( $error );
  95. $pre = ( FALSE !== ( $pos = strpos( $this_line, $error ) ) ? substr( $this_line, 0, $pos ) : FALSE );
  96. $pre = ltrim( htmlspecialchars( $pre ) );
  97. $bad_lines .= "<pre class='tc-grep'>" . __("Line ", "theme-check") . ( $line_index+1 ) . ": " . $pre . htmlspecialchars( substr( stristr( $this_line, $error ), 0, 75 ) ) . "</pre>";
  98. }
  99. $line_index++;
  100. }
  101. return str_replace( $error, '<span class="tc-grep">' . $error . '</span>', $bad_lines );
  102. }
  103. function tc_strxchr($haystack, $needle, $l_inclusive = 0, $r_inclusive = 0){
  104. if(strrpos($haystack, $needle)){
  105. //Everything before last $needle in $haystack.
  106. $left = substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);
  107. //Switch value of $r_inclusive from 0 to 1 and viceversa.
  108. $r_inclusive = ($r_inclusive == 0) ? 1 : 0;
  109. //Everything after last $needle in $haystack.
  110. $right = substr(strrchr($haystack, $needle), $r_inclusive);
  111. //Return $left and $right into an array.
  112. return array($left, $right);
  113. } else {
  114. if(strrchr($haystack, $needle)) return array('', substr(strrchr($haystack, $needle), $r_inclusive));
  115. else return false;
  116. }
  117. }
  118. function tc_filename( $file ) {
  119. $filename = ( preg_match( '/themes\/[a-z0-9]*\/(.*)/', $file, $out ) ) ? $out[1] : basename( $file );
  120. return $filename;
  121. }
  122. function tc_trac( $e ) {
  123. $trac_left = array( '<strong>', '</strong>' );
  124. $trac_right= array( "'''", "'''" );
  125. $html_link = '/<a\s?href\s?=\s?[\'|"]([^"|\']*)[\'|"]>([^<]*)<\/a>/i';
  126. $html_new = '[$1 $2]';
  127. if ( defined( 'TC_TRAC' ) ) {
  128. $e = preg_replace( $html_link, $html_new, $e );
  129. $e = str_replace( $trac_left, $trac_right, $e );
  130. $e = preg_replace( '/<pre.*?>/', "\r\n{{{\r\n", $e );
  131. $e = str_replace( '</pre>', "\r\n}}}\r\n", $e );
  132. }
  133. return $e;
  134. }
  135. function listdir( $dir ) {
  136. $files = array();
  137. $dir_iterator = new RecursiveDirectoryIterator( $dir );
  138. $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
  139. foreach ($iterator as $file) {
  140. array_push( $files, $file->getPathname() );
  141. }
  142. return $files;
  143. }
  144. function old_listdir( $start_dir='.' ) {
  145. $files = array();
  146. if ( is_dir( $start_dir ) ) {
  147. $fh = opendir( $start_dir );
  148. while ( ( $file = readdir( $fh ) ) !== false ) {
  149. # loop through the files, skipping . and .., and recursing if necessary
  150. if ( strcmp( $file, '.' )==0 || strcmp( $file, '..' )==0 ) continue;
  151. $filepath = $start_dir . '/' . $file;
  152. if ( is_dir( $filepath ) )
  153. $files = array_merge( $files, listdir( $filepath ) );
  154. else
  155. array_push( $files, $filepath );
  156. }
  157. closedir( $fh );
  158. } else {
  159. # false if the function was called with an invalid non-directory argument
  160. $files = false;
  161. }
  162. return $files;
  163. }
  164. function tc_print_r( $data ) {
  165. $out = "\n<pre class='html-print-r'";
  166. $out .= " style='border: 1px solid #ccc; padding: 7px;'>\n";
  167. $out .= esc_html( print_r( $data, TRUE ) );
  168. $out .= "\n</pre>\n";
  169. echo $out;
  170. }
  171. function get_theme_data_from_contents( $theme_data ) {
  172. $themes_allowed_tags = array(
  173. 'a' => array(
  174. 'href' => array(),'title' => array()
  175. ),
  176. 'abbr' => array(
  177. 'title' => array()
  178. ),
  179. 'acronym' => array(
  180. 'title' => array()
  181. ),
  182. 'code' => array(),
  183. 'em' => array(),
  184. 'strong' => array()
  185. );
  186. $theme_data = str_replace ( '\r', '\n', $theme_data );
  187. preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name );
  188. preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri );
  189. preg_match( '|Description:(.*)$|mi', $theme_data, $description );
  190. if ( preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri ) )
  191. $author_uri = esc_url( trim( $author_uri[1]) );
  192. else
  193. $author_uri = '';
  194. if ( preg_match( '|Template:(.*)$|mi', $theme_data, $template ) )
  195. $template = wp_kses( trim( $template[1] ), $themes_allowed_tags );
  196. else
  197. $template = '';
  198. if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
  199. $version = wp_kses( trim( $version[1] ), $themes_allowed_tags );
  200. else
  201. $version = '';
  202. if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
  203. $status = wp_kses( trim( $status[1] ), $themes_allowed_tags );
  204. else
  205. $status = 'publish';
  206. if ( preg_match('|Tags:(.*)|i', $theme_data, $tags) )
  207. $tags = array_map( 'trim', explode( ',', wp_kses( trim( $tags[1] ), array() ) ) );
  208. else
  209. $tags = array();
  210. $theme = ( isset( $theme_name[1] ) ) ? wp_kses( trim( $theme_name[1] ), $themes_allowed_tags ) : '';
  211. $theme_uri = ( isset( $theme_uri[1] ) ) ? esc_url( trim( $theme_uri[1] ) ) : '';
  212. $description = ( isset( $description[1] ) ) ? wp_kses( trim( $description[1] ), $themes_allowed_tags ) : '';
  213. if ( preg_match( '|Author:(.*)$|mi', $theme_data, $author_name ) ) {
  214. if ( empty( $author_uri ) ) {
  215. $author = wp_kses( trim( $author_name[1] ), $themes_allowed_tags );
  216. } else {
  217. $author = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $author_uri, __( 'Visit author homepage' ), wp_kses( trim( $author_name[1] ), $themes_allowed_tags ) );
  218. }
  219. } else {
  220. $author = __('Anonymous');
  221. }
  222. return array( 'Name' => $theme, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Author_URI' => $author_uri, 'Version' => $version, 'Template' => $template, 'Status' => $status, 'Tags' => $tags );
  223. }
  224. /*
  225. * 3.3/3.4 compat
  226. *
  227. */
  228. function tc_get_themes() {
  229. if ( ! class_exists( 'WP_Theme' ) )
  230. return get_themes();
  231. global $wp_themes;
  232. if ( isset( $wp_themes ) )
  233. return $wp_themes;
  234. $themes = wp_get_themes();
  235. $wp_themes = array();
  236. foreach ( $themes as $theme ) {
  237. $name = $theme->get('Name');
  238. if ( isset( $wp_themes[ $name ] ) )
  239. $wp_themes[ $name . '/' . $theme->get_stylesheet() ] = $theme;
  240. else
  241. $wp_themes[ $name ] = $theme;
  242. }
  243. return $wp_themes;
  244. }
  245. function tc_get_theme_data( $theme_file ) {
  246. if ( ! class_exists( 'WP_Theme' ) )
  247. return get_theme_data( $theme_file );
  248. $theme = new WP_Theme( basename( dirname( $theme_file ) ), dirname( dirname( $theme_file ) ) );
  249. $theme_data = array(
  250. 'Name' => $theme->get('Name'),
  251. 'URI' => $theme->display('ThemeURI', true, false),
  252. 'Description' => $theme->display('Description', true, false),
  253. 'Author' => $theme->display('Author', true, false),
  254. 'AuthorURI' => $theme->display('AuthorURI', true, false),
  255. 'Version' => $theme->get('Version'),
  256. 'Template' => $theme->get('Template'),
  257. 'Status' => $theme->get('Status'),
  258. 'Tags' => $theme->get('Tags'),
  259. 'Title' => $theme->get('Name'),
  260. 'AuthorName' => $theme->display('Author', false, false),
  261. 'License' => $theme->display( 'License', false, false),
  262. 'License URI' => $theme->display( 'License URI', false, false),
  263. 'Template Version' => $theme->display( 'Template Version', false, false)
  264. );
  265. return $theme_data;
  266. }