Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- require_once ("../config/DBConfig.php");
- require_once ("../config/RespConfig.php");
- require_once ("../vendor/mysqli/MysqliDb.php");
- //require_once ("PredisService.php");
- class LoginService{
- var $FILENAME = "LoginService";
- var $db, $redis, $timezone;
- public function __construct(){
- //$this->redis = new PredisService();
- $this->connectDB();
- }
- public function validatePhoneNumber($params, $gcm_server_id = "1"){ //1 = google firebase; 2 = Pushy.me
- $data = new stdClass();
- /*mandatory fields*/
- $data->msgType = "verificationResp";
- $data->msgStatusCode = RespConfig::SUCCESS;
- $data->msgDescription = null;
- /*additional fields*/
- $data->phone = $params["data"]["msgData"]["phone"];
- $data->verificationCode = $this->generateToken($params["data"]["msgData"]["phone"]);
- $data->contactDetails = $this->getContactDetailsByPhone($params["data"]["msgData"]["phone"]);
- $notification = new stdClass();
- $notification->title = "WPO Connect";
- $notification->body = "Verification Code";
- $notification->icon = "ic_launcher";
- $message = new stdClass();
- $message->notification = $notification;
- $message->data = $data;
- $result = new stdClass();
- $result->message = $message;
- if($data->contactDetails != null){
- $factoryCode = $data->contactDetails->factory; //harus diganti factory ID nya jangan 80
- $factory = $this->validateFactoryCode($factoryCode);
- $this->timezone = $factory["timezone"];
- $this->saveRegistrationToken($params["from"], $params["data"]["msgData"]["phone"], $factoryCode, $gcm_server_id);
- //$this->sendGreetings($params["data"]["msgData"]["phone"]);
- }
- return $result;
- }
- public function registerPhoneNumber($params, $gcm_server_id = "1"){
- // $this->print_log($this->FILENAME, $params);
- // $params = json_decode($params, true);
- //$this->print_log($this->FILENAME, json_encode($params));
- //return false;
- $contactDetails = null;
- $contactDetailsComplete = null;
- $verifiedStatus = "0";
- $factoryCode = $this->validateFactoryCode($params["data"]["msgData"]["factoryID"]);
- $this->timezone = $factoryCode["timezone"];
- $msgStatusCode = $factoryCode["statusCode"];
- $msgDescription = $factoryCode["message"];
- $factoryId = $factoryCode["factoryID"];
- $factoryShortname = $factoryCode["shortname"];
- $statusCode = false;
- if($factoryCode["statusCode"] == RespConfig::SUCCESS){
- $verifiedStatus = "1";
- $factObj = new stdClass();
- $factObj->factory = $factoryId;
- $contactDetails = $factObj;
- }
- if($params["data"]["msgData"]["oldPhoneNumber"] != ""){
- $phoneNumber = $this->validateOldPhoneNumber($params["data"]["msgData"]["oldPhoneNumber"]);
- if($factoryCode["statusCode"] == RespConfig::SUCCESS){
- $statusCode = $phoneNumber["statusCode"];
- $msgDescription = $phoneNumber["message"];
- $verifiedStatus = "1";
- //$this->saveRegistrationToken($params["from"], $params["data"]["msgData"]["phone"], $factoryId);
- /* save new phone number*/
- $this->saveNewPhoneNumber($params["data"]["msgData"]["oldPhoneNumber"], $params["data"]["msgData"]["phone"], $factoryId);
- }else{
- $statusCode = RespConfig::OLD_NUMBER_NOT_FOUND;
- $msgDescription = $factoryCode["message"];
- $verifiedStatus = "0";
- }
- $msgStatusCode = $statusCode;
- $contactDetails = $phoneNumber["contactDetails"];
- $contactDetailsComplete = $phoneNumber["contactDetails"];
- }
- $data = new stdClass();
- /*mandatory fields*/
- $data->msgType = "registrationResp";
- $data->msgStatusCode = $msgStatusCode;
- $data->msgDescription = $msgDescription;
- /*additional fields*/
- $data->contactDetails = $contactDetails;
- $data->verifiedStatus = $verifiedStatus;
- $notification = new stdClass();
- $notification->title = "WPO Connect";
- $notification->body = "Register Phone";
- $notification->icon = "ic_launcher";
- $message = new stdClass();
- $message->notification = $notification;
- $message->data = $data;
- $result = new stdClass();
- $result->message = $message;
- /*if statusCode === SUCCESS, then save to Redis*/
- if($msgStatusCode == RespConfig::SUCCESS){
- $this->saveRegistrationToken($params["from"], $params["data"]["msgData"]["phone"], $params["data"]["msgData"]["factoryID"], $gcm_server_id);
- //$this->sendGreetings($params["data"]["msgData"]["phone"]);
- //$this->sendNewsletter($params["data"]["msgData"]["phone"]);
- $registerContact = new RegisterContactService();
- $registerContact->processNewContactStep1($params);
- }
- if($contactDetailsComplete == null && $msgStatusCode == RespConfig::SUCCESS){
- //$this->sendAskFillProfileForm($params["data"]["msgData"]["phone"]);
- }
- return $result;
- }
- public function updateRegistrationToken($params, $gcm_server_id){
- $phone_number = $params["data"]["msgData"]["phone_number"];
- $registration_token = $params["data"]["msgData"]["registration_token"];
- $factory_code = $params["data"]["msgData"]["factoryCode"];
- $data = array(
- 'registration_token' => $registration_token,
- 'gcm_server_id' => $gcm_server_id,
- 'factory_code' => $factory_code
- );
- $this->db->where('phone_number', $phone_number);
- $this->db->update(DBConfig::TABLE_ANDROID_TOKEN, $data);
- return true;
- }
- public function debug(){
- print_r($this->saveRegistrationToken("token", "087781796692", "54"));
- }
- private function saveRegistrationToken($registrationToken, $phoneNumber, $factoryID, $gcm_server_id = "1"){
- //$this->redis->setKey(REDIS_PREFIX_TOKEN_TO_PHONE.$registrationToken, $phoneNumber);
- //$this->redis->setKey(REDIS_PREFIX_PHONE_TO_TOKEN.$phoneNumber, $registrationToken);
- $data = array(
- 'registration_token' => $registrationToken,
- 'phone_number' => $phoneNumber,
- 'factoryID' => $factoryID,
- 'gcm_server_id' => $gcm_server_id
- );
- //$id = $this->db->insert (DBConfig::TABLE_ANDROID_TOKEN, $data);
- $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));
- if ($id){
- return true;
- }else{
- error_log("Last executed query was ". $this->db->getLastQuery());
- error_log('insert failed: ' . $this->db->getLastError());
- return false;
- }
- }
- private function saveNewPhoneNumber($oldPhoneNumber, $newPhoneNumber, $factoryID){
- $data = array(
- 'old_phone' => $oldPhoneNumber,
- 'new_phone' => $newPhoneNumber,
- 'factory_code' => $factoryID
- );
- $id = $this->db->insert (DBConfig::TABLE_ANDROID_REGISTRATION_NEWPHONE, $data);
- if ($id){
- return true;
- }else{
- return false;
- }
- }
- private function validateFactoryCode($factoryID){
- $factorys = $this->db->get(DBConfig::TABLE_KOMPAK_KOMPAKFACTORY);
- foreach ($factorys as $factory) {
- //if($factory["id"] == $factoryID && ($factoryID == 80 || $factoryID == 63)){
- return array(
- "statusCode" => RespConfig::SUCCESS,
- "message" => null,
- "factoryID" => $factoryID,
- "shortname" => $factory["id"],
- "timezone" => $factory["timezone"]
- );
- //}
- }
- return array(
- "statusCode" => RespConfig::NOT_FOUND,
- "message" => "Invalid Factory Code",
- "factoryID" => null,
- "shortname" => null,
- "timezone" => null
- );
- }
- private function validateOldPhoneNumber($oldPhoneNumber){
- $oldPhoneNumber = $this->getContactDetailsByPhone($oldPhoneNumber);
- return array(
- "statusCode" => $oldPhoneNumber != null ? RespConfig::SUCCESS : RespConfig::OLD_NUMBER_NOT_FOUND,
- "message" => $oldPhoneNumber != null ? null : "Old Phone Number Not Found",
- "contactDetails" => $oldPhoneNumber
- );
- }
- private function generateToken($phoneNumber, $length = 6) {
- $characters = '0123456789';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- $outbox = $this->sendVerificationCode($phoneNumber, $randomString);
- return $randomString;
- }
- private function sendVerificationCode($phoneNumber, $verificationCode){
- $message = "WPO Connect Verification Code : ".$verificationCode;
- $this->insertIntoInbox($phoneNumber, $message, DBConfig::SENDER_ID);
- }
- private function sendGreetings($phoneNumber){
- $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! :)";
- $this->insertIntoInbox($phoneNumber, $message, DBConfig::SENDER_ID_WPO_CONNECT);
- }
- private function sendNewsletter($phoneNumber){
- $phone = substr($phoneNumber, 1);
- $url = 'http://172.18.21.182/api/wpoconnect-apps-newsletter-byphone.php?phone='.$phone;
- $this->http_get(array('url' => $url));
- return true;
- }
- private function sendAskFillProfileForm($phoneNumber){
- $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! ';
- $this->insertIntoInbox($phoneNumber, $message, DBConfig::SENDER_ID_WPO_CONNECT);
- }
- private function insertIntoInbox($phoneNumber, $message, $senderID){
- if(!empty($this->timezone)){
- date_default_timezone_set($this->timezone);
- }
- $data = array(
- 'DestinationNumber' => $phoneNumber,
- 'InsertIntoDB' => date("Y-m-d H:i:s"),
- 'TextDecoded' => $message,
- 'SenderID' => $senderID,
- 'DeliveryReport' => 'default'
- );
- $id = $this->db->insert (DBConfig::TABLE_OUTBOX, $data);
- if ($id){
- return true;
- }else{
- return false;
- }
- }
- private function connectDB(){
- $this->db = new MysqliDb (DBConfig::DB_SERVER, DBConfig::DB_USERNAME, DBConfig::DB_PASSWORD, DBConfig::DB_NAME);
- }
- private function getContactDetailsByPhone($phoneNumber){
- $this->db->where ("phone", $phoneNumber);
- $cols = array("name","phone","division","factory_id_number","factory");
- $user = $this->db->ObjectBuilder()->getOne(DBConfig::TABLE_USER_DETAIL, $cols);
- return $user;
- }
- private function http_get($params){
- $header = array();
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $params['url']);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
- $result = curl_exec ($curl);
- curl_close ($curl);
- return $result;
- }
- private function http_post($params){
- $header = array();
- if(isset($params['token'])){
- $header[] = 'Authorization: Token '.$params['token'];
- }
- $header[] = 'Content-Type: application/json';
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $params['url']);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $params['fields']);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
- $result = curl_exec ($curl);
- curl_close ($curl);
- unset($curl);
- return $result;
- }
- function print_log($fileName, $msg) {
- $file = dirname(__DIR__)."/logs/".$fileName."_".date("Ymd").".log";
- file_put_contents($file, date("Y-m-d H:i:s")." >> ".$msg."\n",FILE_APPEND );
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement