PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/installer/install.php

https://github.com/fayazv/Taarifa_Web
PHP | 823 lines | 602 code | 105 blank | 116 comment | 68 complexity | 1dbe8129d9cf58f5189bd599158746ba MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * This class acts like a controller.
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @module Admin Dashboard Controller
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General
  14. * Public License (LGPL)
  15. */
  16. require_once('form.php');
  17. require_once('modulecheck.php');
  18. class Install
  19. {
  20. private $database_file;
  21. private $install_directory;
  22. public function __construct()
  23. {
  24. global $form;
  25. $this->install_directory = dirname(dirname(__FILE__));
  26. $this->_index();
  27. }
  28. public function _index()
  29. {
  30. session_start();
  31. }
  32. /**
  33. * Validates the form fields and does the necessary processing.
  34. */
  35. public function _install_db_info( $username, $password, $host, $select_db_type,
  36. $db_name, $table_prefix, $base_path )
  37. {
  38. global $form;
  39. //check for empty fields
  40. if(!$username || strlen($username = trim($username)) == 0 ){
  41. $form->set_error("username", "Please make sure to " .
  42. "enter the <strong>username</strong> of the database server.");
  43. }
  44. if( !$host || strlen($host = trim($host)) == 0 ){
  45. $form->set_error("host","Please enter the <strong>host</strong> of the
  46. database server." );
  47. }
  48. if( !$db_name || strlen($db_name = trim($db_name)) == 0 ){
  49. $form->set_error("db_name","Please enter the <strong>name</strong> of your database.");
  50. }
  51. // load database.template.php and work from it.
  52. if(!file_exists('../application/config/database.template.php')){
  53. $form->set_error("load_db_tpl","<strong>Oops!</strong> I need the file called " .
  54. "<code>database.template.php</code> to work
  55. from. Please make sure this file is in the <code>application/config/</code> folder.");
  56. }
  57. // load .htaccess file and work with it.
  58. if(!file_exists('../.htaccess')){
  59. $form->set_error("load_htaccess_file","<strong>Oops!</strong> I need a file called " .
  60. "<code>.htaccess</code> to work
  61. with. Please make sure this file is in the root directory of your Ushahidi files.");
  62. }
  63. if( !is_writable('../.htaccess')) {
  64. $form->set_error('htaccess_perm',
  65. "<strong>Oops!</strong> Ushahidi is unable to write to the <code>.htaccess</code> file. " .
  66. "Please change the permissions of that file to allow write access (777). " .
  67. "<p>Here are instructions for changing file permissions:</p>" .
  68. "<ul>" .
  69. " <li><a href=\"http://www.washington.edu/computing/unix/permissions.html\">Unix/Linux</a></li>" .
  70. " <li><a href=\"http://support.microsoft.com/kb/308419\">Windows</a></li>" .
  71. "</ul>");
  72. }
  73. if( !is_writable('../application/config')) {
  74. $form->set_error('permission',
  75. "<strong>Oops!</strong> Ushahidi is trying to create and/or edit a file called \"" .
  76. "database.php\" and is unable to do so at the moment. This is probably due to the fact " .
  77. "that your permissions aren't set up properly for the <code>config</code> folder. " .
  78. "Please change the permissions of that folder to allow write access (777). " .
  79. "<p>Here are instructions for changing file permissions:</p>" .
  80. "<ul>" .
  81. " <li><a href=\"http://www.washington.edu/computing/unix/permissions.html\">Unix/Linux</a></li>" .
  82. " <li><a href=\"http://support.microsoft.com/kb/308419\">Windows</a></li>" .
  83. "</ul>");
  84. }
  85. if( !is_writable('../application/config/config.php')) {
  86. $form->set_error('config_perm',
  87. "<strong>Oops!</strong> Ushahidi is trying to edit a file called \"" .
  88. "config.php\" and is unable to do so at the moment. This is probably due to the fact " .
  89. "that your permissions aren't set up properly for the <code>config.php</code> file. " .
  90. "Please change the permissions of that folder to allow write access (777). " .
  91. "<p>Here are instructions for changing file permissions:</p>" .
  92. "<ul>" .
  93. " <li><a href=\"http://www.washington.edu/computing/unix/permissions.html\">Unix/Linux</a></li>" .
  94. " <li><a href=\"http://support.microsoft.com/kb/308419\">Windows</a></li>" .
  95. "</ul>"
  96. /* CB: Commenting this out... I think it's better if we just have them change the permissions of the specific
  97. files and folders rather than all the files
  98. "Alternatively, you could make the webserver own all the ushahidi files. On unix usually, you" .
  99. "issue this command <code>chown -R www-data:ww-data</code>");
  100. */
  101. );
  102. }
  103. if(!$this->_make_connection($username, $password, $host)){
  104. $form->set_error("connection","<strong>Oops!</strong>, We couldn't make a connection to
  105. the database server with the credentials given. Please make sure they are correct.");
  106. }
  107. /**
  108. * error exists, have user correct them.
  109. */
  110. if( $form->num_errors > 0 ) {
  111. return 1;
  112. } else {
  113. $this->_add_config_details($base_path);
  114. $this->_add_htaccess_entry($base_path);
  115. $this->_add_db_details( $username, $password, $host, $select_db_type,
  116. $db_name, $table_prefix );
  117. $this->_import_sql($username, $password, $host, $db_name, $table_prefix);
  118. $this->_chmod_folders();
  119. $sitename = $this->_get_url();
  120. $url = $this->_get_url();
  121. $configure_stats = $this->_configure_stats($sitename, $url, $host, $username, $password, $db_name, $table_prefix);
  122. return 0;
  123. }
  124. }
  125. /**
  126. * Validates general settings fields and then add details to
  127. * the settings table.
  128. */
  129. public function _general_settings($site_name, $site_tagline, $default_lang, $site_email, $table_prefix,$clean_url)
  130. {
  131. global $form;
  132. //check for empty fields
  133. if(!$site_name || strlen($site_name = trim($site_name)) == 0 ){
  134. $form->set_error("site_name", "Please make sure to " .
  135. "enter a <strong>site name</strong>.");
  136. } else {
  137. $site_name = stripslashes($site_name);
  138. }
  139. if(!$site_tagline || strlen($site_tagline = trim($site_tagline)) == 0 ){
  140. $form->set_error("site_tagline", "Please make sure to " .
  141. "enter a <strong>site tagline</strong>.");
  142. } else {
  143. $site_tagline = stripslashes($site_tagline);
  144. }
  145. /* Email error checking */
  146. if(!$site_email || strlen($site_email = trim($site_email)) == 0){
  147. $form->set_error("site_email", "Please enter a <strong>site email address</strong>.");
  148. } else{
  149. /* Check if valid email address */
  150. $regex = "/^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  151. ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  152. ."\.([a-z]{2,}){1}$/i";
  153. if(!preg_match($regex,$site_email)){
  154. $form->set_error("site_email", "Please enter a valid email address. ex: johndoe@email.com.");
  155. }
  156. $site_email = stripslashes($site_email);
  157. }
  158. /**
  159. * error exists, have user correct them.
  160. */
  161. if( $form->num_errors > 0 ) {
  162. return 1;
  163. } else {
  164. $this->_add_general_settings($site_name, $site_tagline, $default_lang, $site_email, $table_prefix,$clean_url);
  165. return 0;
  166. }
  167. }
  168. public function _map_info($map_provider, $map_api_key, $table_prefix)
  169. {
  170. global $form;
  171. //check for API key only if map provider is google
  172. if($map_provider != '4' && (!$map_api_key || strlen($map_api_key = trim($map_api_key)) == 0)){
  173. $form->set_error("map_provider_api_key", "Please make sure to " .
  174. "enter an<strong> api key</strong> for your map provider.");
  175. } else {
  176. $map_api_key = stripslashes($map_api_key);
  177. }
  178. /**
  179. * error exists, have user correct them.
  180. */
  181. if( $form->num_errors > 0 ) {
  182. return 1;
  183. } else {
  184. $this->_add_map_info($map_provider, $map_api_key, $table_prefix );
  185. return 0;
  186. }
  187. }
  188. public function _mail_server($alert_email, $mail_username,$mail_password,
  189. $mail_port,$mail_host,$mail_type,$mail_ssl,$table_prefix){
  190. global $form;
  191. //check for empty fields
  192. if(!$alert_email || strlen($alert_email = trim($alert_email)) == 0 ){
  193. $form->set_error("site_alert_email", "Please make sure to " .
  194. "enter a <strong>site alert email address</strong>.");
  195. }
  196. if( !$mail_username || strlen($mail_username = trim($mail_username)) == 0 ){
  197. $form->set_error("mail_server_username","Please enter the <strong>user name</strong> of your mail server." );
  198. }
  199. if( !$mail_password || strlen($mail_password = trim($mail_password)) == 0 ){
  200. $form->set_error("mail_server_pwd","Please enter the <strong>password</strong> for your email account.");
  201. }
  202. if(!$mail_port|| strlen($mail_port = trim($mail_port)) == 0 ){
  203. $form->set_error("mail_server_port", "Please make sure to " .
  204. "enter the <strong>port</strong> for your mail server.");
  205. }
  206. if(!$mail_host|| strlen($mail_host = trim($mail_host)) == 0 ){
  207. $form->set_error("mail_server_host", "Please make sure to " .
  208. "enter the <strong>host</strong> of the mail server.");
  209. }
  210. /**
  211. * error exists, have user correct them.
  212. */
  213. if( $form->num_errors > 0 ) {
  214. return 1;
  215. } else {
  216. $this->_add_mail_server_info( $alert_email, $mail_username,$mail_password,
  217. $mail_port,$mail_host,$mail_type,$mail_ssl,$table_prefix );
  218. return 0;
  219. }
  220. }
  221. /**
  222. * gets the URL
  223. */
  224. private function _get_url()
  225. {
  226. global $_SERVER;
  227. if ($_SERVER["SERVER_PORT"] != "80") {
  228. $url = $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  229. } else {
  230. $url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  231. }
  232. return 'http://'.substr($url,0,stripos($url,'/installer/'));
  233. }
  234. /**
  235. * adds the database details to the config/database.php file.
  236. */
  237. private function _add_db_details( $username, $password, $host,
  238. $select_db_type, $db_name, $table_prefix )
  239. {
  240. // echo "$username, $password, $host,
  241. // $select_db_type, $db_name, $table_prefix";
  242. $database_file = @file('../application/config/database.template.php');
  243. $handle = @fopen('../application/config/database.php', 'w');
  244. foreach( $database_file as $line_number => $line )
  245. {
  246. switch( trim(substr( $line,0,14 )) ) {
  247. case "'type' =":
  248. fwrite($handle, str_replace("'mysql'","'".
  249. $select_db_type."'",$line ));
  250. break;
  251. case "'user' =":
  252. fwrite($handle, str_replace("'username'","'".
  253. $username."'",$line ));
  254. break;
  255. case "'pass' =":
  256. fwrite($handle, str_replace("'password'","'".
  257. $password."'",$line));
  258. break;
  259. case "'host' =":
  260. fwrite($handle, str_replace("'localhost'","'".
  261. $host."'",$line));
  262. break;
  263. case "'database' =":
  264. fwrite($handle, str_replace("'db'","'".
  265. $db_name."'",$line));
  266. break;
  267. case "'table_prefix":
  268. fwrite($handle, str_replace("''","'".
  269. ($table_prefix ? $table_prefix."_'" : "'"),$line));
  270. break;
  271. default:
  272. fwrite($handle, $line);
  273. }
  274. }
  275. fclose($handle);
  276. //for security reasons change permission on the file to 666
  277. chmod('../application/config/database.php',0666);
  278. }
  279. /**
  280. * adds the site_name to the application/config/config.php file
  281. */
  282. private function _add_config_details( $base_path )
  283. {
  284. $config_file = @file('../application/config/config.template.php');
  285. $handle = @fopen('../application/config/config.php', 'w');
  286. foreach( $config_file as $line_number => $line )
  287. {
  288. if( !empty( $base_path ) )
  289. {
  290. switch( trim(substr( $line,0,23 )) ) {
  291. case "\$config['site_domain']":
  292. fwrite($handle, str_replace("/","/".
  293. $base_path."/",$line ));
  294. break;
  295. default:
  296. fwrite($handle, $line);
  297. }
  298. }else {
  299. fwrite($handle, $line);
  300. }
  301. }
  302. }
  303. /**
  304. * Removes index.php from index page variable in application/config.config.php file
  305. */
  306. private function _remove_index_page($yes_or_no) {
  307. $config_file = @file('../application/config/config.php');
  308. $handle = @fopen('../application/config/config.php', 'w');
  309. if(is_array($config_file) ) {
  310. foreach( $config_file as $line_number => $line )
  311. {
  312. if( $yes_or_no == 1 ) {
  313. if( strpos(" ".$line,"\$config['index_page'] = 'index.php';") != 0 ) {
  314. fwrite($handle, str_replace("index.php","",$line ));
  315. } else {
  316. fwrite($handle, $line);
  317. }
  318. } else {
  319. if( strpos(" ".$line,"\$config['index_page'] = '';") != 0 ) {
  320. fwrite($handle, str_replace("''","'index.php'",$line ));
  321. } else {
  322. fwrite($handle, $line);
  323. }
  324. }
  325. }
  326. }
  327. }
  328. /**
  329. * Adds the right RewriteBase entry to the .htaccess file.
  330. *
  331. * @param base_path - the base path.
  332. */
  333. private function _add_htaccess_entry($base_path) {
  334. $htaccess_file = @file('../.htaccess');
  335. $handle = @fopen('../.htaccess','w');
  336. if( is_array( $htaccess_file ) ) {
  337. foreach($htaccess_file as $line_number => $line ) {
  338. if( !empty($base_path) && $base_path != "/" ) {
  339. if( strpos(" ".$line,"RewriteBase /") != 0 ) {
  340. fwrite($handle, str_replace("/","/".$base_path,$line));
  341. } else {
  342. fwrite($handle,$line);
  343. }
  344. } else {
  345. fwrite($handle,$line);
  346. }
  347. }
  348. }
  349. }
  350. /**
  351. * Imports sql file to the database.
  352. */
  353. private function _import_sql($username, $password, $host, $db_name, $table_prefix = NULL)
  354. {
  355. $connection = @mysql_connect("$host", "$username", "$password");
  356. $db_schema = @file_get_contents('../sql/ushahidi.sql');
  357. // If a table prefix is specified, add it to sql
  358. if ($table_prefix) {
  359. $find = array(
  360. 'CREATE TABLE IF NOT EXISTS `',
  361. 'INSERT INTO `',
  362. 'ALTER TABLE `',
  363. 'UPDATE `',
  364. 'DELETE FROM `'
  365. );
  366. $replace = array(
  367. 'CREATE TABLE IF NOT EXISTS `'.$table_prefix.'_',
  368. 'INSERT INTO `'.$table_prefix.'_',
  369. 'ALTER TABLE `'.$table_prefix.'_',
  370. 'UPDATE `'.$table_prefix.'_',
  371. 'DELETE FROM `'.$table_prefix.'_'
  372. );
  373. $db_schema = str_replace($find, $replace, $db_schema);
  374. }
  375. // Use todays date as the date for the first incident in the system
  376. $db_schema = str_replace('2010-01-01 12:00:00',
  377. date("Y-m-d H:i:s",time()), $db_schema);
  378. $result = @mysql_query('CREATE DATABASE '.$db_name);
  379. // select newly created db
  380. @mysql_select_db($db_name,$connection);
  381. /**
  382. * split by ; to get the sql statement for creating individual
  383. * tables.
  384. */
  385. $tables = explode(';',$db_schema);
  386. foreach($tables as $query) {
  387. $result = @mysql_query($query,$connection);
  388. }
  389. @mysql_close( $connection );
  390. }
  391. /**
  392. * Adds general settings detail to the db.
  393. * @param site_name - site name.
  394. * @param site_tagline - site name.
  395. * @param defaul_lang - default language.
  396. * @param site_email - site email.
  397. */
  398. private function _add_general_settings($site_name, $site_tagline, $default_lang, $site_email, $table_prefix = NULL,$clean_url) {
  399. $table_prefix = ($table_prefix) ? $table_prefix.'_' : "";
  400. $connection = @mysql_connect($_SESSION['host'],$_SESSION['username'], $_SESSION['password']);
  401. @mysql_select_db($_SESSION['db_name'],$connection);
  402. @mysql_query('UPDATE `'.$table_prefix.'settings` SET `site_name` = \''.mysql_escape_string($site_name).
  403. '\', site_tagline = \''.mysql_escape_string($site_tagline).'\', site_language= \''.mysql_escape_string($default_lang).'\' , site_email= \''.mysql_escape_string($site_email).'\' ');
  404. @mysql_close($connection);
  405. //enable / disable clean url
  406. $this->_remove_index_page($clean_url);
  407. }
  408. /**
  409. * Adds google map api key to the settings table.
  410. * @param map_provider - map provider.
  411. * @param map_api_key - map api key
  412. */
  413. private function _add_map_info($map_provider, $map_api_key, $table_prefix = NULL ){
  414. $table_prefix = ($table_prefix) ? $table_prefix.'_' : "";
  415. //TODO modularize the db connection part.
  416. $connection = @mysql_connect($_SESSION['host'],$_SESSION['username'], $_SESSION['password']);
  417. @mysql_select_db($_SESSION['db_name'],$connection);
  418. @mysql_query('UPDATE `'.$table_prefix.'settings` SET `default_map` = \''.mysql_escape_string($map_provider).
  419. '\', api_google = \''.mysql_escape_string($map_api_key).'\' ');
  420. @mysql_close($connection);
  421. }
  422. /**
  423. * Adds mail server details to the settings table.
  424. *
  425. */
  426. private function _add_mail_server_info( $alert_email, $mail_username,$mail_password,
  427. $mail_port,$mail_host,$mail_type,$mail_ssl, $table_prefix = NULL ) {
  428. $table_prefix = ($table_prefix) ? $table_prefix.'_' : "";
  429. $connection = @mysql_connect($_SESSION['host'],$_SESSION['username'], $_SESSION['password']);
  430. @mysql_select_db($_SESSION['db_name'],$connection);
  431. @mysql_query('UPDATE `'.$table_prefix.'settings` SET `alerts_email` = \''.mysql_escape_string($alert_email).
  432. '\', `email_username` = \''.mysql_escape_string($mail_username).'\' , `email_password` = \''.mysql_escape_string($mail_password).'\'' .
  433. ', `email_port` = \''.mysql_escape_string($mail_port).'\' , `email_host` = \''.mysql_escape_string($mail_host).'\' ' .
  434. ', `email_servertype` = \''.mysql_escape_string($mail_type).'\' , `email_ssl` = \''.mysql_escape_string($mail_ssl).'\' ');
  435. @mysql_close($connection);
  436. }
  437. /**
  438. * check if we can make connection to the db server with the credentials
  439. * given.
  440. */
  441. private function _make_connection($username, $password, $host)
  442. {
  443. $connection = @mysql_connect("$host", "$username", "$password");
  444. if( $connection ) {
  445. @mysql_close( $connection );
  446. return TRUE;
  447. }else {
  448. @mysql_close( $connection );
  449. return FALSE;
  450. }
  451. }
  452. /**
  453. * Set up stat tracking
  454. */
  455. private function _configure_stats($sitename, $url, $host, $username, $password, $db_name, $table_prefix = NULL)
  456. {
  457. $table_prefix = ($table_prefix) ? $table_prefix.'_' : "";
  458. $stat_url = 'http://tracker.ushahidi.com/px.php?task=cs&sitename='.urlencode($sitename).'&url='.urlencode($url);
  459. $xml = simplexml_load_string($this->_curl_req($stat_url));
  460. $stat_id = (string)$xml->id[0];
  461. $stat_key = (string)$xml->key[0];
  462. if($stat_id > 0){
  463. $connection = @mysql_connect("$host", "$username", "$password");
  464. @mysql_select_db($db_name,$connection);
  465. @mysql_query('UPDATE `'.$table_prefix.'settings` SET `stat_id` = \''.mysql_escape_string($stat_id).'\', `stat_key` = \''.mysql_escape_string($stat_key).'\' WHERE `id` =1 LIMIT 1;');
  466. @mysql_close($connection);
  467. return $stat_id;
  468. }
  469. return false;
  470. }
  471. /**
  472. * Change permissions on the cache, logs, and upload folders.
  473. */
  474. private function _chmod_folders()
  475. {
  476. @chmod('../application/cache',0777);
  477. @chmod('../application/logs',0777);
  478. @chmod('../media/uploads',0777);
  479. }
  480. /**
  481. * check if ushahidi has been installed.
  482. */
  483. public function is_ushahidi_installed()
  484. {
  485. /**
  486. * Check if config file exists.
  487. */
  488. $is_installed = true;
  489. if( file_exists('../application/config/database.php') )
  490. {
  491. $database_file = file('../application/config/database.php');
  492. if( preg_match( "/username/",$database_file[22] ) &&
  493. preg_match( "/password/",$database_file[23] ) ){
  494. $is_installed = false;
  495. }
  496. } else {
  497. $is_installed = false;
  498. }
  499. return $is_installed;
  500. }
  501. /**
  502. * Helper function to send a cURL request
  503. * @param url - URL for cURL to hit
  504. */
  505. public function _curl_req( $url )
  506. {
  507. // Make sure cURL is installed
  508. if (!function_exists('curl_exec')) {
  509. return false;
  510. }
  511. $curl_handle = curl_init();
  512. curl_setopt($curl_handle,CURLOPT_URL,$url);
  513. curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,15); // Timeout set to 15 seconds. This is somewhat arbitrary and can be changed.
  514. curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); //Set curl to store data in variable instead of print
  515. $buffer = curl_exec($curl_handle);
  516. curl_close($curl_handle);
  517. return $buffer;
  518. }
  519. /**
  520. * Check if relevant directories are writable.
  521. */
  522. public function _check_writable_dir() {
  523. global $form;
  524. if( !is_writable('../.htaccess')) {
  525. $form->set_error('htaccess_perm',
  526. "<strong>Oops!</strong> Ushahidi is unable to write to your <code>.htaccess</code> file. " .
  527. "Please change the permissions of that file to allow write access (777). ");
  528. }
  529. if( !is_writable('../application/config')) {
  530. $form->set_error('config_folder_perm',
  531. "<strong>Oops!</strong> Ushahidi needs the <code>application/config</code> folder to be writable. ".
  532. "Please change the permissions of that folder to allow write access (777). ");
  533. }
  534. if( !is_writable('../application/config/config.php')) {
  535. $form->set_error('config_file_perm',
  536. "<strong>Oops!</strong> Ushahidi is unable to write to <code>application/config/config.php</code> file. " .
  537. "Please change the permissions of that file to allow write access (777). ");
  538. }
  539. if( !is_writable('../application/cache')) {
  540. $form->set_error('cache_perm',
  541. "<strong>Oops!</strong> Ushahidi needs <code>application/cache</code> folder to be writable. ".
  542. "Please change the permissions of that folder to allow write access (777). ");
  543. }
  544. if( !is_writable('../application/logs')) {
  545. $form->set_error('logs_perm',
  546. "<strong>Oops!</strong> Ushahidi needs <code>application/logs</code> folder to be writable. " .
  547. "Please change the permissions of that folder to allow write access (777). ");
  548. }
  549. if( !is_writable('../media/uploads')) {
  550. $form->set_error('uploads_perm',
  551. "<strong>Oops!</strong> Ushahidi needs <code>media/uploads</code> folder to be writable. " .
  552. "Please change the permissions of that folder to allow write access (777). ");
  553. }
  554. /**
  555. * error exists, have user correct them.
  556. */
  557. if( $form->num_errors > 0 ) {
  558. return 1;
  559. } else {
  560. return 0;
  561. }
  562. }
  563. /**
  564. * Check if required PHP libraries are installed. Basic Mode.
  565. */
  566. public function _check_modules() {
  567. global $form, $modules;
  568. if( ! $modules->isLoaded('curl')
  569. OR ! $modules->isLoaded('pcre')
  570. OR ! $modules->isLoaded('iconv')
  571. OR ! $modules->isLoaded('mcrypt')
  572. OR ! $modules->isLoaded('SPL')
  573. OR ! $modules->isLoaded('mysql')
  574. ) {
  575. $form->set_error('modules',
  576. "<strong>Oops!</strong> Send an email to your system administrator or web host saying: \"I'm installing an application which requires
  577. <a href=\"http://php.net/curl\" target=\"_blank\">cURL</a>,
  578. <a href=\"http://php.net/pcre\" target=\"_blank\">PCRE</a>,
  579. <a href=\"http://php.net/iconv\" target=\"_blank\">iconv</a>,
  580. <a href=\"http://php.net/mcrypt\" target=\"_blank\">mcrypt</a>,
  581. <a href=\"http://php.net/spl\" target=\"_blank\">SPL</a> and
  582. <a href=\"http://php.net/mysql\" target=\"_blank\">MySQL</a>.
  583. Can you ensure that these PHP libraries are installed?\"");
  584. }
  585. /**
  586. * error exists, have user correct them.
  587. */
  588. if( $form->num_errors > 0 ) {
  589. return 1;
  590. } else {
  591. return 0;
  592. }
  593. }
  594. /**
  595. * Check if required PHP libraries are installed. Advanced Mode.
  596. */
  597. public function _check_modules_advanced() {
  598. global $form, $modules;
  599. if( ! $modules->isLoaded('curl')) {
  600. $form->set_error('curl',
  601. "<strong>Oops!</strong> Ushahidi needs <a href=\"http://php.net/curl\" target=\"_blank\">cURL</a> for getting or sending files using the URL syntax. ");
  602. }
  603. if( ! $modules->isLoaded('pcre')) {
  604. $form->set_error('pcre',
  605. "<strong>Oops!</strong> Ushahidi needs <a href=\"http://php.net/pcre\" target=\"_blank\">PCRE</a> compiled with <code>–enable-utf8</code> and <code>–enable-unicode-properties</code> for UTF-8 functions to work properly. ");
  606. }
  607. if( ! $modules->isLoaded('iconv')) {
  608. $form->set_error('iconv',
  609. "<strong>Oops!</strong> Ushahidi needs <a href=\"http://php.net/iconv\" target=\"_blank\">iconv</a> for UTF-8 transliteration. ");
  610. }
  611. if( ! $modules->isLoaded('mcrypt')) {
  612. $form->set_error('mcrypt',
  613. "<strong>Oops!</strong> Ushahidi needs <a href=\"http://php.net/mcrypt\" target=\"_blank\">mcrypt</a> for encryption. ");
  614. }
  615. if( ! $modules->isLoaded('SPL')) {
  616. $form->set_error('spl',
  617. "<strong>Oops!</strong> Ushahidi needs <a href=\"http://php.net/spl\" target=\"_blank\">SPL</a> for several core libraries. ");
  618. }
  619. if ( ! $modules->isLoaded('mysql')) {
  620. $form->set_error('mysql',
  621. "<strong>Oops!</strong> Ushahidi needs <a href=\"http://php.net/mysql\" target=\"_blank\">MySQL</a> for database access. ");
  622. }
  623. /**
  624. * error exists, have user correct them.
  625. */
  626. if( $form->num_errors > 0 ) {
  627. return 1;
  628. } else {
  629. return 0;
  630. }
  631. }
  632. /**
  633. * Adds header details to the installer html pages.
  634. */
  635. public function _include_html_header() {
  636. /*TODO make title tag configurable*/
  637. $header = <<<HTML
  638. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  639. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  640. <html xmlns="http://www.w3.org/1999/xhtml">
  641. <head>
  642. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  643. <title>Database Connections / Ushahidi Web Installer</title>
  644. <link href="../media/css/installer.css" rel="stylesheet" type="text/css" />
  645. </head>
  646. <script src="../media/js/jquery.js" type="text/javascript" charset="utf-8"></script>
  647. <script src="../media/js/login.js" type="text/javascript" charset="utf-8"></script>
  648. </head>
  649. HTML;
  650. return $header;
  651. }
  652. /**
  653. * Gets the current directory ushahidi is installed in.
  654. */
  655. public function _get_base_path($request_uri) {
  656. return substr( substr($request_uri,0,stripos($request_uri,'/installer/')) ,1);
  657. }
  658. /**
  659. * Check if clean url can be enabled on the server so
  660. * Ushahidi can emit clean URLs
  661. *
  662. * @return boolean
  663. */
  664. function _check_for_clean_url() {
  665. $url = $this->_get_url()."/installer/mod_rewrite/";
  666. $curl_handle = curl_init();
  667. curl_setopt($curl_handle, CURLOPT_URL, $url);
  668. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true );
  669. curl_exec($curl_handle);
  670. $return_code = curl_getinfo($curl_handle,CURLINFO_HTTP_CODE);
  671. curl_close($curl_handle);
  672. if( $return_code == 404 OR $return_code == 403 ) {
  673. return FALSE;
  674. } else {
  675. return TRUE;
  676. }
  677. }
  678. }
  679. $install = new Install();
  680. $form = new Form();
  681. $modules = new Modulecheck();
  682. ?>