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
|
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
(define (sdat-set-curr-err! vec val)(vector-set! vec 27 val))
(define (sdat-set-log-port! vec val)(vector-set! vec 28 val))
(define (sdat-set-logfile! vec val)(vector-set! vec 29 val))
(define (sdat-set-seen-pages! vec val)(vector-set! vec 30 val))
(define (sdat-set-page-dir-style! vec val)(vector-set! vec 31 val))
(define (sdat-set-debugmode! vec val)(vector-set! vec 32 val))
;; (define-class <session> ()
;; (dbtype ;; 'pg or 'sqlite3
;; dbinit
;; conn
;; params ;; params from the key=val&key1=val2 string
;; path-params ;; remaining params from the path
;; session-key
;; session-id
;; The global session
;; domain
;; toppage ;; defaults to "index" - override in .stml.config if desired
;; page ;; the page name - defaults to home
;; curr-page ;; the current page being evaluated
;; content-type ;; the default content type is text/html, override to deliver other stuff
;; page-type ;; use in conjunction with content-type to deliver other payloads
;; sroot
;; twikidir ;; location for twikis - needs to be fully writable by web server
;; pagedat
(define s:session (make-sdat))
;; alt-page-dat
;; pagevars ;; session vars specific to this page
;; pagevars-before
;; sessionvars ;; session vars visible to all pages
;; sessionvars-before
;; globalvars ;; global vars visible to all sessions
;; globalvars-before
;; logpt
;; formdat
;; request-method
;; session-cookie
;; curr-err
;; log-port
;; logfile
;; seen-pages
;; page-dir-style ;; #t = new style, #f = old style
;; debugmode))
;; SPLIT INTO STRAIGHT FORWARD INIT AND COMPLEX INIT
(define (session:initialize self)
(sdat-set-dbtype! self 'pg)
(sdat-set-page! self "home") ;; these are defaults
(sdat-set-curr-page! self "home")
(sdat-set-content-type! self "Content-type: text/html; charset=iso-8859-1\n\n")
|
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
-
+
-
+
|
(define (session:page-get self key)
(hash-table-ref/default (sdat-get-pagevars self) key #f))
;; get session vars for a specified page
;;
(define (session:get self page key)
(let ((ht (session:get-page-hash self page)))
(hash-table-ref/default ht key #f)))
(hash-table-ref/default ht (s:any->string key) #f)))
;; delete a session var for a specified page
;;
(define (session:del! self page key)
(let ((ht (session:get-page-hash self page)))
(hash-table-delete! ht key)))
(hash-table-delete! ht (s:any->string key))))
;; get ALL keys for this page and store in the session pagevars hash
;;
(define (session:get-vars self)
(let ((session-id (sdat-get-session-id self)))
(if (not session-id)
(err:log "ERROR: No session id in session object! session:get-vars")
|
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
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
+
+
|
(cond
((list? x) x)
((string? x) x)
(else '())))
(port-map (lambda (s)
(eval s e))
(lambda ()(read p))))))
(define (session:process-file f)
(let* ((p (open-input-file f))
(dat (process-port p)))
(close-input-port p)
dat))
;; May 2011, putting all pages into one directory for the following reasons:
;; 1. want filename to reflect page name (emacs limitation)
;; 2. that's it! no other reason. could make it configurable ...
;; page-dir-style is:
;; 'stored => stored in executable
;; 'flat => pages flat directory
;; 'dir => directory tree pages/<pagename>/{view,control}.scm
;; parts:
;; 'both => load control and view (anything other than view or control
;; 'view => load view only
;; 'control => load control only
(define (session:call-parts self page parts)
(define (session:call-parts self page #!key (parts 'both))
(sdat-set-curr-page! self page)
;; (session:log self "page-dir-style: " (sdat-get-page-dir-style self))
(let* ((dir-style ;; (equal? (sdat-get-page-dir-style self) "onedir")) ;; flag #t for onedir, #f for old style
(session:log self "page-dir-style: " (sdat-get-page-dir-style self))
(let* ((dir-style (sdat-get-page-dir-style self));; (equal? (sdat-get-page-dir-style self) "onedir")) ;; flag #t for onedir, #f for old style
(sdat-get-page-dir-style self))
(dir (string-append (sdat-get-sroot self)
(if dir-style
(conc "/pages/")
(conc "/pages/" page))))
(control (string-append dir (if dir-style
(dir (string-append (sdat-get-sroot self)
(if dir-style
(conc "/pages/")
(conc "/pages/" page)))))
(case dir-style
(conc page "_ctrl.scm")
"/control.scm")))
(view (string-append dir (if dir-style
;; NB// Stored always loads both control and view
((stored)((eval (string->symbol (conc "pages:" page)))))
(conc page "_view.scm")
"/view.scm")))
(load-view (and (file-exists? view)
(or (eq? parts 'both)(eq? parts 'view))))
(load-control (and (file-exists? control)
(or (eq? parts 'both)(eq? parts 'control))))
(view-dat '()))
;; (session:log self "dir-style: " dir-style)
;; (sugar "/home/matt/kiatoa/stml/sugar.scm" ))
;; (print "dir=" dir " control=" control " view=" view " load-view=" load-view " load=control=" load-control)
(if load-control
(begin
(load control)
((dir)
;; first the control
(session:set-called! self page)))
;; move this to where it gets exectuted only once
;;
;;(s:log "s:b yields " (s:b "blah"))
(if load-view
;; option one:
;;
;; (let ((inp (open-input-string
;; (files-read->string "/home/matt/kiatoa/stml/sugar.scm"
;; view))))
;; (map
(let ((control-file (conc "pages/" page "_ctrl.scm"))
(view-file (conc "pages/" page "_view.scm")))
(if (and (file-exists? control-file)
(not (eq? parts 'view)))
(begin
;; (lambda (x)
;; (cond
;; ((list? x) x)
;; ((string? x) x)
;; (else '())))
(session:set-called! self page)
(load control-file)))
(if (file-exists? view-file)
(if (not (eq? parts 'control))
;; (port-map eval (lambda ()
;; (read inp)))))
;;
;; option two:
;;
(let* (;; (inps (map open-input-file (list view))) ;; sugar view)))
(p (open-input-file view)) ;; (apply make-concatenated-port inps))
(dat (process-port p)))
(session:process-file view-file))
;;(map
;; (lambda (x)
;; (cond
;; ((list? x) x)
;; ((string? x) x)
;; (else '())))
;; (port-map eval (lambda ()(read p))))))
;; (map close-input-port inps)
(close-input-port p)
dat)
(list "<p>Page not found " page " </p>"))))
;;(define (session:call self page)
(list "<p>Page not found " page " </p>"))))
((flat))
(else
;; (session:call-parts self page 'both))
(list "ERROR: page-dir-style must be stored, dir or flat, got " dir-style)))))
(define (session:call self page parts)
(session:call-parts self page 'both))
(define (session:load-model self model)
(let ((model.scm (string-append (sdat-get-sroot self) "/models/" model ".scm"))
(model.so (string-append (sdat-get-sroot self) "/models/" model ".so")))
(if (file-exists? model.so)
(load model.so)
(if (file-exists? model.scm)
(load model.scm)
(s:log "ERROR: model " model.scm " not found")))))
;; (define (session:load-model self model)
;; (let ((model.scm (string-append (sdat-get-sroot self) "/models/" model ".scm"))
;; (model.so (string-append (sdat-get-sroot self) "/models/" model ".so")))
;; (if (file-exists? model.so)
;; (load model.so)
;; (if (file-exists? model.scm)
;; (load model.scm)
;; (s:log "ERROR: model " model.scm " not found")))))
(define (session:model-path self model)
(string-append (sdat-get-sroot self) "/models/" model ".scm"))
;; (define (session:model-path self model)
;; (string-append (sdat-get-sroot self) "/models/" model ".scm"))
(define (session:pp-formdat self)
(let ((dat (formdat:all->strings (sdat-get-formdat self))))
(string-intersperse dat "<br> ")))
(define (session:param->string params)
;; (err:log "params=" params)
|