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

/wp-content/plugins/redux-framework/ReduxCore/inc/class.redux_filesystem.php

https://gitlab.com/pankajmohale/chef2go
PHP | 287 lines | 201 code | 53 blank | 33 comment | 77 complexity | fa8db2ce711f17e5aff68cfe54d14b79 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. if ( ! class_exists( 'Redux_Filesystem' ) ) {
  6. class Redux_Filesystem {
  7. /**
  8. * Instance of this class.
  9. *
  10. * @since 1.0.0
  11. * @var object
  12. */
  13. protected static $instance = null;
  14. protected static $direct = null;
  15. private $creds = array();
  16. public $fs_object = null;
  17. public $parent = null;
  18. public function __construct() {
  19. $this->parent->admin_notices[] = array(
  20. 'type' => 'error',
  21. 'msg' => '<strong>' . __( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please check your permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%s" target="_blank">outlined here</a>.', 'redux-framework' ), 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ),
  22. 'id' => 'redux-wp-login',
  23. 'dismiss' => false,
  24. );
  25. }
  26. /**
  27. * Return an instance of this class.
  28. *
  29. * @since 1.0.0
  30. * @return object A single instance of this class.
  31. */
  32. public static function get_instance( $parent = null ) {
  33. // If the single instance hasn't been set, set it now.
  34. if ( null == self::$instance ) {
  35. self::$instance = new self;
  36. }
  37. if ( $parent !== null ) {
  38. self::$instance->parent = $parent;
  39. }
  40. return self::$instance;
  41. }
  42. public function ftp_form() {
  43. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  44. echo '<div class="wrap"><div class="error"><p>';
  45. echo '<strong>' . __( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2s" target="_blank">outlined here</a>.', 'redux-framework' ), Redux_Helpers::cleanFilePath( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' );
  46. echo '</p></div><h2></h2>' . '</div>';
  47. }
  48. }
  49. function filesystem_init( $form_url, $method = '', $context = false, $fields = null ) {
  50. global $wp_filesystem;
  51. if ( ! empty( $this->creds ) ) {
  52. return true;
  53. }
  54. ob_start();
  55. /* first attempt to get credentials */
  56. if ( false === ( $this->creds = request_filesystem_credentials( $form_url, $method, false, $context ) ) ) {
  57. $this->creds = array();
  58. $this->parent->ftp_form = ob_get_contents();
  59. ob_end_clean();
  60. /**
  61. * if we comes here - we don't have credentials
  62. * so the request for them is displaying
  63. * no need for further processing
  64. **/
  65. return false;
  66. }
  67. /* now we got some credentials - try to use them*/
  68. if ( ! WP_Filesystem( $this->creds ) ) {
  69. $this->creds = array();
  70. /* incorrect connection data - ask for credentials again, now with error message */
  71. request_filesystem_credentials( $form_url, '', true, $context );
  72. $this->parent->ftp_form = ob_get_contents();
  73. ob_end_clean();
  74. return false;
  75. }
  76. return true;
  77. }
  78. public static function load_direct() {
  79. if ( self::$direct === null ) {
  80. require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php';
  81. require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php';
  82. self::$direct = new WP_Filesystem_Direct( array() );
  83. }
  84. }
  85. public function execute( $action, $file = '', $params = '' ) {
  86. if ( empty( $this->parent->args ) ) {
  87. return;
  88. }
  89. if ( ! empty ( $params ) ) {
  90. extract( $params );
  91. }
  92. if ( ! is_dir( ReduxFramework::$_upload_dir ) ) {
  93. wp_mkdir_p( ReduxFramework::$_upload_dir );
  94. }
  95. // Setup the filesystem with creds
  96. require_once ABSPATH . '/wp-admin/includes/template.php';
  97. require_once ABSPATH . '/wp-includes/pluggable.php';
  98. require_once ABSPATH . '/wp-admin/includes/file.php';
  99. if ( $this->parent->args['menu_type'] == 'submenu' ) {
  100. $page_parent = $this->parent->args['page_parent'];
  101. $base = $page_parent . '?page=' . $this->parent->args['page_slug'];
  102. } else {
  103. $base = 'admin.php?page=' . $this->parent->args['page_slug'];
  104. }
  105. $url = wp_nonce_url( $base, 'redux-options' );
  106. $this->filesystem_init( $url, 'direct', dirname( $file ) );
  107. return $this->do_action( $action, $file, $params );
  108. }
  109. public function do_action( $action, $file = '', $params = '' ) {
  110. if ( ! empty ( $params ) ) {
  111. extract( $params );
  112. }
  113. global $wp_filesystem;
  114. if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
  115. if ( defined( 'FS_CHMOD_FILE' ) ) {
  116. $chmod = FS_CHMOD_FILE;
  117. } else {
  118. $chmod = 0644;
  119. }
  120. }
  121. $res = false;
  122. if ( ! isset( $recursive ) ) {
  123. $recursive = false;
  124. }
  125. //$target_dir = $wp_filesystem->find_folder( dirname( $file ) );
  126. // Do unique stuff
  127. if ( $action == 'mkdir' ) {
  128. if ( defined( 'FS_CHMOD_DIR' ) ) {
  129. $chmod = FS_CHMOD_DIR;
  130. } else {
  131. $chmod = 0755;
  132. }
  133. $res = $wp_filesystem->mkdir( $file );
  134. if ( ! $res ) {
  135. wp_mkdir_p( $file );
  136. $res = file_exists( $file );
  137. if ( ! $res ) {
  138. mkdir( $file, $chmod, true );
  139. $res = file_exists( $file );
  140. }
  141. }
  142. } elseif ( $action == 'rmdir' ) {
  143. $res = $wp_filesystem->rmdir( $file, $recursive );
  144. } elseif ( $action == 'copy' && ! isset( $this->filesystem->killswitch ) ) {
  145. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  146. $res = copy( $file, $destination );
  147. if ( $res ) {
  148. chmod( $destination, $chmod );
  149. }
  150. } else {
  151. $res = $wp_filesystem->copy( $file, $destination, $overwrite, $chmod );
  152. }
  153. } elseif ( $action == 'move' && ! isset( $this->filesystem->killswitch ) ) {
  154. $res = $wp_filesystem->copy( $file, $destination, $overwrite );
  155. } elseif ( $action == 'delete' ) {
  156. $res = $wp_filesystem->delete( $file, $recursive );
  157. } elseif ( $action == 'rmdir' ) {
  158. $res = $wp_filesystem->rmdir( $file, $recursive );
  159. } elseif ( $action == 'dirlist' ) {
  160. if ( ! isset( $include_hidden ) ) {
  161. $include_hidden = true;
  162. }
  163. $res = $wp_filesystem->dirlist( $file, $include_hidden, $recursive );
  164. } elseif ( $action == 'put_contents' && ! isset( $this->filesystem->killswitch ) ) {
  165. // Write a string to a file
  166. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  167. self::load_direct();
  168. $res = self::$direct->put_contents( $file, $content, $chmod );
  169. } else {
  170. $res = $wp_filesystem->put_contents( $file, $content, $chmod );
  171. }
  172. } elseif ( $action == 'chown' ) {
  173. // Changes file owner
  174. if ( isset( $owner ) && ! empty( $owner ) ) {
  175. $res = $wp_filesystem->chmod( $file, $chmod, $recursive );
  176. }
  177. } elseif ( $action == 'owner' ) {
  178. // Gets file owner
  179. $res = $wp_filesystem->owner( $file );
  180. } elseif ( $action == 'chmod' ) {
  181. if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
  182. $chmod = false;
  183. }
  184. $res = $wp_filesystem->chmod( $file, $chmod, $recursive );
  185. } elseif ( $action == 'get_contents' ) {
  186. // Reads entire file into a string
  187. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  188. self::load_direct();
  189. $res = self::$direct->get_contents( $file );
  190. } else {
  191. $res = $wp_filesystem->get_contents( $file );
  192. }
  193. } elseif ( $action == 'get_contents_array' ) {
  194. // Reads entire file into an array
  195. $res = $wp_filesystem->get_contents_array( $file );
  196. } elseif ( $action == 'object' ) {
  197. $res = $wp_filesystem;
  198. } elseif ( $action == 'unzip' ) {
  199. $unzipfile = unzip_file( $file, $destination );
  200. if ( $unzipfile ) {
  201. $res = true;
  202. }
  203. }
  204. if ( ! $res ) {
  205. if ($action == 'dirlist') {
  206. if (empty($res) || $res == false || $res == '' ) {
  207. return;
  208. }
  209. if (is_array($res) && empty($res)) {
  210. return;
  211. }
  212. if (!is_array($res)) {
  213. if (count(glob("$file*")) == 0) {
  214. return;
  215. }
  216. }
  217. }
  218. $this->killswitch = true;
  219. $this->parent->admin_notices[] = array(
  220. 'type' => 'error',
  221. 'msg' => '<strong>' . __( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2s" target="_blank">outlined here</a>.', 'redux-framework' ), Redux_Helpers::cleanFilePath( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ),
  222. 'id' => 'redux-wp-login',
  223. 'dismiss' => false,
  224. );
  225. //add_action( "redux/page/{$this->parent->args['opt_name']}/form/before", array(
  226. // $this,
  227. // 'ftp_form'
  228. //) );
  229. }
  230. return $res;
  231. }
  232. }
  233. Redux_Filesystem::get_instance();
  234. }