Beispiel #1
0
	private static function SendCommand($channelId, $message, $options = array())
	{
		if (!is_array($channelId))
			$channelId = Array($channelId);

		$channelId = implode('/', array_unique($channelId));

		if (strlen($channelId) <=0 || strlen($message) <= 0)
			return false;

		$defaultOptions = array(
			"method" => "POST",
			"timeout" => 5,
			"dont_wait_answer" => true
		);

		$options = array_merge($defaultOptions, $options);

		if (!in_array($options["method"], Array('POST', 'GET')))
			return false;

		$nginx_error = COption::GetOptionString("pull", "nginx_error", "N");
		if ($nginx_error != "N")
		{
			$nginx_error = unserialize($nginx_error);
			if (intval($nginx_error['date'])+120 < time())
			{
				COption::SetOptionString("pull", "nginx_error", "N");
				CAdminNotify::DeleteByTag("PULL_ERROR_SEND");
				$nginx_error = "N";
			}
			else if ($nginx_error['count'] >= 10)
			{
				$ar = Array(
					"MESSAGE" => GetMessage('PULL_ERROR_SEND'),
					"TAG" => "PULL_ERROR_SEND",
					"MODULE_ID" => "pull",
				);
				CAdminNotify::Add($ar);
				return false;
			}
		}

		$postdata = CHTTP::PrepareData($message);

		$CHTTP = new CHTTP();
		$CHTTP->http_timeout = intval($options["timeout"]);
		if (isset($options["expiry"]))
		{
			$CHTTP->SetAdditionalHeaders(array("Message-Expiry" => intval($options["expiry"])));
		}

		$arUrl = $CHTTP->ParseURL(CPullOptions::GetPublishUrl($channelId), false);
		if ($CHTTP->Query($options["method"], $arUrl['host'], $arUrl['port'], $arUrl['path_query'], $postdata, $arUrl['proto'], 'N', $options["dont_wait_answer"]))
		{
			$result = $options["dont_wait_answer"] ? '{}': $CHTTP->result;
		}
		else
		{
			if ($nginx_error == "N")
			{
				$nginx_error = Array(
					'count' => 1,
					'date' => time(),
					'date_increment' => time(),
				);
			}
			else if (intval($nginx_error['date_increment'])+1 < time())
			{
				$nginx_error['count'] = intval($nginx_error['count'])+1;
				$nginx_error['date_increment'] = time();
			}
			COption::SetOptionString("pull", "nginx_error", serialize($nginx_error));
			$result = false;
		}

		return $result;
	}
Beispiel #2
0
 /** Static Post with the ability to add headers and set the http timeout
  * @static
  * @param $url
  * @param $arPostData
  * @param array $arHeader
  * @param int $http_timeout
  * @return bool|string
  */
 public static function sPostHeader($url, $arPostData, $arHeader = array(), $http_timeout = 0)
 {
     $http_timeout = intval($http_timeout);
     $ob = new CHTTP();
     if (!empty($arHeader)) {
         $ob->SetAdditionalHeaders($arHeader);
     }
     if ($http_timeout > 0) {
         $ob->http_timeout = $http_timeout;
     }
     return $ob->Post($url, $arPostData);
 }
Beispiel #3
0
 public function createBlankFile(array $fileData)
 {
     $accessToken = $this->getAccessToken();
     $googleMimeType = $this->getInternalMimeTypeListByExtension(getFileExtension($fileData['name']));
     $fileName = getFileNameWithoutExtension($fileData['name']);
     CWebDavTools::convertToUtf8($fileName);
     if (!$googleMimeType) {
         return false;
     }
     $http = new CHTTP();
     $http->http_timeout = 10;
     $arUrl = $http->ParseURL('https://www.googleapis.com/drive/v2/files');
     $http->SetAdditionalHeaders(array("Authorization" => "Bearer {$accessToken}"));
     $postFields = "{\"title\":\"{$fileName}\",\"mimeType\":\"{$googleMimeType}\"}";
     $postContentType = 'application/json; charset=UTF-8';
     if (!$http->Query('POST', $arUrl['host'], $arUrl['port'], $arUrl['path_query'], $postFields, $arUrl['proto'], $postContentType)) {
         return false;
     }
     $this->checkHttpResponse($http);
     // access token expired, let's get a new one and try again
     if ($http->status == "401") {
         //todo: invalid credential response
         return false;
     }
     // error checking
     if ($http->status != "200") {
         return false;
     }
     $finalOutput = json_decode($http->result);
     //last signed user must delete file from google drive
     $this->insertPermission(array('link' => $finalOutput->alternateLink, 'id' => $finalOutput->id));
     return array('link' => $finalOutput->alternateLink, 'id' => $finalOutput->id);
 }