/** * creates the content for the howto-metabox * * @access public * * @return void * @since 1.0 */ public function connect() { $helper = new WPB_Google_Drive_Cdn_Service_Helper($this->_google_drive_cdn); $is_api_working = $helper->is_api_working(); $client = $this->_google_drive_cdn->get_google_client(); $client->setState('got_code'); // get offline access $client->setAccessType('offline'); // makes the UI window appear ALWAYS $client->setApprovalPrompt('force'); echo '<p>' . __('Please note: you will be redirected to a Google\'s authorization page. Please press the blue button to give this application the right to upload files.', $this->_google_drive_cdn->get_textdomain()) . '</p>'; echo '<a class="button-primary" href="' . $client->createAuthUrl() . '">' . __('Grant writing permissions to your Google Drive', $this->_google_drive_cdn->get_textdomain()) . '</a>'; if (!$is_api_working) { echo '<div class="error"><p>', __('The Google Drive API is currently not working.', $this->_google_drive_cdn->get_textdomain()), ' ' . __('Try to (re-)authenticate by using the button "Grant writing permissions to your Google Drive" button.', $this->_google_drive_cdn->get_textdomain()), '</p></div>'; } }
/** * A loop that syncs files to the Google Drive * @since 1.0 */ public function db_sync() { //if( ! isset( $_REQUEST['wpbgdc_nonce'] ) ) return; // nonces are always attached to a user // therefore nonces will not work because there is no user that can have a nonce //if( ! wp_verify_nonce( $_REQUEST['wpbgdc_nonce'], 'wpbgdc_sync_nonce' ) ) return; global $wpdb; if (!$wpdb instanceof wpdb) { return; } // starting sync update_option('wpbgdc_currently_syncing', time()); // delete old caching entries if any WPB_Google_Drive_Cdn_Service_Helper::remove_old_entries(); // @see http://codex.wordpress.org/Function_Reference/wp_upload_dir //$upload_dir = wp_upload_dir(); //$content_url = trailingslashit( $upload_dir['baseurl'] ); // this is wp-content/uploads/ $content_url = trailingslashit(content_url()); // this is wp-content/ //$upload_dir = trailingslashit( $upload_dir['basedir'] ); $content_dir = trailingslashit(WP_CONTENT_DIR); $service_helper = new WPB_Google_Drive_Cdn_Service_Helper($this); /** * If the "Upload new files only" option is set, the plugin will not check if the file has changed on Google Drive * Instead it just uploads the files that have not yet been uploaded */ $options = get_option('wpbgdc'); if (!isset($options['upload_new_files_only'])) { $options['upload_new_files_only'] = 0; } $upload_only = (bool) $options['upload_new_files_only']; /*** * CHECK FOR NEW OR UPDATED FILES AND UPLOAD OR UPDATE THEM */ if ($upload_only) { $db_files = $wpdb->get_results('SELECT * FROM `' . $wpdb->prefix . 'wpbgdc_files` WHERE `file_synced` = "0" AND `file_drive_id` = ""', OBJECT); } else { $db_files = $wpdb->get_results('SELECT * FROM `' . $wpdb->prefix . 'wpbgdc_files` WHERE `file_synced` = "0"', OBJECT); } /** * check if the script should sleep after a file upload */ $sleeping_time = WPB_Google_Drive_Cdn_Settings::get_setting('sleep_time'); $sleeping_time = intval($sleeping_time); $sleeping_time = max(0, $sleeping_time); foreach ($db_files as $db_file) { $url = $db_file->file_live_url; $is_content_url = false !== strpos($url, $content_url) ? true : false; /** * Workaround for SSL-Sites * This sets the $content_url to a new value when the file could not be found on https:// but on http * or the other way around */ if (!$is_content_url) { // are we on ssl? if (false !== strpos($content_url, 'https://')) { // yes, we are on SSL // check if the file was found on non ssl-url $is_content_url = false !== stripos($url, str_replace('https://', 'http://', $content_url)) ? true : false; if ($is_content_url) { $content_url = str_replace('https://', 'http://', $content_url); } } else { // no, we are not on SSL $is_content_url = false !== stripos($url, str_replace('http://', 'https://', $content_url)) ? true : false; if ($is_content_url) { $content_url = str_replace('http://', 'https://', $content_url); } } } if (!$is_content_url) { // set a flag in the db entry to prevent doing the same operation again $wpdb->update($wpdb->prefix . 'wpbgdc_files', array('file_synced' => 1), array('file_id' => $db_file->file_id)); continue; } // get the path to the file $file = str_replace($content_url, $content_dir, $url); // if the file does not exist on the server, return the url = stop here if (!is_file($file)) { // set a flag in the db entry to prevent doing the same operation again $wpdb->update($wpdb->prefix . 'wpbgdc_files', array('file_synced' => 1), array('file_id' => $db_file->file_id)); continue; } // count file uploads. set to 0 if not yet set if (!isset($this->file_upload_count)) { $this->file_upload_count = 0; } // stop if 3 files have been uploaded because otherwise it's too much for the webserver if ($this->file_upload_count >= 3) { // redirect will not work here because this was invoked by wp_remote_post and this function does not follow redirects to an unlimited length // wp_redirect( admin_url( 'admin-ajax.php' ) . '?action=wpbgdc_db_sync&wpbgdc_stop_ping=1&wpbgdc_nonce=' . wp_create_nonce( 'wpbgdc_sync_nonce' ), 301 ); // so we start doing another ping, but at first we have to stop the current sync process to restart it later (in the ping_sync() function) update_option('wpbgdc_currently_syncing', 0); if (isset($_REQUEST['wpbgdc_stop_ping'])) { unset($_REQUEST['wpbgdc_stop_ping']); } $this->ping_sync(); // then die die; } // upload the file and update the database entry $service_helper->file_upload($file, $url); sleep($sleeping_time); // only count if a file has been uploaded if ($service_helper->_file_uploaded) { $this->file_upload_count++; } // mark as synced anyway (it doesn't really matter if the file has been uploaded or not // the only thing that matters if there has been the approach to upload it $wpdb->update($wpdb->prefix . 'wpbgdc_files', array('file_synced' => 1), array('file_id' => $db_file->file_id)); } // stop syncing update_option('wpbgdc_currently_syncing', 0); // reset the synced flag within all database entries $wpdb->query('UPDATE `' . $wpdb->prefix . 'wpbgdc_files` SET `file_synced`= 0'); }