The WF PHP Functions War Chest

OK, here is something I cobbled together

An abstracts crawl, taking abstracts from Google and Bing and counting words used in it, plus a stop list.

Really, really shoddily thrown together, but should give you an idea of the space you are operating in.

Several queries can be put together at once, use the humble , as a seperator.

Have fun.

Code:
<HEAD>
<STYLE type="text/css">
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {margin:0;padding:0;border:0;font-weight:inherit;font-style:inherit;font-size:100%;font-family:inherit;vertical-align:baseline;}
body {font-size:75%;color:#333;background:#fff;font-family:"Helvetica Neue", Arial, Helvetica, sans-serif;}
h1, h2, h3, h4, h5, h6 {font-weight:normal;color:#444;}
h1 {font-size:3em;line-height:1;margin-bottom:0.5em;}
h2 {font-size:2em;margin-bottom:0.75em;}
h3 {font-size:1.5em;line-height:1;margin-bottom:1em;}
h4 {font-size:1.2em;line-height:1.25;margin-bottom:1.25em;}
h5 {font-size:1em;font-weight:bold;margin-bottom:1.5em;}
h6 {font-size:1em;font-weight:bold;}
h1 img, h2 img, h3 img, h4 img, h5 img, h6 img {margin:0;}
p {margin:0 0 1.5em;}

.container {width:750px; margin:0 auto;}
.column, div.span-1, div.span-2, div.span-3, div.span-4, div.span-5, div.span-6, div.span-7, div.span-8, div.span-9, div.span-10, div.span-11, div.span-12, div.span-13, div.span-14, div.span-15, div.span-16, div.span-17, div.span-18, div.span-19, div.span-20, div.span-21, div.span-22, div.span-23, div.span-24 {float:left;margin-right:10px;}
.last, div.last {margin-right:0;}
.span-1 {width:30px;}
.span-2 {width:70px;}
.span-3 {width:110px;}
.span-4 {width:150px;}
.span-5 {width:190px;}
.span-15 {width:590px;}
.span-16 {width:630px;}
.span-19 {width:750px;}

</STYLE>
</HEAD>
<BODY>
<div class="container">
<div class="column span-19 last">
    <h2>Abstracts Crawl</h2>
    <form method="GET" action="GoogleAbstracts01.php">
        <input type="textbox" name="queries"> Enter queries here, seperated by comma </input><br>
        <input type="submit" name="submit">
    </form>
</div>

<?php

if(empty($_GET['queries'])) {}
else 
    {
    echo ('<div class="column span-19 last">');
        //$GooglePrefix = "http://www.google.ch/search?hl=de&lr=lang_de&q=";
        
        $GooglePrefix = "http://www.google.com/search?q=";
        $GoogleCountSuffix ="&start=";
        $GoogleRegx = "/div.class=.s..(.*)\<cite>/U";
        
        $bingPrefix = "http://www.bing.com/search?q=";
        $BingCountSuffix = "&first=";
        $BingRegx = "/a>..h3>..div>.p>(.*).\/p>.div.class..sb.meta/U";
        $BingSuffix ="&setmkt=en-WW&setlang=match";
        
        $queries = explode(',', $_GET['queries']);
                //print_r($query);
        $resultat = '';
        for ($j = 0; $j < count($queries); $j++) 
            {
            
            echo "<h1>Google</h1>Suche nach: ".$queries[$j]."<br>";
            $resultat = $resultat.getAbstracts(urlencode($queries[$j]), $GooglePrefix, $GoogleCountSuffix, "", $GoogleRegx);
            SEoutput($resultat);
            $resultat ='';
            echo "<h1>Bing</h1>Suche nach: ".$queries[$j]."<br>";
            $resultat .= getAbstracts(urlencode($queries[$j]), $bingPrefix, $BingCountSuffix, $BingSuffix, $BingRegx);
            SEoutput($resultat);
            }
        echo ("</div>");    
    }

function SEoutput($resultat)
    {
        echo "<h3>Abstracts</h3></div>";
        //echo $resultat;
        
        $index = index_page(strip_tags($resultat));
        $stopwords = array("the", "this", "then", "a", "and", "i", "of", "or", "to", "on", "with", "is", "all", "for", "in", "you", "me", "an", "as", "are" ,"we", "be", "can", "your", "it", "do", "how", "that", "what", "will", "was", "he", "search", "may", "at", "from", "about", "any", "by", "has", "there", "no", "yes");
        echo ("<div class=column span-19 last>");
        echo "<h3>Word Counts</h3>";
        for ($i = 0; $i < count($index); $i++)
        {

            if ($index[$i]['count']>2 AND !(in_array($index[$i]['word'], $stopwords) ) )
            {
                echo ('<div class="column span-3">');
                echo "word: ".$index[$i]['word']."</div>";
                echo ('<div class="column span-15 last">');
                echo ('count: '.$index[$i]['count']."</div>");
            }
        }
        
    }
    

function getAbstracts($query, $Prefix, $CountSuffix, $Suffix, $regX )
    {
        $loop = 0;
        $result ='';
        $resultat = '';
        while ($loop<= 10)
        {
            $CompleteUrl = $Prefix.$query.$CountSuffix.$loop.$Suffix;    
            $crawl = curl_init();
            curl_setopt ($crawl, CURLOPT_URL, $CompleteUrl);
            curl_setopt($crawl, CURLOPT_RETURNTRANSFER, 1);
            $result = $result.curl_exec($crawl); 
            curl_close($crawl);
            $loop = $loop+10;
        }
        $regxResult = do_reg($result, $regX);
        for ($i = 1; $i < count($regxResult); $i++) 
            {
                $clean = strip_tags ($regxResult[$i]);
                $resultat=$resultat."<div class='column span-1'>".$i."</div><div class='column span-18 last'>".$clean."</div>";
            }
        return $resultat;
        }
                
function do_reg($text, $regex)
    {
        preg_match_all($regex, $text, $regxresult, PREG_PATTERN_ORDER);
        return $regresult = $regxresult[1];
    }

    

function index_page($file) {
    $index = array();
    $find = array(
        '/\r/',
        '/\n/',
        '/\s\s+/'
    );
    $replace = array(
        ' ',
        ' ',
        ' '
    );
    $work = $file;
    $work = preg_replace('/[>][<]/', '> <', $work);
    $work = strip_tags($work);
    $work = strtolower($work);
    $work = preg_replace($find, $replace, $work);
    $work = trim($work);
    $work = explode(' ', $work);
    natcasesort($work);
    $i = 0;
    foreach($work as $word) {
        $word = trim($word);
        $junk = preg_match('/[^a-zA-Z]/', $word);
        if($junk == 1) {
            $word = '';
        }
        if( (!empty($word)) && ($word != '') ) {
            if(!isset($index[$i]['word'])) { // if not set this is a new index
                $index[$i]['word'] = $word;
                $index[$i]['count'] = 1;
            } elseif( $index[$i]['word'] == $word ) {  // count repeats
                $index[$i]['count'] += 1;
            } else { // else this is a different word, increment $i and create an entry
                $i++;
                $index[$i]['word'] = $word;
                $index[$i]['count'] = 1;
            }
        }
    }
    unset($work);
    return($index);
}

?>
</BODY>
 


Simple function which removes those lines from the text file that contain the given keyword:

PHP:
<?php
function remove_lines($filename, $keyword) {
    file_put_contents ( $filename, preg_replace ( '/^.*?' . preg_quote ( $keyword, '/' ) . '.*\n?/m', '', file_get_contents ( $filename ) ) );
}
?>
Very compact and fast enough ;)
 
The point of this thread was to post code, not post links to other links that have code.

lol it is not like I'm linking to my blog... but here you go. FTP pasted, Spider attached.

Code:
<?php
try {
    $ftp = new Ftp($cfg['hostname'], $cfg['username'], $cfg['password']);
    $ftp->cd('public_html');
    $ftp->cd('fortesting');
    $ftp->upload('sketch.txt', 'sketchdos.txt');
    /*
        .... do FTP stuff here. If any of it throws an exception, it goes to the catch block
    */
    print_r($ftp->ls());
} catch (Exception $e) {
    echo $e->getMessage();
}
echo "\n";
print_r($ftp->log->getAllItems());
echo '<hr>Done.';

class Log {
    private $stack = array();
    public function __construct(Log $base_log = null) {
        if(!is_null($base_log))
            $this->stack = $base_log->getAllItems();
    }
    
    public function message($item) {
        $this->add($item, 'message');
    }
    
    public function error($item, $throw_exception = false) {
        $this->add($item, 'error');
        
        if($throw_exception)
            throw new Exception($this->getLastItem()->message);
    }
    
    private function add($item, $type = null) {
        if(!is_a($item, 'LogItem') && $type == null) 
            throw new Exception('$item passed to Log::add() must be LogItem or $type must be defined');
        if(!is_a($item, 'LogItem')) $item = new LogItem($item, $type);
        $this->stack[] = $item;
    }
    
    public function getLastItem() {
        return end($this->stack);
    }
    
    public function getAllItems() {
        return $this->stack;
    }
}

class LogItem {
    public $message, $type;
    public function __construct($message, $type = 'error') {
        $this->message = $message;
        $this->type = $type;
    }
    public function isType($type) {
        return ($this->type == $type);
    }
}

class Util {
    public static function parseConstant($constant) {
        // If $constant is not defined, we assume $constant is a string value to be parsed
        // otherwise, we parse the value of the constant with the name $constant
        if(!defined($constant))
            $constant_value = $constant;
        else 
            $constant_value = constant($constant);
        if(preg_match_all("/\{([A-za-z0-9]+)\}/i", $constant_value, $matches)) {
            foreach($matches[1] as $needle) {
                global ${$needle};
                $constant_value = str_replace('{' . $needle . '}', ${$needle}, $constant_value);
            }
        }
        return $constant_value;
    }
}

class Ftp {
    private $conn, $current_dir;
    public $log;
        
    public function __construct($host, $username, $password, $passive = false) {
        $this->log = new Log;
        
        $this->connect($host, $username, $password, $passive);
    }
    
    private function connect($host, $username, $password, $passive = false) {
        $this->conn = ftp_connect($host);
        $login = ftp_login($this->conn, $username, $password);
        ftp_pasv($this->conn, $passive);
        
        if(!$this->conn || !$login)
            $this->log->error('There was a login problem.', true);
        $this->log->message('Sucessfully connected.');
    }
    
    public function ls($dir = '.', $raw = false) {
        $ls = ($raw) ? ftp_rawlist($this->conn, $dir) : ftp_nlist($this->conn, $dir);
        if(!$ls)
            $this->log->error("Could not list contents of $dir", true);
        return $ls;
    }
    
    public function isDir($dir) {
        $original_dir = ftp_pwd($this->conn);
        $result = @ftp_chdir($this->conn, $dir);
        if($result) {
            ftp_chdir($this->conn, $original_dir);
            return true;
        }    
        return false;
    }
        
    public function isFile($file) {
        return in_array($file, $this->ls());
    }
    
    public function chmod($path, $mode_dec) {
        $mode_oct = octdec(str_pad($mode_dec, 4, '0' ,STR_PAD_LEFT));
        $result = ftp_chmod($this->conn, $mode_oct, $path);
        if(!$result)
            $this->log->error("Error changing permissions for $path to $mode_dec", true);
        $this->log->message("Permissions for $path are now $mode_dec");
    }
        
    public function cd($dir) {
        $result = ftp_chdir($this->conn, $dir);
        if(!$result)
            $this->log->error("Could not change directory to $dir", true);
        $this->log->message('Current directory is now' . ftp_pwd($this->conn));
    }
    
    public function mkdir($dir, $mode_dec = null) {
        $current_dir = ftp_pwd($this->conn);
        $result = ftp_mkdir($this->conn, $dir);
        if(!$result)
            $this->log->error("Could not make directory $dir in " . ftp_pwd($this->conn), true);
        $this->log->message("Made directory $dir in " . ftp_pwd($this->conn));
        if(!is_null($mode_dec))
            $this->chmod($dir, $mode_dec);
    }
    
    public function rmdir($dir) {
        if($dir == '.' || $dir == '..')
            return false; // quietly return
        $result = ftp_rmdir($this->conn, $dir);
        if(!$result)
            $this->log->error("Could not remove directory $dir - you should ensure that it is empty.", true);
        $this->log->message("Removed directory $dir");
    }
    
    public function delete($path) {
        $result = ftp_delete($this->conn, $path);
        if(!$result)
            $this->log->error("Could not delete file $path", true);
        $this->log->message("Deleted file $path");
    }
    
    public function upload($from_path, $to_path, $mode = null) {
        if($mode != 'ascii' || $mode != 'binary' || $mode != FTP_ASCII || $mode != FTP_BINARY)
            $mode = self::determineUploadMode($from_path);
        else if(!defined($mode))
            $mode = constant('FTP_' . strtoupper($mode));
        
        $result = ftp_put($this->conn, $to_path, $from_path, $mode);
        
        if(!$result)
            $this->log->error("Error uploading $from_path to $to_path", true);
        $this->log->message("Successfully uploaded $from_path to $to_path");            
    }
    
    public function download($from_path, $to_path) {
        $mode = self::determineUploadMode($to_path);
        $result = ftp_get($this->conn, $to_path, $from_path, $mode, 0);
        if(!$result)
            $this->log->error('Error downloading $from_path to $to_path');
        $this->log->message('Successfully downloaded $from_path to $to_path');
    }
    
    public static function determineUploadMode($path) {
        $ascii_always = array('.htm', '.html', '.shtml', '.php', '.pl', '.cgi', '.js', '.py', '.cnf', '.css', '.forward', '.htaccess', '.map', '.pwd', '.txt', '.grp', '.ctl');
        $extension = array_pop(explode('.', $from_path));
        if(in_array($extension, $ascii_always))
            return FTP_ASCII;
        return FTP_BINARY;
    }
    
    public function __destruct() {
        if($this->conn) ftp_close($this->conn);
    }    
    
}

?>
 

Attachments

  • Spider.zip
    10.5 KB · Views: 7
I'm starting to suck less at coding, so I'll contribute a few functions I wrote recently to check out the competition. They're incredibly simple, but you might find them useful.
1.jpg

2.jpg

6.jpg

GO ON...
 
I'm starting to suck less at coding, so I'll contribute a few functions I wrote recently to check out the competition. They're incredibly simple, but you might find them useful.
jh2.jpg

2.jpg
 
I'm starting to suck less at coding, so I'll contribute a few functions I wrote recently to check out the competition. They're incredibly simple, but you might find them useful.
huang4.jpg

huang3.jpg
 
Cpanel Uploader

Creates a subdomain, uploads a zipfile and unzips the file before cleaning up everything. This one is made for OpenBH but can be customized for anything.

Make sure to get the xmlapi from https://github.com/CpanelInc/xmlapi-php as well.

More explanations on my blog if needed hehejo » Blog Archive » OpenBH Installer Part 1

Code:
<?php  
/*
    OpenBH Cpanel Installer
    by hehejo
    14.11.2011
*/  
$username      = 'PWUSERNAME'; 
$password      = 'CPPASSWORD'; 
$server        = 'CPDOMAIN'; 
$domain        = 'ADDONDOMAIN';  

$ftplogin      = 'installerftp@' . $server;
$ftppassword   = 'abcd1234';

$subdomain = 'subdomain';

set_time_limit(300);

/*
   Load xmlapi
   xmlapi settings
*/
require_once('xmlapi.php');
$xmlapi = new xmlapi($server);
$xmlapi->set_port(2082);
$xmlapi->password_auth($username,$password);
$xmlapi->set_debug(1);
$xmlapi->set_output('xml');

/*
   Create FTP account for file uploads
*/
$args = array( 
  'user'=>$ftplogin,
  'pass'=>$ftppassword,
  'homedir'=>'/public_html/',
  'quota'=>'unlimited'
);
$res = $xmlapi->api2_query($username, 'Ftp', 'addftp', $args);

/*
   Open FTP connection
*/
$ftp = ftp_connect('ftp.' . $server);
$login_result = ftp_login($ftp, $ftplogin, $ftppassword);  

/*
   Create Subdomain
*/
$args = array($subdomain,$domain,0,0,'/public_html/' . $subdomain . '.' . $domain);
$res = $xmlapi->api1_query($username,'SubDomain','addsubdomain', $args);

echo $res;

/*
   Upload openbh.zip to subdomain folder
*/

$upload = ftp_put($ftp,$subdomain . '.' . $domain . '/openbh.zip', 'openbh.zip', FTP_BINARY);


/*
   Unzip openbh.zip into subdomain folder
*/
$args = array('/public_html/' . $subdomain . '.' . $domain,'openbh.zip');
$res = $xmlapi->api1_query($username, 'Fileman', 'extractfile', $args);  

echo $res;


// Extract file <cpanel Fileman="extractfile($FORM{'dir'},$FORM{'file'})">


/*
   Delete openbh.zip
   Close FTP connection
*/
ftp_delete($ftp,$subdomain . '.' . $domain . '/openbh.zip'); 
ftp_close($ftp);

/*
   Delete installer ftp account
*/
$args = array( 
  'user'=>$ftplogin,
);
$res = $xmlapi->api2_query($username, 'Ftp', 'delftp', $args);
 
  • Like
Reactions: clenard
Code:
<?php
  /**
   * Simple PHP Daemon
   * daemon.php
   *
   * @author peakinformatik.com
   *
   */


$config = array(
    'password'=>'bananas',                     // Password required to control daemon
    'php'=>'/Applications/MAMP/bin/php/php5.3.6/bin/php',     // PHP location
    'sleep'=>1,                         // Sleep between loops
    'daemonfile'=>'daemonfile',                // File that controls the daemon
    'arg'=>'startdaemon',                     // Argument to check if script is run from commandline
);


if(!isset($argv[1])){
    $argv = false;
}


if ($argv[1] == $config['arg']){
    // Script is run from commandline
    // Start daemon


    $dir = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/'));


    while(file_exists($dir . '/' . $config['daemonfile'])){


        // ***** START OF CODE *****


        touch($dir.'/test'); // replace with your own code


        // *****  END OF CODE  *****


        sleep($config['sleep']);
        clearstatcache();
    }
    exit();


}else{


    // Script is run from browser
    // Start control panel


    if (isset($_GET['password'])){
        if ($_GET['password'] == $config['password']){


            if (isset($_GET['action'])){
                switch ($_GET['action']){
                    case 'start':
                        touch($config['daemonfile']);
                        exec($config['php'] . ' -f ' . $_SERVER['SCRIPT_FILENAME'] . ' ' 
                           . $config['arg'] . '> /dev/null &');
                    break;


                    case 'stop':
                        if(file_exists($config['daemonfile'])){
                            unlink($config['daemonfile']);                        
                        }
                    break;
                }
            }


            echo '<h2>Simple PHP Daemon</h2>'.PHP_EOL;
            echo '<p><b>Status:</b> ';
            if(file_exists($config['daemonfile'])){
                echo 'running';
            }else{
                echo 'stopped';
            }
            echo '</p>'.PHP_EOL;
            echo '<p>Use GET parameter <b>action</b> to control daemon</p>' . PHP_EOL;
            echo '<ul>' . PHP_EOL;
            echo ' <li><a href="' . $_SERVER['PHP_SELF'] . '?password=' . $_GET['password'] 
               . '&action=start"><b>start</b></a> - start daemon</li>' . PHP_EOL;
            echo ' <li><a href="' . $_SERVER['PHP_SELF'] . '?password='.$_GET['password']
               . '&action=stop"><b>stop</b></a> - shut down daemon</li>' . PHP_EOL;
            echo '</ul>'.  PHP_EOL;                                


        }else{
            echo '<h2>Simple PHP Daemon</h2>' . PHP_EOL;
            echo '<p>Enter password with GET parameter to gain access</p>' . PHP_EOL;
            echo '<p>Example: daemon.php?password=<i>yourpassword</i></p>' . PHP_EOL;
        }        
    }    
}
https://github.com/peakinformatik/Simple-PHP-Daemon
 
great work yar

Thanks a lot . great thiing u did

OK.. I changed the scraper script a bit to circumvent an error that get_file_content throws when you get 0 results.

So here are the changes:
- introduced function getHttp which sends a curl to an url
- replaced get_file_content with the new function
- replaced the curl in the lower script with a call to the new function
- added a table to the output
- integrated the check to include the output (no more empty lines)

Here ya go:
Code:
<?php
function text_between($start,$end,$string) {
  if ($start != '') {$temp = explode($start,$string,2);} else {$temp = array('',$string);}
  $temp = explode($end,$temp[1],2);
  return $temp[0];
}

function getHttp($url)
    { 
        $userAgent = 'Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
        
        // make the cURL request to $target_url
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        $html= curl_exec($ch);
        if (!$html) 
        {
            echo "<br />cURL error number:" .curl_errno($ch);
            echo "<br />cURL error:" . curl_error($ch);
            exit;
        }
        return $html;
    }

function gsscrape($keyword) {
  $keyword=str_replace(" ","+",$keyword);
  global $kw;
    $url='http://clients1.google.com/complete/search?hl=en&q='.$keyword;
    $data = getHttp($url);
    $data=explode('[',$data,3);
    $data=explode('],[',$data[2]);
    foreach($data as $temp) {
        $kw[]= text_between('"','"',$temp);
    }
}

#simple to use, just use yourscriptname.php?keywords
if ($_SERVER['QUERY_STRING']!='') {
  gsscrape($_SERVER['QUERY_STRING']);
  foreach ($kw as $keyword) {
  gsscrape($keyword);
  }
}
    echo"<table>";
#all results are in array $kw...
foreach($kw as $keyword) {

    if ($keyword !='')
    { 
        $url = 'http://www.google.com/search?q='.urlencode($keyword);
        $html=getHttp($url);
        $abc = str_replace('of about <b>', '', strstr($html, 'of about <b>'));
        echo '<tr><td><a href="'.$url.'">'.$keyword.'</a></td><td>'.substr($abc, 0, strpos($abc, '</b>')).' Results<br /></td></tr>';
    }
}
echo'</table>';
?>

::emp::
 
I don't know how useful this will be, but I'm just learning PHP. I built off of Bofu's sentence shuffler from a couple years back, with a little form submit so you can keep pressing 'Shuffle' until you get the configuration you like:

The PHP:

PHP:
<?php

if(isset($_POST['submit'])) {

	// get the data from the form
	$article = $_POST['article'];
	
	// create array of delimiters
	$delimiters = array('.','?','!');
	$new_delimiters = array('.%DELIM%','?%DELIM%','!%DELIM%');
	
	// replace all delimiter types with a uniform delimiter
	$step1 = str_replace($delimiters, $new_delimiters, $article);
	$sentences = explode('%DELIM%' , $step1);
	
	// shuffle it up
	shuffle($sentences);
	
	// echo out the shuffled sentences, with any slashes stripped out
	foreach($sentences as $element) {
	echo stripcslashes($element) . " ";
	}
}
?>

Snippet to display the submitted article in the form again (for easy reshuffling)

PHP:
<?php if(isset($_POST['submit']))
	{
		$repaste = $_POST['article'];
		echo stripcslashes($repaste);
	}
	else {
		echo "Paste article here...";
	}
?>
 
Last edited:
How do you guys do your benchmarks or test how efficient your script is? is there a function/class that I can just include and it can output at the end of the file the stats on how the script ran?