Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
form di login crittografata senza Https - main.php

main.php

Caricato da:
Scarica il programma completo

  1. <?php
  2. require_once("./lib/jcryption.php");
  3.  
  4. $keyLength = 256;
  5. $jCryption = new jCryption();
  6.  
  7. if(isset($_GET["generateKeypair"]))
  8. {
  9.          if (empty($_SESSION["e"])){
  10.      
  11.       // logic added by ma.gi.a. di rossini to avoid the key generation
  12.       // every time we use this module
  13.       // asymmetric keys are once generated and saved in a file using  array2string() -
  14.       // To get the keys from file we use string2array()
  15.      
  16.            if ((!file_exists("lib/keypair.txt")))
  17.            {
  18.                  $keys = $jCryption->generateKeypair($keyLength);
  19.                  // e scrivi un file con le chiavi
  20.            array2string($keys,$output,$parent);
  21.            // Store the string in a file
  22.            $f1 = fopen("lib/keypair.txt","w+");
  23.            fwrite($f1,$output);
  24.            fclose($f1);
  25.        }
  26.            else
  27.            {
  28.                // Read the file back from the disk
  29.                    $f1 = fopen("lib/keypair.txt","r");
  30.                    $newString = fread($f1,filesize('lib/keypair.txt'));
  31.                    fclose($f1);
  32.                    // Convert the content back to an array
  33.                string2array($newString, $keys);
  34.            }
  35.  
  36.            // sets the keys in the session to have them ready when you submit the form
  37.            // please keep this structure.
  38.            // You'll need the hex value of the key for javascript
  39.            // and the int value of the key for PHP. e = public key, d = private key, n = modulo
  40.  
  41.            $_SESSION["e"] = array("int" => $keys["e"], "hex" => $jCryption->dec2string($keys["e"],16));
  42.            $_SESSION["d"] = array("int" => $keys["d"], "hex" => $jCryption->dec2string($keys["d"],16));
  43.            $_SESSION["n"] = array("int" => $keys["n"], "hex" => $jCryption->dec2string($keys["n"],16));
  44.      }
  45.  
  46.      //returns the needed keys for the javascript part in a JSON string
  47.         //maxdigits is need for the javascript and calculated like ($keyLength * 2 / 16 + 3)
  48.  
  49.      /* debug
  50.      $f1 = fopen("keyparhex.txt","w+");
  51.      fwrite($f1, '{"e":"'.$_SESSION["e"]["hex"].'","n":"'.$_SESSION["n"]["hex"].'","maxdigits":"'.intval($keyLength*2/16+3).'"}');
  52.      fclose($f1);
  53.      */
  54.      echo '{"e":"'.$_SESSION["e"]["hex"].'","n":"'.$_SESSION["n"]["hex"].'","maxdigits":"'.intval($keyLength*2/16+3).'"}';
  55.  
  56.   } else {
  57.         //
  58.         //here the decrypt function is called. The first parameter is the encrypted POST.
  59.         //Second parameter is the private key d in it's int form and third the modulo n.
  60.         //This function will return your orginal decrypted POST.
  61.         $var = $jCryption->decrypt($_POST['jCryption'], $_SESSION["d"]["int"], $_SESSION["n"]["int"]);
  62.  
  63.         //unset($_SESSION["e"]);
  64.         //unset($_SESSION["d"]);
  65.         //unset($_SESSION["n"]);
  66.  
  67.      // parse the query string from post -  into variables
  68.         parse_str($var,$result);
  69.         $posted = $_POST['jCryption'];
  70.  
  71.     //debug
  72.         /*
  73.         $f1 = fopen("received.txt","w+");
  74.            fwrite($f1,"\r\n-posted-\r\n");
  75.            fwrite($f1,$posted);
  76.            fwrite($f1,"\r\n- var decrypted -\r\n");
  77.            fwrite($f1,$var);
  78.            fclose($f1);
  79.    
  80.     */
  81.         //echo "<br> decrypted data are:" .$var;
  82.         //$result["User"] = "rg";
  83.         //$result["Passw"] = "rr";
  84.  
  85.     /* original code commented - with password we do IMAP authentication
  86.         if (preg_match('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $result["Passw"])) {
  87.                 echo "true";
  88.         } else {
  89.                 echo "false";
  90.         }
  91.     */
  92.         $username = $result["User"];
  93.         $password = $result["Passw"];
  94.         $where_we_are = pathinfo($_SERVER['SCRIPT_FILENAME']) ;
  95.         $root= $where_we_are['dirname']."/";
  96.  
  97.         // if not exists username file try with IMAP - not used in this example
  98.         if ((!file_exists($root."user/".$username.".txt")))
  99.         {
  100.             // try imap autentication
  101.                 $imap_host[] = "10.103.8.21";  // mailpp01.postel.it
  102.                 $imap_port = "143";
  103.                 $ldap_ok = 0;
  104.                 require("lib/auth_imap.inc");
  105.                 if(authValidateUser($username, $password))
  106.                 {
  107.                         // echo " Ldap ". $imap_host[0] . " AUTENTICATO - ";
  108.                         $ldap_ok = 1;
  109.                         $_SESSION['UUser'] = $username;
  110.                         $_SESSION['LoggedIn'] = 2;
  111.                 }
  112.                 else
  113.                 {
  114.                         $msg_Ldap = " Ldap 10.103.8.21 NON autenticato ";
  115.                         //echo $msg_Ldap;
  116.                 }
  117.         }
  118.          // if exists username file
  119.                 if ((file_exists($root."user/".$username.".txt")))
  120.                 {
  121.                         $msglocalfile = "Non autenticato su file $username";
  122.                         $fp=fopen($root."user/".$username.".txt","r");
  123.                         $contents=fread($fp,filesize($root."user/".$username.".txt"));
  124.                         fclose($fp);
  125.                         $info=explode("|",$contents);
  126.                         $username1=$info[0];
  127.                         $password1=$info[1];
  128.                         //if($password1==$_SESSION['password'])
  129.                         if($password1 == $password)     {
  130.                                 //$firstuser = $temp_username;
  131.                                 $username = $username1;
  132.                                 $msglocalfile = " Autenticato su file $username ";
  133.                                 $_SESSION['UUser'] = $username;
  134.                                 $_SESSION['LoggedIn'] = 2;
  135.                         }else {
  136.                                     session_unregister('LoggedIn') ;
  137.                                     session_unregister('UUser');
  138.                 }
  139.                         // remove comment in next line to have this info at top
  140.                         //echo $msglocalfile;
  141.         }
  142.  
  143.         // return from authentication
  144.         //echo " -- $username1 -- $password1";
  145.         if(isset($_SESSION['UUser']))
  146.           {
  147.                
  148.                 //$_SESSION['LoggedIn'] = 1;
  149.            
  150.                  echo "<h1>Success</h1>";
  151.                  echo "<p>We are now redirecting you to the member area as $username1.</p>";
  152.              
  153.                         echo ' <TEXTAREA ROWS=8 COLS=100 WRAP="off" name=textcode>';                   
  154.                         echo '<!-- to redirect to index_step_1.php (application menu) uncomment the following lines -->'."\n\r flush();\n ob_flush();\n sleep(2);\n echo ".
  155.                         '"<script type="text/javascript" > document.location.href="index_step_1.php"</script>";';
  156.                         echo '</TEXTAREA>';
  157.  
  158.  
  159.                    // display message ...
  160.                // flush();
  161.                // ob_flush();
  162.                    //  sleep(2);
  163.            // redirect
  164.                    // - this not works sometime in IE - see Security tab, then choose Custom Level and the Meta Tag Refresh
  165.                  //   echo "<meta http-equiv='refresh' content='=2;index.php' />";
  166.                    // - this give warning
  167.                    //   header("refresh: 10; index.php");
  168.                    //echo "<script type='text/javascript'>document.location.href='index_step_1.php'</script> ";
  169.            
  170.            //echo "true";
  171.  
  172.         }
  173.         else
  174.         {
  175.                  
  176.                 echo "<h1>Error</h1>";
  177.                         //echo " root is $root" . $msglocalfile."<br>";
  178.                         echo "<p>Sorry, la coppia di user/password($username - ******) non e' stata autenticata<br>";
  179.                         echo " $msg_Ldap - $msglocalfile <br>";
  180.                         echo "<a href=\"index.php\">premi qui per riprovare</a>.</p>";
  181.              
  182.          // for test we can display sent password as decrypted -  
  183.          // echo $password;
  184.          //echo "false";
  185.         }
  186.   }
  187.  
  188.    /**
  189.     * This function converts an array into a separated string
  190.     *
  191.     * @param Array $myarray The array to convert to string
  192.     * @param String $output The reference to the output string
  193.     * @param String $parentkey It is a helper variable
  194.     */
  195.    function array2string($myarray,&$output,&$parentkey){
  196.       foreach($myarray as $key=>$value){
  197.          if (is_array($value)) {
  198.             $parentkey .= $key."·";
  199.             array2string($value,$output,$parentkey);
  200.             $parentkey = "";
  201.          }
  202.          else {
  203.             $output .= $parentkey.$key."·".$value."\n";
  204.          }
  205.       }
  206.    }
  207.  
  208.    /**
  209.     * This function converts a separated string into an array
  210.     *
  211.     * @param String $string The string to convert into an Array
  212.     * @param Array $myarray The array to store the output
  213.     */
  214.    function string2array($string,&$myarray){
  215.       $lines = explode("\n",$string);
  216.       foreach ($lines as $value){
  217.          $items = explode("·",$value);
  218.          if (sizeof($items) == 2){
  219.             $myarray[$items[0]] = $items[1];
  220.          }
  221.          else if (sizeof($items) == 3){
  222.             $myarray[$items[0]][$items[1]] = $items[2];
  223.          }
  224.       }
  225.    }
  226.  
  227. ?>