GiiModule.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * GiiModule class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('system.gii.CCodeGenerator');
  11. Yii::import('system.gii.CCodeModel');
  12. Yii::import('system.gii.CCodeFile');
  13. Yii::import('system.gii.CCodeForm');
  14. /**
  15. * GiiModule is a module that provides Web-based code generation capabilities.
  16. *
  17. * To use GiiModule, you must include it as a module in the application configuration like the following:
  18. * <pre>
  19. * return array(
  20. * ......
  21. * 'modules'=>array(
  22. * 'gii'=>array(
  23. * 'class'=>'system.gii.GiiModule',
  24. * 'password'=>***choose a password***
  25. * ),
  26. * ),
  27. * )
  28. * </pre>
  29. *
  30. * Because GiiModule generates new code files on the server, you should only use it on your own
  31. * development machine. To prevent other people from using this module, it is required that
  32. * you specify a secret password in the configuration. Later when you access
  33. * the module via browser, you will be prompted to enter the correct password.
  34. *
  35. * By default, GiiModule can only be accessed by localhost. You may configure its {@link ipFilters}
  36. * property if you want to make it accessible on other machines.
  37. *
  38. * With the above configuration, you will be able to access GiiModule in your browser using
  39. * the following URL:
  40. *
  41. * http://localhost/path/to/index.php?r=gii
  42. *
  43. * If your application is using path-format URLs with some customized URL rules, you may need to add
  44. * the following URLs in your application configuration in order to access GiiModule:
  45. * <pre>
  46. * 'components'=>array(
  47. * 'urlManager'=>array(
  48. * 'urlFormat'=>'path',
  49. * 'rules'=>array(
  50. * 'gii'=>'gii',
  51. * 'gii/<controller:\w+>'=>'gii/<controller>',
  52. * 'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
  53. * ...other rules...
  54. * ),
  55. * )
  56. * )
  57. * </pre>
  58. *
  59. * You can then access GiiModule via:
  60. *
  61. * http://localhost/path/to/index.php/gii
  62. *
  63. * @property string $assetsUrl The base URL that contains all published asset files of gii.
  64. *
  65. * @author Qiang Xue <qiang.xue@gmail.com>
  66. * @package system.gii
  67. * @since 1.1.2
  68. */
  69. class GiiModule extends CWebModule
  70. {
  71. /**
  72. * @var string the password that can be used to access GiiModule.
  73. * If this property is set false, then GiiModule can be accessed without password
  74. * (DO NOT DO THIS UNLESS YOU KNOW THE CONSEQUENCE!!!)
  75. */
  76. public $password;
  77. /**
  78. * @var array the IP filters that specify which IP addresses are allowed to access GiiModule.
  79. * Each array element represents a single filter. A filter can be either an IP address
  80. * or an address with wildcard (e.g. 192.168.0.*) to represent a network segment.
  81. * If you want to allow all IPs to access gii, you may set this property to be false
  82. * (DO NOT DO THIS UNLESS YOU KNOW THE CONSEQUENCE!!!)
  83. * The default value is array('127.0.0.1', '::1'), which means GiiModule can only be accessed
  84. * on the localhost.
  85. */
  86. public $ipFilters=array('127.0.0.1','::1');
  87. /**
  88. * @var array a list of path aliases that refer to the directories containing code generators.
  89. * The directory referred by a single path alias may contain multiple code generators, each stored
  90. * under a sub-directory whose name is the generator name.
  91. * Defaults to array('application.gii').
  92. */
  93. public $generatorPaths=array('application.gii');
  94. /**
  95. * @var integer the permission to be set for newly generated code files.
  96. * This value will be used by PHP chmod function.
  97. * Defaults to 0666, meaning the file is read-writable by all users.
  98. */
  99. public $newFileMode=0666;
  100. /**
  101. * @var integer the permission to be set for newly generated directories.
  102. * This value will be used by PHP chmod function.
  103. * Defaults to 0777, meaning the directory can be read, written and executed by all users.
  104. */
  105. public $newDirMode=0777;
  106. private $_assetsUrl;
  107. /**
  108. * Initializes the gii module.
  109. */
  110. public function init()
  111. {
  112. parent::init();
  113. Yii::setPathOfAlias('gii',dirname(__FILE__));
  114. Yii::app()->setComponents(array(
  115. 'errorHandler'=>array(
  116. 'class'=>'CErrorHandler',
  117. 'errorAction'=>$this->getId().'/default/error',
  118. ),
  119. 'user'=>array(
  120. 'class'=>'CWebUser',
  121. 'stateKeyPrefix'=>'gii',
  122. 'loginUrl'=>Yii::app()->createUrl($this->getId().'/default/login'),
  123. ),
  124. 'widgetFactory' => array(
  125. 'class'=>'CWidgetFactory',
  126. 'widgets' => array()
  127. )
  128. ), false);
  129. $this->generatorPaths[]='gii.generators';
  130. $this->controllerMap=$this->findGenerators();
  131. }
  132. /**
  133. * @return string the base URL that contains all published asset files of gii.
  134. */
  135. public function getAssetsUrl()
  136. {
  137. if($this->_assetsUrl===null)
  138. $this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('gii.assets'));
  139. return $this->_assetsUrl;
  140. }
  141. /**
  142. * @param string $value the base URL that contains all published asset files of gii.
  143. */
  144. public function setAssetsUrl($value)
  145. {
  146. $this->_assetsUrl=$value;
  147. }
  148. /**
  149. * Performs access check to gii.
  150. * This method will check to see if user IP and password are correct if they attempt
  151. * to access actions other than "default/login" and "default/error".
  152. * @param CController $controller the controller to be accessed.
  153. * @param CAction $action the action to be accessed.
  154. * @throws CHttpException if access denied
  155. * @return boolean whether the action should be executed.
  156. */
  157. public function beforeControllerAction($controller, $action)
  158. {
  159. if(parent::beforeControllerAction($controller, $action))
  160. {
  161. $route=$controller->id.'/'.$action->id;
  162. if(!$this->allowIp(Yii::app()->request->userHostAddress) && $route!=='default/error')
  163. throw new CHttpException(403,"You are not allowed to access this page.");
  164. $publicPages=array(
  165. 'default/login',
  166. 'default/error',
  167. );
  168. if($this->password!==false && Yii::app()->user->isGuest && !in_array($route,$publicPages))
  169. Yii::app()->user->loginRequired();
  170. else
  171. return true;
  172. }
  173. return false;
  174. }
  175. /**
  176. * Checks to see if the user IP is allowed by {@link ipFilters}.
  177. * @param string $ip the user IP
  178. * @return boolean whether the user IP is allowed by {@link ipFilters}.
  179. */
  180. protected function allowIp($ip)
  181. {
  182. if(empty($this->ipFilters))
  183. return true;
  184. foreach($this->ipFilters as $filter)
  185. {
  186. if($filter==='*' || $filter===$ip || (($pos=strpos($filter,'*'))!==false && !strncmp($ip,$filter,$pos)))
  187. return true;
  188. }
  189. return false;
  190. }
  191. /**
  192. * Finds all available code generators and their code templates.
  193. * @return array
  194. */
  195. protected function findGenerators()
  196. {
  197. $generators=array();
  198. $n=count($this->generatorPaths);
  199. for($i=$n-1;$i>=0;--$i)
  200. {
  201. $alias=$this->generatorPaths[$i];
  202. $path=Yii::getPathOfAlias($alias);
  203. if($path===false || !is_dir($path))
  204. continue;
  205. $names=scandir($path);
  206. foreach($names as $name)
  207. {
  208. if($name[0]!=='.' && is_dir($path.'/'.$name))
  209. {
  210. $className=ucfirst($name).'Generator';
  211. if(is_file("$path/$name/$className.php"))
  212. {
  213. $generators[$name]=array(
  214. 'class'=>"$alias.$name.$className",
  215. );
  216. }
  217. if(isset($generators[$name]) && is_dir("$path/$name/templates"))
  218. {
  219. $templatePath="$path/$name/templates";
  220. $dirs=scandir($templatePath);
  221. foreach($dirs as $dir)
  222. {
  223. if($dir[0]!=='.' && is_dir($templatePath.'/'.$dir))
  224. $generators[$name]['templates'][$dir]=strtr($templatePath.'/'.$dir,array('/'=>DIRECTORY_SEPARATOR,'\\'=>DIRECTORY_SEPARATOR));
  225. }
  226. }
  227. }
  228. }
  229. }
  230. return $generators;
  231. }
  232. }