[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/wiki/ewiki/plugins/ -> spages.php (source)

   1  <?php
   2  
   3  /*
   4     The StaticPages plugin allows you to put some .html or .php files
   5     into dedicated directories, which then will get available with their
   6     basename as ewiki pages. The files can be in wiki format (.txt or no
   7     extension), they can also be in .html format and they may even contain
   8     php code (.php).
   9  
  10     Of course it is not possible to provide anything else, than viewing
  11     those pages (editing is not possible), but it is of course up to you
  12     to add php code to achieve some interactivity.
  13     The idea for this plugin was 'borought' from http://geeklog.org/.
  14  
  15     In your static page .php files you cannot do everything you could
  16     normally do, there are some restrictions because of the way these static
  17     pages are processed. You need to use $GLOBALS to access variables other
  18     than the $ewiki_ ones. To return headers() you must append them to the
  19     $headers[] or $ewiki_headers[] array.
  20  
  21     If you define("EWIKI_SPAGES_DIR") then this directory will be read
  22     initially, but you could also just edit the following list/array of 
  23     directories, or call ewiki_init_spages() yourself.
  24  */
  25  
  26  
  27  #-- specify which dirs to search for page files
  28  ewiki_init_spages(
  29     array(
  30        "spages",
  31        # "/usr/local/share/wikipages",
  32        # "C:/Documents/StaticPages/",
  33     )
  34  );
  35  if (defined("EWIKI_SPAGES_DIR")) {
  36     ewiki_init_spages(EWIKI_SPAGES_DIR);
  37  }
  38  
  39  
  40  #-- plugin glue
  41  # - will be added automatically by _init_spages()
  42  
  43  
  44  #-- return page
  45  function ewiki_spage($id, $data, $action) {
  46  
  47     global $ewiki_spages, $ewiki_plugins;
  48  
  49     $r = "";
  50  
  51     #-- filename from $id
  52     $fn = $ewiki_spages[strtolower($id)];
  53  
  54     #-- php file
  55     if (strpos($fn, ".php") || strpos($fn, ".htm")) {
  56  
  57        #-- start new ob level
  58        ob_start();
  59        ob_implicit_flush(0);
  60  
  61        #-- prepare environment
  62        global $ewiki_id, $ewiki_title, $ewiki_author, $ewiki_ring,
  63               $ewiki_t, $ewiki_config, $ewiki_action, $_EWIKI,
  64               $ewiki_auth_user, $ewiki_headers, $headers;
  65        $ewiki_headers = array();
  66        $headers = &$ewiki_headers;
  67  
  68        #-- execute script
  69        include($fn);
  70  
  71        #-- close ob
  72        $r = ob_get_contents();
  73        ob_end_clean();
  74  
  75        #-- add headers
  76        if ($ewiki_headers) {
  77           headers(implode("\n", $ewiki_headers));
  78        }
  79     }
  80  
  81     #-- wiki file
  82     else {
  83  
  84        $f = fopen($fn, "rb");
  85        $r = fread($f, 256<<10);
  86        fclose($f);
  87  
  88        $r = $ewiki_plugins["render"][0]($r);
  89     }
  90  
  91     #-- strip <html> and <head> parts (if any)
  92     if (($l = strpos(strtolower($r), "<body")) &&
  93         ($w = strpos(strtolower($r), "</body"))  )
  94     {
  95        $l = strpos($r, ">", $l+1) + 1;
  96        $w = $w - $l;
  97        $r = substr($r, $l, $w);
  98     }
  99  
 100     #-- return body (means successful handled)
 101     return($r);
 102  
 103  }
 104  
 105  
 106  
 107  #-- return page
 108  #<old># $ewiki_plugins["handler"][] = "ewiki_handler_spages";
 109  function ewiki_handler_spages($id, $data, $action) {
 110  
 111     global $ewiki_spages;
 112  
 113     #-- compare requested page $id with spages` $id values
 114     $i0 = strtolower($id);
 115     foreach ($ewiki_spages as $i1 => $fn) {
 116        if (strtolower($i1)==$i0) {
 117  
 118           return(ewiki_spage($id));
 119  
 120        }
 121     }
 122  
 123  }
 124  
 125  
 126  #-- init
 127  function ewiki_init_spages($dirs, $idprep="") {
 128  
 129     global $ewiki_spages, $ewiki_plugins;
 130  
 131     if (!is_array($dirs)) {
 132        $dirs = array($dirs);
 133     }
 134  
 135     #-- go through list of directories
 136     foreach ($dirs as $dir) {
 137  
 138        if (empty($dir)) {
 139           continue;
 140        }
 141  
 142        #-- read in one directory
 143        $dh = opendir($dir);
 144        while (false !== ($fn = readdir($dh))) {
 145  
 146           #-- skip over . and ..
 147           if ($fn[0] == ".") { continue; }
 148  
 149           #-- be recursive
 150           if ($fn && is_dir("$dir/$fn")) {
 151              if ($fn != trim($fn, ".")) {
 152                 $fnadd = trim($fn, ".") . ".";
 153              }
 154              else {
 155                 $fnadd = "$fn/";
 156              }
 157  
 158              ewiki_init_spages(array("$dir/$fn"), "$idprep$fnadd");
 159  
 160              continue;
 161           }
 162  
 163           #-- strip filename extensions
 164           $id = str_replace(
 165                    array(".html", ".htm", ".php", ".txt", ".wiki", ".src"),
 166                    "",
 167                    basename($fn)
 168           );
 169  
 170           #-- register spage file and as page plugin (one for every spage)
 171           $ewiki_spages[strtolower("$idprep$id")] = "$dir/$fn";
 172           $ewiki_plugins["page"]["$idprep$id"] = "ewiki_spage";
 173  
 174        }
 175        closedir($dh);
 176     }
 177  
 178  }
 179  
 180  
 181  
 182  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7