PageRenderTime 40ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/basics.php

https://github.com/hashing/basercms
PHP | 788 lines | 564 code | 52 blank | 172 comment | 103 complexity | e96b59b7eac027c39ec3f74774bcd883 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * baserCMS共通関数
  5. *
  6. * baser/config/bootstrapより呼び出される
  7. *
  8. * PHP versions 5
  9. *
  10. * baserCMS : Based Website Development Project <http://basercms.net>
  11. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  12. *
  13. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  14. * @link http://basercms.net baserCMS Project
  15. * @package baser
  16. * @since baserCMS v 0.1.0
  17. * @version $Revision$
  18. * @modifiedby $LastChangedBy$
  19. * @lastmodified $Date$
  20. * @license http://basercms.net/license/index.html
  21. */
  22. /**
  23. * WEBサイトのベースとなるURLを取得する
  24. *
  25. * コントローラーが初期化される前など {$this->base} が利用できない場合に利用する
  26. * / | /index.php/ | /subdir/ | /subdir/index.php/
  27. *
  28. * ※ プログラムフォルダ内の画像やCSSの読み込み時もbootstrap.php で呼び出されるのでサーバーキャッシュは利用しない
  29. *
  30. * @return string ベースURL
  31. */
  32. function baseUrl() {
  33. $baseUrl = Configure::read('App.baseUrl');
  34. if($baseUrl) {
  35. if(!preg_match('/\/$/', $baseUrl)) {
  36. $baseUrl .= '/';
  37. }
  38. }else {
  39. if(!empty($_SERVER['QUERY_STRING'])) {
  40. // $_GET['url'] からURLを取得する場合、Controller::requestAction では、
  41. // $_GET['url'] をリクエストしたアクションのURLで書き換えてしまい
  42. // ベースとなるURLが取得できないので、$_SERVER['QUERY_STRING'] を利用
  43. $url = '';
  44. if(preg_match('/url=([^&]+)(&|$)/', $_SERVER['QUERY_STRING'], $maches)) {
  45. $url = $maches[1];
  46. }
  47. if($url) {
  48. $requestUri = '/';
  49. if(!empty($_SERVER['REQUEST_URI'])) {
  50. $requestUri = urldecode($_SERVER['REQUEST_URI']);
  51. }
  52. if(strpos($requestUri, '?') !== false) {
  53. list($requestUri) = explode('?', $requestUri);
  54. }
  55. $baseUrl = str_replace($url, '', $requestUri);
  56. }
  57. } else {
  58. // /index の場合、$_SERVER['QUERY_STRING'] が入ってこない為
  59. $requestUri = '/';
  60. if(!empty($_SERVER['REQUEST_URI'])) {
  61. $requestUri = $_SERVER['REQUEST_URI'];
  62. }
  63. $baseUrl = preg_replace("/index$/", '', $requestUri);
  64. }
  65. }
  66. return $baseUrl;
  67. }
  68. /**
  69. * ドキュメントルートを取得する
  70. *
  71. * サブドメインの場合など、$_SERVER['DOCUMENT_ROOT'] が正常に取得できない場合に利用する
  72. * UserDir に対応
  73. *
  74. * @return string ドキュメントルートの絶対パス
  75. */
  76. function docRoot() {
  77. if(empty($_SERVER['SCRIPT_NAME'])) {
  78. return '';
  79. }
  80. if(strpos($_SERVER['SCRIPT_NAME'],'.php') === false){
  81. // さくらの場合、/index を呼びだすと、拡張子が付加されない
  82. $scriptName = $_SERVER['SCRIPT_NAME'] . '.php';
  83. }else{
  84. $scriptName = $_SERVER['SCRIPT_NAME'];
  85. }
  86. $path = explode('/', $scriptName);
  87. krsort($path);
  88. // WINDOWS環境の場合、SCRIPT_NAMEのDIRECTORY_SEPARATORがスラッシュの場合があるので
  89. // スラッシュに一旦置換してスラッシュベースで解析
  90. $docRoot = str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']);
  91. foreach($path as $value) {
  92. $reg = "/\/".$value."$/";
  93. $docRoot = preg_replace($reg, '', $docRoot);
  94. }
  95. return str_replace('/', DS, $docRoot);
  96. }
  97. /**
  98. * リビジョンを取得する
  99. * @param string baserCMS形式のバージョン表記 (例)baserCMS 1.5.3.1600 beta
  100. * @return string リビジョン番号
  101. */
  102. function revision($version) {
  103. return preg_replace("/baserCMS [0-9]+?\.[0-9]+?\.[0-9]+?\.([0-9]*)[\sa-z]*/is", "$1", $version);
  104. }
  105. /**
  106. * バージョンを特定する一意の数値を取得する
  107. * 2つ目以降のバージョン番号は3桁として結合
  108. * 1.5.9 => 1005009
  109. * ※ 2つ目以降のバージョン番号は999までとする
  110. * β版の場合はfalseを返す
  111. *
  112. * @param mixed $version Or false
  113. */
  114. function verpoint($version) {
  115. $version = str_replace('baserCMS ', '', $version);
  116. if(preg_match("/([0-9]+)\.([0-9]+)\.([0-9]+)([\sa-z\-]+|\.[0-9]+|)([\sa-z\-]+|\.[0-9]+|)/is", $version, $maches)) {
  117. if(isset($maches[4]) && preg_match('/^\.[0-9]+$/', $maches[4])) {
  118. if(isset($maches[5]) && preg_match('/^[\sa-z\-]+$/', $maches[5])) {
  119. return false;
  120. }
  121. $maches[4] = str_replace('.', '', $maches[4]);
  122. } elseif(isset($maches[4]) && preg_match('/^[\sa-z\-]+$/', $maches[4])) {
  123. return false;
  124. } else {
  125. $maches[4] = 0;
  126. }
  127. return $maches[1]*1000000000 + $maches[2]*1000000 + $maches[3]*1000 + $maches[4];
  128. }else {
  129. return 0;
  130. }
  131. }
  132. /**
  133. * 拡張子を取得する
  134. * @param string mimeタイプ
  135. * @return string 拡張子
  136. * @access public
  137. */
  138. function decodeContent($content,$fileName=null) {
  139. $contentsMaping=array(
  140. "image/gif" => "gif",
  141. "image/jpeg" => "jpg",
  142. "image/pjpeg" => "jpg",
  143. "image/x-png" => "png",
  144. "image/jpg" => "jpg",
  145. "image/png" => "png",
  146. "application/x-shockwave-flash" => "swf",
  147. /*"application/pdf" => "pdf",*/ // TODO windows で ai ファイルをアップロードをした場合、headerがpdfとして出力されるのでコメントアウト
  148. "application/pgp-signature" => "sig",
  149. "application/futuresplash" => "spl",
  150. "application/msword" => "doc",
  151. "application/postscript" => "ai",
  152. "application/x-bittorrent" => "torrent",
  153. "application/x-dvi" => "dvi",
  154. "application/x-gzip" => "gz",
  155. "application/x-ns-proxy-autoconfig" => "pac",
  156. "application/x-shockwave-flash" => "swf",
  157. "application/x-tgz" => "tar.gz",
  158. "application/x-tar" => "tar",
  159. "application/zip" => "zip",
  160. "audio/mpeg" => "mp3",
  161. "audio/x-mpegurl" => "m3u",
  162. "audio/x-ms-wma" => "wma",
  163. "audio/x-ms-wax" => "wax",
  164. "audio/x-wav" => "wav",
  165. "image/x-xbitmap" => "xbm",
  166. "image/x-xpixmap" => "xpm",
  167. "image/x-xwindowdump" => "xwd",
  168. "text/css" => "css",
  169. "text/html" => "html",
  170. "text/javascript" => "js",
  171. "text/plain" => "txt",
  172. "text/xml" => "xml",
  173. "video/mpeg" => "mpeg",
  174. "video/quicktime" => "mov",
  175. "video/x-msvideo" => "avi",
  176. "video/x-ms-asf" => "asf",
  177. "video/x-ms-wmv" => "wmv"
  178. );
  179. if (isset($contentsMaping[$content])) {
  180. return $contentsMaping[$content];
  181. } elseif($fileName) {
  182. $info = pathinfo($fileName);
  183. if(!empty($info['extension'])) {
  184. return $info['extension'];
  185. }else {
  186. return false;
  187. }
  188. } else {
  189. return false;
  190. }
  191. }
  192. /**
  193. * 環境変数よりURLパラメータを取得する
  194. *
  195. * モバイルプレフィックスは除外する
  196. * bootstrap実行後でのみ利用可
  197. */
  198. function getUrlParamFromEnv() {
  199. $agentAlias = Configure::read('BcRequest.agentAlias');
  200. $url = getUrlFromEnv();
  201. return preg_replace('/^'.$agentAlias.'\//','',$url);
  202. }
  203. /**
  204. * 環境変数よりURLを取得する
  205. *
  206. * スマートURLオフ&bootstrapのタイミングでは、$_GET['url']が取得できてない為、それをカバーする為に利用する
  207. * 先頭のスラッシュは除外する
  208. * baseUrlは除外する
  209. * TODO QUERY_STRING ではなく、全て REQUEST_URI で判定してよいのでは?
  210. */
  211. function getUrlFromEnv() {
  212. if(!empty($_GET['url'])) {
  213. return preg_replace('/^\//', '', $_GET['url']);
  214. }
  215. if(!isset($_SERVER['REQUEST_URI'])) {
  216. return;
  217. } else {
  218. $requestUri = $_SERVER['REQUEST_URI'];
  219. }
  220. $appBaseUrl = Configure::read('App.baseUrl');
  221. $parameter = '';
  222. if($appBaseUrl) {
  223. $base = dirname($appBaseUrl);
  224. if(strpos($requestUri, $appBaseUrl) !== false) {
  225. $parameter = str_replace($appBaseUrl, '', $requestUri);
  226. }else {
  227. // トップページ
  228. $parameter = str_replace($base.'/', '', $requestUri);
  229. }
  230. }else {
  231. $parameter = '';
  232. if(isset($_SERVER['QUERY_STRING'])) {
  233. $query = $_SERVER['QUERY_STRING'];
  234. }
  235. if(!empty($query)){
  236. if(strpos($query, '&')){
  237. $queries = split('&',$query);
  238. foreach($queries as $_query) {
  239. if(strpos($_query, '=')){
  240. list($key,$value) = split('=',$_query);
  241. if($key=='url'){
  242. $parameter = $value;
  243. break;
  244. }
  245. }
  246. }
  247. }else{
  248. if(strpos($query, '=')){
  249. list($key,$value) = split('=',$query);
  250. if($key=='url'){
  251. $parameter = $value;
  252. }
  253. }
  254. }
  255. }elseif (preg_match('/^'.str_replace('/', '\/', baseUrl()).'/is', $requestUri)){
  256. $parameter = preg_replace('/^'.str_replace('/', '\/', baseUrl()).'/is', '', $requestUri);
  257. } else {
  258. $parameter = $requestUri;
  259. }
  260. }
  261. $parameter = preg_replace('/^\//','',$parameter);
  262. return $parameter;
  263. }
  264. /**
  265. * Viewキャッシュを削除する
  266. * URLを指定しない場合は全てのViewキャッシュを削除する
  267. * 全て削除する場合、標準の関数clearCacheだとemptyファイルまで削除されてしまい、
  268. * 開発時に不便なのでFolderクラスで削除
  269. *
  270. * @param $url
  271. * @return void
  272. * @access public
  273. */
  274. function clearViewCache($url=null,$ext='.php') {
  275. $url = preg_replace('/^\/mobile\//is', '/m/', $url);
  276. if ($url == '/' || $url == '/index' || $url == '/index.html' || $url == '/m/' || $url == '/m/index' || $url == '/m/index.html') {
  277. $homes = array('','index','index_html');
  278. foreach($homes as $home){
  279. if(preg_match('/^\/m/is',$url)){
  280. if($home){
  281. $home = 'm_'.$home;
  282. }else{
  283. $home = 'm';
  284. }
  285. } elseif (preg_match('/^\/s/is', $url)) {
  286. if($home){
  287. $home = 's_'.$home;
  288. }else{
  289. $home = 's';
  290. }
  291. }
  292. $baseUrl = baseUrl();
  293. if($baseUrl) {
  294. $baseUrl = str_replace(array('/', '.'), '_', $baseUrl);
  295. $baseUrl = preg_replace('/^_/', '', $baseUrl);
  296. $baseUrl = preg_replace('/_$/', '', $baseUrl);
  297. if($home){
  298. $home = $baseUrl.$home;
  299. }else{
  300. $home = $baseUrl;
  301. }
  302. }elseif(!$home){
  303. $home = 'home';
  304. }
  305. clearCache($home);
  306. }
  307. }elseif($url) {
  308. $url = preg_replace('/\/index$/', '', $url);
  309. clearCache(strtolower(Inflector::slug($url)),'views',$ext);
  310. }else {
  311. App::import('Core','Folder');
  312. $folder = new Folder(CACHE.'views'.DS);
  313. $files = $folder->read(true,true);
  314. foreach($files[1] as $file) {
  315. if($file != 'empty') {
  316. @unlink(CACHE.'views'.DS.$file);
  317. }
  318. }
  319. }
  320. }
  321. /**
  322. * データキャッシュを削除する
  323. */
  324. function clearDataCache() {
  325. App::import('Core','Folder');
  326. $folder = new Folder(CACHE.'datas'.DS);
  327. $files = $folder->read(true,true,true);
  328. foreach($files[1] as $file) {
  329. @unlink($file);
  330. }
  331. }
  332. /**
  333. * キャッシュファイルを全て削除する
  334. */
  335. function clearAllCache() {
  336. /* 標準の関数だとemptyファイルまで削除されてしまい、開発時に不便なのでFolderクラスで削除
  337. Cache::clear();
  338. Cache::clear(false,'_cake_core_');
  339. Cache::clear(false,'_cake_model_');
  340. clearCache();
  341. */
  342. App::import('Core','Folder');
  343. $folder = new Folder(CACHE);
  344. $files = $folder->read(true,true,true);
  345. foreach($files[1] as $file) {
  346. @unlink($file);
  347. }
  348. foreach($files[0] as $dir) {
  349. $folder = new Folder($dir);
  350. $caches = $folder->read(true,true,true);
  351. foreach($caches[1] as $file) {
  352. if(basename($file) != 'empty') {
  353. @unlink($file);
  354. }
  355. }
  356. }
  357. }
  358. /**
  359. * baserCMSのインストールが完了しているかチェックする
  360. * @return boolean
  361. */
  362. function isInstalled () {
  363. if(getDbConfig() && file_exists(CONFIGS.'install.php')){
  364. return true;
  365. }
  366. return false;
  367. }
  368. /**
  369. * DBセッティングが存在するかチェックする
  370. *
  371. * @param string $name
  372. * @return mixed DatabaseConfig Or false
  373. */
  374. function getDbConfig($name = 'baser') {
  375. if(file_exists(CONFIGS.'database.php')) {
  376. require_once CONFIGS.'database.php';
  377. $dbConfig = new DATABASE_CONFIG();
  378. if(!empty($dbConfig->{$name}['driver'])){
  379. return $dbConfig->{$name};
  380. }
  381. }
  382. return false;
  383. }
  384. /**
  385. * 必要な一時フォルダが存在するかチェックし、
  386. * なければ生成する
  387. */
  388. function checkTmpFolders(){
  389. if(!is_writable(TMP)){
  390. return;
  391. }
  392. App::import('Core','Folder');
  393. $folder = new Folder();
  394. $folder->create(TMP.'logs',0777);
  395. $folder->create(TMP.'sessions',0777);
  396. $folder->create(TMP.'schemas',0777);
  397. $folder->create(TMP.'schemas'.DS.'baser', 0777);
  398. $folder->create(TMP.'schemas'.DS.'plugin', 0777);
  399. $folder->create(CACHE, 0777);
  400. $folder->create(CACHE.'models',0777);
  401. $folder->create(CACHE.'persistent',0777);
  402. $folder->create(CACHE.'views',0777);
  403. $folder->create(CACHE.'datas',0777);
  404. $folder->create(CACHE.'environment',0777);
  405. }
  406. /**
  407. * フォルダの中をフォルダを残して空にする
  408. *
  409. * @param string $path
  410. * @return boolean
  411. */
  412. function emptyFolder($path) {
  413. $result = true;
  414. $Folder = new Folder($path);
  415. $files = $Folder->read(true, true, true);
  416. if(is_array($files[1])) {
  417. foreach($files[1] as $file) {
  418. if($file != 'empty') {
  419. if(!@unlink($file)) {
  420. $result = false;
  421. }
  422. }
  423. }
  424. }
  425. if(is_array($files[0])) {
  426. foreach($files[0] as $file) {
  427. if(!emptyFolder($file)) {
  428. $result = false;
  429. }
  430. }
  431. }
  432. return $result;
  433. }
  434. /**
  435. * 現在のビューディレクトリのパスを取得する
  436. *
  437. * @return string
  438. */
  439. function getViewPath() {
  440. if (ClassRegistry::isKeySet('SiteConfig')) {
  441. $SiteConfig = ClassRegistry::getObject('SiteConfig');
  442. }else {
  443. $SiteConfig = ClassRegistry::init('SiteConfig');
  444. }
  445. $siteConfig = $SiteConfig->findExpanded();
  446. $theme = $siteConfig['theme'];
  447. if($theme) {
  448. return WWW_ROOT.'themed'.DS.$theme.DS;
  449. }else {
  450. return VIEWS;
  451. }
  452. }
  453. /**
  454. * ファイルポインタから行を取得し、CSVフィールドを処理する
  455. *
  456. * @param stream handle
  457. * @param int length
  458. * @param string delimiter
  459. * @param string enclosure
  460. * @return mixed ファイルの終端に達した場合を含み、エラー時にFALSEを返します。
  461. */
  462. function fgetcsvReg (&$handle, $length = null, $d = ',', $e = '"') {
  463. $d = preg_quote($d);
  464. $e = preg_quote($e);
  465. $_line = "";
  466. $eof = false;
  467. while (($eof != true)and(!feof($handle))) {
  468. $_line .= (empty($length) ? fgets($handle) : fgets($handle, $length));
  469. $itemcnt = preg_match_all('/'.$e.'/', $_line, $dummy);
  470. if ($itemcnt % 2 == 0) $eof = true;
  471. }
  472. $_csv_line = preg_replace('/(?:\r\n|[\r\n])?$/', $d, trim($_line));
  473. $_csv_pattern = '/('.$e.'[^'.$e.']*(?:'.$e.$e.'[^'.$e.']*)*'.$e.'|[^'.$d.']*)'.$d.'/';
  474. preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
  475. $_csv_data = $_csv_matches[1];
  476. for($_csv_i=0;$_csv_i<count($_csv_data);$_csv_i++) {
  477. $_csv_data[$_csv_i]=preg_replace('/^'.$e.'(.*)'.$e.'$/s','$1',$_csv_data[$_csv_i]);
  478. $_csv_data[$_csv_i]=str_replace($e.$e, $e, $_csv_data[$_csv_i]);
  479. }
  480. return empty($_line) ? false : $_csv_data;
  481. }
  482. /**
  483. * httpからのフルURLを取得する
  484. *
  485. * @param mixed $url
  486. * @return string
  487. */
  488. function fullUrl($url) {
  489. $url = Router::url($url);
  490. return topLevelUrl(false).$url;
  491. }
  492. /**
  493. * サイトのトップレベルのURLを取得する
  494. *
  495. * @param boolean $lastSlash
  496. * @return string
  497. */
  498. function topLevelUrl($lastSlash = true) {
  499. $protocol = 'http://';
  500. if(!empty($_SERVER['HTTPS'])) {
  501. $protocol = 'https://';
  502. }
  503. $host = $_SERVER['HTTP_HOST'];
  504. $url = $protocol.$host;
  505. if($lastSlash) {
  506. $url .= '/';
  507. }
  508. return $url;
  509. }
  510. /**
  511. * サイトの設置URLを取得する
  512. *
  513. * index.phpは含まない
  514. *
  515. * @return string
  516. */
  517. function siteUrl() {
  518. $baseUrl = preg_replace('/index\.php\/$/', '', baseUrl());
  519. return topLevelUrl(false).$baseUrl;
  520. }
  521. /**
  522. * 配列を再帰的に上書きする
  523. * 二つまで
  524. * @param array $a
  525. * @param array $b
  526. * @return array
  527. */
  528. function amr($a, $b) {
  529. foreach ($b as $k => $v) {
  530. if(is_array($v)) {
  531. if(isset($a[$k])) {
  532. $a[$k] = amr($a[$k], $v);
  533. continue;
  534. }
  535. }
  536. if(!is_array($a)) {
  537. $a = array($a);
  538. }
  539. $a[$k] = $v;
  540. }
  541. return $a;
  542. }
  543. /**
  544. * プラグインのコンフィグファイルを読み込む
  545. *
  546. * @param string $name
  547. * @return boolean
  548. */
  549. function loadPluginConfig($name) {
  550. if(strpos($name, '.') === false) {
  551. return false;
  552. }
  553. list($plugin, $file) = explode('.', $name);
  554. $plugin = Inflector::underscore($plugin);
  555. $pluginPaths = array(
  556. APP.'plugins'.DS,
  557. BASER_PLUGINS
  558. );
  559. $config = null;
  560. foreach($pluginPaths as $pluginPath) {
  561. $configPath = $pluginPath.$plugin.DS.'config'.DS.$file.'.php';
  562. if(file_exists($configPath)) {
  563. include $configPath;
  564. }
  565. }
  566. if($config) {
  567. return Configure::write($config);
  568. } else {
  569. return false;
  570. }
  571. }
  572. /**
  573. * URLにセッションIDを付加する
  574. * 既に付加されている場合は重複しない
  575. *
  576. * @param mixed $url
  577. * @return mixed
  578. */
  579. function addSessionId($url, $force = false) {
  580. // use_trans_sid が有効になっている場合、2重で付加されてしまう
  581. if(Configure::read('BcRequest.agent') == 'mobile' && Configure::read('BcAgent.mobile.sessionId') && (!ini_get('session.use_trans_sid') || $force)) {
  582. if(is_array($url)) {
  583. $url["?"][session_name()] = session_id();
  584. } else {
  585. if(strpos($url, '?') !== false) {
  586. $args = array();
  587. $_url = explode('?', $url);
  588. if(!empty($_url[1])) {
  589. if(strpos($_url[1], '&') !== false) {
  590. $aryUrl = explode('&', $_url[1]);
  591. foreach($aryUrl as $pass) {
  592. if(strpos($pass, '=') !== false) {
  593. list($key, $value) = explode('=', $pass);
  594. $args[$key] = $value;
  595. }
  596. }
  597. } else {
  598. if(strpos($_url[1], '=') !== false) {
  599. list($key, $value) = explode('=', $_url[1]);
  600. $args[$key] = $value;
  601. }
  602. }
  603. }
  604. $args[session_name()] = session_id();
  605. $pass = '';
  606. foreach($args as $key => $value) {
  607. if($pass) {
  608. $pass .= '&';
  609. }
  610. $pass .= $key.'='.$value;
  611. }
  612. $url = $_url[0] . '?' . $pass;
  613. } else {
  614. $url .= '?'.session_name().'='.session_id();
  615. }
  616. }
  617. }
  618. return $url;
  619. }
  620. /**
  621. * 利用可能なプラグインのリストを取得する
  622. *
  623. * PluginHookBehavior::setup() で、プラグインリストを参照できるように、
  624. * ClassRegistry::removeObject('Plugin'); で一旦 Plugin オブジェクトを削除
  625. * エラーの際も呼び出される事があるので、テーブルが実際に存在するかチェックする
  626. *
  627. * @return array
  628. */
  629. function getEnablePlugins() {
  630. $enablePlugins = array();
  631. if(!Configure::read('Cache.disable')) {
  632. $enablePlugins = Cache::read('enable_plugins', '_cake_env_');
  633. }
  634. if(!$enablePlugins) {
  635. $db =& ConnectionManager::getDataSource('baser');
  636. $sources = $db->listSources();
  637. $pluginTable = $db->config['prefix'] . 'plugins';
  638. $enablePlugins = array();
  639. if (!is_array($sources) || in_array(strtolower($pluginTable), array_map('strtolower', $sources))) {
  640. $Plugin = ClassRegistry::init('Plugin');
  641. $plugins = $Plugin->find('all', array('fields' => array('Plugin.name'), 'conditions' => array('Plugin.status' => true)));
  642. ClassRegistry::removeObject('Plugin');
  643. if($plugins) {
  644. $enablePlugins = Set::extract('/Plugin/name',$plugins);
  645. if(!Configure::read('Cache.disable')) {
  646. Cache::write('enable_plugins', $enablePlugins, '_cake_env_');
  647. }
  648. }
  649. }
  650. }
  651. return $enablePlugins;
  652. }
  653. /**
  654. * サイト基本設定をConfigureへ読み込む
  655. *
  656. * @return void
  657. */
  658. function loadSiteConfig() {
  659. $SiteConfig = ClassRegistry::init('SiteConfig');
  660. Configure::write('BcSite', $SiteConfig->findExpanded());
  661. ClassRegistry::removeObject('SiteConfig');
  662. }
  663. /**
  664. * バージョンを取得する
  665. *
  666. * @return string Or false
  667. */
  668. function getVersion($plugin = '') {
  669. $corePlugins = array('blog', 'feed', 'mail');
  670. if(!$plugin || in_array($plugin, $corePlugins)) {
  671. $path = BASER.'VERSION.txt';
  672. } else {
  673. $appPath = APP.'plugins'.DS.$plugin.DS.'VERSION.txt';
  674. $baserPath = BASER_PLUGINS.$plugin.DS.'VERSION.txt';
  675. if(file_exists($appPath)) {
  676. $path = $appPath;
  677. }elseif(file_exists($baserPath)) {
  678. $path = $baserPath;
  679. } else {
  680. return false;
  681. }
  682. }
  683. App::import('File');
  684. $versionFile = new File($path);
  685. $versionData = $versionFile->read();
  686. $aryVersionData = split("\n",$versionData);
  687. if(!empty($aryVersionData[0])) {
  688. return $aryVersionData[0];
  689. }else {
  690. return false;
  691. }
  692. }
  693. /**
  694. * アップデートのURLを記載したメールを送信する
  695. */
  696. function sendUpdateMail() {
  697. $bcSite = Configure::read('BcSite');
  698. $bcSite['update_id'] = String::uuid();
  699. $SiteConfig = ClassRegistry::init('SiteConfig');
  700. $SiteConfig->saveKeyValue(array('SiteConfig' => $bcSite));
  701. ClassRegistry::removeObject('SiteConfig');
  702. App::import('Core', 'Email');
  703. App::import('Component', 'BcEmail', array('file'=>CAKE_CORE_INCLUDE_PATH.DS.'baser'.DS.'controllers'.DS.'components'.DS.'bc_email.php'));
  704. $BcEmail = new BcEmailComponent();
  705. if(!empty($bcSite['mail_encode'])) {
  706. $encode = $bcSite['mail_encode'];
  707. } else {
  708. $encode = 'ISO-2022-JP';
  709. }
  710. $BcEmail->charset = $encode;
  711. $BcEmail->sendAs = 'text';
  712. $BcEmail->lineLength=105;
  713. if(!empty($bcSite['smtp_host'])) {
  714. $BcEmail->delivery = 'smtp';
  715. $BcEmail->smtpOptions = array('host' =>$bcSite['smtp_host'],
  716. 'port' => 25,
  717. 'timeout' => 30,
  718. 'username' => ($bcSite['smtp_user'])?$bcSite['smtp_user']:null,
  719. 'password' => ($bcSite['smtp_password'])?$bcSite['smtp_password']:null);
  720. } else {
  721. $BcEmail->delivery = "mail";
  722. }
  723. $BcEmail->to = $bcSite['email'];
  724. $BcEmail->subject = 'baserCMSアップデート';
  725. $BcEmail->from = $bcSite['name'].' <'.$bcSite['email'].'>';
  726. $message = array();
  727. $message[] = '下記のURLよりbaserCMSのアップデートを完了してください。';
  728. $message[] = topLevelUrl(false).baseUrl().'updaters/index/'.$bcSite['update_id'];
  729. $BcEmail->send($message);
  730. }
  731. ?>