/application/modules/Storage/Service/Abstract.php

https://github.com/shopaholiccompany/shopaholic · PHP · 225 lines · 147 code · 42 blank · 36 comment · 27 complexity · 3e93b6a2d6dbf3a19ebd30cb489de070 MD5 · raw file

  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Application_Core
  6. * @package Storage
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Abstract.php 7244 2010-09-01 01:49:53Z john $
  10. * @author John
  11. */
  12. /**
  13. * @category Application_Core
  14. * @package Storage
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. abstract class Storage_Service_Abstract implements Storage_Service_Interface
  19. {
  20. protected $_scheme;
  21. protected $_schemeClass = 'Storage_Service_Scheme_Extended';
  22. //protected $_schemeClass = 'Storage_Service_Scheme_Standard';
  23. // Scheme
  24. public function getScheme()
  25. {
  26. if( null === $this->_scheme )
  27. {
  28. $class = $this->_schemeClass;
  29. $this->_scheme = new $class();
  30. }
  31. return $this->_scheme;
  32. }
  33. public function setScheme(Storage_Service_Scheme_Interface $scheme)
  34. {
  35. $this->_scheme = $scheme;
  36. return $this;
  37. }
  38. // Transaction
  39. public function inTransaction()
  40. {
  41. return Engine_Api::_()->storage()->inTransaction();
  42. }
  43. public function rollBack()
  44. {
  45. // @todo
  46. }
  47. public function commit()
  48. {
  49. // @todo
  50. }
  51. /* Utility */
  52. public function getBaseUrl()
  53. {
  54. return $this->_removeScriptName(Zend_Controller_Front::getInstance()->getBaseUrl());
  55. }
  56. public function fileInfo($file)
  57. {
  58. // $file is an instance of Zend_Form_Element_File
  59. if( $file instanceof Zend_Form_Element_File )
  60. {
  61. $info = $file->getFileInfo();
  62. $info = current($info);
  63. }
  64. // $file is a key of $_FILES
  65. else if( is_array($file) )
  66. {
  67. $info = $file;
  68. }
  69. // $file is a string
  70. else if( is_string($file) )
  71. {
  72. $info = array(
  73. 'tmp_name' => $file,
  74. 'name' => basename($file),
  75. 'type' => 'unknown/unknown', // @todo
  76. 'size' => filesize($file)
  77. );
  78. // Try to get image info
  79. if( function_exists('getimagesize') && ($imageinfo = getimagesize($file)) )
  80. {
  81. $info['type'] = $imageinfo['mime'];
  82. }
  83. }
  84. // $file is an unknown type
  85. else
  86. {
  87. throw new Storage_Service_Exception('Unknown file type specified');
  88. }
  89. // Check to make sure file exists and not security problem
  90. self::_checkFile($info['tmp_name']);
  91. // Do some other stuff
  92. $mime_parts = explode('/', $info['type'], 2);
  93. $info['mime_major'] = $mime_parts[0];
  94. $info['mime_minor'] = $mime_parts[1];
  95. $info['hash'] = md5_file($info['tmp_name']);
  96. $info['extension'] = ltrim(strrchr($info['name'], '.'), '.');
  97. unset($info['type']);
  98. return $info;
  99. }
  100. protected function _removeScriptName($url)
  101. {
  102. if (!isset($_SERVER['SCRIPT_NAME'])) {
  103. // We can't do much now can we? (Well, we could parse out by ".")
  104. return $url;
  105. }
  106. if (($pos = strripos($url, basename($_SERVER['SCRIPT_NAME']))) !== false) {
  107. $url = substr($url, 0, $pos);
  108. }
  109. return $url;
  110. }
  111. protected function _checkFile($file, $mode = 06)
  112. {
  113. // @todo This is fubared, fix up later
  114. //if( preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $file) )
  115. //if( preg_match('/[^a-z0-9 \\/\\\\_.:-]/i', $file) )
  116. //{
  117. //throw new Storage_Service_Exception(sprintf('Security check: Illegal character in filename: %s', $file));
  118. //}
  119. if( $mode && !file_exists($file) )
  120. {
  121. throw new Storage_Service_Exception('File does not exist: '.$file);
  122. }
  123. if( ($mode & 04) && (!is_readable($file)) )
  124. {
  125. throw new Storage_Service_Exception('File not readable: '.$file);
  126. }
  127. if( ($mode & 02) && (!is_writable($file)) )
  128. {
  129. throw new Storage_Service_Exception('File not writeable: '.$file);
  130. }
  131. if( ($mode & 01) && (!is_executable($file)) )
  132. {
  133. throw new Storage_Service_Exception('File not executable: '.$file);
  134. }
  135. }
  136. protected function _mkdir($path, $mode = 0777)
  137. {
  138. if( is_dir($path) )
  139. {
  140. @chmod($path, $mode);
  141. return;
  142. }
  143. if( !@mkdir($path, $mode, true) )
  144. {
  145. throw new Storage_Service_Exception("Could not create folder: ".$path);
  146. }
  147. }
  148. protected function _move($from, $to)
  149. {
  150. if( !@chmod($from, 0777) || !@rename($from, $to) )
  151. {
  152. throw new Storage_Service_Exception('Unable to move file ('.$from.') -> ('.$to.')');
  153. }
  154. }
  155. protected function _delete($file)
  156. {
  157. if( !@chmod($file, 0777) || !@unlink($file) )
  158. {
  159. throw new Storage_Service_Exception('Unable to delete file: '.$file);
  160. }
  161. }
  162. protected function _copy($from, $to)
  163. {
  164. if( !@copy($from, $to) )
  165. {
  166. throw new Storage_Service_Exception('Unable to copy file ('.$from.') -> ('.$to.')');
  167. }
  168. }
  169. protected function _write($file, $data)
  170. {
  171. if( !@file_put_contents($file, $data) )
  172. {
  173. throw new Storage_Service_Exception('Unable to write to file: '.$file);
  174. }
  175. }
  176. protected function _read($file)
  177. {
  178. if( !@file_get_contents($file) )
  179. {
  180. throw new Storage_Service_Exception('Unable to read file: '.$file);
  181. }
  182. }
  183. }