]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/doc.php
Merge branch '0.9.x' into testing
[quix0rs-gnu-social.git] / actions / doc.php
1 <?php
2 /**
3  * Documentation action.
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @author   Robin Millette <millette@status.net>
11  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12  * @link     http://status.net/
13  *
14  * StatusNet - the distributed open-source microblogging tool
15  * Copyright (C) 2008-2010, StatusNet, Inc.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Documentation class.
37  *
38  * @category Action
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @author   Robin Millette <millette@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://status.net/
44  */
45 class DocAction extends Action
46 {
47     var $output   = null;
48     var $filename = null;
49     var $title    = null;
50
51     function prepare($args)
52     {
53         parent::prepare($args);
54
55         $this->title  = $this->trimmed('title');
56         if (!preg_match('/^[a-zA-Z0-9_-]*$/', $this->title)) {
57             $this->title = 'help';
58         }
59         $this->output = null;
60
61         $this->loadDoc();
62         return true;
63     }
64
65     /**
66      * Handle a request
67      *
68      * @param array $args array of arguments
69      *
70      * @return nothing
71      */
72     function handle($args)
73     {
74         parent::handle($args);
75         $this->showPage();
76     }
77
78     /**
79      * Page title
80      *
81      * Gives the page title of the document. Override default for hAtom entry.
82      *
83      * @return void
84      */
85     function showPageTitle()
86     {
87         $this->element('h1', array('class' => 'entry-title'), $this->title());
88     }
89
90     /**
91      * Block for content.
92      *
93      * Overrides default from Action to wrap everything in an hAtom entry.
94      *
95      * @return void.
96      */
97     function showContentBlock()
98     {
99         $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
100         $this->showPageTitle();
101         $this->showPageNoticeBlock();
102         $this->elementStart('div', array('id' => 'content_inner',
103                                          'class' => 'entry-content'));
104         // show the actual content (forms, lists, whatever)
105         $this->showContent();
106         $this->elementEnd('div');
107         $this->elementEnd('div');
108     }
109
110     /**
111      * Display content.
112      *
113      * Shows the content of the document.
114      *
115      * @return void
116      */
117     function showContent()
118     {
119         $this->raw($this->output);
120     }
121
122     /**
123      * Page title.
124      *
125      * Uses the title of the document.
126      *
127      * @return page title
128      */
129     function title()
130     {
131         return ucfirst($this->title);
132     }
133
134     /**
135      * These pages are read-only.
136      *
137      * @param array $args unused.
138      *
139      * @return boolean read-only flag (false)
140      */
141     function isReadOnly($args)
142     {
143         return true;
144     }
145
146     function loadDoc()
147     {
148         if (Event::handle('StartLoadDoc', array(&$this->title, &$this->output))) {
149
150             $this->filename = $this->getFilename();
151
152             if (empty($this->filename)) {
153                 // TRANS: Client exception thrown when requesting a document from the documentation that does not exist.
154                 // TRANS: %s is the non-existing document.
155                 throw new ClientException(sprintf(_('No such document "%s".'), $this->title), 404);
156             }
157
158             $c = file_get_contents($this->filename);
159
160             $this->output = common_markup_to_html($c);
161
162             Event::handle('EndLoadDoc', array($this->title, &$this->output));
163         }
164     }
165
166     function getFilename()
167     {
168         $localDef = null;
169         $local    = null;
170
171         $site = StatusNet::currentSite();
172
173         if (!empty($site) && file_exists(INSTALLDIR.'/local/doc-src/'.$site.'/'.$this->title)) {
174             $localDef = INSTALLDIR.'/local/doc-src/'.$site.'/'.$this->title;
175
176             $local = glob(INSTALLDIR.'/local/doc-src/'.$site.'/'.$this->title.'.*');
177             if ($local === false) {
178                 // Some systems return false, others array(), if dir didn't exist.
179                 $local = array();
180             }
181         } else {
182             if (file_exists(INSTALLDIR.'/local/doc-src/'.$this->title)) {
183                 $localDef = INSTALLDIR.'/local/doc-src/'.$this->title;
184             }
185
186             $local = glob(INSTALLDIR.'/local/doc-src/'.$this->title.'.*');
187             if ($local === false) {
188                 $local = array();
189             }
190         }
191
192         if (count($local) || isset($localDef)) {
193             return $this->negotiateLanguage($local, $localDef);
194         }
195
196         if (file_exists(INSTALLDIR.'/doc-src/'.$this->title)) {
197             $distDef = INSTALLDIR.'/doc-src/'.$this->title;
198         }
199
200         $dist = glob(INSTALLDIR.'/doc-src/'.$this->title.'.*');
201         if ($dist === false) {
202             $dist = array();
203         }
204
205         if (count($dist) || isset($distDef)) {
206             return $this->negotiateLanguage($dist, $distDef);
207         }
208
209         return null;
210     }
211
212     function negotiateLanguage($filenames, $defaultFilename=null)
213     {
214         // XXX: do this better
215
216         $langcode = common_language();
217
218         foreach ($filenames as $filename) {
219             if (preg_match('/\.'.$langcode.'$/', $filename)) {
220                 return $filename;
221             }
222         }
223
224         return $defaultFilename;
225     }
226 }