css_user[] = '/data/_addoncode/'.$addonFolderName.'/multi_site.css'; //$page->head_js[] = '/data/_addoncode/'.$addonFolderName.'/multi_site.js'; $page->head_js[] = '/include/js/admin_users.js'; $page->admin_links[] = array('Admin_Site_Setup','Multi-Site Home'); $page->admin_links[] = array('Admin_Site_Setup','New Installation','cmd=new'); $page->admin_links[] = array('Admin_Site_Setup','Settings','cmd=settings'); $page->admin_links[] = array('Admin_Site_Setup','About','cmd=about'); $_REQUEST += array('install'=>array()); //ftp setup $this->GetSiteData(); $hide = false; $cmd = \gp\tool::GetCommand(); switch($cmd){ case 'about': $this->About(true); $hide = true; break; case 'installed': $this->ShowSites(); $hide = true; break; /* settings */ case 'settings': $this->SettingsForm($this->siteData); $hide = true; break; case 'Save Settings': if( !$this->SaveSettings() ){ $this->SettingsForm($_POST); $hide = true; } break; case 'save_options': case 'options': $this->Options($cmd); $hide = true; break; case 'uninstall': $this->UninstallSite(); break; /* * New Installation */ case 'new'; case 'Install Now': case 'new_plugins': case 'new_install': case 'new_destination': case 'new_themes': case 'Continue': $this->InstallStatus($cmd); $hide = true; break; case 'subfolder': $this->SubFolder(); $this->InstallStatus($cmd); $hide = true; break; case 'expandfolder': $this->ExpandFolder(); $hide = true; return; case 'newfolder': $this->NewFolder(); $hide = true; break; case 'Delete Folder': $hide = true; $this->RemoveDir(); break; case 'rmdir': $hide = true; $this->RemoveDirPrompt(); break; } if( !$hide ){ $this->FrontPage(); } } public function Options($cmd = ''){ global $langmessage; $site =& $_REQUEST['site']; if( !isset($this->siteData['sites'][$site]) ){ msg($langmessage['OOPS']); return false; } switch($cmd){ case 'save_options'; $this->Options_Save($site); break; } $args = $_POST + $this->siteData['sites'][$site] + array('url'=>'http://'); echo '
'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo $langmessage['options']; echo ': '.$site; echo '
'; echo $langmessage['site url']; echo ''; echo ''; echo '
'; echo $langmessage['hide_index']; echo ''; if( \gp\tool\RemoteGet::Test() ){ if( isset($args['hide_index']) ){ echo ''; }else{ echo ''; } }else{ echo 'Unavailable: Your php installation doesn\'t support the necessary functions to enable this option.'; } echo '
'; echo '
'; echo ''; echo ''; echo ''; echo ' '; echo '
'; echo '

'; echo \gp\tool::Link('Admin_Site_Setup',$langmessage['back']); echo '

'; echo '
'; echo '
'; } public function Options_Save($site){ global $langmessage; $save = $this->Options_SiteUrl($site); $save = $save && $this->Options_htaccess($site); if( $save ){ $this->SaveSiteData(); msg($langmessage['SAVED']); } } public function Options_htaccess($site){ global $langmessage; if( !\gp\tool\RemoteGet::Test() ){ return; } $site_info = $this->siteData['sites'][$site]; $site_url = $site_info['url']; $site_uniq = false; if( isset($site_info['gpuniq']) ){ $site_uniq = $site_info['gpuniq']; } $file_path = $site.'/.htaccess'; if( file_exists($file_path) ){ $original_contents = $contents = file_get_contents($file_path); } if( !isset($_POST['hide_index']) ){ $to_hide_index = false; unset($this->siteData['sites'][$site]['hide_index']); $prefix = ''; }else{ if( empty($site_url) ){ msg('A valid site url is required to hide index.php'); return false; } $array = @parse_url($site_url); $prefix =& $array['path']; $to_hide_index = $this->siteData['sites'][$site]['hide_index'] = true; } //add the gpeasy rules \gp\admin\Settings\Permalinks::StripRules($contents); $contents .= \gp\admin\Settings\Permalinks::Rewrite_Rules($to_hide_index,$prefix); if( !\gp\tool\Files::Save($file_path,$contents) ){ msg($langmessage['OOPS'].' (Couldn\'t save .htaccess)'); return false; } //check for valid response when hiding index.php if( $to_hide_index ){ $check_url = $site_url.'/Special_Site_Map'; $result = \gp\tool\RemoteGet::Get_Successful($check_url); if( !$result ){ msg('Did not recieve valid response when fetching url without index.php: '.htmlspecialchars($check_url)); \gp\tool\Files::Save($file_path,$original_contents); return false; } } return true; } public function Options_SiteUrl($site){ global $langmessage; if( empty($_POST['url']) ){ unset($this->siteData['sites'][$site]['url']); return true; } $site_url = $_POST['url']; //remove index.php $pos = strpos($site_url,'/index.php'); if( $pos ){ $site_url = substr($site_url,0,$pos); } if( $site_url == 'http://' ){ msg($langmessage['OOPS'].' (Invalid URL)'); return false; } $array = @parse_url($site_url); if( $array === false ){ msg($langmessage['OOPS'].' Invalid URL'); return false; } if( empty($array['scheme']) ){ $site_url = 'http://'.$site_url; } $this->siteData['sites'][$site]['url'] = rtrim($site_url,'/'); return true; } public function FrontPage(){ global $langmessage; $this->Heading(); echo '
'; echo '
'; echo 'Add multiple installations of gpEasy to your server.'; echo '
'; echo '
'; echo ''; $this->ShowSimple(); } /** * About this plugin * */ public function About($full){ global $langmessage; $this->Heading($langmessage['about']); echo '
'; echo '

'; echo $langmessage['easily add installations']; echo $langmessage['multi_site_notes']; echo '

'; echo '
'; echo '

'; echo \gp\tool::Link('Admin_Site_Setup',$langmessage['Settings'],'cmd=settings'); echo '

'; echo '
'; echo '
Service Provider ID
'; echo '
When your provider id is entered, gpEasy.com Services can attribute each installation to your service.
'; echo '
'; echo '
'; echo '
Service Provider Name
'; echo '
Displayed on the site map of your hosted installations.
'; echo '
'; echo '

'; echo \gp\tool::Link('Admin_Site_Setup',$langmessage['back']); echo '

'; } public function SaveSettings(){ global $langmessage; $UpdateIndexFiles = false; //ftp information $ok_to_save = $this->SaveFTPInformation(); //provider id if( !empty($_POST['service_provider_id']) ){ if( is_numeric($_POST['service_provider_id']) ){ //update index.php files if( !isset($this->siteData['service_provider_id']) || ($_POST['service_provider_id'] != $this->siteData['service_provider_id']) ){ $UpdateIndexFiles = true; } $this->siteData['service_provider_id'] = $_POST['service_provider_id']; }else{ msg('The Service Provider ID must be a number.'); $ok_to_save = false; } } //provider name if( !empty($_POST['service_provider_name']) ){ //update index.php files if( !isset($this->siteData['service_provider_name']) || ($_POST['service_provider_name'] != $this->siteData['service_provider_name']) ){ $UpdateIndexFiles = true; } $this->siteData['service_provider_name'] = $_POST['service_provider_name']; } if( $UpdateIndexFiles ){ $this->UpdateProviderID(); } if( !$ok_to_save ){ return false; } if( $this->SaveSiteData() ){ msg($langmessage['SAVED']); return true; } msg($langmessage['OOPS']); return false; } public function UpdateProviderID(){ foreach($this->siteData['sites'] as $path => $info){ if( !isset($info['unique']) ){ $info['unique'] = $this->NewId(); } $this->CreateIndex($path,$info['unique']); } $this->SaveSiteData(); } public function SettingsForm($values=array()){ global $langmessage,$config; $values += array('service_provider_id'=>'','service_provider_name'=>''); $ftp_vals = $_POST + $config + array('ftp_server'=>\gp\tool\FileSystemFtp::GetFTPServer(),'ftp_user'=>''); $this->Heading('Settings'); echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; if( function_exists('ftp_connect') ){ echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
Service Provider Identification
'; echo 'When your provider id is entered, gpEasy.com Services can attribute each installation to your service.'; echo '
'; echo 'Service Provider ID'; echo ''; echo ''; echo '
'; echo 'Service Provider Name'; echo ''; echo ''; echo '
  
FTP 
FTP Server'; echo ''; echo '
FTP Username'; echo ''; echo '
FTP Password'; echo ''; echo '
'; echo '
'; echo ''; echo ' '; echo '
'; echo '
'; } public function ShowSimple(){ global $langmessage; if( !isset($this->siteData['sites']) || (count($this->siteData['sites']) == 0) ){ return; } echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; $reverse = array_reverse($this->siteData['sites']); $i = 0; foreach($reverse as $site => $data){ $this->ShowRow($site,$data); $i++; if( $i == 5 ){ break; } } $this->SearchRow(); echo '
'; echo 'Recent Installations'; echo ''; echo 'URL'; echo ''; echo $langmessage['options']; echo '
'; if( count($this->siteData['sites']) > 5 ){ echo '

'; echo \gp\tool::Link('Admin_Site_Setup','More Installations','cmd=installed'); echo '

'; } } public function ShowRow(&$site,&$data){ global $langmessage; echo ''; echo ''; if( strlen($site) > 25 ){ echo '...'.substr($site,-21);; }else{ echo $site; } echo ''; echo ''; if( !empty($data['url']) ){ echo ''; if( strlen($data['url']) > 40 ){ echo substr($data['url'],0,35).'...'; }else{ echo $data['url']; } echo ''; } echo ''; echo ''; echo \gp\tool::Link('Admin_Site_Setup',$langmessage['options'],'cmd=options&site='.urlencode($site)); echo '   '; //echo \gp\tool::Link('Admin_Site_Setup',$langmessage['uninstall'],'cmd=uninstall&site='.urlencode($site),' data-cmd="gpajax"'); $title = sprintf($langmessage['generic_delete_confirm'],' "'.htmlspecialchars($site).'" '); echo \gp\tool::Link('Admin_Site_Setup',$langmessage['uninstall'],'cmd=uninstall&site='.urlencode($site),array('data-cmd'=>'postlink','class'=>'gpconfirm','title'=>$title)); echo ''; echo ''; } public function ShowSites(){ global $langmessage; $limit = 20; //20 $offset = 0; if( isset($_GET['offset']) && is_numeric($_GET['offset']) ){ $offset = $_GET['offset']; } if( !isset($this->siteData['sites']) || (count($this->siteData['sites']) == 0) ){ return; } echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; $this->SearchRow(); $reverse = array_reverse($this->siteData['sites']); if( !empty($_GET['q']) ){ $reverse = $this->Search($reverse); if( count($reverse) == 0 ){ echo ''; echo ''; echo ''; } } if( $offset > 0 ){ $reverse = array_splice($reverse,$offset); } $i = 0; foreach($reverse as $site => $data){ $this->ShowRow($site,$data); $i++; if( $i == $limit ){ break; } } echo '
'; echo 'Recent Installations'; echo ''; echo 'URL'; echo ''; echo ' '; echo '
'; echo 'Could not find any installations matching your search criteria.'; echo '
'; echo '
'; //navigation links if( $offset > 0 ){ echo \gp\tool::Link('Admin_Site_Setup','Prev','cmd=installed&q='.urlencode($_GET['q']).'&offset='.max(0,$offset-$limit)); }else{ echo 'Prev'; } echo '   '; if( count($reverse) > $limit ){ echo \gp\tool::Link('Admin_Site_Setup','Next','cmd=installed&q='.urlencode($_GET['q']).'&offset='.($offset+$limit)); }else{ echo 'Next'; } echo '

'; echo \gp\tool::Link('Admin_Site_Setup',$langmessage['back']); echo '

'; } public function Search(&$array){ $result = array(); $key = $_GET['q']; foreach($array as $path => $info){ if( strpos($path,$key) !== false ){ $result[$path] = $info; continue; } } return $result; } public function SearchRow(){ $_GET += array('q'=>''); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } /** * Remove the files and folders of an installation as determined by the post request * */ public function UninstallSite(){ global $langmessage, $config; $site =& $_POST['site']; if( empty($site) ){ return false; } if( !isset($this->siteData['sites'][$site]) ){ msg($langmessage['OOPS'].' (Invalid Site)'); return false; } if( !$this->RmSite($site) ){ msg($langmessage['OOPS'].'(Files not completely removed)'); return false; } msg($langmessage['SAVED']); unset($this->siteData['sites'][$site]); $this->SaveSiteData(); } /** * Remove the files and folders of an installation * */ public function RmSite($site){ global $config; if( !$this->EmptyDir($site) ){ return false; } return $this->RmDir($site); } /** * Remove a folder that was created by the multi-site manager * */ public function RmDir($dir){ global $config; if( @rmdir($dir) ){ return true; } if( empty($config['ftp_server']) ){ return false; } if( !function_exists('ftp_connect') ){ return false; } $conn_id = self::FTPConnect(); if( !$conn_id ){ return false; } $ftp_site = \gp\tool\FileSystemFtp::GetFTPRoot($conn_id,$dir); if( $ftp_site === false ){ return false; } return ftp_rmdir($conn_id,$ftp_site); } /** * Remove all the contents of a directory * */ public function EmptyDir($dir){ if( !file_exists($dir) ){ return true; } if( is_link($dir) ){ return unlink($dir); } $dh = @opendir($dir); if( !$dh ){ return false; } $dh = @opendir($dir); if( !$dh ){ return false; } $success = true; $subDirs = array(); while( ($file = readdir($dh)) !== false){ if( $file == '.' || $file == '..' ){ continue; } $fullPath = $dir.'/'.$file; if( is_link($fullPath) ){ if( !unlink($fullPath) ){ $success = false; } continue; } if( is_dir($fullPath) ){ $subDirs[] = $fullPath; continue; } if( !unlink($fullPath) ){ $success = false; } } closedir($dh); foreach($subDirs as $subDir){ if( !$this->EmptyDir($subDir) ){ $success = false; } if( !\gp\tool\Files::RmDir($subDir) ){ $success = false; } } return $success; } public function GetSiteData(){ global $addonPathData; $this->dataFile = $addonPathData.'/data.php'; $this->siteData = \gp\tool\Files::Get($this->dataFile,'siteData'); $this->siteData += array('sites'=>array()); $this->checksum = $this->CheckSum($this->siteData); } public function SaveSiteData(){ $check = $this->CheckSum($this->siteData); if( $check === $this->checksum ){ return true; } unset($this->siteData['destination']); //no longer used return \gp\tool\Files::SaveData( $this->dataFile,'siteData',$this->siteData ); } public function CheckSum($array){ return crc32( serialize($array) ); } public function CreatePlugins($destination,$args = false){ global $rootDir; if( $args === false ){ $args = $_POST; } //may be valid even if plugins is not set $args += array('plugins'=>array()); //selection of themes if( !\gp\tool\Files::CheckDir($destination.'/addons') ){ msg('Failed to create '.$destination.'/addons'.''); return false; } foreach($args['plugins'] as $plugin){ $target = $rootDir.'/addons/'.$plugin; if( !file_exists($target) ){ continue; } $name = $destination.'/addons/'.$plugin; $this->Create_Symlink($target,$name); } return true; } //Don't create symlink for /themes, users may want to add to their collection of themes public function CopyThemes($destination,$args=false){ global $rootDir; if( $args === false ){ $args = $_POST; } //selection of themes if( !\gp\tool\Files::CheckDir($destination.'/themes') ){ msg('Failed to create '.$destination.'/themes'.''); return false; } $count = 0; foreach($args['themes'] as $theme){ $target = $rootDir.'/themes/'.$theme; if( !file_exists($target) ){ continue; } $name = $destination.'/themes/'.$theme; if( $this->Create_Symlink($target,$name) ){ $count++; } } if( $count == 0 ){ msg('Failed to populate '.$destination.'/themes'.''); return false; } return true; } //create the index.php file public function CreateIndex($destination,$unique){ $path = $destination.'/index.php'; $indexA = array(); $indexA[] = '<'.'?'.'php'; if( isset($this->siteData['service_provider_id']) ){ $indexA[] = 'define(\'service_provider_id\',\''.(int)$this->siteData['service_provider_id'].'\');'; } if( isset($this->siteData['service_provider_name']) ){ $indexA[] = 'define(\'service_provider_name\',\''.addslashes($this->siteData['service_provider_name']).'\');'; } $indexA[] = 'define(\'multi_site_unique\',\''.$unique.'\');'; $indexA[] = 'require_once(\'include/main.php\');'; $index = implode("\n",$indexA); if( !\gp\tool\Files::Save($path,$index) ){ return false; } @chmod($path,0644); //to prevent 500 Internal Server Errors on some servers return true; } public function NewId(){ do{ $unique = \gp\tool::RandomString(20); foreach($this->siteData['sites'] as $array){ if( isset($array['unique']) && ($array['unique'] == $unique) ){ $unique = false; break; } } }while($unique==false); return $unique; } //create a symbolic link and test for $test_file public function Create_Symlink($target,$path,$test_file = false ){ echo '
  • Create Symlink: '.$path.'
  • '; if( !symlink($target,$path) ){ msg('Oops, Symlink creation failed (1)'); return false; } if( $test_file && !file_exists($path.'/'.$test_file) ){ msg('Oops, Symlink creation failed (2)'); return false; } return true; } /** * Save the ftp connection information if a connection can be made * */ public function SaveFTPInformation(){ global $config, $langmessage; $_POST += array('ftp_server'=>'','ftp_user'=>'','ftp_pass'=>''); //try to connect and login if ftp_server is not empty if( !empty($_POST['ftp_server']) ){ $conn_id = @ftp_connect($_POST['ftp_server'],21,6); if( !$conn_id ){ msg('Oops, could not connect using ftp_connect() for server '.htmlspecialchars($_POST['ftp_server']).''); return false; } ob_start(); $login_result = @ftp_login($conn_id,$_POST['ftp_user'],$_POST['ftp_pass'] ); if( !$login_result ){ msg('Oops, could not login using ftp_login() for server '.$_POST['ftp_server'].' and user '.$_POST['ftp_user'].''); @ftp_close($conn_id); ob_end_clean(); return false; } @ftp_close($conn_id); ob_end_clean(); } $config['ftp_user'] = $_POST['ftp_user']; $config['ftp_server'] = $_POST['ftp_server']; $config['ftp_pass'] = $_POST['ftp_pass']; if( !admin_tools::SaveConfig() ){ msg('Oops, there was an error saving your ftp information.'); return false; } return true; } /* * New Installation Functions * * * */ public function InstallStatus($cmd){ global $rootDir; $default_theme = explode('/',gp_default_theme); //make sure default theme exists $path = $rootDir.'/themes/'.$default_theme[0]; if( !file_exists($path) ){ msg('The default theme for gpEasy "'.$default_theme[0].'" does not exist. Please make sure it exists before continuing.'); return; } if( empty($cmd) || $cmd == 'Continue' ){ $cmd = false; }elseif( $cmd == 'Install Now' ){ if( $this->NewCreate() ){ return; }else{ $cmd = false; } } $this->CheckFolder(); $this->Heading('Installation'); $this->InstallStatus_Steps($cmd); echo '
    '; switch($cmd){ case 'subfolder': case 'new': case 'new_destination': $this->NewDestination(); break; case 'new_themes': $this->NewThemes($_REQUEST['install']); break; case 'new_plugins': $this->NewPlugins($_REQUEST['install']); break; case 'new_install': $this->NewInstall(); break; } echo '
    '; } public function InstallStatus_Steps(&$cmd){ echo '
    '; echo '
    '; echo ''; echo '
    '; echo '
    '; } /** * Show an installation step and it's status * */ public function InstallStatus_Step(&$cmd,$ready,$label,$step_cmd,$step_key,$step_key2=false){ $class = 'ready'; if( isset($_REQUEST['install'][$step_key]) ){ $step_value = $_REQUEST['install'][$step_key]; if( is_array($step_value) ){ $link_label = implode(', ',$step_value); if( strlen($link_label) > 40 ){ $link_label = substr($link_label,0,40).'...'; } }else{ $link_label = $step_value; } }elseif( $step_key2 && isset($_REQUEST['install'][$step_key2]) ){ $link_label = 'Empty'; }else{ $ready = false; $query_array = array('cmd'=>'new_destination'); $link_label = 'Not Set'; $class = ''; if( !$cmd ){ $cmd = $step_cmd; } } if( empty($link_label) ){ $link_label = 'Empty'; } $query_array = array('cmd'=>$step_cmd); echo '
  • '; echo $this->InstallLink($label.': '.$link_label,$query_array); echo '
  • '; return $ready; } /** * Make sure the install folder is writable before continuing with the installation process * */ public function CheckFolder(){ global $config; if( empty($_REQUEST['install']['folder']) ){ return; } $folder = $_REQUEST['install']['folder']; if( is_writable($folder) ){ return true; } if( !function_exists('ftp_connect') ){ $this->FolderNotWritable('FTP Extension Not Available'); return false; } if( empty($config['ftp_server']) ){ $this->FolderNotWritable('FTP connection values not set'); return false; } $conn_id = self::FTPConnect(); if( !$conn_id ){ $this->FolderNotWritable('FTP connection could not be made with the supplied values'); return false; } $ftp_root = \gp\tool\FileSystemFtp::GetFTPRoot($conn_id,$folder); if( $ftp_root === false ){ $this->FolderNotWritable('Root folder not found by FTP'); return false; } } /** * Display message to user about not being able to write to the installation folder * */ public function FolderNotWritable($reason = ''){ global $langmessage; $message = '

    Sorry, the selected folder could not be written to.

    '; $message .= ''.$reason.' '; $message .= '

    You may still be able to install in this folder by doing one of the following:

    '; $message .= ''; //msg($langmessage['not_created'].' (FTP Connection Failed)'); unset($_REQUEST['install']['folder']); msg($message); } public function NewInstall(){ echo '
    '; echo ''; \gp\install\Tools::Form_UserDetails(); echo '
    '; $this->InstallFields($_REQUEST['install'],'install'); echo '
    '; echo ''; echo ' '; echo '
    '; echo '
    '; } public function NewDestination(){ global $rootDir,$config; if( empty($this->siteData['last_folder']) ){ $folder = $rootDir; }else{ $folder = $this->siteData['last_folder']; } $this->InstallFolder($folder); } /** * Display form for selecting which themes should be included * */ public function NewThemes($values=array()){ global $rootDir; if( !isset($values['themes']) ){ $values += array('all_themes'=>'all'); } $values += array('themes'=>array()); $all_themes = false; if( isset($values['all_themes']) && $values['all_themes'] == 'all' ){ $all_themes = true; } echo '
    '; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
    Select Themes
    '; echo '
    '; echo 'Select which themes will be available to the new installation. '; echo '
    '; echo '
    '; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
    '; $checked = ''; if( $all_themes ){ $checked = ' checked="checked" '; } echo ' '; echo '
    '; echo ' OR '; echo '
    '; $default_theme = explode('/',gp_default_theme); //default theme echo ''; echo ''; //all other available themes echo ' And ...
    '; echo '

    '; $dir = $rootDir.'/themes'; $layouts = \gp\tool\Files::readDir($dir,1); asort($layouts); $i = 1; foreach($layouts as $name){ if( $name == $default_theme[0] ){ continue; } $checked = ''; if( $all_themes || (array_search($name,$values['themes']) > 0) ){ $checked = ' checked="checked" '; } echo ''; } echo '

    '; echo '
    '; echo '
    '; $this->InstallFields($_REQUEST['install'],'install'); echo '
    '; echo ' '; echo ' '; echo '
    '; echo '
    '; } public function NewPlugins($values = array()){ global $rootDir; $values += array('plugins'=>array()); echo '
    '; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
    Select Plugins
    '; echo '
    '; echo 'Select which plugins will be available to the new installation. Note, selected plugins will not be installed.'; echo '
    '; echo '
    '; $dir = $rootDir.'/addons'; $addons = \gp\tool\Files::readDir($dir,1); $i = 1; foreach($addons as $addon){ $checked = ''; if( array_search($addon,$values['plugins']) > 0 ){ $checked = ' checked="checked" '; } echo ''; } echo '
    '; $this->InstallFields($_REQUEST['install'],'install'); echo '
    '; echo ''; echo ' '; echo '
    '; echo ''; echo '
    '; } public function InstallLink($label,$query_array=array(),$attr=''){ return ''.\gp\tool::Ampersands($label).''; } public function InstallUrl($query_array=array()){ $query_array += array('install'=>array()); $query_array['install'] = $query_array['install'] + $_REQUEST['install']; $query = http_build_query($query_array); return \gp\tool::GetUrl('Admin_Site_Setup',$query); } public function InstallFields($array,$key=null){ foreach($array as $k => $v){ if( !empty($key) || ($key === 0) ){ $k = $key.'['.urlencode($k).']'; } if (is_array($v) || is_object($v)) { $this->InstallFields($v,$k); } else { echo ''; } } } /** * Display window for selecting where to install * */ public function InstallFolder($destination){ echo ''; echo '
    '; echo 'Select Destination Folder'; echo '
    '; $previous = $destination; $parent = $destination; $links = array(); do{ $previous = $parent; $query_array = array('cmd'=>'expandfolder','folder'=>$parent); $links[] = $this->InstallLink(basename($parent).'/',$query_array,' data-cmd="gpajax" '); $parent = dirname($parent); }while( $previous != $parent ); echo '
    '; $links = array_reverse($links); echo implode('',$links); $query_array = array('cmd'=>'expandfolder','folder'=>$destination); echo '
    '; echo ''; echo ''; echo '
    '; echo '
    '; //show subfolders echo '
    '; echo '
    '; $this->InstallFolders($destination); echo '
    '; echo '
    '; echo '
    '; echo ''; echo '
    '; echo '
    '; } public function InstallFolders($dir){ if( !is_readable($dir) ){ echo '

    '; echo 'Sorry, '.$dir.' is not readable.'; echo '

    '; return; } $subdirs = \gp\tool\Files::readDir($dir,1); echo ''; } public function GetSubdirs($dir){ global $config; if( is_readable($dir) ){ return \gp\tool\Files::readDir($dir,1); } return false; } public function FolderLink($base,$full,$class){ echo ''; $this->InstallLinks($full); $query_array = array('cmd'=>'expandfolder','folder'=>$full); echo $this->InstallLink($base,$query_array,' data-cmd="gpajax" rel="'.htmlspecialchars($full).'" '); echo ''; } /* * Check for /addons, /data, /include, /themes and /index.php */ public function InstallLinks($dir){ global $config; $check_short = array('addons','data','include','themes','index.php'); $failed = array(); //readable if( !is_readable($dir) ){ return false; } //existing contents foreach($check_short as $short){ $check_full = rtrim($dir,'/').'/'.$short; if( file_exists($check_full) ){ $failed[] = $check_short; } } if( count($failed) > 0 ){ return false; } $query_array = array('cmd'=>'Continue','install'=>array('folder'=>$dir)); echo $this->InstallLink('Install Here',$query_array,' class="select" '); $query_array = array('cmd'=>'rmdir','dir'=>$dir); echo $this->InstallLink('Delete',$query_array,' class="rm" data-cmd="gpajax" '); } /** * Show the contents of folder * */ public function ExpandFolder(){ global $page, $langmessage,$config; $_REQUEST += array('install'=>array()); $page->ajaxReplace = array(); $page->ajaxReplace[] = 'messages'; $folder =& $_REQUEST['folder']; if( empty($folder) || !file_exists($folder) || !is_dir($folder) ){ msg($langmessage['OOPS']); return; } $this->LoadFolder($folder); } /** * Go to a user supplied sub directory in the browser * */ public function SubFolder(){ global $langmessage; $folder =& $_REQUEST['folder']; if( !empty($_REQUEST['sub_dir']) ){ $folder .= '/'.$_REQUEST['sub_dir']; } if( empty($folder) || !file_exists($folder) || !is_dir($folder) ){ msg($langmessage['OOPS']); return; } $this->LoadFolder($folder); } public function LoadFolder($folder){ global $page; ob_start(); echo $this->InstallFolder($folder); $content = ob_get_clean(); $page->ajaxReplace[] = array('inner','#install_step',$content); //save the folder location if( !isset($this->siteData['last_folder']) || $this->siteData['last_folder'] !== $folder ){ $this->siteData['last_folder'] = $folder; unset($this->siteData['last_folder_ftp']); $this->SaveSiteData(); } } /* * Create a new folder * */ public function NewFolder(){ global $page, $langmessage; $page->ajaxReplace = array(); $page->ajaxReplace[] = 'messages'; $folder =& $_POST['folder']; if( empty($folder) || !file_exists($folder) || !is_dir($folder) ){ msg($langmessage['OOPS']. ' (Parent Dir)'); return false; } $new_name =& $_POST['new_folder']; if( empty($new_name) ){ msg($langmessage['OOPS']. ' (Empty Name)'); return false; } $new_name = trim($new_name,'/\\'); $folder = rtrim($folder,'/\\'); $new_folder = $folder.'/'.$new_name; if( file_exists($new_folder) ){ msg($langmessage['OOPS']. ' (Already Exists)'); return false; } if( !$this->MakeDir($folder,$new_name) ){ return false; } $this->ExpandFolder(); } public function RemoveDirPrompt(){ global $page, $langmessage; $page->ajaxReplace = array(); $page->ajaxReplace[] = 'messages'; $dir = $_REQUEST['dir']; if( !$this->RemoveDirCheck($dir) ){ return; } ob_start(); echo '
    '; echo '
    '; echo ''; echo ''; $this->InstallFields($_REQUEST['install'],'install'); echo sprintf($langmessage['generic_delete_confirm'],''.htmlspecialchars($dir).''); echo '

    '; echo ''; echo ' '; echo '

    '; echo '
    '; echo '
    '; $content = ob_get_clean(); $page->ajaxReplace[] = array('gpabox','',$content); } public function RemoveDir(){ global $page, $langmessage; $page->ajaxReplace = array(); $page->ajaxReplace[] = 'messages'; $dir = $_POST['dir']; if( !$this->RemoveDirCheck($dir) ){ return; } $parent = dirname($dir); if( !$this->RmDir($dir) ){ msg($langmessage['OOPS']); return; } $this->LoadFolder($parent); } public function RemoveDirCheck($dir){ global $langmessage; if( empty($dir) || !file_exists($dir) || !is_dir($dir) ){ msg($langmessage['OOPS'].' (Invalid)'); return false; } $dh = @opendir($dir); if( !$dh ){ msg($langmessage['OOPS'].' (Not Readable)'); return false; } $count = 0; while( ($file = readdir($dh)) !== false){ if( $file == '.' || $file == '..' ){ continue; } closedir($dh); msg($langmessage['dir_not_empty']); return false; } closedir($dh); return true; } public function MakeDir($parent,$new_name){ global $config, $langmessage; $langmessage['not_created'] = 'Oops, the folder could not be created. '; $langmessage['not_created'] .= 'You may still be able to create it by doing one of the following: '; $langmessage['not_created'] .= ''; $langmessage['not_created'] = sprintf($langmessage['not_created'],\gp\tool::GetUrl('Admin_Site_Setup','cmd=settings')); $new_folder = $parent.'/'.$new_name; if( mkdir($new_folder,0755) ){ chmod($new_folder,0755); //some systems need more than just the 0755 in the mkdir() function return true; } if( $this->HasFTP() ){ msg($langmessage['not_created']); return false; } $conn_id = self::FTPConnect(); if( !$conn_id ){ msg($langmessage['not_created'].' (FTP Connection Failed)'); return false; } $ftp_parent = \gp\tool\FileSystemFtp::GetFTPRoot($conn_id,$parent); if( $ftp_parent === false ){ msg('Oops, could not find the ftp location of '.$parent.' using the current ftp login.'); return false; } $ftp_destination = $ftp_parent.'/'.$new_name; if( !ftp_mkdir($conn_id,$ftp_destination) ){ msg('Oops, could not create the folder using the current ftp login.'); return false; } ftp_site($conn_id, 'CHMOD 0755 '. $ftp_destination ); return true; } public function NewCreate(){ global $rootDir,$config,$checkFileIndex; global $dataDir; //for SaveTitle(), SaveConfig() $_POST += array('themes'=>array(),'plugins'=>array()); $destination = $_REQUEST['install']['folder']; $this->site_uniq_id = $this->NewId(); $checkFileIndex = false; //prevent reposting if( isset($this->siteData['sites'][$destination]) ){ msg('Oops, there\'s already an installation in '.htmlspecialchars($destination)); return false; } echo ''; if( $destination ){ $this->EmptyDir($destination); } } public function Install_Success(){ echo ''; echo '

    '; echo 'Installation was completed successfully. '; //show the options $_REQUEST['site'] = $_REQUEST['install']['folder']; $this->Options(); } /** * Return true if FTP can be used * */ public function HasFTP(){ global $config; if( empty($config['ftp_server']) || !function_exists('ftp_connect') ){ return false; } return true; } /** * Multi Site Heading * */ public function Heading($sub_heading=false){ echo '

    '; echo \gp\tool::Link('Admin_Site_Setup','Multi-Site'); if( $sub_heading ){ echo ' » '; echo $sub_heading; } echo '

    '; } /** * FTP Functions * */ public static function FTPConnect(){ global $config; static $conn_id = false; if( $conn_id ){ return $conn_id; } if( empty($config['ftp_server']) ){ return false; } $conn_id = @ftp_connect($config['ftp_server'], 21, 6); if( !$conn_id ){ //trigger_error('ftp_connect() failed for server : '.$config['ftp_server']); return false; } $login_result = @ftp_login($conn_id, $config['ftp_user'], $config['ftp_pass']); if( !$login_result ){ //trigger_error('ftp_login() failed for server : '.$config['ftp_server'].' and user: '.$config['ftp_user']); return false; } register_shutdown_function(array('SetupSite', 'ftpClose'), $conn_id); return $conn_id; } public static function ftpClose($connection=false){ if( $connection !== false ){ @ftp_quit($connection); } } }