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 OIDplusPagePublicLinks extends OIDplusPagePlugin {
  21.         public function type() {
  22.                 return 'public';
  23.         }
  24.  
  25.         public function priority() {
  26.                 return 501;
  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 getHyperlinkTitle($file) {
  42.                 return preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($file));
  43.         }
  44.  
  45.         private static function getHyperlinkURL($file) {
  46.                 /*
  47.                 [{000214A0-0000-0000-C000-000000000046}]
  48.                 Prop3=19,2
  49.                 [InternetShortcut]
  50.                 URL=http://www.example.com/
  51.                 IDList=
  52.                 */
  53.                 $cont = file_get_contents($file);
  54.                 if (!preg_match('@URL=(.+)\n@ismU', $cont, $m)) return null;
  55.                 return trim($m[1]);
  56.         }
  57.  
  58.         public function gui($id, &$out, &$handled) {
  59.                 if (explode('$',$id)[0] === 'oidplus:links') {
  60.                         $handled = true;
  61.  
  62.                         $file = @explode('$',$id)[1];
  63.                         $auth = @explode('$',$id)[2];
  64.  
  65.                         if (!OIDplus::authUtils()::validateAuthKey("oidplus:links;$file", $auth)) {
  66.                                 $out['title'] = 'Access denied';
  67.                                 $out['icon'] = 'img/error_big.png';
  68.                                 $out['text'] = '<p>Invalid authentification token</p>';
  69.                                 return $out;
  70.                         }
  71.  
  72.                         if (file_exists($file) && (!is_dir($file))) {
  73.                                 $out['title'] = $this->getHyperlinkTitle($file);
  74.  
  75.                                 $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_big.png';
  76.                                 if (file_exists($icon_candidate)) {
  77.                                         $out['icon'] = $icon_candidate;
  78.                                 } else if (file_exists(__DIR__.'/icon_leaf_big.png')) {
  79.                                         $out['icon'] = 'plugins/publicPages/'.basename(__DIR__).'/icon_leaf_big.png';
  80.                                 } else {
  81.                                         $out['icon'] = '';
  82.                                 }
  83.  
  84.                                 // Should not happen though, due to conditionalselect
  85.                                 $out['text'] = '<a href="'.htmlentities(self::getHyperlinkURL($file)).'" target="_blank">Open in new window</a>';
  86.                         } else if (is_dir($file)) {
  87.                                 $out['title'] = ($file == 'links/') ? 'External resources' : basename($file);
  88.  
  89.                                 if ($file == 'links/') {
  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:links$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:links;$dir").'">'.$ic.' '.htmlentities(basename($dir)).'</a></p>';
  127.                                         $count++;
  128.                                 }
  129.  
  130.                                 $files = glob($file.'/*.url');
  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="'.htmlentities(self::getHyperlinkURL($file)).'" target="_blank">'.$ic.' '.htmlentities($this->getHyperlinkTitle($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 resource doesn\'t exist anymore.</p>';
  154.                                 return $out;
  155.                         }
  156.                 }
  157.         }
  158.  
  159.         private function tree_rec(&$children, $rootdir='links/', $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:links$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:links;$dir"),
  179.                                 'icon' => $tree_icon,
  180.                                 'text' => basename($dir),
  181.                                 'children' => $tmp
  182.                         );
  183.                 }
  184.  
  185.                 $files = glob($rootdir.'*.url');
  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:links$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:links;$file"),
  198.                                 'conditionalselect' => 'window.open('.js_escape(self::getHyperlinkURL($file)).'); false;',
  199.                                 'icon' => $tree_icon,
  200.                                 'text' => $this->getHyperlinkTitle($file)
  201.                         );
  202.                 }
  203.         }
  204.  
  205.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  206.                 $children = array();
  207.  
  208.                 $this->tree_rec($children, 'links/');
  209.  
  210.                 if (count($children) > 0) {
  211.                         if (file_exists(__DIR__.'/treeicon.png')) {
  212.                                 $tree_icon = 'plugins/publicPages/'.basename(__DIR__).'/treeicon.png';
  213.                         } else {
  214.                                 $tree_icon = null; // default icon (folder)
  215.                         }
  216.  
  217.                         $json[] = array(
  218.                                 'id' => 'oidplus:links$links/$'.OIDplus::authUtils()::makeAuthKey("oidplus:links;links/"),
  219.                                 'icon' => $tree_icon,
  220.                                 'state' => array("opened" => true),
  221.                                 'text' => 'External resources',
  222.                                 'children' => $children
  223.                         );
  224.                 }
  225.  
  226.                 return true;
  227.         }
  228. }
  229.  
  230. OIDplus::registerPagePlugin(new OIDplusPagePublicLinks());
  231.