Current location: Hot Scripts Forums » Programming Languages » Perl » convert from perl code to php


convert from perl code to php

Reply
  #1 (permalink)  
Old 06-15-07, 08:57 AM
hostilio hostilio is offline
New Member
 
Join Date: Jun 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
convert from perl code to php

Hi

I need to convert below perl scripts to php scripts. I dont know much on perl and wonder if anyone there can assist me to convert below perl script to php scripts. thanks in advance.

Hostilio
perl Code:
  1. #!/usr/bin/perl
  2. use DBI;
  3. use User::pwent;
  4. require 'cookie.lib';
  5.  
  6. # Get the input
  7. if($ARGV[0] eq ""){
  8. $buffer=$ENV{'QUERY_STRING'};
  9. if($buffer eq ""){
  10. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  11. }
  12. }
  13. else {
  14. $buffer=$ARGV[0];
  15. }
  16. $buffer=~ s/\\//;
  17.  
  18. # Split the name-value pairs
  19. @pairs = split(/&/, $buffer);
  20.  
  21. foreach $pair (@pairs) {
  22. ($name, $value) = split(/=/, $pair);
  23. $value =~ tr/+/ /;
  24. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  25. $value =~ s/'/´/g;
  26. $FORM{$name} = $value;
  27. }
  28. $buffer=`cat mysql.conf`;
  29. @pairs = split(/\n/, $buffer);
  30. foreach $pair (@pairs) {
  31. ($name, $value) = split(/=/, $pair);
  32. $value =~ tr/+/ /;
  33. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  34. $FORM{$name} = $value;
  35. }
  36. $nome_bd=$FORM{"nome_bd"};
  37. $nome_server=$FORM{"nome_server"};
  38. $login=$FORM{"login"};
  39. $pass=$FORM{"pass"};
  40. $linha_conn = "DBI:mysql:$nome_bd:$nome_server:3306";
  41. $dbh=DBI->connect($linha_conn,$login,$pass,{ RaiseError => 1, AutoCommit => 1 });
  42. print "Content-type: text/html\n";
  43. $cookie = GetCookies ('Maputo');
  44. $COOKIE = $Cookies{'Maputo'};
  45. print "\n";
  46. $sth=$dbh->prepare("select ID from USER where COOKIE='$COOKIE'");
  47. $sth->execute;
  48. @row = $sth->fetchrow_array();
  49. $ID=$row[0];
  50. $sth->finish;
  51. if($row[0] eq ""){
  52. $sth->finish;
  53. $dbh->disconnect;
  54. exit;
  55. }
  56. $sth->finish;
  57. $mostra_dados=0;
  58. if($FORM{'INSERT'} eq "ok"){
  59. &verifica_dados;
  60. if($erro_dados == 0){
  61. if($FORM{'ID'} ne ""){
  62. &actualiza_dados;
  63. }
  64. else {
  65. &insere_dados;
  66. }
  67. $mostra_dados=1;
  68. &formulario;
  69. }
  70. else {
  71. &formulario;
  72. }
  73. }
  74. else {
  75. &formulario;
  76. }
  77. $dbh->disconnect;
  78. exit;
  79. ########################################################
  80. # CORRIGE DADOS #
  81. ########################################################
  82. sub formulario{
  83. print <<EOM;
  84. <html>
  85. <head>
  86. <title>Maputo Admin</title>
  87. </head>
  88. <link rel="stylesheet" type="text/css" href="/css/estilo.css">
  89. <body leftmargin="5" marginwidth="5" topmargin="5" marginheight="5" >
  90. EOM
  91. if($FORM{'ID'} eq ""){
  92. print <<EOM;
  93. <span class="titre2">Add User</span>
  94. <br><br>
  95. EOM
  96. }
  97. else {
  98. print <<EOM;
  99. <span class="titre2">Edit User</span>
  100. <br><br>
  101. EOM
  102. }
  103. if($erro_dados == 1){
  104. print <<EOM;
  105. <span class="titre3"><font color=red>Please correct the fields with <font size=-0>*</font></font></span>
  106. EOM
  107. }
  108. print <<EOM;
  109. <table border=0 cellpadding=2>
  110. <form method="post" action="/cgi-bin/add_user.pl">
  111. <input type=hidden name="ID" value="$FORM{'ID'}">
  112. <input type=hidden name="INSERT" value="ok">
  113. EOM
  114. if($FORM{'INSERT'} ne "ok" && $mostra_dados == 0){
  115. if($FORM{'ID'} ne ""){
  116. $sth=$dbh->prepare("select * from USER where ID='$FORM{'ID'}'");
  117. $sth->execute;
  118. @row = $sth->fetchrow_array();
  119. $FORM{'LOGIN'} = $row[1];
  120. $FORM{'PASSWORD'} = $row[2];
  121. $FORM{'NAME'} = $row[4];
  122. $FORM{'EMAIL'} = $row[5];
  123. $sth->finish;
  124. }
  125. }
  126. print<<EOM;
  127. <tr>
  128. <td align=right>
  129. <span class=titre3>Login: </span>
  130. </td>
  131. <td>
  132. EOM
  133. if($mostra_dados == 0){
  134. print <<EOM;
  135. <span class="searchField"><input type="text" name="LOGIN" size="60" style="font-family: Arial; font-size: 11; color:#000000; background:#EEEEEE;" value="$FORM{'LOGIN'}"></span>
  136. EOM
  137. }
  138. else {
  139. print <<EOM;
  140. <span class=titre3>$FORM{'LOGIN'} </span>
  141. EOM
  142. }
  143. print <<EOM;
  144. </td>
  145. </tr>
  146. <tr>
  147. <td align=right>
  148. <span class=titre3>Password: </span>
  149. </td>
  150. <td>
  151. EOM
  152. if($mostra_dados == 0){
  153. print <<EOM;
  154. <span class="searchField"><input type="password" name="PASSWORD" size="60" style="font-family: Arial; font-size: 11; color:#000000; background:#EEEEEE;" value="$FORM{'PASSWORD'}"></span>
  155. EOM
  156. }
  157. else {
  158. print <<EOM;
  159. <span class=titre3>$FORM{'PASSWORD'} </span>
  160. EOM
  161. }
  162. print <<EOM;
  163. </td>
  164. </tr>
  165. <tr>
  166. <td align=right>
  167. <span class=titre3>Name: </span>
  168. </td>
  169. <td>
  170. EOM
  171. if($mostra_dados == 0){
  172. print <<EOM;
  173. <span class="searchField"><input type="text" name="NAME" size="60" style="font-family: Arial; font-size: 11; color:#000000; background:#EEEEEE;" value="$FORM{'NAME'}"></span>
  174. EOM
  175. }
  176. else {
  177. print <<EOM;
  178. <span class=titre3>$FORM{'NAME'} </span>
  179. EOM
  180. }
  181. print <<EOM;
  182. </td>
  183. </tr>
  184. <tr>
  185. <td align=right>
  186. <span class=titre3>Email: </span>
  187. </td>
  188. <td>
  189. EOM
  190. if($mostra_dados == 0){
  191. print <<EOM;
  192. <span class="searchField"><input type="text" name="EMAIL" size="60" style="font-family: Arial; font-size: 11; color:#000000; background:#EEEEEE;" value="$FORM{'EMAIL'}"></span>
  193. EOM
  194. }
  195. else {
  196. print <<EOM;
  197. <span class=titre3>$FORM{'EMAIL'} </span>
  198. EOM
  199. }
  200. print <<EOM;
  201. </td>
  202. </tr>
  203. </table>
  204. EOM
  205. $sth=$dbh->prepare("select ID from ACCESS where IDUSER='$ID' and TABLE_NAME='ACCESS' and R=1 and W=1 and D=1");
  206. $sth->execute;
  207. @row = $sth->fetchrow_array();
  208. $table_access=$row[0];
  209. $sth->finish;
  210. if($table_access ne ""){
  211. print <<EOM;
  212. <table border=1 cellpadding=2>
  213. EOM
  214. $sth=$dbh->prepare("select * from ACCESS where ID='$FORM{'ID'}'");
  215. $sth->execute;
  216. @row = $sth->fetchrow_array();
  217. $sth->finish;
  218. print<<EOM;
  219. <tr>
  220. <td bgcolor=black><span class=titre3><b><font color=white>Table</font></b></span></td>
  221. <td bgcolor=black><span class=titre3><b><font color=white>Read</font></b></span></td>
  222. <td bgcolor=black><span class=titre3><b><font color=white>Write</font></b></span></td>
  223. <td bgcolor=black><span class=titre3><b><font color=white>Delete</font></b></span></td>
  224. </tr>
  225. EOM
  226. $sth2=$dbh->prepare("select TABLE_NAME from TABLE_LIST order by TABLE_NAME");
  227. $sth2->execute;
  228. while((@row2) = $sth2->fetchrow_array()){
  229. $estado_r="";
  230. $estado_w="";
  231. $estado_d="";
  232. $mostra_r="&nbsp;";
  233. $mostra_w="&nbsp;";
  234. $mostra_d="&nbsp;";
  235. if($FORM{'INSERT'} ne "ok" && $mostra_dados == 0){
  236. if($FORM{'ID'} ne ""){
  237. $sth3=$dbh->prepare("select R, W, D from ACCESS where IDUSER='$FORM{'ID'}' and TABLE_NAME = '$row2&#91;0]'");
  238. $sth3->execute;
  239. @row3 = $sth3->fetchrow_array();
  240. if($row3&#91;0] eq "1"){
  241. $estado_r='checked="true"';
  242. $mostra_r="X";
  243. }
  244. if($row3&#91;1] eq "1"){
  245. $estado_w='checked="true"';
  246. $mostra_w="X";
  247. }
  248. if($row3&#91;2] eq "1"){
  249. $estado_d='checked="true"';
  250. $mostra_d="X";
  251. }
  252. $sth3->finish;
  253. }
  254. }
  255. else {
  256. if($FORM{"R_$row2[0]"} eq "on"){
  257. $estado_r='checked="true"';
  258. $mostra_r="X";
  259. }
  260. if($FORM{"W_$row2[0]"} eq "on"){
  261. $estado_w='checked="true"';
  262. $mostra_w="X";
  263. }
  264. if($FORM{"D_$row2[0]"} eq "on"){
  265. $estado_d='checked="true"';
  266. $mostra_d="X";
  267. }
  268. }
  269.  
  270. if($mostra_dados == 0){
  271. print <<EOM;
  272. <tr>
  273. <td><span class=titre3>$row2&#91;0]</span></td>
  274. <td align=center><input name="R_$row2[0]" type="checkbox" $estado_r></td>
  275. <td align=center><input name="W_$row2[0]" type="checkbox" $estado_w></td>
  276. <td align=center><input name="D_$row2[0]" type="checkbox" $estado_d></td>
  277. </tr>
  278. EOM
  279. }
  280. else {
  281. print <<EOM;
  282. <tr>
  283. <td><span class=titre3>$row2&#91;0]</span></td>
  284. <td align=center><span class="titre2">$mostra_r</span></td>
  285. <td align=center><span class="titre2">$mostra_w</span></td>
  286. <td align=center><span class="titre2">$mostra_d</span></td>
  287. </tr>
  288. EOM
  289. }
  290. }
  291. $sth2->finish;
  292.  
  293. print <<EOM;
  294. </td>
  295. </tr>
  296. </table>
  297. EOM
  298. }
  299.  
  300. if($mostra_dados == 0){
  301. print <<EOM;
  302. <font size=-1><INPUT TYPE=submit VALUE = "Send" style="font-family: Arial; font-size: 9; color:#000000; background:#EEEEEE;"></font>
  303. </form>
  304. EOM
  305. }
  306. else {
  307. print <<EOM;
  308. <br>
  309. <span class=titre2>&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>Info updated</font></span>
  310. EOM
  311. }
  312.  
  313. print <<EOM;
  314. </body>
  315. </html>
  316. EOM
  317.  
  318. }
  319.  
  320.  
  321. ########################################################
  322. # VERIFICA DADOS #
  323. ########################################################
  324.  
  325. sub verifica_dados{
  326. $erro_dados=0;
  327. if($FORM{'LOGIN'} eq "" || $FORM{'LOGIN'} eq "*"){
  328. $erro_dados=1;
  329. $FORM{'LOGIN'}="*";
  330. }
  331. if($FORM{'PASSWORD'} eq "" || $FORM{'PASSWORD'} eq "*"){
  332. $erro_dados=1;
  333. $FORM{'PASSWORD'}="*";
  334. }
  335. if($FORM{'NAME'} eq "" || $FORM{'NAME'} eq "*"){
  336. $erro_dados=1;
  337. $FORM{'NAME'}="*";
  338. }
  339. if($FORM{'EMAIL'} !~ /\@/ || $FORM{'EMAIL'} eq "*"){
  340. $erro_dados=1;
  341. $FORM{'EMAIL'}="*";
  342. }
  343. $sth=$dbh->prepare("select ID, LOGIN from USER where LOGIN='$FORM{'LOGIN'}'");
  344. $sth->execute;
  345. @row = $sth->fetchrow_array();
  346. if($row&#91;0] ne "" && $row[0] ne $FORM{'ID'}){
  347. $erro_dados=1;
  348. $FORM{'LOGIN'}="*";
  349. }
  350. $sth->finish;
  351.  
  352. }
  353.  
  354. ########################################################
  355. # ACTUALIZA DADOS #
  356. ########################################################
  357.  
  358. sub actualiza_dados{
  359. $dbh->do("insert into USER_HIST
  360. (IDINI, LOGIN, PASSWORD, NAME, EMAIL, IDLOGIN)
  361. values ($FORM{'ID'}, '$FORM{'LOGIN'}', '$FORM{'PASSWORD'}', '$FORM{'NAME'}', '$FORM{'EMAIL'}', $ID)");
  362. $dbh->do("update USER set
  363. LOGIN='$FORM{'LOGIN'}', PASSWORD='$FORM{'PASSWORD'}', NAME='$FORM{'NAME'}', EMAIL='$FORM{'EMAIL'}'
  364. where ID=$FORM{'ID'}");
  365.  
  366. $sth=$dbh->prepare("select ID from ACCESS where IDUSER='$ID' and TABLE_NAME='ACCESS' and R=1 and W=1 and D=1");
  367. $sth->execute;
  368. @row = $sth->fetchrow_array();
  369. $table_access=$row&#91;0];
  370. $sth->finish;
  371. if($table_access ne ""){
  372. $sth2=$dbh->prepare("select TABLE_NAME from TABLE_LIST order by TABLE_NAME");
  373. $sth2->execute;
  374. while((@row2) = $sth2->fetchrow_array()){
  375. if($FORM{"R_$row2[0]"} eq "on"){
  376. $R=1;
  377. }
  378. else {
  379. $R=0;
  380. }
  381. if($FORM{"W_$row2[0]"} eq "on"){
  382. $W=1;
  383. }
  384. else {
  385. $W=0;
  386. }
  387. if($FORM{"D_$row2[0]"} eq "on"){
  388. $D=1;
  389. }
  390. else {
  391. $D=0;
  392. }
  393.  
  394. $sth3=$dbh->prepare("select ID from ACCESS where IDUSER=$FORM{'ID'} and TABLE_NAME='$row2[0]'");
  395. $sth3->execute;
  396. @row3 = $sth3->fetchrow_array();
  397. $IDAUX=$row3&#91;0];
  398. $sth3->finish;
  399. if($IDAUX ne ""){
  400. $dbh->do("insert into ACCESS_HIST
  401. (IDINI, TABLE_NAME, IDUSER, R, W, D, IDLOGIN)
  402. values ($IDAUX, '$row2[0]', $FORM{'ID'}, $R, $W, $D, $ID)");
  403. $dbh->do("update ACCESS set
  404. R=$R, W=$W, D=$D where ID=$IDAUX");
  405. }
  406. else {
  407. $dbh->do("insert into ACCESS
  408. (TABLE_NAME, IDUSER, R, W, D)
  409. values ('$row2[0]', $FORM{'ID'}, $R, $W, $D)");
  410. $sth3=$dbh->prepare("select ID from ACCESS where IDUSER=$FORM{'ID'} and TABLE_NAME='$row2[0]'");
  411. $sth3->execute;
  412. @row3 = $sth3->fetchrow_array();
  413. $IDAUX=$row3&#91;0];
  414. $sth3->finish;
  415. $dbh->do("insert into ACCESS_HIST
  416. (IDINI, TABLE_NAME, IDUSER, R, W, D, IDLOGIN)
  417. values ($IDAUX, '$row2[0]', $FORM{'ID'}, $R, $W, $D, $ID)");
  418. }
  419. }
  420. $sth2->finish;
  421. }
  422. }
  423.  
  424. ########################################################
  425. # INSERE DADOS #
  426. ########################################################
  427.  
  428. sub insere_dados{
  429. $dbh->do("insert into USER
  430. (LOGIN, PASSWORD, NAME, EMAIL)
  431. values ('$FORM{'LOGIN'}', '$FORM{'PASSWORD'}', '$FORM{'NAME'}', '$FORM{'EMAIL'}')");
  432. $sth=$dbh->prepare("select ID from USER where LOGIN='$FORM{'LOGIN'}'");
  433. $sth->execute;
  434. @row = $sth->fetchrow_array();
  435. $IDAUX=$row&#91;0];
  436. $sth->finish;
  437. $dbh->do("insert into USER_HIST
  438. (IDINI, LOGIN, PASSWORD, NAME, EMAIL, IDLOGIN)
  439. values ($IDAUX , '$FORM{'LOGIN'}', '$FORM{'PASSWORD'}', '$FORM{'NAME'}', '$FORM{'EMAIL'}', $ID)");
  440.  
  441. $sth2=$dbh->prepare("select TABLE_NAME from TABLE_LIST order by TABLE_NAME");
  442. $sth2->execute;
  443. while((@row2) = $sth2->fetchrow_array()){
  444. if($FORM{"R_$row2[0]"} eq "on"){
  445. $R=1;
  446. }
  447. else {
  448. $R=0;
  449. }
  450. if($FORM{"W_$row2[0]"} eq "on"){
  451. $W=1;
  452. }
  453. else {
  454. $W=0;
  455. }
  456. if($FORM{"D_$row2[0]"} eq "on"){
  457. $D=1;
  458. }
  459. else {
  460. $D=0;
  461. }
  462.  
  463. $dbh->do("insert into ACCESS
  464. (TABLE_NAME, IDUSER, R, W, D)
  465. values ('$row2[0]', $IDAUX, $R, $W, $D)");
  466. $sth3=$dbh->prepare("select ID from ACCESS where IDUSER=$IDAUX and TABLE_NAME='$row2[0]'");
  467. $sth3->execute;
  468. @row3 = $sth3->fetchrow_array();
  469. $IDAUX2=$row3&#91;0];
  470. $sth3->finish;
  471. $dbh->do("insert into ACCESS_HIST
  472. (IDINI, TABLE_NAME, IDUSER, R, W, D, IDLOGIN)
  473. values ($IDAUX2, '$row2[0]', $IDAUX, $R, $W, $D, $ID)");
  474. }
  475. $sth2->finish;
  476.  
  477. }

Last edited by Nico; 06-15-07 at 09:05 AM. Reason: Please use [highlight=perl] wrappers.
Reply With Quote
  #2 (permalink)  
Old 06-21-07, 05:08 AM
hostilio hostilio is offline
New Member
 
Join Date: Jun 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Please help me...
Reply With Quote
  #3 (permalink)  
Old 06-21-07, 05:38 AM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
It is generally not status quo to ask someone on here to port an entire script but rather assist you with debugging code. This should really be moved to script requests or more so to the jobs area. Ideally, you require not only a translator, a person well versed in PHP as well as Perl.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent need to convert small snippet from perl to asp.net (asp) hejlsberg ASP.NET 3 04-21-06 06:56 AM
Translate ASP to PHP and PERL POSERA ASP 0 06-12-04 01:12 AM
Perl code : print "." x 20; - How to do it with PHP ? kevin PHP 2 07-04-03 04:29 AM


All times are GMT -5. The time now is 04:47 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.