t.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. define('DS', DIRECTORY_SEPARATOR);
  3. define('APP_PATH_ROOT', dirname(__FILE__));
  4. //define('APP_PATH_ROOT', '/Users/plabudda/se-dev-pl/SE');
  5. define('APP_PATH_LIB', APP_PATH_ROOT . '/se-lib');
  6. define('APP_PATH_WWW', APP_PATH_ROOT);
  7. define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');
  8. //session_save_path("./tmp") ;
  9. session_start();
  10. date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
  11. error_reporting(0);
  12. ini_set('error_reporting', 0);
  13. ini_set('display_startup_errors','0');
  14. //display_startup_errors(0);
  15. #TEST $_SESSION['DEBUG'] = 3;// TODO: TEST
  16. if (!isset($_SESSION['DEBUG'])) $_SESSION['DEBUG'] = 0;// set default value
  17. if (file_exists(APP_PATH_ROOT . "/config/.config_{$_SERVER['SERVER_NAME']}.php")) {
  18. require APP_PATH_ROOT . "/config/.config_{$_SERVER['SERVER_NAME']}.php";
  19. }
  20. if (file_exists(APP_PATH_ROOT . "/.config.php")) include APP_PATH_ROOT . "/.config.php";
  21. require_once APP_PATH_ROOT . "/superedit-SEF.php";
  22. require_once APP_PATH_LIB . '/' . 'Lib.php';
  23. Lib::loadClass('V');
  24. Lib::loadClass('DB');
  25. Lib::loadClass('User');
  26. Lib::loadClass('SE_Layout');
  27. Lib::loadClass('S');
  28. if (User::logged() && V::get('testDigest', '', $_GET) == 1) {
  29. $realm = "Browse access /Library/Server/Web/Data/Sites/Default/PLIKI";
  30. if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
  31. header('HTTP/1.1 401 Unauthorized');
  32. header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
  33. die('Text to send if user hits Cancel button');
  34. }
  35. // analyze the PHP_AUTH_DIGEST variable
  36. if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) || !isset($users[$data['username']])) {
  37. die('Wrong Credentials!');
  38. }
  39. // generate the valid response
  40. $A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
  41. $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
  42. $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
  43. if ($data['response'] != $valid_response) {
  44. die('Wrong Credentials!');
  45. }
  46. // ok, valid username & password
  47. echo 'You are logged in as: ' . $data['username'];
  48. // function to parse the http auth header
  49. function http_digest_parse($txt){
  50. // protect against missing data
  51. $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
  52. $data = array();
  53. $keys = implode('|', array_keys($needed_parts));
  54. preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
  55. foreach ($matches as $m) {
  56. $data[$m[1]] = $m[3] ? $m[3] : $m[4];
  57. unset($needed_parts[$m[1]]);
  58. }
  59. return $needed_parts ? false : $data;
  60. }
  61. die('Digest Login Test');
  62. }
  63. if (User::logged() && V::get('ajaxDigest', '', $_GET) == 1) {
  64. //header('Access-Control-Allow-Origin: *');
  65. SE_Layout::gora();
  66. Lib::loadClass('Crypt');
  67. ?>
  68. <script>
  69. /*
  70.  * A JavaScript implementation of the Digest Authentication
  71.  * Digest Authentication, as defined in RFC 2617.
  72.  * Version 1.0 Copyright (C) Maricn Michalski (http://marcin-michalski.pl)
  73.  * Distributed under the BSD License
  74.  *
  75.  * site: http://arrowgroup.eu
  76.  */
  77.  
  78. DigestAuthentication = function() {
  79. return {
  80. MAX_ATTEMPTS : 1,
  81. AUTHORIZATION_HEADER : "Authorization",
  82. WWW_AUTHENTICATE_HEADER : 'WWW-Authenticate',
  83. NC : "00000001", //currently nc value is fixed it is not incremented
  84. HTTP_METHOD : "GET",
  85. /**
  86.   * settings json:
  87.   * - onSuccess - on success callback
  88.   * - onFailure - on failure callback
  89.   * - username - user name
  90.   * - password - user password
  91.   * - cnonce - client nonce
  92.   */
  93. init : function(settings) {
  94. this.settings = settings;
  95. },
  96. setCredentials: function(username, password){
  97. this.settings.username = username;
  98. this.settings.password = password;
  99. },
  100. call : function(uri){
  101. this.attempts = 0;
  102. this.invokeCall(uri);
  103. },
  104. invokeCall: function(uri,authorizationHeader){
  105. var digestAuth = this;
  106. $.ajax({
  107. url: uri,
  108. type: this.HTTP_METHOD,
  109. beforeSend: function(request){
  110. if(typeof authorizationHeader != 'undefined'){
  111. request.setRequestHeader(digestAuth.AUTHORIZATION_HEADER, authorizationHeader);
  112. }
  113. },
  114. success: function(response) {
  115. digestAuth.settings.onSuccess(response);
  116. },
  117. error: function(response) {
  118. if(digestAuth.attempts == digestAuth.MAX_ATTEMPTS){
  119. digestAuth.settings.onFailure(response);
  120. return;
  121. }
  122. var paramParser = new HeaderParamsParser(response.getResponseHeader(digestAuth.WWW_AUTHENTICATE_HEADER));
  123. var nonce = paramParser.getParam("nonce");
  124. var realm = paramParser.getParam("realm");
  125. var qop = paramParser.getParam("qop");
  126. var response = digestAuth.calculateResponse(uri, nonce, realm, qop);
  127. var authorizationHeaderValue = digestAuth.generateAuthorizationHeader(paramParser.headerValue, response, uri);
  128. digestAuth.attempts++;
  129. digestAuth.invokeCall(uri, authorizationHeaderValue);
  130. }
  131. });
  132. },
  133. calculateResponse : function(uri, nonce, realm, qop){
  134. var a2 = this.HTTP_METHOD + ":" + uri;
  135. var a2Md5 = hex_md5(a2);
  136. var a1Md5 = hex_md5(this.settings.username + ":" + realm + ":" + this.settings.password);
  137. var digest = a1Md5 + ":" + nonce + ":" + this.NC + ":" + this.settings.cnonce + ":" + qop + ":" +a2Md5;
  138. return hex_md5(digest);
  139. },
  140. generateAuthorizationHeader : function(wwwAuthenticationHeader, response, uri){
  141. return wwwAuthenticationHeader+', username="'+this.settings.username+'", uri="'+
  142. uri+'", response="'+response+'", nc='+
  143. this.NC+', cnonce="'+this.settings.cnonce+'"';
  144. }
  145. }
  146. };
  147. HeaderParamsParser = function () {
  148. return {
  149. init : function(headerValue) {
  150. this.headerValue = headerValue;
  151. this.headerParams = this.headerValue.split(",");
  152. },
  153. getParam: function(paramName){
  154. var paramVal = null;
  155. $.each(this.headerParams, function(index, value){
  156. if(value.indexOf(paramName)>0){
  157. paramVal = value.split(paramName+"=")[1];
  158. paramVal = paramVal.substring(1, paramVal.length-1);
  159. }
  160. });
  161. return paramVal;
  162. }
  163. }
  164. };
  165. var ajaxDigest = new DigestAuthentication();
  166. ajaxDigest.init({username: '<?php echo User::getLogin(); ?>', password:'<?php echo Crypt::decrypt($_SESSION['ADM_PASS_HASH']); ?>'});
  167. //ajaxDigest.setCredentials('<?php echo User::getLogin(); ?>', '<?php echo Crypt::decrypt($_SESSION['ADM_PASS_HASH']); ?>');
  168. ajaxDigest.call('<?php echo "http://{$_SERVER['SERVER_NAME']}/PLIKI/"; ?>');
  169. </script>
  170. <?php
  171. SE_Layout::dol();
  172. exit;
  173. }
  174. if (User::logged() && V::get('onlySrv', '', $_GET) == 1) {
  175. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">SRV (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_SERVER);echo'</pre>';
  176. exit;
  177. }
  178. if (User::logged()) {
  179. Lib::loadClass('Crypt');
  180. $login = User::getLogin();
  181. $pass = Crypt::decrypt($_SESSION['ADM_PASS_HASH']);
  182. $ch = curl_init();
  183. // set url
  184. curl_setopt($ch, CURLOPT_URL, "http://{$_SERVER['SERVER_NAME']}/PLIKI/");
  185. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  186. curl_setopt($ch, CURLOPT_USERPWD, "{$login}:{$pass}");
  187. // first authentication with a head request
  188. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  189. curl_setopt($ch, CURLOPT_NOBODY, 1);
  190. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  191. curl_setopt($ch, CURLOPT_HEADER, 1);
  192. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  193. $output = curl_exec($ch);
  194. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">curl output 1 (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($output);echo'</pre>';
  195. $info = curl_getinfo($ch);
  196. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">curl info 1 (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($info);echo'</pre>';
  197. // the get the real output
  198. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  199. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  200. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  201. curl_setopt($ch, CURLOPT_HEADER, 1);
  202. curl_setopt($ch, CURLOPT_HTTPHEADER, 1);
  203. curl_setopt($ch, CURLOPT_HTTPGET, 1);
  204. $output = curl_exec($ch);
  205. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">curl output 2 (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($output);echo'</pre>';
  206. $info = curl_getinfo($ch);
  207. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">curl info 2 (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($info);echo'</pre>';
  208. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">curl (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($ch);echo'</pre>';
  209. curl_close($ch);
  210. // login by browser result in:
  211. // $_SERVER[PHP_AUTH_DIGEST] => username="plabudda", realm="Browse access /Library/Server/Web/Data/Sites/Default/PLIKI", nonce="03d338604c5e373eb15912fa3a9e75341381747501704512", uri="/SE/se-dev-pl/t.php", response="219a9c728b9f97317041c6f2cec672d5"
  212. // curl $info['request_header']
  213. // Authorization: Digest username="plabudda", realm="Browse access /Library/Server/Web/Data/Sites/Default/PLIKI", nonce="ab0433e0ddfd7c8875351f60ab0bfadf1381747561090631", uri="/PLIKI/", response="90758c48f0420635a45053902af41ab5"
  214. $info['request_header'] = explode("\n", $info['request_header']);
  215. foreach ($info['request_header'] as $vHeader) {
  216. if (substr($vHeader, 0, 22) == 'Authorization: Digest ') {
  217. $_SERVER['PHP_AUTH_DIGEST'] = substr($vHeader, 22);
  218. }
  219. }
  220. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">$_SERVER[PHP_AUTH_DIGEST] (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_SERVER['PHP_AUTH_DIGEST']);echo'</pre>';
  221. } else {
  222. echo '<p>Log in to test digest</p>';
  223. }
  224. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($SQL_DATABASE);echo'</pre>';
  225. $tbls = array();
  226. $db = DB::getDB();
  227. $sql = "show tables;";
  228. $res = $db->query($sql);
  229. while ($r = $db->fetch($res)) {
  230. $tbls[] = $r;
  231. }
  232. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tbls (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tbls);echo'</pre>';
  233. ?>