function edit_preferences($variable_name, $variable_value)
 {
     if (!$this->check_token()) {
         return false;
     }
     if (!vivvo_hooks_manager::call('vivvoCore_preferences', array(&$variable_name, &$variable_value))) {
         return vivvo_hooks_manager::get_status();
     }
     $sm = vivvo_lite_site::get_instance();
     if ($this->_user && $this->_user->is_admin()) {
         require_once VIVVO_FS_FRAMEWORK . 'vivvo_preference.php';
         $preferences_list = new preferences_list();
         $preferences = $preferences_list->get_preference_by_variable_name($variable_name);
         if ($preferences != false) {
             $variable_value = htmlspecialchars($variable_value, ENT_QUOTES, 'UTF-8');
             if ($variable_name == 'VIVVO_GENERAL_TIME_ZONE_FORMAT') {
                 $timezone_abbreviations = DateTimeZone::listIdentifiers();
                 if (in_array($variable_value, $timezone_abbreviations)) {
                     $preferences->set_variable_value($variable_value);
                     $this->_post_master->set_data_object($preferences);
                 } else {
                     $this->set_error_code(5122);
                     return false;
                 }
             } elseif ($variable_name == 'VIVVO_ALLOWED_IP_ADDRESSES') {
                 // normalize passed string (convert to comma-separated IPs)
                 $variable_value = str_replace(array("\r\n", "\n"), ',', $variable_value);
                 $variable_value = preg_replace('/\\s+/', '', $variable_value);
                 $variable_value = trim(preg_replace('/,+/', ',', $variable_value), ',');
                 if (!empty($variable_value)) {
                     $addresses = explode(',', $variable_value);
                     // check valid IP format (with wildcards)
                     foreach ($addresses as $addr) {
                         $parts = explode('.', $addr);
                         if (count($parts) == 4) {
                             foreach ($parts as $part) {
                                 if ($part == '*' or is_numeric($part) and ($part = (int) $part) > -1 and $part < 256) {
                                     continue;
                                 }
                                 $this->set_error_code(5120);
                                 return false;
                             }
                         } else {
                             $this->set_error_code(5120);
                             return false;
                         }
                     }
                     // prevent user blocking itself
                     $listed = false;
                     foreach ($addresses as $addr) {
                         if (fnmatch($addr, $this->_user->ip)) {
                             $listed = true;
                             break;
                         }
                     }
                     if ($listed == false) {
                         $this->set_error_code(5119);
                         return false;
                     }
                     unset($addresses, $addr, $parts, $part);
                 }
                 $preferences->set_variable_value($variable_value);
                 $this->_post_master->set_data_object($preferences);
             } elseif ($variable_name == 'VIVVO_EMAIL_SMTP_PHP' && $variable_value == 0) {
                 $um = $sm->get_url_manager();
                 $lang = vivvo_lang::get_instance();
                 $missing = array();
                 $host = $um->get_param('VIVVO_EMAIL_SMTP_HOST');
                 $username = $um->get_param('VIVVO_EMAIL_SMTP_USERNAME');
                 $password = $um->get_param('VIVVO_EMAIL_SMTP_PASSWORD');
                 empty($host) and $missing[] = $lang->get_value('LNG_CONF_VIVVO_EMAIL_SMTP_HOST');
                 empty($username) and $missing[] = $lang->get_value('LNG_CONF_VIVVO_EMAIL_SMTP_USERNAME');
                 empty($password) and $missing[] = $lang->get_value('LNG_CONF_VIVVO_EMAIL_SMTP_PASSWORD');
                 if (!empty($missing)) {
                     $this->set_error_code(5121, implode(', ', $missing));
                     $template = $sm->get_template();
                     $template->assign('VIVVO_EMAIL_SMTP_HOST', $host);
                     $template->assign('VIVVO_EMAIL_SMTP_USERNAME', $username);
                     $template->assign('VIVVO_EMAIL_SMTP_PASSWORD', $password);
                     return false;
                 }
                 $preferences->set_variable_value($variable_value);
                 $this->_post_master->set_data_object($preferences);
             } elseif ($variable_name == 'VIVVO_GA_ENABLED' and $variable_value) {
                 $um = $sm->get_url_manager();
                 $lang = vivvo_lang::get_instance();
                 $missing = array();
                 $email = $um->get_param('VIVVO_GA_EMAIL');
                 $password = $um->get_param('VIVVO_GA_PASSWORD');
                 $code = $um->get_param('VIVVO_GA_CODE');
                 empty($email) and $missing[] = $lang->get_value('LNG_CONF_VIVVO_GA_EMAIL');
                 empty($password) and $missing[] = $lang->get_value('LNG_CONF_VIVVO_GA_PASSWORD');
                 empty($code) and $missing[] = $lang->get_value('LNG_CONF_VIVVO_GA_CODE');
                 if (!empty($missing)) {
                     $this->set_error_code(5123, implode(', ', $missing));
                     // missing google analytics parameter(s)
                     $template = $sm->get_template();
                     $template->assign('VIVVO_GA_EMAIL', $email);
                     $template->assign('VIVVO_GA_PASSWORD', $password);
                     $template->assign('VIVVO_GA_CODE', $code);
                     return false;
                 }
                 $params = array('username' => $email, 'password' => $password, 'no_auth' => true);
                 if (vivvo_ga::get_instance($params)->authorize($email, $password, true) == false) {
                     $this->set_error_code(5124);
                     // google analytics authentication failed.
                     $template = $sm->get_template();
                     $template->assign('VIVVO_GA_EMAIL', $email);
                     $template->assign('VIVVO_GA_PASSWORD', $password);
                     $template->assign('VIVVO_GA_CODE', $code);
                     return false;
                 }
                 $preferences->set_variable_value($variable_value);
                 $this->_post_master->set_data_object($preferences);
             } elseif ($preferences->check_value($variable_value)) {
                 $preferences->set_variable_value($variable_value);
                 $this->_post_master->set_data_object($preferences);
             } else {
                 $preferences->set_error_code();
                 return false;
             }
             if ($this->_post_master->sql_update()) {
                 admin_log($sm->user->get_username(), 'Edited vivvo preferences.');
                 return true;
             } else {
                 $this->set_error_code(5101);
                 return false;
             }
         } else {
             $this->set_error_code(5102, $variable_name);
             return false;
         }
     } else {
         $this->set_error_code(5103);
         return false;
     }
 }
Example #2
0
 public function generate_output($params = array())
 {
     static $data_provider = null;
     static $ga = null;
     $data_provider != null or $data_provider = new vivvo_ga_chart_provider();
     $ga != null or $ga = vivvo_ga::get_instance(array('email' => VIVVO_GA_EMAIL, 'password' => VIVVO_GA_PASSWORD, 'profileId' => VIVVO_GA_PROFILEID, 'no_auth' => true));
     $this->set_template($params);
     unset($params['template_string']);
     unset($params['template']);
     $params = $data_provider->filter_box_params($params);
     $cache_key = 'ga_box_data_' . md5(serialize($params));
     if (vivvo_cache::get_instance()->exists($cache_key)) {
         $report = vivvo_cache::get_instance()->get($cache_key);
     } else {
         $report = $ga->getReport($params);
         empty($params['data_handler']) and $params['data_handler'] = '';
         switch ($params['data_handler']) {
             case 'article_views':
                 class_exists('Articles') or (require VIVVO_FS_INSTALL_ROOT . 'lib/vivvo/core/Articles.class.php');
                 $article_ids = array();
                 foreach ($report as $row) {
                     $article_ids[$row['dimensions']['ga:eventLabel']] = 1;
                 }
                 $articles = Articles_list::factory()->get_articles_by_ids(array_keys($article_ids));
                 for ($i = 0, $count = count($report); $i < $count; $i++) {
                     if (!empty($articles[$report[$i]['dimensions']['ga:eventLabel']])) {
                         $report[$i]['article'] = $articles[$report[$i]['dimensions']['ga:eventLabel']];
                         $report[$i]['views'] = $report[$i]['metrics']['ga:totalEvents'];
                     } else {
                         unset($report[$i]);
                     }
                 }
                 $report = array_merge($report);
             default:
         }
         vivvo_cache::get_instance()->put($cache_key, $report, null, VIVVO_GA_CACHE_PERIOD);
     }
     $this->_template->assign('report', $report);
 }