1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * X2WebApplication class file.
39: *
40: * X2WebApplication extends CWebApplication to provide additional functionality.
41: *
42: * @package application.modules.contacts
43: *
44: */
45: class X2WebApplication extends CWebApplication {
46:
47: /**
48: * Checks whether the named component has been created.
49: * @param string $id application component ID
50: * @return boolean whether the named application component has been created
51: */
52: public function componentCreated($id)
53: {
54: $components = $this->getComponents (true);
55: return isset($components[$id]);
56: }
57:
58: /**
59: * Creates a controller instance based on a route.
60: * Modified to check in /custom for controller files.
61: * See {@link CWebApplication::createController()} for details.
62: *
63: * @param string $route the route of the request.
64: * @param CWebModule $owner the module that the new controller will belong to. Defaults to null, meaning the application
65: * instance is the owner.
66: * @return array the controller instance and the action ID. Null if the controller class does not exist or the route is invalid.
67: */
68: public function createController($route,$owner=null)
69: {
70: if($owner===null)
71: $owner=$this;
72: if(($route=trim($route,'/'))==='')
73: $route=$owner->defaultController;
74: $caseSensitive=$this->getUrlManager()->caseSensitive;
75:
76: $route.='/';
77: while(($pos=strpos($route,'/'))!==false)
78: {
79: $id=substr($route,0,$pos);
80: if(!preg_match('/^\w+$/',$id))
81: return null;
82: if(!$caseSensitive)
83: $id=strtolower($id);
84: $route=(string)substr($route,$pos+1);
85: if(!isset($basePath)) // first segment
86: {
87: if(isset($owner->controllerMap[$id]))
88: {
89: return array(
90: Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner),
91: $this->parseActionParams($route),
92: );
93: }
94:
95: if(($module=$owner->getModule($id))!==null) {
96:
97: // fix module's base paths in case module was loaded from /custom
98: $module->basePath = Yii::resetCustomPath($module->basePath);
99: $module->viewPath = Yii::resetCustomPath($module->viewPath);
100: Yii::setPathOfAlias($module->getId(),$module->basePath);
101:
102: return $this->createController($route,$module);
103: }
104: $basePath=$owner->getControllerPath();
105: $controllerID='';
106: }
107: else
108: $controllerID.='/';
109: $className=ucfirst($id).'Controller';
110:
111: $classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';
112:
113: $extendedClassFile = Yii::getCustomPath($basePath.DIRECTORY_SEPARATOR.'My'.$className.'.php');
114:
115: if(is_file($extendedClassFile)) { // see if there's an extended controller in /custom
116: if(!class_exists($className,false))
117: require(Yii::getCustomPath($classFile)); // import the 'real' controller
118: $className = 'My'.$className; // add "My" to the class name
119: $classFile = $extendedClassFile;
120: } else {
121: $classFile = Yii::getCustomPath($classFile); // look in /custom for controller file
122: }
123:
124: if(is_file($classFile)) {
125: if(!class_exists($className,false))
126: require($classFile);
127: if(class_exists($className,false) && is_subclass_of($className,'CController'))
128: {
129: $id[0]=strtolower($id[0]);
130: return array(
131: new $className($controllerID.$id,$owner===$this?null:$owner),
132: $this->parseActionParams($route),
133: );
134: }
135: return null;
136: }
137: $controllerID.=$id;
138: $basePath.=DIRECTORY_SEPARATOR.$id;
139: }
140: }
141:
142: public function onEndRequest($event) {
143: if (!empty(Yii::$translationLog)) {
144: $username = '';
145: if (isset(Yii::$systemuser)) {
146: $username = Yii::$systemuser;
147: }
148: if (!is_dir(Yii::app()->basePath . '/messages/log_' . $username)) {
149: mkdir(Yii::app()->basePath . '/messages/log_' . $username);
150: }
151: foreach (Yii::$translationLog as $category => $messages) {
152: $fileName = Yii::app()->basePath . '/messages/log_' . $username . '/' . $category . '.php';
153: if (!is_file($fileName)) {
154: file_put_contents($fileName,
155: '<?php return ' . var_export(array(), true) . ";\n");
156: }
157: $messages = array_merge(require $fileName, $messages);
158: file_put_contents($fileName,
159: '<?php return ' . var_export($messages, true) . ";\n");
160: }
161: }
162: parent::onEndRequest($event);
163: }
164:
165: }
166: