1<?php
2if(!defined('DOKU_INC')) die();
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once(DOKU_PLUGIN.'action.php');
5
6class action_plugin_whennotfound extends DokuWiki_Action_Plugin {
7    function getInfo(){ return conf_loadfile(dirname(__FILE__).'/info.txt'); }
8	  function register($contr){
9    $contr->register_hook('ACTION_ACT_PREPROCESS','BEFORE',$this,'handle_action',array());
10    $contr->register_hook('TPL_CONTENT_DISPLAY', 'AFTER', $this, 'handle_content', array());
11   }
12  function handle_action(&$e, $param){
13    if($e->data != 'show') return;
14    global $ID;
15
16    if($_GET['whennotfounded']){
17      msg("You are automatically redirected here from the non-existent page [".hsc($_GET['whennotfounded'])."].".(auth_quickaclcheck($_GET['whennotfounded'])>=AUTH_CREATE ? " If you did not want to be redirected, you may also <a href='".wl($_GET['whennotfounded'], "do=edit")."'>create and edit [".hsc($_GET['whennotfounded'])."]</a>":''));
18      return;
19    }
20    if(is_file(wikiFN($ID))) return;
21    global $INFO;
22    global $conf;
23
24
25    $excludes=$this->getConf('excludepatterns');
26    if(is_string($excludes)) $excludes=explode(',',$excludes);
27    foreach($excludes as $exclude){
28      if(strpos($ID,$exclude)!==false) return;
29    }
30    $actions=$this->getConf('actions');
31    if(is_string($actions)) $actions=explode(',',$actions);
32    foreach($actions as $action){
33      $func="do_$action";
34      if(is_callable([$this,$func])){
35        if($this->$func($e)) break;
36      }
37      else dbg("Undefined whennotfound thing to do [".hsc($action)."]");
38    }
39  }
40  function handle_content(&$e, $param){
41    global $action_plugin_whennotfound_pagelist;
42    if($action_plugin_whennotfound_pagelist) echo $action_plugin_whennotfound_pagelist;
43  }
44
45  function do_send404(&$e){
46    header('HTTP/1.0 404 Not Found');
47    die();
48  }
49  function do_send404_onlynoneditor(&$e){
50    global $ID;
51    if(auth_quickaclcheck($ID)>=AUTH_EDIT) return;
52    $this->do_send404($e);
53  }
54  function do_startpage(&$e){
55    global $ID;
56    $startpages = explode(',',$this->getConf('startpages'));
57    foreach($startpages as $index){
58      if($index == '@subpage_with_same_name') $index = noNS($ID);
59      if(is_file(wikiFN("$ID:$index"))){
60        #Redirect. Add whennotfounded param if user has CREATE permission, so on the resulting page they can be prompted if they intended to create this $ID page instead of being redirected there.
61        $get=$_GET; unset($get['id']);
62        if(auth_quickaclcheck($ID)>=AUTH_CREATE) $get['whennotfounded']=$ID;
63        header("Location: ".wl("$ID:$index", $get,null,'&'));
64        die();
65      }
66    }
67  }
68  function do_translation(&$e){
69    global $ID;
70    global $conf;
71    if(!($h =& plugin_load('helper', 'translation'))) return;
72    if($id=$h->getavailableid($ID)){
73      $get=$_GET;
74      $get['lang']=$conf['lang'];
75      if(auth_quickaclcheck($ID)>=AUTH_CREATE) $get['whennotfounded']=$ID;
76      header("Location: ".wl($id, $get,null,'&'));
77      die();
78    }
79  }
80  function do_pagelist(&$e){
81    global $ID;
82    if(!is_dir(dirname(wikiFN("$ID:dummy"))) || !($h =& plugin_load('syntax', 'indexmenu_indexmenu'))) return;
83    if(!($renderer = p_get_renderer('xhtml'))) return;
84    $handler = new Doku_Handler();
85    $handled = $h->handle("{{indexmenu>:$ID#2|js#default}}",$renderer,null,$handler);
86    $h->render('xhtml',$renderer, $handled);
87
88    global $action_plugin_whennotfound_pagelist;
89    $action_plugin_whennotfound_pagelist=$renderer->doc; #this'll be printed later in handle_content
90    msg("The page you requested [".hsc($ID)."] is a namespace and does not exist as a separate page. A search action is triggered for that page name below."
91      .( auth_quickaclcheck($ID)>=AUTH_CREATE ? " You may also <a href='".wl($ID, "do=edit")."'>create and edit [".hsc($ID)."]</a>." : "")
92    );
93    return true;
94   }
95
96   function do_search(&$e){
97    global $ID;
98    if(!actionOK('search')) return;
99    msg("The page you requested [".hsc($ID)."] is a namespace and does not exist as a separate page. A search action is triggered for that page name below."
100      .( auth_quickaclcheck($ID)>=AUTH_CREATE ? " You may also <a href='".wl($ID, "do=edit")."'>create and edit [".hsc($ID)."]</a>." : "") );
101    $e->data = 'search';
102    return true;
103  }
104}
105