Completed
Push — development ( e90f69...2fb2af )
by Ashutosh
11:39
created

BaseAuthController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 113
dl 0
loc 207
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A sendActivation() 0 51 5
A sendOtp() 0 15 2
A requestOtpFromAjax() 0 33 2
A sendForReOtp() 0 15 2
A redirectPath() 0 8 4
A addToZoho() 0 20 1
A reqFields() 0 27 2
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\ApiKey;
8
use App\User;
9
use App\Model\User\AccountActivate;
10
11
class BaseAuthController extends Controller
12
{
13
	//Required Fields for Zoho
14
    public function reqFields($user, $email)
15
    {
16
        $user = $user->where('email', $email)->first();
17
        if ($user) {
18
            $xml = '      <Leads>
19
                        <row no="1">
20
                        <FL val="Lead Source">Faveo Billing</FL>
21
                        <FL val="Company">'.$user->company.'</FL>
22
                        <FL val="First Name">'.$user->first_name.'</FL>
23
                        <FL val="Last Name">'.$user->last_name.'</FL>
24
                        <FL val="Email">'.$user->email.'</FL>
25
                        <FL val="Manager">'.$user->manager.'</FL>
26
27
                        <FL val="Phone">'.$user->mobile_code.''.$user->mobile.'</FL>
28
                        <FL val="Mobile">'.$user->mobile_code.''.$user->mobile.'</FL>
29
                        <FL val="Industry">'.$user->bussiness.'</FL>
30
                        <FL val="City">'.$user->town.'</FL>
31
                        <FL val="Street">'.$user->address.'</FL>
32
                        <FL val="State">'.$user->state.'</FL>
33
                        <FL val="Country">'.$user->country.'</FL>
34
35
                        <FL val="Zip Code">'.$user->zip.'</FL>
36
                         <FL val="No. of Employees">'.$user->company_size.'</FL>
37
                        </row>
38
                        </Leads>';
39
40
            return $xml;
41
        }
42
    }
43
44
    /**
45
    * Add User ToZoho
46
    */
47
    public function addToZoho($auth,$zoho)
48
    {
49
    	$zohoUrl = 'https://crm.zoho.com/crm/private/xml/Leads/insertRecords??duplicateCheck=1&';
50
        $query = 'authtoken='.$auth.'&scope=crmapi&xmlData='.$zoho;
51
        $ch = curl_init();
52
        curl_setopt($ch, CURLOPT_URL, $zohoUrl);
53
        /* allow redirects */
54
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
55
        /* return a response into a variable */
56
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
57
        /* times out after 30s */
58
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
59
        /* set POST method */
60
        curl_setopt($ch, CURLOPT_POST, 1);
61
        /* add POST fields parameters */
62
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query); // Set the request as a POST FIELD for curl.
63
64
        //Execute cUrl session
65
        $response = curl_exec($ch);
66
        curl_close($ch);
67
    }
68
    
69
70
    /**
71
    * Sends Otp
72
    */
73
    public static function sendOtp($mobile, $code)
74
    {
75
        $client = new \GuzzleHttp\Client();
76
        $number = $code.$mobile;
77
        $key = ApiKey::where('id', 1)->value('msg91_auth_key');
78
        $response = $client->request('GET', 'https://control.msg91.com/api/sendotp.php', [
79
            'query' => ['authkey' => $key, 'mobile' => $number],
80
        ]);
81
        $send = $response->getBody()->getContents();
82
        $array = json_decode($send, true);
83
        if ($array['type'] == 'error') {
84
            throw new \Exception($array['message']);
85
        }
86
87
        return $array['type'];
88
    }
89
90
      /**
91
    * ReSends Otp
92
    */
93
    public function sendForReOtp($mobile, $code)
94
    {
95
        $client = new \GuzzleHttp\Client();
96
        $number = $code.$mobile;
97
        $key = ApiKey::where('id', 1)->value('msg91_auth_key');
98
        $response = $client->request('GET', 'https://control.msg91.com/api/retryotp.php', [
99
            'query' => ['authkey' => $key, 'mobile' => $number],
100
        ]);
101
        $send = $response->getBody()->getContents();
102
        $array = json_decode($send, true);
103
        if ($array['type'] == 'error') {
104
            throw new \Exception($array['message']);
105
        }
106
107
        return $array['type'];
108
    }
109
    
110
111
     /**
112
    * Sends otp and email for confirmatiob
113
    */
114
    public function requestOtpFromAjax(Request $request)
115
    {
116
        // dd($request->allow());
117
        $this->validate($request, [
118
            'email'  => 'required|email',
119
            'code'   => 'required|numeric',
120
            'mobile' => 'required|numeric',
121
        ]);
122
        $email = $request->oldemail;
123
        $newEmail = $request->newemail;
124
        $number = $request->oldnumber;
125
        $newNumber = $request->newnumber;
126
        User::where('email', $email)->update(['email'=>$newEmail, 'mobile'=>$newNumber]);
127
128
        try {
129
            $code = $request->input('code');
130
            $mobile = $request->input('mobile');
131
            $userid = $request->input('id');
132
            $email = $request->input('email');
133
            $pass = $request->input('password');
134
            $number = $code.$mobile;
135
136
            $result = $this->sendOtp($mobile, $code);
137
            $method = 'POST';
138
139
            $this->sendActivation($email, $method, $pass);
140
            $response = ['type' => 'success', 'message' => 'Activation link has been sent to '.$email.'.<br>OTP has been sent to '.$number.'.<br>Please enter the OTP received on your mobile No below. Incase you did not recieve OTP,please get in touch with us on <a href="mailto:[email protected]">[email protected]</a>'];
141
142
            return response()->json($response);
143
        } catch (\Exception $ex) {
144
            $result = [$ex->getMessage()];
145
146
            return response()->json(compact('result'), 500);
147
        }
148
    }
149
150
    public function sendActivation($email, $method, $str = '')
151
    {
152
        try {
153
            $user = new User();
154
155
            $activate_model = new AccountActivate();
156
            $user = $user->where('email', $email)->first();
157
            if (!$user) {
158
                return redirect()->back()->with('fails', 'Invalid Email');
159
            }
160
161
            if ($method == 'GET') {
162
                $activate_model = $activate_model->where('email', $email)->first();
163
                $token = $activate_model->token;
164
            } else {
165
                $token = str_random(40);
166
                $activate = $activate_model->create(['email' => $email, 'token' => $token]);
167
                $token = $activate->token;
168
            }
169
170
            $url = url("activate/$token");
171
            //check in the settings
172
            $settings = new \App\Model\Common\Setting();
173
            $settings = $settings->where('id', 1)->first();
174
175
            //template
176
            $template = new \App\Model\Common\Template();
177
            $temp_id = $settings->where('id', 1)->first()->welcome_mail;
178
            $template = $template->where('id', $temp_id)->first();
179
            $from = $settings->email;
180
            // var_dump($temp_id);
181
            //  die();
182
            $to = $user->email;
183
            $subject = $template->name;
184
            $data = $template->data;
185
            $replace = ['name' => $user->first_name.' '.$user->last_name, 'username' => $user->email, 'password' => $str, 'url' => $url];
186
            $type = '';
187
188
            if ($template) {
189
                $type_id = $template->type;
190
                $temp_type = new \App\Model\Common\TemplateType();
191
                $type = $temp_type->where('id', $type_id)->first()->name;
192
            }
193
194
            //dd($from, $to, $data, $subject, $replace, $type);
195
            $templateController = new \App\Http\Controllers\Common\TemplateController();
196
            $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
197
198
            return $mail;
199
        } catch (\Exception $ex) {
200
            throw new \Exception($ex->getMessage());
201
        }
202
    }
203
204
205
    /**
206
     * Get the post register / login redirect path.
207
     *
208
     * @return string
209
     */
210
    public function redirectPath()
211
    {
212
        if (\Session::has('session-url')) {
213
            $url = \Session::get('session-url');
214
215
            return property_exists($this, 'redirectTo') ? $this->redirectTo : '/'.$url;
216
        } else {
217
            return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
218
        }
219
    }
220
221
}
222