2011-06-20 14:11:35 +09:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2011, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* Data structure for blog entries
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Blog
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2011 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
// This check helps protect against security problems;
|
|
|
|
// your code file can't be executed directly from the web.
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data structure for blog entries
|
|
|
|
*
|
|
|
|
* @category Blog
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2011 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
2011-06-20 23:20:01 +09:00
|
|
|
class Blog_entry extends Managed_DataObject
|
2011-06-20 14:11:35 +09:00
|
|
|
{
|
|
|
|
public $__table = 'blog_entry';
|
|
|
|
|
|
|
|
public $id; // UUID
|
|
|
|
public $profile_id; // int
|
2015-02-13 02:18:55 +09:00
|
|
|
public $title; // varchar(191) not 255 because utf8mb4 takes more space
|
2011-06-20 14:11:35 +09:00
|
|
|
public $summary; // text
|
|
|
|
public $content; // text
|
2015-02-13 02:18:55 +09:00
|
|
|
public $uri; // varchar(191) not 255 because utf8mb4 takes more space
|
|
|
|
public $url; // varchar(191) not 255 because utf8mb4 takes more space
|
2011-06-20 14:11:35 +09:00
|
|
|
public $created; // datetime
|
|
|
|
public $modified; // datetime
|
|
|
|
|
2011-06-20 23:48:02 +09:00
|
|
|
const TYPE = ActivityObject::ARTICLE;
|
2011-06-20 14:11:35 +09:00
|
|
|
|
|
|
|
static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'description' => 'lite blog entry',
|
|
|
|
'fields' => array(
|
|
|
|
'id' => array('type' => 'char',
|
|
|
|
'length' => 36,
|
|
|
|
'not null' => true,
|
|
|
|
'description' => 'Unique ID (UUID)'),
|
|
|
|
'profile_id' => array('type' => 'int',
|
|
|
|
'not null' => true,
|
|
|
|
'description' => 'Author profile ID'),
|
|
|
|
'title' => array('type' => 'varchar',
|
2015-02-13 02:18:55 +09:00
|
|
|
'length' => 191,
|
2011-06-20 14:11:35 +09:00
|
|
|
'description' => 'title of the entry'),
|
|
|
|
'summary' => array('type' => 'text',
|
|
|
|
'description' => 'initial summary'),
|
|
|
|
'content' => array('type' => 'text',
|
|
|
|
'description' => 'HTML content of the entry'),
|
|
|
|
'uri' => array('type' => 'varchar',
|
2015-02-13 02:18:55 +09:00
|
|
|
'length' => 191,
|
2011-06-20 14:11:35 +09:00
|
|
|
'description' => 'URI (probably http://) for this entry'),
|
|
|
|
'url' => array('type' => 'varchar',
|
2015-02-13 02:18:55 +09:00
|
|
|
'length' => 191,
|
2011-06-20 14:11:35 +09:00
|
|
|
'description' => 'URL (probably http://) for this entry'),
|
|
|
|
'created' => array('type' => 'datetime',
|
|
|
|
'not null' => true,
|
|
|
|
'description' => 'date this record was created'),
|
|
|
|
'modified' => array('type' => 'datetime',
|
|
|
|
'not null' => true,
|
|
|
|
'description' => 'date this record was created'),
|
|
|
|
),
|
|
|
|
'primary key' => array('id'),
|
2011-07-05 04:58:56 +09:00
|
|
|
'unique keys' => array(
|
|
|
|
'blog_entry_uri_key' => array('uri'),
|
|
|
|
),
|
2011-06-20 14:11:35 +09:00
|
|
|
'indexes' => array(
|
2014-08-18 22:16:22 +09:00
|
|
|
'blog_entry_profile_id_idx' => array('profile_id'),
|
2011-07-05 04:58:56 +09:00
|
|
|
'blog_entry_created_idx' => array('created')
|
2011-06-20 14:11:35 +09:00
|
|
|
),
|
2014-08-18 22:16:22 +09:00
|
|
|
'foreign keys' => array(
|
|
|
|
'blog_entry_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
|
|
|
|
),
|
2011-06-20 14:11:35 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function saveNew($profile, $title, $content, $options=null)
|
|
|
|
{
|
|
|
|
if (is_null($options)) {
|
|
|
|
$options = array();
|
|
|
|
}
|
2011-06-21 00:28:46 +09:00
|
|
|
|
2011-06-20 23:20:01 +09:00
|
|
|
$be = new Blog_entry();
|
2011-06-20 14:11:35 +09:00
|
|
|
$be->id = (string) new UUID();
|
|
|
|
$be->profile_id = $profile->id;
|
2011-06-21 00:01:50 +09:00
|
|
|
$be->title = $title; // Note: not HTML-protected
|
|
|
|
$be->content = self::purify($content);
|
2011-06-21 00:28:46 +09:00
|
|
|
|
2011-06-20 14:11:35 +09:00
|
|
|
if (array_key_exists('summary', $options)) {
|
2011-06-21 00:01:50 +09:00
|
|
|
$be->summary = self::purify($options['summary']);
|
2011-06-20 14:11:35 +09:00
|
|
|
} else {
|
2011-06-21 00:01:50 +09:00
|
|
|
// Already purified
|
2011-06-21 00:28:46 +09:00
|
|
|
$be->summary = self::summarize($be->content);
|
2011-06-20 14:11:35 +09:00
|
|
|
}
|
|
|
|
|
2011-06-20 23:26:24 +09:00
|
|
|
// Don't save an identical summary
|
|
|
|
|
|
|
|
if ($be->summary == $be->content) {
|
|
|
|
$be->summary = null;
|
|
|
|
}
|
|
|
|
|
2011-06-20 14:11:35 +09:00
|
|
|
$url = common_local_url('showblogentry', array('id' => $be->id));
|
|
|
|
|
|
|
|
if (!array_key_exists('uri', $options)) {
|
|
|
|
$options['uri'] = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$be->uri = $options['uri'];
|
|
|
|
|
|
|
|
if (!array_key_exists('url', $options)) {
|
|
|
|
$options['url'] = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$be->url = $options['url'];
|
|
|
|
|
|
|
|
if (!array_key_exists('created', $options)) {
|
|
|
|
$be->created = common_sql_now();
|
|
|
|
}
|
|
|
|
|
|
|
|
$be->created = $options['created'];
|
|
|
|
|
|
|
|
$be->modified = common_sql_now();
|
|
|
|
|
|
|
|
$be->insert();
|
|
|
|
|
|
|
|
// Use user's preferences for short URLs, if possible
|
|
|
|
|
|
|
|
try {
|
2013-09-10 04:35:16 +09:00
|
|
|
$user = $profile->isLocal()
|
|
|
|
? $profile->getUser()
|
|
|
|
: null;
|
|
|
|
$shortUrl = File_redirection::makeShort($url, $user);
|
2011-06-20 14:11:35 +09:00
|
|
|
} catch (Exception $e) {
|
|
|
|
// Don't let this stop us.
|
|
|
|
$shortUrl = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: this might be too long.
|
|
|
|
|
2011-06-20 23:48:02 +09:00
|
|
|
if (!empty($be->summary)) {
|
|
|
|
$options['rendered'] = $be->summary . ' ' .
|
|
|
|
XMLStringer::estring('a', array('href' => $url,
|
|
|
|
'class' => 'blog-entry'),
|
|
|
|
_('More...'));
|
2014-07-14 20:52:23 +09:00
|
|
|
$text = common_strip_html($be->summary);
|
2011-06-20 23:48:02 +09:00
|
|
|
} else {
|
2011-06-21 00:01:50 +09:00
|
|
|
$options['rendered'] = $be->content;
|
2014-07-14 20:52:23 +09:00
|
|
|
$text = common_strip_html($be->content);
|
2011-06-20 23:48:02 +09:00
|
|
|
}
|
2011-06-20 14:11:35 +09:00
|
|
|
|
|
|
|
|
2011-06-21 00:28:46 +09:00
|
|
|
if (Notice::contentTooLong($text)) {
|
|
|
|
$text = substr($text, 0, Notice::maxContent() - mb_strlen($shortUrl) - 2) .
|
2011-06-20 14:11:35 +09:00
|
|
|
'… ' . $shortUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override this no matter what.
|
|
|
|
|
|
|
|
$options['object_type'] = self::TYPE;
|
|
|
|
|
|
|
|
$source = array_key_exists('source', $options) ?
|
|
|
|
$options['source'] : 'web';
|
|
|
|
|
2011-06-21 00:28:46 +09:00
|
|
|
$saved = Notice::saveNew($profile->id, $text, $source, $options);
|
2011-06-20 23:48:02 +09:00
|
|
|
|
|
|
|
return $saved;
|
2011-06-20 14:11:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Summarize the contents of a blog post
|
|
|
|
*
|
|
|
|
* We take the first div or paragraph of the blog post if there's a hit;
|
|
|
|
* Otherwise we take the whole thing.
|
|
|
|
*
|
|
|
|
* @param string $html HTML of full content
|
|
|
|
*/
|
|
|
|
static function summarize($html)
|
|
|
|
{
|
|
|
|
if (preg_match('#<p>.*?</p>#s', $html, $matches)) {
|
|
|
|
return $matches[0];
|
|
|
|
} else if (preg_match('#<div>.*?</div>#s', $html, $matches)) {
|
|
|
|
return $matches[0];
|
|
|
|
} else {
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static function fromNotice($notice)
|
|
|
|
{
|
2013-08-18 20:04:58 +09:00
|
|
|
return Blog_entry::getKV('uri', $notice->uri);
|
2011-06-20 14:11:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function getNotice()
|
|
|
|
{
|
2013-08-18 20:04:58 +09:00
|
|
|
return Notice::getKV('uri', $this->uri);
|
2011-06-20 14:11:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function asActivityObject()
|
|
|
|
{
|
|
|
|
$obj = new ActivityObject();
|
|
|
|
|
|
|
|
$obj->id = $this->uri;
|
|
|
|
$obj->type = self::TYPE;
|
|
|
|
$obj->title = $this->title;
|
|
|
|
$obj->summary = $this->summary;
|
|
|
|
$obj->content = $this->content;
|
|
|
|
$obj->link = $this->url;
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
2011-06-21 00:01:50 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up input HTML
|
|
|
|
*/
|
|
|
|
static function purify($html)
|
|
|
|
{
|
|
|
|
require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
|
2011-06-21 00:28:46 +09:00
|
|
|
|
2011-06-21 00:01:50 +09:00
|
|
|
$config = array('safe' => 1,
|
|
|
|
'deny_attribute' => 'id,style,on*');
|
2011-06-21 00:28:46 +09:00
|
|
|
$pure = htmLawed($html, $config);
|
|
|
|
|
|
|
|
return $pure;
|
2011-06-21 00:01:50 +09:00
|
|
|
}
|
2011-06-20 14:11:35 +09:00
|
|
|
}
|