/** * Converts a fully fledged data bean into a basic * object for the Moodle DB layer * @param TxttoolsAccount $txttoolsAccount Account to convert * @param object $existingRecord Existing DB record * @return object Converted object * @version 2015062901 * @since 2011042601 */ private function convertBeanToStandardClass(TxttoolsAccount $txttoolsAccount, $existingRecord = null) { if ($existingRecord == null) { $existingRecord = new object(); } // Map fields in $existingRecord->username = $txttoolsAccount->getUsername(); $existingRecord->password = $txttoolsAccount->getEncryptedPassword(); $existingRecord->description = $txttoolsAccount->getDescription(); $existingRecord->defaultuser = $txttoolsAccount->getDefaultUser()->getId(); $existingRecord->creditsused = $txttoolsAccount->getCreditsUsed(); $existingRecord->creditsremaining = $txttoolsAccount->getCreditsRemaining(); $existingRecord->outboundenabled = $txttoolsAccount->isOutboundEnabled() ? 1 : 0; $existingRecord->inboundenabled = $txttoolsAccount->isInboundEnabled() ? 1 : 0; $existingRecord->accounttype = $txttoolsAccount->getBillingType(); $existingRecord->lastupdate = $txttoolsAccount->getLastUpdate(); $existingRecord->url = $txttoolsAccount->getUrl(); $existingRecord->location = $txttoolsAccount->getLocation(); return $existingRecord; }
/** * Method to ensure that all account names within the * module are outputted in the same manner * @param TxttoolsAccount $account * @return string Formatted display string * @version 2015062901 * @since 2011102401 */ public static final function formatAccountForDisplay($account) { $formatted = $account->getUsername(); $description = $account->getDescription(); if (isset($description) && !empty($description)) { $formatted = $formatted . " (" . $description . ")"; } $formatted = $formatted . " - " . $account->getUrl(); return $formatted; }
/** * Sends requests to txttools. * Takes an array of requests and shoves them to txttools, then grabs the responses and passes them back up to the calling object * @param array $requestArray Requests to send * @param TxttoolsAccount $ctxtAccount txttools account to use * @return array Unparsed response from txttools * @throws Exception * @throws HTTPException * @throws SocketException * @throws dml_exception * @version 2015062901 * @since 2011032901 */ public function sendData($requestArray, $ctxtAccount) { // If the parameter is not formatted as an array, make it one if (!is_array($requestArray)) { $requestArray = array($requestArray); } // Set defaults $pathPrefix = "https://"; $txttoolsHost = $ctxtAccount->getUrl(); $filePath = '/connectors/XML/xml.jsp'; $fullPath = $filePath; $connectionPrefix = 'ssl://'; $connectionHost = $txttoolsHost; $connectionPort = 443; // Get proxy details $proxyHost = get_config('moodletxt', 'Proxy_Host'); $proxyPort = get_config('moodletxt', 'Proxy_Port'); $proxyUsername = get_config('moodletxt', 'Proxy_Username'); $proxyPassword = get_config('moodletxt', 'Proxy_Password'); // Check for protocol if ($this->protocol != MoodletxtOutboundController::$CONNECTION_TYPE_SSL) { $connectionPort = 80; $pathPrefix = "http://"; $connectionPrefix = ''; } // If proxy details are set, override defaults if ($proxyHost != '' && $proxyPort != '') { $connectionHost = $proxyHost; $connectionPort = $proxyPort; $fullPath = "http://" . $txttoolsHost . $filePath; $connectionPrefix = ''; } $responseArray = array(); foreach ($requestArray as $request) { // Build URL-encoded string from XML request $poststring = "XMLPost=" . urlencode($request); // Build connection string $request = "POST " . $fullPath . " HTTP/1.0\r\n"; $request .= "Host: " . $txttoolsHost . "\r\n"; $request .= "User-Agent: " . $this->USER_AGENT . "\r\n"; if ($proxyHost != '' && $proxyPort != '') { $request .= "Proxy-Authorization: Basic " . base64_encode($proxyUsername . ":" . $proxyPassword) . "\r\n"; } $request .= "Content-type: application/x-www-form-urlencoded\r\n"; $request .= "Content-length: " . strlen($poststring) . "\r\n"; $request .= "Connection: close\r\n\r\n"; $request .= $poststring . "\r\n"; // error_log(get_string('logxmlblocksent', 'block_moodletxt') . "\r\n\r\n" . $request); // Open socket $fp = @fsockopen($connectionPrefix . $connectionHost, $connectionPort, $errorNo, $errorStr, $timeout = 30); if (!$fp) { throw new SocketException(); } else { // Send request to server fputs($fp, $request); // Get server response $response = ''; while (!feof($fp)) { $response .= @fgets($fp, 128); // Bug in PHP SSL handling causes problems here - suppress } fclose($fp); // Check that XML has been returned $XMLproc = '<?xml'; $checkForXML = strpos($response, $XMLproc); if ($checkForXML === false) { // If no XML is received, check for HTTP error codes // Uses only the txttools server, so safe to assume Apache,HTTP 1.1, Linux $responseCode = substr($response, 9, 3); throw new HTTPException('HTTP error encountered when sending.', $responseCode); } else { $response = substr($response, $checkForXML); } } // error_log(get_string('logxmlresponse', 'block_moodletxt') . "\r\n\r\n" . $response); // Push response from website onto response array array_push($responseArray, $response); } // Give responses back to parsers return $responseArray; }