These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @package Com_Localise |
||
4 | * @subpackage helper |
||
5 | * |
||
6 | * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
||
7 | * @license GNU General Public License version 2 or later; see LICENSE.txt |
||
8 | */ |
||
9 | |||
10 | defined('_JEXEC') or die; |
||
11 | |||
12 | /** |
||
13 | * Automatic translation class. |
||
14 | * |
||
15 | * @see http://msdn.microsoft.com/en-us/library/hh454950.aspx |
||
16 | * @since 1.0 |
||
17 | */ |
||
18 | Class HTTPTranslator |
||
19 | { |
||
20 | /** |
||
21 | * @var null |
||
22 | * @Todo: add description to this property |
||
23 | */ |
||
24 | protected $authheader = null; |
||
25 | |||
26 | /** |
||
27 | * Create and execute the HTTP CURL request. |
||
28 | * |
||
29 | * @param string $url HTTP Url |
||
30 | * @param string $authHeader Authorisation Header string |
||
31 | * @param string $postData Data to post |
||
32 | * |
||
33 | * @return mixed |
||
34 | * |
||
35 | * @throws Exception |
||
36 | */ |
||
37 | public function curlRequest($url, $authHeader, $postData = '') |
||
38 | { |
||
39 | // Initialize the Curl Session. |
||
40 | $ch = curl_init(); |
||
41 | |||
42 | // Set the Curl url. |
||
43 | curl_setopt($ch, CURLOPT_URL, $url); |
||
44 | |||
45 | // Set the HTTP HEADER Fields. |
||
46 | curl_setopt($ch, CURLOPT_HTTPHEADER, array($authHeader, "Content-Type: text/xml")); |
||
47 | |||
48 | // CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec(). |
||
49 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
50 | |||
51 | // CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate. |
||
52 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||
53 | |||
54 | if ($postData) |
||
55 | { |
||
56 | // Set HTTP POST Request. |
||
57 | curl_setopt($ch, CURLOPT_POST, true); |
||
58 | |||
59 | // Set data to POST in HTTP "POST" Operation. |
||
60 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); |
||
61 | } |
||
62 | |||
63 | // Execute the cURL session. |
||
64 | $curlResponse = curl_exec($ch); |
||
65 | |||
66 | // Get the Error Code returned by Curl. |
||
67 | $curlErrno = curl_errno($ch); |
||
68 | |||
69 | if ($curlErrno) |
||
70 | { |
||
71 | $curlError = curl_error($ch); |
||
72 | throw new Exception($curlError); |
||
73 | } |
||
74 | |||
75 | // Close a cURL session. |
||
76 | curl_close($ch); |
||
77 | |||
78 | return $curlResponse; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Get authentication header |
||
83 | * |
||
84 | * @param string $clientID Client id |
||
85 | * @param string $clientSecret Client pass |
||
86 | * |
||
87 | * @return bool|null|string |
||
88 | */ |
||
89 | protected function getAuthHeader($clientID, $clientSecret) |
||
90 | { |
||
91 | if (!is_null($this->authheader)) |
||
92 | { |
||
93 | return $this->authheader; |
||
94 | } |
||
95 | |||
96 | try |
||
97 | { |
||
98 | require __DIR__ . '/azuretoken.php'; |
||
99 | |||
100 | // OAuth Url. |
||
101 | $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; |
||
102 | |||
103 | // Application Scope Url |
||
104 | $scopeUrl = "http://api.microsofttranslator.com"; |
||
105 | |||
106 | // Application grant type |
||
107 | $grantType = "client_credentials"; |
||
108 | |||
109 | // Create the AccessTokenAuthentication object. |
||
110 | $authObj = new AccessTokenAuthentication; |
||
111 | |||
112 | // Get the Access token. |
||
113 | $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); |
||
114 | |||
115 | // Create the authorization Header string. |
||
116 | $this->authHeader = "Authorization: Bearer " . $accessToken; |
||
0 ignored issues
–
show
|
|||
117 | |||
118 | return $this->authHeader; |
||
0 ignored issues
–
show
The property
authHeader does not seem to exist. Did you mean authheader ?
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
119 | } |
||
120 | catch (Exception $e) |
||
121 | { |
||
122 | JError::raiseWarning('SOME_ERROR_CODE', $e->getMessage()); |
||
123 | |||
124 | return false; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Translate a string |
||
130 | * |
||
131 | * @param string $clientID Client ID of the application. |
||
132 | * @param string $clientSecret Client Secret key of the application. |
||
133 | * @param string $to Language into witch the string will be translated |
||
134 | * @param string $string Text to be translated |
||
135 | * @param string $from Original language of the passed string |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function translate($clientID, $clientSecret, $to, $string, $from = null) |
||
140 | { |
||
141 | $string = trim($string); |
||
142 | |||
143 | if (JString::strlen($string) < 1) |
||
144 | { |
||
145 | return ''; |
||
146 | } |
||
147 | |||
148 | try |
||
149 | { |
||
150 | $authHeader = $this->getAuthHeader($clientID, $clientSecret); |
||
151 | |||
152 | if (empty($from)) |
||
153 | { |
||
154 | // HTTP Detect Method URL. |
||
155 | $detectMethodUrl = "http://api.microsofttranslator.com/V2/Http.svc/Detect?text=" . urlencode($string); |
||
156 | |||
157 | // Call the curlRequest. |
||
158 | $strResponse = $this->curlRequest($detectMethodUrl, $authHeader); |
||
0 ignored issues
–
show
It seems like
$authHeader defined by $this->getAuthHeader($clientID, $clientSecret) on line 150 can also be of type false ; however, HTTPTranslator::curlRequest() does only seem to accept string , did you maybe forget to handle an error condition?
This check looks for type mismatches where the missing type is Consider the follow example <?php
function getDate($date)
{
if ($date !== null) {
return new DateTime($date);
}
return false;
}
This function either returns a new
Loading history...
|
|||
159 | |||
160 | // Interprets a string of XML into an object. |
||
161 | $xmlObj = simplexml_load_string($strResponse); |
||
162 | |||
163 | foreach ((array) $xmlObj[0] as $val) |
||
164 | { |
||
165 | $from = $val; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | $getTranslateurl = "http://api.microsofttranslator.com/V2/Http.svc/Translate?From=$from" |
||
170 | . "&To=$to&Text=" . urlencode($string); |
||
171 | $curlResponse = $this->curlRequest($getTranslateurl, $authHeader); |
||
0 ignored issues
–
show
It seems like
$authHeader defined by $this->getAuthHeader($clientID, $clientSecret) on line 150 can also be of type false ; however, HTTPTranslator::curlRequest() does only seem to accept string , did you maybe forget to handle an error condition?
This check looks for type mismatches where the missing type is Consider the follow example <?php
function getDate($date)
{
if ($date !== null) {
return new DateTime($date);
}
return false;
}
This function either returns a new
Loading history...
|
|||
172 | |||
173 | // Interprets a string of XML into an object. |
||
174 | $xmlObj = simplexml_load_string($curlResponse); |
||
175 | |||
176 | return (string) $xmlObj; |
||
177 | } |
||
178 | catch (Exception $e) |
||
179 | { |
||
180 | JError::raiseWarning('SOME_ERROR_CODE', $e->getMessage()); |
||
181 | |||
182 | return ''; |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.