survey_seahorse

Software Engineering Project - Fall 2018
Log | Files | Refs | README

index.php (3657B)


      1 <?php    
      2 /*
      3  * PHP QR Code encoder
      4  *
      5  * Exemplatory usage
      6  *
      7  * PHP QR Code is distributed under LGPL 3
      8  * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
      9  *
     10  * This library is free software; you can redistribute it and/or
     11  * modify it under the terms of the GNU Lesser General Public
     12  * License as published by the Free Software Foundation; either
     13  * version 3 of the License, or any later version.
     14  *
     15  * This library is distributed in the hope that it will be useful,
     16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
     18  * Lesser General Public License for more details.
     19  *
     20  * You should have received a copy of the GNU Lesser General Public
     21  * License along with this library; if not, write to the Free Software
     22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
     23  */
     24     
     25     echo "<h1>PHP QR Code</h1><hr/>";
     26     
     27     //set it to writable location, a place for temp generated PNG files
     28     $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
     29     
     30     //html PNG location prefix
     31     $PNG_WEB_DIR = 'temp/';
     32 
     33     include "qrlib.php";    
     34     
     35     //ofcourse we need rights to create temp dir
     36     if (!file_exists($PNG_TEMP_DIR))
     37         mkdir($PNG_TEMP_DIR);
     38     
     39     
     40     $filename = $PNG_TEMP_DIR.'test.png';
     41     
     42     //processing form input
     43     //remember to sanitize user input in real-life solution !!!
     44     $errorCorrectionLevel = 'L';
     45     if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
     46         $errorCorrectionLevel = $_REQUEST['level'];    
     47 
     48     $matrixPointSize = 4;
     49     if (isset($_REQUEST['size']))
     50         $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);
     51 
     52 
     53     if (isset($_REQUEST['data'])) { 
     54     
     55         //it's very important!
     56         if (trim($_REQUEST['data']) == '')
     57             die('data cannot be empty! <a href="?">back</a>');
     58             
     59         // user data
     60         $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
     61         QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);    
     62         
     63     } else {    
     64     
     65         //default data
     66         echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';    
     67         QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);    
     68         
     69     }    
     70         
     71     //display generated file
     72     echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>';  
     73     
     74     //config form
     75     echo '<form action="index.php" method="post">
     76         Data:&nbsp;<input name="data" value="'.(isset($_REQUEST['data'])?htmlspecialchars($_REQUEST['data']):'PHP QR Code :)').'" />&nbsp;
     77         ECC:&nbsp;<select name="level">
     78             <option value="L"'.(($errorCorrectionLevel=='L')?' selected':'').'>L - smallest</option>
     79             <option value="M"'.(($errorCorrectionLevel=='M')?' selected':'').'>M</option>
     80             <option value="Q"'.(($errorCorrectionLevel=='Q')?' selected':'').'>Q</option>
     81             <option value="H"'.(($errorCorrectionLevel=='H')?' selected':'').'>H - best</option>
     82         </select>&nbsp;
     83         Size:&nbsp;<select name="size">';
     84         
     85     for($i=1;$i<=10;$i++)
     86         echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>';
     87         
     88     echo '</select>&nbsp;
     89         <input type="submit" value="GENERATE"></form><hr/>';
     90         
     91     // benchmark
     92     QRtools::timeBenchmark();    
     93 
     94