/** * Should handle execution of the task, taking as much (optional) parameters as needed * * The parameters should be optional and failing to provide them should be handled by * the task * * @param string $location * @param string $sql * @param int $completed * @param int $patchId */ public function execute($location = null, $sql = null, $completed = null, $patchId = null) { $batch = $this->getBatch(); $db = $this->patcher->getPatchDatabase($location); $data['gpa_executed'] = 1; $data['gpa_changed'] = new \MUtil_Db_Expr_CurrentTimestamp(); try { $stmt = $db->query($sql); if ($rows = $stmt->rowCount()) { // No translation to avoid conflicting translations $data['gpa_result'] = 'OK: ' . $rows . ' changed'; } else { $data['gpa_result'] = 'OK'; } $data['gpa_completed'] = 1; } catch (\Zend_Db_Statement_Exception $e) { $message = $e->getMessage(); // Make sure these do not remain uncompleted if (\MUtil_String::contains($message, 'Duplicate column name')) { $data['gpa_result'] = 'Column exists in table'; $data['gpa_completed'] = 1; } elseif (\MUtil_String::contains($message, "DROP") && \MUtil_String::contains($message, 'check that column/key exists')) { $data['gpa_result'] = 'Column does not exists in table'; $data['gpa_completed'] = 1; } else { $data['gpa_result'] = substr($message, 0, 254); $data['gpa_completed'] = $completed ? $completed : 0; } $batch->addMessage($data['gpa_result']); } // $this->db, not the database the patch was executed on $this->db->update('gems__patches', $data, $this->db->quoteInto('gpa_id_patch = ?', $patchId)); // \MUtil_Echo::track($data, $patchId); $batch->addToCounter('executed'); $batch->setMessage('executed', sprintf($this->_('%d patch(es) executed.'), $batch->getCounter('executed'))); }
/** * Creates a filter for this model for the given wildcard search text. * * @param string $searchText * @return array An array of filter statements for wildcard text searching for this model type */ public function getTextSearchFilter($searchText) { $filter = array(); if ($searchText) { $fields = array(); foreach ($this->getItemNames() as $name) { // TODO: multiOptions integratie if ($this->get($name, 'label')) { $fields[] = $name; } } if ($fields) { foreach ($this->getTextSearches($searchText) as $searchOn) { $textFilter = array(); // Almost always use, this allows reuse $textFunction = function ($value) use($searchOn) { // \MUtil_Echo::track($value . ' - ' . $searchOn . ' = ' . \MUtil_String::contains($value, $searchOn)); return \MUtil_String::contains($value, $searchOn, true); }; foreach ($fields as $name) { if ($options = $this->get($name, 'multiOptions')) { $items = array(); foreach ($options as $value => $label) { if (\MUtil_String::contains($label, $searchOn)) { $items[$value] = $value; } } if ($items) { if (count($items) == count($options)) { // This filter always returns true, do not add this filter // \MUtil_Echo::track('Always true'); $textFilter = false; break; } // Function is different for each multiOptions $textFilter[$name] = function ($value) use($items) { return array_key_exists($value, $items); }; } } else { $textFilter[$name] = $textFunction; } } if ($textFilter) { $filter[] = $textFilter; } } } } return $filter; }
/** * Returns a select statement to find a corresponding user. * * @param string $login_name * @param int $organization * @return \Zend_Db_Select */ protected function getUserClassSelect($login_name, $organization) { $select = $this->db->select(); /** * tolerance field: * 1 - login and organization match * 2 - login found in an organization with access to the requested organization * 3 - login found in another organization without rights to the requested organiation * (could be allowed due to privilege with rights to ALL organizations) */ $select->from('gems__user_logins', array("gul_user_class", 'gul_id_organization', 'gul_login'))->where('gul_can_login = 1'); if ($this->allowLoginOnWithoutOrganization && !$organization) { $select->columns(new \Zend_Db_Expr('1 AS tolerance')); } else { $select->from('gems__organizations', array())->columns(new \Zend_Db_Expr("CASE\n WHEN gor_id_organization = gul_id_organization THEN 1\n WHEN gor_accessible_by LIKE CONCAT('%:', gul_id_organization, ':%') THEN 2\n ELSE 3\n END AS tolerance"))->where('gor_active = 1')->where('gor_id_organization = ?', $organization)->order('tolerance'); } $wheres[] = $this->db->quoteInto('gul_login = ?', $login_name); $isEmail = \MUtil_String::contains($login_name, '@'); if ($isEmail && $this->allowStaffEmailLogin) { $rows = $this->db->fetchAll("SELECT gsf_login, gsf_id_organization FROM gems__staff WHERE gsf_email = ?", $login_name); if ($rows) { foreach ($rows as $row) { $wheres[] = $this->db->quoteInto('gul_login = ? AND ', $row['gsf_login']) . $this->db->quoteInto('gul_id_organization = ?', $row['gsf_id_organization']); } } } if ($isEmail && $this->allowRespondentEmailLogin) { $rows = $this->db->fetchAll("SELECT gr2o_patient_nr, gr2o_id_organization FROM gems__respondent2org " . "INNER JOIN gems__respondents WHERE gr2o_id_user = grs_id_user AND grs_email = ?", $login_name); if ($rows) { foreach ($rows as $row) { $wheres[] = $this->db->quoteInto('gul_login = ? AND ', $row['gr2o_patient_nr']) . $this->db->quoteInto('gul_id_organization = ?', $row['gr2o_id_organization']); } } } // Add search fields $select->where(new \Zend_Db_Expr('(' . implode(') OR (', $wheres) . ')')); // \MUtil_Echo::track($select->__toString()); return $select; }
/** * * @param \SplFileInfo $fileinfo * @param string $content * @param array $messages */ protected function _checkTablesChanged(\SplFileInfo $fileinfo, $content, array &$messages) { $obsoleteFields = array('gtr_track_type', 'gtr_track_name' => 'calc_track_name', 'gr2t_track_info' => 'calc_track_info', 'gto_round_description' => 'calc_round_description'); foreach ($obsoleteFields as $replacement => $old) { if (\MUtil_String::contains($content, $old)) { if (is_integer($replacement)) { $messages[] = "Contains a reference to the obsolete '{$old}' field/variable."; } else { $messages[] = "Contains a reference to the '{$old}' field/variable, replace it with '{$replacement}'."; } } } $obsoleteTables = array('gems__log_actions' => array('glac_id_action', 'glac_name', 'glac_change', 'glac_log', 'glac_created'), 'gems__log_useractions' => array('glua_id_action', 'glua_to', 'glua_by', 'glua_organization', 'glua_action', 'glua_message', 'glua_role', 'glua_remote_ip', 'glua_created'), 'gems__mail_jobs' => array('gmj_id_job', 'gmj_id_message', 'gmj_id_user_as', 'gmj_active', 'gmj_from_method', 'gmj_from_fixed', 'gmj_process_method', 'gmj_filter_mode', 'gmj_filter_days_between', 'gmj_filter_max_reminders', 'gmj_id_organization', 'gmj_id_track', 'gmj_id_survey', 'gmj_changed', 'gmj_changed_by', 'gmj_created', 'gmj_created_by'), 'gems__mail_templates' => array('gmt_id_message', 'gmt_subject', 'gmt_body', 'gmt_organizations', 'gmt_changed', 'gmt_changed_by', 'gmt_created', 'gmt_created_by')); foreach ($obsoleteTables as $table => $fields) { if (\MUtil_String::contains($content, $table)) { $messages[] = "Contains a reference to the obsolete '{$table}' database table."; } foreach ($fields as $field) { if (\MUtil_String::contains($content, $field)) { $messages[] = "Contains a reference to the obsolete '{$field}' field in the '{$table}' database table."; } } } }
/** * Processes a request and sets its controller and action. If * no route was possible, an exception is thrown. * * @param \Zend_Controller_Request_Abstract * @throws \Zend_Controller_Router_Exception * @return \Zend_Controller_Request_Abstract|boolean */ public function route(\Zend_Controller_Request_Abstract $request) { $options = array('help|h' => 'Show this help', 'org|o=i' => 'The user organization number', 'pwd|p=s' => 'User password', 'user|u=s' => 'The user name'); $getopt = new \Zend_Console_Getopt($options); try { $getopt->parse(); } catch (\Zend_Console_Getopt_Exception $e) { echo $this->_expandMessage($e); exit; } if ($getopt->getOption('h')) { // $getopt->s echo $this->_expandMessage($getopt); exit; } if ($request instanceof \MUtil_Controller_Request_Cli) { $request->setUserLogin($getopt->getOption('u'), $getopt->getOption('o'), $getopt->getOption('p')); } $arguments = $getopt->getRemainingArgs(); if ($arguments) { $controller = array_shift($arguments); $action = array_shift($arguments); if (!$action) { $action = 'index'; } if (preg_match('/^\\w+(-\\w+)*$/', $controller) && preg_match('/^\\w+(-\\w+)*$/', $action)) { $request->setControllerName($controller); $request->setActionName($action); $params[$request->getControllerKey()] = $controller; $params[$request->getActionKey()] = $action; foreach ($arguments as $arg) { if (\MUtil_String::contains($arg, '=')) { list($name, $value) = explode('=', $arg, 2); } else { $name = $arg; $value = ''; } $params[$name] = $value; } $request->setParams($params); return $request; } echo "Invalid command: {$controller}/{$action}.\n", exit; } echo "No command given.\n\n"; echo $this->_expandMessage($getopt), exit; }
/** * Remove password and pwd contents and clean up message status data and single item arrays * * @param array $data * @return mixed */ private function _toCleanArray(array $data) { switch (count($data)) { case 0: return null; case 1: if (isset($data[0])) { // Return array content when only one item // with the key 0. if (is_array($data[0])) { return $this->_toCleanArray($data[0]); } else { return $data[0]; } } break; case 2: if (isset($data[0], $data[1]) && is_string($data[1])) { if ('info' === $data[1] || 'warning' === $data[1] || 'error' === $data[1]) { if (is_array($data[0])) { return $this->_toCleanArray($data[0]); } else { return $data[0]; } } } } $output = array(); foreach ($data as $key => $value) { if (is_array($value)) { $output[$key] = $this->_toCleanArray($value); } else { if (is_string($value)) { if (\MUtil_String::contains($key, 'password', true) || \MUtil_String::contains($key, 'pwd', true)) { $value = '****'; } } $output[$key] = $value; } } return $output; }
/** * Output for browsing rols * * @param array $privileges * @return array */ public function formatLongLine(array $privileges) { $output = \MUtil_Html::create('div'); if (count($privileges)) { $privileges = array_combine($privileges, $privileges); foreach ($this->getUsedPrivileges() as $privilege => $description) { if (isset($privileges[$privilege])) { if (count($output) > 11) { $output->append('...'); return $output; } if (\MUtil_String::contains($description, '<br/>')) { $description = substr($description, 0, strpos($description, '<br/>') - 1); } $output->raw($description); $output->br(); } } } return $output; }
/** * Hook 6: Called after \Zend_Controller_Router has determined the route set by the request. * * This events enables you to adjust the route after the routing has run it's course. * * Not initialized is the $controller object. * * Previous hook: routeStartup() * Actions since: $router->route() * Actions after: nothing, but the route consisting of controller, action and module should now be fixed * Next hook: dispatchLoopStartup() * * Also sets $this->currentOrganization and $this->menu to access afterwards * * @param \Zend_Controller_Request_Abstract $request * @return void */ public function routeShutdown(\Zend_Controller_Request_Abstract $request) { $loader = $this->getLoader(); // Load the menu. As building the menu can depend on all resources and the request, we do it here. // // PS: The REQUEST is needed because otherwise the locale for translate is not certain. $menu = $loader->createMenu($this); $source = $menu->getParameterSource(); $user = $this->_container->currentUser; $user->setRequest($request); $organization = $user->getCurrentOrganization(); $organization->applyToMenuSource($source); $this->_container->currentOrganization = $organization; $this->_container->menu = $menu; $this->_updateVariable(array('currentOrganization', 'menu')); // Now is a good time to check for required values // Moved down here to prevent unit test from failing on missing salt $this->project->checkRequiredValues(); /** * Check if we are in maintenance mode or not. This is triggeren by a file in the var/settings * directory with the name lock.txt */ if ($this->getUtil()->getMaintenanceLock()->isLocked()) { if ($user->isActive() && !$user->hasPrivilege('pr.maintenance.maintenance-mode')) { //Still allow logoff so we can relogin as master if (!('index' == $request->getControllerName() && 'logoff' == $request->getActionName())) { $this->setError($this->_('Please check back later.'), 401, $this->_('System is in maintenance mode')); } $user->unsetAsCurrentUser(); } else { $this->addMessage($this->_('System is in maintenance mode')); \MUtil_Echo::r($this->_('System is in maintenance mode')); } } // Gems does not use index/index $action = $request->getActionName(); if ('index' == $request->getControllerName() && ('index' == $action || $user->isActive() && 'login' == $action)) { // Instead Gems routes to the first available menu item when this is the request target if (!$user->gotoStartPage($menu, $request)) { $this->setError($this->_('No access to site.'), 401, $this->_('You have no access to this site.'), true); return; } } else { //find first allowed item in the menu $menuItem = $menu->find(array('action' => $request->getActionName(), 'controller' => $request->getControllerName())); // Display error when not having the right priviliges if (!($menuItem && $menuItem->get('allowed'))) { // When logged in if ($user->getUserId()) { $this->setError($this->_('No access to page'), 403, sprintf($this->_('Access to the %s/%s page is not allowed for current role: %s.'), $request->getControllerName(), $request->getActionName(), $user->getRole()), true); } else { // No longer logged in if (\MUtil_Console::isConsole()) { $this->setError('No access to page.', 401, sprintf('Controller "%s" action "%s" is not accessible.', $request->getControllerName(), $request->getActionName()), true); return; } if ($request->getActionName() == 'autofilter') { // Throw an exception + HTTP 401 when an autofilter is called throw new \Gems_Exception("Session expired", 401); } $menuItem = $menu->findFirst(array('allowed' => true, 'visible' => true)); if ($menuItem) { // Do not store previous request & show message when the intended action is logoff if (!($request->getControllerName() == 'index' && $request->getActionName() == 'logoff')) { $this->addMessage($this->_('You are no longer logged in.')); $this->addMessage($this->_('You must login to access this page.')); if (!\MUtil_String::contains($request->getControllerName() . $request->getActionName(), '.')) { // save original request, we will redirect back once the user succesfully logs in $staticSession = $this->getStaticSession(); $staticSession->previousRequestParameters = $request->getParams(); $staticSession->previousRequestMode = $request->isPost() ? "POST" : "GET"; } } $redirector = \Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); $redirector->gotoRoute($menuItem->toRouteUrl($request)); } else { $this->setError($this->_('You are no longer logged in.'), 401, $this->_('You have no access to this site.'), true); return; } } } } if (isset($menuItem)) { $menuItem->applyHiddenParameters($request, $source); $menu->setCurrent($menuItem); } }
/** * Check a filter for a match * * @param \Gems\Agenda\Gems_Agenda_Appointment $appointment * @return boolean */ public function matchAppointment(\Gems_Agenda_Appointment $appointment) { return \MUtil_String::contains($appointment->getSubject(), $this->_data['gaf_filter_text1']); }
/** * Return false when the needle is not contained in the haystack */ public function testContainsNot() { $result = MUtil_String::contains('abcdef', 'xyz'); $this->assertEquals($result, false); }