WYSIJA_help_file

The MailPoet Newsletters WYSIJA help file class.

Defined (1)

The class is defined in the following location(s).

/helpers/file.php  
  1. class WYSIJA_help_file extends WYSIJA_object{ 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. /** 
  7. * Get the full path of a file 
  8. * @param type $csvfilename 
  9. * @param type $folder 
  10. * @return boolean 
  11. */ 
  12. function exists($fileFolder=false) { 
  13. $upload_base_dir = $this->getUploadBaseDir(); 
  14.  
  15. $filename=str_replace('/', DS, $upload_base_dir).DS.'wysija'.DS.$fileFolder; 
  16. if(!file_exists($filename)) { 
  17. return array('result'=>false, 'file'=>$filename); 
  18.  
  19. return array('result'=>true, 'file'=>$filename); 
  20.  
  21. /** 
  22. * Get the full path of a file 
  23. * @param type $csvfilename 
  24. * @param type $folder 
  25. * @return boolean 
  26. */ 
  27. function get($csvfilename, $folder='temp') { 
  28. $upload_base_dir = $this->getUploadBaseDir(); 
  29.  
  30. $filename=$upload_base_dir.DS.'wysija'.DS.$folder.DS.$csvfilename; 
  31. if(!file_exists($filename)) { 
  32. $filename=$upload_base_dir.DS.$csvfilename; 
  33. if(!file_exists($filename)) $filename=false; 
  34.  
  35. return $filename; 
  36.  
  37. // Description: create a directory recursively if possible 
  38. // Parameters: (Name of the directory, permissions) 
  39. // Returns: Directory path. False if impossible to create folder. 
  40. function makeDir($folder='temp', $mode=0755) { 
  41. $upload_base_dir = $this->getUploadBaseDir(); 
  42.  
  43. if(strpos(str_replace('/', DS, $folder), str_replace('/', DS, $upload_base_dir))!==false) { 
  44. $dirname=$folder; 
  45. }else{ 
  46. $dirname=$upload_base_dir.DS.'wysija'.DS.$folder.DS; 
  47. if(!file_exists($dirname)) { 
  48. if(!mkdir($dirname, $mode, true)) { 
  49. $this->error('Cannot create folder '.$dirname.' try to create the folder manually'); 
  50. return false; 
  51. chmod($dirname, $mode); 
  52. return $dirname; 
  53.  
  54.  
  55. function getUploadDir($folder=false) { 
  56. $upload_base_dir = $this->getUploadBaseDir(); 
  57.  
  58. $dirname=$upload_base_dir.DS.'wysija'.DS; 
  59. if($folder) $dirname.=$folder.DS; 
  60. if(file_exists($dirname)) return $dirname; 
  61. return false; 
  62.  
  63. function getUploadBaseDir() { 
  64. $upload_dir = wp_upload_dir(); 
  65.  
  66. if(!isset($upload_dir['basedir'])) { 
  67. if(isset($upload_dir['error'])) $this->wp_error('<b>WordPress error</b> : '.$upload_dir['error'], 1); 
  68. return false; 
  69.  
  70. //having .. in a path is not safe as it can lead to some parent path where we don't have control 
  71. if(strpos($upload_dir['basedir'], '..')!==false) { 
  72. $pathsections=$pathsectionsc=explode(DS, $upload_dir['basedir']); 
  73.  
  74. while($key = array_search('..', $pathsections)) { 
  75. unset($pathsections[$key]); 
  76. unset($pathsections[$key-1]); 
  77. $newpatharray=array(); 
  78. foreach($pathsections as $ky=>$vy) { 
  79. $newpatharray[]=$vy; 
  80. $pathsections=$newpatharray; 
  81. $cleanBaseDir=implode(DS, $pathsections); 
  82.  
  83. if(file_exists($cleanBaseDir)) { 
  84. $upload_dir['basedir']=$cleanBaseDir; 
  85.  
  86.  
  87. return $upload_dir['basedir']; 
  88.  
  89. /** 
  90. * make a temporary file 
  91. * @param type $content 
  92. * @param type $key 
  93. * @param type $format 
  94. * @return type 
  95. */ 
  96. function temp($content, $key='temp', $format='.tmp') { 
  97. $tempDir=$this->makeDir(); 
  98.  
  99. if(!$tempDir) return false; 
  100.  
  101. $time_created = substr( md5(rand()), 0, 20); 
  102. $file_name = $key.'-'.$time_created.$format; 
  103.  
  104. $handle=fopen($tempDir.$file_name, 'w'); 
  105. fwrite($handle, $content); 
  106. fclose($handle); 
  107.  
  108. return array('path'=>$tempDir.$file_name, 'name'=>$file_name, 'url'=>$this->url($file_name, 'temp')); 
  109.  
  110. /** 
  111. * Get the url of a wysija file based on the filename and the wysija folder 
  112. * @param type $filename 
  113. * @param type $folder 
  114. * @return string 
  115. */ 
  116. function url($filename, $folder='temp') { 
  117. $upload_dir = wp_upload_dir(); 
  118.  
  119. if(file_exists($upload_dir['basedir'].DS.'wysija')) { 
  120. $url=$upload_dir['baseurl'].'/wysija/'.$folder.'/'.$filename; 
  121. }else{ 
  122. $url=$upload_dir['baseurl'].'/'.$filename; 
  123.  
  124. return str_replace(DS, '/', $url); 
  125.  
  126. /** 
  127. */ 
  128. function clear() { 
  129. $folders_to_clear = array("import", "temp"); 
  130. $filename_removal = array("import-", "export-", 'export_userids-'); 
  131. $deleted=array(); 
  132. foreach($folders_to_clear as $folder) { 
  133. $path=$this->getUploadDir($folder); 
  134. /** get a list of files from this folder and clear them */ 
  135. if(!$path) continue; 
  136. $files = scandir($path); 
  137. foreach($files as $filename) { 
  138. if(!in_array($filename, array('.', '..', ".DS_Store", "Thumbs.db"))) { 
  139. if(preg_match('/('.implode($filename_removal, '|').')[a-f0-9]*\.(csv|txt)/', $filename, $match)) { 
  140. $deleted[]=$path.$filename; 
  141. foreach($deleted as $filename) { 
  142. if(file_exists($filename)) { 
  143. $filename=str_replace('/', DS, $filename); 
  144. unlink($filename); 
  145.  
  146.  
  147. function rrmdir($dir) { 
  148. if(strpos($dir, '..')!==false) { 
  149. $this->error('Path is not safe, cannot contain ..'); 
  150. return false; 
  151. if (is_dir($dir)) { 
  152. $files = scandir($dir); 
  153. foreach ($files as $file) { 
  154. if ($file != "." && $file != "..") $this->rrmdir("$dir".DS."$file"); 
  155.  
  156. if(!rmdir($dir)) { 
  157. chmod($dir, 0777); 
  158. rmdir($dir); 
  159.  
  160.  
  161. else if (file_exists($dir)) { 
  162. $dir=str_replace('/', DS, $dir); 
  163. unlink($dir); 
  164.  
  165. function rcopy($src, $dst) { 
  166. if(strpos($src, '..')!==false || strpos($dst, '..')!==false) { 
  167. $this->error('src : '.$src); 
  168. $this->error('dst : '.$dst); 
  169. $this->error('Path is not safe, cannot contain ..'); 
  170. return false; 
  171. }else{ 
  172. if (file_exists($dst)) $this->rrmdir($dst); 
  173.  
  174. if (is_dir($src)) { 
  175. mkdir($dst); 
  176. $files = scandir($src); 
  177. foreach ($files as $file) { 
  178. if ($file != "." && $file != "..") $this->rcopy("$src/$file", "$dst/$file"); 
  179.  
  180. else if (file_exists($src)) { 
  181. copy(str_replace('/', DS, $src), str_replace('/', DS, $dst));