r, 'externalRevokeUrl' => $this->getExternalRevokeUrl(), ], $echo ); } /** * Update data from http request, this method don't save data, just update object properties * * @param string $message Message * * @return bool True if success and all data is valid, false otherwise */ public function updateFromHttpRequest(&$message = '') { if ((parent::updateFromHttpRequest($message) === false)) { return false; } $this->config['max_packages'] = SnapUtil::sanitizeIntInput(SnapUtil::INPUT_REQUEST, 'onedrive_msgraph_max_files', 10); $oldFolder = $this->config['storage_folder']; $this->config['storage_folder'] = self::getSanitizedInputFolder('_onedrive_msgraph_storage_folder', 'remove'); $this->config['all_folders_perm'] = SnapUtil::sanitizeBoolInput(SnapUtil::INPUT_REQUEST, 'onedrive_msgraph_all_folders_read_write_perm', false); if ($this->isAuthorized() && $oldFolder != $this->config['storage_folder']) { $this->config['storage_folder_id'] = ''; // Create new folder $folder = $this->getOneDriveStorageFolder(); $this->config['storage_folder_id'] = $folder->id; $this->config['storage_folder_web_url'] = $folder->webUrl; $this->save(); } $message = sprintf( __('OneDrive Storage Updated.', 'duplicator-pro'), $this->getStorageFolder() ); return true; } /** * Get account info * * @return false|object */ protected function getAccountInfo() { if (! $this->isAuthorized()) { return false; } $storageFolder = $this->getOneDriveStorageFolder(); if (!$storageFolder || empty($storageFolder->user)) { return false; } return (object) $storageFolder->user; } /** * Get onedrive storage folder * * @return OneDriveStoragePathInfo|false */ protected function getOneDriveStorageFolder() { $adapter = $this->getAdapter(); if (! $adapter->isValid()) { DUP_PRO_Log::trace("OneDrive adapter is not valid, can't get storage folder."); return false; } if (!$this->config['storage_folder_id']) { if (! $adapter->initialize($error)) { DUP_PRO_Log::trace("Failed to initialize OneDrive adapter: $error"); return false; } $folder = $adapter->getPathInfo('/'); $this->config['storage_folder_id'] = $folder->id; $this->save(); } else { $folder = $adapter->getPathInfo('/'); } return $folder; } /** * Get stoage adapter * * @return OnedriveAdapter */ protected function getAdapter() { if (!$this->adapter) { $global = DUP_PRO_Global_Entity::getInstance(); $token = $this->getTokenFromConfig(); $this->adapter = new OneDriveAdapter( $token, $this->config['storage_folder'], $this->config['storage_folder_id'], !$global->ssl_disableverify, ($global->ssl_useservercerts ? '' : DUPLICATOR_PRO_CERT_PATH), DynamicGlobalEntity::getInstance()->getVal('onedrive_http_version', OnedriveAdapter::HTTP_VERSION_AUTO) ); if (! $this->adapter->initialize($error)) { DUP_PRO_Log::trace("Failed to initialize OneDrive adapter: $error"); } } return $this->adapter; } /** * Get the token entity using config values * * @return TokenEntity|false */ public function getTokenFromConfig() { $token = new TokenEntity(self::getSType(), [ 'created' => $this->config['token_obtained'], 'expires_in' => 3600, 'scope' => self::GRAPH_SCOPES, 'access_token' => $this->config['access_token'], 'refresh_token' => $this->config['refresh_token'], ]); if ($token->isAboutToExpire()) { $token->refresh(true); if (!$token->isValid()) { return false; } $this->config['token_obtained'] = $token->getCreated(); $this->config['refresh_token'] = $token->getRefreshToken(); $this->config['access_token'] = $token->getAccessToken(); $this->save(); } return $token; } /** * @return void * @throws Exception */ public static function registerType() { parent::registerType(); add_action('duplicator_update_global_storage_settings', function () { $dGlobal = DynamicGlobalEntity::getInstance(); foreach (static::getDefaultSettings() as $key => $default) { $value = SnapUtil::sanitizeIntInput(SnapUtil::INPUT_REQUEST, $key, $default); $dGlobal->setVal($key, $value); } }); } /** * Get default settings * * @return array */ protected static function getDefaultSettings() { return [ 'onedrive_http_version' => OnedriveAdapter::HTTP_VERSION_AUTO, 'onedrive_upload_chunksize_in_kb' => DUPLICATOR_PRO_ONEDRIVE_UPLOAD_CHUNK_DEFAULT_SIZE_IN_KB, 'onedrive_download_chunksize_in_kb' => self::DEFAULT_DOWNLOAD_CHUNK_SIZE_IN_KB, ]; } /** * @return void */ public static function renderGlobalOptions() { $dGlobal = DynamicGlobalEntity::getInstance(); TplMng::getInstance()->render( 'onedriveaddon/configs/global_options', [ 'uploadChunkSize' => $dGlobal->getVal('onedrive_upload_chunksize_in_kb', DUPLICATOR_PRO_ONEDRIVE_UPLOAD_CHUNK_DEFAULT_SIZE_IN_KB), 'downloadChunkSize' => $dGlobal->getVal('onedrive_download_chunksize_in_kb', self::DEFAULT_DOWNLOAD_CHUNK_SIZE_IN_KB), 'httpVersion' => $dGlobal->getVal('onedrive_http_version', OnedriveAdapter::HTTP_VERSION_AUTO), ] ); } }