/**
  * 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;
 }
 function __construct($id)
 {
     $this->formId = $id;
     $mydata = array();
     foreach (lizmap::getRepositoryList() as $repo) {
         $rep = lizmap::getRepository($repo);
         $mydata[$repo] = (string) $rep->getData('label');
     }
     $this->data = $mydata;
 }
 /**
  * Redirect to the default repository project list.
  * 
  * @return Redirection to the default repository list
  */
 function index()
 {
     $rep = $this->getResponse('redirect');
     // Get repository data
     $repository = $this->param('repository');
     // Get the corresponding repository
     $lrep = lizmap::getRepository($repository);
     // Set the redirection parameters
     if ($lrep) {
         $rep->params = array('repository' => $lrep->getKey());
     }
     $rep->action = 'view~default:index';
     return $rep;
 }
 public function getData($form)
 {
     $pdata = array();
     $criteria = $form->getData($this->criteriaFrom[0]);
     if ($criteria && array_key_exists($criteria, $this->data)) {
         $rep = lizmap::getRepository($criteria);
         $projects = $rep->getProjects();
         foreach ($projects as $p) {
             $pOptions = $p->getOptions();
             if (property_exists($pOptions, 'hideProject') && $pOptions->hideProject == 'True') {
                 continue;
             }
             $pdata[$p->getData('id')] = (string) $p->getData('title');
         }
     }
     return $pdata;
 }
 /**
  * Use DynamicLayers python plugin to get a child project
  * And redirect to Lizmap view map controller with changed project parameter
  */
 function index()
 {
     // Set up redirect response
     $rep = $this->getResponse('redirect');
     $rep->action = 'view~map:index';
     $params = jApp::coord()->request->params;
     $rep->params = $params;
     // Redirect to normal map if no suitable parameters
     if (!$params['dlsourcelayer'] or !$params['dlexpression']) {
         jLog::log('Dynamic layers - no parameters DLSOURCELAYER or DLEXPRESSION');
         return $rep;
     }
     // Get project path
     $project = $params['project'];
     $repository = $params['repository'];
     $lrep = lizmap::getRepository($repository);
     $projectTemplatePath = realpath($lrep->getPath()) . '/' . $project . ".qgs";
     // Use QGIS python plugins dynamicLayers to get child project
     $lizmapServices = lizmap::getServices();
     $url = $lizmapServices->wmsServerURL . '?';
     $qparams = array();
     $qparams['service'] = 'dynamicLayers';
     $qparams['map'] = $projectTemplatePath;
     $qparams['dlsourcelayer'] = $params['dlsourcelayer'];
     $qparams['dlexpression'] = $params['dlexpression'];
     $rparams = http_build_query($qparams);
     $querystring = $url . $rparams;
     // Get remote data
     $lizmapCache = jClasses::getService('lizmap~lizmapCache');
     $getRemoteData = $lizmapCache->getRemoteData($querystring, $this->services->proxyMethod, $this->services->debugMode);
     $data = $getRemoteData[0];
     $mime = $getRemoteData[1];
     // Get returned response and redirect to appropriate project page
     $json = json_decode($data);
     if ($json->status == 0) {
         jLog::log('DynamicLayers error : ' . $json->message);
     } else {
         $params['project'] = preg_replace('#\\.qgs$#', '', $json->childProject);
         unset($params['dlsourcelayer']);
         unset($params['dlexpression']);
         $rep->params = $params;
         jLog::log('DynamicLayers message : ' . $json->message . ' - ' . $json->childProject);
     }
     return $rep;
 }
 /**
  * @return mixed  the repository key, or false if clear has failed
  */
 public static function clearCache($repository)
 {
     // Get config utility
     $lrep = lizmap::getRepository($repository);
     $ser = lizmap::getServices();
     // Remove the cache for the repository for file/sqlite cache type
     $cacheRootDirectory = $ser->cacheRootDirectory;
     if (!is_writable($cacheRootDirectory) or !is_dir($cacheRootDirectory)) {
         $cacheRootDirectory = sys_get_temp_dir();
     }
     $clearCacheOk = jFile::removeDir($cacheRootDirectory . '/' . $lrep->getKey());
     if ($ser->cacheStorageType == 'redis') {
         // remove the cache from redis
         $cacheName = 'lizmapCache_' . $repository;
         self::declareRedisProfile($ser, $cacheName, $repository);
         $clearCacheOk = $clearCacheOk && jCache::flush($cacheName);
     }
     jEvent::notify('lizmapProxyClearCache', array('repository' => $repository));
     if ($clearCacheOk) {
         return $lrep->getKey();
     }
     return false;
 }
 /**
  * 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 !', 'ProjectNotDefind');
         return false;
     }
     // Get repository data
     $repository = $this->iParam('repository');
     // Get the corresponding repository
     $lrep = lizmap::getRepository($repository);
     // Redirect if no rights to access this repository
     if (!jAcl2::check('lizmap.repositories.view', $lrep->getKey())) {
         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 = lizmap::getProject($repository . '~' . $project);
     $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;
 }
 /**
  * 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 (!jAcl2::check('lizmap.repositories.view', $lrep->getKey())) {
         $rep = $this->getResponse('redirect');
         $rep->action = 'view~default:error';
         jMessage::add(jLocale::get('view~default.repository.access.denied'), 'error');
         return $rep;
     }
     // Get the project
     $project = $this->param('project');
     // 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 (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;
 }
 /**
  * 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;
 }
 protected function _prepareTpl()
 {
     $protocol = jApp::coord()->request->getProtocol();
     $this->_tpl->assign('protocol', $protocol);
     $domain = jApp::coord()->request->getDomainName();
     $this->_tpl->assign('domain', $domain);
     $this->_tpl->assign('auth_url_return', $this->param('auth_url_return'));
     $this->_tpl->assign('isConnected', jAuth::isConnected());
     // Get lizmap services
     $services = lizmap::getServices();
     if ($services->allowUserAccountRequests) {
         $this->_tpl->assign('allowUserAccountRequests', True);
     }
     jClasses::inc('lizmapMainViewItem');
     $maps = array();
     // Get repository data
     $repository = $this->param('repository');
     $repositories = array();
     if ($repository != null && jAcl2::check('lizmap.repositories.view', $repository)) {
         $repositories[] = $repository;
     } else {
         $repositories = lizmap::getRepositoryList();
     }
     // Get excluded project
     $excludedProject = $this->param('excludedProject');
     foreach ($repositories as $r) {
         if (jAcl2::check('lizmap.repositories.view', $r)) {
             $lrep = lizmap::getRepository($r);
             $mrep = new lizmapMainViewItem($r, $lrep->getData('label'));
             $lprojects = $lrep->getProjects();
             // WMS GetCapabilities Url
             $wmsGetCapabilitiesUrl = jAcl2::check('lizmap.tools.displayGetCapabilitiesLinks', $lrep->getKey());
             foreach ($lprojects as $p) {
                 $pOptions = $p->getOptions();
                 if (property_exists($pOptions, 'hideProject') && $pOptions->hideProject == 'True') {
                     continue;
                 }
                 if ($wmsGetCapabilitiesUrl) {
                     $wmsGetCapabilitiesUrl = $p->getData('wmsGetCapabilitiesUrl');
                 }
                 if ($lrep->getKey() . '~' . $p->getData('id') != $excludedProject) {
                     $mrep->childItems[] = new lizmapMainViewItem($p->getData('id'), $p->getData('title'), $p->getData('abstract'), $p->getData('proj'), $p->getData('bbox'), jUrl::get('view~map:index', array("repository" => $p->getData('repository'), "project" => $p->getData('id'))), jUrl::get('view~media:illustration', array("repository" => $p->getData('repository'), "project" => $p->getData('id'))), 0, $r, 'map', $wmsGetCapabilitiesUrl);
                     /*} else {
                       $this->_tpl->assign('auth_url_return', jUrl::get('view~map:index',
                         array(
                           "repository"=>$lrep->getKey(),
                           "project"=>$p->getData('id'),
                         )
                       ) );*/
                 }
             }
             if (count($mrep->childItems) != 0) {
                 usort($mrep->childItems, "mainViewItemSort");
                 $maps[$r] = $mrep;
             }
         }
     }
     $items = jEvent::notify('mainviewGetMaps')->getResponse();
     foreach ($items as $item) {
         if ($item->parentId) {
             if ($item->parentId . '~' . $item->id == $excludedProject) {
                 continue;
             }
             if (!isset($maps[$item->parentId])) {
                 $maps[$item->parentId] = new lizmapMainViewItem($item->parentId, '', '');
             }
             $replaced = false;
             foreach ($maps[$item->parentId]->childItems as $k => $i) {
                 if ($i->id == $item->id) {
                     $maps[$item->parentId]->childItems[$k] = $item;
                     $replaced = true;
                 }
             }
             if (!$replaced) {
                 $maps[$item->parentId]->childItems[] = $item;
                 usort($maps[$item->parentId]->childItems, "mainViewItemSort");
             }
         } else {
             if (isset($maps[$item->id])) {
                 $maps[$item->id]->copyFrom($item);
             } else {
                 $maps[$item->id] = $item;
             }
         }
     }
     usort($maps, "mainViewItemSort");
     foreach ($maps as $topitem) {
         usort($topitem->childItems, "mainViewItemSort");
     }
     $this->_tpl->assign('mapitems', $maps);
     $this->_tpl->assign('onlyMaps', $this->param('onlyMaps', False));
 }
 /**
  * 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 the jForm for a repository.
  *
  */
 public static function constructRepositoryForm($rep, $form)
 {
     $services = lizmap::getServices();
     $rootRepositories = $services->getRootRepositories();
     $repositories = array();
     foreach (lizmap::getRepositoryList() as $repo) {
         if ($rep && $rep->getKey() == $repo) {
             continue;
         }
         $repositories[] = lizmap::getRepository($repo);
     }
     // reconstruct form fields based on repositoryPropertyList
     $propertiesOptions = lizmap::getRepositoryPropertiesOptions();
     foreach (lizmap::getRepositoryProperties() as $k) {
         $ctrl = null;
         if ($propertiesOptions[$k]['fieldType'] == 'checkbox') {
             $ctrl = new jFormsControlCheckbox($k);
         } else {
             if ($k == 'path' && $rootRepositories != '') {
                 if ($rep == null || substr($rep->getPath(), 0, strlen($rootRepositories)) === $rootRepositories) {
                     $ctrl = new jFormsControlMenulist($k);
                     $dataSource = new jFormsStaticDatasource();
                     $data = array();
                     $data[''] = '';
                     if ($dh = opendir($rootRepositories)) {
                         while (($file = readdir($dh)) !== false) {
                             if ($file == '.' || $file == '..') {
                                 continue;
                             }
                             $filePath = $rootRepositories . $file . '/';
                             if (is_dir($filePath)) {
                                 $allreadyUsed = False;
                                 foreach ($repositories as $repo) {
                                     if ($repo->getPath() == $filePath) {
                                         $allreadyUsed = True;
                                         break;
                                     }
                                 }
                                 if (!$allreadyUsed) {
                                     $data[$filePath] = $file;
                                 }
                             }
                         }
                     }
                     $dataSource->data = $data;
                     $ctrl->datasource = $dataSource;
                 } else {
                     $ctrl = new jFormsControlHidden($k);
                 }
             } else {
                 $ctrl = new jFormsControlInput($k);
                 $ctrl->datatype = new jDatatypeString();
             }
         }
         $ctrl->required = $propertiesOptions[$k]['required'];
         $ctrl->label = jLocale::get("admin~admin.form.admin_section.repository." . $k . ".label");
         $ctrl->size = 100;
         $form->addControl($ctrl);
     }
     if ($rep) {
         foreach ($rep->getProperties() as $k) {
             $v = $rep->getData($k);
             if ($k == 'path' && $rootRepositories != '' && substr($rep->getPath(), 0, strlen($rootRepositories)) === $rootRepositories) {
                 $v = $rep->getPath();
             }
             $form->setData($k, $v);
         }
     }
     return $form;
 }
 /**
  * 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;
 }
 protected function _prepareTpl()
 {
     $protocol = jApp::coord()->request->getProtocol();
     $this->_tpl->assign('protocol', $protocol);
     $domain = jApp::coord()->request->getDomainName();
     $this->_tpl->assign('domain', $domain);
     $maps = array();
     // Get repository data
     $repository = $this->param('repository');
     $repositories = array();
     if ($repository != null && jAcl2::check('lizmap.repositories.view', $repository)) {
         $repositories[] = $repository;
     } else {
         $repositories = lizmap::getRepositoryList();
     }
     foreach ($repositories as $r) {
         if (jAcl2::check('lizmap.repositories.view', $r)) {
             $lrep = lizmap::getRepository($r);
             $mrep = new lizmapMainViewItem($r, $lrep->getData('label'));
             $lprojects = $lrep->getProjects();
             foreach ($lprojects as $p) {
                 if (!$p->checkAcl()) {
                     continue;
                 }
                 $mrep->childItems[] = new lizmapMainViewItem($p->getData('id'), $p->getData('title'), $p->getData('abstract'), $p->getData('proj'), $p->getData('bbox'), jUrl::getFull('view~map:index', array("repository" => $p->getData('repository'), "project" => $p->getData('id'))), jUrl::getFull('view~media:illustration', array("repository" => $p->getData('repository'), "project" => $p->getData('id'))), 0, $r, 'map');
             }
             $maps[$r] = $mrep;
         }
     }
     $req = jApp::coord()->request;
     $items = jEvent::notify('mainviewGetMaps')->getResponse();
     foreach ($items as $item) {
         if ($item->parentId) {
             if (!isset($maps[$item->parentId])) {
                 $maps[$item->parentId] = new lizmapMainViewItem($item->parentId, '', '');
             }
             $replaced = false;
             foreach ($maps[$item->parentId]->childItems as $k => $i) {
                 if ($i->id == $item->id) {
                     if (!preg_match('/^http/', $item->img)) {
                         $item->img = $req->getServerURI() . $item->img;
                     }
                     if (!preg_match('/^http/', $item->url)) {
                         $item->url = $req->getServerURI() . $item->url;
                     }
                     $maps[$item->parentId]->childItems[$k] = $item;
                     $replaced = true;
                 }
             }
             if (!$replaced) {
                 $maps[$item->parentId]->childItems[] = $item;
             }
         } else {
             if (isset($maps[$item->id])) {
                 $item->img = $maps[$item->id]->img;
                 $maps[$item->id]->copyFrom($item);
             } else {
                 $maps[$item->id] = $item;
             }
         }
     }
     usort($maps, "mainViewItemSort");
     foreach ($maps as $topitem) {
         usort($topitem->childItems, "mainViewItemSort");
     }
     $this->_tpl->assign('mapitems', $maps);
 }
 /**
  * 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;
 }
 /**
  * Replace a feature attribute value by its html representation
  *
  * @param string $attributeName Feature Attribute name.
  * @param string $attributeValue Feature Attribute value.
  * @param string $repository Lizmap Repository.
  * @param string $project Name of the project.
  * @param string $popupFeatureContent Content of the popup template (created by lizmap plugin) and passed several times. IF false, return only modified attribute.
  * @return string The html for the feature attribute.
  */
 public function getHtmlFeatureAttribute($attributeName, $attributeValue, $repository, $project, $popupFeatureContent = Null)
 {
     // Force $attributeValue to be a string
     $attributeName = (string) $attributeName;
     $attributeValue = (string) $attributeValue;
     if ($attributeValue == 'NULL') {
         $attributeValue = '';
     }
     // Regex to replace links, medias and images
     $urlRegex = '/(ftp|http|https):\\/\\/(\\w+:{0,1}\\w*@)?(\\S+)(:[0-9]+)?(\\/|\\/([\\w#!:.?+=&%@!\\-\\/]))?/';
     $emailRegex = '/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/';
     $imageRegex = '/\\.(jpg|jpeg|png|gif|bmp)$/i';
     $mediaRegex = '/^(\\/)?media\\//';
     $mediaTextRegex = '/\\.(txt|htm|html)$/i';
     // Remote urls and images
     if (preg_match($urlRegex, $attributeValue)) {
         if (preg_match($imageRegex, $attributeValue)) {
             $attributeValue = '<img src="' . $attributeValue . '" border="0"/>';
         } else {
             if (!$popupFeatureContent) {
                 // only if no template is passed by the user
                 $attributeValue = '<a href="' . $attributeValue . '" target="_blank">' . $attributeValue . '</a>';
             }
         }
     }
     // E-mail
     if (preg_match($emailRegex, $attributeValue)) {
         if (!$popupFeatureContent) {
             // only if no template is passed by the user
             $attributeValue = '<a href="mailto:' . $attributeValue . '"</td></tr>';
         }
     }
     // Media = file stored in the repository media folder
     if (preg_match($mediaRegex, $attributeValue)) {
         $sharps = array();
         preg_match('/(.+)#(page=[0-9]+)$/i', $attributeValue, $sharps);
         if (count($sharps) == 3) {
             $pathVal = $sharps[1];
             $sharp = $sharps[2];
         } else {
             $pathVal = $attributeValue;
             $sharp = '';
         }
         $mediaUrl = jUrl::getFull('view~media:getMedia', array('repository' => $repository, 'project' => $project, 'path' => $pathVal), 0, $_SERVER['SERVER_NAME']);
         if ($sharp) {
             $mediaUrl .= '#' . $sharp;
         }
         // Display if it is an image
         if (preg_match($imageRegex, $attributeValue)) {
             if (!$popupFeatureContent) {
                 // only if no template is passed by the user
                 $attributeValue = '<a href="' . $mediaUrl . '" target="_blank"><img src="' . $mediaUrl . '" border="0"/></a>';
             } else {
                 $attributeValue = $mediaUrl;
             }
         } else {
             if (preg_match($mediaTextRegex, $attributeValue)) {
                 $data = '';
                 // Get full path to the file
                 $lrep = lizmap::getRepository($repository);
                 $repositoryPath = realpath($lrep->getPath());
                 $abspath = realpath($repositoryPath . '/' . $attributeValue);
                 $n_repositoryPath = str_replace('\\', '/', $repositoryPath);
                 $n_abspath = str_replace('\\', '/', $abspath);
                 if (preg_match("#^" . $n_repositoryPath . "(/)?media/#", $n_abspath) and file_exists($abspath)) {
                     $data = jFile::read($abspath);
                 }
                 // Replace images src by full path
                 $iUrl = jUrl::get('view~media:getMedia', array('repository' => $repository, 'project' => $project));
                 $data = preg_replace('#src="(.+(jpg|jpeg|gif|png))"?#i', 'src="' . $iUrl . '&path=$1"', $data);
                 $attributeValue = $data;
             } else {
                 if (!$popupFeatureContent) {
                     // only if no template is passed by the user
                     $attributeValue = '<a href="' . $mediaUrl . '" target="_blank">' . $attributeValue . '</a>';
                 } else {
                     $attributeValue = $mediaUrl;
                 }
             }
         }
     } else {
         $attributeValue = preg_replace('#\\n#', '<br>', $attributeValue);
     }
     // Return the modified template or only the resulted attribute value
     if ($popupFeatureContent) {
         // Replace {$mycol} by the processed column value
         $popupFeatureContent = preg_replace('#\\{\\$' . $attributeName . '\\}#i', $attributeValue, $popupFeatureContent);
         return $popupFeatureContent;
     } else {
         // Return the modified attributeValue
         return $attributeValue;
     }
 }
 /**
  * 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;
 }