a nice article, must read
http://www.softwarebyrob.com/articles/Nine_Things_Developers_Want_More_Than_Money.aspx
| ► | آذار 2009 | ◄ | ||||
| سبت | أحد | إثنين | ثلاثاء | أربعاء | خميس | جمعة |
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | |||

a nice article, must read
http://www.softwarebyrob.com/articles/Nine_Things_Developers_Want_More_Than_Money.aspx
Sometimes while you are coding, you need some kind of a wrap for some primitive data type such as an integer, just to add some feature that is not available directly from that primitive data type, or just to give it some identity.
A Date, Integer, or Dollar class is a handy - and inexpensive - encapsulation, easily copied, compared, or created when needed.
For example managing Bytes and printing them can be something annoying, for example, when ever you need to print the number of Bytes in a file, you would like to check if it can be represented in KB, MB, or GB.
So what I’m going to do here is to wrap the integer datatype into a Byte class, and override it’s __toString function (one of the magic functions in PHP 5) so every time time I need to print it a simple print would do the issue.
Our example here would be for a mailbox, every mailbox has a quota, I want to give it a starting quota, then increase it, so first I’m going to write the Test for it (Using simpletest).
<?php
// …. includes and stuff
class TestByte extends UnitTestCase {
function testQuotaByte () {
$this->UnitTestCase("Test Byte Value");
$startingQuota = new Byte(1048576); // One Mega
$mailbox1 = new MailBox($startingQuota);
$mailbox2 = new MailBox($startingQuota);
$this->assertEqual(1048576,$mailbox1->getQuota()->getBytes());
$this->assertEqual(1048576,$mailbox2->getQuota()->getBytes());
$mailbox1->increaseQuotaBy(new Byte(524288)); // add 512KB
$this->assertEqual(1572864,$mailbox1->getQuota()->getBytes());
$this->assertEqual(1048576,$mailbox2->getQuota()->getBytes());
$this->assertEqual(1048576,$startingQuota->getBytes());
}
}
$test = new TestByte();
$test->run(new HtmlReporter());
?>
Here I created to Mailboxes, and gave them a starting quota of 1 MB, after that I added 512 KB to one of them, and checked that it was added, and that the other mailbox and starting Quota was not changed.
Let’s start with the implementation:
<?php
class Byte {
protected $amount;
protected static $Names = array("B" => "B",
‘KB’ => ‘KB’,
‘MB’ => ‘MB’,
‘GB’ => ‘GB’,
‘TB’ => ‘TB’);
function __construct($bytes) {
$this->amount = $bytes;
}
public function add(Byte $bytes) {
$this->amount += $bytes->getBytes();
}
public function getBytes() {
return $this->amount;
}
public function getKB () {
return $this->amount/1024;
}
public function getMB () {
return $this->amount/1048576;
}
public function getGB() {
return $this->amount/1073741824;
}
public function getTB() {
return $this->amount/1099511627776;
}
public funcالمزيد
Perhaps no other coding practice is as important as testing your code. Also in the nature of Business Development, where parts of your code always change on the request of a client (including Management), or even when you want to make your code run with better performance, Automated tests are highly needed, you can’t just spread your print statements all over your code every time you need to test it.
With automated tests, you can just be sure that your interface is not broken after some change, you just run your tests, if they succeed, you know that you didn’t break your code (this means you should right good tests).
Let’s start by a simple example, Imagine that we have been asked to test PHP’s built-in Array. One bit of functionality to test is the function sizeof(). For a newly created array we expect the sizeof() function to return 0. After we add an element, sizeof() should return 1. (I know it’s not a big deal, but it’s just an example).
you can do it with simple print statements (i.e. print (1 == sizeof($myArray) ? "OK":"Failure").PHP_EOL; ), but here we are going to do it by using an assertion function.
<?php
$fixture = Array();
assertTrue(sizeof($fixture) == 0);
$fixture[] = "element";
assertTrue(sizeof($fixture) == 1);
function assertTrue($condition) {
if (!$condition) {
throw new Exception("Assertion failed.");
}
}
?>
Well, we could know if something went wrong if some Exception was raised, of course assertTrue is not the only thing you want to do, UnitTesting has gone far beyond that, usually UnitTesting frameworks provide a whole lot of features that you need for your testing.
Lot’s of Testing frameworks are available for PHP, I’m going to point to 2 of them, the first one is SimpleTest (http://simpletest.org/), it’s written in PHP 4 (Although they are going to migrate to PHP 5 when they reach version 2), so you can use it on both PHP 4 and 5 programs, also it includes a nice HTML reporter for some good looking html results the same test above could be written using this framework as:
<?php
require_once ’simpletest/unit_tester.php’;
require_once ’simpletest/reporter.php’;
class TestingFixtureArray extends UnitTestCase {
function TestArray() {
$this->UnitTestCase("Testing Fixture Array");
$fixture = Array();
$this->assertTrue(sizeof($fixture) == 0);
$fixture[] = "element";
$this->assertTrue(sizeof($fixture) == 1);
}
}
//run the Test
$test = new TestingFixtureArray();
$test->run(new HtmlReporter());
?>
Another Well know testing framework is knoالمزيد
"Every single server has it [APC] at Yahoo, and it handles billions of requests per day" Said Rasmus Lerdorf, the creator of PHP, at the php|works conference.
Parsing and compiling speed can be significantly boosted with the use of an opcode cache.
In PHP, as with most scripting languages, code is parsed from human-readable to machine-readable instruction. The machine-readable script is known in PHP as opcodes.
An opcode cache stores or caches the compiled code in shared memory so that the code compilation for similar operations only needs to happen once.
PHP 6, which is still in development, will have opcode cache built in by default.
For current PHP 5 users, there are various opcode cache implementations that can be used, including the Alternative PHP Cache (APC), which is what Lerdorf recommended.
Installation (Linالمزيد
Many people say uploading files with
Granted this solution only works with FireFox/Mozilla. And the user has to change a setting in "about:config" and accept the advanced access privileges.
Anyway, such an
FireFox/Mozilla settings:
Open about:config and check that
signed.applets.codebase_principal_support
is set to "true"
Otherwise Firefox will display something like this
Error: uncaught exception: A script from "http://www.captain.at"
was denied UniversalXPConnect privileges.
Also make sure you check the checkbox "Remember this decision", when FireFox will display this message
A script from "http://www.captain.at" is requesting enhanced abilities that are
UNSAFE and could be used to compromise your machine or data:
Run or install software on your machine
Allow these abilities only if you trust this source to be free of viruses or malicious
programs.
[ ] Remember this decision
and click "Allow", otherwise you have to click "Allow" everytime you upload a file.
The example itself is rather straightforward:
We use some Components.classes and Components.interfaces stuff to open the local file from within FireFox/Mozilla - we read the file, construct our request body for the POST request and send the whole data with an
NOTE about encoding the local files:
Since we also want to upload binary files, we need to encode (javascript "escape") the file content. This is basically encoding a string for use in an URL. On the server, after uploading the file we need to decode ("urldecode") the file. "escape" does not encode the plus sign "+", but on the server PHP’s "urldecode" interprets any "+" and space. So we need an additional preg_replace to replace any "+" to the HEX value "%2B".
This is a little annoying, since escaping large files (up to 1MB it is still fast) with javascript can hang the browser for a few seconds. The problem here is that the
ADVANTAGES:
If you upload images and process them on the server, it is common that the server stops the script due too much memory consumption and/or the runtime limit has been exceeded. In such a case PHP is just returning an error message ("Fatal error: memory limit exceeded" or "Fatal error: running too long" or whatever) and the user usually has to back up with the browser back button to repeat the procedure with a smaller image. With
A possible extension to this example would be:
Let the user select a directory with a custom "directory-browser" or one file in a directory with the regular file-dialog as shown here, then parse the directory automatically for file with a certain extension and upload them in a bulk.
index.html
<html>
<body>
>
var url = "post.php";
var binary;
var filename;
var mytext;
function upload() {
filename = document.getElementById('myfile').value;
mytext = document.getElementById('mytext').value;
document.getElementById('ajaxbutton').disabled = true;
// request local file read permission
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("Permission to read file was denied.");
}
// open the local file
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( filename );
stream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
stream.init(file, 0x01, 00004, null);
var bstream = Components.classes["@mozilla.or
Well I guess MySpace.com is the worst #1
Whitehouse.com #13
Microsoft Windows Update #16
Hotmail #20
more details here
Software Freedom Day (SFD) is a worldwide celebration of Free and Open Source Software (FOSS). Our goal in this celebration is to educate the worldwide public about of the benefits of using high quality FOSS in education, in government, at home, and in business — in short, everywhere! The no
if you use firefox you can think of adding this addon to your search engines
download here
Him - PHP is Insecure.
Me - What makes you say so?
Him - I read it in a book.
Me - And what have you read exactly?
Him - I can’t remember, I’ll send you the book.
and he did actually send me the book, I read it, it was about 70 pages, actually I guess it was first an e-zine or something from some hackers website or mailing list, what ever. after that I saw the guy.
Me - Did you read the book, or just skimmed through.
Him - No, I read it.
Me - Do you know anything about PHP.
Him - Yeah, I wrote some scripts.
Me - OK, first of all the book does not talk about any PHP vulnerability, and the examples he gave for his hacks were just a stupid code, and if anyone working in this industry writes code like that, he should have a capital punishment.
Him - what, no no it just that PHP is not secure.
Me - ok here is the book, tell me where does it say that PHP is not secure.
Him - in the first chapter he says about a vulnerability in the include function.
Me - Yeah I noticed that, first of all include is not a function it’s an operator, second thing the code says something like
<?php
include ($page);
?>
and he says that when register_globals is turneالمزيد
While surfing the Internet last night, I reached some sites talking about a free (as in freedom) clone of MS Windows called ReactOS, ReactOS is an effort to create a Free Software replacement for Microsoft Windows(TM) that is compatible with existing hardware and software!
A good review for the product can be found on The NeoSmart Files here.
Well, it’s still in an alpha version, but they seem to have a lot of good work done.
Now to my point of view, for GNU/Linux which is baالمزيد










