public function loadLocale($set_current = false)
 {
     $locale_path = $this->path . '/locale';
     $domain = ($this->app_id == 'webasyst' ? '' : $this->app_id . '_') . 'widget_' . $this->widget;
     if (file_exists($locale_path)) {
         waLocale::load(wa()->getLocale(), $locale_path, $domain, $set_current);
     }
 }
 /**
  * Trigger event with given $name from current active application.
  *
  * @param  string    $name        Event name.
  * @param  mixed     $params      Parameters passed to event handlers.
  * @param  string[]  $array_keys  Array of expected template items for UI events.
  * @return  array  app_id or plugin_id => data returned from handler (unless null is returned)
  */
 public function event($name, &$params = null, $array_keys = null)
 {
     $result = array();
     if (is_array($name)) {
         $event_app_id = $name[0];
         $event_system = self::getInstance($event_app_id);
         $name = $name[1];
     } else {
         $event_app_id = $this->getConfig()->getApplication();
         $event_system = $this;
     }
     $event_prefix = wa($event_app_id)->getConfig()->getPrefix();
     if (!isset(self::$handlers['apps'])) {
         self::$handlers['apps'] = array();
         $cache_file = $this->config->getPath('cache', 'config/handlers');
         if (!waSystemConfig::isDebug() && file_exists($cache_file)) {
             self::$handlers['apps'] = (include $cache_file);
         }
         if (!self::$handlers['apps'] || !is_array(self::$handlers['apps'])) {
             $apps = $this->getApps();
             $path = $this->getConfig()->getPath('apps');
             foreach ($apps as $app_id => $app_info) {
                 $files = waFiles::listdir($path . '/' . $app_id . '/lib/handlers/');
                 foreach ($files as $file) {
                     if (substr($file, -12) == '.handler.php') {
                         $file = explode('.', substr($file, 0, -12), 2);
                         self::$handlers['apps'][$file[0]][$file[1]][] = $app_id;
                     }
                 }
             }
             if (!waSystemConfig::isDebug()) {
                 waUtils::varExportToFile(self::$handlers['apps'], $cache_file);
             }
         }
     }
     if (!isset(self::$handlers['plugins'][$event_app_id])) {
         self::$handlers['plugins'][$event_app_id] = array();
         $plugins = $event_system->getConfig()->getPlugins();
         foreach ($plugins as $plugin_id => $plugin) {
             if (!empty($plugin['handlers'])) {
                 foreach ($plugin['handlers'] as $handler_event => $handler_method) {
                     self::$handlers['plugins'][$event_app_id][$handler_event][$plugin_id] = $handler_method;
                 }
             }
         }
     }
     if (isset(self::$handlers['apps'][$event_app_id][$name])) {
         $path = $this->getConfig()->getPath('apps');
         foreach (self::$handlers['apps'][$event_app_id][$name] as $app_id) {
             $file_path = $path . '/' . $app_id . '/lib/handlers/' . $event_prefix . "." . $name . ".handler.php";
             if (!file_exists($file_path)) {
                 continue;
             }
             wa($app_id);
             include_once $file_path;
             $class_name = $name;
             if (strpos($name, '.') !== false) {
                 $class_name = strtok($class_name, '.') . ucfirst(strtok(''));
             }
             $class_name = $app_id . ucfirst($event_prefix) . ucfirst($class_name) . "Handler";
             /**
              * @var $handler waEventHandler
              */
             $handler = new $class_name();
             try {
                 $r = $handler->execute($params);
                 if ($r !== null) {
                     $result[$app_id] = $r;
                 }
             } catch (Exception $e) {
                 waLog::log('Event handling error in ' . $file_path . ': ' . $e->getMessage());
             }
         }
     }
     if (isset(self::$handlers['plugins'][$event_app_id][$name])) {
         $plugins = $event_system->getConfig()->getPlugins();
         foreach (self::$handlers['plugins'][$event_app_id][$name] as $plugin_id => $method) {
             if (!isset($plugins[$plugin_id])) {
                 continue;
             }
             $plugin = $plugins[$plugin_id];
             self::pushActivePlugin($plugin_id, $event_prefix);
             $class_name = $event_app_id . ucfirst($plugin_id) . 'Plugin';
             try {
                 $class = new $class_name($plugin);
                 // Load plugin locale if it exists
                 $locale_path = $this->getAppPath('plugins/' . $plugin_id . '/locale', $event_app_id);
                 if (is_dir($locale_path)) {
                     waLocale::load($this->getLocale(), $locale_path, self::getActiveLocaleDomain(), false);
                 }
                 if (method_exists($class, $method) && null !== ($r = $class->{$method}($params))) {
                     if ($array_keys && is_array($r)) {
                         foreach ($array_keys as $k) {
                             if (!isset($r[$k])) {
                                 $r[$k] = '';
                             }
                         }
                     }
                     $result[$plugin_id . '-plugin'] = $r;
                 }
             } catch (Exception $e) {
                 waLog::log('Event handling error in ' . $class_name . '->' . $name . '(): ' . $e->getMessage());
             }
             self::popActivePlugin();
         }
     }
     return $result;
 }
 /**
  * Trigger event with given $name from current active application.
  * @param string $name
  * @param mixed $params passed to event handlers
  * @return array app_id or plugin_id => data returned from handler (unless null is returned)
  */
 public function event($name, &$params = null)
 {
     $result = array();
     if (is_array($name)) {
         $event_app_id = $name[0];
         $system = self::getInstance($event_app_id);
         $name = $name[1];
     } else {
         $event_app_id = $this->getConfig()->getApplication();
         $system = $this;
     }
     $prefix = wa($event_app_id)->getConfig()->getPrefix();
     //
     // Call handlers defined by applications
     //
     $apps = $this->getApps();
     $path = $this->getConfig()->getPath('apps');
     foreach ($apps as $app_id => $info) {
         $file_path = $path . "/" . $app_id . "/lib/handlers/" . $prefix . "." . $name . ".handler.php";
         if (file_exists($file_path)) {
             waSystem::getInstance($app_id);
             include_once $file_path;
             $class_name = $name;
             if (strpos($name, '.') !== false) {
                 $class_name = strtok($class_name, '.') . ucfirst(strtok(''));
             }
             $class_name = $app_id . ucfirst($prefix) . ucfirst($class_name) . "Handler";
             /**
              * @var $handler waEventHandler
              */
             $handler = new $class_name();
             try {
                 $r = $handler->execute($params);
                 if ($r !== null) {
                     $result[$app_id] = $r;
                 }
             } catch (Exception $e) {
                 waLog::log('Event handling error in ' . $file_path . ': ' . $e->getMessage());
             }
         }
     }
     //self::setActive($event_app_id);
     //
     // Call handlers defined by current application's plugins
     //
     $plugins = $system->getConfig()->getPlugins();
     foreach ($plugins as $plugin_id => $plugin) {
         foreach ($plugin['handlers'] as $handler_event => $handler_method) {
             if ($name == $handler_event) {
                 // Remember active plugin locale name for _wp() to work
                 self::pushActivePlugin($plugin_id, wa($event_app_id)->getConfig()->getPrefix());
                 $class_name = $event_app_id . ucfirst($plugin_id) . 'Plugin';
                 try {
                     $class = new $class_name($plugin);
                     // Load plugin locale if it exists
                     $locale_path = $this->getAppPath('plugins/' . $plugin_id . '/locale', $event_app_id);
                     if (is_dir($locale_path)) {
                         waLocale::load($this->getLocale(), $locale_path, self::getActiveLocaleDomain(), false);
                     }
                     if (method_exists($class, $handler_method) && null !== ($r = $class->{$handler_method}($params))) {
                         $result[$plugin_id . '-plugin'] = $r;
                     }
                 } catch (Exception $e) {
                     waLog::log('Event handling error in ' . $class_name . '->' . $handler_method . '(): ' . $e->getMessage());
                 }
                 self::popActivePlugin();
             }
         }
     }
     return $result;
 }
 /**
  * Returns information about all app's installed plugins as an associative array.
  *
  * @return array
  */
 public function getPlugins()
 {
     if ($this->plugins === null) {
         $locale = wa()->getLocale();
         $file = waConfig::get('wa_path_cache') . "/apps/" . $this->application . '/config/plugins.' . $locale . '.php';
         if (!file_exists($file) || SystemConfig::isDebug()) {
             waFiles::create(waConfig::get('wa_path_cache') . "/apps/" . $this->application . '/config');
             // read plugins from file wa-config/[APP_ID]/plugins.php
             $path = $this->getConfigPath('plugins.php', true);
             if (!file_exists($path)) {
                 $this->plugins = array();
                 return $this->plugins;
             }
             $all_plugins = (include $path);
             $this->plugins = array();
             foreach ($all_plugins as $plugin_id => $enabled) {
                 if ($enabled) {
                     $plugin_config = $this->getPluginPath($plugin_id) . "/lib/config/plugin.php";
                     if (!file_exists($plugin_config)) {
                         continue;
                     }
                     $plugin_info = (include $plugin_config);
                     waSystem::pushActivePlugin($plugin_id, $this->application);
                     // Load plugin locale if it exists
                     $locale_path = wa()->getAppPath('plugins/' . $plugin_id . '/locale', $this->application);
                     if (is_dir($locale_path)) {
                         waLocale::load($locale, $locale_path, wa()->getActiveLocaleDomain(), false);
                     }
                     $plugin_info['name'] = _wp($plugin_info['name']);
                     if (isset($plugin_info['title'])) {
                         $plugin_info['title'] = _wp($plugin_info['title']);
                     }
                     if (isset($plugin_info['description'])) {
                         $plugin_info['description'] = _wp($plugin_info['description']);
                     }
                     waSystem::popActivePlugin();
                     $plugin_info['id'] = $plugin_id;
                     $plugin_info['app_id'] = $this->application;
                     if (isset($plugin_info['img'])) {
                         $plugin_info['img'] = 'wa-apps/' . $this->application . '/plugins/' . $plugin_id . '/' . $plugin_info['img'];
                     }
                     if (isset($plugin_info['rights']) && $plugin_info['rights']) {
                         $plugin_info['handlers']['rights.config'] = 'rightsConfig';
                     }
                     if (isset($plugin_info['frontend']) && $plugin_info['frontend']) {
                         $plugin_info['handlers']['routing'] = 'routing';
                     }
                     if (!empty($plugin_info[$this->application . '_settings'])) {
                         $plugin_info['custom_settings'] = $plugin_info[$this->application . '_settings'];
                     }
                     $this->plugins[$plugin_id] = $plugin_info;
                 }
             }
             if (!SystemConfig::isDebug()) {
                 waUtils::varExportToFile($this->plugins, $file);
             } else {
                 waFiles::delete($file);
             }
         } else {
             $this->plugins = (include $file);
         }
     }
     return $this->plugins;
 }
 public function execute()
 {
     $wa_vars = array('$wa_url' => _w('URL of this Webasyst installation (relative)'), '$wa_app_url' => _w('URL of the current app settlement (relative)'), '$wa_backend_url' => _w('URL to access Webasyst backend (relative)'), '$wa_theme_url' => _w('URL of the current app design theme folder (relative)'), '$wa->title()' => _w('Title'), '$wa->title("<em>title</em>")' => _w('Assigns a new title'), '$wa->accountName()' => _w('Returns name of this Webasyst installation (name is specified in “Installer” app settings)'), '$wa->apps()' => _w('Returns this site’s core navigation menu which is either set automatically or manually in the “Site settings” screen'), '$wa->currentUrl(bool <em>$absolute</em>)' => _w('Returns current page URL (either absolute or relative)'), '$wa->domainUrl()' => _w('Returns this domain’s root URL (absolute)'), '$wa->globals("<em>key</em>")' => _w('Returns value of the global var by <em>key</em>. Global var array is initially empty, and can be used arbitrarily.'), '$wa->globals("<em>key</em>", "<em>value</em>")' => _w('Assigns global var a new value'), '$wa->get("<em>key</em>")' => _w('Returns GET parameter value (same as PHP $_GET["<em>key</em>"])'), '$wa->isMobile()' => _w('Based on current session data returns <em>true</em> or <em>false</em> if user is using a multi-touch mobile device; if no session var reflecting current website version (mobile or desktop) is available, User Agent information is used'), '$wa->locale()' => _w('Returns user locale, e.g. "en_US", "ru_RU". In case user is authorized, locale is retrieved from “Contacts” app user record, or detected automatically otherwise'), '$wa->post("<em>key</em>")' => _w('Returns POST parameter value (same as PHP $_POST["<em>key</em>"])'), '$wa->server("<em>key</em>")' => _w('Returns SERVER parameter value (same as PHP $_SERVER["KEY"])'), '$wa->session("<em>key</em>")' => _w('Returns SESSION var value (same as PHP $_SESSION["<em>key</em>"])'), '$wa->block("<em>id</em>")' => _w('Embeds HTML block by ID'), '$wa->user("<em>field</em>")' => _w('Returns authorized user data from associated record in “Contacts” app. "<em>field</em>" (string) is optional and indicates the field id to be returned. If not  Returns <em>false</em> if user is not authorized'), '$wa->userAgent("<em>key</em>")' => _w('Returns User Agent info by specified “<em>key</em>” parameter:') . '<br />' . _w('— <em>"platform"</em>: current visitor device platform name, e.g. <em>windows, mac, linux, ios, android, blackberry</em>;') . '<br />' . _w('— <em>"isMobile"</em>: returns <em>true</em> or <em>false</em> if user is using a multi-touch mobile device (iOS, Android and similar), based solely on User Agent string;'), '$wa-><em>APP_ID</em>->themePath("<em>theme_id</em>")' => _w('Returns path to theme folder by <em>theme_id</em> and <em>APP_ID</em>'));
     $app_id = waRequest::get('app');
     $file = waRequest::get('file');
     $vars = array();
     if ($app_id) {
         $app = wa()->getAppInfo($app_id);
         $path = $this->getConfig()->getAppsPath($app_id, 'lib/config/site.php');
         if (file_exists($path)) {
             $site = (include $path);
             if (isset($site['vars'])) {
                 if (isset($site['vars'][$file])) {
                     $vars += $site['vars'][$file];
                 }
                 if (isset($site['vars']['$wa'])) {
                     $vars += $site['vars']['$wa'];
                 }
                 if (isset($site['vars']['all'])) {
                     $vars += $site['vars']['all'];
                 }
             }
         }
         if ($app_id == 'site' && ($id = waRequest::get('id'))) {
             $page_model = new sitePageModel();
             $page = $page_model->getById($id);
             $file = $page['name'];
             $vars += $site['vars']['page.html'];
         }
     } else {
         $app = null;
     }
     $this->view->assign('vars', $vars);
     $this->view->assign('file', $file);
     $this->view->assign('app', $app);
     $this->view->assign('wa_vars', $wa_vars);
     $this->view->assign('smarty_vars', array('{$foo}' => _w('Displays a simple variable (non array/object)'), '{$foo[4]}' => _w('Displays the 5th element of a zero-indexed array'), '{$foo.bar}' => _w('Displays the "<em>bar</em>" key value of an array. Similar to PHP $foo["bar"]'), '{$foo.$bar}' => _w('Displays variable key value of an array. Similar to PHP $foo[$bar]'), '{$foo->bar}' => _w('Displays the object property named <em>bar</em>'), '{$foo->bar()}' => _w('Displays the return value of object method named <em>bar()</em>'), '{$foo|print_r}' => _w('Displays structured information about variable. Arrays and objects are explored recursively with values indented to show structure. Similar to PHP var_dump($foo)'), '{$foo|escape}' => _w('Escapes a variable for safe display in HTML'), '{$foo|wa_datetime:$format}' => _w('Outputs <em>$var</em> datetime in a user-friendly form. Supported <em>$format</em> values: <em>monthdate, date, dtime, datetime, fulldatetime, time, fulltime, humandate, humandatetime</em>'), '{$x+$y}' => _w('Outputs the sum of <em>$x</em> and <em>$y</em>'), '{$foo=3*4}' => _w('Assigns variable a value'), '{time()}' => _w('Direct PHP function access. E.g. <em>{time()}</em> displays the current timestamp'), '{literal}...{/literal}' => _w('Content between {literal} tags will not be parsed by Smarty'), '{include file="..."}' => _w('Embeds a Smarty template into the current content. <em>file</em> attribute specifies a template filename within the current design theme folder'), '{if}...{else}...{/if}' => _w('Similar to PHP if statements'), '{foreach from=$a key=k item=v}...{foreachelse}...{/foreach}' => _w('{foreach} is for looping over arrays of data')));
     $model = new siteBlockModel();
     $blocks = $model->order('sort')->fetchAll('id');
     $active_app = wa()->getApp();
     $apps = wa()->getApps();
     foreach ($apps as $app_id => $app) {
         $path = $this->getConfig()->getAppsPath($app_id, 'lib/config/site.php');
         if (file_exists($path)) {
             waLocale::load(wa()->getLocale(), $this->getConfig()->getAppsPath($app_id, 'locale'), $app_id, true);
             $site_config = (include $path);
             if (!empty($site_config['blocks'])) {
                 foreach ($site_config['blocks'] as $block_id => $block) {
                     if (!is_array($block)) {
                         $block = array('content' => $block, 'description' => '');
                     }
                     $block_id = $app_id . '.' . $block_id;
                     if (!isset($blocks[$block_id])) {
                         $block['id'] = $block_id;
                         $block['app'] = $app;
                         $blocks[$block_id] = $block;
                     }
                 }
             }
         }
     }
     wa()->setActive($active_app);
     $this->view->assign('blocks', $blocks);
 }
 /** Execute appropriate controller and return it's result.
  * Throw 404 exception if no controller found. */
 public function execute($plugin = null, $module = null, $action = null, $default = false)
 {
     if (!$this->system->getConfig()->checkRights($module, $action)) {
         throw new waRightsException(_ws("Access denied."));
     }
     // current app prefix
     $prefix = $this->system->getConfig()->getPrefix();
     // Load plugin locale and set plugin as active
     if ($plugin) {
         $plugin_path = $this->system->getAppPath('plugins/' . $plugin, $this->system->getApp());
         if (!file_exists($plugin_path . '/lib/config/plugin.php')) {
             $plugin = null;
         } else {
             $plugin_info = (include $plugin_path . '/lib/config/plugin.php');
             // check rights
             if (isset($plugin_info['rights']) && $plugin_info['rights']) {
                 if (!$this->system->getUser()->getRights($this->system->getConfig()->getApplication(), 'plugin.' . $plugin)) {
                     throw new waRightsException(_ws("Access denied"), 403);
                 }
             }
             waSystem::pushActivePlugin($plugin, $prefix);
             if (is_dir($plugin_path . '/locale')) {
                 waLocale::load($this->system->getLocale(), $plugin_path . '/locale', waSystem::getActiveLocaleDomain(), false);
             }
         }
     }
     // custom login and signup
     if (wa()->getEnv() == 'frontend') {
         if (!$plugin && !$action && $module == 'login') {
             $login_action = $this->system->getConfig()->getFactory('login_action');
             if ($login_action) {
                 $controller = $this->system->getDefaultController();
                 $controller->setAction($login_action);
                 $r = $controller->run();
                 return $r;
             }
         } elseif (!$plugin && !$action && $module == 'signup') {
             $signup_action = $this->system->getConfig()->getFactory('signup_action');
             if ($signup_action) {
                 $controller = $this->system->getDefaultController();
                 $controller->setAction($signup_action);
                 $r = $controller->run();
                 return $r;
             }
         }
     }
     //
     // Check possible ways to handle the request one by one
     //
     // list of failed class names (for debugging)
     $class_names = array();
     // Single Controller (recomended)
     $class_name = $prefix . ($plugin ? ucfirst($plugin) . 'Plugin' : '') . ucfirst($module) . ($action ? ucfirst($action) : '') . 'Controller';
     if (class_exists($class_name, true)) {
         /**
          * @var $controller waController
          */
         $controller = new $class_name();
         $r = $controller->run();
         if ($plugin) {
             waSystem::popActivePlugin();
         }
         return $r;
     }
     $class_names[] = $class_name;
     // Single Action
     $class_name = $prefix . ($plugin ? ucfirst($plugin) . 'Plugin' : '') . ucfirst($module) . ($action ? ucfirst($action) : '') . 'Action';
     if (class_exists($class_name)) {
         // get default view controller
         /**
          * @var $controller waDefaultViewController
          */
         $controller = $this->system->getDefaultController();
         $controller->setAction($class_name);
         $r = $controller->run();
         if ($plugin) {
             waSystem::popActivePlugin();
         }
         return $r;
     }
     $class_names[] = $class_name;
     // Controller Multi Actions, Zend/Symfony style
     $class_name = $prefix . ($plugin ? ucfirst($plugin) . 'Plugin' : '') . ucfirst($module) . 'Actions';
     if (class_exists($class_name, true)) {
         $controller = new $class_name();
         $r = $controller->run($action);
         if ($plugin) {
             waSystem::popActivePlugin();
         }
         return $r;
     }
     $class_names[] = $class_name;
     // Plugin is no longer active
     if ($plugin) {
         waSystem::popActivePlugin();
     }
     // Last chance: default action for this module
     if ($action && $default) {
         return $this->execute($plugin, $module);
     }
     // Too bad. 404.
     throw new waException(sprintf('Empty module and/or action after parsing the URL "%s" (%s/%s).<br />Not found classes: %s', $this->system->getConfig()->getCurrentUrl(), $module, $action, implode(', ', $class_names)), 404);
 }
 private static function __w($string, $type, $id, $path)
 {
     static $domains = array();
     $domain = sprintf('%s_%s', $type, $id);
     if (!isset($domains[$domain])) {
         $locale_path = $path . '/locale';
         if ($domains[$domain] = file_exists($locale_path)) {
             waLocale::load(waLocale::getLocale(), $locale_path, $domain, false);
         }
     }
     $args = (array) $string;
     if ($domains[$domain]) {
         array_unshift($args, $domain);
         $string = call_user_func_array('_wd', $args);
     } else {
         $string = reset($args);
     }
     return $string;
 }
 public function _w($string)
 {
     static $domains = array();
     $domain = sprintf('%s_%s', $this->type, $this->id);
     if (!isset($domains[$domain])) {
         $locale_path = $this->path . '/locale';
         if ($domains[$domain] = file_exists($locale_path)) {
             waLocale::load(waLocale::getLocale(), $locale_path, $domain, false);
         }
     }
     if ($domains[$domain]) {
         $args = func_get_args();
         array_unshift($args, $domain);
         $string = call_user_func_array('_wd', $args);
     }
     return $string;
 }