public function execute($input_parameters = NULL) { if (MPF::get_instance()->is_debug_enabled()) { MPF::get_instance()->debug(__CLASS__ . '[' . $this->pdo->config['dsn'] . '|' . $this->pdo->get_name() . ']' . "->execute: " . $this->queryString); } $logger = MPF::get_instance()->get_logger(); $logger->debug(__CLASS__, '[' . $this->pdo->get_name() . ']->execute: ', $this->queryString); MPF::get_instance()->pf_benchmark_inc_begin('dbtime'); $start = microtime(true); $ret = parent::execute($input_parameters); $end = microtime(true); MPF::get_instance()->pf_benchmark_inc_end('dbtime'); //add by hexin for record SQL execute time MPF::get_instance()->pf_benchmark("sql_time", array($this->i => $end - $start)); // 按照惯用格式记录sql执行时间和占用内存 // added by htlv $tmp_time = $end - $start; $tmp_mem = memory_get_usage(); mpf_require_class('MPF_Performance'); MPF::get_instance()->pf_benchmark("sql_time_af", array($this->i => array('sql' => $this->_sql, MPF_Performance::MESSAGE_TIME => $tmp_time, MPF_Performance::MESSAGE_MEMORY => $tmp_mem))); if (!$ret) { $error_info = parent::errorInfo(); /** * remove duplicated error log $logger->error(__CLASS__, '['. $this->pdo->get_name() .']->execute: ', $this->queryString); $_error_info = preg_replace("#[\r\n \t]+#",' ',print_r($error_info,true)); $logger->error(__CLASS__, '['. $this->pdo->get_name() .']->execute: ', $_error_info); */ if (parent::errorCode() !== '00000') { throw new MPF_Exception_SqlException(parent::errorCode(), $this->pdo->get_name() . ' | ' . $this->pdo->config['dsn'] . ' | ' . $this->queryString . ' | ' . join(' | ', $error_info)); } } return $ret; }
public function insert_xprof() { mpf_require_class('Aifang_Core_Bll_Tools_Xhprof'); $bll = new Aifang_Core_Bll_Tools_Xhprof(); $data = array('namespace' => $this->namespace, 'xhprof_data' => $this->xhprof_data); return $bll->insert_xhporf($data); }
/** * @return APF_MQ_Stomp */ public function load_stomp($name = "default") { $cfg = MPF::get_instance()->get_config($name, 'mq'); mpf_require_class('MPF_MQ_Stomp'); $stomp = new MPF_MQ_Stomp($cfg['uri']); return $stomp; }
/** * 创建DFS实例. 根据name,从配置文件获取信息创建实例. * * @param string $name * @return APF_DB_PDO */ protected function create_dfs($name) { $mpf = MPF::get_instance(); $config = $mpf->get_config($name, self::CONFIG_F_DFS); if (!isset($config[self::CONFIG_N_CLASS])) { throw new Exception("class not provided for dfs::{$name}"); } $class = $config[self::CONFIG_N_CLASS]; mpf_require_class($class); $dfs = new $class(); $dfs->init($config); return $dfs; }
public function get_inline_styles() { $url = $this->get_boundable_styles_url(); $key = "css-" . md5($url); mpf_require_class('MPF_Cache_Factory'); $mem = MPF_Cache_Factory::get_instance()->get_memcache(); $css = $mem->get($key); if ($css) { return $css; } mpf_require_class('MPF_Http_Client_Factory'); $c = MPF_Http_Client_Factory::get_instance()->get_curl(); $c->set_url($url); $c->execute(); $css = $c->get_response_text(); $mem->set($key, $css, 0, 0); return $css; }
/** * Returns new instance of pdo * * @param string $name * @return APF_DB_PDO */ public function load_pdo($name = "default") { $mpf = MPF::get_instance(); if ($mpf->is_debug_enabled()) { $mpf->benchmark_begin(__CLASS__ . ": open pdo '{$name}'"); } $dbcfg = MPF::get_instance()->get_config($name, "database"); mpf_require_class($this->pdo_class); $pdo = new $this->pdo_class($dbcfg['dsn'], @$dbcfg['username'], @$dbcfg['password'], isset($dbcfg['driver_options']) ? $dbcfg['driver_options'] : array()); $pdo->set_name($name); if (isset($dbcfg['default_fetch_mode'])) { $pdo->set_default_fetch_mode($dbcfg['default_fetch_mode']); } if (isset($dbcfg['init_statements'])) { foreach ($dbcfg['init_statements'] as $sql) { $pdo->exec($sql); } } if ($mpf->is_debug_enabled()) { $mpf->benchmark_end(__CLASS__ . ": open pdo '{$name}'"); } return $pdo; }
protected function get_connection($con_name) { if (array_key_exists($con_name, self::$connections)) { return self::$connections[$con_name]; } mpf_require_class("MPF_DB_Factory"); $con = MPF_DB_Factory::get_instance()->get_pdo($con_name); if (self::$auto_commit === false) { $con->beginTransaction(); } self::$connections[$con_name] = $con; return $con; }
<?php mpf_require_class("MPF_DFS_DFS"); class MPF_DFS_LocalFS implements MPF_DFS_DFS { private $localpath; public function init($config) { $cfg = $config["localfs"]; $domain = $cfg["domain"]; $localpath = $cfg["path"]; $localpath = "{$localpath}/{$domain}/"; if (!is_dir($localpath)) { throw new Exception("{$localpath} is not a directory"); } if (!is_writable($localpath)) { throw new Exception("{$localpath} is not writable"); } $this->localpath = $localpath; return true; } public function load($filename) { $filepath = $this->localpath . $filename; if (!is_file($filepath)) { throw new Exception("{$filepath} is not a file"); } return file_get_contents($filepath, false); } public function save($filename, $content) {
<?php mpf_require_class('MPF_Util_StringUtils'); mpf_require_class('MPF_RequestParametersLoader'); class MPF_Request implements MPF_RequestParametersLoader { public function __construct() { $this->parameters_loader = $this; } public function __destruct() { } // /** * Router类会在在url_mapping匹配后设置 * * @param unknown_type $matches */ public function set_router_matches($matches) { $this->router_matches = $matches; } /** * 得到url_mapping匹配的结果 * * @return array */ public function get_router_matches() { return $this->router_matches;
<?php mpf_require_class("MPF_Util_CodecUtils"); class MPF_Cache_Filecache { const MAX_SECONDS = 2592000; public function __construct() { } public function __destruct() { } public function add($key, $var, $flag, $expire = 0) { /* $path = $this->get_path_by_key($key); if (!mkdir($path, 0755, true)) { return FALSE; }*/ $path = $this->cache_dir; //add by minjiewang 2011-12-19 $created = time(); if ($expire > 0 && $expire <= self::MAX_SECONDS) { $expire += $created; } $data_body = serialize($var); $data_head = pack('LL', $created, $expire); $filename = $path . '/' . $this->get_file_by_key($key); $fp = fopen($filename, 'w'); if (!$fp) { return FALSE; }
<?php mpf_require_class('MPF_Solr_Factory'); mpf_require_class('MPF_Solr_Accessor'); abstract class MPF_Solr_Object { public function __construct() { foreach ($this->get_fields() as $k => $v) { unset($this->{$k}); } } /** * @return APF_Solr_Accessor */ public static function get_accessor() { return new MPF_Solr_Accessor(get_called_class()); } protected function get_fields() { $mapping = static::get_mapping(); return $mapping['fields']; } protected function get_key() { $mapping = static::get_mapping(); return $mapping['key']; } protected function get_commit() {
/** * @return APF_Http_Client_Curl */ public function load_curl() { mpf_require_class("MPF_Http_Client_Curl"); $curl = new MPF_Http_Client_Curl(); return $curl; }
<?php mpf_require_class("MPF"); /** * 去掉魔术引号转意拦截器 */ class MPF_DisablingMagicQuotesInterceptor extends MPF_Interceptor { public function before() { // http://cn2.php.net/manual/en/security.magicquotes.disabling.php if (get_magic_quotes_gpc()) { function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); $_GET = array_map('stripslashes_deep', $_GET); $_COOKIE = array_map('stripslashes_deep', $_COOKIE); $_REQUEST = array_map('stripslashes_deep', $_REQUEST); } return self::STEP_CONTINUE; } public function after() { return self::STEP_CONTINUE; } }
<?php mpf_require_class("MPF_Performance"); class MPF_PerformanceInterceptor extends MPF_Interceptor { public function before() { if (!$this->is_allow_performance()) { return self::STEP_CONTINUE; } $mpf = MPF::get_instance(); $rate = @$mpf->get_config("performance_rate"); if (empty($rate)) { $rate = 100; } $rand = rand(1, $rate); if ($rand == 1) { $request = $mpf->get_request(); $mpf->set_performance($this->create_performance()); } return self::STEP_CONTINUE; } public function after() { return self::STEP_CONTINUE; } protected function create_performance() { return new MPF_Performance(); } protected function is_allow_performance()
<?php mpf_require_class('MPF_DFS_DFS'); mpf_require_file('MogileFS.php'); class MPF_DFS_MogileFS implements MPF_DFS_DFS { public function init($config) { if ($this->mfs) { throw new Exception("Cannot initialize twice!"); } $mfscfg = $config['mogilefs']; $domain = $mfscfg['domain']; $class = $mfscfg['class']; $trackers = $mfscfg['trackers']; // TODO: validate parameters? // TODO: other parameters like timeouts? $this->mfs = new MogileFS($domain, $class, $trackers); } public function load($filename) { return $this->mfs->get($filename); } public function save($filename, $content) { return $this->mfs->set($filename, $content); } public function delete($filename) { return $this->mfs->delete($filename); }
<?php mpf_require_class('MPF_Solr_Factory'); class MPF_Solr_Accessor { public function __construct($class) { $this->class = $class; $this->mapping = $class::get_mapping(); } private $mapping; public function add_doc_field($field, $value) { $this->doc_fields[$field] = $value; return $this; } private $doc_fields; public function add_field($field) { if (!is_array($this->fields) || !in_array($field, $this->fields)) { $this->fields[] = $field; } return $this; } private $fields; public function set_query($query) { $this->query = $query; return $this; } private $query;
function url2hash($type, $file, $ext, $redisurl, $onlyhash = false) { $mpf = MPF::get_instance(); $request = $mpf->get_request(); $response = $mpf->get_response(); $type_single = self::DEFAULT_RESOURCE_TYPE_SINGLE; $type_boundable = self::DEFAULT_RESOURCE_TYPE_BOUNDABLE; if ($this->is_use_redis()) { mpf_require_class('MPF_Cache_Factory'); $objRedis = MPF_Cache_Factory::get_instance()->get_redis('redis'); $strContentHash = ''; if ($type == $type_boundable) { $res = $this->fetch_boundable_resources($file, $ext, TRUE); if (!$res) { $new_file = $this->try_new_file($file); if ($new_file) { //301 $url = "{$new_file}.{$ext}"; $response->redirect($url, true); } else { //404 $response->set_header("HTTP/1.1", "404 Not Found", "404"); return; } } //if(true===ANJUKE_PHP_IN_GA){ ob_start(); $response->add_header('ori_url', $redisurl . '|' . date('Y-m-d H:i:s')); $hashversion = $mpf->get_config('hash_url_version', 'resource'); echo '/*' . $hashversion . '*/'; $this->passthru_boundable_resources(); $res = ob_get_contents(); ob_clean(); $strContentHash = md5($res); $objRedis->setex('url_' . $redisurl, 233280000, $strContentHash); $objRedis->setex('hash_' . $strContentHash, 233280000, $redisurl); if (!$onlyhash) { echo $res; } /*}else{ $this->passthru_boundable_resources(); }*/ } elseif ($type == $type_single) { ob_start(); $response->add_header('ori_url', $redisurl . '|' . date('Y-m-d H:i:s')); $hashversion = $mpf->get_config('hash_url_version', 'resource'); echo '/*' . $hashversion . '*/'; if (!$this->include_resource_file("{$file}.{$ext}")) { trigger_error("Unable to include resource \"{$file}.{$ext}\"", E_USER_WARNING); } $res = ob_get_contents(); ob_clean(); $strContentHash = md5($res); $objRedis->setex('url_' . $redisurl, 233280000, $strContentHash); $objRedis->setex('hash_' . $strContentHash, 233280000, $redisurl); if (!$onlyhash) { echo $res; } } return $strContentHash; } else { if ($type == $type_boundable) { $res = $this->fetch_boundable_resources($file, $ext, TRUE); if (!$res) { $new_file = $this->try_new_file($file); if ($new_file) { //301 $url = "{$new_file}.{$ext}"; $response->redirect($url, true); } else { //404 $response->set_header("HTTP/1.1", "404 Not Found", "404"); return; } } $this->passthru_boundable_resources(); } elseif ($type == $type_single) { if (!$this->include_resource_file("{$file}.{$ext}")) { trigger_error("Unable to include resource \"{$file}.{$ext}\"", E_USER_WARNING); } } } }
<?php mpf_require_class("MPF_Debugger"); class MPF_DebuggerInterceptor extends MPF_Interceptor { const TRIGGER_PARAMETER_NAME = 'debug'; const TRIGGER_COOKIE_NAME = 'debug'; const TRIGGER_MEMORY_LIMIT = 'memory_limit'; const TRIGGER_DISPALY_ERRORS = 'display_errors'; public function before() { if (!$this->is_allow_debug()) { return self::STEP_CONTINUE; } $mpf = MPF::get_instance(); $request = $mpf->get_request(); $response = $mpf->get_response(); $debug_param = @$request->get_parameter(self::TRIGGER_PARAMETER_NAME); $debug_cookie = @$request->get_cookie(self::TRIGGER_COOKIE_NAME); if (isset($debug_param) && $debug_param == 0) { // disable debug output, remove debug cookie if (isset($debug_cookie)) { $response->remove_cookie(self::TRIGGER_COOKIE_NAME); } return self::STEP_CONTINUE; } $memory_limit = @$request->get_parameter(self::TRIGGER_MEMORY_LIMIT); $memory_limit = @floatval($memory_limit); if (0 < $memory_limit && 10240) { /* 1~10240M */ ini_set('memory_limit', $memory_limit . 'M');
/** * 导入v2页面,mpf_require_class的简单封装。 * @param string $class 类名 * @return boolean */ function mpf_require_page($class) { if (!(defined('G_LOAD_AUTO') && G_LOAD_AUTO === false)) { return true; } if (class_exists($class . "Page")) { return true; } return mpf_require_class($class, "page"); }
<?php //mpf_require_class("MPF_Ratelimiting"); mpf_require_class('MPF_Cache_Factory'); class MPF_RatelimitingInterceptor extends MPF_Interceptor { public function before() { $this->process_vews(); return self::STEP_CONTINUE; } public function after() { return self::STEP_CONTINUE; } private function get_views($idorip) { $cache = MPF_Cache_Factory::get_instance()->get_memcache(); if (!cache) { return false; } $views = $cache->get($idorip); return $views; //return $views[$idorip]=array('t'=>1,'c'=>1); } private function set_views($idorip, $inc = 1, $time = 0) { $cache = MPF_Cache_Factory::get_instance()->get_memcache(); if (!cache) { return false; }
<?php mpf_require_class("MPF_Component"); /** * APF 页面类,是一个特殊的组件 * added by microhuang */ abstract class MPF_Page extends MPF_Component { /** * 页面是所有组件的父组件 * @param unknown_type $parent * @param unknown_type $html_id */ public function __construct($parent = NULL, $html_id = NULL) { parent::__construct($parent, $html_id); } public function __toString() { $cn = get_class($this); return trim(substr($cn, 0, strlen($cn) - 4), '_'); } /** * 获取页面标题 * @return string 页面标题 */ public function get_title() { return "AnjukePHP " . MPF::VERSION; }
<?php mpf_require_class("MPF_DB_PDOStatement"); mpf_require_class("MPF_Exception_SqlException"); class MPF_DB_PDO extends PDO { private $i = 0; public function __construct($dsn, $username = "", $password = "", $driver_options = array()) { /*if ($username == 'caixh') { APF::get_instance()->get_logger()->error("PDO error:" . $dsn . "," . $_SERVER['REQUEST_URI']); }*/ parent::__construct($dsn, $username, $password, $driver_options); $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MPF_DB_PDOStatement', array($this))); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); } public function exec($statement) { if (isset($this->dsn) and stristr($this->dsn, 'anjuke_db') and preg_match('/\\sajk_propertys\\s/i', $statement)) { if (stristr($statement, 'select CITYID') or stristr($statement, 'insert') or stristr($statement, 'update ')) { } else { $dir = '/home/www/logs/propsql'; if (!is_dir($dir)) { mkdir($dir, 0755, true); $content = '-=-=-=-=-=-=-=-=-=-=' . PHP_EOL; $content .= 'DSN: ' . $this->dsn . PHP_EOL; $content .= 'URI: ' . $_SERVER['REQUEST_URI'] . PHP_EOL; $content .= 'JOB: ' . var_export($_SERVER['argv'], true) . PHP_EOL; $content .= 'SQL: ' . $statement . PHP_EOL; file_put_contents($dir . '/' . date('Ymd'), $content, FILE_APPEND);
/** * @return MPF_NLogger */ public function get_nlogger() { if (!isset($this->nlogger)) { mpf_require_class($this->nlogger_class); $this->nlogger = new $this->nlogger_class(); } return $this->nlogger; }
public function get_memcache_keys() { mpf_require_class("MPF_Cache_Factory"); $keys = MPF_Cache_Factory::get_instance()->get_memcache()->get_read_keys(); return $keys ? $keys : array(); }
<?php //TODO it's should be a factory pattern, just like AMQP_ObjFactory::get_building() mpf_require_class('MPF_AMQP_Factory'); abstract class MPF_AMQP_Object { public $is_loaded = false; public $commit; public function save() { if ($this->is_loaded == false) { return false; } try { $msg = $this->build_message(); $con = MPF_AMQP_Factory::get_instance()->get_connection(); $ex = new AMQPExchange($con, self::get_exchange()); $ret = $ex->publish($msg, self::get_routing()); } catch (Exception $e) { MPF::get_instance()->get_debugger()->debug('MMPQ: Can not be published - ' . $e->getMessage()); } return true; } public function delete() { if ($this->is_loaded == false) { return false; } try { $msg = $this->build_delete(); $con = MPF_AMQP_Factory::get_instance()->get_connection();
<?php mpf_require_class("MPF_Page"); /** * APF页面装饰器,其实是页面的骨架,起占位作用。 * 所有的组件和实际页面内容都依附于装饰器。 * 所谓的“装饰器”其实就是页面模板。 * 在页面模板中: * real_page方法显示页面主体; * component方法显示特定组件; * * added by htlv */ abstract class MPF_DecoratorPage extends MPF_Page { /** * 重载了组件的execute方法 * 载入装饰器 * @see APF_Page::execute() */ public function execute() { $view = $this->get_decorator(); MPF::get_instance()->debug("decorator: {$view}"); $file = "page/" . $view . ".phtml"; global $G_LOAD_PATH; foreach ($G_LOAD_PATH as $path) { if (file_exists($path . $file)) { $this->render($path . $file, TRUE); break; }
<?php mpf_require_class("MPF_Xhprof"); class MPF_XhprofInterceptor extends MPF_Interceptor { const TRIGGER_PARAMETER_NAME = 'xhprof'; const TRIGGER_COOKIE_NAME = 'xhprof'; public function before() { if (!$this->is_allow_xhprof()) { return self::STEP_CONTINUE; } $mpf = MPF::get_instance(); $request = $mpf->get_request(); $response = $mpf->get_response(); $xhprof_param = @$request->get_parameter(self::TRIGGER_PARAMETER_NAME); $xhprof_cookie = @$request->get_cookie(self::TRIGGER_COOKIE_NAME); if (isset($xhprof_param) && $xhprof_param == 0) { if (isset($xhprof_cookie)) { $response->remove_cookie(self::TRIGGER_COOKIE_NAME); } return self::STEP_CONTINUE; } if (isset($xhprof_param) && $xhprof_param > 0) { $this->create_xhprof(); $response->set_cookie(self::TRIGGER_COOKIE_NAME, $xhprof_param); return self::STEP_CONTINUE; } if (isset($xhprof_cookie) && $xhprof_cookie > 0) { $this->create_xhprof(); } else {
/** * @return APF_Cache_Redis */ public function load_redis($name) { mpf_require_class($this->redis_class); $redis = new $this->redis_class(); $redis_conf = MPF::get_instance()->get_config($name, 'cache'); $redis->connect($redis_conf['host'], $redis_conf['port'], $redis_conf['timeout']); return $redis; }