]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blog/classes/Blog_entry.php
Merge branch 'master' into social-master
[quix0rs-gnu-social.git] / plugins / Blog / classes / Blog_entry.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Data structure for blog entries
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Blog
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Data structure for blog entries
39  *
40  * @category  Blog
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2011 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47
48 class Blog_entry extends Managed_DataObject
49 {
50     public $__table = 'blog_entry';
51
52     public $id; // UUID
53     public $profile_id; // int
54     public $title; // varchar(255)
55     public $summary; // text
56     public $content; // text
57     public $uri; // text
58     public $url; // text
59     public $created; // datetime
60     public $modified; // datetime
61
62     const TYPE = ActivityObject::ARTICLE;
63
64     static function schemaDef()
65     {
66         return array(
67             'description' => 'lite blog entry',
68             'fields' => array(
69                 'id' => array('type' => 'char',
70                               'length' => 36,
71                               'not null' => true,
72                               'description' => 'Unique ID (UUID)'),
73                 'profile_id' => array('type' => 'int',
74                                       'not null' => true,
75                                       'description' => 'Author profile ID'),
76                 'title' => array('type' => 'varchar',
77                                  'length' => 255,
78                                  'description' => 'title of the entry'),
79                 'summary' => array('type' => 'text',
80                                    'description' => 'initial summary'),
81                 'content' => array('type' => 'text',
82                                    'description' => 'HTML content of the entry'),
83                 'uri' => array('type' => 'varchar',
84                                'length' => 255,
85                                'description' => 'URI (probably http://) for this entry'),
86                 'url' => array('type' => 'varchar',
87                                'length' => 255,
88                                'description' => 'URL (probably http://) for this entry'),
89                 'created' => array('type' => 'datetime',
90                                    'not null' => true,
91                                    'description' => 'date this record was created'),
92                 'modified' => array('type' => 'datetime',
93                                     'not null' => true,
94                                     'description' => 'date this record was created'),
95             ),
96             'primary key' => array('id'),
97             'unique keys' => array(
98                 'blog_entry_uri_key' => array('uri'),
99             ),
100             'indexes' => array(
101                 'blog_entry_profile_id_idx' => array('profile_id'),
102                 'blog_entry_created_idx' => array('created')
103             ),
104             'foreign keys' => array(
105                 'blog_entry_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
106             ),
107         );
108     }
109
110     static function saveNew(Profile $profile, $title, $content, array $options = array())
111     {
112         $be             = new Blog_entry();
113         $be->id         = (string) new UUID();
114         $be->profile_id = $profile->id;
115         $be->title      = $title; // Note: not HTML-protected
116         $be->content    = self::purify($content);
117
118         if (array_key_exists('summary', $options)) {
119             $be->summary = self::purify($options['summary']);
120         } else {
121             // Already purified
122             $be->summary = self::summarize($be->content);
123         }
124
125         // Don't save an identical summary
126
127         if ($be->summary == $be->content) {
128             $be->summary = null;
129         }
130
131         $url = common_local_url('showblogentry', array('id' => $be->id));
132
133         if (!array_key_exists('uri', $options)) {
134             $options['uri'] = $url;
135         } 
136
137         $be->uri = $options['uri'];
138
139         if (!array_key_exists('url', $options)) {
140             $options['url'] = $url;
141         }
142
143         $be->url = $options['url'];
144
145         if (!array_key_exists('created', $options)) {
146             $be->created = common_sql_now();
147         }
148         
149         $be->created = $options['created'];
150
151         $be->modified = common_sql_now();
152
153         $be->insert();
154
155         // Use user's preferences for short URLs, if possible
156
157         try {
158             $user = $profile->isLocal()
159                         ? $profile->getUser()
160                         : null;
161             $shortUrl = File_redirection::makeShort($url, $user);
162         } catch (Exception $e) {
163             // Don't let this stop us.
164             $shortUrl = $url;
165         }
166
167         // XXX: this might be too long.
168
169         if (!empty($be->summary)) {
170             $options['rendered'] = $be->summary . ' ' . 
171                 XMLStringer::estring('a', array('href' => $url,
172                                                 'class' => 'blog-entry'),
173                                      _('More...'));
174             $text = common_strip_html($be->summary);
175         } else {
176             $options['rendered'] = $be->content;
177             $text = common_strip_html($be->content);
178         }
179
180
181         if (Notice::contentTooLong($text)) {
182             $text = substr($text, 0, Notice::maxContent() - mb_strlen($shortUrl) - 2) .
183                 '… ' . $shortUrl;
184         }
185
186         // Override this no matter what.
187         
188         $options['object_type'] = self::TYPE;
189
190         $source = array_key_exists('source', $options) ?
191                                     $options['source'] : 'web';
192         
193         $saved = Notice::saveNew($profile->id, $text, $source, $options);
194
195         return $saved;
196     }
197
198     /**
199      * Summarize the contents of a blog post
200      *
201      * We take the first div or paragraph of the blog post if there's a hit;
202      * Otherwise we take the whole thing.
203      * 
204      * @param string $html HTML of full content
205      */
206     static function summarize($html)
207     {
208         if (preg_match('#<p>.*?</p>#s', $html, $matches)) {
209             return $matches[0];
210         } else if (preg_match('#<div>.*?</div>#s', $html, $matches)) {
211             return $matches[0];
212         } else {
213             return $html;
214         }
215     }
216
217     static function fromNotice(Notice $notice)
218     {
219         return Blog_entry::getKV('uri', $notice->uri);
220     }
221
222     function getNotice()
223     {
224         return Notice::getKV('uri', $this->uri);
225     }
226
227     function asActivityObject()
228     {
229         $obj = new ActivityObject();
230
231         $obj->id      = $this->uri;
232         $obj->type    = self::TYPE;
233         $obj->title   = $this->title;
234         $obj->summary = $this->summary;
235         $obj->content = $this->content;
236         $obj->link    = $this->url;
237
238         return $obj;
239     }
240
241     /**
242      * Clean up input HTML
243      */
244     static function purify($html)
245     {
246         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
247
248         $config = array('safe' => 1,
249                         'deny_attribute' => 'id,style,on*');
250         $pure = htmLawed($html, $config);
251
252         return $pure;
253     }
254 }