PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/elgg/mod/openid_server/openid_server_include.php

https://bitbucket.org/rhizomatik/lorea_production/
PHP | 347 lines | 268 code | 61 blank | 18 comment | 38 complexity | fc5b51fb29f0e12fbdfc5c96c6b7a6bd MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * An Elgg 1.x compatible store implementation
  4. */
  5. /**
  6. * Require base class for creating a new interface.
  7. */
  8. require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");
  9. require_once 'Auth/OpenID.php';
  10. require_once 'Auth/OpenID/Interface.php';
  11. require_once 'Auth/OpenID/Consumer.php';
  12. try {
  13. include_once "Auth/OpenID/HMACSHA1.php";
  14. } catch(Exception $e) {
  15. // new way :P
  16. require_once "Auth/OpenID/HMAC.php";
  17. }
  18. require_once 'Auth/OpenID/Nonce.php';
  19. require_once 'Auth/OpenID/SReg.php';
  20. define('header_connection_close', 'Connection: close');
  21. function openid_server_delete_entity($entity)
  22. {
  23. global $CONFIG;
  24. $entity->clearMetadata();
  25. $entity->clearAnnotations();
  26. $guid = $entity->getGUID();
  27. delete_data("DELETE from {$CONFIG->dbprefix}entities where guid={$guid}");
  28. }
  29. function openid_server_delete_entities($type = "", $subtype = "", $owner_guid = 0)
  30. {
  31. $entities = get_entities($type, $subtype, $owner_guid, "time_created desc", 0);
  32. foreach ($entities as $entity) {
  33. openid_server_delete_entity($entity);
  34. }
  35. return true;
  36. }
  37. class OpenIDServer_ElggStore extends Auth_OpenID_OpenIDStore {
  38. function resetAssociations () {
  39. openid_server_delete_entities('object', 'openid_client::association');
  40. }
  41. function resetNonces () {
  42. openid_server_delete_entities('object', 'openid_client::nonce');
  43. }
  44. function getAssociation ($server_url, $handle = null) {
  45. if (isset($handle)) {
  46. $meta_array = array(
  47. 'server_url' => $server_url,
  48. 'handle' => $handle
  49. );
  50. $assocs = get_entities_from_metadata_multi($meta_array, 'object', 'openid_client::association');
  51. } else {
  52. $assocs = get_entities_from_metadata('server_url', $server_url, 'object','openid_client::association');
  53. }
  54. if (!$assocs || (count($assocs) == 0)) {
  55. error_log("in getAssociations - cannot get associations for server url: $server_url, handle: $handle");
  56. return null;
  57. } else {
  58. $associations = array();
  59. foreach ($assocs as $assoc_row) {
  60. $assoc = new Auth_OpenID_Association($assoc_row->handle,
  61. base64_decode($assoc_row->secret),
  62. $assoc_row->issued,
  63. $assoc_row->lifetime,
  64. $assoc_row->assoc_type);
  65. if ($assoc->getExpiresIn() == 0) {
  66. OpenIDServer_ElggStore::removeAssociation($server_url, $assoc->handle);
  67. } else {
  68. $associations[] = array($assoc->issued, $assoc);
  69. }
  70. }
  71. if ($associations) {
  72. $issued = array();
  73. $assocs = array();
  74. foreach ($associations as $key => $assoc) {
  75. $issued[$key] = $assoc[0];
  76. $assocs[$key] = $assoc[1];
  77. }
  78. array_multisort($issued, SORT_DESC, $assocs, SORT_DESC,
  79. $associations);
  80. // return the most recently issued one.
  81. list($issued, $assoc) = $associations[0];
  82. return $assoc;
  83. } else {
  84. return null;
  85. }
  86. }
  87. }
  88. function removeAssociation ($server_url, $handle) {
  89. if (isset($handle)) {
  90. $meta_array = array(
  91. 'server_url' => $server_url,
  92. 'handle' => $handle
  93. );
  94. $entities = get_entities_from_metadata_multi($meta_array, 'object', 'openid_client::association');
  95. } else {
  96. $entities = get_entities_from_metadata('server_url', $server_url, 'object','openid_client::association');
  97. }
  98. if ($entities) {
  99. foreach ($entities as $entity) {
  100. openid_server_delete_entity($entity);
  101. }
  102. }
  103. }
  104. function reset () {
  105. OpenIDServer_ElggStore::resetAssociations ();
  106. OpenIDServer_ElggStore::resetNonces ();
  107. }
  108. function storeAssociation ($server_url, $association) {
  109. // Initialise a new ElggObject
  110. $association_obj = new ElggObject();
  111. $association_obj->subtype = 'openid_client::association';
  112. $association_obj->owner_guid = 0;
  113. $association_obj->access_id = 2;
  114. $association_obj->title = 'association';
  115. error_log("in storeAssociation, attempting to save association with new handle: ".$association->handle);
  116. if ($association_obj->save()) {
  117. $association_obj->server_url = $server_url;
  118. $association_obj->handle = $association->handle;
  119. $association_obj->secret = base64_encode($association->secret);
  120. $association_obj->issued = $association->issued;
  121. $association_obj->lifetime = $association->lifetime;
  122. $association_obj->assoc_type = $association->assoc_type;
  123. error_log("in storeAssociation, saved association with new handle: ".$association->handle);
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. }
  129. function useNonce ( $server_url, $timestamp, $salt) {
  130. global $Auth_OpenID_SKEW;
  131. if ( abs($timestamp - time()) > $Auth_OpenID_SKEW ) {
  132. return false;
  133. }
  134. // check to see if the nonce already exists
  135. $meta_array = array(
  136. 'server_url' => $server_url,
  137. 'timestamp' => $timestamp,
  138. 'salt' => $salt
  139. );
  140. $entities = get_entities_from_metadata_multi($meta_array, 'object', 'openid_client::nonce');
  141. if ($entities) {
  142. // bad - this nonce is already in use
  143. return false;
  144. } else {
  145. // Initialise a new ElggObject
  146. $nonce_obj = new ElggObject();
  147. $nonce_obj->subtype = 'openid_client::nonce';
  148. $nonce_obj->owner_guid = 0;
  149. $nonce_obj->title = 'nonce';
  150. if ($nonce_obj->save()) {
  151. $nonce_obj->server_url = $server_url;
  152. $nonce_obj->timestamp = $timestamp;
  153. $nonce_obj->salt = $salt;
  154. return true;
  155. } else {
  156. return false;
  157. }
  158. }
  159. }
  160. function getTrustedSites() {
  161. error_log("GET TRUSTED");
  162. $results = get_entities_from_metadata('openid_url', getLoggedInUser(), 'object','openid_server::trust_root');
  163. $sites = array();
  164. if ($results) {
  165. foreach ($results as $site) {
  166. $sites[] = $site->trust_root;
  167. error_log("GET TRUST".$site->trust_root);
  168. }
  169. }
  170. return $sites;
  171. }
  172. /**
  173. * Returns the autologin URLs for every trusted site
  174. */
  175. function getAutoLoginSites() {
  176. $default_trusted_sites = get_entities_from_metadata('openid_url', '', 'object','openid_server::trust_root');
  177. $sites = array();
  178. if ($default_trusted_sites) {
  179. foreach ($default_trusted_sites as $site) {
  180. if ($site->auto_login_url) {
  181. $sites[] = $site;
  182. }
  183. }
  184. }
  185. return $sites;
  186. }
  187. /**
  188. * Returns the autologout URLs for every trusted site
  189. */
  190. function getAutoLogoutSites() {
  191. $default_trusted_sites = get_entities_from_metadata('openid_url', '', 'object','openid_server::trust_root');
  192. $sites = array();
  193. if ($default_trusted_sites) {
  194. foreach ($default_trusted_sites as $site) {
  195. if ($site->auto_logout_url) {
  196. $sites[] = $site;
  197. }
  198. }
  199. }
  200. return $sites;
  201. }
  202. function setTrustedSite($trust_root) {
  203. $openid_url = getLoggedInUser();
  204. $site = new ElggObject();
  205. error_log("SET TRUST-"."X".$trust_root->site_name."X".$trust_root->trust_root.":-:".$openid_url);
  206. $site->subtype = 'openid_server::trust_root';
  207. $site->owner_guid = 0;
  208. $site->title = 'association';
  209. $site->access_id = 2;
  210. if ($site->save()) {
  211. $site->openid_url = $openid_url;
  212. $site->trust_root = $trust_root->trust_root;
  213. $site->site_name = $trust_root->site_name;
  214. $site->autologin = $trust_root->autologin;
  215. $site->autologout = $trust_root->autologout;
  216. $site->width = $trust_root->width;
  217. $site->height = $trust_root->height;
  218. return true;
  219. } else {
  220. return false;
  221. }
  222. }
  223. function removeAllTrustedSites() {
  224. $openid_url = getLoggedInUser();
  225. if ($openid_url != null) {
  226. $results = get_entities_from_metadata('openid_url', $openid_url, 'object','openid_server::trust_root');
  227. if ($results) {
  228. foreach($results as $trust_root) {
  229. $trust_root->delete();
  230. }
  231. }
  232. }
  233. return true;
  234. }
  235. function removeTrustedSite($trust_root) {
  236. $openid_url = getLoggedInUser();
  237. if ($openid_url != null) {
  238. $meta_array = array(
  239. 'openid_url' => $openid_url,
  240. 'trust_root' => $trust_root
  241. );
  242. $results = get_entities_from_metadata_multi($meta_array, 'object', 'openid_server::trust_root');
  243. if ($results) {
  244. foreach($results as $trust_root) {
  245. $trust_root->delete();
  246. }
  247. }
  248. }
  249. return true;
  250. }
  251. }
  252. function getOpenIDServerStore() {
  253. return new OpenIDServer_ElggStore();
  254. }
  255. if (!function_exists('fnmatch')) {
  256. function fnmatch($pattern, $string) {
  257. for ($op = 0, $npattern = '', $n = 0, $l = strlen($pattern); $n < $l; $n++) {
  258. switch ($c = $pattern[$n]) {
  259. case '\\':
  260. $npattern .= '\\' . @$pattern[++$n];
  261. break;
  262. case '.': case '+': case '^': case '$': case '(': case ')': case '{': case '}': case '=': case '!': case '<': case '>': case '|':
  263. $npattern .= '\\' . $c;
  264. break;
  265. case '?': case '*':
  266. $npattern .= '.' . $c;
  267. break;
  268. case '[': case ']': default:
  269. $npattern .= $c;
  270. if ($c == '[') {
  271. $op++;
  272. } else if ($c == ']') {
  273. if ($op == 0) return false;
  274. $op--;
  275. }
  276. break;
  277. }
  278. }
  279. if ($op != 0) return false;
  280. return preg_match('/' . $npattern . '/i', $string);
  281. }
  282. }
  283. ?>