phpDocumentor ZNF
Util
[ class tree: ZNF ] [ index: ZNF ] [ all elements ]

Source for file Utility.php

Documentation is available at Utility.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4.  * PHP version 5
  5.  *
  6.  * This source file is subject to version 2.1 of the GNU Lesser General Public
  7.  * License, that is bundled with this package in the file COPYING, available
  8.  * through the world wide web at the following URI:
  9.  * http://www.gnu.org/copyleft/lesser.html.
  10.  *
  11.  * @package    ZNF
  12.  * @subpackage Util
  13.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  14.  * @author     Graziano Liberati <http://www.liberati.org>
  15.  * @copyright  2004-2007 The ZNF Development Team
  16.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  17.  * @version    SVN $Id: Utility.php 44 2007-07-22 23:55:30Z evolic $
  18.  * @since      Release 0.5.0
  19.  * @link       http://www.zeronotice.org
  20.  */
  21.  
  22. /**
  23.  * <i>ZNF_Util_Utility</i> class provides useful methods.
  24.  *
  25.  * @access     public
  26.  * @package    ZNF
  27.  * @subpackage Util
  28.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  29.  * @author     Graziano Liberati <http://www.liberati.org>
  30.  * @copyright  2004-2007 The ZNF Development Team
  31.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  32.  * @version    SVN $Id: Utility.php 44 2007-07-22 23:55:30Z evolic $
  33.  * @since      Release 0.5.0
  34.  * @link       http://www.zeronotice.org
  35.  */
  36. {
  37.  
  38.     /**
  39.      * Private constructor to avoid <i>ZNF_Util_Utility</i> object creation.
  40.      *
  41.      * @access private
  42.      */
  43.     private function __construct()
  44.     {
  45.     }
  46.  
  47.     /**
  48.      * Private clonator to avoid <i>ZNF_Util_Utility</i> object clonation.
  49.      *
  50.      * @access private
  51.      */
  52.     private function __clone()
  53.     {
  54.     }
  55.  
  56.     /**
  57.      * Exports a generic array in a well formed XML string.
  58.      *
  59.      * @static
  60.      * @access public
  61.      * @param array $array 
  62.      * @param int $level Level of iteration, determines the depth of the
  63.      *                    array currently processed
  64.      * @param string $version 
  65.      * @param string $encoding 
  66.      * @return string 
  67.      */
  68.     static public function arrayToXML($array$level 0$version '1.0'$encoding 'utf-8')
  69.     {
  70.         if ($level == 0{
  71.             $xml "<?xml version=\"$version\" encoding=\"$encoding\" ?>\n";
  72.             $xml .= "<content>\n";
  73.         }
  74.  
  75.         $result null;
  76.  
  77.         // calculate spaces for indentation
  78.         $indent '  ';
  79.  
  80.         for ($i 0$i<$level$i++{
  81.             $indent .= '  ';
  82.         }
  83.  
  84.         // process the array
  85.         foreach ($array as $name => $value{
  86.  
  87.             if (is_array($value&& is_int($name)) {
  88.                 $result .= "$indent<item key=\"$name\">\n";
  89.                 $result .= self::arrayToXML($value$level+1);
  90.                 $result .= "$indent</item>\n";
  91.             elseif (is_array($value)) {
  92.                 $result .= "$indent<$name>\n";
  93.                 $result .= self::arrayToXML($value$level+1);
  94.                 $result .= "$indent</$name>\n";
  95.             elseif (is_int($name&& $value != null{
  96.                 $result .= "$indent<item key=\"$name\">htmlspecialchars($value"</item>\n";
  97.             elseif (is_int($name)) {
  98.                 $result .= "$indent<item key=\"$name\"/>\n";
  99.             elseif ($value != null{
  100.                 $result .= "$indent<$name>htmlspecialchars($value"</$name>\n";
  101.             else {
  102.                 $result .= "$indent<$name/>\n";
  103.             }
  104.  
  105.         }
  106.  
  107.         if ($level == 0{
  108.             $xml .= $result;
  109.             $xml .= "</content>\n";
  110.             return $xml;
  111.         else {
  112.             return $result;
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * Checks if an email address is valid.
  118.      *
  119.      * @static
  120.      * @access public
  121.      * @param string $email 
  122.      * @return boolean 
  123.      */
  124.     static public function checkEmail($email)
  125.     {
  126.         $mxhosts null;
  127.  
  128.         if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$'$email)) {
  129.             return false;
  130.         }
  131.  
  132.         list ($username$domainsplit ('@'$email);
  133.  
  134.         if (checkdnsrr($domain'MX'))  {
  135.  
  136.             if (!getmxrr($domain$mxhosts)) {
  137.                 return false;
  138.             }
  139.  
  140.             $connectAddress $mxhosts[0];
  141.         else {
  142.             return false;
  143.         }
  144.  
  145.         $connect fsockopen($connectAddress25);
  146.  
  147.         if (!$connect{
  148.             return false;
  149.         }
  150.  
  151.         $out fgets($connect1024);
  152.  
  153.         if (ereg("^220"$out)) {
  154.             fputs ($connect"HELO 82.49.142.198\r\n");
  155.             $out fgets($connect1024);
  156.             fputs ($connect"MAIL FROM: <" $email ">\r\n");
  157.             $from fgets($connect1024);
  158.             fputs ($connect"RCPT TO: <" $email ">\r\n");
  159.             $to fgets($connect1024);
  160.             fputs($connect"QUIT\r\n");
  161.             fclose($connect);
  162.  
  163.             if (!ereg ("^250"$from || !ereg "^250"$to )) {
  164.                 return false;
  165.             }
  166.  
  167.         }
  168.  
  169.         return true;
  170.     }
  171.  
  172.     /**
  173.      * Checks if an URL is valid and returns it in the correct format.
  174.      *
  175.      * @static
  176.      * @access public
  177.      * @param string $url 
  178.      * @return string 
  179.      */
  180.     static public function checkUrl($url)
  181.     {
  182.         $mxhosts null;
  183.         $domain preg_replace('/http:\/\/|https:\/\/|http:\/\/www.|https:\/\/www.|^www./'''$url);
  184.         $parts explode('/'$domain);
  185.         $domain $parts[0];
  186.  
  187.         if ((getmxrr($domain$mxhosts== FALSE && gethostbyname($domain== $domain)) {
  188.             return false;
  189.         }
  190.  
  191.         if (!(strstr($url'http://'|| strstr("Url $url not valid"'https://'))) {
  192.             $url "http://$url";
  193.         }
  194.  
  195.         return $url;
  196.     }
  197.  
  198.     /**
  199.      * Checks a date and returns is timestamp.
  200.      *
  201.      * @static
  202.      * @access public
  203.      * @param string $year 
  204.      * @param string $month 
  205.      * @param string $day 
  206.      * @return timestamp 
  207.      */
  208.     static public function getDateTimestamp($year null$month null$day null)
  209.     {
  210.         if (!(checkdate($month$day$year))) {
  211.             $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  212.             throw new ZNF_Util_UtilityException($translation->get('errorDateNotValid'"$day-$month-$year");
  213.         }
  214.  
  215.         return mktime(000$month$day$year);
  216.     }
  217.  
  218.     /**
  219.      * Applies addslaslashes PHP function to all the elements of an array.
  220.      *
  221.      * @static
  222.      * @access public
  223.      * @param array &$array 
  224.      * @return array 
  225.      */
  226.     static public function &addAllSlashes(&$array)
  227.     {
  228.         foreach ($array as &$current{
  229.  
  230.             if (is_array($current)) {
  231.                 $current self::addAllSlashes($current);
  232.             else {
  233.                 $current addslashes($current);
  234.             }
  235.  
  236.         }
  237.  
  238.         return $array;
  239.     }
  240.  
  241.     /**
  242.      * Applies stripslaslashes PHP function to all the elements of an array.
  243.      *
  244.      * @static
  245.      * @access public
  246.      * @param array &$array 
  247.      * @return array 
  248.      */
  249.     static public function &stripAllSlashes(&$array)
  250.     {
  251.         foreach ($array as &$current{
  252.  
  253.             if (is_array($current)) {
  254.                 $current self::stripAllSlashes($current);
  255.             else {
  256.                 $current stripslashes($current);
  257.             }
  258.  
  259.         }
  260.  
  261.         return $array;
  262.     }
  263.  
  264.     /**
  265.      * Destroys the <i>ZNF_Util_Utility</i> object.</>
  266.      *
  267.      * @access public
  268.      */
  269.     public function __destruct()
  270.     {
  271.     }
  272.  
  273. }
  274.  
  275. /**
  276.  * <i>ZNF_Util_UtilityException</i> is the exception type for the <i>ZNF_Util_Utility</i> class.
  277.  *
  278.  * <i>ZNF_Util_UtilityException</i> extends the <i>Exception</i> class of PHP5.
  279.  *
  280.  * @access     public
  281.  * @package    ZNF
  282.  * @subpackage Util
  283.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  284.  * @author     Graziano Liberati <http://www.liberati.org>
  285.  * @copyright  2004-2007 The ZNF Development Team
  286.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  287.  * @version    SVN $Id: Utility.php 44 2007-07-22 23:55:30Z evolic $
  288.  * @since      Release 0.5.0
  289.  * @link       http://www.zeronotice.org
  290.  */
  291. class ZNF_Util_UtilityException extends Exception
  292. {
  293. }
  294.  
  295. ?>

Documentation generated on Wed, 14 Nov 2007 23:47:42 +0100 by phpDocumentor 1.4.0