home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / notes2sql.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  7.0 KB  |  222 lines

  1. Notes2SQL by Hans Schou 
  2.  
  3. PHP script for converting data from Lotus Notes to SQL statements. 
  4.  
  5.  
  6.  
  7. <html> 
  8. <title>Lotus Notes to SQL converter</title> 
  9. <h1>Lotus Notes to SQL converter</h1> 
  10. <i>by Hans Schou notesql@schou.dk</i> 
  11. <p> 
  12. <?php 
  13.  
  14. if (file_exists(  "db-oci8.phl")) { 
  15.   $gotsql = 1; 
  16.   include(  "db-oci8.phl"); 
  17. } else 
  18.   $gotsql = 0; 
  19.  
  20. function loaddemo() { 
  21.   $d[] =   "FirstName:  Hans"; 
  22.   $d[] =   "Lastname:  Schou"; 
  23.   $d[] =   "Company:  ISS"; 
  24.   $d[] =   "Phone:  +45 3817 6372"; 
  25.   $d[] = chr(12); 
  26.   $d[] =   "FirstName:  John"; 
  27.   $d[] =   "LastName:  Doe"; 
  28.   $d[] =   "Email:  john.doe@acme.com"; 
  29.   $d[] =   "Company:  Acme"; 
  30.   $d[] =   "Address:  Go to gate"; 
  31.   $d[] =   "The beach";          // this line demonstrates 'multiple-lines'  
  32.   $d[] =   "Christmas Island"; 
  33.   $d[] = chr(12); 
  34.   return $d; 
  35.  
  36. // Select list for SQL types  
  37. function listtypes( $fname ) { 
  38. echo   "<select name=\"$ftyp_$fname\" size=\"1\">\n"; 
  39. ?> 
  40. <option>VARCHAR2</option> 
  41. <option>NVARCHAR2</option> 
  42. <option>NUMBER</option> 
  43. <option>LONG</option> 
  44. <option>DATE</option> 
  45. <option>CHAR</option> 
  46. <option>NCHAR</option> 
  47. <option>ROWID</option> 
  48. <option>MLSLABEL</option> 
  49. <option>CLOB</option> 
  50. <option>NCLOB</option> 
  51. <option>BLOB</option> 
  52. <option>BFILE</option> 
  53. </select>  <?php 
  54.  
  55. // Parse the structured text file and convert to SQL  
  56.  
  57. function parseone( $userfile, $userfile_name, $table ) { 
  58. /* ParseOne: get all field names and there size. Select which fields which should be included and there size */   
  59.  if (!strlen($userfile_name)) 
  60.    echo   "Loading demo...<br>\n"; 
  61.  else 
  62.    echo   "Proccesing <b>".$userfile_name.  "</b>...<br>\n"; 
  63.    // load file  
  64.  if ($userfile_name) 
  65.    $data = file($userfile); 
  66.  else 
  67.    $data = loaddemo(); 
  68.  echo   "<pre>"; 
  69.    // Lastfield is used for 'body text' with several lines  
  70.  $lastfname =   ""; 
  71.    // init array  
  72.  $record = array(); 
  73.  while (list($key,$value) = each($data)) { 
  74.      // filter value for CR-LF  
  75.    $v = ereg_replace(  "\r",  "",ereg_replace(  "\n",  "",$value)); 
  76.      // check if it looks like a field name, colon-space-space  
  77.    if (strpos($v,  ":  ")) { 
  78.      $pair = split(  ":  ",$v); 
  79.        // get field name and filter out 'dollar'  
  80.      $fname = strtolower(ereg_replace(  '$',  '',$pair[0])); 
  81.        // get field size  
  82.      $fsize = strlen(trim($pair[1])); 
  83.        // add field name and size to the record  
  84.      if (strlen(trim($fname)) && $fsize) { 
  85.        $record[$fname] = ereg_replace(  "'",  "''",trim($pair[1])); 
  86.          // add up the global field list  
  87.        if ($field[$fname] < $fsize) 
  88.          $field[$fname] = $fsize; 
  89.      } 
  90.    } else if (ord($value) == 12) { 
  91.        // check for form-feed and output record in SQL  
  92.        // output field names  
  93.      echo   "INSERT INTO\n $table("; 
  94.      reset($record); 
  95.      $comma = 0; 
  96.        // output field names  
  97.      while (list($fn,$fv) = each($record)) 
  98.        if (strlen($fv)) { 
  99.          if ($comma) 
  100.            echo   ","; 
  101.          $comma = 1; 
  102.          echo   "'".$fn.  "'"; 
  103.        } 
  104.      echo   ")\n VALUES("; 
  105.      reset($record); 
  106.      $comma = 0; 
  107.        // output values  
  108.      while (list($fn,$fv) = each($record)) 
  109.        if (strlen($fv)) { 
  110.          if ($comma) 
  111.            echo   ","; 
  112.          $comma = 1; 
  113.            // replace one quote with two quotes  
  114.          echo   "'".ereg_replace(  "'",  "''",$fv).  "'"; 
  115.        } 
  116.      echo   ");\n"; 
  117.        // clear records  
  118.      $record = array(); 
  119.    } else { 
  120.        // it was not a field name or FF, then it must be the 'body'  
  121.        // add up the data to previus field  
  122.      $record[$fname] .=   "\n".$v; 
  123.      if ($field[$fname] < strlen($record[$fname])) 
  124.        $field[$fname] = strlen($record[$fname]); 
  125.    } 
  126.  }   // while  
  127.  echo   "<p><hr><p>"; 
  128.    // sort fields  
  129.  ksort($field); 
  130.  reset($field); 
  131.  $comma = 0; 
  132.  echo   "# Suggested table definition\n"; 
  133.  echo   "CREATE TABLE $table(\n"; 
  134.  while (list($key,$value) = each($field)) { 
  135.    if ($comma) 
  136.      echo   ",\n"; 
  137.    $comma = 1; 
  138.    echo   " ".$key.  " VARCHAR2(".$value.  ")"; 
  139.  } 
  140.  echo   ");\n"; 
  141.  echo   "<hr>\n"; 
  142.  reset($field); 
  143.  echo   "<form action=\"$PHP_SELF\" method=\"post\">\n"; 
  144.  while (list($key,$value) = each($field)) { 
  145.    echo   "<input type=\"checkbox\" checked name=\"fuse_$key\">"; 
  146.    echo   "<input type=\"text\" name=\"fnam_$key\" value=\"$key\">"; 
  147.    listtypes( $key ); 
  148.    echo   "<input type=\"text\" name=\"fsiz_$key\" value=\"$value\" size=\"4\">"; 
  149.    echo   "\n"; 
  150.  } 
  151.    // delete input file  
  152.  echo   "\n<!-- Deleting: $userfile -->\n"; 
  153.  if (strlen($userfile_name)>1 && unlink($userfile)) 
  154.    echo   "<!-- $userfile has been deleted -->\n"; 
  155.  
  156. switch (strtolower($action)) { 
  157.   case   "send file": 
  158.     parseone( $userfile, $userfile_name, $table ); 
  159.     break; 
  160.   default: 
  161. ?> 
  162. <table border="5"><tr><td> 
  163. <form enctype="multipart/form-data" action="  <?php echo $PHP_SELF;?>" method="post"> 
  164. <table border="0" cellpadding="5"> 
  165. <tr> 
  166.  <td>New table name</td> 
  167.  <td><input type="text" name="table" value="mytable" size="8"></td> 
  168. </tr> 
  169. <tr> 
  170.  <td>Upload file</td> 
  171.  <td><input type="file" name="userfile" value="c:\notoes2sql.txt"></td> 
  172. </tr> 
  173. <tr> 
  174.  <td colspan="2"><input type="submit" name="action" value="Send file"></td> 
  175. </tr> 
  176. </table> 
  177. </td></tr></table> 
  178. <p><hr> 
  179. <h2>Description</h2> 
  180. This program convert a "structured text" export from Lotus Notes to standard 
  181. SQL statements. The limitations with this convertion is that you do not 
  182. get attachments in the export file. Attachments has to be inserted manually 
  183. afterwards. 
  184. <ol> 
  185. <li>Go in to Lotus Notes</li> 
  186. <li>Select a view</li> 
  187. <li>Choose File/Export</li> 
  188. <li>Type a file name and choose "Save as type: Structured Text"</li> 
  189. <li>Select all documents and "Inter-document delimiter=12"</li> 
  190. <li>On this page, "browse" to the file and send it</li> 
  191. <li>Copy and paste the result into a text file and load it in your SQL database</li> 
  192. </ol> 
  193. <h3>Example of "structured text"</h3> 
  194. <pre> 
  195. <?php 
  196.   $demo = loaddemo(); 
  197.   while (list($key,$value) = each($demo)) 
  198.     echo $value.  "\n"; 
  199. ?> 
  200. </pre> 
  201. To try a the example above: copy and paste the fields and values into a new filethen upload it. If you are very impacient just press 'send file' and the demo will be loaded internally. 
  202. <p> 
  203. <h3>Error: Maximum execution time exceeded</h3> 
  204. This server has been set to terminate a script after a certain amount of time to prevent poorly written scripts with end-less loops. 
  205. <br> 
  206. If you run your own server with this demo you can edit http.conf and add "php3_max_execution_time 120" to allow a prgram to run up to 120 seconds. Read the PHP Documentation for further details. 
  207. <p> 
  208. <?php 
  209.  
  210.  if (file_exists($fn = basename($PHP_SELF.   "s"))) 
  211.    echo    "<i>Source file: <a href=\"".$fn.   "\">$fn</a></i>\n"; 
  212.  else 
  213.    echo    "<i>Source file $fn is not available</i>\n"; 
  214.  
  215. ?> 
  216.  
  217. </html> 
  218.