Source for file MapFishPrintProxy.php

Documentation is available at MapFishPrintProxy.php

  1. <?php
  2. /**
  3. * Proxy of MapFishPrint type
  4. *
  5. * PHP versions 5
  6. @category  PHP
  7. @package   GeoPrisma
  8. @author    Alexandre Dube
  9. @copyright 2010, Centre de Geomatique du Quebec
  10. @license   http://www.geoprisma.org/license BSD License
  11. @link      http://www.geoprisma.org
  12. */
  13.  
  14. /**
  15. * Proxy of MapFishPrint type
  16. @category   PHP
  17. @package    GeoPrisma
  18. @subpackage Proxy
  19. @author     Alexadnre Dube
  20. */     
  21. {
  22.     /**
  23.     * @var Array 
  24.     */
  25.     private $m_objArraySpec null;
  26.  
  27.     /**
  28.     * @var Array 
  29.     */
  30.     private $m_objArraySpecLayers null;
  31.  
  32.     /**
  33.     * Get the action this proxy does.
  34.     *                                   
  35.     * @return string 
  36.     */
  37.     public function getAction()
  38.     {
  39.         return self::CRUD_READ;    
  40.     }
  41.  
  42.     /**
  43.     * Forward the call end return the result
  44.     * 
  45.     * @return void 
  46.     */
  47.     public function process()
  48.     {
  49.         // get the print capabilities to have the 'createURL'
  50.         $objCapabilities
  51.             = json_decode($this->getService()->getCapabilities()true);
  52.  
  53.         // get the params of the 'spec' with original 'source' values of the
  54.         // services
  55.         $strParams stripslashes(urldecode(json_encode($this->getSourceSpec())));
  56.  
  57.         ob_start();
  58.         $objCurl curl_init();
  59.         curl_setopt($objCurlCURLOPT_URL$objCapabilities['createURL']);
  60.         curl_setopt($objCurlCURLOPT_POST1);
  61.         curl_setopt($objCurlCURLOPT_RETURNTRANSFER1);
  62.         curl_setopt($objCurlCURLOPT_POSTFIELDS$strParams);
  63.         curl_setopt($objCurlCURLOPT_HTTPHEADERarray('Content-Type: text/plain'))
  64.         $strResult curl_exec($objCurl);
  65.         $objResult json_decode($strResult);
  66.  
  67.         $objArrayInfo curl_getinfo($objCurl);
  68.  
  69.         if ($objResult == null || !isset($objResult->getURL)) 
  70.         {
  71.             $objResult $strResult;
  72.         }
  73.         else
  74.         {
  75.             $objResult $objResult->getURL;
  76.         }
  77.  
  78.         curl_close($objCurl);
  79.  
  80.         if $_SERVER['REQUEST_METHOD'== 'POST' 
  81.         {
  82.             if (!session_id()) 
  83.             {
  84.                 session_start();
  85.             }
  86.  
  87.             $strID uniqid();
  88.  
  89.             $_SESSION[$strIDarray(
  90.                 "resources" => array(),
  91.                 "url" => $objResult
  92.             );
  93.  
  94.             foreach ($this->getResources(as $iIndex => $objResource
  95.             {
  96.                 array_push(
  97.                     $_SESSION[$strID]['resources'],
  98.                     $objResource->getName()
  99.                 );
  100.             }
  101.  
  102.             $strPrintURL $this->getService()->urlAppend(
  103.                 org_geoprisma_SettingImpl::getProxyURL(),
  104.                 array(
  105.                     "osmservice" => $this->getService()->getName(),
  106.                     "osmfile" => $strID
  107.                 )
  108.             );
  109.  
  110.             echo json_encode(array('getURL' => $strPrintURL));
  111.         }
  112.         else
  113.         {
  114.             header("Content-type: application/force-download");
  115.             header("Content-Transfer-Encoding: Binary");
  116.             header('Content-Disposition: attachment; filename="map.pdf"');
  117.  
  118.             readfile($objResult);
  119.         }
  120.  
  121.         ob_end_flush();
  122.     }
  123.  
  124.     // --------------------------
  125.     // Getter
  126.     // --------------------------
  127.     
  128.     /**
  129.     * Not used by this proxy.
  130.     * 
  131.     * @return void 
  132.     */
  133.     public function getLayers()
  134.     {
  135.         return;
  136.     }
  137.  
  138.     /**
  139.     * Returns the spec layers array
  140.     * 
  141.     * @return array 
  142.     */
  143.     public function getSpecLayers(
  144.     {
  145.         return $this->m_objArraySpecLayers;
  146.     }
  147.  
  148.     // --------------------------
  149.     // Setter
  150.     // --------------------------
  151.  
  152.     /**
  153.     * Set the spec layers array property
  154.     *
  155.     * @param array $pobjArraySpecLayers The 'spec' layers array
  156.     *
  157.     * @return void 
  158.     */
  159.     private function setSpecLayers($pobjArraySpecLayers
  160.     {
  161.         $this->m_objArraySpecLayers $pobjArraySpecLayers;
  162.     }
  163.  
  164.     // --------------------------
  165.     // Other methods
  166.     // --------------------------
  167.  
  168.     /**
  169.     * Get the spec array converted from the $_REQUEST['spec'] parameter if
  170.     * request is GET or from 'php://input' if POST.
  171.     *
  172.     * @return array 
  173.     */
  174.     public function getSpec()
  175.     {
  176.         if (!$this->m_objArraySpec
  177.         {
  178.             if $_SERVER['REQUEST_METHOD'== 'POST' 
  179.             {
  180.                 $strPostRequest file_get_contents('php://input');
  181.                 $this->m_objArraySpec json_decode($strPostRequesttrue);
  182.             }
  183.             else
  184.             {
  185.                 if (!isset($_REQUEST['spec'])) 
  186.                 {
  187.                     throw new Exception("spec param is missing");
  188.                 }
  189.  
  190.                 $this->m_objArraySpec json_decode($_REQUEST['spec']true);
  191.             }
  192.         }
  193.  
  194.         return $this->m_objArraySpec;
  195.     }
  196.  
  197.     /**
  198.     * Get the resources from the 'spec' object.  At the same time, get all
  199.     *     the requested 'layers' as well and the resources according 'services'
  200.     *     that are going to be used to render the images to print.
  201.     *
  202.     * @param org_geoprisma_config_Config &$pobjConfig The config object.
  203.     *
  204.     * @return array of org_geoprisma_resource_Resource objects
  205.     */
  206.     public function getResourcesFromRequest(&$pobjConfig)
  207.     {
  208.         $objSpec $this->getSpec();
  209.         $objArrayResources Array();
  210.         $objArraySpecLayers Array();
  211.  
  212.         foreach ($objSpec['layers'as $objLayer
  213.         {
  214.             if (!isset($objLayer['baseURL']&& $objLayer['type'== "Vector"
  215.             {
  216.                 continue;
  217.             }
  218.  
  219.             if (!$pobjConfig->isProxyURL($objLayer['baseURL'])) 
  220.             {
  221.                 continue;
  222.             }
  223.  
  224.             $objURL parse_url($objLayer['baseURL']);
  225.  
  226.             parse_str($objURL['query']);
  227.  
  228.             if (!isset($osmresource)) 
  229.             {
  230.                 throw new Exception("osmresource param is missing");
  231.             }
  232.  
  233.             if (!isset($osmservice)) 
  234.             {
  235.                 throw new Exception("osmservice param is missing");
  236.             }
  237.  
  238.             $objArraySubResources parent::getResourcesFromRequest(
  239.                 $pobjConfig,
  240.                 array(
  241.                     'osmresource' => $osmresource,
  242.                     'osmservice' => $osmservice
  243.                 )
  244.             );
  245.  
  246.             // keep track of the renderer service to use for each resource
  247.             foreach ($objArraySubResources as $objSubResource
  248.             {
  249.                 $objSubResource->m_objRendererService
  250.                     = $pobjConfig->getService($osmservice);
  251.             }
  252.  
  253.             // for each 'layers' of this layer, keep track of each mentioned
  254.             // resource for later validation
  255.             if (isset($objLayer['layers'])) 
  256.             {
  257.                 foreach ($objLayer['layers'as $strLayer
  258.                 {
  259.                     array_push(
  260.                         $objArraySpecLayers,
  261.                         array(
  262.                             'layer' => $strLayer,
  263.                             'resources' => $objArraySubResources
  264.                         )
  265.                     );
  266.                 }
  267.             }
  268.             else if (isset($objLayer['layer'])) 
  269.             {
  270.                 array_push(
  271.                     $objArraySpecLayers,
  272.                     array(
  273.                         'layer' => $objLayer['layer'],
  274.                         'resources' => $objArraySubResources
  275.                     )
  276.                 );
  277.             }
  278.             else 
  279.             {
  280.                 throw new Exception("The layer had no 'layer' or 'layers' properties defining which layer to print.");
  281.             }
  282.  
  283.             $objArrayResources array_merge(
  284.                 $objArrayResources$objArraySubResources
  285.             );
  286.  
  287.             unset($osmresource);
  288.             unset($osmservice);
  289.         }
  290.  
  291.         $this->setSpecLayers($objArraySpecLayers);
  292.  
  293.         return $objArrayResources;
  294.     }
  295.  
  296.     /**
  297.     * Validate the resources from the request using the parent method of the
  298.     *     same name.
  299.     *
  300.     * @return void 
  301.     */
  302.     public function validateResourcesFromRequest(
  303.     {
  304.         $objArrayResources $this->getResources();
  305.  
  306.         foreach ($objArrayResources as $objResource
  307.         {
  308.             parent::validateResourcesFromRequest(
  309.                 $objResource->m_objRendererService,
  310.                 Array($objResource)
  311.             );
  312.         }
  313.     }
  314.  
  315.     /**
  316.     * Validate the layers from the request using the parent method of the
  317.     *     same name.
  318.     *
  319.     * @return void 
  320.     */
  321.     public function validateLayersFromRequest()
  322.     {
  323.         $objArraySpecLayers $this->getSpecLayers();
  324.  
  325.         foreach ($objArraySpecLayers as $objSpecLayer
  326.         {
  327.             // resources that are grouped together always share the same
  328.             // service so it is safe to get the first one since they are all
  329.             // the same.
  330.             $objRendererService 
  331.                 = $objSpecLayer['resources'][0]->m_objRendererService;
  332.  
  333.             parent::validateLayersFromRequest(
  334.                 $objRendererService,
  335.                 $objSpecLayer['resources'],
  336.                 Array($objSpecLayer['layer'])
  337.             );
  338.         }
  339.     }
  340.  
  341.     /**
  342.     * Get the 'spec' object with all 'baseURL' changed to point to the according
  343.     *     service source url instead of the geoprisma proxy url.
  344.     *
  345.     * @return array 
  346.     */
  347.     public function getSourceSpec(
  348.     {
  349.         $objSpec $this->getSpec();
  350.         $objConfig org_geoprisma_config_ConfigFactory::getConfig()
  351.  
  352.         for ($iIter=0$iLen=count($objSpec['layers'])$iIter<$iLen$iIter++
  353.         {
  354.             $objLayer $objSpec['layers'][$iIter];
  355.  
  356.             if (!isset($objLayer['baseURL']&& $objLayer['type'== "Vector"
  357.             {
  358.                 continue;
  359.             }
  360.  
  361.             if (!$objConfig->isProxyURL($objLayer['baseURL'])) 
  362.             {
  363.                 continue;
  364.             }
  365.  
  366.             $objURL parse_url($objLayer['baseURL']);
  367.  
  368.             parse_str($objURL['query']);
  369.             $objSpec['layers'][$iIter]['baseURL']
  370.                 = $objConfig->getService($osmservice)->getSource();
  371.             unset($osmresource);
  372.             unset($osmservice);
  373.         }
  374.  
  375.         return $objSpec;
  376.     }
  377.  
  378. ?>

Documentation generated on Thu, 19 Jan 2012 00:08:40 +0400 by phpDocumentor 1.4.1