PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/mem_openid/mem_openid.php

https://bitbucket.org/Manfre/txp-plugins
PHP | 330 lines | 243 code | 64 blank | 23 comment | 31 complexity | d57bc949f5b1b38ebea44e02fd62f513 MD5 | raw file
  1. <?php
  2. // This is a PLUGIN TEMPLATE.
  3. // Copy this file to a new name like abc_myplugin.php. Edit the code, then
  4. // run this file at the command line to produce a plugin for distribution:
  5. // $ php abc_myplugin.php > abc_myplugin-0.1.txt
  6. // Plugin name is optional. If unset, it will be extracted from the current
  7. // file name. Uncomment and edit this line to override:
  8. $plugin['name'] = 'mem_openid';
  9. // 0 = Plugin help is in Textile format, no raw HTML allowed (default).
  10. // 1 = Plugin help is in raw HTML. Not recommended.
  11. # $plugin['allow_html_help'] = 1;
  12. $plugin['version'] = '0.1';
  13. $plugin['author'] = 'Michael Manfre';
  14. $plugin['author_uri'] = 'http://manfre.net/';
  15. $plugin['description'] = 'OpenID class';
  16. // Plugin types:
  17. // 0 = regular plugin; loaded on the public web side only
  18. // 1 = admin plugin; loaded on both the public and admin side
  19. // 2 = library; loaded only when include_plugin() or require_plugin() is called
  20. $plugin['type'] = 2;
  21. if (!defined('txpinterface'))
  22. @include_once('../zem_tpl.php');
  23. if (0) {
  24. ?>
  25. # --- BEGIN PLUGIN HELP ---
  26. h1(title). OpenID Library Plugin
  27. p. This plugin functions as a wrapper to the JanRain OpenID PHP library.
  28. # --- END PLUGIN HELP ---
  29. <?php
  30. }
  31. # --- BEGIN PLUGIN CODE ---
  32. define('Auth_OpenID_BUGGY_GMP', false);
  33. define('Auth_OpenID_RAND_SOURCE', NULL);
  34. if (session_id() == "") session_start();
  35. global $mem_openid_lang, $sitepath, $prefs;
  36. // Path to the JanRain OpenID Auth folder
  37. if (!isset($prefs['mem_openid_lib_path'])) {
  38. $path = txpath.DS.'Auth';
  39. set_pref('mem_openid_lib_path', doSlash($path), 'openid', 1);
  40. $prefs['mem_openid_lib_path'] = $mem_openid_lib_path = $path;
  41. unset($path);
  42. }
  43. // Path to the JanRain OpenID File Store folder
  44. if (!isset($prefs['mem_openid_file_store_path'])) {
  45. $path = txpath.DS.'tmp'.DS.'openid_store';
  46. set_pref('mem_openid_file_store_path', doSlash($path), 'openid', 1);
  47. $prefs['mem_openid_file_store_path'] = $mem_openid_file_store_path = $path;
  48. unset($path);
  49. }
  50. if (file_exists($prefs['mem_openid_lib_path']))
  51. {
  52. $path = ini_get('include_path');
  53. $path = $prefs['mem_openid_lib_path'] . PATH_SEPARATOR . $path;
  54. ini_set('include_path', $path);
  55. require_once "Auth/OpenID/Consumer.php";
  56. require_once "Auth/OpenID/FileStore.php";
  57. require_once "Auth/OpenID/SReg.php";
  58. require_once "Auth/OpenID/PAPE.php";
  59. }
  60. if (!is_array($mem_openid_lang))
  61. {
  62. $mem_openid_lang = array(
  63. 'no_consumer' => 'No consumer',
  64. 'redirect_failed' => 'Failed to redirect. {msg}',
  65. 'invalid_openid' => 'OpenID "{id}" is invalid.',
  66. 'auth_cancel' => 'Authentication attempt was cancelled.',
  67. 'auth_failure' => 'Authentication request failed.',
  68. 'no_response' => 'No response',
  69. 'mkdir_failed' => 'Failed to create OpenID file store "{path}"',
  70. );
  71. }
  72. register_callback( 'mem_openid_enumerate_strings' , 'l10n.enumerate_strings' );
  73. function mem_openid_enumerate_strings($event , $step='' , $pre=0)
  74. {
  75. global $mem_openid_lang;
  76. $r = array (
  77. 'owner' => 'mem_openid', # Change to your plugin's name
  78. 'prefix' => 'mem_openid', # Its unique string prefix
  79. 'lang' => 'en-gb', # The language of the initial strings.
  80. 'event' => 'public', # public/admin/common = which interface the strings will be loaded into
  81. 'strings' => $mem_openid_lang, # The strings themselves.
  82. );
  83. return $r;
  84. }
  85. function mem_openid_gTxt($what,$args = array())
  86. {
  87. global $mem_openid_lang, $textarray;
  88. $key = strtolower( 'mem_openid' . '-' . $what );
  89. if (isset($textarray[$key]))
  90. $str = $textarray[$key];
  91. else
  92. {
  93. $key = strtolower($what);
  94. if (isset($mem_openid_lang[$key]))
  95. $str = $mem_openid_lang[$key];
  96. elseif (isset($textarray[$key]))
  97. $str = $textarray[$key];
  98. else
  99. $str = $what;
  100. }
  101. if( !empty($args) )
  102. $str = strtr( $str , $args );
  103. return $str;
  104. }
  105. class memOpenID
  106. {
  107. static $store = NULL;
  108. static $consumer = NULL;
  109. var $error;
  110. var $openid;
  111. var $trust_root;
  112. var $return_url;
  113. var $sreg_required = array();
  114. var $sreg_optional = array();
  115. var $policy_uris = array();
  116. var $auth_request = NULL;
  117. function memOpenID($openid, $trust_root = '', $return_url = '')
  118. {
  119. // preload the consumer
  120. self::get_consumer();
  121. $this->openid = $openid;
  122. $this->trust_root = $trust_root;
  123. $this->return_url = $return_url;
  124. $this->error = false;
  125. }
  126. function add_required()
  127. {
  128. $args = func_get_args();
  129. $this->sreg_required = array_merge($this->sreg_required, array_values($args));
  130. }
  131. function add_optional()
  132. {
  133. $args = func_get_args();
  134. $this->sreg_optional = array_merge($this->sreg_optional, array_values($args));
  135. }
  136. function add_policy_uri()
  137. {
  138. $args = func_get_args();
  139. $this->policy_uris = array_merge($this->policy_uris, array_values($args));
  140. }
  141. function is_response()
  142. {
  143. $nonce = gps('janrain_nonce');
  144. return !empty($nonce);
  145. }
  146. function authenticate()
  147. {
  148. if (!($c = self::get_consumer()))
  149. {
  150. $this->error = mem_openid_gTxt('no_consumer');
  151. return false;
  152. }
  153. $this->auth_request = $c->begin($this->openid);
  154. if ($this->auth_request)
  155. {
  156. $this->sreg_request = Auth_OpenID_SRegRequest::build($this->sreg_required, $this->sreg_optional);
  157. if ($this->sreg_request)
  158. $this->auth_request->addExtension($this->sreg_request);
  159. if (!empty($this->policy_uris))
  160. {
  161. if ( ($pape = new Auth_OpenID_PAPE_Request($this->policy_uris)) )
  162. $this->auth_request->addExtension($pape);
  163. }
  164. if ($this->auth_request->shouldSendRedirect())
  165. {
  166. $redirect_url = $this->auth_request->redirectURL( $this->trust_root, $this->return_url );
  167. if (Auth_OpenID::isFailure($redirect_url))
  168. {
  169. $this->error = mem_openid_gTxt('redirect_failed', array('{msg}'=> $redirect_url->message));
  170. }
  171. else
  172. {
  173. while (@ob_end_clean());
  174. header("Location: ".$redirect_url);
  175. return true;
  176. }
  177. }
  178. else
  179. {
  180. $form_id = 'openid_message';
  181. $form_html = $this->auth_request->formMarkup($this->trust_root, $this->return_url,
  182. false, array('id' => $form_id));
  183. // Display an error if the form markup couldn't be generated;
  184. // otherwise, render the HTML.
  185. if (Auth_OpenID::isFailure($form_html)) {
  186. $this->error = mem_openid_gTxt('redirect_failed', array('{msg}'=> $form_html->message));
  187. } else {
  188. $page_contents = array(
  189. "<html><head><title>",
  190. "OpenID transaction in progress",
  191. "</title></head>",
  192. "<body onload='document.getElementById(\"".$form_id."\").submit()'>",
  193. $form_html,
  194. "</body></html>");
  195. print implode("\n", $page_contents);
  196. return true;
  197. }
  198. }
  199. }
  200. else
  201. {
  202. // invalid openid
  203. $this->error = mem_openid_gTxt('invalid_openid', array('{id}'=> $this->openid));
  204. }
  205. return false;
  206. }
  207. function complete()
  208. {
  209. if (!($c = self::get_consumer()))
  210. {
  211. $this->error = mem_openid_gTxt('no_consumer');
  212. return false;
  213. }
  214. if ($this->is_response())
  215. {
  216. $resp = $c->complete($this->return_url);
  217. switch ($resp->status)
  218. {
  219. case Auth_OpenID_CANCEL:
  220. $this->error = mem_openid_gTxt('auth_cancel');
  221. break;
  222. case Auth_OpenID_FAILURE:
  223. $this->error = mem_openid_gTxt('auth_failure');
  224. break;
  225. case Auth_OpenID_SUCCESS:
  226. $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($resp);
  227. return $sreg_resp->contents();
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. else
  234. {
  235. $this->error = mem_openid_gTxt('no_response');
  236. }
  237. return false;
  238. }
  239. static function &get_store()
  240. {
  241. global $mem_openid_file_store_path;
  242. if (self::$store == NULL)
  243. {
  244. if (!file_exists($mem_openid_file_store_path) && !mkdir($mem_openid_file_store_path))
  245. {
  246. trigger_error( mem_openid_gTxt('mkdir_failed', array('{path}'=> $mem_openid_file_store_path)), E_USER_WARNING );
  247. }
  248. else
  249. {
  250. self::$store = new Auth_OpenID_FileStore($mem_openid_file_store_path);
  251. }
  252. }
  253. return self::$store;
  254. }
  255. static function &get_consumer()
  256. {
  257. $s = self::get_store();
  258. if ($s != NULL)
  259. {
  260. self::$consumer = new Auth_OpenID_Consumer($s);
  261. }
  262. return self::$consumer;
  263. }
  264. }
  265. # --- END PLUGIN CODE ---
  266. ?>