| 1 |
<?php |
| 2 |
/** |
| 3 |
* Classes for CryptX |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Don't load this file direct! |
| 8 |
*/ |
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
return ; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* class to scan files for string |
| 15 |
*/ |
| 16 |
class rw_cryptx_FileSystemStringSearch |
| 17 |
{ |
| 18 |
// MEMBERS |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string $_searchPath path to file/directory to search |
| 22 |
*/ |
| 23 |
var $_searchPath; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var string $_searchString the string to search for |
| 27 |
*/ |
| 28 |
var $_searchString; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var array $_searchResults holds search result information |
| 32 |
*/ |
| 33 |
var $_searchResults; |
| 34 |
|
| 35 |
// CONSTRUCTOR |
| 36 |
|
| 37 |
/** |
| 38 |
* Class constructor |
| 39 |
* @param string $searchPath path to file or directory to search |
| 40 |
* @param string $searchString string to search for |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
function __construct($searchPath, $searchString) |
| 44 |
{ |
| 45 |
$this->_searchPath = $searchPath; |
| 46 |
$this->_searchString = $searchString; |
| 47 |
$this->_searchResults = array(); |
| 48 |
} |
| 49 |
|
| 50 |
// MANIPULATORS |
| 51 |
|
| 52 |
/** |
| 53 |
* Checks path is valid |
| 54 |
* @return bool |
| 55 |
*/ |
| 56 |
function isValidPath() |
| 57 |
{ |
| 58 |
if(file_exists($this->_searchPath)) { |
| 59 |
return true; |
| 60 |
} else { |
| 61 |
return false; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Determines if path is a file or directory |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
function searchPathIsFile() |
| 70 |
{ |
| 71 |
// check for trailing slash |
| 72 |
if(substr($this->_searchPath, -1, 1)=='/' || |
| 73 |
substr($this->_searchPath, -1, 1)=='\\') { |
| 74 |
return false; |
| 75 |
} else { |
| 76 |
return true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Searches given file for search term |
| 82 |
* @param string $file the file path |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
function searchFileForString($file) |
| 86 |
{ |
| 87 |
// open file to an array |
| 88 |
$fileLines = file($file); |
| 89 |
|
| 90 |
// loop through lines and look for search term |
| 91 |
$lineNumber = 1; |
| 92 |
foreach($fileLines as $line) { |
| 93 |
$searchCount = substr_count($line, $this->_searchString); |
| 94 |
if($searchCount > 0) { |
| 95 |
// log result |
| 96 |
$this->addResult($file, $line, $lineNumber, $searchCount); |
| 97 |
} |
| 98 |
$lineNumber++; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Adds result to the result array |
| 104 |
* @param string $lineContents the line itself |
| 105 |
* @param int $lineNumber the file line number |
| 106 |
* @param int $searchCount the number of occurances of the search term |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
function addResult($filePath, $lineContents, $lineNumber, $searchCount) |
| 110 |
{ |
| 111 |
$this->_searchResults[] = array('filePath' => $filePath, |
| 112 |
'lineContents' => $lineContents, |
| 113 |
'lineNumber' => $lineNumber, |
| 114 |
'searchCount' => $searchCount); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Takes a given string (usually a line from search results) |
| 119 |
* and highlights the search term |
| 120 |
* @param string $string the string containing the search term(s) |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
function highlightSearchTerm($string) |
| 124 |
{ |
| 125 |
return str_replace($this->_searchString, |
| 126 |
'<strong>'.$this->_searchString.'</strong>', |
| 127 |
$string); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Recursively scan a folder and sub folders for search term |
| 132 |
* @param string path to the directory to search |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
function scanDirectoryForString($dir) |
| 136 |
{ |
| 137 |
$subDirs = array(); |
| 138 |
$dirFiles = array(); |
| 139 |
|
| 140 |
$dh = opendir($dir); |
| 141 |
while(($node = readdir($dh)) !== false) { |
| 142 |
// ignore . and .. nodes |
| 143 |
if(!($node=='.' || $node=='..')) { |
| 144 |
if(is_dir($dir.$node)) { |
| 145 |
$subDirs[] = $dir.$node.'/'; |
| 146 |
} else { |
| 147 |
$dirFiles[] = $dir.$node; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// loop through files and search for string |
| 153 |
foreach($dirFiles as $file) { |
| 154 |
$this->searchFileForString($file); |
| 155 |
} |
| 156 |
|
| 157 |
// if there are sub directories, scan them |
| 158 |
if(count($subDirs) > 0) { |
| 159 |
foreach($subDirs as $subDir) { |
| 160 |
$this->scanDirectoryForString($subDir); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Run the search |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
function run() |
| 170 |
{ |
| 171 |
// check path exists |
| 172 |
if($this->isValidPath()) { |
| 173 |
|
| 174 |
if($this->searchPathIsFile()) { |
| 175 |
// run search on the file |
| 176 |
$this->searchFileForString($this->_searchPath); |
| 177 |
} else { |
| 178 |
// scan directory contents for string |
| 179 |
$this->scanDirectoryForString($this->_searchPath); |
| 180 |
} |
| 181 |
|
| 182 |
} else { |
| 183 |
|
| 184 |
die('FileSystemStringSearch Error: File/Directory does not exist'); |
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// ACCESSORS |
| 190 |
function getResults() |
| 191 |
{ |
| 192 |
return $this->_searchResults; |
| 193 |
} |
| 194 |
|
| 195 |
function getResultCount() |
| 196 |
{ |
| 197 |
$count = 0; |
| 198 |
foreach($this->_searchResults as $result) { |
| 199 |
$count += $result['searchCount']; |
| 200 |
} |
| 201 |
return $count; |
| 202 |
} |
| 203 |
|
| 204 |
function getSearchPath() |
| 205 |
{ |
| 206 |
return $this->_searchPath; |
| 207 |
} |
| 208 |
|
| 209 |
function getSearchString() |
| 210 |
{ |
| 211 |
return $this->_searchString; |
| 212 |
} |
| 213 |
} |
| 214 |
?> |