Also apply the __toString fix to UserInfo
[cacert-mgr.git] / external / ZendFramework-1.9.5 / demos / Zend / Gdata / Health.php
1 <?php
2 /**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category Zend
16 * @package Zend_Gdata
17 * @subpackage Demos
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 */
21
22 //////////////////////////////////////////////////////////////////////////////
23 // Configuration: You must change these settings before running this sample //
24 //////////////////////////////////////////////////////////////////////////////
25
26 // Change this to point to the location of your private signing key. See:
27 // http://code.google.com/apis/health/getting_started.html#DomainRegistration
28 define('HEALTH_PRIVATE_KEY', '/path/to/your/rsa_private_key.pem');
29
30 //////////////////////////////////////////////////////////////////////////////
31 // End Configuration //
32 //////////////////////////////////////////////////////////////////////////////
33
34 // Load the Zend Gdata classes.
35 require_once 'Zend/Loader.php';
36 Zend_Loader::loadClass('Zend_Gdata_AuthSub');
37 Zend_Loader::loadClass('Zend_Gdata_Health');
38 Zend_Loader::loadClass('Zend_Gdata_Health_Query');
39
40 session_start();
41
42 // Google H9 Sandbox AuthSub/OAuth scope
43 define('SCOPE', 'https://www.google.com/h9/feeds/');
44
45 try {
46 // Setup the HTTP client and fetch an AuthSub token for H9
47 $client = authenticate(@$_GET['token']);
48 $useH9 = true;
49 $healthService = new Zend_Gdata_Health($client, 'google-HealthPHPSample-v1.0', $useH9);
50 } catch(Zend_Gdata_App_Exception $e) {
51 echo 'Error: ' . $e->getMessage();
52 }
53 ?>
54
55 <html>
56 <head>
57 <style>
58 body { margin:0; }
59 div { width:75%;margin-left:10px;padding:5px;font-family: "Courier New"; }
60 div#tokenstats { border-bottom:5px solid;background-color:#99ccff;margin:0 0 10px 0;padding:5px;width:100%; }
61 .code { margin:5px 0 5px 10px;background-color:#eee; }
62 div.data{ height:600px;border:1px solid;overflow:auto; }
63 </style>
64 </head>
65 <body>
66
67 <div id="tokenstats">
68 <b>Token info</b>: <?php echo getTokenInfo($client); ?><br>
69 <b>Session Token</b>: <?php echo $client->getAuthSubToken(); ?><br>
70 </div>
71
72 <?php
73 // =============================================================================
74 // Return the user's entire profile in a single atom <entry>
75 // =============================================================================
76 try {
77 $snippet = '
78 // =========================================================================
79 // Return the user\'s entire profile in a single atom <entry>
80 // =========================================================================
81
82 $query = new Zend_Gdata_Health_Query();
83 $query->setDigest("true");
84 $profileFeed = $healthService->getHealthProfileFeed($query);';
85
86 $snippet2 = '
87 $entries = $profileFeed->getEntries();
88 $ccr = $entries[0]->getCcr();
89 $xmlStr = $ccr->saveXML($ccr);
90 echo "<p>" . xmlpp($xmlStr) . "</p>";';
91 echo '<div class="code"><pre>' . htmlentities($snippet . $snippet2) . '</pre></div>';
92 eval($snippet);
93 } catch(Zend_Gdata_App_Exception $e) {
94 echo 'Error: ' . $e->getMessage();
95 }
96
97 echo '<div class="data"><pre>';
98 echo 'num entries: ' . count($profileFeed->getEntries());
99 eval($snippet2);
100 echo '</pre></div>';
101
102 // =============================================================================
103 // Return a user's medication from the entire CCR
104 // =============================================================================
105 try {
106 $snippet = '
107 // =========================================================================
108 // Return a user\'s medication for the entire CCR
109 // =========================================================================
110 $profileFeed = $healthService->getHealthProfileFeed();';
111
112 $snippet2 = '
113 foreach ($profileFeed->entry as $entry) {
114 $medications = $entry->getCcr()->getMedications();
115 foreach ($medications as $med) {
116 $xmlStr = $med->ownerDocument->saveXML($med);
117 echo "<p>" . xmlpp($xmlStr) . "</p>";
118 }
119 }';
120
121 } catch(Zend_Gdata_App_Exception $e) {
122 echo 'Error: ' . $e->getMessage();
123 }
124
125 echo '<div class="code"><pre>' . htmlentities($snippet . $snippet2) . '</pre></div>';
126 eval($snippet);
127
128 echo '<div class="data"><pre>';
129 eval($snippet2);
130 echo '</pre></div>';
131
132 // =============================================================================
133 // Category query: return a user's medication
134 // =============================================================================
135 try {
136 $snippet = '
137 // =========================================================================
138 // Category query: return a user\'s medication
139 // =========================================================================
140
141 $query = new Zend_Gdata_Health_Query(SCOPE . "profile/default");
142 $query->setCategory("medication");
143 $profileFeed = $healthService->getHealthProfileFeed($query);';
144
145 $snippet2 = '
146 foreach ($profileFeed->entry as $entry) {
147 $medications = $entry->getCcr()->getMedications();
148 foreach ($medications as $med) {
149 $xmlStr = $med->ownerDocument->saveXML($med);
150 echo "<p>" . xmlpp($xmlStr) . "</p>";
151 }
152 }';
153
154 } catch(Zend_Gdata_App_Exception $e) {
155 echo 'Error: ' . $e->getMessage();
156 }
157
158 echo '<div class="code"><pre>' . htmlentities($snippet . $snippet2) . '</pre></div>';
159 eval($snippet);
160
161 echo '<div class="data"><pre>';
162 echo 'query: ' . $query->getQueryUrl() . '<br>';
163 echo 'num entries: ' . count($profileFeed->getEntries());
164 eval($snippet2);
165 echo '</pre></div>';
166
167 // =============================================================================
168 // Query for a specific item within a category: allgergy A-Fil
169 // =============================================================================
170 try {
171 $snippet = '
172 // =========================================================================
173 // Query for a specific item within a category: allgergy A-Fil
174 // =========================================================================
175
176 $query = new Zend_Gdata_Health_Query(SCOPE . "profile/default");
177 $query->setCategory("allergy", "A-Fil");
178 $query->setGrouped("true");
179 $profileFeed = $healthService->getHealthProfileFeed($query);';
180
181 $snippet2 = '
182 foreach ($profileFeed->getEntries() as $entry) {
183 $allergies = $entry->getCcr()->getAllergies();
184 foreach ($allergies as $allergy) {
185 $xmlStr = $allergy->ownerDocument->saveXML($allergy);
186 echo "<p>" . xmlpp($xmlStr) . "</p>";
187 }
188 }';
189
190 } catch(Zend_Gdata_App_Exception $e) {
191 echo 'Error: ' . $e->getMessage();
192 }
193
194 echo '<div class="code"><pre>' . htmlentities($snippet . $snippet2) . '</pre></div>';
195 eval($snippet);
196
197 echo '<div class="data"><pre>';
198 echo 'query: ' . $query->getQueryUrl() . '<br>';
199 echo 'num entries: ' . count($profileFeed->getEntries());
200 eval($snippet2);
201 echo '</pre></div>';
202
203 // =============================================================================
204 // Query (and return) the user\'s medications OR conditions
205 // =============================================================================
206 try {
207 $snippet = '
208 // =========================================================================
209 // Query (and return) the user\'s medications OR conditions
210 // =========================================================================
211
212 $queryStr = SCOPE . "profile/default/-/medication%7Ccondition?digest=false";
213 $profileFeed = $healthService->getHealthProfileFeed($queryStr);';
214 $snippet2 = '
215 $entries = $profileFeed->getEntries();
216 foreach ($entries as $entry) {
217 $ccr = $entry->getCcr();
218 $xmlStr = $ccr->saveXML($ccr);
219 echo "<p>" . xmlpp($xmlStr) . "</p>";
220 }';
221
222 echo '<div class="code"><pre>' . htmlentities($snippet . $snippet2) . '</pre></div>';
223 eval($snippet);
224 } catch(Zend_Gdata_App_Exception $e) {
225 echo 'Error: ' . $e->getMessage();
226 }
227
228 echo '<div class="data"><pre>';
229 echo 'num entries: ' . count($profileFeed->getEntries());
230 eval($snippet2);
231 echo '</pre></div>';
232
233 // =============================================================================
234 // Send a notice to the user's profile that includes a CCR payload
235 // =============================================================================
236 try {
237 $snippet = '
238 // =========================================================================
239 // Send a notice to the user\'s profile that includes a CCR payload
240 // =========================================================================
241
242 $subject = "Title of your notice goes here";
243 $body = "Notice body can contain <b>html</b> entities";
244 $ccr = \'<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">
245 <Body>
246 <Problems>
247 <Problem>
248 <DateTime>
249 <Type><Text>Start date</Text></Type>
250 <ExactDateTime>2007-04-04T07:00:00Z</ExactDateTime>
251 </DateTime>
252 <Description>
253 <Text>Aortic valve disorders</Text>
254 <Code>
255 <Value>410.10</Value>
256 <CodingSystem>ICD9</CodingSystem>
257 <Version>2004</Version>
258 </Code>
259 </Description>
260 <Status><Text>Active</Text></Status>
261 </Problem>
262 </Problems>
263 </Body>
264 </ContinuityOfCareRecord>\';
265 $responseEntry = $healthService->sendHealthNotice($subject, $body, "html", $ccr);';
266 } catch(Zend_Gdata_App_Exception $e) {
267 echo 'Error: ' . $e->getMessage();
268 }
269
270 echo '<div class="code"><pre>' . htmlentities($snippet) . '</pre></div>';
271 eval($snippet);
272
273 echo '<div class="data"><pre>';
274 echo xmlpp($responseEntry->getXML());
275 echo '</pre></div>';
276
277 // =============================================================================
278 // Revoke the AuthSub session token
279 // =============================================================================
280 //$revoked = Zend_Gdata_AuthSub::AuthSubRevokeToken($client->getAuthSubToken(), $client) ? 'yes' : 'no';
281 //echo '<b>Token revoked</b>: ' . @$revoked;
282 //unset($_SESSION['sessionToken']);
283 ?>
284 </body>
285 </html>
286
287 <?php
288 function getCurrentUrl() {
289 $phpRequestUri =
290 htmlentities(substr($_SERVER['REQUEST_URI'], 0, strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);
291
292 if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
293 $protocol = 'https://';
294 } else {
295 $protocol = 'http://';
296 }
297 $host = $_SERVER['HTTP_HOST'];
298 if ($_SERVER['SERVER_PORT'] != '' &&
299 (($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
300 ($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) {
301 $port = ':' . $_SERVER['SERVER_PORT'];
302 } else {
303 $port = '';
304 }
305 return $protocol . $host . $port . $phpRequestUri;
306 }
307
308 function authenticate($singleUseToken=null) {
309 $sessionToken = isset($_SESSION['sessionToken']) ? $_SESSION['sessionToken'] : null;
310
311 // If there is no AuthSub session or one-time token waiting for us,
312 // redirect the user to Google Health's AuthSub handler to get one.
313 if (!$sessionToken && !$singleUseToken) {
314 $next = getCurrentUrl();
315 $secure = 1;
316 $session = 1;
317 $authSubHandler = 'https://www.google.com/h9/authsub';
318 $permission = 1; // 1 - allows reading of the profile && posting notices
319 $authSubURL =
320 Zend_Gdata_AuthSub::getAuthSubTokenUri($next, SCOPE, $secure, $session,
321 $authSubHandler);
322 $authSubURL .= '&permission=' . $permission;
323 echo '<a href="' . $authSubURL . '">Link your Google Health Account</a>';
324 exit();
325 }
326
327 $client = new Zend_Gdata_HttpClient();
328 $client->setAuthSubPrivateKeyFile(HEALTH_PRIVATE_KEY, null, true);
329
330 // Convert an AuthSub one-time token into a session token if needed
331 if ($singleUseToken && !$sessionToken) {
332 $sessionToken =
333 Zend_Gdata_AuthSub::getAuthSubSessionToken($singleUseToken, $client);
334 $_SESSION['sessionToken'] = $sessionToken;
335 }
336 $client->setAuthSubToken($sessionToken);
337 return $client;
338 }
339
340 function getTokenInfo($client) {
341 $sessionToken = $client->getAuthSubToken();
342 return Zend_Gdata_AuthSub::getAuthSubTokenInfo($sessionToken, $client);
343 }
344
345 function revokeToken($client) {
346 $sessionToken = $client->getAuthSubToken();
347 return Zend_Gdata_AuthSub::AuthSubRevokeToken($sessionToken, $client);
348 }
349
350 /** Prettifies an XML string into a human-readable and indented work of art
351 * @param string $xml The XML as a string
352 * @param boolean $html_output True if the output should be escaped (for use in HTML)
353 */
354 function xmlpp($xml, $html_output=true) {
355 $xml_obj = new SimpleXMLElement($xml);
356 $level = 4;
357 $indent = 0; // current indentation level
358 $pretty = array();
359
360 // get an array containing each XML element
361 $xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));
362
363 // shift off opening XML tag if present
364 if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
365 $pretty[] = array_shift($xml);
366 }
367
368 foreach ($xml as $el) {
369 if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
370 // opening tag, increase indent
371 $pretty[] = str_repeat(' ', $indent) . $el;
372 $indent += $level;
373 } else {
374 if (preg_match('/^<\/.+>$/', $el)) {
375 $indent -= $level; // closing tag, decrease indent
376 }
377 if ($indent < 0) {
378 $indent += $level;
379 }
380 $pretty[] = str_repeat(' ', $indent) . $el;
381 }
382 }
383 $xml = implode("\n", $pretty);
384 return ($html_output) ? htmlentities($xml) : $xml;
385 }
386 ?>