Freelancer Martin Zeller

php asp.net java c# .net c++ xml xhtml seo magento zend framework dotnetnuke tomcat iis sql server mysql oracle typo3 coremedia

Zend_Captcha, an example without Zend_Form

Tags: ,

Here is your example how to implement Zend_Captcha_Image without using Zend_Form.

In your action method:

public function indexAction()
{
//...
$captcha = new Zend_Captcha_Image();
$captcha->setImgDir(APPLICATION_PATH . '/../public/tmp/captcha/');
$captcha->setImgUrl($this->view->baseUrl('/tmp/captcha/'));
$captcha->setFont(APPLICATION_PATH . '/fonts/elephant.ttf');
$captcha->setWidth(350);
$captcha->setHeight(150);
$captcha->setWordlen(5);
$captcha->setFontSize(70);
$captcha->setLineNoiseLevel(3);
$captcha->generate();
$this->view->captcha = $captcha;   // giving captcha object to the view
//...
}

In the corresponding view render the captcha and add a hidden field with the captcha id:

<form>
<input id="captcha" type="text" name="captcha" />
<?php echo $this->captcha->render($this, null) ?>
<input type="hidden" name="cid" value="<?php echo $this->captcha->getId() ?>" >
</form>

Validating the input of the user after postback:

// ...
$capId = $_POST['cid'];
$capSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$capId);
if ($_POST['captcha']==$capSession->word)
{
  // input OK
}
else {
  // input NOK
}
// ...

Good luck!

PS: If you want to use your Zend_Captcha WITH Zend_Form, this blog post may help you: Zend_Captcha with Zend_Form

PHP – Delete files by pattern

Tags:

If you want to delete files by pattern (e.g. “*.txt”) in a directory, you can do this with the php function glob!

$files = glob('/path/to/files/*.txt'); // finds all txt files in the directory /path/to/files/
foreach($files as $file)
  unlink($file);

With this method you can also delete all files of a directory – with pattern *.*

PHP – Delete all files in a directory

Tags:

Simple question – simple answer:

How do I delete all files in a directory with PHP?

$files = glob('/path/to/directory/*.*');
foreach($files as $file)
  unlink($file);

PDF: direct printing with .NET

Tags: ,

>> Download Example project (VS 2005)

In this article I will show you the results of my search to find a way to print a pdf with .NET.

My task was to find a simple solution for an intranet web application where the user gets pdf reports (ActiveReports). Depending on configuration settings these pdf files should be displayed in the browser and be printed immediately or just be printed immediatly – to the default printer or to a known network printer.
The existing solution was based on the pdf browser plugins. But it was not satisfactory because the user had to do the following steps: view the pdf file in the browser (pdf browser plugin), click the print button, handle the printer dialog popup, click the OK button to send the document to the printer. And some users had to do this more then one hundred times a day. And some users had to do this more than a hundred times a day!
My (more than revolutionary ;-) idea was, that I may need the pdf browser plugin for viewing pdf files, but not for printing them! So I tried different ways, which I wanted you to show now – I did not bring all of them to an end, as some turned out to be inappropriate for  my intentions very soon.
Important: these solutions are only making sense if you want to print to a known printer (like in intranets) – they will not work on normal, “public” web sites.

You can download the sample project and do your own experiments. Here is a screenshot of the sample project:

PrintingPDF WebApp Screenshot

My results:

  1. Printing a pdf with Ghostscript
  2. Printing a pdf with windows shell command
  3. Printing a pdf with PrintDocument object
  4. Printing a pdf with PDFsharp
  5. Printing a pdf with Acrobat Reader command line

1. Printing a pdf with Ghostscript

First you have to install Ghostscript, an interpreter for the PostScript language and for PDF. Code:

ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Arguments = String.Format(" -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\{0}\" \"{1}\"", printerName, pdfFileName);
psInfo.FileName = @"C:\Program Files\gs\gs8.70\bin\gswin32c.exe";
psInfo.UseShellExecute = false;
Process process = Process.Start(psInfo);

2. Printing a pdf with windows shell command

This is another version I found on the web. The code uses the print/printto command of the DOS shell. In the example project, which you can download above, there are two versions of parameters in the code. I never got it work, but I think, it cannot be done: you cannot print a pdf file via shell – but it will work with raw text files. Please tell me your results!
Try this in your DOS prompt:

print /D:"\\COMPUTERNAME\PRINTERNAME" "PDFFILEPATH"

For those who want to try it themselves

ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Verb = "Print"; // or "PrintTo"
psInfo.FileName = pdfFileName;
psInfo.Arguments = String.Format("/p /h \"{0}\" \"{1}\"", pdfFileName, printerName);
psInfo.WindowStyle = ProcessWindowStyle.Hidden;
psInfo.CreateNoWindow = true;
psInfo.UseShellExecute = true;
Process process = Process.Start(psInfo);

3. Printing a pdf with PrintDocument object

Using the .NET object PrintDocument is another possible way, but you will need third party components to raster the pdf. More information: >>Component for rendering pdf documents

PrintDocument pd = new PrintDocument();
pd.DocumentName = pdfName;
pd.PrinterSettings.PrinterName =printerName;
pd.PrinterSettings.PrintFileName = fileName;
pd.PrintController = new StandardPrintController();
pd.OriginAtMargins = false;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();

4. Printing a pdf with PDFsharp

PDFsharp is an open source .NET library for processing PDF. You can use it for printing too. Simple usage, but the problem here: the AdobeReader application window appears (not suitable for my intentions). But I found out that you do not need PDFsharp to print out a pdf – see chapter 5

PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"
PdfFilePrinter printer = new PdfFilePrinter(pdfFileName, printerName);
printer.Print();

5. Printing a pdf with Acrobat Reader command line

This was the best way for me! Although I’ve read that this only works with old versions of Acrobat Reader, it works with version 9.0!
A word to the arguments:
print it to the default printer: /s /o /h /p “pdfFileName”
print it to a defined printer: /s /o /h /t “pdfFileName” “printername” “drivername” “portname”
More information at the Adobe Acrobat Developer FAQ on page 27!
To download the free acrobat reader use this link: Acrobat Reader

ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.FileName = @"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe";
psInfo.Arguments = String.Format("/s /o /h /p{0}", pdfFileName);
psInfo.WindowStyle = ProcessWindowStyle.Hidden;
psInfo.CreateNoWindow = true;
psInfo.UseShellExecute = true;
Process process = Process.Start(psInfo);

Client localisation with zend framework

Tags: ,

If you want to find out the language or country of the clients browser, do it this way:

<?php
require_once 'Zend/Locale.php';
$locale = new Zend_Locale();
echo $locale->toString();
?>

But be aware! This way you only get the localisation of the clients browser. And this is configurable by the user.

The code above will give you output like this:

de_AT

or

en_US

There are many methods you can use with zend localisation:

<?php echo $locale->toString() ?>
<?php echo $locale->getLanguage() ?>
<?php echo $locale->getRegion() ?>
<?php print_r($locale->getBrowser()); // Return an array of all accepted languages of the client ?>

Example output:

de_AT
de
AT
Array (     [de_AT] => 1     [de] => 1     [de_DE] => 0.8     [en_GB] => 0.5     [en] => 0.5     [en_US] => 0.3 )

Good luck!

Passwort setzen für das Gastkonto unter Windows 7

Tags:

Das Passwort für den Gast-Account unter Windows 7 zu setzen ist eigentlich gar nicht schwer, wenn man einmal weiß, wie’s geht ;-)

  1. “windows”-Taste + “r”
  2. Eingabe von “control userpasswords2?
  3. Im sich öffnenden Fenster findet man dann die notwendigen Möglichkeiten

Viel Erfolg

Setting a password for the guest account in Windows 7

Tags:

Do you want to set a password for the guest account in windows 7 (or vista)?

It’s quite simple… if you know it ;-)

  1. “windows”-key + “r”
  2. Enter: “control userpasswords2″

MySQL: import a dump

Tags:

If you have got a sql-File and want to import it to your MySQL database then you can do this with a simple line in the shell:


mysql -u username -p password -h databaseservername  datebasename < sqlfile.sql

Example:
Say there is a local MySQL database, with a database called ‘mybestsellers’, we have got a dump file ‘/home/users/jripper/bestsellerdump.sql’ and we connect to our database with username ‘dbuser’ and password ‘xxx’
Our input at the shell would be:


mysql -u dbuser -p xxx -h localhost mybestsellers < /home/users/jripper/bestsellerdump.sql

Magento: Warning in Mage/Eav/Model/Entity/Abstract.php ?

Tags: ,

All you want to do is saving a product like this?

$api = new Mage_Catalog_Model_Product_Api();
$productData = array();
$productData['visibility'] = Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE;
$productData['status'] = Mage_Catalog_Model_Product_Status::STATUS_DISABLED;
$api->update($product->getId(), $productData);

Or maybe without the API?

$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
$product->save();

And all you get is an error like this?

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/whlid/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 972

#0 /opt/lampp/htdocs/whlid/app/code/core/Mage/Eav/Model/Entity/Abstract.php(972): mageCoreErrorHandler(2, 'Invalid argumen...', '/opt/lampp/htdo...', 972, Array)
#1 /opt/lampp/htdocs/whlid/app/code/core/Mage/Eav/Model/Entity/Abstract.php(927): Mage_Eav_Model_Entity_Abstract-&gt;_collectSaveData(Object(Mage_Catalog_Model_Product))
#2 /opt/lampp/htdocs/whlid/app/code/core/Mage/Core/Model/Abstract.php(251): Mage_Eav_Model_Entity_Abstract-&gt;save(Object(Mage_Catalog_Model_Product))
#3 /opt/lampp/htdocs/whlid/app/code/core/Mage/Catalog/Model/Product/Api.php(219): Mage_Core_Model_Abstract-&gt;save()
#4 /opt/lampp/htdocs/whlid/app/code/local/Willhaben/Customer/controllers/AccountController.php(20): Mage_Catalog_Model_Product_Api-&gt;update('161', Array)
#5 /opt/lampp/htdocs/whlid/app/code/core/Mage/Core/Controller/Varien/Action.php(376): Willhaben_Customer_AccountController-&gt;deleteanzeigeAction()
#6 /opt/lampp/htdocs/whlid/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(248): Mage_Core_Controller_Varien_Action-&gt;dispatch('deleteanzeige')
#7 /opt/lampp/htdocs/whlid/app/code/core/Mage/Core/Controller/Varien/Front.php(158): Mage_Core_Controller_Varien_Router_Standard-&gt;match(Object(Mage_Core_Controller_Request_Http))
#8 /opt/lampp/htdocs/whlid/app/Mage.php(459): Mage_Core_Controller_Varien_Front-&gt;dispatch()
#9 /opt/lampp/htdocs/whlid/index.php(65): Mage::run()
#10 {main}

The error occurs in this area of /app/code/core/Mage/Eav/Model/Entity/Abstract.php ?

$origData = $this->_getOrigObject($newObject)->getOrigData();

/**
* drop attributes that are unknown in new data
* not needed after introduction of partial entity loading
*/
foreach ($origData as $k=>$v) {
if (!array_key_exists($k, $newData)) {
unset($origData[$k]);
}
}

SOLUTION: try to prepend this line to your code:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Visual Studio 2005 und Windows 7

Tags: , ,

Da der Bildschirm meines alten Laptops mit XP (Vista habe ich an mir spurlos vorüberziehen lassen) langsam aber sicher einen bunten Vorhang aus bunten Streifen fallen läßt, habe ich  mich dazu entschlossen, mir rechtzeitig einen neuen zuzulegen. Gleich mit Windows 7, etwas Risiko muß sein.

Ich  muß sagen, die Portierung meiner Entwicklungsumgebungen für php und Java klappte ohne Probleme - es war nur ein Kopieren vom alten Rechner zum Neuen. Sogar bei der Umstellung von Sql Server 2005 auf Sql Server 2008 lief alles glatt – Datenbanken sichern auf der einen Seite, Datenbanken herstellen auf der anderen.

Erst bei Visual Studio 2005 geriet der Vorgang etwas ins Stocken. Die transferierten Projekte wollten nicht kompilieren (Probleme mit den Resourcen). Nach etwas Nachforschen im Internet war auch das gelöst – es gibt scheinbar seit Vista die Möglichkeit festzulegen, daß man einen Applikation als Administrator starten möchte. Und tatsächlich, so funktionierts:

Unter Windows 7 bei der Verknüpfung zu Visual Studio 2005: rechte Maustaste > Eigenschaften > Kompatibilität > “Programm als Administrator ausführen”

Von da an lief VS 2005 wie unter XP. Bin gespannt, was ich noch alles mit Windows 7 entdecken werde… z.B. klappt die Installation von McAfee Total Protection noch nicht – bekomme gleich nach Start des Setups für die Installation die Meldung, daß keine Internetverbindung vorhanden ist… mal sehen

© 2009 Freelancer Martin Zeller. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.