1 | <?php |
||
36 | trait CommonCode |
||
37 | { |
||
38 | |||
39 | use CommonViews; |
||
40 | |||
41 | /** |
||
42 | * Reads the content of a remote file through CURL extension |
||
43 | * |
||
44 | * @param string $fullURL |
||
45 | * @param array $features |
||
46 | * @return blob |
||
47 | */ |
||
48 | protected function getContentFromUrlThroughCurl($fullURL, $features = null) |
||
49 | { |
||
50 | if (!function_exists('curl_init')) { |
||
51 | $aReturn = ['info' => $this->lclMsgCmn('i18n_Error_ExtensionNotLoaded'), 'response' => '']; |
||
52 | return $this->setArrayToJson($aReturn); |
||
53 | } |
||
54 | if (!filter_var($fullURL, FILTER_VALIDATE_URL)) { |
||
55 | $aReturn = ['info' => $this->lclMsgCmn('i18n_Error_GivenUrlIsNotValid'), 'response' => '']; |
||
56 | return $this->setArrayToJson($aReturn); |
||
57 | } |
||
58 | $aReturn = $this->getContentFromUrlThroughCurlRawArray($fullURL, $features); |
||
59 | return '{ ' . $this->packIntoJson($aReturn, 'info') . ', ' . $this->packIntoJson($aReturn, 'response') . ' }'; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Reads the content of a remote file through CURL extension |
||
64 | * |
||
65 | * @param string $fullURL |
||
66 | * @param array $features |
||
67 | * @return blob |
||
68 | */ |
||
69 | protected function getContentFromUrlThroughCurlAsArrayIfJson($fullURL, $features = null) |
||
70 | { |
||
71 | $result = $this->setJsonToArray($this->getContentFromUrlThroughCurl($fullURL, $features)); |
||
72 | if (is_array($result['info'])) { |
||
73 | ksort($result['info']); |
||
74 | } |
||
75 | if (is_array($result['response'])) { |
||
76 | ksort($result['response']); |
||
77 | } |
||
78 | return $result; |
||
79 | } |
||
80 | |||
81 | protected function getContentFromUrlThroughCurlRaw($fullURL, $features = null) |
||
82 | { |
||
83 | $chanel = curl_init(); |
||
84 | curl_setopt($chanel, CURLOPT_USERAGENT, $this->getUserAgentByCommonLib()); |
||
85 | if ((strpos($fullURL, 'https') !== false)) { |
||
86 | $chk = false; |
||
87 | if (isset($features['forceSSLverification'])) { |
||
88 | $chk = true; |
||
89 | } |
||
90 | curl_setopt($chanel, CURLOPT_SSL_VERIFYHOST, $chk); |
||
91 | curl_setopt($chanel, CURLOPT_SSL_VERIFYPEER, $chk); |
||
92 | } |
||
93 | curl_setopt($chanel, CURLOPT_URL, $fullURL); |
||
94 | curl_setopt($chanel, CURLOPT_HEADER, false); |
||
95 | curl_setopt($chanel, CURLOPT_RETURNTRANSFER, true); |
||
96 | curl_setopt($chanel, CURLOPT_FRESH_CONNECT, true); //avoid a cached response |
||
97 | curl_setopt($chanel, CURLOPT_FAILONERROR, true); |
||
98 | $aReturn = [curl_exec($chanel), curl_getinfo($chanel), curl_errno($chanel), curl_error($chanel)]; |
||
99 | curl_close($chanel); |
||
100 | return ['response' => $aReturn[0], 'info' => $aReturn[1], 'errNo' => $aReturn[2], 'errMsg' => $aReturn[3]]; |
||
101 | } |
||
102 | |||
103 | protected function getContentFromUrlThroughCurlRawArray($fullURL, $features = null) |
||
104 | { |
||
105 | $curlFeedback = $this->getContentFromUrlThroughCurlRaw($fullURL, $features); |
||
106 | if ($curlFeedback['errNo'] !== 0) { |
||
107 | $info = $this->setArrayToJson(['#' => $curlFeedback['errNo'], 'description' => $curlFeedback['errMsg']]); |
||
108 | return ['info' => $info, 'response' => '']; |
||
109 | } |
||
110 | return ['info' => $this->setArrayToJson($curlFeedback['info']), 'response' => $curlFeedback['response']]; |
||
111 | } |
||
112 | |||
113 | protected function getFeedbackMySQLAffectedRecords() |
||
121 | |||
122 | /** |
||
123 | * returns the details about Communicator (current) file |
||
124 | * |
||
125 | * @param string $fileGiven |
||
126 | * @return array |
||
127 | */ |
||
128 | protected function getFileDetails($fileGiven) |
||
135 | |||
136 | /** |
||
137 | * returns a multi-dimensional array with list of file details within a given path |
||
138 | * (by using Symfony/Finder package) |
||
139 | * |
||
140 | * @param string $pathAnalised |
||
141 | * @return array |
||
142 | */ |
||
143 | protected function getListOfFiles($pathAnalised) |
||
144 | { |
||
145 | if (realpath($pathAnalised) === false) { |
||
146 | return ['error' => sprintf($this->lclMsgCmn('i18n_Error_GivenPathIsNotValid'), $pathAnalised)]; |
||
147 | } elseif (!is_dir($pathAnalised)) { |
||
148 | return ['error' => $this->lclMsgCmn('i18n_Error_GivenPathIsNotFolder')]; |
||
149 | } |
||
150 | $finder = new \Symfony\Component\Finder\Finder(); |
||
151 | $iterator = $finder->files()->sortByName()->in($pathAnalised); |
||
152 | $aFiles = null; |
||
153 | foreach ($iterator as $file) { |
||
154 | $aFiles[$file->getRealPath()] = $this->getFileDetails($file); |
||
155 | } |
||
156 | return $aFiles; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Returns server Timestamp into various formats |
||
161 | * |
||
162 | * @param string $returnType |
||
163 | * @return string |
||
164 | */ |
||
165 | protected function getTimestamp($returnType = 'string') |
||
172 | |||
173 | /** |
||
174 | * Tests if given string has a valid Json format |
||
175 | * |
||
176 | * @param string $inputJson |
||
177 | * @return boolean|string |
||
178 | */ |
||
179 | protected function isJsonByDanielGP($inputJson) |
||
187 | |||
188 | private function packIntoJson($aReturn, $keyToWorkWith) |
||
195 | |||
196 | /** |
||
197 | * Send an array of parameters like a form through a POST action |
||
198 | * |
||
199 | * @param string $urlToSendTo |
||
200 | * @param array $params |
||
201 | * @throws \Exception |
||
202 | */ |
||
203 | protected function sendBackgroundEncodedFormElementsByPost($urlToSendTo, $params = []) |
||
216 | |||
217 | private function sendBackgroundPostData($postingUrl, $params, $cntFailErrMsg) |
||
232 | |||
233 | private function sendBackgroundPrepareData($pUrlParts, $postingString) |
||
246 | |||
247 | /** |
||
248 | * Converts a JSON string into an Array |
||
249 | * |
||
250 | * @param string $inputJson |
||
251 | * @return array |
||
252 | */ |
||
253 | protected function setJsonToArray($inputJson) |
||
265 | } |
||
266 |