PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/api/core/flib.php

https://bitbucket.org/rakmana/forkan
PHP | 273 lines | 211 code | 8 blank | 54 comment | 40 complexity | f2da703b957015deb0a24e53826fdd26 MD5 | raw file
  1. <?php
  2. /**
  3. * WAPL File Library
  4. * @package WAPL
  5. * @author Khedrane Jnom <Jnom23@gmail.com>
  6. * @copyright (C) 2003 - 2011 . n-Teek
  7. * @license http://gnu.org/copyleft/gpl.html GNU GPL
  8. * @version $Id: flib.php,v2.0 06:53 09/04/2011 NP++ Exp $
  9. */
  10. /**
  11. * FLIB
  12. * WAPL File & Folder Managment class
  13. * Require ...
  14. *
  15. * @package WAPL
  16. */
  17. class flib{
  18. var $description = 'System Files & Folders Functions Class';
  19. /**
  20. * Copy File with overwrite option.
  21. *
  22. * @access public
  23. * @param string $source Source File Path
  24. * @param string $destination Target File Path
  25. * @param bool $overwrite If target exist decide action
  26. */
  27. public static function copy($source,$destination,$overwrite=false){
  28. if( $overwrite && flib::exists($destination) )
  29. return false;
  30. return copy($source,$destination);
  31. }
  32. /**
  33. * Move File with overwrite option.
  34. *
  35. * @access public
  36. * @param string $source Source File Path
  37. * @param string $destination Target File Path
  38. * @param bool $overwrite If target exist decide action
  39. */
  40. public static function move($source,$destination,$overwrite=false){
  41. //Possible to use rename()
  42. if( flib::copy($source,$destination,$overwrite) && flib::exists($destination) ){
  43. flib::delete($source);
  44. return true;
  45. } else {
  46. return false;
  47. }
  48. }
  49. public static function delete($file,$recursive=false){
  50. $file = str_replace('\\','/',$file); //for win32, occasional problems deleteing files otherwise
  51. if( flib::is_file($file) )
  52. return @unlink($file);
  53. if( !$recursive && flib::is_dir($file) )
  54. return @rmdir($file);
  55. $filelist = flib::dirlist($file);
  56. if( ! $filelist )
  57. return true; //No files exist, Say we've deleted them
  58. $retval = true;
  59. foreach($filelist as $filename=>$fileinfo){
  60. if( ! flib::delete($file.'/'.$filename,$recursive) )
  61. $retval = false;
  62. }
  63. if( ! @rmdir($file) )
  64. return false;
  65. return $retval;
  66. }
  67. public static function exists($file){
  68. return @file_exists($file);
  69. }
  70. public static function is_file($file){
  71. return @is_file($file);
  72. }
  73. public static function is_dir($path){
  74. return @is_dir($path);
  75. }
  76. public static function is_readable($file){
  77. return @is_readable($file);
  78. }
  79. public static function is_writable($file){
  80. return @is_writable($file);
  81. }
  82. public static function infomp3($file){
  83. /*Array
  84. (
  85. [copyright] => Dirty Mac
  86. [originalArtist] => Dirty Mac
  87. [composer] => Marcus G?tze
  88. [artist] => Dirty Mac
  89. [title] => Little Big Man
  90. [album] => Demo-Tape
  91. [track] => 5/12
  92. [genre] => (17)Rock
  93. [year] => 2001
  94. )*/
  95. $i = function_exists(id3_get_tag)? @id3_get_tag($file, ID3_V2_3 ) : "ID3 Not Enabled";
  96. $i ='<b>{_title}: </b>'.$i[title].'<br />
  97. <b>{_artist}: </b>'.$i[artist].'<br />
  98. <b>{_album}: </b>'.$i[album].'<br />
  99. <b>{_track}: </b>'.$i[track].'<br />
  100. <b>{_genre}: </b>'.$i[genre].'<br />';
  101. $i = KTemplate::replace($i);
  102. return $i;
  103. }
  104. public static function infoimg($file){
  105. /*($width, $height, $type, $attr)*/
  106. $file = flib::tohtml($file);
  107. list($w, $h, $t, $p) = @getimagesize($file) or die('Cannot Find Informations');
  108. //write infos
  109. $ret['width'] = $w;
  110. $ret['height'] = $h;
  111. $ret['type'] =image_type_to_mime_type(exif_imagetype($file));
  112. return $ret;
  113. }
  114. public static function size($size){
  115. if ($size >= 1073741824){ $size = round($size / 1073741824 * 100) / 100 .' '. txt('gb'); }
  116. elseif ($size >= 1048576){ $size = round($size / 1048576 * 100) / 100 .' '. txt('mb'); }
  117. elseif ($size >= 1024){ $size = round($size / 1024 * 100) / 100 .' '. txt('kb'); }
  118. elseif ($size > 0){ $size = $size .' '. txt('byte'); }
  119. elseif ($size <= 0){ $size = txt('empty'); }
  120. else {$size = txt('unknown') ; }
  121. return $size;
  122. }
  123. public static function dsize($idir){
  124. if (strrchr($idir, "/")!='/') $idir .= '/';
  125. $size = 0;
  126. $handle=opendir($idir);
  127. while ($rep = readdir($handle)) {
  128. if ($rep != "." && $rep != "..") {
  129. if (flib::is_dir($idir.$rep)){
  130. $folder[]=$rep;//we dont need it
  131. $size += flib::dsize($idir.$rep);}
  132. else{
  133. $size += filesize($idir.$rep);
  134. }
  135. }
  136. }
  137. closedir($handle);
  138. return $size;
  139. }
  140. public static function fsize($fname){//return file size in B, KB , MB or GB
  141. $size = filesize($fname);
  142. return flib::size($size);
  143. }
  144. public static function fileslist ($currentdir, $startdir=NULL,$ext=NULL) {
  145. global $pathchar;
  146. //chdir ($currentdir);
  147. // remember where we started from
  148. if (!$startdir)
  149. {
  150. $startdir = $currentdir;
  151. }
  152. $d = @opendir($currentdir);
  153. $files = array();
  154. if(!$d)
  155. return $files;
  156. //list the files in the dir
  157. while (false !== ($file = readdir($d)))
  158. {
  159. if ($file != ".." && $file != ".")
  160. {
  161. if (flib::is_dir($currentdir.DS.$file))
  162. {
  163. // If $file is a directory take a look inside
  164. $a = flib::fileslist ($currentdir.DS.$file, $startdir,$ext);
  165. if(is_array($a))
  166. $files = array_merge($files,$a);
  167. }
  168. else
  169. {
  170. if($ext!=NULL)
  171. {
  172. if(is_array($ext)){
  173. $fext = flib::extension($file);
  174. if (in_array($fext ,$ext))
  175. $files[] = $currentdir.DS.$file;
  176. }
  177. else{
  178. $extstr = stristr($file,".".$ext);
  179. if(strlen($extstr))
  180. $files[] = $currentdir.DS.$file;
  181. };
  182. }
  183. else
  184. $files[] = $currentdir.DS.$file;
  185. }
  186. }
  187. }
  188. closedir ($d);
  189. return $files;
  190. }
  191. public static function dirslist ($currentdir, $startdir=NULL) {
  192. global $pathchar;
  193. //chdir ($currentdir);
  194. // remember where we started from
  195. if (!$startdir)
  196. {
  197. $startdir = $currentdir;
  198. }
  199. $d = @opendir($currentdir);
  200. $dirs = array();
  201. if(!$d)
  202. return $dirs;
  203. //list the dirs in the dir
  204. while (false !== ($dir = readdir($d)))
  205. {
  206. if ($dir != ".." && $dir != ".")
  207. {
  208. if (flib::is_dir($currentdir.DS.$dir))
  209. {
  210. $dirs[] = $dir;
  211. }
  212. else
  213. {
  214. }
  215. }
  216. }
  217. closedir ($d);
  218. return $dirs;
  219. }
  220. public static function extension($file) {
  221. $file_type = explode(".", $file);
  222. $ext = mb_strtolower($file_type[(count($file_type)-1)]);
  223. return $ext;
  224. }
  225. public static function get_contents($file){
  226. //return @file_get_contents($file);
  227. $fp = fopen($file, 'r');
  228. $ret = fread($fp, filesize($file));
  229. fclose($fp);
  230. return $ret;
  231. }
  232. public static function get_contents_array($file){
  233. return @file($file);
  234. }
  235. public static function put_contents($file,$contents,$type=''){
  236. $fp=@fopen($file,'w'.$type);
  237. if (!$fp)
  238. return false;
  239. @fwrite($fp,$contents);
  240. @fclose($fp);
  241. return true;
  242. }
  243. public static function getchmod($file){
  244. return @fileperms($file);
  245. }
  246. public static function getnumchmodfromh($mode) {
  247. $realmode = "";
  248. $legal = array("","w","r","x","-");
  249. $attarray = preg_split("//",$mode);
  250. for($i=0;$i<count($attarray);$i++){
  251. if($key = array_search($attarray[$i],$legal)){
  252. $realmode .= $legal[$key];
  253. }
  254. }
  255. $mode = str_pad($realmode,9,'-');
  256. $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
  257. $mode = strtr($mode,$trans);
  258. $newmode = '';
  259. $newmode .= $mode[0]+$mode[1]+$mode[2];
  260. $newmode .= $mode[3]+$mode[4]+$mode[5];
  261. $newmode .= $mode[6]+$mode[7]+$mode[8];
  262. return $newmode;
  263. }
  264. }
  265. ?>