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