1<?php
2/**
3 * dwlog.php
4 * Dokuwiki website tagger - act like a weblog
5 *
6 * Version 0.6.0
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Robin Gareus <robin@gareus.org>
10 * @based_on   http://wiki.splitbrain.org/wiki:tips:weblog_bookmarklet by riny [at] bk [dot] ru
11 *
12 * see http://mir.dnsalias.com/wiki/dokubookmark
13 *
14 * USAGE :
15 *  Create bookmark in your browser using bookmarklet part shown below,
16 *  Change the window.open statement to reflect the location of your dokuwiki script.
17 *
18 * BOOKMARKLET :
19 *  javascript:Q=document.selection?document.selection.createRange().text:document.getSelection(); void(window.open('http://your.host/doku.php?do=dokubookmark&te='+encodeURIComponent(Q)+'&ur='+ encodeURIComponent(location.href)+'&ti='+encodeURIComponent(document.title),'dokuwikiadd','scrollbars=yes,resizable=yes,toolbars=yes,width=200,height=100,left=200,top=200,status=yes'));
20 *
21 */
22
23if(!defined('DOKU_INC')) die();
24if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
25
26require_once(DOKU_PLUGIN.'action.php');
27
28/**
29 *
30 */
31class action_plugin_dokubookmark extends DokuWiki_Action_Plugin {
32
33  /**
34   * return some info
35   */
36  function getInfo(){
37    return array(
38     'author' => 'Robin Gareus',
39     'email'  => 'robin@gareus.org',
40     'date'   => '2011-12-20',
41     'name'   => 'Dokubookmark',
42     'desc'   => 'manage your bookmarks with this dokuwiki website tagger.',
43     'url'    => 'http://gareus.org/wiki/dokubookmark',
44     );
45  }
46
47  /**
48   * Register its handlers with the dokuwiki's event controller
49   */
50  function register(&$controller) {
51    $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE',  $this, '_hookdo');
52  }
53
54  /**
55   *
56   */
57  function _hookdo(&$event, $param) {
58    global $lang;
59
60    if ($this->getConf('enable_save') && $event->data['dokubookmark'] == $lang['btn_save'] ) {
61        $this->_save_bookmark($event);
62    } else if ($event->data['dokubookmark'] == "Save") {
63      global $ACT;
64      $ACT="show";
65      msg('Direct saving of weblog entries has been disabled. Use your browser back-button and retry.',-1);
66    } else if ($event->data == 'dokubookmark') {
67        $this->_bookmarkpage(); # this function will call exit();
68    }
69  }
70
71  /**
72   *
73   */
74  function _bookmarkpage() {
75    global $conf;
76    require_once(DOKU_PLUGIN.'dokubookmark/helper.php');
77
78    // Parse and prepare variables.
79    $selection = rawurldecode($_GET['te']);  // selected text
80    $url       = rawurldecode($_GET['ur']);  // URL
81    $title     = rawurldecode($_GET['ti']);  // page title
82    $timestamp = date($this->getConf('dateformat'));
83
84    #$wikitpl = "====== @T@ ======\n[[@U@]]\n----\n@S@\n\n{{tag>Bookmark}}";
85    #$wikitpl = "====== @T@ ======\n~~META:url=@U@~~\n@S@\n\n{{tag>Bookmark}}";
86    $wikitpl  = str_replace('\n',"\n", $this->getConf('wikitemplate'));
87
88    $foo = $_SERVER['REMOTE_USER'];
89    if (!isset($foo) || empty($foo)) {
90      $foo= 'anonymous';
91    }
92
93    $data=array(
94      'baseurl'   => $conf['baseurl'].DOKU_BASE.'doku.php',
95      'wikiidtpl' => $this->getConf('namespace'),
96      'wikitpl'   => $wikitpl,
97      'timestamp' => $timestamp,
98      'title'     => $title,
99      'url'       => $url,
100      'foo'       => $foo,
101      'selection' => $selection);
102
103    $dwtpl=pageTemplate(array(parseWikiIdTemplate($data['wikiidtpl'], $data)));
104    if ($dwtpl) {
105      $data['wikitpl'] = $dwtpl;
106    }
107
108    # parse Preset configuration
109    $cfg_presets=array();
110    foreach (explode(';', trim($this->getConf('presets'))) as $p) {
111      $l=explode('=',$p,2);
112      $d=explode('|',$l[1],2);
113      if (empty($d[0]) || empty($l[0])) continue;
114
115      $tpl='';
116      if (!empty($d[1])) {
117	# TODO: optionally specify template-file instead of 'namespace:_template.txt'
118        #$file = wikiFN($d[1]);
119        #if (@file_exists($file)){
120        #   $tpl = io_readFile($file);
121        # TODO: replace Placeholders alike ../../../inc/common.php pageTemplate() ?!
122        #} else {
123          $tpl = pageTemplate(array($d[1]));
124        #}
125      }
126
127      # allow ID-only presets, if template ns == '-'
128      if (empty($tpl) && $d[1] != '-') {
129        $tpl = str_replace('\n',"\n", $this->getConf('wikitemplate'));
130      }
131
132      $n=parseWikiIdTemplate($d[0], $data);
133      $file = wikiFN($n);
134
135		# TODO: check if we'd be allowed to create/edit this page
136		# else save will fail later :( - or hide this preset,
137		# or push session on stack and opt to log-on.
138
139      # check if a page with this preset's ID already exists
140      if (@file_exists($file)){
141        msg('preset \''.$l[0].'\' - a page with <a href="'.wl($n).'">this ID</a> already exists.', -1);
142      } else {
143        $cfg_presets[$l[0]]=array('id' => $d[0], 'tpl' => $tpl);
144      }
145
146    } # done.  now $cfg_presets holds an array of presets;
147
148    $options   = array(
149      'enable_save' => $this->getConf('enable_save'),
150      'preset'      => count($cfg_presets)>0,
151      'presets'     => $cfg_presets
152    );
153
154    # output the page and form
155
156    printHeader();
157    if(function_exists('html_msgarea')){
158      html_msgarea();
159    }
160    printForm($data, $options, null);
161    printFooter();
162    exit;
163  }
164
165  /**
166   *
167   * - used only if 'enabled_save' is configured.
168   */
169  function _save_bookmark(&$event) {
170    global $conf;
171    global $ACT;
172    global $ID;
173
174    // we can handle it -> prevent others
175    $event->stopPropagation();
176    $event->preventDefault();
177
178    // check if we are allowed to create this file
179    if (auth_quickaclcheck($ID) < AUTH_CREATE){
180      $ACT = 'show';
181      msg('You may not create bookmarks in this namespace - go back with your browser\'s back-button and change the page ID.',-1);
182      return;
183    }
184
185    $file = wikiFN($ID);
186
187    //check if locked by anyone - if not lock for my self
188    if (checklock($ID)){
189      $ACT = 'locked';
190      #return;  ??
191    } else {
192      lock($ID);
193    }
194
195    if (@file_exists($file)){
196      $ACT = 'edit';
197      msg('Page did already exist. entered edit mode. feel free to go back with your browser\'s back-button and change the page ID.',-1);
198      return;
199    }
200
201    global $TEXT;
202    global $INFO;
203    global $conf;
204
205    $TEXT = $_POST['wikitext'];
206    if (!$TEXT) {
207      $ACT = 'show';
208      msg('empty wiki page text. page has not been created.',-1);
209      return;
210    }
211    #if (!$TEXT)
212    #  $TEXT = pageTemplate($ID);# FIXME: evaluate $this->conf('namespace') ?!
213    #if (!$TEXT) $TEXT = "====== $title ======\n\n\n\n".
214    #                    "{{tag>Bookmark}}\n"; # TODO wrap $_GET['ur']; ?!
215
216    if(checkSecurityToken()){
217      $ACT = act_save($ACT);
218    } else {
219      $ACT = 'show';
220      msg('Security Token did not match. Possible CSRF attack.',-1);
221    }
222  }
223}
224//Setup VIM: ex: et ts=2 :
225