@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3abstract class PhameLiveController extends PhameController {
4
5 private $isExternal;
6 private $isLive;
7 private $blog;
8 private $post;
9
10 public function shouldAllowPublic() {
11 return true;
12 }
13
14 protected function getIsExternal() {
15 return $this->isExternal;
16 }
17
18 protected function getIsLive() {
19 return $this->isLive;
20 }
21
22 protected function getBlog() {
23 return $this->blog;
24 }
25
26 protected function getPost() {
27 return $this->post;
28 }
29
30 protected function setupLiveEnvironment() {
31 $request = $this->getRequest();
32 $viewer = $this->getViewer();
33
34 $site = $request->getSite();
35 $blog_id = $request->getURIData('blogID');
36 $post_id = $request->getURIData('id');
37
38 if ($site instanceof PhameBlogSite) {
39 // This is a live page on a custom domain. We already looked up the blog
40 // in the Site handler by examining the domain, so we don't need to do
41 // more lookups.
42
43 $blog = $site->getBlog();
44 $is_external = true;
45 $is_live = true;
46 } else if ($blog_id) {
47 // This is a blog detail view, an internal blog live view, or an
48 // internal post live view The internal post detail view is handled
49 // below.
50
51 $is_external = false;
52 if ($request->getURIData('live')) {
53 $is_live = true;
54 } else {
55 $is_live = false;
56 }
57
58 $blog_query = id(new PhameBlogQuery())
59 ->setViewer($viewer)
60 ->needProfileImage(true)
61 ->needHeaderImage(true)
62 ->withIDs(array($blog_id));
63
64 // If this is a live view, only show active blogs.
65 if ($is_live) {
66 $blog_query->withStatuses(
67 array(
68 PhameBlog::STATUS_ACTIVE,
69 ));
70 }
71
72 $blog = $blog_query->executeOne();
73 if (!$blog) {
74 $this->isExternal = $is_external;
75 $this->isLive = $is_live;
76 return new Aphront404Response();
77 }
78
79 } else {
80 // This is a post detail page, so we'll figure out the blog by loading
81 // the post first.
82 $is_external = false;
83 $is_live = false;
84 $blog = null;
85 }
86
87 $this->isExternal = $is_external;
88 $this->isLive = $is_live;
89
90 if (phutil_nonempty_string($post_id)) {
91 $post_query = id(new PhamePostQuery())
92 ->setViewer($viewer)
93 ->needHeaderImage(true)
94 ->withIDs(array($post_id));
95
96 // Only show published posts on external domains.
97 if ($is_external) {
98 $post_query->withVisibility(
99 array(PhameConstants::VISIBILITY_PUBLISHED));
100 }
101
102 $post = $post_query->executeOne();
103 if (!$post) {
104 // Not a valid Post
105 $this->blog = $blog;
106 return new Aphront404Response();
107 }
108
109 // If this is a post detail page, the URI didn't come with a blog ID,
110 // so fill that in.
111 if (!$blog) {
112 $blog = $post->getBlog();
113 }
114 } else {
115 $post = null;
116 }
117
118 $this->blog = $blog;
119 $this->post = $post;
120
121 // If we have a post, canonicalize the URI to the post's current slug and
122 // redirect the user if it isn't correct. Likewise, canonicalize the URI
123 // if the blog ID is wrong. See T13353.
124 if ($post) {
125 $slug = $request->getURIData('slug');
126
127 $wrong_slug = ($post->getSlug() !== $slug);
128 $wrong_blog = ($post->getBlog()->getID() !== $blog->getID());
129
130 if ($wrong_slug || $wrong_blog) {
131 if ($is_live) {
132 if ($is_external) {
133 $uri = $post->getExternalLiveURI();
134 } else {
135 $uri = $post->getInternalLiveURI();
136 }
137 } else {
138 $uri = $post->getViewURI();
139 }
140
141 $response = id(new AphrontRedirectResponse())
142 ->setURI($uri);
143
144 if ($is_external) {
145 $response->setIsExternal(true);
146 }
147
148 return $response;
149 }
150 }
151
152 return null;
153 }
154
155 protected function buildApplicationCrumbs() {
156 $blog = $this->getBlog();
157 $post = $this->getPost();
158
159 $is_live = $this->getIsLive();
160 $is_external = $this->getIsExternal();
161
162 // If this is an external view, don't put the "Phame" crumb or the
163 // "Blogs" crumb into the crumbs list.
164 if ($is_external) {
165 $crumbs = new PHUICrumbsView();
166 // Link back to parent site
167 if ($blog->getParentSite() && $blog->getParentDomain()) {
168 $crumbs->addTextCrumb(
169 $blog->getParentSite(),
170 $blog->getExternalParentURI());
171 }
172 } else {
173 $crumbs = parent::buildApplicationCrumbs();
174 $crumbs->addTextCrumb(
175 pht('Blogs'),
176 $this->getApplicationURI('blog/'));
177 }
178
179 $crumbs->setBorder(true);
180
181 if ($blog) {
182 if ($post) {
183 if ($is_live) {
184 if ($is_external) {
185 $blog_uri = $blog->getExternalLiveURI();
186 } else {
187 $blog_uri = $blog->getInternalLiveURI();
188 }
189 } else {
190 $blog_uri = $blog->getViewURI();
191 }
192 } else {
193 $blog_uri = null;
194 }
195
196 $crumbs->addTextCrumb($blog->getName(), $blog_uri);
197 }
198
199 if ($post) {
200 if (!$is_external) {
201 $crumbs->addTextCrumb('J'.$post->getID());
202 }
203 }
204
205 return $crumbs;
206 }
207
208 public function willSendResponse(AphrontResponse $response) {
209 if ($this->getIsExternal()) {
210 if ($response instanceof Aphront404Response) {
211 $page = $this->newPage()
212 ->setCrumbs($this->buildApplicationCrumbs());
213
214 $response = id(new Phame404Response())
215 ->setPage($page);
216 }
217 }
218
219 return parent::willSendResponse($response);
220 }
221
222 public function newPage() {
223 $page = parent::newPage();
224
225 if ($this->getIsLive()) {
226 $page
227 ->addClass('phame-live-view')
228 ->setShowChrome(false)
229 ->setShowFooter(false);
230 }
231
232 return $page;
233 }
234
235}