public function sendSlackNotify($message, $type) { $color = false; switch ($type) { case 'error': $pretext = "Error"; $color = 'danger'; break; case 'warn': $pretext = "Warning"; $color = 'warning'; break; default: $pretext = "Info"; $color = '#28D7E5'; break; } $data = array("attachments" => array(array("pretext" => SSUtilities::currentPageURL(), "text" => $message, "title" => $pretext, "color" => $color))); if (strlen(json_encode($data)) > $this->socket_limit) { $response = $this->request_curl->sendSlackNotify($data); } else { $response = $this->request_socket->sendSlackNotify($data); } }
public function sendRequest($data, $url = null, $id_handle = false) { if (!$this->_state_socket) { $this->createSocket(); } if ($id_handle) { $this->id_handle = $id_handle; } if ($this->_state_socket === true) { if ($url === null) { $url = $this->api_path . '/' . $data['index'] . '/' . $data['eType']; } else { $url = $this->api_path . $url; } $content = json_encode($data); $req = ""; $req .= "POST /{$url} HTTP/1.1\r\n"; $req .= "Host: " . $this->host . "\r\n"; $req .= "Content-Type: application/json\r\n"; $req .= "Accept: application/json\r\n"; $req .= "Content-length: " . strlen($content) . "\r\n"; $req .= "\r\n"; $req .= $content; if ($sended_lenth = @fwrite($this->_socket, $req)) { $this->setDebugInfo(false, $sended_lenth); } else { $sended = false; $error_num = $this->_socket_error['error_num']; $error_message = $this->_socket_error['error_message']; for ($i = 0; $i <= $this->max_retry; $i++) { usleep(200000); if ($sended_lenth = @fwrite($this->_socket, $req)) { $sended = true; $this->setDebugInfo(false, $sended_lenth); break; } else { $error_num = $this->_socket_error['error_num']; $error_message = $this->_socket_error['error_message']; SSUtilities::error_log("Error fwrire socket. Tried {$i} times...", 'error_socket_connection'); $this->setDebugInfo(true, "#{$error_num}: {$error_message}"); } } if ($sended === false) { $this->closeSocket(); usleep(200000); $this->createSocket(); if ($sended_lenth = @fwrite($this->_socket, $req)) { $this->setDebugInfo(false, $sended_lenth); } else { SSUtilities::error_log("Error fwrire socket after sleep.", 'error_socket_connection'); $cURL = new SSHttpRequestCurl(); $cURL->sendRequest($data); $this->setDebugInfo(true, "#{$error_num}: {$error_message}. Data will sends through cURL"); } } } } else { if (!empty($this->_socket_error)) { $error_num = $this->_socket_error['error_num']; $error_message = $this->_socket_error['error_message']; SSUtilities::error_log("#{$error_num}: {$error_message}", 'error_socket_connection'); $this->setDebugInfo(true, "#{$error_num}: {$error_message}"); } } }
/** * Compose a ready to use in MEAN network intex a styled message. * Consists of two part: colored Font Awesome icon and text description * @param array $event array( * 'action', // 'created', 'deleted', 'updated', 'added', 'activated', 'deactivated', 'accessed', 'file_updated', 'logged_in', 'logged_out', 'wrong_password', 'installed', etc... * 'object_type', // 'Attachment', 'Menu', 'Options', 'Plugin', 'Post', 'Taxonomy', 'Theme', 'User', 'Widget', etc... * 'object_subtype', // Page, image. May not be applicable * 'object_id', // id of the object * 'object_name' // name of the object * ) * @return array array( * 'design' => array( * 'icon' => 'fa-file-text', // Font Awesome icon name * 'color' => '#8FD5FF' // Color * ), * 'message' => 'A new user "test (email@domain.com)" has been registered' * ) */ static function composeEventData($event, $default_message = '') { if (!is_array($event)) { SSUtilities::error_log('Expected to get an array', 'error'); return; } if (empty($event)) { SSUtilities::error_log('Empty an array', 'warn'); return; } $design = array(); $message = ''; $color_created = '#238a36'; $color_updated = '#8FD5FF'; $color_deleted = '#9f253f'; switch ($event['object_type']) { case 'content': $design['icon'] = 'fa-file-text'; switch ($event['action']) { case 'created': $design['color'] = $color_created; break; case 'updated': $design['color'] = $color_updated; break; case 'deleted': $design['color'] = $color_deleted; break; } $message = SSUtilities::t('{type} "{name}" has been {action}.', array('{type}' => ucfirst($event['object_subtype']), '{name}' => $event['object_name'], '{action}' => $event['action'])); break; case 'user': $design['icon'] = 'fa-user'; $design['color'] = '#8664aa'; break; case 'system': $design['icon'] = 'fa-cubes'; $design['color'] = '#c79696'; break; case 'actions': $design['icon'] = 'fa-certificate'; $design['color'] = '#fd8e00'; break; case 'cron': $design['icon'] = 'fa-cogs'; $design['color'] = '#de1b16'; $message = SSUtilities::t('Cron run completed.'); break; default: $design['color'] = '#19617a'; $design['icon'] = 'fa-bars'; break; } $message = $message ? $message : $default_message; $data = array(); $data['key'] = $event['object_type']; $data['name'] = $message; $data['data']['description'] = $message; $data['design'] = $design; return $data; }