PageRenderTime 21ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/websumon/tosnib
PHP | 285 lines | 200 code | 52 blank | 33 comment | 77 complexity | d05b1066aa74886017a5cf12bf98209a 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-admin/includes/file.php';
  98. if ( $this->parent->args['menu_type'] == 'submenu' ) {
  99. $page_parent = $this->parent->args['page_parent'];
  100. $base = $page_parent . '?page=' . $this->parent->args['page_slug'];
  101. } else {
  102. $base = 'admin.php?page=' . $this->parent->args['page_slug'];
  103. }
  104. $url = wp_nonce_url( $base, 'redux-options' );
  105. $this->filesystem_init( $url, 'direct', dirname( $file ) );
  106. return $this->do_action( $action, $file, $params );
  107. }
  108. public function do_action( $action, $file = '', $params = '' ) {
  109. if ( ! empty ( $params ) ) {
  110. extract( $params );
  111. }
  112. global $wp_filesystem;
  113. if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
  114. if ( defined( 'FS_CHMOD_FILE' ) ) {
  115. $chmod = FS_CHMOD_FILE;
  116. } else {
  117. $chmod = 0644;
  118. }
  119. }
  120. $res = false;
  121. if ( ! isset( $recursive ) ) {
  122. $recursive = false;
  123. }
  124. //$target_dir = $wp_filesystem->find_folder( dirname( $file ) );
  125. // Do unique stuff
  126. if ( $action == 'mkdir' ) {
  127. if ( defined( 'FS_CHMOD_DIR' ) ) {
  128. $chmod = FS_CHMOD_DIR;
  129. } else {
  130. $chmod = 0755;
  131. }
  132. $res = $wp_filesystem->mkdir( $file );
  133. if ( ! $res ) {
  134. wp_mkdir_p( $file );
  135. $res = file_exists( $file );
  136. if ( ! $res ) {
  137. mkdir( $file, $chmod, true );
  138. $res = file_exists( $file );
  139. }
  140. }
  141. } elseif ( $action == 'rmdir' ) {
  142. $res = $wp_filesystem->rmdir( $file, $recursive );
  143. } elseif ( $action == 'copy' && ! isset( $this->filesystem->killswitch ) ) {
  144. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  145. $res = copy( $file, $destination );
  146. if ( $res ) {
  147. chmod( $destination, $chmod );
  148. }
  149. } else {
  150. $res = $wp_filesystem->copy( $file, $destination, $overwrite, $chmod );
  151. }
  152. } elseif ( $action == 'move' && ! isset( $this->filesystem->killswitch ) ) {
  153. $res = $wp_filesystem->copy( $file, $destination, $overwrite );
  154. } elseif ( $action == 'delete' ) {
  155. $res = $wp_filesystem->delete( $file, $recursive );
  156. } elseif ( $action == 'rmdir' ) {
  157. $res = $wp_filesystem->rmdir( $file, $recursive );
  158. } elseif ( $action == 'dirlist' ) {
  159. if ( ! isset( $include_hidden ) ) {
  160. $include_hidden = true;
  161. }
  162. $res = $wp_filesystem->dirlist( $file, $include_hidden, $recursive );
  163. } elseif ( $action == 'put_contents' && ! isset( $this->filesystem->killswitch ) ) {
  164. // Write a string to a file
  165. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  166. self::load_direct();
  167. $res = self::$direct->put_contents( $file, $content, $chmod );
  168. } else {
  169. $res = $wp_filesystem->put_contents( $file, $content, $chmod );
  170. }
  171. } elseif ( $action == 'chown' ) {
  172. // Changes file owner
  173. if ( isset( $owner ) && ! empty( $owner ) ) {
  174. $res = $wp_filesystem->chmod( $file, $chmod, $recursive );
  175. }
  176. } elseif ( $action == 'owner' ) {
  177. // Gets file owner
  178. $res = $wp_filesystem->owner( $file );
  179. } elseif ( $action == 'chmod' ) {
  180. if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
  181. $chmod = false;
  182. }
  183. $res = $wp_filesystem->chmod( $file, $chmod, $recursive );
  184. } elseif ( $action == 'get_contents' ) {
  185. // Reads entire file into a string
  186. if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
  187. self::load_direct();
  188. $res = self::$direct->get_contents( $file );
  189. } else {
  190. $res = $wp_filesystem->get_contents( $file );
  191. }
  192. } elseif ( $action == 'get_contents_array' ) {
  193. // Reads entire file into an array
  194. $res = $wp_filesystem->get_contents_array( $file );
  195. } elseif ( $action == 'object' ) {
  196. $res = $wp_filesystem;
  197. } elseif ( $action == 'unzip' ) {
  198. $unzipfile = unzip_file( $file, $destination );
  199. if ( $unzipfile ) {
  200. $res = true;
  201. }
  202. }
  203. if ( ! $res ) {
  204. if ($action == 'dirlist') {
  205. if (empty($res) || $res == false || $res == '' ) {
  206. return;
  207. }
  208. if (is_array($res) && empty($res)) {
  209. return;
  210. }
  211. if (!is_array($res)) {
  212. if (count(glob("$file*")) == 0) {
  213. return;
  214. }
  215. }
  216. }
  217. $this->killswitch = true;
  218. $this->parent->admin_notices[] = array(
  219. 'type' => 'error',
  220. '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' ),
  221. 'id' => 'redux-wp-login',
  222. 'dismiss' => false,
  223. );
  224. //add_action( "redux/page/{$this->parent->args['opt_name']}/form/before", array(
  225. // $this,
  226. // 'ftp_form'
  227. //) );
  228. }
  229. return $res;
  230. }
  231. }
  232. Redux_Filesystem::get_instance();
  233. }