function get_app_db_headers($controller_dblink, $app_data, $environment) { # Fetching a valid token $query = "\tSELECT\n\t\t\t\t\t`api_key`\n\t\t\t\tFROM\n\t\t\t\t\t" . NQ_APP_TOKENS_TABLE . "\n\t\t\t\tWHERE\n\t\t\t\t\t`app_id`\t\t= " . (int) $app_data['id'] . " AND\n\t\t\t\t\t`environment`\t= '" . mysqli_escape_string($controller_dblink, $environment) . "' AND\n\t\t\t\t\t`db_fetch` \t= b'1'\n\t\t\t\tLIMIT 1"; $token_data = mysqli_single_result_query($controller_dblink, $query); # Adding our headers $headers = ['Referring-Host: ' . explode(',', $app_data['domain'])[0], 'Content-Type: ' . NQ_DEFAULT_CONTENT_TYPE]; # Post object $post = (object) ['app_secret' => hash('sha256', $app_data['secret']), 'token' => $token_data['api_key'], 'user_agent' => 'nuQuery/1.0 (Emailbot)']; # Performing our curl $s = curl_init(); curl_setopt($s, CURLOPT_URL, NQ_AUTH_HOST . $app_data['id'] . '/create'); curl_setopt($s, CURLOPT_HTTPHEADER, $headers); curl_setopt($s, CURLOPT_RETURNTRANSFER, true); curl_setopt($s, CURLOPT_USERAGENT, 'nuQuery/1.0 (Emailbot)'); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, PostParser::encode($post)); # Our return data type $token_data = PostParser::decode(curl_exec($s), NQ_DEFAULT_CONTENT_TYPE); curl_close($s); # Updating our headers $headers[] = 'Access-Token: ' . $token_data->id; # Sending our headers back return $headers; }
public static function encode($data, $content_type = false, $parentname = false, $open = true, $close = true, $top = true) { # Defaulting $default_type = isset($_SERVER['HTTP_CONTENT_TYPE']) ? $_SERVER['HTTP_CONTENT_TYPE'] : NQ_DEFAULT_CONTENT_TYPE; $content_type = $content_type === false ? $default_type : $content_type; # Sorting the object by its keys if ($top) { $data = (array) $data; ksort($data); $data = (object) $data; } # Adding debug data global $G_DEBUG_DATA; if ($top && NQ_DEBUG_ENABLED && NQ_DEBUG_SEND_RESPONSE) { # Debug elapsed time $G_DEBUG_DATA->elapsed_time = ['total' => microtime(true) - NQ_TRACKING_START_TIME]; $G_DEBUG_DATA->mysql_query_count = count($G_DEBUG_DATA->mysql_queries); $G_DEBUG_DATA->elapsed_time['mysql'] = 0; foreach ($G_DEBUG_DATA->mysql_queries as $query_data) { $G_DEBUG_DATA->elapsed_time['mysql'] += $query_data['time']; } $G_DEBUG_DATA->elapsed_time['php'] = $G_DEBUG_DATA->elapsed_time['total'] - $G_DEBUG_DATA->elapsed_time['mysql']; $G_DEBUG_DATA = (array) $G_DEBUG_DATA; ksort($G_DEBUG_DATA); # Add or replace if (NQ_DEBUG_REPLACE_CONTENT) { $data = $G_DEBUG_DATA; } else { $data->debug = $G_DEBUG_DATA; } } # Choosing our type $obj = ''; switch ($content_type) { # JSON case 'json': case 'application/json': # Setting our response header $top && header('Content-Type: application/json; charset=utf-8', true); # Handling arrays and objects $obj = new stdClass(); if (is_array($data) || is_object($data)) { foreach ($data as $key => $value) { # If we are an attribute or value $attr_or_value = is_object($value) && count(get_object_vars($value)) == 1; # We are a value if ($attr_or_value && isset($value->{PostParser::value_flag})) { $obj->{$key} = $value->{PostParser::value_flag}; } elseif ($attr_or_value && isset($value->{PostParser::attribute_flag})) { $obj->{$key} = $value->{PostParser::attribute_flag}; } elseif (is_object($value)) { $obj->{$key} = PostParser::encode($value, 'json', false, false, false, false); } else { $obj->{$key} = $value; } } } else { $obj = $data; } # If we are at the top, we want to encode if ($top) { $obj = json_encode($obj, NQ_JSON_PRINT_FORMAT); } break; # XML # XML case 'xml': case 'application/xml': # Setting our response header $top && header('Content-Type: application/xml; charset=utf-8', true); # We need to create our response object if ($top) { $root = $parentname === false ? 'nq-response' : $parentname; $data = [(object) [$root => (object) $data]]; } # Holders $attributes = []; $children = []; # If we are a value if (!is_array($data) && !is_object($data)) { return '<' . $parentname . '>' . (is_string($data) ? str_replace(['&', '>', '<', '"'], ['&', '>', '<', '"'], $data) : var_export($data, true)) . '</' . $parentname . '>'; } # Checking to see if we have a value, if we do all other nodes are attributes $has_value = false; foreach ($data as $child => $child_value) { if (is_object($child_value) && count(get_object_vars($child_value)) == 1 && isset($child_value->{PostParser::value_flag})) { $has_value = true; break; } } # Looping through our propreties foreach ($data as $child => $child_value) { # If we should be closing the tag $prop_count = is_array($child_value) ? count($child_value) : 1; $prop_count = is_object($child_value) ? count(get_object_vars($child_value)) : $prop_count; $start_count = $prop_count; # If we are an attribute or a value $attr_or_value = is_object($child_value) && count(get_object_vars($child_value)) == 1; # Attributes if ($attr_or_value && isset($child_value->{PostParser::attribute_flag})) { $attributes[] = $child . '="' . $child_value->{PostParser::attribute_flag} . '"'; $prop_count--; } elseif ($attr_or_value && isset($child_value->{PostParser::value_flag})) { $children[] = $child_value->{PostParser::value_flag}; $prop_count--; } elseif ($has_value && !is_object($child_value) && !is_array($child_value)) { $attributes[] = $child . '="' . $child_value . '"'; $prop_count--; } elseif (is_array($child_value) && $child_value === array_values($child_value)) { # Error checking $has_value && trigger_error('Error compiling XML - Attempting to add child node to a text value. <' . $child . '> into <' . $parentname . '>'); # Printing out our children properly foreach ($child_value as $ckey => $cval) { if (!is_array($cval) && !is_object($cval)) { $children[] = '<' . $child . '>' . (is_string($cval) ? $cval : var_export($cval, true)) . '</' . $child . '>'; } else { $children[] = PostParser::encode($cval, 'xml', $child, true, true, false); } $prop_count--; } } else { # Error checking $has_value && trigger_error('Error compiling XML - Attempting to add child node to a text value. <' . $child . '> into <' . $parentname . '>'); # Adding our children $children[] = PostParser::encode($child_value, 'xml', $child, $start_count == $prop_count, --$prop_count == 0, false); } } # If we have reached here, we want to close $close = true; # Saving our xml properly $tagname = $parentname === false ? $child : $parentname; if (!is_numeric($tagname)) { # We are opening up the tag if ($open) { $obj .= '<' . $tagname . (count($attributes) > 0 ? ' ' . implode(' ', $attributes) : ''); } # We dont have any children if (count($children) == 0) { $obj .= $open ? ' />' : ''; } else { $obj .= $open ? '>' : ''; $obj .= implode('', $children); $obj .= $close ? '</' . $tagname . '>' : ''; } } else { # Adding our properties as sub-children if (count($attributes) > 0) { foreach ($attributes as $prop) { $tag = explode('=', $prop); $obj .= '<' . $tag[0] . '>' . trim($tag[1], '"') . '</' . $tag[0] . '>'; } } # Adding our children $obj .= implode('', $children); } # We are prefixing the object if ($top) { $obj = '<?xml version="1.0" encoding="UTF-8"?>' . $obj; } break; # POST Body # POST Body case 'form': case 'application/x-www-form-urlencoded': # Setting our response header $top && header('Content-Type: application/x-www-form-urlencoded; charset=utf-8', true); # Doing a little manipulation to take care of attributes and values $obj = http_build_query(PostParser::encode($data, 'json', false, false, false, false)); break; } # Returning our object return $obj; }