PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/frog/helpers/Dir.php

http://frogcms-ru.googlecode.com/
PHP | 627 lines | 466 code | 76 blank | 85 comment | 25 complexity | 56542b9b191989aa78044f09084fcf44 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php if(!defined('DEBUG')) die;
  2. /**
  3. * Frog CMS - Content Management Simplified. <http://www.madebyfrog.com>
  4. * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
  5. * Copyright (C) 2008 Martijn van der Kleijn <martijn.niji@gmail.com>
  6. * Copyright (C) 2008 Maslakov Alexander <jmas.ukraine@gmail.com>
  7. *
  8. * This file is part of Frog CMS.
  9. *
  10. * Frog CMS is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Frog CMS is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Frog CMS. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * Frog CMS has made an exception to the GNU General Public License for plugins.
  24. * See exception.txt for details and the full text.
  25. */
  26. /**
  27. * @package frog
  28. * @subpackage helpers
  29. *
  30. * @author Maslakov Alexandr <jmas.ukraine@gmail.com>
  31. * @version 0.1
  32. * @license http://www.gnu.org/licenses/gpl.html GPL License
  33. * @copyright Maslakov Alexander, 2010
  34. */
  35. /*
  36. Class Dir
  37. Working with directories
  38. */
  39. class Dir
  40. {
  41. private $_path = null;
  42. private $_files = array();
  43. private $_dirs = array();
  44. // Directory path as constructor param
  45. public function __construct( $path, $sort_callback = 'natsort' )
  46. {
  47. if( is_dir($path) )
  48. {
  49. // Set directory path
  50. $this->_path = str_replace('\\', '/', realpath($path));
  51. // Read directory items
  52. $dir = opendir( $this->_path );
  53. while( $item = readdir( $dir ) )
  54. {
  55. if( strpos($item, '.') !== 0 )
  56. {
  57. if( is_dir( $this->_path .'/'. $item ) )
  58. {
  59. $this->_dirs[] = $item;
  60. }
  61. else
  62. {
  63. $this->_files[] = $item;
  64. }
  65. }
  66. }
  67. // Sorting
  68. call_user_func_array( $sort_callback, array($this->_dirs) );
  69. call_user_func_array( $sort_callback, array($this->_files) );
  70. }
  71. else
  72. {
  73. throw new Exception('This is not valid directory path!');
  74. }
  75. }
  76. // Return all subdirectories of this directory
  77. public function getDirs()
  78. {
  79. $dirs = array();
  80. if( empty($dirs) )
  81. {
  82. foreach( $this->_dirs as $dir_name )
  83. {
  84. $dirs[] = new Dir( $this->_path .'/'. $dir_name );
  85. }
  86. }
  87. return $dirs;
  88. }
  89. // Return all files of this directory
  90. public function getFiles()
  91. {
  92. $files = array();
  93. if( empty($files) )
  94. {
  95. foreach( $this->_files as $file_name )
  96. {
  97. $ext = preg_replace( '/^.+\./', '', $file_name );
  98. $class_name = 'DirFile';
  99. switch( $ext )
  100. {
  101. case 'jpg':
  102. case 'jpeg':
  103. case 'gif':
  104. case 'png':
  105. $class_name = 'DirFileImage';
  106. break;
  107. }
  108. $files[] = new $class_name( $this->_path .'/'. $file_name );
  109. }
  110. }
  111. return $files;
  112. }
  113. // Remove dir and subdirs
  114. public function remove()
  115. {
  116. // Remove all directory files
  117. if( !empty( $this->_files ) )
  118. {
  119. foreach( $this->getFiles() as $file )
  120. {
  121. if( !$file->remove() )
  122. return false;
  123. }
  124. }
  125. // Remove all sub directories
  126. if( !empty( $this->_dirs ) )
  127. {
  128. foreach( $this->getDirs() as $dir )
  129. {
  130. if( !$dir->remove() )
  131. return false;
  132. }
  133. }
  134. return @rmdir( $this->_path );
  135. }
  136. // Short permistions
  137. public function getPermsShort()
  138. {
  139. $perms = $this->getPerms();
  140. //$p = decoct($perms);
  141. return substr(sprintf('%o', $perms), -4, 4);
  142. //return (strlen($p) > 4 ? substr($p, -4) : $p);
  143. }
  144. // Return file permissions
  145. public function getPerms()
  146. {
  147. return fileperms( $this->_path );
  148. }
  149. // Return file permissions as humannized string
  150. public function getPermsString()
  151. {
  152. $perms = $this->getPerms( $this->_path );
  153. if (($perms & 0xC000) == 0xC000) {
  154. // Socket
  155. $info = 's';
  156. } elseif (($perms & 0xA000) == 0xA000) {
  157. // Symbolic Link
  158. $info = 'l';
  159. } elseif (($perms & 0x8000) == 0x8000) {
  160. // Regular
  161. $info = '-';
  162. } elseif (($perms & 0x6000) == 0x6000) {
  163. // Block special
  164. $info = 'b';
  165. } elseif (($perms & 0x4000) == 0x4000) {
  166. // Directory
  167. $info = 'd';
  168. } elseif (($perms & 0x2000) == 0x2000) {
  169. // Character special
  170. $info = 'c';
  171. } elseif (($perms & 0x1000) == 0x1000) {
  172. // FIFO pipe
  173. $info = 'p';
  174. } else {
  175. // Unknown
  176. $info = 'u';
  177. }
  178. // Owner
  179. $info .= (($perms & 0x0100) ? 'r' : '-');
  180. $info .= (($perms & 0x0080) ? 'w' : '-');
  181. $info .= (($perms & 0x0040) ?
  182. (($perms & 0x0800) ? 's' : 'x' ) :
  183. (($perms & 0x0800) ? 'S' : '-'));
  184. // Group
  185. $info .= (($perms & 0x0020) ? 'r' : '-');
  186. $info .= (($perms & 0x0010) ? 'w' : '-');
  187. $info .= (($perms & 0x0008) ?
  188. (($perms & 0x0400) ? 's' : 'x' ) :
  189. (($perms & 0x0400) ? 'S' : '-'));
  190. // World
  191. $info .= (($perms & 0x0004) ? 'r' : '-');
  192. $info .= (($perms & 0x0002) ? 'w' : '-');
  193. $info .= (($perms & 0x0001) ?
  194. (($perms & 0x0200) ? 't' : 'x' ) :
  195. (($perms & 0x0200) ? 'T' : '-'));
  196. return $info;
  197. }
  198. // Return directory path
  199. public function getPath()
  200. {
  201. return $this->_path;
  202. }
  203. // FrogCMS path shorted
  204. public function getPathShort( $dir_name = PUBLIC_FILES )
  205. {
  206. return substr($this->_path, strpos($this->_path, $dir_name), strlen($this->_path));
  207. }
  208. // Return directory path
  209. public function __toString()
  210. {
  211. return $this->_path;
  212. }
  213. // ID hash
  214. public function getId()
  215. {
  216. return md5($this->_path);
  217. }
  218. // Check for childrens (directories or files)
  219. public function hasChildrens( $only_dirs = false )
  220. {
  221. if( $only_dirs )
  222. return !(empty($this->_dirs));
  223. else
  224. return !(empty($this->_dirs) && empty($this->_files));
  225. }
  226. // Check for files
  227. public function hasFiles()
  228. {
  229. return !empty($this->_files);
  230. }
  231. // Get directory name
  232. public function getName()
  233. {
  234. return substr($this->_path, strrpos($this->_path, '/')+1, strlen($this->_path));
  235. }
  236. } // end
  237. /*
  238. Class DirFile
  239. Workin with files
  240. */
  241. class DirFile
  242. {
  243. private $_path = null;
  244. // File path as constructor param
  245. public function __construct( $path )
  246. {
  247. if( is_file( $path ) )
  248. {
  249. $this->_path = str_replace('\\', '/', $path);
  250. }
  251. else
  252. {
  253. throw new Exception('This is not valid file path!');
  254. }
  255. }
  256. // Get file size
  257. public function getSize()
  258. {
  259. return filesize( $this->_path );
  260. }
  261. // Short permistions
  262. public function getPermsShort($octal = true)
  263. {
  264. $perms = $this->getPerms();
  265. $cut = $octal ? 2 : 3;
  266. return substr(decoct($perms), $cut);
  267. }
  268. // Return file permissions
  269. public function getPerms()
  270. {
  271. return fileperms( $this->_path );
  272. }
  273. // Return file permissions as humannized string
  274. public function getPermsString()
  275. {
  276. $perms = $this->getPerms( $this->_path );
  277. if (($perms & 0xC000) == 0xC000) {
  278. // Socket
  279. $info = 's';
  280. } elseif (($perms & 0xA000) == 0xA000) {
  281. // Symbolic Link
  282. $info = 'l';
  283. } elseif (($perms & 0x8000) == 0x8000) {
  284. // Regular
  285. $info = '-';
  286. } elseif (($perms & 0x6000) == 0x6000) {
  287. // Block special
  288. $info = 'b';
  289. } elseif (($perms & 0x4000) == 0x4000) {
  290. // Directory
  291. $info = 'd';
  292. } elseif (($perms & 0x2000) == 0x2000) {
  293. // Character special
  294. $info = 'c';
  295. } elseif (($perms & 0x1000) == 0x1000) {
  296. // FIFO pipe
  297. $info = 'p';
  298. } else {
  299. // Unknown
  300. $info = 'u';
  301. }
  302. // Owner
  303. $info .= (($perms & 0x0100) ? 'r' : '-');
  304. $info .= (($perms & 0x0080) ? 'w' : '-');
  305. $info .= (($perms & 0x0040) ?
  306. (($perms & 0x0800) ? 's' : 'x' ) :
  307. (($perms & 0x0800) ? 'S' : '-'));
  308. // Group
  309. $info .= (($perms & 0x0020) ? 'r' : '-');
  310. $info .= (($perms & 0x0010) ? 'w' : '-');
  311. $info .= (($perms & 0x0008) ?
  312. (($perms & 0x0400) ? 's' : 'x' ) :
  313. (($perms & 0x0400) ? 'S' : '-'));
  314. // World
  315. $info .= (($perms & 0x0004) ? 'r' : '-');
  316. $info .= (($perms & 0x0002) ? 'w' : '-');
  317. $info .= (($perms & 0x0001) ?
  318. (($perms & 0x0200) ? 't' : 'x' ) :
  319. (($perms & 0x0200) ? 'T' : '-'));
  320. return $info;
  321. }
  322. // Return file extension
  323. public function getExt()
  324. {
  325. return preg_replace( '/^.+\./', '', $this->_path );
  326. }
  327. // Remove file
  328. public function remove()
  329. {
  330. return @unlink( $this->_path );
  331. }
  332. // Return directory path
  333. public function getPath()
  334. {
  335. return $this->_path;
  336. }
  337. // FrogCMS path shorted
  338. public function getPathShort( $dir_name = PUBLIC_FILES )
  339. {
  340. return substr($this->_path, strpos($this->_path, $dir_name), strlen($this->_path));
  341. }
  342. // Return file path
  343. public function __toString()
  344. {
  345. return $this->_path;
  346. }
  347. // ID hash
  348. public function getId()
  349. {
  350. return md5($this->_path);
  351. }
  352. // Get file name
  353. public function getName()
  354. {
  355. return substr($this->_path, strrpos($this->_path, '/')+1, strlen($this->_path));
  356. }
  357. // Only dir path
  358. public function getDirPath()
  359. {
  360. return substr($this->getPathShort(), 0, strrpos($this->getPathShort(), '/')+1);
  361. }
  362. } // end
  363. /*
  364. DirFileImage
  365. */
  366. /*
  367. * File: SimpleImage.php
  368. * Author: Simon Jarvis
  369. * Copyright: 2006 Simon Jarvis
  370. * Date: 08/11/06
  371. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  372. *
  373. * This program is free software; you can redistribute it and/or
  374. * modify it under the terms of the GNU General Public License
  375. * as published by the Free Software Foundation; either version 2
  376. * of the License, or (at your option) any later version.
  377. *
  378. * This program is distributed in the hope that it will be useful,
  379. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  380. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  381. * GNU General Public License for more details:
  382. * http://www.gnu.org/licenses/gpl.html
  383. *
  384. */
  385. class DirFileImage extends DirFile
  386. {
  387. private $image;
  388. private $image_type;
  389. public function __construct( $file_path )
  390. {
  391. parent::__construct( $file_path );
  392. $this->load( $file_path );
  393. }
  394. public function __toString()
  395. {
  396. $this->output();
  397. }
  398. function load( $filename )
  399. {
  400. $image_info = getimagesize( $filename );
  401. $this->image_type = $image_info[2];
  402. if( $this->image_type == IMAGETYPE_JPEG )
  403. {
  404. $this->image = imagecreatefromjpeg( $filename );
  405. }
  406. elseif( $this->image_type == IMAGETYPE_GIF )
  407. {
  408. $this->image = imagecreatefromgif( $filename );
  409. }
  410. elseif( $this->image_type == IMAGETYPE_PNG )
  411. {
  412. $this->image = imagecreatefrompng( $filename );
  413. }
  414. }
  415. function save( $filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null )
  416. {
  417. if( $permissions != null)
  418. {
  419. chmod( $filename, $permissions );
  420. }
  421. if( $image_type == IMAGETYPE_JPEG )
  422. {
  423. return imagejpeg( $this->image, $filename, $compression );
  424. }
  425. elseif( $image_type == IMAGETYPE_GIF )
  426. {
  427. return imagegif( $this->image, $filename);
  428. }
  429. elseif( $image_type == IMAGETYPE_PNG )
  430. {
  431. return imagepng( $this->image, $filename );
  432. }
  433. }
  434. function output( $image_type = IMAGETYPE_JPEG )
  435. {
  436. switch( $image_type )
  437. {
  438. case IMAGETYPE_JPEG: header('Content-type: image/jpg'); break;
  439. case IMAGETYPE_GIF: header('Content-type: image/gif'); break;
  440. case IMAGETYPE_PNG: header('Content-type: image/png'); break;
  441. }
  442. if( $image_type == IMAGETYPE_JPEG )
  443. {
  444. imagejpeg($this->image);
  445. }
  446. elseif( $image_type == IMAGETYPE_GIF )
  447. {
  448. imagegif($this->image);
  449. }
  450. elseif( $image_type == IMAGETYPE_PNG )
  451. {
  452. imagepng($this->image);
  453. }
  454. }
  455. function getWidth()
  456. {
  457. return imagesx($this->image);
  458. }
  459. function getHeight()
  460. {
  461. return imagesy( $this->image );
  462. }
  463. function resizeToHeight( $height )
  464. {
  465. $ratio = $height / $this->getHeight();
  466. $width = $this->getWidth() * $ratio;
  467. $this->resize($width,$height);
  468. }
  469. function resizeToWidth( $width )
  470. {
  471. $ratio = $width / $this->getWidth();
  472. $height = $this->getheight() * $ratio;
  473. $this->resize( $width, $height );
  474. }
  475. function scale( $scale )
  476. {
  477. $width = $this->getWidth() * ($scale / 100);
  478. $height = $this->getheight() * ($scale / 100);
  479. $this->resize( $width, $height );
  480. }
  481. function resize( $width, $height )
  482. {
  483. $new_width = $this->getWidth();
  484. $new_height = $this->getHeight();
  485. $new_image = imagecreatetruecolor( $width, $height );
  486. imagecopyresampled( $new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight() );
  487. $this->image = $new_image;
  488. }
  489. function thumb( $width, $height, $x1, $y1, $x2, $y2 )
  490. {
  491. $new_width = $x2 - $x1;
  492. $new_height = $y2 - $y1;
  493. // Coefficient that tell how crop area higer or smaller than thumb
  494. $k = $new_width / $width;
  495. $new_image = imagecreatetruecolor( $width, $height );
  496. imagecopyresampled( $new_image, $this->image, 0, 0, $x1 * $k, $y1 * $k, $width, $height, $new_width * $k, $new_height * $k );
  497. $this->image = $new_image;
  498. }
  499. } // end
  500. ?>