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