1<?php
2/**
3 * DokuWiki Plugin bible (Syntax Component).
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Budi Susanto <budsus@ti.ukdw.ac.id>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'syntax.php';
17
18class syntax_plugin_indobible extends DokuWiki_Syntax_Plugin {
19    function getInfo(){ return confToHash(dirname(__FILE__).'/plugin.info.txt'); }
20
21    # Required. Define plugin type (see https://www.dokuwiki.org/devel:syntax_plugins)
22    public function getType() {
23        return 'substition';
24    }
25
26	# Required. Define paragraph handling (see https://www.dokuwiki.org/devel:syntax_plugins)
27    public function getPType() {
28        return 'normal';
29    }
30
31	# Required. Define sort order to determine which plugin trumps which. This value was somewhat arbitrary
32	## And can be changed without really affecting the plugin. (see https://www.dokuwiki.org/devel:syntax_plugins)
33    public function getSort() {
34        return 275;
35    }
36
37    #Plugin Part 1 of 3. This is the actual plugin. This part defines how to invoke the plugin
38    public function connectTo($mode) {
39      $this->Lexer->addSpecialPattern('\!\*[a-zA-Z0-9:;\-\s]+\*\!', $mode, 'plugin_indobible');
40    }
41
42    #Plugin Part 2 of 3. This is the handler - the workhorse - which takes the matched result from part 1 and modifies it.
43    function handle($match, $state, $pos, Doku_Handler &$handler){
44        $data = array(); # Blank array
45        # Make sure that we're dealing with the match from our regexp in part 1, which is in the DOKU_LEXER_SPECIAL context.
46        switch ($state) {
47            case DOKU_LEXER_SPECIAL :
48                # Okay awesome, lets process that regexp match. Call my custom function called _fetchBibleVerse().
49                $match = trim($match, "!*");
50                $match = trim($match, "*!");
51                $bibleLink = $this->_getIndoBibleWS($match);
52                # Modified match obtained! Now return that to Dokuwiki for collection in Part 3.
53                return array($bibleLink, $state, $pos);
54        }
55
56        return $data; # Upon failure, return that blank array
57    }
58
59    #Plugin part 3 of 3. This takes that result from part 2 and actually renders it to the page.
60    public function render($mode, Doku_Renderer &$renderer, $data) {
61        if ($mode != 'xhtml') {
62            return false;
63        } # If mode is not html, like if it is metadata, just return.
64        //dbglog($data[0]);
65        $renderer->doc .= $data[0]; # Otherwise, fetch that $bibleLink (stored in $data[0]) and pass it to the dokuwiki renderer.
66        return true;
67    }
68
69    // Method: POST, PUT, GET etc
70    // Data: array("param" => "value") ==> index.php?param=value
71    // source: http://stackoverflow.com/questions/9802788/call-a-rest-api-in-php
72    public function _CallAPI($method, $url, $data = false) {
73        $curl = curl_init();
74
75        switch ($method) {
76            case "POST":
77                curl_setopt($curl, CURLOPT_POST, 1);
78
79                if ($data)
80                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
81                break;
82            case "PUT":
83                curl_setopt($curl, CURLOPT_PUT, 1);
84                break;
85            default:
86                if ($data)
87                    $url = sprintf("%s?%s", $url, http_build_query($data));
88        }
89
90        // Optional Authentication:
91        curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Content-Type: application/json; charset=utf-8"));
92        curl_setopt($curl, CURLOPT_URL, $url);
93        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
94
95        $result = curl_exec($curl);
96
97        curl_close($curl);
98
99        return $result;
100    }
101
102    public function _getIndoBibleWS($text) {
103      //buat id unik untuk rujukan ayat
104      $nospace = preg_replace('/[:;\-\s]+/', '', $text);
105
106      //ganti spasi dengan +
107      $enctext = str_replace(' ', '+', $text);
108      $url = 'http://ws.wiblika.or.id/alkitab/baca/' . $enctext;
109
110      // ambil json response dari web service
111      $jsonObject = json_decode($this->_CallAPI('GET', $url), true);
112
113      if ($jsonObject && !isset($jsonObject['code'])) {
114      	$isiayat = '';
115        foreach($jsonObject as $k => $o){
116          $isiayat .= '<strong>(' .$o["Alkitab"]["pasal"] . ':' . $o["Alkitab"]["ayat"] . ')</strong> ';
117          $firman = $o["Alkitab"]["firman"];
118          if (preg_match('/^\/(.)+\*$/', $firman)) {
119            $firman = substr($o["Alkitab"]["firman"], 1, -1);
120          }
121          $isiayat .= str_replace('"', '&quot;', $firman) . '<br>';
122        }
123
124        $txtFocus = "";
125        if (preg_match('/Chrome/i', $_SERVER['HTTP_USER_AGENT']) || preg_match('/Firefox/i', $_SERVER['HTTP_USER_AGENT'])) {
126      	  $txtFocus = 'data-trigger="focus"';
127      	}
128      	$txt = '<alkitab><a href="#' . $nospace . '" class="alkitab" role="button" ' . $txtFocus . ' data-container="body" data-placement="bottom" data-toggle="popover" title="'. $text .'" data-content="' . $isiayat . '">' . $text . '</a></alkitab>';
129
130        return $txt;
131      } else {
132        return "[" . $text . "]";
133      }
134    }
135}
136