Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / phame / storage / PhameBlog.php
bloba8ba53ae156ec3f11a2ddeaa306510202460a168
1 <?php
3 final class PhameBlog extends PhameDAO
4 implements
5 PhabricatorPolicyInterface,
6 PhabricatorMarkupInterface,
7 PhabricatorSubscribableInterface,
8 PhabricatorFlaggableInterface,
9 PhabricatorProjectInterface,
10 PhabricatorDestructibleInterface,
11 PhabricatorApplicationTransactionInterface,
12 PhabricatorConduitResultInterface,
13 PhabricatorFulltextInterface,
14 PhabricatorFerretInterface {
16 protected $name;
17 protected $subtitle;
18 protected $description;
19 protected $domain;
20 protected $domainFullURI;
21 protected $parentSite;
22 protected $parentDomain;
23 protected $configData;
24 protected $creatorPHID;
25 protected $viewPolicy;
26 protected $editPolicy;
27 protected $interactPolicy;
28 protected $status;
29 protected $mailKey;
30 protected $profileImagePHID;
31 protected $headerImagePHID;
33 private $profileImageFile = self::ATTACHABLE;
34 private $headerImageFile = self::ATTACHABLE;
36 const STATUS_ACTIVE = 'active';
37 const STATUS_ARCHIVED = 'archived';
39 protected function getConfiguration() {
40 return array(
41 self::CONFIG_AUX_PHID => true,
42 self::CONFIG_SERIALIZATION => array(
43 'configData' => self::SERIALIZATION_JSON,
45 self::CONFIG_COLUMN_SCHEMA => array(
46 'name' => 'text64',
47 'subtitle' => 'text64',
48 'description' => 'text',
49 'domain' => 'text128?',
50 'domainFullURI' => 'text128?',
51 'parentSite' => 'text128?',
52 'parentDomain' => 'text128?',
53 'status' => 'text32',
54 'mailKey' => 'bytes20',
55 'profileImagePHID' => 'phid?',
56 'headerImagePHID' => 'phid?',
58 'editPolicy' => 'policy',
59 'viewPolicy' => 'policy',
60 'interactPolicy' => 'policy',
62 self::CONFIG_KEY_SCHEMA => array(
63 'key_phid' => null,
64 'phid' => array(
65 'columns' => array('phid'),
66 'unique' => true,
68 'domain' => array(
69 'columns' => array('domain'),
70 'unique' => true,
73 ) + parent::getConfiguration();
76 public function save() {
77 if (!$this->getMailKey()) {
78 $this->setMailKey(Filesystem::readRandomCharacters(20));
80 return parent::save();
83 public function generatePHID() {
84 return PhabricatorPHID::generateNewPHID(
85 PhabricatorPhameBlogPHIDType::TYPECONST);
88 public static function initializeNewBlog(PhabricatorUser $actor) {
89 $blog = id(new PhameBlog())
90 ->setCreatorPHID($actor->getPHID())
91 ->setStatus(self::STATUS_ACTIVE)
92 ->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy())
93 ->setEditPolicy(PhabricatorPolicies::POLICY_USER)
94 ->setInteractPolicy(PhabricatorPolicies::POLICY_USER);
96 return $blog;
99 public function isArchived() {
100 return ($this->getStatus() == self::STATUS_ARCHIVED);
103 public static function getStatusNameMap() {
104 return array(
105 self::STATUS_ACTIVE => pht('Active'),
106 self::STATUS_ARCHIVED => pht('Archived'),
111 * Makes sure a given custom blog uri is properly configured in DNS
112 * to point at this Phabricator instance. If there is an error in
113 * the configuration, return a string describing the error and how
114 * to fix it. If there is no error, return an empty string.
116 * @return string
118 public function validateCustomDomain($domain_full_uri) {
119 $example_domain = 'http://blog.example.com/';
120 $label = pht('Invalid');
122 // note this "uri" should be pretty busted given the desired input
123 // so just use it to test if there's a protocol specified
124 $uri = new PhutilURI($domain_full_uri);
125 $domain = $uri->getDomain();
126 $protocol = $uri->getProtocol();
127 $path = $uri->getPath();
128 $supported_protocols = array('http', 'https');
130 if (!in_array($protocol, $supported_protocols)) {
131 return pht(
132 'The custom domain should include a valid protocol in the URI '.
133 '(for example, "%s"). Valid protocols are "http" or "https".',
134 $example_domain);
137 if (strlen($path) && $path != '/') {
138 return pht(
139 'The custom domain should not specify a path (hosting a Phame '.
140 'blog at a path is currently not supported). Instead, just provide '.
141 'the bare domain name (for example, "%s").',
142 $example_domain);
145 if (strpos($domain, '.') === false) {
146 return pht(
147 'The custom domain should contain at least one dot (.) because '.
148 'some browsers fail to set cookies on domains without a dot. '.
149 'Instead, use a normal looking domain name like "%s".',
150 $example_domain);
153 if (!PhabricatorEnv::getEnvConfig('policy.allow-public')) {
154 $href = PhabricatorEnv::getProductionURI(
155 '/config/edit/policy.allow-public/');
156 return pht(
157 'For custom domains to work, this this server must be '.
158 'configured to allow the public access policy. Configure this '.
159 'setting %s, or ask an administrator to configure this setting. '.
160 'The domain can be specified later once this setting has been '.
161 'changed.',
162 phutil_tag(
163 'a',
164 array('href' => $href),
165 pht('here')));
168 return null;
171 public function getLiveURI() {
172 if ($this->getDomain() !== null && strlen($this->getDomain())) {
173 return $this->getExternalLiveURI();
174 } else {
175 return $this->getInternalLiveURI();
179 public function getExternalLiveURI() {
180 $uri = new PhutilURI($this->getDomainFullURI());
181 PhabricatorEnv::requireValidRemoteURIForLink($uri);
182 return (string)$uri;
185 public function getExternalParentURI() {
186 $uri = $this->getParentDomain();
187 PhabricatorEnv::requireValidRemoteURIForLink($uri);
188 return (string)$uri;
191 public function getInternalLiveURI() {
192 return '/phame/live/'.$this->getID().'/';
195 public function getViewURI() {
196 return '/phame/blog/view/'.$this->getID().'/';
199 public function getManageURI() {
200 return '/phame/blog/manage/'.$this->getID().'/';
203 public function getProfileImageURI() {
204 return $this->getProfileImageFile()->getBestURI();
207 public function attachProfileImageFile(PhabricatorFile $file) {
208 $this->profileImageFile = $file;
209 return $this;
212 public function getProfileImageFile() {
213 return $this->assertAttached($this->profileImageFile);
216 public function getHeaderImageURI() {
217 return $this->getHeaderImageFile()->getBestURI();
220 public function attachHeaderImageFile(PhabricatorFile $file) {
221 $this->headerImageFile = $file;
222 return $this;
225 public function getHeaderImageFile() {
226 return $this->assertAttached($this->headerImageFile);
230 /* -( PhabricatorPolicyInterface Implementation )-------------------------- */
233 public function getCapabilities() {
234 return array(
235 PhabricatorPolicyCapability::CAN_VIEW,
236 PhabricatorPolicyCapability::CAN_INTERACT,
237 PhabricatorPolicyCapability::CAN_EDIT,
242 public function getPolicy($capability) {
243 switch ($capability) {
244 case PhabricatorPolicyCapability::CAN_VIEW:
245 return $this->getViewPolicy();
246 case PhabricatorPolicyCapability::CAN_INTERACT:
247 return $this->getInteractPolicy();
248 case PhabricatorPolicyCapability::CAN_EDIT:
249 return $this->getEditPolicy();
253 public function hasAutomaticCapability($capability, PhabricatorUser $user) {
254 $can_edit = PhabricatorPolicyCapability::CAN_EDIT;
256 switch ($capability) {
257 case PhabricatorPolicyCapability::CAN_VIEW:
258 // Users who can edit or post to a blog can always view it.
259 if (PhabricatorPolicyFilter::hasCapability($user, $this, $can_edit)) {
260 return true;
262 break;
265 return false;
269 public function describeAutomaticCapability($capability) {
270 switch ($capability) {
271 case PhabricatorPolicyCapability::CAN_VIEW:
272 return pht(
273 'Users who can edit a blog can always view it.');
276 return null;
280 /* -( PhabricatorMarkupInterface Implementation )-------------------------- */
283 public function getMarkupFieldKey($field) {
284 $content = $this->getMarkupText($field);
285 return PhabricatorMarkupEngine::digestRemarkupContent($this, $content);
289 public function newMarkupEngine($field) {
290 return PhabricatorMarkupEngine::newPhameMarkupEngine();
294 public function getMarkupText($field) {
295 return $this->getDescription();
299 public function didMarkupText(
300 $field,
301 $output,
302 PhutilMarkupEngine $engine) {
303 return $output;
306 public function shouldUseMarkupCache($field) {
307 return (bool)$this->getPHID();
310 /* -( PhabricatorDestructibleInterface )----------------------------------- */
312 public function destroyObjectPermanently(
313 PhabricatorDestructionEngine $engine) {
315 $this->openTransaction();
317 $posts = id(new PhamePostQuery())
318 ->setViewer($engine->getViewer())
319 ->withBlogPHIDs(array($this->getPHID()))
320 ->execute();
321 foreach ($posts as $post) {
322 $engine->destroyObject($post);
324 $this->delete();
326 $this->saveTransaction();
330 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
333 public function getApplicationTransactionEditor() {
334 return new PhameBlogEditor();
337 public function getApplicationTransactionTemplate() {
338 return new PhameBlogTransaction();
342 /* -( PhabricatorSubscribableInterface Implementation )-------------------- */
345 public function isAutomaticallySubscribed($phid) {
346 return false;
350 /* -( PhabricatorConduitResultInterface )---------------------------------- */
353 public function getFieldSpecificationsForConduit() {
354 return array(
355 id(new PhabricatorConduitSearchFieldSpecification())
356 ->setKey('name')
357 ->setType('string')
358 ->setDescription(pht('The name of the blog.')),
359 id(new PhabricatorConduitSearchFieldSpecification())
360 ->setKey('description')
361 ->setType('string')
362 ->setDescription(pht('Blog description.')),
363 id(new PhabricatorConduitSearchFieldSpecification())
364 ->setKey('status')
365 ->setType('string')
366 ->setDescription(pht('Archived or active status.')),
370 public function getFieldValuesForConduit() {
371 return array(
372 'name' => $this->getName(),
373 'description' => $this->getDescription(),
374 'status' => $this->getStatus(),
378 public function getConduitSearchAttachments() {
379 return array();
383 /* -( PhabricatorFulltextInterface )--------------------------------------- */
385 public function newFulltextEngine() {
386 return new PhameBlogFulltextEngine();
390 /* -( PhabricatorFerretInterface )----------------------------------------- */
393 public function newFerretEngine() {
394 return new PhameBlogFerretEngine();