1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36:
37: 38: 39:
40: class AuxLib {
41:
42: 43: 44:
45: public static function getFileUploadErrorMessage ($errCode) {
46: switch ($errCode) {
47: case UPLOAD_ERR_OK:
48: break;
49: case UPLOAD_ERR_FORM_SIZE:
50: case UPLOAD_ERR_INI_SIZE:
51: $errMsg = Yii::t('app', 'File exceeds the maximum upload size.');
52: break;
53: case UPLOAD_ERR_PARTIAL:
54: $errMsg = Yii::t('app', 'File upload was not completed.');
55: break;
56: case UPLOAD_ERR_NO_FILE:
57: $errMsg = Yii::t('app', 'Zero-length file uploaded.');
58: break;
59: case UPLOAD_ERR_NO_TMP_DIR:
60: break;
61: case UPLOAD_ERR_CANT_WRITE:
62: break;
63: case UPLOAD_ERR_EXTENSION:
64: break;
65: default:
66: $errMsg = Yii::t('app', 'Failed to upload file.');
67: }
68: return $errMsg;
69: }
70:
71: 72: 73:
74: public static function checkFileUploadError ($name) {
75: if (!isset ($_FILES[$name])) return false;
76: if(empty($_FILES[$name]['tmp_name']))
77: return true;
78: return false;
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88: 89:
90: public static function registerTranslationsScript (
91: $namespace, $messages, $translationFile='app', $scriptName='passMsgsToClientScript') {
92:
93: $passVarsToClientScript = "
94: if (!x2.".$namespace.") x2.".$namespace." = {};
95: x2.".$namespace.".translations = {};
96: ";
97: foreach ($messages as $key=>$val) {
98: $passVarsToClientScript .= "x2.".$namespace.".translations['".
99: $key. "'] = '" . addslashes (Yii::t($translationFile, $val)) . "';\n";
100: }
101: Yii::app()->clientScript->registerScript(
102: $scriptName, $passVarsToClientScript,
103: CClientScript::POS_HEAD);
104: }
105:
106: 107: 108: 109: 110: 111: 112:
113: public static function registerPassVarsToClientScriptScript (
114: $namespace, $vars, $scriptName='passVarsToClientScript') {
115:
116: $namespaces = explode ('.', $namespace);
117: $rootNamespace = array_shift ($namespaces);
118:
119:
120: $passVarsToClientScript = "
121: ;(function () {
122: if (typeof ".$rootNamespace." === 'undefined') ".$rootNamespace." = {};
123: var namespaces = ".CJSON::encode ($namespaces).";
124: var prevNameSpace = ".$rootNamespace.";
125:
126: for (var i in namespaces) {
127: if (typeof prevNameSpace[namespaces[i]] === 'undefined') {
128: prevNameSpace[namespaces[i]] = {};
129: }
130: prevNameSpace = prevNameSpace[namespaces[i]];
131: }
132: }) ();
133: ";
134: foreach ($vars as $key=>$val) {
135: $passVarsToClientScript .= $namespace.".".$key." = ".$val.";";
136: }
137: Yii::app()->clientScript->registerScript(
138: $scriptName, $passVarsToClientScript,
139: CClientScript::POS_HEAD);
140: }
141:
142: 143: 144: 145:
146: public static function printTestError ($message) {
147: if (YII_DEBUG) echo CJSON::encode (array ('error' => array (Yii::t('app', $message))));
148: }
149:
150: 151: 152:
153: public static function printError ($message) {
154: echo CJSON::encode (array (false, $message));
155: }
156:
157: 158: 159:
160: public static function printSuccess ($message) {
161: echo CJSON::encode (array (true, $message));
162: }
163:
164: 165: 166: 167: 168: 169: 170: 171:
172: public static function ajaxReturn ($success, $successMessage, $errorMessage) {
173: if ($success) {
174: self::printSuccess ($successMessage);
175: } else {
176: self::printError ($errorMessage);
177: }
178: }
179:
180: 181: 182:
183: public static function debugLog ($message) {
184: if (!YII_DEBUG) return;
185: Yii::log ($message, 'error', 'application.debug');
186: }
187:
188: public static function debugLogR ($arr) {
189: if (!YII_DEBUG) return;
190: $logMessage = print_r ($arr, true);
191: Yii::log ($logMessage, 'error', 'application.debug');
192: }
193:
194: 195: 196: 197:
198: public static function debugLogHd ($data) {
199: if (!YII_DEBUG) return;
200: static $from = '';
201: static $to = '';
202: static $width = 16;
203: static $pad = '.';
204:
205: if ($from==='') {
206: for ($i=0; $i<=0xFF; $i++) {
207: $from .= chr($i);
208: $to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
209: }
210: }
211:
212: $hex = str_split(bin2hex($data), $width*2);
213: $chars = str_split(strtr($data, $from, $to), $width);
214:
215: $output = "\n";
216: $offset = 0;
217: foreach ($hex as $i => $line) {
218: $hexBytes = str_split($line, 16);
219: $hexContent = implode(' ', str_split($hexBytes[0],2));
220: if (isset($hexBytes[1]))
221: $hexContent .= ' '.implode(' ', str_split($hexBytes[1],2));
222: $output .= sprintf('%06X %-48s',$offset, $hexContent). ' |' . $chars[$i] . "|\n";
223: $offset += $width;
224: }
225: self::debugLog ($output);
226: }
227:
228: 229: 230:
231: public static function logR ($arr, $route) {
232: $logMessage = print_r ($arr, true);
233: Yii::log ($logMessage, 'error', 'application.'.$route);
234: }
235:
236: public static function debugLogExport ($arr) {
237: if (!YII_DEBUG) return;
238: $logMessage = var_export ($arr, true);
239: Yii::log ($logMessage, 'error', 'application.debug');
240: }
241:
242: public static function isIE8 () {
243: $userAgentStr = strtolower(Yii::app()->request->userAgent);
244: return preg_match('/msie 8/i', $userAgentStr);
245: }
246:
247: public static function isIE () {
248: $userAgentStr = strtolower(Yii::app()->request->userAgent);
249: return preg_match('/msie/i', $userAgentStr);
250: }
251:
252: public static function isAndroid () {
253: $userAgentStr = strtolower(Yii::app()->request->userAgent);
254: return preg_match('/android/', $userAgentStr);
255: }
256:
257: public static function isIPad () {
258: $userAgentStr = strtolower(Yii::app()->request->userAgent);
259: return preg_match('/ipad/', $userAgentStr);
260: }
261:
262: public static function getLayoutType () {
263: $pathInfo = strtolower(Yii::app()->request->getPathInfo ());
264: if (AuxLib::isIE8 () || strpos ($pathInfo, 'admin') === 0 ||
265: preg_match ('/flowDesigner(\/\d+)?$/', $pathInfo)) {
266:
267: return 'static';
268: } else {
269: return 'responsive';
270: }
271: }
272:
273: 274: 275:
276: public static function getIEVer () {
277: $userAgentStr = strtolower(Yii::app()->request->userAgent);
278: preg_match('/msie ([0-9]+)/', $userAgentStr, $matches);
279: if (sizeof ($matches) === 2) {
280: $ver = (int) $matches[1];
281: } else {
282: $ver = INF;
283: }
284: return $ver;
285: }
286:
287: public static function setCookie ($key, $val, $time) {
288: if (YII_DEBUG) {
289: $serverName = Yii::app()->request->getServerName() === 'localhost' ? '' :
290: Yii::app()->request->getServerName();
291: } else {
292: $serverName = Yii::app()->request->getServerName();
293: }
294: setcookie($key,$val,time()+$time,dirname(Yii::app()->request->getScriptUrl()), $serverName);
295: }
296:
297: public static function clearCookie ($key){
298: if(YII_DEBUG){
299: $serverName = Yii::app()->request->getServerName() === 'localhost' ? '' :
300: Yii::app()->request->getServerName();
301: }else{
302: $serverName = Yii::app()->request->getServerName();
303: }
304: unset($_COOKIE[$key]);
305: setcookie(
306: $key, '', time() - 3600, dirname(Yii::app()->request->getScriptUrl()), $serverName);
307: }
308:
309: 310: 311: 312: 313: 314:
315: public static function bindArray ($arr, $prefix='X2') {
316: $placeholders = array ();
317: $arrLen = sizeof ($arr);
318: for ($i = 0; $i < $arrLen; ++$i) {
319: $placeholders[] = ':' . $prefix . $i;
320: }
321: if ($arrLen === 0) {
322: return array ();
323: }
324: return array_combine ($placeholders, $arr);
325: }
326:
327: public static function arrToStrList ($arr) {
328: return '('.implode (',', $arr).')';
329: }
330:
331: 332: 333:
334: public static function coerceToArray (&$arr) {
335: $arr = ArrayUtil::coerceToArray ($arr);
336: }
337:
338: 339: 340: 341: 342: 343:
344: public static function trace ($limit=null) {
345: if ($limit !== null) {
346: AuxLib::debugLogR (
347: array_slice (debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS), 0, $limit));
348: } else {
349: AuxLib::debugLogR (debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS));
350: }
351: }
352:
353: 354: 355: 356: 357:
358: public static function dropdownForJson($options) {
359: $dropdownData = array();
360: foreach($options as $value => &$label)
361: $dropdownData[] = array($value,$label);
362: return $dropdownData;
363: }
364:
365: public static function println ($message) {
366: print ($message."\n");
367: }
368:
369: public static function issetIsArray ($param) {
370: return (isset ($param) && is_array ($param));
371: }
372:
373: public static function captureOutput ($fn) {
374: ob_start();
375: ob_implicit_flush(false);
376: $fn ();
377: return ob_get_clean();
378: }
379:
380:
381: public static function isMac () {
382: $user_agent = getenv ("HTTP_USER_AGENT");
383: return (strpos ($user_agent, "Mac") !== false);
384: }
385:
386:
387: public static function isAjax () {
388: return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
389:
390: }
391:
392: public static function getRequestUrl () {
393: $protocol = !empty ($_SERVER['HTTPS']) ? 'https' : 'http';
394: $baseUrl = $protocol . '://' . $_SERVER['HTTP_HOST'];
395: $uri = $_SERVER['REQUEST_URI'];
396: return $baseUrl.$uri;
397: }
398:
399:
400: }
401: