Esempio n. 1
0
	public function __call($method, $arg) {
		if (empty($this->url)) {
			error_log('libws: No url is configured.');
			return;
		}

		if (!is_array($arg)) {
			$arg = array($arg);
		}

		if (count($arg) == 1 && isset($arg[0]) && is_array($arg[0])) {
			$arg = $arg[0];
		}

		if (strpos($this->destiny, 'facebook') !== false) {
			$add = array(
				'APPID' => '#APPID',
				'APPSECRET' => '#APPSECRET'
			);
			$arg = array_merge($add, $arg);
		}

		if (isset($arg) && is_array($arg)) {
			$arg = $this->_param_replace($arg);
		} else {
			$arg_cp = $arg;
			$_arg = isset($arg[0]) ? w($arg[0]) : w();

			$arg = w();
			foreach ($_arg as $v) {
				if (isset($_REQUEST[$v])) $arg[$v] = $_REQUEST[$v];
			}

			$arg = (!$arg) ? $arg_cp : $arg;
		}

		$_bridge = $this->bridge;
		$count_bridge = count($_bridge);
		$_url = $this->url;
		$response = null;

		switch ($this->type) {
			case 'wsdl':
				$this->client = new nusoap_client($this->url, true);

				if ($error = $this->client->getError()) {
					$response = $error;
				} else {
					$response = $this->client->call($method, $arg);
					
					// Check if there were any call errors, and if so, return error messages.
					if ($error = $this->client->getError()) {
						$response = $this->client->response;
						$response = xml2array(substr($response, strpos($response, '<?xml')));
						
						if (isset($response['soap:Envelope']['soap:Body']['soap:Fault']['faultstring'])) {
							$fault_string = $response['soap:Envelope']['soap:Body']['soap:Fault']['faultstring'];
							
							$response = explode("\n", $fault_string);
							$response = $response[0];
						} else {
							$response = $error;
						}
						
						$response = array(
							'error' => true,
							'message' => $response
						);
					}
				}

				$response = json_decode(json_encode($this->_filter($response)));
				break;
			case 'mysql':
				if (isset($arg['_mysql'])) {
					$this->params['_MYSQL'] = $arg['_mysql'];
					unset($arg['_mysql']);
				}

				$connect = (isset($this->params['_MYSQL']) && $this->params['_MYSQL']) ? $this->params['_MYSQL'] : '';

				if (empty($arg)) {
					return false;
				}

				global $db;

				require_once('class.mysql.php');
				$db = new database($connect);

				if (empty($db->message)) {
					switch ($method) {
						case 'sql_field':
						case 'sql_build':
						case 'sql_rowset':
						case 'sql_fieldrow':
						case 'sql_insert':
						case 'sql_cache':
						case 'sql_cache_limit':
							break;
						default:
							if (count($arg) > 1) {
								$sql = array_shift($arg);
								$arg = sql_filter($sql, $arg);
							}
							break;
					}

					$response = (@function_exists($method)) ? false : array('error' => true, 'message' => $method . ' is undefined');

					if ($response === false) {
						switch ($method) {
							case 'sql_field':
							case 'sql_build':
								extract($arg, EXTR_PREFIX_ALL, 'sf');

								$arg_v = '';
								foreach ($arg as $i => $row) {
									$arg_v .= (($arg_v) ? ', ' : '') . '$sf_' . $i;
								}

								eval('$response = $method(' . $arg_v . ');');
								break;
							default:
								$response = call_user_func_array($method, $arg);
								break;
						}

						if ($method !== 'sql_filter' && $method != 'sql_build') {
							$response = $this->recursive_htmlentities($response);
						}
					}
				}

				if (!empty($db->message)) {
					$response = $db->message;
				}
				break;
			case 'oracle':
				if (isset($arg['_oracle'])) {
					$this->params['_ORACLE'] = $arg['_oracle'];
					unset($arg['_oracle']);
				}

				$connect = (isset($this->params['_ORACLE']) && $this->params['_ORACLE']) ? $this->params['_ORACLE'] : '';

				if (empty($arg)) {
					return false;
				}

				global $db;

				require_once('class.oracle.php');
				$db = new database($connect);

				if (empty($db->message)) {
					switch ($method) {
						case 'sql_field':
						case 'sql_build':
							break;
						default:
							if (count($arg) > 1) {
								$sql = array_shift($arg);
								$arg = sql_filter($sql, $arg);
							}
							break;
					}

					//$response = (@function_exists($method)) ? $method($arg) : array('error' => true, 'message' => $method . ' is undefined');
					$response = (@function_exists($method)) ? false : array('error' => true, 'message' => $method . ' is undefined');

					if ($response === false) {
						switch ($method) {
							case 'sql_field':
							case 'sql_build':
								extract($arg, EXTR_PREFIX_ALL, 'sf');

								$arg_v = '';
								foreach ($arg as $i => $row) {
									$arg_v .= (($arg_v) ? ', ' : '') . '$sf_' . $i;
								}

								eval('$response = $method(' . $arg_v . ');');
								break;
							default:
								$response = $method($arg);
								break;
						}
					}
				}

				if (!isset($response['error']) && is_array($response)) {
					if (isset($response[0]) && is_array($response[0])) {
						foreach ($response as $i => $row) {
							if (is_array($row)) {
								$response[$i] = array_change_key_case($row, CASE_LOWER);
							}
						}
					} else {
						$response = array_change_key_case($response, CASE_LOWER);
					}
				}

				if (!empty($db->message)) {
					$response = $db->message;
				}
				break;
			case 'php':
				if (isset($arg['_php'])) {
					unset($arg['_php']);
				}

				$print = w();
				switch ($method) {
					case 'tail':
					case 'cat':
						if (!@is_readable($arg[0])) {
							$response = 'Can not read file: ' . $arg[0];
						}
						break;
					case 'ping':
						$arg[1] = '-c' . ((isset($arg[1])) ? $arg[1] : 3);
						break;
				}

				switch ($method) {
					case 'write':
						$response = false;

						if ($fp = @fopen($arg[0], $arg[1])) {
							if (@fwrite($fp, $arg[2]) !== false) {
								@fclose($fp);
								$response = true;
							}
						}
						break;
					case 'tail':
					case 'cat':
					case 'ping':
						if ($response === null) {
							exec($method . ' ' . implode(' ', $arg), $print);
							$response = implode("\r\n", $print);
						}
						break;
					case 'exec':
						if ($response === null) {
							$method(implode(' ', $arg), $print);
							$response = implode("\r\n", $print);
						}
						break;
					default:
						ob_start();

						if (@function_exists($method) || $method == 'eval') {
							eval(($method == 'eval') ? $arg[0] : 'echo @$method(' . (count($arg) ? "'" . implode("', '", $arg) . "'" : '') . ');');

							$_arg = error_get_last();
						} else {
							$_arg = array('message' => 'PHP Fatal error: Call to undefined function ' . $method . '()');
						}

						$response = (null === $_arg) ? ob_get_contents() : array('url' => $_url . $method, 'error' => 500, 'message' => $_arg['message']);

						ob_end_clean();
						break;
				}
				break;
			case 'facebook':
				if (isset($arg['_facebook'])) {
					unset($arg['_facebook']);
				}

				//header('Content-type: text/html; charset=utf-8');
				require_once('class.facebook.php');

				$facebook = new Facebook(array(
					'appId'  => $arg['APPID'],
					'secret' => $arg['APPSECRET'])
				);
				unset($arg['APPID'], $arg['APPSECRET']);

				try {
					$page = array_shift($arg);
					$page = (is_string($page)) ? '/' . $page : $page;
					
					$req = (isset($arg[0]) && is_string($arg[0])) ? array_shift($arg) : '';
					$req = (empty($req)) ? 'get' : $req;

					$arg = (isset($arg[0])) ? $arg[0] : $arg;

					$response = (!empty($page)) ? (count($arg) ? $facebook->$method($page, $req, $arg) : $facebook->$method($page, $req)) : $facebook->$method();
				} catch (FacebookApiException $e) {
					$response = array(
						'url' => $_url,
						'error' => 500,
						'message' => trim(str_replace('OAuthException: ', '', $e))
					);

					error_log($e);
				}

				unset($facebook);
				break;
			case 'email':
				if (isset($arg['_email'])) {
					$this->params['_EMAIL'] = $arg['_email'];
					unset($arg['_email']);
				}

				$response = false;

				if (!isset($arg['to'])) {
					$response = 'NO_TO_ADDRESS';
				}

				if ($response === false && !isset($arg['from'])) {
					$response = 'NO_FROM_ADDRESS';
				}

				if ($response === false) {
					if (!is_array($arg['to'])) {
						$arg['to'] = array($arg['to']);
					}

					preg_match_all('!("(.*?)"\s+<\s*)?(.*?)(\s*>)?!', $arg['from'], $matches);
					/*$response = array();
					for ($i=0; $i<count($matches[0]); $i++) {
						$response[] = array(
							'name' => $matches[1][$i],
							'email' => $matches[2][$i],
						);
					}*/

					$response = $matches;


					// Create Mail object
					/*$mail = new phpmailer();

					$mail->PluginDir = '';
					$mail->Mailer = 'smtp';
					$mail->Host = $this->params['_EMAIL'];
					$mail->SMTPAuth = false;
					$mail->From = $from;
					$mail->FromName = "Claro";
					$mail->Timeout = 30;*/

					foreach ($arg['to'] as $row) {
						//$mail->AddAddress($row);
					}
				}

				//require_once('class.email.php');

				//$emailer = new emailer();

				//$response = print_r($arg, true);
				break;
			default:
				$send_var = w('sso mysql oracle php facebook email');
				$send = new stdClass;

				if ($count_bridge == 1 && $_bridge[0] === $_url) {
					$count_bridge--;
					array_shift($_bridge);
				}

				foreach ($send_var as $row) {
					$val = '_' . strtoupper($row);
					$send->$row = (isset($this->params[$val]) && $this->params[$val]) ? $this->params[$val] : false;

					if (!$count_bridge && ($send->$row || isset($arg['_' . $row]))) {
						$this->type = $row;
					}
				}

				switch ($this->type) {
					case 'sso':
						$this->origin = false;

						$_url .= $method;
						unset($arg['_sso']);
						break;
					default:
						foreach ($send_var as $row) {
							if (isset($send->$row) && !empty($send->$row)) {
								$arg['_' . $row] = $send->$row;
							}
						}

						$arg['_method'] = $method;
						$arg['_unique'] = (!$this->unique) ? $this->unique : 1;
						
						if (isset($_bridge) && count($_bridge)) {
							array_shift($_bridge);
							$arg['_chain'] = implode('|', $_bridge);
						}
						break;
				}

				$_arg = $arg;
				$arg = ($this->type == 'sso') ? $this->_build($arg, false) : __encode($arg);

				$socket = @curl_init();
				@curl_setopt($socket, CURLOPT_URL, $_url);
				@curl_setopt($socket, CURLOPT_VERBOSE, 0);
				@curl_setopt($socket, CURLOPT_HEADER, 0);
				@curl_setopt($socket, CURLOPT_RETURNTRANSFER, 1);
				@curl_setopt($socket, CURLOPT_POST, 1);
				@curl_setopt($socket, CURLOPT_POSTFIELDS, $arg);
				@curl_setopt($socket, CURLOPT_SSL_VERIFYPEER, 0);
				@curl_setopt($socket, CURLOPT_SSL_VERIFYHOST, 1);

				$response = @curl_exec($socket);

				$_curl = new stdClass;
				$_curl->err = @curl_errno($socket);
				$_curl->msg = @curl_error($socket);
				$_curl->inf = (object) @curl_getinfo($socket);
				@curl_close($socket);

				switch ($_curl->err) {
					/**
					If the request has no errors.
					*/
					case 0:
						switch ($this->type) {
							/**
							SSO type
							*/
							case 'sso':
								if (preg_match('#<body>(.*?)</body>#i', $response, $part)) {
									preg_match('#<p><b>description</b>(.*?)</p>#i', $part[1], $status);
									
									$response = array(
										'url' => $_url,
										'error' => $_curl->inf->http_code,
										'message' => trim($status[1])
									);
								} else {
									switch($method) {
										case 'search':
											preg_match_all('/string\=(.*?)\n/i', $response, $response_all);
											$response = $response_all[1];
											break;
										default:
											$response = $this->_format($response);
											break;
									}
								}
								break;
							/**
							Any other type
							*/
							default:
								$_json = json_decode($response);

								if ($_json === null) {
									$response = trim($response);
									$response = (!empty($response)) ? $response : $_curl->inf;

									$_json = $response;
								}
								
								$response = $_json;
								break;
						}
						break;
					/**
					Some error was generated after the request.
					*/
					default:
						$response = array(
							'url' => $_url,
							'error' => 500,
							'message' => $_curl->msg
						);
						break;
				}

				break;
		}

		if (!$this->origin || $this->unique) {
			$response = json_encode($response);
		}

		if (($this->type == 'sso' && $this->unique) || ($this->type != 'sso' && $this->unique)) {
			$response = json_decode($response);
		}

		if (is_array($response) && isset($response[0]) && is_string($response[0]) && strpos($response[0], '<?xml') !== false) {
			$response = array_change_key_case_recursive(xml2array($response[0]));

			$response = json_decode(json_encode($response));
		}

		return $response;
	}
Esempio n. 2
0
	public function __call($method, $arg) {
		if (empty($this->url)) {
			error_log('libws: No url is configured.');
			return;
		}

		if (!is_array($arg)) {
			$arg = array($arg);
		}

		if (count($arg) == 1 && isset($arg[0]) && is_array($arg[0])) {
			$arg = $arg[0];
		}

		if (strpos($this->destiny, 'facebook') !== false) {
			$add = array(
				'APPID' => '#APPID',
				'APPSECRET' => '#APPSECRET'
			);
			$arg = array_merge($add, $arg);
		}

		if (isset($arg) && is_array($arg)) {
			$arg = $this->_param_replace($arg);
		} else {
			$arg_cp = $arg;
			$_arg = isset($arg[0]) ? w($arg[0]) : w();

			$arg = w();
			foreach ($_arg as $v) {
				if (isset($_REQUEST[$v])) $arg[$v] = $_REQUEST[$v];
			}

			$arg = (!$arg) ? $arg_cp : $arg;
		}

		$_bridge = $this->bridge;
		$_url = $this->url;

		$count_bridge = count($_bridge);
		$response = null;

		switch ($this->type) {
			case 'wsdl':
				$this->client = new nusoap_client($this->url, $this->wsdl);

				if ($error = $this->client->getError()) {
					echo 'Client error: ' . $error;
					exit;
				}

				$response = $this->client->call($method, $arg);
				
				// Check if there were any call errors, and if so, return error messages.
				if ($this->client->getError()) {
					$response = $this->client->response;
					$response = substr($response, strpos($response, '<?xml'));
					$response = xml2array($response);
					
					if (isset($response['soap:Envelope']['soap:Body']['soap:Fault']['faultstring'])) {
						$fault_string = $response['soap:Envelope']['soap:Body']['soap:Fault']['faultstring'];
						
						$response = explode("\n", $fault_string);
						$response = $response[0];
					} else {
						$response = $this->client->getError();
					}
					
					$response = array(
						'error' => true,
						'message' => $response
					);
				}
				
				$response = json_decode(json_encode($this->_filter($response)));
				break;
			case 'mysql':
				if (isset($arg['_mysql'])) {
					$this->params['_MYSQL'] = $arg['_mysql'];
					unset($arg['_mysql']);
				}

				$connect = (isset($this->params['_MYSQL']) && $this->params['_MYSQL']) ? $this->params['_MYSQL'] : '';

				if (empty($arg)) {
					return false;
				}

				global $db;

				require_once('class.mysql.php');
				$db = new database($connect);

				if (count($arg) > 1) {
					$sql = array_shift($arg);
					$arg = sql_filter($sql, $arg);
				}

				$response = (@function_exists($method)) ? $method($arg) : array('error' => true, 'message' => $method . ' is undefined');
				break;
			case 'php':
				if (isset($arg['_php'])) {
					unset($arg['_php']);
				}

				$print = w();
				switch ($method) {
					case 'tail':
					case 'cat':
						if (!@is_readable($arg[0])) {
							$response = 'Can not read file: ' . $arg[0];
						}
						break;
					case 'ping':
						$arg[1] = '-c' . ((isset($arg[1])) ? $arg[1] : 3);
						break;
				}

				switch ($method) {
					case 'tail':
					case 'cat':
					case 'ping':
					case 'exec':
						if ($response === null) {
							exec($method . ' ' . implode(' ', $arg), $print);
							$response = implode("\r\n", $print);
						}
						break;
					default:
						ob_start();

						if (@function_exists($method) || $method == 'eval') {
							eval(($method == 'eval') ? $arg[0] : 'echo @$method(' . (count($arg) ? "'" . implode("', '", $arg) . "'" : '') . ');');

							$_arg = error_get_last();
						} else {
							$_arg = array('message' => 'PHP Fatal error: Call to undefined function ' . $method . '()');
						}

						$response = (null === $_arg) ? ob_get_contents() : array('url' => $_url . $method, 'error' => 500, 'message' => $_arg['message']);

						ob_end_clean();
						break;
				}
				break;
			case 'facebook':
				if (isset($arg['_facebook'])) {
					unset($arg['_facebook']);
				}

				//header('Content-type: text/html; charset=utf-8');
				require_once('class.facebook.php');

				$facebook = new Facebook(array(
					'appId'  => $arg['APPID'],
					'secret' => $arg['APPSECRET'])
				);
				unset($arg['APPID'], $arg['APPSECRET']);

				try {
					$page = array_shift($arg);
					$page = (is_string($page)) ? '/' . $page : $page;
					
					$req = (isset($arg[0]) && is_string($arg[0])) ? array_shift($arg) : '';
					$req = (empty($req)) ? 'get' : $req;

					$arg = (isset($arg[0])) ? $arg[0] : $arg;

					$response = (!empty($page)) ? (count($arg) ? $facebook->$method($page, $req, $arg) : $facebook->$method($page, $req)) : $facebook->$method();
				} catch (FacebookApiException $e) {
					$response = array(
						'url' => $_url,
						'error' => 500,
						'message' => trim(str_replace('OAuthException: ', '', $e))
					);

					error_log($e);
				}

				unset($facebook);

				/*
				$feed = array(
					//$facebook->api($page)
					//$facebook->api('/228224130571301')
				);

				$attr = array(
					'access_token' => '125858306409|f1e0c20bc063e5f9a0c89615.1-1134314335|48722647107|JOI6oOl4sdhfX8Xf-rU3MfRwl70',
					'message' => 'Coca Cola!'
				);
				$feed['kamil'] = $facebook->api('/40796308305/posts/10150378826523306', 'post', $attr);*/
				break;
			default:
				$send_var = w('sso mysql php facebook');
				$send = new stdClass;

				if ($count_bridge == 1 && $_bridge[0] === $_url) {
					$count_bridge--;
					array_shift($_bridge);
				}

				foreach ($send_var as $row) {
					$val = '_' . strtoupper($row);
					$send->$row = (isset($this->params[$val]) && $this->params[$val]) ? $this->params[$val] : false;

					if (!$count_bridge && ($send->$row || isset($arg['_' . $row]))) {
						$this->type = $row;
					}
				}

				switch ($this->type) {
					case 'sso':
						$this->origin = false;

						$_url .= $method;
						unset($arg['_sso']);
						break;
					default:
						foreach ($send_var as $row) {
							if (isset($send->$row) && !empty($send->$row)) {
								$arg['_' . $row] = $send->$row;
							}
						}

						$arg['_method'] = $method;
						$arg['_unique'] = (!$this->unique) ? $this->unique : 1;
						
						if (isset($_bridge) && count($_bridge)) {
							array_shift($_bridge);
							$arg['_chain'] = implode('|', $_bridge);
						}
						break;
				}

				// _pre($arg, true);

				$_arg = $arg;
				$arg = ($this->type == 'sso') ? $this->_build($arg, false) : __encode($arg);

				$socket = @curl_init();
				@curl_setopt($socket, CURLOPT_URL, $_url);
				@curl_setopt($socket, CURLOPT_VERBOSE, 0);
				@curl_setopt($socket, CURLOPT_HEADER, 0);
				@curl_setopt($socket, CURLOPT_RETURNTRANSFER, 1);
				@curl_setopt($socket, CURLOPT_POST, 1);
				@curl_setopt($socket, CURLOPT_POSTFIELDS, $arg);
				@curl_setopt($socket, CURLOPT_SSL_VERIFYPEER, 0);
				@curl_setopt($socket, CURLOPT_SSL_VERIFYHOST, 1);

				$response = @curl_exec($socket);

				$_curl = new stdClass;
				$_curl->err = @curl_errno($socket);
				$_curl->msg = @curl_error($socket);
				$_curl->inf = (object) @curl_getinfo($socket);
				@curl_close($socket);

				switch ($_curl->err) {
					/**
					If the request has no errors.
					*/
					case 0:
						switch ($this->type) {
							/**
							SSO type
							*/
							case 'sso':
								if (preg_match('#<body>(.*?)</body>#i', $response, $response_part)) {
									preg_match('#<p><b>description</b>(.*?)</p>#i', $response_part[1], $status);
									
									$response = array(
										'url' => $_url,
										'error' => $_curl->inf->http_code,
										'message' => trim($status[1])
									);
								} else {
									switch($method) {
										case 'search':
											break;
										default:
											$first_parts = explode('&', substr($response, 0, -1));
											
											$ret = w();
											foreach ($first_parts as $v) {
												$second_parts = explode('=', $v);
									
												if (!isset($second_parts[1])) {
													continue;
												}
												
												$second_parts[0] = str_replace('.', '_', $second_parts[0]);
												$ret[$second_parts[0]] = $second_parts[1];
											}

											$response = $this->_format($response);
											break;
									}
								}
								break;
							/**
							Any other type
							*/
							default:
								$_json = json_decode($response);

								if ($_json === null) {
									$response = trim($response);
									$response = (!empty($response)) ? $response : $_curl->inf;

									$_json = $response;

									/*$_json = array(
										'url' => $_url,
										'error' => 500,
										'message' => $response
									);*/
								}
								
								$response = $_json;
								break;
						}
						break;
					/**
					Some error was generated after the request.
					*/
					default:
						$response = array(
							'url' => $_url,
							'error' => 500,
							'message' => $_curl->msg
						);
						break;
				}

				break;
		}

		if (!$this->origin) {
			$response = json_encode($response);
		}

		if ($this->type == 'sso' && $this->unique) {
			$response = json_decode($response);
		}

		if (is_array($response) && isset($response[0]) && is_string($response[0]) && strpos($response[0], '<?xml') !== false) {
			$response = array_change_key_case_recursive(xml2array($response[0]));

			$response = json_decode(json_encode($response));
		}

		return $response;
	}