php

You are currently browsing the archive for the php category.

The Scp Transfer plugin allows Ortro to transfer files between remote hosts in a secure way.

This release add the capability to use compression during transfer and enable the recursive copy of files and folders.
Download
and enjoy ;-)

Tags: , , ,

The Scp Transfer plugin allows Ortro to transfer files between remote hosts in a secure way.

This release add the capability to filter the copy using whitelist or blacklist based on the host:path string and the saving of transfer time.
Download
and enjoy ;-)

Tags: , , ,

If you use both Pear and Zend framework you may want to extend the Zend_Auth adapters to use all the containers shipped with the Pear::Auth package.

Let’s go to create the class implements the Zend_Auth_Adapter_Interface starting from the Zend_Auth_Adapter_Ldap class.

Tags: , , ,

The first beta version of Minerva (Ldap Password Changer) at version 0.5 has been released!

Minerva is a very little and simple PHP application with only one scope: permit at your openldap accounts to simply change its password without use other application (such as a webmail) that you can have or not.

Download it and tell us what you think about it!

Tags: , , , , ,

Requirements

Simple ReCaptcha

Zend Form Class

source: application/forms/ReCaptcha.php

<?php
class Form_ReCaptcha extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');
        //Add your elements here...

        $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

        $captcha = new Zend_Form_Element_Captcha('challenge',
              array('captcha'        => 'ReCaptcha',
                    'captchaOptions' => array('captcha' => 'ReCaptcha', 'service' => $recaptcha)));

        $this->addElement($captcha);

        // Add the submit button
        $this->addElement('submit', 'submit', array('label' => 'Submit'));
    }
}
?>

Zend Controller Class

source: application/controller/ReCaptchaController.php

<?php
class ReCaptchaController extends Zend_Controller_Action
{
    public function indexAction()
    {
        require_once APPLICATION_PATH . '/forms/Contact.php';

        $form = new Form_ReCaptcha();

        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

                $result = $recaptcha->verify($this->_getParam('recaptcha_challenge_field'),
                                             $this->_getParam('recaptcha_response_field'));
                if (!$result->isValid()) {
                    //ReCaptcha validation error
                    //Your action here...
               }
            }
        }
        $this->view->form = $form;
    }
}
?>

Customized ReCaptcha

You may also want to internationalizing or change colors to ReCaptcha, to do it you need to specify some options for the Zend_Service_ReCaptcha object.
See the ReCaptcha wiki for a complete list of available options.

Zend Form Class

source: application/forms/ReCaptcha.php

<?php
class Form_ReCaptcha extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');
        //Add your elements here...
        $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

        //Translate in your language
        $recaptcha_it_translation =
            array('visual_challenge' => "Verifica video",
                  'audio_challenge' => "Verifica audio",
                  'refresh_btn' => "Effettua una nuova verifica",
                  'instructions_visual' => "Scrivi le due parole",
                  'instructions_audio' => "Scrivi quello che ascolti",
                  'help_btn' => "Aiuto",
                  'play_again' => "Riascolto di nuovo l'audio",
                  'cant_hear_this' => "Scarica l'audio come MP3",
                  'incorrect_try_again' => "Incorretto. Prova ancora.");

        $recaptcha->setOption('custom_translations', $recaptcha_it_translation);
        //Change theme
        $recaptcha->setOption('theme', 'clean');

        $captcha = new Zend_Form_Element_Captcha('challenge',
              array('captcha'        => 'ReCaptcha',
                    'captchaOptions' => array('captcha' => 'ReCaptcha',
                                             'service' => $recaptcha)));

        $this->addElement($captcha);

        // Add the submit button
        $this->addElement('submit', 'submit', array('label' => 'Submit'));
    }
}
?>

Tags: , , ,

Requirements:

- PHP5
- PEAR::Net_Nmap
- nmap

<?php

/**
 * Scan network to retrieve hosts and services information.
 */

require_once 'Net/Nmap.php';

//Define the target to scan
$target = array('127.0.0.1','www.yourserver.com');

$options = array('nmap_binary' => '/usr/local/bin/nmap');

try {
    $nmap = new Net_Nmap($options);

    //Enable nmap options
    $nmap_options = array('os_detection' => true,
                          'service_info' => true,
                          'port_ranges' => 'U:53,111,137,T:21-25,80,139,8080',//to scan only specified ports
                          );

    $nmap->enableOptions($nmap_options);

    //Scan target
    $res = $nmap->scan($target);

    //Get failed hosts
    $failed_to_resolve = $nmap->getFailedToResolveHosts();

    if (count($failed_to_resolve) > 0) {
        echo 'Failed to resolve given hostname/IP: ' .
             implode (', ', $failed_to_resolve) .
             "\n";
    }

    //Parse XML Output to retrieve Hosts Object
    $hosts = $nmap->parseXMLOutput();

    //Print results
    foreach ($hosts as $key => $host) {
        echo 'Hostname: ' . $host->getHostname() . "\n";
        echo 'Address: ' . $host->getAddress() . "\n";
        echo 'OS: ' . $host->getOS() . "\n";
        echo 'Status: ' . $host->getStatus . "\n";
        $services = $host->getServices();
        echo 'Number of discovered services: ' . count($services) . "\n";
        foreach ($services as $key => $service) {
            echo "\n";
            echo 'Service Name: ' . $service->name . "\n";
            echo 'Port: ' . $service->port . "\n";
            echo 'Protocol: ' . $service->protocol . "\n";
            echo 'Product information: ' . $service->product . "\n";
            echo 'Product version: ' . $service->version . "\n";
            echo 'Product additional info: ' . $service->extrainfo . "\n";
        }
    }
} catch (Net_Nmap_Exception $ne) {
    echo $ne->getMessage();
}
?>

Tags: , , ,

The code below allows to retrieve and execute all SQL statements defined in a SQL script file removing all comments.

<?php
$sql_file = 'test.sql';

$contents = file_get_contents($sql_file);

// Remove C style and inline comments
$comment_patterns = array('/\/\*.*(\n)*.*(\*\/)?/', //C comments
                          '/\s*--.*\n/', //inline comments start with --
                          '/\s*#.*\n/', //inline comments start with #
                          );
$contents = preg_replace($comment_patterns, "\n", $contents);

//Retrieve sql statements
$statements = explode(";\n", $contents);
$statements = preg_replace("/\s/", ' ', $statements);

require_once 'MDB2.php';

$mdb2 =& MDB2::connect('mysql://usr:pw@localhost/dbnam');

foreach ($statements as $query) {
    if (trim($query) != '') {
        echo 'Executing query: ' . $query . "\n";
        $res = $mdb2->exec($query);

        if (PEAR::isError($res)) {
            die($res->getMessage());
        }
    }
}
?>

I have used Pear::MDB2 abstraction layer to interact with the database but the code above should work with any other db abstraction layer or PHP built-in functions.

Tags: , ,