<?php// Zpracovává odkazy ve formátu http://www.mlab.cz/PermaLink/Projekt/DalšíCesta// Skript se volá ze souborů /PermaLink.cs.php a /PermaLink.en.php kde se nastavuje proměnná $Lang// První slovo (až po lomítko) za PermaLink se chápe jako jméno modulu/projektu nebo adresáře/sekce.// Pokud zadanému jménu odpovídá jediný odkaz, provede se hned přesměrování, jinak se vypíše seznam kandidátů.// Pro snadné odkázání lze permanentní odkaz doplnit cestou k adresáři či souboru v rámci projektu.define('DISPLAY_ALL_IF_EMPTY' , true); // Povol zobrazení všech sekcí a projektů když je zadaný prázdný permalinkdefine('MAX_DEPTH', 50); // Maximální hloubka prohledávání adresářů (pojistka proti zacyklené struktuře adresářů)require_once('Common.php'); // Modul společných funkcírequire_once('DirMetadata.php'); // Modul pro načítání informace o adresáříchrequire_once('TemplateEngine.php'); // Stroj pro práci s templejty// Převezmi odkazif($_REQUEST['PermaLink']!='')// Odkaz z formuláře$LinkFull = $_REQUEST['PermaLink'];else// Odkaz z cesty$LinkFull = $_SERVER['PATH_INFO'];// Decode entities ?// $LinkFull=htmlspecialchars_decode($LinkFull);// Split PermaLink and aditional Path$LinkFull = ltrim($LinkFull, DIRECTORY_SEPARATOR);$LinkFull = rtrim($LinkFull, DIRECTORY_SEPARATOR);$SeparatorPosition = strpos($LinkFull, DIRECTORY_SEPARATOR);if ($SeparatorPosition===false){$LinkPath = '';$Link = $LinkFull;}else{$LinkPath = substr($LinkFull, $SeparatorPosition+1);$Link = substr($LinkFull, 0, $SeparatorPosition);}unset($SeparatorPosition);// Najdi cíle pro PermaLink$PermaLinkArray = array();if(DISPLAY_ALL_IF_EMPTY or !($Link==='')){// Načti seznam všech projektů a sekcí (od začátku)$ProjectsAndSections = RecurseDirs('/');// Převeď projekty a sekce na permalinky$AllPermas = PermaLinkCreateArray($ProjectsAndSections);unset($ProjectsAndSections);if($LinkFull===''){// Prázdný permalink - zkopíruj vše$PermaLinkArray = $AllPermas;}else{// Najdi zadaný permalink$PermaLinkArray = PermaLinkFind($AllPermas, PermaLinkCreateKey($Link));}unset($AllPermas);}$PermaLinkCount = count($PermaLinkArray);//print_rx('Found Permalinks', $PermaLinkArray);// Nalezen právě jeden odkaz bez přídavné cesty - rovnou přesměrujif($PermaLinkCount==1 and $LinkPath==='')Redirect($PermaLinkArray[0]['Path'], $Lang); // ------------------------------------->>>// Setřiď nalezené (třídí primárně podle první položky, což je lowercase verze permalinku)sort($PermaLinkArray);// ***************************// Zpracuj PermaLink bez cesty// ***************************if ($LinkPath===''){// Založ objekt templejtu a načti jej$Template = new TemplateEngine();$error = $Template->LoadTemplate($_SERVER['DOCUMENT_ROOT'].'/'.TEMPLATE_PERMALINK, $Lang);if ($error!='')Error($error);// Generuj dynamický obsah do proměnných (společná část)$Template->SetVariable('InfoThisLink', $LinkFull );$Template->SetVariable('InfoThisServer', $_SERVER['HTTP_HOST']);// Nalezeno více položek - zjisti více informací a vlož je do templejtu$Lines = '';if($PermaLinkCount>1){// Nachystej společné hodnoty z templejtu$PictureGlue = $Template->GetVariable('InfoPictureGlue');$PictureAlt = $Template->GetVariable('InfoPictureAlt');$PictureNoPicture = '/'.TEMPLATE_PATH.'/'.$Template->GetVariable('InfoNoPicture');// Načti informace o projektu$DirInfo = new DirMetadata();foreach($PermaLinkArray as $Data){// Naplň proměnné pro definici položky// Type / Picture / PermaLink / Path$Variables['InfoType'] = $Data['Type'];$Variables['InfoPicture'] = CreatePicturesLink($Data['Path'], $PictureGlue, $PictureAlt, $PictureNoPicture);$Variables['InfoPermaLink'] = "<a href=\"".PermaLinkCreateLink($Data['PermaLink'])."\">{$Data['PermaLink']}</a>";$PathMarked = MarkPath($Data['Path']);$Variables['InfoPath'] = "<a href=\"".CreateProjectLink($Data['Path'], $Lang)."\">$PathMarked</a>";unset($PathMarked);// Zjisti informace o projektuif($Data['Type']==="Section")$DirInfo->LoadDirInfo($Data['Path'].'/'.DIRINFO, $Lang);else$DirInfo->LoadDirInfo($Data['Path'].'/'.PRJINFO, $Lang);// Vlož všechny zjištěné informaceforeach($DirInfo->DirData as $Key => $ItemInfo)$Variables[$Key] = $ItemInfo;// Ošetři chybějící popisif($Variables['InfoShortDescription']=='' and $Variables['InfoLongDescription']=='')$Variables['InfoShortDescription'] = $Template->GetVariable('InfoNoText');// Proveď substituci a střádej výsledek (po položkách)$Lines .= $Template->Substitute($Variables, $Template->GetVariable('InfoLine'));// Zahoď nalezená data (aby se nepoužily v dalším cyklu)unset($Variables);}// Vlož řádky do templejtu$Template->SetVariable('InfoLine', $Lines);}else{// Nenalezeno nic (nebo prázdné zadání)$Template->SetVariable('InfoLine', $Template->GetVariable('InfoNoData'));}// Odešli výsledek klientoviecho $Template->GetPage();return;}else// **************************// Zpracuj PermaLink s cestou// **************************{// Založ objekt templejtu a načti jej$Template = new TemplateEngine();$error = $Template->LoadTemplate($_SERVER['DOCUMENT_ROOT'].'/'.TEMPLATE_PERMALINK_PATH, $Lang);if ($error!='')Error($error);// Generuj dynamický obsah do proměnných (společná část)$Template->SetVariable('InfoThisLink', $LinkFull );$Template->SetVariable('InfoThisServer', $_SERVER['HTTP_HOST']);// Načti proměnné z templejtu$Glue = $Template->GetVariable('InfoNamesGlue');// Prohledej adresáře (nestriktní verze)// -------------------------------------$FilesPathArray = array();$FilesPathCount = 0;// Rozděl cestu na dílky$LinkPathArray = explode('/', $LinkPath);$LinkPathDepth = count($LinkPathArray);//print_rx("Path to be found", $LinkPathArray);// Cyklus přes všechny nalezené permalinkyforeach($PermaLinkArray as $Item){// Rekurentně procházej atrukturu adresáře// Podadresáře přidávej průběžně na konec pole// První položka je hloubka zanoření (indexuje $LinkPathArray)// Střádej nalezené položky do pole $LinkPathData$Dirs = array();$Dirs[] = array('Depth'=>0, 'Path'=>$Item['Path']);// Omez hloubku procházení adresářůfor ($i=0; $i<count($Dirs) and $i<MAX_DEPTH; $i++){//print "At Depth " . $Dirs[$i]['Depth'] . " Reading Directory " . $Dirs[$i]['Path'] . PHP_EOL;//print "Looking for " . $LinkPathArray[$Dirs[$i]['Depth']] . PHP_EOL;$GlobFlags = GLOB_NOSORT;if ($LinkPathDepth>$Dirs[$i]['Depth']+1){// Nejsme ještě u konce cesty - přidej jen adresáře$GlobFlags |= GLOB_ONLYDIR;}// Čti adresáře$SubDirs = glob($_SERVER['DOCUMENT_ROOT'].$Dirs[$i]['Path'].DIRECTORY_SEPARATOR.'*', $GlobFlags);foreach($SubDirs as $Item){// Převeď na cestu od rootu webu$CurentName = basename($Item);$CurentPath = $Dirs[$i]['Path' ];$CurentDepth = $Dirs[$i]['Depth'];//print " $CurentName\n";// ===> POROVNÁNÍ JE ZDE <===if(mb_strtolower(PermaLinkFilter($CurentName))===mb_strtolower(PermaLinkFilter($LinkPathArray[$CurentDepth]))){//print" ^----> $CurentDepth : $CurentPath/$CurentName\n";// Přidej další adresář do rekurzeif(is_dir($Item)){$Dirs[] = array('Depth'=>$CurentDepth+1, 'Path'=>$CurentPath.'/'.$CurentName);}// Nalezený soubor v adresářiif(is_file($Item)){//print " ^----> File: $Item\n";$FilesPathArray[$Dirs[$i]['Path']][]=$CurentName;$FilesPathCount++;}}// V posledním adresáři přidej všechny souboryif($CurentDepth>=$LinkPathDepth){//print " Test ".$_SERVER['DOCUMENT_ROOT'].$CurentPath.'/'.$CurentName.PHP_EOL;if (is_file($_SERVER['DOCUMENT_ROOT'].$CurentPath.'/'.$CurentName)){//print " ^----> File All: $Item\n";$FilesPathArray[$CurentPath][]=$CurentName;$FilesPathCount++;}}}//print("\n");}}//print_rx('Found Files', $FilesPathArray);//print "Files Found Count $FilesPathCount\n";/*// Prohledej adresáře a soubory (striktní verze)// ---------------------------------------------$EatIt = strlen($_SERVER['DOCUMENT_ROOT']);foreach($PermaLinkArray as $Item){$Path = $_SERVER['DOCUMENT_ROOT'].$Item['Path'].DIRECTORY_SEPARATOR.$LinkPath;if (is_file($Path)){$FilesPathArray[substr(dirname($Path), $EatIt)] = array(basename($Path));$FilesPathCount++;}if (is_dir($Path)){foreach (scandir($Path) as $DirItem){if (is_file($Path.DIRECTORY_SEPARATOR.$DirItem)){$FilesPathArray[substr($Path, $EatIt)][]=$DirItem;$FilesPathCount++;}}}}print_rx('Nalezene soubory', $FilesPathArray);print "$FilesPathCoutn\n";*/// Nalezen právě jeden odkaz na soubor - rovnou přesměrujif($FilesPathCount===1){reset($FilesPathArray);$tmp = current($FilesPathArray);$Path = key($FilesPathArray).'/'.$tmp[0];RedirectToFile($Path); // ------------------------------------->>>}// Nalezeno více položek - zjisti více informací a vlož je do templejtu$Lines = '';if($FilesPathCount>0){foreach($FilesPathArray as $Path=>$FileArray){$Variables['InfoName']=CreateFilesLink($Path, '<br>', '');$FileNames = '';foreach($FileArray as $FileItem){$FileNames .= '<br> '.$FileItem;}$Variables['InfoPath'] = $Path;$Variables['InfoNames'] = CreateFilesLink2($Path, $FileArray, $Glue);// Proveď substituci a střádej výsledek (po položkách)$Lines .= $Template->Substitute($Variables, $Template->GetVariable('InfoLine'));// Zahoď nalezená data (aby se nepoužily v dalším cyklu)unset($Variables);}// Vlož řádky do templejtu$Template->SetVariable('InfoLine', $Lines);}else{// Nenalezeno nic$Template->SetVariable('InfoLine', $Template->GetVariable('InfoNoData'));}// Odešli výsledek klientoviecho $Template->GetPage();return;}?>