Dotclear

source: plugins/contribute/inc/lib.contribute.document.php @ 2172

Revision 2172, 17.7 KB checked in by Moe, 14 years ago (diff)

Contribute 1.0-alpha24 :

  • moved PHP classes in their own files
  • added a different style to the post preview
  • updated strings for new translations
Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3#
4# This file is part of Contribute, a plugin for Dotclear 2
5# Copyright (C) 2008,2009,2010 Moe (http://gniark.net/)
6#
7# Contribute is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License v2.0
9# as published by the Free Software Foundation.
10#
11# Contribute is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, see <http://www.gnu.org/licenses/>.
18#
19# Icon (icon.png) is from Silk Icons :
20# <http://www.famfamfam.com/lab/icons/silk/>
21#
22# ***** END LICENSE BLOCK *****
23
24if (!defined('DC_RC_PATH')) {return;}
25
26/**
27@ingroup Contribute
28@brief Document
29*/
30class contributeDocument extends dcUrlHandlers
31{
32     /**
33     serve the document
34     @param    args <b>string</b>  Argument
35     */
36     public static function page($args)
37     {
38          # from /dotclear/inc/public/lib.urlhandlers.php
39          # Spam trap
40          if (!empty($_POST['f_mail']))
41          {
42               http::head(412,'Precondition Failed');
43               header('Content-Type: text/plain');
44               echo "So Long, and Thanks For All the Fish";
45               exit;
46          }
47          # /from /dotclear/inc/public/lib.urlhandlers.php
48         
49          global $core;
50         
51          $settings =& $core->blog->settings;
52
53          if (!$settings->contribute_active)
54          {
55               self::p404();
56               return;
57          }
58         
59          $_ctx =& $GLOBALS['_ctx'];
60         
61          $_ctx->contribute = new ArrayObject();
62          $_ctx->contribute->help = @base64_decode($settings->contribute_help);
63          $_ctx->contribute->message = '';
64          $_ctx->contribute->preview = false;
65          $_ctx->contribute->form = true;
66          $_ctx->contribute->choose_format = false;
67          # selected tags
68          $_ctx->contribute->selected_tags = array();
69         
70          # Metadata
71          if ($core->plugins->moduleExists('metadata'))
72          {
73               $meta = new dcMeta($core);
74          }
75          else
76          {
77               $meta = false;
78          }
79         
80          # My Meta
81          if ($core->plugins->moduleExists('mymeta')
82               && ($settings->contribute_allow_mymeta))
83          {
84               $mymeta_values = array();
85         
86               $_ctx->contribute->mymeta = new myMeta($core);
87               
88               if ($_ctx->contribute->mymeta->hasMeta())
89               {
90                    $mymeta_values = @unserialize(@base64_decode(
91                         $settings->contribute_mymeta_values));
92                   
93                    if (!is_array($mymeta_values)) {$mymeta_values = array();}
94                   
95                    $mymeta = array();
96                   
97                    foreach ($_ctx->contribute->mymeta->getAll() as $k => $v)
98                    {
99                         if (((bool) $v->enabled) && in_array($k,$mymeta_values))
100                         {
101                              $mymeta[] = $k;
102                         }
103                    }
104                   
105                    unset($mymeta_values);
106               }
107               else
108               {
109                    $_ctx->contribute->mymeta = false;
110               }
111          }
112          else
113          {
114               $_ctx->contribute->mymeta = false;
115          }
116          # /My Meta
117         
118          # name, mail and site of the contributor
119         
120          $name = '';
121          $mail = '';
122          $site = '';
123         
124          # inspired by contactMe/_public.php
125          if ($args == 'sent')
126          {
127               $_ctx->contribute->message = 'sent';
128               $_ctx->contribute->preview = false;
129               $_ctx->contribute->form = false;
130               # avoid error with <tpl:ContributeIf format="xhtml">
131               $_ctx->posts = new ArrayObject();
132               $_ctx->posts->post_format = '';
133          }
134          else
135          {
136               try
137               {
138                    # default post
139                    $default_post = $settings->contribute_default_post;
140                    if (is_int($default_post) && ($default_post > 0))
141                    {
142                         # get default post
143                         $_ctx->posts = $core->auth->sudo(array($core->blog,'getPosts'),
144                              array('post_id' => $default_post));
145                         
146                         if ($_ctx->posts->isEmpty())
147                         {
148                              throw new Exception(__('No default post.'));
149                         }
150                         
151                         # modify $_ctx->posts for preview
152                         $post =& $_ctx->posts;
153                         
154                         # tags
155                         # remove selected tags
156                         $post_meta = unserialize($_ctx->posts->post_meta);
157                         
158                         if (isset($post_meta['tag']) && !empty($post_meta['tag']))
159                         {
160                              foreach ($post_meta['tag'] as $k => $tag)
161                              {
162                                        $_ctx->contribute->selected_tags[] = $tag;
163                              }
164                         }
165                         
166                         # My Meta
167                         $post->mymeta = array();
168                         
169                         if ($_ctx->contribute->mymeta !== false)
170                         {
171                              foreach ($mymeta as $k => $v)
172                              {
173                                   $post->mymeta[$v] = $meta->getMetaStr($post->post_meta,$v);
174                              }
175                         }
176                         # /My Meta
177                    }
178                    # empty post
179                    else
180                    {
181                         # create empty fields from default columns
182                         $record = array();
183                         $empty_post = $core->auth->sudo(array($core->blog,'getPosts'),
184                              array('post_id' => -1));
185                         
186                         $record[0] = array();
187                         
188                         foreach ($empty_post->columns() as $k => $v)
189                         {
190                              $record[0][$v] = '';
191                         }
192                         $_ctx->posts = staticRecord::newFromArray($record);
193                         
194                         unset($empty_post,$record);
195                         
196                         $_ctx->posts->core = $core;
197                         $_ctx->posts->extend('rsExtPost');
198                         
199                         # --BEHAVIOR-- coreBlogGetPosts
200                         $core->callBehavior('coreBlogGetPosts',$_ctx->posts);
201                         
202                         # modify $_ctx->posts for preview
203                         $post =& $_ctx->posts;
204                         
205                         # My Meta
206                         $post->mymeta = array();
207                         # /My Meta
208                         
209                         # formats
210                         # default format setting
211                         $post->post_format = $settings->contribute_format;
212                         
213                         # contributor can choose the post format,
214                         # it overrides the default format
215                         if ($settings->contribute_format == '')
216                         
217                         {
218                              $_ctx->contribute->choose_format = true;
219                             
220                              if ((isset($_POST['post_format']))
221                                   && in_array($_POST['post_format'],$core->getFormaters()))
222                              {
223                                   $post->post_format = $_POST['post_format'];
224                              }
225                         }
226                    }
227                   
228                    unset($default_post);
229                   
230                    # formats
231                    $formaters_combo = array();
232                    # Formaters combo
233                    foreach ($core->getFormaters() as $v)
234                    {
235                         $formaters_combo[] = array('format' => $v);
236                    }
237                    $_ctx->contribute->formaters =
238                              staticRecord::newFromArray($formaters_combo);
239                             
240                    unset($formaters_combo);
241                   
242                    # current date
243                    $post->post_dt = dt::str('%Y-%m-%d %T',null,
244                         $settings->blog_timezone);
245                    # remove URL
246                    $post->post_url = '';
247                   
248                    if (isset($_POST['post_title']))
249                    {
250                         $post->post_title = $_POST['post_title'];
251                    }
252                   
253                    # excerpt
254                    if (($settings->contribute_allow_excerpt)
255                         && (isset($_POST['post_excerpt'])))
256                    {
257                         $post->post_excerpt = $_POST['post_excerpt'];
258                    }
259                    # content
260                    if (isset($_POST['post_content']))
261                    {
262                         $post->post_content = $_POST['post_content'];
263                    }
264                   
265                    # avoid Notice: Indirect modification of overloaded property
266                    # record::$post_excerpt has no effect in .../contribute/_public.php
267                    # on line 146
268                    $post_excerpt = $post->post_excerpt;
269                    $post_excerpt_xhtml = $post->post_excerpt_xhtml;
270                    $post_content = $post->post_content;
271                    $post_content_xhtml = $post->post_content_xhtml;
272                   
273                    # HTML filter
274                    # get the setting value
275                    $enable_html_filter = $settings->enable_html_filter;
276                    # set the setting to true
277                    $settings->enable_html_filter = true;
278                   
279                    $core->blog->setPostContent(
280                         '',$post->post_format,$settings->lang,
281                         $post_excerpt,$post_excerpt_xhtml,
282                         $post_content,$post_content_xhtml
283                    );
284                   
285                    # set the old value to the setting
286                    $settings->enable_html_filter = $enable_html_filter;
287                    unset($enable_html_filter);
288                   
289                    $post->post_excerpt = $post_excerpt;
290                    $post->post_excerpt_xhtml = $post_excerpt_xhtml;
291                    $post->post_content = $post_content;
292                    $post->post_content_xhtml = $post_content_xhtml;
293                   
294                    unset($post_excerpt,$post_excerpt_xhtml,$post_content,
295                         $post_content_xhtml);
296                   
297                    if ($_ctx->contribute->choose_format
298                         && (isset($_POST['convert-xhtml']))
299                         && ($post->post_format != 'xhtml'))
300                    {
301                         $post->post_excerpt = $post->post_excerpt_xhtml;
302                         $post->post_content = $post->post_content_xhtml;
303                         $post->post_format = 'xhtml';
304                    }
305                   
306                    $_ctx->formaters = new ArrayObject;
307                    $_ctx->formaters->format = $post->post_format;
308                   
309                    # category
310                    if (($settings->contribute_allow_category)
311                         && (isset($_POST['cat_id'])))
312                    {
313                         # check category
314                         if (($_POST['cat_id'] != '')
315                              && (!preg_match('/^[0-9]+$/',$_POST['cat_id'])))
316                         {
317                              throw new Exception(__('Invalid cat_id'));
318                         }
319                         
320                         $cat = $core->blog->getCategories(array(
321                              'start' => $_POST['cat_id'],
322                              'level' => 1,
323                              'cat_id' => $_POST['cat_id']
324                         ));
325                         
326                         while ($cat->fetch())
327                         {
328                              # set category
329                              $post->cat_id = $cat->cat_id;
330                              $post->cat_title = $cat->cat_title;
331                              $post->cat_url = $cat->cat_url;
332                              break;
333                         }
334                         
335                         unset($cat);
336                    }
337                    # /category
338                   
339                    if ($meta !== false)
340                    {
341                         # tags
342                         if (($settings->contribute_allow_tags)
343                              && (isset($_POST['post_tags'])))
344                         {
345                              $post_meta = unserialize($_ctx->posts->post_meta);
346                             
347                              # remove post tags
348                              unset($post_meta['tag']);
349                             
350                              # get all the existing tags
351                              $available_tags = contribute::getTags();
352                             
353                              # set post tags
354                              # from /dotclear/plugins/metadata/_admin.php
355                              foreach ($meta->splitMetaValues($_POST['post_tags'])
356                                   as $k => $tag)
357                              {
358                              # /from /dotclear/plugins/metadata/_admin.php
359                                   $tag = dcMeta::sanitizeMetaID($tag);
360                                   
361                                   if ($settings->contribute_allow_new_tags)
362                                   {
363                                        $post_meta['tag'][] = $tag;
364                                        $_ctx->contribute->selected_tags[] = $tag;
365                                   }
366                                   else
367                                   {
368                                        # check that this tag already exists
369                                        if (in_array($tag,$available_tags))
370                                        {
371                                             $post_meta['tag'][] = $tag;
372                                             $_ctx->contribute->selected_tags[] = $tag;
373                                        }
374                                   }
375                              }
376                             
377                              unset($available_tags);
378                             
379                              $_ctx->posts->post_meta = serialize($post_meta);
380                              unset($post_meta);
381                             
382                         }
383                         
384                         # My Meta
385                         if ($_ctx->contribute->mymeta !== false)
386                         {
387                              foreach ($mymeta as $k => $v)
388                              {
389                                   $post->mymeta[$v] = (isset($_POST['mymeta_'.$v])
390                                        ? $_POST['mymeta_'.$v] : '');
391                              }
392                         }
393                         # /My Meta
394                    }
395                   
396                    # notes
397                    if (($settings->contribute_allow_notes)
398                         && (isset($_POST['post_notes'])))
399                    {
400                         $post->post_notes = $_POST['post_notes'];
401                    }
402                   
403                    # author
404                    if (($meta !== false)
405                         && ($settings->contribute_allow_author))
406                    {
407                         $post_meta = unserialize($_ctx->posts->post_meta);
408                         
409                         if (isset($_POST['c_name']) && (!empty($_POST['c_name'])))
410                         {
411                              $post_meta['contribute_author'][] = $name = $_POST['c_name'];
412                         }
413                         if (isset($_POST['c_mail']) && (!empty($_POST['c_mail'])))
414                         {
415                              $post_meta['contribute_mail'][] = $mail = $_POST['c_mail'];
416                         }
417                         # inspired by dcBlog > getCommentCursor()
418                         if (isset($_POST['c_site']) && (!empty($_POST['c_site'])))
419                         {
420                              $site = $_POST['c_site'];
421                             
422                              if (!preg_match('|^http(s?)://|',$site))
423                              {
424                                   $site = 'http://'.$site;
425                              }
426                              # /inspired by dcBlog > getCommentCursor()
427                             
428                              $post_meta['contribute_site'][] = $site;
429                         }
430                         
431                         $_ctx->posts->post_meta = serialize($post_meta);
432                         unset($post_meta);
433                    }
434                   
435                    # check some inputs
436                    if ((isset($_POST['preview'])) || (isset($_POST['add'])))
437                    {
438                         # these fields can't be empty
439                         if (isset($_POST['post_content']) && empty($post->post_content))
440                         {
441                              throw new Exception(__('No entry content'));
442                         } elseif (isset($_POST['post_title']) && empty($post->post_title))
443                         {
444                              throw new Exception(__('No entry title'));
445                         }
446                         
447                         # if name and email address are required
448                         if (($settings->contribute_allow_author)
449                              && ($settings->contribute_require_name_email))
450                         {
451                              if (empty($name))
452                              {
453                                   $_ctx->contribute->preview = false;
454                                   $_ctx->contribute->message = '';
455                                   throw new Exception(__('You must provide an author name'));
456                              } elseif (!text::isEmail($mail))
457                              {
458                                   $_ctx->contribute->preview = false;
459                                   $_ctx->contribute->message = '';
460                                   throw new Exception(
461                                        __('You must provide a valid email address.'));
462                              }
463                         }
464                    }
465                   
466                    if (isset($_POST['preview']))
467                    { 
468                         $_ctx->contribute->preview = true;
469                         $_ctx->contribute->message = 'preview';
470                    }
471                   
472                    if (isset($_POST) && empty($_POST))
473                    {
474                         $_ctx->contribute->preview = false;
475                         $_ctx->contribute->message = '';
476                    }
477                   
478                    if (isset($_POST['add']))
479                    {
480                         # log in as the user
481                         # usage OR contentadmin permission is needed
482                         $core->auth->checkUser($settings->contribute_user);
483                         
484                         if (!$core->auth->check('usage,contentadmin',$core->blog->id))
485                         {
486                              throw new Exception(
487                                   __('The user is not allowed to create an entry'));
488                         }
489                         
490                         $post_status = 0;
491                         
492                         # antispam
493                         if ($settings->contribute_enable_antispam
494                              && $core->plugins->moduleExists('antispam'))
495                         {
496                              $cur = $core->con->openCursor($core->prefix.'comment');
497                             
498                              $cur->comment_trackback = 0;
499                              $cur->comment_author = $name;
500                              $cur->comment_email = $mail;
501                              $cur->comment_site = $site;
502                              $cur->comment_ip = http::realIP();
503                              $cur->comment_content = $post->post_excerpt."\n".
504                                   $post->post_content;
505                              $cur->post_id = $core->con->select(
506                              'SELECT MAX(post_id) '.
507                              'FROM '.$core->prefix.'post ')->f(0) + 1;
508                              $cur->comment_status = 0;
509                             
510                              $is_spam = contributeAntispam::isSpam($cur);
511                             
512                              if ($is_spam === true)
513                              {
514                                   $post_status = -2;
515                                   
516                                   # if the auto deletion is enable, don't save the post and exit
517                                   # empty() doesn't work with $cur->comment_content
518                                   $comment_content = $cur->comment_content;
519                                   if (empty($comment_content))
520                                   {
521                                        http::redirect($core->blog->url.
522                                             $core->url->getBase('contribute').'/sent');
523                                   }
524                                   unset($comment_content,$cur);
525                              }   
526                         }
527                         # /antispam
528                         
529                         $cur = $core->con->openCursor($core->prefix.'post');
530                         
531                         $cur->user_id = $core->auth->userID();
532                         $cur->cat_id = ((empty($post->cat_id)) ? NULL : $post->cat_id);
533                         $cur->post_dt = $post->post_dt;
534                         $cur->post_status = $post_status;
535                         $cur->post_title = $post->post_title;
536                         $cur->post_format = $post->post_format;
537                         $cur->post_excerpt = $post->post_excerpt;
538                         $cur->post_content = $post->post_content;
539                         $cur->post_notes = $post->post_notes;
540                         $cur->post_lang = $core->auth->getInfo('user_lang');
541                         $cur->post_open_comment = (integer) $settings->allow_comments;
542                         $cur->post_open_tb = (integer) $settings->allow_trackbacks;
543                         
544                         unset($post_status);
545                         
546                         # --BEHAVIOR-- publicBeforePostCreate
547                         $core->callBehavior('publicBeforePostCreate',$cur);
548                         
549                         $post_id = $core->blog->addPost($cur);
550                         
551                         # l10n from $core->blog->addPost();
552                         __('You are not allowed to create an entry');
553                         
554                         # --BEHAVIOR-- publicAfterPostCreate
555                         $core->callBehavior('publicAfterPostCreate',$cur,$post_id);
556                         
557                         if ($meta !== false)
558                         {
559                              # inspired by planet/insert_feeds.php
560                              if (!empty($name))
561                              {
562                                   $meta->setPostMeta($post_id,'contribute_author',$name);
563                              }
564                              if (!empty($mail))
565                              {
566                                   $meta->setPostMeta($post_id,'contribute_mail',$mail);
567                              }
568                              if (!empty($site))
569                              {
570                                   $meta->setPostMeta($post_id,'contribute_site',$site);
571                              }
572                             
573                              # tags
574                              $post_meta = unserialize($_ctx->posts->post_meta);
575                              if (($settings->contribute_allow_tags)
576                                   && (isset($post_meta['tag']))
577                                   && (is_array($post_meta['tag'])))
578                              {
579                                   foreach ($post_meta['tag'] as $k => $tag)
580                                   {
581                                        # from /dotclear/plugins/metadata/_admin.php
582                                        $meta->setPostMeta($post_id,'tag',$tag);
583                                   }
584                                   
585                                   unset($post_meta);
586                              }
587                              # /tags
588                             
589                              # My Meta
590                              if ($_ctx->contribute->mymeta !== false)
591                              {                                 
592                                   foreach ($post->mymeta as $k => $v)
593                                   {
594                                        $meta->setPostMeta($post_id,$k,$v);
595                                   }
596                              }
597                              # /My Meta
598                         }
599                         
600                         # send email notification
601                         if ($settings->contribute_email_notification)
602                         {
603                              $headers = array(
604                                   'From: '.'dotclear@'.$_SERVER['HTTP_HOST'],
605                                   'MIME-Version: 1.0',
606                                   'Content-Type: text/plain; charset=UTF-8;',
607                                   'X-Mailer: Dotclear'
608                              );
609                             
610                              $subject = sprintf(__('New post submitted on %s'),
611                                   $core->blog->name);
612                             
613                              $content = sprintf(__('Title: %s'),$post->post_title);
614                              $content .= "\n\n";
615                             
616                              if (!empty($name))
617                              {
618                                   $content .= sprintf(__('Author: %s'),$name);
619                                   $content .= "\n\n";
620                              }
621                             
622                              if (!empty($mail))
623                              {
624                                   $headers[] = 'Reply-To: '.$mail;
625                                   
626                                   $content .= sprintf(__('Email address: %s'),$mail);
627                                   $content .= "\n\n";
628                              }
629                             
630                              $params = array();
631                              $params['post_id'] = $post_id;
632                             
633                              $post = $core->blog->getPosts($params);
634
635                              $content .= __('URL:').' '.$post->getURL();
636                              unset($post);
637                              $content .= "\n\n";
638                                   
639                              $content .= __('Edit this entry:').' '.DC_ADMIN_URL.
640                                   ((substr(DC_ADMIN_URL,-1) == '/') ? '' : '/').
641                                   'post.php?id='.$post_id.'&switchblog='.$core->blog->id;
642                              $content .= "\n\n".
643                                   __('You must log in on the backend before clicking on this link to go directly to the post.');
644                             
645                              foreach(explode(',',
646                                   $settings->contribute_email_notification)
647                                   as $to)
648                              {
649                                   $to = trim($to);
650                                   if (text::isEmail($to))
651                                   {
652                                        # don't display errors
653                                        try {
654                                             # from /dotclear/admin/auth.php : mail::B64Header($subject)
655                                             mail::sendMail($to,mail::B64Header($subject),
656                                                  wordwrap($content,70),$headers);
657                                        } catch (Exception $e) {}
658                                   }
659                              }
660                         }
661                         # /send email notification
662                         
663                         http::redirect($core->blog->url.
664                              $core->url->getBase('contribute').'/sent');
665                    }
666                   
667                    $_ctx->comment_preview = new ArrayObject();
668                    $_ctx->comment_preview['name'] = $name;
669                    $_ctx->comment_preview['mail'] = $mail;
670                    $_ctx->comment_preview['site'] = $site;
671               }
672               catch (Exception $e)
673               {
674                    $_ctx->form_error = $e->getMessage();
675               }
676          }
677         
678          $core->tpl->setPath($core->tpl->getPath(),
679               dirname(__FILE__).'/../default-templates/');
680         
681          self::serveDocument('contribute.html','text/html');
682     }
683}
684
685?>
Note: See TracBrowser for help on using the repository browser.

Sites map