home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Weather.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  8.5 KB  |  220 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Weather.php,v 1.24 2004/03/28 13:19:39 eru Exp $
  20.  
  21. // {{{ constants
  22. // {{{ cache times
  23. define("SERVICES_WEATHER_EXPIRES_UNITS",      900);
  24. define("SERVICES_WEATHER_EXPIRES_LOCATION",   900);
  25. define("SERVICES_WEATHER_EXPIRES_WEATHER",   1800);
  26. define("SERVICES_WEATHER_EXPIRES_FORECAST",  7200);
  27. define("SERVICES_WEATHER_EXPIRES_LINKS",    43200);
  28. // }}}
  29.  
  30. // {{{ error codes
  31. define("SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND",   10);
  32. define("SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION",    11);
  33. define("SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA",   12);
  34. define("SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED",   13);
  35. define("SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED",    14);
  36. // }}}
  37.  
  38. // {{{ error codes defined by weather.com
  39. define("SERVICES_WEATHER_ERROR_UNKNOWN_ERROR",            0);
  40. define("SERVICES_WEATHER_ERROR_NO_LOCATION",              1);
  41. define("SERVICES_WEATHER_ERROR_INVALID_LOCATION",         2);
  42. define("SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID",     100);
  43. define("SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE",   101);
  44. define("SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY",    102);
  45. // }}}
  46. // }}}
  47.  
  48. // {{{ class Services_Weather
  49. /**
  50. * PEAR::Services_Weather
  51. *
  52. * This class acts as an interface to various online weather-services.
  53. *
  54. * Services_Weather searches for given locations and retrieves current weather data
  55. * and, dependant on the used service, also forecasts. Up to now, SOAP services from
  56. * CapeScience and EJSE, XML from weather.com and METAR from noaa.gov are supported,
  57. * further services will get included, if they become available and are
  58. * properly documented.
  59. *
  60. * @author       Alexander Wirtz <alex@pc4p.net>
  61. * @package      Services_Weather
  62. * @license      http://www.php.net/license/2_02.txt
  63. * @version      1.2
  64. */
  65. class Services_Weather {
  66.  
  67.     // {{{ &service()
  68.     /**
  69.     * Factory for creating the services-objects
  70.     *
  71.     * Usable keys for the options array are:
  72.     * o debug               enables debugging output
  73.     * --- Common Options
  74.     * o cacheType           defines what type of cache to use
  75.     * o cacheOptions        passes cache options
  76.     * o unitsFormat         use (US)-standard, metric or custom units
  77.     * o customUnitsFormat   defines the customized units format
  78.     * o httpTimeout            sets timeout for HTTP requests
  79.     * o dateFormat          string to use for date output
  80.     * o timeFormat          string to use for time output
  81.     * --- EJSE Options
  82.     * none
  83.     * --- GlobalWeather Options
  84.     * none
  85.     * --- METAR Options
  86.     * o dsn                 String for defining the DB connection
  87.     * o dbOptions           passes DB options
  88.     * o source              http, ftp or file - type of data-source
  89.     * o sourcePath          where to look for the source, URI or filepath
  90.     * --- weather.com Options
  91.     * o partnerID           You'll receive these keys after registering
  92.     * o licenseKey          with the weather.com XML-service
  93.     *
  94.     * @param    string                      $service
  95.     * @param    array                       $options
  96.     * @return   PEAR_Error|object
  97.     * @throws   PEAR_Error
  98.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND
  99.     * @access   public
  100.     */
  101.     function &service($service, $options = null)
  102.     {
  103.         $service = ucfirst(strtolower($service));
  104.         $classname = "Services_Weather_".$service;
  105.  
  106.         // Check for debugging-mode and set stuff accordingly
  107.         if (is_array($options) && isset($options["debug"]) && $options["debug"] >= 2) {
  108.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  109.                 define("SERVICES_WEATHER_DEBUG", true);
  110.             }
  111.             include_once("Services/Weather/".$service.".php");
  112.         } else {
  113.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  114.                 define("SERVICES_WEATHER_DEBUG", false);
  115.             }
  116.             @include_once("Services/Weather/".$service.".php");
  117.         }
  118.  
  119.         // No such service... bail out
  120.         if (!class_exists($classname)) {
  121.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND);
  122.         }
  123.  
  124.         // Create service and return
  125.         $error = null;
  126.         @$obj = &new $classname($options, $error);
  127.  
  128.         if (Services_Weather::isError($error)) {
  129.             return $error;
  130.         } else {
  131.             return $obj;
  132.         }
  133.     }
  134.     // }}}
  135.  
  136.     // {{{ apiVersion()
  137.     /**
  138.     * For your convenience, when I come up with changes in the API...
  139.     *
  140.     * @return   string
  141.     * @access   public
  142.     */
  143.    function apiVersion()
  144.     {
  145.         return "1.2";
  146.     }
  147.     // }}}
  148.  
  149.     // {{{ _errorMessage()
  150.     /**
  151.     * Returns the message for a certain error code
  152.     *
  153.     * @param    PEAR_Error|int              $value
  154.     * @return   string
  155.     * @access   private
  156.     */
  157.     function _errorMessage($value)
  158.     {
  159.         static $errorMessages;
  160.         if (!isset($errorMessages)) {
  161.             $errorMessages = array(
  162.                 SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND         => "Requested service could not be found.",
  163.                 SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION          => "Unknown location provided.",
  164.                 SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA         => "Server data wrong or not available.",
  165.                 SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED         => "Cache init was not completed.",
  166.                 SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED          => "MetarDB is not connected.",
  167.                 SERVICES_WEATHER_ERROR_UNKNOWN_ERROR             => "An unknown error has occured.",
  168.                 SERVICES_WEATHER_ERROR_NO_LOCATION               => "No location provided.",
  169.                 SERVICES_WEATHER_ERROR_INVALID_LOCATION          => "Invalid location provided.",
  170.                 SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID        => "Invalid partner id.",
  171.                 SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE      => "Invalid product code.",
  172.                 SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY       => "Invalid license key."
  173.             );
  174.         }
  175.  
  176.         if (Services_Weather::isError($value)) {
  177.             $value = $value->getCode();
  178.         }
  179.  
  180.         return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[SERVICES_WEATHER_ERROR_UNKNOWN_ERROR];
  181.     }
  182.     // }}}
  183.  
  184.     // {{{ isError()
  185.     /**
  186.     * Checks for an error object, same as in PEAR
  187.     *
  188.     * @param    PEAR_Error|mixed            $value
  189.     * @return   bool
  190.     * @access   public
  191.     */
  192.     function isError($value)
  193.     {
  194.         return (is_object($value) && (strtolower(get_class($value)) == "pear_error" || is_subclass_of($value, "pear_error")));
  195.     }
  196.     // }}}
  197.  
  198.     // {{{ &raiseError()
  199.     /**
  200.     * Creates error, same as in PEAR with a customized flavor
  201.     *
  202.     * @param    int                         $code
  203.     * @return   PEAR_Error
  204.     * @access   private
  205.     */
  206.     function &raiseError($code = SERVICES_WEATHER_ERROR_UNKNOWN_ERROR)
  207.     {
  208.         // This should improve the performance of the script, as PEAR is only included, when
  209.         // really needed.
  210.         include_once "PEAR.php";
  211.  
  212.         $message = "Services_Weather: ".Services_Weather::_errorMessage($code);
  213.  
  214.         return PEAR::raiseError($message, $code, PEAR_ERROR_RETURN, E_USER_NOTICE, "Services_Weather_Error", null, false);
  215.     }
  216.     // }}}
  217. }
  218. // }}}
  219. ?>
  220.