]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blog/Blog_entry.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / Blog / 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     function staticGet($k, $v=null)
65     {
66         return Managed_DataObject::staticGet('Blog_entry', $k, $v);
67     }
68
69     static function schemaDef()
70     {
71         return array(
72             'description' => 'lite blog entry',
73             'fields' => array(
74                 'id' => array('type' => 'char',
75                               'length' => 36,
76                               'not null' => true,
77                               'description' => 'Unique ID (UUID)'),
78                 'profile_id' => array('type' => 'int',
79                                       'not null' => true,
80                                       'description' => 'Author profile ID'),
81                 'title' => array('type' => 'varchar',
82                                  'length' => 255,
83                                  'description' => 'title of the entry'),
84                 'summary' => array('type' => 'text',
85                                    'description' => 'initial summary'),
86                 'content' => array('type' => 'text',
87                                    'description' => 'HTML content of the entry'),
88                 'uri' => array('type' => 'varchar',
89                                'length' => 255,
90                                'description' => 'URI (probably http://) for this entry'),
91                 'url' => array('type' => 'varchar',
92                                'length' => 255,
93                                'description' => 'URL (probably http://) for this entry'),
94                 'created' => array('type' => 'datetime',
95                                    'not null' => true,
96                                    'description' => 'date this record was created'),
97                 'modified' => array('type' => 'datetime',
98                                     'not null' => true,
99                                     'description' => 'date this record was created'),
100             ),
101             'primary key' => array('id'),
102             'unique keys' => array(
103                 'blog_entry_uri_key' => array('uri'),
104             ),
105             'foreign keys' => array(
106                 'blog_entry_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
107             ),
108             'indexes' => array(
109                 'blog_entry_created_idx' => array('created')
110             ),
111         );
112     }
113
114     static function saveNew($profile, $title, $content, $options=null)
115     {
116         if (is_null($options)) {
117             $options = array();
118         }
119
120         $be             = new Blog_entry();
121         $be->id         = (string) new UUID();
122         $be->profile_id = $profile->id;
123         $be->title      = $title; // Note: not HTML-protected
124         $be->content    = self::purify($content);
125
126         if (array_key_exists('summary', $options)) {
127             $be->summary = self::purify($options['summary']);
128         } else {
129             // Already purified
130             $be->summary = self::summarize($be->content);
131         }
132
133         // Don't save an identical summary
134
135         if ($be->summary == $be->content) {
136             $be->summary = null;
137         }
138
139         $url = common_local_url('showblogentry', array('id' => $be->id));
140
141         if (!array_key_exists('uri', $options)) {
142             $options['uri'] = $url;
143         } 
144
145         $be->uri = $options['uri'];
146
147         if (!array_key_exists('url', $options)) {
148             $options['url'] = $url;
149         }
150
151         $be->url = $options['url'];
152
153         if (!array_key_exists('created', $options)) {
154             $be->created = common_sql_now();
155         }
156         
157         $be->created = $options['created'];
158
159         $be->modified = common_sql_now();
160
161         $be->insert();
162
163         // Use user's preferences for short URLs, if possible
164
165         try {
166             $user = $profile->getUser();
167             $shortUrl = File_redirection::makeShort($url,
168                                                     empty($user) ? null : $user);
169         } catch (Exception $e) {
170             // Don't let this stop us.
171             $shortUrl = $url;
172         }
173
174         // XXX: this might be too long.
175
176         if (!empty($be->summary)) {
177             $options['rendered'] = $be->summary . ' ' . 
178                 XMLStringer::estring('a', array('href' => $url,
179                                                 'class' => 'blog-entry'),
180                                      _('More...'));
181             $text = html_entity_decode(strip_tags($be->summary), ENT_QUOTES, 'UTF-8');
182         } else {
183             $options['rendered'] = $be->content;
184             $text = html_entity_decode(strip_tags($be->content), ENT_QUOTES, 'UTF-8');
185         }
186
187
188         if (Notice::contentTooLong($text)) {
189             $text = substr($text, 0, Notice::maxContent() - mb_strlen($shortUrl) - 2) .
190                 '… ' . $shortUrl;
191         }
192
193         // Override this no matter what.
194         
195         $options['object_type'] = self::TYPE;
196
197         $source = array_key_exists('source', $options) ?
198                                     $options['source'] : 'web';
199         
200         $saved = Notice::saveNew($profile->id, $text, $source, $options);
201
202         return $saved;
203     }
204
205     /**
206      * Summarize the contents of a blog post
207      *
208      * We take the first div or paragraph of the blog post if there's a hit;
209      * Otherwise we take the whole thing.
210      * 
211      * @param string $html HTML of full content
212      */
213     static function summarize($html)
214     {
215         if (preg_match('#<p>.*?</p>#s', $html, $matches)) {
216             return $matches[0];
217         } else if (preg_match('#<div>.*?</div>#s', $html, $matches)) {
218             return $matches[0];
219         } else {
220             return $html;
221         }
222     }
223
224     static function fromNotice($notice)
225     {
226         return Blog_entry::staticGet('uri', $notice->uri);
227     }
228
229     function getNotice()
230     {
231         return Notice::staticGet('uri', $this->uri);
232     }
233
234     function asActivityObject()
235     {
236         $obj = new ActivityObject();
237
238         $obj->id      = $this->uri;
239         $obj->type    = self::TYPE;
240         $obj->title   = $this->title;
241         $obj->summary = $this->summary;
242         $obj->content = $this->content;
243         $obj->link    = $this->url;
244
245         return $obj;
246     }
247
248     /**
249      * Clean up input HTML
250      */
251     static function purify($html)
252     {
253         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
254
255         $config = array('safe' => 1,
256                         'deny_attribute' => 'id,style,on*');
257         $pure = htmLawed($html, $config);
258
259         return $pure;
260     }
261 }