Skip to content

Commit 539ad79

Browse files
committed
Add comment endpoint
1 parent 6a31bd4 commit 539ad79

File tree

2 files changed

+310
-5
lines changed

2 files changed

+310
-5
lines changed

easyblog/easyblog.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ public function __construct(&$subject, $config = array())
2424
return;
2525
}
2626

27+
// Load Easyblog language & bootstrap files
28+
$language = JFactory::getLanguage();
29+
$language->load('com_easyblog');
30+
require_once( JPATH_ROOT . '/components/com_easyblog/constants.php' );
31+
require_once( EBLOG_HELPERS . '/helper.php' );
32+
2733
// Set resources & access
2834
ApiResource::addIncludePath(dirname(__FILE__).'/easyblog');
2935
$this->setResourceAccess('latest', 'public', 'get');
3036
$this->setResourceAccess('category', 'public', 'get');
3137
$this->setResourceAccess('blog', 'public', 'get');
38+
$this->setResourceAccess('comments', 'public', 'get');
3239

33-
// Load Easyblog language & bootstrap files
34-
$language = JFactory::getLanguage();
35-
$language->load('com_easyblog');
36-
require_once( JPATH_ROOT . '/components/com_easyblog/constants.php' );
37-
require_once( EBLOG_HELPERS . '/helper.php' );
40+
$config = EasyBlogHelper::getConfig();
41+
if ($config->get('main_allowguestcomment')) {
42+
$this->setResourceAccess('comments', 'public', 'post');
43+
}
44+
3845
}
3946
}

easyblog/easyblog/comments.php

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,208 @@ public function get() {
5757

5858
}
5959

60+
public function post() {
61+
{
62+
$app = JFactory::getApplication();
63+
$my = JFactory::getUser();
64+
$config = EasyBlogHelper::getConfig();
65+
$acl = EasyBlogACLHelper::getRuleSet();
66+
$post = $app->input->post->getArray();
67+
68+
if( empty($acl->rules->allow_comment) && (empty($my->id) && !$config->get('main_allowguestcomment')) )
69+
{
70+
$this->plugin->setResponse( $this->getErrorResponse(500, JText::_('COM_EASYBLOG_NO_PERMISSION_TO_POST_COMMENT')) );
71+
}
72+
73+
$isModerated = false;
74+
$parentId = isset($post['parent_id']) ? $post['parent_id'] : 0;
75+
$commentDepth = isset($post['comment_depth']) ? $post['comment_depth'] : 0;
76+
$blogId = isset($post['id']) ? $post['id'] : 0;
77+
$subscribeBlog = isset($post['subscribe-to-blog']) ? true : false;
78+
79+
if (!$blogId) {
80+
$this->plugin->setResponse( $this->getErrorResponse(404, 'Invalid Blog') );
81+
return;
82+
}
83+
84+
// @task: Cleanup posted values.
85+
array_walk($post, array($this, '_trim') );
86+
array_walk($post, array($this, '_revertValue') );
87+
88+
if( !$config->get( 'comment_require_email' ) && !isset( $post['esemail'] ) )
89+
{
90+
$post['esemail'] = '';
91+
}
92+
93+
// @task: Run some validation tests on the posted values.
94+
if(! $this->_validateFields($post))
95+
{
96+
$this->plugin->setResponse( $this->getErrorResponse(500, $this->err[0]) );
97+
return;
98+
}
99+
100+
// @task: Akismet detection service.
101+
if( $config->get( 'comment_akismet' ) )
102+
{
103+
$data = array(
104+
'author' => $post['esname'],
105+
'email' => $post['esname'],
106+
'website' => JURI::root() ,
107+
'body' => $post['comment'] ,
108+
'permalink' => EasyBlogRouter::_( 'index.php?option=com_easyblog&view=entry&id=' . $post['id'] )
109+
);
110+
111+
if( EasyBlogHelper::getHelper( 'Akismet' )->isSpam( $data ) )
112+
{
113+
$this->plugin->setResponse( $this->getErrorResponse(500, JText::_('COM_EASYBLOG_SPAM_DETECTED_IN_COMMENT')) );
114+
return false;
115+
}
116+
}
117+
118+
// @task: Retrieve the comments model
119+
$model = EasyBlogHelper::getModel( 'Comment' );
120+
121+
// @task: Retrieve the comment's table
122+
$comment = EasyBlogHelper::getTable( 'Comment' );
123+
124+
// We need to rename the esname and esemail back to name and email.
125+
$post['name'] = $post['esname'];
126+
$post['email'] = $post['esemail'];
127+
128+
unset($post['esname']);
129+
unset($post['esemail']);
130+
131+
// @task: Bind posted values into the table.
132+
$comment->bindPost( $post );
133+
134+
if( !EasyBlogHelper::getHelper( 'Captcha' )->verify( $post ) )
135+
{
136+
return EasyBlogHelper::getHelper( 'Captcha' )->getError( $ajax , $post );
137+
}
138+
139+
// @task: Process registrations
140+
$registerUser = isset( $post[ 'esregister' ] ) ? true : false;
141+
$fullname = isset( $post[ 'name' ] ) ? $post['name'] : '';
142+
$username = isset( $post[ 'esusername' ] ) ? $post[ 'esusername' ] : '';
143+
$email = $post[ 'email' ];
144+
$message = '';
145+
$newUserId = 0;
146+
147+
// @task: Process registrations if necessary
148+
if( $registerUser && $my->id <= 0 )
149+
{
150+
$state = $this->processRegistrations( $post , $username , $email , $ajax );
151+
152+
if( !is_numeric( $state ) )
153+
{
154+
$ajax->script( "eblog.loader.doneLoading();" );
155+
$ajax->script( 'eblog.comment.displayInlineMsg( "error" , "' . $state . '");' );
156+
EasyBlogHelper::getHelper( 'Captcha' )->reload( $ajax , $post );
157+
158+
return $ajax->send();
159+
}
160+
161+
$newUserId = $state;
162+
}
163+
164+
$totalComments = empty( $post[ 'totalComment' ] ) ? 1 : $post[ 'totalComment' ];
165+
166+
$date = EasyBlogHelper::getDate();
167+
168+
$comment->set( 'created' , $date->toMySQL() );
169+
$comment->set( 'modified' , $date->toMySQL() );
170+
$comment->set( 'published' , 1 );
171+
$comment->set( 'parent_id' , $parentId );
172+
$comment->set( 'sent' , 0 );
173+
$comment->set( 'created_by' , $my->id );
174+
175+
// @rule: Update the user's id if they have just registered earlier.
176+
if( $newUserId != 0 )
177+
{
178+
$comment->set( 'created_by' , $newUserId );
179+
}
180+
181+
// @rule: Update publish status if the comment requires moderation
182+
if( ($config->get( 'comment_moderatecomment') == 1) || ($my->id == 0 && $config->get( 'comment_moderateguestcomment') == 1) )
183+
{
184+
$comment->set( 'published' , EBLOG_COMMENT_STATUS_MODERATED );
185+
$isModerated = true;
186+
}
187+
188+
$blog = EasyBlogHelper::getTable( 'Blog' );
189+
$blog->load($blogId);
190+
191+
// If moderation for author is disabled, ensure that the comment is published.
192+
// If the author is the owner of the blog, it should never be moderated.
193+
if( !$config->get( 'comment_moderateauthorcomment' ) && $blog->created_by == $my->id )
194+
{
195+
$comment->set( 'published' , 1 );
196+
$isModerated = false;
197+
}
198+
199+
if( !$comment->store() )
200+
{
201+
$this->plugin->setResponse( $this->getErrorResponse(500, 'There was a problem saving the comment') );
202+
}
203+
204+
$message = JText::_('COM_EASYBLOG_COMMENTS_SUCCESS');
205+
206+
if( $newUserId != 0 && $registerUser )
207+
{
208+
$message = JText::_('COM_EASYBLOG_COMMENTS_SUCCESS_AND_REGISTERED');
209+
}
210+
211+
// @rule: Process subscription for blog automatically when the user submits a new comment and wants to subscribe to the blog.
212+
if( $subscribeBlog && $config->get( 'main_subscription' ) && $blog->subscription )
213+
{
214+
$isSubscribed = false;
215+
$userId = $my->id;
216+
$blogModel = EasyblogHelper::getModel('Blog');
217+
218+
if( $userId == 0 )
219+
{
220+
$sid = $blogModel->isBlogSubscribedEmail( $blog->id , $email );
221+
222+
if( empty( $sid ) )
223+
{
224+
$isSubscribed = $blogModel->addBlogSubscription( $blog->id , $email, '', $fullname );
225+
}
226+
}
227+
else
228+
{
229+
$sid = $blogModel->isBlogSubscribedUser( $blog->id , $userId , $email);
230+
if( !empty( $sid ) )
231+
{
232+
// @task: User found, update the email address
233+
$blogModel->updateBlogSubscriptionEmail($sid, $userId, $email);
234+
}
235+
else
236+
{
237+
$isSubscribed = $blogModel->addBlogSubscription( $blog->id , $email, $userId, $fullname);
238+
}
239+
}
240+
}
241+
242+
$row = $comment;
243+
$creator = EasyBlogHelper::getTable( 'Profile' );
244+
$creator->load( $my->id );
245+
246+
$row->poster = $creator;
247+
$row->comment = nl2br($row->comment);
248+
$row->comment = EasyBlogCommentHelper::parseBBCode($row->comment);
249+
$row->depth = (is_null($commentDepth)) ? '0' : $commentDepth;
250+
$row->likesAuthor = '';
251+
252+
// @rule: Process notifications
253+
$comment->processEmails( $isModerated , $blog );
254+
255+
//update the sent flag to sent
256+
$comment->updateSent();
257+
258+
$this->plugin->setResponse( $comment );
259+
260+
}}
261+
60262
public static function getName() {
61263

62264
}
@@ -65,4 +267,100 @@ public static function describe() {
65267

66268
}
67269

270+
271+
function _validateFields($post)
272+
{
273+
$config = EasyBlogHelper::getConfig();
274+
$my = JFactory::getUser();
275+
276+
if( !isset( $post[ 'comment' ] ) )
277+
{
278+
return false;
279+
}
280+
281+
if(JString::strlen($post['comment']) == 0)
282+
{
283+
$this->err[0] = JText::_('COM_EASYBLOG_COMMENT_IS_EMPTY');
284+
$this->err[1] = 'comment';
285+
return false;
286+
}
287+
288+
if( $config->get('comment_requiretitle') && (JString::strlen($post['title']) == 0 || $post['title'] == JText::_('COM_EASYBLOG_TITLE')))
289+
{
290+
$this->err[0] = JText::_( 'COM_EASYBLOG_COMMENT_TITLE_IS_EMPTY' );
291+
$this->err[1] = 'title';
292+
return false;
293+
}
294+
295+
if(isset($post['esregister']) && isset($post['esusername']))
296+
{
297+
if(JString::strlen($post['esusername']) == 0 || $post['esusername'] == JText::_('COM_EASYBLOG_USERNAME'))
298+
{
299+
$this->err[0] = JText::_('COM_EASYBLOG_SUBSCRIPTION_USERNAME_IS_EMPTY');
300+
$this->err[1] = 'esusername';
301+
return false;
302+
}
303+
}
304+
305+
if(JString::strlen($post['esname']) == 0 || $post['esname'] == JText::_('COM_EASYBLOG_NAME'))
306+
{
307+
$this->err[0] = JText::_('COM_EASYBLOG_COMMENT_NAME_IS_EMPTY');
308+
$this->err[1] = 'esname';
309+
return false;
310+
}
311+
312+
313+
// @rule: Only check for valid email when the email is really required
314+
if( $config->get( 'comment_require_email' ) && (JString::strlen($post['esemail']) == 0 || $post['esemail'] == JText::_('COM_EASYBLOG_EMAIL') ) )
315+
{
316+
$this->err[0] = JText::_('COM_EASYBLOG_COMMENT_EMAIL_IS_EMPTY');
317+
$this->err[1] = 'esemail';
318+
return false;
319+
}
320+
else if( isset( $post['subscribe-to-blog']) && (JString::strlen($post['esemail']) == 0 || $post['esemail'] == JText::_('COM_EASYBLOG_EMAIL') ))
321+
{
322+
$this->err[0] = JText::_('COM_EASYBLOG_COMMENT_EMAIL_IS_EMPTY');
323+
$this->err[1] = 'esemail';
324+
return false;
325+
}
326+
else
327+
{
328+
if( (! EasyBlogHelper::getHelper( 'email' )->isValidInetAddress( $post['esemail'] )) && ($config->get( 'comment_require_email' ) || isset( $post['subscribe-to-blog']) ))
329+
{
330+
$this->err[0] = JText::_('COM_EASYBLOG_COMMENT_EMAIL_INVALID');
331+
$this->err[1] = 'esemail';
332+
return false;
333+
}
334+
}
335+
336+
if($config->get('comment_tnc') == true && ( ( $config->get('comment_tnc_users') == 0 && $my->id <=0) || ( $config->get('comment_tnc_users') == 1 && $my->id >= 0) || ( $config->get('comment_tnc_users') == 2) ) )
337+
{
338+
if(empty($post['tnc']))
339+
{
340+
$this->err[0] = JText::_( 'COM_EASYBLOG_YOU_MUST_ACCEPT_TNC' );
341+
$this->err[1] = 'tnc';
342+
return false;
343+
}
344+
}
345+
346+
return true;
347+
}
348+
349+
function _trim(&$text)
350+
{
351+
$text = JString::trim($text);
352+
}
353+
354+
function _revertValue(&$text)
355+
{
356+
if( $text == JText::_('COM_EASYBLOG_TITLE') ||
357+
$text == JText::_('COM_EASYBLOG_USERNAME') ||
358+
$text == JText::_('COM_EASYBLOG_NAME') ||
359+
$text == JText::_('COM_EASYBLOG_EMAIL') ||
360+
$text == JText::_('COM_EASYBLOG_WEBSITE'))
361+
{
362+
$text = '';
363+
}
364+
}
365+
68366
}

0 commit comments

Comments
 (0)