"Fossies" - the Fresh Open Source Software Archive 
Member "fogproject-1.5.9/packages/web/lib/plugins/site/hooks/addsitehost.hook.php" (13 Sep 2020, 14054 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "addsitehost.hook.php":
1.5.8_vs_1.5.9.
1 <?php
2 /**
3 * Associate Hosts to a Site.
4 *
5 * PHP version 5
6 *
7 * @category AddSiteHost
8 * @package FOGProject
9 * @author Fernando Gietz <fernando.gietz@gmail.com>
10 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
11 * @link https://fogproject.org
12 */
13 /**
14 * Associate Hosts to a Site.
15 *
16 * @category AddSiteHost
17 * @package FOGProject
18 * @author Fernando Gietz <fernando.gietz@gmail.com>
19 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
20 * @link https://fogproject.org
21 */
22 class AddSiteHost extends Hook
23 {
24 /**
25 * The name of this hook.
26 *
27 * @var string
28 */
29 public $name = 'AddSiteHost';
30 /**
31 * The description of this hook.
32 *
33 * @var string
34 */
35 public $description = 'Add Hosts to a Site';
36 /**
37 * The active flag (always true but for posterity)
38 *
39 * @var bool
40 */
41 public $active = true;
42 /**
43 * The node this hook enacts with.
44 *
45 * @var string
46 */
47 public $node = 'site';
48 /**
49 * Initializes object.
50 *
51 * @return void
52 */
53 public function __construct()
54 {
55 parent::__construct();
56 self::$HookManager
57 ->register(
58 'HOST_HEADER_DATA',
59 array(
60 $this,
61 'hostTableHeader'
62 )
63 )
64 ->register(
65 'HOST_DATA',
66 array(
67 $this,
68 'hostData'
69 )
70 )
71 ->register(
72 'HOST_FIELDS',
73 array(
74 $this,
75 'hostFields'
76 )
77 )
78 ->register(
79 'HOST_ADD_SUCCESS',
80 array(
81 $this,
82 'hostAddSite'
83 )
84 )
85 ->register(
86 'HOST_EDIT_SUCCESS',
87 array(
88 $this,
89 'hostAddSite'
90 )
91 )
92 ->register(
93 'HOST_IMPORT',
94 array(
95 $this,
96 'hostImport'
97 )
98 )
99 ->register(
100 'HOST_EXPORT_REPORT',
101 array(
102 $this,
103 'hostExport'
104 )
105 )
106 ->register(
107 'DESTROY_HOST',
108 array(
109 $this,
110 'hostDestroy'
111 )
112 )
113 ->register(
114 'HOST_INFO_EXPOSE',
115 array(
116 $this,
117 'hostInfoExpose'
118 )
119 );
120 }
121 /**
122 * This function modifies the header of the user page.
123 * Add one column calls 'Associated Sites'
124 *
125 * @param mixed $arguments The arguments to modify.
126 *
127 * @return void
128 */
129 public function hostTableHeader($arguments)
130 {
131 global $node;
132 global $sub;
133 if (!in_array($this->node, (array)self::$pluginsinstalled)) {
134 return;
135 }
136 if ($node != 'host') {
137 return;
138 }
139 if ($sub == 'pending') {
140 return;
141 }
142 if (!in_array('accesscontrol', (array)self::$pluginsinstalled)) {
143 $insertIndex = 4;
144 } else {
145 $insertIndex = 5;
146 }
147 foreach ((array)$arguments['headerData'] as $index => &$str) {
148 if ($index == $insertIndex) {
149 $arguments['headerData'][$index] = _('Associated Sites');
150 $arguments['headerData'][] = $str;
151 }
152 unset($str);
153 }
154 }
155 /**
156 * Adjusts the host data.
157 *
158 * @param mixed $arguments The arguments to change.
159 *
160 * @return void
161 */
162 public function hostData($arguments)
163 {
164 global $node;
165 global $sub;
166 if (!in_array($this->node, (array)self::$pluginsinstalled)) {
167 return;
168 }
169 if ($node != 'host') {
170 return;
171 }
172 if ($sub == 'pending') {
173 return;
174 }
175 if (!in_array('accesscontrol', (array)self::$pluginsinstalled)) {
176 $insertIndex = 4;
177 } else {
178 $insertIndex = 5;
179 }
180 foreach ((array)$arguments['attributes'] as $index => &$str) {
181 if ($index == $insertIndex) {
182 $arguments['attributes'][$index] = array();
183 $arguments['attributes'][] = $str;
184 }
185 unset($str);
186 }
187 foreach ((array)$arguments['templates'] as $index => &$str) {
188 if ($index == $insertIndex) {
189 $arguments['templates'][$index] = '${site}';
190 $arguments['templates'][] = $str;
191 }
192 unset($str);
193 }
194 foreach ((array)$arguments['data'] as $index => &$vals) {
195 $find = array(
196 'hostID' => $vals['id']
197 );
198 $Sites = self::getSubObjectIDs(
199 'SiteHostAssociation',
200 $find,
201 'siteID'
202 );
203 $cnt = count($Sites);
204 if ($cnt !== 1) {
205 $arguments['data'][$index]['site'] = '';
206 continue;
207 }
208 $SiteNames = array_values(
209 array_unique(
210 array_filter(
211 self::getSubObjectIDs(
212 'Site',
213 array('id' => $Sites),
214 'name'
215 )
216 )
217 )
218 );
219 $arguments['data'][$index]['site'] = $SiteNames[0];
220 unset($vals);
221 unset($Sites, $SiteNames);
222 }
223 }
224 /**
225 * Adjusts the host fields.
226 *
227 * @param mixed $arguments The arguments to change.
228 *
229 * @return void
230 */
231 public function hostFields($arguments)
232 {
233 global $node;
234 global $sub;
235 if (!in_array($this->node, (array)self::$pluginsinstalled)) {
236 return;
237 }
238 if ($node != 'host') {
239 return;
240 }
241 $SiteIDs = self::getSubObjectIDs(
242 'SiteHostAssociation',
243 array(
244 'hostID' => $arguments['Host']->get('id')
245 ),
246 'siteID'
247 );
248 $cnt = self::getClass('SiteManager')->count(
249 array(
250 'id' => $SiteIDs
251 )
252 );
253 if ($cnt !== 1) {
254 $sID = 0;
255 } else {
256 $Sites = self::getSubObjectIDs(
257 'Site',
258 array('id' => $SiteIDs)
259 );
260 $sID = array_shift($Sites);
261 }
262 $UserIsRestricted = self::getSubObjectIDs(
263 'SiteUserRestriction',
264 array('userID' => self::$FOGUser->get('id')),
265 'isRestricted'
266 )[0];
267 if ($UserIsRestricted == 1) {
268 $SitesFiltered = array_diff(
269 self::getSubObjectIDs(
270 'Site',
271 '',
272 'id'
273 ),
274 self::getSubObjectIDs(
275 'SiteUserAssociation',
276 array('userID' => self::$FOGUser->get('id')),
277 'siteID'
278 )
279 );
280 }
281 self::arrayInsertAfter(
282 '<label for="productKey">'
283 . _('Host Product Key')
284 . '</label>',
285 $arguments['fields'],
286 '<label for="site">'
287 . _('Host Site')
288 . '</label>',
289 self::getClass('SiteManager')->buildSelectBox(
290 $sID,
291 '',
292 'name',
293 $SitesFiltered
294 )
295 );
296 }
297 /**
298 * Adds the site selector to the host.
299 *
300 * @param mixed $arguments The arguments to modify.
301 *
302 * @return void
303 */
304 public function hostAddSite($arguments)
305 {
306 global $node;
307 global $sub;
308 global $tab;
309 if (!in_array($this->node, (array)self::$pluginsinstalled)) {
310 return;
311 }
312 global $node;
313 global $sub;
314 global $tab;
315 $subs = array(
316 'add',
317 'edit',
318 'addPost',
319 'editPost'
320 );
321 if ($node != 'host') {
322 return;
323 }
324 if (!in_array($sub, $subs)) {
325 return;
326 }
327
328 $mac = trim(filter_input(INPUT_POST, 'mac'));
329
330 if (str_replace('_', '-', $tab) != 'host-general') {
331 self::getClass('HostManager')->getHostByMacAddresses($mac);
332 $hostID = self::$Host->get('id');
333 } else {
334 $hostID = $arguments['Host']->get('id');
335 }
336 self::getClass('SiteHostAssociationManager')->destroy(
337 array(
338 'hostID' => $hostID
339 )
340 );
341 $site = (int)filter_input(INPUT_POST, 'site');
342 if ($site) {
343 $insert_fields = array(
344 'siteID',
345 'hostID'
346 );
347 $insert_values = array();
348 $insert_values[] = array(
349 $site,
350 $hostID
351 );
352 if (count($insert_values)) {
353 self::getClass('SiteHostAssociationManager')
354 ->insertBatch(
355 $insert_fields,
356 $insert_values
357 );
358 }
359 }
360 }
361 /**
362 * Adds the site to import.
363 *
364 * @param mixed $arguments The arguments to change.
365 *
366 * @retrun void
367 */
368 public function hostImport($arguments)
369 {
370 if (!in_array($this->node, (array)self::$pluginsinstalled)) {
371 return;
372 }
373 if (!in_array('accesscontrol', (array)self::$pluginsinstalled)) {
374 $insertIndex = 4;
375 } else {
376 $insertIndex = 5;
377 }
378 self::getClass('SiteHostAssociation')
379 ->set('hostID', $argumetns['Host']->get('id'))
380 ->load('hostID')
381 ->set('siteID', $arguments['data'][$insertIndex])
382 ->save();
383 }
384 /**
385 * Adds the site to export.
386 *
387 * @param mixed $arguments The arguments to change.
388 *
389 * return void
390 */
391 public function hostExport($arguments)
392 {
393 if (!in_array($this->node, (array)self::$pluginsinstalled)) {
394 return;
395 }
396 $find = array(
397 'hostID' => $arguments['Host']->id
398 );
399 $Sites = self::getSubObjectIDs(
400 'SiteHostAssociation',
401 $find,
402 'siteID'
403 );
404 $cnt = self::getClass('SiteHostAssociationManager')->count(
405 array('id' => $Sites)
406 );
407 if ($cnt !== 1) {
408 $arguments['report']->addCSVCell('');
409 return;
410 }
411 Route::listem(
412 'site',
413 'name',
414 false,
415 array('id' => $Sites)
416 );
417 $Sites = json_decode(
418 Route::getData()
419 );
420 $Sites = $Sites->sites;
421 foreach ((array)$Sites as &$Site) {
422 $arguments['report']->addCSVCell(
423 $Site->id
424 );
425 unset($Site);
426 }
427 unset($Sites);
428 }
429 /**
430 * Removes site when host is destroyed.
431 *
432 * @param mixed $arguments The arguments to change.
433 *
434 * @return void
435 */
436 public function hostDestroy($arguments)
437 {
438 if (!in_array($this-node, (array)self::$pluginsinstalled)) {
439 return;
440 }
441 self::getClass('SiteHostAssociationManager')->destroy(
442 array(
443 'hostID' => $arguments['Host']->get('id')
444 )
445 );
446 }
447 /**
448 * Adds the site to host email stuff.
449 *
450 * @param mixed $arguments The arguments to change.
451 *
452 * @return void
453 */
454 public function hostEmailHook($arguments)
455 {
456 if (!in_array($this->node, (array)self::$pluginsinstalled)) {
457 return;
458 }
459 $Sites = self::getSubObjectIDs(
460 'SiteHostAssociation',
461 array(
462 'hostID' => $arguments['Host']->get('id')
463 ),
464 'siteID'
465 );
466 $cnt = self::getClass('SiteManager')
467 ->count(array('id' => $Sites));
468 if ($cnt !== 1) {
469 $siteName = '';
470 } else {
471 foreach ((array)self::getClass('SiteManager')
472 ->find(array('id' => $Sites)) as $Site
473 ) {
474 $siteName = $Site->get('name');
475 unset($Site);
476 break;
477 }
478 }
479 self::arrayInsertAfter(
480 "\nSite Used: ",
481 $arguments['email'],
482 "\nImaged from (Site): ",
483 $siteName
484 );
485 self::array_insertAfter(
486 "\nSite Imaged From (Site): ",
487 $arguments['email'],
488 "\nSiteName=",
489 $siteName
490 );
491 }
492 /**
493 * Exposes site during host info request.
494 *
495 * @param mixed $arguments The arguments to change.
496 *
497 * @return void
498 */
499 public function hostInfoExpose($arguments)
500 {
501 if (!in_array($this->node, (array)self::$pluginsinstalled)) {
502 return;
503 }
504 $Sites = self::getSubObjectIDs(
505 'SiteHostAssociation',
506 array(
507 'hostID' => $arguments['Host']->get('id')
508 ),
509 'siteID'
510 );
511 $cnt = self::getClass('SiteManager')
512 ->count(array('id' => $Sites));
513 if ($cnt !== 1) {
514 $arguments['repFields']['site'] = '';
515 return;
516 }
517 foreach ((array)self::getClass('SiteManager')
518 ->find(array('id' => $Sites)) as &$Site
519 ) {
520 $arguments['repFields']['site'] = $Site
521 ->get('name');
522 unset($Site);
523 }
524 }
525 }