loadSettings(); // Initialise connection to Google Analytics API and set date range $this->initGoogle(); } /** * Retrieves google username and password and creates an instance of the google analytics connector * class, which is stored in the variable $this->google * */ private function loadGoogle() { // If the SEO component is not installed then we check the jLite settings table. if($this->seoTableExists()){ $details = $this->getSeoDetails(); // If 'guser' or 'gpass' is not set in #__seo_opts, then check to see if they are set in #__jlite_settings if($details['guser'] == '' || $details['gpass'] == ''){ $jlitedetails = $this->getJliteDetails(); if($jlitedetails['guser'] != '' && $jlitedetails['gpass'] != ''){ $details['guser'] = $jlitedetails['guser']; $details['gpass'] = $jlitedetails['gpass']; // The jLite database contains Google user, and pass, so we update // the #__seo_opts table with these values $this->updateSeoTable($jlitedetails['guser'], $jlitedetails['gpass']); } } } else { $details = $this->getJliteDetails(); } require 'includes/analytics.class.php'; $this->google = new analytics($details['guser'], base64_decode($details['gpass'])); } private function initGoogle(){ try { // Authenticates to Google using data from DB $this->loadGoogle(); $this->profiles = $this->google->getProfileList(); $this->profileKeys = array_keys($this->profiles); // go through profile keys to see if the profile id stored in the database is present $active_profile_id = 0; if($this->settings[gprofileid] != '' && !empty($this->profileKeys)){ // Go through the profile keys from the Google account and match the // profile id to the one held in the database foreach($this->profileKeys as $profileIDFromGoogle){ // We must put 'ga:' in front of the profile id when authorizing into google $profileIDFromDB = 'ga:' . $this->settings[gprofileid]; if($profileIDFromGoogle == $profileIDFromDB){ $active_profile_id = $profileIDFromGoogle; } } // If a match was not found, use the first profile supplied by google if(!$active_profile_id){ $this->googleError = true; $this->googleErrorMessage = 'The Profile ID you have provided is not available on the Google account you have specified. Please ensure you have entered your profile ID correctly.'; } } else { // If no profile id has been supplied by user, ask them to do so $this->googleError = true; $this->googleErrorMessage = 'You have not entered a Profile ID. Please enter a Profile ID to enable your website statistics.'; } // Set the profile ID $this->google->setProfileById($active_profile_id); $this->selectedMonth = date('n'); // Check request variables to see if the month has been set through GET or POST $this->view = JRequest::getVar('view', 'graph'); $date = JRequest::getVar('month', ''); if ($date != "") { $this->selectedMonth = $date; } // calculate start date and end date for google lookup $year = date('Y'); // The month must have a leading zero if less than 10 $month = $this->selectedMonth; if($month < 10){ $month = '0' . $month; } // With the ISO week number for the first day of the month we can then // retrieve the first day of the ISO week, which may be on the previous month // the end date is set to 22 days wich will give us five weeks of results $ISOWeekNumber = date('W', strtotime("{$year}-{$month}-01")); if($month == '01'){ $ISOWeekNumber = '01'; } $firstISOWeekDay = $this->getFirstDayOfISOWeek($ISOWeekNumber, $year); $startDate = date('Y-m-d', $firstISOWeekDay - 172800); // 172800 is 2 days in seconds $endDate = date('Y-m-d', ($firstISOWeekDay + 1900800)); // 1900800 is 22 days in seconds $this->google->setDateRange($startDate,$endDate); } catch (Exception $e) { //If an exception is caught here it is likely that either the Username or Password for the Google account is invalid $this->googleError = true; $this->googleErrorMessage = 'We could not connect to Google Analytics using the username and password you provided. Please ensure you have entered your Google account details correctly.'; } } private function loadSettings() { $db =& JFactory::getDBO(); // if($this->seoTableExists()){ // Load details from #__seo_opts table $seodetails = $this->getSeoDetails(); $this->settings['guser'] = $seodetails['guser']; $this->settings['gpass'] = $seodetails['gpass']; $this->settings['gprofileid'] = $seodetails['gprofileid']; $this->settings['gwebpropertyid'] = $seodetails['gwebpropertyid']; // Load details from #__jlite_settings to gain estara details $jlitedetails = $this->getJliteDetails(); $this->settings['phone'] = $jlitedetails['phone']; $this->settings['euser'] = $jlitedetails['euser']; $this->settings['epass'] = $jlitedetails['epass']; // If 'guser' or 'gpass' is not set in #__seo_opts, then check to see if they are set in #__jlite_settings if($seodetails['guser'] == '' || $seodetails['gpass'] == ''){ if($jlitedetails['guser'] != '' && $jlitedetails['gpass'] != ''){ $this->settings['guser'] = $jlitedetails['guser']; $this->settings['gpass'] = $jlitedetails['gpass']; // The jLite database contains Google user, and pass, so we update // the #__seo_opts table with these values $this->updateSeoTable($jlitedetails['guser'], $jlitedetails['gpass']); } } // If the jLite phone value is empty, try the SEO table. Also we must check that // if both the tables have a value we use the value from the SEO table, as this // must be treated as correct. if($seodetails['estara'] != ''){ if($this->settings['phone'] == '' || $this->settings['phone'] != $seodetails['estara']){ $this->settings['phone'] = $seodetails['estara']; // Update jLite table to include phone value $this->updateJliteDetails(false, false, false, false, $this->settings['phone']); } } } else { // Load details from #__jlite_settings to gain estara details $jlitedetails = $this->getJliteDetails(); $this->settings['guser'] = $jlitedetails['guser']; $this->settings['gpass'] = $jlitedetails['gpass']; $this->settings['gprofileid'] = $jlitedetails['gprofileid']; $this->settings['gwebpropertyid'] = $jlitedetails['gwebpropertyid']; $this->settings['phone'] = $jlitedetails['phone']; $this->settings['euser'] = $jlitedetails['euser']; $this->settings['epass'] = $jlitedetails['epass']; } } /** * Updates SEO table if it exists (if SEO component installed), and update jLite Settings table * * @param Array data supplied by form */ public function saveSettings($data) { // If the SEO table exists (i.e. SEO component) update guser, gpass, gprofileid, gwebpropertyid if($this->seoTableExists()){ $this->updateSeoTable($data['guser'], $data['gpass'], $data['gprofileid'], $data['gwebpropertyid']); } // Update the #__jlite_settings table $this->updateJliteDetails($data['guser'], $data['gpass'], $data['gprofileid'], $data['gwebpropertyid'], false, $data['euser'], $data['epass']); }// End function: saveSettings() /** * Returns true if SEM installed, false otherwise * * @return Boolean true if installed, false otherwise */ public function semInstalled(){ if($this->seoTableExists()){ $db =& JFactory::getDBO(); $query = "SELECT * FROM `#__seo_opts` WHERE `id`='1'"; $db->setQuery($query); if (!$db->query()) { return false; } $details = $db->loadAssoc(); // If the field `semactive` is 1, SEM installed if($details['semactive'] == '1'){ return true; } else { return false; } } else { return false; } }// End function: semInstalled() /** * Looks for the database table #__seo_opts, and returns true if it is present, and false otherwise. * * @return Boolean TRUE if table exists, FALSE if not */ private function seoTableExists(){ $db =& JFactory::getDBO(); $table_name = $db->getPrefix() . 'seo_opts'; $db->getPrefix(); $query = $query = "show tables like '{$table_name}'"; $db->setQuery( $query ); $res = $db->loadAssoc(); // A result will only be present if the SQL found a table with the required name if(is_array($res) && !empty($res)){ return true; } else { return false; } }// End function: seoTableExists() /** * Returns results from #__seo_opts table as Associative array, false if error occured. * * @return Array results from database, or FALSE if error occured */ private function getSeoDetails(){ $db =& JFactory::getDBO(); $query = "SELECT * FROM `#__seo_opts` WHERE `id`='1'"; $db->setQuery($query); if (!$db->query()) { return false; } $details = $db->loadAssoc(); return $details; }// End function: getSeoDetails() /** * Updates the #__seo_opts table * * @param string guser value * @param string gpass value * @param string gprofileid value * @param string gwebpropertyid */ private function updateSeoTable($guser, $gpass, $gprofileid = false, $gwebpropertyid = false){ $query = "UPDATE `#__seo_opts` SET `guser`='" . $this->cleanLines($guser) . "', `gpass`='" . $this->cleanLines(base64_encode($gpass)) . "'"; if($gprofileid !== false){ $query .= ", `gprofileid`='" . $this->cleanLines($gprofileid) . "'"; } if($gwebpropertyid !== false){ $query .= ", `gwebpropertyid`='" . $this->cleanLines($gwebpropertyid) . "'"; } $query .= " WHERE `id` = '1'"; $db =& JFactory::getDBO(); $db->setQuery( $query ); $db->query(); }// End function: updateSeoTable() /** * Returns results from #__jlite_settings table as Associative array, false if error occured. * * @return Array results from database, or FALSE if error occured */ private function getJliteDetails(){ $db =& JFactory::getDBO(); $query = "SELECT * FROM `#__jlite_settings` WHERE `id`='1'"; $db->setQuery($query); if (!$db->query()) { return false; } $details = $db->loadAssoc(); return $details; }// End function: getJliteDetails() /** * Updates the #__jlite_settings table * * @param string guser value * @param string gpass value * @param string gprofileid value * @param string gwebpropertyid * @param string phone value * @param string euser * @param string epass */ private function updateJliteDetails($guser = false, $gpass = false, $gprofileid = false, $gwebpropertyid = false, $phone = false, $euser = false, $epass = false){ $updateRequired = false; $first = true; $query = 'UPDATE `#__jlite_settings` SET '; if($guser !== false){ if(!$first){ $query .= ', '; } else { $first = false; } $query .= "`guser`='" . $this->cleanLines($guser) . "'"; $updateRequired = true; } if($gpass !== false){ if(!$first){ $query .= ', '; } else { $first = false; } $query .= "`gpass`='" . $this->cleanLines(base64_encode($gpass)) . "'"; $updateRequired = true; } if($gprofileid !== false){ if(!$first){ $query .= ', '; } else { $first = false; } $query .= "`gprofileid`='" . $this->cleanLines($gprofileid) . "'"; $updateRequired = true; } if($gwebpropertyid !== false){ if(!$first){ $query .= ', '; } else { $first = false; } $query .= "`gwebpropertyid`='" . $this->cleanLines($gwebpropertyid) . "'"; $updateRequired = true; } if($phone !== false){ if(!$first){ $query .= ', '; } else { $first = false; } $query .= "`phone`='" . $this->cleanLines($phone) . "'"; $updateRequired = true; } if($euser !== false){ if(!$first){ $query .= ', '; } else { $first = false; } $query .= "`euser`='" . $this->cleanLines($euser) . "'"; $updateRequired = true; } if($epass !== false){ if(!$first){ $query .= ', '; } else { $first = false; } $query .= "`epass`='" . $this->cleanLines($epass) . "'"; $updateRequired = true; } $query .= " WHERE `id` = '1'"; if($updateRequired){ $db =& JFactory::getDBO(); $db->setQuery( $query ); $db->query(); } }// End function: updateJliteDetails() public function build($seo = false) { if ($this->googleError) { echo "