PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/Common/PortalUtil.php

http://pradoportal.googlecode.com/
PHP | 898 lines | 767 code | 89 blank | 42 comment | 155 complexity | b6a30a4f4b603ea3ab172e7c9ecb7646 MD5 | raw file
  1. <?php
  2. /**
  3. * Prado Portal.
  4. *
  5. * @author Steen Rabol <steen.rabol@gmail.com>
  6. * @link http://www.pradoportal.dk/
  7. * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
  8. * @license http://www.pradoportal.dk
  9. * @version $Id: PortalUtil.php 467 2011-01-25 17:24:30Z steen.rabol $
  10. * @package Pradoportal.Common
  11. *
  12. */
  13. /**
  14. *
  15. * @package Pradoportal.Common
  16. */
  17. Prado::using('System.Util.TVarDumper');
  18. class PortalUtil
  19. {
  20. public static function fatalError($msg, $debug = false)
  21. {
  22. if($debug)
  23. {
  24. echo '<h1>Fatal Error</h1>';
  25. if(is_array($msg))
  26. {
  27. echo "<pre>";
  28. print_r($msg);
  29. echo "</pre>";
  30. }
  31. else
  32. {
  33. echo '<p>'.$msg.'</p>';
  34. }
  35. if(!function_exists('debug_backtrace'))
  36. {
  37. return;
  38. }
  39. echo '<h2>Debug Backtrace</h2>';
  40. echo '<pre>';
  41. $index=-1;
  42. foreach(debug_backtrace() as $t)
  43. {
  44. $index++;
  45. if($index==0) // hide the backtrace of this function
  46. continue;
  47. echo '#'.$index.' ';
  48. if(isset($t['file']))
  49. echo basename($t['file']) . ':' . $t['line'];
  50. else
  51. echo '<PHP inner-code>';
  52. echo ' -- ';
  53. if(isset($t['class']))
  54. echo $t['class'] . $t['type'];
  55. echo $t['function'] . '(';
  56. if(isset($t['args']) && sizeof($t['args']) > 0)
  57. {
  58. $count=0;
  59. foreach($t['args'] as $item)
  60. {
  61. if(is_string($item))
  62. {
  63. $str=htmlentities(str_replace("\r\n", "", $item), ENT_QUOTES);
  64. if (strlen($item) > 70)
  65. echo "'". substr($str, 0, 70) . "...'";
  66. else
  67. echo "'" . $str . "'";
  68. }
  69. else if (is_int($item) || is_float($item))
  70. echo $item;
  71. else if (is_object($item))
  72. echo get_class($item);
  73. else if (is_array($item))
  74. echo 'array(' . count($item) . ')';
  75. else if (is_bool($item))
  76. echo $item ? 'true' : 'false';
  77. else if ($item === null)
  78. echo 'NULL';
  79. else if (is_resource($item))
  80. echo get_resource_type($item);
  81. $count++;
  82. if (count($t['args']) > $count)
  83. echo ', ';
  84. }
  85. }
  86. echo ")\n";
  87. }
  88. echo '</pre>';
  89. exit(1);
  90. }
  91. else
  92. {
  93. echo '<h1>Fatal Error</h1>';
  94. if(is_array($msg))
  95. {
  96. foreach($msg as $m)
  97. {
  98. echo '<p>'.$m.'</p>';
  99. }
  100. }
  101. else if(is_object($msg))
  102. {
  103. var_dump($msg);
  104. }
  105. else
  106. {
  107. echo '<p>' . $msg . '<p>';
  108. }
  109. }
  110. exit(0);
  111. }
  112. /**
  113. * @parm $portletname is the name to check if is installed
  114. */
  115. public static function IsPortletInstalled($portletname)
  116. {
  117. $application = Prado::getApplication();
  118. $db = $application->DataAccess;
  119. $portletid = $db->createCommand("select id from tblportlets where name='$portletname'")->queryScalar();
  120. return TPropertyValue::ensureBoolean($portletid);
  121. }
  122. public static function getAvailablePortalLayouts()
  123. {
  124. $application = Prado::getApplication();
  125. $basePath = $application->PortalBasePath . "/protected/Layouts/Portal/" . $application->Parameters['ThemeName'];
  126. $layouts = array();
  127. if(!is_dir($basePath))
  128. {
  129. $this->reportError(1,"Could not find layout directory $basePath");
  130. }
  131. $folder = @opendir($basePath);
  132. while($file = @readdir($folder))
  133. {
  134. if($file!=='.' && $file!=='..' && $file!=='.svn' && !is_dir($basePath.'/'.$file))
  135. {
  136. $path_parts = pathinfo($file);
  137. if($path_parts['extension'] == "php")
  138. {
  139. $clsName = basename($path_parts['basename'],".php");
  140. if(!class_exists($clsName,false))
  141. {
  142. $clsPath = 'Application.Layouts.Portal.' . $application->Parameters['ThemeName'] . '.' . $clsName;
  143. Prado::using($clsPath);
  144. }
  145. $obj = new $clsName();
  146. if($obj->Type == 1)
  147. {
  148. $layouts[] = array("id" => $clsName,"name" =>$obj->Name);
  149. }
  150. }
  151. }
  152. }
  153. closedir($folder);
  154. return $layouts;
  155. }
  156. public static function getAvailableAdminThemes()
  157. {
  158. $application = Prado::getApplication();
  159. $basePath = $application->PortalBasePath . "/themes/Admin/";
  160. $themes = array();
  161. if(!is_dir($basePath))
  162. {
  163. $this->reportError(1,"Could not find theme directory $basePath");
  164. }
  165. $folder = @opendir($basePath);
  166. while($dir = @readdir($folder))
  167. {
  168. if($dir !== '.' && $dir !== '..' && $dir !== '.svn' && is_dir($basePath.'/'.$dir))
  169. {
  170. $themes[] = $dir;
  171. }
  172. }
  173. closedir($folder);
  174. return $themes;
  175. }
  176. public static function getAvailablePortalThemes()
  177. {
  178. $application = Prado::getApplication();
  179. $basePath = $application->PortalBasePath . "/themes/Portal/";
  180. $themes = array();
  181. if(!is_dir($basePath))
  182. {
  183. $this->reportError(1,"Could not find theme directory $basePath");
  184. }
  185. $folder = @opendir($basePath);
  186. while($dir = @readdir($folder))
  187. {
  188. if($dir !== '.' && $dir !== '..' && $dir !== '.svn' && is_dir($basePath.'/'.$dir))
  189. {
  190. $themes[] = $dir;
  191. }
  192. }
  193. closedir($folder);
  194. return $themes;
  195. }
  196. public static function return_bytes($val)
  197. {
  198. $val = trim($val);
  199. $last = strtolower($val{strlen($val)-1});
  200. switch($last)
  201. {
  202. // The 'G' modifier is available since PHP 5.1.0
  203. case 'g':
  204. $val *= 1024;
  205. case 'm':
  206. $val *= 1024;
  207. case 'k':
  208. $val *= 1024;
  209. }
  210. return $val;
  211. }
  212. public static function return_letter_size($fsize)
  213. {
  214. $retval = "";
  215. // Format size:
  216. if ($fsize > ((1024 * 1024) * 1024))
  217. {
  218. $retval = number_format(((($fsize / 1024) / 1024) / 1024), 2) . ' GB';
  219. }
  220. elseif($fsize > (1024 * 1024))
  221. {
  222. $retval = number_format((($fsize / 1024) / 1024), 2) . ' MB';
  223. }
  224. elseif ($fsize > 1000)
  225. {
  226. $retval = number_format(($fsize/1024), 2) . ' KB';
  227. }
  228. else
  229. {
  230. $retval = $fsize . ' Bytes';
  231. }
  232. return $retval;
  233. }
  234. public static function isUTF8($str)
  235. {
  236. if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32"))
  237. {
  238. return true;
  239. }
  240. else
  241. {
  242. return false;
  243. }
  244. }
  245. public static function getCookie($cookiename, $default_value = null)
  246. {
  247. $ret_val = $default_value;
  248. $application = Prado::getApplication();
  249. foreach($application->Request->Cookies as $cookie)
  250. {
  251. if($cookie->Name == $cookiename)
  252. {
  253. return $cookie;
  254. }
  255. }
  256. return $ret_val;
  257. }
  258. public static function getCookieValue($cookiename, $default_value = null)
  259. {
  260. $ret_val = $default_value;
  261. $application = Prado::getApplication();
  262. foreach($application->Request->Cookies as $cookie)
  263. {
  264. if($cookie->Name == $cookiename)
  265. {
  266. return $cookie->Value;
  267. }
  268. }
  269. return $ret_val;
  270. }
  271. public static function Log($msg)
  272. {
  273. PortalUtil::LogAlert($msg);
  274. }
  275. public static function LogAlert($msg,$level = TLogger::ALERT)
  276. {
  277. Prado::log(TVarDumper::dump($msg),$level);
  278. }
  279. public static function Info($msg)
  280. {
  281. Prado::trace(TVarDumper::dump($msg));
  282. }
  283. public static function Debug($msg)
  284. {
  285. Prado::using('Application.Common.3rdParty.dBug');
  286. new dBug($msg);
  287. }
  288. /**
  289. * @return Return a array with all system pages
  290. */
  291. public static function getAvailableSystemPages()
  292. {
  293. $application = Prado::getApplication();
  294. $basePath = $application->BasePath . "/Pages/System";
  295. $layouts = array();
  296. if(!is_dir($basePath))
  297. $this->reportError(1,"Could not find layout directory $basePath");
  298. $folder = @opendir($basePath);
  299. while($file = @readdir($folder))
  300. {
  301. if($file!=='.' && $file!=='..' && $file!=='.svn' && !is_dir($basePath.'/'.$file))
  302. {
  303. $path_parts = pathinfo($file);
  304. if($path_parts['extension'] == "php")
  305. {
  306. $layouts[] = array("name" => "System." . basename($path_parts['basename'],".php"));
  307. }
  308. }
  309. }
  310. closedir($folder);
  311. return $layouts;
  312. }
  313. public static function getAvailableAdminPages()
  314. {
  315. $application = Prado::getApplication();
  316. $basePath = $application->BasePath . "/Pages/Admin";
  317. $layouts = array();
  318. if(!is_dir($basePath))
  319. $this->reportError(1,"Could not find layout directory $basePath");
  320. $folder = @opendir($basePath);
  321. while($file = @readdir($folder))
  322. {
  323. if($file!=='.' && $file!=='..' && $file!=='.svn' && !is_dir($basePath.'/'.$file))
  324. {
  325. $path_parts = pathinfo($file);
  326. if($path_parts['extension'] == "php")
  327. {
  328. $layouts[] = array("name" => "Admin." . basename($path_parts['basename'],".php"));
  329. }
  330. }
  331. }
  332. closedir($folder);
  333. return $layouts;
  334. }
  335. public static function AvailablePagesInPath($path)
  336. {
  337. $application = Prado::getApplication();
  338. $basePath = $application->BasePath . "/Pages/$path";
  339. $layouts = array();
  340. if(!is_dir($basePath))
  341. {
  342. return $layouts;
  343. }
  344. $folder = @opendir($basePath);
  345. while($file = @readdir($folder))
  346. {
  347. if($file!=='.' && $file!=='..' && $file!=='.svn' && !is_dir($basePath.'/'.$file))
  348. {
  349. $path_parts = pathinfo($file);
  350. if($path_parts['extension'] == "php")
  351. {
  352. $layouts[] = array("name" => "$path." . basename($path_parts['basename'],".php"));
  353. }
  354. }
  355. }
  356. closedir($folder);
  357. return $layouts;
  358. }
  359. public static function getFilesInPath($path, $arrayprefix = null)
  360. {
  361. $application = Prado::getApplication();
  362. $files = array();
  363. if(!is_dir($path))
  364. {
  365. return false;
  366. }
  367. $folder = @opendir($path);
  368. while($file = @readdir($folder))
  369. {
  370. if($file!=='.' && $file!=='..' && $file!=='.svn' && !is_dir($path.'/'.$file))
  371. {
  372. if($arrayprefix === null)
  373. {
  374. $files[] = $file;
  375. }
  376. else
  377. {
  378. $files[] = array($arrayprefix => $file);
  379. }
  380. }
  381. }
  382. closedir($folder);
  383. return $files;
  384. }
  385. public static function GetCssClassesInPortlet($p_portlet)
  386. {
  387. $pCss = null;
  388. $application = Prado::getApplication();
  389. $p_file = $application->BasePath . "/Portlets/" . $p_portlet . "/Common/" . $p_portlet ."Common.php";
  390. $p_class = $p_portlet . 'Common';
  391. if(!class_exists($p_portlet.'Common',false))
  392. {
  393. Prado::using('Application.Portlets.' . $p_portlet . '.Common.' . $p_portlet . 'Common');
  394. }
  395. return call_user_func($p_class . '::GetCssClasses');
  396. }
  397. public static function GetCssClassesInAllPortlets()
  398. {
  399. $application = Prado::getApplication();
  400. $db = $application->DataAccess;
  401. $pDr = $db->createCommand("select name from tblportlets")->query();
  402. $pCss = array();
  403. if($pDr)
  404. {
  405. foreach($pDr as $p)
  406. {
  407. $p_portlet = $p['name'];
  408. $pres = PortalUtil::GetCssClassesInPortlet($p_portlet);
  409. if($pres !== null)
  410. {
  411. $pCss = array_merge($pCss,$pres);
  412. }
  413. }
  414. }
  415. return $pCss;
  416. }
  417. public static function CreateDirStructure($p_path)
  418. {
  419. if(is_dir($p_path) || $p_path == '' )
  420. {
  421. return;
  422. }
  423. try
  424. {
  425. return mkdir($p_path,0777,true);
  426. }
  427. catch (Exception $e)
  428. {
  429. throw new PortalException(1,sprintf("code %d msg: %s dir: %s",$e->getErrorCode(),$e->getErrorMessage(),$p_path));
  430. }
  431. }
  432. public static function xCreateDirStructure($p_path)
  433. {
  434. if (!file_exists($p_path))
  435. {
  436. $_open_basedir_ini = ini_get('open_basedir');
  437. if (DIRECTORY_SEPARATOR=='/')
  438. {
  439. /* unix-style paths */
  440. $_dir = $p_path;
  441. $_dir_parts = preg_split('!/+!', $_dir, -1, PREG_SPLIT_NO_EMPTY);
  442. $_new_dir = ($_dir{0}=='/') ? '/' : getcwd().'/';
  443. if($_use_open_basedir = !empty($_open_basedir_ini))
  444. {
  445. $_open_basedirs = explode(':', $_open_basedir_ini);
  446. }
  447. }
  448. else
  449. {
  450. /* other-style paths */
  451. $_dir = str_replace('\\','/', $p_path);
  452. $_dir_parts = preg_split('!/+!', $_dir, -1, PREG_SPLIT_NO_EMPTY);
  453. if (preg_match('!^((//)|([a-zA-Z]:/))!', $_dir, $_root_dir))
  454. {
  455. /* leading "//" for network volume, or "[letter]:/" for full path */
  456. $_new_dir = $_root_dir[1];
  457. /* remove drive-letter from _dir_parts */
  458. if (isset($_root_dir[3])) array_shift($_dir_parts);
  459. }
  460. else
  461. {
  462. $_new_dir = str_replace('\\', '/', getcwd()).'/';
  463. }
  464. if($_use_open_basedir = !empty($_open_basedir_ini))
  465. {
  466. $_open_basedirs = explode(';', str_replace('\\', '/', $_open_basedir_ini));
  467. }
  468. }
  469. /* all paths use "/" only from here */
  470. foreach ($_dir_parts as $_dir_part)
  471. {
  472. $_new_dir .= $_dir_part;
  473. if ($_use_open_basedir)
  474. {
  475. // do not attempt to test or make directories outside of open_basedir
  476. $_make_new_dir = false;
  477. foreach ($_open_basedirs as $_open_basedir)
  478. {
  479. if (substr($_new_dir, 0, strlen($_open_basedir)) == $_open_basedir)
  480. {
  481. $_make_new_dir = true;
  482. break;
  483. }
  484. }
  485. }
  486. else
  487. {
  488. $_make_new_dir = true;
  489. }
  490. if ($_make_new_dir && !file_exists($_new_dir) && !@mkdir($_new_dir) && !is_dir($_new_dir))
  491. {
  492. trigger_error("problem creating directory '" . $_new_dir . "'");
  493. return false;
  494. }
  495. $_new_dir .= '/';
  496. }
  497. }
  498. }
  499. public static function LicensInformation()
  500. {
  501. $licinfo = array();
  502. $licinfo['PRODUCT'] = 'Prado Portal';
  503. $licinfo['LICTYPE'] = 'Open Source';
  504. $licinfo['CUSTNAME'] = 'Get Your own Open Source licens';
  505. $licinfo['CUSTCONTACTNAME'] = 'Open source is free, but donations are welcome.';
  506. $licinfo['CUSTCONTACTEMAIL'] = 'licens[at]pradoportal.dk';
  507. $licfile = Prado::getPathOfNamespace('Application.Data') . '/' . 'licens.dat';
  508. if(file_exists($licfile))
  509. {
  510. try
  511. {
  512. Prado::using('Application.Common.PortalLicens');
  513. $pl = new PortalLicens();
  514. $vinfo = $pl->validate(file_get_contents($licfile));
  515. if($vinfo['RESULT'] == 'OK')
  516. {
  517. if($vinfo['DATA']['PRODUCT'] === 'pradoportal' && $vinfo['DATA']['LICTYPE'] === 'opensource' && $vinfo['DATA']['CUSTCONTACTEMAIL'] === 'info@pradoportal.dk')
  518. {
  519. $licinfo['PRODUCT'] = 'Prado Portal';
  520. $licinfo['LICTYPE'] = 'Open Source';
  521. $licinfo['CUSTNAME'] = 'Get Your own Open Source licens';
  522. $licinfo['CUSTCONTACTNAME'] = 'Open source is free, but donations are welcome.';
  523. $licinfo['CUSTCONTACTEMAIL'] = 'licens[at]pradoportal.dk';
  524. }
  525. else
  526. {
  527. $licinfo = $vinfo['DATA'];
  528. $licinfo['PRODUCT'] = ($vinfo['DATA']['PRODUCT'] === 'pradoportal' ? 'Prado Portal': $vinfo['DATA']['PRODUCT']);
  529. $licinfo['LICTYPE'] = ($vinfo['DATA']['LICTYPE'] === 'opensource' ? 'Open Source' : $vinfo['DATA']['LICTYPE']);
  530. }
  531. }
  532. }
  533. catch(Exception $e)
  534. {
  535. $licinfo['PRODUCT'] = 'Prado Portal';
  536. $licinfo['LICTYPE'] = 'Open Source';
  537. $licinfo['CUSTNAME'] = 'Get Your own Open Source licens';
  538. $licinfo['CUSTCONTACTNAME'] = 'Open source is free, but donations are welcome.';
  539. $licinfo['CUSTCONTACTEMAIL'] = 'licens[at]pradoportal.dk';
  540. }
  541. }
  542. return $licinfo;
  543. }
  544. public static function LicenseType()
  545. {
  546. $licinfo = self::LicensInformation();
  547. return $licinfo['LICTYPE'];
  548. }
  549. public static function DeleteDirStructure($dir,$p_deldir = false)
  550. {
  551. $current_dir = @opendir($dir);
  552. while($entryname = readdir($current_dir))
  553. {
  554. if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!=".."))
  555. {
  556. PortalUtil::DeleteDirStructure("${dir}/${entryname}",true);
  557. }
  558. elseif($entryname != "." and $entryname!="..")
  559. {
  560. @unlink("${dir}/${entryname}");
  561. }
  562. }
  563. @closedir($current_dir);
  564. if($p_deldir)
  565. {
  566. @rmdir($dir);
  567. }
  568. }
  569. public static function MakePassword()
  570. {
  571. $makepass = "";
  572. $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  573. srand((double)microtime()*1000000);
  574. $i = 0;
  575. while ($i <= 7)
  576. {
  577. $num = rand() % 33;
  578. $tmp = substr($salt, $num, 1);
  579. $makepass = $makepass . $tmp;
  580. $i++;
  581. }
  582. return ($makepass);
  583. }
  584. public static function getIP()
  585. {
  586. // Find the user's IP address. (but don't let it give you 'unknown'!)
  587. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_CLIENT_IP']) && (preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['HTTP_CLIENT_IP']) == 0 || preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['REMOTE_ADDR']) != 0))
  588. {
  589. // We have both forwarded for AND client IP... check the first forwarded for as the block - only switch if it's better that way.
  590. if (strtok($_SERVER['HTTP_X_FORWARDED_FOR'], '.') != strtok($_SERVER['HTTP_CLIENT_IP'], '.') && '.' . strtok($_SERVER['HTTP_X_FORWARDED_FOR'], '.') == strrchr($_SERVER['HTTP_CLIENT_IP'], '.') && (preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['HTTP_X_FORWARDED_FOR']) == 0 || preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['REMOTE_ADDR']) != 0))
  591. $_SERVER['REMOTE_ADDR'] = implode('.', array_reverse(explode('.', $_SERVER['HTTP_CLIENT_IP'])));
  592. else
  593. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CLIENT_IP'];
  594. }
  595. if (!empty($_SERVER['HTTP_CLIENT_IP']) && (preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['HTTP_CLIENT_IP']) == 0 || preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['REMOTE_ADDR']) != 0))
  596. {
  597. // Since they are in different blocks, it's probably reversed.
  598. if (strtok($_SERVER['REMOTE_ADDR'], '.') != strtok($_SERVER['HTTP_CLIENT_IP'], '.'))
  599. $_SERVER['REMOTE_ADDR'] = implode('.', array_reverse(explode('.', $_SERVER['HTTP_CLIENT_IP'])));
  600. else
  601. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CLIENT_IP'];
  602. }
  603. elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  604. {
  605. // If there are commas, get the last one.. probably.
  606. if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false)
  607. {
  608. $ips = array_reverse(explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']));
  609. // Go through each IP...
  610. foreach ($ips as $i => $ip)
  611. {
  612. // Make sure it's in a valid range...
  613. if (preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $ip) != 0 && preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['REMOTE_ADDR']) == 0)
  614. continue;
  615. // Otherwise, we've got an IP!
  616. $_SERVER['REMOTE_ADDR'] = trim($ip);
  617. break;
  618. }
  619. }
  620. // Otherwise just use the only one.
  621. elseif (preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['HTTP_X_FORWARDED_FOR']) == 0 || preg_match('~^((0|10|172\.16|192\.168|255|127\.0)\.|unknown)~', $_SERVER['REMOTE_ADDR']) != 0)
  622. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
  623. }
  624. elseif (!isset($_SERVER['REMOTE_ADDR']))
  625. $_SERVER['REMOTE_ADDR'] = '';
  626. }
  627. public static function getGUID()
  628. {
  629. // The field names refer to RFC 4122 section 4.1.2
  630. return sprintf(/*'%04x%04x-%04x-%03x4-%04x-%04x%04x%04x'*/ '%04x%04x-%04x-4%03x-%04x-%04x%04x%04x',
  631. mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
  632. mt_rand(0, 65535), // 16 bits for "time_mid"
  633. mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
  634. bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
  635. // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
  636. // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
  637. // 8 bits for "clk_seq_low"
  638. mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
  639. );
  640. }
  641. public static function getPagesAsUrl()
  642. {
  643. $app = Prado::getApplication();
  644. $db = $app->DataAccess;
  645. $service = $app->Service;
  646. $p_name = array();
  647. $p_url = array();
  648. $pages = $db->createCommand("select fullpath from tblpages")->query()->readAll();
  649. if($pages && is_array($pages) && count($pages) > 0)
  650. {
  651. foreach($pages as $page)
  652. {
  653. $p_name[] = "{" . $page['fullpath'] . "}";
  654. $p_url[] = $service->constructUrl($page['fullpath']);
  655. }
  656. }
  657. return array($p_name,$p_url);
  658. }
  659. public static function getMemoryUsage($real = true)
  660. {
  661. return (memory_get_peak_usage($real) / 1024) . " kb";
  662. }
  663. public static function GenId($seqname='ppseq', $startID=1)
  664. {
  665. $db = Prado::getApplication()->DbConnection;
  666. if($db)
  667. {
  668. try
  669. {
  670. $getnext = sprintf("update %s set id=id+1;", $seqname);
  671. $result = $db->createCommand($getnext)->execute();
  672. return $db->createCommand("select id from $seqname")->queryScalar();
  673. }
  674. catch(Exception $e)
  675. {
  676. $db->createCommand(sprintf("drop table if exists %s",$seqname))->execute();
  677. $db->createCommand(sprintf("create table %s (id int not null)",$seqname))->execute();
  678. $db->createCommand(sprintf("insert into %s values (%d)",$seqname, $startID-1))->execute();
  679. return PortalUtil::GenID($seqname,$startID);
  680. }
  681. }
  682. else
  683. {
  684. PortalUtil::fatalError("Faild to connect to the database");
  685. }
  686. }
  687. public static function CreateSequence($seqname='adodbseq',$startID=1)
  688. {
  689. $db = Prado::getApplication()->DbConnection;
  690. if($db)
  691. {
  692. try
  693. {
  694. $db->createCommand(sprintf("create table %s (id int not null)",$seqname))->execute();
  695. }
  696. catch(Exception $e)
  697. {
  698. PortalUtil::fatalError("Db error: " . $e->getMessage());
  699. }
  700. }
  701. else
  702. {
  703. PortalUtil::fatalError("Faild to connect to the database");
  704. }
  705. }
  706. public static function DropSequence($seqname='adodbseq')
  707. {
  708. $db = Prado::getApplication()->DbConnection;
  709. if($db)
  710. {
  711. try
  712. {
  713. $db->createCommand(sprintf("drop table if exists %s",$seqname))->execute();
  714. }
  715. catch(Exception $e)
  716. {
  717. PortalUtil::fatalError("Db error: " . $e->getMessage());
  718. }
  719. }
  720. else
  721. {
  722. PortalUtil::fatalError("Faild to connect to the database");
  723. }
  724. }
  725. public static function PathName($p_path, $p_createdir = false)
  726. {
  727. $retval = "";
  728. $retval = str_replace('\\','/',$p_path);
  729. if(substr($retval,-1) != '/')
  730. {
  731. $retval .= '/';
  732. }
  733. // Remove double //
  734. $retval = str_replace('//','/',$retval);
  735. if($p_createdir)
  736. {
  737. @mkdir($p_path,0777,true);
  738. }
  739. return $retval;
  740. }
  741. public static function DbTableExists($tblname)
  742. {
  743. $db = Prado::getApplication()->DbConnection;
  744. $sql = "show tables like '$tblname'";
  745. $dbresult = $db->createCommand($sql)->query();
  746. if($dbresult === null)
  747. {
  748. $retval = false;
  749. }
  750. else
  751. {
  752. $retval = true;
  753. }
  754. return $retval;
  755. }
  756. public static function CreateLangString($string, $key = 'APPLICATION')
  757. {
  758. if($string === "" || empty($string))
  759. {
  760. return;
  761. }
  762. Prado::using('Application.Common.Data.PortalLanguageRecord');
  763. Prado::using('Application.Common.Data.PortalLanguageStringRecord');
  764. // Get Active languages
  765. $actLang = PortalLanguageRecord::finder()->findAll("active =?",1);
  766. if($actLang !== 0 && is_array($actLang) && count($actLang) > 0)
  767. {
  768. foreach($actLang as $aLang)
  769. {
  770. $lsRec = PortalLanguageStringRecord::finder()->findByculture_AND_fromvalue($aLang->culture,$string);
  771. if($lsRec === null)
  772. {
  773. $lsRec = new PortalLanguageStringRecord();
  774. $lsRec->culture = $aLang->culture;
  775. $lsRec->lkey = 'APPLICATION';
  776. $lsRec->fromvalue = $string;
  777. $lsRec->tovalue = $string;
  778. $lsRec->save();
  779. }
  780. }
  781. }
  782. }
  783. public static function request_uri()
  784. {
  785. $uri = "";
  786. if (isset($_SERVER['REQUEST_URI']))
  787. {
  788. $uri = $_SERVER['REQUEST_URI'];
  789. }
  790. else
  791. {
  792. if (isset($_SERVER['argv']))
  793. {
  794. $uri = $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['argv'][0];
  795. }
  796. elseif (isset($_SERVER['QUERY_STRING']))
  797. {
  798. $uri = $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING'];
  799. }
  800. else
  801. {
  802. $uri = $_SERVER['SCRIPT_NAME'];
  803. }
  804. }
  805. return $uri;
  806. }
  807. }
  808. ?>