-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasti.inc
1345 lines (1262 loc) · 53.4 KB
/
tasti.inc
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// tasti includes
require_once "tasti_config.php" ;
$dbhost=DB_HOST ;
$dbport=DB_PORT ;
$dbname=DB_NAME ;
$dbuser=DB_USER ;
$dbpass=DB_PASS ;
// connect to the database
$db = pg_connect("host=$dbhost port=$dbport dbname=$dbname user=$dbuser password=$dbpass ");
if (!$db) {
echo '<BR><span class="bad">Cannot connect to the database server.<BR><BR>';
echo pg_last_error() . '</span><BR><BR>';
}
// check whether the user has logged in
function auth_check(){
if(isset($_COOKIE['tasti_username']) && hash_check()){
$username=strtolower($_COOKIE['tasti_username']) ;
return array('1',$username);
}else{
return array('0','');
}
}
// page header
function header0($isAuthenticated=2){
list($auth_check0,$username)=auth_check() ;
if((($auth_check0 && strlen($username)) || $isAuthenticated=='1') && $isAuthenticated!='3'){
$header_string=' Hi <A HREF="account.php">' . $username . '</a> | <A HREF="login.php?do=1">Logout</a><BR><BR><UL><LI><A HREF="add.php">ADD</a> a bookmark<LI><A HREF="bmarks.php?whose=mine">MY BOOKMARKS</a></UL>' ;
}else{
$header_string=' <A HREF="login.php?do=0">Login</a> | <A HREF="register.php">Register</a> ' ;
}
echo '<span class="header_class1"><TABLE width="100%"><TR><TD><A HREF="index.php"><IMG SRC="tasti-logo.png"></a></TD><TD> </TD><TD align="right" valign="top">' . $header_string . '</TD></TR></TABLE></span>' ;
}
// page footer
function footer(){
$year=date('Y') ;
echo '<span class="footer_class1"><CENTER><A HREF="index.php">HOME</a> | <A HREF="mailto:' . ADMIN_EMAIL . '">HELP</a><BR><A HREF="https://code.google.com/p/tasti/">Tasti</a> is licensed under the <A HREF="http://www.gnu.org/licenses/gpl.html">GPL</a>. Copyright ' . $year . '<BR></CENTER></span>' ;
}
// manage bookmarks
function do_bmarks(){
global $db ;
if(isset($_GET['func']) && isset($_COOKIE['tasti_username']) && hash_check() && isset($_GET['id'])){
$username=$_COOKIE['tasti_username'] ;
$id=$_GET['id'] ;
if($_GET['func']=='del'){
$old_bmark_sql="SELECT created,url,notes,name FROM bmarks WHERE id='$id' LIMIT 1" ;
$old_bmark_qry=pg_query($db,$old_bmark_sql) ;
if(!$old_bmark_qry){
echo '<span class="bad">Old Bookmark query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
exit() ;
}
$old_created=pg_result($old_bmark_qry,0,0) ;
$old_url=pg_result($old_bmark_qry,0,1) ;
$old_notes=pg_result($old_bmark_qry,0,2) ;
$old_name=pg_result($old_bmark_qry,0,3) ;
$bmark_del_sql="DELETE FROM bmarks WHERE owner='$username' AND url='$old_url' AND name='$old_name' AND notes='$old_notes'" ;
$bmark_del_qry=pg_query($db,$bmark_del_sql) ;
if(!$bmark_del_qry){
echo '<span class="bad">Bookmark deletion FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
}else{
echo '<span class="huge">Bookmark successfully deleted</span><BR><BR>' ;
}
}
}else{
show_bmarks() ;
}
}
// edit bmarks form
function edit_bmarks_form(){
global $db ;
$username=$_COOKIE['tasti_username'] ;
$id=$_GET['id'] ;
if($_GET['func']=='edit'){
$bmarks_sql="SELECT name,url,notes FROM bmarks WHERE id='$id' AND owner='$username' LIMIT 1" ;
$bmarks_qry=pg_query($db,$bmarks_sql) ;
if(!$bmarks_qry){
echo '<span class="bad">Bookmark query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
exit() ;
}else{
$name=pg_result($bmarks_qry,0,0) ;
$url=pg_result($bmarks_qry,0,1);
$notes=pg_result($bmarks_qry,0,2);
$tags_sql="SELECT tag FROM bmarks WHERE owner='$username' AND url='$url' ORDER BY tag" ;
$tags_qry=pg_query($db,$tags_sql) ;
if(!$tags_qry){
echo '<span class="bad">Tag query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
exit() ;
}else{
$num_tags=pg_num_rows($tags_qry) ;
echo '<span class="huge">Edit this bookmark:</span><BR><BR>' ;
echo '<FORM method="POST" action="edit.php" id="edit_bmark"><TABLE>
<TR><TD>Name/Description*: </TD><TD> </TD><TD><INPUT TYPE="text" NAME="name" id="name" size="55" VALUE="' . $name . '"></TD></TR>
<TR><TD>URL*: </TD><TD> </TD><TD><INPUT TYPE="text" NAME="url" id="url" size="55" VALUE="' . $url . '"></TD></TR>
<TR><TD>Notes: </TD><TD> </TD><TD><INPUT TYPE="text" NAME="notes" id="notes" size="55" VALUE="' . $notes . '"></TD></TR>
<TR><TD>Tags: </TD><TD> </TD><TD><INPUT TYPE="text" NAME="tags" id="tags" size="55" VALUE="' ;
for($lt=0; $lt < $num_tags ; $lt++){
$tag=trim(pg_result($tags_qry,$lt,0)) ;
echo $tag . ' ' ;
}
echo '"></TD></TR><TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
<TR><TD> </TD><TD><DIV class="submit"><INPUT type="submit" value="Submit" /></TD><TD> </TD></TR>
</TABLE>
<INPUT type="hidden" id="id" name="id" value="' . $id . '">
</FORM><BR><span class="tiny"> * Required field</span><BR><BR>' ;
// display a clickable list of the user's tags that will be added to the Tags form field
$all_tags_sql="SELECT tag FROM tags WHERE owner='$username' ORDER BY tag" ;
$all_tags_qry=pg_query($db,$all_tags_sql) ;
if(!$all_tags_qry){
echo '<span class="bad">Tag query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
}
$num_users_tags=pg_num_rows($all_tags_qry) ;
if($num_users_tags>0){
echo '<span class="big">Click below to add your pre-existing tags to the bookmark above:</span><BR><BR>' ;
$row_tag_count=0 ;
for ($tagid=0; $tagid < $num_users_tags ; $tagid++){
$tagname=htmlentities(trim(pg_result($all_tags_qry,$tagid,0)), ENT_QUOTES,"UTF-8") ;
echo '<A onclick="document.getElementById(\'tags\').value=document.getElementById(\'tags\').value + \' \' + \'' . $tagname . '\';">' . $tagname . '</a> ' ;
$row_tag_count++ ;
// only display 10 tags per row
if($row_tag_count>10){
echo '<BR>' ;
$row_tag_count=0 ;
}
}
echo '<BR><BR><BR>' ;
}
}
}
}else{
echo '<span class="bad">Invalid function, or not your bookmark.</span><BR><BR><BR>' ;
}
}
// process POST'd bookmark edit data
function edit_bmarks_process(){
global $db ;
$id=$_POST['id'] ;
$username=$_COOKIE['tasti_username'] ;
$bmark_name=trim($_POST['name']) ;
$bmark_url=trim($_POST['url']) ;
if(isset($_POST['notes']) && strlen(trim($_POST['notes']))){
$bmark_notes=trim($_POST['notes']);
}else{
$bmark_notes='' ;
}
if(isset($_POST['tags']) && strlen(trim($_POST['tags']))){
$bmark_tags_string=trim($_POST['tags']);
$bmark_tags_array_dupes=explode(" ",$bmark_tags_string) ;
$bmark_tags_array=array_unique($bmark_tags_array_dupes) ;
}
$old_bmark_sql="SELECT created,url,notes,name FROM bmarks WHERE id='$id' LIMIT 1" ;
$old_bmark_qry=pg_query($db,$old_bmark_sql) ;
if(!$old_bmark_qry){
echo '<span class="bad">Old Bookmark query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
exit() ;
}
$old_created=pg_result($old_bmark_qry,0,0) ;
$old_url=pg_result($old_bmark_qry,0,1) ;
$old_notes=pg_result($old_bmark_qry,0,2) ;
$old_name=pg_result($old_bmark_qry,0,3) ;
$tr_status=0 ;
$tr_begin=pg_query($db, 'BEGIN;');
$bmark_del_sql="DELETE FROM bmarks WHERE owner='$username' AND url='$old_url' AND name='$old_name' AND notes='$old_notes'" ;
$bmark_del_qry=pg_query($db,$bmark_del_sql) ;
if(!$bmark_del_qry){
echo '<span class="bad">Bookmark delete FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
exit() ;
}
// insert tags first, if they do not already exist
foreach($bmark_tags_array as $tag){
$tag=trim($tag) ;
$userHasTag_sql="SELECT count(id) FROM tags WHERE owner='$username' AND tag='$tag'" ;
$userHasTag_query=pg_query($db,$userHasTag_sql) ;
if(!$userHasTag_query){
echo '<span class="bad">Tag query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
exit() ;
}else{
$tag_count=pg_result($userHasTag_query,0,0) ;
if($tag_count==0){
// if the tag doesn't exist, insert it as long as its not just white space
if(strlen(trim($tag))){
$tag=htmlentities($tag,ENT_QUOTES, "UTF-8") ;
$addUserTag_sql="INSERT INTO tags (owner,tag) VALUES ('$username','$tag')" ;
$addUserTag_qry=pg_query($db,$addUserTag_sql) ;
if(!$addUserTag_qry){
echo '<span class="bad">Tag INSERT FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
exit() ;
}
}
}
}
$bmark_notes=htmlentities($bmark_notes,ENT_QUOTES, "UTF-8") ;
$bmark_name=htmlentities($bmark_name,ENT_QUOTES, "UTF-8") ;
$addBmark_sql="INSERT INTO bmarks (owner,url,notes,tag,name,created) VALUES ('$username','$bmark_url','$bmark_notes','$tag','$bmark_name','$old_created')" ;
$addBmark_qry=pg_query($db,$addBmark_sql) ;
if(!$addBmark_qry){
echo '<span class="bad">Bookmark INSERT FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
exit() ;
}
}
// rollback everything if any part of the transaction failed
if($tr_status){
$tr_end = pg_query($db,'ROLLBACK;');
exit();
}
$tr_end=pg_query($db,'COMMIT;');
echo 'Bookmark <B>( ' . htmlentities($bmark_name) . ' )</B> successfully updated!<BR><BR>' ;
}
// edit bookmarks
function edit_bmarks(){
if(isset($_GET['func']) && isset($_COOKIE['tasti_username']) && hash_check()){
// display the editing form
edit_bmarks_form() ;
}elseif(isset($_POST['name']) && isset($_COOKIE['tasti_username'])){
// process the data POST'd from the edit form
edit_bmarks_process() ;
}else{
echo '<span class="bad">Invalid function, or not your bookmark.</span><BR><BR><BR>' ;
}
}
// show bookmarks
function show_bmarks(){
global $db ;
if(isset($_COOKIE['tasti_bmarks_per_page'])){
$bmarks_per_page=$_COOKIE['tasti_bmarks_per_page'] ;
}else{
$bmarks_per_page=15 ;
}
$a5_marker='' ; $a10_marker='' ; $a15_marker='' ; $a20_marker='' ; $a30_marker='' ;
if(isset($_GET['num']) && is_numeric($_GET['num'])){
$user_num_bmarks=$_GET['num'] ;
$limit_sql=' LIMIT ' . $user_num_bmarks ;
if($user_num_bmarks=='5'){
$a5_marker='* ' ;
}elseif($user_num_bmarks=='10'){
$a10_marker='* ' ;
}elseif($user_num_bmarks=='15'){
$a15_marker='* ' ;
}elseif($user_num_bmarks=='20'){
$a20_marker='* ' ;
}elseif($user_num_bmarks=='30'){
$a30_marker='* ' ;
}
$expire=time()+(3600*24*LOGIN_AGE) ;
setcookie("tasti_bmarks_per_page",$user_num_bmarks,$expire) ;
}else{
$user_num_bmarks=$bmarks_per_page ;
$limit_sql=' LIMIT ' . $user_num_bmarks ;
if($user_num_bmarks=='5'){
$a5_marker='* ' ;
}elseif($user_num_bmarks=='10'){
$a10_marker='* ' ;
}elseif($user_num_bmarks=='15'){
$a15_marker='* ' ;
}elseif($user_num_bmarks=='20'){
$a20_marker='* ' ;
}elseif($user_num_bmarks=='30'){
$a30_marker='* ' ;
}
}
// pagination setup
$page=(!isset($_GET['page']))? 1 : $_GET['page'] ;
$prev=($page - 1) ;
$next=($page + 1) ;
$max_results=$user_num_bmarks ;
// Calculate the offset
$from=(($page * $max_results) - $max_results) ;
if(isset($_GET['whose']) && $_GET['whose']=='mine' && hash_check()){
$username=$_COOKIE['tasti_username'] ;
$whose='mine' ;
$url_get_base=basename($_SERVER['REQUEST_URI']) . '&' ;
$num_bmarks_menu_offset='77' ;
$bmarks_intro='<span class="huge">Your bookmarks:</span><BR><BR>' ;
$bmarks_sql_all="SELECT distinct ON (url) url,id,date(last_update) AS last_update,notes,name,owner FROM bmarks WHERE owner='$username' ORDER BY url,last_update" ;
}elseif(isset($_GET['whose']) && $_GET['whose']!='mine'){
$owner0=$_GET['whose'] ;
$whose=$owner0 ;
$url_get_base=basename($_SERVER['REQUEST_URI']) . '&' ;
$num_bmarks_menu_offset='177' ;
$bmarks_intro='<span class="huge">' . $owner0 . '\'s bookmarks:</span><BR><BR>' ;
$bmarks_sql_all="SELECT distinct ON (url) url,id,date(last_update) AS last_update,notes,name,owner FROM bmarks WHERE owner='$owner0' ORDER BY url,last_update" ;
}else{
$num_bmarks_menu_offset='73' ;
$whose='' ;
$url_get_base=basename($_SERVER['REQUEST_URI']) . '?' ;
$bmarks_intro='<span class="huge">Recent bookmarks:</span><BR><BR>' ;
$bmarks_sql_all="SELECT distinct ON (url) url,id,date(last_update) AS last_update,notes,name,owner FROM bmarks ORDER BY url,last_update" ;
}
$bmarks_sql=$bmarks_sql_all . $limit_sql . ' OFFSET ' . $from ;
$bmarks_qry=pg_query($db,$bmarks_sql) ;
if(!$bmarks_qry){
echo '<span class="bad">Bookmark query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
exit() ;
}else{
$bmarks_qry_all=pg_query($db,$bmarks_sql_all) ;
$num_bmarks_all=pg_num_rows($bmarks_qry_all) ;
$num_bmarks=pg_num_rows($bmarks_qry) ;
if($num_bmarks>0){
echo '<TABLE><TR><TD>' . $bmarks_intro . '</TD>' ;
// only render the bmarks/page menu on the 'MY BOOKMARKS' page
if($num_bmarks_menu_offset=='77'){
echo '<TD width="' . $num_bmarks_menu_offset . '%"> </TD><TD align="center" valign="top"><div id="menu">
<UL id="item1">
<LI class="top"><B>Bookmarks/page</B></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=5"> ' . $a5_marker . '5 </a></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=10"> ' . $a10_marker . '10 </a></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=15"> ' . $a15_marker . '15 </a></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=20"> ' . $a20_marker . '20 </a></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=30"> ' . $a30_marker . '30 </a></LI></UL>
</DIV> </TD>' ;
}
echo '</TR></TABLE>' ;
for($lt=0; $lt < $num_bmarks ; $lt++){
$url=pg_result($bmarks_qry,$lt,0) ;
$id=pg_result($bmarks_qry,$lt,1) ;
$last_update=pg_result($bmarks_qry,$lt,2) ;
$notes=str_replace('&quot;','"',str_replace('&#039;',"'",htmlspecialchars_decode(htmlentities(pg_result($bmarks_qry,$lt,3))))) ;
$name=str_replace('&quot;','"',str_replace('&#039;',"'",htmlspecialchars_decode(htmlentities(pg_result($bmarks_qry,$lt,4))))) ;
$owner=pg_result($bmarks_qry,$lt,5) ;
$tags_sql="SELECT tag FROM bmarks WHERE url='$url' " ;
if(isset($username)){
$tags_sql.="AND owner='$username' " ;
}
$tags_sql.="AND length(tag)>0 GROUP BY tag ORDER BY tag LIMIT 15" ;
$tags_qry=pg_query($db,$tags_sql) ;
if(!$tags_qry){
echo '<span class="bad">Bookmark query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
exit() ;
}else{
if(strlen($notes)){
$notes_string='<BR><span class="small"><B>' . $notes . '</B></span>' ;
}else{
$notes_string='' ;
}
if(isset($username)){
$bmark_user_edit_string='<BR><A HREF="edit.php?id=' . $id . '&func=edit"><span class="normal"><B>EDIT</B></a> | <A HREF="bmarks.php?id=' . $id . '&func=del"><B>DELETE</B></a> </span>' ;
}else{
$bmark_user_edit_string='<BR><span class="normal">Created by <A HREF="bmarks.php?whose=' . $owner . '"><B>' . $owner . '</B></a> </span>' ;
}
echo '<TABLE><TR><TD valign="top"><span class="big">' . $last_update . ' </span></TD><TD width="65%"><span class="big"><A HREF="' . $url . '">' . $name . '</a></span>' . $notes_string . $bmark_user_edit_string . '</TD>' ;
$num_tags=pg_num_rows($tags_qry) ;
if($num_tags>0){
echo '<TD width="95%" valign="top" align="right">' ;
$tag_counter=0 ;
for($counter=0; $counter < $num_tags ; $counter++){
$tag=pg_result($tags_qry,$counter,0) ;
$tag_sql="SELECT id FROM tags WHERE tag='$tag' " ;
if(isset($username)){
$tag_sql.="AND owner='$username' " ;
}
$tag_sql.=" ORDER BY id LIMIT 1" ;
$tag_qry=pg_query($db,$tag_sql) ;
$tag_id=pg_result($tag_qry,0,0) ;
echo '<A HREF="tags.php?id=' . $tag_id . '">' . $tag . '</a> ' ;
$tag_counter++ ;
if($tag_counter>4){
$tag_counter=0 ;
echo '<BR>' ;
}
}
echo ' </TD>' ;
}
echo '</TR></TABLE><HR><BR>' ;
}
}
// pagination settings
$my_row_count=$num_bmarks_all ;
$total_pages=ceil($my_row_count / $max_results) ;
$pagination = '' ;
// Create a PREV link if one is needed
if($page > 1) {
$pagination.='<A STYLE="text-decoration:none" title="PREVIOUS PAGE" HREF="bmarks.php?whose=' . $whose . '&page=' . $prev . '&num=' . $user_num_bmarks;
$pagination .= '"><span class="huge"><H1>←</H1></span></A> ' ;
}
if(!strlen($whose)){
$page=0 ;
$total_pages=0 ;
}
// Create a NEXT link if one is needed
if($page < $total_pages){
$pagination .= '<A STYLE="text-decoration:none" title="NEXT PAGE" HREF="bmarks.php?whose=' . $whose . '&page=' . $next . '&num=' . $user_num_bmarks; ;
$pagination .= '"><span class="huge"><H1>→</H1></span></A>' ;
}else{
// adjust the total count when on the last page since it might not have $user_num_bmarks items remaining
$new_max_results = ($max_results - (($page * $max_results) - $my_row_count)) ;
$max_results=$new_max_results ;
}
if($my_row_count>1){
$plural='s' ;
}else{
$plural='' ;
}
echo '<TABLE width="100%"><TR COLSPAN="1"><TD> </TD></TR><TR><TD COLSPAN="9"> ' . $my_row_count . ' Tasti bookmark' . $plural . '</TD><TD
class="page" COLSPAN="4"><B>' . $pagination . '</B></TD></TR></TABLE><BR>' ;
}else{
echo '<BR>There are currently no bookmarks<BR><BR>' ;
}
}
}
// show tags
function show_tags(){
global $db ;
$num_bmarks_menu_offset='' ;
$show_mine='' ;
$tag_get='' ;
if(isset($_GET['id'])){
$tagid=$_GET['id'] ;
$tag_get='&id=' . $tagid ;
}
$tags_sql="SELECT tag FROM tags " ;
if(isset($_COOKIE['tasti_username']) && hash_check()){
$username=$_COOKIE['tasti_username'] ;
if(isset($_GET['mine']) && $_GET['mine']=='yes'){
$show_mine="AND owner='$username'" ;
$url_get_base=basename($_SERVER['REQUEST_URI']) . '&' ;
$num_bmarks_menu_offset='47' ;
}
if(isset($tagid)){
$tagid=$_GET['id'] ;
$tags_sql.="WHERE id='$tagid' " . $show_mine . " LIMIT 1" ;
}else{
$tags_sql.="WHERE true=true " . $show_mine . " ORDER BY id DESC LIMIT 1" ;
}
}else{
if(isset($tagid)){
$tagid=$_GET['id'] ;
$tags_sql.="WHERE id='$tagid' LIMIT 1" ;
}else{
$tags_sql.="ORDER BY id DESC LIMIT 1" ;
}
}
$tags_qry=pg_query($db,$tags_sql) ;
if(!$tags_qry){
echo '<span class="bad">Tag query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
exit() ;
}else{
$tag_count=pg_num_rows($tags_qry) ;
if($tag_count>0){
if(isset($_COOKIE['tasti_bmarks_per_page'])){
$bmarks_per_page=$_COOKIE['tasti_bmarks_per_page'] ;
}else{
$bmarks_per_page=15 ;
}
$a5_marker='' ; $a10_marker='' ; $a15_marker='' ; $a20_marker='' ; $a30_marker='' ;
if(isset($_GET['num']) && is_numeric($_GET['num'])){
$user_num_bmarks=$_GET['num'] ;
$limit_sql=' LIMIT ' . $user_num_bmarks ;
if($user_num_bmarks=='5'){
$a5_marker='* ' ;
}elseif($user_num_bmarks=='10'){
$a10_marker='* ' ;
}elseif($user_num_bmarks=='15'){
$a15_marker='* ' ;
}elseif($user_num_bmarks=='20'){
$a20_marker='* ' ;
}elseif($user_num_bmarks=='30'){
$a30_marker='* ' ;
}
$expire=time()+(3600*24*LOGIN_AGE) ;
setcookie("tasti_bmarks_per_page",$user_num_bmarks,$expire) ;
}else{
$user_num_bmarks=$bmarks_per_page ;
$limit_sql=' LIMIT ' . $user_num_bmarks;
if($user_num_bmarks=='5'){
$a5_marker='* ' ;
}elseif($user_num_bmarks=='10'){
$a10_marker='* ' ;
}elseif($user_num_bmarks=='15'){
$a15_marker='* ' ;
}elseif($user_num_bmarks=='20'){
$a20_marker='* ' ;
}elseif($user_num_bmarks=='30'){
$a30_marker='* ' ;
}
}
// pagination setup
$page=(!isset($_GET['page']))? 1 : $_GET['page'] ;
$prev=($page - 1) ;
$next=($page + 1) ;
$max_results=$user_num_bmarks ;
// Calculate the offset
$from=(($page * $max_results) - $max_results) ;
$tag=htmlentities(pg_result($tags_qry,0,0)) ;
$bmark_sql="SELECT id,date(last_update) as last_update,owner,url,notes,name FROM bmarks WHERE tag='$tag' " ;
if(isset($username) && isset($_GET['mine']) && $_GET['mine']=='yes'){
$bmark_sql.=" AND owner='$username' " ;
$mine='&mine=yes' ;
}else{
$mine='' ;
}
$bmarks_sql_all=$bmark_sql . 'ORDER BY owner,last_update,name' ;
$bmark_sql=$bmarks_sql_all . $limit_sql . ' OFFSET ' . $from ;
$bmark_qry=pg_query($db,$bmark_sql) ;
if(!$bmark_qry){
echo '<span class="bad">Bookmark query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
}else{
$bmarks_qry_all=pg_query($db,$bmarks_sql_all) ;
$num_bmarks_all=pg_num_rows($bmarks_qry_all) ;
$num_bmarks=pg_num_rows($bmark_qry) ;
if($num_bmarks>0){
echo '<TABLE width="100%"><TR><TD width="85%"><span class="huge">Bookmarks tagged with <B>' . $tag . '</B></span></TD>' ;
// only render the bmarks/page menu on the 'MY BOOKMARKS' page
if($num_bmarks_menu_offset=='47'){
echo '<TD width="70%" align="center" valign="top"><div id="menu">
<UL id="item1">
<LI class="top"><B>Bookmarks/page</B></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=5"> ' . $a5_marker . '5 </a></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=10"> ' . $a10_marker . '10 </a></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=15"> ' . $a15_marker . '15 </a></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=20"> ' . $a20_marker . '20 </a></LI>
<LI class="item"><A HREF="' . $url_get_base . 'num=30"> ' . $a30_marker . '30 </a></LI></UL>
</DIV></TD>' ;
}
echo '</TR></TABLE>' ;
echo '<HR><BR>' ;
for($lt=0; $lt < $num_bmarks ; $lt++){
$id=pg_result($bmark_qry,$lt,0) ;
$last_update=pg_result($bmark_qry,$lt,1) ;
$owner=pg_result($bmark_qry,$lt,2) ;
$url=pg_result($bmark_qry,$lt,3) ;
$notes=str_replace('&quot;','"',str_replace('&#039;',"'",htmlentities(pg_result($bmark_qry,$lt,4)))) ;
$name=str_replace('&quot;','"',str_replace('&#039;',"'",htmlentities(pg_result($bmark_qry,$lt,5)))) ;
if(strlen($notes)){
$notes_string='<BR><span class="small"><B>' . $notes . '</B></span>' ;
}else{
$notes_string='' ;
}
if(isset($username) && $username==$owner){
$bmark_user_edit_string='<BR><A HREF="edit.php?id=' . $id . '&func=edit"><span class="normal"><B>EDIT</B></a> | <A HREF="bmarks.php?id=' . $id . '&func=del"><B>DELETE</B></a> </span>' ;
}else{
$bmark_user_edit_string='' ;
}
echo '<TABLE><TR><TD valign="top"><span class="big">' . $last_update . ' </span></TD><TD><span class="big"><A HREF="' . $url . '">' . $name . '</a></span>' . $notes_string . $bmark_user_edit_string . '</TD>' ;
echo '</TR></TABLE><HR><BR>' ;
}
// pagination settings
$my_row_count=$num_bmarks_all ;
$total_pages=ceil($my_row_count / $max_results) ;
$pagination = '' ;
// Create a PREV link if one is needed
if($page > 1) {
$pagination.='<A STYLE="text-decoration:none" title="PREVIOUS PAGE" HREF="tags.php?page=' . $prev . '&num=' . $user_num_bmarks . $mine . $tag_get;
$pagination .= '"><span class="huge"><H1>←</H1></span></A> ' ;
}
// Create a NEXT link if one is needed
if($page < $total_pages){
$pagination .= '<A STYLE="text-decoration:none" title="NEXT PAGE" HREF="tags.php?page=' . $next . '&num=' . $user_num_bmarks . $mine . $tag_get;
$pagination .= '"><span class="huge"><H1>→</H1></span></A>' ;
}else{
// adjust the total count when on the last page since it might not have $user_num_bmarks items remaining
$new_max_results = ($max_results - (($page * $max_results) - $my_row_count)) ;
$max_results=$new_max_results ;
}
if($my_row_count>1){
$plural='s' ;
}else{
$plural='' ;
}
echo '<TABLE width="100%"><TR COLSPAN="1"><TD> </TD></TR><TR><TD COLSPAN="9"> ' . $my_row_count . ' Tasti bookmark' . $plural . '</TD><TD
class="page" COLSPAN="4"><B>' . $pagination . '</B></TD></TR></TABLE><BR>' ;
}else{
echo '<BR>There are currently no bookmarks associated with the ' . $tag . ' tag.<BR><BR><BR>' ;
}
}
}else{
echo 'No tags selected.<BR><BR><BR>' ;
}
}
}
// add the bookmark to the database
function add_bmark_db($username){
global $db ;
$bmark_name=$_POST['name'] ;
$bmark_url=$_POST['url'] ;
if(isset($_POST['notes']) && strlen(trim($_POST['notes']))){
$bmark_notes=trim($_POST['notes']);
}else{
$bmark_notes='' ;
}
if(isset($_POST['tags']) && strlen(trim($_POST['tags']))){
$bmark_tags_string=trim($_POST['tags']);
$bmark_tags_array_dupes=explode(" ",$bmark_tags_string) ;
$bmark_tags_array=array_unique($bmark_tags_array_dupes) ;
}
$tr_status=0 ;
$tr_begin=pg_query($db, 'BEGIN;');
// insert tags first, if they do not already exist
foreach($bmark_tags_array as $tag){
$tag=trim($tag) ;
$userHasTag_sql="SELECT count(id) FROM tags WHERE owner='$username' AND tag='$tag'" ;
$userHasTag_query=pg_query($db,$userHasTag_sql) ;
if(!$userHasTag_query){
echo '<span class="bad">Tag query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
exit() ;
}else{
$tag_count=pg_result($userHasTag_query,0,0) ;
if($tag_count==0){
// if the tag doesn't exist, insert it
if(strlen($tag)){
$tag=htmlentities($tag,ENT_QUOTES,"UTF-8") ;
$addUserTag_sql="INSERT INTO tags (owner,tag) VALUES ('$username','$tag')" ;
$addUserTag_qry=pg_query($db,$addUserTag_sql) ;
if(!$addUserTag_qry){
echo '<span class="bad">Tag INSERT FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
exit() ;
}
}
}
}
$bmark_name=htmlentities($bmark_name,ENT_QUOTES,"UTF-8") ;
$bmark_notes=htmlentities($bmark_notes,ENT_QUOTES,"UTF-8") ;
$addBmark_sql="INSERT INTO bmarks (owner,url,notes,tag,name) VALUES ('$username','$bmark_url','$bmark_notes','$tag','$bmark_name')" ;
$addBmark_qry=pg_query($db,$addBmark_sql) ;
if(!$addBmark_qry){
echo '<span class="bad">Bookmark INSERT FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
exit() ;
}
}
// rollback everything if any part of the transaction failed
if($tr_status){
$tr_end = pg_query($db,'ROLLBACK;');
exit();
}
$tr_end=pg_query($db,'COMMIT;');
echo 'Bookmark <B>( ' . htmlentities($bmark_name) . ' )</B> successfully added!<BR><BR>' ;
}
// add a new bookmark
function add_bmark(){
if(isset($_COOKIE['tasti_username']) && hash_check()){
if(isset($_POST['url'])){
add_bmark_db($_COOKIE['tasti_username']) ;
}
add_bmark_form() ;
}else{
echo 'Only users who have <A HREF="login.php?do=0">logged in</a> may add new bookmarks.<BR>' ;
}
}
// add a new bookmark form
function add_bmark_form($edit=null){
global $db ;
$name='' ;
$url='' ;
if(isset($_GET['url'])){
$url=$_GET['url'] ;
}
if(isset($_GET['name'])){
$name=$_GET['name'] ;
}
$username=$_COOKIE['tasti_username'] ;
$tags_sql="SELECT tag FROM tags WHERE owner='$username' ORDER BY tag LIMIT 100" ;
$tags_query=pg_query($db,$tags_sql) ;
$num_users_tags=pg_num_rows($tags_query) ;
echo '<span class="huge">Add a new bookmark to <B>Tasti</B>:</span><BR><BR>' ;
echo '<FORM method="POST" action="add.php" id="add_bmark"><TABLE>
<TR><TD>Name/Description*: </TD><TD> </TD><TD><INPUT TYPE="text" NAME="name" id="name" size="55" value="' . $name . '"></TD></TR>
<TR><TD>URL*: </TD><TD> </TD><TD><INPUT TYPE="text" NAME="url" id="url" size="55" value="' . $url . '"></TD></TR>
<TR><TD>Notes: </TD><TD> </TD><TD><INPUT TYPE="text" NAME="notes" id="notes" size="55"></TD></TR>
<TR><TD>Tags: </TD><TD> </TD><TD><INPUT TYPE="text" NAME="tags" id="tags" size="55"></TD></TR>
<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
<TR><TD> </TD><TD><DIV class="submit"><INPUT type="submit" value="Add" /></TD><TD> </TD></TR>
</TABLE></FORM><BR><span class="tiny"> * Required field</span><BR><BR>' ;
// display a clickable list of the user's tags that will be added to the Tags form field
if($num_users_tags>0){
echo '<span class="big">Click below to add your pre-existing tags to the new bookmark above:</span><BR><BR>' ;
$row_tag_count=0 ;
for ($tagid=0; $tagid < $num_users_tags ; $tagid++){
$tagname=htmlentities(trim(pg_result($tags_query,$tagid,0))) ;
echo '<A onclick="document.getElementById(\'tags\').value=document.getElementById(\'tags\').value + \' \' + \'' . $tagname . '\';">' . $tagname . '</a> ' ;
$row_tag_count++ ;
// only display 1 tags per row
if($row_tag_count>10){
echo '<BR>' ;
$row_tag_count=0 ;
}
}
echo '<BR><BR><BR>' ;
}
}
// list tags - only the user's if logged in, most recently added otherwise
function list_tags (){
global $db ;
if(isset($_COOKIE['tasti_username']) && hash_check()){
$username=$_COOKIE['tasti_username'] ;
$tags_sql="SELECT id,tag FROM tags WHERE owner='$username' ORDER BY tag" ;
}else{
$tags_sql="SELECT id,tag FROM tags ORDER BY last_update,tag LIMIT 50" ;
}
$tags_qry=pg_query($db,$tags_sql) ;
if(!$tags_qry){
echo '<span class="bad">Tags query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
return 1 ;
}else{
if(isset($username)){
$tag_string='<span class="big"><B>Your Tags</B></span><BR><BR>' ;
$show_mine='&mine=yes' ;
}else{
$tag_string='<span class="big"><B>Recent Tags</B></span><BR><BR>' ;
$show_mine='' ;
}
$num_tags=pg_num_rows($tags_qry) ;
if($num_tags>0){
echo $tag_string ;
for ($tagid=0 ; $tagid < $num_tags ; $tagid++){
$id=pg_result($tags_qry,$tagid,0) ;
$tag=htmlentities(pg_result($tags_qry,$tagid,1)) ;
echo ' <A HREF="tags.php?id=' . $id . $show_mine . '">' . $tag . '</a> <BR>' ;
}
}
echo '<BR>' ;
}
}
// account details form
function account_details_form (){
global $db ;
$name='' ; $email='' ;
$username=$_COOKIE['tasti_username'] ;
$account_sql="SELECT name,email FROM users WHERE username='$username' LIMIT 1" ;
$account_query=pg_query($db,$account_sql) ;
$name=pg_result($account_query,0,0) ;
$email=pg_result($account_query,0,1) ;
echo '<div id="content"><span class="big">Please edit the fields that you wish to change for your <B>Tasti</B> account:</span><BR><BR>' ;
echo '<FORM method="POST" action="account.php" id="register"><TABLE>
<TR><TD><label for="password0">Change your Password (at least 6 characters): </label></TD><TD> </TD><TD><INPUT NAME="password0" TYPE="password" id="password0" /></TD></TR>
<TR><TD><label for="password1">Enter the new password again: </label></TD><TD> </TD><TD><INPUT NAME="password1" TYPE="password" id="password1" /></TD></TR>
<TR><TD><label for="fullname">Update your full name: </label></TD><TD> </TD><TD><INPUT NAME="fullname" TYPE="text" id="fullname" value="' . $name . '" /></TD></TR>
<TR><TD><label for="email">Update your email address: </label></TD><TD> </TD><TD><INPUT NAME="email" TYPE="text" id="email" value="' . $email . '" /></TD></TR>
<TR><TD> </TD><TD> </TD><TD> </TD></TR>
<TR><TD> </TD><TD><DIV class="submit"><INPUT type="submit" value="Submit" /></TD><TD> </TD></TR>
</TABLE></FORM><BR></div>' ;
}
// bookmark import form
function bmark_import_form (){
echo '<div id="content"><span class="big"><B>Tasti</B> currently supports bulk imports via a bookmarks file (either from your web browser, or from a <A HREF="http://www.delicious.com">Delicious</a> export):</span><BR><BR>' ;
echo '<FORM method="POST" action="import.php" id="import" enctype="multipart/form-data"><UL>' ;
echo '<LI> Upload a bookmarks file from your web browser: <INPUT TYPE="file" NAME="upload_bmark"><BR> </LI>
<LI> <input type="checkbox" name="import_tag"> Add the \'imported\' tag to each bookmark<BR> </LI>
</UL><BR><CENTER><INPUT TYPE="submit" NAME="SAVE" value="Import"></CENTER></FORM><BR></div>' ;
}
// generate the tabs for the acct mgmt page
function generate_tabs (){
$pagename=substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1) ;
if($pagename=='account.php'){
$accountSelected='id="selected"' ;
}else{
$accountSelected='' ;
}
if($pagename=='import.php'){
$importSelected='id="selected"' ;
}else{
$importSelected='' ;
}
if($pagename=='bmarklet.php'){
$bmarkletSelected='id="selected"' ;
}else{
$bmarkletSelected='' ;
}
if($pagename=='edit_tags.php'){
$tagSelected='id="selected"' ;
}else{
$tagSelected='' ;
}
echo '<div id="tabs"><ul>
<li ' . $accountSelected . '><a href="account.php">Details</a></li><!-- these comments between lis solve a bug in IE that prevents spaces appearing between list items that appear on different lines in the source
--><li ' . $importSelected . '><a href="import.php">Import Bookmarks</a></li><!-- these comments between lis solve a bug in IE that prevents spaces appearing between list items that appear on different lines in the source
--><li ' . $bmarkletSelected . '><a href="bmarklet.php">Bookmarklet</a></li><!-- these comments between lis solve a bug in IE that prevents spaces appearing between list items that appear on different lines in the source
--><li ' . $tagSelected . '><a href="edit_tags.php">Tags</a></li><!-- ' ;
echo '--></ul>
</div>' ;
return $pagename ;
}
// process the bookmark file for import
function bmark_import_file(){
$bmark_array=file(($_FILES['upload_bmark']['tmp_name'])) ;
$imported_bmark_count=0 ;
$importBad=0 ;
foreach($bmark_array as $bmark_row){
if(preg_match("/href/i",$bmark_row) && preg_match("/http/i",$bmark_row)){
$row_parts=explode('href',strtolower($bmark_row),2) ;
foreach($row_parts as $row_part){
if(preg_match("/http/i",$row_part)){
$url_array=explode('"',trim($row_part)) ;
$item_count=count($url_array) - 1 ;
$url=$url_array['1'] ;
$bmark_name=str_replace('</a>','',preg_replace('/^>/','',$url_array[$item_count])) ;
// process tags, if they exist
if(preg_match('/tags="/',$row_part)){
$tag_array=explode('tags="',trim($row_part)) ;
$tags0=explode(',',trim(current(explode('"',$tag_array[1])))) ;
$tags=array_filter($tags0) ;
}else{
$tags=array() ;
}
// add the imported tag
if(isset($_POST['import_tag'])){
$tags[]='imported' ;
}
$tags=array_unique($tags) ;
$username=$_COOKIE['tasti_username'] ;
// drop javascript URLs
if(!preg_match('/^javascript:/',$url)){
$insertRC=insert_bmark($username,$url,$bmark_name,$tags) ;
if($insertRC){
$importBad++ ;
}else{
$imported_bmark_count++ ;
}
}
}
}
}
}
echo '<div id="content">' ;
if($imported_bmark_count > 0){
echo '<BR> <B>SUCCESS!</B><BR><BR>You have imported ' . $imported_bmark_count . ' bookmarks.<BR><BR>Click <A HREF="bmarks.php?whose=mine">here</a> to view them now.<BR><BR>' ;
if($importBad>0){
echo $importBad . ' bookmark(s) failed to import.<BR><BR>' ;
}
}else{
echo '<BR> No bookmarks were successfully imported. Please verify that your bookmarks file is valid, and then try again later.<BR><BR>' ;
}
echo '</div>' ;
}
// insert a new bookmark into the database
function insert_bmark ($username,$bmark_url,$bmark_name,$bmark_tags_array) {
global $db ;
$bmark_notes='' ;
$tr_status=0 ;
$tr_begin=pg_query($db, 'BEGIN;');
// insert tags first, if they do not already exist
foreach($bmark_tags_array as $tag){
$tag=trim($tag) ;
$userHasTag_sql="SELECT count(id) FROM tags WHERE owner='$username' AND tag='$tag'" ;
$userHasTag_query=pg_query($db,$userHasTag_sql) ;
if(!$userHasTag_query){
echo '<span class="bad">Tag query FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
pg_close($db) ;
exit() ;
}else{
$tag_count=pg_result($userHasTag_query,0,0) ;
if($tag_count==0){
// if the tag doesn't exist, insert it
if(strlen($tag)){
$tag=htmlentities($tag,ENT_QUOTES) ;
$addUserTag_sql="INSERT INTO tags (owner,tag) VALUES ('$username','$tag')" ;
$addUserTag_qry=pg_query($db,$addUserTag_sql) ;
if(!$addUserTag_qry){
echo '<span class="bad">Tag INSERT FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
pg_close($db) ;
exit() ;
}
}
}
}
$check_dup_sql="SELECT count(id) FROM bmarks WHERE owner='$username' AND url='$bmark_url' AND tag='$tag'" ;
$check_dup_qry=pg_query($db,$check_dup_sql) ;
$dup_count=pg_result($check_dup_qry,0,0) ;
if($dup_count==0){
$bmark_name=htmlentities($bmark_name,ENT_QUOTES,"UTF-8") ;
$bmark_notes=htmlentities($bmark_notes,ENT_QUOTES,"UTF-8") ;
$addBmark_sql="INSERT INTO bmarks (owner,url,notes,tag,name) VALUES ('$username','$bmark_url','$bmark_notes','$tag','$bmark_name')" ;
$addBmark_qry=pg_query($db,$addBmark_sql) ;
if(!$addBmark_qry){
echo '<span class="bad">Bookmark INSERT FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
$tr_end = pg_query($db,'ROLLBACK;');
pg_close($db) ;
exit() ;
}
}
}
// rollback everything if any part of the transaction failed
if($tr_status){
$tr_end = pg_query($db,'ROLLBACK;');
pg_close($db) ;
exit();
}
$tr_end=pg_query($db,'COMMIT;');
return 0 ;
}
// account mgmt
function account_mgmt(){
if(isset($_COOKIE['tasti_username']) && hash_check()){
$password='' ; $name='' ; $email='' ;
$username=$_COOKIE['tasti_username'] ;
echo '<span class="huge"><B>Tasti Account Management</B></span><BR><BR>' ;
global $db ;
$account_sql='UPDATE users SET ' ;
if(isset($_POST['password0']) && isset($_POST['password1']) && trim($_POST['password0'])==trim($_POST['password1']) && strlen(trim($_POST['password0']))){
$password=sha1(trim($_POST['password0']));
$account_sql.="password='$password', " ;
}
if(isset($_POST['fullname']) && strlen(trim($_POST['fullname']))){
$name=trim($_POST['fullname']);
$account_sql.="name='$name', " ;
}
if(isset($_POST['email'])){
$email=trim($_POST['email']) ;
$account_sql.="email='$email', " ;
}
if(strlen($password) || strlen($name) || strlen($email)){
$account_sql.="username='$username' WHERE username='$username'" ;
$account_query=pg_query($db,$account_sql) ;
if(!$account_query){
echo '<span class="bad">Account update FAILED: <BR>' ;
echo pg_last_error() . '</span><BR><BR>';
}else{
echo '<span class="big"><B><i>Update Successful</i></B></span><BR><BR>' ;
}
}
$pagename=generate_tabs() ;
if($pagename=='account.php'){
account_details_form() ;
}elseif($pagename=='import.php'){
if(isset($_FILES['upload_bmark'])){
bmark_import_file() ;