-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjax.php
More file actions
218 lines (178 loc) · 6.12 KB
/
Ajax.php
File metadata and controls
218 lines (178 loc) · 6.12 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
* Blog\Ajax
* PHP version 7
*
* @category Controller
* @package Library
* @author Bill Rocha <prbr@ymail.com>
* @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
* @license <https://opensource.org/licenses/MIT> MIT
* @version GIT: 0.0.1
* @link http://paulorocha.tk/devbr
*/
namespace Blog;
/**
* Ajax Class
*
* @category Controller
* @package Library
* @author Bill Rocha <prbr@ymail.com>
* @license <https://opensource.org/licenses/MIT> MIT
* @link http://paulorocha.tk/devbr
*/
class Ajax
{
public $patchHtml = __DIR__.'/Html/';
private $articlePatch = 'media/article/';
private $articleId = 0;
/**
* Hook test
* @return string Data in POST
*/
function put()
{
\App::e($_POST);
}
/**
* Savar article
* @return array|string return array with "status": "ok' OR "error"
*/
function save()
{
$dt = '';
if (isset($_POST['dt'])) {
$dt = base64_decode($_POST['dt']);
$dt = utf8_encode($dt);
$dt = json_decode($dt);
}
if (isset($dt->content)) {
$media = isset($dt->media) ? $dt->media : '{}';
$category = isset($dt->category) ? $dt->category : 1;
$link = isset($dt->link) ? $dt->link : '';
$tags = isset($dt->tags) ? $dt->tags : '';
$status = isset($dt->status) ? $dt->status : 'R';
$title = isset($dt->title) ? $dt->title : 'Sem Título';
$destaque = isset($dt->destaque) ? $dt->destaque : 'Nenhum destaque ...';
$uID = isset($dt->info->user) ? $dt->info->user : 0;
$aID = isset($dt->info->article) ? $dt->info->article : 0;
$article = new Model\Article($aID);
$article->set('author', $uID);
$article->set('category', $category);
$article->set('pubdate', date('Y-m-d H:i:s'));
$article->set('editdate', date('Y-m-d H:i:s'));
$article->set('link', $link);
$article->set('tags', $tags);
$article->set('status', $status);
$article->set('media', json_encode($media));
$article->set('title', $title);
$article->set('content', str_replace(["--", "..."], ["—", "…"], $dt->content));
$article->set('resume', $destaque);
//Gravando...
$article->save();
//Send to client
$this->send(['status'=>'ok', 'id'=>$article->get('id'), 'link'=>$article->get('link')]);
}
$this->sendError();
}
/**
* Check if "link" is "in use"
* @return void Send data to javascript (Json)
*/
function checkLink()
{
if (!isset($_POST['link'])) {
$this->sendError();
}
$link = strtolower(str_replace([" ",'"',"'",';','.',','], ["-",""], preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities(trim($_POST['link'])))));
$base = new Model\Base;
$status = $base->checkLink($link, $_POST['aID']) === false ? 'ok' : 'error';
$this->send(['status'=>$status,'link'=>$link]);
}
/**
* Delete a article image (and thumbnail)
* @param string $r URL requested
* @param array $param article ID
* @return void void...
*/
function delete($r, $param)
{
$this->setUp($param);
if (isset($_POST['file'])) {
$file = _WWW.$this->articlePatch.basename(rawurldecode($_POST['file']));
$mini = _WWW.$this->articlePatch.'mini_'.basename(rawurldecode($_POST['file']));
if (file_exists($file)) {
unlink($file);
}
if (file_exists($mini)) {
unlink($mini);
}
}
}
/**
* Upload image
* @param string $r URL requested
* @param array $param article ID
* @return void Send data to client
*/
function upload($r, $param)
{
$this->setUp($param);
if (isset($_FILES['files']['error'][0])
&& $_FILES['files']['error'][0] == UPLOAD_ERR_OK) {
$name = basename($_FILES["files"]["name"][0]);
$ext = explode('.', $name);
$ext = '.'.end($ext);
$name = md5($name);
$a['files'][0] = [
'name' => $name.$ext,
'size' => $_FILES['files']['size'][0],
'type' => $_FILES['files']['type'][0],
'url' => _URL.$this->articlePatch.$name.$ext];
//Create a directory (if not exists)
\Devbr\Cli\Main::checkAndOrCreateDir(_WWW.$this->articlePatch, true);
//Save uploaded file
move_uploaded_file($_FILES["files"]["tmp_name"][0], _WWW.$this->articlePatch.$name.$ext);
//Resize image
$canvas = new \Devbr\Canvas(_WWW.$this->articlePatch.$name.$ext);
$canvas->set_quality(80)
->resize('540')
->save(_WWW.$this->articlePatch.$name.$ext);
//Thumbnail
$canvas->set_rgb('#000')
->set_quality(70)
//->resize('265', '150', 'fill')
->resize('120', '68', 'fill')
->save(_WWW.$this->articlePatch.'mini_'.$name.$ext);
//Send to javascript
$this->send($a);
}
header('HTTP/1.1 403 Forbidden');
exit();
}
// ------------------------- Privates
private function setUp($param)
{
if (!isset($param['id'])) {
$this->sendError();
}
$this->articleId = 0 + $param['id'];
$this->articlePatch = $this->articlePatch.$this->articleId.'/';
}
/**
* Send encoded json data
* @return void Send data and stop PHP execution.
*/
static function send($data)
{
@ob_end_clean();
ob_start('ob_gzhandler');
header('Vary: Accept-Language, Accept-Encoding');
header('Content-Type: application/json');
exit(json_encode($data));
}
static function sendError($data = false)
{
self::send(array_merge(['status'=>'error'], $data));
}
}