Show
Ignore:
Timestamp:
09/30/08 00:31:41 (5 years ago)
Author:
sabel
Message:

update for Sabel

Location:
events/phpframework/sabel/trunk
Files:
19 modified

Legend:

Unmodified
Added
Removed
  • events/phpframework/sabel/trunk/Sabel/sabel/Functions.php

    r20063 r20238  
    134134{ 
    135135  Sabel_Logger::create()->write($message, $level, $identifier); 
    136 } 
    137  
    138 function uri($uriParameter, $secure = false, $absolute = false) 
    139 { 
    140   $context   = Sabel_Context::getContext(); 
    141   $uriPrefix = get_uri_prefix($secure, $absolute); 
    142    
    143   return $uriPrefix . "/" . $context->getCandidate()->uri($uriParameter); 
    144 } 
    145  
    146 function get_uri_prefix($secure = false, $absolute = false) 
    147 { 
    148   $uriPrefix = ""; 
    149    
    150   if ($secure || $absolute) { 
    151     $uriPrefix = (($secure) ? "https" : "http") . "://" . $_SERVER["SERVER_NAME"]; 
    152   } 
    153    
    154   if (defined("URI_IGNORE")) { 
    155     $uriPrefix .= $_SERVER["SCRIPT_NAME"]; 
    156   } elseif (defined("NO_REWRITE")) { 
    157     $uriPrefix .= "?" . NO_REWRITE_PREFIX . "="; 
    158   } 
    159    
    160   return $uriPrefix; 
    161136} 
    162137 
  • events/phpframework/sabel/trunk/Sabel/sabel/Response.php

    r20063 r20238  
    7575   
    7676  public function getLocation(); 
    77   public function setLocation($to, $host = null); 
     77  public function setLocation($location); 
    7878   
    7979  public function setResponse($name, $value); 
  • events/phpframework/sabel/trunk/addon/acl/Processor.php

    r20065 r20238  
    6868      $response->getStatus()->setCode(Sabel_Response::FORBIDDEN); 
    6969    } else { 
    70       $user->login_uri = $response->getRedirector()->to($authUri); 
     70      $user->login_uri = ltrim($response->getRedirector()->to($authUri), "/"); 
    7171    } 
    7272  } 
  • events/phpframework/sabel/trunk/addon/acl/User.php

    r20065 r20238  
    7373    } 
    7474     
    75     $backUri    = null; 
    76     $bus        = Sabel_Context::getContext()->getBus(); 
    77     $request    = $bus->get("request"); 
    78     $redirector = $bus->get("response")->getRedirector(); 
    79      
    80     if (is_object($request)) { 
    81       $backUri = $request->getValueWithMethod("back_uri"); 
    82     } 
    83      
    84     if ($backUri === null) { 
    85       $redirector->to($redirectTo); 
    86     } else { 
    87       l("ACL: back to the page before authentication.", SBL_LOG_DEBUG); 
    88       $redirector->uri($backUri); 
     75    if ($response = Sabel_Context::getResponse()) { 
     76      $backUri = null; 
     77      if ($request = Sabel_Context::getRequest()) { 
     78        $backUri = $request->getValueWithMethod("back_uri"); 
     79      } 
     80       
     81      if ($backUri === null) { 
     82        $response->getRedirector()->to($redirectTo); 
     83      } else { 
     84        l("ACL: back to the page before authentication.", SBL_LOG_DEBUG); 
     85        $response->getRedirector()->uri($backUri); 
     86      } 
    8987    } 
    9088     
  • events/phpframework/sabel/trunk/app/helpers/application.php

    r20065 r20238  
    11<?php 
     2 
     3function h($string, $charset = null) 
     4{ 
     5  return htmlescape($string, $charset); 
     6} 
    27 
    38function a($uri, $anchor, $uriQuery = "") 
     
    1520} 
    1621 
     22/** 
     23 * create uri for css, image, js, etc... 
     24 */ 
    1725function linkto($file) 
    1826{ 
    19   if (defined("URI_IGNORE")) { 
    20     return dirname($_SERVER["SCRIPT_NAME"]) . "/" . $file; 
    21   } else { 
    22     return "/" . $file; 
     27  if ($bus = Sabel_Context::getContext()->getBus()) { 
     28    if ($bus->get("NO_VIRTUAL_HOST")) { 
     29      return dirname($_SERVER["SCRIPT_NAME"]) . "/" . $file; 
     30    } 
    2331  } 
     32   
     33  return "/" . $file; 
    2434} 
    2535 
    26 function h($string, $charset = null) 
     36function get_uri_prefix($secure = false, $absolute = false) 
    2737{ 
    28   return htmlescape($string, $charset); 
     38  $prefix = ""; 
     39   
     40  if ($secure || $absolute) { 
     41    $server = (isset($_SERVER["SERVER_NAME"])) ? $_SERVER["SERVER_NAME"] : "localhost"; 
     42    $prefix = (($secure) ? "https" : "http") . "://" . $server; 
     43  } 
     44   
     45  if ($bus = Sabel_Context::getContext()->getBus()) { 
     46    if ($bus->get("NO_VIRTUAL_HOST") && isset($_SERVER["SCRIPT_NAME"])) { 
     47      $prefix .= $_SERVER["SCRIPT_NAME"]; 
     48    } 
     49     
     50    if ($bus->get("NO_REWRITE_MODULE") && defined("NO_REWRITE_PREFIX")) { 
     51      $prefix .= "?" . NO_REWRITE_PREFIX . "="; 
     52    } 
     53  } 
     54   
     55  return $prefix; 
    2956} 
    3057 
    31 function mb_trim($string) 
     58/** 
     59 * create uri 
     60 */ 
     61function uri($param, $secure = false, $absolute = false) 
    3262{ 
    33   $string = new Sabel_Util_String($string); 
    34   return $string->trim()->toString(); 
     63  $context = Sabel_Context::getContext(); 
     64  $prefix  = get_uri_prefix($secure, $absolute); 
     65   
     66  return $prefix . "/" . $context->getCandidate()->uri($param); 
    3567} 
    3668 
     69/* 
    3770function to_date($date, $format) 
    3871{ 
    3972  return Helpers_Date::format($date, constant("Helpers_Date::" . $format)); 
    40 } 
    41  
    42 function datetime($date, $format = "m/d H:i") 
    43 { 
    44   return date($format, strtotime($date)); 
    4573} 
    4674 
     
    5078  $requester->values($values)->withLayout($withLayout); 
    5179  return $requester->request($uri)->getResult(); 
     80} 
     81*/ 
     82 
     83function datetime($date, $format = "m/d H:i") 
     84{ 
     85  return date($format, strtotime($date)); 
     86} 
     87 
     88function mb_trim($string) 
     89{ 
     90  $string = new Sabel_Util_String($string); 
     91  return $string->trim()->toString(); 
    5292} 
    5393 
  • events/phpframework/sabel/trunk/app/helpers/paginator/Status.php

    r20153 r20238  
    4949    return $paginator->build($limit, $params); 
    5050  } 
     51   
     52  public function getPublicStatuses($params, $limit = null) 
     53  { 
     54    if ($limit === null) { 
     55      $limit = self::DEFAULT_ITEM_LIMIT; 
     56    } 
     57     
     58    $join = new Sabel_Db_Join("Status"); 
     59    $paginator = new Paginator($join->add("User")); 
     60    $paginator->setCondition("User.private_flag", false); 
     61    $paginator->setDefaultOrder("Status.created_at", "desc"); 
     62     
     63    return $paginator->build($limit, $params); 
     64  } 
    5165} 
  • events/phpframework/sabel/trunk/app/index/controllers/Index.php

    r20153 r20238  
    1616                   . 'href="http://' . $_SERVER["SERVER_NAME"] . uri("a: rss") . '">'; 
    1717     
    18     $this->paginator = $this->getPublicTimeline($this->GET_VARS); 
     18    $helper = new Helpers_Paginator_Status(); 
     19    $this->paginator = $helper->getPublicStatuses($this->GET_VARS); 
    1920     
    2021    if ($this->aclUser->isAuthenticated()) { 
     
    3233    $rss = new Sabel_Rss_Writer(); 
    3334    $rss->setInfo(array("title" => "Phwittr public timeline", "language" => "ja")); 
    34     $paginator = $this->getPublicTimeline(); 
     35     
     36    $helper = new Helpers_Paginator_Status(); 
     37    $paginator = $helper->getPublicStatuses(array()); 
    3538     
    3639    if ($paginator->results) { 
     
    7073     
    7174    if ($isSelf) { 
    72       $this->assignUserSubmenu($aUser->id); 
     75      $this->assignUserSubmenu(); 
    7376    } else { 
    7477      $this->assignOthersSubmenu($aUser->id, $aUser->name); 
    7578    } 
    7679  } 
    77    
    78   protected function getPublicTimeline($params = array()) 
    79   { 
    80     $join = new Sabel_Db_Join("Status"); 
    81     $paginator = new Paginator($join->add("User")); 
    82     $paginator->setCondition("User.private_flag", false); 
    83     $paginator->setDefaultOrder("Status.created_at", "desc"); 
    84      
    85     return $paginator->build(20, $params); 
    86   } 
    8780} 
  • events/phpframework/sabel/trunk/app/index/controllers/Users.php

    r20153 r20238  
    3636    } 
    3737     
    38     if ($protected && $authenticated) { 
    39       $this->isRequested = Request::isRequested($this->aclUser->id, $this->aUser->id); 
     38    if ($protected) { 
     39      if ($authenticated) { 
     40        $this->isRequested = Request::isRequested($this->aclUser->id, $this->aUser->id); 
     41      } 
     42      $this->view->setName("protected"); 
    4043    } else { 
    4144      $helper = new Helpers_Paginator_Status(); 
    4245      $this->paginator = $helper->getStatuses($this->aUser->id, $this->GET_VARS, 50); 
    43     } 
    44      
    45     if ($protected) { 
    46       $this->view->setName("protected"); 
    47     } else { 
    4846      $this->view->setName("home"); 
    4947    } 
  • events/phpframework/sabel/trunk/app/models/Follower.php

    r20153 r20238  
    33class Follower extends Sabel_Db_Model 
    44{ 
    5   public static function getFriends($userId, $limit = 100) 
     5  public static function getFriends($userId, $limit) 
    66  { 
    77    $join = new Sabel_Db_Join(new self()); 
     
    2020    $stmt->projection(array("follow_id")) 
    2121         ->where("WHERE user_id = @user_id@") 
    22          ->constraints(array("order" => array("created_at" => "ASC"))) 
     22         ->constraints(array("order" => array("created_at" => "asc"))) 
    2323         ->setBindValue("user_id", $userId); 
    2424     
  • events/phpframework/sabel/trunk/lib/Paginator.php

    r20153 r20238  
    175175      $attributes["results"] = $model->{$this->method}(); 
    176176       
    177       if ($this->uri === null) { 
    178         if ($request = Sabel_Context::getContext()->getBus()->get("request")) { 
    179           $attributes["uri"] = get_uri_prefix() . "/" . $request->getUri(); 
    180         } 
     177      if ($this->uri === null && $request = Sabel_Context::getRequest()) { 
     178        $attributes["uri"] = get_uri_prefix() . "/" . $request->getUri(); 
    181179      } 
    182180    } 
  • events/phpframework/sabel/trunk/lib/processor/Action.php

    r20073 r20238  
    2424    $controller->setAction($action); 
    2525     
    26     if ($isAjax = $bus->get("isAjaxRequest")) { 
     26    if ($isAjax = $bus->get("AJAX_REQUEST")) { 
    2727      $controller->setAttribute("ajax", new AjaxResult()); 
    2828    } 
     
    6060     
    6161    if ($controller->getAttribute("layout") === false) { 
    62       $bus->set("noLayout", true); 
     62      $bus->set("NO_LAYOUT", true); 
    6363    } 
    6464  } 
  • events/phpframework/sabel/trunk/lib/processor/Request.php

    r20065 r20238  
    1717      $request = $bus->get("request"); 
    1818    } else { 
    19       $uri = ""; 
    20       if (isset($_SERVER["REQUEST_URI"])) { 
    21         l("REQUEST URI: " . $_SERVER["REQUEST_URI"]); 
    22         $uri = normalize_uri($_SERVER["REQUEST_URI"]); 
    23       } 
    24        
     19      $uri = $this->getRequestUri($bus); 
    2520      $request = new Sabel_Request_Object($uri); 
    2621       
     
    4843    } 
    4944     
     45    l("REQUEST URI: /" . $request->getUri(true)); 
     46     
     47    // ajax request. 
    5048    if ($request->getHttpHeader("X-Requested-With") === "XMLHttpRequest") { 
    51       $bus->set("noLayout",      true); 
    52       $bus->set("isAjaxRequest", true); 
     49      $bus->set("NO_LAYOUT", true); 
     50      $bus->set("AJAX_REQUEST", true); 
    5351    } 
    5452  } 
     53   
     54  protected function getRequestUri($bus) 
     55  { 
     56    $uri = (isset($_SERVER["REQUEST_URI"])) ? $_SERVER["REQUEST_URI"] : "/"; 
     57     
     58    if (isset($_SERVER["SCRIPT_NAME"]) && strpos($_SERVER["SCRIPT_NAME"], "/index.php") >= 1) { 
     59      $uri = substr($uri, strlen($_SERVER["SCRIPT_NAME"])); 
     60      $bus->set("NO_VIRTUAL_HOST", true); 
     61    } 
     62     
     63    if (defined("NO_REWRITE_PREFIX") && isset($_GET[NO_REWRITE_PREFIX])) { 
     64      $uri = substr($uri, strlen(NO_REWRITE_PREFIX) + 2); 
     65      $parsed = parse_url($uri); 
     66      if (isset($parsed["query"])) { 
     67        parse_str($parsed["query"], $_GET); 
     68      } 
     69       
     70      unset($_GET[NO_REWRITE_PREFIX]); 
     71      $bus->set("NO_REWRITE_MODULE", true); 
     72    } 
     73     
     74    return normalize_uri($uri); 
     75  } 
    5576} 
  • events/phpframework/sabel/trunk/lib/processor/Response.php

    r20067 r20238  
    5656        $response->setLocation($url); 
    5757      } else { 
    58         $session   = $bus->get("session"); 
    5958        $token     = $bus->get("request")->getValueWithMethod("token"); 
    6059        $hasToken  = !empty($token); 
    6160        $hasParams = $redirector->hasParameters(); 
     61        $location  = $redirector->getUri(); 
    6262         
    63         if (!$hasToken) { 
    64           $to = $redirector->getUri(); 
    65         } elseif ($hasParams) { 
    66           $to = $redirector->getUri() . "&token={$token}"; 
    67         } else { 
    68           $to = $redirector->getUri() . "?token={$token}"; 
     63        if ($hasToken) { 
     64          $glue = ($hasParams) ? "&" : "?"; 
     65          $location .= $glue . "token={$token}"; 
    6966        } 
    7067         
     68        $session = $bus->get("session"); 
    7169        if ($session->isStarted() && !$session->isCookieEnabled()) { 
    7270          $glue = ($hasToken || $hasParams) ? "&" : "?"; 
    73           $to .= $glue . $session->getName() . "=" . $session->getId(); 
     71          $location .= $glue . $session->getName() . "=" . $session->getId(); 
    7472        } 
    7573         
    76         $response->setLocation($to, $_SERVER["SERVER_NAME"]); 
     74        if (function_exists("get_uri_prefix")) { 
     75          $location = get_uri_prefix() . "/" . ltrim($location, "/"); 
     76        } 
     77         
     78        $response->setLocation($location); 
    7779      } 
    7880    } 
  • events/phpframework/sabel/trunk/lib/processor/View.php

    r20065 r20238  
    2323    list ($m, $c, $a) = $bus->get("destination")->toArray(); 
    2424     
    25     $controller = new Sabel_View_Location_File($m . DS . VIEW_DIR_NAME . DS . $c . DS); 
    26     $view = new Sabel_View_Object("controller", $controller); 
     25    $view = new Sabel_View_Object("controller", new Sabel_View_Location_File( 
     26      $m . DS . VIEW_DIR_NAME . DS . $c . DS 
     27    )); 
    2728     
    28     $module = new Sabel_View_Location_File($m . DS . VIEW_DIR_NAME . DS); 
    29     $view->addLocation("module", $module); 
    30      
    31     $app = new Sabel_View_Location_File(VIEW_DIR_NAME . DS); 
    32     $view->addLocation("app", $app); 
     29    $view->addLocation("module", new Sabel_View_Location_File($m . DS . VIEW_DIR_NAME . DS)); 
     30    $view->addLocation("app", new Sabel_View_Location_File(VIEW_DIR_NAME . DS)); 
    3331     
    3432    if ($renderer = $bus->get("renderer")) { 
     
    3937     
    4038    $this->view = $view; 
    41      
    4239    $bus->set("view", $view); 
    4340    $bus->get("controller")->setAttribute("view", $view); 
     
    5249    $responses  = $response->getResponses(); 
    5350    $contents   = (isset($responses["contents"])) ? $responses["contents"] : ""; 
    54     $isAjax     = $bus->get("isAjaxRequest"); 
    5551     
     52    $isAjax = $bus->get("AJAX_REQUEST"); 
     53     
     54    // @todo... 
    5655    if ($isAjax && isset($responses["ajax"])) { 
    5756      return $bus->set("result", $responses["ajax"]->toString()); 
     
    5958     
    6059    $view = $this->getView($response->getStatus(), $bus->get("destination")->getAction(), $isAjax); 
    61      
    62     if (isset($responses["renderText"]) && $responses["renderText"]) { 
    63       $renderer = $view->getRenderer(); 
    64       return $bus->set("result", $renderer->rendering($contents, $responses)); 
    65     } 
    6660     
    6761    if ($contents === "") { 
     
    7872    } 
    7973     
    80     if ($bus->get("noLayout")) { 
     74    if ($bus->get("NO_LAYOUT")) { 
    8175      $bus->set("result", $contents); 
    8276    } else { 
  • events/phpframework/sabel/trunk/public/index.php

    r20065 r20238  
    1515} 
    1616 
    17 if (strpos($_SERVER["SCRIPT_NAME"], "/index.php") >= 1) { 
    18   $ignore = str_replace($_SERVER["SCRIPT_NAME"], "", $_SERVER["REQUEST_URI"]); 
    19   $_SERVER["REQUEST_URI"] = ltrim($ignore, "/"); 
    20   define("URI_IGNORE", $ignore); 
    21 } elseif (isset($_GET[NO_REWRITE_PREFIX])) { 
    22   $_uri = substr(ltrim($_SERVER["REQUEST_URI"], "/"), strlen(NO_REWRITE_PREFIX) + 2); 
    23   $_SERVER["REQUEST_URI"] = $_uri; 
    24   $parsed = parse_url($_SERVER["REQUEST_URI"]); 
    25   if (isset($parsed["query"])) { 
    26     parse_str($parsed["query"], $_GET); 
    27   } 
    28    
    29   unset($_GET[NO_REWRITE_PREFIX]); 
    30   define("NO_REWRITE", true); 
    31 } 
    32  
    3317if ((ENVIRONMENT & PRODUCTION) > 0) { 
    3418  Sabel::init(); 
  • events/phpframework/sabel/trunk/sabel.php

    r20065 r20238  
    1616$_SERVER["HTTP_HOST"]   = "localhost"; 
    1717$_SERVER["SERVER_NAME"] = "localhost"; 
     18$_SERVER["REQUEST_URI"] = "/"; 
    1819 
    1920if (isset($_SERVER["argv"][2])) { 
     
    3435    } 
    3536  } 
    36 } else { 
    37   $_SERVER["REQUEST_URI"] = "/"; 
    3837} 
    3938 
  • events/phpframework/sabel/trunk/tests/fixture/test/Status.php

    r20153 r20238  
    4545      )); 
    4646    } 
     47     
     48    for ($i = 1; $i < 10; $i++) { 
     49      $this->insert(array( 
     50        "user_id"       => $user2->id, 
     51        "comment"       => "comment{$i}", 
     52        "created_at"    => "2008-01-0{$i} 23:59:59", 
     53      )); 
     54    } 
    4755  } 
    4856   
  • events/phpframework/sabel/trunk/tests/unit/models/Follower.php

    r20073 r20238  
    1212  { 
    1313    $user2 = $this->getUser("test2"); 
    14     $friends = Follower::getFriends($user2->id); 
     14    $friends = Follower::getFriends($user2->id, 100); 
    1515     
    1616    $this->eq(2, count($friends)); 
  • events/phpframework/sabel/trunk/tests/unit/paginators/Status.php

    r20153 r20238  
    130130    $this->eq("2008-01-01 18:00:00", $items[3]->created_at); 
    131131  } 
     132   
     133  /** 
     134   * @test 
     135   */ 
     136  public function getPublicStatuses() 
     137  { 
     138    $user1 = $this->getUser("test1"); 
     139    $user2 = $this->getUser("test2"); 
     140    $user3 = $this->getUser("test3"); 
     141     
     142    $helper = new Helpers_Paginator_Status(); 
     143     
     144    $paginator = $helper->getPublicStatuses(array(), 5); 
     145    $this->eq(36, $paginator->count); 
     146     
     147    $user2->private_flag = false; 
     148    $user2->save(); 
     149     
     150    $paginator = $helper->getPublicStatuses(array(), 5); 
     151    $this->eq(45, $paginator->count); 
     152     
     153    $user2->private_flag = true; 
     154    $user2->save(); 
     155  } 
    132156}