PageRenderTime 74ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/php/janrain-engage-php-sdk-0.1/console/index.php

https://github.com/chadisel/Janrain-Sample-Code
PHP | 467 lines | 438 code | 24 blank | 5 comment | 57 complexity | 811ad72d26bd04bd02632a01d339a9e6 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011
  4. * Janrain Inc.
  5. * All rights reserved.
  6. */
  7. ob_start();
  8. $the_error = '';
  9. if (file_exists('console.conf.php')) {
  10. require_once('console.conf.php');
  11. } else {
  12. $the_error .= 'console.conf.php not found';
  13. }
  14. if (file_exists('../library/engage.lib.php')) {
  15. require_once('../library/engage.lib.php');
  16. } elseif (file_exists('engage.lib.php')) {
  17. require_once('engage.lib.php');
  18. } else {
  19. $the_error .= 'engage.lib.php not found';
  20. }
  21. if (file_exists('index.inc.php')) {
  22. require_once('index.inc.php');
  23. } else {
  24. $the_error .= 'index.inc.php not found';
  25. }
  26. $site_domain = $_SERVER['SERVER_NAME'];
  27. $base_request = str_ireplace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
  28. $base_url = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$base_request;
  29. $current_url = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
  30. $token_url = $current_url;
  31. $forty_stars = '****************************************';
  32. if (ENGAGE_CONSOLE_API_KEY != ''){
  33. $api_key_default = $forty_stars;
  34. } else {
  35. $api_key_default = '';
  36. }
  37. $action_map = array();
  38. $action_map['auth_info'] = array(
  39. 'app_dom' => array('required'=>true, 'default'=>ENGAGE_CONSOLE_APP_DOM),
  40. 'token' => array('required'=>true, 'default'=>''),
  41. 'api_key' => array('required'=>true, 'default'=>$api_key_default),/* Fill in your API key in the console.conf.php. */
  42. 'format' => array('required'=>true, 'default'=>'json'),
  43. 'extended'=> array('required'=>false, 'default'=>'')
  44. );
  45. $info_steps = array();
  46. $info_steps['one'] = array (
  47. 'title' => 'step_one_title',
  48. 'info' => 'step_one_instructions',
  49. 'trigger' => 'format'
  50. );
  51. $info_steps['two'] = array (
  52. 'title' => 'step_two_title',
  53. 'info' => 'step_two_instructions',
  54. 'trigger' => 'app_dom'
  55. );
  56. $info_steps['three'] = array (
  57. 'title' => 'step_three_title',
  58. 'info' => 'step_three_instructions',
  59. 'trigger' => 'token'
  60. );
  61. $info_steps['four'] = array (
  62. 'title' => 'step_four_title',
  63. 'info' => 'step_four_instructions',
  64. 'trigger' => 'api_key'
  65. );
  66. $info_steps['five'] = array (
  67. 'title' => 'step_five_title',
  68. 'info' => 'step_five_instructions',
  69. 'trigger' => 'identifier'
  70. );
  71. $actions = array();
  72. foreach ($action_map as $action=>$action_def) {
  73. $do = true;
  74. foreach ($action_def as $val_name=>$properties) {
  75. $val = $properties['default'];
  76. if (isset($_REQUEST[$val_name])) {
  77. $val = trim(strip_tags($_REQUEST[$val_name]));
  78. }
  79. if (!isset($actions[$action])) {
  80. $actions[$action] = array();
  81. }
  82. if ($properties['required'] === true && empty($val)){
  83. $val = '';
  84. $do = false;
  85. }
  86. $actions[$action][$val_name] = $val;
  87. }
  88. $actions[$action]['do'] = $do;
  89. }
  90. $clean = true;
  91. reset($actions);
  92. while (list($action, $vals) = each($actions)) {
  93. if ($vals['do'] === true) {
  94. $actions[$action]['do'] = false;
  95. switch ($action) {
  96. case 'auth_info':
  97. $extended = false;
  98. if ($vals['extended'] == 'true') {
  99. $extended = true;
  100. }
  101. if ( $vals['api_key'] == $forty_stars && ENGAGE_CONSOLE_API_KEY != '' ) {
  102. $vals['api_key'] = ENGAGE_CONSOLE_API_KEY;
  103. }
  104. $result = engage_auth_info($vals['api_key'], $vals['token'], $vals['format'], $extended);
  105. if ($result === false) {
  106. $clean = false;
  107. }else{
  108. $actions['parse_result'] = array(
  109. 'result' => $result,
  110. 'format' => $vals['format'],
  111. 'export' => array('identifier' => array('profile','identifier')),
  112. 'do' => true
  113. );
  114. if ($vals['format'] == 'json') {
  115. $actions['indent_json'] = array(
  116. 'json' => $result,
  117. 'do' => true
  118. );
  119. }
  120. if ($vals['format'] == 'xml') {
  121. $actions['indent_xml'] = array(
  122. 'xml' => $result,
  123. 'do' => true
  124. );
  125. }
  126. }
  127. break;
  128. case 'parse_result':
  129. $parse_result = engage_parse_result($vals['result'], $vals['format']);
  130. if ($parse_result === false) {
  131. $clean = false;
  132. } else {
  133. if (is_array($parse_result)) {
  134. if (isset($parse_result['err'])) {
  135. $engage_error = true;
  136. } elseif (is_array($vals['export'])) {
  137. foreach ($vals['export'] as $export_name => $export_path) {
  138. $export_val = '';
  139. foreach ($export_path as $e_key => $e_path) {
  140. if (empty($export_path)) {
  141. $export_val = $parse_result["$e_path"];
  142. } else {
  143. $export_val = $export_val["$e_path"];
  144. }
  145. }
  146. $$export_name = $export_val = '';
  147. }
  148. }
  149. } elseif (is_object($parse_result)) {
  150. if ($parse_result->err != '') {
  151. $engage_error = true;
  152. } elseif (is_array($vals['export'])) {
  153. foreach ($vals['export'] as $export_name => $export_path) {
  154. $export_val = '';
  155. foreach ($export_path as $e_key => $e_path) {
  156. if (empty($export_path)) {
  157. $export_val = $parse_result->$e_path;
  158. } else {
  159. $export_val = $export_val->$e_path;
  160. }
  161. }
  162. $$export_name = $export_val = '';
  163. }
  164. }
  165. }
  166. $actions['parse_dump'] = array(
  167. 'data' => $parse_result,
  168. 'do' => true
  169. );
  170. }
  171. break;
  172. case 'indent_json':
  173. $indent_json = indent_json($vals['json']);
  174. $actions['raw_dump'] = array(
  175. 'data' => $indent_json,
  176. 'do' => true
  177. );
  178. break;
  179. case 'indent_xml':
  180. $indent_xml = indent_xml($vals['xml']);
  181. $actions['raw_dump'] = array(
  182. 'data' => $indent_xml,
  183. 'do' => true
  184. );
  185. break;
  186. case 'raw_dump':
  187. $the_raw_result = $vals['data'];
  188. break;
  189. case 'parse_dump':
  190. $the_parse_result = print_r($vals['data'], true);
  191. break;
  192. }
  193. }
  194. if ($clean === false) {
  195. $the_errors = engage_get_errors();
  196. if ($the_errors === false) {
  197. $the_errors = array('unknown_error'=>'error');
  198. }
  199. }
  200. }
  201. $style = '';
  202. if (!isset($the_raw_result)) {
  203. $the_raw_result = '';
  204. }
  205. if(!empty($the_raw_result)) {
  206. $style .= ' #raw_results_wrapper {
  207. display:block;
  208. }
  209. ';
  210. }
  211. if (!isset($the_parse_result)) {
  212. $the_result = '';
  213. }
  214. if(!empty($the_parse_result)) {
  215. $style .= ' #parse_results_wrapper {
  216. display:block;
  217. }
  218. ';
  219. }
  220. if (!isset($the_error)) {
  221. $the_error = '';
  222. }
  223. if (isset($the_errors)) {
  224. foreach ($the_errors as $error_msg=>$label) {
  225. $the_error .= "$error_msg" . "\n";
  226. }
  227. }
  228. if (!empty($the_error)) {
  229. $style .= ' #the_errors {
  230. display:block;
  231. }
  232. ';
  233. }
  234. $step_style = '';
  235. $step_missed = false;
  236. foreach ($info_steps as $info_step=>$info_vals) {
  237. if ( !empty($actions['auth_info'][$info_vals['trigger']]) && $step_missed === false){
  238. $step_style =
  239. ' #instructions #'.$info_vals['info'].' {
  240. width:780px;
  241. height:75px;
  242. border:0px;
  243. top:26px;
  244. left:0px;
  245. background-color:#FFF;
  246. font-size:13px;
  247. display:block;
  248. }
  249. #instructions #'.$info_vals['title'].' {
  250. color:#000;
  251. background-color:#FFF;
  252. }
  253. ';
  254. } else {
  255. $step_missed = true;
  256. }
  257. }
  258. if ($engage_error === true || !empty($the_error)){
  259. $step_style =
  260. ' #instructions #step_error_instructions {
  261. width:780px;
  262. height:75px;
  263. border:0px;
  264. top:26px;
  265. left:0px;
  266. background-color:#FEE;
  267. font-size:13px;
  268. display:block;
  269. }
  270. #instructions #step_error_title {
  271. display:block;
  272. color:#000;
  273. background-color:#FEE;
  274. }
  275. ';
  276. }
  277. $style .= $step_style;
  278. ob_end_clean();
  279. ob_start();
  280. ?>
  281. <!DOCTYPE html>
  282. <html>
  283. <head>
  284. <meta charset="UTF-8" />
  285. <title>Janrain Engage API console</title>
  286. <link rel="stylesheet" type="text/css" media="screen" href="style.css" />
  287. <style type="text/css">
  288. <?php echo $style; ?>
  289. </style>
  290. </head>
  291. <body>
  292. <div id="toolkit_wrapper">
  293. <div id="main_toolkit">
  294. <div id="page_title">
  295. Janrain Engage API console
  296. </div>
  297. <div id="top_ui">
  298. <div id="api_menu">
  299. <ul id="api_menu_list">
  300. <li id="menu_auth_info">auth_info</ol>
  301. </li>
  302. </ul>
  303. </div>
  304. <div id="instructions_wrapper">
  305. <div id="instructions">
  306. <div id="step_one" class="instruction_step">
  307. <h3 id="step_one_title" class="instruction_title">Step One&nbsp;-&gt;</h3>
  308. <div id="step_one_instructions" class="instruction">
  309. Copy and paste your Application Domain from your <a target="_blank" href="https://rpxnow.com/">Engage dashboard.</a><br />
  310. The domain should be a full URL. (e.g. https://myapp.rpxnow.com/) Click submit after pasting the URL.<br />
  311. <form id="app_domain_form" method="get">
  312. <label for="app_domain_input">Application Domain</label>
  313. <input id="app_domain_input" type="text" name="app_dom" value="<?php echo htmlentities($actions['auth_info']['app_dom']); ?>" />
  314. <input id="app_domain_submit" type="submit" name="phpapi" value="submit" />
  315. </form>
  316. </div>
  317. </div>
  318. <div id="step_two" class="instruction_step">
  319. <h3 id="step_two_title" class="instruction_title">Step Two&nbsp;-&gt;</h3>
  320. <div id="step_two_instructions" class="instruction">
  321. <div id="whitelist_note" class="note">
  322. You may need to whitelist your current domain. <br />
  323. <a target="_blank" href="https://rpxnow.com/">Engage dashboard</a>-&gt;Settings-&gt;<br />
  324. &nbsp;add&nbsp;"<?php echo htmlentities($site_domain); ?>" to "Token URL Domains"
  325. </div>
  326. Click <a class="rpxnow" onclick="return false;"
  327. href="<?php echo htmlentities($actions['auth_info']['app_dom']); ?>openid/v2/signin?token_url=<?php echo urlencode($token_url); ?>"> Sign In </a><br />
  328. This will fill in the token.<br />
  329. The token is one time use by default.
  330. </div>
  331. </div>
  332. <div id="step_three" class="instruction_step">
  333. <h3 id="step_three_title" class="instruction_title">Step Three&nbsp;-&gt;</h3>
  334. <div id="step_three_instructions" class="instruction">
  335. Copy your API key from your <a target="_blank" href="https://rpxnow.com/">Engage dashboard.</a><br />
  336. Paste the API key in to the apiKey field.<br />
  337. Select format and extended(Plus/Pro/Enterprise only) options.
  338. Click submit.
  339. </div>
  340. </div>
  341. <div id="step_four" class="instruction_step">
  342. <h3 id="step_four_title" class="instruction_title">Step Four&nbsp;&nbsp;&nbsp;</h3>
  343. <div id="step_four_instructions" class="instruction">
  344. You have completed the auth_info API call.<br />
  345. This is the point where your code would begin.<br />
  346. <a href="<?php echo htmlentities($current_url); ?>">Start over</a>
  347. </div>
  348. </div>
  349. <div id="step_error" class="instruction_step">
  350. <h3 id="step_error_title" class="instruction_title">Oops!&nbsp;&nbsp;&nbsp;</h3>
  351. <div id="step_error_instructions" class="instruction">
  352. Make sure your token URL domain is in your token URL domain list.<br />
  353. Remember that tokens are one-time use.<br />
  354. Reusing a token can result in "Data not found".<br />
  355. <a href="<?php echo htmlentities($base_url); ?>">Start over</a>.
  356. </div>
  357. </div>
  358. </div>
  359. </div>
  360. </div>
  361. <div class="instruction">
  362. <div id="step_five" class="instruction_step">
  363. <h3 id="step_five_title" class="instruction_title">Indentifier Collected</h3>
  364. <div id="step_five_instructions" class="instruction">
  365. Hi <?php echo $identifier; ?>
  366. </div>
  367. </div>
  368. </div>
  369. <div id="main_form_wrapper">
  370. <form id="main_form" method="post">
  371. <label for="token">token</label>
  372. <input id="token" type="text" name="token" value="<?php echo htmlentities($actions['auth_info']['token']); ?>" />
  373. <label for="api_key">apiKey</label>
  374. <input id="api_key" type="text" name="api_key" value="<?php echo htmlentities($actions['auth_info']['api_key']); ?>" />
  375. <label for="format">format</label>
  376. <select id="format" name="format">
  377. <?php
  378. $select_json = '';
  379. $select_xml = '';
  380. $selected = ' selected="selected"';
  381. if ($actions['auth_info']['format'] == 'json') {
  382. $select_json = $selected;
  383. } elseif ($actions['auth_info']['format'] == 'xml') {
  384. $select_xml = $selected;
  385. }
  386. ?>
  387. <option value="json"<?php echo $select_json; ?>>json</option>
  388. <option value="xml"<?php echo $select_xml; ?>>xml</option>
  389. </select>
  390. <label for="extended">extended</label>
  391. <input id="extended" type="checkbox" name="extended" value="true"<?php
  392. if ($actions['auth_info']['extended'] == 'true') { echo ' checked="checked"'; } ?> />
  393. <input type="submit" name="go" value="submit" />
  394. <div id="api_sections">
  395. <div id="auth_info_section">
  396. <div id="auth_info_instructions">
  397. </div>
  398. <div id="auth_info_options">
  399. </div>
  400. </div>
  401. </div>
  402. </form>
  403. </div>
  404. <div id="output_wrapper">
  405. <div id="results_wrapper">
  406. <form id="results_form">
  407. <div id="raw_results_wrapper">
  408. <div id="raw_results_title">The response:</div>
  409. <div id="the_raw_results" class="results"><pre>
  410. <?php echo htmlentities($the_raw_result); ?>
  411. </pre></div>
  412. </div>
  413. <div id="parse_results_wrapper">
  414. <div id="parse_results_title">The parsed (as an array) response:</div>
  415. <div id="the_parse_results" class="results"><pre>
  416. <?php echo htmlentities($the_parse_result); ?>
  417. </pre></div>
  418. </div>
  419. </form>
  420. </div>
  421. <div id="error_wrapper">
  422. <form id="error_form">
  423. <div id="the_errors" class="results"><pre>
  424. <?php echo htmlentities($the_error); ?>
  425. </pre></div>
  426. </form>
  427. </div>
  428. </div>
  429. </div>
  430. </div>
  431. <script type="text/javascript">
  432. var rpxJsHost = (("https:" == document.location.protocol) ? "https://" : "http://static.");
  433. document.write(unescape("%3Cscript src='" + rpxJsHost +
  434. "rpxnow.com/js/lib/rpx.js' type='text/javascript'%3E%3C/script%3E"));
  435. </script>
  436. <script type="text/javascript">
  437. RPXNOW.overlay = true;
  438. RPXNOW.language_preference = 'en';
  439. </script>
  440. </body>
  441. </html><?php
  442. ob_end_flush();
  443. ?>