> * * ----------------------------------------------------------------------------- * * See main.inc.php for release information * * manage all the ajax requests * ----------------------------------------------------------------------------- */ // in this case, PHPWG_ROOT_PATH must be declared as an absolute path... define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/'); /* * set ajax module in admin mode if request is used for admin interface */ if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']=''; if(preg_match('/^admin\./i', $_REQUEST['ajaxfct'])) define('IN_ADMIN', true); if(!defined('AJAX_CALL')) define('AJAX_CALL', true); // the common.inc.php file loads all the main.inc.php plugins files include_once(PHPWG_ROOT_PATH.'include/common.inc.php' ); include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php'); include_once('gmaps_root.class.inc.php'); load_language('plugin.lang', GMAPS_PATH); class GMaps_Ajax extends GMaps_root { /** * constructor */ public function __construct($prefixeTable, $filelocation) { parent::__construct($prefixeTable, $filelocation); $this->loadConfig(); $this->checkRequest(); $this->returnAjaxContent(); } /** * check the $_REQUEST values and set default values * */ protected function checkRequest() { global $user; if(!isset($_REQUEST['errcode'])) $_REQUEST['errcode']=''; GPCAjax::checkToken(); // check if asked function is valid if(!($_REQUEST[GPC_AJAX]=='admin.maps.getList' or $_REQUEST[GPC_AJAX]=='admin.maps.setMap' or $_REQUEST[GPC_AJAX]=='admin.maps.getMap' or $_REQUEST[GPC_AJAX]=='admin.maps.deleteMap' or $_REQUEST[GPC_AJAX]=='admin.assoc.getList' or $_REQUEST[GPC_AJAX]=='admin.assoc.setAssoc' or $_REQUEST[GPC_AJAX]=='admin.assoc.getAssoc' or $_REQUEST[GPC_AJAX]=='admin.assoc.deleteAssoc' or $_REQUEST[GPC_AJAX]=='admin.kmlFiles.getList' or $_REQUEST[GPC_AJAX]=='admin.kmlFiles.setFile' or $_REQUEST[GPC_AJAX]=='admin.kmlFiles.getFile' or $_REQUEST[GPC_AJAX]=='admin.kmlFiles.deleteFile' or $_REQUEST[GPC_AJAX]=='public.maps.init' or $_REQUEST[GPC_AJAX]=='public.maps.getMarkers' )) $_REQUEST[GPC_AJAX]=''; if(preg_match('/^admin\./i', $_REQUEST[GPC_AJAX]) and !is_admin()) $_REQUEST[GPC_AJAX]=''; if($_REQUEST[GPC_AJAX]!='') { /* * no check for admin.maps.getList & admin.assoc.getList requests */ /* * check values for * admin.maps.getMap * admin.assoc.getAssoc * admin.kmlFiles.getFile */ if($_REQUEST[GPC_AJAX]=="admin.maps.getMap" or $_REQUEST[GPC_AJAX]=="admin.assoc.getAssoc" or $_REQUEST[GPC_AJAX]=="admin.kmlFiles.getFile") { if(!isset($_REQUEST['id']) or !is_numeric($_REQUEST['id']) or $_REQUEST['id']=='') $_REQUEST[GPC_AJAX]=''; } /* * check values for * admin.maps.deleteMap * admin.assoc.deleteAssoc * admin.kmlFiles.deleteFile */ if($_REQUEST[GPC_AJAX]=="admin.maps.deleteMap" or $_REQUEST[GPC_AJAX]=="admin.assoc.deleteAssoc" or $_REQUEST[GPC_AJAX]=="admin.kmlFiles.deleteFile") { if(!isset($_REQUEST['id']) or !is_numeric($_REQUEST['id']) or $_REQUEST['id']=='') $_REQUEST[GPC_AJAX]=''; } /* * check admin.maps.setMap values */ if($_REQUEST[GPC_AJAX]=="admin.maps.setMap") { if(!isset($_REQUEST['id']) or !isset($_REQUEST['datas']) or !is_array($_REQUEST['datas'])) { $_REQUEST[GPC_AJAX]=''; } else { if(!(isset($_REQUEST['datas']['displayType']) and isset($_REQUEST['datas']['sizeMode']) and isset($_REQUEST['datas']['name']) and isset($_REQUEST['datas']['width']) and isset($_REQUEST['datas']['height']) and isset($_REQUEST['datas']['zoomLevel']) and isset($_REQUEST['datas']['mapType']) and isset($_REQUEST['datas']['mapTypeControl']) and isset($_REQUEST['datas']['navigationControl']) and isset($_REQUEST['datas']['scaleControl']) and isset($_REQUEST['datas']['streetViewControl']) and isset($_REQUEST['datas']['style']) and isset($_REQUEST['datas']['zoomLevelMaxActivated']) )) { $_REQUEST[GPC_AJAX]=''; } else { if(!($_REQUEST['datas']['displayType']=='IC' or $_REQUEST['datas']['displayType']=='IP' or $_REQUEST['datas']['displayType']=='MP')) $_REQUEST[GPC_AJAX]=''; if(!($_REQUEST['datas']['sizeMode']=='A' or $_REQUEST['datas']['sizeMode']=='F')) $_REQUEST[GPC_AJAX]=''; if(!is_numeric($_REQUEST['datas']['width']) or $_REQUEST['datas']['width']<100 or $_REQUEST['datas']['width']>1280) $_REQUEST['datas']['width']=470; if(!is_numeric($_REQUEST['datas']['height']) or $_REQUEST['datas']['height']<100 or $_REQUEST['datas']['height']>1280) $_REQUEST['datas']['height']=210; if(!is_numeric($_REQUEST['datas']['zoomLevel']) or $_REQUEST['datas']['zoomLevel']<1 or $_REQUEST['datas']['zoomLevel']>20) $_REQUEST['datas']['zoomLevel']=4; if(!($_REQUEST['datas']['mapType']=='hybrid' or $_REQUEST['datas']['mapType']=='terrain' or $_REQUEST['datas']['mapType']=='roadmap' or $_REQUEST['datas']['mapType']=='satellite')) $_REQUEST['datas']['mapType']='hybrid'; if(!is_numeric($_REQUEST['datas']['mapTypeControl']) or $_REQUEST['datas']['mapTypeControl']<-1 or $_REQUEST['datas']['mapTypeControl']>2) $_REQUEST['datas']['mapTypeControl']=0; if(!is_numeric($_REQUEST['datas']['navigationControl']) or $_REQUEST['datas']['navigationControl']<-1 or $_REQUEST['datas']['navigationControl']>3) $_REQUEST['datas']['navigationControl']=0; if(!($_REQUEST['datas']['scaleControl']=='y' or $_REQUEST['datas']['scaleControl']=='n')) $_REQUEST['datas']['scaleControl']='y'; if(!($_REQUEST['datas']['streetViewControl']=='y' or $_REQUEST['datas']['streetViewControl']=='n')) $_REQUEST['datas']['streetViewControl']='n'; if(!($_REQUEST['datas']['zoomLevelMaxActivated']=='y' or $_REQUEST['datas']['zoomLevelMaxActivated']=='n')) $_REQUEST['datas']['zoomLevelMaxActivated']='n'; } } } /* * check admin.maps.setAssoc values */ if($_REQUEST[GPC_AJAX]=="admin.assoc.setAssoc") { if(!isset($_REQUEST['id']) or !isset($_REQUEST['datas']) or !is_array($_REQUEST['datas'])) { $_REQUEST[GPC_AJAX]=''; } else { if(!(isset($_REQUEST['datas']['categoryId']) and isset($_REQUEST['datas']['mapId']) and isset($_REQUEST['datas']['applySubCat']) and isset($_REQUEST['datas']['kmlFileId']) and isset($_REQUEST['datas']['kmlFileUrl']) and isset($_REQUEST['datas']['icon']) and isset($_REQUEST['datas']['marker']) and isset($_REQUEST['datas']['title']) )) { $_REQUEST[GPC_AJAX]=''; } else { if($_REQUEST['datas']['categoryId']=='' or $_REQUEST['datas']['categoryId']<0) $_REQUEST[GPC_AJAX]=''; if($_REQUEST['datas']['mapId']=='') $_REQUEST[GPC_AJAX]=''; if(!($_REQUEST['datas']['applySubCat']=='y' or $_REQUEST['datas']['applySubCat']=='n')) $_REQUEST['datas']['applySubCat']='y'; if(!isset($_REQUEST['datas']['forceDisplay'])) $_REQUEST['datas']['forceDisplay']='n'; if(!($_REQUEST['datas']['forceDisplay']=='y' or $_REQUEST['datas']['forceDisplay']=='n')) $_REQUEST['datas']['forceDisplay']='n'; } } } /* * check admin.kmlFiles.setFile values */ if($_REQUEST[GPC_AJAX]=="admin.kmlFiles.setFile") { if(!isset($_REQUEST['id']) ) { $_REQUEST[GPC_AJAX]=''; } else { if($_REQUEST['id']=='') { if(!(isset($_FILES['file']['name']) and isset($_FILES['file']['tmp_name']))) $_REQUEST[GPC_AJAX]=''; } } } /* * check public.maps.getMarkers values */ if($_REQUEST[GPC_AJAX]=="public.maps.getMarkers") { if(!isset($_REQUEST['datas']) or !is_array($_REQUEST['datas'])) { $_REQUEST[GPC_AJAX]=''; } else { if(!(isset($_REQUEST['datas']['requestId']) and isset($_REQUEST['datas']['callId']) and isset($_REQUEST['datas']['bounds']) and isset($_REQUEST['datas']['width']) and isset($_REQUEST['datas']['height']) and isset($_REQUEST['datas']['distanceTreshold']) and isset($_REQUEST['datas']['bounds']['north']) and isset($_REQUEST['datas']['bounds']['south']) and isset($_REQUEST['datas']['bounds']['east']) and isset($_REQUEST['datas']['bounds']['west']) )) { $_REQUEST[GPC_AJAX]=''; } if(!isset($_REQUEST['datas']['loadIndex'])) $_REQUEST['datas']['loadIndex']=''; } } /* * check public.maps.getMarkerInfos values */ if($_REQUEST[GPC_AJAX]=="public.maps.init") { if(!isset($_REQUEST['category'])) $_REQUEST[GPC_AJAX]=''; if(!isset($_REQUEST['mapId'])) $_REQUEST['mapId']=null; } } } //checkRequest /** * return ajax content */ protected function returnAjaxContent() { $result="
An error has occured
"; switch($_REQUEST[GPC_AJAX]) { case 'admin.maps.getList': $result=$this->ajax_gmaps_admin_mapsGetList(); break; case 'admin.maps.getMap': $result=$this->ajax_gmaps_admin_mapsGetMap($_REQUEST['id']); break; case 'admin.maps.setMap': $result=$this->ajax_gmaps_admin_mapsSetMap($_REQUEST['id'], $_REQUEST['datas']); break; case 'admin.maps.deleteMap': $result=$this->ajax_gmaps_admin_mapsDeleteMap($_REQUEST['id']); break; case 'admin.assoc.getList': $result=$this->ajax_gmaps_admin_assocGetList(); break; case 'admin.assoc.getAssoc': $result=$this->ajax_gmaps_admin_assocGetAssoc($_REQUEST['id']); break; case 'admin.assoc.setAssoc': $result=$this->ajax_gmaps_admin_assocSetAssoc($_REQUEST['id'], $_REQUEST['datas']); break; case 'admin.assoc.deleteAssoc': $result=$this->ajax_gmaps_admin_assocDeleteAssoc($_REQUEST['id']); break; case 'admin.kmlFiles.getList': $result=$this->ajax_gmaps_admin_kmlFilesGetList(); break; case 'admin.kmlFiles.getFile': $result=$this->ajax_gmaps_admin_kmlFilesGetFile($_REQUEST['id']); break; case 'admin.kmlFiles.setFile': $result=$this->ajax_gmaps_admin_kmlFilesSetFile($_REQUEST['id'], $_REQUEST['name']); break; case 'admin.kmlFiles.deleteFile': $result=$this->ajax_gmaps_admin_kmlFilesDeleteFile($_REQUEST['id']); break; case 'public.maps.init': $result=$this->ajax_gmaps_public_mapsInit($_REQUEST['category'], $_REQUEST['mapId']); break; case 'public.maps.getMarkers': $result=$this->ajax_gmaps_public_mapsGetMarkers($_REQUEST['datas']); break; case 'public.maps.getMarkersInfos': $result=$this->ajax_gmaps_public_mapsGetMarkersInfos($_REQUEST['datas']); break; } GPCAjax::returnResult($result); } /*------------------------------------------------------------------------* * * ADMIN FUNCTIONS * *----------------------------------------------------------------------- */ /** * return a HTML list of defined maps * * @return String */ private function ajax_gmaps_admin_mapsGetList() { global $template; $template->set_filename('list_page', dirname($this->getFileLocation()).'/admin/gmaps_maps_iListMaps.tpl'); $datas=Array(); $sql="SELECT id, name, displayType, sizeMode, width, height, zoomLevel, mapType, mapTypeControl, navigationControl, scaleControl, streetViewControl, zoomLevelMaxActivated FROM ".$this->tables['maps']." ORDER BY displayType ASC, name ASC"; $result=pwg_query($sql); if($result) { while($row=pwg_db_fetch_assoc($result)) { if($row['displayType']=='IC') { $row['zoomLevel']=l10n('gmaps_auto'); } $row['displayType']=l10n('gmaps_displayTypeShort'.$row['displayType']); if($row['sizeMode']=='A') { $row['dimensions']=l10n('gmaps_auto'); } else { $row['dimensions']=$row['width'].'x'.$row['height']; } $row['mapType']=l10n('gmaps_googleMapType_'.$row['mapType']); $row['mapTypeControl']=l10n('gmaps_googleMapTypeControl_'.$row['mapTypeControl']); $row['navigationControl']=l10n('gmaps_googleMapNavigationControl_'.$row['navigationControl']); $row['scaleControl']=($row['scaleControl']=='y')?l10n('gmaps_display_visible'):l10n('gmaps_display_hidden'); $row['streetViewControl']=($row['streetViewControl']=='y')?l10n('gmaps_display_visible'):l10n('gmaps_display_hidden'); $datas[]=$row; } } $template->assign('datas', $datas); return($template->parse('list_page', true)); } //ajax_gmaps_admin_mapsGetList /** * set properties for a given map id ; if no map id is given, create a new * map * * @param String id : the map Id * @param Array datas : properties of the map (assuming array index were * checked by the checkRequest function) * @return String : the num id, or an error message */ private function ajax_gmaps_admin_mapsSetMap($id, $properties) { global $template; if($id=='') { $sql="INSERT INTO ".$this->tables['maps']." VALUES ('', '".mysql_escape_string($properties['name'])."', '". $properties['displayType']."', '". $properties['sizeMode']."', '". $properties['width']."', '". $properties['height']."', '". $properties['zoomLevel']."', '". $properties['mapType']."', '". $properties['mapTypeControl']."', '". $properties['navigationControl']."', '". $properties['scaleControl']."', '". $properties['streetViewControl']."', '". mysql_escape_string($properties['style'])."', '". $properties['zoomLevelMaxActivated']."');"; $result=pwg_query($sql); $id=pwg_db_insert_id(); } else { $sql="UPDATE ".$this->tables['maps']." SET name='".mysql_escape_string($properties['name'])."', displayType='". $properties['displayType']."', sizeMode='". $properties['sizeMode']."', width='". $properties['width']."', height='". $properties['height']."', zoomLevel='". $properties['zoomLevel']."', mapType='". $properties['mapType']."', mapTypeControl='". $properties['mapTypeControl']."', navigationControl='". $properties['navigationControl']."', scaleControl='". $properties['scaleControl']."', streetViewControl='". $properties['streetViewControl']."', style='". mysql_escape_string($properties['style'])."', zoomLevelMaxActivated='". $properties['zoomLevelMaxActivated']."' WHERE id='$id';"; $result=pwg_query($sql); } return($id); } //ajax_gmaps_admin_mapsSetMap /** * get properties for a given map id * * @param String id : the map Id * @return String : data formatted in a JSON string */ private function ajax_gmaps_admin_mapsGetMap($id) { $returned=array( 'id' => '', 'name' => '', 'displayType' => '', 'sizeMode' => '', 'width' => 470, 'height' => 210, 'zoomLevel' => 4, 'mapType' => 'hybrid', 'mapTypeControl' => 0, 'navigationControl' => 0, 'scaleControl' => 'y', 'streetViewControl' => 'n', 'zoomLevelMaxActivated' => 'n', 'associations' => 0, ); $sql="SELECT pgmm.id, pgmm.displayType, pgmm.sizeMode, pgmm.name, pgmm.width, pgmm.height, pgmm.zoomLevel, pgmm.mapType, pgmm.mapTypeControl, pgmm.navigationControl, pgmm.scaleControl, pgmm.streetViewControl, pgmm.zoomLevelMaxActivated, COUNT(pgcm.id) AS nbAssoc FROM ".$this->tables['maps']." pgmm LEFT JOIN ".$this->tables['category_maps']." pgcm ON pgcm.mapId=pgmm.id WHERE pgmm.id='$id' GROUP BY pgmm.id;"; $result=pwg_query($sql); if($result) { while($row=pwg_db_fetch_assoc($result)) { $returned=$row; } } return(json_encode($returned)); } //ajax_gmaps_admin_mapsGetMap /** * delete a map * * @param String id : if of the map to delete * @return String : ok or ko */ private function ajax_gmaps_admin_mapsDeleteMap($id) { $sql="DELETE FROM ".$this->tables['maps']." WHERE id='$id';"; $result=pwg_query($sql); if($result) { $sql="DELETE FROM ".$this->tables['category_maps']." WHERE mapId='$id';"; $result=pwg_query($sql); if($result) return('ok'); } return('ko!unknown error'); } //ajax_gmaps_admin_mapsDeleteMap /** * return a HTML list of defined associations * * @return String */ private function ajax_gmaps_admin_assocGetList() { global $template; $template->set_filename('list_page', dirname($this->getFileLocation()).'/admin/gmaps_category_maps_iListMaps.tpl'); $datas=Array(); $sql="SELECT pgcm.id, pgcm.categoryId, pct.name AS catName, pgmm.name AS mapName, pgmm.displayType, pgcm.kmlFileId, pgcm.kmlFileUrl, pgcm.imgSort, pgcm.applySubCat, pgcm.icon, pgcm.title, pgkf.name AS kmlName, pgcm.forceDisplay FROM ((".$this->tables['category_maps']." pgcm LEFT JOIN ".CATEGORIES_TABLE." pct ON pct.id=pgcm.categoryId) LEFT JOIN ".$this->tables['maps']." pgmm ON pgcm.mapId = pgmm.id) LEFT JOIN ".$this->tables['kmlfiles']." pgkf ON pgkf.id = pgcm.kmlFileId ORDER BY pct.name, pgmm.displayType, pgcm.imgSort"; $result=pwg_query($sql); if($result) { $keys=array( 'prev' => '', 'curr' => '' ); while($row=pwg_db_fetch_assoc($result)) { $keys['prev']=$keys['curr']; if($row['kmlFileId']>0) { $mapKmlFile=$row['kmlName']; } elseif($row['kmlFileUrl']!='') { $mapKmlFile='URL'; } else { $mapKmlFile=''; } $tmp=array( 'id' => $row['id'], 'catName' => ($row['categoryId']==0)?l10n('gmaps_applyForAllTheGallery'):GPCCore::getUserLanguageDesc($row['catName']), 'applySubCat' => l10n('gmaps_'.$row['applySubCat']), 'mapName' => $row['mapName'], 'mapKmlFile' => $mapKmlFile, 'icon' => $row['icon'], 'displayType' => l10n('gmaps_displayTypeShort'.$row['displayType']), 'title' => $row['title'], 'forceDisplay' => l10n('gmaps_'.$row['forceDisplay']), ); $keys['curr']=$row['categoryId'].$row['displayType']; if($keys['curr']==$keys['prev']) { $tmp['catName']=''; $tmp['displayType']=''; $tmp['applySubCat']=''; } $datas[]=$tmp; } } $template->assign('datas', $datas); return($template->parse('list_page', true)); } //ajax_gmaps_admin_assocGetList /** * set properties for a given association id ; if no association id is given * create a new association * * @param String id : the association Id * @param Array datas : properties of the association (assuming array index * were checked by the checkRequest function) * @return String : the num id, or an error message */ private function ajax_gmaps_admin_assocSetAssoc($id, $properties) { global $template; // if create a new assoc, get the last imgSort $db=array( 'nbId' => 0, 'displayType' => '', 'maxImgSort' => 0 ); $sql="SELECT MAX(imgSort) AS maxImgSort, pgmm.displayType, COUNT(pgcm.id) AS nbId FROM (".$this->tables['category_maps']." pgcm LEFT JOIN ".$this->tables['maps']." pgmm ON pgmm.id = pgcm.mapId) LEFT JOIN ".$this->tables['maps']." pgmm2 ON pgmm2.displayType=pgmm.displayType WHERE categoryId='".$properties['categoryId']."' AND pgmm2.id='".$properties['mapId']."' GROUP BY pgmm.displayType;"; $result=pwg_query($sql); if($result) { while($row=pwg_db_fetch_assoc($result)) { $db=$row; } } if($id=='' and ($db['displayType']=='IC' or $db['displayType']=='IP') and $db['nbId']>0 ) { return('!'.l10n('gmaps_only_one_map_is_allowed_for_this_mode')); } if($id=='') { $sql="INSERT INTO ".$this->tables['category_maps']." VALUES ('', '". $properties['categoryId']."', '". $properties['mapId']."', '". ($db['maxImgSort']+1)."', '". $properties['applySubCat']."', '". $properties['kmlFileId']."', '". $properties['kmlFileUrl']."', '". basename($properties['icon'])."', '". basename($properties['marker'])."', '". mysql_escape_string($properties['title'])."', '". $properties['forceDisplay']."');"; $result=pwg_query($sql); $id=pwg_db_insert_id(); } else { $sql="UPDATE ".$this->tables['category_maps']." SET categoryId='". $properties['categoryId']."', mapId='". $properties['mapId']."', applySubCat='". $properties['applySubCat']."', kmlFileUrl='". $properties['kmlFileUrl']."', kmlFileId='". $properties['kmlFileId']."', icon='". basename($properties['icon'])."', marker='". basename($properties['marker'])."', title='". mysql_escape_string($properties['title'])."', forceDisplay='". $properties['forceDisplay']."' WHERE id='$id';"; $result=pwg_query($sql); } return($id); } //ajax_gmaps_admin_assocSetAssoc /** * get properties for a given association id * * @param String id : the association Id * @return String : data formatted in a JSON string */ private function ajax_gmaps_admin_assocGetAssoc($id) { $returned=array( 'id' => '', 'categoryId' => 0, 'mapId' => '', 'applySubCat'=> 'y', 'kmlFileId' => 0, 'kmlFileUrl' => '', 'icon' => '', 'marker' => '', 'title' => '', 'forceDisplay' => 'n' ); $sql="SELECT id, categoryId, mapId, applySubCat, kmlFileId, kmlFileUrl, icon, marker, title, forceDisplay FROM ".$this->tables['category_maps']." WHERE id='$id';"; $result=pwg_query($sql); if($result) { while($row=pwg_db_fetch_assoc($result)) { $returned=$row; } } return(json_encode($returned)); } //ajax_gmaps_admin_assocGetAssoc /** * delete an association * * @param String id : if of the association to delete * @return String : ok or ko */ private function ajax_gmaps_admin_assocDeleteAssoc($id) { $sql="DELETE FROM ".$this->tables['category_maps']." WHERE id='$id';"; $result=pwg_query($sql); if($result) return('ok'); return('ko!unknown error'); } //ajax_gmaps_admin_assocDeleteAssoc /** * return a HTML list of referenced kml files * * @return String */ private function ajax_gmaps_admin_kmlFilesGetList() { global $template; $template->set_filename('list_page', dirname($this->getFileLocation()).'/admin/gmaps_kmlfiles_iListFiles.tpl'); $datas=Array(); $sql="SELECT pgkf.id, pgkf.file, pgkf.name, pgkf.fileDate, pgkf.fileSize, COUNT(pgcm.id) AS nbAssoc FROM ".$this->tables['kmlfiles']." pgkf LEFT JOIN ".$this->tables['category_maps']." pgcm ON pgcm.kmlFileId = pgkf.id GROUP BY pgkf.id ORDER BY pgkf.name"; $result=pwg_query($sql); if($result) { while($row=pwg_db_fetch_assoc($result)) { $row['fileSize']=GPCCore::formatOctet($row['fileSize']); $datas[]=$row; } } $template->assign('datas', $datas); return($template->parse('list_page', true)); } //ajax_gmaps_admin_kmlFilesGetList /** * set properties for a given file id ; if no file id is given create a new * file * * @param String $id : the file Id * @param String $name : the name * @param String $file : the file name * @return String : the num id, or an error message */ private function ajax_gmaps_admin_kmlFilesSetFile($id, $name) { global $template; if(isset($_FILES['file']['name'])) { $file=$_FILES['file']['name']; } else { $file=''; } if($file=='' and $id!='') { // update name for an existing file $sql="UPDATE ".$this->tables['kmlfiles']." SET name='".mysql_escape_string($name)."' WHERE id='$id';"; $result=pwg_query($sql); } elseif($file!='') { // check if this file is not already used by an another file id $sql="SELECT file FROM ".$this->tables['kmlfiles']." WHERE file='".mysql_escape_string($_FILES['file']['name'])."'"; if($id!='') $sql.=" AND id!='$id'"; $result=pwg_query($sql); if($result) { $ok=''; while($row=pwg_db_fetch_assoc($result)) { $ok=$row['file']; } if($ok!='') { //file already used by another file id header("Location: ".$_SERVER['HTTP_REFERER']."&errcode=aksf10"); exit(); } } else { //unknown error header("Location: ".$_SERVER['HTTP_REFERER']."&errcode=aksf01"); exit(); } $file=GPCCore::getPiwigoSystemPath().'/'.PWG_LOCAL_DIR.self::KML_DIRECTORY.$file; if(file_exists($file)) unlink($file); if($id!='') { $currentInfo=$this->ajax_gmaps_admin_kmlFilesGetFile($id, false); if(file_exists(GPCCore::getPiwigoSystemPath().'/'.PWG_LOCAL_DIR.self::KML_DIRECTORY.$currentInfo['file'])) unlink(GPCCore::getPiwigoSystemPath().'/'.PWG_LOCAL_DIR.self::KML_DIRECTORY.$currentInfo['file']); } if(!move_uploaded_file($_FILES['file']['tmp_name'], $file)) { /* * something was wrong, previous file was deleted, so delete the file * from the database */ if($id!='') $this->ajax_gmaps_admin_kmlFilesDeleteFile($id); // error when trying to copy the uploaded file header("Location: ".$_SERVER['HTTP_REFERER']."&errcode=aksf02"); exit(); } if($id!='') { // update file & name for an existing file $sql="UPDATE ".$this->tables['kmlfiles']." SET name='".mysql_escape_string($name)."', file='".mysql_escape_string($_FILES['file']['name'])."', fileSize=".filesize($file).", fileDate='".date('Y-m-d H:i:s', filemtime($file))."' WHERE id='$id';"; $result=pwg_query($sql); } else { $sql="INSERT INTO ".$this->tables['kmlfiles']." VALUES ('', '".mysql_escape_string($_FILES['file']['name'])."', '".mysql_escape_string($name)."', '".date('Y-m-d H:i:s', filemtime($file))."', '".filesize($file)."')"; $result=pwg_query($sql); $id=pwg_db_insert_id(); } header("Location: ".$_SERVER['HTTP_REFERER']); exit(); } return($id); } //ajax_gmaps_admin_kmlFilesSetFile /** * get properties for a given file id * * @param String id : the file Id * @param Bool $json : if true, return a json string, otherwise an array * @return String : data formatted in a JSON string */ private function ajax_gmaps_admin_kmlFilesGetFile($id, $json=true) { $returned=array( 'id' => '', 'file' => '', 'name' => '', 'fileDate'=> '', 'fileSize' => 0, 'fileSizeUnits' => 0, ); $sql="SELECT id, file, name, fileDate, fileSize FROM ".$this->tables['kmlfiles']." WHERE id='$id';"; $result=pwg_query($sql); if($result) { while($row=pwg_db_fetch_assoc($result)) { $row['fileSizeUnits']=GPCCore::formatOctet($row['fileSize']); $returned=$row; } } if($json) { return(json_encode($returned)); } else { return($returned); } } //ajax_gmaps_admin_kmlFilesGetFile /** * delete a file * * @param String id : if of the file to delete * @return String : ok or ko */ private function ajax_gmaps_admin_kmlFilesDeleteFile($id) { $file=''; /* * before deleting record in the table, get the associated file name stored * on the server */ $sql="SELECT file FROM ".$this->tables['kmlfiles']." WHERE id='$id';"; $result=pwg_query($sql); if($result) { while($row=pwg_db_fetch_assoc($result)) { $file=$row['file']; } } // reset kmlFileId for assocation $sql="UPDATE ".$this->tables['category_maps']." SET kmlFileId='0' WHERE kmlFileId='$id'"; pwg_query($sql); // delete the file $sql="DELETE FROM ".$this->tables['kmlfiles']." WHERE id='$id';"; $result=pwg_query($sql); if($result) { if($file!='' and file_exists(GPCCore::getPiwigoSystemPath().'/'.PWG_LOCAL_DIR.self::KML_DIRECTORY.$file)) { unlink(GPCCore::getPiwigoSystemPath().'/'.PWG_LOCAL_DIR.self::KML_DIRECTORY.$file); } return('ok'); } return('ko!unknown error'); } //ajax_gmaps_admin_kmlFilesDeleteFile /** * prepare the cache for the user / category * * * @param Integer $category : the category Id, 0 if want to init map for all * the gallery * @return String : the requestId */ private function ajax_gmaps_public_mapsInit($category, $mapId) { global $prefixeTable, $user; $requestId=''; if($mapId==null) { $this->buildMapList($category, 'C', self::ID_MODE_CATEGORY); } if($category>0) { $sqlCatRestrict="AND FIND_IN_SET($category, pct.uppercats)!=0"; } else { $sqlCatRestrict=""; } if(count($this->maps)>0 or $mapId!=null) { $scripts=array(); $bounds=array( 'N' => -90, 'S' => 90, 'E' => -180, 'W' => 180 ); // there is some maps to display $requestId=$this->getCacheRequestId(); $this->cleanCache(); // prepare the cache (same request in the 'GMaps_pip->displayCategoryPageMap' function) $sql="SELECT DISTINCT pic.image_id, GROUP_CONCAT(DISTINCT pait.value ORDER BY pait.numID ASC SEPARATOR ';') AS coords FROM (((".USER_CACHE_CATEGORIES_TABLE." pucc LEFT JOIN ".CATEGORIES_TABLE." pct ON pucc.cat_id = pct.id) LEFT JOIN ".IMAGE_CATEGORY_TABLE." pic ON pic.category_id = pucc.cat_id) LEFT JOIN ".$prefixeTable."amd_images_tags pait ON pait.imageId = pic.image_id) LEFT JOIN ".$prefixeTable."amd_used_tags paut ON pait.numId = paut.numId WHERE pucc.user_id = '".$user['id']."' AND (paut.tagId = 'magic.GPS.LatitudeNum' OR paut.tagId = 'magic.GPS.LongitudeNum') AND pic.image_id IS NOT NULL $sqlCatRestrict GROUP BY pic.image_id"; /* * get_sql_condition_FandF( array ( 'forbidden_categories' => 'pic.category_id', 'visible_categories' => 'pic.category_id', 'visible_images' => 'pic.image_id' ), 'AND' ); */ $result=pwg_query($sql); if($result) { $massInsert=array(); while($row=pwg_db_fetch_assoc($result)) { $coords=explode(';', $row['coords']); $this->updateBounds($bounds, array( 'lat' => $coords[0], 'lng' => $coords[1] ) ); $massInsert[]=array( 'requestId' => $requestId, 'imageId' => $row['image_id'], 'latitude' => $coords[0], 'longitude' => $coords[1] ); } mass_inserts($this->tables['cache'], array('requestId', 'imageId', 'latitude', 'longitude'), $massInsert); $returned=$requestId; } } return($requestId); } /** * returns a list of markers * * * @param Array $datas : 'requestId' id of the request * 'bounds' (north, east, west, south) : only markers * inside the given bounds are returned * 'width' : width of maps in pixels * 'height' : height of maps in pixels * @return Array|String : a JSON string of an array of points (nbPictures, lat, lng) */ private function ajax_gmaps_public_mapsGetMarkers($datas) { global $user, $page, $conf; $returned=array( 'loadIndex' => $datas['loadIndex'], 'callId' => $datas['callId'], 'markers' => array(), 'datas' => array( 'nbPhotos' => 0 ) ); $this->updateCacheRequestId($datas['requestId']); $this->cleanCache(); /* * the 'make_picture_url' function use the 'get_root_url' function which * use the $page['root_path'] value * * here, this $page index doesn't exist, so we need to initialize it */ $page['root_path']=''; if($datas['bounds']['east']<$datas['bounds']['west']) { /* E = -xxx W = +xxx * a(lng E:+x) * b(lng W:-x) * * E +180/-180 W * (+) (-) * +-------------+---------+ * | : | * | : b | * | a : | * | : | * +-------------+---------+ * */ $lngClause=" longitude <= ".$datas['bounds']['east']." OR longitude >= ".$datas['bounds']['west']." "; } else { /* E = -xxx W = +xxx * a(lng W:-x) * b(lng E:+x) * * W 0 E * (-) (+) * +-------------+---------+ * | : | * | : b | * | a : | * | : | * +-------------+---------+ * */ $lngClause=" longitude >= ".$datas['bounds']['west']." AND longitude <= ".$datas['bounds']['east']." "; } $sql="SELECT DISTINCT pgc.latitude, pgc.longitude, pgc.imageId, GROUP_CONCAT(DISTINCT pic.category_id ORDER BY pic.category_id SEPARATOR ';') AS imageCatsId, GROUP_CONCAT(DISTINCT CONCAT(LPAD(pct.id,6,'0'), '-', pct.name) ORDER BY pct.id SEPARATOR ';') AS imageCatsNames, GROUP_CONCAT(DISTINCT IF(pct.permalink IS NULL, CONCAT('*', pct.id), pct.permalink) ORDER BY pct.id SEPARATOR ';') AS imageCatsPLink, pit.name AS imageName, pit.path FROM (((".$this->tables['cache']." pgc LEFT JOIN ".IMAGE_CATEGORY_TABLE." pic ON pic.image_id = pgc.imageId) LEFT JOIN ".USER_CACHE_CATEGORIES_TABLE." pucc ON pucc.cat_id = pic.category_id) LEFT JOIN ".CATEGORIES_TABLE." pct ON pucc.cat_id = pct.id) LEFT JOIN ".IMAGES_TABLE." pit ON pit.id = pgc.imageId WHERE pucc.user_id = '".$user['id']."' AND requestId='".$datas['requestId']."' AND ($lngClause) AND latitude >= ".$datas['bounds']['south']." AND latitude <= ".$datas['bounds']['north']." GROUP BY pgc.imageId ORDER BY latitude, longitude;"; $result=pwg_query($sql); if($result) { // compute ratio map size / bound size $ratioW=($datas['bounds']['east']-$datas['bounds']['west'])/$datas['width']; $ratioH=($datas['bounds']['north']-$datas['bounds']['south'])/$datas['height']; // works with the highest ratio $ratio=max($ratioW, $ratioH); $datas['distanceTreshold']=pow($datas['distanceTreshold']*$ratio,2); $groups=array(); /* * build groups */ while($row=pwg_db_fetch_assoc($result)) { $row['imageName'] =GPCCore::getUserLanguageDesc($row['imageName']); $row['imageTnFile']=str_replace(PHPWG_ROOT_PATH, './', DerivativeImage::thumb_url(array('id'=>$row['imageId'], 'path'=>$row['path']))); if(!$this->checkInGroup($row, $groups, $datas['distanceTreshold'])) { $groups[]=array($row); } } /* * for each group, calculate coordinates for a unique representative * point */ foreach($groups as $keyGroup => $group) { $coords=array( 'nbImgTxt' => '', 'nbImg' => 0, 'lat' => 0, // 'lat' rather than 'latitude' 'lng' => 0, // 'lng' rather than 'longitude' 'imgTn' => array(), 'imgCatsNames' => array(), 'imgName' => array(), 'imgCatsUrl' => array(), ); foreach($group as $point) { $tmpCatsUrl=array(); $tmpCatsId=explode(';', $point['imageCatsId']); $tmpCatsNames=explode(';', $point['imageCatsNames']); $tmpCatsPLinks=explode(';', $point['imageCatsPLink']); foreach($tmpCatsId as $key => $id) { $tmpCatsNames[$key]=GPCCore::getUserLanguageDesc(substr($tmpCatsNames[$key],7)); $tmpCatsUrl[]=$this->replaceRoot(str_replace(PHPWG_ROOT_PATH, './', make_picture_url( array( 'image_id' => $point['imageId'], 'category' => array( 'id' => $id, 'name' => $tmpCatsNames[$key], 'permalink' => (substr($tmpCatsPLinks[$key],0,1)=='*')?'':$tmpCatsPLinks[$key], ) ) ) )); } if(count($tmpCatsId)<=1) $tmpCatsNames=''; $coords['nbImg']++; $coords['lat']+=$point['latitude']; $coords['lng']+=$point['longitude']; $coords['imgTn'][]=$this->replaceRoot($point['imageTnFile']); $coords['imgCatsNames'][]=$tmpCatsNames; $coords['imgName'][]=GPCCore::getUserLanguageDesc($point['imageName']); $coords['imgCatsUrl'][]=$tmpCatsUrl; } $coords['lat']=$coords['lat']/count($group); $coords['lng']=$coords['lng']/count($group); $coords['uId']=md5($coords['lat'].$coords['lng']); if($coords['nbImg']==1) { $coords['nbImgTxt']=l10n('gmaps_1_picture'); } else { $coords['nbImgTxt']=sprintf(l10n('gmaps_nb_pictures'), $coords['nbImg']); } $returned['markers'][]=$coords; $returned['datas']['nbPhotos']+=$coords['nbImg']; } } if($returned['datas']['nbPhotos']>1) { $returned['datas']['nbPhotos']=sprintf(l10n('gmaps_nb_pictures'), $returned['datas']['nbPhotos']); } elseif($returned['datas']['nbPhotos']==1) { $returned['datas']['nbPhotos']=l10n('gmaps_1_picture'); } else { $returned['datas']['nbPhotos']=l10n('gmaps_0_picture'); } return(json_encode($returned)); } //ajax_gmaps_public_mapsGetMarkers /** * check if a point is inside an existing group, and add it * @param Array $point : the point * @param Array &$groups : the groups * @param Float $maxDist : maximum distance to be inside group * @return Bool : true if added in a group, otherwise false */ private function checkInGroup($point, &$groups, $maxDist) { foreach($groups as $key=>$group) { $dist=pow($point['latitude']-$group[0]['latitude'],2) + pow($point['longitude']-$group[0]['longitude'],2); if($dist<=$maxDist) { $groups[$key][]=$point; return(true); } } return(false); } /** * replace the root directroy for image * => './_data/i/galleries/' becomes "G" * => './i.php?/galleries/' becomes "I" * => './picture.php?/' becomes "P" * => other case : no chnages * * @param String $value : path * @returned String : */ private function replaceRoot($value) { if(preg_match('/^\.\/_data\/i\/galleries\//i', $value)>0) { return("G".substr($value,20)); } if(preg_match('/^\.\/i\.php\?\/galleries\//i', $value)>0) { return("I".substr($value,19)); } if(preg_match('/^\.\/picture\.php\?\//i', $value)>0) { return("P".substr($value,15)); } return($value); } } //class $returned=new GMaps_Ajax($prefixeTable, __FILE__); ?>