-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertPost.php
125 lines (118 loc) · 3.4 KB
/
insertPost.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
125
<?php
require_once 'core/init.php';
$user = new User();
if($user->hasPermission('banned')){
exit("you are banned");
}
if(!$user->isLoggedIn()){
Redirect::to('index.php');
exit("you are not logged in");
}
if(Input::exists()){
$imgVal = ImageVal('userfile');
// if(Token::check(Input::get('token'))){
$validate = new Validate();
$validation = $validate->check($_POST, array(
'pTitle' => array(
'required' => true,
'min' => 5,
'max' => 40
),
'titleOne' => array(
'required' => true,
'min' => 5,
'max' => 60
),
'titleTwo' => array(
'min' => 5,
'max' => 60
),
'titleThree' => array(
'min' => 5,
'max' => 60
)
));
if($validation->passed()){
if(!array_key_exists(0, $imgVal)){
$public = (Input::get('public') === 'on') ? 1 : 0;
try {
$time = date('Y-m-d H:i:s');
$str = $time.$user->data()->id;
$uni = sha1($str);
$user->create('post','posts',array(
'userID' => $user->data()->id,
'title' => Input::get('pTitle'),
'timeStamp' => $time,
'public' => $public,
'username' => $user->data()->username,
'gender' => $user->data()->gender,
'identifier' => $uni,
'description' => Input::get('desc')
));
$posts = DB::getInstance()->get('posts', array('identifier', '=', $uni));
$results = $posts->results();
if (!$posts->count()){ // row count
echo 'no posts';
}else{
$target_dir = "images/photos/";
foreach ($results as $post) {
$postID = $post->id;
$countN = 0;
$uploadOk = 1;
if ($_FILES['userfile']) {
$file_ary = reArrayFiles($_FILES['userfile']);
foreach ($file_ary as $file) {
if($file['tmp_name'] != null){
$ext = explode("." ,$file['name']);
$uniq = uniqid();
$target_file = $target_dir . $post->id . "-" . $uniq . "." . end($ext);
$filename = $target_dir. "small-".$post->id . "-" . $uniq . "." . end($ext);
$filePath = $post->id . "-" . $uniq . "." . end($ext);
$image = new SimpleImage($file['tmp_name']);
$image->resizeToWidth(640);
if ($image->save($target_file)) {
$image->resize(60,60);
$image->save($filename);
if($countN == 0){
$addedTitle = Input::get('titleOne');
$countN = $countN + 1;
} elseif ($countN == 1) {
$addedTitle = Input::get('titleTwo');
$countN = $countN + 1;
} elseif ($countN == 2) {
$addedTitle = Input::get('titleThree');
$countN = $countN + 1;
}
$user->create('image','images',array(
'postID' => $post->id,
'userID' => $post->userID,
'addedTitle' => $addedTitle,
'filePath' => $filePath,
'timeStamp' => date('Y-m-d H:i:s')
));
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
}
}
}
} catch(Exception $e){
die($e->getMessage());
}
Redirect::to('viewPost.php?post='.$postID);
}else{
foreach ($imgVal as $imgError) {
echo "Error:$imgError<br>";
}
}
}else{
foreach ($validation->errors() as $error) {
echo "Error:$error<br>";
}
}
// }
}else{
echo "sorry there was no input";
}