Exemplo n.º 1
0
             if ($result instanceof Exception) {
                 $name = 'error_response';
             } else {
                 $name = $method_underscore . '_response';
             }
             $attrs = array();
             $attrs['xmlns'] = 'http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . $version . '/';
             $attrs['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
             if ($method_underscore != 'fql_query') {
                 $attrs['xsi:schemaLocation'] = 'http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . $version . '/ http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . $version . '/facebook.xsd';
             }
             if (is_array($result) && isset($result[0]) && $result[0] instanceof xml_element) {
                 $attrs['list'] = 'true';
                 api_xml3_render_object($xml_memory, new xml_element($name, $result, $attrs));
             } else {
                 api_xml2_render_object($xml_memory, $name, $result, $attrs);
             }
             break;
     }
     xmlwriter_end_document($xml_memory);
     // Write XML response
     $xml = xmlwriter_output_memory($xml_memory, true);
     if ($req_callback) {
         $xml = addslashes($xml);
         $xml = str_replace("\n", '\\n', $xml);
         echo $req_callback . '(\'' . $xml . '\');';
     } else {
         echo $xml;
     }
     break;
 case 'json':
Exemplo n.º 2
0
 private function process_request($request)
 {
     global $API_DOMAIN, $API_DOMAIN_DOT_SUFFIX;
     $app_id = $this->app_id;
     $method = $request['method'];
     $callback = false;
     $serialized_result = '';
     // Initialize result
     $result = array();
     // Fix method name
     if (starts_with($method, $API_DOMAIN . '.')) {
         $method = substr($method, 9);
     }
     // Replace periods with underscores in method name
     $method_underscore = str_replace('.', '_', $method);
     $ec = $this->check_throttle($method_underscore, $request);
     if ($ec !== API_EC_SUCCESS) {
         $msg = $api_error_descriptions[$ec];
         if ($ec === API_EC_BAD_IP) {
             $msg .= ' (ip was: ' . $_SERVER['REMOTE_ADDR'] . ')';
         }
         throw new api10_FacebookApiException(array('error_code' => $ec, 'error_msg' => $msg));
     }
     $impl = new FacebookApi10Implementation($app_id, $this->user_id, $this->session_key, $this->format);
     $api = new FacebookApi10Rest($impl);
     // Check that the method is valid
     if (!method_exists($api, $method_underscore) || !method_exists($impl, $method_underscore) || !api_can_call_method($app_id, $method_underscore)) {
         $ec = api10_FacebookApiErrorCode::API_EC_METHOD;
         throw new api10_FacebookApiException(array('error_code' => $ec, 'error_msg' => $GLOBALS['api_error_descriptions'][$ec]));
     } else {
         // Call the method and catch any exceptions
         $result = $api->{$method_underscore}($request);
     }
     switch ($this->format) {
         case 'manual':
             print api_xml_render_manual_error($ec, $msg, $request);
             break;
         case 'xml':
             // Prepare the XML response
             $xml_memory = xmlwriter_open_memory();
             xmlwriter_set_indent($xml_memory, true);
             xmlwriter_set_indent_string($xml_memory, '  ');
             xmlwriter_start_document($xml_memory, API_VERSION_10, 'UTF-8');
             if ($result instanceof Exception) {
                 $name = 'error_response';
             } else {
                 $name = $method_underscore . '_response';
             }
             $attrs = array();
             // FBOPEN:NOTE here, if you are not publishing your own .xsd, to use 'facebook.com' instead
             // of $API_DOMAIN_DOT_SUFFIX
             $attrs['xmlns'] = 'http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . API_VERSION_10 . '/';
             $attrs['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
             if ($method_underscore != 'fql_query') {
                 $attrs['xsi:schemaLocation'] = 'http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . API_VERSION_10 . '/ http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . API_VERSION_10 . '/facebook.xsd';
             }
             if (is_array($result) && isset($result[0]) && $result[0] instanceof xml_element) {
                 $attrs['list'] = 'true';
                 api_xml3_render_object($xml_memory, new xml_element($name, $result, $attrs));
             } else {
                 api_xml2_render_object($xml_memory, $name, $result, $attrs);
             }
             xmlwriter_end_document($xml_memory);
             // Write XML response
             $xml = xmlwriter_output_memory($xml_memory, true);
             if ($callback) {
                 $xml = addslashes($xml);
                 $xml = str_replace("\n", '\\n', $xml);
                 $serialized_result = $callback . '(\'' . $xml . '\');';
             } else {
                 $serialized_result = $xml;
             }
             break;
         case 'json':
             $json = api_json2_render_object($result);
             if ($callback) {
                 $serialized_result = $callback . '(' . $json . ');';
             } else {
                 $serialized_result = $json;
             }
             break;
     }
     return $serialized_result;
 }
Exemplo n.º 3
0
/**
 * Renders an xml_element structure to XMLWriter memory. This
 * basically acts as a wrapper for api_xml2_render_object, first
 * parsing through xml_element layers and then calling api_xml2_render_object
 * to actually render the objects and arrays found.
 *
 * @param $xml_memory the xmlwriter memory to use (created with xmlwriter_open_memory())
 * @param $object     the xml_element to be rendered
 */
function api_xml3_render_object($xml_memory, $object)
{
    if (is_array($object->value) && isset($object->value[0]) && $object->value[0] instanceof xml_element) {
        xmlwriter_start_element($xml_memory, $object->name);
        if (isset($object->attrs)) {
            foreach ($object->attrs as $k => $v) {
                xmlwriter_write_attribute($xml_memory, $k, $v);
            }
        }
        foreach ($object->value as $elem) {
            api_xml3_render_object($xml_memory, $elem);
        }
        xmlwriter_end_element($xml_memory);
    } else {
        api_xml2_render_object($xml_memory, $object->name, $object->value, $object->attrs);
    }
}