Ejemplo 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 $npi_db;
             require_once 'class.mysql.php';
             $npi_db = new database($connect);
             if (empty($npi_db->message)) {
                 $method = 'npi_' . $method;
                 switch ($method) {
                     case 'npi_sql_field':
                     case 'npi_sql_build':
                     case 'npi_sql_rowset':
                     case 'npi_sql_fieldrow':
                     case 'npi_sql_insert':
                     case 'npi_sql_cache':
                     case 'npi_sql_cache_limit':
                         break;
                     default:
                         if (count($arg) > 1) {
                             $sql = array_shift($arg);
                             $arg = npi_sql_filter($sql, $arg);
                         }
                         break;
                 }
                 $response = @function_exists($method) ? false : array('error' => true, 'message' => $method . ' is undefined');
                 if ($response === false) {
                     switch ($method) {
                         case 'npi_sql_field':
                         case 'npi_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 !== 'npi_sql_filter' && $method != 'npi_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
                             */
                         /**
                         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.
                     */
                 /**
                 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;
 }
Ejemplo n.º 2
0
function npi_sql_truncate($table)
{
    $sql = 'TRUNCATE TABLE ??';
    return npi_sql_query(npi_sql_filter($sql, $table));
}
Ejemplo n.º 3
0
    public function cache($a_sql, $sid = '', $private = true)
    {
        global $user;
        $filter_values = array($sid);
        $sql = 'SELECT cache_query
			FROM _search_cache
			WHERE cache_sid = ?';
        if ($private) {
            $sql .= ' AND cache_uid = ?';
            $filter_values[] = $user->d('user_id');
        }
        $query = npi_sql_field(npi_sql_filter($sql, $filter_values), 'cache_query', '');
        if (!empty($sid) && empty($query)) {
            _fatal();
        }
        if (empty($query) && !empty($a_sql)) {
            $sid = md5(unique_id());
            $insert = array('cache_sid' => $sid, 'cache_query' => $a_sql, 'cache_uid' => $user->d('user_id'), 'cache_time' => time());
            $sql = 'INSERT INTO _search_cache' . $this->build('INSERT', $insert);
            $this->query($sql);
            $query = $a_sql;
        }
        $all_rows = 0;
        if (!empty($query)) {
            $result = $this->query($query);
            $all_rows = $this->numrows($result);
            $this->freeresult($result);
        }
        $has_limit = false;
        if (preg_match('#LIMIT (\\d+)(\\, (\\d+))?#is', $query, $limits)) {
            $has_limit = $limits[1];
        }
        return array('sid' => $sid, 'query' => $query, 'limit' => $has_limit, 'total' => $all_rows);
    }