PageRenderTime 326ms CodeModel.GetById 141ms app.highlight 12ms RepoModel.GetById 169ms app.codeStats 0ms

/php/config.php

http://github.com/newscloud/open-social-media-toolkit
PHP | 267 lines | 204 code | 15 blank | 48 comment | 34 complexity | f426c8c957c86d12e17d0aafcdb3f924 MD5 | raw file
  1<?php
  2	/* INSTALLATION MENU */
  3	/* Used for preparing the application */
  4
  5	// check for database
  6	global $init; // global handle to the secret keys
  7	$database = $init['database'];
  8	$username = $init['username'];
  9	$password = $init['password'];
 10	$hostname = $init['hostname'];
 11	$con = mysql_connect($hostname, $username, $password);
 12	if (!$con)
 13    {
 14	  die('Could not connect: ' . mysql_error());
 15	}
 16	if (mysql_num_rows(mysql_query("SHOW DATABASES like '$database'"))==0) {
 17		if (mysql_query("CREATE DATABASE $database")) {
 18		  echo "Database created<br />";
 19		} else
 20		  {
 21		  echo "Error creating database: " . mysql_error();
 22		  }		
 23	}
 24		
 25	/* initialize database and libraries */
 26	include_once ('initialize.php');
 27		
 28	/* Process incoming variable requests */	
 29	if (isset($_GET['apiKey'])) {
 30		$apiKey=$_GET['apiKey'];
 31		if (strlen($apiKey)>35) die('API key is invalid');
 32	}
 33	if (!defined('NO_SECURITY') OR !NO_SECURITY) {	
 34		if (!isset($_GET['apiKey'])) 
 35 		{
 36			echo 'You need to include your secret as an argument in your url.';
 37			die();		
 38		} else {
 39			// verify api key
 40			if ($init['apiKey']<>$apiKey) {
 41				echo 'Invalid access!';
 42				die();
 43			}			
 44		}
 45	}
 46	if (isset($_GET['action'])) {
 47		$action=$_GET['action'];
 48		if (strlen($action)>15) exit();
 49	} else 
 50		$action='menu';
 51
 52	$menu='<a name="menu" /><h1>Welcome to the NewsCloud Social Media Toolkit</h1>';
 53	$menu.='<p>Connected to the '.$db->database.' database</p>';
 54	$menu.='<h3>Installation Menu:</h3><ol>';
 55	$menu.='<li style="list-style-type: decimal;"><a href="?p=config&action=initDB&apiKey='.$apiKey.'">Initialize the database</a></li>';
 56	$menu.='<li style="list-style-type: decimal;"><a href="?p=config&action=addTestData&apiKey='.$apiKey.'">Populate test data</a></li>';
 57	$menu.='<li style="list-style-type: decimal;"><a href="?p=config&action=uploadSettings&apiKey='.$apiKey.'">Upload your Application settings to Facebook</a></li>';	
 58	$menu.='<li style="list-style-type: decimal;">Visit your <a target="_fbapp" href="'.URL_CANVAS.'">Application on Facebook</a></li>';
 59	$menu.='<li style="list-style-type: decimal;">Visit the <a target="_fbmc" href="'.URL_CONSOLE.'">Management Console</a></li>';
 60	$menu.='<li style="list-style-type: decimal;">Remove <a href="?p=config&action=removeTestData&apiKey='.$apiKey.'">test data</a></li>';
 61	$menu.='<li style="list-style-type: decimal;"><a target="_fbhelp" href="http://support.newscloud.com/discussions/site-gallery">Add your site</a> to our public gallery</li>';
 62	$menu.='</ol>';
 63		
 64	// deprecated -	$menu.='<li><a href="?p=config&action=reSync&apiKey='.$apiKey.'">Synchronize the database</a> with the remote NewsCloud server</li>';
 65	// deprecated -		$menu.='<li>Hit p=config, action=cleanupUser with userid param set to userid to clean out their user records</li>';
 66	// deprecated -		$menu.='<li><a href="?p=config&action=rewardBetaTesters&apiKey='.$apiKey.'">Submits beta tester\'s emails for Beta Test HotDish challenge (1-time)</a></li>';
 67	//$menu.='<li><a href="?p=config&action=resetDB&apiKey='.$apiKey.'">Reset the database</a> - erases everything</li>';
 68	//$menu.='<li><a href="?p=config&action=resetLog&apiKey='.$apiKey.'">Reset the log</a> - erases all user activities</li>';
 69		
 70	switch ($action) {
 71		default:
 72		break;
 73		case 'initDB':
 74			echo 'Scroll down to the <a href="#menu">installation menu</a> when complete<br /><br />';
 75			// override default time limit
 76			set_time_limit(300);			
 77			include_once PATH_CORE."utilities/initDatabase.php";
 78			require_once(PATH_CORE.'/classes/cron.class.php');
 79			$cObj=new cron($init['apiKey']);
 80			$cObj->initJobs();
 81			$cObj->resetJobs();
 82		break;
 83		case 'addTestData':
 84			include_once PATH_CORE."utilities/populateTestData.php";			
 85		break;
 86		case 'removeTestData':
 87			include_once PATH_CORE."utilities/removeTestData.php";			
 88		break;
 89		case 'uploadSettings':
 90				echo 'Uploading settings in SystemStatus table up to Facebook Developer application<br />';
 91				require_once (PATH_CORE.'/classes/systemStatus.class.php');
 92				$ssObj=new systemStatus();
 93				$propList=$ssObj->loadFacebookProperties();
 94				echo 'These settings will be uploaded:<br />';
 95				print_r($propList);
 96				echo '<br />';
 97				require_once PATH_FACEBOOK."/classes/app.class.php";
 98				$app=new app(NULL,true);
 99				$facebook=&$app->loadFacebookLibrary();				
100				$props=$facebook->api_client->admin_setAppProperties($propList);
101				echo 'Completed settings upload. Visit the <a href="http://www.facebook.com/developers/apps.php?app_id='.$init['fbAppId'].'">Facebook Developer app</a> to review them.<br />';						
102		break;					
103		case 'populateSubscriptions':
104			require_once(PATH_CORE.'/classes/subscriptions.class.php');
105			$subObj=new SubscriptionsManager($db);
106			$subObj->populateSubscriptions();
107		break;
108		case 'resetDB':
109			require_once(PATH_CORE.'/classes/cleanup.class.php');
110			$cObj=new cleanup($db);
111			$cObj->flushDatabase();
112			if (MODULE_FACEBOOK)
113			{	
114				echo 'cleaning up fb tables<br />';
115				require_once(PATH_FACEBOOK.'/classes/cleanupFacebook.class.php');
116				$cleanupObj=new cleanupFacebook($db,'');
117				$cleanupObj->flushDatabase(); 
118				echo 'fb/st Database cleansed<br />';
119			}	
120		break;
121		case 'resetLog':
122			$db->delete("Log"); // for debug purposes
123		break;
124		case 'testJob':			
125			$job = $_GET['job'];
126			echo 'Testing job:'. $job. '<br>';
127				require_once(PATH_CORE.'/classes/cron.class.php');
128			$cObj=new cron($init['apiKey']);
129			$cObj->forceJob($job);
130		break;
131		case 'cleanupUser':
132			require_once (PATH_CORE. '/classes/teamBackend.class.php');
133			$teamObj = new teamBackend($db);
134			$teamObj->cleanupUser($_GET['userid']);
135		break;
136		/* deprecated
137			case 'rewardBetaTesters':
138				require_once (PATH_CORE. '/classes/teamBackend.class.php');
139				$teamObj = new teamBackend($db);
140				$teamObj->rewardBetaTesters(); 				
141			break; 
142			case 'reSync':
143				// call all cron jobs with new sync info
144				require_once(PATH_CORE.'/classes/cron.class.php');
145				$cObj=new cron($init['apiKey']);
146				$db->update("cronJobs","nextRun=0","1=1");
147				$cObj->fetchJobs();
148			break;
149			*/
150	}
151	// check for warnings
152	// re-run
153	$warnings=checkWarnings($db);			
154	echo $menu.$warnings;
155	$resources='<h3>Useful resources:</h3><ul>';
156	$resources.='<li><a target="_fbhelp" href="http://support.newscloud.com">NewsCloud Open Source Support Community </a></li>';
157	$resources.='<li><a target="_fbhelp" href="http://opensource.newscloud.com">NewsCloud Open Source Blog</a></li>';
158	$resources.='<li><a target="_fbhelp" href="http://blog.newscloud.com/services.html">NewsCloud Consulting Services</a></li>';
159	$resources.='<li><a target="_fbhelp" href="http://youreyelevel.com/smt/">Eye Level Design Consulting</a></li>';
160	$resources.='</ul>';
161	$resources.='<p><a href="http://www.twitter.com/newscloud"><img src="http://twitter-badges.s3.amazonaws.com/follow_bird-c.png" alt="Follow newscloud on Twitter"/></a>';
162	echo $resources;
163	
164	function checkWarnings(&$db) {
165		$warnings='';
166		$q=$db->query("SHOW TABLES;"); // check if db has been configured
167		if ($db->countQ($q)>0) {
168			$q=$db->query("SELECT * FROM User,UserInfo WHERE User.userid=UserInfo.userid AND isAdmin=1");
169			if ($db->countQ($q)==0) {
170				$warnings.='<p><span style="color:red;"><strong>WARNING: No administrator yet.</strong></span> Set admin email in constants and run populate test data.</p>';
171			} else {
172				$data=$db->readQ($q);
173				if ($data->fbId==0) {
174					$warnings.='<p><span style="color:red;"><strong>WARNING: Administrator Facebook account not configured. <a href="'.URL_CANVAS.'?p=setAdmin">Configure now</a></strong></span></p>';
175				}
176			}			
177		}
178		
179		// check for write access to cache directory for CSS, JS files
180		try {	
181			$tempStr=' <p><span style="color:red;"><strong>WARNING:</strong></span>Caching problem with CSS stylesheets and Javascript files - could not open or write to '.PATH_CACHE.' - set ownership for Apache Web service e.g. chown www-user:www-user '.PATH_CACHE.' and set write permissions for the directory chmod -R 755 '.PATH_CACHE.'</p>';
182			$handle = fopen(PATH_CACHE.DS.'configTest.txt', "a");
183			if (!$handle) 
184				$warnings.=$tempStr;
185			else {
186				$res=fwrite($handle,"config.php cache test");
187				if (!$res) $warnings.=$tempStr;									
188			}
189		} catch (Exception $e) {
190			$warnings.=$tempStr;
191		}			
192
193		if (defined('NO_SECURITY') AND NO_SECURITY) {
194			global $init;	
195			$warnings.='<p><span style="color:red;"><strong>WARNING: Security is turned off</strong></span> Before you launch, be sure to turn security on in constants.php. <strong>Important</strong>: Bookmark <strong><a href="'.SITE_DOMAIN.'?p=config&apiKey='.$init['apiKey'].'">this link</a></strong> before you turn security on. After you turn security on, you can reach the management console from the Admin link in the footer of your Facebook application.</p>';
196		}
197
198		if (defined('NO_CACHE') AND NO_CACHE) {	
199			$warnings.='<p><span style="color:orange;"><strong>NOTICE: Caching is turned off</strong></span> When you are ready to launch, you can turn on caching in constants.php to enhance performance. You will need to permission the '.PATH_CACHE.' directory for Apache to write to.</p>';
200		} 
201		
202		if (defined('NO_LOGGING') AND NO_LOGGING) {	
203			$warnings.='<p><span style="color:orange;"><strong>NOTICE: Logging is turned off</strong></span> You can turn it on constants.php. You will need to touch and permission the specified /var/logs/*.log files for Apache to write to.</p>';
204		} else {
205			try {	
206				$tempStr=' <p><span style="color:red;"><strong>WARNING:</strong></span> Logging problem - could not open or write to '.PATH_LOGFILE.' - create the file e.g. touch '.PATH_LOGFILE.', then set ownership for Apache Web service e.g. chown www-data:www-data '.PATH_LOGFILE.'</p>';
207				$handle = fopen(PATH_LOGFILE, "a");
208				if (!$handle) 
209					$warnings.=$tempStr;	
210				else {
211					$res=fwrite($handle,"config.php log test");
212					if (!$res) $warnings.=$tempStr;									
213				}
214			} catch (Exception $e) {
215				$warnings.=$tempStr;
216			}			
217		}
218
219		// check for write access to uploaded file directories - PATH_UPLOAD_IMAGES
220		try {	
221			$tempStr=' <p><span style="color:yellow;"><strong>Warning:</strong></span> Uploading user submissions - could not open or write to '.PATH_UPLOAD_IMAGES.' - set ownership for Apache Web service e.g. chown www-user:www-user '.PATH_UPLOAD_IMAGES.' and set write permissions for the directory chmod -R 755 '.PATH_UPLOAD_IMAGES.'</p>';
222			$handle = fopen(PATH_UPLOAD_IMAGES.'configTest.txt', "a");
223			if (!$handle) 
224				$warnings.=$tempStr;
225			else {
226				$res=fwrite($handle,"config.php upload test");
227				if (!$res) $warnings.=$tempStr;									
228			}
229		} catch (Exception $e) {
230			$warnings.=$tempStr;
231		}			
232
233		// check for write access to uploaded file directories - PATH_UPLOAD_SUBMISSIONS
234		try {	
235			$tempStr=' <p><span style="color:yellow;"><strong>Warning:</strong></span> Uploading user submissions - could not open or write to '.PATH_UPLOAD_SUBMISSIONS.' - set ownership for Apache Web service e.g. chown www-user:www-user '.PATH_UPLOAD_SUBMISSIONS.' and set write permissions for the directory chmod -R 755 '.PATH_UPLOAD_SUBMISSIONS.'</p>';
236			$handle = fopen(PATH_UPLOAD_SUBMISSIONS.'configTest.txt', "a");
237			if (!$handle) 
238				$warnings.=$tempStr;
239			else {
240				$res=fwrite($handle,"config.php upload test");
241				if (!$res) $warnings.=$tempStr;									
242			}
243		} catch (Exception $e) {
244			$warnings.=$tempStr;
245		}			
246
247		
248		return $warnings;
249	}
250	/* deprecated
251	// initialize partner registration for this site and domain (if first time)
252	require_once (PATH_CORE.'classes/systemStatus.class.php');
253	$ssObj=new systemStatus($db);
254	if ($ssObj->checkTable()) {
255		$partnerid=$ssObj->getState('partnerid');
256		if ($partnerid==0) {
257			require_once (PATH_CORE.'/classes/apiCloud.class.php');
258			$apiObj=new apiCloud($db,$init[apiKey]);
259			$resp=$apiObj->partnerRegister(SITE_CLOUDID,URL_HOME,SITE_TITLE);
260			if ($resp[result]) {
261				$partnerid=$resp[items][0][partnerid];
262				$ssObj->setState('partnerid',$partnerid);
263			}
264		}		
265	}
266	*/
267?>