1: <?php
2: declare(strict_types=1);
3: /**
4: * +------------------------------------------------------------+
5: * | apnscp |
6: * +------------------------------------------------------------+
7: * | Copyright (c) Apis Networks |
8: * +------------------------------------------------------------+
9: * | Licensed under Artistic License 2.0 |
10: * +------------------------------------------------------------+
11: * | Author: Matt Saladna (msaladna@apisnetworks.com) |
12: * +------------------------------------------------------------+
13: */
14:
15: /**
16: * Provides demonstrative results of various module invocations
17: *
18: * @package core
19: */
20: class Demo_Module extends Module_Skeleton
21: {
22: protected $exportedFunctions =
23: array('*' => PRIVILEGE_ALL,);
24:
25: /* }}} */
26:
27: /**
28: * Test AJAX tail support
29: *
30: * @param int $cnt
31: * @param double $timing
32: */
33: public function test_tail($cnt = 20, $timing = 0.5)
34: {
35: if (!is_debug()) {
36: return error('test_tail may not be used in production');
37: }
38: if (!IS_CLI) {
39: return $this->query('demo_test_tail', $cnt, $timing);
40: }
41: $tail = Util_Process_Tee::watch(new Util_Process);
42: $script = 'echo ' . date('r') . '; for (( C=' . $cnt . '0; C > 0; C-- )) ; do echo $C; usleep ' . ($timing * 100000) . '; done';
43: $tail->exec($script);
44: $script = 'echo ' . date('r') . '; for (( C=0; C < ' . $cnt . ' ; C++ )) ; do echo $C; usleep ' . ($timing * 1000000) . '; done ; echo `date`';
45: $tail->exec($script);
46:
47: return true;
48: }
49:
50: public function test_tail2($cnt = 20, $timing = 0.5)
51: {
52: if (!is_debug()) {
53: return error('test_tail may not be used in production');
54: }
55: if (!IS_CLI) {
56: return $this->query('demo_test_tail2', $cnt, $timing);
57: }
58: $p = new Util_Process();
59: $proc = Util_Process_Tee::auto($p);
60: $script = 'echo ' . date('r') . '; for (( C=' . $cnt . '0; C > 0; C-- )) ; do echo $C; usleep ' . ($timing * 10000) . '; done';
61: $proc->exec($script);
62: $script = 'echo ' . date('r') . '; for (( C=0; C < ' . $cnt . ' ; C++ )) ; do echo $C; usleep ' . ($timing * 10000) . '; done ; echo `date`';
63: $proc->exec($script);
64: $p = null;
65: sleep(10);
66: }
67:
68: public function test_tail3()
69: {
70: if (!is_debug()) {
71: return error('test_tail may not be used in production');
72: }
73: $proc = Util_Process_Tee::auto(new Util_Process());
74: $proc->exec('idontexist');
75: $proc->log('Testing!');
76: $proc->exec('whoami');
77:
78: return true;
79: }
80:
81: public function test_error($message = 'generic error')
82: {
83: return error($message);
84: }
85:
86: public function test1()
87: {
88: return Util_Process::exec('test.sh');
89: }
90:
91: public function test_backend_error($message = 'abcdef')
92: {
93: if (!IS_CLI) {
94: return $this->query('demo_test_backend_error', $message);
95: }
96:
97: return error($message);
98:
99: }
100:
101: public function test_sudo()
102: {
103: if (!IS_CLI) {
104: return $this->query('demo_test_sudo');
105: }
106:
107: return Util_Process_Sudo::exec('echo whoami: `whoami`');
108: }
109:
110: /**
111: * array test_array(int[, int])
112: * Demonstration test of returning an array. If invoked through a SOAP
113: * call, this would return a three tuple object, non-associative, handled
114: * by the function interceptor class
115: *
116: * @param int $arg1 integer to add
117: * @param int $arg2 integer to add
118: * @return array associative array containing indicies ret1, ret2, and ret3
119: * @privilege PRIVILEGE_ALL
120: */
121: public function test_array($arg1, $arg2 = null)
122: {
123: return array('ret1' => 80, 'ret2' => 10, 'ret3' => $arg2 + $arg1);
124: }
125:
126:
127: /**
128: * bool test_exception([bool = false])
129: * Tests exception handling of modules
130: *
131: * @privilege PRIVILEGE_ALL
132: * @param bool $trigger whether to trigger an exception or just return true
133: * @return bool Returns true if no exception to be thrown,
134: * exception obj otherwise, which is derived from the Exception
135: * class
136: */
137: public function test_exception($trigger = false)
138: {
139: if ($trigger) {
140: return new SocketError('This is another test for objects');
141: } else {
142: return true;
143: }
144: }
145: /* }}} */
146:
147: /**
148: * int test_scalar(int, int)
149: *
150: * @privilege PRIVILEGE_ALL
151: * @param int $arg1 integer to add
152: * @param int $arg2 integer to add
153: * Similar to test_array, but returns the scalar sum of $arg1 + $arg2
154: * @return int sum of $arg1 + $arg2
155: */
156: public function test_scalar($arg1, $arg2)
157: {
158: return $arg1 + $arg2;
159: }
160:
161: /* }}} */
162:
163: public function test_basic()
164: {
165: return 'Hello World!';
166: }
167:
168: public function test_account_metadata()
169: {
170: return $this->getServiceValue('sendmail', 'mailserver');
171: }
172: }
173:
174: ?>
175: