- Fixed #16160: Call to undefined function ezi18n()
[tinyz:tinyz.git] / kernel / shop / classes / exchangeratehandlers / ezecb / ezecbhandler.php
1 <?php
2 //
3 // Definition of eZECBHandler class
4 //
5 // Created on: <12-Mar-2006 13:06:15 dl>
6 //
7 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
8 // SOFTWARE NAME: eZ Publish
9 // SOFTWARE RELEASE: 4.1.x
10 // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
11 // SOFTWARE LICENSE: GNU General Public License v2.0
12 // NOTICE: >
13 //   This program is free software; you can redistribute it and/or
14 //   modify it under the terms of version 2.0  of the GNU General
15 //   Public License as published by the Free Software Foundation.
16 //
17 //   This program is distributed in the hope that it will be useful,
18 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 //   GNU General Public License for more details.
21 //
22 //   You should have received a copy of version 2.0 of the GNU General
23 //   Public License along with this program; if not, write to the Free
24 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 //   MA 02110-1301, USA.
26 //
27 //
28 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
29 //
30
31 /*! \file
32 */
33
34 class eZECBHandler extends eZExchangeRatesUpdateHandler
35 {
36     function eZECBHandler()
37     {
38         $this->ServerName = false;
39         $this->ServerPort = false;
40         $this->RatesURI = false;
41
42         eZExchangeRatesUpdateHandler::eZExchangeRatesUpdateHandler();
43     }
44
45     function initialize( $params = array() )
46     {
47         eZExchangeRatesUpdateHandler::initialize( $params );
48
49         $shopINI = eZINI::instance( 'shop.ini' );
50         if ( !isset( $params['ServerName'] ) )
51         {
52             $params['ServerName'] = '';
53             if ( $shopINI->hasVariable( 'ECBExchangeRatesSettings', 'ServerName' ) )
54                 $params['ServerName'] = $shopINI->variable( 'ECBExchangeRatesSettings', 'ServerName' );
55         }
56
57         if ( !isset( $params['ServerPort'] ) )
58         {
59             $params['ServerPort'] = '';
60             if ( $shopINI->hasVariable( 'ECBExchangeRatesSettings', 'ServerPort' ) )
61                 $params['ServerPort'] = $shopINI->variable( 'ECBExchangeRatesSettings', 'ServerPort' );
62         }
63
64         if ( !isset( $params['RatesURI'] ) )
65         {
66             $params['RatesURI'] = '';
67             if ( $shopINI->hasVariable( 'ECBExchangeRatesSettings', 'RatesURI' ) )
68                 $params['RatesURI'] = $shopINI->variable( 'ECBExchangeRatesSettings', 'RatesURI' );
69         }
70
71         if ( !isset( $params['BaseCurrency'] ) )
72         {
73             // the ECB returns currencies against 'EUR'
74             $params['BaseCurrency'] = 'EUR';
75         }
76
77         $this->setServerName( $params['ServerName'] );
78         $this->setServerPort( $params['ServerPort'] );
79         $this->setRatesURI( $params['RatesURI'] );
80         $this->setBaseCurrency( $params['BaseCurrency'] );
81     }
82
83     function requestRates()
84     {
85         $error = array( 'code' => self::OK,
86                         'description' => ezpI18n::translate( 'kernel/shop', "'Autorates' were retrieved successfully" ) );
87
88         $serverName = $this->serverName();
89         $serverPort = $this->serverPort();
90         $ratesURI = $this->ratesURI();
91
92         $ratesList = array();
93
94         $buf = eZHTTPTool::sendHTTPRequest( "{$serverName}/{$ratesURI}", $serverPort,  false, 'eZ Publish', false );
95         if ( $buf )
96         {
97             $header = false;
98             $body = false;
99
100             if ( eZHTTPTool::parseHTTPResponse( $buf, $header, $body ) )
101             {
102                 if ( $header['content-type'] === 'text/xml' )
103                 {
104                     // parse xml
105                     $dom = new DOMDocument( '1.0', 'utf-8' );
106                     $dom->preserveWhiteSpace = false;
107                     $success = $dom->loadXML( $body );
108
109                     $xpath = new DOMXPath( $dom );
110                     $xpath->registerNamespace( 'eurofxref', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref' );
111
112                     $rootNode = $dom->documentElement;
113                     $cubeNode = $xpath->query( 'eurofxref:Cube', $rootNode )->item( 0 );
114                     $timeNode = $cubeNode->firstChild;
115
116                     foreach ( $timeNode->childNodes as $currencyNode )
117                     {
118                         $currencyCode = $currencyNode->getAttribute( 'currency' );
119                         $rateValue = $currencyNode->getAttribute( 'rate' );
120                         $ratesList[$currencyCode] = $rateValue;
121                     }
122                 }
123                 else
124                 {
125                     $error['code'] = self::FAILED;
126                     $error['description'] = ezpI18n::translate( 'kernel/shop', "Unknown body format in HTTP response. Expected 'text/xml'" );
127                 }
128             }
129             else
130             {
131                 $error['code'] = self::FAILED;
132                 $error['description'] = ezpI18n::translate( 'kernel/shop', "Invalid HTTP response" );
133             }
134         }
135         else
136         {
137             $error['code'] = self::FAILED;
138             $error['description'] = ezpI18n::translate( 'kernel/shop', "Unable to send http request: %1:%2/%3", null, array( $serverName, $serverPort, $ratesURI ) );
139         }
140
141         $this->setRateList( $ratesList );
142         return $error;
143     }
144
145     function setServerName( $serverName )
146     {
147         $this->ServerName = $serverName;
148     }
149
150     function serverName()
151     {
152         return $this->ServerName;
153     }
154
155     function setServerPort( $serverPort )
156     {
157         $this->ServerPort = $serverPort;
158     }
159
160     function setRatesURI( $ratesURI )
161     {
162         $this->RatesURI = $ratesURI;
163     }
164
165     function serverPort()
166     {
167         return $this->ServerPort;
168     }
169
170     function ratesURI()
171     {
172         return $this->RatesURI;
173     }
174
175     public $ServerName;
176     public $ServerPort;
177     public $RatesURI;
178 }
179
180 ?>