PageRenderTime 76ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/FSN/mediatheque/zp-core/zp-extensions/federated_logon/Auth/OpenID_detect.php

https://gitlab.com/r.collas/site_central
PHP | 511 lines | 438 code | 71 blank | 2 comment | 70 complexity | 55c1fbc19b29012eb6c8cf02ac9a1608 MD5 | raw file
  1. <?php
  2. $path_extra = dirname(dirname(__FILE__));
  3. $path = ini_get('include_path');
  4. $path = $path_extra . PATH_SEPARATOR . $path;
  5. ini_set('include_path', $path);
  6. define('IS_WINDOWS', strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
  7. class PlainText {
  8. function start($title) {
  9. return '';
  10. }
  11. function tt($text) {
  12. return $text;
  13. }
  14. function link($href, $text = null) {
  15. if ($text) {
  16. return $text . ' <' . $href . '>';
  17. } else {
  18. return $href;
  19. }
  20. }
  21. function b($text) {
  22. return '*' . $text . '*';
  23. }
  24. function contentType() {
  25. return 'text/plain';
  26. }
  27. function p($text) {
  28. return wordwrap($text) . "\n\n";
  29. }
  30. function pre($text) {
  31. $out = '';
  32. $lines = array_map('trim', explode("\n", $text));
  33. foreach ($lines as $line) {
  34. $out .= ' ' . $line . "\n";
  35. }
  36. $out .= "\n";
  37. return $out;
  38. }
  39. function ol($items) {
  40. $out = '';
  41. $c = 1;
  42. foreach ($items as $item) {
  43. $item = wordwrap($item, 72);
  44. $lines = array_map('trim', explode("\n", $item));
  45. $out .= $c . '. ' . $lines[0] . "\n";
  46. unset($lines[0]);
  47. foreach ($lines as $line) {
  48. $out .= ' ' . $line . "\n";
  49. }
  50. $out .= "\n";
  51. $c += 1;
  52. }
  53. return $out;
  54. }
  55. function h2($text) {
  56. return $this->h($text, 2);
  57. }
  58. function h1($text) {
  59. return $this->h($text, 1);
  60. }
  61. function h($text, $n) {
  62. $chars = '#=+-.';
  63. $c = $chars[$n - 1];
  64. return "\n" . $text . "\n" . str_repeat($c, strlen($text)) . "\n\n";
  65. }
  66. function end() {
  67. return '';
  68. }
  69. }
  70. class HTML {
  71. function start($title) {
  72. return '<html><head><title>' . $title . '</title>' .
  73. $this->stylesheet() .
  74. '</head><body>' . "\n";
  75. }
  76. function stylesheet() {
  77. return "<style type='text/css'>\n" .
  78. "p {\n" .
  79. " width: 50em;\n" .
  80. "}\n" .
  81. '</style>';
  82. }
  83. function tt($text) {
  84. return '<code>' . $text . '</code>';
  85. }
  86. function contentType() {
  87. return 'text/html';
  88. }
  89. function b($text) {
  90. return '<strong>' . $text . '</strong>';
  91. }
  92. function p($text) {
  93. return '<p>' . wordwrap($text) . "</p>\n";
  94. }
  95. function pre($text) {
  96. return '<pre>' . $text . "</pre>\n";
  97. }
  98. function ol($items) {
  99. $out = '<ol>';
  100. foreach ($items as $item) {
  101. $out .= '<li>' . wordwrap($item) . "</li>\n";
  102. }
  103. $out .= "</ol>\n";
  104. return $out;
  105. }
  106. function h($text, $n) {
  107. return "<h$n>$text</h$n>\n";
  108. }
  109. function h2($text) {
  110. return $this->h($text, 2);
  111. }
  112. function h1($text) {
  113. return $this->h($text, 1);
  114. }
  115. function link($href, $text = null) {
  116. return '<a href="' . $href . '">' . ($text ? $text : $href) . '</a>';
  117. }
  118. function end() {
  119. return "</body>\n</html>\n";
  120. }
  121. }
  122. if (isset($_SERVER['REQUEST_METHOD'])) {
  123. $r = new HTML();
  124. } else {
  125. $r = new PlainText();
  126. }
  127. function detect_math($r, &$out) {
  128. $out .= $r->h2('Math support');
  129. $ext = Auth_OpenID_detectMathLibrary(Auth_OpenID_math_extensions());
  130. if (!isset($ext['extension']) || !isset($ext['class'])) {
  131. $out .= $r->p(
  132. 'Your PHP installation does not include big integer math ' .
  133. 'support. This support is required if you wish to run a ' .
  134. 'secure OpenID server without using SSL.');
  135. $out .= $r->p('To use this library, you have a few options:');
  136. $gmp_lnk = $r->link('http://www.php.net/manual/en/ref.gmp.php', 'GMP');
  137. $bc_lnk = $r->link('http://www.php.net/manual/en/ref.bc.php', 'bcmath');
  138. $out .= $r->ol(array(
  139. 'Install the ' . $gmp_lnk . ' PHP extension',
  140. 'Install the ' . $bc_lnk . ' PHP extension',
  141. 'If your site is low-security, call ' .
  142. 'Auth_OpenID_setNoMathSupport(), defined in Auth/OpenID/BigMath.php. ',
  143. 'The library will function, but ' .
  144. 'the security of your OpenID server will depend on the ' .
  145. 'security of the network links involved. If you are only ' .
  146. 'using consumer support, you should still be able to operate ' .
  147. 'securely when the users are communicating with a ' .
  148. 'well-implemented server.'));
  149. return false;
  150. } else {
  151. switch ($ext['extension']) {
  152. case 'bcmath':
  153. $out .= $r->p('Your PHP installation has bcmath support. This is ' .
  154. 'adequate for small-scale use, but can be CPU-intensive. ' .
  155. 'You may want to look into installing the GMP extension.');
  156. $lnk = $r->link('http://www.php.net/manual/en/ref.gmp.php');
  157. $out .= $r->p('See ' . $lnk . ' for more information ' .
  158. 'about the GMP extension.');
  159. break;
  160. case 'gmp':
  161. $out .= $r->p('Your PHP installation has gmp support. Good.');
  162. break;
  163. default:
  164. $class = $ext['class'];
  165. $lib = new $class();
  166. $one = $lib->init(1);
  167. $two = $lib->add($one, $one);
  168. $t = $lib->toString($two);
  169. $out .= $r->p('Uh-oh. I do not know about the ' .
  170. $ext['extension'] . ' extension!');
  171. if ($t != '2') {
  172. $out .= $r->p('It looks like it is broken. 1 + 1 = ' .
  173. var_export($t, false));
  174. return false;
  175. } else {
  176. $out .= $r->p('But it seems to be able to add one and one.');
  177. }
  178. }
  179. return true; // Math library is OK
  180. }
  181. }
  182. function detect_random($r, &$out) {
  183. $out .= $r->h2('Cryptographic-quality randomness source');
  184. if (Auth_OpenID_RAND_SOURCE === null) {
  185. $out .= $r->p('Using (insecure) pseudorandom number source, because ' .
  186. 'Auth_OpenID_RAND_SOURCE has been defined as null.');
  187. return false;
  188. }
  189. $msg = 'The library will try to access ' . Auth_OpenID_RAND_SOURCE
  190. . ' as a source of random data. ';
  191. $numbytes = 6;
  192. $f = @fopen(Auth_OpenID_RAND_SOURCE, 'r');
  193. if ($f !== false) {
  194. $data = fread($f, $numbytes);
  195. $stat = fstat($f);
  196. $size = $stat['size'];
  197. fclose($f);
  198. } else {
  199. $data = null;
  200. $size = true;
  201. }
  202. if ($f !== false) {
  203. $dataok = (Auth_OpenID::bytes($data) == $numbytes);
  204. $ok = $dataok && !$size;
  205. $msg .= 'It seems to exist ';
  206. if ($dataok) {
  207. $msg .= 'and be readable. Here is some hex data: ' .
  208. bin2hex($data) . '.';
  209. } else {
  210. $msg .= 'but reading data failed.';
  211. }
  212. if ($size) {
  213. $msg .= ' This is a ' . $size . ' byte file. Unless you know ' .
  214. 'what you are doing, it is likely that you are making a ' .
  215. 'mistake by using a regular file as a randomness source.';
  216. }
  217. } else {
  218. $msg .= Auth_OpenID_RAND_SOURCE .
  219. ' could not be opened. This could be because of restrictions on' .
  220. ' your PHP environment or that randomness source may not exist' .
  221. ' on this platform.';
  222. if (IS_WINDOWS) {
  223. $msg .= ' You seem to be running Windows. This library does not' .
  224. ' have access to a good source of randomness on Windows.';
  225. }
  226. $ok = false;
  227. }
  228. $out .= $r->p($msg);
  229. if (!$ok) {
  230. $out .= $r->p(
  231. 'To set a source of randomness, define Auth_OpenID_RAND_SOURCE ' .
  232. 'to the path to the randomness source. If your platform does ' .
  233. 'not provide a secure randomness source, the library can' .
  234. 'operate in pseudorandom mode, but it is then vulnerable to ' .
  235. 'theoretical attacks. If you wish to operate in pseudorandom ' .
  236. 'mode, define Auth_OpenID_RAND_SOURCE to null.');
  237. $out .= $r->p('You are running on:');
  238. $out .= $r->pre(php_uname());
  239. $out .= $r->p('There does not seem to be an available source ' .
  240. 'of randomness. On a Unix-like platform ' .
  241. '(including MacOS X), try /dev/random and ' .
  242. '/dev/urandom.');
  243. }
  244. return $ok;
  245. }
  246. function detect_stores($r, &$out) {
  247. $out .= $r->h2('Data storage');
  248. $found = array();
  249. foreach (array('sqlite', 'mysql', 'pgsql') as $dbext) {
  250. if (extension_loaded($dbext) || ini_get('enable_dl') && function_exists('dl') && dl($dbext . '.' . PHP_SHLIB_SUFFIX)) {
  251. $found[] = $dbext;
  252. }
  253. }
  254. if (count($found) == 0) {
  255. $text = 'No SQL database support was found in this PHP ' .
  256. 'installation. See the PHP manual if you need to ' .
  257. 'use an SQL database.';
  258. } else {
  259. $text = 'Support was found for ';
  260. if (count($found) == 1) {
  261. $text .= $found[0] . '.';
  262. } else {
  263. $last = array_pop($found);
  264. $text .= implode(', ', $found) . ' and ' . $last . '.';
  265. }
  266. $text = $r->b($text);
  267. }
  268. $text .= ' The library supports the MySQL, PostgreSQL, and SQLite ' .
  269. 'database engines, as well as filesystem-based storage. In ' .
  270. 'addition, PEAR DB is required to use databases.';
  271. $out .= $r->p($text);
  272. if (function_exists('posix_getpwuid') &&
  273. function_exists('posix_geteuid')) {
  274. $processUser = posix_getpwuid(posix_geteuid());
  275. $web_user = $r->b($r->tt($processUser['name']));
  276. } else {
  277. $web_user = 'the PHP process';
  278. }
  279. if (in_array('sqlite', $found)) {
  280. $out .= $r->p('If you are using SQLite, your database must be ' .
  281. 'writable by ' . $web_user . ' and not available over' .
  282. ' the web.');
  283. }
  284. $basedir_str = ini_get('open_basedir');
  285. if (gettype($basedir_str) == 'string') {
  286. $url = 'http://www.php.net/manual/en/features.safe-mode.php' .
  287. '#ini.open-basedir';
  288. $lnk = $r->link($url, 'open_basedir');
  289. $out .= $r->p('If you are using a filesystem-based store or SQLite, ' .
  290. 'be aware that ' . $lnk . ' is in effect. This means ' .
  291. 'that your data will have to be stored in one of the ' .
  292. 'following locations:');
  293. $out .= $r->pre(var_export($basedir_str, true));
  294. } else {
  295. $out .= $r->p('The ' . $r->b($r->tt('open_basedir')) . ' configuration restriction ' .
  296. 'is not in effect.');
  297. }
  298. $out .= $r->p('If you are using the filesystem store, your ' .
  299. 'data directory must be readable and writable by ' .
  300. $web_user . ' and not availabe over the Web.');
  301. return true;
  302. }
  303. function detect_xml($r, &$out) {
  304. global $__Auth_Yadis_xml_extensions;
  305. $out .= $r->h2('XML Support');
  306. // Try to get an XML extension.
  307. $ext = Auth_Yadis_getXMLParser();
  308. if ($ext !== null) {
  309. $out .= $r->p('XML parsing support is present using the ' .
  310. $r->b(get_class($ext)) . ' interface.');
  311. return true;
  312. } else {
  313. $out .= $r->p('XML parsing support is absent; please install one ' .
  314. 'of the following PHP extensions:');
  315. foreach ($__Auth_Yadis_xml_extensions as $name => $cls) {
  316. $out .= "<li>" . $r->b($name) . "</li>";
  317. }
  318. return false;
  319. }
  320. }
  321. function detect_query_corruption($r, &$out) {
  322. $out .= $r->h2('Query Corruption');
  323. if ($_SERVER["QUERY_STRING"] != "test_query=a%26b") {
  324. $out.=$r->p("Your web server seems to corrupt queries. Received " . $_SERVER["QUERY_STRING"] . ", expected a=%26b. Check for mod_encoding.");
  325. return false;
  326. } else {
  327. $out.=$r->p("Your web server does not corrupt queries. Good.");
  328. return true;
  329. }
  330. }
  331. function detect_fetcher($r, &$out) {
  332. $out .= $r->h2('HTTP Fetching');
  333. $result = @include 'Auth/Yadis/Yadis.php';
  334. if (!$result) {
  335. $out .= $r->p('Yadis code unavailable; could not test fetcher support.');
  336. return false;
  337. }
  338. if (Auth_Yadis_Yadis::curlPresent()) {
  339. $out .= $r->p('This PHP installation has support for libcurl. Good.');
  340. $lnk = '';
  341. } else {
  342. $out .= $r->p('This PHP installation does not have support for ' .
  343. 'libcurl. CURL is not required but is recommended. ' .
  344. 'The OpenID library will use an fsockopen()-based fetcher.');
  345. $lnk = $r->link('http://us3.php.net/manual/en/ref.curl.php');
  346. $out .= $r->p('See ' . $lnk . ' about enabling the libcurl support ' .
  347. 'for PHP.');
  348. }
  349. $ok = true;
  350. $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
  351. $fetch_url = 'http://gist.github.com/raw/465630/c57eff55ebc0c54973903af5f72bac72762cf4f4/helloworld';
  352. $expected_url = 'https://raw.github.com/gist/465630/c57eff55ebc0c54973903af5f72bac72762cf4f4/helloworld';
  353. $result = $fetcher->get($fetch_url);
  354. if (isset($result)) {
  355. $parts = array('An HTTP request was completed.');
  356. // list ($code, $url, $data) = $result;
  357. if ($result->status != '200' && $result->status != '206') {
  358. $ok = false;
  359. $parts[] = $r->b(
  360. sprintf(
  361. 'Got %s instead of the expected HTTP status ' .
  362. 'code (200 or 206).', $result->status));
  363. }
  364. $url = $result->final_url;
  365. if ($url != $expected_url) {
  366. $ok = false;
  367. if ($url == $fetch_url) {
  368. $msg = 'The redirected URL was not returned.';
  369. } else {
  370. $msg = 'An unexpected URL was returned: <' . $url . '>.';
  371. }
  372. $parts[] = $r->b($msg);
  373. }
  374. $data = $result->body;
  375. if ($data != 'Hello World!') {
  376. $ok = false;
  377. $parts[] = $r->b('Unexpected data was returned.');
  378. }
  379. $out .= $r->p(implode(' ', $parts));
  380. } else {
  381. $ok = false;
  382. $out .= $r->p('Fetching URL ' . $lnk . ' failed!');
  383. }
  384. if ($fetcher->supportsSSL()) {
  385. $out .= $r->p('Your PHP installation appears to support SSL, so it ' .
  386. 'will be able to process HTTPS identity URLs and server URLs.');
  387. } else {
  388. $out .= $r->p('Your PHP installation does not support SSL, so it ' .
  389. 'will NOT be able to process HTTPS identity URLs and server URLs.');
  390. }
  391. return $ok;
  392. }
  393. header('Content-Type: ' . $r->contentType() . '; charset=us-ascii');
  394. if (empty($_GET["test_query"])) {
  395. header("Location: " . $_SERVER['PHP_SELF'] . "?test_query=a%26b");
  396. }
  397. $title = 'OpenID Library Support Report';
  398. $out = $r->start($title) .
  399. $r->h1($title) .
  400. $r->p('This script checks your PHP installation to determine if you ' .
  401. 'are set up to use the JanRain PHP OpenID library.');
  402. $body = '';
  403. $_include = include 'OpenID.php';
  404. if (!$_include) {
  405. $path = ini_get('include_path');
  406. $body .= $r->p(
  407. 'Cannot find the OpenID library. It must be in your PHP include ' .
  408. 'path. Your PHP include path is currently:');
  409. $body .= $r->pre($path);
  410. } else {
  411. $status = array();
  412. $status[] = detect_math($r, $body);
  413. $status[] = detect_random($r, $body);
  414. $status[] = detect_stores($r, $body);
  415. $status[] = detect_fetcher($r, $body);
  416. $status[] = detect_xml($r, $body);
  417. $status[] = detect_query_corruption($r, $body);
  418. $result = true;
  419. foreach ($status as $v) {
  420. if (!$v) {
  421. $result = false;
  422. break;
  423. }
  424. }
  425. if ($result) {
  426. $out .= $r->h2('Setup Complete!');
  427. $out .= $r->p('Your system should be ready to run the OpenID library.');
  428. } else {
  429. $out .= $r->h2('Setup Incomplete');
  430. $out .= $r->p('Your system needs a few changes before it will be ready to run the OpenID library.');
  431. }
  432. }
  433. if (!defined('OFFSET_PATH'))
  434. define('OFFSET_PATH', 5);
  435. require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/admin-functions.php');
  436. setOption('federated_logon_detect', 1);
  437. $body .= '<a href="' . FULLWEBPATH . '/' . ZENFOLDER . '/admin-plugins.php">' . gettext('back to Zenphoto') . '</a>';
  438. $out .= $body . $r->end();
  439. print $out;
  440. ?>