php class


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting php class
# 1  
Old 03-08-2010
php class

I was hoping you could help me out? Is it possible to add the ability to choose multiple directions to search within? I can have not figured out how to build the array with the form, something todo with [] in the value.

Any help would be appericated.

Index.php
PHP Code:
<html>
<
head>
<
title>DNS Search</title>
<
link href="css.css" rel="stylesheet" type="text/css">
</
head>
<
script type="text/javascript">
var 
ID_Message = new Array();
ID_Message["search"] = "Wait, Wait..... Wait.  You need to search for something?";
function 
CheckAllRequiredFields() {
var 
message = new Array();
for(var 
k in ID_Message) {
   if(!
document.getElementById(k).value.length) { message.push(ID_Message[k]); }
   }
if(
message.length) {
   
alert(message.join("\n\n"));
   return 
false;
   }
return 
true;
}
</script>
<body>
<form method="post" action="test.php" onSubmit="return CheckAllRequiredFields()">
  <p class="big">DNS Log Search  </p>
  <p>
    <input type="text" size="75" maxlength="100" name="search">
    <br>
  </p>
  <table width="451" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td>
        <div align="left">
          <input type="checkbox" name="logform[]" value="/opt/coolstack/apache2/htdocs/dnslog/logs/h/"> 
          a
</div></td>
      <td>
        <div align="left">
          <input type="checkbox" name="logform[]" value="/opt/coolstack/apache2/htdocs/dnslog/logs/b/"> 
          b
</div></td>
    </tr>
  </table>
  <br />
<input type="submit" value="search" name="submit">
</form>
</body>
</html> 
test.php
PHP Code:
<html>
<head>
<title>DNS Search</title>
<link href="css.css" rel="stylesheet" type="text/css">
</head>
<p class="big">DNS Log Search  </p>
<?php
$logform 
$_POST["logform"];
$search $_POST["search"];
/** 
 * FileSystemStringSearch 
 * Searches a file or directory of files for a search string 
 */ 
class FileSystemStringSearch 

    
//  MEMBERS 
 
    /** 
     * @var string $_searchPath path to file/directory to search 
     */ 
    
var $_searchPath
 
    
/** 
     * @var string $_searchString the string to search for 
     */ 
    
var $_searchString
 
    
/** 
     * @var array $_searchResults holds search result information 
     */ 
    
var $_searchResults
 
    
//  CONSTRUCTOR 
 
    /** 
     * Class constructor 
     * @param string $searchPath path to file or directory to search 
     * @param string $searchString string to search for 
     * @return void 
     */ 
    
function FileSystemStringSearch($searchPath$searchString
    { 
        
$this->_searchPath $searchPath
        
$this->_searchString $searchString
        
$this->_searchResults = array(); 
    } 
 
    
//  MANIPULATORS 
 
    /** 
     * Checks path is valid 
     * @return bool 
     */ 
    
function isValidPath() 
    { 
        if(
file_exists($this->_searchPath)) { 
            return 
true
        } else { 
            return 
false
        } 
    } 
 
    
/** 
     * Determines if path is a file or directory 
     * @return bool 
     */ 
    
function searchPathIsFile() 
    { 
        
// check for trailing slash 
        
if(substr($this->_searchPath, -11)=='/' || 
           
substr($this->_searchPath, -11)=='\\') { 
           return 
false
        } else { 
           return 
true
        } 
    } 
 
    
/** 
     * Searches given file for search term 
     * @param string $file the file path 
     * @return void 
     */ 
    
function searchFileForString($file
    { 
        
// open file to an array 
        
$fileLines file($file); 
 
        
// loop through lines and look for search term 
        
$lineNumber 1
        foreach(
$fileLines as $line) { 
            
$searchCount substr_count($line$this->_searchString); 
            if(
$searchCount 0) { 
                
// log result 
                
$this->addResult($file$line$lineNumber$searchCount); 
            } 
            
$lineNumber++; 
        } 
    } 
 
    
/** 
     * Adds result to the result array 
     * @param string $lineContents the line itself 
     * @param int $lineNumber the file line number 
     * @param int $searchCount the number of occurances of the search term 
     * @return void 
     */ 
    
function addResult($filePath$lineContents$lineNumber
$searchCount
    { 
        
$this->_searchResults[] = array('filePath' => $filePath
                                        
'lineContents' => $lineContents
                                        
'lineNumber' => $lineNumber
                                        
'searchCount' => $searchCount); 
    } 
 
    
/** 
     * Takes a given string (usually a line from search results) 
     * and highlights the search term 
     * @param string $string the string containing the search term(s) 
     * @return string 
     */ 
    
function highlightSearchTerm($string
    { 
        return 
str_replace($this->_searchString
                           
'<strong>'.$this->_searchString.'</strong>'
                           
$string); 
    } 
 
    
/** 
     * Recursively scan a folder and sub folders for search term 
     * @param string path to the directory to search 
     * @return void 
     */ 
    
function scanDirectoryForString($dir
    { 
        
$subDirs = array(); 
        
$dirFiles = array(); 
 
        
$dh opendir($dir); 
        while((
$node readdir($dh)) !== false) { 
            
// ignore . and .. nodes 
            
if(!($node=='.' || $node=='..')) { 
                if(
is_dir($dir.$node)) { 
                    
$subDirs[] = $dir.$node.'/'
                } else { 
                    
$dirFiles[] = $dir.$node
                } 
            } 
        } 
 
        
// loop through files and search for string 
        
foreach($dirFiles as $file) { 
            
$this->searchFileForString($file); 
        } 
 
        
// if there are sub directories, scan them 
        
if(count($subDirs) > 0) { 
            foreach(
$subDirs as $subDir) { 
                
$this->scanDirectoryForString($subDir); 
            } 
        } 
    } 
 
    
/** 
     * Run the search 
     * @return void 
     */ 
    
function run() 
    { 
        
// check path exists 
        
if($this->isValidPath()) { 
 
            if(
$this->searchPathIsFile()) { 
                
// run search on the file 
                
$this->searchFileForString($this->_searchPath); 
            } else { 
                
// scan directory contents for string 
                
$this->scanDirectoryForString($this->_searchPath); 
            } 
 
         } else { 
 
            die(
'FileSystemStringSearch Error: File/Directory does not 
exist'
); 
 
         } 
    } 
 
    
//  ACCESSORS 
    
function getResults() 
    { 
        return 
$this->_searchResults
    } 
 
    function 
getResultCount() 
    { 
        
$count 0
        foreach(
$this->_searchResults as $result) { 
            
$count += $result['searchCount']; 
        } 
        return 
$count
    } 
 
    function 
getSearchPath() 
    { 
        return 
$this->_searchPath
    } 
 
    function 
getSearchString() 
    { 
        return 
$this->_searchString
    } 
}  
$searcher = new 
FileSystemStringSearch("$logform""$search");
$searcher->run();
if(
$searcher->getResultCount() > 0) { 
    echo(
'<p>Searched for string 
<strong>"'
.$searcher->getSearchString().'":</strong></p>'); 
    echo(
'<p>Search term found <strong>'.$searcher->getResultCount().' times.</strong></p>');  
echo(
'<ul>'); 
     foreach(
$searcher->getResults() as $result) { 
echo(
'<li>'.$searcher->highlightSearchTerm($result['lineContents']).'</li>'); 
        }
echo(
'</ul>'); 
} else {
    echo(
'<p>Searched "'.$searcher->getSearchPath().'" for string 
<strong>"'
.$searcher->getSearchString().'":</strong></p>'); 
    echo(
'<p>No results returned</p>'); 
}  
?>
</html>
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

2. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

3. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies
Login or Register to Ask a Question