Share Pastebin
Guest
Public paste!

functions.php

By: a guest | Apr 8th, 2010 | Syntax: PHP | Size: 9.61 KB | Hits: 296 | Expires: Never
Copy text to clipboard
  1. <?php
  2.  
  3. // return: boolean | compares two strings, returns true only if $prefix appears at the beginning of $word
  4. function isPrefix( $word, $prefix)
  5. {
  6.     if ( strlen($word) < strlen($prefix))
  7.     { return false; }
  8.     $word = substr($word, 0, strlen($prefix));
  9.     if ($prefix == $word)
  10.     { return true; }
  11.     return false;
  12. }
  13.  
  14. // return: string | takes two strings, if $prefix is a prefix of $word, then returns $word without prefix, else returns $word
  15. function stripPrefix($word, $prefix)
  16. {
  17.     if (isPrefix($word, $prefix))
  18.     { return substr($word, strlen($prefix)); }
  19.     return $word;
  20. }
  21.  
  22. // return: string | when given a full filename (a.b)returns the extention (b)
  23. function fileExtension($file)
  24. { return strtolower(substr(strrchr($file, '.'), 1)); }
  25.  
  26. // return: boolean | takes a directory, returns true if it contains subdirectories
  27. function hasSubFolders($dir)
  28. {
  29.     // Open a known directory, and proceed to read its contents
  30.     if (is_dir($dir))
  31.     {
  32.         if ($dh = opendir($dir))
  33.         {
  34.             while (($file = readdir($dh)) !== false)
  35.             {
  36.                 if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
  37.                 {
  38.                     return (true);
  39.                 }
  40.             }
  41.             closedir($dh);
  42.         }
  43.     }
  44.     return(false);
  45. }
  46.  
  47. //return: array of two arrays of strings | when given a root address and a subfolder path (folder/subfolder/ect), returns an array of names and array of links to each parent directory
  48. function getBreadcrumbs ($dir)
  49. {
  50.     $directories = explode("/", $dir);
  51.     $paths = array();
  52.    
  53.     for($i = 0; $i<count($directories);$i++)
  54.     {
  55.         $tempPath = "";
  56.         for( $j=1; $j<=$i;$j++)
  57.         {
  58.             $tempPath .= "/" . $directories[$j];
  59.         }
  60.         $paths[] = $tempPath;
  61.     }
  62.     $crumbs = array($directories, $paths);
  63.    
  64.     return $crumbs;
  65. }
  66.  
  67. // return: string | when given a directory path, returns the path to the cover art it contains
  68. function getCover($dir, $address)
  69. {
  70.     $typesArray = array('jpg', 'jpeg');
  71.     $defaultFilename = 'cover.jpg';
  72.     $coverList = array();
  73.  
  74.     // Open a known directory, and proceed to read its contents
  75.     if (is_dir($dir))
  76.     {
  77.         if ($dh = opendir($dir))
  78.         {
  79.             while (($file = readdir($dh)) !== false)
  80.             {
  81.                 if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
  82.                 {/*ignore folders and links*/}
  83.                 else if (in_array(fileExtension($file), $typesArray))
  84.                 {
  85.                     if((strtolower($file) == "cover.jpg") || (strtolower($file) == "cover.jpeg"))
  86.                         return ($address . stripPrefix(($dir .'/'. $file), dirname(__FILE__)));
  87.                     $coverList[] = $address . stripPrefix(($dir .'/'. $file), dirname(__FILE__));
  88.                 }
  89.             }
  90.             closedir($dh);
  91.             if(count($coverList) >0)
  92.                 return($coverList[0]);
  93.         }
  94.     }
  95.     return(recursiveGetCover($dir, $address));
  96. }
  97.  
  98. // return: string | when given a directory path, returns the path to the cover art it contains
  99. function recursiveGetCover($dir, $address)
  100. {
  101.     $typesArray = array('jpg', 'jpeg');
  102.     $defaultFilename = 'cover.jpg';
  103.     $coverList = array();
  104.     $subCoverList = array();
  105.     // Open a known directory, and proceed to read its contents
  106.     if (is_dir($dir))
  107.     {
  108.         if ($dh = opendir($dir))
  109.         {
  110.             while (($file = readdir($dh)) !== false)
  111.             {
  112.                 if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
  113.                 {
  114.                     $temp = recursiveGetCover(($dir .'/'. $file), $address);
  115.                     if($temp !== 0)
  116.                     {
  117.                         $subCoverList[] = $temp;
  118.                     }
  119.                 }
  120.                 else if (in_array(fileExtension($file), $typesArray))
  121.                 {
  122.                     if((strtolower($file) == "cover.jpg") || (strtolower($file) == "cover.jpeg"))
  123.                         return ($address . stripPrefix(($dir .'/'. $file), dirname(__FILE__)));
  124.                     $coverList[] = $address . stripPrefix(($dir .'/'. $file), dirname(__FILE__));
  125.                 }
  126.             }
  127.             closedir($dh);
  128.             if(count($coverList) >0)
  129.                 return($coverList[0]);
  130.             else if (count($subCoverList) >0)
  131.                 return($subCoverList[0]);
  132.         }
  133.     }
  134.     return(0);
  135. }
  136.  
  137. // return: array => array => string, string, boolean, string | when given a directory path, returns an array of subfolder information
  138. // return[0] - array of subfolder names
  139. // return[1] - array of subfolder paths
  140. // return[2] - array of booleans - true is subfolder has subfolders
  141. // return[3] - array of paths to cover art (cover.jpg, first *.jpg in subfolder, default)
  142. function listFolders($dir, $address)
  143. {
  144.     $folderNameList   = array();
  145.     $folderPathList   = array();
  146.     $folderSubFolders = array();
  147.     $folderCover      = array();
  148.     // Open a known directory, and proceed to read its contents
  149.     if (is_dir($dir))
  150.     {
  151.         if ($dh = opendir($dir))
  152.         {
  153.             $tempArray = array();
  154.             while (($file = readdir($dh)) !== false)
  155.             {
  156.                 $tempArray[] = $file;
  157.             }
  158.             closedir($dh);
  159.             sort($tempArray);
  160.             foreach ($tempArray as $file)
  161.             {
  162.                 if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
  163.                 {
  164.                     $folderNameList[] = $file;
  165.                     $folderPathList[] = $dir .'/'. $file;
  166.                     $folderSubFolders[] = hasSubFolders($dir .'/'. $file);
  167.                     $folderCover[] = getCover($dir .'/'. $file, $address);
  168.                 }
  169.             }
  170.             $folderList = array($folderNameList, $folderPathList, $folderSubFolders, $folderCover);
  171.             return ($folderList);
  172.         }
  173.     }
  174.     return(0);
  175. }
  176.  
  177. // return: array => array => string, string, string | when given a directory path, returns an array of file information
  178. // return[0] - array of file names
  179. // return[1] - array of file paths
  180. // return[2] - array of file extensions
  181. function listFiles($dir, $types, $address)
  182. {
  183.     $typesArray = explode('|', $types);
  184.    
  185.     $fileNameList = array();
  186.     $filePathList = array();
  187.     $fileTypeList = array();
  188.     // Open a known directory, and proceed to read its contents
  189.     if (is_dir($dir))
  190.     {
  191.         if ($dh = opendir($dir))
  192.         {
  193.             $tempArray = array();
  194.             while (($file = readdir($dh)) !== false)
  195.             {
  196.                 $tempArray[] = $file;
  197.             }
  198.             closedir($dh);
  199.             sort($tempArray);
  200.             foreach ($tempArray as $file)
  201.             {
  202.                 if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
  203.                 {/*ignore folders and links*/}
  204.                 else if (in_array(fileExtension($file), $typesArray))
  205.                 {
  206.                     $fileNameList[] = $file;
  207.                     $filePathList[] = urlencode($address . stripPrefix(($dir .'/'. $file), dirname(__FILE__)));
  208.                     $fileTypeList[] = fileExtension($file);
  209.                 }
  210.             }
  211.             $fileList = array($fileNameList, $filePathList, $fileTypeList);
  212.             return($fileList);
  213.         }
  214.     }
  215.     return(0);
  216. }
  217.  
  218. // return: array => array => string, string, string | when given a directory path, returns an array of file information
  219. // return[0] - array of file names
  220. // return[1] - array of file paths
  221. // return[2] - array of file extensions
  222. function recursiveListFiles($dir, $types, $address)
  223. {
  224.     $typesArray = explode('|', $types);
  225.    
  226.     $fileNameList = array();
  227.     $filePathList = array();
  228.     $fileTypeList = array();
  229.     // Open a known directory, and proceed to read its contents
  230.     if (is_dir($dir))
  231.     {
  232.         if ($dh = opendir($dir))
  233.         {
  234.             $tempArray = array();
  235.             while (($file = readdir($dh)) !== false)
  236.             {
  237.                 $tempArray[] = $file;
  238.             }
  239.             closedir($dh);
  240.             sort($tempArray);
  241.             foreach ($tempArray as $file)
  242.             {
  243.                 if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
  244.                 {
  245.                     $temp = recursiveListFiles(($dir .'/'. $file), $types, $address);
  246.                     if($temp !== 0)
  247.                     {
  248.                         $fileNameList = array_merge($fileNameList, $temp[0]);
  249.                         $filePathList = array_merge($filePathList, $temp[1]);
  250.                         $fileTypeList = array_merge($fileTypeList, $temp[2]);
  251.                        
  252.                     }
  253.                 }
  254.                 else if (in_array(fileExtension($file), $typesArray))
  255.                 {
  256.                     $fileNameList[] = $file;
  257.                     $filePathList[] = urlencode($address . stripPrefix(($dir .'/'. $file), dirname(__FILE__)));
  258.                     $fileTypeList[] = fileExtension($file);
  259.                 }
  260.             }
  261.             $fileList = array($fileNameList, $filePathList, $fileTypeList);
  262.             return($fileList);
  263.         }
  264.     }
  265.     return(0);
  266. }
  267.  
  268. ?>