1<?php
2
3/**
4 * DokuWiki linksenhanced PlugIn - Ajax component
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Böhler <dev@aboehler.at>
7 */
8
9if(!defined('DOKU_INC')) die();
10
11class action_plugin_linksenhanced extends DokuWiki_Action_Plugin {
12
13    function register(Doku_Event_Handler $controller) {
14        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
15        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'add_jsinfo_information');
16        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_parser_cache_use');
17    }
18
19    /**
20     * Add the language variable to the JSINFO variable
21     */
22    function add_jsinfo_information(Doku_Event $event, $param) {
23      global $JSINFO;
24
25      $JSINFO['plugin']['linksenhanced']['sectok'] = getSecurityToken();
26    }
27
28    function handle_ajax_call_unknown(&$event, $param) {
29      if($event->data != 'plugin_linksenhanced') return;
30
31      $event->preventDefault();
32      $event->stopPropagation();
33      global $INPUT;
34
35      $action = trim($INPUT->post->str('action'));
36      $url = trim($INPUT->post->str('url'));
37
38      if(!checkSecurityToken())
39      {
40          echo "CSRF Attack.";
41          return;
42      }
43
44      $data = array();
45
46      $data['result'] = false;
47
48      if($action === 'check')
49      {
50           $client = new DokuHTTPClient();
51           $client->sendRequest($url);
52           $httpcode = $client->status;
53
54           $data['error'] = $client->error;
55           $data['httpcode'] = $httpcode;
56
57           if($httpcode>=200 && $httpcode<300)
58            $data['result'] = true;
59      }
60      else
61      {
62          echo "Unknown operation.";
63          return;
64      }
65
66      // If we are still here, JSON output is requested
67
68      //json library of DokuWiki
69      require_once DOKU_INC . 'inc/JSON.php';
70      $json = new JSON();
71
72      //set content type
73      header('Content-Type: application/json');
74      echo $json->encode($data);
75    }
76
77    function handle_parser_cache_use(&$event, $param) {
78      global $ID;
79      $cache = &$event->data;
80      if(!isset($cache->page)) return;
81
82      $leMeta = p_get_metadata($ID, 'plugin_linksenhanced');
83      if(!$leMeta)
84        return;
85
86      if(isset($leMeta['nocache']) && $leMeta['nocache'] === true)
87      {
88            $event->preventDefault();
89            $event->stopPropagation();
90            $event->result = false;
91      }
92    }
93
94}
95