PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Trunk/www/include/file.class.php

https://github.com/mathcoll/Public-Storm
PHP | 232 lines | 205 code | 1 blank | 26 comment | 1 complexity | 6a6ae95b8ed825cc8abe76452b961caa MD5 | raw file
Possible License(s): GPL-2.0, MIT, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Public-Storm
  4. Copyright (C) 2008-2012 Mathieu Lory <mathieu@internetcollaboratif.info>
  5. This file is part of Public-Storm.
  6. Public-Storm is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Public-Storm is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Public-Storm. If not, see <http://www.gnu.org/licenses/>.
  16. Project started on 2008-11-22 with help from Serg Podtynnyi
  17. <shtirlic@users.sourceforge.net>
  18. */
  19. /**
  20. * @package Public-Storm
  21. * @subpackage file
  22. * @author Mathieu Lory <mathieu@internetcollaboratif.info>
  23. */
  24. if (basename($_SERVER["SCRIPT_NAME"])==basename(__FILE__))die(gettext("You musn't call this page directly ! please, go away !"));
  25. class file {
  26. /**
  27. * File to be processed
  28. *
  29. * @access private
  30. * @var string
  31. */
  32. private $sFile;
  33. /**
  34. * Class constructor
  35. *
  36. * @access public
  37. * @param string $sFile Source file
  38. */
  39. public function __construct($sFile='') {
  40. if ($sFile != '') {
  41. $this->sFile = $sFile;
  42. }
  43. }
  44. /**
  45. * Read all content from a file and return as a string
  46. *
  47. * @access public
  48. * @return string
  49. */
  50. public function Read() {
  51. if ($this->sFile!='') {
  52. try {
  53. if ($this->Exists()) {
  54. if ($this->IsReadable())
  55. return file_get_contents($this->sFile);
  56. else
  57. throw new FileException('can\'t read file "'.$this->sFile.'"');
  58. } else {
  59. throw new FileException('file "'.$this->sFile.'" does not exist');
  60. }
  61. } catch (Exception $e) {
  62. Debug::Log($e,ERROR, __LINE__, __FILE__);
  63. return false;
  64. }
  65. }
  66. return '';
  67. }
  68. /**
  69. * Write some content to a file
  70. *
  71. * @access public
  72. * @param string $sContent
  73. * @return boolean
  74. */
  75. public function Write($sContent) {
  76. try {
  77. return file_put_contents($this->sFile,$sContent);
  78. } catch (Exception $e) {
  79. Debug::Log($e,ERROR, __LINE__, __FILE__);
  80. return false;
  81. }
  82. }
  83. /**
  84. * Append some content to file
  85. *
  86. * @access public
  87. * @param string $sContent
  88. * @return boolean
  89. */
  90. public function Append($sContent) {
  91. try {
  92. return file_put_contents($this->sFile,$sContent,FILE_APPEND);
  93. } catch (Exception $e) {
  94. Debug::Log($e,ERROR, __LINE__, __FILE__);
  95. return false;
  96. }
  97. }
  98. /**
  99. * Check if the selected file is readable
  100. *
  101. * @access public
  102. * @return boolean
  103. */
  104. public function IsReadable() {
  105. return is_readable($this->sFile);
  106. }
  107. /**
  108. * Check if the selected file has the permissions set to a value
  109. *
  110. * @access public
  111. * @return boolean
  112. */
  113. public function HasPermissions($wantedPerms) {
  114. $actualPerms = substr(decoct(fileperms($this->sFile)), 1);
  115. if($actualPerms == $wantedPerms) {
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. /**
  122. * Check if the selected file is writable
  123. *
  124. * @access public
  125. * @return boolean
  126. */
  127. public function IsWritable() {
  128. return is_writable($this->sFile);
  129. }
  130. /**
  131. * Check if the selected file is exists
  132. *
  133. * @access public
  134. * @return boolean
  135. */
  136. public function Exists() {
  137. return file_exists($this->sFile);
  138. }
  139. /**
  140. * Return the file name
  141. *
  142. * @access public
  143. * @return string
  144. */
  145. public function Name() {
  146. $sExt = strrchr($this->sFile,'.');
  147. if ($sExt != false) {
  148. return substr($this->sFile,0,-strlen($sExt));
  149. }
  150. return '';
  151. }
  152. public function filemtime() {
  153. return filemtime($this->sFile);
  154. }
  155. /**
  156. * Return the file ext
  157. *
  158. * @access public
  159. * @return string
  160. */
  161. public function FileExt() {
  162. return strtolower(end(explode('.',$this->sFile)));
  163. }
  164. /**
  165. * Delete a file from server
  166. *
  167. * @access public
  168. * @return bool
  169. */
  170. public function Delete() {
  171. if ($this->Exists()) {
  172. return unlink($this->sFile);
  173. }
  174. return true;
  175. }
  176. /**
  177. * List files from a directory
  178. *
  179. * @access public
  180. * @return array
  181. */
  182. public function GetFiles($dir, $regexp="/*/") {
  183. $liste = array();
  184. $dh = opendir($dir);
  185. //print $dir;
  186. while (($file = readdir($dh)) !== false) {
  187. if (preg_match($regexp, $file, $match)) {
  188. $id = $match[1];
  189. array_push($liste, $id);
  190. }
  191. }
  192. return $liste;
  193. }
  194. /**
  195. * List directories from a directory
  196. *
  197. * @access public
  198. * @return array
  199. */
  200. public function GetDirs($dir, $regexp="/*/") {
  201. $liste = array();
  202. foreach( scandir($dir) as $node ) {
  203. if( !is_dir($node) ) /* TODO ! pourquoi c'est inversé ?????????? */ {
  204. array_push($liste, $node);
  205. }
  206. }
  207. return $liste;
  208. }
  209. }
  210. ?>