Class: Knj::Web

Inherits:
Object show all
Defined in:
lib/knj/web.rb

Class Method Summary collapse

Class Method Details

.ahref_parse(str) ⇒ Object

Parses a string to be safe for use in <a href=“”>.



801
802
803
# File 'lib/knj/web.rb', line 801

def self.ahref_parse(str)
  return str.to_s.gsub("&", "&amp;")
end

.alert(string) ⇒ Object



171
172
173
174
175
176
# File 'lib/knj/web.rb', line 171

def self.alert(string)
  require "#{$knjpath}strings"
  @alert_sent = true
  html = "<script type=\"text/javascript\">alert(\"#{Knj::Strings.js_safe(string.to_s)}\");</script>"
  print html
end

.attr_html(attrs) ⇒ Object



244
245
246
247
248
249
250
251
252
253
# File 'lib/knj/web.rb', line 244

def self.attr_html(attrs)
  return "" if attrs.length <= 0
  
  html = ""
  attrs.each do |key, val|
    html << " #{key}=\"#{val.to_s.html}\""
  end
  
  return html
end

.backObject



196
197
198
199
# File 'lib/knj/web.rb', line 196

def self.back
  print "<script type=\"text/javascript\">history.go(-1);</script>"
  exit
end

.browser(servervar = nil) ⇒ Object



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
# File 'lib/knj/web.rb', line 570

def self.browser(servervar = nil)
  servervar = _server if !servervar
  raise "Could not figure out meta data." if !servervar
  agent = servervar["HTTP_USER_AGENT"].to_s.downcase
  
  if match = agent.index("knj:true") != nil
    browser = "bot"
    title = "Bot"
    version = "KnjHttp"
  elsif match = agent.match(/chrome\/(\d+\.\d+)/)
    browser = "chrome"
    title = "Google Chrome"
    version = match[1]
  elsif match = agent.match(/firefox\/(\d+\.\d+)/)
    browser = "firefox"
    title = "Mozilla Firefox"
    version = match[1]
  elsif match = agent.match(/msie\s*(\d+\.\d+)/)
    browser = "ie"
    title = "Microsoft Internet Explorer"
    version = match[1]
  elsif match = agent.match(/opera\/([\d+\.]+)/)
    browser = "opera"
    title = "Opera"
    version = match[1]
  elsif match = agent.match(/wget\/([\d+\.]+)/)
    browser = "bot"
    title = "Bot"
    version = "Wget #{match[1]}"
  elsif agent.index("baiduspider") != nil
    browser = "bot"
    title = "Bot"
    version = "Baiduspider"
  elsif agent.index("googlebot") != nil
    browser = "bot"
    title = "Bot"
    version = "Googlebot"
  elsif agent.index("gidbot") != nil
    browser = "bot"
    title = "Bot"
    version = "GIDBot"
  elsif match = agent.match(/android\s+([\d\.]+)/)
    browser = "android"
    title = "Android"
    version = match[1]
  elsif match = agent.match(/safari\/(\d+)/)
    browser = "safari"
    title = "Safari"
    version = match[1]
  elsif agent.index("iPad") != nil
    browser = "safari"
    title = "Safari (iPad)"
    version = "ipad"
  elsif agent.index("bingbot") != nil
    browser = "bot"
    title = "Bot"
    version = "Bingbot"
  elsif agent.index("yahoo! slurp") != nil
    browser = "bot"
    title = "Bot"
    version = "Yahoo! Slurp"
  elsif agent.index("hostharvest") != nil
    browser = "bot"
    title = "Bot"
    version = "HostHarvest"
  elsif agent.index("exabot") != nil
    browser = "bot"
    title = "Bot"
    version = "Exabot"
  elsif agent.index("dotbot") != nil
    browser = "bot"
    title = "Bot"
    version = "DotBot"
  elsif agent.index("msnbot") != nil
    browser = "bot"
    title = "Bot"
    version = "MSN bot"
  elsif agent.index("yandexbot") != nil
    browser = "bot"
    title = "Bot"
    version = "Yandex Bot"
  elsif agent.index("mj12bot") != nil
    browser = "bot"
    title = "Bot"
    version = "Majestic12 Bot"
  elsif agent.index("facebookexternalhit") != nil
    browser = "bot"
    title = "Bot"
    version = "Facebook Externalhit"
  elsif agent.index("sitebot") != nil
    browser = "bot"
    title = "Bot"
    version = "SiteBot"
  elsif match = agent.match(/java\/([\d\.]+)/)
    browser = "bot"
    title = "Java"
    version = match[1]
  elsif match = agent.match(/ezooms\/([\d\.]+)/)
    browser = "bot"
    title = "Ezooms"
    version = match[1]
  elsif match = agent.match(/ahrefsbot\/([\d\.]+)/)
    browser = "bot"
    title = "AhrefsBot"
    version = match[1]
  elsif agent.index("sosospider") != nil
    browser = "bot"
    title = "Bot"
    version = "Sosospider"
  else
    browser = "unknown"
    title = "(unknown browser)"
    version = "(unknown version)"
  end
  
  os = nil
  os_version = nil
  if agent.index("linux") != nil
    os = "linux"
  elsif match = agent.match(/mac\s+os\s+x\s+([\d_+])/)
    os = "mac"
  elsif match = agent.match(/windows\s+nt\s+([\d\.]+)/)
    os = "windows"
    
    if match[1] == "5.1"
      os_version = "xp"
    end
  end
  
  return {
    "browser" => browser,
    "title" => title,
    "version" => version,
    "os" => os,
    "os_version" => os_version
  }
end

.checkval(value, val1, val2 = nil) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/knj/web.rb', line 201

def self.checkval(value, val1, val2 = nil)
  if val2 != nil
    if !value or value == "" or value == "false"
      return val2
    else
      return val1
    end
  else
    if !value or value == "" or value == "false"
      return val1
    else
      return value
    end
  end
end


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/knj/web.rb', line 51

def self.cookie_str(cookie_data)
  raise "Not a hash: '#{cookie_data.class.name}', '#{cookie_data}'." unless cookie_data.is_a?(Hash)
  cookiestr = "#{self.urlenc(cookie_data["name"])}=#{self.urlenc(cookie_data["value"])}"
  
  cookie_data.each do |key, val|
    next if key == "name" or key == "value"
    
    if key.to_s.downcase == "expires" and val.is_a?(Time)
      cookiestr << "; Expires=#{val.httpdate}"
    else
      cookiestr << "; #{key}=#{val}"
    end
  end
  
  return cookiestr
end

.hiddens(hidden_arr) ⇒ Object



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
# File 'lib/knj/web.rb', line 773

def self.hiddens(hidden_arr)
  html = ""
  
  hidden_arr.each do |hidden_hash|
    if hidden_hash.is_a?(Array)
      hidden_hash = {
        :name => hidden_hash[0],
        :value => hidden_hash[1]
      }
    else
      if hidden_hash[:value].is_a?(Array)
        if !hidden_hash[:value][0]
          hidden_hash[:value] = nil
        else
          key = hidden_hash[:value][1]
          obj = hidden_hash[:value][0]
          hidden_hash[:value] = obj[key]
        end
      end
    end
    
    html << "<input type=\"hidden\" name=\"#{hidden_hash[:name].to_s.html}\" value=\"#{hidden_hash[:value].to_s.html}\" />"
  end
  
  return html
end

.html(string) ⇒ Object

Escapes HTML-characters in a string.



822
823
824
# File 'lib/knj/web.rb', line 822

def self.html(string)
  return string.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
end

.html_args(h) ⇒ Object



826
827
828
829
830
831
832
833
# File 'lib/knj/web.rb', line 826

def self.html_args(h)
  str = ""
  h.each do |key, val|
    str << "&#{Php4r.urlencode(key)}=#{Php4r.urlencode(val)}"
  end
  
  return str
end

.input(args) ⇒ Object



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
# File 'lib/knj/web.rb', line 255

def self.input(args)
  Knj::ArrayExt.hash_sym(args)
  
  if args.key?(:value)
    if args[:value].is_a?(Array) and (args[:value].first.is_a?(NilClass) or args[:value].first == false)
      value = nil
    elsif args[:value].is_a?(Array)
      if !args[:value][2] or args[:value][2] == :key
        value = args[:value].first[args[:value][1]]
      elsif args[:value][2] == :callb
        value = args[:value].first.send(args[:value][1])
      else
        value = args[:value]
      end
    elsif args[:value].is_a?(String) or args[:value].is_a?(Integer)
      value = args[:value].to_s
    else
      value = args[:value]
    end
  end
  
  args[:value_default] = args[:default] if args[:default]
  
  if value.is_a?(NilClass) and args[:value_default]
    value = args[:value_default]
  elsif value.is_a?(NilClass)
    value = ""
  end
  
  if value and args.key?(:value_func) and args[:value_func]
    cback = args[:value_func]
    
    if cback.is_a?(Method)
      value = cback.call(value)
    elsif cback.is_a?(Array)
      value = Php4r.call_user_func(args[:value_func], value)
    elsif cback.is_a?(Proc)
      value = cback.call(value)
    else
      raise "Unknown class: #{cback.class.name}."
    end
  end
  
  value = args[:values] if args[:values]
  args[:id] = args[:name] if !args[:id]
  
  if !args[:type]
    if args[:opts]
      args[:type] = :select
    elsif args[:name] and args[:name].to_s[0..2] == "che"
      args[:type] = :checkbox
    elsif args[:name] and args[:name].to_s[0..3] == "file"
      args[:type] = :file
    else
      args[:type] = :text
    end
  else
    args[:type] = args[:type].to_sym
  end
  
  attr = {
    "name" => args[:name],
    "id" => args[:id],
    "type" => args[:type],
    "class" => "input_#{args[:type]}"
  }
  attr.merge!(args[:attr]) if args[:attr]
  attr["disabled"] = "disabled" if args[:disabled]
  attr["maxlength"] = args[:maxlength] if args.key?(:maxlength)
  
  raise "No name given to the Web::input()-method." if !args[:name] and args[:type] != :info and args[:type] != :textshow and args[:type] != :plain and args[:type] != :spacer and args[:type] != :headline
  
  css = {}
  css["text-align"] = args[:align] if args.key?(:align)
  css.merge!(args[:css]) if args.key?(:css)
  
  attr_keys = [:onchange]
  attr_keys.each do |tag|
    if args.key?(tag)
      attr[tag] = args[tag]
    end
  end
  
  classes_tr = []
  classes_tr += args[:classes_tr] if args[:classes_tr]
  
  if !classes_tr.empty?
    classes_tr_html = " class=\"#{classes_tr.join(" ")}\""
  else
    classes_tr_html = ""
  end
  
  if args.key?(:title)
    title_html = args[:title].to_s.html
  elsif args.key?(:title_html)
    title_html = args[:title_html]
  end
  
  html = ""
  
  classes = ["input_#{args[:type]}"]
  classes = classes | args[:classes] if args.key?(:classes)
  attr["class"] = classes.join(" ")
  
  if args[:type] == :checkbox
    attr["value"] = args[:value_active] if args.key?(:value_active)
    attr["checked"] = "checked" if value.is_a?(String) and value == "1" or value.to_s == "1" or value.to_s == "on" or value.to_s == "true"
    attr["checked"] = "checked" if value.is_a?(TrueClass)
    
    html << "<tr#{classes_tr_html}>"
    html << "<td colspan=\"2\" class=\"tdcheck\">"
    html << "<input#{self.attr_html(attr)} />"
    html << "<label for=\"#{args[:id].html}\">#{title_html}</label>"
    html << "</td>"
    html << "</tr>"
  elsif args[:type] == :headline
    html << "<tr#{classes_tr_html}><td colspan=\"2\"><h2 class=\"input_headline\">#{title_html}</h2></td></tr>"
  elsif args[:type] == :spacer
    html << "<tr#{classes_tr_html}><td colspan=\"2\">&nbsp;</td></tr>"
  else
    html << "<tr#{classes_tr_html} id=\"#{Knj::Web.html("#{args[:id]}_tr")}\">"
    html << "<td class=\"tdt\" id=\"#{Knj::Web.html("#{args[:id]}_label")}\"><div>"
    html << title_html
    html << "</div></td>"
    html << "<td#{self.style_html(css)} class=\"tdc\" id=\"#{Knj::Web.html("#{args[:id]}_content")}\"><div>"
    
    if args[:type] == :textarea
      if args.key?(:height)
        if (Float(args[:height]) rescue false)
          css["height"] = "#{args[:height]}px"
        else
          css["height"] = args[:height]
        end
      end
      
      html << "<textarea#{self.style_html(css)} class=\"input_textarea\" name=\"#{args[:name].html}\" id=\"#{args[:id].html}\">#{value}</textarea>"
    elsif args[:type] == :fckeditor
      args[:height] = 400 if !args[:height]
      
      require "/usr/share/fckeditor/fckeditor.rb" if !Kernel.const_defined?(:FCKeditor)
      fck = FCKeditor.new(args[:name])
      fck.Height = args[:height].to_i
      fck.Value = value
      html << fck.CreateHtml
    elsif args[:type] == :ckeditor
      args[:height] = 400 if !args[:height]
      require "ckeditor4ruby" if !Kernel.const_defined?(:CKEditor)
      ck = CKEditor.new
      ck.return_output = true
      html << ck.editor(args[:name], value)
    elsif args[:type] == :select
      attr["multiple"] = "multiple" if args[:multiple]
      attr["size"] = args["size"] if args[:size]
      
      html << "<select#{self.attr_html(attr)}>"
      html << Knj::Web.opts(args[:opts], value, args[:opts_args])
      html << "</select>"
      
      if args[:moveable]
        html << "<div style=\"padding-top: 3px;\">"
        html << "<input type=\"button\" value=\"#{_("Up")}\" onclick=\"select_moveup($('##{args[:id]}'));\" />"
        html << "<input type=\"button\" value=\"#{_("Down")}\" onclick=\"select_movedown($('##{args[:id]}'));\" />"
        html << "</div>"
      end
    elsif args[:type] == :imageupload
      html << "<table class=\"designtable\"><tr#{classes_tr_html}><td style=\"width: 100%;\">"
      html << "<input type=\"file\" name=\"#{args[:name].html}\" class=\"input_file\" />"
      html << "</td><td style=\"padding-left: 5px;\">"
      
      raise "No path given for imageupload-input." if !args.key?(:path)
      raise "No value given in arguments for imageupload-input." if !args.key?(:value)
      
      path = args[:path].gsub("%value%", value.to_s).untaint
      if File.exists?(path)
        html << "<img src=\"image.rhtml?path=#{self.urlenc(path).html}&smartsize=100&rounded_corners=10&border_color=black&force=true&ts=#{Time.new.to_f}\" alt=\"Image\" />"
        
        if args[:dellink]
          dellink = args[:dellink].gsub("%value%", value.to_s)
          html << "<div style=\"text-align: center;\">(<a href=\"javascript: if (confirm('#{_("Do you want to delete the image?")}')){location.href='#{dellink}';}\">#{_("delete")}</a>)</div>"
        end
      end
      
      html << "</td></tr></table>"
    elsif args[:type] == :file
      html << "<input type=\"#{args[:type].to_s}\" class=\"input_#{args[:type].to_s}\" name=\"#{args[:name].html}\" />"
    elsif args[:type] == :textshow or args[:type] == :info
      html << value.to_s
    elsif args[:type] == :plain
      html << "#{Php4r.nl2br(Knj::Web.html(value))}"
    elsif args[:type] == :editarea
      css["width"] = "100%"
      css["height"] = args[:height] if args.key?(:height)
      html << "<textarea#{self.attr_html(attr)}#{self.style_html(css)} id=\"#{args[:id]}\" name=\"#{args[:name]}\">#{value}</textarea>"
      
      jshash = {
        "id" => args[:id],
        "start_highlight" => true
      }
      
      pos_keys = [:skip_init, :allow_toggle, :replace_tab_by_spaces, :toolbar, :syntax]
      pos_keys.each do |key|
        jshash[key.to_s] = args[key] if args.key?(key)
      end
      
      html << "<script type=\"text/javascript\">"
      html << "function knj_web_init_#{args[:name]}(){"
      html << "editAreaLoader.init(#{Php4r.json_encode(jshash)});"
      html << "}"
      html << "</script>"
    elsif args[:type] == :numeric
      attr[:type] = :text
      attr[:value] = value
      html << "<input#{self.attr_html(attr)} />"
    else
      attr[:value] = value
      html << "<input#{self.attr_html(attr)} />"
    end
    
    html << "</div></td></tr>"
  end
  
  html << "<tr#{classes_tr_html}><td colspan=\"2\" class=\"tdd\">#{args[:descr]}</td></tr>" if args[:descr]
  return html
end

.inputs(arr) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/knj/web.rb', line 217

def self.inputs(arr)
  html = ""
  arr.each do |args|
    if RUBY_ENGINE == "rbx"
      html << self.input(args).to_s.encode(html.encoding)
    else
      html << self.input(args)
    end
  end
  
  return html
end

.locale(args = {}) ⇒ Object



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
# File 'lib/knj/web.rb', line 708

def self.locale(args = {})
  if args[:servervar]
    servervar = args[:servervar]
  else
    servervar = _server
  end
  
  if !servervar
    raise "Could not figure out meta data."
  end
  
  ret = {
    :recommended => [],
    :browser => []
  }
  
  alangs = servervar["HTTP_ACCEPT_LANGUAGE"].to_s
  if alangs.length > 0
    alangs.split(/\s*,\s*/).each do |alang|
      if qmatch = alang.match(/;\s*q=([\d\.]+)/)
        alang.gsub!(/;\s*q=([\d\.]+)/, "")
        q = qmatch[1].to_f
      else
        q = 1.0
      end
      
      if match = alang.match(/^([A-z]+)-([A-z]+)$/)
        locale = match[1]
        sublocale = match[2]
      else
        locale = alang
        sublocale = false
      end
      
      ret[:browser] << {
        :locale => locale,
        :sublocale => sublocale,
        :q => q
      }
    end
  end
  
  if args[:supported] and ret[:browser]
    ret[:browser].each do |locale|
      args[:supported].each do |supported_locale|
        if match = supported_locale.match(/^([A-z]+)_([A-z]+)$/)
          if match[1] == locale[:locale]
            if !locale[:sublocale]
              ret[:recommended] << supported_locale if ret[:recommended].index(supported_locale) == nil
            elsif locale[:sublocale] == match[1]
              ret[:recommended] << supported_locale if ret[:recommended].index(supported_locale) == nil
            end
          end
        end
      end
    end
  end
  
  if args[:default]
    ret[:recommended] << args[:default] if ret[:recommended].index(args[:default]) == nil
  end
  
  return ret
end

.opts(opthash, curvalue = nil, opts_args = {}) ⇒ Object



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
# File 'lib/knj/web.rb', line 480

def self.opts(opthash, curvalue = nil, opts_args = {})
  opts_args = {} if !opts_args
  Knj::ArrayExt.hash_sym(opts_args)
  
  return "" if !opthash
  cname = curvalue.class.name
  curvalue = curvalue.id if (cname == "Knj::Db_row" or cname == "Knj::Datarow")
  
  html = ""
  addsel = " selected=\"selected\"" if !curvalue
  
  html << "<option#{addsel} value=\"\">#{_("Add new")}</option>" if opts_args and (opts_args[:add] or opts_args[:addnew])
  html << "<option#{addsel} value=\"\">#{_("Choose")}</option>" if opts_args and opts_args[:choose]
  html << "<option#{addsel} value=\"\">#{_("None")}</option>" if opts_args and opts_args[:none]
  html << "<option#{addsel} value=\"\">#{_("All")}</option>" if opts_args and opts_args[:all]
  
  if opthash.is_a?(Hash) or opthash.class.to_s == "Dictionary"
    opthash.each do |key, value|
      html << "<option"
      sel = false
      
      if curvalue.is_a?(Array) and curvalue.index(key) != nil
        sel = true
      elsif curvalue.to_s == key.to_s
        sel = true
      elsif curvalue and curvalue.respond_to?(:is_knj?) and curvalue.id.to_s == key.to_s
        sel = true
      end
      
      html << " selected=\"selected\"" if sel
      html << " value=\"#{Knj::Web.html(key)}\">#{Knj::Web.html(value)}</option>"
    end
  elsif opthash.is_a?(Array)
    opthash.each_index do |key|
      if opthash[key.to_i] != nil
        html << "<option"
        html << " selected=\"selected\"" if curvalue.to_s == key.to_s
        html << " value=\"#{Knj::Web.html(key)}\">#{Knj::Web.html(opthash[key])}</option>"
      end
    end
  end
  
  return html
end

.os(servervar = nil) ⇒ Object



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/knj/web.rb', line 547

def self.os(servervar = nil)
  servervar = _server if !servervar
  if !servervar
    raise "Could not figure out meta data."
  end
  
  agent = servervar["HTTP_USER_AGENT"].to_s.downcase
  
  if agent.index("(windows;") != nil or agent.index("windows nt") != nil
    return {
      "os" => "win",
      "title" => "Windows"
    }
  elsif agent.index("linux") != nil
    return {
      "os" => "linux",
      "title" => "Linux"
    }
  end
  
  raise "Unknown OS: #{agent}"
end

.parse_cookies(str) ⇒ Object

Parses cookies-string and returns hash with parsed cookies.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/knj/web.rb', line 16

def self.parse_cookies(str)
  ret = {}
  
  str.split(/;\s*/).each do |cookie_str|
    if !match = cookie_str.match(/^(.*?)=\"(.*)\"$/)
      match = cookie_str.match(/^(.*?)=(.*)$/)
    end
    
    ret[self.urldec(match[1])] = self.urldec(match[2]) if match
  end
  
  return ret
end

.parse_name(seton, varname, value, args = {}) ⇒ Object



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
# File 'lib/knj/web.rb', line 85

def self.parse_name(seton, varname, value, args = {})
  if value.respond_to?(:filename) and value.filename
    realvalue = value
  else
    realvalue = value.to_s
    realvalue = self.urldec(realvalue) if args[:urldecode]
    realvalue = realvalue.force_encoding("utf-8") if args[:force_utf8] if realvalue.respond_to?(:force_encoding)
  end
  
  if varname and varname.index("[") != nil and match = varname.match(/\[(.*?)\]/)
    namepos = varname.index(match[0])
    name = varname.slice(0..namepos - 1)
    name = name.to_sym if args[:syms]
    seton[name] = {} if !seton.key?(name)
    
    secname, secname_empty = Knj::Web.parse_secname(seton[name], match[1], args)
    
    valuefrom = namepos + secname.to_s.length + 2
    restname = varname.slice(valuefrom..-1)
    
    if restname and restname.index("[") != nil
      seton[name][secname] = {} if !seton[name].key?(secname)
      Knj::Web.parse_name_second(seton[name][secname], restname, value, args)
    else
      seton[name][secname] = realvalue
    end
  else
    seton[varname] = realvalue
  end
end

.parse_name_second(seton, varname, value, args = {}) ⇒ Object



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
# File 'lib/knj/web.rb', line 137

def self.parse_name_second(seton, varname, value, args = {})
  if value.respond_to?(:filename) and value.filename
    realvalue = value
  else
    realvalue = value.to_s
    realvalue = realvalue.force_encoding("utf-8") if args[:force_utf8]
  end
  
  match = varname.match(/^\[(.*?)\]/)
  if match
    namepos = varname.index(match[0])
    name = match[1]
    secname, secname_empty = Knj::Web.parse_secname(seton, match[1], args)
    
    valuefrom = namepos + match[1].length + 2
    restname = varname.slice(valuefrom..-1)
    
    if restname and restname.index("[") != nil
      seton[secname] = {} if !seton.key?(secname)
      Knj::Web.parse_name_second(seton[secname], restname, value, args)
    else
      seton[secname] = realvalue
    end
  else
    seton[varname] = realvalue
  end
end

.parse_secname(seton, secname, args) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/knj/web.rb', line 116

def self.parse_secname(seton, secname, args)
  secname_empty = false
  if secname.length <= 0
    secname_empty = true
    try = 0
    
    loop do
      if !seton.key?(try.to_s)
        break
      else
        try += 1
      end
    end
    
    secname = try.to_s
  end
  
  secname = secname.to_sym if args[:syms] and secname.is_a?(String) and !(Float(secname) rescue false)
  return [secname, secname_empty]
end

.parse_set_cookies(str) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/knj/web.rb', line 30

def self.parse_set_cookies(str)
  str = String.new(str.to_s)
  return [] if str.length <= 0
  args = {}
  cookie_start_regex = /^(.+?)=(.*?)(;\s*|$)/
  
  match = str.match(cookie_start_regex)
  raise "Could not match cookie: '#{str}'." if !match
  str.gsub!(cookie_start_regex, "")
  
  args["name"] = self.urldec(match[1].to_s)
  args["value"] = self.urldec(match[2].to_s)
  
  while match = str.match(/(.+?)=(.*?)(;\s*|$)/)
    str = str.gsub(match[0], "")
    args[match[1].to_s.downcase] = match[2].to_s
  end
  
  return [args]
end

.parse_uri(str) ⇒ Object

Parses URI and returns hash with data.



5
6
7
8
9
10
11
12
13
# File 'lib/knj/web.rb', line 5

def self.parse_uri(str)
  uri_match = str.to_s.match(/^\/(.+?\..*?|)(\?(.*)|)$/)
  raise "Could not parse the URI: '#{str}'." if !uri_match
  
  return {
    :path => "/#{uri_match[1]}",
    :query => uri_match[3]
  }
end

.parse_urlquery(querystr, args = {}) ⇒ Object

Parses a query and returns the various _GET-variables.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/knj/web.rb', line 69

def self.parse_urlquery(querystr, args = {})
  get = {}
  querystr.to_s.split("&").each do |value|
    pos = value.index("=")
    
    if pos != nil
      name = value[0..pos-1]
      name = name.to_sym if args[:syms]
      valuestr = value.slice(pos+1..-1)
      Knj::Web.parse_name(get, Knj::Web.urldec(name), valuestr, args)
    end
  end
  
  return get
end

.redirect(string, args = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/knj/web.rb', line 178

def self.redirect(string, args = {})
  do_js = true
  
  #Header way
  if !@alert_sent
    if args[:perm]
      Php4r.header("Status: 301 Moved Permanently")
    else
      Php4r.header("Status: 303 See Other")
    end
    
    Php4r.header("Location: #{string}")
  end
  
  print "<script type=\"text/javascript\">location.href=\"#{string}\";</script>" if do_js
  exit
end

.rendering_engine(servervar = nil) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/knj/web.rb', line 525

def self.rendering_engine(servervar = nil)
  servervar = _server if !servervar
  if !servervar
    raise "Could not figure out meta data."
  end
  
  agent = servervar["HTTP_USER_AGENT"].to_s.downcase
  
  if agent.index("webkit") != nil
    return "webkit"
  elsif agent.index("gecko") != nil
    return "gecko"
  elsif agent.index("msie") != nil
    return "msie"
  elsif agent.index("w3c") != nil or agent.index("baiduspider") != nil or agent.index("googlebot") != nil or agent.index("bot") != nil
    return "bot"
  else
    #print "Unknown agent: #{agent}"
    return false
  end
end

.require_eruby(filepath) ⇒ Object



165
166
167
168
169
# File 'lib/knj/web.rb', line 165

def self.require_eruby(filepath)
  cont = File.read(filepath).untaint
  parse = Erubis.Eruby.new(cont)
  eval(parse.src.to_s)
end

.style_html(css) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/knj/web.rb', line 230

def self.style_html(css)
  return "" if css.length <= 0
  
  str = " style=\""
  
  css.each do |key, val|
    str << "#{key}: #{val};"
  end
  
  str << "\""
  
  return str
end

.url(args = {}) ⇒ Object

Calculates the URL from meta hash.



836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'lib/knj/web.rb', line 836

def self.url(args = {})
  if args[:meta]
    meta = args[:meta]
  else
    meta = _meta
  end
  
  url = ""
  
  if meta["HTTP_SSL_ENABLED"] == "1"
    url << "https://"
  else
    url << "http://"
  end
  
  url << meta["HTTP_HOST"]
  url << meta["REQUEST_URI"] if !args.key?(:uri) or args[:uri]
  
  return url
end

.urldec(string) ⇒ Object

URL-decodes a string.



814
815
816
817
818
819
# File 'lib/knj/web.rb', line 814

def self.urldec(string)
  #Thanks to CGI framework
  str = string.to_s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) do
    [$1.delete('%')].pack('H*')
  end
end

.urlenc(string) ⇒ Object

URL-encodes a string.



806
807
808
809
810
811
# File 'lib/knj/web.rb', line 806

def self.urlenc(string)
  #Thanks to CGI framework
  string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/) do
    '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
  end.tr(' ', '+')
end