00001 <?php
00009
00010 define('__PRAGYAN_CMS',')$!%^!%#^@');
00011
00012 global $sourceFolder;
00013 global $scriptPath;
00014 global $URL_REWRITE;
00015 $sourceFolder = 'cms';
00016 $installFolder = '.';
00017 $URL_REWRITE = 'false';
00018 $cmsFolder = "../$sourceFolder";
00019 $templateFolder = "$cmsFolder/templates/crystalx";
00020 $scriptPathWithFolder = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/'));
00021 $scriptPath = substr($scriptPathWithFolder , 0, strrpos($scriptPathWithFolder , '/'));
00022
00023
00024
00025 require_once($cmsFolder."/common.lib.php");
00026 define('CMS_SETUP', true);
00027
00028
00029 $installPageNumber = 2;
00030 $prerequisiteText = CheckPrerequisites();
00031 if ($prerequisiteText != '')
00032 $installPageNumber = 1;
00033 elseif (isset($_POST['btnSubmitSettings']))
00034 $installPageNumber = 3;
00035
00036 if ($installPageNumber == 1) {
00037 $installPageContent = $prerequisiteText;
00038 }
00039 else if ($installPageNumber == 2) {
00040 include_once('settingsform.php');
00041 $installPageContent = $settingsForm;
00042
00043 }
00044 else if ($installPageNumber == 3) {
00045 $successImage = "<img src=\"$installFolder/images/instsuccess.png\" alt=\"Successful\" />";
00046 $errorImage = "<img src=\"$installFolder/images/insterror.png\" alt=\"Aborted\" />";
00047 $installationProgress = installCMS();
00048 $installationErrors = '';
00049 $installPageContent = '';
00050 for ($i = 0; $i < count($installationProgress); ++$i) {
00051 $installPageContent .= "<tr><td width=\"100%\">{$installationProgress[$i][1]}</td><td>";
00052 if ($installationProgress[$i][2] !== true) {
00053 $installPageContent .= $errorImage;
00054 if (isset($installationProgress[$i][3]))
00055 $installationErrors = $installationProgress[$i][3] . "<a href=\"javascript: history.back()\">Click here</a> to go back.</a>";
00056 }
00057 else
00058 $installPageContent .= $successImage;
00059 $installPageContent .= "</td></tr>\n";
00060 }
00061
00062
00063
00064 $installPageContent = "<br /><table width=\"100%\" border=\"0\">\n $installPageContent </table>\n $installationErrors";
00065 if ($installationErrors == '') {
00066 $installPageContent .= <<<HTTPDCONF
00067 <b>Your installation is almost over. As a security measure, either delete the INSTALL folder or remove read permissions from it so that no-one else can access that folder.</b>
00068
00069
00070 For Pragyan CMS to work, .htaccess needs to be supported your webserver. <br />
00071 For this, the <b>AllowOverride</b> setting in the httpd.conf has to be made <i>Options FileInfo Limit</i> under the relevant <mono><Directory></mono> section.<br />
00072 The default location of httpd.conf is <mono>/etc/httpd/conf/httpd.conf</mono>, but may be different for you according to your installation.
00073 <br /><br />
00074 Add the following lines in the httpd.conf of your webserver :
00075 <pre><xmp>
00076 <Directory "$scriptPath">
00077 AllowOverride All
00078 </Directory>
00079 </xmp></pre>
00080 <p>If you have done this, <a href="../">click here</a> to go to the CMS.</p>
00081 HTTPDCONF;
00082 }
00083 }
00084
00085 include_once('template.php');
00086
00087
00092 function installCMS() {
00093 global $URL_REWRITE;
00094 $installationSteps =
00095 array(
00096
00097 array('saveConfigurationSettings', 'Saving Configuration Settings', false),
00098 array('checkDatabaseAccess', 'Checking Database Access', false),
00099 array('checkOpenidCurl','Checking if cURL needed and installed',false),
00100 array('importDatabase', 'Importing Database', false),
00101 array('saveHtaccess', 'Checking .htaccess Settings (for Pretty URLs)', false)
00102
00103 );
00104
00105
00106
00107
00108
00109
00110 for ($i = 0; $i < count($installationSteps); ++$i) {
00111 $installationProcedure = $installationSteps[$i][0];
00112
00113 $stepResult = $installationProcedure();
00114
00116 if($i==1 && OPENID_ENABLED!='true')
00117 unset($installationSteps[2]);
00118
00120 $installationSteps=array_values($installationSteps);
00121
00123 if($i==1 && $URL_REWRITE=='false')
00124 unset($installationSteps[count($installationSteps)-1]);
00125
00127 $installationSteps=array_values($installationSteps);
00128
00129 if ($stepResult != '') {
00130 $installationSteps[$i][] = $stepResult;
00131 return $installationSteps;
00132 }
00133 $installationSteps[$i][2] = true;
00134 }
00135 return $installationSteps;
00136 }
00141 function checkOpenidCurl(){
00142 if(OPENID_ENABLED=='true')
00143 {
00144
00145 if (iscurlinstalled())
00146 return '';
00147 else
00148 {
00149 global $curl_message;
00150 return "<p><b>Error:</b>$curl_message</p>";
00151 }
00152 }
00153 else
00154 {
00155 return '';
00156 }
00157 }
00158
00159
00164 function saveConfigurationSettings() {
00165 global $URL_REWRITE;
00166 $configurationMap = array(
00167 'txtMySQLServerHost' => 'MYSQL_SERVER',
00168 'txtMySQLUsername' => 'MYSQL_USERNAME',
00169 'txtMySQLPassword' => 'MYSQL_PASSWORD',
00170 'txtMySQLDatabase' => 'MYSQL_DATABASE',
00171 'txtMySQLTablePrefix' => 'MYSQL_DATABASE_PREFIX',
00172 'txtAdminUsername' => 'ADMIN_USERNAME',
00173 'txtAdminEmail' => 'ADMIN_EMAIL',
00174 'txtAdminFullname' => 'ADMIN_FULLNAME',
00175 'txtAdminPassword' => 'ADMIN_PASSWORD',
00176 'optSendVerification' => 'SEND_MAIL_ON_REGISTRATION',
00177 'optDefaultUserActive' => 'DEFAULT_USER_ACTIVATE',
00178 'txtCMSMailId' => 'CMS_EMAIL',
00179 'txtCMSTitle' => 'CMS_TITLE',
00180 'selTemplate' => 'CMS_TEMPLATE',
00181 'txtUploadLimit' => 'UPLOAD_LIMIT',
00182 'optEnableOpenID' => 'OPENID_ENABLED',
00183 'txtCookieTimeout' => 'cookie_timeout',
00184 'selErrorReporting' => 'error_level',
00185 'optEnableIMAP' => 'AUTH_IMAP_STATUS',
00186 'txtIMAPServerAddress' => 'AUTH_IMAP_SERVER',
00187 'txtIMAPPort' => 'AUTH_IMAP_PORT',
00188 'txtIMAPUserDomain' => 'AUTH_IMAP_DOMAIN',
00189 'optEnableLDAP' => 'AUTH_LDAP_STATUS',
00190 'txtLDAPServerAddress' => 'AUTH_LDAP_SERVER',
00191 'txtLDAPSearchGroup' => 'AUTH_LDAP_SEARCHGROUP',
00192 'txtLDAPUserDomain' => 'AUTH_LDAP_DOMAIN',
00193 'optEnableADS' => 'AUTH_ADS_STATUS',
00194 'txtADSServerAddress' => 'AUTH_ADS_SERVER',
00195 'txtADSNetworkName' => 'AUTH_ADS_NETWORK',
00196 'txtADSUserDomain' => 'AUTH_ADS_DOMAIN',
00197 'txtMySQLServerPort' => 'MYSQL_PORT',
00198 'optURLRewrite' => 'URL_REWRITE'
00199
00200 );
00201
00202 foreach ($configurationMap as $postVariableName => $configVariableName) {
00203 if (substr($postVariableName, 0, 3) == "opt") {
00204 ${$configVariableName} = (isset($_POST[$postVariableName]) && $_POST[$postVariableName] == "Yes") ? 'true' : 'false';
00205 }
00206 else {
00207 ${$configVariableName} = isset($_POST[$postVariableName]) ? $_POST[$postVariableName] : '';
00208 }
00209
00210 }
00211 if($MYSQL_PORT!="") $MYSQL_SERVER.=":$MYSQL_PORT";
00212
00213 global $cmsFolder;
00214
00215 $configFileText = '';
00216 require_once('config.inc-dist.php');
00217 $writeHandle = @fopen("$cmsFolder/config.inc.php", 'w');
00218 if (!$writeHandle)
00219 return 'Could not write to config.inc.php. Please make sure that the file is writable.';
00220
00221 fwrite($writeHandle, $configFileText);
00222 fclose($writeHandle);
00223
00224 $writeHandle = @fopen("$cmsFolder/modules/search/settings/database.php",'w');
00225 if(!$writeHandle)
00226 return "Could not write to $cmsFolder/modules/search/settings/database.php. Please make sure that the file is writable.";
00227
00228 fwrite($writeHandle, $searchConfigFileText);
00229 fclose($writeHandle);
00230
00231 $c = 0;
00232 foreach ($configurationMap as $postVariableName => $configVariableName) {
00233
00234 define ($configVariableName, ${$configVariableName});
00235 if (++$c == 16)
00236 break;
00237 }
00238
00239 return '';
00240 }
00241
00242 function checkDatabaseAccess() {
00243 $dbaccessInfo = '';
00244 $dbaccessErrorTip = <<<WHATEVER
00245 <p>To create a database and a user with all priviliges to that database, run the following queries after replacing <b>pragyandatabase</b>, <b>localhost</b>, <b>pragyanuser</b> and <b>pragyanpassword</b> as required. </p>
00246 <pre>CREATE DATABASE `pragyandatabase`;
00247 CREATE USER 'pragyanuser'@'localhost' IDENTIFIED BY 'pragyanpassword';
00248 GRANT ALL PRIVILEGES ON `pragyandatabase` . * TO 'pragyanuser'@'localhost';</pre>
00249 <p>After you run these queries successfully in your MySQL client, please run this install script again.</p>
00250 WHATEVER;
00251
00252 $dbhost=MYSQL_SERVER;
00253 $dbname=MYSQL_DATABASE;
00254
00255 $dbuser=MYSQL_USERNAME;
00256 $dbpasswd=MYSQL_PASSWORD;
00257 $dblink=mysql_connect($dbhost,$dbuser,$dbpasswd);
00258 if($dblink==false)
00259 {
00260 $dbaccessInfo.="<p><b>Error:</b> Pragyan CMS could not connect to database on '$dbhost' using username '$dbuser'. Please check the username/password you provided.</p>$dbpasswd";
00261 return $dbaccessInfo . $dbaccessErrorTip;
00262 }
00263 $db=mysql_select_db($dbname);
00264 if($db==false)
00265 {
00266 $dbaccessInfo.="<p><b>Error:</b> Pragyan CMS could not select the database '$dbname'.<br/> Please make sure the database exists and the user $dbuser has permissions over it.</p>";
00267 return $dbaccessInfo . $dbaccessErrorTip;
00268 }
00269 $res=mysql_query("CREATE TABLE IF NOT EXISTS `testtable948823` ( `testuserid` int(10) )");
00270 if($res==false)
00271 {
00272 $dbaccessInfo.="<p><b>Error:</b> The User '$dbuser' does not have permissions to CREATE tables in '$dbname'.</p>";
00273 return $dbaccessInfo . $dbaccessErrorTip;
00274 }
00275 $res=mysql_query("INSERT INTO `testtable948823` VALUES (123)");
00276 if($res==false)
00277 {
00278 $dbaccessInfo.="<p><b>Error:</b> The User '$dbuser' does not have permissions to INSERT values in tables of database '$dbname'.</p>";
00279 return $dbaccessInfo . $dbaccessErrorTip;
00280 }
00281 $res=mysql_query("UPDATE `testtable948823` SET testuserid=0");
00282 if($res==false)
00283 {
00284 $dbaccessInfo.="<p><b>Error:</b> The User '$dbuser' does not have permissions to UPDATE values in tables of database '$dbname'.</p>";
00285 return $dbaccessInfo . $dbaccessErrorTip;
00286 }
00287 $res=mysql_query("SELECT * FROM `testtable948823`");
00288 if($res==false)
00289 {
00290 $dbaccessInfo.="<p><b>Error:</b> The User '$dbuser' does not have permissions to SELECT values in tables of database '$dbname'.</p>";
00291 return $dbaccessInfo . $dbaccessErrorTip;
00292 }
00293 $res=mysql_query("DROP TABLE `testtable948823`");
00294 if($res==false)
00295 {
00296 $dbaccessInfo.="<p><b>Error:</b> The User '$dbuser' does not have permissions to DROP tables of database '$dbname'.</p>";
00297 return $dbaccessInfo . $dbaccessErrorTip;
00298 }
00299 return '';
00300 }
00301
00302 function importDatabase() {
00303 global $installFolder, $URL_REWRITE;
00304
00305 mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD);
00306 mysql_select_db(MYSQL_DATABASE);
00307
00308 $handle = @fopen($installFolder."/pragyan_structure.sql", "r");
00309 $query = '';
00310 if ($handle) {
00311 while (!feof($handle)) {
00312 $buffer = fgets($handle, 4096);
00313 if (strpos($buffer,"--") !== 0)
00314 $query.=$buffer;
00315 }
00316 fclose($handle);
00317 }
00318 $query = str_replace("pragyanV3_",MYSQL_DATABASE_PREFIX,$query);
00319 $singlequeries = explode(";\n",$query);
00320 foreach ($singlequeries as $singlequery) {
00321 if (trim($singlequery)!="") {
00322 $result1 = mysql_query($singlequery);
00323 if (!$result1) {
00324 $output = "<h3>Error:</h3><pre>".$singlequery."</pre>\n<br/>Unable to create structure. ".mysql_error();
00325 return $output;
00326 }
00327 }
00328 }
00329 $error = include 'searchStructure.php';
00330 if($error != '')
00331 return $error;
00332 $handle = @fopen($installFolder."/pragyan_inserts.sql", "r");
00333 if ($handle) {
00334 while (!feof($handle)) {
00335 $buffer = fgets($handle, 4096);
00336 if (strpos($buffer,"--")!==0)
00337 $query.=$buffer;
00338 }
00339 fclose($handle);
00340 }
00341 $query = str_replace("pragyanV3_",MYSQL_DATABASE_PREFIX,$query);
00342 $singlequeries = explode(";\n",$query);
00343 foreach ($singlequeries as $singlequery) {
00344 if (trim($singlequery)!="") {
00345 $result1 = mysql_query($singlequery);
00346 if (!$result1) {
00347 $output = "<h3>Error:</h3><pre>".$singlequery."</pre>\n<br/>Unable to import the rows. " . mysql_error();
00348 return $output;
00349 }
00350 }
00351 }
00352 $DEFAULT_USER_ACTIVATE=(DEFAULT_USER_ACTIVATE=="true"?1:0);
00353 $SEND_MAIL_ON_REGISTRATION=(SEND_MAIL_ON_REGISTRATION=="true"?1:0);
00354
00355 setGlobalSettingByAttribute("cms_title",CMS_TITLE);
00356 setGlobalSettingByAttribute("cms_email",CMS_EMAIL);
00357 setGlobalSettingByAttribute("default_template",CMS_TEMPLATE);
00358 setGlobalSettingByAttribute("url_rewrite",$URL_REWRITE);
00359 setGlobalSettingByAttribute("default_user_activate",$DEFAULT_USER_ACTIVATE);
00360 setGlobalSettingByAttribute("default_mail_verify",$SEND_MAIL_ON_REGISTRATION);
00361 setGlobalSettingByAttribute("upload_limit",UPLOAD_LIMIT);
00362 setGlobalSettingByAttribute("cms_desc",CMS_TITLE);
00363 setGlobalSettingByAttribute("cms_keywords",CMS_TITLE);
00364 setGlobalSettingByAttribute("reindex_frequency","2");
00365 setGlobalSettingByAttribute("allow_login","1");
00366 setGlobalSettingByAttribute("cms_footer","© 2010 - powered by <a href=\"http://sourceforge.net/projects/pragyan\" title=\"Praygan CMS\">Pragyan CMS v3.0</a>");
00367 setGlobalSettingByAttribute("openid_enabled",OPENID_ENABLED);
00368
00369
00370 $query="INSERT IGNORE INTO `".MYSQL_DATABASE_PREFIX."users` (`user_id`,`user_name`,`user_email`,`user_fullname`,`user_password`,`user_regdate`,`user_lastlogin`,`user_activated`,`user_loginmethod`) VALUES (
00371 1,'".ADMIN_USERNAME."','".ADMIN_EMAIL."','".ADMIN_FULLNAME."','".md5(ADMIN_PASSWORD)."',NOW(),'',1,'db')";
00372 mysql_query($query);
00373 global $cmsFolder;
00374 $templates=scandir($cmsFolder.'/templates');
00375
00376 foreach($templates as $tdir)
00377 {
00378
00379 if(is_dir($cmsFolder.'/templates/'.$tdir) && $tdir[0]!='.' && $tdir!="common")
00380 {
00381
00382 $query="INSERT IGNORE INTO `".MYSQL_DATABASE_PREFIX."templates` (`template_name`) VALUES ('$tdir')";
00383 mysql_query($query);
00384 }
00385 }
00386
00387 return '';
00388 }
00389
00408 function checkLocationAccess($path)
00409 {
00410 $code=0;
00411 if(is_file($path))
00412 {
00413 $code+=1;
00414 $code+=is_writable($path)?4:8;
00415 }
00416 else if(is_dir($path))
00417 {
00418 $code+=2;
00419 $testFolder=@fopen($path."/testfolder",'w');
00420 $code+=($testFolder)?4:8;
00421 if($testFolder)
00422 {
00423 fclose($testFolder);
00424 unlink($path."/testfolder");
00425 }
00426 }
00427 else $code+=16;
00428 return $code;
00429 }
00430
00435 function CheckPrerequisites() {
00436 global $sourceFolder;
00437 global $scriptPath;
00438
00439 $cmsfolder = "$scriptPath/$sourceFolder";
00440
00441 $checklist=array(
00442 "$scriptPath/.htaccess" => "file",
00443 "$cmsfolder" => "folder",
00444 "$cmsfolder/uploads" => "folder",
00445 "$cmsfolder/templates" => "folder",
00446 "$cmsfolder/widgets" => "folder",
00447 "$cmsfolder/languages" => "folder",
00448 "$cmsfolder/modules" => "folder",
00449 "$cmsfolder/config.inc.php" => "file",
00450 "$cmsfolder/modules/search/settings/database.php" => "file",
00451 );
00452
00453 $checklist2=array();
00454
00455 foreach($checklist as $path=>$type)
00456 {
00457 switch(checkLocationAccess($path))
00458 {
00459 case 9 : $prereq.="<li><p>Pragyan CMS doesn't have write permissions over the file <b>$path</b>.</p></li>"; break;
00460 case 10 : $prereq.="<li><p>Pragyan CMS doesn't have write permissions over the folder <b>$path</b>.</p></li>"; break;
00461 case 16 :
00462
00463 $trycreate=($type=="file")?(touch($path)&&chmod($path,0755)):mkdir($path);
00464 if($trycreate)
00465 $checklist2[$path]=$type;
00466 else
00467 $prereq.="<li><p>The following $type is missing : <b>$path</b> and Pragyan CMS was not able to create it. Please create the $type manually and make sure Pragyan CMS has write permissions over it.</p></li>";
00468 break;
00469 }
00470 }
00471 foreach($checklist2 as $path=>$type)
00472 {
00473 switch(checkLocationAccess($path))
00474 {
00475 case 9 : $prereq.="<li><p>Pragyan CMS doesn't have write permissions over the file <b>$path</b>.</p></li>"; break;
00476 case 10 : $prereq.="<li><p>Pragyan CMS doesn't have write permissions over the folder <b>$path</b>.</p></li>"; break;
00477 case 16 :
00478
00479 $trycreate=($type=="file")?touch($path):mkdir($path);
00480 if($trycreate)
00481 $checklist2[$path]=$type;
00482 else
00483 $prereq.="<li><p>The following $type is missing : <b>$path</b> and Pragyan CMS was not able to create it. Please create the $type manually and make sure Pragyan CMS has write permissions over it.</p></li>";
00484 break;
00485 }
00486 }
00487
00488
00489 if ($prereq != '') {
00490 $prereq = "<p>The following prerequisite(s) need to be resolved before Pragyan CMS can continue installation.</p>\n<ul>\n$prereq\n</ul>";
00491 $prereq .= '<p>Please make the necessary changes, and <a href="javascript: location.reload(true)">click here</a> to refresh this page.</p>';
00492 $prereq .= <<<MSG
00493 <hr/>
00494 <p>Pragyan CMS should have full write-access to its root installation directory. In your case it is : <b>$scriptPath</b>.<br/>
00495 Please make sure the installation directory is writable by your webserver user. On a <i>linux</i> server, run the following commands as root:
00496 <pre>chown -R <httpd-process-user> $scriptPath</pre>
00497 where <httpd-process-user> is the default user for your webserver process. In most cases, it is either 'www-data' or 'apache'.
00498 <br/><br/>OR if you don't know your webserver process user and you're also not so concerned about security, execute the following command as root :
00499 <br /><pre>chmod -R 777 $scriptPath</pre></p>
00500 MSG;
00501 }
00502
00503 clearstatcache();
00504 return $prereq;
00505 }
00506
00510 function saveHtaccess() {
00511
00512 $urlRequestRootWithFolder = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
00513 $urlRequestRoot = substr($urlRequestRootWithFolder , 0, strrpos($urlRequestRootWithFolder , '/'));
00514 $urlRequestRoot = ($urlRequestRoot==""?"/":$urlRequestRoot);
00515 $scriptPathWithFolder = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/'));
00516 $scriptPath = substr($scriptPathWithFolder , 0, strrpos($scriptPathWithFolder , '/'));
00517
00518 $errorMessage = <<<HTACCESSERROR
00519 <p>Could not save .htaccess file.</p>
00520 <p>Please ensure that the .htaccess file in the Pragyan CMS installation folder exists and is writable.</p>
00521 <p>You can change permissions back to the way it was, after the installation completes.</p>
00522 <p><a href="javascript: location.reload(true)">Click here</a> to refresh this page.</p>
00523 HTACCESSERROR;
00524
00525 $htaccessdist = "../htaccess-dist";
00526 $htaccessFile = "../.htaccess";
00527
00528 copy($htaccessdist,$htaccessFile);
00529
00530 $htaccessHandle = fopen($htaccessFile, 'r');
00531 if (!$htaccessHandle)
00532 return $errorMessage;
00533
00534 $lines = file('../.htaccess');
00535 $htaccessHandle = fopen($htaccessFile, 'w');
00536
00537 if (!$lines || !$htaccessHandle) {
00538 return $errorMessage;
00539 }
00540
00541 for ($i = 0; $i < count($lines); ++$i) {
00542 if (trim($lines[$i]) == '#BASEDIRECTORY') {
00543 fwrite($htaccessHandle, "#BASEDIRECTORY\n");
00544 fwrite($htaccessHandle, "RewriteBase $urlRequestRoot\n");
00545 fwrite($htaccessHandle, "RewriteCond %{REQUEST_URI} ^$urlRequestRoot/\$\n");
00546 $i += 2;
00547 }
00548 if (trim($lines[$i]) == '#REWRITEENGINE') {
00549 fwrite($htaccessHandle, "#REWRITEENGINE\n");
00550 fwrite($htaccessHandle, "RewriteEngine On\n");
00551 $i += 1;
00552 }
00553 else
00554 fwrite($htaccessHandle, $lines[$i]);
00555 }
00556 fclose($htaccessHandle);
00557
00558 return '';
00559 }