PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/library/X/Env.php

http://vlc-shares.googlecode.com/
PHP | 349 lines | 227 code | 42 blank | 80 comment | 53 complexity | 71b3e197b3e734d2158bff9acc056953 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. require_once 'Zend/Translate.php';
  3. require_once 'Zend/Config.php';
  4. require_once 'Zend/Controller/Front.php';
  5. require_once 'X/VlcShares/Plugins/Abstract.php';
  6. require_once 'X/VlcShares/Plugins.php';
  7. class X_Env {
  8. const EXECUTE_OUT_NONE = 0;
  9. const EXECUTE_OUT_LASTLINE = 1;
  10. const EXECUTE_OUT_ARRAY = 2;
  11. const EXECUTE_OUT_IMPLODED = 3;
  12. const EXECUTE_PS_WAIT = 0;
  13. const EXECUTE_PS_BACKGROUND = 1;
  14. const EXECUTE_PS_BACKGROUND_SPECIAL = 2;
  15. private static $_isWindows = null;
  16. private static $_psExec = null;
  17. private static $_debug = null;
  18. private static $_pluginsEvents = array();
  19. /**
  20. * @var Zend_Translate
  21. */
  22. private static $_translator = null;
  23. private static $_forcedPort = '80';
  24. private static $_serverUrlProvider = null;
  25. static public function initForcedPort($port) {
  26. self::$_forcedPort = $port;
  27. X_Debug::i("Apache port is: $port");
  28. }
  29. static public function isWindows() {
  30. if ( is_null(self::$_isWindows) ) {
  31. self::$_isWindows = ( array_key_exists('WINDIR', $_SERVER) || array_key_exists('windir', $_SERVER));
  32. }
  33. return self::$_isWindows;
  34. }
  35. static function isset_or($value, $alternative) {
  36. return (isset($value) ? $value : $alternative);
  37. }
  38. /**
  39. * Execute a call in system env
  40. * @param string $command command to be executed
  41. * @param int $outputType type of output
  42. * @param int $spawnType type of execution (synch or asynch)
  43. */
  44. static public function execute($command, $outputType = self::EXECUTE_OUT_LASTLINE, $spawnType = self::EXECUTE_PS_WAIT) {
  45. $output = array();
  46. $lastLine = '';
  47. if ( $spawnType == self::EXECUTE_PS_BACKGROUND || $spawnType == self::EXECUTE_PS_BACKGROUND_SPECIAL ) {
  48. if ( self::isWindows() ) {
  49. // su windows leggo quanto tornato perche puo servire per il pid
  50. //$command = /*self::_psExec().*/$command;
  51. $WshShell = new COM("WScript.Shell");
  52. X_Debug::i("Executing: $command");
  53. $lastLine = $WshShell->Run($command, 0,false);
  54. } else {
  55. // su linux ignoro semplicemente l'output
  56. if ( $spawnType == self::EXECUTE_PS_BACKGROUND ) {
  57. $command = trim($command).' > /dev/null 2>&1';
  58. }
  59. X_Debug::i("Executing: $command");
  60. if ( $outputType == self::EXECUTE_OUT_NONE ) {
  61. $lastLine = exec($command);
  62. } else {
  63. $lastLine = exec($command, $output);
  64. }
  65. }
  66. } else {
  67. X_Debug::i("Executing: $command");
  68. if ( $outputType == self::EXECUTE_OUT_NONE ) {
  69. $lastLine = exec($command);
  70. } else {
  71. $lastLine = exec($command, $output);
  72. }
  73. }
  74. switch ($outputType) {
  75. case self::EXECUTE_OUT_NONE:
  76. return;
  77. case self::EXECUTE_OUT_LASTLINE:
  78. return $lastLine;
  79. case self::EXECUTE_OUT_IMPLODED:
  80. return implode('', $output);
  81. case self::EXECUTE_OUT_ARRAY:
  82. default:
  83. return $output;
  84. }
  85. }
  86. static private function _psExec() {
  87. if ( is_null(self::$_psExec) ) {
  88. self::$_psExec = '"'.dirname(__FILE__).'/PsExec.exe" -d ';
  89. }
  90. return self::$_psExec;
  91. }
  92. /**
  93. * Init debug system
  94. * @deprecated
  95. * @see X_Debug::init()
  96. * @param string $path
  97. */
  98. static public function initDebug($path) {
  99. X_Debug::init($path);
  100. }
  101. /**
  102. * Add a new message in debug log
  103. * @deprecated
  104. * @see X_Debug::i()
  105. * @param string $message
  106. */
  107. static public function debug($message) {
  108. X_Debug::i($message, 2);
  109. }
  110. /**
  111. * @deprecated
  112. * @param Zend_Config $options
  113. */
  114. static public function initPlugins(Zend_Config $options) {
  115. return X_VlcShares_Plugins::init($options);
  116. }
  117. /**
  118. * @deprecated
  119. * @param unknown_type $eventName
  120. * @param X_VlcShares_Plugins_Abstract $plugin
  121. * @param unknown_type $methodName
  122. */
  123. static public function registerPlugin($eventName, X_VlcShares_Plugins_Abstract $plugin, $methodName) {
  124. return X_VlcShares_Plugins::register($eventName, $plugin, $methodName);
  125. }
  126. /**
  127. * @deprecated
  128. * @param unknown_type $eventName
  129. * @param unknown_type $pluginId
  130. */
  131. static public function unregisterPlugin($eventName, $pluginId) {
  132. return X_VlcShares_Plugins::unregisterPlugin($eventName, $pluginId);
  133. }
  134. /**
  135. * @deprecated
  136. * @param unknown_type $eventName
  137. */
  138. static public function unregisterAll($eventName = null) {
  139. return X_VlcShares_Plugins::unregisterAll($eventName);
  140. }
  141. /**
  142. * @deprecated
  143. * @param unknown_type $eventName
  144. * @param unknown_type $args
  145. */
  146. static public function triggerEvent($eventName, &$args = array()) {
  147. return X_VlcShares_Plugins::trigger($eventName, $args);
  148. }
  149. static public function routeLink($controller = 'index', $action = 'index', $params = array()) {
  150. $link = 'http://';
  151. $link .= $_SERVER['HTTP_HOST'];
  152. if ( self::$_forcedPort != '80' || $_SERVER['SERVER_PORT'] != '80' ) {
  153. if ( strpos($_SERVER["HTTP_HOST"], ':') === false ) {
  154. $link .= ':'.($_SERVER["SERVER_PORT"] == '80' ? self::$_forcedPort : $_SERVER["SERVER_PORT"]);
  155. }
  156. }
  157. //$link .= '/'.;
  158. $link .= Zend_Controller_Front::getInstance()->getBaseUrl() . "/$controller/$action";
  159. foreach ($params as $key => $value) {
  160. $link.= "/$key/$value";
  161. }
  162. return $link;
  163. }
  164. static public function completeUrl($url) {
  165. $link = 'http://';
  166. $link .= $_SERVER['HTTP_HOST'];
  167. if ( self::$_forcedPort != '80' || $_SERVER['SERVER_PORT'] != '80' ) {
  168. if ( strpos($_SERVER["HTTP_HOST"], ':') === false ) {
  169. $link .= ':'.($_SERVER["SERVER_PORT"] == '80' ? self::$_forcedPort : $_SERVER["SERVER_PORT"]);
  170. }
  171. }
  172. return $link.$url;
  173. }
  174. static private $_stringsWriter = null;
  175. /**
  176. * Set a static reference to a global Zend_Translate object
  177. * usable through self::_() function
  178. * If called more then one time, from the second call
  179. * the $translator object will be appended to the previous one
  180. * @param Zend_Translate $translator
  181. */
  182. static public function initTranslator(Zend_Translate $translator) {
  183. //if ( true ) self::$_stringsWriter = new StringsWriter();
  184. if ( is_null(self::$_translator) ) {
  185. self::$_translator = $translator;
  186. X_Debug::i("Translator enabled");
  187. } else {
  188. self::$_translator->getAdapter()->addTranslation($translator);
  189. }
  190. }
  191. /**
  192. * @param string message id
  193. * @param mixed ...args a long list of params
  194. */
  195. static public function _($message) {
  196. //if ( !is_null(self::$_stringsWriter)) self::$_stringsWriter->_($message);
  197. if ( !is_null(self::$_translator) ) {
  198. if ( false && APPLICATION_ENV == 'development') {
  199. return "#(".self::$_translator->_($message).")";
  200. } else {
  201. $return = self::$_translator->_($message);
  202. if ( func_num_args() > 1 ) {
  203. $args = func_get_args();
  204. array_shift($args);
  205. // remove warning output
  206. if ( $message != $return ) {
  207. $return = @vsprintf($return, $args);
  208. } else {
  209. $return = "#($return, ".implode(', ', $args).")";
  210. }
  211. }
  212. return $return;
  213. }
  214. }
  215. }
  216. static public function formatTime($seconds) {
  217. return floor($seconds/3600) . ':' . str_pad((floor($seconds/60) % 60), 2, "0", STR_PAD_LEFT) . ":" . str_pad($seconds % 60, 2, "0", STR_PAD_LEFT);
  218. }
  219. static public function startWith($string, $substring) {
  220. return (substr($string, 0, strlen($substring)) == $substring);
  221. }
  222. /**
  223. * Encode $string in a url-safe way
  224. * @param string $string
  225. * @return string
  226. */
  227. static public function encode($string) {
  228. return str_replace('/', '_', base64_encode($string));
  229. }
  230. /**
  231. * Decode $string in a url-safe way
  232. * @param string $string
  233. * @return string
  234. */
  235. static public function decode($string) {
  236. return base64_decode(str_replace('_', '/', $string));
  237. }
  238. static public function codeFormat($string, $addLevel = '{', $sameLevel = ',', $removeLevel = '}') {
  239. $string = str_replace(array("\n", "\r", "\t"), '', $string);
  240. $string = str_replace(array($addLevel, $sameLevel, $removeLevel), array($addLevel."\n", $sameLevel."\n", "\n".$removeLevel), $string);
  241. $stringArray = explode("\n", $string);
  242. $currentLvl = 0;
  243. foreach ($stringArray as $key => $value) {
  244. $padd = '';
  245. for ( $i = 0; $i < $currentLvl; $i++) {
  246. $padd .= "\t";
  247. }
  248. if ( substr($value, -1) == $addLevel ) {
  249. $stringArray[$key] = $padd . $stringArray[$key];
  250. $currentLvl++;
  251. } elseif ( substr($value, -1) == $sameLevel ) {
  252. $stringArray[$key] = $padd . $stringArray[$key];
  253. } elseif ( substr($value, -1) == $sameLevel ) {
  254. $stringArray[$key] = $padd . $stringArray[$key];
  255. } else {
  256. $stringArray[$key] = $padd . $stringArray[$key];
  257. $currentLvl--;
  258. }
  259. $currentLvl = $currentLvl >= 0 ? $currentLvl : 0;
  260. }
  261. return implode("\n", $stringArray);
  262. }
  263. /**
  264. * Split a string in lines and reduce each line length to a (max size - #length of end) (adding end)
  265. * @param string $string
  266. * @param int $lineMaxLength
  267. * @param string $end
  268. */
  269. static public function reduceLinesMaxLength($string, $lineMaxLength = 50, $end = '') {
  270. // break string in lines
  271. $array = explode("\n", $string);
  272. for ( $i = 0; $i < count($array); $i++) {
  273. if ( strlen($array[$i]) > $lineMaxLength ) {
  274. $array[$i] = substr($array[$i], 0, $lineMaxLength - strlen($end)) . $end;
  275. }
  276. }
  277. return implode("\n", $array);
  278. }
  279. }
  280. class StringsWriter {
  281. private $_stringsQueue = array();
  282. function __construct() {
  283. if ( file_exists(dirname(__FILE__).'/strings.inc')) {
  284. $this->_stringsQueue = unserialize(file_get_contents(dirname(__FILE__).'/strings.inc'));
  285. }
  286. }
  287. function _($string) {
  288. $traceBack = 2;
  289. $btraces = debug_backtrace();
  290. $traces = $btraces[$traceBack];
  291. $func = $traces['function'];
  292. if ( @$traces['class'] ) {
  293. $func = "{$traces['class']}::{$func}";
  294. }
  295. $line = @$btraces[$traceBack-1]['line'];
  296. $this->_stringsQueue[$string] = "$func:$line";
  297. }
  298. function __destruct() {
  299. ksort($this->_stringsQueue);
  300. X_Debug::i(file_put_contents(dirname(__FILE__).'/strings.inc', serialize($this->_stringsQueue)));
  301. file_put_contents(dirname(__FILE__).'/strings.ini', ";File writer output\n\n\n");
  302. foreach ($this->_stringsQueue as $key => $value) {
  303. file_put_contents(dirname(__FILE__).'/strings.ini', "$key=\"$value\"\n", FILE_APPEND );
  304. }
  305. X_Debug::i('Destructor called');
  306. }
  307. }