/** * Inject * * @return void */ public function onInject() { parent::onInject(); //$this->_session = BEAR::dependency('BEAR_Session'); $this->_resource = BEAR::dependency('BEAR_Resource'); }
/** * UAインジェクト * * 携帯3キャリア/iPhone/iPad/Androidの判定を行います。 * BEAR_Agentの$config['ua_inject']でこのクラスが指定されています。 * * @param BEAR_Main &$object BEAR_Agentオブジェクト * @param array $config 設定 * * @return void * @see http://code.google.com/p/bear-project/wiki/agent */ public static function inject($object, $config) { if (!isset($config['user_agent'])) { $object->setService('_ua', BEAR_Agent::UA_DEFAULT); return; } $agentMobile = BEAR::dependency('BEAR_Agent_Mobile', array('user_agent' => $config['user_agent'])); if ($agentMobile->isNonMobile()) { if (strpos($config['user_agent'], 'iPhone') !== false) { // iPhoneの場合 $ua = BEAR_Agent::UA_IPHONE; } else { if (strpos($config['user_agent'], 'iPad') !== false) { // iPadの場合 $ua = BEAR_Agent::UA_IPAD; } else { if (strpos($config['user_agent'], 'Android') !== false) { // Androidの場合 $ua = BEAR_Agent::UA_ANDROID; } else { $ua = BEAR_Agent::UA_DEFAULT; } } } } else { $ua = ucwords(strtolower($agentMobile->getCarrierLongName())); } $object->setService('_ua', $ua); }
/** * emoji * * <pre> * スタティックな絵文字を表示します。テンプレート生成時のみ動作します。 * </pre> * * @category BEAR * @package BEAR_Smarty * @subpackage Plugin * @author Akihito Koriyama <*****@*****.**> * @copyright 2008-2011 Akihito Koriyama All rights reserved. * @license http://opensource.org/licenses/bsd-license.php BSD * @version SVN: Release: @package_version@ $Id:$ * @link http://www.bear-project.net/ * * @param string $tagArg * @param Smarty &$smarty Smarty object * * @return string */ function smarty_compiler_emoji($tagArg, &$smarty) { $emoji = BEAR::dependency('BEAR_Emoji')->getAgentEmoji($tagArg); // SBの絵文字のエラーを避けるためecho文を使わない return '?>' . "{$emoji}" . '<?php '; // return 'echo "' . "{$emoji}" . '";'; }
/** * エージェントブロック関数 * * <pre> * エージェントによって表示/非表示を制御します。 * エージェントの指定は大文字でも小文字問いません。 * * Example * </pre> * * <code> * {agent in='docomo,au'}ドコモとAUだけ表示{/agent} * {agent out='softbank'}SBのみ非表示{/agent} * {agent in='iphone' func='upper_case'}iPhoneのみ大文字で{/agent} * </code> * <pre> * * $params * * 'in' mixed カンマ区切りで含まれていたら表示。 * 'out' string カンマ区切りで含まれていなかったら表示 * 'func' string ユーザー関数 * </pre> * * @category BEAR * @package BEAR_Smarty * @subpackage Plugin * @author Akihito Koriyama <*****@*****.**> * @copyright 2008-2011 Akihito Koriyama All rights reserved. * @license http://opensource.org/licenses/bsd-license.php BSD * @version SVN: Release: @package_version@ $Id:$ * @link http://www.bear-project.net/ * * @param string $params パラメーター * @param string $content HTML * @param Smarty &$smarty &Smarty object * @param bool &$repeat &$repeat 呼び出し回数 * * @return string */ function smarty_block_agent($params, $content, &$smarty, &$repeat) { $ua = strtolower(BEAR::dependency('BEAR_Agent')->getUa()); //開始タグ if (is_null($content)) { $valid = false; if (array_key_exists('in', $params)) { $in = explode(',', $params['in']); if (in_array($ua, $in)) { $valid = true; } } if (!$valid) { if (array_key_exists('out', $params)) { $out = explode(',', $params['out']); if (!in_array($ua, $out)) { $valid = true; } } } if (!$valid) { $repeat = false; } } else { if (array_key_exists('func', $params)) { assert(function_exists($params['func'])); return call_user_func($params['func'], $content); } else { return $content; } } return ''; }
/** * 初期化 * * @return void */ public function onInit(array $args) { // Create our Application instance (replace this with your appId and secret). $facebook = BEAR::factory('App_Fb'); // We may or may not have this data based on a $_GET or $_COOKIE based session. // // If we get a session here, it means we found a correctly signed session using // the Application Secret only Facebook and the Application know. We dont know // if it is still valid until we make an API call using the session. A session // can become invalid if it has already expired (should not be getting the // session back in this case) or if the user logged out of Facebook. $session = $facebook->getSession(); $me = $uid = null; // Session based API call. if ($session) { $uid = $facebook->getUser(); $me = $facebook->api('/me'); } // login or logout url will be needed depending on current user state. $loginUrl = $logoutUrl = null; if ($me) { $logoutUrl = $facebook->getLogoutUrl(); } else { $loginUrl = $facebook->getLoginUrl(); } // This call will always work since we are fetching public data. $naitik = $facebook->api('/naitik'); $appId = $facebook->getAppId(); $encodedSession = json_encode($session); $this->set(compact('loginUrl', 'logoutUrl', 'appId', 'encodedSession', 'me', 'uid', 'naitik')); }
/** * App init * * @param int $bearMode * * @return void */ public static function init($bearMode = 1) { $app = BEAR::loadConfig(_BEAR_APP_HOME . '/App/app.yml'); switch ($bearMode) { case 1: //debug mode (cache disabled) $app['BEAR_Cache']['adapter'] = 0; case 2: //debug mode (cache enabled) $app['core']['debug'] = true; $app['App_Db']['dsn']['default'] = $app['App_Db']['dsn']['slave'] = $app['App_Db']['dsn']['test']; $app['BEAR_Ro_Prototype']['__class'] = 'BEAR_Ro_Prototype_Debug'; break; case 100: // for HTTP access UNIT test $app['core']['debug'] = true; $app['BEAR_Log']['__class'] = 'BEAR_Log_Test'; $app['BEAR_Resource_Request']['__class'] = 'BEAR_Resource_Request_Test'; break; case 0: default: // live $app['core']['debug'] = false; break; } BEAR::init($app); }
/** * appinfo * * <pre> * appinfo(app.ymlの['core']['info'])の値を出力します。テンプレート生成時のみ動作します。 * </pre> * * @category BEAR * @package BEAR_Smarty * @subpackage Plugin * @author Akihito Koriyama <*****@*****.**> * @copyright 2008-2011 Akihito Koriyama All rights reserved. * @license http://opensource.org/licenses/bsd-license.php BSD * @version SVN: Release: @package_version@ $Id:$ * @link http://www.bear-project.net/ * * @param string $tagArg * @param Smarty &$smarty Smarty object * * @return string */ function smarty_compiler_appinfo($tagArg, &$smarty) { static $app = array(); if (!$app) { $app = BEAR::get('app'); } return 'echo \'' . "{$app['core']['info'][$tagArg]}" . '\';'; }
/** * 変数画面出力 * * @param array $values 値 * @param array $options オプション * * @return BEAR_Ro */ function outputPrint($values, array $options) { $body = print_a($values, 'return:1'); $headers = array('X-BEAR-Output: PRINT' => 'Content-Type: text/html; charset=utf-8'); $ro = BEAR::factory('BEAR_Ro'); $ro->setBody($body); $ro->setHeaders($headers); return $ro; }
/** * PHPシリアライズ変数出力 * * @param array $values 値 * @param array $options オプション * * @return BEAR_Ro */ function outputPhp($values, array $options) { $body = serialize($values); $headers = array('X-BEAR-Output: PHP' => 'Content-Type: text/html; charset=utf-8'); $ro = BEAR::factory('BEAR_Ro'); $ro->setBody($body); $ro->setHeaders($headers); return $ro; }
/** * JSON出力 * * @param array $values 値 * @param array $options オプション * * @return BEAR_Ro */ function outputJson($values, array $options) { $body = json_encode($values); $headers = array('X-BEAR-Output: JSON' => 'Content-Type: text/javascript+json; charset=utf-8'); $ro = BEAR::factory('BEAR_Ro'); $ro->setBody($body); $ro->setHeaders($headers); return $ro; }
public function test_isTokenPoeValidFailure() { $val = '03c88eb462460abeeff7' . '6ca881e559ef98901975'; $_POST['_token'] = $val; $token = new BEAR_Form_Token(array()); $token->onInject(); BEAR::dependency('BEAR_Session')->set(BEAR_Form_Token::SESSION_POE, array($val)); $isValid = $token->isTokenPoeValid(); $this->assertFalse($isValid); }
/** * UAインジェクト * * @param BEAR_Main &$object BEAR_Mainオブジェクト * @param array $config 設定 * * @return void */ public static function inject($object, $config) { $userAgent = isset($config['http_user_agent']) ? $config['http_user_agent'] : (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''); $agent = BEAR::dependency('BEAR_Agent', array('user_agent' => $userAgent)); $object->setService('_agent', $agent); $object->setConfig('ua', $agent->getUa()); $object->setConfig('enable_ua_sniffing', true); //エージェント依存サブミット(絵文字、コード) $agent->adapter->submit(); }
/** * Db factorys * * @return array MDB2_Driver_Datatype_mysqli */ public function factory() { $options['default_table_type'] = 'INNODB'; $options['use_transactions'] = true; $config = array('dsn' => $this->_config['dsn']); $config['options'] = $options; $db = BEAR::factory('BEAR_Mdb2', $config); $db->setOption('quote_identifier', true); $db->loadModule('Extended'); return $db; }
/** * Resource request * * @return BEAR_Ro * * @see BEAR_Resource_Request::request() */ public function request() { $query = http_build_query($this->_config['values']); if ($query) { $query = '?' . $query; } $resurceQuery = $this->_config['method'] . ' ' . $this->_config['uri'] . (string) $query; BEAR::dependency('BEAR_Log')->log('resource', $resurceQuery); $result = parent::request(); return $result; }
/** * @see BEAR_Base::onInject() */ public function onInject() { $this->_tokenStrage = BEAR::dependency('BEAR_Session'); if (isset($_POST['_token'])) { $token = $_POST['_token']; } elseif (isset($_GET['_token'])) { $token = $_POST['_token']; } else { $token = ''; } $this->_submitToken = $token; }
/** * Inject * * @param BEAR_Agent &$object BEAR_Agentオブジェクト * @param array $config * * @return void */ public static function inject($object, $config) { $agent = BEAR::dependency('BEAR_Agent'); $ua = $agent->getUa(); $config = $agent->adapter->getConfig(); $role = $config['role']; foreach ($role as $agent) { $method = 'onInject' . $agent; if (method_exists($object, $method)) { $object->{$method}(); return; } } $object->onInject(); }
/** * Pullリソース * * <pre> * * Example * </pre> * <code> * {resource uri='Entry' params=$entryParams tepmplate="resource/entry"} * </code> * * @category BEAR * @package BEAR_Smarty * @subpackage Plugin * @author Akihito Koriyama <*****@*****.**> * @copyright 2008-2011 Akihito Koriyama All rights reserved. * @license http://opensource.org/licenses/bsd-license.php BSD * @version SVN: Release: @package_version@ $Id:$ * @link http://www.bear-project.net/ * * @param mixed $params * @param Smarty &$smarty * * @return mixed $resource */ function smarty_function_resource($params, &$smarty) { $config = (array) BEAR::loadValues($params['params']) + array('method' => 'read', 'uri' => $params['uri'], 'values' => array(), 'options' => array()); if (isset($params['template'])) { $config['options']['template'] = $params['template']; } if (isset($params['cache_life'])) { $config['options']['cache']['life'] = $params['cache_life']; } if (isset($params['cache_key'])) { $config['options']['cache']['key'] = $params['cache_key']; } $app = BEAR::get('app'); $string = BEAR::factory('BEAR_Ro_Prototype', array('request' => $config, 'path' => $app['BEAR_View']['path']))->request()->toString(); return $string; }
/** * キャッシュを取得 * * キーを基にキャッシュデータを取得します * * @param string $key キー * @param mixed $default デフォルト * * @return mixed */ public function get($key, $default = null) { $result = apc_fetch($this->_config['prefix'] . $key); if (!$result) { $result = $default; } if ($result instanceof BEAR_Ro_Container) { $ro = BEAR::factory('BEAR_Ro'); $ro->setCode($result->code)->setHeaders((array) $result->header)->setBody($result->body)->setLinks($result->links)->setHtml($result->html); $result = $ro; } if ($result) { $this->_log->log('Apc[R]', $key); } return $result; }
public static function onAutoload($class) { static $growlApp; BEAR::onAutoload($class); $ref = new ReflectionClass($class); $file = $ref->getFileName(); if (!$growlApp) { $growlApp = new Net_Growl_Application(__CLASS__, array("Growl_Notify")); } $growl = Net_Growl::singleton($growlApp, null, null); $growl->setNotificationLimit(16); if (BEAR::exists($class)) { $config = BEAR::get($class)->getConfig(); } $growl->notify("Growl_Notify", $class, $class); }
/** * キャッシュを取得 * * キーを基にキャッシュデータを取得します * * @param string $key キー * @param mixed $options オプション * * @return mixed */ public function get($key, $options = array('default' => null)) { $result = $this->_adapter->get($this->_config['prefix'] . $key); // 結果がなくてデフォルトが用意されていればデフォルト if ($result === false && $options['default']) { $result = $options['default']; } if ($result instanceof BEAR_Ro_Container) { $ro = BEAR::factory('BEAR_Ro'); $ro->setCode($result->code)->setHeaders((array) $result->header)->setBody($result->body)->setLinks($result->links)->setHtml($result->html); return $ro; } if ($result !== false) { $this->_log->log('Cache Lite[R]', $key); } return $result; }
/** * JSON出力 * * @param array $values 値 * @param array $options オプション * * @return BEAR_Ro */ function outputAjax($values, array $options) { $ajax = BEAR::get('BEAR_Page_Ajax'); $values = $ajax->getAjaxValues(); // $body = json_encode($values); // Services_JSONの方が信頼性が高いため採用 $json = new Services_JSON(); $body = $json->encode($values); $log = BEAR::dependency('BEAR_Log'); /* @var $log BEAR_Log */ $log->log('AJAX', $values); $headers = array('X-BEAR-Output: AJAX' => 'Content-Type: text/javascript+json; charset=utf-8'); $ro = BEAR::factory('BEAR_Ro'); /* @var $ro BEAR_Ro */ $ro->setBody($body); $ro->setHeaders($headers); return $ro; }
/** * Form * * @return void */ public function build() { $form = BEAR::dependency('BEAR_Form', $this->_form); $form->setDefaults(array('name' => 'Kuma', 'email' => '*****@*****.**')); // フォームヘッダー $form->addElement('header', 'main', '入力(確認)してください'); // フォームインプットフィールド $form->addElement('text', 'name', '名前', 'size=30 maxlength=30'); $form->addElement('text', 'email', 'メールアドレス', 'size=30 maxlength=30'); $form->addElement('textarea', 'body', '感想'); $form->addElement('submit', '_submit', '送信', ''); // フィルタと検証ルール $form->applyFilter('__ALL__', 'trim'); $form->applyFilter('__ALL__', 'strip_tags'); $form->addRule('name', '名前を入力してください', 'required', null, 'client'); $form->addRule('email', 'emailを入力してください', 'required', null, 'client'); $form->addRule('email', 'emailの形式で入力してください', 'email', null, 'client'); }
/** * プロファイリングストップ * * プロファイラーリンクの表示します * * @return void */ public static function stop() { static $done = false; if ($done !== false) { return; } $done = true; $xhprofData = xhprof_disable(); $app = BEAR::get('app'); $appName = $app['core']['info']['id']; // アプリ名とか識別する名前 include_once _BEAR_BEAR_HOME . '/BEAR/vendors/xhprof_lib/utils/xhprof_lib.php'; include_once _BEAR_BEAR_HOME . '/BEAR/vendors/xhprof_lib/utils/xhprof_runs.php'; $xhprofRuns = new XHProfRuns_Default(); $runId = $xhprofRuns->save_run($xhprofData, $appName); $href = "/__bear/prof/index.php?run={$runId}&source={$appName}"; echo '<a style="padding: 3px; background-color: red; color: white; font-family: Verdana; font-style: normal; font-variant: normal; font-weight: bold; font-size: 8pt; " name="" target="_blank"' . $appName . '" href="' . $href . '"">PROFILE</a>'; }
/** * Inject * * @return void */ public function onInject() { $this->_header = BEAR::dependency('BEAR_Page_Header'); $this->_session = BEAR::dependency('BEAR_Session'); switch (true) { case isset($_GET['extend']): // セッション延長 $this->_mode = self::EXTEND; break; case isset($_GET['logout']): // セッション破棄 $this->_mode = self::LOGOUT; break; default: // どうしますか画面 $this->_mode = null; } }
/** * リソースリクエスト実行 * * スタティクファイルをリソースとして扱います。 * XML, YAML, CSV, INIファイルをサポートしています。 * * @return mixed * @throws BEAR_Resource_Execute_Exception */ public function request() { if ($this->_config['method'] === BEAR_Resource::METHOD_READ) { $result = BEAR::loadValues($this->_config['file']); } else { $config = array('info' => compact('method'), 'code' => 400); throw new BEAR_Resource_Execute_Exception('Method "read" is only allowed with static file', $config); } // valuesでheader=trueが指定されてると一行目はキーに $noHeader = is_array($result) || isset($this->_config['values']['header']) && (bool) $this->_config['values']['header'] === true; if (!$noHeader && is_array($result)) { $index = array_shift($result); $array = array(); foreach ($result as $value) { $array[] = array_combine($index, array_values($value)); } } return $result; }
/** * プロファイリングストップ * * プロファイラーリンクの表示します * * @param int $type 0:xhprof 1:xh gui * * @return void */ public static function stop($type = 0) { $xhprof_data = xhprof_disable(); $isAjax = BEAR::dependency('BEAR_Page_Ajax')->isAjaxRequest(); if ($isAjax === true || PHP_SAPI === 'cli') { return; } $xhprofRuns = new XHProfRuns_Default(); if (BEAR::exists('app')) { $app = BEAR::get('app'); $appName = $app['core']['info']['id']; } else { $appName = 'bear.app'; } $baseHref = self::$_profilerInfo[$type]['href']; $runId = $xhprofRuns->save_run($xhprof_data, $appName); $href = "{$baseHref}/index.php?run={$runId}&source={$appName}"; $link = '<br><a style="padding: 3px; background-color: red; color: white; font-family:'; $link .= 'Verdana; font-style: normal; font-variant: normal; font-weight: bold; font-size: 8pt;'; $link .= ' " name="" target="_blank" href="' . $href . '"">' . self::$_profilerInfo[$type]['label'] . '</a>'; echo $link; }
/** * リソースリクエスト実行 * * App/Ro下のROリソースのリクエストを行います。 * * @return mixed */ public function request() { // ROクラスDI $config = array('method' => $this->_config['method'], 'uri' => $this->_config['uri'], 'class' => $this->_config['class']); if (isset($this->_config['options']['config'])) { $roConfig = array_merge($this->_config, $this->_config['options']['config']); } else { $roConfig = $this->_config; } if (isset($this->_config['options']['injector'])) { $roOptions = array('injector' => $this->_config['options']['injector']); } else { // デフォルトインジェクト $roOptions = array(); } $this->_config['obj'] = BEAR::factory($this->_config['class'], $roConfig, $roOptions); // アノテーションクラスDI $config['method'] = 'on' . $this->_config['method']; $annotation = BEAR::factory('BEAR_Annotation', $config); // requireアノテーション (引数のチェック) $annotation->required($this->_config['values']); // aspectアノテーション (アドバイスの織り込み) $method = $annotation->aspect(); $result = $method->invoke($this->_config['obj'], $this->_config['values']); // 後処理 if (PEAR::isError($result)) { $this->_config['obj']->setCode(BEAR::CODE_ERROR); $this->_config['obj']->setHeader('_error', $result->toString()); } else { if ($result instanceof BEAR_Ro) { // return RO return $result; } else { $this->_config['obj']->setBody($result); } } return $this->_config['obj']; }
/** * アプリケーション実行 * * @param int $mode 動作モード * * @return void */ public static function init($mode = 1) { $app = BEAR::loadConfig(_BEAR_APP_HOME . '/App/app.yml'); switch ($mode) { case 2: case 1: //開発モード (キャッシュ有効) $app['core']['debug'] = true; $app['App_Db']['dsn']['default'] = $app['App_Db']['dsn']['slave'] = $app['App_Db']['dsn']['test']; $app['BEAR_Ro_Prototype']['__class'] = 'BEAR_Ro_Prototype_Debug'; break; case 0: default: //ライブ $app['core']['debug'] = false; break; } BEAR::init($app); // 開発モード(キャッシュクリア) if ($mode == 1) { BEAR_Util::clearAllCache(false); } }
/** * XML出力 * * XMLを出力します。 * * @param array $values 値 * @param array $options オプション * * @return BEAR_Ro */ function outputXml($values, array $options) { $defaultOptions = array("mode" => "", "indent" => " ", "linebreak" => "\n", "indentAttributes" => true, "typeHints" => false, "scalarAsAttributes" => true, "addDecl" => true, "encoding" => "UTF-8", "rootName" => "rdf:RDF", "rootAttributes" => array("xmlns" => "http://purl.org/rss/1.0/", "xmlns:rdf" => "http://www.e3.org/1999/02/22-rdf-syntax-ns#", "xmlns:dc" => "http://purl.org/dc/elements/1.1/", "xmlns:sy" => "http://purl.org/rss/1.0/modules/syndication/", "xmlns:admin" => "http://webns.net/mvcb/", "xmlns:content" => "http://purl.org/rss/1.0/modules/content/", "xml:lang" => "ja"), "defaultTagName" => "item", "attributesArray" => "_attributes"); if (is_array($options)) { $options = $options + $defaultOptions; } else { $options = $defaultOptions; } $serializer = new XML_Serializer($options); $status = $serializer->serialize($values); // エラーチェック if (PEAR::isError($status)) { $xmlString = ''; } else { $xmlString = $serializer->getSerializedData(); } // return $body = $xmlString; $headers = array('X-BEAR-Output: XML' => 'Content-Type: application/xml'); $ro = BEAR::factory('BEAR_Ro'); $ro->setBody($body); $ro->setHeaders($headers); return $ro; }
/** * リンクされたbody配列を取得 * * @return array */ public function getLinkedBody() { $this->_doRequest(); $result = BEAR::dependency('BEAR_Ro_Prototype_Link', $this->_prototypeLink)->chainLink($this->_ro, $this->_chainLink); return $result; }