-
Notifications
You must be signed in to change notification settings - Fork 0
/
authors_info_backend.php
124 lines (119 loc) · 3.49 KB
/
authors_info_backend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
require_once'Core/init.php';
$user = new User;
if(Input::exists('post'))
{
if(Token::check(Input::get('_token')))
{
$json['error_code'] = 0; // error_code = 0 => for all type of errors except token_mismatch
$json['error_status'] = false;
$json['_token'] = Token::generate();
if($user->isLoggedIn())
{
$Validate = new Validate;
$Validate->check($_POST, array(
"name" => array(
"required" => true,
'min' => 2,
'max' => 40
),
"github_username" => array(
"min" => 3,
"max" => 50
),
"facebook_username" => array(
"min" => 3,
"max" => 50
),
"twitter_username" => array(
"min" => 3,
"max" => 50
),
"google_profileId" => array(
"min" => 3,
"max" => 50
),
"description" => array(
"required" => true,
"min" => 10
)
));
if($Validate->passed())
{
$fields = array();
$fields['user_description'] = Input::get('description'); // add description to the array that is gonna be passed for the insert query
$fields["name"] = Input::get('name'); // add github_username if user has entered github_username
$fields["github_username"] = Input::get('github_username'); // add github_username if user has entered github_username
$fields["facebook_username"] = Input::get('facebook_username');
$fields["twitter_username"] = Input::get("twitter_username");
$fields["google_profileId"] = Input::get("google_profileId");
if(!empty($_FILES)) // insert the data if no image uploaded
{
$target_dir = Config::get('url/upload_dir').'/'; // target directory where images are gonna be stored
$target_file = $target_dir . basename($_FILES["profile_pic"]["name"]); // basename of our file
$target_file_type = getimagesize($_FILES["profile_pic"]["tmp_name"])[2]; // getting file_type (extension too)
try
{
if(!in_array($target_file_type, array(IMAGETYPE_JPEG, IMAGETYPE_PNG)))
{
throw new Exception("Incompatible file extension. Only jpeg or png format files allowed");
}
if(!move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $target_file))
{
throw new Exception("couldn't process profile updation. Please try again later");
}
$fields["image_url"] = basename($_FILES["profile_pic"]["name"]) ; // creating index "image_url" if image uploaded is verified
updateInfo('users', $user->data()->id, $fields);
}
catch(Exception $e)
{
$json['error_status'] = true;
$json['error'] = $e->getMessage();
}
}
else
{
updateInfo('users', $user->data()->id, $fields); // insert the data even if there's no image
}
}
else
{
$json['error_status'] = true;
$json['error'] = $Validate->errors()[0];
}
}
else
{
$json['error_status'] = true;
$json['error'] = "You need to log in";
}
header("Content-Type: application/json", true);
echo json_encode($json);
}
else
{
$json['error_code'] = 1; // error_code = 1 => for token_mismatch error
$json['error_status'] = true;
$json['error'] = "Token mismatch error, try again after refreshing the page";
header("Content-Type: application/json", true);
echo json_encode($json);
}
}
else
{
Redirect::to('authors_info.php');
}
function updateInfo($table, $id, $fields)
{
global $user; // setting $user as global so that it could be used in functions
try
{
$user->update($table, $id, $fields);
}
catch(Exception $e)
{
$json['error_status'] = true;
$json['error'] = $e->getMessage();
}
}
?>