/wp-content/plugins/codestyling-localization/includes/class-filesystem-translationfile.php

https://gitlab.com/oxidigitaluser/liguelista · PHP · 349 lines · 301 code · 41 blank · 7 comment · 81 complexity · 01685566cb4d9c8896eb91843b36ded6 MD5 · raw file

  1. <?php
  2. require_once('class-translationfile.php');
  3. class CspFileSystem_TranslationFile extends CspTranslationFile {
  4. function CspFileSystem_TranslationFile($type = 'unknown') {
  5. $this->__construct($type);
  6. }
  7. function __construct($type = 'unknown') {
  8. parent::__construct($type);
  9. //backward compatibility
  10. $this->supports_filesystem = function_exists('request_filesystem_credentials');
  11. $this->real_abspath = str_replace('\\', '/', ABSPATH);
  12. }
  13. function destroy_pofile($pofile) {
  14. global $wp_filesystem, $parent_file;
  15. if ($this->supports_filesystem) {
  16. $current_parent = $parent_file;
  17. $parent_file = 'tools.php'; //needed for screen icon :-)
  18. if (function_exists('set_current_screen')) set_current_screen('tools'); //WP 3.0 fix
  19. //check the file system
  20. ob_start();
  21. $url = 'admin-ajax.php';
  22. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  23. $data = ob_get_contents();
  24. ob_end_clean();
  25. if( ! empty($data) ){
  26. header('Status: 401 Unauthorized');
  27. header('HTTP/1.1 401 Unauthorized');
  28. echo $data;
  29. exit;
  30. }
  31. return;
  32. }
  33. if ( ! WP_Filesystem($credentials) ) {
  34. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  35. $data = ob_get_contents();
  36. ob_end_clean();
  37. if( ! empty($data) ){
  38. header('Status: 401 Unauthorized');
  39. header('HTTP/1.1 401 Unauthorized');
  40. echo $data;
  41. exit;
  42. }
  43. return;
  44. }
  45. ob_end_clean();
  46. $parent_file = $current_parent;
  47. }
  48. $error = false;
  49. if(!$this->supports_filesystem || $wp_filesystem->method == 'direct') {
  50. if (file_exists($pofile)) if (!@unlink($pofile)) $error = sprintf(__("You do not have the permission to delete the file '%s'.", CSP_PO_TEXTDOMAIN), $mofile);
  51. }else {
  52. $target_file = str_replace('//', '/', $wp_filesystem->abspath().str_replace($this->real_abspath, '',$pofile));
  53. if($wp_filesystem->is_file($target_file)) if (!$wp_filesystem->delete($target_file)) $error = sprintf(__("You do not have the permission to delete the file '%s'.", CSP_PO_TEXTDOMAIN), $pofile);
  54. }
  55. if ($error) {
  56. header('Status: 404 Not Found');
  57. header('HTTP/1.1 404 Not Found');
  58. echo $error;
  59. exit();
  60. }
  61. }
  62. function destroy_mofile($mofile) {
  63. global $wp_filesystem, $parent_file;
  64. if ($this->supports_filesystem) {
  65. $current_parent = $parent_file;
  66. $parent_file = 'tools.php'; //needed for screen icon :-)
  67. if (function_exists('set_current_screen')) set_current_screen('tools'); //WP 3.0 fix
  68. //check the file system
  69. ob_start();
  70. $url = 'admin-ajax.php';
  71. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  72. $data = ob_get_contents();
  73. ob_end_clean();
  74. if( ! empty($data) ){
  75. header('Status: 401 Unauthorized');
  76. header('HTTP/1.1 401 Unauthorized');
  77. echo $data;
  78. exit;
  79. }
  80. return;
  81. }
  82. if ( ! WP_Filesystem($credentials) ) {
  83. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  84. $data = ob_get_contents();
  85. ob_end_clean();
  86. if( ! empty($data) ){
  87. header('Status: 401 Unauthorized');
  88. header('HTTP/1.1 401 Unauthorized');
  89. echo $data;
  90. exit;
  91. }
  92. return;
  93. }
  94. ob_end_clean();
  95. $parent_file = $current_parent;
  96. }
  97. $error = false;
  98. if (!$this->supports_filesystem || $wp_filesystem->method == 'direct') {
  99. if (file_exists($mofile)) if (!@unlink($mofile)) $error = sprintf(__("You do not have the permission to delete the file '%s'.", CSP_PO_TEXTDOMAIN), $mofile);
  100. }else {
  101. $target_file = str_replace('//', '/', $wp_filesystem->abspath().str_replace($this->real_abspath, '',$mofile));
  102. if($wp_filesystem->is_file($target_file)) if (!$wp_filesystem->delete($target_file)) $error = sprintf(__("You do not have the permission to delete the file '%s'.", CSP_PO_TEXTDOMAIN), $mofile);
  103. }
  104. if ($error) {
  105. header('Status: 404 Not Found');
  106. header('HTTP/1.1 404 Not Found');
  107. echo $error;
  108. exit();
  109. }
  110. }
  111. function create_directory($path) {
  112. global $wp_filesystem, $parent_file;
  113. if ($this->supports_filesystem) {
  114. $current_parent = $parent_file;
  115. $parent_file = 'tools.php'; //needed for screen icon :-)
  116. if (function_exists('set_current_screen')) set_current_screen('tools'); //WP 3.0 fix
  117. //check the file system
  118. ob_start();
  119. $url = 'admin-ajax.php';
  120. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  121. $data = ob_get_contents();
  122. ob_end_clean();
  123. if( ! empty($data) ){
  124. header('Status: 401 Unauthorized');
  125. header('HTTP/1.1 401 Unauthorized');
  126. echo $data;
  127. exit;
  128. }
  129. return;
  130. }
  131. if ( ! WP_Filesystem($credentials) ) {
  132. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  133. $data = ob_get_contents();
  134. ob_end_clean();
  135. if( ! empty($data) ){
  136. header('Status: 401 Unauthorized');
  137. header('HTTP/1.1 401 Unauthorized');
  138. echo $data;
  139. exit;
  140. }
  141. return;
  142. }
  143. ob_end_clean();
  144. $parent_file = $current_parent;
  145. }
  146. if (!$this->supports_filesystem || $wp_filesystem->method == 'direct') {
  147. return @mkdir($path);
  148. }else{
  149. $target_dir = str_replace('//', '/', $wp_filesystem->abspath().str_replace($this->real_abspath, '',$path));
  150. if(!$wp_filesystem->mkdir($target_dir, FS_CHMOD_DIR) && ! $wp_filesystem->is_dir($target_dir)) return false;
  151. else return true;
  152. }
  153. }
  154. function change_permission($filename) {
  155. global $wp_filesystem, $parent_file;
  156. if ($this->supports_filesystem) {
  157. $current_parent = $parent_file;
  158. $parent_file = 'tools.php'; //needed for screen icon :-)
  159. if (function_exists('set_current_screen')) set_current_screen('tools'); //WP 3.0 fix
  160. //check the file system
  161. ob_start();
  162. $url = 'admin-ajax.php';
  163. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  164. $data = ob_get_contents();
  165. ob_end_clean();
  166. if( ! empty($data) ){
  167. header('Status: 401 Unauthorized');
  168. header('HTTP/1.1 401 Unauthorized');
  169. echo $data;
  170. exit;
  171. }
  172. return;
  173. }
  174. if ( ! WP_Filesystem($credentials) ) {
  175. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  176. $data = ob_get_contents();
  177. ob_end_clean();
  178. if( ! empty($data) ){
  179. header('Status: 401 Unauthorized');
  180. header('HTTP/1.1 401 Unauthorized');
  181. echo $data;
  182. exit;
  183. }
  184. return;
  185. }
  186. ob_end_clean();
  187. $parent_file = $current_parent;
  188. }
  189. $error = false;
  190. if (!$this->supports_filesystem || $wp_filesystem->method == 'direct' || stripos(php_uname('s'),'windows') !== false) {
  191. if (file_exists($filename)) {
  192. @chmod($filename, 0644);
  193. if(!is_writable($filename)) {
  194. @chmod($filename, 0664);
  195. if (!is_writable($filename)) {
  196. @chmod($filename, 0666);
  197. }
  198. if (!is_writable($filename)) $error = __('Server Restrictions: Changing file rights is not permitted.', CSP_PO_TEXTDOMAIN);
  199. }
  200. }
  201. else $error = sprintf(__("You do not have the permission to modify the file rights for a not existing file '%s'.", CSP_PO_TEXTDOMAIN), $filename);
  202. } else {
  203. $target_file = str_replace('//', '/', $wp_filesystem->abspath().str_replace($this->real_abspath, '',$filename));
  204. if($wp_filesystem->is_file($target_file)) {
  205. $wp_filesystem->chmod($target_file, 0644);
  206. if(!is_writable($filename)) {
  207. $wp_filesystem->chmod($target_file, 0664);
  208. if (!is_writable($filename)) {
  209. $wp_filesystem->chmod($target_file, 0666);
  210. }
  211. if (!is_writable($filename)) $error = __('Server Restrictions: Changing file rights is not permitted.', CSP_PO_TEXTDOMAIN);
  212. }
  213. }else{
  214. $error = sprintf(__("You do not have the permission to modify the file rights for a not existing file '%s'.", CSP_PO_TEXTDOMAIN), $filename);
  215. }
  216. }
  217. if ($error) {
  218. header('Status: 404 Not Found');
  219. header('HTTP/1.1 404 Not Found');
  220. echo $error;
  221. exit();
  222. }
  223. }
  224. function write_pofile($pofile, $last = false, $textdomain = false, $tds = 'yes') {
  225. global $wp_filesystem, $parent_file;
  226. if ($this->supports_filesystem) {
  227. $current_parent = $parent_file;
  228. $parent_file = 'tools.php'; //needed for screen icon :-)
  229. if (function_exists('set_current_screen')) set_current_screen('tools'); //WP 3.0 fix
  230. //check the file system
  231. ob_start();
  232. $url = 'admin-ajax.php';
  233. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  234. $data = ob_get_contents();
  235. ob_end_clean();
  236. if( ! empty($data) ){
  237. header('Status: 401 Unauthorized');
  238. header('HTTP/1.1 401 Unauthorized');
  239. echo $data;
  240. exit;
  241. }
  242. return;
  243. }
  244. if ( ! WP_Filesystem($credentials) ) {
  245. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  246. $data = ob_get_contents();
  247. ob_end_clean();
  248. if( ! empty($data) ){
  249. header('Status: 401 Unauthorized');
  250. header('HTTP/1.1 401 Unauthorized');
  251. echo $data;
  252. exit;
  253. }
  254. return;
  255. }
  256. ob_end_clean();
  257. $parent_file = $current_parent;
  258. }
  259. if (!$this->supports_filesystem || $wp_filesystem->method == 'direct') {
  260. return parent::write_pofile($pofile, $last, $textdomain, $tds);
  261. }else{
  262. $target_file = str_replace('//', '/', $wp_filesystem->abspath().str_replace($this->real_abspath, '',$pofile));
  263. return $wp_filesystem->put_contents($target_file, parent::ftp_get_pofile_content($pofile, $last, $textdomain, $tds), FS_CHMOD_FILE);
  264. }
  265. }
  266. function write_mofile($mofile, $textdomain) {
  267. global $wp_filesystem, $parent_file;
  268. if ($this->supports_filesystem) {
  269. $current_parent = $parent_file;
  270. $parent_file = 'tools.php'; //needed for screen icon :-)
  271. if (function_exists('set_current_screen')) set_current_screen('tools'); //WP 3.0 fix
  272. //check the file system
  273. ob_start();
  274. $url = 'admin-ajax.php';
  275. if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  276. $data = ob_get_contents();
  277. ob_end_clean();
  278. if( ! empty($data) ){
  279. header('Status: 401 Unauthorized');
  280. header('HTTP/1.1 401 Unauthorized');
  281. echo $data;
  282. exit;
  283. }
  284. return;
  285. }
  286. if ( ! WP_Filesystem($credentials) ) {
  287. request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  288. $data = ob_get_contents();
  289. ob_end_clean();
  290. if( ! empty($data) ){
  291. header('Status: 401 Unauthorized');
  292. header('HTTP/1.1 401 Unauthorized');
  293. echo $data;
  294. exit;
  295. }
  296. return;
  297. }
  298. ob_end_clean();
  299. $parent_file = $current_parent;
  300. }
  301. if (!$this->supports_filesystem || $wp_filesystem->method == 'direct') {
  302. return parent::write_mofile($mofile, $textdomain);
  303. }else{
  304. $target_file = str_replace('//', '/', $wp_filesystem->abspath().str_replace($this->real_abspath, '',$mofile));
  305. return $wp_filesystem->put_contents($target_file, parent::ftp_get_mofile_content($mofile, $textdomain), FS_CHMOD_FILE);
  306. }
  307. }
  308. }