PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/backupbuddy/_importbuddy/importbuddy/views/_html_1_stash.php

https://gitlab.com/mattswann/launch-housing
PHP | 308 lines | 213 code | 55 blank | 40 comment | 32 complexity | 016bf26d051be8178a9f8d1fe57e86f4 MD5 | raw file
  1. <!-- _html_1_stash.php -->
  2. <?php
  3. global $wp_version;
  4. $wp_version = 1;
  5. function stashAPI( $settings, $action, $additionalParams = array() ) {
  6. $api_url = 'https://stash-api.ithemes.com';
  7. global $wp_version;
  8. $url_params = array(
  9. 'action' => $action,
  10. 'user' => $settings['itxapi_username'],
  11. 'wp' => $wp_version,
  12. 'bb' => 0,
  13. 'ib' => pb_backupbuddy::$options['bb_version'],
  14. 'site' => 'importbuddy',
  15. 'timestamp' => time()
  16. );
  17. if ( isset( $settings['itxapi_password' ] ) ) { // Used on initital connection to
  18. $params = array( 'auth_token' => $settings['itxapi_password'] ); // itxapi_password is a HASH of user's password.
  19. } elseif ( isset( $settings['itxapi_token' ] ) ) { // Used on initital connection to
  20. $params = array( 'token' => $settings['itxapi_token'] ); // itxapi_password is a HASH of user's password.
  21. } else {
  22. $error = 'BackupBuddy Error #793749436: No valid token (itxapi_token) or hashed password (itxapi_password) specified. This should not happen.';
  23. self::_error( $error );
  24. trigger_error( $error, E_USER_NOTICE );
  25. return $error;
  26. }
  27. $params = array_merge( $params, $additionalParams );
  28. $body = array( 'request' => json_encode( $params ) );
  29. /*
  30. echo 'BODY:<pre>';
  31. print_r( $body );
  32. echo '</pre>';
  33. */
  34. $post_url = $api_url . '/?' . http_build_query( $url_params, null, '&' );
  35. $request = new RequestCore( $post_url );
  36. $request->set_method( 'POST' );
  37. $request->set_body( $body );
  38. $response = $request->send_request(true);
  39. /*
  40. echo 'RESPONSE:<pre>';
  41. print_r( $response->body );
  42. echo '</pre>';
  43. */
  44. if( ! $response->isOK() ) {
  45. pb_backupbuddy::status( 'error', 'Stash request for files failed.' );
  46. return $response->body;
  47. } else {
  48. // See if we got a json response.
  49. if ( ! $response_decoded = json_decode($response->body, true) ) {
  50. pb_backupbuddy::status( 'error', 'Stash did not get valid json response.' );
  51. }
  52. // Finally see if the API returned an error.
  53. if ( isset( $response_decoded['error'] ) ) {
  54. if ( isset( $response_decoded['error']['message'] ) ) {
  55. $error = 'Error #39752893. Server reported an error performing action `' . $action . '` with additional params: `' . print_r( $additionalParams, true ) . '`. Details: `' . print_r( $response_decoded['error'], true ) . '`.';
  56. pb_backupbuddy::status( 'warning', $error );
  57. return $response_decoded['error']['message'];
  58. } else {
  59. $error = 'Error #3823973. Received Stash API error but no message found. Details: `' . print_r( $response_decoded, true ) . '`.';
  60. pb_backupbuddy::status( 'warning', $error );
  61. return $error;
  62. }
  63. } else { // NO ERRORS
  64. return $response_decoded;
  65. }
  66. }
  67. }
  68. ?>
  69. <style>
  70. .widefat td {
  71. padding: 20px 7px;
  72. font-size: 14px;
  73. }
  74. .widefat td a:hover {
  75. text-decoration: none;
  76. }
  77. .widefat td form {
  78. margin-top: -12px;
  79. font-size: 12px;
  80. }
  81. .stash_backup_file {
  82. font-size: 1.2em;
  83. }
  84. </style>
  85. <?php
  86. $ITXAPI_KEY = 'ixho7dk0p244n0ob';
  87. $ITXAPI_URL = 'http://api.ithemes.com';
  88. $credentials_form = new pb_backupbuddy_settings( 'pre_settings', false, 'upload=stash#pluginbuddy-tabs-stash' ); // name, savepoint|false, additional querystring
  89. /*
  90. $credentials_form->add_setting( array(
  91. 'type' => 'hidden',
  92. 'name' => 'pass_hash',
  93. 'default' => PB_PASSWORD,
  94. ) );
  95. $credentials_form->add_setting( array(
  96. 'type' => 'hidden',
  97. 'name' => 'options',
  98. 'default' => htmlspecialchars( serialize( pb_backupbuddy::$options ) ),
  99. ) );
  100. */
  101. $credentials_form->add_setting( array(
  102. 'type' => 'text',
  103. 'name' => 'itxapi_username',
  104. 'title' => __( 'iThemes username', 'it-l10n-backupbuddy' ),
  105. 'rules' => 'required|string[1-45]',
  106. ) );
  107. $credentials_form->add_setting( array(
  108. 'type' => 'password',
  109. 'name' => 'itxapi_password_raw',
  110. 'title' => __( 'iThemes password', 'it-l10n-backupbuddy' ),
  111. 'rules' => 'required|string[1-45]',
  112. ) );
  113. $settings_result = $credentials_form->process();
  114. $login_welcome = '<center>' . __( 'Log in to Stash with your iThemes.com member account.', 'it-l10n-backupbuddy' ) . '<br><br>';
  115. if ( count( $settings_result ) == 0 ) { // No form submitted.
  116. echo $login_welcome;
  117. $credentials_form->display_settings( 'Connect to Stash' );
  118. } else { // Form submitted.
  119. if ( count( $settings_result['errors'] ) > 0 ) { // Form errors.
  120. echo $login_welcome;
  121. pb_backupbuddy::alert( implode( '<br>', $settings_result['errors'] ) );
  122. $credentials_form->display_settings( 'Connect to Stash' );
  123. } else { // No form errors; process!
  124. require_once( dirname( dirname( __FILE__ ) ) . '/lib/requestcore/requestcore.class.php' );
  125. //require_once( dirname( dirname( __FILE__ ) ) . '/lib/stash2/init.php' );
  126. require_once( dirname( dirname( __FILE__ ) ) . '/lib/stash2/class.itx_helper2.php' );
  127. global $wp_version;
  128. $itxapi_username = strtolower( $settings_result['data']['itxapi_username'] );
  129. $password_hash = iThemes_Credentials::get_password_hash( $itxapi_username, $settings_result['data']['itxapi_password_raw'] );
  130. $access_token = ITXAPI_Helper2::get_access_token( $itxapi_username, $password_hash, 'importbuddy', $wp_version );
  131. $settings = array(
  132. 'itxapi_username' => $itxapi_username,
  133. 'itxapi_password' => $access_token,
  134. );
  135. $response = stashAPI( $settings, 'connect' );
  136. $logged_in = false;
  137. if ( ! is_array( $response ) ) { // Error message.
  138. pb_backupbuddy::alert( 'Error #3983794 from server: `' . print_r( $response, true ) .'`.' );
  139. $credentials_form->display_settings( 'Submit' );
  140. } else {
  141. if ( isset( $response['error'] ) ) {
  142. pb_backupbuddy::alert( 'Error: ' . $response['error']['message'] );
  143. $credentials_form->display_settings( 'Submit' );
  144. } else {
  145. if ( isset( $response['token'] ) ) {
  146. $settings['itxapi_token'] = $response['token'];
  147. $itxapi_token = $settings['itxapi_token'];
  148. unset( $settings['itxapi_password'] ); // No longern eeded since we have token now.
  149. $logged_in = true;
  150. } else {
  151. pb_backupbuddy::alert( 'Error #34974734323: Unexpected server response. Token missing. Check your login and try again. Detailed response: `' . print_r( $response, true ) .'`.' );
  152. $credentials_form->display_settings( 'Submit' );
  153. }
  154. }
  155. }
  156. if ( true === $logged_in ) {
  157. // Get files.
  158. $stash_files = stashAPI( $settings, 'files' );
  159. /*
  160. echo 'FILES:<pre>';
  161. print_r( $stash_files );
  162. echo '</pre>';
  163. */
  164. // Finally see if the API returned an error.
  165. if ( ! is_array( $stash_files ) ) {
  166. pb_backupbuddy::alert( 'Stash Error: ' . $stash_files );
  167. $credentials_form->display_settings( 'Submit' );
  168. } else { // NO ERRORS
  169. // Sort in order & exclude unwanted backup types.
  170. $backup_list_temp = array();
  171. foreach( (array)$stash_files as $i => $stash_file ) {
  172. $file = $stash_file['filename'];
  173. $url = $stash_file['url'];
  174. $size = $stash_file['size'];
  175. $modified = $stash_file['uploaded_timestamp'];
  176. // Avoid collion for sorting.
  177. while( isset( $backup_list_temp[ $modified ] ) ) {
  178. $modified += 0.1;
  179. }
  180. if ( 'db' == $stash_file['backup_type']) {
  181. $backup_type = 'Database';
  182. } elseif( 'full' == $stash_file['backup_type'] ) {
  183. $backup_type = 'Full';
  184. } elseif( 'themes' == $stash_file['backup_type'] ) { // Omit from list.
  185. unset( $stash_files[ $i ] );
  186. continue;
  187. } elseif( 'plugins' == $stash_file['backup_type'] ) { // Omit from list.
  188. unset( $stash_files[ $i ] );
  189. continue;
  190. } else {
  191. $backup_type = 'Unknown';
  192. continue;
  193. }
  194. $stash_file['backup_type_pretty'] = $backup_type;
  195. $backup_list_temp[ $modified ] = $stash_file;
  196. }
  197. krsort( $backup_list_temp );
  198. $stash_files = $backup_list_temp;
  199. unset( $backup_list_temp );
  200. // Split up into sites.
  201. $backup_lists = array();
  202. foreach( $stash_files as $stash_file ) {
  203. $backup_lists[ $stash_file['site'] ][] = $stash_file;
  204. }
  205. unset( $stash_files );
  206. // Render table listing files.
  207. if ( count( $backup_lists ) == 0 ) {
  208. echo '<b>';
  209. _e( 'You have not sent any backups to Stash yet (or files are still transferring).', 'it-l10n-backupbuddy' );
  210. echo '</b>';
  211. } else {
  212. // Loop through each site.
  213. foreach( $backup_lists as $stash_files ) { // Each site.
  214. // Loop through each file within this site.
  215. $display_list = array();
  216. foreach( $stash_files as $stash_file ) { // Each backup in site.
  217. $site = $stash_file['site'];
  218. $display_list[] = array(
  219. $stash_file['backup_type_pretty'],
  220. '<span class="stash_backup_file">' . pb_backupbuddy::$format->date( pb_backupbuddy::$format->localize_time( $stash_file['uploaded_timestamp'] ), 'l, F j, Y - g:i A' ) . '</span> <span class="description">(' . pb_backupbuddy::$format->time_ago( $stash_file['uploaded_timestamp'] ) . ' ago)</span></a><br><a href="' . $stash_file['url'] . '">' . $stash_file['basename'] . '</a>',
  221. pb_backupbuddy::$format->file_size( $stash_file['size'] ),
  222. '<form action="?#pluginbuddy-tabs-server" method="POST">
  223. <input type="hidden" name="pass_hash" value="' . PB_PASSWORD . '">
  224. <input type="hidden" name="upload" value="stash">
  225. <input type="hidden" name="options" value="' . htmlspecialchars( serialize( pb_backupbuddy::$options ) ) . '">
  226. <input type="hidden" name="link" value="' . $stash_file['url'] . '">
  227. <input type="submit" name="submit" value="Restore" class="button-primary">
  228. </form>',
  229. );
  230. }
  231. echo '<h3>Site: ' . $site . '</h3>';
  232. pb_backupbuddy::$ui->list_table(
  233. $display_list,
  234. array(
  235. //'action' => pb_backupbuddy::page_url() . '&custom=remoteclient&destination_id=' . htmlentities( pb_backupbuddy::_GET( 'destination_id' ) ) . '&remote_path=' . htmlentities( pb_backupbuddy::_GET( 'remote_path' ) ),
  236. 'columns' => array( 'Type', 'Uploaded<img src="' . pb_backupbuddy::plugin_url() . '/images/sort_down.png" style="vertical-align: 0px;" title="Sorted most recent first"><span class="description">(Click to download)</span>', 'File Size', 'Action' ),
  237. 'css' => 'width: 100%;',
  238. )
  239. );
  240. echo '<br><br>';
  241. }
  242. }
  243. } // end no errors getting file info from API.
  244. } // end logged in.
  245. }
  246. } // end form submitted.
  247. ?>
  248. <br><hr>
  249. <center>
  250. <a href="https://sync.ithemes.com/stash" target="_blank" class="button button-secondary">Manage your Stash files via iThemes Sync</a>
  251. </center>
  252. <br>