/** * Init company based on subdomain * * @access public * @param string * @return null * @throws Error */ private function initCompany() { $company = Contacts::getOwnerCompany(); if (!$company instanceof Contact) { throw new OwnerCompanyDnxError(); } $owner = null; if (GlobalCache::isAvailable()) { $owner = GlobalCache::get('owner_company_creator', $success); } if (!$owner instanceof Contact) { $owner = $company->getCreatedBy(); // Update cache if available if ($owner instanceof Contact && GlobalCache::isAvailable()) { GlobalCache::update('owner_company_creator', $owner); } } if (!$owner instanceof Contact) { throw new AdministratorDnxError(); } $this->setCompany($company); }
/** * Return config option by name * * @access public * @param string $name * @return ContactConfigOption */ static function getByName($name) { if (GlobalCache::isAvailable()) { $object = GlobalCache::get('user_copt_obj_' . $name, $success); if ($success) { return $object; } } $object = self::findOne(array('conditions' => array('`name` = ?', $name))); if (GlobalCache::isAvailable()) { GlobalCache::update('user_copt_obj_' . $name, $object); } return $object; }
function popup_reminders() { ajx_current("empty"); // if no new popup reminders don't make useless queries if (GlobalCache::isAvailable()) { $check = GlobalCache::get('check_for_popup_reminders_' . logged_user()->getId(), $success); if ($success && $check == 0) { return; } } $reminders = ObjectReminders::getDueReminders("reminder_popup"); $popups = array(); foreach ($reminders as $reminder) { $object = $reminder->getObject(); $context = $reminder->getContext(); $type = $object->getObjectTypeName(); $date = $object->getColumnValue($reminder->getContext()); if (!$date instanceof DateTimeValue) { continue; } if ($object->isTrashed()) { $reminder->delete(); continue; } // convert time to the user's locale $timezone = logged_user()->getTimezone(); if ($date->getTimestamp() + 5 * 60 < DateTimeValueLib::now()->getTimestamp()) { // don't show popups older than 5 minutes $reminder->delete(); continue; } if ($reminder->getUserId() == 0) { if (!$object->isSubscriber(logged_user())) { // reminder for subscribers and user is not subscriber continue; } } else { if ($reminder->getUserId() != logged_user()->getId()) { continue; } } if ($context == "due_date" && $object instanceof ProjectTask) { if ($object->isCompleted()) { // don't show popups for completed tasks $reminder->delete(); continue; } } $url = $object->getViewUrl(); $link = '<a href="#" onclick="og.openLink(\'' . $url . '\');return false;">' . clean($object->getObjectName()) . '</a>'; evt_add("popup", array('title' => lang("{$context} {$type} reminder"), 'message' => lang("{$context} {$type} reminder desc", $link, format_datetime($date)), 'type' => 'reminder', 'sound' => 'info')); if ($reminder->getUserId() == 0) { // reminder is for all subscribers, so change it for one reminder per user (except logged_user) // otherwise if deleted it won't notify other subscribers and if not deleted it will keep notifying // logged user $subscribers = $object->getSubscribers(); foreach ($subscribers as $subscriber) { if ($subscriber->getId() != logged_user()->getId()) { $new = new ObjectReminder(); $new->setContext($reminder->getContext()); $new->setDate($reminder->getDate()); $new->setMinutesBefore($reminder->getMinutesBefore()); $new->setObject($object); $new->setUser($subscriber); $new->setType($reminder->getType()); $new->save(); } } } $reminder->delete(); } // popup reminders already checked for logged user if (GlobalCache::isAvailable()) { $today_next_reminders = ObjectReminders::findAll(array('conditions' => array("`date` > ? AND `date` < ?", DateTimeValueLib::now(), DateTimeValueLib::now()->endOfDay()), 'limit' => config_option('cron reminder limit', 100))); if (count($today_next_reminders) == 0) { GlobalCache::update('check_for_popup_reminders_' . logged_user()->getId(), 0, 60 * 30); } } }
/** * Set value * */ function setUserValue($new_value, $user_id = 0, $workspace_id = 0) { $val = null; if (GlobalCache::isAvailable()) { $val = GlobalCache::get('user_copt_val_' . $user_id . "_" . $this->getId(), $success); } if (!$val) { $val = UserWsConfigOptionValues::findById(array('option_id' => $this->getId(), 'user_id' => $user_id, 'workspace_id' => $workspace_id)); } if (!$val) { // if value was not found, create it $val = new UserWsConfigOptionValue(); $val->setOptionId($this->getId()); $val->setUserId($user_id); $val->setWorkspaceId($workspace_id); } $val->setValue($new_value); $val->save(); $this->updateUserValueCache($user_id, $workspace_id, $val->getValue()); if (GlobalCache::isAvailable()) { GlobalCache::update('user_copt_val_' . $user_id . "_" . $this->getId(), $val); } }
/** * Return owner company * * @access public * @param void * @return Company */ static function getOwnerCompany() { $owner_company = null; if (GlobalCache::isAvailable()) { $owner_company = GlobalCache::get('owner_company', $success); if ($success && $owner_company instanceof Contact) { return $owner_company; } } $owner_company = Contacts::findOne(array("conditions" => " is_company > 0", "limit" => 1, "order" => "object_id ASC")); if (GlobalCache::isAvailable()) { GlobalCache::update('owner_company', $owner_company); } return $owner_company; }
/** * Return user config option value * * @access public * @param string $name Option name * @param mixed $default Default value that is returned in case of any error * @param int $user_id User Id, if null logged user is taken * @return mixed */ function user_config_option($option, $default = null, $user_id = null, $options_members = false) { if (is_null($user_id)) { if (logged_user() instanceof Contact) { $user_id = logged_user()->getId(); } else if (is_null($default)) { $def_value = null; // check the cache for the option default value if (GlobalCache::isAvailable()) { $def_value = GlobalCache::get('user_config_option_def_'.$option, $success); if ($success) return $def_value; } // default value not found in cache $def_value = ContactConfigOptions::getDefaultOptionValue($option, $default); if (GlobalCache::isAvailable()) { GlobalCache::update('user_config_option_def_'.$option, $def_value); } return $def_value; } else { return $default; } } // check the cache for the option value if (GlobalCache::isAvailable()) { $option_value = GlobalCache::get('user_config_option_'.$user_id.'_'.$option, $success); if ($success) return $option_value; } if($options_members){ $members = implode ( ',',active_context_members(false)); // default value not found in cache $option_value = ContactConfigOptions::getOptionValue($option, $user_id, $default, $members); }else{ $option_value = ContactConfigOptions::getOptionValue($option, $user_id, $default); } if (GlobalCache::isAvailable()) { GlobalCache::update('user_config_option_'.$user_id.'_'.$option, $option_value); } return $option_value; } // user_config_option
/** * Contruct controller and execute specific action * * @access public * @param string $controller_name * @param string $action * @return null */ static function executeAction($controller_name, $action) { $max_users = config_option('max_users'); if ($max_users && Users::count() > $max_users) { echo lang("error") . ": " . lang("maximum number of users exceeded error"); return; } ajx_check_login(); if (isset($_GET['active_project']) && logged_user() instanceof User) { $dont_update = false; if (GlobalCache::isAvailable()) { $option_value = GlobalCache::get('user_config_option_' . logged_user()->getId() . '_lastAccessedWorkspace', $success); if ($success) { $dont_update = $option_value == $_GET['active_project']; } } if (!$dont_update) { set_user_config_option('lastAccessedWorkspace', $_GET['active_project'], logged_user()->getId()); if (GlobalCache::isAvailable()) { GlobalCache::update('user_config_option_' . logged_user()->getId() . '_lastAccessedWorkspace', $_GET['active_project']); } } } Env::useController($controller_name); $controller_class = Env::getControllerClass($controller_name); if (!class_exists($controller_class, false)) { throw new ControllerDnxError($controller_name); } // if $controller = new $controller_class(); if (!instance_of($controller, 'Controller')) { throw new ControllerDnxError($controller_name); } // if if (is_ajax_request()) { // if request is an ajax request return a json response // execute the action $controller->setAutoRender(false); $controller->execute($action); // fill the response $response = AjaxResponse::instance(); if (!$response->hasCurrent()) { // set the current content $response->setCurrentContent("html", $controller->getContent(), page_actions(), ajx_get_panel()); } $response->setEvents(evt_pop()); $error = flash_pop('error'); $success = flash_pop('success'); if (!is_null($error)) { $response->setError(1, clean($error)); } else { if (!is_null($success)) { $response->setError(0, clean($success)); } } // display the object as json tpl_assign("object", $response); $content = tpl_fetch(Env::getTemplatePath("json")); tpl_assign("content_for_layout", $content); TimeIt::start("Transfer"); if (is_iframe_request()) { tpl_display(Env::getLayoutPath("iframe")); } else { tpl_display(Env::getLayoutPath("json")); } TimeIt::stop(); } else { return $controller->execute($action); } }
function popup_reminders() { ajx_current("empty"); // extra data to send to interface $extra_data = array(); // if no new popup reminders don't make useless queries if (GlobalCache::isAvailable()) { $check = GlobalCache::get('check_for_popup_reminders_' . logged_user()->getId(), $success); if ($success && $check == 0) { return; } } $reminders = ObjectReminders::getDueReminders("reminder_popup"); $popups = array(); foreach ($reminders as $reminder) { $context = $reminder->getContext(); if (str_starts_with($context, "mails_in_outbox")) { if ($reminder->getUserId() > 0 && $reminder->getUserId() != logged_user()->getId()) { continue; } preg_match('!\\d+!', $context, $matches); evt_add("popup", array('title' => lang("mails_in_outbox reminder"), 'message' => lang("mails_in_outbox reminder desc", $matches[0]), 'type' => 'reminder', 'sound' => 'info')); $reminder->delete(); continue; } if (str_starts_with($context, "eauthfail")) { if ($reminder->getUserId() == logged_user()->getId()) { $acc = trim(substr($context, strrpos($context, " "))); evt_add("popup", array('title' => lang("failed to authenticate email account"), 'message' => lang("failed to authenticate email account desc", $acc), 'type' => 'reminder', 'sound' => 'info')); $reminder->delete(); } continue; } $object = $reminder->getObject(); $type = $object->getObjectTypeName(); $date = $object->getColumnValue($reminder->getContext()); if (!$date instanceof DateTimeValue) { continue; } if ($object->isTrashed()) { $reminder->delete(); continue; } // convert time to the user's locale $timezone = logged_user()->getTimezone(); if ($date->getTimestamp() + 5 * 60 < DateTimeValueLib::now()->getTimestamp()) { // don't show popups older than 5 minutes //$reminder->delete(); //continue; } if ($reminder->getUserId() == 0) { if (!$object->isSubscriber(logged_user())) { // reminder for subscribers and user is not subscriber continue; } } else { if ($reminder->getUserId() != logged_user()->getId()) { continue; } } if ($context == "due_date" && $object instanceof ProjectTask) { if ($object->isCompleted()) { // don't show popups for completed tasks $reminder->delete(); continue; } } $url = $object->getViewUrl(); $link = '<a href="#" onclick="og.openLink(\'' . $url . '\');return false;">' . clean($object->getObjectName()) . '</a>'; evt_add("popup", array('title' => lang("{$context} {$type} reminder", clean($object->getObjectName())), 'message' => lang("{$context} {$type} reminder desc", $link, format_datetime($date)), 'type' => 'reminder', 'sound' => 'info')); if ($reminder->getUserId() == 0) { // reminder is for all subscribers, so change it for one reminder per user (except logged_user) // otherwise if deleted it won't notify other subscribers and if not deleted it will keep notifying // logged user $subscribers = $object->getSubscribers(); foreach ($subscribers as $subscriber) { if ($subscriber->getId() != logged_user()->getId()) { $new = new ObjectReminder(); $new->setContext($reminder->getContext()); $new->setDate($reminder->getDate()); $new->setMinutesBefore($reminder->getMinutesBefore()); $new->setObject($object); $new->setUser($subscriber); $new->setType($reminder->getType()); $new->save(); } } } $reminder->delete(); } // popup reminders already checked for logged user if (GlobalCache::isAvailable()) { $today_next_reminders = ObjectReminders::findAll(array('conditions' => array("`date` > ? AND `date` < ?", DateTimeValueLib::now(), DateTimeValueLib::now()->endOfDay()), 'limit' => config_option('cron reminder limit', 100))); if (count($today_next_reminders) == 0) { GlobalCache::update('check_for_popup_reminders_' . logged_user()->getId(), 0, 60 * 30); } } // check for member modifications if (isset($_POST['dims_check_date'])) { $dims_check_date = new DateTimeValue($_POST['dims_check_date']); $dims_check_date_sql = $dims_check_date->toMySQL(); $members_log_count = ApplicationLogs::instance()->count("member_id>0 AND created_on>'{$dims_check_date_sql}'"); if ($members_log_count > 0) { $extra_data['reload_dims'] = 1; } } ajx_extra_data($extra_data); }
/** * Return user config option value * * @access public * @param string $name Option name * @param mixed $default Default value that is returned in case of any error * @param int $user_id User Id, if null logged user is taken * @return mixed */ function user_config_option($option, $default = null, $user_id = null) { if (is_null($user_id)) { if (logged_user() instanceof User) { $user_id = logged_user()->getId(); } else { if (is_null($default)) { $def_value = null; // check the cache for the option default value if (GlobalCache::isAvailable()) { $def_value = GlobalCache::get('user_config_option_def_' . $option, $success); if ($success) { return $def_value; } } // default value not found in cache $def_value = UserWsConfigOptions::getDefaultOptionValue($option, $default); if (GlobalCache::isAvailable()) { GlobalCache::update('user_config_option_def_' . $option, $def_value); } return $def_value; } else { return $default; } } } // check the cache for the option value if (GlobalCache::isAvailable()) { $option_value = GlobalCache::get('user_config_option_' . $user_id . '_' . $option, $success); if ($success) { return $option_value; } } // default value not found in cache $option_value = UserWsConfigOptions::getOptionValue($option, $user_id, $default); if (GlobalCache::isAvailable()) { GlobalCache::update('user_config_option_' . $user_id . '_' . $option, $option_value); } return $option_value; }
/** * This function will use session ID from session or cookie and if presend log user * with that ID. If not it will simply break. * * When this function uses session ID from cookie the whole process will be treated * as new login and users last login time will be set to current time. * * @access public * @param void * @return boolean */ private function initLoggedUser() { $user_id = Cookie::getValue('id'); $twisted_token = Cookie::getValue('token'); $cn = Cookie::getValue('cn'); $remember = (bool) Cookie::getValue('remember', false); if (empty($user_id) || empty($twisted_token)) { return false; // we don't have a user } // if // check the cache if available $user = null; if (GlobalCache::isAvailable()) { $user = GlobalCache::get('logged_user_' . $user_id, $success); } if (!$user instanceof User) { $user = Users::findById($user_id); // Update cache if available if ($user instanceof User && GlobalCache::isAvailable()) { GlobalCache::update('logged_user_' . $user->getId(), $user); } } if (!$user instanceof User) { return false; // failed to find user } // if if (!$user->isValidToken($twisted_token)) { return false; // failed to validate token } // if if (!($cn == md5(array_var($_SERVER, 'REMOTE_ADDR', "")))) { return false; // failed to check ip address } // if $last_act = $user->getLastActivity(); if ($last_act) { $session_expires = $last_act->advance(SESSION_LIFETIME, false); } if (!$last_act || $session_expires != null && DateTimeValueLib::now()->getTimestamp() < $session_expires->getTimestamp()) { $this->setLoggedUser($user, $remember, true); } else { $this->logUserIn($user, $remember); } // if //$this->selected_project = $user->getPersonalProject(); }
/** * Return owner company * * @access public * @param void * @return Company */ static function getOwnerCompany() { $owner_company = null; if (GlobalCache::isAvailable()) { $owner_company = GlobalCache::get('owner_company', $success); if ($success && $owner_company instanceof Company) { return $owner_company; } } $owner_company = Companies::findOne(array('conditions' => array('`client_of_id` = ?', 0), 'include_trashed' => true)); // findOne if (GlobalCache::isAvailable()) { GlobalCache::update('owner_company', $owner_company); } return $owner_company; }