Blog_entry.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Data structure for blog entries
  37. *
  38. * @category Blog
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class Blog_entry extends Managed_DataObject
  46. {
  47. public $__table = 'blog_entry';
  48. public $id; // UUID
  49. public $profile_id; // int
  50. public $title; // varchar(255)
  51. public $summary; // text
  52. public $content; // text
  53. public $uri; // text
  54. public $url; // text
  55. public $created; // datetime
  56. public $modified; // datetime
  57. const TYPE = ActivityObject::ARTICLE;
  58. static function schemaDef()
  59. {
  60. return array(
  61. 'description' => 'lite blog entry',
  62. 'fields' => array(
  63. 'id' => array('type' => 'char',
  64. 'length' => 36,
  65. 'not null' => true,
  66. 'description' => 'Unique ID (UUID)'),
  67. 'profile_id' => array('type' => 'int',
  68. 'not null' => true,
  69. 'description' => 'Author profile ID'),
  70. 'title' => array('type' => 'varchar',
  71. 'length' => 255,
  72. 'description' => 'title of the entry'),
  73. 'summary' => array('type' => 'text',
  74. 'description' => 'initial summary'),
  75. 'content' => array('type' => 'text',
  76. 'description' => 'HTML content of the entry'),
  77. 'uri' => array('type' => 'varchar',
  78. 'length' => 255,
  79. 'description' => 'URI (probably http://) for this entry'),
  80. 'url' => array('type' => 'varchar',
  81. 'length' => 255,
  82. 'description' => 'URL (probably http://) for this entry'),
  83. 'created' => array('type' => 'datetime',
  84. 'not null' => true,
  85. 'description' => 'date this record was created'),
  86. 'modified' => array('type' => 'datetime',
  87. 'not null' => true,
  88. 'description' => 'date this record was created'),
  89. ),
  90. 'primary key' => array('id'),
  91. 'unique keys' => array(
  92. 'blog_entry_uri_key' => array('uri'),
  93. ),
  94. 'indexes' => array(
  95. 'blog_entry_profile_id_idx' => array('profile_id'),
  96. 'blog_entry_created_idx' => array('created')
  97. ),
  98. 'foreign keys' => array(
  99. 'blog_entry_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  100. ),
  101. );
  102. }
  103. static function saveNew($profile, $title, $content, $options=null)
  104. {
  105. if (is_null($options)) {
  106. $options = array();
  107. }
  108. $be = new Blog_entry();
  109. $be->id = (string) new UUID();
  110. $be->profile_id = $profile->id;
  111. $be->title = $title; // Note: not HTML-protected
  112. $be->content = self::purify($content);
  113. if (array_key_exists('summary', $options)) {
  114. $be->summary = self::purify($options['summary']);
  115. } else {
  116. // Already purified
  117. $be->summary = self::summarize($be->content);
  118. }
  119. // Don't save an identical summary
  120. if ($be->summary == $be->content) {
  121. $be->summary = null;
  122. }
  123. $url = common_local_url('showblogentry', array('id' => $be->id));
  124. if (!array_key_exists('uri', $options)) {
  125. $options['uri'] = $url;
  126. }
  127. $be->uri = $options['uri'];
  128. if (!array_key_exists('url', $options)) {
  129. $options['url'] = $url;
  130. }
  131. $be->url = $options['url'];
  132. if (!array_key_exists('created', $options)) {
  133. $be->created = common_sql_now();
  134. }
  135. $be->created = $options['created'];
  136. $be->modified = common_sql_now();
  137. $be->insert();
  138. // Use user's preferences for short URLs, if possible
  139. try {
  140. $user = $profile->isLocal()
  141. ? $profile->getUser()
  142. : null;
  143. $shortUrl = File_redirection::makeShort($url, $user);
  144. } catch (Exception $e) {
  145. // Don't let this stop us.
  146. $shortUrl = $url;
  147. }
  148. // XXX: this might be too long.
  149. if (!empty($be->summary)) {
  150. $options['rendered'] = $be->summary . ' ' .
  151. XMLStringer::estring('a', array('href' => $url,
  152. 'class' => 'blog-entry'),
  153. _('More...'));
  154. $text = common_strip_html($be->summary);
  155. } else {
  156. $options['rendered'] = $be->content;
  157. $text = common_strip_html($be->content);
  158. }
  159. if (Notice::contentTooLong($text)) {
  160. $text = substr($text, 0, Notice::maxContent() - mb_strlen($shortUrl) - 2) .
  161. '… ' . $shortUrl;
  162. }
  163. // Override this no matter what.
  164. $options['object_type'] = self::TYPE;
  165. $source = array_key_exists('source', $options) ?
  166. $options['source'] : 'web';
  167. $saved = Notice::saveNew($profile->id, $text, $source, $options);
  168. return $saved;
  169. }
  170. /**
  171. * Summarize the contents of a blog post
  172. *
  173. * We take the first div or paragraph of the blog post if there's a hit;
  174. * Otherwise we take the whole thing.
  175. *
  176. * @param string $html HTML of full content
  177. */
  178. static function summarize($html)
  179. {
  180. if (preg_match('#<p>.*?</p>#s', $html, $matches)) {
  181. return $matches[0];
  182. } else if (preg_match('#<div>.*?</div>#s', $html, $matches)) {
  183. return $matches[0];
  184. } else {
  185. return $html;
  186. }
  187. }
  188. static function fromNotice($notice)
  189. {
  190. return Blog_entry::getKV('uri', $notice->uri);
  191. }
  192. function getNotice()
  193. {
  194. return Notice::getKV('uri', $this->uri);
  195. }
  196. function asActivityObject()
  197. {
  198. $obj = new ActivityObject();
  199. $obj->id = $this->uri;
  200. $obj->type = self::TYPE;
  201. $obj->title = $this->title;
  202. $obj->summary = $this->summary;
  203. $obj->content = $this->content;
  204. $obj->link = $this->url;
  205. return $obj;
  206. }
  207. /**
  208. * Clean up input HTML
  209. */
  210. static function purify($html)
  211. {
  212. require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
  213. $config = array('safe' => 1,
  214. 'deny_attribute' => 'id,style,on*');
  215. $pure = htmLawed($html, $config);
  216. return $pure;
  217. }
  218. }