Subversion Repositories oidplus

Rev

Rev 104 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. class OIDplusPagePublicDocuments extends OIDplusPagePlugin {
  21.         public function type() {
  22.                 return 'public';
  23.         }
  24.  
  25.         public function priority() {
  26.                 return 500;
  27.         }
  28.  
  29.         public function action(&$handled) {
  30.                 // Nothing
  31.         }
  32.  
  33.         public function init($html=true) {
  34.                 // Nothing
  35.         }
  36.  
  37.         public function cfgSetValue($name, $value) {
  38.                 // Nothing
  39.         }
  40.  
  41.         private static function getDocumentTitle($file) {
  42.                 $cont = file_get_contents($file);
  43.                 if (preg_match('@<title>(.+)</title>@ismU', $cont, $m)) return $m[1];
  44.                 if (preg_match('@<h1>(.+)</h1>@ismU', $cont, $m)) return $m[1];
  45.                 if (preg_match('@<h2>(.+)</h2>@ismU', $cont, $m)) return $m[1];
  46.                 if (preg_match('@<h3>(.+)</h3>@ismU', $cont, $m)) return $m[1];
  47.                 if (preg_match('@<h4>(.+)</h4>@ismU', $cont, $m)) return $m[1];
  48.                 if (preg_match('@<h5>(.+)</h5>@ismU', $cont, $m)) return $m[1];
  49.                 if (preg_match('@<h6>(.+)</h6>@ismU', $cont, $m)) return $m[1];
  50.                 return basename($file);
  51.         }
  52.  
  53.         public function gui($id, &$out, &$handled) {
  54.                 if (explode('$',$id)[0] === 'oidplus:documents') {
  55.                         $handled = true;
  56.  
  57.                         $file = @explode('$',$id)[1];
  58.                         $auth = @explode('$',$id)[2];
  59.  
  60.                         if (!OIDplus::authUtils()::validateAuthKey("oidplus:documents;$file", $auth)) {
  61.                                 $out['title'] = 'Access denied';
  62.                                 $out['icon'] = 'img/error_big.png';
  63.                                 $out['text'] = '<p>Invalid authentification token</p>';
  64.                                 return $out;
  65.                         }
  66.  
  67.                         if (file_exists($file) && (!is_dir($file))) {
  68.                                 $out['title'] = $this->getDocumentTitle($file);
  69.  
  70.                                 $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_big.png';
  71.                                 if (file_exists($icon_candidate)) {
  72.                                         $out['icon'] = $icon_candidate;
  73.                                 } else if (file_exists(__DIR__.'/icon_leaf_big.png')) {
  74.                                         $out['icon'] = 'plugins/publicPages/'.basename(__DIR__).'/icon_leaf_big.png';
  75.                                 } else {
  76.                                         $out['icon'] = '';
  77.                                 }
  78.  
  79.                                 $cont = file_get_contents($file);
  80.                                 $cont = preg_replace('@^(.+)<body@isU', '', $cont);
  81.                                 $cont = preg_replace('@</body>.+$@isU', '', $cont);
  82.                                 $cont = preg_replace('@<title>.+</title>@isU', '', $cont);
  83.                                 $cont = preg_replace('@<h1>.+</h1>@isU', '', $cont, 1);
  84.  
  85.                                 $out['text'] = $cont;
  86.                         } else if (is_dir($file)) {
  87.                                 $out['title'] = ($file == 'doc/') ? 'Documents' : basename($file);
  88.  
  89.                                 if ($file == 'doc/') {
  90.                                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/publicPages/'.basename(__DIR__).'/icon_big.png' : '';
  91.                                 } else {
  92.                                         $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_big.png';
  93.                                         if (file_exists($icon_candidate)) {
  94.                                                 $out['icon'] = $icon_candidate;
  95.                                         } else if (file_exists(__DIR__.'/icon_folder_big.png')) {
  96.                                                 $out['icon'] = 'plugins/publicPages/'.basename(__DIR__).'/icon_folder_big.png';
  97.                                         } else {
  98.                                                 $out['icon'] = null; // no icon
  99.                                         }
  100.                                 }
  101.  
  102.                                 if (file_exists(__DIR__.'/treeicon.png')) {
  103.                                         $tree_icon = 'plugins/publicPages/'.basename(__DIR__).'/treeicon.png';
  104.                                 } else {
  105.                                         $tree_icon = null; // default icon (folder)
  106.                                 }
  107.  
  108.                                 $out['text'] = '';
  109.  
  110.                                 $count = 0;
  111.  
  112.                                 $dirs = glob($file.'*'.'/', GLOB_ONLYDIR);
  113.                                 asort($dirs);
  114.                                 foreach ($dirs as $dir) {
  115.                                         $icon_candidate = pathinfo($dir)['dirname'].'/'.pathinfo($dir)['filename'].'_tree.png';
  116.                                         if (file_exists($icon_candidate)) {
  117.                                                 $tree_icon = $icon_candidate;
  118.                                         } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
  119.                                                 $tree_icon = 'plugins/publicPages/'.basename(__DIR__).'/treeicon_folder.png';
  120.                                         } else {
  121.                                                 $tree_icon = null; // no icon
  122.                                         }
  123.  
  124.                                         $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
  125.  
  126.                                         $out['text'] .= '<p><a href="?goto=oidplus:documents$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:documents;$dir").'">'.$ic.' '.htmlentities(basename($dir)).'</a></p>';
  127.                                         $count++;
  128.                                 }
  129.  
  130.                                 $files = glob($file.'/*.htm*');
  131.                                 asort($files);
  132.                                 foreach ($files as $file) {
  133.                                         $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
  134.                                         if (file_exists($icon_candidate)) {
  135.                                                 $tree_icon = $icon_candidate;
  136.                                         } else if (file_exists(__DIR__.'/treeicon_leaf.png')) {
  137.                                                 $tree_icon = 'plugins/publicPages/'.basename(__DIR__).'/treeicon_leaf.png';
  138.                                         } else {
  139.                                                 $tree_icon = null; // default icon (folder)
  140.                                         }
  141.                                         $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
  142.  
  143.                                         $out['text'] .= '<p><a href="?goto=oidplus:documents$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:documents;$file").'">'.$ic.' '.htmlentities($this->getDocumentTitle($file)).'</a></p>';
  144.                                         $count++;
  145.                                 }
  146.  
  147.                                 if ($count == 0) {
  148.                                         $out['text'] .= '<p>This folder does not contain any elements</p>';
  149.                                 }
  150.                         } else {
  151.                                 $out['title'] = 'Not found';
  152.                                 $out['icon'] = 'img/error_big.png';
  153.                                 $out['text'] = '<p>This document doesn\'t exist anymore.</p>';
  154.                                 return $out;
  155.                         }
  156.                 }
  157.         }
  158.  
  159.         private function tree_rec(&$children, $rootdir='doc/', $depth=0) {
  160.                 if ($depth > 100) return false; // something is wrong!
  161.  
  162.                 $dirs = glob($rootdir.'*'.'/', GLOB_ONLYDIR);
  163.                 asort($dirs);
  164.                 foreach ($dirs as $dir) {
  165.                         $tmp = array();
  166.                         $this->tree_rec($tmp, $dir, $depth+1);
  167.  
  168.                         $icon_candidate = pathinfo($dir)['dirname'].'/'.pathinfo($dir)['filename'].'_tree.png';
  169.                         if (file_exists($icon_candidate)) {
  170.                                 $tree_icon = $icon_candidate;
  171.                         } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
  172.                                 $tree_icon = 'plugins/publicPages/'.basename(__DIR__).'/treeicon_folder.png';
  173.                         } else {
  174.                                 $tree_icon = null; // default icon (folder)
  175.                         }
  176.  
  177.                         $children[] = array(
  178.                                 'id' => 'oidplus:documents$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:documents;$dir"),
  179.                                 'icon' => $tree_icon,
  180.                                 'text' => basename($dir),
  181.                                 'children' => $tmp
  182.                         );
  183.                 }
  184.  
  185.                 $files = glob($rootdir.'*.htm*');
  186.                 asort($files);
  187.                 foreach ($files as $file) {
  188.                         $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
  189.                         if (file_exists($icon_candidate)) {
  190.                                 $tree_icon = $icon_candidate;
  191.                         } else if (file_exists(__DIR__.'/treeicon_leaf.png')) {
  192.                                 $tree_icon = 'plugins/publicPages/'.basename(__DIR__).'/treeicon_leaf.png';
  193.                         } else {
  194.                                 $tree_icon = null; // default icon (folder)
  195.                         }
  196.                         $children[] = array(
  197.                                 'id' => 'oidplus:documents$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:documents;$file"),
  198.                                 'icon' => $tree_icon,
  199.                                 'text' => $this->getDocumentTitle($file)
  200.                         );
  201.                 }
  202.         }
  203.  
  204.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  205.                 $children = array();
  206.  
  207.                 $this->tree_rec($children, 'doc/');
  208.  
  209.                 if (count($children) > 0) {
  210.                         if (file_exists(__DIR__.'/treeicon.png')) {
  211.                                 $tree_icon = 'plugins/publicPages/'.basename(__DIR__).'/treeicon.png';
  212.                         } else {
  213.                                 $tree_icon = null; // default icon (folder)
  214.                         }
  215.  
  216.                         $json[] = array(
  217.                                 'id' => 'oidplus:documents$doc/$'.OIDplus::authUtils()::makeAuthKey("oidplus:documents;doc/"),
  218.                                 'icon' => $tree_icon,
  219.                                 'state' => array("opened" => true),
  220.                                 'text' => 'Documents',
  221.                                 'children' => $children
  222.                         );
  223.                 }
  224.  
  225.                 return true;
  226.         }
  227. }
  228.  
  229. OIDplus::registerPagePlugin(new OIDplusPagePublicDocuments());
  230.