Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • DeleteBlock
  • DeleteOperation
  • EditBlock
  • Hooks
  • ManageBlock
  • Model
  • Module
  • SaveOperation
  • ServerName
  • Site
  • StatusOperation
  1 <?php
  2 
  3 /*
  4  * This file is part of the Icybee package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace Icybee\Modules\Sites;
 13 
 14 use ICanBoogie\ActiveRecord\ActiveRecordException;
 15 use Icybee\Modules\Users\User;
 16 use ICanBoogie\HTTP\Request;
 17 
 18 /**
 19  * Models for Sites.
 20  */
 21 class Model extends \ICanBoogie\ActiveRecord\Model
 22 {
 23     /**
 24      * Makes sure that if defined the `path` property starts with a slash '/' but doesn't end
 25      * with one.
 26      *
 27      * Sets the `created_at` and `updated_at` properties if they are not defined.
 28      */
 29     public function save(array $properties, $key=null, array $options=array())
 30     {
 31         if (isset($properties['path']))
 32         {
 33             $path = trim($properties['path'], '/');
 34 
 35             if ($path)
 36             {
 37                 $path = '/' . $path;
 38             }
 39 
 40             $properties['path'] = $path;
 41         }
 42 
 43         if (!$key && empty($properties['created_at']))
 44         {
 45             $properties['created_at'] = gmdate('Y-m-d H:i:s');
 46         }
 47 
 48         if (empty($properties['updated_at']))
 49         {
 50             $properties['updated_at'] = gmdate('Y-m-d H:i:s');
 51         }
 52 
 53         return parent::save($properties, $key, $options);
 54     }
 55 
 56     static private $cached_sites;
 57 
 58     /**
 59      * Finds a site using a request.
 60      *
 61      * If there is no site record defined a default site record is returned.
 62      *
 63      * @param Request $request
 64      * @param User $user
 65      *
 66      * @throws \Exception\DatabaseConnection if the connection to the database where site records
 67      * are stored could not be established.
 68      *
 69      * @return Site
 70      */
 71     static public function find_by_request(Request $request, User $user=null)
 72     {
 73         global $core;
 74 
 75         $sites = self::$cached_sites;
 76 
 77         if ($sites === null)
 78         {
 79             $sites = $core->vars['cached_sites'];
 80         }
 81 
 82         if (!$sites)
 83         {
 84             self::$cached_sites = array();
 85 
 86             try
 87             {
 88                 self::$cached_sites = $sites = $core->models['sites']->all;
 89 
 90                 $core->vars['cached_sites'] = $sites;
 91             }
 92             catch (ActiveRecordException $e)
 93             {
 94                 throw $e;
 95             }
 96             catch (\Exception $e)
 97             {
 98                 return self::get_default_site();
 99             }
100         }
101 
102         $path = $request->path;
103         $parts = array_reverse(explode('.', $request->headers['Host']));
104 
105         $tld = null;
106         $domain = null;
107         $subdomain = null;
108 
109         if (isset($parts[0]))
110         {
111             $tld = $parts[0];
112         }
113 
114         if (isset($parts[1]))
115         {
116             $domain = $parts[1];
117         }
118 
119         if (isset($parts[2]))
120         {
121             $subdomain = implode('.', array_slice($parts, 2));
122         }
123 
124         $match = null;
125         $match_score = -1;
126 
127         foreach ($sites as $site)
128         {
129             $score = 0;
130 
131             #
132             # guest users don't have access to sites that are not online.
133             #
134 
135             if ($site->status != Site::STATUS_OK && $user && $user->is_guest)
136             {
137                 continue;
138             }
139 
140             if ($site->tld)
141             {
142                 $score += ($site->tld == $tld) ? 1000 : -1000;
143             }
144 
145             if ($site->domain)
146             {
147                 $score += ($site->domain == $domain) ? 100 : -100;
148             }
149 
150             if ($site->subdomain)
151             {
152                 $score += ($site->subdomain == $subdomain || (!$site->subdomain && $subdomain == 'www')) ? 10 : -10;
153             }
154 
155             $site_path = $site->path;
156 
157             if ($site_path)
158             {
159                 $score += ($path == $site_path || preg_match('#^' . $site_path . '/#', $path)) ? 1 : -1;
160             }
161             else if ($path == '/')
162             {
163                 $score += 1;
164             }
165 
166             if ($score > $match_score)
167             {
168                 $match = $site;
169                 $match_score = $score;
170             }
171         }
172 
173         return $match ? $match : self::get_default_site();
174     }
175 
176     static private $default_site;
177 
178     /**
179      * Returns a default site active record.
180      *
181      * @return Site
182      */
183     static private function get_default_site()
184     {
185         global $core;
186 
187         if (self::$default_site === null)
188         {
189             self::$default_site = Site::from
190             (
191                 array
192                 (
193                     'title' => 'Undefined',
194                     'language' => $core->language,
195                     'timezone' => $core->timezone,
196                     'status' => Site::STATUS_OK
197                 )
198             );
199         }
200 
201         return self::$default_site;
202     }
203 }
Autodoc API documentation generated by ApiGen 2.8.0