Class: WiserSms::Ph

Inherits:
Object
  • Object
show all
Defined in:
lib/wiser_sms/ph.rb

Constant Summary collapse

ADDRESS =
"121.97.123.218:13013"
VTR =
"9a3a70173b00fd265aa0a9b79163a5bb1f0e157b"
PASSWORD =
"jsWsINhtCBVqka/CeHSJLg==\n"
GLOBE_USER =
"0gBTLoL+SeTKEcOS74FCHw==\n"
GLOBE_MIN =
"r3q08f7qs421w6DNcF07cw==\n"
SMART_USER =
"jeEfqqlyvd6B/t5xKd/9OA==\n"
SMART_MIN =
"oxJ9pMRwdm4M6hcQ/t515Q==\n"
SUN_USER =
"cFrmJHuQRHw8MhFKxqZpuQ==\n"
SUN_MIN =
"G+XBde142eMMppO2IPBYfw==\n"

Class Method Summary collapse

Class Method Details

.encrypt(key, str) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/wiser_sms/ph.rb', line 99

def self.encrypt(key, str)
  aes = OpenSSL::Cipher::Cipher.new('AES-256-CBC')
  aes.encrypt
  aes.key = key
  aes.iv = VTR
  cipher = aes.update(str)
  cipher = aes.final
  enc = [cipher].pack('m')
  return enc
end

.escape(msg) ⇒ Object



32
33
34
# File 'lib/wiser_sms/ph.rb', line 32

def self.escape(msg)
  return CGI::escape(msg)
end

.get_network?(min) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wiser_sms/ph.rb', line 18

def self.get_network?(min)
  return nil if min.size != 11
  prefix = min.to_s[0..3]
  if ['0813', '0907', '0908', '0909', '0910', '0912', '0918', '0919', '0920', '0921', '0928', '0929', '0930', '0938', '0939', '0946', '0947', '0948', '0949', '0989', '0998', '0999'].include?(prefix)
  	return "smart"
  elsif ['0817', '0905', '0906', '0915', '0916', '0917', '0926', '0927', '0935', '0936', '0937', '0994', '0996', '0997'].include?(prefix)
    return "globe"
  elsif ['0922', '0923', '0925', '0932', '0933', '0934', '0942', '0943'].include?(prefix)
    return "sun"
  else
    return "invalid"
  end
end

.request(key, username, password, source, destination, message) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/wiser_sms/ph.rb', line 82

def self.request(key, username, password, source, destination, message)
  begin
    username  = self.validate(key, username)
    password  = self.validate(key, password)
    source    = self.validate(key, source)

    url       = "http://#{ADDRESS}/cgi-bin/sendsms"
    authorize = "?username=#{username}&password=#{password}&from=#{source}"
    payload   = "&to=#{destination}&text=#{message}"
    request   = "#{url}#{authorize}#{payload}"
    response  = open(request).read
    return response.split(":")[0] == "0"
  rescue
    return 0
  end
end

.send(key, mins, msg) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wiser_sms/ph.rb', line 36

def self.send(key, mins, msg)
  return 0 unless key.to_s.strip.present?
  mins = [mins] unless mins.is_a?(Array)
  escaped_msg = self.escape(msg)
  response = {}
  mins.each do |min|
    case self.get_network?(min)
    when "globe"
      resp = self.send_globe(key, min, escaped_msg)
    when "smart"
      resp = self.send_smart(key, min, escaped_msg)
    when "sun"
      resp = self.send_sun(key, min, escaped_msg)
    else
      resp = nil
    end
    return resp
    response[min] = resp
  end
  return response
end

.send_globe(key, min, msg) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/wiser_sms/ph.rb', line 58

def self.send_globe(key, min, msg)
  return nil if min.size != 11
  user = GLOBE_USER
  pass = PASSWORD
  src  = GLOBE_MIN
  return self.request(key, user, pass, src, min, msg)
end

.send_smart(key, min, msg) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/wiser_sms/ph.rb', line 66

def self.send_smart(key, min, msg)
  return nil if min.size != 11
  user = SMART_USER
  pass = PASSWORD
  src  = SMART_MIN
  return self.request(key, user, pass, src, min, msg)
end

.send_sun(key, min, msg) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/wiser_sms/ph.rb', line 74

def self.send_sun(key, min, msg)
  return nil if min.size != 11
  user = SUN_USER
  pass = PASSWORD
  src  = SUN_MIN
  return self.request(key, user, pass, src, min, msg)
end

.validate(key, str) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/wiser_sms/ph.rb', line 110

def self.validate(key, str)
  aes = OpenSSL::Cipher::Cipher.new('AES-256-CBC')
  aes.decrypt
  aes.key = key
  aes.iv = VTR
  cipher = aes.update(str.unpack('m')[0])
  cipher << aes.final
  return cipher
end