Issue 8254: Length restriction for "title" and "uri"
[friendica.git/.git] / mod / share.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Database\DBA;
24 use Friendica\Model\Item;
25
26 function share_init(App $a) {
27         $post_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
28
29         if (!$post_id || !local_user()) {
30                 exit();
31         }
32
33         $fields = ['private', 'body', 'author-name', 'author-link', 'author-avatar',
34                 'guid', 'created', 'plink', 'title'];
35         $item = Item::selectFirst($fields, ['id' => $post_id]);
36
37         if (!DBA::isResult($item) || $item['private'] == Item::PRIVATE) {
38                 exit();
39         }
40
41         if (strpos($item['body'], "[/share]") !== false) {
42                 $pos = strpos($item['body'], "[share");
43                 $o = substr($item['body'], $pos);
44         } else {
45                 $o = share_header($item['author-name'], $item['author-link'], $item['author-avatar'], $item['guid'], $item['created'], $item['plink']);
46
47                 if ($item['title']) {
48                         $o .= '[h3]'.$item['title'].'[/h3]'."\n";
49                 }
50
51                 $o .= $item['body'];
52                 $o .= "[/share]";
53         }
54
55         echo $o;
56         exit();
57 }
58
59 /// @TODO Rewrite to handle over whole record array
60 function share_header($author, $profile, $avatar, $guid, $posted, $link) {
61         $header = "[share author='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $author).
62                 "' profile='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $profile).
63                 "' avatar='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $avatar);
64
65         if ($guid) {
66                 $header .= "' guid='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $guid);
67         }
68
69         if ($posted) {
70                 $header .= "' posted='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $posted);
71         }
72
73         $header .= "' link='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $link)."']";
74
75         return $header;
76 }