PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/template_cms/helpers/dir.php

https://bitbucket.org/Awilum/template-cms/
PHP | 168 lines | 37 code | 37 blank | 94 comment | 7 complexity | 4a37bd9dabb48c9ccde8654ea6423eea MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php if (!defined('TEMPLATE_CMS_ACCESS')) exit('No direct script access allowed');
  2. /**
  3. * Directory Helper
  4. *
  5. * @package TemplateCMS
  6. * @subpackage Helpers
  7. * @author Romanenko Sergey / Awilum
  8. * @copyright 2011 - 2012 Romanenko Sergey / Awilum
  9. * @version $Id$
  10. * @since 3.0.0
  11. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  12. * TemplateCMS is free software. This version may have been modified pursuant
  13. * to the GNU General Public License, and as distributed it includes or
  14. * is derivative of works licensed under the GNU General Public License or
  15. * other free or open source software licenses.
  16. * See COPYING.txt for copyright notices and details.
  17. * @filesource
  18. */
  19. /**
  20. * Dir class
  21. */
  22. class Dir {
  23. /**
  24. * Protected constructor since this is a static class.
  25. *
  26. * @access protected
  27. */
  28. protected function __construct() {
  29. // Nothing here
  30. }
  31. /**
  32. * Creates a directory
  33. *
  34. * <code>
  35. * Dir::create('folder1');
  36. * </code>
  37. *
  38. * @param string $dir Name of directory to create
  39. * @return boolean
  40. */
  41. public static function create($dir, $chmod = 0775) {
  42. // Redefine vars
  43. $dir = (string) $dir;
  44. // Create new dir if $dir !exists
  45. return ( ! Dir::exists($dir)) ? @mkdir($dir, $chmod, true) : true;
  46. }
  47. /**
  48. * Checks if this directory exists.
  49. *
  50. * @param string $directory Full path of the directory to check.
  51. * @return boolean
  52. */
  53. public static function exists($dir) {
  54. // Redefine vars
  55. $dir = (string) $dir;
  56. // Directory exists
  57. if (file_exists($dir) && is_dir($dir)) return true;
  58. // Doesn't exist
  59. return false;
  60. }
  61. /**
  62. * Check dir permission
  63. *
  64. * <code>
  65. * echo Dir::checkPerm('folder1');
  66. * </code>
  67. *
  68. * @param string $dir Directory to check
  69. * @return string
  70. */
  71. public static function checkPerm($dir) {
  72. // Redefine vars
  73. $dir = (string) $dir;
  74. // Clear stat cache
  75. clearstatcache();
  76. // Return perm
  77. return substr(sprintf('%o', fileperms($dir)), -4);
  78. }
  79. /**
  80. * Delete directory
  81. *
  82. * <code>
  83. * Dir::delete('folder1');
  84. * </code>
  85. *
  86. * @param string $dir Name of directory to delete
  87. */
  88. public static function delete($dir) {
  89. // Redefine vars
  90. $dir = (string) $dir;
  91. // Delete dir
  92. if (is_dir($dir)){$ob=scandir($dir);foreach ($ob as $o){if($o!='.'&&$o!='..'){if(filetype($dir.'/'.$o)=='dir')Dir::delete($dir.'/'.$o); else unlink($dir.'/'.$o);}}}
  93. reset($ob); rmdir($dir);
  94. }
  95. /**
  96. * Get list of directories
  97. *
  98. * <code>
  99. * $dirs = Dir::scan('folders');
  100. * </code>
  101. *
  102. * @param string $dir Directory
  103. */
  104. public static function scan($dir){
  105. // Redefine vars
  106. $dir = (string) $dir;
  107. // Scan dir
  108. if (is_dir($dir)&&$dh=opendir($dir)){$f=array();while ($fn=readdir($dh)){if($fn!='.'&&$fn!='..'&&is_dir($dir.DS.$fn))$f[]=$fn;}return$f;}
  109. }
  110. /**
  111. * Check if a directory is writable.
  112. *
  113. * @param string $path The path to check.
  114. * @return booleans
  115. */
  116. public static function writable($path) {
  117. // Redefine vars
  118. $path = (string) $path;
  119. // Create temporary file
  120. $file = tempnam($path, 'writable');
  121. // File has been created
  122. if($file !== false) {
  123. // Remove temporary file
  124. File::delete($file);
  125. // Writable
  126. return true;
  127. }
  128. // Else not writable
  129. return false;
  130. }
  131. }