diff --git a/application/module/Helpers/Curl.php b/application/module/Helpers/Curl.php new file mode 100644 index 00000000..bc819575 --- /dev/null +++ b/application/module/Helpers/Curl.php @@ -0,0 +1,145 @@ +options = array_merge(array( + 'debug' => false, + 'http_port' => '80', + 'user_agent' => 'Westdc DataService', + 'timeout' => 20, + 'curlopts' => null, + 'verifyssl' => true, + ), $options); + } + + /** + * Send a request to the server, receive a response + * + * @param string $apiPath Request API path + * @param array $parameters Parameters + * @param string $httpMethod HTTP method to use + * + * @return string HTTP response + */ + public function request($url, array $parameters = array(), $httpMethod = 'GET', array $options = array()) + { + $options = array_merge($this->options, $options); + + $curlOptions = array(); + $headers = array(); + + if ('POST' === $httpMethod) { + $curlOptions += array( + CURLOPT_POST => true, + ); + } + elseif ('PUT' === $httpMethod) { + $curlOptions += array( + CURLOPT_POST => true, // This is so cURL doesn't strip CURLOPT_POSTFIELDS + CURLOPT_CUSTOMREQUEST => 'PUT', + ); + } + elseif ('DELETE' === $httpMethod) { + $curlOptions += array( + CURLOPT_CUSTOMREQUEST => 'DELETE', + ); + } + + if (!empty($parameters)) + { + if('GET' === $httpMethod) + { + $queryString = utf8_encode($this->buildQuery($parameters)); + $url .= '?' . $queryString; + } elseif ('POST' === $httpMethod) { + $curlOptions += array( + CURLOPT_POSTFIELDS => is_array($parameters) ? http_build_query($parameters) : $parameters, + ); + } else { + $curlOptions += array( + CURLOPT_POSTFIELDS => json_encode($parameters) + ); + $headers[] = 'Content-Type: application/json'; + } + } else { + $headers[] = 'Content-Length: 0'; + } + + $this->debug('send '.$httpMethod.' request: '.$url); + + $curlOptions += array( + CURLOPT_URL => $url, + CURLOPT_PORT => $options['http_port'], + CURLOPT_USERAGENT => $options['user_agent'], + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => $options['timeout'], + CURLOPT_HTTPHEADER => $headers, + CURLOPT_SSL_VERIFYPEER => $options['verifyssl'], + ); + + if (ini_get('open_basedir') == '' && ini_get('safe_mode') != 'On') { + $curlOptions[CURLOPT_FOLLOWLOCATION] = true; + } + + if (is_array($options['curlopts'])) { + $curlOptions += $options['curlopts']; + } + + if (isset($options['proxy'])) { + $curlOptions[CURLOPT_PROXY] = $options['proxy']; + } + + $response = $this->doCurlCall($curlOptions); + + return $response; + } + + /** + * Get a JSON response and transform it to a PHP array + * + * @return array the response + */ + protected function decodeResponse($response) + { + // "false" means a failed curl request + if (false === $response['response']) { + $this->debug(print_r($response, true)); + return false; + } + return parent::decodeResponse($response); + } + + protected function doCurlCall(array $curlOptions) + { + $curl = curl_init(); + + curl_setopt_array($curl, $curlOptions); + + $response = curl_exec($curl); + $headers = curl_getinfo($curl); + $errorNumber = curl_errno($curl); + $errorMessage = curl_error($curl); + + curl_close($curl); + + return compact('response', 'headers', 'errorNumber', 'errorMessage'); + } + + protected function buildQuery($parameters) + { + return http_build_query($parameters, '', '&'); + } + + protected function debug($message) + { + if($this->options['debug']) + { + print $message."\n"; + } + } +} diff --git a/application/module/Helpers/View.php b/application/module/Helpers/View.php index 5c46c3ac..4643f6c9 100644 --- a/application/module/Helpers/View.php +++ b/application/module/Helpers/View.php @@ -1,5 +1,5 @@ "; + echo "
"."\r\n";
 		var_dump($data);
+		echo "\r\n";
 		echo "
"; if($exit) { diff --git a/application/module/Helpers/dbh.php b/application/module/Helpers/dbh.php new file mode 100644 index 00000000..cbadc51e --- /dev/null +++ b/application/module/Helpers/dbh.php @@ -0,0 +1,139 @@ +db = $db; + } + + function insert($table,$data,$return=false) + { + $fields = array(); + $datas = array(); + + foreach($data as $k=>$v) + { + $fields[] = '"'.$k.'"'; + if(is_int($v) || is_float($v) || is_bool($v)) + { + $datas[] = $v; + }else{ + if(preg_match("/\'/",$v)) + { + $v = preg_replace("/\'/","''",$v); + } + $datas[] = "'".$v."'"; + } + } + + $fields = join(",",$fields); + $datas = join(",",$datas); + + if($return == false){ + $sql = "INSERT INTO \"".$table."\" ($fields) VALUES ($datas)"; + try{ + return $this->db->exec($sql); + }catch (Exception $e) { + if($this->product) + { + return false; + }else{ + echo 'Caught exception: '. $e->getMessage(). "\n"; + } + } + }else{ + $sql = "INSERT INTO \"".$table."\" ($fields) VALUES ($datas) RETURNING id"; + try{ + $sth = $this->db->prepare($sql); + if($sth->execute()) + { + $temp = $sth->fetch(PDO::FETCH_ASSOC); + return $temp['id']; + }else{ + return false; + } + }catch (Exception $e) { + if($this->product) + { + return false; + }else{ + echo 'Caught exception: '. $e->getMessage(). "\n"; + } + } + } + }//insert + + function update($table,$data,$condition="",$return=false) + { + $ups = array(); + + foreach($data as $k=>$v) + { + if(is_int($v) || is_float($v) || is_bool($v)) + { + $ups[] = '"'.$k.'"='.$v; + }else{ + if(preg_match("/\'/",$v)) + { + $v = preg_replace("/\'/","''",$v); + } + if(preg_match("/\"/",$v)) + { + $v = preg_replace("/\"/","''",$v); + } + $ups[] = '"'.$k.'"=\''.$v."'"; + } + } + + $fields = join(",",$ups); + + if(!empty($condition)) + { + $wheresql = " WHERE ".$condition; + }else{ + $wheresql = ""; + } + + if($return == false){ + + try{ + $sql = "UPDATE \"".$table."\" SET $fields $wheresql"; + if($this->db->exec($sql)) + { + return true; + }else{ + return false; + } + }catch (Exception $e) { + if($this->product) + { + return false; + }else{ + echo 'Caught exception: '. $e->getMessage(). "\n"; + } + } + }else{ + try{ + $sql = "UPDATE \"".$table."\" SET $fields $wheresql"; + return $this->db->exec($sql); + }catch (Exception $e) { + if($this->product) + { + return false; + }else{ + echo "
";
+					echo $sql."\r\n";
+					echo 'Caught exception: '.  $e->getMessage(). "\r\n";
+					echo "
"; + } + } + } + + }//update + +} \ No newline at end of file