/** * Ends the benchmark and log the result. */ public function end() { if ($this->_logger) { // Compute elapsed time and build message. $elapsed = microtime(true) - $this->_start; $message = sprintf('%s (%.5f)', $this->_message, $elapsed); // Log message (level may be specified as integer or string). if (is_integer($this->_level)) { $this->_logger->log($message, $this->_level); } else { $this->_logger->{$this->_level}($message); } } }
/** * Notifies the user about any active alarms. * * @param string $user Notify this user, all users if null, or guest * users if empty. * @param boolean $load Update active alarms from all applications? * @param boolean $preload Preload alarms that go off within the next * ttl time span? * @param array $exclude Don't notify with these methods. * * @throws Horde_Alarm_Exception if loading of alarms fails, but not if * notifying of individual alarms fails. */ public function notify($user = null, $load = true, $preload = true, array $exclude = array()) { try { $alarms = $this->listAlarms($user, null, $load, $preload); } catch (Horde_Alarm_Exception $e) { if ($this->_logger) { $this->_logger->log($e, 'ERR'); } throw $e; } if (empty($alarms)) { return; } $handlers = $this->handlers(); foreach ($alarms as $alarm) { foreach ($alarm['methods'] as $alarm_method) { if (isset($handlers[$alarm_method]) && !in_array($alarm_method, $exclude)) { try { $handlers[$alarm_method]->notify($alarm); } catch (Horde_Alarm_Exception $e) { $this->_errors[] = $e; } } } } }
/** * Destroy the data for a particular session identifier in the backend. * This method should only be called internally by PHP via * session_set_save_handler(). * * @param string $id The session identifier. * * @return boolean True on success, false otherwise. */ public function destroy($id) { if ($this->_storage->destroy($id)) { $this->_logger->log('Session data destroyed (' . $id . ')', 'DEBUG'); return true; } $this->_logger->log('Failed to destroy session data (' . $id . ')', 'DEBUG'); return false; }
/** * Logs an entry. * * @since 2.5.0 * * @param Horde_Core_Log_Object $ob Log entry object. */ public function logObject(Horde_Core_Log_Object $ob) { if (!$ob->logged) { parent::log($ob->toArray()); if ($bt = $ob->backtrace) { parent::log(strval($bt), Horde_Log::DEBUG); } $ob->logged = true; } }
/** * Runs the compression command and returns the output. * * @param string $text The javascript text. * @param string $cmd Command. * @param Horde_Log_Logger $log Logging object. * * @return string The compressed javascript. */ public function runCmd($text, $cmd, Horde_Log_Logger $log) { $descspec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')); $process = proc_open($cmd, $descspec, $pipes); fwrite($pipes[0], $text); fclose($pipes[0]); $out = ''; while (!feof($pipes[1])) { $out .= fread($pipes[1], 8192); } $error = ''; while (!feof($pipes[2])) { $error .= fread($pipes[2], 8192); } if (strlen($error)) { $log->log(sprintf('Output from %s: %s', $cmd, $error), 'WARN'); } fclose($pipes[1]); fclose($pipes[2]); proc_close($process); return $out; }
/** * Do initialization. * * @throws Horde_Memcache_Exception */ public function _init() { $this->_memcache = new Memcache(); for ($i = 0, $n = count($this->_params['hostspec']); $i < $n; ++$i) { $res = $this->_memcache->addServer($this->_params['hostspec'][$i], empty($this->_params['port'][$i]) ? 0 : $this->_params['port'][$i], !empty($this->_params['persistent']), !empty($this->_params['weight'][$i]) ? $this->_params['weight'][$i] : 1, 1, 15, true, array($this, 'failover')); if ($res) { $this->_servers[] = $this->_params['hostspec'][$i] . (!empty($this->_params['port'][$i]) ? ':' . $this->_params['port'][$i] : ''); } } /* Check if any of the connections worked. */ if (empty($this->_servers)) { throw new Horde_Memcache_Exception('Could not connect to any defined memcache servers.'); } if (!empty($this->_params['c_threshold'])) { $this->_memcache->setCompressThreshold($this->_params['c_threshold']); } // Force consistent hashing ini_set('memcache.hash_strategy', 'consistent'); if (isset($this->_params['logger'])) { $this->_logger = $this->_params['logger']; $this->_logger->log('Connected to the following memcache servers:' . implode($this->_servers, ', '), 'DEBUG'); } }
/** * Fetches a template from the specified file and return the parsed * contents. * * @param string $filename The file to fetch the template from. * * @return string The parsed template. */ public function fetch($filename = null) { $file = $this->_basepath . $filename; $force = $this->getOption('forcecompile'); if (!is_null($filename) && $file != $this->_templateFile) { $this->_template = $this->_templateFile = null; } /* First, check for a cached compiled version. */ $parts = array('horde_template', filemtime($file), $file); if ($this->getOption('gettext')) { $parts[] = setlocale(LC_ALL, 0); } $cacheid = implode('|', $parts); if (!$force && is_null($this->_template) && $this->_cache) { $this->_template = $this->_cache->get($cacheid, 0); if ($this->_template === false) { $this->_template = null; } } /* Parse and compile the template. */ if ($force || is_null($this->_template)) { $this->_template = str_replace("\n", " \n", file_get_contents($file)); $this->_parse(); if ($this->_cache) { $this->_cache->set($cacheid, $this->_template); if ($this->_logger) { $this->_logger->log(sprintf('Saved compiled template file for "%s".', $file), 'DEBUG'); } } } $this->_templateFile = $file; /* Template debugging. */ if ($this->getOption('debug')) { echo '<pre>' . htmlspecialchars($this->_template) . '</pre>'; } return $this->parse(); }
/** * Do initialization. * * @throws Horde_Memcache_Exception */ public function _init() { if (class_exists('Memcached')) { if (empty($this->_params['persistent'])) { $this->_memcache = new Memcached(); } else { $this->_memcache = new Memcached('horde_memcache'); } $this->_params['large_items'] = false; $this->_memcache->setOptions(array(Memcached::OPT_COMPRESSION => $this->_params['compression'], Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT, Memcached::OPT_HASH => Memcached::HASH_MD5, Memcached::OPT_LIBKETAMA_COMPATIBLE => true, Memcached::OPT_PREFIX_KEY => $this->_params['prefix'])); } else { // Force consistent hashing ini_set('memcache.hash_strategy', 'consistent'); $this->_memcache = new Memcache(); } for ($i = 0, $n = count($this->_params['hostspec']); $i < $n; ++$i) { if ($this->_memcache instanceof Memcached) { $res = $this->_memcache->addServer($this->_params['hostspec'][$i], empty($this->_params['port'][$i]) ? 0 : $this->_params['port'][$i], !empty($this->_params['weight'][$i]) ? $this->_params['weight'][$i] : 0); } else { $res = $this->_memcache->addServer($this->_params['hostspec'][$i], empty($this->_params['port'][$i]) ? 0 : $this->_params['port'][$i], !empty($this->_params['persistent']), !empty($this->_params['weight'][$i]) ? $this->_params['weight'][$i] : 1, 1, 15, true, array($this, 'failover')); } if ($res) { $this->_servers[] = $this->_params['hostspec'][$i] . (!empty($this->_params['port'][$i]) ? ':' . $this->_params['port'][$i] : ''); } } /* Check if any of the connections worked. */ if (empty($this->_servers)) { throw new Horde_Memcache_Exception('Could not connect to any defined memcache servers.'); } if ($this->_memcache instanceof Memcache && !empty($this->_params['c_threshold'])) { $this->_memcache->setCompressThreshold($this->_params['c_threshold']); } if (isset($this->_params['logger'])) { $this->_logger = $this->_params['logger']; $this->_logger->log('Connected to the following memcache servers:' . implode($this->_servers, ', '), 'DEBUG'); } }
public function testLogThrowsOnBadLogLevel() { $logger = new Horde_Log_Logger($this->handler); try { $logger->log('foo', 42); $this->fail(); } catch (Exception $e) { $this->assertInstanceOf('Horde_Log_Exception', $e); $this->assertRegExp('/bad log level/i', $e->getMessage()); } }
/** * Debug method. Allows quick shortcut to produce debug output into a * temporary file. * * @param mixed $event Item to log. * @param string $fname Filename to log to. If empty, logs to * 'horde_debug.txt' in the PHP temporary * directory. * @param boolean $backtrace Include backtrace information? */ public static function debug($event = null, $fname = null, $backtrace = true) { if (is_null($fname)) { $fname = self::getTempDir() . '/horde_debug.txt'; } try { $logger = new Horde_Log_Logger(new Horde_Log_Handler_Stream($fname)); } catch (Exception $e) { return; } $html_ini = ini_set('html_errors', 'Off'); self::startBuffer(); if (!is_null($event)) { echo "Variable information:\n"; var_dump($event); echo "\n"; } if (is_resource($event)) { echo "Stream contents:\n"; rewind($event); fpassthru($event); echo "\n"; } if ($backtrace) { echo "Backtrace:\n"; echo strval(new Horde_Support_Backtrace()); } $logger->log(self::endBuffer(), Horde_Log::DEBUG); ini_set('html_errors', $html_ini); }
/** * Finds out what rights the given user has to this object. * * @param mixed $permission The full permission name of the object to * check the permissions of, or the * Horde_Permissions object. * @param string $user The user to check for. * @param string $creator The user who created the event. * * @return mixed A bitmask of permissions the user has, false if there * are none. */ public function getPermissions($permission, $user, $creator = null) { if (is_string($permission)) { try { $permission = $this->getPermission($permission); } catch (Horde_Perms_Exception $e) { /* Ignore not exists errors. */ if ($this->_logger && $e->getCode() != Horde_Perms_Exception::NOT_EXIST) { $this->_logger->log($e, 'DEBUG'); } return false; } } // If this is a guest user, only check guest permissions. if (empty($user)) { return $permission->getGuestPermissions(); } // Combine all other applicable permissions. $type = $permission->get('type'); $composite_perm = $type == 'matrix' ? 0 : array(); // If $creator was specified, check creator permissions. // If the user is the creator of the event see if there are creator // permissions. if (!is_null($creator) && strlen($user) && $user === $creator && ($perms = $permission->getCreatorPermissions()) !== null) { if ($type == 'matrix') { $composite_perm |= $perms; } else { $composite_perm[] = $perms; } } // Check user-level permissions. $userperms = $permission->getUserPermissions(); if (isset($userperms[$user])) { if ($type == 'matrix') { $composite_perm |= $userperms[$user]; } else { $composite_perm[] = $userperms[$user]; } } // If no user permissions are found, try group permissions. if (isset($permission->data['groups']) && is_array($permission->data['groups']) && count($permission->data['groups'])) { $groups = $GLOBALS['injector']->getInstance('Horde_Group')->listGroups($user); foreach ($permission->data['groups'] as $group => $perms) { if (isset($groups[$group])) { if ($type == 'matrix') { $composite_perm |= $perms; } else { $composite_perm[] = $perms; } } } } // If there are default permissions, return them. if (($perms = $permission->getDefaultPermissions()) !== null) { if ($type == 'matrix') { $composite_perm |= $perms; } else { $composite_perm[] = $perms; } } // Return composed permissions. if ($composite_perm) { return $composite_perm; } // Otherwise, deny all permissions to the object. return false; }