Archive

Archive for the ‘Programming’ Category

Hide borders for an input box

November 6, 2009 Wes Leave a comment

I wanted to have a form with read only input boxes that were populated by Ajax calls. The problem is there was a border around the box in IE7 and FF 3.5. In IE6 border=0 solved the issue but not in the other browsers. Thanks to this site for the inspiration I was able to remove the borders using the this css style:

.hiddenlabel[type="text"] {
border:none;
display:inline;
vertical-align:middle;
}

The key is the [type="text"], this applies the style to the input box. Just in case your wondering why I need to do this it is because I need the information in the form to be submitted and need to update the information using Ajax. This was easier than using hidden fields.

 

Categories: CSS Tags:

Save an email attachment using Zend Mail

June 10, 2009 Wes Leave a comment

To do some automated billing I needed to grab an email attachment off the exchange server. The Zend framework made it very easy to connect to the mail server and navigate files, and that part of the code came directly from the Zend Framework documentation. The issue that took the longest was saving the attachment. I could not figure out how to decode the attachment using the Zend framework. Let me know if you have a better way or if it helps you out.
NOTE: If you getting the mail from the INBOX you do not have to use the getFolders() function.

// Connecting with Imap
$mail = new Zend_Mail_Storage_Imap(
        array('host'     => 'SERVER',
              'user'     => 'USERNAME',
              'password' => 'PASSWORD'));

// Navigate to desired folder
$folder = $mail->getFolders()->INBOX->Info;

// Change to folder
$mail->selectFolder($folder);

// Loop through messages
foreach ($mail as $message)
{
 // Find desired message subject
 if($message->subject == 'SUBJECT')
 {
 // Check for attachment
 if($message->isMultipart())
 {
    $part = $message->getPart(2);
 }

 // Get the attacment file name
 $fileName = $part->getHeader('content-description');

 // Get the attachement and decode
 $attachment = base64_decode($part->getContent());

 // Save the attachment
 $fh = fopen($fileName, 'w');

 fwrite($fh, $attachment);

 fclose($fh);
 }
}
Categories: PHP, Zend Framework

Getting the root node from an XML string

June 3, 2009 Wes Leave a comment

PHPs SimpleXML is great for parsing xml files. The downside is that it does not return the root node name. I wrote this function that gets the name.


/**
 * function getXMLRootNode
 * @param string An xml string
 * @return string Return XML root node name
 */

function getXMLRootNode($xmlstr)
{
 // Create DOM model
 $doc = new DOMDocument();

 // Load the XML string
 if(!$doc->loadXML($xmlstr))
 {
 throw new Exception('Unable to parse XML string');
 }

 // Find the root tag name
 $root = $doc->documentElement;

 if(!isset($root))
 {
 throw new Exception('Unable to find XML root node');
 }

 if(!isset($root->nodeName))
 {
 throw new Exception('Unable to find XML root node name');
 }

 return $root->nodeName;
}
Categories: PHP, Programming

New autoloading with Zend Framework 1.8

May 15, 2009 Wes 2 comments

I love the fact that you can autoload classes using the ZF. I was a little shocked at the depreciation errors when I updated to ZF 1.8. Apparently, there is a new way to use the autoload class. It took a little reading but I came up with this solution. Let me know what you think!

/*
 * Set up Zend loader
 */
require_once 'Zend/Loader/Autoloader.php';

$autoloader = Zend_Loader_Autoloader::getInstance();

/*
 * Register Cisc Classes
 */
$autoloader->registerNamespace('Cisc_');

The registerNamespace function registers my own classes.

Categories: PHP Tags:

Creating a Soap Server using the Zend Framework

May 15, 2009 Wes Leave a comment

I decide to try my hand at creating a soap server using the Zend Framework. This was an exersice in getting me more familiar with ZF and web services. Hopefully this will help others. I think the code is pretty self explanitory but feel free to ask questions or provide comments.

Basically the WSDL is auto generated by php if the URL ends in ?wsdl, if not then the soap server is created and the request handled.


/*
* Check to see if soap call is to be handled
* or if the WSDL should be displayed
*/
if(isset($_GET['wsdl']))
{
    createWSDL();
}
else
{
   /*
    * Soap Server WSDL document
    */
    $wsdl = 'http://' . $_SERVER["SERVER_NAME"] .
    $_SERVER['PHP_SELF'] . '?wsdl';

   /*
    * Create a new soap server
    */
    $server = new Zend_Soap_Server($wsdl,
        array('soap_version' => SOAP_1_2)
    );

   /*
    * Set the server to cache the wsdl file
    */
    $server->setWsdlCache(1);

   /*
    * Set the class for the web service
    */
    $server->setClass('Cisc_Ws_Cs3webservice');

   /*
    * Set soap server persistance (For use with login feature)
    */
    $server->setPersistence(SOAP_PERSISTENCE_SESSION);

   /*
    * Register exceptions
    */
    $server->registerFaultException('Exception');
   /*
    * Handle the soap call
    */
    $server->handle();
}

/**
* Create the WSDL file for display
*
* @param none
* @return string The wsdl xml
*/
function createWSDL()
{
    $wsdl = new Zend_Soap_AutoDiscover();

    $wsdl->setClass('Cisc_Ws_Cs3webservice');

    $wsdl->handle();
}

The WSDL is created from the class file, so make sure you use proper documentation blocks for you code. The only issue I have seen so far is that if a function input in the class is a integer,
no matter what is inputted, an integer will be pass to the function. This makes it difficult to verify data, say that your getting an integer and not a float. The workaround is to pass an array
and check the values of the array.

Categories: PHP Tags:

Zend Server Community Edition Install

March 4, 2009 Wes 3 comments

So  I decided I would try out Zend Server CE. I have tried the installation on a server running Windows 2003 and XP Professional with PHP already installed using ISAPI. The install goes fine, but when I try to access the web page I get an FastCGI Access Denied error.

I have tried everything I can think of, including the suggestions on the web for changing permissions on several directories, but I still cannot resolve this issue. I am going to try and get it working but if anybody has any suggestions please let me know.

UPDATE: I was finally able to get Zend Sever installed. I had to remove IIS using add/remove programs, re-install it, then install Zend Server. I like it a lot.

Categories: PHP, Programming

Cross Domain Ajax

September 29, 2008 Wes Leave a comment

I found a nice simple way to do cross domain ajax calls reliably. I found this Yahoo page that gives a nice little tutorial on how to use php and curl to access other domains using a simple proxy. Simple and effective. The code that I tweeked for my own use is found here. Let me know if you have any comments or suggestions.

Categories: PHP, Programming

Usefull C# Code

July 24, 2007 Wes Leave a comment

While creating a couple of programs for work I came across some useful C# code that made my life easier. There may be different or better version you know of, but these worked well for me.

cNetworkDrive from http://www.aejw.com/ is very useful for mounting and unmounting network drives.

FTP Client Library from The Code Project is very useful for connecting to and retrieving files from an FTP server.

Let me know if these were helpful for you or a hindrance. Do you have a better code sample?

Categories: C#

How to install a java application as a windows service.

May 21, 2007 Wes 1 comment

At work we had a java application that was written for us. The application needed to run as a windows service so that it would run on the server without the need for a user to log in. It took me awhile to figure it out, as there was not one spot with all the information I needed. Here’s how I did it, your mileage may vary!

This was compiled with references from MS using the Windows Server 2003 Resource Kit Tools, you can use the resource kit appropriate to your server install. The only files needed from the resource kit are the instsrv.exe and the srvany.exe files. These can be found on the internet, but for a corporate server install I thought it was best to download the kit from Microsoft.

Now to the steps:

  1. Use instsrv.exe to install srvany.exe as the service, from the command line: <path>\<to>\instsrv.exe <application_name> <path>\<to>\srvany.exe.
  2. Run regedit and Find: My Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ Services\<application_name>
  3. Select the <application_name> and select “Edit..”, “New…”, “Key“.
  4. Name the key Parameters.
  5. Select the Parameters key that you have created, and select “Edit…”, “New…“, “String Value“.
  6. The Name should be Application.
  7. Double click the name to open a dialog in which you can enter the value <path>\<to>\<jre>\javaw.exe.
  8. Select “Edit…”, “New…“, “String Value
  9. The Name should be AppDirectory.
  10. Double click the name to open a dialog in which you can enter the value <path>\<to>\<java class files>.
  11. Select “Edit…“, “New…“, “String Value“.
  12. The Name should be AppParameters.
  13. Double click the name to open a dialog in which you can enter the value -Xrs <application_name>

IMPORTANT! : the -Xrs must be in the AppParamenter name.

Notes: Ensure the service is set to “Log on” as a Local System Account, and the “Interact with desktop” is unchecked.

This is how I did it. The most important part was using the -Xrs value. It would not work at all until that value was set.

Comments or Questions?

Categories: Java