Friday, December 5, 2008

News from behind

By thomasw at 19:08:54

Hy fellows,

as you may have noted there has been no update since several weeks.
This is not because I did nothing. Much has been done in background.

Therefor I will give you some information about what I am actually working on and what new features will be available in the near future.

Encryption/Decryption
I’ve been working on a encryption/decryption filter for files and for strings. It’s already finished and can be found on incubator. Actually it awaits the acceptance of the devteam for core integration.

It’s based on PHP’s mcrypt extension which supports more than 20 different encodings.

WordCount validator
I made a new validator which counts words within a textfile. The WordCount validator can also be found within the incubator and awaits review of the devteam.

Zend_Date
I started the rework of Zend_Date for ZF2.0. I planned to shrink the code, and make some internal improvements. The benefit will be a performance improvement. But it will be for 2.0 and not before.
Until now I’ve not decided if we change from mktime to the new date extension. There is no real performance benefit, so I have to do some research before I can decide.

More to come soon, I’ll keep you informed.

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Saturday, November 1, 2008

File Transfer, hashing and other news

By thomasw at 20:45:57

Hy interested ones,

the last days I added hashing functionality to Zend_File_Transfer.
The getHash() method will return you the hash value of uploaded files.
It accepts up to 34 different hash algorithms.

$upload->getHash('uploadform');

Additionally I added a hash validator (Zend_Validate_File_Hash) which can be used to validate for the hash of a filecontent. For convinience Crc32, Md5 and Sha1 are also available.

$upload->addValidator('Hash', false, '3b512d52', 'crc32');

Beside that as reminder for you:
When you want to check if a file has been uploaded you can use isUploaded instead of checking the filename.

if ($upload->isUploaded('uploadform')) {...}

Some of you have also mentioned that they have not seen a complet working example with files osing Zend_Form. Well, I’m no forms expert, but here is an example which can be helpfull to see if the problem is your code, or your environment. This one works when you save it in a file and call it from your browser when your environment is configured correct:

ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.'../library');
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();
	
$request = new Zend_Controller_Request_Http();
	
// setup the form
$form = new Zend_Form();
$form->setMethod("post");
$form->setAttrib("enctype",Zend_Form::ENCTYPE_MULTIPART);
	
// file element, upload is optional, but if file is uploaded run multiple validators
$file1 = $form->createElement("file","file1");
$file1->setRequired(false)
       ->setLabel("File:")
       ->addValidator('Count', true, 2)     
       ->addValidator('Size', true, "100KB")
       ->addValidator('Extension', true, 'jpg')
       ->addValidator('MimeType',true,array('image/jpeg'))
       ->addValidator('ImageSize',true,array(0,0,340,480))
       ->setMultiFile(3);
	
// add another file element with same validators      
$file2 = clone $file1;
$file2->setName("file2")
       ->setMultiFile(0);
	
$submit = $form->createElement("submit","submit");
$submit->setLabel("GO!");
	
$form->addElements(array($file1,$file2,$submit));
	
// check the form
if($request->isPost()) {
    $formData = $request->getPost();
    if($form->isValid($formData)) {
        echo "FORM VALID";
    } else {
        print_r($form->getMessages());                              
    }
}
?>
<html>
<head>
<title>File Test</title>
</head>
<body>
<?php echo $form->render(new Zend_View());?>
</body>
</html>

After the validation was successfull you will only have to receive the files.
You can do this eighter by calling it on the adapter class

if (!$form->getTransferAdapter()->receive()) {
    print "There was a failure at receiving the files";
}

or by calling it on the file element

if (!$form->file1->receive()) {
    print "There was a failure at receiving file1";
}

but the second will only receive this single file element… so you would have to call it on every file element you want to receive.

Additionally I was told by the devteam to add the getValue() method which has also to validate and receive files. So if you call this method all of your files will be validated and received in one step… additionally you will get the filenames returned, but without the file path because of security reasons. If you need the complete path simply call getFileName().

Attention:
Due to this change, which is in my eyes problematic, you should not call isValid() afterwards. The reson is that after you received the files, by getValues(), they are no longer available. So your validation will fail. You can get around this problem when you check if the file has already been received.

if (!$form->getTransferAdapter()->isReceived()) {
    if (!$form->getTransferAdapter()->receive()) {
        print "There was a failure while receiving the files";
    }
}

Beware that getValues() is called by some methods of Zend_Form or their elements automatically.

Anyway, I hope you find this additional functions usefull. Feel free to test it and report problems on it to our issuetracker.

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Saturday, October 18, 2008

I18N compatibility mode

By thomasw at 23:03:34

Hy interested ones,

to prevent problems when you want to change from Zend Framework 1.6 to 1.7 I added a so called “compatibility mode”.

It’s simply a static variable which changes between old API and new API behaviour.
Within release 1.7 the old behaviour is per default active (true).
So your old code will still work with this settings but it will throw a user warning.

You can change this mode by simply setting it to false in your bootstrap.

Zend_Locale::$compatibilityMode = false;

This will no longer return user warnings, but could be in a position where you have to change your code to work again. See the migration chapter of Zend_Locale for details.

The changes include:
* changes on isLocale which now only returns boolean
* changes on getDefault which returns only the framework default
* changes on calling information methods which were made static
* changes on returned locales which do no longer degrade to other types

In 75% of the cases you will not have to change anything. But you should properly check this.

With the next release 1.8 we will change the compatibility mode to false per default, and with 1.9 we will delete it completly so only the new API behaviour is active.

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Sunday, October 5, 2008

Migrating your Script using Zend_File_Transfer from 1.6.1 to a newer release

By thomasw at 13:01:16

Hy interested ones,

most of you know this already. But it seems that users tend not to read migration notes so I will say it once more here.

When you are using Zend_File_Transfer in conjunction with validations you will have to note that the API has changed.

I added a $breakChainOnFailure parameter which allows to break the validation chain for further validators if a validation error occurs.

To give you an example:

// Example for 1.6.1 or earlier
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('FilesSize', array('1B', '100kB'));
// Same example for 1.6.2 and newer
// Note the added boolean false
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('FilesSize', false, array('1B', '100kB'));

So as conclusio for you:
If you have to migrate old scripts simply add a boolean false after the validator name.

Greetings
Thomas Weidner, I18N Team Leader, Zend Framework

Thursday, September 25, 2008

Subforms and multiple file elements

By thomasw at 20:29:25

Hy interested ones,

when you are using Zend_Form for your file upload I have new informations for you.

*) Subforms
Using file elements in sub forms was not possible in the past. The reason was that sub forms are created by using multidimensional names, but in HTML multidimensional names are not supported for file elements.

Now this is possible. I have added a new decorator with the help of Matthew which is automatically be used for file elements. It changes the elements name in a way which is supported by HTML.

As you can see in the following example, even multi-nested subforms are supported.

$form = new Zend_Form();
$form->setEnctype('multipart/form-data');
$form->setAction('index2.php');
	
// Button
$button = new Zend_Form_Element_Submit('submit');
$form->addElement($button);
	
// File Upload Elements
$element = new Zend_Form_Element_File('firstfile');
$element2 = new Zend_Form_Element_File('secondfile');
	
// 3-Level subform
$subform0 = new Zend_Form_SubForm();
$subform0->addElement($element);
$subform0->addElement($element2);
$subform1 = new Zend_Form_SubForm();
$subform1->addSubform($subform0, 'subform0');
$subform2 = new Zend_Form_SubForm();
$subform2->addSubform($subform1, 'subform1');
$subform3 = new Zend_Form_SubForm();
$subform3->addSubform($subform2, 'subform2');
$form->addSubform($subform3, 'subform3');
	
$form->setView(new Zend_View());
echo $form;

But note that the name of the file elements you set in a form and all it’s subforms must be unique. File elements with the same name are displayed but not submitted by your form.

*) Multiple file elements

Sometimes it is usefull to have multiple file elements which uses the same settings and validators. Writing plain HTML you would simply set a name like filename[] several times. Zend_Form_Element_File can handle this for you much simpler. Just create your element like before and call setMultiFile() with the number of files you want to have.

$element = new Zend_Form_Element_File("uploaded");
$element->setMultiFile(3);

The above example will create 3 seperated file elements named uploaded[].

I hope you find these two features handy.
New things will come soon.

Greetings
Thomas, I18N Team Leader, Zend Framework

Sunday, September 21, 2008

Zend_File_Transfer and error messages

By thomasw at 15:57:09

Hy interested ones,

in the last days I worked on Zend_File_Transfer to get this new component more comfortable.
I integrated several new features which will allow simpler usage specially when you use the file element helper.

So what is new:

*) breakChainOnFailure

Like it’s cousine Zend_Form, now also Zend_File_Transfer allows the usage of the breakChainOnFailure parameter when adding validators. This feature will stop the validation for defined validators.

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB')
            ->addValidator('Extension', false, 'jpg,png');

This example will stop validation as soon as a file extends 1MB regardless of it’s extension. So you will not get the error for false extension when the file is for example 1.5MB.

*) Translating error messages

You can now add a translator, which will translate all error messages for you. This feature works the same way like in Zend_Form. You can set a translation with setTranslator().

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB')
            ->setTranslator($mytranslator);

When you now get an error when validation occurs, all returned error messages will be routed through the translator and when you have set a translation you will get your error instead of the adapters one.
So instead of “file not found” you could write “Hey man, give me my file”.

*) Knowing if a error has been occured

I added the hasErrors() method to the adapter which will return true when an error has occured during validation.

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB');
	
if ($adapter->receive()) {
    $errors = 'unknown error';
    if ($adapter->hasErrors()) {
        $errors = $adapter->getErrors();
    }
}

The example does not really need the hasErrors() method as you may mention. But sometimes there is a need of this functionality, so I added it. Also the hasErrors() method of Zend_Form_Element_File routes to the adapters hasErrors() method.

*) Seperated validation from receiving of files

The originate implementation of Zend_Form_Element_File called receive() when the element was validated.
Of course this is not true and leaded to several problems, like the file being transfered even if other form validations failed.

This has been changed by me, but does also lead to a BC break, as you have to call receive() after validation.
But this does not mean that you have to call 2 methods.

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB');
	
if (!$adapter->isValid()) {
    print "Validation error";
}
if (!$adapter->receive()) {
    print "Problem receiving the file";
}

When the adapter has been validated, then the receive() method will not call isValid() again.
But when you have not validated the adapter, it will automatically call isValid() before the file will be transfered to it’s destination.

So you could simply write

$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Size', true, '1MB');
	
if (!$adapter->receive()) {
    print "Problem receiving the file, probably validation problem";
}

*) Checking of the destination exists

Previously you could set anything as destination path and would get an false as soon as you receive the file but you would not know why as the validation would have passed. So I added a check for existance of the destination path. You will now get an exception if the directory you set does not exist, or is not readable.

$adapter = new Zend_File_Transfer_Adapter_Http();
try {
    $adapter->setDestination('/path/does/not/exist');
} catch (Zend_File_Transfer_Exception $e) {
    print $e->getMessage();
}

More to come soon…

Greetings
Thomas
I18N Team Leader, Zend Framework

Sunday, September 7, 2008

Destination and renaming of uploaded files

By thomasw at 23:45:09

Hy insterested ones,

I added for you two new improvements to Zend_File_Transfer or if you work with Forms, to the file element.

The first one is that Zend_File_Transfer allows now the usage of file filters. Note that, of course, file filters work differently to normal string filters. They do not return the content, but the location of the file, aka the real location and filename. It should also be noted that file filters will be attached AFTER the file has been received. This is due to the fact that we are not allowed to change the file before it has been received, as we would then get a attacker exception from PHP itself. Input elements, for example, will do filtering before validation. File elements will always do validation and afterwards filtering. So you could now add your own filters if you are in need. Or you wait for me to do this.

The second improvement is the first file filter.

I am happy to announce that the “Rename” filter is ready in core.
What can it do ?

Well, it will allow you to have much more influence of the behaviour of uploaded files.
Let’s see an example:

$filter = new Zend_Filter_File_Rename('C:\pictures\newpics', true);

or when you use the adapter

$adapter->addFilter('Rename', array('C:\pictures\newpics', true));

All uploads will now be moved to the directory C:\pictures\newpics and will be overwritten if they exist.
The default is false, which means that the filter will not overwrite existing files. Beware: If the file exists, you will get a false as soon as you receive the file with an error, that the file already exists if you do not set the overwrite property.

Of course the rename adapter allows much more.
You can define the location for each single file separatly.
You can even define a new filename and extension for the uploaded file.

$adapter->addFilter('Rename', 'C:\pictures\newname.txt');

For more details about supported notations look into the manual.

I hope you find this addition usefull and use it in your own projects.
PS: If you have a good idea for a additional feature to any of my components dont be shy and share your idea.

Greetings
Thomas, I18N Team Leader, Zend Framework

Zend_File_Transfer and isRequired

By thomasw at 00:41:58

Hy fellows,

some of you have mentioned that Zend_Form_Element_File does not properly handle file uploads when the element was set as not required.

The reason was that Zend_File_Transfer checks all given input, in our case of HTTP POST the complete $_FILES array. Within this array we will find all files, of course also the prepared but not uploaded ones.

So, to get around this, we would have to ignore errors when isRequired is set to false. This was the solution of an issue which has been added by one of you. But this would lead to another problem: When you have multiple file elements, one required, and the other not required, this would not work because the elements setting isRequired to false would also negotate errors from the fileupload which is set required.

The finished and working solution acts different:
I added an new option to Zend_File_Transfer: ignoreNoFile. When it is set it will ignore all errors for the file where this option is set. Of course Zend_Form_Element_File was also changed to handle this new feature itself.

For you, as customer, there is no change in your code. The file element will handle this option in the background.
Of course, to get around of this nasty problem, you will have to use the SVN trunk or wait until 1.6.1

Enjoy it… new features are, of course, already in development and I will inform you as first one. :-)

Greetings
Thomas, I18N Team Leader, Zend Framework

Wednesday, August 27, 2008

Zend_Translate and SVN

By thomasw at 22:50:04

Hy interested ones,

using Zend_Translate with a software version control system like SVN or CVS can drive developers crazy when you use directory search. This is due to the fact that all files are searched and added.

In the special case of a VCS system also the VCS directories are searched and added which often hold a complete but old version of the source file. This leads to the behaviour that old messageids are also added and presented. Your developer will not know why he get’s only old translations or wrong encoded files.

To solve this behaviour I added a new option ignore which defaults to ‘.’. All directories and files beginning with the value of this option will be ignored.

Now with this new option for example all .svn directories are ignored and not added. So your developer will always see the latest translation files.

You could change this option, for example, to ignore .svn if you want to have other . files and directories like .tmp or .source still added.

Greetings
Thomas Weidner, I18N Team Leader, Zend Framework

Wednesday, August 6, 2008

News on the Zend Framework I18N core

By thomasw at 20:39:39

Hy interested ones,

I have good and bad news for you.

The bad news are that due to minor BC changes and the lack of time from the dev-team, which has to review the changes in the API, all new features from the I18N core are delayed until release 1.7.
This is also the case for the application wide locale which I described a chapter before. But, and this is more bad, most fixed issues will also be delayed until 1.7.

The good news are that the new release is scheduled for end September. But you can use all new features already when you are using the trunk.

What has been changed until now for the I18N core:
* I integrated an application wide locale… all I18N components can reuse an instance of Zend_Locale when registered in Zend_Registry.
* I integrated the new CLDR 1.6.1… it add’s not only several more complete informations for different languages, but also new data which will be added soon… for example, telephone data, character fall back rules, unit names just to mention some of them
* A new measurement class for time duration has been added

More things will come soon.

Another good thing is that Zend_File_Transfer was cored yesterday. It is officially available for release 1.6. Also here new features will be available soon, but not until 1.6.

I will keep you informed.

Greetings
Thomas, I18N Team Leader, Zend Framework

Calendar

  • January 2008
    SunMonTueWedThuFriSat
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031 

Last 8 comments

Certificates

zf-zce-logo.gif

zf-education-advisory-board-m.png

Admin area