Module: Knj::Php
- Defined in:
- lib/knj/php.rb
Instance Method Summary collapse
-
#array(*ele) ⇒ Object
Array-function emulator.
- #array_key_exists(key, arr) ⇒ Object
- #base64_decode(str) ⇒ Object
- #base64_encode(str) ⇒ Object
- #basename(filepath) ⇒ Object
- #call_user_func(*paras) ⇒ Object
- #chdir(dirname) ⇒ Object
- #class_exists(classname) ⇒ Object
- #count(array) ⇒ Object
- #date(date_format, date_input = nil) ⇒ Object
- #die(msg) ⇒ Object
- #dirname(filename) ⇒ Object
- #echo(string) ⇒ Object
- #empty(obj) ⇒ Object
- #explode(expl, strexp) ⇒ Object
- #fclose(fp) ⇒ Object
- #fgets(fp, length = 4096) ⇒ Object
- #file_exists(filepath) ⇒ Object
- #file_get_contents(filepath) ⇒ Object
- #file_put_contents(filepath, content) ⇒ Object
- #fopen(filename, mode) ⇒ Object
-
#foreach(element, &block) ⇒ Object
Foreach emulator.
- #fputs(fp, str) ⇒ Object
- #fread(fp, length = 4096) ⇒ Object
- #fwrite(fp, str) ⇒ Object
- #gettext(string) ⇒ Object
- #gtext(string) ⇒ Object
- #gzcompress(str, level = 3) ⇒ Object
- #gzuncompress(str, length = 0) ⇒ Object
- #header(headerstr) ⇒ Object
- #html_entity_decode(string) ⇒ Object
- #htmlspecialchars(string) ⇒ Object
- #http_build_query(obj) ⇒ Object
- #http_build_query_rec(orig_key, obj, first = true) ⇒ Object
- #include_once(filename) ⇒ Object
- #ip2long(ip) ⇒ Object
- #is_a(obj, classname) ⇒ Object
- #is_dir(filepath) ⇒ Object
- #is_file(filepath) ⇒ Object
- #is_numeric(n) ⇒ Object
- #isset(var) ⇒ Object
- #json_decode(data, as_array = false) ⇒ Object
- #json_encode(obj) ⇒ Object
-
#ksort(hash) ⇒ Object
Sort methods.
-
#long2ip(long) ⇒ Object
Thanks to this link for the following functions: snippets.dzone.com/posts/show/4509.
- #md5(string) ⇒ Object
-
#memory_get_peak_usage ⇒ Object
Should return the peak usage of the running script, but I have found no way to detect this…
-
#memory_get_usage ⇒ Object
Returns the scripts current memory usage.
- #method_exists(obj, method_name) ⇒ Object
- #microtime(get_as_float = false) ⇒ Object
- #mktime(hour = nil, min = nil, sec = nil, date = nil, month = nil, year = nil, is_dst = -1)) ⇒ Object
- #move_uploaded_file(tmp_path, new_path) ⇒ Object
- #msgbox(title, msg, type) ⇒ Object
- #nl2br(string) ⇒ Object
-
#number_format(number, precision = 2, seperator = ".", delimiter = ",") ⇒ Object
Returns the number as a formatted string.
- #opendir(dirpath) ⇒ Object
- #parse_str(str, hash) ⇒ Object
-
#passthru(cmd) ⇒ Object
Execute an external program and display raw output.
- #pathinfo(filepath) ⇒ Object
- #print_r(argument, ret = false, count = 1) ⇒ Object
- #readdir(res) ⇒ Object
- #realpath(pname) ⇒ Object
- #require_once(filename) ⇒ Object
- #serialize(argument) ⇒ Object
-
#session_start ⇒ Object
This method is only here for convertion support - it doesnt do anything.
- #setcookie(cname, cvalue, expire = nil, domain = nil) ⇒ Object
- #strip_tags(htmlstr) ⇒ Object
- #strpos(haystack, needle) ⇒ Object
- #strtolower(str) ⇒ Object
- #strtotime(date_string, cur = nil) ⇒ Object
- #strtoupper(str) ⇒ Object
- #substr(string, from, to = nil) ⇒ Object
- #time ⇒ Object
- #trim(argument) ⇒ Object
- #ucwords(string) ⇒ Object
- #unlink(filepath) ⇒ Object
- #unserialize(argument) ⇒ Object
- #urldecode(string) ⇒ Object
- #urlencode(string) ⇒ Object
- #utf8_decode(str) ⇒ Object
- #utf8_encode(str) ⇒ Object
Instance Method Details
#array(*ele) ⇒ Object
Array-function emulator.
933 934 935 936 937 938 939 940 941 |
# File 'lib/knj/php.rb', line 933 def array(*ele) return {} if ele.length <= 0 if ele.length == 1 and ele.first.is_a?(Hash) return ele.first end return ele end |
#array_key_exists(key, arr) ⇒ Object
943 944 945 946 947 948 949 950 951 952 |
# File 'lib/knj/php.rb', line 943 def array_key_exists(key, arr) if arr.is_a?(Hash) return arr.key?(key) elsif arr.is_a?(Array) return true if arr.index(key) != nil return false else raise "Unknown type of argument: '#{arr.class.name}'." end end |
#base64_decode(str) ⇒ Object
749 750 751 |
# File 'lib/knj/php.rb', line 749 def base64_decode(str) return Base64.decode64(str.to_s) end |
#base64_encode(str) ⇒ Object
740 741 742 743 744 745 746 747 |
# File 'lib/knj/php.rb', line 740 def base64_encode(str) #The strict-encode wont do corrupt newlines... if Base64.respond_to?("strict_encode64") return Base64.strict_encode64(str.to_s) else return Base64.encode64(str.to_s) end end |
#basename(filepath) ⇒ Object
731 732 733 734 735 736 737 738 |
# File 'lib/knj/php.rb', line 731 def basename(filepath) splitted = filepath.to_s.split("/").last return false if !splitted ret = splitted.split(".") ret.delete(ret.last) return ret.join(".") end |
#call_user_func(*paras) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/knj/php.rb', line 6 def call_user_func(*paras) if paras[0].is_a?(String) send_paras = [paras[0].to_sym] send_paras << paras[1] if paras[1] send(*send_paras) elsif paras[0].is_a?(Array) send_paras = [paras[0][1].to_sym] send_paras << paras[1] if paras[1] paras[0][0].send(*send_paras) else raise "Unknown user-func: '#{paras[0].class.name}'." end end |
#chdir(dirname) ⇒ Object
639 640 641 |
# File 'lib/knj/php.rb', line 639 def chdir(dirname) Dir.chdir(dirname) end |
#class_exists(classname) ⇒ Object
484 485 486 487 488 489 490 491 |
# File 'lib/knj/php.rb', line 484 def class_exists(classname) begin Kernel.const_get(classname) return true rescue return false end end |
#count(array) ⇒ Object
659 660 661 |
# File 'lib/knj/php.rb', line 659 def count(array) return array.length end |
#date(date_format, date_input = nil) ⇒ Object
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 |
# File 'lib/knj/php.rb', line 714 def date(date_format, date_input = nil) if date_input == nil date_object = Time.now elsif Knj::Php.is_numeric(date_input) date_object = Time.at(date_input.to_i) elsif date_input.is_a?(Knj::Datet) date_object = date_input.time elsif date_input.is_a?(Time) date_object = date_input else raise "Unknown date given: '#{date_input}', '#{date_input.class.name}'." end date_format = date_format.gsub("Y", "%Y").gsub("y", "%y").gsub("m", "%m").gsub("d", "%d").gsub("H", "%H").gsub("i", "%M").gsub("s", "%S") return date_object.strftime(date_format) end |
#die(msg) ⇒ Object
507 508 509 510 |
# File 'lib/knj/php.rb', line 507 def die(msg) print msg exit end |
#dirname(filename) ⇒ Object
635 636 637 |
# File 'lib/knj/php.rb', line 635 def dirname(filename) File.dirname(filename) end |
#echo(string) ⇒ Object
651 652 653 |
# File 'lib/knj/php.rb', line 651 def echo(string) print string end |
#empty(obj) ⇒ Object
954 955 956 957 958 959 960 961 962 |
# File 'lib/knj/php.rb', line 954 def empty(obj) if obj.respond_to?("empty?") return obj.empty? elsif obj == nil return true else raise "Dont know how to handle object on 'empty': '#{obj.class.name}'." end end |
#explode(expl, strexp) ⇒ Object
631 632 633 |
# File 'lib/knj/php.rb', line 631 def explode(expl, strexp) return strexp.to_s.split(expl) end |
#fclose(fp) ⇒ Object
564 565 566 |
# File 'lib/knj/php.rb', line 564 def fclose(fp) fp.close end |
#fgets(fp, length = 4096) ⇒ Object
560 561 562 |
# File 'lib/knj/php.rb', line 560 def fgets(fp, length = 4096) return fp.read(length) end |
#file_exists(filepath) ⇒ Object
430 431 432 433 |
# File 'lib/knj/php.rb', line 430 def file_exists(filepath) return true if File.exists?(filepath.to_s.untaint) return false end |
#file_get_contents(filepath) ⇒ Object
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 |
# File 'lib/knj/php.rb', line 373 def file_get_contents(filepath) filepath = filepath.to_s if http_match = filepath.match(/^http(s|):\/\/([A-z_\d\.]+)(|:(\d+))(\/(.+))$/) if http_match[4].to_s.length > 0 port = http_match[4].to_i end args = { "host" => http_match[2] } if http_match[1] == "s" args["ssl"] = true args["validate"] = false if !port port = 443 end end args["port"] = port if port http = Knj::Http.new(args) data = http.get(http_match[5]) return data["data"] end return File.read(filepath.untaint) end |
#file_put_contents(filepath, content) ⇒ Object
367 368 369 370 371 |
# File 'lib/knj/php.rb', line 367 def file_put_contents(filepath, content) File.open(filepath.untaint, "w") do |file| file.write(content) end end |
#fopen(filename, mode) ⇒ Object
528 529 530 531 532 533 534 |
# File 'lib/knj/php.rb', line 528 def fopen(filename, mode) begin return File.open(filename, mode) rescue return false end end |
#foreach(element, &block) ⇒ Object
Foreach emulator.
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 |
# File 'lib/knj/php.rb', line 902 def foreach(element, &block) raise "No or unsupported block given." if !block.respond_to?(:call) or !block.respond_to?(:arity) arity = block.arity cname = element.class.name.to_s if element.is_a?(Array) or cname == "Array_enumerator" element.each_index do |key| if arity == 2 block.call(key, element[key]) elsif arity == 1 block.call(element[key]) else raise "Unknown arity: '#{arity}'." end end elsif element.is_a?(Hash) element.each do |key, val| if arity == 2 block.call(key, val) elsif arity == 1 block.call(val) else raise "Unknown arity: '#{arity}'." end end else raise "Unknown element: '#{element.class.name}'." end end |
#fputs(fp, str) ⇒ Object
546 547 548 549 550 551 552 553 554 |
# File 'lib/knj/php.rb', line 546 def fputs(fp, str) begin fp.print str rescue return false end return true end |
#fread(fp, length = 4096) ⇒ Object
556 557 558 |
# File 'lib/knj/php.rb', line 556 def fread(fp, length = 4096) return fp.read(length) end |
#fwrite(fp, str) ⇒ Object
536 537 538 539 540 541 542 543 544 |
# File 'lib/knj/php.rb', line 536 def fwrite(fp, str) begin fp.print str rescue return false end return true end |
#gettext(string) ⇒ Object
190 191 192 |
# File 'lib/knj/php.rb', line 190 def gettext(string) return GetText._(string) end |
#gtext(string) ⇒ Object
186 187 188 |
# File 'lib/knj/php.rb', line 186 def gtext(string) return GetText._(string) end |
#gzcompress(str, level = 3) ⇒ Object
866 867 868 869 870 871 872 873 874 |
# File 'lib/knj/php.rb', line 866 def gzcompress(str, level = 3) require "zlib" zstream = Zlib::Deflate.new gzip_str = zstream.deflate(str.to_s, Zlib::FINISH) zstream.close return gzip_str end |
#gzuncompress(str, length = 0) ⇒ Object
876 877 878 879 880 881 882 883 884 885 |
# File 'lib/knj/php.rb', line 876 def gzuncompress(str, length = 0) require "zlib" zstream = Zlib::Inflate.new plain_str = zstream.inflate(str.to_s) zstream.finish zstream.close return plain_str.to_s end |
#header(headerstr) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/knj/php.rb', line 327 def header(headerstr) match = headerstr.to_s.match(/(.*): (.*)/) if match key = match[1] value = match[2] else #HTTP/1.1 404 Not Found match_status = headerstr.to_s.match(/^HTTP\/[0-9\.]+ ([0-9]+) (.+)$/) if match_status key = "Status" value = match_status[1] + " " + match_status[2] else raise "Couldnt parse header." end end _kas.header(key, value) #This is for knjAppServer - knj. return true end |
#html_entity_decode(string) ⇒ Object
493 494 495 496 497 |
# File 'lib/knj/php.rb', line 493 def html_entity_decode(string) string = Knj::Web.html(string) string = string.gsub("ø", "ø").gsub("æ", "æ").gsub("å", "ĂĄ").gsub("€", "€").gsub("#39;", "'").gsub("&", "&").gsub(">", ">").gsub("<", "<").gsub(""", '"').gsub("'", "'") return string end |
#htmlspecialchars(string) ⇒ Object
233 234 235 |
# File 'lib/knj/php.rb', line 233 def htmlspecialchars(string) return Knj::Web.html(string) end |
#http_build_query(obj) ⇒ Object
237 238 239 |
# File 'lib/knj/php.rb', line 237 def http_build_query(obj) return self.http_build_query_rec("", obj) end |
#http_build_query_rec(orig_key, obj, first = true) ⇒ Object
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 |
# File 'lib/knj/php.rb', line 241 def http_build_query_rec(orig_key, obj, first = true) url = "" first_ele = true if obj.is_a?(Array) ele_count = 0 obj.each do |val| orig_key_str = "#{orig_key}[#{ele_count}]" val = "#<Model::#{val.table}::#{val.id}>" if val.respond_to?("is_knj?") if val.is_a?(Hash) or val.is_a?(Array) url << self.http_build_query_rec(orig_key_str, val, false) else url << "&" if !first or !first_ele url << "#{Knj::Web.urlenc(orig_key_str)}=#{Knj::Web.urlenc(val)}" end first_ele = false if first_ele ele_count += 1 end elsif obj.is_a?(Hash) obj.each do |key, val| if first orig_key_str = key else orig_key_str = "#{orig_key}[#{key}]" end val = "#<Model::#{val.table}::#{val.id}>" if val.respond_to?("is_knj?") if val.is_a?(Hash) or val.is_a?(Array) url << self.http_build_query_rec(orig_key_str, val, false) else url << "&" if !first or !first_ele url << "#{Knj::Web.urlenc(orig_key_str)}=#{Knj::Web.urlenc(val)}" end first_ele = false if first_ele end else raise "Unknown class: '#{obj.class.name}'." end return url end |
#include_once(filename) ⇒ Object
643 644 645 |
# File 'lib/knj/php.rb', line 643 def include_once(filename) require filename end |
#ip2long(ip) ⇒ Object
789 790 791 |
# File 'lib/knj/php.rb', line 789 def ip2long(ip) return IPAddr.new(ip).to_i end |
#is_a(obj, classname) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/knj/php.rb', line 24 def is_a(obj, classname) classname = classname.to_s classname = "#{classname[0..0].upcase}#{classname[1..999]}" if obj.is_a?(classname) return true end return false end |
#is_dir(filepath) ⇒ Object
416 417 418 419 420 421 422 423 424 |
# File 'lib/knj/php.rb', line 416 def is_dir(filepath) begin return true if File.directory?(filepath) rescue return false end return false end |
#is_file(filepath) ⇒ Object
404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/knj/php.rb', line 404 def is_file(filepath) begin if File.file?(filepath) return true end rescue return false end return false end |
#is_numeric(n) ⇒ Object
4 |
# File 'lib/knj/php.rb', line 4 def is_numeric(n) Float n rescue false end |
#isset(var) ⇒ Object
288 289 290 291 |
# File 'lib/knj/php.rb', line 288 def isset(var) return false if var == nil or var == false return true end |
#json_decode(data, as_array = false) ⇒ Object
673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'lib/knj/php.rb', line 673 def json_decode(data, as_array = false) #FIXME: Should be able to return as object, which will break all projects using it without second argument... raise "String was not given to 'Knj::Php.json_decode'." if !data.is_a?(String) if Knj::Php.class_exists("Rho") return Rho::JSON.parse(data) elsif Knj::Php.class_exists("JSON") return JSON.parse(data) else raise "Could not figure out which JSON lib to use." end end |
#json_encode(obj) ⇒ Object
663 664 665 666 667 668 669 670 671 |
# File 'lib/knj/php.rb', line 663 def json_encode(obj) if Knj::Php.class_exists("Rho") return Rho::JSON.generate(obj) elsif Knj::Php.class_exists("JSON") return JSON.generate(obj) else raise "Could not figure out which JSON lib to use." end end |
#ksort(hash) ⇒ Object
Sort methods.
888 889 890 891 892 893 894 895 896 897 898 899 |
# File 'lib/knj/php.rb', line 888 def ksort(hash) nhash = hash.sort do |a, b| a[0] <=> b[0] end newhash = {} nhash.each do |val| newhash[val[0]] = val[1][0] end return newhash end |
#long2ip(long) ⇒ Object
Thanks to this link for the following functions: snippets.dzone.com/posts/show/4509
856 857 858 859 860 861 862 863 864 |
# File 'lib/knj/php.rb', line 856 def long2ip(long) ip = [] 4.times do |i| ip.push(long.to_i & 255) long = long.to_i >> 8 end ip.reverse.join(".") end |
#md5(string) ⇒ Object
322 323 324 325 |
# File 'lib/knj/php.rb', line 322 def md5(string) require "digest" return Digest::MD5.hexdigest(string.to_s) end |
#memory_get_peak_usage ⇒ Object
Should return the peak usage of the running script, but I have found no way to detect this… Instead returns the currently memory usage.
785 786 787 |
# File 'lib/knj/php.rb', line 785 def memory_get_peak_usage return self.memory_get_usage end |
#memory_get_usage ⇒ Object
Returns the scripts current memory usage.
778 779 780 781 782 |
# File 'lib/knj/php.rb', line 778 def memory_get_usage # FIXME: This only works on Linux at the moment, since we are doing this by command line - knj. memory_usage = `ps -o rss= -p #{Process.pid}`.to_i * 1024 return memory_usage end |
#method_exists(obj, method_name) ⇒ Object
20 21 22 |
# File 'lib/knj/php.rb', line 20 def method_exists(obj, method_name) return obj.respond_to?(method_name.to_s) end |
#microtime(get_as_float = false) ⇒ Object
691 692 693 694 695 696 697 698 |
# File 'lib/knj/php.rb', line 691 def microtime(get_as_float = false) microtime = Time.now.to_f return microtime if get_as_float splitted = microtime.to_s.split(",") return "#{splitted[0]} #{splitted[1]}" end |
#mktime(hour = nil, min = nil, sec = nil, date = nil, month = nil, year = nil, is_dst = -1)) ⇒ Object
700 701 702 703 704 705 706 707 708 709 710 711 712 |
# File 'lib/knj/php.rb', line 700 def mktime(hour = nil, min = nil, sec = nil, date = nil, month = nil, year = nil, is_dst = -1) cur_time = Time.new hour = cur_time.hour if hour == nil min = cur_time.min if min == nil sec = cur_time.sec if sec == nil date = cur_time.date if date == nil month = cur_time.month if month == nil year = cur_time.year if year == nil new_time = Knj::Datet.in("#{year.to_s}-#{month.to_s}-#{date.to_s} #{hour.to_s}:#{min.to_s}:#{sec.to_s}") return new_time.to_i end |
#move_uploaded_file(tmp_path, new_path) ⇒ Object
568 569 570 |
# File 'lib/knj/php.rb', line 568 def move_uploaded_file(tmp_path, new_path) FileUtils.mv(tmp_path.untaint, new_path.untaint) end |
#msgbox(title, msg, type) ⇒ Object
655 656 657 |
# File 'lib/knj/php.rb', line 655 def msgbox(title, msg, type) Knj::Gtk2.msgbox(msg, type, title) end |
#nl2br(string) ⇒ Object
348 349 350 |
# File 'lib/knj/php.rb', line 348 def nl2br(string) return string.to_s.gsub("\n", "<br />\n") end |
#number_format(number, precision = 2, seperator = ".", delimiter = ",") ⇒ Object
Returns the number as a formatted string.
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 |
# File 'lib/knj/php.rb', line 195 def number_format(number, precision = 2, seperator = ".", delimiter = ",") number = number.to_f if !number.is_a?(Float) precision = precision.to_i return sprintf("%.#{precision.to_s}f", number).gsub(".", seperator) if number < 1 and number > -1 number = sprintf("%.#{precision.to_s}f", number).split(".") str = "" number[0].reverse.scan(/(.{1,3})/) do |match| if match[0] == "-" #This happens if the number is a negative number and we have reaches the minus-sign. str << match[0] else str << delimiter if str.length > 0 str << match[0] end end str = str.reverse if precision > 0 str << "#{seperator}#{number[1]}" end return str end |
#opendir(dirpath) ⇒ Object
512 513 514 515 516 517 518 519 |
# File 'lib/knj/php.rb', line 512 def opendir(dirpath) res = {:files => [], :index => 0} Dir.foreach(dirpath) do |file| res[:files] << file end return res end |
#parse_str(str, hash) ⇒ Object
360 361 362 363 364 365 |
# File 'lib/knj/php.rb', line 360 def parse_str(str, hash) res = Knj::Web.parse_urlquery(str, {:urldecode => true}) res.each do |key, val| hash[key] = val end end |
#passthru(cmd) ⇒ Object
Execute an external program and display raw output.
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 |
# File 'lib/knj/php.rb', line 794 def passthru(cmd) if RUBY_ENGINE == "jruby" IO.popen4(cmd) do |pid, stdin, stdout, stderr| tout = Thread.new do begin stdout.sync = true stdout.each do |str| $stdout.print str end rescue => e $stdout.print Knj::Errors.error_str(e) end end terr = Thread.new do begin stderr.sync = true stderr.each do |str| $stderr.print str end rescue => e $stderr.print Knj::Errors.error_str(e) end end tout.join terr.join end else require "open3" Open3.popen3(cmd) do |stdin, stdout, stderr| tout = Thread.new do begin stdout.sync = true stdout.each do |str| $stdout.print str end rescue => e $stdout.print Knj::Errors.error_str(e) end end terr = Thread.new do begin stderr.sync = true stderr.each do |str| $stderr.print str end rescue => e $stderr.print Knj::Errors.error_str(e) end end tout.join terr.join end end return nil end |
#pathinfo(filepath) ⇒ Object
753 754 755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/knj/php.rb', line 753 def pathinfo(filepath) filepath = filepath.to_s dirname = File.dirname(filepath) dirname = "" if dirname == "." return { "dirname" => dirname, "basename" => self.basename(filepath), "extension" => filepath.split(".").last, "filename" => filepath.split("/").last } end |
#print_r(argument, ret = false, count = 1) ⇒ Object
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 |
# File 'lib/knj/php.rb', line 35 def print_r(argument, ret = false, count = 1) retstr = "" cstr = argument.class.to_s supercl = argument.class.superclass superstr = supercl.to_s if supercl valids = ["Apache::Table", "CGI", "Hash", "Knj::Datarow", "Knj::Datarow_custom", "Knj::Db_row", "Knj::Hash_methods", "Knjappserver::Session_accessor", "SQLite3::ResultSet::HashWithTypes"] if Knj::Strings.is_a?(argument, valids) or argument.respond_to?(:to_hash) if argument.respond_to?(:to_hash) argument_use = argument.to_hash else argument_use = argument end retstr << "#{argument.class.name}{\n" argument_use.each do |pair| i = 0 while(i < count) retstr << " " i += 1 end if pair[0].is_a?(Symbol) keystr = ":#{pair[0].to_s}" else keystr = pair[0].to_s end retstr << "[#{keystr}] => " retstr << Knj::Php.print_r(pair[1], true, count + 1).to_s end i = 0 while(i < count - 1) retstr << " " i += 1 end retstr << "}\n" elsif cstr == "Dictionary" retstr << "#{argument.class.name}{\n" argument.each do |key, val| i = 0 while(i < count) retstr << " " i += 1 end if key.is_a?(Symbol) keystr = ":#{key.to_s}" else keystr = key.to_s end retstr << "[#{keystr}] => " retstr << Knj::Php.print_r(val, true, count + 1).to_s end i = 0 while(i < count - 1) retstr << " " i += 1 end retstr << "}\n" elsif argument.is_a?(MatchData) or argument.is_a?(Array) or cstr == "Array" or supercl.is_a?(Array) retstr << "#{argument.class.name}{\n" arr_count = 0 argument.to_a.each do |i| i_spaces = 0 while(i_spaces < count) retstr << " " i_spaces += 1 end retstr << "[#{arr_count}] => " retstr << Knj::Php.print_r(i, true, count + 1).to_s arr_count += 1 end i_spaces = 0 while(i_spaces < count - 1) retstr << " " i_spaces += 1 end retstr << "}\n" elsif cstr == "WEBrick::HTTPUtils::FormData" retstr << "{#{argument.class.to_s}}" elsif argument.is_a?(String) or argument.is_a?(Integer) or argument.is_a?(Fixnum) or argument.is_a?(Float) retstr << argument.to_s + "\n" elsif argument.is_a?(Symbol) retstr << ":#{argument.to_s}\n" elsif argument.is_a?(Exception) retstr << "#\{#{argument.class.to_s}: #{argument.}}\n" elsif cstr == "Knj::Unix_proc" retstr << "#{argument.class.name}::data - " retstr << Knj::Php.print_r(argument.data, true, count).to_s elsif cstr == "Thread" retstr << "#{argument.class.name} - " hash = {} argument.keys.each do |key| hash[key] = argument[key] end retstr << Knj::Php.print_r(hash, true, count).to_s elsif cstr == "Class" retstr << "#{argument.class.to_s} - " hash = {"name" => argument.name} retstr << Knj::Php.print_r(hash, true, count).to_s elsif cstr == "URI::Generic" retstr << "#{argument.class.to_s}{\n" methods = [:host, :port, :scheme, :path] count += 1 methods.each do |method| i_spaces = 0 while(i_spaces < count - 1) retstr << " " i_spaces += 1 end retstr << "#{method}: #{argument.send(method)}\n" end count -= 1 i = 0 while(i < count - 1) retstr << " " i += 1 end retstr << "}\n" elsif cstr == "Time" or cstr == "Knj::Datet" argument = argument.time if cstr == "Knj::Datet" retstr << "#{cstr}::#{"%04d" % argument.year}-#{"%02d" % argument.month}-#{"%02d" % argument.day} #{"%02d" % argument.hour}:#{"%02d" % argument.min}:#{"%02d" % argument.sec}\n" else #print argument.to_s, "\n" retstr << "Unknown class: '#{cstr}' with superclass '#{supercl}'.\n" end if ret.is_a?(TrueClass) return retstr else print retstr end end |
#readdir(res) ⇒ Object
521 522 523 524 525 526 |
# File 'lib/knj/php.rb', line 521 def readdir(res) ret = res[:files][res[:index]] if res[:files].index(res[:index]) != nil return false if !ret res[:index] += 1 return ret end |
#realpath(pname) ⇒ Object
767 768 769 770 771 772 773 774 775 |
# File 'lib/knj/php.rb', line 767 def realpath(pname) require "pathname" begin return Pathname.new(pname.to_s).realpath.to_s rescue => e return false end end |
#require_once(filename) ⇒ Object
647 648 649 |
# File 'lib/knj/php.rb', line 647 def require_once(filename) require filename end |
#serialize(argument) ⇒ Object
968 969 970 971 |
# File 'lib/knj/php.rb', line 968 def serialize(argument) require "php_serialize" #gem: php-serialize return PHP.serialize(argument) end |
#session_start ⇒ Object
This method is only here for convertion support - it doesnt do anything.
627 628 629 |
# File 'lib/knj/php.rb', line 627 def session_start end |
#setcookie(cname, cvalue, expire = nil, domain = nil) ⇒ Object
613 614 615 616 617 618 619 620 621 622 623 624 |
# File 'lib/knj/php.rb', line 613 def (cname, cvalue, expire = nil, domain = nil) args = { "name" => cname, "value" => cvalue, "path" => "/" } args["expires"] = Time.at(expire) if expire args["domain"] = domain if domain _kas.(args) return status end |
#strip_tags(htmlstr) ⇒ Object
499 500 501 502 503 504 505 |
# File 'lib/knj/php.rb', line 499 def (htmlstr) htmlstr.scan(/(<([\/A-z]+).*?>)/) do |match| htmlstr = htmlstr.gsub(match[0], "") end return htmlstr.gsub(" ", " ") end |
#strpos(haystack, needle) ⇒ Object
293 294 295 296 297 |
# File 'lib/knj/php.rb', line 293 def strpos(haystack, needle) return false if !haystack return false if !haystack.to_s.include?(needle) return haystack.index(needle) end |
#strtolower(str) ⇒ Object
229 230 231 |
# File 'lib/knj/php.rb', line 229 def strtolower(str) return str.to_s.downcase end |
#strtotime(date_string, cur = nil) ⇒ Object
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 |
# File 'lib/knj/php.rb', line 435 def strtotime(date_string, cur = nil) if !cur cur = Time.new else cur = Time.at(cur) end date_string = date_string.to_s.downcase if date_string.match(/[0-9]+-[0-9]+-[0-9]+/i) begin return Time.local(*ParseDate.parsedate(date_string)).to_i rescue return 0 end end date_string.scan(/((\+|-)([0-9]+) (\S+))/) do |match| timestr = match[3] number = match[2].to_i mathval = match[1] add = nil if timestr == "years" or timestr == "year" add = ((number.to_i * 3600) * 24) * 365 elsif timestr == "months" or timestr == "month" add = ((number.to_i * 3600) * 24) * 30 elsif timestr == "weeks" or timestr == "week" add = (number.to_i * 3600) * 24 * 7 elsif timestr == "days" or timestr == "day" add = (number.to_i * 3600) * 24 elsif timestr == "hours" or timestr == "hour" add = number.to_i * 3600 elsif timestr == "minutes" or timestr == "minute" or timestr == "min" or timestr == "mints" add = number.to_i * 60 elsif timestr == "seconds" or timestr == "second" or timestr == "sec" or timestr == "secs" add = number.to_i end if mathval == "+" cur += add elsif mathval == "-" cur -= add end end return cur.to_i end |
#strtoupper(str) ⇒ Object
225 226 227 |
# File 'lib/knj/php.rb', line 225 def strtoupper(str) return str.to_s.upcase end |
#substr(string, from, to = nil) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/knj/php.rb', line 299 def substr(string, from, to = nil) #If 'to' is not given it should be the total length of the string. if to == nil to = string.length end #The behaviour with a negative 'to' is not the same as in PHP. Hack it! if to < 0 to = string.length + to end #Cut the string. string = "#{string[from.to_i, to.to_i]}" #Sometimes the encoding will no longer be valid. Fix that if that is the case. if !string.valid_encoding? and Knj::Php.class_exists("Iconv") string = Iconv.conv("UTF-8//IGNORE", "UTF-8", "#{string} ")[0..-2] end #Return the cut string. return string end |
#time ⇒ Object
687 688 689 |
# File 'lib/knj/php.rb', line 687 def time return Time.now.to_i end |
#trim(argument) ⇒ Object
964 965 966 |
# File 'lib/knj/php.rb', line 964 def trim(argument) return argument.to_s.strip end |
#ucwords(string) ⇒ Object
221 222 223 |
# File 'lib/knj/php.rb', line 221 def ucwords(string) return string.to_s.split(" ").select {|w| w.capitalize! || w }.join(" ") end |
#unlink(filepath) ⇒ Object
426 427 428 |
# File 'lib/knj/php.rb', line 426 def unlink(filepath) FileUtils.rm(filepath) end |
#unserialize(argument) ⇒ Object
973 974 975 976 |
# File 'lib/knj/php.rb', line 973 def unserialize(argument) require "php_serialize" #gem: php-serialize return PHP.unserialize(argument.to_s) end |
#urldecode(string) ⇒ Object
352 353 354 |
# File 'lib/knj/php.rb', line 352 def urldecode(string) return Knj::Web.urldec(string) end |
#urlencode(string) ⇒ Object
356 357 358 |
# File 'lib/knj/php.rb', line 356 def urlencode(string) return Knj::Web.urlenc(string) end |
#utf8_decode(str) ⇒ Object
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/knj/php.rb', line 592 def utf8_decode(str) str = str.to_s if str.respond_to?(:to_s) require "iconv" if RUBY_PLATFORM == "java" #This fixes a bug in JRuby where Iconv otherwise would not be detected. if str.respond_to?(:encode) begin return str.encode("utf-8", "iso-8859-1") rescue Encoding::InvalidByteSequenceError #ignore - try iconv end end require "iconv" begin return Iconv.conv("utf-8", "iso-8859-1", str.to_s) rescue return Iconv.conv("utf-8//ignore", "iso-8859-1", str.to_s) end end |
#utf8_encode(str) ⇒ Object
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/knj/php.rb', line 572 def utf8_encode(str) str = str.to_s if str.respond_to?("to_s") if str.respond_to?("encode") begin return str.encode("iso-8859-1", "utf-8") rescue Encoding::InvalidByteSequenceError #ignore - try iconv end end require "iconv" begin return Iconv.conv("iso-8859-1", "utf-8", str.to_s) rescue return Iconv.conv("iso-8859-1//ignore", "utf-8", "#{str} ").slice(0..-2) end end |