· 5 years ago · Aug 11, 2020, 04:04 AM
1<?php
2/**
3 * To return Return value
4 *
5 * @param string $val
6 * @return false|string
7 */
8function returnVal($val)
9{
10 header('Content-Type: application/json');
11 return json_encode($val);
12}
13
14/**
15 * To check database connection status
16 *
17 * @param mysqli $conn
18 */
19function checkDbConn($conn)
20{
21 if ($conn->connect_errno) {
22 http_response_code(500);
23
24 echo returnVal([
25 'status' => 'FAILED',
26 'code' => 500,
27 'error' => 'Internal server error',
28 ]);
29
30 exit();
31 }
32}
33
34/**
35 * To log api access
36 */
37function apiAccessLog()
38{
39 $logData = [];
40 $logData[] = date('Y/m/d H:i:s');
41 $logData[] = $_SERVER['REMOTE_ADDR'];
42 $logData[] = $_SERVER['REQUEST_URI'];
43 $msg = implode(' ', $logData);
44
45 $log_filename = "log";
46 if (!file_exists($log_filename)) {
47 mkdir($log_filename, 0777, true);
48 }
49
50 $log_file_data = $log_filename . '/access_log_' . date('Ymd') . '.log';
51 $xx = file_put_contents($log_file_data, $msg . "\n", FILE_APPEND);
52}
53
54/**
55 * To log api error
56 *
57 * @param array $errors
58 */
59function apiErrorLog($errors)
60{
61 $logData = [];
62 $logData[] = date('Y/m/d H:i:s');
63 $logData[] = $_SERVER['REMOTE_ADDR'];
64 $logData[] = $_SERVER['REQUEST_URI'];
65 $logData[] = implode(' ', $errors);
66 $msg = implode(' ', $logData);
67
68 $log_filename = "log";
69 if (!file_exists($log_filename)) {
70 mkdir($log_filename, 0777, true);
71 }
72
73 $log_file_data = $log_filename . '/error_log_' . date('Ymd') . '.log';
74 file_put_contents($log_file_data, $msg . "\n", FILE_APPEND);
75}
76
77/**
78 * To check whether method is allowed
79 *
80 * @param array $methods
81 * @return bool
82 */
83function checkAllowMethod($methods)
84{
85 if (in_array($_SERVER['REQUEST_METHOD'], $methods)) {
86 return true;
87 } else {
88 http_response_code(405);
89 apiErrorLog([405, 'Method Not Allowed']);
90
91 echo returnVal([
92 'status' => 'FAILED',
93 'code' => 405,
94 'error' => 'Method Not Allowed',
95 ]);
96
97 exit();
98 }
99}
100
101//polyfill for getallheaders()
102//https://github.com/ralouphie/getallheaders
103if (!function_exists('getallheaders')) {
104 /**
105 * Get all HTTP header key/values as an associative array for the current request.
106 *
107 * @return string[string] The HTTP header key/value pairs.
108 */
109 function getallheaders()
110 {
111 $headers = array();
112
113 $copy_server = array(
114 'CONTENT_TYPE' => 'Content-Type',
115 'CONTENT_LENGTH' => 'Content-Length',
116 'CONTENT_MD5' => 'Content-Md5',
117 );
118
119 foreach ($_SERVER as $key => $value) {
120 if (substr($key, 0, 5) === 'HTTP_') {
121 $key = substr($key, 5);
122 if (!isset($copy_server[$key]) || !isset($_SERVER[$key])) {
123 $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $key))));
124 $headers[$key] = $value;
125 }
126 } elseif (isset($copy_server[$key])) {
127 $headers[$copy_server[$key]] = $value;
128 }
129 }
130
131 if (!isset($headers['Authorization'])) {
132 if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
133 $headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
134 } elseif (isset($_SERVER['PHP_AUTH_USER'])) {
135 $basic_pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
136 $headers['Authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass);
137 } elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) {
138 $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST'];
139 }
140 }
141
142 return $headers;
143 }
144}