Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.53 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3.  
  4. require_once ("../config/DBConfig.php");
  5. require_once ("../config/RespConfig.php");
  6. require_once ("../vendor/mysqli/MysqliDb.php");
  7. //require_once ("PredisService.php");
  8.  
  9.  
  10. class LoginService{
  11. var $FILENAME = "LoginService";
  12. var $db, $redis, $timezone;
  13.  
  14. public function __construct(){
  15. //$this->redis = new PredisService();
  16. $this->connectDB();
  17. }
  18.  
  19. public function validatePhoneNumber($params, $gcm_server_id = "1"){ //1 = google firebase; 2 = Pushy.me
  20.  
  21. $data = new stdClass();
  22.  
  23. /*mandatory fields*/
  24. $data->msgType = "verificationResp";
  25. $data->msgStatusCode = RespConfig::SUCCESS;
  26. $data->msgDescription = null;
  27.  
  28. /*additional fields*/
  29. $data->phone = $params["data"]["msgData"]["phone"];
  30. $data->verificationCode = $this->generateToken($params["data"]["msgData"]["phone"]);
  31. $data->contactDetails = $this->getContactDetailsByPhone($params["data"]["msgData"]["phone"]);
  32.  
  33. $notification = new stdClass();
  34. $notification->title = "WPO Connect";
  35. $notification->body = "Verification Code";
  36. $notification->icon = "ic_launcher";
  37.  
  38. $message = new stdClass();
  39. $message->notification = $notification;
  40. $message->data = $data;
  41.  
  42. $result = new stdClass();
  43. $result->message = $message;
  44.  
  45. if($data->contactDetails != null){
  46. $factoryCode = $data->contactDetails->factory; //harus diganti factory ID nya jangan 80
  47. $factory = $this->validateFactoryCode($factoryCode);
  48. $this->timezone = $factory["timezone"];
  49. $this->saveRegistrationToken($params["from"], $params["data"]["msgData"]["phone"], $factoryCode, $gcm_server_id);
  50. //$this->sendGreetings($params["data"]["msgData"]["phone"]);
  51. }
  52.  
  53. return $result;
  54.  
  55. }
  56.  
  57. public function registerPhoneNumber($params, $gcm_server_id = "1"){
  58.  
  59. // $this->print_log($this->FILENAME, $params);
  60. // $params = json_decode($params, true);
  61. //$this->print_log($this->FILENAME, json_encode($params));
  62. //return false;
  63.  
  64.  
  65. $contactDetails = null;
  66. $contactDetailsComplete = null;
  67. $verifiedStatus = "0";
  68. $factoryCode = $this->validateFactoryCode($params["data"]["msgData"]["factoryID"]);
  69. $this->timezone = $factoryCode["timezone"];
  70.  
  71. $msgStatusCode = $factoryCode["statusCode"];
  72. $msgDescription = $factoryCode["message"];
  73. $factoryId = $factoryCode["factoryID"];
  74. $factoryShortname = $factoryCode["shortname"];
  75. $statusCode = false;
  76.  
  77. if($factoryCode["statusCode"] == RespConfig::SUCCESS){
  78. $verifiedStatus = "1";
  79. $factObj = new stdClass();
  80. $factObj->factory = $factoryId;
  81. $contactDetails = $factObj;
  82. }
  83.  
  84. if($params["data"]["msgData"]["oldPhoneNumber"] != ""){
  85. $phoneNumber = $this->validateOldPhoneNumber($params["data"]["msgData"]["oldPhoneNumber"]);
  86. if($factoryCode["statusCode"] == RespConfig::SUCCESS){
  87. $statusCode = $phoneNumber["statusCode"];
  88. $msgDescription = $phoneNumber["message"];
  89. $verifiedStatus = "1";
  90.  
  91. //$this->saveRegistrationToken($params["from"], $params["data"]["msgData"]["phone"], $factoryId);
  92.  
  93. /* save new phone number*/
  94. $this->saveNewPhoneNumber($params["data"]["msgData"]["oldPhoneNumber"], $params["data"]["msgData"]["phone"], $factoryId);
  95.  
  96. }else{
  97. $statusCode = RespConfig::OLD_NUMBER_NOT_FOUND;
  98. $msgDescription = $factoryCode["message"];
  99. $verifiedStatus = "0";
  100. }
  101.  
  102. $msgStatusCode = $statusCode;
  103. $contactDetails = $phoneNumber["contactDetails"];
  104. $contactDetailsComplete = $phoneNumber["contactDetails"];
  105. }
  106.  
  107. $data = new stdClass();
  108. /*mandatory fields*/
  109. $data->msgType = "registrationResp";
  110. $data->msgStatusCode = $msgStatusCode;
  111. $data->msgDescription = $msgDescription;
  112.  
  113. /*additional fields*/
  114. $data->contactDetails = $contactDetails;
  115. $data->verifiedStatus = $verifiedStatus;
  116.  
  117. $notification = new stdClass();
  118. $notification->title = "WPO Connect";
  119. $notification->body = "Register Phone";
  120. $notification->icon = "ic_launcher";
  121.  
  122. $message = new stdClass();
  123. $message->notification = $notification;
  124. $message->data = $data;
  125.  
  126. $result = new stdClass();
  127. $result->message = $message;
  128.  
  129. /*if statusCode === SUCCESS, then save to Redis*/
  130. if($msgStatusCode == RespConfig::SUCCESS){
  131. $this->saveRegistrationToken($params["from"], $params["data"]["msgData"]["phone"], $params["data"]["msgData"]["factoryID"], $gcm_server_id);
  132. //$this->sendGreetings($params["data"]["msgData"]["phone"]);
  133. //$this->sendNewsletter($params["data"]["msgData"]["phone"]);
  134.  
  135. $registerContact = new RegisterContactService();
  136. $registerContact->processNewContactStep1($params);
  137. }
  138.  
  139. if($contactDetailsComplete == null && $msgStatusCode == RespConfig::SUCCESS){
  140. //$this->sendAskFillProfileForm($params["data"]["msgData"]["phone"]);
  141. }
  142.  
  143. return $result;
  144. }
  145.  
  146. public function updateRegistrationToken($params, $gcm_server_id){
  147. $phone_number = $params["data"]["msgData"]["phone_number"];
  148. $registration_token = $params["data"]["msgData"]["registration_token"];
  149. $factory_code = $params["data"]["msgData"]["factoryCode"];
  150. $data = array(
  151. 'registration_token' => $registration_token,
  152. 'gcm_server_id' => $gcm_server_id,
  153. 'factory_code' => $factory_code
  154. );
  155.  
  156. $this->db->where('phone_number', $phone_number);
  157. $this->db->update(DBConfig::TABLE_ANDROID_TOKEN, $data);
  158. return true;
  159. }
  160.  
  161. public function debug(){
  162. print_r($this->saveRegistrationToken("token", "087781796692", "54"));
  163. }
  164.  
  165.  
  166. private function saveRegistrationToken($registrationToken, $phoneNumber, $factoryID, $gcm_server_id = "1"){
  167. //$this->redis->setKey(REDIS_PREFIX_TOKEN_TO_PHONE.$registrationToken, $phoneNumber);
  168. //$this->redis->setKey(REDIS_PREFIX_PHONE_TO_TOKEN.$phoneNumber, $registrationToken);
  169. $data = array(
  170. 'registration_token' => $registrationToken,
  171. 'phone_number' => $phoneNumber,
  172. 'factoryID' => $factoryID,
  173. 'gcm_server_id' => $gcm_server_id
  174. );
  175.  
  176. //$id = $this->db->insert (DBConfig::TABLE_ANDROID_TOKEN, $data);
  177. $id = $this->db->rawQuery('INSERT INTO '.DBConfig::TABLE_ANDROID_TOKEN.'(registration_token, phone_number, factory_code, gcm_server_id) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE registration_token = ? , phone_number = ? ,factory_code = ?, gcm_server_id = ? ', Array($registrationToken, $phoneNumber, $factoryID, $gcm_server_id, $registrationToken, $phoneNumber, $factoryID, $gcm_server_id));
  178.  
  179. if ($id){
  180. return true;
  181. }else{
  182. error_log("Last executed query was ". $this->db->getLastQuery());
  183. error_log('insert failed: ' . $this->db->getLastError());
  184. return false;
  185. }
  186. }
  187.  
  188.  
  189. private function saveNewPhoneNumber($oldPhoneNumber, $newPhoneNumber, $factoryID){
  190. $data = array(
  191. 'old_phone' => $oldPhoneNumber,
  192. 'new_phone' => $newPhoneNumber,
  193. 'factory_code' => $factoryID
  194. );
  195.  
  196. $id = $this->db->insert (DBConfig::TABLE_ANDROID_REGISTRATION_NEWPHONE, $data);
  197. if ($id){
  198. return true;
  199. }else{
  200. return false;
  201. }
  202. }
  203.  
  204. private function validateFactoryCode($factoryID){
  205. $factorys = $this->db->get(DBConfig::TABLE_KOMPAK_KOMPAKFACTORY);
  206.  
  207. foreach ($factorys as $factory) {
  208. //if($factory["id"] == $factoryID && ($factoryID == 80 || $factoryID == 63)){
  209. return array(
  210. "statusCode" => RespConfig::SUCCESS,
  211. "message" => null,
  212. "factoryID" => $factoryID,
  213. "shortname" => $factory["id"],
  214. "timezone" => $factory["timezone"]
  215. );
  216. //}
  217. }
  218. return array(
  219. "statusCode" => RespConfig::NOT_FOUND,
  220. "message" => "Invalid Factory Code",
  221. "factoryID" => null,
  222. "shortname" => null,
  223. "timezone" => null
  224. );
  225. }
  226.  
  227. private function validateOldPhoneNumber($oldPhoneNumber){
  228. $oldPhoneNumber = $this->getContactDetailsByPhone($oldPhoneNumber);
  229. return array(
  230. "statusCode" => $oldPhoneNumber != null ? RespConfig::SUCCESS : RespConfig::OLD_NUMBER_NOT_FOUND,
  231. "message" => $oldPhoneNumber != null ? null : "Old Phone Number Not Found",
  232. "contactDetails" => $oldPhoneNumber
  233. );
  234. }
  235.  
  236.  
  237. private function generateToken($phoneNumber, $length = 6) {
  238. $characters = '0123456789';
  239. $charactersLength = strlen($characters);
  240. $randomString = '';
  241. for ($i = 0; $i < $length; $i++) {
  242. $randomString .= $characters[rand(0, $charactersLength - 1)];
  243. }
  244.  
  245. $outbox = $this->sendVerificationCode($phoneNumber, $randomString);
  246. return $randomString;
  247. }
  248.  
  249.  
  250. private function sendVerificationCode($phoneNumber, $verificationCode){
  251. $message = "WPO Connect Verification Code : ".$verificationCode;
  252. $this->insertIntoInbox($phoneNumber, $message, DBConfig::SENDER_ID);
  253. }
  254.  
  255. private function sendGreetings($phoneNumber){
  256. $message = "Welcome! You can use this app to keep in contact with your company, ask questions, report any problems you see and keep up with events in the company! :)";
  257. $this->insertIntoInbox($phoneNumber, $message, DBConfig::SENDER_ID_WPO_CONNECT);
  258. }
  259.  
  260.  
  261. private function sendNewsletter($phoneNumber){
  262. $phone = substr($phoneNumber, 1);
  263. $url = 'http://172.18.21.182/api/wpoconnect-apps-newsletter-byphone.php?phone='.$phone;
  264. $this->http_get(array('url' => $url));
  265. return true;
  266. }
  267.  
  268. private function sendAskFillProfileForm($phoneNumber){
  269. $message = 'Tell us about you! The more we know the better we can make the app work for you. Don\'t worry we won\'t share your personal information with anyone else, including your company. <a href="com.workplaceoptions.wpoconnect://form/profile">Click Here</a> to help out! ';
  270. $this->insertIntoInbox($phoneNumber, $message, DBConfig::SENDER_ID_WPO_CONNECT);
  271. }
  272.  
  273. private function insertIntoInbox($phoneNumber, $message, $senderID){
  274. if(!empty($this->timezone)){
  275. date_default_timezone_set($this->timezone);
  276. }
  277.  
  278. $data = array(
  279. 'DestinationNumber' => $phoneNumber,
  280. 'InsertIntoDB' => date("Y-m-d H:i:s"),
  281. 'TextDecoded' => $message,
  282. 'SenderID' => $senderID,
  283. 'DeliveryReport' => 'default'
  284. );
  285.  
  286. $id = $this->db->insert (DBConfig::TABLE_OUTBOX, $data);
  287. if ($id){
  288. return true;
  289. }else{
  290. return false;
  291. }
  292. }
  293.  
  294. private function connectDB(){
  295. $this->db = new MysqliDb (DBConfig::DB_SERVER, DBConfig::DB_USERNAME, DBConfig::DB_PASSWORD, DBConfig::DB_NAME);
  296. }
  297.  
  298. private function getContactDetailsByPhone($phoneNumber){
  299. $this->db->where ("phone", $phoneNumber);
  300. $cols = array("name","phone","division","factory_id_number","factory");
  301. $user = $this->db->ObjectBuilder()->getOne(DBConfig::TABLE_USER_DETAIL, $cols);
  302. return $user;
  303. }
  304.  
  305. private function http_get($params){
  306. $header = array();
  307.  
  308. $curl = curl_init();
  309. curl_setopt($curl, CURLOPT_URL, $params['url']);
  310. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  311. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  312. $result = curl_exec ($curl);
  313. curl_close ($curl);
  314.  
  315. return $result;
  316. }
  317.  
  318. private function http_post($params){
  319. $header = array();
  320. if(isset($params['token'])){
  321. $header[] = 'Authorization: Token '.$params['token'];
  322. }
  323. $header[] = 'Content-Type: application/json';
  324.  
  325. $curl = curl_init();
  326. curl_setopt($curl, CURLOPT_URL, $params['url']);
  327. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  328. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  329. curl_setopt($curl, CURLOPT_POSTFIELDS, $params['fields']);
  330. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  331. $result = curl_exec ($curl);
  332. curl_close ($curl);
  333.  
  334. unset($curl);
  335. return $result;
  336. }
  337.  
  338. function print_log($fileName, $msg) {
  339. $file = dirname(__DIR__)."/logs/".$fileName."_".date("Ymd").".log";
  340. file_put_contents($file, date("Y-m-d H:i:s")." >> ".$msg."\n",FILE_APPEND );
  341. }
  342. }
  343. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement