protected function _prepareTpl()
 {
     // Get the project and repository params
     $project = $this->param('project');
     $repository = $this->param('repository');
     $auth_url_return = $this->param('auth_url_return');
     if (!$auth_url_return) {
         $auth_url_return = jUrl::get('view~map:index', array("repository" => $repository, "project" => $project));
     }
     // Get lizmapProject class
     $assign = array('isConnected' => jAuth::isConnected(), 'user' => jAuth::getUserSession(), 'auth_url_return' => $auth_url_return, "externalSearch" => "", "edition" => false, "measure" => false, "locate" => false, "geolocation" => false, "timemanager" => false, "print" => false, "attributeLayers" => false);
     try {
         $lproj = lizmap::getProject($repository . '~' . $project);
         $configOptions = $lproj->getOptions();
         if (property_exists($configOptions, 'externalSearch')) {
             $assign['externalSearch'] = $configOptions->externalSearch;
         }
     } catch (UnknownLizmapProjectException $e) {
         jLog::logEx($e, 'error');
     }
     $this->_tpl->assign($assign);
     // Get lizmap services
     $services = lizmap::getServices();
     if ($services->allowUserAccountRequests) {
         $this->_tpl->assign('allowUserAccountRequests', True);
     }
 }
 protected function _prepareTpl()
 {
     // Get the project and repository params
     $project = $this->param('project');
     $repository = $this->param('repository');
     // Get lizmapProject class
     $assign = array("edition" => false, "measure" => false, "locate" => false, "geolocation" => false, "timemanager" => false, "print" => false, "attributeLayers" => false);
     try {
         $lproj = lizmap::getProject($repository . '~' . $project);
         $configOptions = $lproj->getOptions();
         if (property_exists($configOptions, 'measure') && $configOptions->measure == 'True') {
             $assign['measure'] = true;
         }
         $assign['locate'] = $lproj->hasLocateByLayer();
         if (property_exists($configOptions, 'print') && $configOptions->print == 'True') {
             $assign['print'] = true;
         }
         $assign['edition'] = $lproj->hasEditionLayers();
         if (property_exists($configOptions, 'geolocation') && $configOptions->geolocation == 'True') {
             $assign['geolocation'] = true;
         }
         $assign['timemanager'] = $lproj->hasTimemanagerLayers();
         $assign['attributeLayers'] = $lproj->hasAttributeLayers();
     } catch (UnknownLizmapProjectException $e) {
         jLog::logEx($e, 'error');
     }
     $this->_tpl->assign($assign);
 }
 /**
  * Displays a list of project for a given repository.
  *
  * @param string $repository. Name of the repository.
  * @return Html page with a list of projects.
  */
 function index()
 {
     if ($this->param('theme')) {
         jApp::config()->theme = $this->param('theme');
     }
     $rep = $this->getResponse('html');
     // Get lizmap services
     $services = lizmap::getServices();
     // only maps
     if ($services->onlyMaps) {
         $repository = lizmap::getRepository($services->defaultRepository);
         if ($repository && jAcl2::check('lizmap.repositories.view', $repository->getKey())) {
             $project = lizmap::getProject($repository->getKey() . '~' . $services->defaultProject);
             if ($project) {
                 // test redirection to an other controller
                 $items = jEvent::notify('mainviewGetMaps')->getResponse();
                 foreach ($items as $item) {
                     if ($item->parentId == $repository->getKey() && $item->id == $services->defaultProject) {
                         $rep = $this->getResponse('redirectUrl');
                         $rep->url = $item->url;
                         return $rep;
                     }
                 }
                 // redirection to default controller
                 $rep = $this->getResponse('redirect');
                 $rep->action = 'view~map:index';
                 return $rep;
             }
         }
     }
     // Get repository data
     $repository = $this->param('repository');
     $repositoryList = array();
     if ($repository) {
         if (!jAcl2::check('lizmap.repositories.view', $repository)) {
             $rep = $this->getResponse('redirect');
             $rep->action = 'view~default:index';
             jMessage::add(jLocale::get('view~default.repository.access.denied'), 'error');
             return $rep;
         }
     }
     $title = jLocale::get("view~default.repository.list.title");
     $rep->body->assign('repositoryLabel', $title);
     $rep->body->assign('isConnected', jAuth::isConnected());
     $rep->body->assign('user', jAuth::getUserSession());
     if ($services->allowUserAccountRequests) {
         $rep->body->assign('allowUserAccountRequests', True);
     }
     if ($repository) {
         $lrep = lizmap::getRepository($repository);
         $title .= ' - ' . $lrep->getData('label');
     }
     $rep->title = $title;
     $rep->body->assignZone('MAIN', 'main_view', array('repository' => $repository));
     $rep->addJSCode("\n      \$(window).load(function() {\n        \$('.liz-project-img').parent().mouseenter(function(){\n          var self = \$(this);\n          self.find('.liz-project-desc').slideDown();\n          self.css('cursor','pointer');\n        }).mouseleave(function(){\n          var self = \$(this);\n          self.find('.liz-project-desc').hide();\n        }).click(function(){\n          var self = \$(this);\n          window.location = self.parent().find('a.liz-project-view').attr('href');\n          return false;\n        });\n      });\n      ");
     // Js hack to normalize the height of the project thumbnails to avoid line breaks with long project titles
     $bp = jApp::config()->urlengine['basePath'];
     $rep->addJSLink($bp . 'js/view.js');
     return $rep;
 }
 public function getLabel2($key, $form)
 {
     $criteria = $form->getData($this->criteriaFrom);
     if ($criteria && array_key_exists($criteria, $this->data)) {
         $p = lizmap::getProject($criteria . '~' . $key);
         if ($p) {
             return (string) $p->getData('title');
         }
     }
     return null;
 }
 public function getLabel2($key, $form)
 {
     $criteria = $form->getData($this->criteriaFrom[0]);
     if ($criteria && array_key_exists($criteria, $this->data)) {
         try {
             $p = lizmap::getProject($criteria . '~' . $key);
             if ($p) {
                 return (string) $p->getData('title');
             }
         } catch (UnknownLizmapProjectException $e) {
             jLog::logEx($e, 'error');
             return null;
         }
     }
     return null;
 }
 /**
  * Get parameters and set classes for the project and repository given.
  *
  * @return array List of needed variables : $params, $lizmapProject, $lizmapRepository, $lizmapCache.
  */
 protected function getServiceParameters()
 {
     // Get the project
     $project = $this->iParam('project');
     if (!$project) {
         jMessage::add('The parameter project is mandatory !', 'ProjectNotDefined');
         return false;
     }
     // Get repository data
     $repository = $this->iParam('repository');
     // Get the corresponding repository
     $lrep = lizmap::getRepository($repository);
     if (!$lrep) {
         jMessage::add('The repository ' . strtoupper($repository) . ' does not exist !', 'RepositoryNotDefined');
         return false;
     }
     // Get the project object
     $lproj = null;
     try {
         $lproj = lizmap::getProject($repository . '~' . $project);
         if (!$lproj) {
             jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'ProjectNotDefined');
             return false;
         }
     } catch (UnknownLizmapProjectException $e) {
         jLog::logEx($e, 'error');
         jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'ProjectNotDefined');
         return false;
     }
     // Redirect if no rights to access this repository
     if (!$lproj->checkAcl()) {
         jMessage::add(jLocale::get('view~default.repository.access.denied'), 'AuthorizationRequired');
         return false;
     }
     // Get and normalize the passed parameters
     $pParams = jApp::coord()->request->params;
     $pParams['map'] = realpath($lrep->getPath()) . '/' . $project . ".qgs";
     $lizmapCache = jClasses::getService('lizmap~lizmapCache');
     $params = $lizmapCache->normalizeParams($pParams);
     // Define class private properties
     $this->project = $lproj;
     $this->repository = $lrep;
     $this->services = lizmap::getServices();
     $this->params = $params;
     $this->lizmapCache = $lizmapCache;
     // Optionnaly filter data by login
     if (isset($params['request'])) {
         $request = strtolower($params['request']);
         if (in_array($request, array('getmap', 'getfeatureinfo', 'getfeature', 'getprint')) and !jAcl2::check('lizmap.tools.loginFilteredLayers.override', $lrep->getKey())) {
             $this->filterDataByLogin();
         }
     }
     return true;
 }
 /**
  * Unlink child feature from their parent ( 1:n ) relation
  * by setting the foreign key to NULL
  *
  * @param string $repository Lizmap Repository
  * @param string $project Name of the project
  * @param string $layerId Child layer id.
  * @param string $pkey Child layer primary key value -> id of the line to update
  * @param string $fkey Child layer foreign key column (pointing to the parent layer primary key)
  * @return Redirect to the validation action.
  */
 function unlinkChild()
 {
     $lid = $this->param('lid');
     $fkey = $this->param('fkey');
     $pkey = $this->param('pkey');
     $pkeyval = $this->param('pkeyval');
     $project = $this->param('project');
     $repository = $this->param('repository');
     if (!$lid or !$fkey or !$pkey or !$pkeyval or !$project or !$repository) {
         jMessage::add(jLocale::get("view~edition.link.error.missing.parameter"), 'error');
         return $this->serviceAnswer();
     }
     // Get project configuration
     $lrep = lizmap::getRepository($repository);
     $lproj = lizmap::getProject($repository . '~' . $project);
     $this->project = $lproj;
     $this->repository = $lrep;
     // Get child layer information
     $layerXml = $lproj->getXmlLayer($lid);
     $layerXmlZero = $layerXml[0];
     $_layerName = $layerXmlZero->xpath('layername');
     $layerName = (string) $_layerName[0];
     $this->layerXml = $layerXml;
     // Get editLayer capabilities
     $eLayers = $lproj->getEditionLayers();
     $eLayer = $eLayers->{$layerName};
     if ($eLayer->capabilities->modifyAttribute != 'True') {
         jMessage::add('Modify feature attributes for this layer ' . $layerName . ' is not in the capabilities!', 'LayerNotEditable');
         return $this->serviceAnswer();
     }
     // Get fields data from the edition database
     $_datasource = $layerXmlZero->xpath('datasource');
     $datasource = (string) $_datasource[0];
     $s_provider = $layerXmlZero->xpath('provider');
     $this->provider = (string) $s_provider[0];
     $this->layerId = $lid;
     $this->layerName = $layerName;
     $this->getDataFields($datasource);
     // Check fields
     if (!array_key_exists($fkey, $this->dataFields) or !array_key_exists($pkey, $this->dataFields)) {
         jMessage::add('Given fields do not exists !', 'error');
         return $this->serviceAnswer();
     }
     // Build SQL
     $sql = '';
     $cnx = jDb::getConnection($this->layerId);
     $msg = false;
     $val = (int) $pkeyval;
     if ($this->dataFields[$key2]->type != 'int') {
         $val = $cnx->quote($val);
     }
     $sql = ' UPDATE ' . $this->table;
     $sql .= ' SET "' . $fkey . '" = NULL';
     $sql .= ' WHERE "' . $pkey . '" = ' . $val;
     $sql .= ';';
     // Need to break SQL ( if sqlite
     try {
         $rs = $cnx->query($sql);
         if (!$msg) {
             jMessage::add(jLocale::get('view~edition.unlink.success'), 'success');
         }
         $msg = true;
     } catch (Exception $e) {
         jLog::log("An error has been raised when modifiying data : " . $e->getMessage(), 'error');
         jLog::log("SQL = " . $sql);
         jMessage::add(jLocale::get('view~edition.unlink.error.sql'), 'error');
     }
     return $this->serviceAnswer();
 }
 /**
  * Load the map page for the given project.
  * @param string $repository Name of the repository.
  * @param string $project Name of the project.
  * @return Page with map and content for the chose Qgis project.
  */
 function index()
 {
     if ($this->param('theme')) {
         jApp::config()->theme = $this->param('theme');
     }
     $rep = $this->getResponse('htmlmap');
     $rep->addJSLink(jUrl::get('view~translate:index'));
     $ok = true;
     // Get the project
     $project = filter_var($this->param('project'), FILTER_SANITIZE_STRING);
     // Get repository data
     $repository = $this->param('repository');
     // Get lizmapRepository class
     // if repository not found get the default
     $lrep = null;
     $lser = lizmap::getServices();
     if (!$repository) {
         $lrep = lizmap::getRepository($lser->defaultRepository);
         $repository = $lser->defaultRepository;
     } else {
         $lrep = lizmap::getRepository($repository);
     }
     if (!$lrep or !jAcl2::check('lizmap.repositories.view', $lrep->getKey())) {
         $rep = $this->getResponse('redirect');
         $rep->action = 'view~default:index';
         jMessage::add(jLocale::get('view~default.repository.access.denied'), 'error');
         return $rep;
     }
     // We must redirect to default repository project list if no project given
     if (!$project) {
         $lproj = lizmap::getProject($lrep->getKey() . '~' . $lser->defaultProject);
         if (!$lproj) {
             jMessage::add('The parameter project is mandatory !', 'error');
             $ok = false;
         } else {
             $project = $lser->defaultProject;
         }
     }
     // Get lizmapProject class
     if ($ok) {
         $lproj = lizmap::getProject($lrep->getKey() . '~' . $project);
         if (!$lproj) {
             jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'error');
             $ok = false;
         }
     }
     // Redirect if project is hidden (lizmap plugin option)
     if ($ok and !$this->forceHiddenProjectVisible) {
         $pOptions = $lproj->getOptions();
         if (property_exists($pOptions, 'hideProject') && $pOptions->hideProject == 'True') {
             jMessage::add(jLocale::get('view~default.project.access.denied'), 'error');
             $ok = false;
         }
     }
     // Redirect if error encountered
     if (!$ok) {
         $rep = $this->getResponse('redirect');
         $rep->params = array('repository' => $lrep->getKey());
         $rep->action = 'view~default:index';
         return $rep;
     }
     // Add js link if google is needed
     if ($lproj->needsGoogle()) {
         $googleKey = $lproj->getGoogleKey();
         if ($googleKey != '') {
             $rep->addJSLink('https://maps.google.com/maps/api/js?v=3.5&sensor=false&key=' . $googleKey);
         } else {
             $rep->addJSLink('https://maps.google.com/maps/api/js?v=3.5&sensor=false');
         }
     }
     // Add the jForms js
     $bp = jApp::config()->urlengine['basePath'];
     $rep->addJSLink($bp . 'jelix/js/jforms_jquery.js');
     $rep->addJSLink($bp . 'jelix/js/jforms/datepickers/default/init.js');
     $rep->addJSLink($bp . 'jelix/js/jforms/datepickers/default/ui.en.js');
     $rep->addJSLink($bp . 'js/fileUpload/jquery.fileupload.js');
     $rep->addJSLink($bp . 'js/bootstrapErrorDecoratorHtml.js');
     // Add botom dock js
     $rep->addJSLink($bp . 'js/bottom-dock.js');
     // Pass some configuration options to the web page through javascript var
     $lizUrls = array("params" => array('repository' => $repository, 'project' => $project), "config" => jUrl::get('lizmap~service:getProjectConfig'), "wms" => jUrl::get('lizmap~service:index'), "media" => jUrl::get('view~media:getMedia'), "nominatim" => jUrl::get('lizmap~osm:nominatim'), "ign" => jUrl::get('lizmap~ign:address'), "edition" => jUrl::get('lizmap~edition:getFeature'), "permalink" => jUrl::getFull('view~map:index'), "dataTableLanguage" => $bp . 'js/dataTables/' . jApp::config()->locale . '.json', "basepath" => $bp, "geobookmark" => jUrl::get('lizmap~geobookmark:index'));
     // Get optionnal WMS public url list
     $lser = lizmap::getServices();
     if ($lser->wmsPublicUrlList) {
         $publicUrlList = $lser->wmsPublicUrlList;
         function f($x)
         {
             return jUrl::getFull('lizmap~service:index', array(), 0, trim($x));
         }
         $pul = array_map('f', explode(',', $publicUrlList));
         $lizUrls['publicUrlList'] = $pul;
     }
     if (jAcl2::check('lizmap.admin.repositories.delete')) {
         $lizUrls['removeCache'] = jUrl::get('admin~config:removeLayerCache');
     }
     $rep->addJSCode("var lizUrls = " . json_encode($lizUrls) . ";");
     $rep->addJSCode("var lizProj4 = " . json_encode($lproj->getAllProj4()) . ";");
     $rep->addStyle('#map', 'background-color:' . $lproj->getCanvasColor() . ';');
     // Get the WMS information
     $wmsInfo = $lproj->getWMSInformation();
     // Set page title from projet title
     if ($wmsInfo['WMSServiceTitle'] != '') {
         $rep->title = $wmsInfo['WMSServiceTitle'];
     } else {
         $rep->title = $repository . ' - ' . $project;
     }
     // Add date.js for timemanager
     if ($lproj->hasTimemanagerLayers()) {
         $rep->addJSLink($bp . 'js/date.js');
     }
     // Assign variables to template
     $assign = array_merge(array('repositoryLabel' => $lrep->getData('label'), 'repository' => $lrep->getKey(), 'project' => $project, 'onlyMaps' => $lser->onlyMaps), $wmsInfo);
     // WMS GetCapabilities Url
     $wmsGetCapabilitiesUrl = jAcl2::check('lizmap.tools.displayGetCapabilitiesLinks', $lrep->getKey());
     if ($wmsGetCapabilitiesUrl) {
         $wmsGetCapabilitiesUrl = $lproj->getData('wmsGetCapabilitiesUrl');
     }
     $assign['wmsGetCapabilitiesUrl'] = $wmsGetCapabilitiesUrl;
     // Get dockable and minidockable element
     $assign['dockable'] = $lproj->getDefaultDockable();
     $items = jEvent::notify('mapDockable', array('repository' => $repository, 'project' => $project))->getResponse();
     $assign['dockable'] = mapDockItemsMerge($assign['dockable'], $items);
     $assign['minidockable'] = $lproj->getDefaultMiniDockable();
     $items = jEvent::notify('mapMiniDockable', array('repository' => $repository, 'project' => $project))->getResponse();
     $assign['minidockable'] = mapDockItemsMerge($assign['minidockable'], $items);
     $assign['bottomdockable'] = $lproj->getDefaultBottomDockable();
     $items = jEvent::notify('mapBottomDockable', array('repository' => $repository, 'project' => $project))->getResponse();
     $assign['bottomdockable'] = mapDockItemsMerge($assign['bottomdockable'], $items);
     // Add dockable js
     foreach (array_merge($assign['dockable'], $assign['minidockable'], $assign['bottomdockable']) as $d) {
         if ($d->js != '') {
             $rep->addJsLink($d->js);
         }
     }
     $themePath = jApp::config()->urlengine['basePath'] . 'themes/' . jApp::config()->theme . '/';
     $rep->addCssLink($themePath . 'css/main.css');
     $rep->addCssLink($themePath . 'css/map.css');
     $rep->addCssLink($themePath . 'css/media.css');
     // Add dockable css
     foreach ($assign['dockable'] as $d) {
         if ($d->css != '') {
             $rep->addCssLink($d->css);
         }
     }
     // Replace default theme by theme found in
     // the repository folder media/themes/default/
     if ($lrep->getData('allowUserDefinedThemes')) {
         $repositoryPath = $lrep->getPath();
         $cssArray = array('main', 'map', 'media');
         $themeArray = array('default', $project);
         foreach ($cssArray as $k) {
             foreach ($themeArray as $theme) {
                 $cssRelPath = 'media/themes/' . $theme . '/css/' . $k . '.css';
                 $cssPath = $lrep->getPath() . '/' . $cssRelPath;
                 if (file_exists($cssPath)) {
                     $cssUrl = jUrl::get('view~media:getCssFile', array('repository' => $lrep->getKey(), 'project' => $project, 'path' => $cssRelPath));
                     //~ $rep->addCssLink( $cssUrl );
                     // Use addHeadContent and not addCssLink to be sure it will be loaded after minified code
                     $rep->addHeadContent('<link type="text/css" href="' . $cssUrl . '" rel="stylesheet" />');
                 }
             }
         }
         // Add JS files found in media/js
         $jsDirArray = array('default', $project);
         foreach ($jsDirArray as $dir) {
             $jsPathRoot = realpath($repositoryPath . '/' . 'media/js/' . $dir);
             if (is_dir($jsPathRoot)) {
                 foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($jsPathRoot)) as $filename) {
                     $path_parts = pathinfo($filename);
                     if ($path_parts['extension'] == 'js') {
                         $jsPath = realpath($filename);
                         $jsRelPath = 'media/js/' . $dir . str_replace($jsPathRoot, '', $jsPath);
                         $jsUrl = jUrl::get('view~media:getMedia', array('repository' => $lrep->getKey(), 'project' => $project, 'path' => $jsRelPath));
                         //~ $rep->addJSLink( $jsUrl );
                         // Use addHeadContent and not addJSLink to be sure it will be loaded after minified code
                         $rep->addContent('<script type="text/javascript" src="' . $jsUrl . '" ></script>');
                     }
                 }
             }
         }
     }
     // optionnally hide some tools
     // header
     $jsCode = '';
     $mapMenuCss = '';
     $h = $this->intParam('h', 1);
     if ($h == 0 or property_exists($pOptions, 'hideHeader') && $pOptions->hideHeader == 'True') {
         $h = 0;
         $rep->addStyle('#body', 'padding-top:0px;');
         $rep->addStyle('#header', 'display:none; height:0px;');
     }
     // menu = left vertical menu with icons
     $m = $this->intParam('m', 1);
     if ($m == 0 or property_exists($pOptions, 'hideMenu') && $pOptions->hideMenu == 'True') {
         $m = 0;
         $rep->addStyle('#mapmenu', 'display:none !important; width:0px;');
         $rep->addStyle('#dock', 'left:0px; border-left:none;');
     }
     // legend = legend open at startup
     $l = $this->intParam('l', 1);
     if ($l == 0 or property_exists($pOptions, 'hideLegend') && $pOptions->hideLegend == 'True') {
         $l = 0;
         //~ $rep->addStyle('#dock', 'display:none;');
         $jsCode .= "\n      \$( document ).ready( function() {\n        lizMap.events.on({\n          'uicreated':function(evt){\n            \$('#button-switcher').click();\n          }\n        });\n      });\n      ";
     }
     // navbar
     $n = $this->intParam('n', 1);
     if ($n == 0 or property_exists($pOptions, 'hideNavbar') && $pOptions->hideNavbar == 'True') {
         $rep->addStyle('#navbar', 'display:none !important;');
     }
     // overview-box = scale & overview
     $o = $this->intParam('o', 1);
     if ($o == 0 or property_exists($pOptions, 'hideOverview') && $pOptions->hideOverview == 'True') {
         $rep->addStyle('#overview-box', 'display:none !important;');
     }
     // Apply interface modifications
     if ($jsCode != '') {
         $rep->addJSCode($jsCode);
     }
     // Hide groups checkboxes
     if (property_exists($pOptions, 'hideGroupCheckbox') && $pOptions->hideGroupCheckbox == 'True') {
         $rep->addStyle('#switcher-layers button[name="group"]', 'display:none !important;');
     }
     // Add filter
     $filterParam = $this->param('filter');
     $filter = array();
     if ($filterParam) {
         $fExp = explode(';', $filterParam);
         foreach ($fExp as $item) {
             $iExp = explode(':', $item);
             if (count($iExp) == 2) {
                 $filter[$iExp[0]] = $iExp[1];
             }
         }
         if (count($filter) > 0) {
             $rep->addJSCode("var lizLayerFilter = " . json_encode($filter) . ";");
         }
     }
     // Add styles if needed
     $stylesParam = $this->param('layerStyles');
     $styles = array();
     if ($stylesParam) {
         $fExp = explode(';', $stylesParam);
         foreach ($fExp as $item) {
             $iExp = explode(':', $item);
             if (count($iExp) == 2) {
                 $styles[$iExp[0]] = $iExp[1];
             }
         }
         if (count($styles) > 0) {
             $rep->addJSCode("var lizLayerStyles = " . json_encode($styles) . ";");
         }
     }
     //$assign['auth_url_return'] = jUrl::get('view~default:index');
     // switcher-layers-actions javascript
     $rep->addJSLink($bp . 'js/switcher-layers-actions.js');
     $rep->body->assign($assign);
     // Log
     $eventParams = array('key' => 'viewmap', 'content' => '', 'repository' => $lrep->getKey(), 'project' => $project);
     jEvent::notify('LizLogItem', $eventParams);
     return $rep;
 }
 /**
  * Query a QuickFinder database
  * @param text $query A query on OpenStreetMap object
  * @param text $bbox A bounding box in EPSG:4326 Optionnal
  * @return GeoJSON.
  */
 function get()
 {
     $rep = $this->getResponse('binary');
     $rep->outputFileName = 'search_results.json';
     $rep->mimeType = 'application/json';
     $content = '[]';
     $rep->content = $content;
     // Get project and repository, and check rights
     $project = $this->param('project');
     $repository = $this->param('repository');
     $lrep = lizmap::getRepository($repository);
     $lproj = null;
     try {
         $lproj = lizmap::getProject($repository . '~' . $project);
         if (!$lproj) {
             jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'ProjectNotDefined');
             return $rep;
         }
     } catch (UnknownLizmapProjectException $e) {
         jLog::logEx($e, 'error');
         jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'ProjectNotDefined');
         return $rep;
     }
     if (!$lproj->checkAcl()) {
         jMessage::add(jLocale::get('view~default.repository.access.denied'), 'AuthorizationRequired');
         return $rep;
     }
     // Parameters
     $pquery = $this->param('query');
     if (!$pquery) {
         return $rep;
     }
     $pquery = filter_var($pquery, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
     // Get FTS searches
     $ftsSearches = $lproj->hasFtsSearches();
     if (!$ftsSearches) {
         return $rep;
     }
     $searches = $ftsSearches['searches'];
     $jdb_profile = $ftsSearches['jdb_profile'];
     // Limitations
     $limit_tot = 30;
     $limit_search = 10;
     // Create FTS query
     $words = explode(' ', $pquery);
     $matches = implode('* ', $words) . '*';
     $sql = "\n    SELECT search_id,content,wkb_geom\n    FROM quickfinder_data\n    WHERE content MATCH\n    ";
     $cnx = jDb::getConnection($jdb_profile);
     $sql .= " " . $cnx->quote($matches);
     $limit = max(count($searches) * $limit_search, $limit_tot);
     $sql .= " LIMIT " . $limit;
     //jLog::log($sql);
     // Run query
     $res = $cnx->query($sql);
     $data = array();
     $nb = array('search' => array(), 'tot' => 0);
     foreach ($res as $item) {
         $key = $item->search_id;
         if (!array_key_exists($key, $nb['search'])) {
             $nb['search'][$key] = 0;
         }
         if ($nb['search'][$key] >= $limit_search) {
             continue;
         }
         if ($nb['tot'] >= $limit_tot) {
             break;
         }
         if (!array_key_exists($key, $data)) {
             $data[$key] = array();
         }
         $data[$key]['search_name'] = $searches[$key]['search_name'];
         $data[$key]['layer_name'] = $searches[$key]['layer_name'];
         $data[$key]['srid'] = $searches[$key]['srid'];
         if (!array_key_exists('features', $data[$key])) {
             $data[$key]['features'] = array();
         }
         $data[$key]['features'][] = array('label' => $item->content, 'geometry' => $item->wkb_geom);
         $nb['search'][$key] += 1;
         $nb['tot'] += 1;
     }
     $rep->content = json_encode($data);
     return $rep;
 }
 /**
  * Empty a map service cache
  * @param string $repository Repository for which to remove all tile cache
  * @return Redirection to the index
  */
 function removeLayerCache()
 {
     // Create response to redirect to the index
     $rep = $this->getResponse("redirect");
     $rep->action = "admin~config:index";
     $repository = $this->param('repository');
     $lrep = lizmap::getRepository($repository);
     if (!$lrep) {
         jMessage::add('The repository ' . strtoupper($repository) . ' does not exist !', 'error');
         return $rep;
     }
     $project = $this->param('project');
     try {
         $lproj = lizmap::getProject($lrep->getKey() . '~' . $project);
         if (!$lproj) {
             jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'error');
             return $rep;
         }
         $layer = $this->param('layer');
         // Remove project cache
         $lproj->clearCache();
         // Remove the cache for the layer
         lizmapProxy::clearLayerCache($repository, $project, $layer);
         jMessage::add(jLocale::get("admin~admin.cache.layer.removed", array($layer)));
         return $rep;
     } catch (UnknownLizmapProjectException $e) {
         jLog::logEx($e, 'error');
         jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'error');
         return $rep;
     }
     return $rep;
 }
 /**
  * Empty a map service cache
  * @param string $repository Repository for which to remove all tile cache
  * @return Redirection to the index
  */
 function removeLayerCache()
 {
     $repository = $this->param('repository');
     $project = $this->param('project');
     $layer = $this->param('layer');
     // Get config utility
     $lrep = lizmap::getRepository($repository);
     $ser = lizmap::getServices();
     $lproj = lizmap::getProject($repository . '~' . $project);
     $project = $lproj->getKey();
     // Remove the cache for the layer
     $cacheRootDirectory = $ser->cacheRootDirectory;
     $cacheProjectDir = $cacheRootDirectory . '/' . $lrep->getKey() . '/' . $project . '/';
     $handle = opendir($cacheProjectDir);
     $results = array();
     // Open the directory and walk through the filenames
     while (false !== ($entry = readdir($handle))) {
         if ($entry != "." && $entry != "..") {
             // Get directories and files corresponding to the layer
             if (preg_match('#^' . $layer . '_#', $entry) or $entry == $layer) {
                 $results[] = $cacheProjectDir . $entry;
             }
         }
     }
     closedir($handle);
     // Remove layer files and folder cache
     if ($lrep && $lproj) {
         foreach ($results as $rem) {
             if (is_dir($rem)) {
                 jFile::removeDir($rem);
             } else {
                 unlink($rem);
             }
         }
     }
     jMessage::add(jLocale::get("admin~admin.cache.layer.removed", array($layer)));
     // Redirect to the index
     $rep = $this->getResponse("redirect");
     $rep->action = "admin~config:index";
     return $rep;
 }
 /**
  * Get a CSS file stored in the repository in a "media/themes" folder.
  * Url to images are replaced by getMedia URL
  *
  * @param string $repository Repository of the project.
  * @param string $project Project key.
  * @param string $path Path to the CSS file relative to the project file.
  * @return binary object The transformed CSS file.
  */
 function getCssFile()
 {
     // Get repository data
     $repository = $this->param('repository');
     $lrep = lizmap::getRepository($repository);
     if (!$lrep or !jAcl2::check('lizmap.repositories.view', $lrep->getKey())) {
         $this->error(jLocale::get('view~default.repository.access.denied'));
     }
     // Get the project
     $project = $this->param('project');
     // Get lizmapProject class
     $lproj = lizmap::getProject($lrep->getKey() . '~' . $project);
     if (!$lproj) {
         $this->error('The lizmapProject ' . strtoupper($project) . ' does not exist !');
     }
     // Redirect if no right to access the project
     if (!$lproj->checkAcl()) {
         return $this->error(jLocale::get('view~default.repository.access.denied'));
     }
     // Get the file
     $path = $this->param('path');
     $repositoryPath = realpath($lrep->getPath());
     $abspath = realpath($repositoryPath . '/' . $path);
     $n_repositoryPath = str_replace('\\', '/', $repositoryPath);
     $n_abspath = str_replace('\\', '/', $abspath);
     $ok = True;
     // Only allow files within the repository for safety reasons
     // and in the media/themes/ folder
     if (!preg_match("#^" . $n_repositoryPath . "(/)?media/themes/#", $n_abspath)) {
         $ok = False;
     }
     // Check if file exists
     if ($ok and !file_exists($abspath)) {
         $ok = False;
     }
     // Check if file is CSS
     $path_parts = pathinfo($abspath);
     if (!isset($path_parts['extension']) || strtolower($path_parts['extension']) != 'css') {
         $ok = False;
     }
     // Redirect if errors
     if (!$ok) {
         $content = "No CSS file in the specified path";
         $rep = $this->getResponse('text');
         $rep->content = $content;
         return $rep;
     }
     // Prepare the file to return
     $rep = $this->getResponse('binary');
     $rep->doDownload = false;
     $rep->fileName = $abspath;
     // Get the name of the file
     $name = $path_parts['basename'] . '.' . $path_parts['extension'];
     $rep->outputFileName = $name;
     // Mime type
     $rep->mimeType = 'text/css';
     // Read content from file
     $content = jFile::read($abspath);
     // Replace relative images URL with getMedia URL
     $newPath = preg_replace("#" . $path_parts['basename'] . "\$#", '', $path);
     $baseUrl = jUrl::get('view~media:getMedia', array('repository' => $lrep->getKey(), 'project' => $project, 'path' => $newPath));
     $pattern = 'url\\((.+)\\)';
     $replacement = 'url(' . $baseUrl . '/\\1)';
     $content = preg_replace("#{$pattern}#", $replacement, $content);
     $content = str_replace('"', '', $content);
     $rep->content = $content;
     $rep->setExpires('+60 seconds');
     return $rep;
 }
 /**
  * Get data from map service or from the cache.
  * @param string $repository The repository.
  * @param string $project The project.
  * @param array $params Array of parameters.
  * @return array $data Normalized and filtered array.
  */
 public static function getServiceData($repository, $project, $params)
 {
     // Get cache if exists
     $keyParams = $params;
     if (array_key_exists('map', $keyParams)) {
         unset($keyParams['map']);
     }
     ksort($keyParams);
     $key = md5(serialize($keyParams));
     $layers = str_replace(',', '_', $params['layers']);
     $crs = preg_replace('#[^a-zA-Z0-9_]#', '_', $params['crs']);
     // Get repository data
     $ser = lizmap::getServices();
     $lrep = lizmap::getRepository($repository);
     $lproj = lizmap::getProject($repository . '~' . $project);
     // Change to true to put some information in debug files
     $debug = $ser->debugMode;
     // Read config file for the current project
     $layername = $params["layers"];
     $configLayers = $lproj->getLayers();
     $configLayer = null;
     if (property_exists($configLayers, $layername)) {
         $configLayers->{$layername};
     }
     // Set or get tile from the parent project in case of embedded layers
     if ($configLayer and property_exists($configLayer, 'sourceRepository') and property_exists($configLayer, 'sourceProject')) {
         $newRepository = (string) $configLayer->sourceRepository;
         $newProject = (string) $configLayer->sourceProject;
         $repository = $newRepository;
         $project = $newProject;
         $lrep = lizmap::getRepository($repository);
         $lproj = lizmap::getProject($repository . '~' . $project);
     }
     // Get tile cache virtual profile (tile storage)
     // And get tile if already in cache
     // --> must be done after checking that parent project is involved
     $profile = 'lizmapCache_' . $repository . '_' . $project . '_' . $layers . '_' . $crs;
     lizmapCache::createVirtualProfile($repository, $project, $layers, $crs);
     $tile = jCache::get($key, $profile);
     // Return tile if cache hit !
     if ($tile) {
         //~ jLog::log( 'cache hit !');
         return $tile;
     }
     // Has the user asked for cache for this layer ?
     $string2bool = array('false' => False, 'False' => False, 'True' => True, 'true' => True);
     $useCache = False;
     if ($configLayer) {
         $string2bool[$configLayer->cached];
     }
     // Avoid using cache for requests concerning not square tiles or too big
     // Focus on real web square tiles
     $wmsClient = 'web';
     if ($useCache and $params['width'] != $params['height'] and ($params['width'] > 300 or $params['height'] > 300)) {
         $wmsClient = 'gis';
         $useCache = False;
     }
     // ***************************
     // No cache hit : need to ask the tile from QGIS Server
     // ***************************
     // Construction of the WMS url : base url + parameters
     $url = $ser->wmsServerURL . '?';
     // Add project path into map parameter
     $params["map"] = realpath($lrep->getPath()) . '/' . $lproj->getKey() . ".qgs";
     // Metatile : if needed, change the bbox
     // Avoid metatiling when the cache is not active for the layer
     $metatileSize = '1,1';
     if ($configLayer and property_exists($configLayer, 'metatileSize')) {
         if (preg_match('#^[3579],[3579]$#', $configLayer->metatileSize)) {
             $metatileSize = $configLayer->metatileSize;
         }
     }
     # Metatile buffer
     $metatileBuffer = 5;
     // Also checks if gd is installed
     if ($metatileSize and $useCache and $wmsClient == 'web' and extension_loaded('gd') && function_exists('gd_info')) {
         # Metatile Size
         $metatileSizeExp = explode(',', $metatileSize);
         $metatileSizeX = (int) $metatileSizeExp[0];
         $metatileSizeY = (int) $metatileSizeExp[1];
         # Get requested bbox
         $bboxExp = explode(',', $params['bbox']);
         $width = $bboxExp[2] - $bboxExp[0];
         $height = $bboxExp[3] - $bboxExp[1];
         # Calculate factors
         $xFactor = (int) ($metatileSizeX / 2);
         $yFactor = (int) ($metatileSizeY / 2);
         # Calculate the new bbox
         $xmin = $bboxExp[0] - $xFactor * $width - $metatileBuffer * $width / $params["width"];
         $ymin = $bboxExp[1] - $yFactor * $height - $metatileBuffer * $height / $params["height"];
         $xmax = $bboxExp[2] + $xFactor * $width + $metatileBuffer * $width / $params["width"];
         $ymax = $bboxExp[3] + $yFactor * $height + $metatileBuffer * $height / $params["height"];
         # Replace request bbox by metatile bbox
         $params["bbox"] = "{$xmin},{$ymin},{$xmax},{$ymax}";
         # Keep original param value
         $originalParams = array("width" => $params["width"], "height" => $params["height"]);
         # Replace width and height before requesting the image from qgis
         $params["width"] = $metatileSizeX * $params["width"] + 2 * $metatileBuffer;
         $params["height"] = $metatileSizeY * $params["height"] + 2 * $metatileBuffer;
     }
     // Build params before send the request to Qgis
     $builtParams = http_build_query($params);
     // Replace needed characters (not needed for php >= 5.4, just use the 4th parameter of the method http_build_query)
     $a = array('+', '_', '.', '-');
     $b = array('%20', '%5F', '%2E', '%2D');
     $builtParams = str_replace($a, $b, $builtParams);
     // Get data from the map server
     $proxyMethod = $ser->proxyMethod;
     $getRemoteData = lizmapCache::getRemoteData($url . $builtParams, $proxyMethod, $debug);
     $data = $getRemoteData[0];
     $mime = $getRemoteData[1];
     if ($useCache && !preg_match('/^image/', $mime)) {
         $useCache = False;
     }
     // Metatile : if needed, crop the metatile into a single tile
     // Also checks if gd is installed
     if ($metatileSize and $useCache and $wmsClient == 'web' and extension_loaded('gd') && function_exists('gd_info')) {
         # Save original content into an image var
         $original = imagecreatefromstring($data);
         # crop parameters
         $newWidth = (int) $originalParams["width"];
         // px
         $newHeight = (int) $originalParams["height"];
         // px
         $positionX = (int) ($xFactor * $originalParams["width"]) + $metatileBuffer;
         // left translation of 30px
         $positionY = (int) ($yFactor * $originalParams["height"]) + $metatileBuffer;
         // top translation of 20px
         # create new gd image
         $image = imageCreateTrueColor($newWidth, $newHeight);
         # save transparency if needed
         if (preg_match('#png#', $params['format'])) {
             imagesavealpha($original, true);
             imagealphablending($image, false);
             $color = imagecolortransparent($image, imagecolorallocatealpha($image, 0, 0, 0, 127));
             imagefill($image, 0, 0, $color);
             imagesavealpha($image, true);
         }
         # crop image
         imagecopyresampled($image, $original, 0, 0, $positionX, $positionY, $newWidth, $newHeight, $newWidth, $newHeight);
         # Output the image as a string (use PHP buffering)
         ob_start();
         if (preg_match('#png#', $params['format'])) {
             imagepng($image, null);
         } else {
             imagejpeg($image, null, 80);
         }
         $data = ob_get_contents();
         // read from buffer
         ob_end_clean();
         // delete buffer
         // Destroy image handlers
         imagedestroy($original);
         imagedestroy($image);
     }
     // Store into cache if needed
     if ($useCache) {
         //~ jLog::log( ' Store into cache');
         $cacheExpiration = (int) $ser->cacheExpiration;
         if (property_exists($configLayer, 'cacheExpiration')) {
             $cacheExpiration = (int) $configLayer->cacheExpiration;
         }
         jCache::set($key, $data, $cacheExpiration, $profile);
     }
     return $data;
 }
 /**
  * Query a QuickFinder database
  * @param text $query A query on OpenStreetMap object
  * @param text $bbox A bounding box in EPSG:4326 Optionnal
  * @return GeoJSON.
  */
 function get()
 {
     $rep = $this->getResponse('binary');
     $rep->outputFileName = 'search_results.json';
     $rep->mimeType = 'application/json';
     $content = '[]';
     $rep->content = $content;
     // Get project and repository, and check rights
     $project = $this->param('project');
     $repository = $this->param('repository');
     $lrep = lizmap::getRepository($repository);
     $lproj = null;
     try {
         $lproj = lizmap::getProject($repository . '~' . $project);
         if (!$lproj) {
             jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'ProjectNotDefined');
             return $rep;
         }
     } catch (UnknownLizmapProjectException $e) {
         jLog::logEx($e, 'error');
         jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'ProjectNotDefined');
         return $rep;
     }
     if (!$lproj->checkAcl()) {
         jMessage::add(jLocale::get('view~default.repository.access.denied'), 'AuthorizationRequired');
         return $rep;
     }
     // Parameters
     $pquery = $this->param('query');
     if (!$pquery) {
         return $rep;
     }
     $pquery = filter_var($pquery, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
     // Get FTS searches
     $ftsSearches = $lproj->hasFtsSearches();
     if (!$ftsSearches) {
         return $rep;
     }
     $searches = $ftsSearches['searches'];
     $jdb_profile = $ftsSearches['jdb_profile'];
     // Limitations
     $limit_tot = 30;
     $limit_search = 15;
     $cnx = jDb::getConnection($jdb_profile);
     // Create FTS query
     $words = explode(' ', $pquery);
     $matches = implode('* ', $words) . '*';
     $sql = "SELECT search_id,content,wkb_geom FROM quickfinder_data WHERE";
     $sql .= " content MATCH " . $cnx->quote($matches);
     // Add filter by groups and user if the user is authenticated
     if (!jAcl2::check('lizmap.tools.loginFilteredLayers.override', $lrep->getKey())) {
         $sql .= " AND ( content LIKE '%@@all' OR content NOT LIKE '%@@%'";
         $isConnected = jAuth::isConnected();
         if ($isConnected) {
             // Ok if any group matches
             $userGroups = jAcl2DbUserGroup::getGroups();
             foreach ($userGroups as $g) {
                 $sql .= " OR content LIKE '%@@" . $g . "'";
             }
             // Ok if user matches
             $user = jAuth::getUserSession();
             $login = $user->login;
             $sql .= " OR content LIKE '%@@" . $login . "'";
         }
         $sql .= ' )';
     }
     // Query and format data for each search key
     $nb = array('search' => array(), 'tot' => 0);
     $data = array();
     foreach ($searches as $skey => $sval) {
         // Add filter to get only data for given search key
         $sql_search = $sql . ' AND search_id = ' . $cnx->quote($skey);
         $limit = $limit_search;
         $sql_search .= " LIMIT " . $limit;
         //jLog::log($sql_search);
         // Run query
         $res = $cnx->query($sql_search);
         // Format data
         foreach ($res as $item) {
             $key = $item->search_id;
             if (!array_key_exists($key, $nb['search'])) {
                 $nb['search'][$key] = 0;
             }
             if ($nb['search'][$key] >= $limit_search) {
                 continue;
             }
             if ($nb['tot'] >= $limit_tot) {
                 break;
             }
             if (!array_key_exists($key, $data)) {
                 $data[$key] = array();
             }
             $data[$key]['search_name'] = $searches[$key]['search_name'];
             $data[$key]['layer_name'] = $searches[$key]['layer_name'];
             $data[$key]['srid'] = $searches[$key]['srid'];
             if (!array_key_exists('features', $data[$key])) {
                 $data[$key]['features'] = array();
             }
             $data[$key]['features'][] = array('label' => preg_replace('#@@.+#', '', $item->content), 'geometry' => $item->wkb_geom);
             $nb['search'][$key] += 1;
             $nb['tot'] += 1;
         }
     }
     $rep->content = json_encode($data);
     return $rep;
 }
 /**
  * Displays map for ajax request.
  *
  * @param string $repository. Name of the repository.
  * @param string $project. Name of the project.
  * @return Html fragment with a list of projects.
  */
 function map()
 {
     $rep = $this->getResponse('htmlfragment');
     // Get the project
     $project = filter_var($this->param('project'), FILTER_SANITIZE_STRING);
     // Get repository data
     $repository = $this->param('repository');
     // Get lizmapRepository class
     // if repository not found get the default
     $lrep = null;
     if (!$repository) {
         $lser = lizmap::getServices();
         $lrep = lizmap::getRepository($lser->defaultRepository);
     } else {
         $lrep = lizmap::getRepository($repository);
     }
     if (!jAcl2::check('lizmap.repositories.view', $lrep->getKey())) {
         jMessage::add(jLocale::get('view~default.repository.access.denied'), 'error');
         return $rep;
     }
     // We must redirect to default repository project list if no project given
     if (!$project) {
         jMessage::add('The parameter project is mandatory !', 'error');
         return $rep;
     }
     // Get lizmapProject class
     $lproj = lizmap::getProject($lrep->getKey() . '~' . $project);
     if (!$lproj) {
         jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'error');
         return $rep;
     }
     $lizUrls = array("params" => array('repository' => $repository, 'project' => $project), "config" => jUrl::getFull('lizmap~service:getProjectConfig'), "wms" => jUrl::getFull('lizmap~service:index'), "media" => jUrl::getFull('view~media:getMedia'), "nominatim" => jUrl::getFull('lizmap~osm:nominatim'), "edition" => jUrl::getFull('lizmap~edition:getFeature'), "permalink" => jUrl::getFull('view~map:index'));
     // Get optionnal WMS public url list
     $lser = lizmap::getServices();
     if ($lser->wmsPublicUrlList) {
         $publicUrlList = $lser->wmsPublicUrlList;
         function f($x)
         {
             return jUrl::getFull('lizmap~service:index', array(), 0, trim($x));
         }
         $pul = array_map('f', explode(',', $publicUrlList));
         $lizUrls['publicUrlList'] = $pul;
     }
     if (jAcl2::check('lizmap.admin.repositories.delete')) {
         $lizUrls['removeCache'] = jUrl::getFull('admin~config:removeLayerCache');
     }
     $content = '<script type="text/javascript" src="' . jUrl::getFull('view~translate:index') . '"/>' . "\n";
     $content .= '<script type="text/javascript">// <![CDATA[' . "\n";
     $content .= "var lizUrls = " . json_encode($lizUrls) . ";\n";
     $content .= 'var lizPosition = {"lon":null, "lat":null, "zoom":null};' . "\n";
     $content .= "\$('#map').css('background-color','" . $lproj->getCanvasColor() . "');\n";
     $content .= "// ]]></script>";
     // Get the WMS information
     $wmsInfo = $lproj->getWMSInformation();
     // Set page title from projet title
     if ($wmsInfo['WMSServiceTitle'] != '') {
         $rep->title = $wmsInfo['WMSServiceTitle'];
     } else {
         $rep->title = $repository . ' - ' . $project;
     }
     $assign = array_merge(array('repositoryLabel' => $lrep->getData('label'), 'repository' => $lrep->getKey(), 'project' => $project), $wmsInfo);
     $tpl = new jTpl();
     $tpl->assign($assign);
     $content .= $tpl->fetch('view~map');
     $rep->addContent($content);
     return $rep;
 }
 function GetTile()
 {
     jClasses::inc('lizmap~lizmapWMTSRequest');
     $wmsRequest = new lizmapWMTSRequest($this->project, $this->params);
     $result = $wmsRequest->process();
     $rep = $this->getResponse('binary');
     $rep->mimeType = $result->mime;
     $rep->content = $result->data;
     $rep->doDownload = false;
     $rep->outputFileName = 'qgis_server_wmts_tile_' . $this->repository->getKey() . '_' . $this->project->getKey();
     $rep->setHttpStatus($result->code, '');
     if (!preg_match('/^image/', $result->mime)) {
         return $rep;
     }
     // HTTP browser cache expiration time
     $layername = $this->params["layer"];
     $lproj = lizmap::getProject($this->repository->getKey() . '~' . $this->project->getKey());
     $configLayers = $lproj->getLayers();
     if (property_exists($configLayers, $layername)) {
         $configLayer = $configLayers->{$layername};
         if (property_exists($configLayer, 'clientCacheExpiration')) {
             $clientCacheExpiration = (int) $configLayer->clientCacheExpiration;
             $rep->setExpires("+" . $clientCacheExpiration . " seconds");
         }
     }
     return $rep;
 }
 /**
  * Get data from map service or from the cache.
  * @param lizmapProject $project The project.
  * @param array $params Array of parameters.
  * @return array $data Normalized and filtered array.
  */
 public static function getMap($project, $params, $forced = False)
 {
     // Get cache if exists
     $keyParams = array();
     foreach ($params as $pk => $value) {
         if (in_array($pk, array("bbox", "format", "height", "layers", "transparent"))) {
             $keyParams[$pk] = $value;
         }
     }
     ksort($keyParams);
     $layers = str_replace(',', '_', $params['layers']);
     $crs = preg_replace('#[^a-zA-Z0-9_]#', '_', $params['crs']);
     // Get repository data
     $ser = lizmap::getServices();
     $lrep = $project->getRepository();
     $lproj = $project;
     $project = $lproj->getKey();
     $repository = $lrep->getKey();
     // Change to true to put some information in debug files
     $debug = $ser->debugMode;
     // Read config file for the current project
     $layername = $params["layers"];
     $configLayers = $lproj->getLayers();
     $configLayer = null;
     if (property_exists($configLayers, $layername)) {
         $configLayer = $configLayers->{$layername};
     }
     // Set or get tile from the parent project in case of embedded layers
     if ($configLayer and property_exists($configLayer, 'sourceRepository') and property_exists($configLayer, 'sourceProject')) {
         $newRepository = (string) $configLayer->sourceRepository;
         $newProject = (string) $configLayer->sourceProject;
         $repository = $newRepository;
         $project = $newProject;
         $lrep = lizmap::getRepository($repository);
         if (!$lrep) {
             jMessage::add('The repository ' . strtoupper($repository) . ' does not exist !', 'RepositoryNotDefined');
             return array('error', 'text/plain', '404');
         }
         try {
             $lproj = lizmap::getProject($repository . '~' . $project);
             if (!$lproj) {
                 jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'ProjectNotDefined');
                 return array('error', 'text/plain', '404');
             }
         } catch (UnknownLizmapProjectException $e) {
             jLog::logEx($e, 'error');
             jMessage::add('The lizmapProject ' . strtoupper($project) . ' does not exist !', 'ProjectNotDefined');
             return array('error', 'text/plain', '404');
         }
     }
     $key = md5(serialize($keyParams));
     // Get tile cache virtual profile (tile storage)
     // And get tile if already in cache
     // --> must be done after checking that parent project is involved
     $profile = lizmapProxy::createVirtualProfile($repository, $project, $layers, $crs);
     if ($debug) {
         lizmap::logMetric('LIZMAP_PROXY_READ_LAYER_CONFIG');
     }
     // Has the user asked for cache for this layer ?
     $useCache = False;
     if ($configLayer) {
         $useCache = strtolower($configLayer->cached) == 'true';
     }
     // Avoid using cache for requests concerning not square tiles or too big
     // Focus on real web square tiles
     $wmsClient = 'web';
     if ($useCache and $params['width'] != $params['height'] and ($params['width'] > 300 or $params['height'] > 300)) {
         $wmsClient = 'gis';
         $useCache = False;
     }
     if ($useCache and !$forced) {
         $tile = jCache::get($key, $profile);
         if ($tile) {
             $_SESSION['LIZMAP_GETMAP_CACHE_STATUS'] = 'read';
             $mime = 'image/jpeg';
             if (preg_match('#png#', $params['format'])) {
                 $mime = 'image/png';
             }
             if ($debug) {
                 lizmap::logMetric('LIZMAP_PROXY_HIT_CACHE');
             }
             return array($tile, $mime, 200);
         }
     }
     // ***************************
     // No cache hit : need to ask the tile from QGIS Server
     // ***************************
     // Construction of the WMS url : base url + parameters
     $url = $ser->wmsServerURL . '?';
     // Add project path into map parameter
     $params["map"] = realpath($lproj->getQgisPath());
     // Metatile : if needed, change the bbox
     // Avoid metatiling when the cache is not active for the layer
     $metatileSize = '1,1';
     if ($configLayer and property_exists($configLayer, 'metatileSize')) {
         if (preg_match('#^[3579],[3579]$#', $configLayer->metatileSize)) {
             $metatileSize = $configLayer->metatileSize;
         }
     }
     # Metatile buffer
     $metatileBuffer = 5;
     // Also checks if gd is installed
     if ($metatileSize and $useCache and $wmsClient == 'web' and extension_loaded('gd') && function_exists('gd_info')) {
         # Metatile Size
         $metatileSizeExp = explode(',', $metatileSize);
         $metatileSizeX = (int) $metatileSizeExp[0];
         $metatileSizeY = (int) $metatileSizeExp[1];
         # Get requested bbox
         $bboxExp = explode(',', $params['bbox']);
         $width = $bboxExp[2] - $bboxExp[0];
         $height = $bboxExp[3] - $bboxExp[1];
         # Calculate factors
         $xFactor = (int) ($metatileSizeX / 2);
         $yFactor = (int) ($metatileSizeY / 2);
         # Calculate the new bbox
         $xmin = $bboxExp[0] - $xFactor * $width - $metatileBuffer * $width / $params["width"];
         $ymin = $bboxExp[1] - $yFactor * $height - $metatileBuffer * $height / $params["height"];
         $xmax = $bboxExp[2] + $xFactor * $width + $metatileBuffer * $width / $params["width"];
         $ymax = $bboxExp[3] + $yFactor * $height + $metatileBuffer * $height / $params["height"];
         # Replace request bbox by metatile bbox
         $params["bbox"] = "{$xmin},{$ymin},{$xmax},{$ymax}";
         # Keep original param value
         $originalParams = array("width" => $params["width"], "height" => $params["height"]);
         # Replace width and height before requesting the image from qgis
         $params["width"] = $metatileSizeX * $params["width"] + 2 * $metatileBuffer;
         $params["height"] = $metatileSizeY * $params["height"] + 2 * $metatileBuffer;
     }
     // Build params before send the request to Qgis
     $builtParams = http_build_query($params);
     // Replace needed characters (not needed for php >= 5.4, just use the 4th parameter of the method http_build_query)
     $a = array('+', '_', '.', '-');
     $b = array('%20', '%5F', '%2E', '%2D');
     $builtParams = str_replace($a, $b, $builtParams);
     // Get data from the map server
     $proxyMethod = $ser->proxyMethod;
     $getRemoteData = lizmapProxy::getRemoteData($url . $builtParams, $proxyMethod, $debug);
     $data = $getRemoteData[0];
     $mime = $getRemoteData[1];
     $code = $getRemoteData[2];
     if ($debug) {
         lizmap::logMetric('LIZMAP_PROXY_REQUEST_QGIS_MAP');
     }
     if ($useCache && !preg_match('/^image/', $mime)) {
         $useCache = False;
     }
     // Metatile : if needed, crop the metatile into a single tile
     // Also checks if gd is installed
     if ($metatileSize and $useCache and $wmsClient == 'web' and extension_loaded('gd') && function_exists('gd_info')) {
         # Save original content into an image var
         $original = imagecreatefromstring($data);
         # crop parameters
         $newWidth = (int) $originalParams["width"];
         // px
         $newHeight = (int) $originalParams["height"];
         // px
         $positionX = (int) ($xFactor * $originalParams["width"]) + $metatileBuffer;
         // left translation of 30px
         $positionY = (int) ($yFactor * $originalParams["height"]) + $metatileBuffer;
         // top translation of 20px
         # create new gd image
         $image = imageCreateTrueColor($newWidth, $newHeight);
         # save transparency if needed
         if (preg_match('#png#', $params['format'])) {
             imagesavealpha($original, true);
             imagealphablending($image, false);
             $color = imagecolortransparent($image, imagecolorallocatealpha($image, 0, 0, 0, 127));
             imagefill($image, 0, 0, $color);
             imagesavealpha($image, true);
         }
         # crop image
         imagecopyresampled($image, $original, 0, 0, $positionX, $positionY, $newWidth, $newHeight, $newWidth, $newHeight);
         # Output the image as a string (use PHP buffering)
         ob_start();
         if (preg_match('#png#', $params['format'])) {
             imagepng($image, null, 9);
         } else {
             imagejpeg($image, null, 90);
         }
         $data = ob_get_contents();
         // read from buffer
         ob_end_clean();
         // delete buffer
         // Destroy image handlers
         imagedestroy($original);
         imagedestroy($image);
         if ($debug) {
             lizmap::logMetric('LIZMAP_PROXY_CROP_METATILE');
         }
     }
     $_SESSION['LIZMAP_GETMAP_CACHE_STATUS'] = 'off';
     // Store into cache if needed
     if ($useCache) {
         //~ jLog::log( ' Store into cache');
         $cacheExpiration = (int) $ser->cacheExpiration;
         if (property_exists($configLayer, 'cacheExpiration')) {
             $cacheExpiration = (int) $configLayer->cacheExpiration;
         }
         jCache::set($key, $data, $cacheExpiration, $profile);
         $_SESSION['LIZMAP_GETMAP_CACHE_STATUS'] = 'write';
         if ($debug) {
             lizmap::logMetric('LIZMAP_PROXY_WRITE_CACHE');
         }
     }
     return array($data, $mime, $code);
 }
 /**
  * Query the IGN Geoportal API
  * @param text $query A query on IGN BD adresse object
  * @param text $bbox A bounding box in EPSG:4326
  * @return XML.
  */
 function address()
 {
     $rep = $this->getResponse('json');
     $rep->data = array();
     $query = $this->param('query');
     if (!$query) {
         return $rep;
     }
     // Get the project
     $project = filter_var($this->param('project'), FILTER_SANITIZE_STRING);
     if (!$project) {
         return $rep;
     }
     // Get repository data
     $repository = $this->param('repository');
     if (!$repository) {
         return $rep;
     }
     // Get the project object
     $lproj = lizmap::getProject($repository . '~' . $project);
     if (!$lproj) {
         return $rep;
     }
     $configOptions = $lproj->getOptions();
     if (!property_exists($configOptions, 'ignKey') || $configOptions->ignKey == '') {
         return $rep;
     }
     $url = 'http://gpp3-wxs.ign.fr/' . $configOptions->ignKey . '/geoportail/ols?';
     $xls = '<XLS xmlns:xls="http://www.opengis.net/xls" xmlns:gml="http://www.opengis.net/gml" xmlns="http://www.opengis.net/xls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="http://www.opengis.net/xls http://schemas.opengis.net/ols/1.2/olsAll.xsd">';
     $xls .= '<RequestHeader/><Request requestID="1" version="1.2" methodName="LocationUtilityService"><GeocodeRequest returnFreeForm="false"><Address countryCode="StreetAddress">';
     $xls .= '<freeFormAddress>' . $query . '</freeFormAddress>';
     $xls .= '</Address></GeocodeRequest></Request></XLS>';
     $params = array("xls" => $xls, "output" => 'xml');
     $url .= http_build_query($params);
     $curl_handle = curl_init();
     curl_setopt($curl_handle, CURLOPT_URL, $url);
     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));
     curl_setopt($curl_handle, CURLOPT_REFERER, jUrl::getFull('lizmap~ign:address'));
     $content = curl_exec($curl_handle);
     curl_close($curl_handle);
     $rep->content = $content;
     $content = str_replace('xmlns=', 'ns=', $content);
     $content = str_replace('gml:', '', $content);
     $xml = simplexml_load_string($content);
     $results = array();
     $GeocodedAddresses = $xml->xpath('//GeocodedAddress');
     foreach ($GeocodedAddresses as $GeocodedAddress) {
         $result = array();
         $address = array();
         /*
          * bug with gml:*
          */
         $Point = $GeocodedAddress->xpath('Point/pos');
         if (count($Point) != 0) {
             $Point = $Point[0];
             $point = explode(' ', (string) $Point);
             $result['point'] = array($point[1], $point[0]);
         }
         $Address = $GeocodedAddress->xpath('Address');
         if (count($Address) == 0) {
             continue;
         }
         $Address = $Address[0];
         $Building = $Address->xpath('StreetAddress/Building');
         if (count($Building) != 0) {
             $Building = $Building[0];
             $address['number'] = (string) $Building['number'];
         }
         $Street = $Address->xpath('StreetAddress/Street');
         if (count($Street) != 0) {
             $Street = $Street[0];
             $address['street'] = (string) $Street;
         }
         $Places = $Address->xpath('Place');
         foreach ($Places as $Place) {
             $PlaceType = (string) $Place['type'];
             if ($PlaceType == 'Municipality') {
                 $address['municipality'] = (string) $Place;
             } else {
                 if ($PlaceType == 'Departement') {
                     $address['departement'] = (string) $Place;
                 } else {
                     if ($PlaceType == 'Bbox') {
                         $result['bbox'] = explode(';', (string) $Place);
                     }
                 }
             }
         }
         $formatted_address = '';
         if (array_key_exists('number', $address)) {
             $formatted_address = $address['number'] . ' ';
         }
         if (array_key_exists('street', $address) && $address['street'] != '') {
             $formatted_address .= $address['street'] . ', ';
         }
         if (array_key_exists('municipality', $address)) {
             $formatted_address .= $address['municipality'] . ', ';
         }
         if (array_key_exists('departement', $address)) {
             $formatted_address .= $address['departement'];
         }
         $result['formatted_address'] = $formatted_address;
         $results[] = $result;
     }
     $rep->data = $results;
     return $rep;
 }
 /**
  *
  */
 function seeding()
 {
     $fakeServer = new jelix\FakeServerConf\ApacheMod(jApp::wwwPath(), '/index.php');
     $forced = $this->option('-f');
     $verbose = $this->option('-v');
     $rep = $this->getResponse();
     // cmdline response by default
     $project = null;
     try {
         $project = lizmap::getProject($this->param('repository') . '~' . $this->param('project'));
         // Project not found
         if (!$project) {
             $rep->addContent("The project has not be found!\n");
             return $rep;
         }
     } catch (UnknownLizmapProjectException $e) {
         $rep->addContent("The project has not be found!\n");
         return $rep;
     }
     $repository = $project->getRepository();
     $cacheId = $repository->getKey() . '_' . $project->getKey() . '_WMTS';
     $tileMatrixSetList = jCache::get($cacheId . '_tileMatrixSetList');
     if (!$tileMatrixSetList) {
         $request = new lizmapWMTSRequest($project, array('service' => 'WMTS', 'request' => 'GetCapabilities'));
         $result = $request->process();
         $tileMatrixSetList = jCache::get($cacheId . '_tileMatrixSetList');
     }
     $layers = $tileMatrixSetList = jCache::get($cacheId . '_layers');
     $layerIds = explode(',', $this->param('layers'));
     $selectedLayers = array();
     foreach ($layers as $l) {
         if (in_array('*', $layerIds) || in_array($l->name, $layerIds)) {
             $selectedLayers[] = $l;
         }
     }
     // Layer not found
     if (count($selectedLayers) == 0) {
         $rep->addContent("The layers '" . implode(',', $layerIds) . "' have not be found!\n");
         return $rep;
     }
     foreach ($selectedLayers as $layer) {
         $TileMatrixSetId = $this->param('TileMatrixSet');
         $tileMatrixSetLink = null;
         foreach ($layer->tileMatrixSetLinkList as $tms) {
             if ($tms->ref == $TileMatrixSetId) {
                 $tileMatrixSetLink = $tms;
                 break;
             }
         }
         // TileMatrixSet not found
         if (!$tileMatrixSetLink) {
             $rep->addContent("The TileMatrixSet '" . $TileMatrixSetId . "' has not be found!\n");
             continue;
         }
         $TileMatrixMin = (int) $this->param('TileMatrixMin');
         $TileMatrixMax = (int) $this->param('TileMatrixMax');
         // count tiles
         $tileCount = 0;
         foreach ($tileMatrixSetLink->tileMatrixLimits as $tileMatrixLimit) {
             if ($tileMatrixLimit->id >= $TileMatrixMin && $tileMatrixLimit->id <= $TileMatrixMax) {
                 $tmCount = ($tileMatrixLimit->maxRow - $tileMatrixLimit->minRow + 1) * ($tileMatrixLimit->maxCol - $tileMatrixLimit->minCol + 1);
                 if ($verbose) {
                     $rep->addContent($tmCount . ' tiles to generate for "' . $layer->name . '" "' . $TileMatrixSetId . '" "' . $tileMatrixLimit->id . '"' . "\n");
                 }
                 $tileCount += $tmCount;
             }
         }
         if ($verbose) {
             $rep->addContent($tileCount . ' tiles to generate for "' . $layer->name . '" "' . $TileMatrixSetId . '" between "' . $TileMatrixMin . '" and "' . $TileMatrixMax . '"' . "\n");
         }
         // generate tiles
         $rep->addContent("Start generation\n");
         $rep->addContent("================\n");
         $tileProgress = 0;
         $tileStepHeight = max(5.0, floor(5 * 100 / $tileCount));
         $tileStep = $tileStepHeight;
         foreach ($tileMatrixSetLink->tileMatrixLimits as $tileMatrixLimit) {
             if ($tileMatrixLimit->id >= $TileMatrixMin && $tileMatrixLimit->id <= $TileMatrixMax) {
                 $row = (int) $tileMatrixLimit->minRow;
                 //$rep->addContent( $tileMatrixLimit->id.' '.$tileMatrixLimit->minRow.' '.$tileMatrixLimit->maxRow.' '.$tileMatrixLimit->minCol.' '.$tileMatrixLimit->maxCol."\n");
                 while ($row <= $tileMatrixLimit->maxRow) {
                     $col = (int) $tileMatrixLimit->minCol;
                     while ($col <= $tileMatrixLimit->maxCol) {
                         $request = new lizmapWMTSRequest($project, array('service' => 'WMTS', 'version' => '1.0.0', 'request' => 'GetTile', 'layer' => $layer->name, 'format' => $layer->imageFormat, 'TileMatrixSet' => $TileMatrixSetId, 'TileMatrix' => $tileMatrixLimit->id, 'TileRow' => $row, 'TileCol' => $col));
                         if ($forced) {
                             $request->setForceRequest(True);
                         }
                         $result = $request->process();
                         //$rep->addContent($layer->name.' '.$layer->imageFormat.' '.$TileMatrixSetId.' '.$tileMatrixLimit->id.' '.$row.' '.$col.' '.$result->code."\n");
                         $col += 1;
                         $tileProgress += 1;
                         if ($verbose && $tileProgress * 100 / $tileCount >= $tileStep) {
                             $tileStep = floor($tileProgress * 100 / $tileCount);
                             $rep->addContent('Progression: ' . $tileStep . '%, ' . $tileProgress . ' tiles generated on ' . $tileCount . ' tiles' . "\n");
                             $tileStep = $tileStep + $tileStepHeight;
                         }
                     }
                     $row += 1;
                 }
             }
         }
         $rep->addContent("================\n");
         $rep->addContent("End generation\n");
     }
     return $rep;
 }
 public function getProjects()
 {
     $projects = array();
     $dir = $this->getPath();
     if (is_dir($dir)) {
         if ($dh = opendir($dir)) {
             $cfgFiles = array();
             $qgsFiles = array();
             while (($file = readdir($dh)) !== false) {
                 if (substr($file, -3) == 'cfg') {
                     $cfgFiles[] = $file;
                 }
                 if (substr($file, -3) == 'qgs') {
                     $qgsFiles[] = $file;
                 }
             }
             closedir($dh);
             foreach ($qgsFiles as $qgsFile) {
                 $proj = null;
                 if (in_array($qgsFile . '.cfg', $cfgFiles)) {
                     try {
                         $proj = lizmap::getProject($this->key . '~' . substr($qgsFile, 0, -4));
                         if ($proj != null) {
                             $projects[] = $proj;
                         }
                     } catch (UnknownLizmapProjectException $e) {
                         jLog::logEx($e, 'error');
                     }
                 }
             }
         }
     }
     return $projects;
 }