/** * @preserveGlobalState disabled * @runInSeparateProcess */ public function test_curl_result() { $api = new Hm_API_Curl(); $this->assertEquals(array('unit' => 'test'), $api->command('asdf', array(), array('foo' => 'bar'))); Hm_Functions::$exec_res = NULL; $this->assertEquals(array(), $api->command('asdf', array(), array('foo' => 'bar'))); }
/** * @preserveGlobalState disabled * @runInSeparateProcess */ public function test_pbkdf2() { $this->assertEquals(base64_encode(Hm_Crypt::pbkdf2('testkey', 'testsalt', 32, 2, 'sha512')), '8RSGqH63sWwLtwAssCsc01AIweWJW/f8Mf36zDCFN7E='); $this->assertNotEquals(base64_encode(Hm_Crypt::pbkdf2('testkey', 'testsalt', 32, 2, 'sha512')), 'asdf'); Hm_Functions::$exists = false; $this->assertEquals(base64_encode(Hm_Crypt::pbkdf2('testkey', 'testsalt', 32, 2, 'sha512')), '8RSGqH63sWwLtwAssCsc01AIweWJW/f8Mf36zDCFN7E='); $this->assertNotEquals(base64_encode(Hm_Crypt::pbkdf2('testkey', 'testsalt', 32, 2, 'sha512')), 'asdf'); }
/** * Process a curl request result * @param resource $ch curl connection * @return array */ private function curl_result($ch) { $curl_result = Hm_Functions::c_exec($ch); $result = @json_decode($curl_result, true); if ($result === NULL) { return array(); } return $result; }
/** * post to an oauth2 endpoint * @param string $url url to post to * @param array $flds post data * @return array */ private function curl_post($url, $flds) { $result = array(); $ch = Hm_Functions::c_init(); Hm_Functions::c_setopt($ch, CURLOPT_URL, $url); Hm_Functions::c_setopt($ch, CURLOPT_POST, 5); Hm_Functions::c_setopt($ch, CURLOPT_POSTFIELDS, $flds); Hm_Functions::c_setopt($ch, CURLOPT_RETURNTRANSFER, 1); Hm_Functions::c_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json')); $curl_result = Hm_Functions::c_exec($ch); if (substr($curl_result, 0, 1) == '{') { $result = @json_decode($curl_result, true); } return $result; }
public function process() { if (Hm_Page_Cache::get('nux_dev_news')) { $this->out('nux_dev_news', Hm_Page_Cache::get('nux_dev_news')); return; } $ch = Hm_Functions::c_init(); $res = array(); Hm_Functions::c_setopt($ch, CURLOPT_URL, 'http://cypht.org/git.txt'); Hm_Functions::c_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $curl_result = Hm_Functions::c_exec($ch); if (trim($curl_result)) { foreach (explode("\n", $curl_result) as $line) { if (preg_match("/^([a-z0-9]{40})\\|([a-z0-9]{7})\\|([^\\|]+)\\|([^\\|]+)\\|([^\\|]+)\$/", $line, $matches)) { $res[] = array('hash' => $matches[1], 'shash' => $matches[2], 'name' => $matches[3], 'age' => date('D, M d', strtotime($matches[4])), 'note' => $matches[5]); } } } Hm_Page_Cache::add('nux_dev_news', $res); $this->out('nux_dev_news', $res); }
/** * Key derivation wth pbkdf2: http://en.wikipedia.org/wiki/PBKDF2 * @param string $key payload * @param string $salt random string from generate_salt * @param string $length result length * @param string $count iterations * @param string $algo hash algorithm to use */ public static function pbkdf2($key, $salt, $length, $count, $algo) { /* requires PHP >= 5.5 */ if (Hm_Functions::function_exists('openssl_pbkdf2')) { return openssl_pbkdf2($key, $salt, $length, $count, $algo); } /* manual version */ $size = strlen(hash($algo, '', true)); $len = ceil($length / $size); $result = ''; for ($i = 1; $i <= $len; $i++) { $tmp = hash_hmac($algo, $salt . pack('N', $i), $key, true); $res = $tmp; for ($j = 1; $j < $count; $j++) { $tmp = hash_hmac($algo, $tmp, $key, true); $res ^= $tmp; } $result .= $res; } return substr($result, 0, $length); }
/** * Perform an HTTP redirect * @param string $url url to redirect to * @param int $status current HTTP status * @return void */ public static function page_redirect($url, $status = false) { if (DEBUG_MODE) { Hm_Debug::add(sprintf('Redirecting to %s', $url)); Hm_Debug::load_page_stats(); Hm_Debug::show('log'); } if ($status == 303) { Hm_Debug::add('Redirect loop found'); Hm_Functions::cease('Redirect loop discovered'); } Hm_Functions::header('HTTP/1.1 303 Found'); Hm_Functions::header('Location: ' . $url); return Hm_Functions::cease(); }
/** * Show all messages * @param string $type can be one of "print", "log", or "return" * @return mixed */ public static function show($type = 'print') { if ($type == 'log') { Hm_Functions::error_log(print_r(self::$msgs, true)); } elseif ($type == 'return') { return self::$msgs; } else { print_r(self::$msgs); } }
/** * Load all module sets and include required modules.php files * @param string $page page id * @return void */ public function load_module_sets($page) { $this->load_modules('Hm_Handler_Modules', $this->handlers, $page); $this->load_modules('Hm_Output_Modules', $this->outputs, $page); $active_mods = array_unique(array_merge($this->get_active_mods(Hm_Output_Modules::get_for_page($page)), $this->get_active_mods(Hm_Handler_Modules::get_for_page($page)))); if (!count($active_mods)) { Hm_Functions::cease('No module assignments found'); } $mods = explode(',', $this->site_config->get('modules', '')); $this->load_module_set_files($mods, $active_mods); }
/** * Set a cookie, secure if possible * @param object $request request details * @param string $name cookie name * @param string $value cookie value * @param string $lifetime cookie lifetime * @param string $path cookie path * @param string $domain cookie domain * @param string $html_only set html only cookie flag * @return void */ public function secure_cookie($request, $name, $value, $lifetime = 0, $path = '', $domain = '', $html_only = true) { if ($name == 'hm_reload_folders') { return Hm_Functions::setcookie($name, $value); } if ($request->tls) { $secure = true; } else { $secure = false; } if (!$path && isset($request->path)) { $path = $request->path; } if (!$domain && array_key_exists('SERVER_NAME', $request->server) && strtolower($request->server['SERVER_NAME']) != 'localhost') { $domain = $request->server['SERVER_NAME']; } return Hm_Functions::setcookie($name, $value, $lifetime, $path, $domain, $secure, $html_only); }
/** * Start the session. This could be an existing session or a new login * @param object $request request details * @return void */ public function start($request) { if (array_key_exists($this->cname, $request->cookie)) { session_id($request->cookie[$this->cname]); } list($secure, $path, $domain) = $this->set_session_params($request); session_set_cookie_params(0, $path, $domain, $secure); Hm_Functions::session_start(); if ($request->type == 'HTTP' && !array_key_exists('logout', $request->post)) { session_regenerate_id(true); $this->cname = session_id(); } if (array_key_exists('data', $_SESSION)) { $data = $this->plaintext($_SESSION['data']); if (is_array($data)) { $this->data = $data; } elseif (!$this->loaded) { $this->destroy($request); Hm_Debug::add('Mismatched session level encryption key'); } } $this->active = true; }
/** * Validate a form key. If this is a non-empty POST form from an * HTTP request or AJAX update, it will take the user to the home * page if the page_key value is either not present or not valid * @return void */ public function process_key() { if (empty($this->request->post)) { return false; } $key = array_key_exists('hm_page_key', $this->request->post) ? $this->request->post['hm_page_key'] : false; $valid = Hm_Request_Key::validate($key); if (!$valid) { if ($this->request->type == 'AJAX') { if (DEBUG_MODE) { Hm_Debug::add('REQUEST KEY check failed'); Hm_Debug::load_page_stats(); Hm_Debug::show('log'); } Hm_Functions::cease(json_encode(array('status' => 'not callable'))); return 'exit'; } else { if ($this->session->loaded) { $this->session->destroy($this->request); } Hm_Debug::add('REQUEST KEY check failed'); Hm_Dispatch::page_redirect('?page=home'); return 'redirect'; } } return false; }