forked from Typesetter/Simple-Blog
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimpleBlog.php
executable file
·407 lines (293 loc) · 9.91 KB
/
SimpleBlog.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
defined('is_running') or die('Not an entry point...');
gpPlugin_incl('SimpleBlogCommon.php');
/**
* Class for displaying the Special_Blog page and performing it's actions
*
*/
class SimpleBlog extends SimpleBlogCommon{
public $showing_category = false;
public function __construct(){
global $page, $langmessage, $addonFolderName;
SimpleBlogCommon::Init();
//get the post id
if( $page->pagetype == 'special_display' ){
$this->post_id = $this->PostID($page->requested);
}
if( common::LoggedIn() ){
$page->admin_links[] = array('Special_Blog','Blog Home');
$page->admin_links[] = array('Admin_Blog','New Blog Post','cmd=new_form');
$page->admin_links[] = array('Admin_Blog','Configuration');
$page->admin_links[] = array('Admin_Theme_Content',$langmessage['editable_text'],'cmd=addontext&addon='.urlencode($addonFolderName),' name="gpabox" ');
$label = 'Number of Posts: '. SimpleBlogCommon::$data['post_count'];
$page->admin_links[$label] = '';
$cmd = common::GetCommand();
switch($cmd){
//delete
case 'deleteentry':
case 'delete':
SimpleBlogCommon::Delete();
break;
}
}
if( $this->post_id ){
$this->ShowPost();
return;
}
$this->ShowPage();
if( common::LoggedIn() && !file_exists(self::$index_file) ){
echo '<p>Congratulations on successfully installing Simple Blog for gpEasy.</p> ';
echo '<p>You\'ll probably want to get started by '.common::Link('Admin_Blog','creating a blog post','cmd=new_form').'.</p>';
}
}
/**
* Get the post id from the requested url
*
*/
public function PostID($requested){
if( isset($_REQUEST['id']) && ctype_digit($_REQUEST['id']) ){
return $_REQUEST['id'];
}
if( strpos($requested,'/') === false ){
return;
}
$parts = explode('/',$requested);
if( SimpleBlogCommon::$data['urls'] != 'Title' ){
$ints = strspn($parts[1],'0123456789');
if( $ints ){
return substr($parts[1],0,$ints);
}
}
$id = SimpleBlogCommon::AStrKey('titles',$parts[1], true);
if( $id !== false ){
return $id;
}
return $this->SimilarPost($parts[1]);
}
/**
* Get a id for the post that is most similar to the requested title
*
*/
public function SimilarPost($title){
global $config;
$titles = SimpleBlogCommon::AStrToArray('titles');
$post_times = SimpleBlogCommon::AStrToArray('post_times');
$similar = array();
$lower = str_replace(' ','_',strtolower($title));
foreach($titles as $post_id => $title){
if( $post_times[$post_id] > time() ){
continue;
}
similar_text($lower,strtolower($title),$percent);
$similar[$percent] = $post_id; //if similarity is the same for two posts, the newer post will take precedence
}
krsort($similar);
$similarity = key($similar);
if( $config['auto_redir'] > 0 && $similarity >= $config['auto_redir'] ){
return current($similar);
}
}
/**
* Output the html for a single blog post
* Handle comment actions
*/
public function ShowPost(){
if( common::LoggedIn() ){
gpPlugin_incl('Admin/SimpleBlogPage.php');
$blog_page = new AdminSimpleBlogPage($this->post_id);
}else{
gpPlugin_incl('SimpleBlogPage.php');
$blog_page = new SimpleBlogPage($this->post_id);
}
$blog_page->ShowPost();
}
/**
* Display a blog page with multiple blog posts
*
*/
public function ShowPage(){
global $page;
$per_page = SimpleBlogCommon::$data['per_page'];
$page_num = 0;
$expected_q = '';
if( isset($_GET['page']) && is_numeric($_GET['page']) ){
$page_num = (int)$_GET['page'];
$expected_q = 'page='.$page_num;
}
//redirect if the request isn't correct
if( $page->requested != SimpleBlogCommon::$root_url ){
$expected_url = common::GetUrl( SimpleBlogCommon::$root_url, $expected_q, false );
common::Redirect($expected_url);
}
$start = $page_num * $per_page;
$include_drafts = common::LoggedIn();
$show_posts = SimpleBlogCommon::WhichPosts($start,$per_page,$include_drafts);
$this->ShowPosts($show_posts);
//pagination links
echo '<p class="blog_nav_links">';
if( $page_num > 0 ){
$html = common::Link('Special_Blog','%s');
echo gpOutput::GetAddonText('Blog Home',$html);
echo ' ';
$html = common::Link('Special_Blog','%s','page='.($page_num-1),'class="blog_newer"');
echo gpOutput::GetAddonText('Newer Entries',$html);
echo ' ';
}
if( ( ($page_num+1) * $per_page) < SimpleBlogCommon::$data['post_count'] ){
$html = common::Link('Special_Blog','%s','page='.($page_num+1),'class="blog_older"');
echo gpOutput::GetAddonText('Older Entries',$html);
}
echo '</p>';
}
/**
* Output the blog posts in the array $post_list
*
*/
public function ShowPosts($post_list){
$posts = array();
foreach($post_list as $post_index){
$this->ShowPostContent( $post_index );
}
}
/**
* Display the html for a single blog post
*
*/
public function ShowPostContent( $post_index ){
if( !common::LoggedIn() && SimpleBlogCommon::AStrGet('drafts',$post_index) ){
return false;
}
$post = SimpleBlogCommon::GetPostContent($post_index);
$class = $id = '';
if( common::LoggedIn() ){
SimpleBlog::EditLinks($post_index, $class, $id);
}
echo '<div class="blog_post post_list_item'.$class.'" '.$id.'>';
$header = '<h2 id="blog_post_'.$post_index.'">';
if( SimpleBlogCommon::AStrGet('drafts',$post_index) ){
$header .= '<span style="opacity:0.3;">';
$header .= gpOutput::SelectText('Draft');
$header .= '</span> ';
}elseif( $post['time'] > time() ){
$header .= '<span style="opacity:0.3;">';
$header .= gpOutput::SelectText('Pending');
$header .= '</span> ';
}
$label = SimpleBlogCommon::Underscores( $post['title'] );
$header .= SimpleBlogCommon::PostLink($post_index,$label);
$header .= '</h2>';
SimpleBlogCommon::BlogHead($header,$post_index,$post);
echo '<div class="twysiwygr">';
if( !empty(SimpleBlogCommon::$data['post_abbrev']) && SimpleBlogCommon::$data['abbrev_image'] ){
$this->GetImageFromPost($post['content']);
}
echo $this->AbbrevContent( $post['content'], $post_index, SimpleBlogCommon::$data['post_abbrev']);
echo '</div>';
echo '</div>';
if( SimpleBlogCommon::$data['abbrev_cat'] && isset($post['categories']) && count($post['categories']) ){
$temp = array();
foreach($post['categories'] as $catindex){
$title = SimpleBlogCommon::AStrGet( 'categories', $catindex );
if( !$title ){
continue;
}
if( SimpleBlogCommon::AStrGet('categories_hidden',$catindex) ){
continue;
}
$temp[] = SimpleBlogCommon::CategoryLink($catindex, $title, $title);
}
if( count($temp) ){
echo '<div class="category_container">';
echo gpOutput::GetAddonText('Categories').' ';
echo implode(', ',$temp);
echo '</div>';
}
}
echo '<div class="clear"></div>';
}
/**
* Get the fist image from the blog post
*
*/
public function GetImageFromPost($item){
$img_pos = strpos($item,'<img');
if( $img_pos === false ){
return;
}
$src_pos = strpos($item,'src=',$img_pos);
if( $src_pos === false ){
return;
}
$src = substr($item,$src_pos+4);
$quote = $src[0];
if( $quote != '"' && $quote != "'" ){
return;
}
$src_pos = strpos($src,$quote,1);
$src = substr($src,1,$src_pos-1);
// check for resized image, get original source if img is resized
if( strpos($src,'image.php') !== false && strpos($src,'img=') !== false ){
$src = $dirPrefix . '/data/_uploaded/' . urldecode(substr($src,strpos($src,'img=')+4));
}
$thumb_path = common::ThumbnailPath($src);
//make it an absolute path
if( isset($_SERVER['HTTP_HOST']) ){
$server = $_SERVER['HTTP_HOST'];
}else{
$server = $_SERVER['SERVER_NAME'];
}
$thumb_path = '//'.$server.$thumb_path;
echo '<img class="img-thumbnail" src="'.$thumb_path.'"/>';
}
/**
* Get the edit links for the post
*
* @param string $class
* @param string $id
*/
public static function EditLinks($post_index, &$class, &$id){
global $langmessage;
$query = 'du'; //dummy parameter
SimpleBlogCommon::UrlQuery( $post_index, $url, $query );
$edit_link = gpOutput::EditAreaLink($edit_index,$url,$langmessage['edit'].' (TWYSIWYG)',$query,'name="inline_edit_generic" rel="text_inline_edit"');
$class = ' editable_area';
$id = 'id="ExtraEditArea'.$edit_index.'"';
echo '<span style="display:none;" id="ExtraEditLnks'.$edit_index.'">';
echo $edit_link;
echo common::Link('Admin_Blog/'.$post_index,$langmessage['edit'].' (All)','cmd=edit_post',' style="display:none"');
echo common::Link('Special_Blog',$langmessage['delete'],'cmd=deleteentry&del_id='.$post_index,array('class'=>'delete gpconfirm','data-cmd'=>'cnreq','title'=>$langmessage['delete_confirm']));
if( SimpleBlogCommon::$data['allow_comments'] ){
$comments_closed = SimpleBlogCommon::AStrGet('comments_closed',$post_index);
if( $comments_closed ){
$label = gpOutput::SelectText('Open Comments');
echo SimpleBlogCommon::PostLink($post_index,$label,'cmd=opencomments','name="cnreq" style="display:none"');
}else{
$label = gpOutput::SelectText('Close Comments');
echo SimpleBlogCommon::PostLink($post_index,$label,'cmd=closecomments','name="cnreq" style="display:none"');
}
}
echo common::Link('Admin_Blog','New Blog Post','cmd=new_form',' style="display:none"');
echo common::Link('Admin_Blog',$langmessage['administration'],'',' style="display:none"');
echo '</span>';
}
/**
* Abbreviate $content if a $limit greater than zero is given
*
*/
public function AbbrevContent( $content, $post_index, $limit = 0 ){
if( !is_numeric($limit) || $limit == 0 ){
return $content;
}
$content = strip_tags($content);
if( mb_strlen($content) < $limit ){
return $content;
}
$pos = mb_strpos($content,' ',$limit-5);
if( ($pos > 0) && ($limit+20 > $pos) ){
$limit = $pos;
}
$content = mb_substr($content,0,$limit).' ... ';
$label = gpOutput::SelectText('Read More');
return $content . SimpleBlogCommon::PostLink($post_index,$label);
}
}