]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/EditToolbar.php
Present only allowed plugins
[SourceForge/phpwiki.git] / lib / EditToolbar.php
1 <?php
2 // rcs_id('$Id$');
3 /* Copyright 2004-2010 $ThePhpWikiProgrammingTeam
4  * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * EDIT Toolbar Initialization.
25  * The default/themes/toolbar.js is from Mediawiki, this PHP is written from scratch.
26  *
27  * Features: 
28  * - save-preview and formatting buttons from mediawiki
29  * - Search&Replace from walterzorn.de
30  * - pageinsert popup by Reini Urban (TODO: should be a pulldown, use acdropdown)
31  */
32
33 class EditToolbar {
34
35     function EditToolbar() {
36         global $WikiTheme;
37
38         $this->tokens = array();
39
40         //FIXME: enable Undo button for all other buttons also, not only the search/replace button
41         if (JS_SEARCHREPLACE) {
42             $this->tokens['JS_SEARCHREPLACE'] = 1;
43             $undo_btn = $WikiTheme->getImageURL("ed_undo.png"); 
44             $undo_d_btn = $WikiTheme->getImageURL("ed_undo_d.png"); 
45             // JS_SEARCHREPLACE from walterzorn.de
46             $js = Javascript("
47 uri_undo_btn   = '".$undo_btn."'
48 msg_undo_alt   = '"._("Undo")."'
49 uri_undo_d_btn = '".$undo_d_btn."'
50 msg_undo_d_alt = '"._("Undo disabled")."'
51 msg_do_undo    = '"._("Operation undone")."'
52 msg_replfound  = '"._("Substring \"\\1\" found \\2 times. Replace with \"\\3\"?")."'
53 msg_replnot    = '"._("String \"%s\" not found.")."'
54 msg_repl_title     = '"._("Search & Replace")."'
55 msg_repl_search    = '"._("Search for")."'
56 msg_repl_replace_with = '"._("Replace with")."'
57 msg_repl_ok        = '"._("OK")."'
58 msg_repl_close     = '"._("Close")."'
59 ");
60             if (empty($WikiTheme->_headers_printed)) {
61                 $WikiTheme->addMoreHeaders($js);
62                 $WikiTheme->addMoreAttr('body', "SearchReplace"," onload='define_f()'");
63             } else { // from an actionpage: WikiBlog, AddComment, WikiForum
64                 printXML($js);
65             }
66         } else {
67             $WikiTheme->addMoreAttr('body', "editfocus", "document.getElementById('edit-content]').editarea.focus()");
68         }
69     
70         if (ENABLE_EDIT_TOOLBAR) {
71             $js = JavaScript('',array('src' => $WikiTheme->_findData("toolbar.js")));
72             if (empty($WikiTheme->_headers_printed))
73                 $WikiTheme->addMoreHeaders($js);
74             else { // from an actionpage: WikiBlog, AddComment, WikiForum
75                 printXML($js);
76                 printXML(JavaScript('define_f()'));
77             }
78         }
79
80         require_once("lib/WikiPluginCached.php");
81         $cache = WikiPluginCached::newCache();
82         $dbi = $GLOBALS['request']->getDbh();
83         // regenerate if number of pages changes (categories, pages, templates)
84         $key = $dbi->numPages();
85         $key .= '+categories+plugin' . (isBrowserSafari() ? '+safari' : '');
86         if (TOOLBAR_PAGELINK_PULLDOWN) {
87             $key .= "+pages";
88         }
89         if (TOOLBAR_TEMPLATE_PULLDOWN) {
90             $key .= "+templates_" . $dbi->getTimestamp();
91         }
92         $id = $cache->generateId($key);
93         $content = $cache->get($id, 'toolbarcache');
94
95         if (!empty($content)) {
96             $this->tokens['EDIT_TOOLBAR'] =& $content;
97         } else {
98             $content = $this->_generate();
99             // regenerate buttons every 3600 seconds
100             $cache->save($id, $content, '+3600', 'toolbarcache'); 
101             $this->tokens['EDIT_TOOLBAR'] =& $content;
102         }
103     }
104
105     function getTokens () {
106         return $this->tokens;
107     }
108
109     function _generate () {
110         global $WikiTheme, $request;
111
112         $toolbar = "document.writeln(\"<div class=\\\"edit-toolbar\\\" id=\\\"toolbar\\\">\");\n";
113
114         if (ENABLE_EDIT_TOOLBAR) {
115             $username = $request->_user->UserName();
116             if (defined('GFORGE') and GFORGE) {
117                 $username = '[['.$username.']]';
118             } else if (DISABLE_MARKUP_WIKIWORD or (!isWikiWord($username))) {
119                 $username = '['.$username.']';
120             }
121             $signature = " ––".$username." ".CTime();
122             $toolarray = array(
123                            array(
124                                  "image"=>"ed_format_bold.png",
125                                  "open"=>"**",
126                                  "close"=>"**",
127                                  "sample"=>_("Bold text"),
128                                  "title"=>_("Bold text [alt-b]")),
129                            array("image"=>"ed_format_italic.png",
130                                  "open"=>"//",
131                                  "close"=>"//",
132                                  "sample"=>_("Italic text"),
133                                  "title"=>_("Italic text [alt-i]")),
134                            array("image"=>"ed_format_strike.png",
135                                  "open"=>"<s>",
136                                  "close"=>"</s>",
137                                  "sample"=>_("Strike-through text"),
138                                  "title"=>_("Strike")),
139                            array("image"=>"ed_format_color.png",
140                                  "open"=>"%color=green% ",
141                                  "close"=>" %%",
142                                  "sample"=>_("Color text"),
143                                  "title"=>_("Color")),
144                            array("image"=>"ed_pagelink.png",
145                                  "open"=>"[[",
146                                  "close"=>"]]",
147                                  "sample"=>_("PageName|optional label"),
148                                  "title"=>_("Link to page")),
149                            array("image"=>"ed_link.png",
150                                  "open"=>"[[",
151                                  "close"=>"]]",
152                                  "sample"=>_("http://www.example.com|optional label"),
153                                  "title"=>_("External link (remember http:// prefix)")),
154                            array("image"=>"ed_headline.png",
155                                  "open"=>"\\n== ",
156                                  "close"=>" ==\\n",
157                                  "sample"=>_("Headline text"),
158                                  "title"=>_("Level 1 headline")),
159                            array("image"=>"ed_nowiki.png",
160                                  "open"=>"\\<verbatim\\>\\n",
161                                  "close"=>"\\n\\</verbatim\\>",
162                                  "sample"=>_("Insert non-formatted text here"),
163                                  "title"=>_("Ignore wiki formatting")),
164                            array("image"=>"ed_sig.png",
165                                  "open" => $signature,
166                                  "close" => "",
167                                  "sample"=>"",
168                                  "title"=>_("Your signature")),
169                            array("image"=>"ed_hr.png",
170                                  "open"=>"\\n----\\n",
171                                  "close"=>"",
172                                  "sample"=>"",
173                                  "title"=>_("Horizontal line")),
174                            array("image"=>"ed_table.png",
175                                  "open"=>"\\n{| class=\"bordered\"\\n|+ This is the table caption\\n|= This is the table summary\\n|-\\n! Header A !! Header B !! Header C\\n|-\\n| Cell A1 || Cell B1 || Cell C1\\n|-\\n| Cell A2 || Cell B2 || Cell C2\\n|-\\n| Cell A3 || Cell B3 || Cell C3\\n|}\\n",
176                                  "close"=>"",
177                                  "sample"=>"",
178                                  "title"=>_("Sample table")),
179                            array("image"=>"ed_enumlist.png",
180                                  "open"=>"\\n# Item 1\\n# Item 2\\n# Item 3\\n",
181                                  "close"=>"",
182                                  "sample"=>"",
183                                  "title"=>_("Enumeration")),
184                            array("image"=>"ed_list.png",
185                                  "open"=>"\\n* Item 1\\n* Item 2\\n* Item 3\\n",
186                                  "close"=>"",
187                                  "sample"=>"",
188                                  "title"=>_("List")),
189                            array("image"=>"ed_toc.png",
190                                  "open"=>"<<CreateToc with_toclink||=1>>\\n",
191                                  "close"=>"",
192                                  "sample"=>"",
193                                  "title"=>_("Table of Contents")),
194                            array("image"=>"ed_redirect.png",
195                                  "open"=>"<<RedirectTo page=\"",
196                                  "close"=>"\">>",
197                                  "sample"=>_("Page Name"),
198                                  "title"=>_("Redirect")),
199                            array("image"=>"ed_templateplugin.png",
200                                  "open"=>"{{",
201                                  "close"=>"}}",
202                                  "sample"=>_("Template Name"),
203                                  "title"=>_("Template"))
204                            );
205             $btn = new SubmitImageButton(_("Save"), "edit[save]", 'toolbar', 
206                                          $WikiTheme->getImageURL("ed_save.png"));
207             $btn->addTooltip(_("Save"));
208             $btn->setAccesskey("s");
209             $toolbar .= ('document.writeln("'.addslashes($btn->asXml()).'");'."\n");
210             // preview not supported yet on Wikiblog
211             if (empty($WikiTheme->_headers_printed)) {
212                 $btn = new SubmitImageButton(_("Preview"), "edit[preview]", 'toolbar', 
213                                              $WikiTheme->getImageURL("ed_preview.png"));
214                 $btn->addTooltip(_("Preview"));
215                 $btn->setAccesskey("p");
216                 $toolbar .= ('document.writeln("'.addslashes($btn->asXml()).'");'."\n");
217             }
218     
219             foreach ($toolarray as $tool) {
220                 global $WikiTheme;
221                 $image = $WikiTheme->getImageURL($tool["image"]);
222                 $open  = $tool["open"];
223                 $close = $tool["close"];
224                 $sample = addslashes( $tool["sample"] );
225                 // Note that we use the title both for the ALT tag and the TITLE tag of the image.
226                 // Older browsers show a "speedtip" type message only for ALT.
227                 // Ideally these should be different, realistically they
228                 // probably don't need to be.
229                 $tool = $WikiTheme->fixAccesskey($tool);
230                 $title = addslashes( $tool["title"] );
231                 $toolbar .= ("addTagButton('$image','$title','$open','$close','$sample');\n");
232             }
233             /* Fails with Chrome */
234             if (!isBrowserSafari()) {
235                 $toolbar .= ("addInfobox('" 
236                              . addslashes( _("Click a button to get an example text") ) 
237                              . "');\n");
238             }
239         }
240
241         if (JS_SEARCHREPLACE) {
242             $undo_d_btn = $WikiTheme->getImageURL("ed_undo_d.png"); 
243             //$redo_btn = $WikiTheme->getImageURL("ed_redo.png");
244             $sr_btn   = $WikiTheme->getImageURL("ed_replace.png");
245             //TODO: generalize the UNDO button and fix it for Search & Replace
246             $sr_html = HTML(HTML::img
247                             (array('class'=>"toolbar",
248                                    'id'   =>"sr_undo",
249                                    'src'  =>$undo_d_btn,
250                                    'title'=>_("Undo Search & Replace"),
251                                    'alt'  =>_("Undo Search & Replace"),
252                                    //'disabled'=>"disabled",   //non-XHTML conform
253                                    //'onfocus' =>"if(this.blur && undo_buffer_index==0) this.blur()",
254                                    'onclick' =>"do_undo()")),
255                             HTML::img
256                             (array('class'=>"toolbar",
257                                    'src'  => $sr_btn,
258                                    'alt'  =>_("Search & Replace"),
259                                    'title'=>_("Search & Replace"),
260                                    'onclick'=>"replace()")));
261         } else {
262             $sr_html = '';
263         }
264
265         //TODO: Delegate this to run-time with showing an hidden input at the right, and do 
266         // a seperate moacdropdown and xmlrpc:titleSearch.
267
268         // Button to generate categories, display in extra window as popup and insert
269         $sr_html = HTML($sr_html, $this->categoriesPulldown());
270         // Button to generate plugins, display in extra window as popup and insert
271         $sr_html = HTML($sr_html, $this->pluginPulldown());
272
273         // Button to generate pagenames, display in extra window as popup and insert
274         if (TOOLBAR_PAGELINK_PULLDOWN)
275             $sr_html = HTML($sr_html, $this->pagesPulldown(TOOLBAR_PAGELINK_PULLDOWN));
276         // Button to insert from an template, display pagename in extra window as popup and insert
277         if (TOOLBAR_TEMPLATE_PULLDOWN)
278             $sr_html = HTML($sr_html, $this->templatePulldown(TOOLBAR_TEMPLATE_PULLDOWN));
279
280         // Button to add images, display in extra window as popup and insert
281         if (TOOLBAR_IMAGE_PULLDOWN)
282             $sr_html = HTML($sr_html, $this->imagePulldown(TOOLBAR_IMAGE_PULLDOWN));
283
284         // don't use document.write for replace, otherwise self.opener is not defined.
285         $toolbar_end = "document.writeln(\"</div>\");";
286         if ($sr_html)
287             return HTML(Javascript($toolbar),
288                         "\n", $sr_html, "\n",
289                         Javascript($toolbar_end));
290         else
291             return HTML(Javascript($toolbar . $toolbar_end));
292     }
293
294     //result is cached
295     function categoriesPulldown() {
296         global $WikiTheme;
297
298         require_once('lib/TextSearchQuery.php');
299         $dbi =& $GLOBALS['request']->_dbi;
300         // KEYWORDS formerly known as $KeywordLinkRegexp
301         $pages = $dbi->titleSearch(new TextSearchQuery(KEYWORDS, true));
302         if ($pages) {
303             $categories = array();
304             while ($p = $pages->next()) {
305                 $page = $p->getName();
306                 if (defined('GFORGE') and GFORGE) {
307                     $categories[] = "['$page', '%0A----%0A%5B%5B".$page."%5D%5D']";
308                 } else if (DISABLE_MARKUP_WIKIWORD or (!isWikiWord($page))) {
309                     $categories[] = "['$page', '%0A%5B".$page."%5D']";
310                 } else {
311                     $categories[] = "['$page', '%0A".$page."']";
312                 }
313             }
314             if (!$categories) return '';
315             // Ensure this to be inserted at the very end. Hence we added the id to the function.
316             $more_buttons = HTML::img(array('class'=> "toolbar",
317                                             'id' => 'tb-categories',
318                                             'src'  => $WikiTheme->getImageURL("ed_category.png"),
319                                             'title'=>_("AddCategory"),
320                                             'alt'=>"AddCategory", // to detect this at js
321                                             'onclick'=>"showPulldown('".
322                                             _("Insert Categories (double-click)")
323                                             ."',[".join(",",$categories)."],'"
324                                             ._("Insert")."','"
325                                             ._("Close")."','tb-categories')"));
326             return HTML("\n", $more_buttons);
327         }
328         return '';
329     }
330
331     // result is cached. Esp. the args are expensive
332     function pluginPulldown() {
333         global $WikiTheme;
334         global $AllAllowedPlugins;
335
336         $plugin_dir = 'lib/plugin';
337         if (defined('PHPWIKI_DIR'))
338             $plugin_dir = PHPWIKI_DIR . "/$plugin_dir";
339         $pd = new fileSet($plugin_dir, '*.php');
340         $plugins = $pd->getFiles();
341         unset($pd);
342         sort($plugins);
343         if (!empty($plugins)) {
344             $plugin_js = '';
345             require_once("lib/WikiPlugin.php");
346             $w = new WikiPluginLoader;
347             foreach ($plugins as $plugin) {
348                 $pluginName = str_replace(".php", "", $plugin);
349                 if (in_array($pluginName, $AllAllowedPlugins)) {
350                     $p = $w->getPlugin($pluginName, false); // second arg?
351                     // trap php files which aren't WikiPlugin~s
352                     if (strtolower(substr(get_parent_class($p), 0, 10)) == 'wikiplugin') {
353                         $plugin_args = '';
354                         $desc = $p->getArgumentsDescription();
355                         $src = array("\n",'"',"'",'|','[',']','\\');
356                         $replace = array('%0A','%22','%27','%7C','%5B','%5D','%5C');
357                         $desc = str_replace("<br />",' ',$desc->asXML());
358                         if ($desc)
359                             $plugin_args = ' '.str_replace($src, $replace, $desc);
360                         $toinsert = "%0A<<".$pluginName.$plugin_args.">>"; // args?
361                         $plugin_js .= ",['$pluginName','$toinsert']";
362                     }
363                 }
364             }
365             $plugin_js = substr($plugin_js, 1);
366             $more_buttons = HTML::img(array('class'=>"toolbar",
367                                             'id' => 'tb-plugins',
368                                             'src'  => $WikiTheme->getImageURL("ed_plugins.png"),
369                                             'title'=>_("AddPlugin"),
370                                             'alt'=>_("AddPlugin"),
371                                             'onclick'=>"showPulldown('".
372                                             _("Insert Plugin (double-click)")
373                                             ."',[".$plugin_js."],'"
374                                             ._("Insert")."','"
375                                             ._("Close")."','tb-plugins')"));
376             return HTML("\n", $more_buttons);
377         }
378         return '';
379     }
380
381     // result is cached. Esp. the args are expensive
382     function pagesPulldown($query, $case_exact=false, $regex='auto') {
383         require_once('lib/TextSearchQuery.php');
384         $dbi =& $GLOBALS['request']->_dbi;
385         $page_iter = $dbi->titleSearch(new TextSearchQuery($query, $case_exact, $regex));
386         if ($page_iter->count()) {
387             global $WikiTheme;
388             $pages = array();
389             while ($p = $page_iter->next()) {
390                 $page = $p->getName();
391                 if (DISABLE_MARKUP_WIKIWORD or (!isWikiWord($page)))
392                     $pages[] = "['$page', '%5B".$page."%5D']";
393                 else
394                     $pages[] = "['$page', '$page']";
395             }
396             return HTML("\n", HTML::img(array('class'=>"toolbar",
397                                               'id' => 'tb-pages',
398                                               'src'  => $WikiTheme->getImageURL("ed_pages.png"),
399                                               'title'=>_("AddPageLink"),
400                                               'alt'=>_("AddPageLink"),
401                                               'onclick'=>"showPulldown('".
402                                               _("Insert PageLink (double-click)")
403                                               ."',[".join(",",$pages)."],'"
404                                               ._("Insert")."','"
405                                               ._("Close")."','tb-pages')")));
406         }
407         return '';
408     }
409
410     // result is cached. Esp. the args are expensive
411     function imagePulldown($query, $case_exact=false, $regex='auto') {
412         global $WikiTheme;
413
414         $image_dir = getUploadFilePath();
415         $pd = new fileSet($image_dir, '*');
416         $images = $pd->getFiles();
417         unset($pd);
418         sort($images);
419         if (!empty($images)) {
420             $image_js = '';
421             foreach ($images as $image) {
422                 // Select only image and video files
423                 if (is_image($image) or is_video($image)) {
424                     $image_js .= ",['$image','{{".$image."}}']";
425                 }
426             }
427             $image_js = substr($image_js, 1);
428             $more_buttons = HTML::img(array('class'=>"toolbar",
429                                             'id' => 'tb-images',
430                                             'src'  => $WikiTheme->getImageURL("ed_image.png"),
431                                             'title'=>_("Add Image or Video"),
432                                             'alt'=>_("Add Image or Video"),
433                                             'onclick'=>"showPulldown('".
434                                             _("Insert Image or Video (double-click)")
435                                             ."',[".$image_js."],'"
436                                             ._("Insert")."','"
437                                             ._("Close")."','tb-images')"));
438             return HTML("\n", $more_buttons);
439         }
440         return '';
441     }
442
443     // result is cached. Esp. the args are expensive
444     // FIXME!
445     function templatePulldown($query, $case_exact=false, $regex='auto') {
446         global $request;
447         require_once('lib/TextSearchQuery.php');
448         $dbi =& $request->_dbi;
449         $page_iter = $dbi->titleSearch(new TextSearchQuery($query, $case_exact, $regex));
450         $count = 0;
451         if ($page_iter->count()) {
452             global $WikiTheme;
453             $pages_js = '';
454             while ($p = $page_iter->next()) {
455                 $rev = $p->getCurrentRevision();
456                 $toinsert = str_replace(array("\n",'"'), array('_nl','_quot'), $rev->_get_content());
457                 //$toinsert = str_replace("\n",'\n',addslashes($rev->_get_content()));
458                 $pages_js .= ",['".$p->getName()."','_nl$toinsert']";
459             }
460             $pages_js = substr($pages_js, 1);
461             if (!empty($pages_js))
462                 return HTML("\n", HTML::img
463                             (array('class'=>"toolbar",
464                                    'id' => 'tb-templates',
465                                    'src'  => $WikiTheme->getImageURL("ed_template.png"),
466                                    'title'=>_("AddTemplate"),
467                                    'alt'=>_("AddTemplate"),
468                                    'onclick'=>"showPulldown('".
469                                    _("Insert Template (double-click)")
470                                    ."',[".$pages_js."],'"
471                                    ._("Insert")."','"
472                                    ._("Close")."','tb-templates')")));
473         }
474         return '';
475     }
476
477 }
478
479 // Local Variables:
480 // mode: php
481 // tab-width: 8
482 // c-basic-offset: 4
483 // c-hanging-comment-ender-p: nil
484 // indent-tabs-mode: nil
485 // End:
486 ?>