default
[ class tree: default ] [ index: default ] [ all elements ]

Source for file guifi_api.php

Documentation is available at guifi_api.php

  1. <?php
  2. // The source code packaged with this file is Free Software, Copyright (C) 2005 by
  3. // Eduard Duran <eduard.duran at iglu.cat>.
  4. // It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
  5. // You can get copies of the licenses here:
  6. //         http://www.affero.org/oagpl.html
  7. // AFFERO GENERAL PUBLIC LICENSE is also included in the file called "LICENSE.txt".
  8.  
  9.  
  10. /**
  11.  * Client class for the guifi.net API
  12.  *
  13.  */
  14. class guifiAPI {
  15.     /**
  16.      * Which is the HTTP interface used by PHP to open HTTP connections (either curl, fopen or autodetection)
  17.      * @var string 
  18.      */
  19.     const http_interface 'auto';
  20.     
  21.     /**
  22.      * guifi.net API URL used with normal metods
  23.      * @var string 
  24.      */
  25.     private $url 'http://dev.guifi.net/api';
  26.     
  27.     /**
  28.      * guifi.net API URL used to authenticate the user
  29.      * @var string 
  30.      */
  31.     private $auth_url 'http://dev.guifi.net/api/auth';
  32.     
  33.     /**
  34.      * Whether the class is using the Development mode or not
  35.      * @var boolean 
  36.      */
  37.     const dev_mode false;
  38.     
  39.     /**
  40.      * What is the input format of the incoming responses from the API
  41.      * @var string 
  42.      */
  43.     const input_format 'json';
  44.     
  45.     /**
  46.      * What is the output format of the outcoming parameters to the API
  47.      * @var string 
  48.      */
  49.     const output_format 'get';
  50.     
  51.     private $username '';
  52.     private $password '';
  53.     
  54.     private $auth_token null;
  55.     private $errors array();
  56.     
  57.     /**
  58.      * Adds a zone to guifi.net
  59.      * @param $title Title of the zone
  60.      * @param $master Parent zone of the new zone
  61.      * @param $miny Latitude coordinate, in decimal degrees, of the lower-left corner of the zone (SW)
  62.      * @param $minx Longitude coordinate, in decimal degrees, of the lower-left corner of the zone (SW)
  63.      * @param $maxy Latitude coordinate, in decimal degrees, of the upper-right corner of the zone (NE)
  64.      * @param $maxx Longitude coordinate, in decimal degrees, of the upper-right corner of the zone (NE)
  65.      * @param $parameters Extra parameters to create the zone
  66.      * @return mixed An array with the zone_id, or false in case of failure
  67.      */
  68.     public function addZone$title$master$miny$minx$maxy$maxx$parameters array() ) {
  69.         $variables array();
  70.         
  71.         foreach$parameters as $key => $value {
  72.             $variables[$key$value;
  73.         }
  74.         
  75.         $variables['command''guifi.zone.add';
  76.         $variables['title'$title;
  77.         $variables['master'$master;
  78.         $variables['minx'$minx;
  79.         $variables['miny'$miny;
  80.         $variables['maxx'$maxx;
  81.         $variables['maxy'$maxy;
  82.         
  83.         $response $this->sendRequest$this->url$variables );
  84.         $body $this->parseResponse$response );
  85.         
  86.         if$body !== false {
  87.             return $body->responses;
  88.         else {
  89.             return false;
  90.         }
  91.     }
  92.     
  93.     /**
  94.      * Updates a guifi zone
  95.      * @param $zone_id Zone ID to edit
  96.      * @param $parameters Parameters to edit
  97.      * @return boolean Whether the zone was edited or not
  98.      */
  99.     public function updateZone$zone_id$parameters {
  100.         $variables array();
  101.         
  102.         foreach$parameters as $key => $value {
  103.             $variables[$key$value;
  104.         }
  105.         
  106.         $variables['command''guifi.zone.update';
  107.         $variables['zone_id'$zone_id;
  108.         
  109.         $response $this->sendRequest$this->url$variables );
  110.         $body $this->parseResponse$response );
  111.         return $body !== false;
  112.     }
  113.     
  114.     /**
  115.      * Removes a guifi zone
  116.      * @param $zone_id ID of the zone which should be removed
  117.      * @return boolean Whether the zone was removed or not
  118.      */
  119.     public function removeZone$zone_id {
  120.         $variables array();
  121.         $variables['command''guifi.zone.remove';
  122.         $variables['zone_id'$zone_id;
  123.         
  124.         $response $this->sendRequest$this->url$variables );
  125.         $body $this->parseResponse$response );
  126.         return $body !== false;
  127.     }
  128.     
  129.     /**
  130.      * Gets the zone which can contain a certain point
  131.      * @param $lat Latitude of the point
  132.      * @param $lon Longitude of the point
  133.      * @return mixed Nearest zones which can contain a certain point
  134.      */
  135.     public function nearestZone$lat$lon {
  136.         $variables array();
  137.         $variables['command''guifi.zone.nearest';
  138.         $variables['lat'$lat;
  139.         $variables['lon'$lon;
  140.         
  141.         $response $this->sendRequest$this->url$variables );
  142.         $body $this->parseResponse$response );
  143.         
  144.         if$body !== false {
  145.             return $body->responses;
  146.         else {
  147.             return false;
  148.         }
  149.     }
  150.     
  151.     /**
  152.      * Adds a new guifi.net node
  153.      * @param $title Title of the node
  154.      * @param $zone_id Zone ID of the node
  155.      * @param $lat Latitude where the node is
  156.      * @param $lon Longitude where the node is
  157.      * @param $parameters Parameters to specify node settings
  158.      * @return mixed Information of the newly created node (node_id)
  159.      */
  160.     public function addNode$title$zone_id$lat$lon$parameters array() ) {
  161.         $variables array();
  162.         
  163.         foreach$parameters as $key => $value {
  164.             $variables[$key$value;
  165.         }
  166.         
  167.         $variables['command''guifi.node.add';
  168.         $variables['title'$title;
  169.         $variables['zone_id'$zone_id;
  170.         $variables['lat'$lat;
  171.         $variables['lon'$lon;
  172.         
  173.         $response $this->sendRequest$this->url$variables );
  174.         $body $this->parseResponse$response );
  175.         
  176.         if$body !== false {
  177.             return $body->responses;
  178.         else {
  179.             return false;
  180.         }
  181.     }
  182.     
  183.     /**
  184.      * Updates a guifi node
  185.      * @param $node_id Node ID to edit
  186.      * @param $parameters Parameters to edit
  187.      * @return boolean Whether the node was edited or not
  188.      */
  189.     public function updateNode$node_id$parameters {
  190.         $variables array();
  191.         
  192.         foreach$parameters as $key => $value {
  193.             $variables[$key$value;
  194.         }
  195.         
  196.         $variables['command''guifi.node.update';
  197.         $variables['node_id'$node_id;
  198.         
  199.         $response $this->sendRequest$this->url$variables );
  200.         $body $this->parseResponse$response );
  201.         return $body !== false;
  202.     }
  203.     
  204.     /**
  205.      * Removes a guifi node
  206.      * @param $zone_id ID of the node which should be removed
  207.      * @return boolean Whether the node was removed or not
  208.      */
  209.     public function removeNode$node_id {
  210.         $variables array();
  211.         $variables['command''guifi.node.remove';
  212.         $variables['node_id'$node_id;
  213.         
  214.         $response $this->sendRequest$this->url$variables );
  215.         $body $this->parseResponse$response );
  216.         return $body !== false;
  217.     }
  218.     
  219.     /**
  220.      * Adds a guifi device to a node
  221.      * @param $node_id ID of the node where the device should be added
  222.      * @param $type Type of device which should be added (radio, mobile, server, nat, generic, adsl, cam, phone)
  223.      * @param $parameters Other parameters depending on the type of device, such as model_id, MAC address or firmware
  224.      * @return mixed The response with the newly created device_id or false in case of error
  225.      */
  226.     public function addDevice$node_id$type$mac$parameters array() ) {
  227.         $variables array();
  228.         
  229.         foreach$parameters as $key => $value {
  230.             $variables[$key$value;
  231.         }
  232.         
  233.         $variables['command''guifi.device.add';
  234.         $variables['node_id'$node_id;
  235.         $variables['type'$type;
  236.         $variables['mac'$mac;
  237.         
  238.         $response $this->sendRequest$this->url$variables );
  239.         $body $this->parseResponse$response );
  240.         if$body !== false {
  241.             return $body->responses;
  242.         else {
  243.             return false;
  244.         }
  245.     }
  246.     
  247.     /**
  248.      * Updates a guifi device
  249.      * @param $device_id Device ID to edit
  250.      * @param $parameters Parameters to edit
  251.      * @return boolean Whether the device was edited or not
  252.      */
  253.     public function updateDevice$device_id$parameters {
  254.         $variables array();
  255.         
  256.         foreach$parameters as $key => $value {
  257.             $variables[$key$value;
  258.         }
  259.         
  260.         $variables['command''guifi.device.update';
  261.         $variables['device_id'$device_id;
  262.         
  263.         $response $this->sendRequest$this->url$variables );
  264.         $body $this->parseResponse$response );
  265.         return $body !== false;
  266.     }
  267.     
  268.     /**
  269.      * Removes a guifi device from a node
  270.      * @param $device_id ID of the device which should be removed
  271.      * @return boolean Whether the device was removed or not
  272.      */
  273.     public function removeDevice$device_id {
  274.         $variables array();
  275.         $variables['command''guifi.device.remove';
  276.         $variables['device_id'$device_id;
  277.         
  278.         $response $this->sendRequest$this->url$variables );
  279.         $body $this->parseResponse$response );
  280.         return $body !== false;
  281.     }
  282.     
  283.     /**
  284.      * Adds a guifi Radio to a device
  285.      * @param $mode Mode of the radio to be added
  286.      * @param $device_id Device where the radio should be added
  287.      * @param $mac MAC address of the radio
  288.      * @return mixed Information about the added radio, such as radiodev_counter
  289.      */
  290.     public function addRadio$mode$device_id$mac ''$parameters array() ) {
  291.         $variables array();
  292.         
  293.         foreach$parameters as $key => $value {
  294.             $variables[$key$value;
  295.         }
  296.         
  297.         $variables['command''guifi.radio.add';
  298.         $variables['mode'$mode;
  299.         $variables['device_id'$device_id;
  300.         $variables['mac'$mac;
  301.         
  302.         $response $this->sendRequest$this->url$variables );
  303.         $body $this->parseResponse$response );
  304.         if$body !== false {
  305.             return $body->responses;
  306.         else {
  307.             return false;
  308.         }
  309.     }
  310.     
  311.     /**
  312.      * Updates a guifi radio of a device
  313.      * @param $device_id Device ID of the radio to be updated
  314.      * @param $radiodev_counter Position within the device where the radio is location
  315.      * @return boolean Whether the radio was updated or not
  316.      */
  317.     public function updateRadio$device_id$radiodev_counter$parameters {
  318.         $variables array();
  319.         
  320.         foreach$parameters as $key => $value {
  321.             $variables[$key$value;
  322.         }
  323.         
  324.         $variables['command''guifi.radio.update';
  325.         $variables['device_id'$device_id;
  326.         $variables['radiodev_counter'$radiodev_counter;
  327.         
  328.         $response $this->sendRequest$this->url$variables );
  329.         $body $this->parseResponse$response );
  330.         return $body !== false;
  331.     }
  332.     
  333.     /**
  334.      * Removes a guifi radio from a device
  335.      * @param $device_id ID of the device where the radio to be removed is
  336.      * @param $radiodev_counter Position within the device where the radio is
  337.      * @return boolean Whether the radio was removed or not
  338.      */
  339.     public function removeRadio$device_id$radiodev_counter {
  340.         $variables array();
  341.         $variables['command''guifi.radio.remove';
  342.         $variables['device_id'$device_id;
  343.         $variables['radiodev_counter'$radiodev_counter;
  344.         
  345.         $response $this->sendRequest$this->url$variables );
  346.         $body $this->parseResponse$response );
  347.         return $body !== false;
  348.     }
  349.     
  350.     /**
  351.      * Searches the nearest radios from a given node
  352.      * @param $node_id Node where to find the nearest radios
  353.      * @param $parameters Parameters such as maximum or minimum distance
  354.      * @return mixed Nearest radios from a given node
  355.      */
  356.     public function nearestRadio$node_id$parameters array() ) {
  357.         $variables array();
  358.         
  359.         foreach$parameters as $key => $value {
  360.             $variables[$key$value;
  361.         }
  362.         
  363.         $variables['command''guifi.radio.nearest';
  364.         $variables['node_id'$node_id;
  365.         
  366.         $response $this->sendRequest$this->url$variables );
  367.         $body $this->parseResponse$response );
  368.         
  369.         if$body !== false {
  370.             return $body->responses;
  371.         else {
  372.             return false;
  373.         }
  374.     }
  375.     
  376.     /**
  377.      * Adds a wLan interface to a radio to accept more clients
  378.      * @param $device_id Device where the interface should be added
  379.      * @param $radiodev_counter Position of the radio within the device where the interface should be added
  380.      * @return mixed Information about the newly created interface, such as interface_id
  381.      */
  382.     public function addInterface$device_id$radiodev_counter {
  383.         $variables array();
  384.         $variables['command''guifi.interface.add';
  385.         $variables['device_id'$device_id;
  386.         $variables['radiodev_counter'$radiodev_counter;
  387.         
  388.         $response $this->sendRequest$this->url$variables );
  389.         $body $this->parseResponse$response );
  390.         if$body !== false {
  391.             return $body->responses;
  392.         else {
  393.             return false;
  394.         }
  395.     }
  396.     
  397.     /**
  398.      * Removes a guifi interface from a radio
  399.      * @param $interface_id ID of the interface to be removed
  400.      * @return boolean Whether the interface was removed or not
  401.      */
  402.     public function removeInterface$interface_id {
  403.         $variables array();
  404.         $variables['command''guifi.interface.remove';
  405.         $variables['interface_id'$interface_id;
  406.         
  407.         $response $this->sendRequest$this->url$variables );
  408.         $body $this->parseResponse$response );
  409.         return $body !== false;
  410.     }
  411.     
  412.     /**
  413.      * Adds a link to an guifi.net interface
  414.      * @param $from_device_id Device ID of the origin of the link
  415.      * @param $from_radiodev_counter Position of the radio within its device of the origin of the link
  416.      * @param $to_device_id Device ID of the other extreme of the link
  417.      * @param $to_radiodev_counter Position of the radio within its device of the other extreme of the link
  418.      * @param $parameters Other parameters of the link to be added
  419.      * @return mixed Information about the newly created link, such as link_id
  420.      */
  421.     public function addLink$from_device_id$from_radiodev_counter$to_device_id$to_radiodev_counter$parameters array() ) {
  422.         $variables array();
  423.         
  424.         foreach$parameters as $key => $value {
  425.             $variables[$key$value;
  426.         }
  427.         
  428.         $variables['command''guifi.link.add';
  429.         $variables['from_device_id'$from_device_id;
  430.         $variables['from_radiodev_counter'$from_radiodev_counter;
  431.         $variables['to_device_id'$to_device_id;
  432.         $variables['to_radiodev_counter'$to_radiodev_counter;
  433.         
  434.         $response $this->sendRequest$this->url$variables );
  435.         $body $this->parseResponse$response );
  436.         if$body !== false {
  437.             return $body->responses;
  438.         else {
  439.             return false;
  440.         }
  441.     }
  442.     
  443.     /**
  444.      * Updates a guifi link
  445.      * @param $link_id Link ID to be updated
  446.      * @param $parameters Parameters of the link to be updated
  447.      * @return boolean Whether the link was updated or not
  448.      */
  449.     public function updateLink$link_id$parameters {
  450.         $variables array();
  451.         
  452.         foreach$parameters as $key => $value {
  453.             $variables[$key$value;
  454.         }
  455.         
  456.         $variables['command''guifi.link.update';
  457.         $variables['link_id'$link_id;
  458.         
  459.         $response $this->sendRequest$this->url$variables );
  460.         $body $this->parseResponse$response );
  461.         return $body !== false;
  462.     }
  463.     
  464.     /**
  465.      * Removes a link from guifi.net
  466.      * @param $link_id Link ID to be removed
  467.      * @return boolean Whether the link was removed or not
  468.      */
  469.     public function removeLink$link_id {
  470.         $variables array();
  471.         $variables['command''guifi.link.remove';
  472.         $variables['link_id'$link_id;
  473.         
  474.         $response $this->sendRequest$this->url$variables );
  475.         $body $this->parseResponse$response );
  476.         return $body !== false;
  477.     }
  478.     
  479.     /**
  480.      * Gets a list of devices models
  481.      * @param $parameters string[] of possible parameters to retrieve filtered models
  482.      * @return string[] Models retrieved from the server
  483.      */
  484.     public function getModels$parameters array() ) {
  485.         $variables array();
  486.         
  487.         foreach$parameters as $key => $value {
  488.             $variables[$key$value;
  489.         }
  490.         
  491.         $variables['command''guifi.misc.model';
  492.         
  493.         $response $this->sendRequest$this->url$variables );
  494.         $body $this->parseResponse$response );
  495.         if!empty$body->responses->models ) ) {
  496.             return $body->responses->models;
  497.         else {
  498.             return false;
  499.         }
  500.     }
  501.     
  502.     /**
  503.      * Gets a list of device manufacturers
  504.      * @return string[] Manufacturers retrieved from the server
  505.      */
  506.     public function getManufacturers({
  507.         $variables array();
  508.         $variables['command''guifi.misc.manufacturer';
  509.         
  510.         $response $this->sendRequest$this->url$variables );
  511.         $body $this->parseResponse$response );
  512.         if!empty$body->responses->manufacturers ) ) {
  513.             return $body->responses->manufacturers;
  514.         else {
  515.             return false;
  516.         }
  517.     }
  518.     
  519.     /**
  520.      * Gets a list of supported firmwares to be used with devices
  521.      * @param $parameters Firmware filters to be applied
  522.      * @return string[] Firmwares retrieved from the server
  523.      */
  524.     public function getFirmwares$parameters array() ) {
  525.         $variables array();
  526.         
  527.         foreach$parameters as $key => $value {
  528.             $variables[$key$value;
  529.         }
  530.         
  531.         $variables['command''guifi.misc.firmware';
  532.         
  533.         $response $this->sendRequest$this->url$variables );
  534.         $body $this->parseResponse$response );
  535.         if!empty$body->responses->firmwares ) ) {
  536.             return $body->responses->firmwares;
  537.         else {
  538.             return false;
  539.         }
  540.     }
  541.     
  542.     /**
  543.      * Gets a list of supported protocols to be used with links
  544.      * @return string[] Protocols retrieved from the server
  545.      */
  546.     public function getProtocols({
  547.         $variables array();
  548.         $variables['command''guifi.misc.protocol';
  549.         
  550.         $response $this->sendRequest$this->url$variables );
  551.         $body $this->parseResponse$response );
  552.         if!empty$body->responses->protocols ) ) {
  553.             return $body->responses->protocols;
  554.         else {
  555.             return false;
  556.         }
  557.     }
  558.     
  559.     /**
  560.      * Gets a list of channels to be used with links
  561.      * @param $protocol Protocol the channels apply to
  562.      * @return string[] Channels retrieved from the server
  563.      */
  564.     public function getChannels$protocol {
  565.         $variables array();
  566.         $variables['command''guifi.misc.channel';
  567.         $variables['protocol'$protocol;
  568.         
  569.         $response $this->sendRequest$this->url$variables );
  570.         $body $this->parseResponse$response );
  571.         if!empty$body->responses->channels ) ) {
  572.             return $body->responses->channels;
  573.         else {
  574.             return false;
  575.         }
  576.     }
  577.     
  578.     /**
  579.      * Constructor function for all new guifiAPI instances
  580.      * 
  581.      * Set up authentication with guifi and gets authentication token
  582.      *
  583.      * @param String $username Username of the guifi.net account wanted to authenticate
  584.      * @param String $password Password of the guifi.net account wanted to authenticate
  585.      * @param String $token If any token is given, no need to send the username and password to the server
  586.      */
  587.     public function __construct$username$password$token null {
  588.         $this->username $username;
  589.         $this->password $password;
  590.         if!empty$token ) ) {
  591.             $this->auth_token $token;
  592.         else {
  593.             $this->authenticateUser$username$password );
  594.         }
  595.     }
  596.     
  597.     /**
  598.      * Authenticate guifi.net account against guifi.net
  599.      *
  600.      * @param string $email 
  601.      * @param string $password 
  602.      * @return boolean Whether the authentication was successful or not
  603.      */
  604.     protected function authenticateUser$username$password {
  605.         $variables array('command' => 'guifi.auth.login''username' => $username'password' => $password'method' => 'password' );
  606.         
  607.         $response $this->sendRequest$this->auth_url$variables );
  608.         
  609.         // Parses the response from the guifi.net API
  610.         $body $this->parseResponse$response );
  611.         if$body !== false {
  612.             $responses $body->responses;
  613.             if!empty$responses->authToken ) ) {
  614.                 $this->auth_token $responses->authToken;
  615.                 return true;
  616.             else {
  617.                 return false;
  618.             }
  619.         else {
  620.             return false;
  621.         }
  622.     }
  623.     
  624.     /**
  625.      * Retreives the authentication token used to authenticate the user in upcoming methods without sending the username and password each time
  626.      * @return string Authentication token
  627.      */
  628.     public function getAuthToken({
  629.         return $this->auth_token;
  630.     }
  631.     
  632.     /**
  633.      * Generates the authentication header to authenticate using a token against guifi.net
  634.      * @return mixed Header of authentication
  635.      */
  636.     protected function generateAuthHeader({
  637.         if$this->auth_token {
  638.             return array('Authorization: GuifiLogin auth=' $this->auth_token );
  639.         else {
  640.             return array();
  641.         }
  642.     }
  643.     
  644.     /**
  645.      * Performs the request to the guifi.net API server
  646.      * @param $url URL to send the request to
  647.      * @param $variables Variables to be formatted to be sent to the server
  648.      * @return mixed response from the API server
  649.      */
  650.     protected function sendRequest$url$variables {
  651.         $this->pendingUrl $url;
  652.         $this->pendingVariables $variables;
  653.         
  654.         switchguifiAPI::output_format {
  655.             case 'get':
  656.                 $get_variables $variables;
  657.                 $post_variables array();
  658.                 break;
  659.             case 'post':
  660.                 $get_variables array();
  661.                 $post_variables $variables;
  662.                 break;
  663.         }
  664.         
  665.         $response $this->httpRequest$url$get_variables$post_variables$this->generateAuthHeader() );
  666.         return $response;
  667.     }
  668.     
  669.     /**
  670.      * Parses a response from the server, according ti the input format
  671.      * @param $response Response string to be parsed
  672.      * @return mixed Returns the body of the response in case of success, false in case of failure
  673.      */
  674.     protected function parseResponse$response {
  675.         $code $response['code'];
  676.         
  677.         switchguifiAPI::input_format {
  678.             case 'json':
  679.                 $body json_decode$response['body');
  680.                 break;
  681.             case 'url':
  682.                 parse_strstr_replacearray("\n""\r\n" )'&'$response['body')$body );
  683.                 break;
  684.         }
  685.         
  686.         ifsubstr$code0!= '2' || !is_object$body ) ) {
  687.             throw new Exception'guifiAPI: Failed to parse response. Error: "' strip_tags$response['body''"' );
  688.         }
  689.         
  690.         if!empty$body->errors ) ) {
  691.             if$body->errors[0]->code == 502 {
  692.                 unset$this->auth_token );
  693.                 $pendingUrl $this->pendingUrl;
  694.                 $pendingVariables $this->pendingVariables;
  695.                 $authenticated $this->authenticateUser$this->username$this->password );
  696.                 
  697.                 if$authenticated {
  698.                     $response $this->sendRequest$pendingUrl$pendingVariables );
  699.                     return $this->parseResponse$response );
  700.                 }
  701.             }
  702.             $this->errors $body->errors;
  703.             
  704.             return false;
  705.         }
  706.         
  707.         ifempty$body->code || substr$body->code->code0!= '2' {
  708.             return false;
  709.         }
  710.         
  711.         $this->responseCode $body->code;
  712.         ifisset$body->responses ) ) {
  713.             $this->responses $body->responses;
  714.         }
  715.         
  716.         return $body;
  717.     }
  718.     
  719.     /**
  720.      * Retreives the possible errors commited during a method
  721.      * @return string[] 
  722.      */
  723.     public function getErrors({
  724.         return $this->errors;
  725.     }
  726.     
  727.     /**
  728.      * Retreives a list of the errors parsed as a string
  729.      *
  730.      * @param string $format Format of the list, either 'html' or 'plain'
  731.      * @return string List of formatted errors
  732.      */
  733.     public function getErrorsStr$format 'html' {
  734.         $ret '';
  735.         if!$this->errors {
  736.             return $ret;
  737.         }
  738.         if$format == 'html' {
  739.             $ret .= '<ul>';
  740.         }
  741.         foreach$this->errors as $error {
  742.             if$format == 'html' {
  743.                 $ret .= '<li>';
  744.             }
  745.             $ret .= "Code $error->code$error->str";
  746.             ifisset$error->extra ) ) {
  747.                 $ret .= " (Extra: $error->extra)";
  748.             }
  749.             if$format == 'html' {
  750.                 $ret .= '</li>';
  751.             else if$format == 'plain' {
  752.                 $ret .= "\n";
  753.             }
  754.         }
  755.         if$format == 'html' {
  756.             $ret .= '</ul>';
  757.         }
  758.         return $ret;
  759.     }
  760.     
  761.     /**
  762.      * Perform HTTP request
  763.      *
  764.      * @param array $get_variables 
  765.      * @param array $post_variables 
  766.      * @param array $headers 
  767.      */
  768.     protected function httpRequest$url$get_variables null$post_variables null$headers null {
  769.         $interface guifiAPI::http_interface;
  770.         
  771.         ifguifiAPI::http_interface == 'auto' {
  772.             iffunction_exists'curl_exec' ) ) {
  773.                 $interface 'curl';
  774.             else {
  775.                 $interface 'fopen';
  776.             }
  777.         }
  778.         
  779.         if$interface == 'curl' {
  780.             return $this->curlRequest$url$get_variables$post_variables$headers );
  781.         elseif$interface == 'fopen' {
  782.             return $this->fopenRequest$url$get_variables$post_variables$headers );
  783.         else {
  784.             throw new Exception'Invalid http interface defined. No such interface "' GA_api::http_interface '"' );
  785.         }
  786.     }
  787.     /**
  788.      * HTTP request using PHP CURL functions
  789.      * Requires curl library installed and configured for PHP
  790.      * 
  791.      * @param array $get_variables 
  792.      * @param array $post_variables 
  793.      * @param array $headers 
  794.      */
  795.     private function curlRequest$url$get_variables null$post_variables null$headers null {
  796.         $ch curl_init();
  797.         
  798.         ifis_array$get_variables ) ) {
  799.             $get_variables '?' str_replace'&amp;''&'http_build_query$get_variables ) );
  800.         else {
  801.             $get_variables null;
  802.         }
  803.         
  804.         curl_setopt$chCURLOPT_URL$url $get_variables );
  805.         curl_setopt$chCURLOPT_RETURNTRANSFERtrue );
  806.         curl_setopt$chCURLOPT_SSL_VERIFYPEERfalse );
  807.         
  808.         ifis_array$post_variables ) ) {
  809.             curl_setopt$chCURLOPT_POSTtrue );
  810.             curl_setopt$chCURLOPT_POSTFIELDS$post_variables );
  811.         }
  812.         
  813.         ifis_array$headers ) ) {
  814.             curl_setopt$chCURLOPT_HTTPHEADER$headers );
  815.         }
  816.         
  817.         curl_setopt$chCURLOPT_HEADERtrue );
  818.         
  819.         $response curl_exec$ch );
  820.         $code curl_getinfo$chCURLINFO_HTTP_CODE );
  821.         $header_size curl_getinfo$chCURLINFO_HEADER_SIZE );
  822.         $headers substr$response0$header_size );
  823.         $body substr$response$header_size );
  824.         
  825.         curl_close$ch );
  826.         
  827.         return array('body' => $body'headers' => $headers'code' => $code );
  828.     }
  829. }
  830.  
  831. ?>

Documentation generated on Sun, 10 Jan 2010 21:02:50 +0100 by phpDocumentor 1.4.3