knjrbfw 0.0.72 → 0.0.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -0
- data/VERSION +1 -1
- data/knjrbfw.gemspec +4 -3
- data/lib/knj/arrayext.rb +7 -5
- data/lib/knj/datarow.rb +6 -0
- data/lib/knj/datarow_custom.rb +4 -0
- data/lib/knj/datestamp.rb +4 -4
- data/lib/knj/degulesider.rb +1 -1
- data/lib/knj/erb/include.rb +1 -1
- data/lib/knj/fs/fs.rb +3 -3
- data/lib/knj/gtk2_menu.rb +1 -1
- data/lib/knj/hash_methods.rb +9 -0
- data/lib/knj/iotop.rb +1 -1
- data/lib/knj/ironruby-gtk2/gladexml.rb +1 -1
- data/lib/knj/jruby-gtk2/gladexml.rb +1 -1
- data/lib/knj/jruby-gtk2/tests/test_glade_window.rb +1 -1
- data/lib/knj/knj.rb +2 -2
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb +1 -1
- data/lib/knj/knjdb/libknjdb.rb +2 -2
- data/lib/knj/knjdb/libknjdb_row.rb +4 -0
- data/lib/knj/locales.rb +1 -1
- data/lib/knj/notify.rb +1 -1
- data/lib/knj/objects.rb +4 -4
- data/lib/knj/objects/objects_sqlhelper.rb +1 -1
- data/lib/knj/php_parser/php_parser.rb +1 -1
- data/lib/knj/php_parser/tests/test.rb +2 -2
- data/lib/knj/power_manager.rb +1 -1
- data/lib/knj/process.rb +1 -0
- data/lib/knj/scripts/ip2location.rb +2 -2
- data/lib/knj/scripts/php_to_rb_helper.rb +6 -6
- data/lib/knj/scripts/process_meta_exec.rb +1 -1
- data/lib/knj/sms.rb +3 -3
- data/lib/knj/strings.rb +3 -1
- data/lib/knj/table_writer.rb +4 -4
- data/lib/knj/tests/test_degulesider.rb +1 -1
- data/lib/knj/tests/test_http2.rb +1 -1
- data/lib/knj/tests/test_mount.rb +1 -1
- data/lib/knj/tests/test_retry.rb +1 -1
- data/lib/knj/unix_proc.rb +5 -0
- data/lib/knj/web.rb +11 -9
- data/spec/cmd_parser_spec.rb +2 -2
- data/spec/process_spec.rb +1 -1
- data/spec/web_spec.rb +1 -1
- metadata +23 -14
- data/lib/knj/php.rb +0 -978
- data/spec/php_spec.rb +0 -98
@@ -159,12 +159,12 @@ end
|
|
159
159
|
|
160
160
|
#Replace various forms of foreach'es.
|
161
161
|
cont.scan(/((\s+?)foreach\s*\((.+?)\s+as\s+\$(#{regexes["var"]}?)\s+=>\s+\$(#{regexes["var"]}?)\)\s*{(\s+?))/i) do |match|
|
162
|
-
rb_str = "#{match[1]}
|
162
|
+
rb_str = "#{match[1]}Php4r.foreach(#{match[2]}) do |$#{match[3]}, $#{match[4]}|#{match[5]}"
|
163
163
|
cont = cont.gsub(match[0], rb_str)
|
164
164
|
end
|
165
165
|
|
166
166
|
cont.scan(/((\s+?)foreach\s*\((.+?)\s+as\s+\$(#{regexes["var"]}?)\s*\)\s*{(\s+?))/i) do |match|
|
167
|
-
rb_str = "#{match[1]}
|
167
|
+
rb_str = "#{match[1]}Php4r.foreach(#{match[2]}) do |$#{match[3]}|#{match[4]}"
|
168
168
|
cont = cont.gsub(match[0], rb_str)
|
169
169
|
end
|
170
170
|
|
@@ -327,7 +327,7 @@ replaces = {
|
|
327
327
|
";\n" => "\n"
|
328
328
|
}
|
329
329
|
|
330
|
-
pinfo =
|
330
|
+
pinfo = Php4r.pathinfo(options[:file])
|
331
331
|
|
332
332
|
if options.key?(:tags) and !options[:tags]
|
333
333
|
rbfname = "#{pinfo["basename"]}.rb"
|
@@ -354,7 +354,7 @@ end
|
|
354
354
|
|
355
355
|
funcs_skip = [:foreach]
|
356
356
|
funcs_remove = [:session_start]
|
357
|
-
funcs_all = funcs_skip |
|
357
|
+
funcs_all = funcs_skip | Php4r.instance_methods
|
358
358
|
|
359
359
|
funcs_all.each do |method_name|
|
360
360
|
next if funcs_skip.index(method_name) != nil
|
@@ -362,7 +362,7 @@ funcs_all.each do |method_name|
|
|
362
362
|
if funcs_remove.index(method_name) != nil
|
363
363
|
cont = cont.gsub(match[0], "#{match[1]}#removal of func: #{method_name}(")
|
364
364
|
else
|
365
|
-
cont = cont.gsub(match[0], "#{match[1]}
|
365
|
+
cont = cont.gsub(match[0], "#{match[1]}Php4r.#{method_name}(")
|
366
366
|
end
|
367
367
|
end
|
368
368
|
end
|
@@ -371,6 +371,6 @@ if pinfo["dirname"].to_s.length > 0
|
|
371
371
|
rbfname = "#{pinfo["dirname"]}/#{rbfname}"
|
372
372
|
end
|
373
373
|
|
374
|
-
|
374
|
+
Php4r.file_put_contents(rbfname, cont)
|
375
375
|
#require "#{Dir.pwd}/#{rbfname}"
|
376
376
|
#print cont
|
@@ -130,7 +130,7 @@ objects = {}
|
|
130
130
|
elsif obj["type"] == "process_data"
|
131
131
|
d.answer("type" => "process_data_success", "pid" => Process.pid)
|
132
132
|
else
|
133
|
-
raise "Didnt know how to handle hash: '#{
|
133
|
+
raise "Didnt know how to handle hash: '#{Php4r.print_r(obj, true)}'."
|
134
134
|
end
|
135
135
|
else
|
136
136
|
raise "Unknown object: '#{obj.class.name}'."
|
data/lib/knj/sms.rb
CHANGED
@@ -30,10 +30,10 @@ class Knj::Sms
|
|
30
30
|
|
31
31
|
result = @soap.SendMessage({
|
32
32
|
"cellphone" => @opts[:user],
|
33
|
-
"password" =>
|
33
|
+
"password" => Php4r.md5(@opts[:pass]),
|
34
34
|
"smsTo" => {"string" => number},
|
35
35
|
"smscontents" => msg,
|
36
|
-
"sendDate" =>
|
36
|
+
"sendDate" => Php4r.date("Y-m-d"),
|
37
37
|
"deliveryReport" => "0",
|
38
38
|
"fromNumber" => @opts[:user]
|
39
39
|
})
|
@@ -45,7 +45,7 @@ class Knj::Sms
|
|
45
45
|
@db.insert("outbox", {
|
46
46
|
"number" => number,
|
47
47
|
"text" => msg,
|
48
|
-
"insertdate" =>
|
48
|
+
"insertdate" => Php4r.date("Y-m-d H:i:s")
|
49
49
|
})
|
50
50
|
else
|
51
51
|
raise "Not supported: " + @type
|
data/lib/knj/strings.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
#encoding: utf-8
|
2
2
|
|
3
|
+
require "php4r"
|
4
|
+
|
3
5
|
#This module contains various methods to escape, change or treat strings.
|
4
6
|
module Knj::Strings
|
5
7
|
#Returns a string that is safe to use on the command line.
|
@@ -121,7 +123,7 @@ module Knj::Strings
|
|
121
123
|
|
122
124
|
#Returns 'Yes' or 'No' based on a value. The value can be 0, 1, yes, no, true or false.
|
123
125
|
def self.yn_str(value, str_yes = "Yes", str_no = "No")
|
124
|
-
value = value.to_i if
|
126
|
+
value = value.to_i if Php4r.is_numeric(value)
|
125
127
|
value_s = value.to_s
|
126
128
|
|
127
129
|
if value.is_a?(Integer)
|
data/lib/knj/table_writer.rb
CHANGED
@@ -59,9 +59,9 @@ class Knj::Table_writer
|
|
59
59
|
val = arr[key]
|
60
60
|
|
61
61
|
if val.is_a?(Hash) and val["type"] == "decimal"
|
62
|
-
arr[key] =
|
62
|
+
arr[key] = Php4r.number_format(val["value"], @args["amount_decimals"], @args["amount_dsep"], @args["amount_tsep"])
|
63
63
|
elsif val.is_a?(Hash) and val["type"] == "date"
|
64
|
-
arr[key] =
|
64
|
+
arr[key] = Php4r.date(@args["date_format"], val["value"])
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
@@ -72,9 +72,9 @@ class Knj::Table_writer
|
|
72
72
|
col_count = 0
|
73
73
|
arr.each do |val|
|
74
74
|
if val.is_a?(Hash) and val["type"] == "decimal"
|
75
|
-
@ws[@row, col_count] =
|
75
|
+
@ws[@row, col_count] = Php4r.number_format(val["value"], @args["amount_decimals"], @args["amount_dsep"], @args["amount_tsep"])
|
76
76
|
elsif val.is_a?(Hash) and val["type"] == "date"
|
77
|
-
@ws[@row, col_count] =
|
77
|
+
@ws[@row, col_count] = Php4r.date(@args["date_format"], val["value"])
|
78
78
|
else
|
79
79
|
@ws[@row, col_count] = val
|
80
80
|
end
|
data/lib/knj/tests/test_http2.rb
CHANGED
data/lib/knj/tests/test_mount.rb
CHANGED
data/lib/knj/tests/test_retry.rb
CHANGED
data/lib/knj/unix_proc.rb
CHANGED
@@ -140,6 +140,11 @@ class Knj::Unix_proc
|
|
140
140
|
Process.kill(9, @data["pid"].to_i)
|
141
141
|
end
|
142
142
|
|
143
|
+
#Hash-compatible.
|
144
|
+
def to_h
|
145
|
+
return @data.clone
|
146
|
+
end
|
147
|
+
|
143
148
|
#Tries to kill the process gently, waits a couple of secs to check if the process is actually dead, then sends -9 kill signals.
|
144
149
|
def kill_ensure(args = {})
|
145
150
|
begin
|
data/lib/knj/web.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "php4r"
|
2
|
+
|
1
3
|
class Knj::Web
|
2
4
|
#Parses URI and returns hash with data.
|
3
5
|
def self.parse_uri(str)
|
@@ -128,7 +130,7 @@ class Knj::Web
|
|
128
130
|
secname = try.to_s
|
129
131
|
end
|
130
132
|
|
131
|
-
secname = secname.to_sym if args[:syms] and secname.is_a?(String) and !
|
133
|
+
secname = secname.to_sym if args[:syms] and secname.is_a?(String) and !Php4r.is_numeric(secname)
|
132
134
|
return [secname, secname_empty]
|
133
135
|
end
|
134
136
|
|
@@ -179,12 +181,12 @@ class Knj::Web
|
|
179
181
|
#Header way
|
180
182
|
if !@alert_sent
|
181
183
|
if args[:perm]
|
182
|
-
|
184
|
+
Php4r.header("Status: 301 Moved Permanently")
|
183
185
|
else
|
184
|
-
|
186
|
+
Php4r.header("Status: 303 See Other")
|
185
187
|
end
|
186
188
|
|
187
|
-
|
189
|
+
Php4r.header("Location: #{string}")
|
188
190
|
end
|
189
191
|
|
190
192
|
print "<script type=\"text/javascript\">location.href=\"#{string}\";</script>" if do_js
|
@@ -285,7 +287,7 @@ class Knj::Web
|
|
285
287
|
if cback.is_a?(Method)
|
286
288
|
value = cback.call(value)
|
287
289
|
elsif cback.is_a?(Array)
|
288
|
-
value =
|
290
|
+
value = Php4r.call_user_func(args[:value_func], value)
|
289
291
|
elsif cback.is_a?(Proc)
|
290
292
|
value = cback.call(value)
|
291
293
|
else
|
@@ -378,7 +380,7 @@ class Knj::Web
|
|
378
380
|
|
379
381
|
if args[:type] == :textarea
|
380
382
|
if args.key?(:height)
|
381
|
-
if
|
383
|
+
if Php4r.is_numeric(args[:height])
|
382
384
|
css["height"] = "#{args[:height]}px"
|
383
385
|
else
|
384
386
|
css["height"] = args[:height]
|
@@ -438,7 +440,7 @@ class Knj::Web
|
|
438
440
|
elsif args[:type] == :textshow or args[:type] == :info
|
439
441
|
html << value.to_s
|
440
442
|
elsif args[:type] == :plain
|
441
|
-
html << "#{
|
443
|
+
html << "#{Php4r.nl2br(Knj::Web.html(value))}"
|
442
444
|
elsif args[:type] == :editarea
|
443
445
|
css["width"] = "100%"
|
444
446
|
css["height"] = args[:height] if args.key?(:height)
|
@@ -456,7 +458,7 @@ class Knj::Web
|
|
456
458
|
|
457
459
|
html << "<script type=\"text/javascript\">"
|
458
460
|
html << "function knj_web_init_#{args[:name]}(){"
|
459
|
-
html << "editAreaLoader.init(#{
|
461
|
+
html << "editAreaLoader.init(#{Php4r.json_encode(jshash)});"
|
460
462
|
html << "}"
|
461
463
|
html << "</script>"
|
462
464
|
elsif args[:type] == :numeric
|
@@ -824,7 +826,7 @@ class Knj::Web
|
|
824
826
|
def self.html_args(h)
|
825
827
|
str = ""
|
826
828
|
h.each do |key, val|
|
827
|
-
str << "&#{
|
829
|
+
str << "&#{Php4r.urlencode(key)}=#{Php4r.urlencode(val)}"
|
828
830
|
end
|
829
831
|
|
830
832
|
return str
|
data/spec/cmd_parser_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe "Cmd_parser" do
|
|
4
4
|
it "should be able to parse various strings" do
|
5
5
|
require "knjrbfw"
|
6
6
|
require "knj/cmd_parser"
|
7
|
-
require "
|
7
|
+
require "php4r"
|
8
8
|
|
9
9
|
strs = [
|
10
10
|
"-rw-r--r-- 1 admin administ 186.3M Aug 30 18:09 b4u_synoptik_2011_08_30_17_57_32.sql.gz\n",
|
@@ -18,7 +18,7 @@ describe "Cmd_parser" do
|
|
18
18
|
res = Knj::Cmd_parser.lsl(str)
|
19
19
|
|
20
20
|
res.each do |file|
|
21
|
-
raise "Byte was not numeric in: '#{str}'." if !
|
21
|
+
raise "Byte was not numeric in: '#{str}'." if !Php4r.is_numeric(file[:size])
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/spec/process_spec.rb
CHANGED
data/spec/web_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "Web" do
|
4
4
|
it "should be able to parse url and generate hashes" do
|
5
|
-
require "
|
5
|
+
require "php4r"
|
6
6
|
require "knj/web"
|
7
7
|
|
8
8
|
url = "first=test&#{Knj::Web.urlenc("second[trala][]")}=1&#{Knj::Web.urlenc("second[trala][]")}=2&#{Knj::Web.urlenc("second[trala][]")}=3"
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: knjrbfw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.74
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kasper Johansen
|
@@ -58,8 +58,19 @@ dependencies:
|
|
58
58
|
prerelease: false
|
59
59
|
version_requirements: *id004
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
|
-
name:
|
61
|
+
name: php4r
|
62
62
|
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: rspec
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
63
74
|
none: false
|
64
75
|
requirements:
|
65
76
|
- - ~>
|
@@ -67,10 +78,10 @@ dependencies:
|
|
67
78
|
version: 2.3.0
|
68
79
|
type: :development
|
69
80
|
prerelease: false
|
70
|
-
version_requirements: *
|
81
|
+
version_requirements: *id006
|
71
82
|
- !ruby/object:Gem::Dependency
|
72
83
|
name: bundler
|
73
|
-
requirement: &
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
74
85
|
none: false
|
75
86
|
requirements:
|
76
87
|
- - ">="
|
@@ -78,10 +89,10 @@ dependencies:
|
|
78
89
|
version: 1.0.0
|
79
90
|
type: :development
|
80
91
|
prerelease: false
|
81
|
-
version_requirements: *
|
92
|
+
version_requirements: *id007
|
82
93
|
- !ruby/object:Gem::Dependency
|
83
94
|
name: jeweler
|
84
|
-
requirement: &
|
95
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
85
96
|
none: false
|
86
97
|
requirements:
|
87
98
|
- - ~>
|
@@ -89,10 +100,10 @@ dependencies:
|
|
89
100
|
version: 1.6.3
|
90
101
|
type: :development
|
91
102
|
prerelease: false
|
92
|
-
version_requirements: *
|
103
|
+
version_requirements: *id008
|
93
104
|
- !ruby/object:Gem::Dependency
|
94
105
|
name: sqlite3
|
95
|
-
requirement: &
|
106
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
96
107
|
none: false
|
97
108
|
requirements:
|
98
109
|
- - ">="
|
@@ -100,10 +111,10 @@ dependencies:
|
|
100
111
|
version: "0"
|
101
112
|
type: :development
|
102
113
|
prerelease: false
|
103
|
-
version_requirements: *
|
114
|
+
version_requirements: *id009
|
104
115
|
- !ruby/object:Gem::Dependency
|
105
116
|
name: rmagick
|
106
|
-
requirement: &
|
117
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
107
118
|
none: false
|
108
119
|
requirements:
|
109
120
|
- - ">="
|
@@ -111,7 +122,7 @@ dependencies:
|
|
111
122
|
version: "0"
|
112
123
|
type: :development
|
113
124
|
prerelease: false
|
114
|
-
version_requirements: *
|
125
|
+
version_requirements: *id010
|
115
126
|
description: Including stuff for HTTP, SSH and much more.
|
116
127
|
email: k@spernj.org
|
117
128
|
executables: []
|
@@ -303,7 +314,6 @@ files:
|
|
303
314
|
- lib/knj/objects/objects_sqlhelper.rb
|
304
315
|
- lib/knj/opts.rb
|
305
316
|
- lib/knj/os.rb
|
306
|
-
- lib/knj/php.rb
|
307
317
|
- lib/knj/php_parser/arguments.rb
|
308
318
|
- lib/knj/php_parser/functions.rb
|
309
319
|
- lib/knj/php_parser/php_parser.rb
|
@@ -364,7 +374,6 @@ files:
|
|
364
374
|
- spec/db_spec_encoding_test_file.txt
|
365
375
|
- spec/knjrbfw_spec.rb
|
366
376
|
- spec/objects_spec.rb
|
367
|
-
- spec/php_spec.rb
|
368
377
|
- spec/process_meta_spec.rb
|
369
378
|
- spec/process_spec.rb
|
370
379
|
- spec/spec_helper.rb
|
@@ -385,7 +394,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
385
394
|
requirements:
|
386
395
|
- - ">="
|
387
396
|
- !ruby/object:Gem::Version
|
388
|
-
hash:
|
397
|
+
hash: -1092444068000497155
|
389
398
|
segments:
|
390
399
|
- 0
|
391
400
|
version: "0"
|
data/lib/knj/php.rb
DELETED
@@ -1,978 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module Knj::Php
|
4
|
-
def is_numeric(n) Float n rescue false end
|
5
|
-
|
6
|
-
def call_user_func(*paras)
|
7
|
-
if paras[0].is_a?(String)
|
8
|
-
send_paras = [paras[0].to_sym]
|
9
|
-
send_paras << paras[1] if paras[1]
|
10
|
-
send(*send_paras)
|
11
|
-
elsif paras[0].is_a?(Array)
|
12
|
-
send_paras = [paras[0][1].to_sym]
|
13
|
-
send_paras << paras[1] if paras[1]
|
14
|
-
paras[0][0].send(*send_paras)
|
15
|
-
else
|
16
|
-
raise "Unknown user-func: '#{paras[0].class.name}'."
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def method_exists(obj, method_name)
|
21
|
-
return obj.respond_to?(method_name.to_s)
|
22
|
-
end
|
23
|
-
|
24
|
-
def is_a(obj, classname)
|
25
|
-
classname = classname.to_s
|
26
|
-
classname = "#{classname[0..0].upcase}#{classname[1..999]}"
|
27
|
-
|
28
|
-
if obj.is_a?(classname)
|
29
|
-
return true
|
30
|
-
end
|
31
|
-
|
32
|
-
return false
|
33
|
-
end
|
34
|
-
|
35
|
-
def print_r(argument, ret = false, count = 1)
|
36
|
-
retstr = ""
|
37
|
-
cstr = argument.class.to_s
|
38
|
-
supercl = argument.class.superclass
|
39
|
-
superstr = supercl.to_s if supercl
|
40
|
-
|
41
|
-
valids = ["Apache::Table", "CGI", "Hash", "Knj::Datarow", "Knj::Datarow_custom", "Knj::Db_row", "Knj::Hash_methods", "Knjappserver::Session_accessor", "SQLite3::ResultSet::HashWithTypes"]
|
42
|
-
|
43
|
-
if Knj::Strings.is_a?(argument, valids) or argument.respond_to?(:to_hash)
|
44
|
-
if argument.respond_to?(:to_hash)
|
45
|
-
argument_use = argument.to_hash
|
46
|
-
else
|
47
|
-
argument_use = argument
|
48
|
-
end
|
49
|
-
|
50
|
-
retstr << "#{argument.class.name}{\n"
|
51
|
-
argument_use.each do |pair|
|
52
|
-
i = 0
|
53
|
-
while(i < count)
|
54
|
-
retstr << " "
|
55
|
-
i += 1
|
56
|
-
end
|
57
|
-
|
58
|
-
if pair[0].is_a?(Symbol)
|
59
|
-
keystr = ":#{pair[0].to_s}"
|
60
|
-
else
|
61
|
-
keystr = pair[0].to_s
|
62
|
-
end
|
63
|
-
|
64
|
-
retstr << "[#{keystr}] => "
|
65
|
-
retstr << Knj::Php.print_r(pair[1], true, count + 1).to_s
|
66
|
-
end
|
67
|
-
|
68
|
-
i = 0
|
69
|
-
while(i < count - 1)
|
70
|
-
retstr << " "
|
71
|
-
i += 1
|
72
|
-
end
|
73
|
-
|
74
|
-
retstr << "}\n"
|
75
|
-
elsif cstr == "Dictionary"
|
76
|
-
retstr << "#{argument.class.name}{\n"
|
77
|
-
argument.each do |key, val|
|
78
|
-
i = 0
|
79
|
-
while(i < count)
|
80
|
-
retstr << " "
|
81
|
-
i += 1
|
82
|
-
end
|
83
|
-
|
84
|
-
if key.is_a?(Symbol)
|
85
|
-
keystr = ":#{key.to_s}"
|
86
|
-
else
|
87
|
-
keystr = key.to_s
|
88
|
-
end
|
89
|
-
|
90
|
-
retstr << "[#{keystr}] => "
|
91
|
-
retstr << Knj::Php.print_r(val, true, count + 1).to_s
|
92
|
-
end
|
93
|
-
|
94
|
-
i = 0
|
95
|
-
while(i < count - 1)
|
96
|
-
retstr << " "
|
97
|
-
i += 1
|
98
|
-
end
|
99
|
-
|
100
|
-
retstr << "}\n"
|
101
|
-
elsif argument.is_a?(MatchData) or argument.is_a?(Array) or cstr == "Array" or supercl.is_a?(Array)
|
102
|
-
retstr << "#{argument.class.name}{\n"
|
103
|
-
|
104
|
-
arr_count = 0
|
105
|
-
argument.to_a.each do |i|
|
106
|
-
i_spaces = 0
|
107
|
-
while(i_spaces < count)
|
108
|
-
retstr << " "
|
109
|
-
i_spaces += 1
|
110
|
-
end
|
111
|
-
|
112
|
-
retstr << "[#{arr_count}] => "
|
113
|
-
retstr << Knj::Php.print_r(i, true, count + 1).to_s
|
114
|
-
arr_count += 1
|
115
|
-
end
|
116
|
-
|
117
|
-
i_spaces = 0
|
118
|
-
while(i_spaces < count - 1)
|
119
|
-
retstr << " "
|
120
|
-
i_spaces += 1
|
121
|
-
end
|
122
|
-
|
123
|
-
retstr << "}\n"
|
124
|
-
elsif cstr == "WEBrick::HTTPUtils::FormData"
|
125
|
-
retstr << "{#{argument.class.to_s}}"
|
126
|
-
elsif argument.is_a?(String) or argument.is_a?(Integer) or argument.is_a?(Fixnum) or argument.is_a?(Float)
|
127
|
-
retstr << argument.to_s + "\n"
|
128
|
-
elsif argument.is_a?(Symbol)
|
129
|
-
retstr << ":#{argument.to_s}\n"
|
130
|
-
elsif argument.is_a?(Exception)
|
131
|
-
retstr << "#\{#{argument.class.to_s}: #{argument.message}}\n"
|
132
|
-
elsif cstr == "Knj::Unix_proc"
|
133
|
-
retstr << "#{argument.class.name}::data - "
|
134
|
-
retstr << Knj::Php.print_r(argument.data, true, count).to_s
|
135
|
-
elsif cstr == "Thread"
|
136
|
-
retstr << "#{argument.class.name} - "
|
137
|
-
|
138
|
-
hash = {}
|
139
|
-
argument.keys.each do |key|
|
140
|
-
hash[key] = argument[key]
|
141
|
-
end
|
142
|
-
|
143
|
-
retstr << Knj::Php.print_r(hash, true, count).to_s
|
144
|
-
elsif cstr == "Class"
|
145
|
-
retstr << "#{argument.class.to_s} - "
|
146
|
-
hash = {"name" => argument.name}
|
147
|
-
retstr << Knj::Php.print_r(hash, true, count).to_s
|
148
|
-
elsif cstr == "URI::Generic"
|
149
|
-
retstr << "#{argument.class.to_s}{\n"
|
150
|
-
methods = [:host, :port, :scheme, :path]
|
151
|
-
count += 1
|
152
|
-
methods.each do |method|
|
153
|
-
i_spaces = 0
|
154
|
-
while(i_spaces < count - 1)
|
155
|
-
retstr << " "
|
156
|
-
i_spaces += 1
|
157
|
-
end
|
158
|
-
|
159
|
-
retstr << "#{method}: #{argument.send(method)}\n"
|
160
|
-
end
|
161
|
-
|
162
|
-
count -= 1
|
163
|
-
|
164
|
-
i = 0
|
165
|
-
while(i < count - 1)
|
166
|
-
retstr << " "
|
167
|
-
i += 1
|
168
|
-
end
|
169
|
-
|
170
|
-
retstr << "}\n"
|
171
|
-
elsif cstr == "Time" or cstr == "Datet"
|
172
|
-
argument = argument.time if cstr == "Datet"
|
173
|
-
retstr << "#{cstr}::#{"%04d" % argument.year}-#{"%02d" % argument.month}-#{"%02d" % argument.day} #{"%02d" % argument.hour}:#{"%02d" % argument.min}:#{"%02d" % argument.sec}\n"
|
174
|
-
else
|
175
|
-
#print argument.to_s, "\n"
|
176
|
-
retstr << "Unknown class: '#{cstr}' with superclass '#{supercl}'.\n"
|
177
|
-
end
|
178
|
-
|
179
|
-
if ret.is_a?(TrueClass)
|
180
|
-
return retstr
|
181
|
-
else
|
182
|
-
print retstr
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
def gtext(string)
|
187
|
-
return GetText._(string)
|
188
|
-
end
|
189
|
-
|
190
|
-
def gettext(string)
|
191
|
-
return GetText._(string)
|
192
|
-
end
|
193
|
-
|
194
|
-
#Returns the number as a formatted string.
|
195
|
-
def number_format(number, precision = 2, seperator = ".", delimiter = ",")
|
196
|
-
number = number.to_f if !number.is_a?(Float)
|
197
|
-
precision = precision.to_i
|
198
|
-
return sprintf("%.#{precision.to_s}f", number).gsub(".", seperator) if number < 1 and number > -1
|
199
|
-
|
200
|
-
number = sprintf("%.#{precision.to_s}f", number).split(".")
|
201
|
-
|
202
|
-
str = ""
|
203
|
-
number[0].reverse.scan(/(.{1,3})/) do |match|
|
204
|
-
if match[0] == "-"
|
205
|
-
#This happens if the number is a negative number and we have reaches the minus-sign.
|
206
|
-
str << match[0]
|
207
|
-
else
|
208
|
-
str << delimiter if str.length > 0
|
209
|
-
str << match[0]
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
str = str.reverse
|
214
|
-
if precision > 0
|
215
|
-
str << "#{seperator}#{number[1]}"
|
216
|
-
end
|
217
|
-
|
218
|
-
return str
|
219
|
-
end
|
220
|
-
|
221
|
-
def ucwords(string)
|
222
|
-
return string.to_s.split(" ").select{|w| w.capitalize! or w }.join(" ")
|
223
|
-
end
|
224
|
-
|
225
|
-
def strtoupper(str)
|
226
|
-
return str.to_s.upcase
|
227
|
-
end
|
228
|
-
|
229
|
-
def strtolower(str)
|
230
|
-
return str.to_s.downcase
|
231
|
-
end
|
232
|
-
|
233
|
-
def htmlspecialchars(string)
|
234
|
-
return Knj::Web.html(string)
|
235
|
-
end
|
236
|
-
|
237
|
-
def http_build_query(obj)
|
238
|
-
return self.http_build_query_rec("", obj)
|
239
|
-
end
|
240
|
-
|
241
|
-
def http_build_query_rec(orig_key, obj, first = true)
|
242
|
-
url = ""
|
243
|
-
first_ele = true
|
244
|
-
|
245
|
-
if obj.is_a?(Array)
|
246
|
-
ele_count = 0
|
247
|
-
|
248
|
-
obj.each do |val|
|
249
|
-
orig_key_str = "#{orig_key}[#{ele_count}]"
|
250
|
-
val = "#<Model::#{val.table}::#{val.id}>" if val.respond_to?("is_knj?")
|
251
|
-
|
252
|
-
if val.is_a?(Hash) or val.is_a?(Array)
|
253
|
-
url << self.http_build_query_rec(orig_key_str, val, false)
|
254
|
-
else
|
255
|
-
url << "&" if !first or !first_ele
|
256
|
-
url << "#{Knj::Web.urlenc(orig_key_str)}=#{Knj::Web.urlenc(val)}"
|
257
|
-
end
|
258
|
-
|
259
|
-
first_ele = false if first_ele
|
260
|
-
ele_count += 1
|
261
|
-
end
|
262
|
-
elsif obj.is_a?(Hash)
|
263
|
-
obj.each do |key, val|
|
264
|
-
if first
|
265
|
-
orig_key_str = key
|
266
|
-
else
|
267
|
-
orig_key_str = "#{orig_key}[#{key}]"
|
268
|
-
end
|
269
|
-
|
270
|
-
val = "#<Model::#{val.table}::#{val.id}>" if val.respond_to?("is_knj?")
|
271
|
-
|
272
|
-
if val.is_a?(Hash) or val.is_a?(Array)
|
273
|
-
url << self.http_build_query_rec(orig_key_str, val, false)
|
274
|
-
else
|
275
|
-
url << "&" if !first or !first_ele
|
276
|
-
url << "#{Knj::Web.urlenc(orig_key_str)}=#{Knj::Web.urlenc(val)}"
|
277
|
-
end
|
278
|
-
|
279
|
-
first_ele = false if first_ele
|
280
|
-
end
|
281
|
-
else
|
282
|
-
raise "Unknown class: '#{obj.class.name}'."
|
283
|
-
end
|
284
|
-
|
285
|
-
return url
|
286
|
-
end
|
287
|
-
|
288
|
-
def isset(var)
|
289
|
-
return false if var == nil or var == false
|
290
|
-
return true
|
291
|
-
end
|
292
|
-
|
293
|
-
def strpos(haystack, needle)
|
294
|
-
return false if !haystack
|
295
|
-
return false if !haystack.to_s.include?(needle)
|
296
|
-
return haystack.index(needle)
|
297
|
-
end
|
298
|
-
|
299
|
-
def substr(string, from, to = nil)
|
300
|
-
#If 'to' is not given it should be the total length of the string.
|
301
|
-
if to == nil
|
302
|
-
to = string.length
|
303
|
-
end
|
304
|
-
|
305
|
-
#The behaviour with a negative 'to' is not the same as in PHP. Hack it!
|
306
|
-
if to < 0
|
307
|
-
to = string.length + to
|
308
|
-
end
|
309
|
-
|
310
|
-
#Cut the string.
|
311
|
-
string = "#{string[from.to_i, to.to_i]}"
|
312
|
-
|
313
|
-
#Sometimes the encoding will no longer be valid. Fix that if that is the case.
|
314
|
-
if !string.valid_encoding? and Knj::Php.class_exists("Iconv")
|
315
|
-
string = Iconv.conv("UTF-8//IGNORE", "UTF-8", "#{string} ")[0..-2]
|
316
|
-
end
|
317
|
-
|
318
|
-
#Return the cut string.
|
319
|
-
return string
|
320
|
-
end
|
321
|
-
|
322
|
-
def md5(string)
|
323
|
-
require "digest"
|
324
|
-
return Digest::MD5.hexdigest(string.to_s)
|
325
|
-
end
|
326
|
-
|
327
|
-
def header(headerstr)
|
328
|
-
match = headerstr.to_s.match(/(.*): (.*)/)
|
329
|
-
if match
|
330
|
-
key = match[1]
|
331
|
-
value = match[2]
|
332
|
-
else
|
333
|
-
#HTTP/1.1 404 Not Found
|
334
|
-
|
335
|
-
match_status = headerstr.to_s.match(/^HTTP\/[0-9\.]+ ([0-9]+) (.+)$/)
|
336
|
-
if match_status
|
337
|
-
key = "Status"
|
338
|
-
value = match_status[1] + " " + match_status[2]
|
339
|
-
else
|
340
|
-
raise "Couldnt parse header."
|
341
|
-
end
|
342
|
-
end
|
343
|
-
|
344
|
-
_kas.header(key, value) #This is for knjAppServer - knj.
|
345
|
-
return true
|
346
|
-
end
|
347
|
-
|
348
|
-
def nl2br(string)
|
349
|
-
return string.to_s.gsub("\n", "<br />\n")
|
350
|
-
end
|
351
|
-
|
352
|
-
def urldecode(string)
|
353
|
-
return Knj::Web.urldec(string)
|
354
|
-
end
|
355
|
-
|
356
|
-
def urlencode(string)
|
357
|
-
return Knj::Web.urlenc(string)
|
358
|
-
end
|
359
|
-
|
360
|
-
def parse_str(str, hash)
|
361
|
-
res = Knj::Web.parse_urlquery(str, {:urldecode => true})
|
362
|
-
res.each do |key, val|
|
363
|
-
hash[key] = val
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
def file_put_contents(filepath, content)
|
368
|
-
File.open(filepath.untaint, "w") do |file|
|
369
|
-
file.write(content)
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
def file_get_contents(filepath)
|
374
|
-
filepath = filepath.to_s
|
375
|
-
|
376
|
-
if http_match = filepath.match(/^http(s|):\/\/([A-z_\d\.]+)(|:(\d+))(\/(.+))$/)
|
377
|
-
port = http_match[4].to_i if http_match[4].to_s.length > 0
|
378
|
-
|
379
|
-
args = {
|
380
|
-
:host => http_match[2]
|
381
|
-
}
|
382
|
-
|
383
|
-
if http_match[1] == "s"
|
384
|
-
args[:ssl] = true
|
385
|
-
args[:validate] = false
|
386
|
-
|
387
|
-
if !port
|
388
|
-
port = 443
|
389
|
-
end
|
390
|
-
end
|
391
|
-
|
392
|
-
args[:port] = port if port
|
393
|
-
|
394
|
-
require "http2"
|
395
|
-
Http2.new(args) do |http|
|
396
|
-
return http.get(http_match[5]).body
|
397
|
-
end
|
398
|
-
end
|
399
|
-
|
400
|
-
return File.read(filepath.untaint)
|
401
|
-
end
|
402
|
-
|
403
|
-
def is_file(filepath)
|
404
|
-
begin
|
405
|
-
if File.file?(filepath)
|
406
|
-
return true
|
407
|
-
end
|
408
|
-
rescue
|
409
|
-
return false
|
410
|
-
end
|
411
|
-
|
412
|
-
return false
|
413
|
-
end
|
414
|
-
|
415
|
-
def is_dir(filepath)
|
416
|
-
begin
|
417
|
-
return true if File.directory?(filepath)
|
418
|
-
rescue
|
419
|
-
return false
|
420
|
-
end
|
421
|
-
|
422
|
-
return false
|
423
|
-
end
|
424
|
-
|
425
|
-
def unlink(filepath)
|
426
|
-
FileUtils.rm(filepath)
|
427
|
-
end
|
428
|
-
|
429
|
-
def file_exists(filepath)
|
430
|
-
return true if File.exists?(filepath.to_s.untaint)
|
431
|
-
return false
|
432
|
-
end
|
433
|
-
|
434
|
-
def strtotime(date_string, cur = nil)
|
435
|
-
if !cur
|
436
|
-
cur = Time.new
|
437
|
-
else
|
438
|
-
cur = Time.at(cur)
|
439
|
-
end
|
440
|
-
|
441
|
-
date_string = date_string.to_s.downcase
|
442
|
-
|
443
|
-
if date_string.match(/[0-9]+-[0-9]+-[0-9]+/i)
|
444
|
-
begin
|
445
|
-
return Time.local(*ParseDate.parsedate(date_string)).to_i
|
446
|
-
rescue
|
447
|
-
return 0
|
448
|
-
end
|
449
|
-
end
|
450
|
-
|
451
|
-
date_string.scan(/((\+|-)([0-9]+) (\S+))/) do |match|
|
452
|
-
timestr = match[3]
|
453
|
-
number = match[2].to_i
|
454
|
-
mathval = match[1]
|
455
|
-
add = nil
|
456
|
-
|
457
|
-
if timestr == "years" or timestr == "year"
|
458
|
-
add = ((number.to_i * 3600) * 24) * 365
|
459
|
-
elsif timestr == "months" or timestr == "month"
|
460
|
-
add = ((number.to_i * 3600) * 24) * 30
|
461
|
-
elsif timestr == "weeks" or timestr == "week"
|
462
|
-
add = (number.to_i * 3600) * 24 * 7
|
463
|
-
elsif timestr == "days" or timestr == "day"
|
464
|
-
add = (number.to_i * 3600) * 24
|
465
|
-
elsif timestr == "hours" or timestr == "hour"
|
466
|
-
add = number.to_i * 3600
|
467
|
-
elsif timestr == "minutes" or timestr == "minute" or timestr == "min" or timestr == "mints"
|
468
|
-
add = number.to_i * 60
|
469
|
-
elsif timestr == "seconds" or timestr == "second" or timestr == "sec" or timestr == "secs"
|
470
|
-
add = number.to_i
|
471
|
-
end
|
472
|
-
|
473
|
-
if mathval == "+"
|
474
|
-
cur += add
|
475
|
-
elsif mathval == "-"
|
476
|
-
cur -= add
|
477
|
-
end
|
478
|
-
end
|
479
|
-
|
480
|
-
return cur.to_i
|
481
|
-
end
|
482
|
-
|
483
|
-
def class_exists(classname)
|
484
|
-
begin
|
485
|
-
Kernel.const_get(classname)
|
486
|
-
return true
|
487
|
-
rescue
|
488
|
-
return false
|
489
|
-
end
|
490
|
-
end
|
491
|
-
|
492
|
-
def html_entity_decode(string)
|
493
|
-
string = Knj::Web.html(string)
|
494
|
-
string = string.gsub("ø", "ø").gsub("æ", "æ").gsub("å", "å").gsub("€", "€").gsub("#39;", "'").gsub("&", "&").gsub(">", ">").gsub("<", "<").gsub(""", '"').gsub("'", "'")
|
495
|
-
return string
|
496
|
-
end
|
497
|
-
|
498
|
-
def strip_tags(htmlstr)
|
499
|
-
htmlstr.scan(/(<([\/A-z]+).*?>)/) do |match|
|
500
|
-
htmlstr = htmlstr.gsub(match[0], "")
|
501
|
-
end
|
502
|
-
|
503
|
-
return htmlstr.gsub(" ", " ")
|
504
|
-
end
|
505
|
-
|
506
|
-
def die(msg)
|
507
|
-
print msg
|
508
|
-
exit
|
509
|
-
end
|
510
|
-
|
511
|
-
def opendir(dirpath)
|
512
|
-
res = {:files => [], :index => 0}
|
513
|
-
Dir.foreach(dirpath) do |file|
|
514
|
-
res[:files] << file
|
515
|
-
end
|
516
|
-
|
517
|
-
return res
|
518
|
-
end
|
519
|
-
|
520
|
-
def readdir(res)
|
521
|
-
ret = res[:files][res[:index]] if res[:files].index(res[:index]) != nil
|
522
|
-
return false if !ret
|
523
|
-
res[:index] += 1
|
524
|
-
return ret
|
525
|
-
end
|
526
|
-
|
527
|
-
def fopen(filename, mode)
|
528
|
-
begin
|
529
|
-
return File.open(filename, mode)
|
530
|
-
rescue
|
531
|
-
return false
|
532
|
-
end
|
533
|
-
end
|
534
|
-
|
535
|
-
def fwrite(fp, str)
|
536
|
-
begin
|
537
|
-
fp.print str
|
538
|
-
rescue
|
539
|
-
return false
|
540
|
-
end
|
541
|
-
|
542
|
-
return true
|
543
|
-
end
|
544
|
-
|
545
|
-
def fputs(fp, str)
|
546
|
-
begin
|
547
|
-
fp.print str
|
548
|
-
rescue
|
549
|
-
return false
|
550
|
-
end
|
551
|
-
|
552
|
-
return true
|
553
|
-
end
|
554
|
-
|
555
|
-
def fread(fp, length = 4096)
|
556
|
-
return fp.read(length)
|
557
|
-
end
|
558
|
-
|
559
|
-
def fgets(fp, length = 4096)
|
560
|
-
return fp.read(length)
|
561
|
-
end
|
562
|
-
|
563
|
-
def fclose(fp)
|
564
|
-
fp.close
|
565
|
-
end
|
566
|
-
|
567
|
-
def move_uploaded_file(tmp_path, new_path)
|
568
|
-
FileUtils.mv(tmp_path.untaint, new_path.untaint)
|
569
|
-
end
|
570
|
-
|
571
|
-
def utf8_encode(str)
|
572
|
-
str = str.to_s if str.respond_to?("to_s")
|
573
|
-
|
574
|
-
if str.respond_to?("encode")
|
575
|
-
begin
|
576
|
-
return str.encode("iso-8859-1", "utf-8")
|
577
|
-
rescue Encoding::InvalidByteSequenceError
|
578
|
-
#ignore - try iconv
|
579
|
-
end
|
580
|
-
end
|
581
|
-
|
582
|
-
require "iconv"
|
583
|
-
|
584
|
-
begin
|
585
|
-
return Iconv.conv("iso-8859-1", "utf-8", str.to_s)
|
586
|
-
rescue
|
587
|
-
return Iconv.conv("iso-8859-1//ignore", "utf-8", "#{str} ").slice(0..-2)
|
588
|
-
end
|
589
|
-
end
|
590
|
-
|
591
|
-
def utf8_decode(str)
|
592
|
-
str = str.to_s if str.respond_to?(:to_s)
|
593
|
-
require "iconv" if RUBY_PLATFORM == "java" #This fixes a bug in JRuby where Iconv otherwise would not be detected.
|
594
|
-
|
595
|
-
if str.respond_to?(:encode)
|
596
|
-
begin
|
597
|
-
return str.encode("utf-8", "iso-8859-1")
|
598
|
-
rescue Encoding::InvalidByteSequenceError
|
599
|
-
#ignore - try iconv
|
600
|
-
end
|
601
|
-
end
|
602
|
-
|
603
|
-
require "iconv"
|
604
|
-
|
605
|
-
begin
|
606
|
-
return Iconv.conv("utf-8", "iso-8859-1", str.to_s)
|
607
|
-
rescue
|
608
|
-
return Iconv.conv("utf-8//ignore", "iso-8859-1", str.to_s)
|
609
|
-
end
|
610
|
-
end
|
611
|
-
|
612
|
-
def setcookie(cname, cvalue, expire = nil, domain = nil)
|
613
|
-
args = {
|
614
|
-
"name" => cname,
|
615
|
-
"value" => cvalue,
|
616
|
-
"path" => "/"
|
617
|
-
}
|
618
|
-
args["expires"] = Time.at(expire) if expire
|
619
|
-
args["domain"] = domain if domain
|
620
|
-
|
621
|
-
_kas.cookie(args)
|
622
|
-
return status
|
623
|
-
end
|
624
|
-
|
625
|
-
#This method is only here for convertion support - it doesnt do anything.
|
626
|
-
def session_start
|
627
|
-
|
628
|
-
end
|
629
|
-
|
630
|
-
def explode(expl, strexp)
|
631
|
-
return strexp.to_s.split(expl)
|
632
|
-
end
|
633
|
-
|
634
|
-
def dirname(filename)
|
635
|
-
File.dirname(filename)
|
636
|
-
end
|
637
|
-
|
638
|
-
def chdir(dirname)
|
639
|
-
Dir.chdir(dirname)
|
640
|
-
end
|
641
|
-
|
642
|
-
def include_once(filename)
|
643
|
-
require filename
|
644
|
-
end
|
645
|
-
|
646
|
-
def require_once(filename)
|
647
|
-
require filename
|
648
|
-
end
|
649
|
-
|
650
|
-
def echo(string)
|
651
|
-
print string
|
652
|
-
end
|
653
|
-
|
654
|
-
def msgbox(title, msg, type)
|
655
|
-
Knj::Gtk2.msgbox(msg, type, title)
|
656
|
-
end
|
657
|
-
|
658
|
-
def count(array)
|
659
|
-
return array.length
|
660
|
-
end
|
661
|
-
|
662
|
-
def json_encode(obj)
|
663
|
-
if Knj::Php.class_exists("Rho")
|
664
|
-
return Rho::JSON.generate(obj)
|
665
|
-
elsif Knj::Php.class_exists("JSON")
|
666
|
-
return JSON.generate(obj)
|
667
|
-
else
|
668
|
-
raise "Could not figure out which JSON lib to use."
|
669
|
-
end
|
670
|
-
end
|
671
|
-
|
672
|
-
def json_decode(data, as_array = false)
|
673
|
-
#FIXME: Should be able to return as object, which will break all projects using it without second argument...
|
674
|
-
|
675
|
-
raise "String was not given to 'Knj::Php.json_decode'." if !data.is_a?(String)
|
676
|
-
|
677
|
-
if Knj::Php.class_exists("Rho")
|
678
|
-
return Rho::JSON.parse(data)
|
679
|
-
elsif Knj::Php.class_exists("JSON")
|
680
|
-
return JSON.parse(data)
|
681
|
-
else
|
682
|
-
raise "Could not figure out which JSON lib to use."
|
683
|
-
end
|
684
|
-
end
|
685
|
-
|
686
|
-
def time
|
687
|
-
return Time.now.to_i
|
688
|
-
end
|
689
|
-
|
690
|
-
def microtime(get_as_float = false)
|
691
|
-
microtime = Time.now.to_f
|
692
|
-
|
693
|
-
return microtime if get_as_float
|
694
|
-
|
695
|
-
splitted = microtime.to_s.split(",")
|
696
|
-
return "#{splitted[0]} #{splitted[1]}"
|
697
|
-
end
|
698
|
-
|
699
|
-
def mktime(hour = nil, min = nil, sec = nil, date = nil, month = nil, year = nil, is_dst = -1)
|
700
|
-
cur_time = Time.new
|
701
|
-
|
702
|
-
hour = cur_time.hour if hour == nil
|
703
|
-
min = cur_time.min if min == nil
|
704
|
-
sec = cur_time.sec if sec == nil
|
705
|
-
date = cur_time.day if date == nil
|
706
|
-
month = cur_time.month if month == nil
|
707
|
-
year = cur_time.year if year == nil
|
708
|
-
|
709
|
-
new_time = Datet.in("#{year.to_s}-#{month.to_s}-#{date.to_s} #{hour.to_s}:#{min.to_s}:#{sec.to_s}")
|
710
|
-
return new_time.to_i
|
711
|
-
end
|
712
|
-
|
713
|
-
def date(date_format, date_input = nil)
|
714
|
-
if date_input == nil
|
715
|
-
date_object = Time.now
|
716
|
-
elsif Knj::Php.is_numeric(date_input)
|
717
|
-
date_object = Time.at(date_input.to_i)
|
718
|
-
elsif date_input.is_a?(Datet)
|
719
|
-
date_object = date_input.time
|
720
|
-
elsif date_input.is_a?(Time)
|
721
|
-
date_object = date_input
|
722
|
-
else
|
723
|
-
raise "Unknown date given: '#{date_input}', '#{date_input.class.name}'."
|
724
|
-
end
|
725
|
-
|
726
|
-
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")
|
727
|
-
return date_object.strftime(date_format)
|
728
|
-
end
|
729
|
-
|
730
|
-
def basename(filepath)
|
731
|
-
splitted = filepath.to_s.split("/").last
|
732
|
-
return false if !splitted
|
733
|
-
|
734
|
-
ret = splitted.split(".")
|
735
|
-
ret.delete(ret.last)
|
736
|
-
return ret.join(".")
|
737
|
-
end
|
738
|
-
|
739
|
-
def base64_encode(str)
|
740
|
-
#The strict-encode wont do corrupt newlines...
|
741
|
-
if Base64.respond_to?("strict_encode64")
|
742
|
-
return Base64.strict_encode64(str.to_s)
|
743
|
-
else
|
744
|
-
return Base64.encode64(str.to_s)
|
745
|
-
end
|
746
|
-
end
|
747
|
-
|
748
|
-
def base64_decode(str)
|
749
|
-
return Base64.decode64(str.to_s)
|
750
|
-
end
|
751
|
-
|
752
|
-
def pathinfo(filepath)
|
753
|
-
filepath = filepath.to_s
|
754
|
-
|
755
|
-
dirname = File.dirname(filepath)
|
756
|
-
dirname = "" if dirname == "."
|
757
|
-
|
758
|
-
return {
|
759
|
-
"dirname" => dirname,
|
760
|
-
"basename" => self.basename(filepath),
|
761
|
-
"extension" => filepath.split(".").last,
|
762
|
-
"filename" => filepath.split("/").last
|
763
|
-
}
|
764
|
-
end
|
765
|
-
|
766
|
-
def realpath(pname)
|
767
|
-
require "pathname"
|
768
|
-
|
769
|
-
begin
|
770
|
-
return Pathname.new(pname.to_s).realpath.to_s
|
771
|
-
rescue => e
|
772
|
-
return false
|
773
|
-
end
|
774
|
-
end
|
775
|
-
|
776
|
-
# Returns the scripts current memory usage.
|
777
|
-
def memory_get_usage
|
778
|
-
# FIXME: This only works on Linux at the moment, since we are doing this by command line - knj.
|
779
|
-
memory_usage = `ps -o rss= -p #{Process.pid}`.to_i * 1024
|
780
|
-
return memory_usage
|
781
|
-
end
|
782
|
-
|
783
|
-
# Should return the peak usage of the running script, but I have found no way to detect this... Instead returns the currently memory usage.
|
784
|
-
def memory_get_peak_usage
|
785
|
-
return self.memory_get_usage
|
786
|
-
end
|
787
|
-
|
788
|
-
def ip2long(ip)
|
789
|
-
return IPAddr.new(ip).to_i
|
790
|
-
end
|
791
|
-
|
792
|
-
# Execute an external program and display raw output.
|
793
|
-
def passthru(cmd)
|
794
|
-
if RUBY_ENGINE == "jruby"
|
795
|
-
IO.popen4(cmd) do |pid, stdin, stdout, stderr|
|
796
|
-
tout = Thread.new do
|
797
|
-
begin
|
798
|
-
stdout.sync = true
|
799
|
-
stdout.each do |str|
|
800
|
-
$stdout.print str
|
801
|
-
end
|
802
|
-
rescue => e
|
803
|
-
$stdout.print Knj::Errors.error_str(e)
|
804
|
-
end
|
805
|
-
end
|
806
|
-
|
807
|
-
terr = Thread.new do
|
808
|
-
begin
|
809
|
-
stderr.sync = true
|
810
|
-
stderr.each do |str|
|
811
|
-
$stderr.print str
|
812
|
-
end
|
813
|
-
rescue => e
|
814
|
-
$stderr.print Knj::Errors.error_str(e)
|
815
|
-
end
|
816
|
-
end
|
817
|
-
|
818
|
-
tout.join
|
819
|
-
terr.join
|
820
|
-
end
|
821
|
-
else
|
822
|
-
require "open3"
|
823
|
-
Open3.popen3(cmd) do |stdin, stdout, stderr|
|
824
|
-
tout = Thread.new do
|
825
|
-
begin
|
826
|
-
stdout.sync = true
|
827
|
-
stdout.each do |str|
|
828
|
-
$stdout.print str
|
829
|
-
end
|
830
|
-
rescue => e
|
831
|
-
$stdout.print Knj::Errors.error_str(e)
|
832
|
-
end
|
833
|
-
end
|
834
|
-
|
835
|
-
terr = Thread.new do
|
836
|
-
begin
|
837
|
-
stderr.sync = true
|
838
|
-
stderr.each do |str|
|
839
|
-
$stderr.print str
|
840
|
-
end
|
841
|
-
rescue => e
|
842
|
-
$stderr.print Knj::Errors.error_str(e)
|
843
|
-
end
|
844
|
-
end
|
845
|
-
|
846
|
-
tout.join
|
847
|
-
terr.join
|
848
|
-
end
|
849
|
-
end
|
850
|
-
|
851
|
-
return nil
|
852
|
-
end
|
853
|
-
|
854
|
-
# Thanks to this link for the following functions: http://snippets.dzone.com/posts/show/4509
|
855
|
-
def long2ip(long)
|
856
|
-
ip = []
|
857
|
-
4.times do |i|
|
858
|
-
ip.push(long.to_i & 255)
|
859
|
-
long = long.to_i >> 8
|
860
|
-
end
|
861
|
-
|
862
|
-
ip.reverse.join(".")
|
863
|
-
end
|
864
|
-
|
865
|
-
def gzcompress(str, level = 3)
|
866
|
-
require "zlib"
|
867
|
-
|
868
|
-
zstream = Zlib::Deflate.new
|
869
|
-
gzip_str = zstream.deflate(str.to_s, Zlib::FINISH)
|
870
|
-
zstream.close
|
871
|
-
|
872
|
-
return gzip_str
|
873
|
-
end
|
874
|
-
|
875
|
-
def gzuncompress(str, length = 0)
|
876
|
-
require "zlib"
|
877
|
-
|
878
|
-
zstream = Zlib::Inflate.new
|
879
|
-
plain_str = zstream.inflate(str.to_s)
|
880
|
-
zstream.finish
|
881
|
-
zstream.close
|
882
|
-
|
883
|
-
return plain_str.to_s
|
884
|
-
end
|
885
|
-
|
886
|
-
#Sort methods.
|
887
|
-
def ksort(hash)
|
888
|
-
nhash = hash.sort do |a, b|
|
889
|
-
a[0] <=> b[0]
|
890
|
-
end
|
891
|
-
|
892
|
-
newhash = {}
|
893
|
-
nhash.each do |val|
|
894
|
-
newhash[val[0]] = val[1][0]
|
895
|
-
end
|
896
|
-
|
897
|
-
return newhash
|
898
|
-
end
|
899
|
-
|
900
|
-
#Foreach emulator.
|
901
|
-
def foreach(element, &block)
|
902
|
-
raise "No or unsupported block given." if !block.respond_to?(:call) or !block.respond_to?(:arity)
|
903
|
-
arity = block.arity
|
904
|
-
cname = element.class.name.to_s
|
905
|
-
|
906
|
-
if element.is_a?(Array) or cname == "Array_enumerator"
|
907
|
-
element.each_index do |key|
|
908
|
-
if arity == 2
|
909
|
-
block.call(key, element[key])
|
910
|
-
elsif arity == 1
|
911
|
-
block.call(element[key])
|
912
|
-
else
|
913
|
-
raise "Unknown arity: '#{arity}'."
|
914
|
-
end
|
915
|
-
end
|
916
|
-
elsif element.is_a?(Hash)
|
917
|
-
element.each do |key, val|
|
918
|
-
if arity == 2
|
919
|
-
block.call(key, val)
|
920
|
-
elsif arity == 1
|
921
|
-
block.call(val)
|
922
|
-
else
|
923
|
-
raise "Unknown arity: '#{arity}'."
|
924
|
-
end
|
925
|
-
end
|
926
|
-
else
|
927
|
-
raise "Unknown element: '#{element.class.name}'."
|
928
|
-
end
|
929
|
-
end
|
930
|
-
|
931
|
-
#Array-function emulator.
|
932
|
-
def array(*ele)
|
933
|
-
return {} if ele.length <= 0
|
934
|
-
|
935
|
-
if ele.length == 1 and ele.first.is_a?(Hash)
|
936
|
-
return ele.first
|
937
|
-
end
|
938
|
-
|
939
|
-
return ele
|
940
|
-
end
|
941
|
-
|
942
|
-
def array_key_exists(key, arr)
|
943
|
-
if arr.is_a?(Hash)
|
944
|
-
return arr.key?(key)
|
945
|
-
elsif arr.is_a?(Array)
|
946
|
-
return true if arr.index(key) != nil
|
947
|
-
return false
|
948
|
-
else
|
949
|
-
raise "Unknown type of argument: '#{arr.class.name}'."
|
950
|
-
end
|
951
|
-
end
|
952
|
-
|
953
|
-
def empty(obj)
|
954
|
-
if obj.respond_to?("empty?")
|
955
|
-
return obj.empty?
|
956
|
-
elsif obj == nil
|
957
|
-
return true
|
958
|
-
else
|
959
|
-
raise "Dont know how to handle object on 'empty': '#{obj.class.name}'."
|
960
|
-
end
|
961
|
-
end
|
962
|
-
|
963
|
-
def trim(argument)
|
964
|
-
return argument.to_s.strip
|
965
|
-
end
|
966
|
-
|
967
|
-
def serialize(argument)
|
968
|
-
require "php_serialize" #gem: php-serialize
|
969
|
-
return PHP.serialize(argument)
|
970
|
-
end
|
971
|
-
|
972
|
-
def unserialize(argument)
|
973
|
-
require "php_serialize" #gem: php-serialize
|
974
|
-
return PHP.unserialize(argument.to_s)
|
975
|
-
end
|
976
|
-
|
977
|
-
module_function(*instance_methods)
|
978
|
-
end
|