-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyphpmysql.py
812 lines (772 loc) · 26.9 KB
/
easyphpmysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
#-------------------------------
#!/usr/bin/python
#-----------------------EasyPHPMYSQL-------------------------------
#Copyrighted by Matthew Grant on 09.09.2013
#
#EasyPHPMYSQL is a command-line python script that is used to
#write a PHP class files that connect and manipulate data in a
#MYSQL database just by answering a couple question about the
#database. Also, it writes an optional login script for an
#administrator backend and an optional example script that shows
#a person how to use the php class.
#
#Free to use as you wish (MIT license)
#
#Proud member of the Zeitgest-Movement
#-----------------------------------------------------------------
import sys, getopt, MySQLdb, os
#----------Global Variable List---------------
host=''
user=''
password=''
filename=''
login=0
database=''
example=0
name=''
logintable=''
loginuser=''
loginpass=''
#----------Functions---------------------------
def usage():
# This is the function that handles the help flag
print '------------Easyphpsql Help------------------------------------------------'
print "-h --help Display help for easyphpsql"
print "-u --user The user for the database"
print "-s --server The host of the database"
print "-p --password The password for the user of the database"
print "-d --database The name of the database"
print "-l --login Adds login function to the php class <table:username:password>"
print "-e --example Add example of the usage of the php class"
print "-n --name The name of the php class that will be created"
print "-x --table The name of the table that has the login information
print "-y --username The name of the column that holds the login username
print "-z --password The name of the column that holds the login password
print "---------------------------------------------------------------------------"
return
def checkarg():
global host
global user
global password
global filename
global login
global database
global example
global name
c1=0
if host=='':
host=raw_input("What is the host name for the database (ie - localhost)\n ")
c1+=1
if user=='':
user=raw_input("What is the userename for the database \n")
c1+=1
if password=='':
password=raw_input("What is the password for the database \n")
c1+=1
if database=='':
database=raw_input("What is the name of the database \n")
c1+=1
if filename=='':
filename=raw_input("What is the filename for the php class \n")
c1+=1
if name=='':
name=raw_input("What is the name of the php class \n")
c1+=1
if (c1>3):
flag1=0
while (flag1==0):
login1=raw_input("Do you want login script in the php class (y)es or (n)o\n")
if (login1=="yes" or login1=="y" or login1=='Y' or login1=="YES" or login1=="Yes"):
login=1
flag1=1
logintable=raw_input("What is the name of the table with the login information\n")
loginuser=raw_input("What is the column name that holds the username\n")
loginpass=raw_input("What is the column name that holds the password\n")
elif (login1=="no" or login1=="n" or login1=='N' or login1=="NO" or login1=="No"):
login=0
flag1=1
else:
print "[warning] Did not recognized the input you entered\n"
if (c1>3):
flag1=0
while (flag1==0):
example1=raw_input("Do you want example php files to be wrote to demostrate the php class (y)es or (n)o\n")
if (example1=="yes" or example1=="y" or example1=='Y' or example1=="YES" or example1=="Yes"):
example=1
flag1=1
elif (example1=="no" or example1=="n" or example1=='N' or example1=="NO" or example1=="No"):
example=0
flag1=1
else:
print "[warning] Did not recognized the input you entered\n"
return
def main(argv):
try:
opts, args = getopt.getopt(argv, 'hs:u:p:o:ld:en:x:y:z:',['help=''server=','user=','password=','output=','login=','database=','example=','name=','table=','username=', 'password='])
except getopt.GetoptError:
print 'There was an error in the format of FileCreator option'
print 'Enter filecreator.py -h for help'
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit()
elif opt in ('-s','--server='):
global host
host=arg
# replace with a call to a function to handle this flag
elif opt in ('-u','--user='):
global user
user=arg
# replace with a call to a function to handle this flag
elif opt in ('-p','--password='):
global password
password=arg
# replace with a call to a function to handle this flag
elif opt in ('-o','--output='):
global filename
filename=arg
# replace with a call to a function to handle this flag
elif opt in ('-l','--login='):
# replace with a call to a function to handle this flag
global login
login=1
elif opt in ('-e','--example='):
# replace with a call to a function to handle this flag
global example
example=1
elif opt in ('-d','--database='):
# replace with a call to a function to handle this flag
global database
database=arg
elif opt in ('-n','--name='):
# replace with a call to a function to handle this flag
global name
name=arg
#------------------Classes--------------------------------
class easyphpsql:
tablelist=[]
colnamelist=[]
colid=0
def __init__(self, host, user, password, database):
self.host = host
self.user = user
self.password = password
self.database = database
def connectdb(self):
db = MySQLdb.connect(self.host,self.user,self.password,self.database)
cursor = db.cursor()
return (db, cursor)
def gettables(self):
db,cursor=self.connectdb()
cursor.execute('SHOW TABLES')
results = cursor.fetchall()
print "------------Tables for "+database+"------------"
c=0
for tblist in results:
self.tablelist.append(str(results[c][0]))
#print str(results[c][0])
print self.tablelist[c]
c+=1
print "-----------------------------------------------"
return
def getcolumns(self,table):
del self.colnamelist[:]
db,cursor=self.connectdb()
cursor.execute('DESCRIBE '+table)
results = cursor.fetchall()
print "------------Columns for "+table+"------------"
for clist in results:
#tablelist.append(str(results[c][0]))
#print str(results[c][0])
print clist
print "-----------------------------------------------"
numrows = len(results) # 3 rows in your example
numcols = len(results[0]) # 2 columns in your example
print "-----------column names-----------------------"
c1=0
for num in range(0,numrows):
self.colnamelist.append(results[num][0])
print results[num][0]
print "-----------------------------------------------"
return
def phpconstruct(self,host,user,password,database):
str1 = "function __construct(){\n"
str1+= " $this->host=\""+host+"\";\n"
str1+= " $this->user=\""+user+"\";\n"
str1+= " $this->password=\""+password+"\";\n"
str1+= " $this->database=\""+database+"\";\n"
str1+= "}\n"
return str1
def phpconnect(self):
str1 = "public function connectdb(){\n"
str1+= " $mysqli = new mysqli($this->host, $this->user, $this->password, $this->database);\n"
str1+= " if ($mysqli->connect_errno) {\n"
str1+= " $strerror=\"Connect failed: $mysqli->connect_error\";\n"
str1+= " $this->writeerror($strerror);\n"
str1+= " exit();\n"
str1+= " }\n"
str1+= " return $mysqli;\n"
str1+= "}\n"
return str1
def phpclassintro(self):
str1 = "<?php\n"
str1+= "class "+name+" {\n"
str1+= "public $host;\n"
str1+= "public $user;\n"
str1+= "public $password;\n"
str1+= "public $database;\n"
str1+= "public $tables = array();\n"
str1+= "public $columns = array();\n"
str1+= "public $colid;\n"
return str1
def phpclassclose(self):
str1 = "}\n"
str1+= "?>\n"
return str1
def phpadd(self,table):
str1="public function "+table+"add ("
for list1 in self.colnamelist:
str1+="$"+list1+","
str1=str1[:-1]
str1+="){\n"
for list1 in self.colnamelist:
str1+= " $"+list1+" = stripslashes($"+list1+");\n"
str1+= " $"+list1+" = $mysqli->real_escape_string($"+list1+");\n"
str1+= " $query=\"INSERT INTO "+table+" ("
for list2 in self.colnamelist:
str1+=list2+","
str1=str1[:-1]
str1+=") VALUES ("
for list2 in self.colnamelist:
str1+="$"+list2+","
str1=str1[:-1]
str1+=")\";\n"
str1+= " $result=$this->doquery($query);\n"
str1+= " $result->close();\n"
str1+= " return $result;\n"
str1+= "}\n"
return str1
def phpadd1(self):
str1 = "public function addtotable($table,$col,$mysqli){\n"
str1+= " $str1 = '';\n"
str1+= " $str2 = '';\n"
str1+= " $this->getcolumns($table,$mysqli);\n"
str1+= " $columnid1=$this->getid($mysqli,$table);\n"
str1+= " $query=\"Insert into $table (\";\n"
str1+= " $count2=0;\n"
str1+= " foreach ($this->columns as $value){\n"
str1+= " if ($count2!=$this->colid){
str1+= " $str1.=\"$value,\";\n"
str1+= " }\n"
str1+= " $count2++;\n"
str1+= " }\n"
str1+= " $str1 = substr($str1, 0, -1);\n"
str1+= " $str1.=\") VALUES (\";\n"
str1+= " foreach ($col as $value){\n"
str1+= " $str2.=\"'$value',\";\n"
str1+= " }\n"
str1+= " $str2 = substr($str2, 0, -1);\n"
str1+= " $str2 .=\")\";\n"
str1+= " $query = $query.$str1.$str2;\n"
str1+= " $result=$this->doquery($query,$mysqli);\n"
str1+= " return $result;\n"
str1+= "}\n"
return str1
def phpedit(self,table):
self.findid(table)
str1="public function "+table+"edit ("
for list1 in self.colnamelist:
str1+="$"+list1+","
str1=str1[:-1]
str1+="){\n"
for list1 in self.colnamelist:
str1+= " $"+list1+" = stripslashes($"+list1+");\n"
str1+= " $"+list1+" = $mysqli->real_escape_string($"+list1+");\n"
str1+= " $query=\"UPDATE "+table+" SET "
for list2 in self.colnamelist:
str1+=list2+"=$"+list2+","
str1=str1[:-1]
str1+=" WHERE "+self.colnamelist[self.colid]+"='$"+self.colnamelist[self.colid]+"'\";\n"
str1+= " $result=$this->doquery($query);\n"
str1+= " $result->close();\n"
str1+= " return $result;\n"
str1+= "}\n"
return str1
def phpedit1(self):
str1 = "public function edittable($table,$coldata,$id,$mysqli){\n"
str1+= " $this->getcolumns($table,$mysqli);\n"
str1+= " $columnid1=$this->getid($mysqli,$table);\n"
str1+= " $col1=$this->columns[$this->colid];\n"
str1+= " $query=\"UPDATE $table SET \";\n"
str1+= " $count2=0;\n"
str1+= " foreach ($this->$columns as $value){\n"
str1+= " if ($count2!=$this->colid){
str1+= " $str1+=\"$value='$coldata[$count2]',\";\n"
str1+= " $count2++;\n"
str1+= " }
str1+= " }\n"
str1+= " $str1 = substr(str1, 0, -1);\n"
str1+= " $query .=\" where id='$id'\""
str1+= " $result=$this->doquery($query,$mysqli);\n"
str1+= " $result->close();\n"
str1+= " return $result;\n"
str1+= "}\n"
return str1
def phpdelete(self,table):
str1="public function "+table+"delete ("
str1+="$"+self.colnamelist[0]+"){\n"
str1+= " $"+self.colnamelist[0]+" = stripslashes($"+self.colnamelist[0]+");\n"
str1+= " $"+self.colnamelist[0]+" = $mysqli->real_escape_string($"+self.colnamelist[0]+");\n"
str1+=" $query=\"DELETE FROM "+table+" WHERE "+self.colnamelist[0]+"='$"+self.colnamelist[0]+"'\";\n"
str1+= " $result=$this->doquery($query);\n"
str1+= " $result->close();\n"
str1+= " return $result;\n"
str1+= "}\n"
return str1
def phpdelete1(self):
str1 = "public function deletefromtable($table,$id,$mysqli){\n"
str1+= " $table = stripslashes($table);\n"
str1+= " $table = $mysqli->real_escape_string($table);\n"
str1+= " $id = stripslashes($id);\n"
str1+= " $id = $mysqli->real_escape_string($id);\n"
str1+= " $this->getcolumns($table,$mysqli);\n"
str1+= " $columnid1=$this->getid($mysqli,$table);\n"
str1+= " $col1=$this->columns[$this->colid];\n"
str1+= " $query=\"DELETE FROM $table WHERE $col1='$id'\";\n"
str1+= " $result=$this->doquery($query,$mysqli);\n"
str1+= " return $result;\n"
str1+= "}\n"
return str1
def phpgetone(self,table):
str1="public function "+table+"getone ("
str1+="$"+self.colnamelist[0]+"){\n"
str1+=" $query=\"SELECT * FROM "+table+" WHERE "+self.colnamelist[0]+"='$"+self.colnamelist[0]+"'\";\n"
str1+= " $result=$this->doquery($query);\n"
str1+= " $row = $result->fetch_array(MYSQLI_BOTH);\n"
str1+= " $result->close();\n"
str1+= " return $row;\n"
str1+= "}\n"
return str1
def phpgetone1(self):
str1 = "public function rowfromtable($table,$id,$mysqli){\n"
str1+= " $table = stripslashes($table);\n"
str1+= " $table = $mysqli->real_escape_string($table);\n"
str1+= " $id = stripslashes($id);\n"
str1+= " $id = $mysqli->real_escape_string($id);\n"
str1+= " $query=\"SELECT * FROM $table where $column[0]='$id'\";\n"
str1+= " $result=$this->doquery($query,$mysqli);\n"
str1+= " $row = $result->fetch_array(MYSQLI_BOTH);\n"
str1+= " $result->close();\n"
str1+= " return $row;\n"
str1+= "}\n"
return str1
def phpget(self,table):
str1="public function "+table+"getall ("
str1+="$"+self.colnamelist[0]+"){\n"
str1+=" $query=\"SELECT * FROM "+table+"\";\n"
str1+= " $result=$this->doquery($query);\n"
str1+= " return $result;\n"
str1+= "}\n"
return str1
def phpget1(self):
str1 = "public function getall ($table,$mysqli){\n"
str1+= " $query=\"SELECT * FROM $table\";\n"
str1+= " $result=$this->doquery($query,$mysqli);\n"
str1+= " return $result;\n"
str1+= " $result->close();\n"
str1+= " return 1;\n"
str1+= "}\n"
return str1
def phpchecklogin(self,logintable,loginuser,loginpass):
str1 = "public function checklogin($username,$password,$mysqli){\n"
str1+= " $username = stripslashes($username);\n"
str1+= " $username = $mysqli->real_escape_string($username);\n"
str1+= " $password = stripslashes($password);\n"
str1+= " $password = $mysqli->real_escape_string($password);\n"
str1+= " $query=\"SELECT "+loginpass+" FROM "+logintable+" where "+loginuser+"='$username'\";\n"
str1+= " $result=$this->doquery($query,$mysqli);\n"
str1+= " $row = $result->fetch_array(MYSQLI_BOTH);\n"
str1+= " $password1=$row['password'];\n"
str1+= " if ($password==$password1){\n"
str1+= " return 1;\n"
str1+= " }\n"
str1+= " else{\n"
str1+= " return 0;\n"
str1+= " }\n"
str1+= "}\n"
return str1
def javascript1(self):
print "<script>"
print " delete (id){"
print " res=confirm('Are you sure you want to delete this data');"
print " if (res==true){"
print " location.href = 'admin.php?method=delete&id='+id"
print " }"
print " }"
print "</script>"
return
def phpprinttable(self, table):
return
def phpprinttable1(self):
str1 = "public function printtable($table,$mode){\n"
str1+= "// Mode can be 0 -> just table no buttons\n"
str1+= "// 1 -> table + change and delete button\n"
str1+= " $this->getcolumns($table,$mysqli);\n"
str1+= " $colnum=count($this->columns);\n"
str1+= " $columnid1=$this->getid($mysqli,$table);\n"
str1+= " $columnid=$this->colid;\n"
str1+= " echo \"<table border='1'>\";\n"
str1+= " echo '<tr>';\n"
str1+= " $count2=0;
str1+= " foreach ($this->columns as $value){\n"
str1+= " if ($count2!=$this->colid){\n"
str1+= " echo \"<td>$values</td>\";\n"
str1+= " }\n"
str1+= " $count2++;\n"
str1+= " }\n"
str1+= " if ($mode==1){\n"
str1+= " echo '<td></td>';\n"
str1+= " }\n"
str1+= " echo '</tr>';\n"
str1+= " $result=$this->getall($table);\n"
str1+= " while($row = $result->fetch_array(MYSQLI_BOTH)){\n"
str1+= " if ($mode==1){\n"
str1+= " echo \"<form method='GET'>\";\n"
str1+= " }\n"
str1+= " echo '<tr>';\n"
str1+= " for ($c1=0;$c1<$colnum;$c1++){\n"
str1+= " if ($c1!=$this->colid){\n"
str1+= " if ($mode==1){\n"
str1+= " echo \"<td><input type='text' name='$col1[$c1]' id='$col1[$c1]' value='$row[$c1]'></td>\";\n"
str1+= " }\n"
str1+= " else{\n"
str1+= " echo '<td>$row[$c1]</td>';\n"
str1+= " }\n"
str1+= " }\n"
str1+= " }\n"
str1+= " if ($mode==1){\n"
str1+= " echo '<td>';\n"
str1+= " echo \"<input type='submit' value='Edit' id='method' name='method'> <input type='button' value='Delete' id='delete' name='delete' onclick='delete1($row[$columnid]);'>\";\n"
str1+= " echo \"<input type='hidden' name='table' id='table' value='$table'>\";\n"
str1+= " echo \"<input type='hidden' name='id' id='id' value='$row[$columnid]'>\";\n"
str1+= " echo '</form>';\n"
str1+= " echo '</td>';\n"
str1+= " }\n"
str1+= " echo '</tr>';\n"
str1+= " }\n"
str1+= " echo '</table>';\n"
str1+= " return 1;\n"
str1+= "}\n"
return str1
def phpquery(self):
str1 = "public function doquery($query,$mysqli){\n"
str1+= " $result = $mysqli->query($query);\n"
str1+= " if (!$result){\n"
str1+= " $strerror=\"Error in query: $mysqli->error\";\n"
str1+= " $this->writeerror($strerror);\n"
str1+= " return 0;\n"
str1+= " }\n"
str1+= " return $result;\n"
str1+= "}\n"
return str1
def phpgettables(self):
str1 = "public function gettables($mysqli){\n"
str1+= " $query = 'show tables';\n"
str1+= " $result=$this->doquery($query,$mysqli);\n"
str1+= " while ($row = $result->fetch_array(MYSQLI_BOTH)){\n"
str1+= " $this->tables[]=$row[0];\n"
str1+= " }\n"
str1+= " $result->close();\n"
str1+= " return 1;\n"
str1+= "}\n"
return str1
def phpgetcolumns(self):
str1 = "public function getcolumns($table,$mysqli){\n"
str1+= " $query = \"describe $table\";\n"
str1+= " $result=$this->doquery($query,$mysqli);\n"
str1+= " unset ($this->columns);\n"
str1+= " while ($row = $result->fetch_array(MYSQLI_BOTH)){\n"
str1+= " $this->columns[]=$row[0];\n"
str1+= " }\n"
str1+= " $result->close();\n"
str1+= " return 1;\n"
str1+= "}\n"
return str1
def phperror(self):
str1 = "private function writeerror($strerror){\n"
str1+= " $filename = './logs/errorlog.txt';\n"
str1+= " $tstamp1 = new DateTime('NOW');\n"
str1+= " $tstamp=$tstamp1->format('Y-m-d H:i:s');\n"
str1+= " $remoteadd = $_SERVER['REMOTE_ADDR'];\n"
str1+= " $str1=\"$tstamp - $remoteadd - $strerror\\n\";\n"
str1+= " if (is_writable($filename)) {\n"
str1+= " if (!$handle = fopen($filename, 'a')) {\n"
str1+= " //echo \"Cannot open file ($filename)\";\n"
str1+= " exit;\n"
str1+= " }\n"
str1+= " if (fwrite($handle, $str1) === FALSE) {\n"
str1+= " //echo \"Cannot write to file ($filename)\";\n"
str1+= " exit;\n"
str1+= " }\n"
str1+= " //echo \"Success, wrote ($somecontent) to file ($filename)\";\n"
str1+= " }\n"
str1+= " else {\n"
str1+= " //echo \"The file $filename is not writable\";\n"
str1+= " }\n"
str1+= " fclose($handle);\n"
str1+= " return 1;\n"
str1+= "}\n"
return str1
def findid(self,table):
db,cursor=self.connectdb()
cursor.execute('DESCRIBE '+table)
results = cursor.fetchall()
numrows = len(results) # 3 rows in your example"
for num in range(0,numrows):
#self.colnamelist.append(results[num][3])
print results[num][3]
if results[num][3]=="PRI":
self.colid=num
return
def phpfindid(self):
str1 = "public function getid($table){\n"
str1+= " $c=0;\n"
str1+= " $query = \"describe $tables\";\n"
str1+= " $result=$this->doquery($query);\n"
str1+= " while ($row = $result->fetch_array(MYSQLI_BOTH)){\n"
str1+= " if ($row[3]=='PRI'){\n"
str1+= " $this->colid=$c;\n"
str1+= " $c++;\n"
str1+= " }\n"
str1+= " }\n"
str1+= " $result->close();\n"
str1+= " return 1;\n"
str1+= "}\n"
return str1
def phplogin(self,name,filename):
str1="<?php\n"
str1+="include '"+filename+".php';\n"
str1+="$"+name+"= new "+name+"(self.host,self.user,self.password,self.database)\n;"
str1+="$link=$"+name+"->connectdb();\n"
str1+="$e1=\"<p></p>\";"
str1+="if (isset($_POST['Login'])){\n"
str1+=" if (isset($_POST['username']) && isset($_POST['password']) && $_POST['username']!='' && $_POST['password']!=''){\n"
str1+=" $username = $_POST['username'];\n"
str1+=" $password = $_POST['password'];\n"
str1+=" $username = stripslashes($username);\n"
str1+=" $username = $link->real_escape_string($username);\n"
str1+=" $password = stripslashes($password);\n"
str1+=" $password = $link->real_escape_string($password);\n"
str1+=" $password = md5($password);\n"
str1+=" $result=$"+name+"->checklogin($username, $password, $link);\n"
str1+=" }\n"
str1+=" else{\n"
str1+=" $result=0;\n"
str1+=" }\n"
str1+=" if( $result == 1 ) {\n"
str1+=" header ('location: http://localhost/admin.php');\n"
str1+=" } else {\n"
str1+=" $e1 = \"<p><font color='red'>Username and/or password incorrect.</font></p>\";\n"
str1+=" callhtml($e1);\n"
str1+=" }\n"
str1+="}\n"
str1+="else{\n"
str1+=" callhtml($e1);\n"
str1+="}\n"
str1+="function callhtml(){\n"
str1+=" echo '<html>';\n"
str1+=" echo '<head>';\n"
str1+=" echo '</head>';\n"
str1+=" echo '<body>';\n"
str1+=" echo '<center>';\n"
str1+=" echo '<h1>Login Sc0ript</h1>';\n"
str1+=" echo \"<form action='login.php' method='POST'>\";\n"
str1+=" echo $e1;\n"
str1+=" echo '<table>';\n"
str1+=" echo '<tr>';\n"
str1+=" echo '<td>Username</td>';\n"
str1+=" echo \"<td><input type='text' id='username' name='username'></td>\";\n"
str1+=" echo '</tr>';\n"
str1+=" echo '<tr>';\n"
str1+=" echo '<td>Password</td>';\n"
str1+=" echo \"<td><input type='password' id='password' name='password'></td>\";\n"
str1+=" echo '</tr>';\n"
str1+=" echo '<tr>';\n"
str1+=" echo \"<td colspan='2' align='center'><input type='submit' value='Login' id='Login' name='Login'></td>\";\n"
str1+=" echo '</tr>';\n"
str1+=" echo '</table>';\n"
str1+=" echo '</form>';\n"
str1+=" echo '</center>';\n"
str1+=" echo '</body>';\n"
str1+=" echo '</html>';\n"
str1+="}\n"
str1+="?>\n"
return str1
def phplogout(self):
str1 ="<?php\n"
str1+="session_start();\n"
str1+="session_destroy();\n"
str1+="header ('location: http://localhost/rain/admin/login.php');\n"
str1+="?>\n"
return str1
def phpexample(self):
str1 = "<?php\n"
str1+= "include \""+filename+".php\";\n"
str1+= "session_start();\n"
str1+= "$r1='';\n"
str1+= "$"+name+" = new "+name+";\n"
str1+= "$link=$"+name+"->connectdb();\n"
str1+= "$int1=$"+name+"->gettables($link);\n"
str1+= "if (isset($_GET['table'])){\n"
str1+= " $table=$_GET['table'];\n"
str1+= "}\n"
str1+= "else{\n"
str1+= " $table=$"+name+"->tables[0];\n"
str1+= "}\n"
str1+= "$t1=$"+name+"->tables;\n"
str1+= "$columnid1=$test->getid($mysqli,$table);\n"
str1+= "$int1=$"+name+"->getcolumns($table,$link);\n"
str1+= "$c1=$"+name+"->columns;\n"
str1+= "$colnum=$count($"+name+"->columns);\n"
str1+= "if (isset($_GET['method'])){\n"
str1+= " $method=$_GET['method'];\n"
str1+= " $id=$_GET['id'];\n"
str1+= " if ($method=='Delete'){\n"
str1+= " $r1=$"+name+"->deletefromtable($table,$id);\n"
str1+= " }\n"
str1+= " elseif ($method=='Edit'){\n"
str1+= " //$c1=0;\n"
str1+= " $count2=0;\n"
str1+= " foreach($"+name+"->columns as $value){\n"
str1+= " if ($count2!=$test->colid){\n"
str1+= " $coldata[]=$_GET[$value];\n"
str1+= " }\n"
str1+= " $count2++;\n"
str1+= " }\n"
str1+= " $r1=$"+name+"->edittable($mysqli,$table,$id,$coldata);\n"
str1+= " }\n"
str1+= " elseif ($method=='Add'){\n"
str1+= " //$c1=0;\n"
str1+= " $count2=0;\n"
str1+= " foreach($"+name+"->columns as $value){\n"
str1+= " if ($count2!=$test->colid){\n"
str1+= " $coldata[]=$_GET[$value];\n"
str1+= " }\n"
str1+= " $count2++;\n"
str1+= " }\n"
str1+= " $r1=$"+name+"->addtotable($table,$coldata);\n"
str1+= " }\n"
str1+= "}\n"
str1+= "?>\n"
str1+= "<html>\n"
str1+= "<head>\n"
str1+= "<script>\n"
str1+= " function delete1(id){\n"
str1+= " res=confirm(\"Are you sure you want to delete this data\");\n"
str1+= " if (res==true){\n"
str1+= " location.href = \"admin.php?method=delete&id=\"+id+\"&table=<?=$table ?>\";\n"
str1+= " }\n"
str1+= " }\n"
str1+= "</script>\n"
str1+= "</head>\n"
str1+= "<body>\n"
str1+= "<center>\n"
str1+= "<h1>Administrator Script</h1>\n"
str1+= "<table>\n"
str1+= "<tr>\n"
str1+= "<?php\n"
str1+= "foreach($t1 as $value){\n"
str1+= " echo \"<td><a href='admin.php?table=$value'>$value</a></td>\";\n"
str1+= "}\n"
str1+= "?>\n"
str1+= "<td><a href=\"logoff.php\">Logoff</a></td>\n"
str1+= "</tr>\n"
str1+= "</table>\n"
str1+= "<h2>Add to Database</h2>\n"
str1+= "<form method='GET' action='admin.php'>\n"
str1+= "<input type='hidden' name='table' id='table' value='<?=$table ?>'>\n"
str1+= "<table border='1'>\n"
str1+= "<tr>\n"
str1+= "<?php\n"
str1+= "$count2=0\n";
str1+= "foreach($c1 as $value){\n"
str1+= " if ($count2!=$"+name+"->colid){\n"
str1+= " echo '<td>$value</td>';\n"
str1+= " }\n"
str1+= " $count2++\n"
str1+= "}\n"
str1+= "?>\n"
str1+= "<td></td>\n"
str1+= "</tr>\n"
str1+= "<tr>\n"
str1+= "<?php\n"
str1+= "$count2=0\n";
str1+= "foreach($c1 as $value){\n"
str1+= " if ($count2!=$"+name+"->colid){\n"
str1+= " echo \"<td><input type='text' name='$value' id='$value'></td>\";\n"
str1+= " }\n"
str1+= " $count2++\n"
str1+= "}\n"
str1+= "?>\n"
str1+= "<td><input type='submit' value=' Add ' id='method' name='method'></td>\n"
str1+= "</tr>\n"
str1+= "</table>\n"
str1+= "</form>\n"
str1+= "<h2>Edit/Delete Database</h2>\n"
str1+= "<?php\n"
str1+= "$int1=$"+name+"->printtable($table, 1);\n"
str1+= "?>\n"
str1+= "</center>\n"
str1+= "</body>\n"
str1+= "</html>\n"
return str1
def writefile(self,file1, str1):
# Open a file
f = open("./"+file1, "w")
f.write(str1);
# Close opend file
f.close()
return
#-------------Main Body of the Program--------
if __name__ == "__main__":
main(sys.argv[1:])
#---------------------------------------------
checkarg()
easyphpsql = easyphpsql(host,user,password,database)
easyphpsql.gettables()
content=easyphpsql.phpclassintro()
content+=easyphpsql.phpconstruct(host,user,password,database)
content+=easyphpsql.phpconnect()
for table in easyphpsql.tablelist:
easyphpsql.getcolumns(table)
content+=easyphpsql.phpadd(table)
content+=easyphpsql.phpedit(table)
content+=easyphpsql.phpdelete(table)
content+=easyphpsql.phpgetone(table)
content+=easyphpsql.phpget(table)
content+=easyphpsql.phpadd1()
content+=easyphpsql.phpedit1()
content+=easyphpsql.phpdelete1()
content+=easyphpsql.phpgetone1()
content+=easyphpsql.phpget1()
content+=easyphpsql.phpquery()
content+=easyphpsql.phpgettables()
content+=easyphpsql.phpgetcolumns()
content+=easyphpsql.phperror()
content+=easyphpsql.phpfindid()
content+=easyphpsql. phpprinttable1()
if login==1:
content+=easyphpsql.phpchecklogin()
content+=easyphpsql.phpclassclose()
easyphpsql.writefile(filename+".php",content)
if login==1:
content=easyphpsql.phplogin(name,filename)
easyphpsql.writefile("login.php",content)
content=easyphpsql.phplogout()
easyphpsql.writefile("logoff.php",content)
if example==1:
content=easyphpsql.phpexample()
easyphpsql.writefile("admin.php",content)
print "The files has been written. Finished!!"
sys.exit(0)