1
1
<?php
2
2
3
3
interface AbstractTransport {
4
- public function saveMessage ($ msgid= NULL , $ echo , $ message , $ raw );
4
+ public function saveMessage ($ msgid , $ echo , $ message , $ raw );
5
5
public function updateMessage ($ msgid , $ message );
6
6
public function deleteMessage ($ msgid , $ echo =NULL );
7
7
public function deleteMessages ($ msgids , $ echo =NULL );
@@ -178,7 +178,7 @@ function countMessages($echo) {
178
178
return count ($ this ->getMsgList ($ echo ));
179
179
}
180
180
181
- function saveMessage ($ msgid= NULL , $ echo , $ message , $ raw ) {
181
+ function saveMessage ($ msgid , $ echo , $ message , $ raw ) {
182
182
if (!$ raw ) $ message =$ this ->makeRaw ($ message );
183
183
if ($ msgid == NULL ) {
184
184
$ msgid =hsh ($ message );
@@ -285,7 +285,7 @@ function insertData($msg) {
285
285
return $ this ->executeQuery ("insert into ` $ this ->tablename ` values(NULL, ' " .$ msg ['id ' ]."', ' " .$ msg ['tags ' ]."', ' " .$ msg ['echo ' ]."', ' " .$ msg ['time ' ]."', ' " .$ msg ['from ' ]."', ' " .$ msg ['addr ' ]."', ' " .$ msg ['to ' ]."', ' " .$ msg ['subj ' ]."', ' " .$ msg ['msg ' ]."') " );
286
286
}
287
287
288
- function saveMessage ($ msgid= NULL , $ echo , $ message , $ raw ) {
288
+ function saveMessage ($ msgid , $ echo , $ message , $ raw ) {
289
289
if ($ raw ) {
290
290
if (!$ msgid ) $ msgid =hsh ($ message );
291
291
$ message =$ this ->makeReadable ($ message );
@@ -449,8 +449,216 @@ function deleteEchoarea($echo, $with_contents=true) {
449
449
}
450
450
}
451
451
452
+ class PostgreBase extends TextBase implements AbstractTransport {
453
+ function __construct ($ data ) {
454
+ $ host =$ data ["host " ];
455
+ $ db_name =$ data ["db " ];
456
+ $ user =$ data ["user " ];
457
+ $ pass =$ data ["pass " ];
458
+ $ table =$ data ["table " ];
459
+ $ port =$ data ["port " ];
460
+
461
+ $ this ->db =pg_connect ("host= $ host port= $ port dbname= $ db_name user= $ user password= $ pass " );
462
+ unset($ this ->setMsgList );
463
+
464
+ $ db =$ this ->db ;
465
+ $ this ->tablename =$ table ;
466
+
467
+ if (!$ db ) die ("unable to connect to database " );
468
+ }
469
+
470
+ function __destruct () {
471
+ pg_close ($ this ->db );
472
+ }
473
+
474
+ function executeQuery ($ query ) {
475
+ $ ret = pg_query ($ this ->db , $ query );
476
+ return $ ret ;
477
+ }
478
+
479
+ function prepareInsert ($ message ) {
480
+ $ keys =array_keys ($ message );
481
+ foreach ($ keys as $ key ) {
482
+ $ message [$ key ]=pg_escape_literal ($ this ->db , $ message [$ key ]);
483
+ }
484
+ return $ message ;
485
+ }
486
+
487
+ function insertData ($ msg ) {
488
+ $ query = "insert into $ this ->tablename values(DEFAULT, " .$ msg ['id ' ].", " .$ msg ['tags ' ].", " .$ msg ['echo ' ].", " .$ msg ['time ' ].", " .$ msg ['from ' ].", " .$ msg ['addr ' ].", " .$ msg ['to ' ].", " .$ msg ['subj ' ].", " .$ msg ['msg ' ].") " ;
489
+ return $ this ->executeQuery ($ query );
490
+ }
491
+
492
+ function saveMessage ($ msgid , $ echo , $ message , $ raw ) {
493
+ if ($ raw ) {
494
+ if (!$ msgid ) $ msgid =hsh ($ message );
495
+ $ message =$ this ->makeReadable ($ message );
496
+ }
497
+ if (!$ msgid ) $ msgid =hsh (serialize ($ message ));
498
+ $ message ["id " ]=$ msgid ;
499
+
500
+ $ message ["tags " ]=$ this ->collectTags ($ message ["tags " ]);
501
+ $ message =$ this ->prepareInsert ($ message );
502
+
503
+ $ this ->insertData ($ message );
504
+
505
+ return $ msgid ;
506
+ }
507
+
508
+ function getMessages ($ msgids ) {
509
+ $ db =$ this ->db ;
510
+ $ messages =[];
511
+
512
+ $ part ="" ;
513
+
514
+ for ($ i =0 ;$ i <count ($ msgids );$ i ++) {
515
+ $ part .="id=' " .pg_escape_string ($ this ->db , $ msgids [$ i ])."' " ;
516
+ if ($ i !=count ($ msgids )-1 ) $ part .=" OR " ;
517
+ }
518
+ $ query_text ="SELECT * FROM $ this ->tablename WHERE " .$ part ;
519
+ $ query =$ this ->executeQuery ($ query_text );
520
+
521
+ if (!$ query ) {
522
+ echo pg_last_error ($ this ->db )."\n" .$ query_text ."\n" ;
523
+ return [];
524
+ }
525
+ while ($ row =pg_fetch_assoc ($ query )) {
526
+ $ msgid =$ row ["id " ];
527
+ $ messages [$ msgid ]=[
528
+ "id " => $ msgid ,
529
+ "tags " => $ this ->parseTags ($ row ["tags " ]),
530
+ "echo " => $ row ["echoarea " ],
531
+ "time " => $ row ["date " ],
532
+ "from " => $ row ["msgfrom " ],
533
+ "addr " => $ row ["addr " ],
534
+ "to " => $ row ["msgto " ],
535
+ "subj " => $ row ["subj " ],
536
+ "msg " => $ row ["msg " ]
537
+ ];
538
+ if (isset ($ messages [$ msgid ]["tags " ]["repto " ])) {
539
+ $ messages [$ msgid ]["repto " ]=$ messages [$ msgid ]["tags " ]["repto " ];
540
+ } else $ messages [$ msgid ]["repto " ]=false ;
541
+ }
542
+ $ got_msgids =array_keys ($ messages );
543
+ $ difference =array_diff ($ msgids , $ got_msgids );
544
+ if (count ($ difference ) > 0 ) {
545
+ foreach ($ difference as $ msgid ) $ messages [$ msgid ]=$ this ->nomessage ;
546
+ }
547
+ return $ messages ;
548
+ }
549
+
550
+ function getMessage ($ msgid ) {
551
+ $ data =$ this ->getMessages ([$ msgid ]);
552
+ if (isset ($ data [$ msgid ])) return $ data [$ msgid ];
553
+ else return $ this ->nomessage ;
554
+ }
555
+
556
+ function getRawMessage ($ msgid ) {
557
+ $ message =$ this ->getMessage ($ msgid );
558
+ return $ this ->makeRaw ($ message );
559
+ }
560
+
561
+ function getRawMessages ($ msgids ) {
562
+ $ messages =$ this ->getMessages ($ msgids );
563
+ $ keys =array_keys ($ messages );
564
+ $ output =[];
565
+ foreach ($ keys as $ msgid ) {
566
+ $ output [$ msgid ]=$ this ->makeRaw ($ messages [$ msgid ]);
567
+ }
568
+ return $ output ;
569
+ }
570
+
571
+ function updateMessage ($ msgid , $ message ) {
572
+ $ message ["tags " ]=$ this ->collectTags ($ message ["tags " ]);
573
+ $ message =$ this ->prepareInsert ($ message );
574
+
575
+ $ query_text ="UPDATE $ this ->tablename SET tags= " .$ message ["tags " ].
576
+ ", echoarea= " .$ message ["echo " ].
577
+ ", date= " .$ message ["time " ].
578
+ ", msgfrom= " .$ message ["from " ].
579
+ ", addr= " .$ message ["addr " ].
580
+ ", msgto= " .$ message ["to " ].
581
+ ", subj= " .$ message ["subj " ].
582
+ ", msg= " .$ message ["msg " ]." " .
583
+ "WHERE id=' " .$ msgid ."' " ;
584
+
585
+ return $ this ->executeQuery ($ query_text );
586
+ }
587
+
588
+ function deleteMessage ($ msgid , $ withecho =NULL ) {
589
+ $ query_text ="DELETE from $ this ->tablename WHERE id=' " .$ msgid ."' " ;
590
+ return $ this ->executeQuery ($ query_text );
591
+ }
592
+
593
+ function getMsgList ($ echo , $ offset =NULL , $ length =NULL ) {
594
+ $ query_text ="SELECT id, number from $ this ->tablename " .
595
+ "where echoarea=' " .$ echo ."' order by number " ;
596
+
597
+ if ($ offset !== NULL ) $ a =intval ($ offset );
598
+ else $ a =NULL ;
599
+ if ($ length !== NULL ) $ b =intval ($ length );
600
+ else $ b =NULL ;
601
+
602
+ if ($ a === NULL ) $ query_text .=" asc " ;
603
+ elseif ($ a < 0 ) {
604
+ $ a =-$ a ;
605
+ $ query_text ="SELECT * from ( $ query_text desc LIMIT $ a) as tmp order by number asc " ;
606
+ if ($ b and $ b > 0 ) $ query_text .=" LIMIT $ b " ;
607
+ } else {
608
+ if (!$ b or $ b < 0 ) $ b =18446744073709551610 ;
609
+ $ query_text .=" asc LIMIT $ b " ;
610
+ if ($ a != 0 ) $ query_text .=" OFFSET $ a " ;
611
+ }
612
+
613
+ $ query =$ this ->executeQuery ($ query_text );
614
+
615
+ if ($ query ) {
616
+ $ array =pg_fetch_all ($ query );
617
+ $ lst = [];
618
+ foreach ($ array as $ key => $ value ) {
619
+ $ lst [] = $ value ["id " ];
620
+ }
621
+ return $ lst ;
622
+ } else {
623
+ die (pg_last_error ($ this ->db )."\n" .$ query_text );
624
+ }
625
+ }
626
+
627
+ function countMessages ($ echo ) {
628
+ $ query_text ="SELECT count(*) from $ this ->tablename where echoarea=' " .$ echo ."' " ;
629
+ $ result =$ this ->executeQuery ($ query_text );
630
+
631
+ $ count =pg_fetch_row ($ result )[0 ];
632
+ return $ count ;
633
+ }
634
+
635
+ function fullEchoList () {
636
+ $ query_text ="SELECT DISTINCT echoarea from $ this ->tablename " ;
637
+ $ result =$ this ->executeQuery ($ query_text );
638
+
639
+ if (!$ result ) {
640
+ echo pg_last_error ($ this ->db )."\n" .$ query_text ."\n" ;
641
+ return [];
642
+ }
643
+
644
+ $ output =[];
645
+ while ($ row =pg_fetch_assoc ($ result )) {
646
+ $ output []=$ row ["echoarea " ];
647
+ }
648
+ return $ output ;
649
+ }
650
+
651
+ function deleteEchoarea ($ echo , $ with_contents =true ) {
652
+ $ messages =$ this ->getMsgList ($ echo );
653
+ foreach ($ messages as $ msgid ) {
654
+ $ this ->deleteMessage ($ msgid );
655
+ }
656
+ }
657
+ }
658
+
659
+
452
660
interface AbstractFileTransport {
453
- public function saveFile ($ hash= NULL , $ fecho , $ file , $ filename , $ address , $ description );
661
+ public function saveFile ($ hash , $ fecho , $ file , $ filename , $ address , $ description );
454
662
public function updateInfo ($ hash , $ fecho , $ filename , $ address , $ description );
455
663
public function deleteFile ($ hash , $ echo =NULL );
456
664
@@ -477,7 +685,7 @@ public function __construct($indexdir, $filedir) {
477
685
$ this ->filedir = $ filedir ;
478
686
}
479
687
480
- public function saveFile ($ hash= NULL , $ fecho , $ file , $ filename , $ address , $ description ) {
688
+ public function saveFile ($ hash , $ fecho , $ file , $ filename , $ address , $ description ) {
481
689
if ($ hash != NULL ) {
482
690
$ echoContents = $ this ->getFileList ($ fecho );
483
691
0 commit comments