OssnComponents.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @package (Informatikon.com).ossn
6  * @author OSSN Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
12  /**
13  * Get components from compnents directory
14  *
15  * @return array
16  */
17  public function getComponentsDir() {
18  $dir = ossn_route()->com;
19  $com_ids = array();
20  $handle = opendir($dir);
21 
22  if($handle) {
23  while($com_id = readdir($handle)) {
24  if(substr($com_id, 0, 1) !== '.' && is_dir($dir . $com_id) && !preg_match('/\s/', $com_id) && is_file("{$dir}{$com_id}/ossn_com.php") && is_file("{$dir}{$com_id}/ossn_com.xml")) {
25  $com_ids[] = $com_id;
26  }
27  }
28  }
29 
30  sort($com_ids);
31  return $com_ids;
32  }
33 
34  /**
35  * Count total components
36  *
37  * @return integer
38  */
39  public function total() {
40  return count($this->getComponents());
41  }
42 
43  /**
44  * Get components list
45  *
46  * @return array
47  */
48  public function getComponents() {
49  $params['from'] = 'ossn_components';
50  $this->coms = $this->select($params, true);
51  if(!$this->coms) {
52  return false;
53  }
54  foreach($this->coms as $com_id) {
55  $com_ids[] = $com_id->com_id;
56  }
57  return $com_ids;
58  }
59 
60  /**
61  * Upload component
62  *
63  * Requires component package file,
64  *
65  * @return boolean
66  */
67  public function upload() {
68  $archive = new ZipArchive;
69  $data_dir = ossn_get_userdata('tmp/components');
70  if(!is_dir($data_dir)) {
71  mkdir($data_dir, 0755, true);
72  }
73  $zip = $_FILES['com_file'];
74  $newfile = "{$data_dir}/{$zip['name']}";
75  if(move_uploaded_file($zip['tmp_name'], $newfile)) {
76  if($archive->open($newfile) === TRUE) {
77  $translit = OssnTranslit::urlize($zip['name']);
78 
79  //make community components works on installer #394
80  //Component installer problems with certain zip - archives #420
81 
82  $archive->extractTo($data_dir . '/' . $translit);
83  $dirctory = scandir($data_dir . '/' . $translit, 1);
84  $dirctory = $dirctory[0];
85 
86  $files = $data_dir . '/' . $translit . '/' . $dirctory . '/';
87  $archive->close();
88 
89  if(is_dir($files) && is_file("{$files}ossn_com.php") && is_file("{$files}ossn_com.xml")) {
90 
91  $ossn_com_xml = simplexml_load_file("{$files}ossn_com.xml");
92  //need to check id , since ossn v3.x
93  if(isset($ossn_com_xml->id) && !empty($ossn_com_xml->id)) {
94  //move to components directory
95  if(OssnFile::moveFiles($files, ossn_route()->com . $ossn_com_xml->id . '/')) {
96  //add new component to system
97  $this->newCom($ossn_com_xml->id);
98 
99  //why it shows success even if the component is not updated #510
100  OssnFile::DeleteDir($data_dir);
101  return true;
102  }
103  }
104  }
105  }
106  }
107  return false;
108  }
109 
110  /**
111  * Insert a new component to system
112  *
113  * @return boolean
114  */
115  public function newCom($com) {
116  if(!empty($com) && is_dir(ossn_route()->com . $com)) {
117  /*
118  * Get a com;
119  * @last edit: $arsalanshah
120  * @Reason: Initial;
121  */
122  $this->statement("SELECT * FROM ossn_components
123  WHERE (com_id='$com');");
124  $this->execute();
125  $CHECK = $this->fetch();
126  if(!isset($CHECK->active)) {
127  /*
128  * Update com status;
129  * @last edit: $arsalanshah
130  * @Reason: Initial;
131  */
132  $this->statement("INSERT INTO `ossn_components`
133  (`com_id`, `active`)
134  VALUES ('$com', '0')");
135  $this->execute();
136  return true;
137  }
138  }
139  return false;
140  }
141 
142  /**
143  * Load all active components
144  *
145  * @return false|null startup files;
146  */
147  public function loadComs() {
148  $coms = $this->getActive();
149  $lang = ossn_site_settings('language');
150 
151  $vars['activated'] = $coms;
152  ossn_trigger_callback('components', 'before:load', $vars);
153  if(!$coms) {
154  return false;
155  }
156  foreach($coms as $com) {
157  $dir = ossn_route()->com;
158  $name = $this->getCom($com->com_id);
159  if(!empty($name->name)) {
160  ossn_register_plugins_by_path("{$dir}{$com->com_id}/plugins/");
161  if(is_file("{$dir}{$com->com_id}/locale/ossn.{$lang}.php")) {
162  include("{$dir}{$com->com_id}/locale/ossn.{$lang}.php");
163  }
164  include_once("{$dir}{$com->com_id}/ossn_com.php");
165  }
166  }
167  ossn_trigger_callback('components', 'after:load', $vars);
168  }
169 
170  /**
171  * Get active components
172  *
173  * @return active components;
174  */
175  public function getActive() {
176  $params['from'] = 'ossn_components';
177  $params['wheres'] = array(
178  "active='1'"
179  );
180  return $this->select($params, true);
181  }
182 
183  /**
184  * Get component details
185  *
186  * @params string $name Component id;
187  *
188  * @return false|object
189  */
190  public static function getCom($name) {
191  $name = trim($name);
192  if(!preg_match('/\s/', $name)) {
193  $dir = ossn_route()->com;
194  $component = $dir . $name;
195  if(is_file("{$component}/ossn_com.xml")) {
196  $component = simplexml_load_file("{$component}/ossn_com.xml");
197  return $component;
198  }
199  }
200  return false;
201  }
202  /**
203  * Check if component is older than 3.x
204  *
205  * @param string $element Component xml string.
206  *
207  * @return boolean
208  */
209  public function isOld($element) {
210  if(empty($element)) {
211  return false;
212  }
213  $version = current($element->getNamespaces());
214  if(substr($version, -3) == '1.0') {
215  return true;
216  }
217  return false;
218  }
219  /**
220  * Check component requirments
221  *
222  * @param string $element A valid component xml file
223  *
224  * @return false|array
225  */
226  public static function checkRequirments($element) {
227  if(empty($element)) {
228  return false;
229  }
230  $types = array(
231  'ossn_version',
232  'php_extension',
233  'php_version',
234  'php_function'
235  );
236  if(isset($element->name)) {
237  if(isset($element->requires)) {
238  $result = array();
239  $requires = $element->requires;
240  foreach($requires as $item) {
241  if(!in_array($item->type, $types)) {
242  continue;
243  }
244  $requirments = array();
245  //version checks
246  if($item->type == 'ossn_version') {
247 
248  $requirments['type'] = ossn_print('ossn:version');
249  $requirments['value'] = (string) $item->version;
250  $requirments['availability'] = 0;
251  $site_version = (int) ossn_site_settings('site_version');
252 
253  if(($site_version <= $item->version) && ($site_version == (int) $item->version)) {
254  $requirments['availability'] = 1;
255  }
256 
257  }
258  //check php extension
259  if($item->type == 'php_extension') {
260 
261  $requirments['type'] = ossn_print('php:extension');
262  $requirments['value'] = (string) $item->name;
263  $requirments['availability'] = 0;
264 
265  if(extension_loaded($item->name)) {
266  $requirments['availability'] = 1;
267  }
268  }
269  //check php version
270  if($item->type == 'php_version') {
271 
272  $requirments['type'] = ossn_print('php:version');
273  $requirments['value'] = (string) $item->version;
274  $requirments['availability'] = 0;
275 
276  $phpversion = substr(PHP_VERSION, 0, 6);
277  if($phpversion >= $item->version) {
278  $requirments['availability'] = 1;
279  }
280  }
281  //check php function
282  if($item->type == 'php_function') {
283 
284  $requirments['type'] = ossn_print('php:function');
285  $requirments['value'] = (string) $item->name;
286  $requirments['availability'] = 0;
287 
288  if(function_exists($item->name)) {
289  $requirments['availability'] = 1;
290  }
291  }
292  $result[] = $requirments;
293  } //loop
294  return $result;
295  }
296  }
297  return false;
298  } //func
299  /**
300  * Check if component is active or not
301  *
302  * @return boolean
303  */
304  public function isActive($id = '') {
305  if(empty($id)) {
306  return false;
307  }
308  $params['from'] = 'ossn_components';
309  $params['wheres'] = array(
310  "com_id='{$id}'"
311  );
312  $this->settings = $this->select($params);
313  if($this->settings->active == 1) {
314  return true;
315  }
316  return false;
317  }
318 
319  /**
320  * Enable Component
321  *
322  * @return boolean
323  */
324  public function enable($com) {
325  if(!empty($com) && is_dir(ossn_route()->com . $com)) {
326  /*
327  * Get a com;
328  * @last edit: $arsalanshah
329  * @Reason: Initial;
330  */
331  $this->statement("SELECT * FROM ossn_components
332  WHERE (com_id='$com');");
333  $this->execute();
334  $CHECK = $this->fetch();
335  /*
336  * Update com status;
337  * @last edit: $arsalanshah
338  * @Reason: Initial;
339  */
340  if(isset($CHECK->active) && $CHECK->active == 0) {
341  $this->statement("UPDATE ossn_components
342  SET active='1'
343  WHERE (com_id='$com');");
344  $this->execute();
345  return true;
346  } elseif(!isset($CHECK->active)) {
347  /*
348  * Update com status;
349  * @last edit: $arsalanshah
350  * @Reason: Initial;
351  */
352  $this->statement("INSERT INTO `ossn_components`
353  (`com_id`, `active`)
354  VALUES ('$com', '1')");
355  $this->execute();
356  return true;
357  }
358  }
359  return false;
360  }
361 
362  /**
363  * Delete component
364  *
365  * @return boolean
366  */
367  public function delete($com) {
368  if(in_array($com, $this->requiredComponents())) {
369  return false;
370  }
371  $component = $this->getbyName($com);
372  if(!$component){
373  return false;
374  }
375  $params = array();
376  $params['from'] = "ossn_components";
377  $params['wheres'] = array(
378  "com_id='{$com}'"
379  );
380  if(parent::delete($params)) {
381  //Delete component settings upon its deletion #538
382  $entities = new OssnEntities;
383  $entities->deleteByOwnerGuid($component->id, 'component');
384 
385  //delete component directory
386  OssnFile::DeleteDir(ossn_route()->com . "{$com}/");
387  return true;
388  }
389  return false;
390  }
391 
392  /**
393  * Required Components
394  *
395  * Admin can't disable required components;
396  *
397  * @return array
398  */
399  public function requiredComponents() {
400  return array(
401  'OssnAds',
402  'OssnComments',
403  'OssnLikes',
404  'OssnMessages',
405  'OssnNotifications',
406  'OssnPhotos',
407  'OssnProfile',
408  'OssnSearch',
409  'OssnWall'
410  );
411  }
412 
413  /**
414  * Disable component
415  *
416  * @return boolean
417  */
418  public function DISABLE($com = NULL) {
419  if(in_array($com, $this->requiredComponents())) {
420  return false;
421  }
422  if(!empty($com)) {
423  $this->statement("UPDATE ossn_components
424  SET active='0' WHERE (com_id='$com')");
425  $this->execute();
426  return true;
427  }
428  return false;
429  }
430 
431  /**
432  * Bundled components
433  *
434  * @return array
435  */
436  public function bundledComponents() {
437  return array_merge(array(
438  'OssnGroups',
439  'OssnSitePages',
440  'OssnChat',
441  'OssnPoke',
442  'OssnBlock',
443  'OssnSmilies',
444  'OssnInvite',
445  'OssnEmbed'
446  ), $this->requiredComponents());
447  }
448  /**
449  * Set component settings
450  *
451  * setSettings should have array() to accept values #434
452  *
453  * @param string $component Component id
454  * @param array vars Setting (two-dem array)
455  *
456  * @return boolean
457  */
458  public function setSettings($component, array $vars = array()) {
459  $settings = self::getComSettings($component);
460  $guid = $this->getbyName($component)->getID();
461  $entity = new OssnEntities;
462  if(empty($guid)) {
463  return false;
464  }
465  foreach($vars as $name => $value) {
466  if($settings && !$settings->isParam($name)) {
467  $entity->owner_guid = $guid;
468  $entity->type = 'component';
469  $entity->subtype = $name;
470  $entity->value = $value;
471  $entity->add();
472  } else {
473  $entity->owner_guid = $guid;
474  $entity->type = 'component';
475  $entity->data->$name = $value;
476  $entity->save();
477  }
478  }
479  return true;
480  }
481  /**
482  * Set component settings
483  *
484  * @param string $component Component id
485  * @param string $setting Setting name
486  * @param string $value Setting value
487  *
488  * @note This method is deprecated and will be removed in Ossn v4.0
489  *
490  * @return boolean
491  */
492  public function setComSETTINGS($component, $setting, $value) {
493  return $this->setSettings($component, array(
494  $setting => $value
495  ));
496  }
497 
498  /**
499  * Get Component Settings
500  *
501  * @params string $component Component id
502  *
503  * @return false|array;
504  */
505  public function getSettings($component) {
506  $entity = new OssnEntities;
507  $entity->type = 'component';
508  $entity->owner_guid = $this->getbyName($component)->getGUID();
509  $settings = $entity->get_entities();
510  if(is_array($settings) && !empty($settings)) {
511  foreach($settings as $setting) {
512  $comsettings[$setting->subtype] = $setting->value;
513  }
514  return arrayObject($comsettings, 'OssnComponents');
515  }
516  return false;
517  }
518  /**
519  * Get Component Settings
520  *
521  * @params string $component Component id
522  *
523  * @note This method is deprecated and will be removed in Ossn v4.0
524  *
525  * @return false|array;
526  */
527  public function getComSettings($component) {
528  return $this->getSettings($component);
529  }
530  /**
531  * Get component
532  *
533  * @note This id is not a package id
534  *
535  * @return integer|false;
536  */
537  public function getbyName($name) {
538  $params = array();
539  $params['from'] = 'ossn_components';
540  $params['where'] = array(
541  "com_id='{$name}'"
542  );
543  if($data = $this->select($params)) {
544  return arrayObject($data, get_class($this));
545  }
546  return false;
547  }
548 
549 
550 } //class