MTrackWeb.php
[web.mtrack] / MTrackWeb.php
1 <?php
2 /**
3  * every class extends this...
4  */
5  
6  
7 class MTrackWeb extends HTML_FlexyFramework_Page
8 {
9     var $template = 'wiki.html';
10     var $priorities = array();
11     var $severities = array();
12     var $link = false; // the link handler..
13     
14     function hasPerm($what, $cando) {
15         // our whole perm logic sits in here....
16         
17         // here's how it works
18         // a) anonymous users - not authenticated.
19         // - can see projects that are in *PUBLIC project "MTrack.Repos", "S"
20         // - can see bugs that are in *PUBLIC project "MTrack.Issue", "S"
21         // - can see bugs that are in *PUBLIC project "MTrack.Wiki", "S"
22         if (!$this->authUser) {
23             if ($cando = 'S' &&
24                     in_array( $what , array( 'MTrack.Repos', 'MTrack.Issue', 'MTrack.Wiki'))) {
25                 return true; // not a diffinative answer...
26             }
27             return false;
28         }
29         
30         return $this->authUser->hasPerm($what, $cando); 
31     }
32     
33     function projectPerm($project_id, $what, $cando)
34     {
35         if (!$project_id) {
36             return false;
37         }
38         $p = DB_DataObject::factory('Projects');
39         $p->get($project_id);
40         if (!$this->authUser) {
41             if ($p->code != '*PUBLIC') {
42                 return false; // only public projects
43             }
44             if ($cando != 'S') {
45                 return false;
46             }
47             // all permissions to view public stuff.
48             return true;
49         }
50         if (!$au->hasPerm($what, $cando)) {
51             return false;
52         }
53         // membership rules?
54         if ($au->company()->comptype == 'OWNER') {
55                 
56             if ($au->hasPerm('Projects_All', $cando)) { // they can do what they like on all projects.
57                return true;
58             }
59            // return $p->hasPerm($what, $cando);
60         }
61         // otherwise they have to be a team member of that project.
62         
63         $pd = DB_DataObject::factory('ProjectDirectory');
64         $pd->project_id = $project_id;
65         $pd->user_id = $this->authUser->id;
66         if (!$pd->count()) {
67             return false;
68         }
69         return true;
70         
71         
72         
73         
74     }
75     
76     
77     function getAuthUser()
78     {
79         $u = DB_DataObject::factory('Person');
80         if (!$u->isAuth()) {
81             return false;
82         }
83         return $u->getAuthUser();
84     }
85     /**
86      * base getAuth allows everyone in..
87      */
88     
89     function getAuth()
90     {
91         $this->registerClasses(); // to be destroyed??
92         
93         $ff = HTML_FlexyFramework::get();
94         if ($ff->cli) {
95             return true;
96         }
97         
98         // default timezone first..
99         $ff = HTML_FlexyFramework::get();
100         if (isset($ff->MTrack['timezone'])) {
101             date_default_timezone_set($ff->MTrack['timezone']);
102         }
103         
104         //MTrackConfig::boot(); // eak.. .remove me...
105       
106         $this->authUser = DB_DataObject::factory('Person')->getAuthUser();
107         if (!$this->authUser) {
108             return true; // we do allow people in this far..
109         }
110         
111          
112         // timezone setting... -- this may be a good addon to our core person class.
113         
114         if (!empty($this->authUser->timezone)) {
115             date_default_timezone_set($this->authUser->timezone);
116             return;
117         }
118          
119         /// fixme...
120         //$this->authUser = 
121         return true; // anyone at present..
122     }
123     function get($loc='')
124     {
125         // 
126         if (!empty($loc)) {
127             die ("invalid location". htmlspecialchars($loc));
128         }
129         if (!$this->authUser) {
130             die("wiki todo");
131             return HTML_FlexyFramework::run('Wiki'); 
132         }
133         return HTML_FlexyFramework::run('Wiki/Today'); 
134  
135     }
136     function post()
137     {
138         header("Status: 404 Not Found");
139         die("not valid");
140     }
141     
142     
143     function initOptions()
144     {
145         
146          
147         $q = MTrackDB::q('select priorityname, value from priorities');
148
149         foreach ($q->fetchAll() as $row) {
150             $this->priorities[$row[0]] = $row[1];
151         }
152         $q = MTrackDB::q('select sevname, ordinal from severities');
153         
154         foreach ($q->fetchAll() as $row) {
155             $this->severities[$row[0]] = $row[1];
156         }
157
158     }
159     
160     function registerClasses()
161     {
162         require_once 'MTrack/Wiki.php';
163         require_once 'MTrack/Wiki/Item.php';
164         require_once 'MTrack/Milestone.php';
165         //require_once 'MTrack/Report.php';
166         //require_once 'MTrack/SearchDB.php';
167         //require_once 'MTrack/Watch.php'; 
168         //require_once 'MTrack/CommitChecker.php'; 
169       
170         
171         require_once 'MTrackWeb/LinkHandler.php';
172         require_once 'MTrack/Wiki/HTMLFormatter.php';
173         
174         $this->link = new MTrackWeb_LinkHandler();
175         MTrack_Wiki_HTMLFormatter::registerLinkHandler($this->link);
176  
177
178         $r = DB_DataObject::factory('mtrack_repos');
179         $r->get('shortname', 'wiki'); // default wiki?
180         MTrack_Wiki_Item::$repo = $r->impl();
181         
182         
183         
184         //MTrack_Wiki::register_macro('MilestoneSummary', array('MTrack_Milestone', 'macro_MilestoneSummary'));
185        // MTrack_Wiki::register_macro('BurnDown', array('MTrack_Milestone', 'macro_BurnDown'));
186         //MTrack_Wiki::register_macro('RunReport', array('MTrack_Report', 'macro_RunReport')); << fixme how are we to hanlde this..
187         //MTrack_Wiki::register_macro('TicketQuery', array('MTrack_Report', 'macro_TicketQuery'));
188         MTrack_Wiki::register_macro('IncludeWikiPage', array('MTrack_Wiki', 'macro_IncludeWiki'));
189         MTrack_Wiki::register_macro('IncludeHelpPage', array('MTrack_Wiki', 'macro_IncludeHelp'));
190         MTrack_Wiki::register_macro('Comment', array('MTrack_Wiki', 'macro_comment'));
191         MTrack_Wiki::register_processor('comment', array('MTrack_Wiki', 'processor_comment'));
192         MTrack_Wiki::register_processor('html', array('MTrack_Wiki', 'processor_html'));
193         MTrack_Wiki::register_processor('dataset', array('MTrack_Wiki', 'processor_dataset'));
194
195
196         //MTrackACL::registerAncestry('milestone', 'Roadmap');
197         //MTrackACL::registerAncestry('report', 'Reports');
198         //MTrackACL::registerAncestry('snippet', 'Snippets');
199         //MTrackACL::registerAncestry('repo', 'Browser');
200         //MTrackACL::registerAncestry('enum',     'Enumerations');
201         //MTrackACL::registerAncestry("component", 'Components');
202         //MTrackACL::registerAncestry("project",  'Projects');
203         //MTrackACL::registerAncestry("ticket",   "Tickets");
204         //MTrackACL::registerAncestry('wiki', array('MTrack_Wiki_Item', '_get_parent_for_acl'));
205
206         //MTrackSearchDB::register_indexer('ticket', array('MTrackIssue', 'index_issue'));
207         //MTrackSearchDB::register_indexer('wiki', array('MTrack_Wiki_Item', 'index_item'));
208
209
210
211         //MTrackWatch::registerEventTypes('ticket', array( 'ticket' => 'Tickets' ));
212         //MTrackWatch::registerEventTypes('milestone', array( 'ticket' => 'Tickets', 'changeset' => 'Code changes' ));
213         //MTrackWatch::registerEventTypes('repo', array( 'ticket' => 'Tickets', 'changeset' => 'Code changes' ));
214
215         // should this get registered here??
216         //MTrackCommitChecker::addCheck('Wiki');
217         
218         
219         
220    }
221     
222     function favicon()
223     {
224         return false;
225         /// FIXME - we should allow upload of a favion...
226         $ff = HTML_FlexyFramework::get();
227         
228         
229     }
230      
231     
232     /* renders the attachment list for a given object */
233     // was Attachments::render
234     // move it to MTrackWebAttachemnt...
235     
236   function attachmentsToHtml($object)
237   {
238     if (is_object($object)) {
239         $object = $object->toIdString(); // eg. ticket:1
240     }
241     $atts = MTrackDB::q('
242       select * from attachments
243       left join changes on (attachments.cid = changes.cid)
244       where attachments.object = ? order by changedate, filename',
245         $object)->fetchAll(PDO::FETCH_ASSOC);
246
247     if (count($atts) == 0) return '';
248
249     $max_dim = 150;
250
251     $html = "<div class='attachment-list'><b>Attachments</b><ul>";
252     foreach ($atts as $row) {
253       $url = "{$this->baseURL}/Attachment/$object/". $row['cid'] . '/' . $row['filename'];
254       
255       $html .= "<li><a class='attachment'" .
256         " href='$url'>".
257         "$row[filename]</a> ($row[size]) added by " .
258         $this->link->username($row['who'], array(
259           'no_image' => true
260         )) .
261         " " . $this->link->date($row['changedate']);
262         require_once 'MTrack/Attachment.php';
263       list($width, $height) = getimagesize(MTrackAttachment::local_path($row['hash']));
264       if ($width + $height) {
265         /* limit maximum size */
266         if ($width > $max_dim) {
267           $height *= $max_dim / $width;
268           $width = $max_dim;
269         }
270         if ($height > $max_dim) {
271           $width *= $max_dim / $height;
272           $height = $max_dim;
273         }
274         $html .= "<br><a href='$url'><img src='$url' width='$width' border='0' height='$height'></a>";
275       }
276
277       $html .= "</li>\n";
278     }
279     $html .= "</ul></div>";
280     return $html;
281   }
282     
283     
284
285 }