Dotclear

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

Revision 3034, 18.4 KB checked in by Moe, 12 years ago (diff)

Contribute 1.0-alpha32: removed tests for Dotclear < 2.2

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

Sites map