public function setUp() { if (is_null(static::$LOGFILE_PATH)) { static::$LOGFILE_PATH = '/tmp/no2-' . uuidv4() . '.log'; } No2_Logger::setup(array('name' => 'PHPUnit', 'level' => No2_Logger::DEBUG, 'logfile_path' => static::$LOGFILE_PATH)); }
/** * override save() in order to set some "magic" fields. */ public function save($do_validate = true) { $now = new DateTime(); $new = $this->is_new_record(); $db_infos = $this->db_infos(); $id_is_uuidv4 = array_key_exists('type', $db_infos['id']) && $db_infos['id']['type'] === 'uuidv4'; $has_created_at = array_key_exists('created_at', $db_infos); $has_updated_at = array_key_exists('updated_at', $db_infos); $has_created_by = array_key_exists('created_by', $db_infos); $has_updated_by = array_key_exists('updated_by', $db_infos); if ($new && $id_is_uuidv4) { $this->id = uuidv4(); } if ($new && $has_created_at) { $saved_created_at = $this->created_at; $this->created_at = $now; } if ($new && $has_created_by) { $saved_created_by = $this->created_by; $this->created_by = current_user()->id; } if ($has_updated_at) { $saved_updated_at = $this->updated_at; $this->updated_at = $now; } if ($has_updated_by) { $saved_updated_by = $this->updated_by; $this->updated_by = current_user()->id; } $success = parent::save($do_validate); // if save() has failed, restore the previous values. if (!$success) { if ($new && $id_is_uuidv4) { unset($this->id); } if ($new && $has_created_at) { $this->created_at = $saved_created_at; } if ($new && $has_created_by) { $this->created_by = $saved_created_by; } if ($has_updated_at) { $this->updated_at = $saved_updated_at; } if ($has_updated_by) { $this->updated_by = $saved_updated_by; } } return $success; }
function db_addAtomElement($db, $contents) { // $retv[0] = $stmt->errno (0 if success) // $retv[1] = $eid (assigned) or $stmt->error if some error occurred. $retv = array(); $stmt = $db->prepare(QUERY_ADD_AtomElement); $eid = uuidv4(); $stmt->bind_param(QUERY_ADD_AtomElement_TYPES, $eid, $contents); $stmt->execute(); // $stmt->store_result(); $retv[0] = $stmt->errno; if ($stmt->errno == 0) { $retv[1] = $eid; } else { $retv[1] = $stmt->error; } $stmt->close(); return $retv; }
/** * Cross-site request forgery token generator. * * see https://en.wikipedia.org/wiki/Cross-site_request_forgery * * @return * A token as a string that is HTML / Javascript safe. */ function csrf_token() { if (!session_active()) { return ""; } if (!array_key_exists('_no2_csrf_token', $_SESSION)) { $_SESSION['_no2_csrf_token'] = sprintf("csrf.%s", uuidv4()); } return $_SESSION['_no2_csrf_token']; }
/** * This method should be called before any attempt to log a message. * * <b>Example</b> * @code * No2_Logger::setup([ * 'name' => 'My App.', * 'level' => No2_Logger::WARN, * 'logfile_path' => '/var/log/webapp.log' * ]); * @endcode * * @param $params * A associative array with the following keys: * - name (optional, default: '') * the name of the application * - level (optional, default: INFO) * the logging level. Messages sent to No2_Logger above this value * will silently be ignored. It can be an integer (using this class * constants) or a string (that will be passed to string_to_level()). * - syslog_facility (optional, default: LOG_LOCAL0) * set to a syslog facility if syslog logging is desired. Use for * example LOG_LOCAL0. * - logfile_path (optional) * Path to a writable log file (will be opened in append mode). * * @return * false on error, true otherwise. */ public static function setup($params) { $level = array_key_exists('level', $params) ? $params['level'] : self::INFO; if (is_string($level)) { $level = static::string_to_level($level); } if (!is_numeric($level) || $level < self::EMERG || $level > self::_NO2_DEBUG) { return false; } $name = array_key_exists('name', $params) ? $params['name'] : ''; $syslog = false; if (array_key_exists('syslog_facility', $params)) { $facility = $params['facility']; if (!openlog($name, LOG_ODELAY, $facility)) { return false; } static::$syslog = true; } $fd = null; if (array_key_exists('logfile_path', $params)) { $fd = fopen($params['logfile_path'], 'a'); if (!$fd) { return false; } static::$fd = $fd; } /* * this ensure that either the logger is fully setup, or no config * values has changed since the last call. */ static::$level = $level; static::$name = $name; static::$fd = $fd; static::$syslog = $syslog; static::$reqid = uuidv4(); return true; }
// 16 bits for "time_hi_and_version", // four most significant bits holds version number 4 mt_rand(0, 0x0fff) | 0x4000, // 16 bits, 8 bits for "clk_seq_hi_res", // 8 bits for "clk_seq_low", // two most significant bits holds zero and one for variant DCE1.1 mt_rand(0, 0x3fff) | 0x8000, // 48 bits for "node" mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) ); } $config->set('site/uuid', uuidv4()); $config->save(); DB::open($config->get('dbdsn'), $config->get('dbuser'), $config->get('dbpass')); foreach ($config->get('enabled_plugins') as $plugin) { $path = 'plugins/'.$plugin.'/install.php'; if (is_file($path)) { include_once $path; $f = str_replace('-', '_', $plugin); call_user_func($f.'_install_db'); } }