From 8ad5d23ee609edd9e13414f3e242ad2ced7417dd Mon Sep 17 00:00:00 2001 From: martin Date: Tue, 22 Feb 2011 16:16:37 -0500 Subject: [PATCH] -api_key autogeneration and usage implemented --- application/configs/conf.php | 34 +++++++++---------- application/models/Schedule.php | 5 ++- build/{database.conf => airtime.conf} | 3 ++ build/update_db_info.py | 2 +- install/airtime-install.php | 47 +++++++++++++++++++-------- public/js/playlist/helperfunctions.js | 2 +- public/js/playlist/playlist.js | 3 ++ 7 files changed, 63 insertions(+), 33 deletions(-) rename build/{database.conf => airtime.conf} (76%) diff --git a/application/configs/conf.php b/application/configs/conf.php index be67fb6cc..2fea96bae 100644 --- a/application/configs/conf.php +++ b/application/configs/conf.php @@ -5,9 +5,11 @@ define('CAMPCASTER_COPYRIGHT_DATE', '2010'); // These are the default values for the config. global $CC_CONFIG; +$values = load_airtime_config(); + $CC_CONFIG = array( // Database config - 'dsn' => load_db_config(), + 'dsn' => $values['database'], // Name of the web server user 'webServerUser' => 'www-data', @@ -17,7 +19,7 @@ $CC_CONFIG = array( /* ================================================ storage configuration */ - 'apiKey' => array('AAA'), + 'apiKey' => $values['api_key'], 'apiPath' => "/api/", @@ -162,22 +164,20 @@ set_include_path('.'.PATH_SEPARATOR.$CC_CONFIG['pearPath'] .PATH_SEPARATOR.$CC_CONFIG['zendPath'] .PATH_SEPARATOR.$old_include_path); -//$dsn = $CC_CONFIG['dsn']; -//$CC_DBC = DB::connect($dsn, TRUE); -//if (PEAR::isError($CC_DBC)) { -// echo "ERROR: ".$CC_DBC->getMessage()." ".$CC_DBC->getUserInfo()."\n"; -// exit(1); -//} -//$CC_DBC->setFetchMode(DB_FETCHMODE_ASSOC); -function load_db_config(){ - $ini_array = parse_ini_file(dirname(__FILE__).'/../../build/database.conf', true); +function load_airtime_config(){ + $ini_array = parse_ini_file(dirname(__FILE__).'/../../build/airtime.conf', true); - return array( - 'username' => $ini_array['database']['dbuser'], - 'password' => $ini_array['database']['dbpass'], - 'hostspec' => $ini_array['database']['host'], - 'phptype' => 'pgsql', - 'database' => $ini_array['database']['dbname']); + return array( + 'database' => array( + 'username' => $ini_array['database']['dbuser'], + 'password' => $ini_array['database']['dbpass'], + 'hostspec' => $ini_array['database']['host'], + 'phptype' => 'pgsql', + 'database' => $ini_array['database']['dbname']), + 'api_key' => array($ini_array['general']['api_key']) + ); + + } ?> diff --git a/application/models/Schedule.php b/application/models/Schedule.php index 87ff2193b..c98a65ae2 100644 --- a/application/models/Schedule.php +++ b/application/models/Schedule.php @@ -432,6 +432,8 @@ class Schedule { return array(); } + global $CC_CONFIG; + $date = new Application_Model_DateHelper; $timeNow = $date->getDate(); return array("env"=>APPLICATION_ENV, @@ -442,7 +444,8 @@ class Schedule { "currentShow"=>Show_DAL::GetCurrentShow($timeNow), "nextShow"=>Show_DAL::GetNextShow($timeNow), "timezone"=> date("T"), - "timezoneOffset"=> date("Z")); + "timezoneOffset"=> date("Z"), + "apiKey"=>$CC_CONFIG['apiKey'][0]); } /** diff --git a/build/database.conf b/build/airtime.conf similarity index 76% rename from build/database.conf rename to build/airtime.conf index 9b0e7fca8..e72855457 100644 --- a/build/database.conf +++ b/build/airtime.conf @@ -4,3 +4,6 @@ dbname = airtime dbuser = airtime dbpass = airtime +[general] +api_key = AAA + diff --git a/build/update_db_info.py b/build/update_db_info.py index 70a341f50..ece58390a 100644 --- a/build/update_db_info.py +++ b/build/update_db_info.py @@ -11,7 +11,7 @@ from xml.dom.minidom import Node #Read the universal values parser = ConfigParser.SafeConfigParser() -parser.read('database.conf') +parser.read('airtime.conf') section_names = parser.sections(); items_in_section = parser.items(section_names[0]) diff --git a/install/airtime-install.php b/install/airtime-install.php index 833a5ab7c..25a1ce360 100644 --- a/install/airtime-install.php +++ b/install/airtime-install.php @@ -15,10 +15,31 @@ if (isset($arr["DOCUMENT_ROOT"]) && ($arr["DOCUMENT_ROOT"] != "") ) { exit(1); } +createAPIKey(); + require_once(dirname(__FILE__).'/../application/configs/conf.php'); require_once(dirname(__FILE__).'/../application/models/GreenBox.php'); require_once(dirname(__FILE__).'/installInit.php'); + +function rand_string($len=20, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') +{ + $string = ''; + for ($i = 0; $i < $len; $i++) + { + $pos = mt_rand(0, strlen($chars)-1); + $string .= $chars{$pos}; + } + return $string; +} + +function createAPIKey(){ + + $api_key = rand_string(); + updateINIKeyValues('../build/airtime.conf', 'api_key', $api_key); + updateINIKeyValues('../pypo/config.cfg', 'api_key', "'$api_key'"); +} + function checkIfRoot(){ // Need to check that we are superuser before running this. if(exec("whoami") != "root"){ @@ -27,21 +48,21 @@ function checkIfRoot(){ } } -// Need to check if build.properties project home is set correctly. -function setBuildPropertiesPath(){ - $property = 'project.home'; - $lines = file('../build/build.properties'); - foreach ($lines as $key => &$line) { - if ($property == substr($line, 0, strlen($property))){ - $line = $property." = ".realpath(__dir__.'/../')."\n"; - } +function updateINIKeyValues($filename, $property, $value){ + $lines = file($filename); + $n=count($lines); + for ($i=0; $i<$n; $i++) { + if (strlen($lines[$i]) > strlen($property)) + if ($property == substr($lines[$i], 0, strlen($property))){ + $lines[$i] = "$property = $value\n"; + } } - $fp=fopen('../build/build.properties', 'w'); - foreach($lines as $key => $line){ - fwrite($fp, $line); + $fp=fopen($filename, 'w'); + for($i=0; $i<$n; $i++){ + fwrite($fp, $lines[$i]); } - fclose($fp); + fclose($fp); } function directorySetup($CC_CONFIG){ @@ -75,7 +96,7 @@ echo " *** Directory Setup ***\n"; checkIfRoot(); -setBuildPropertiesPath(); +updateINIKeyValues('../build/build.properties', 'project.home', realpath(__dir__.'/../')); echo "******************************** Install Begin *********************************\n"; diff --git a/public/js/playlist/helperfunctions.js b/public/js/playlist/helperfunctions.js index 19c73c96d..a9a57467c 100644 --- a/public/js/playlist/helperfunctions.js +++ b/public/js/playlist/helperfunctions.js @@ -157,7 +157,7 @@ function audioPreview(filename, elemID){ } var ext = getFileExt(filename); - var uri = "/api/get-media/api_key/AAA/file/" + filename; + var uri = "/api/get-media/api_key/" + apiKey + "/file/" + filename; var media; var supplied; diff --git a/public/js/playlist/playlist.js b/public/js/playlist/playlist.js index 17e48082f..e4aced3d6 100644 --- a/public/js/playlist/playlist.js +++ b/public/js/playlist/playlist.js @@ -23,6 +23,8 @@ var APPLICATION_ENV = ""; var nextSongPrepare = true; var nextShowPrepare = true; +var apiKey = ""; + function getTrackInfo(song){ var str = ""; @@ -187,6 +189,7 @@ function calcAdditionalShowData(show){ function parseItems(obj){ APPLICATION_ENV = obj.env; + apiKey = obj.apiKey; $('#time-zone').text(obj.timezone);