OssnThemes.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Open Source Social Network
5  *
6  * @package (Informatikon.com).ossn
7  * @author OSSN Core Team <[email protected]>
8  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
9  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
10  * @link http://www.opensource-socialnetwork.org/licence
11  */
12 class OssnThemes extends OssnSite {
13  /**
14  * Get theme details
15  *
16  * @param string $name Theme id;
17  *
18  * @return object|false;
19  */
20  public static function getTheme($name) {
21  $name = trim($name);
22  if(!preg_match('/\s/', $name)) {
23  $dir = ossn_route()->themes;
24  $theme = $dir . $name;
25  if(is_file("{$theme}/ossn_theme.xml")) {
26  $theme_path = simplexml_load_file("{$theme}/ossn_theme.xml");
27  return $theme_path;
28  }
29  }
30  return false;
31  }
32 
33  /**
34  * Get active theme
35  *
36  * @return string;
37  */
38  public function getActive() {
39  return $this->getSettings('theme');
40  }
41 
42  /**
43  * Get total themes
44  *
45  * @return integer
46  */
47  public function total() {
48  return count($this->getThemes());
49  }
50 
51  /**
52  * Get components list
53  *
54  * @return components ids;
55  */
56  public function getThemes() {
57  $dir = ossn_route()->themes;
58  $theme_ids = array();
59  $handle = opendir($dir);
60 
61  if($handle) {
62  while($theme_id = readdir($handle)) {
63  if(substr($theme_id, 0, 1) !== '.' && is_dir($dir . $theme_id) && !preg_match('/\s/', $theme_id) && is_file("{$dir}{$theme_id}/ossn_theme.php") && is_file("{$dir}{$theme_id}/ossn_theme.xml")) {
64  $theme_ids[] = $theme_id;
65  }
66  }
67  }
68 
69  sort($theme_ids);
70  return $theme_ids;
71  }
72 
73  /**
74  * Upload component
75  *
76  * Requires component package file,
77  *
78  * @return boolean
79  */
80  public function upload() {
81  $archive = new ZipArchive;
82  $data_dir = ossn_get_userdata('tmp/themes');
83  if(!is_dir($data_dir)) {
84  mkdir($data_dir, 0755, true);
85  }
86  $file = new OssnFile;
87  $file->setFile('theme_file');
88  $file->setExtension(array(
89  'zip'
90  ));
91  $zip = $file->file;
92  $newfile = "{$data_dir}/{$zip['name']}";
93  if(move_uploaded_file($zip['tmp_name'], $newfile)) {
94  if($archive->open($newfile) === TRUE) {
95  $archive->extractTo($data_dir);
96  $archive->close();
97  $validate = pathinfo($zip['name'], PATHINFO_FILENAME);
98  if(is_file("{$data_dir}/{$validate}/ossn_theme.php") && is_file("{$data_dir}/{$validate}/ossn_theme.xml")) {
99  $archive->open($newfile);
100  $archive->extractTo(ossn_route()->themes);
101  $archive->close();
102  OssnFile::DeleteDir($data_dir);
103  return true;
104  }
105  }
106  }
107  return false;
108  }
109 
110  /**
111  * Get active theme startup file
112  *
113  * @return string
114  */
115  public function getActivePath() {
116  $path = ossn_route()->themes;
117  return "{$path}{$this->getSettings('theme')}/ossn_theme.php";
118  }
119  /**
120  * Get active theme startup file
121  *
122  * @return string
123  */
124  public function loadActive() {
125  $path = ossn_route()->themes;
126  if(is_file("{$path}{$this->getSettings('theme')}/ossn_theme.php")) {
127  $lang = ossn_site_settings('language');
128  $lang_file = "{$path}{$this->getSettings('theme')}/locale/ossn.{$lang}.php";
129  if(is_file($lang_file)) {
130  //feature request: multilanguage themes #281
131  include_once($lang_file);
132  }
133  require_once("{$path}{$this->getSettings('theme')}/ossn_theme.php");
134  }
135  }
136  /**
137  * Enable Theme
138  *
139  * @params string $name Theme id;
140  *
141  * @return boolean
142  */
143  public function Enable($theme) {
144  if(!empty($theme)) {
145  if($this->UpdateSettings(array(
146  'value'
147  ), array(
148  $theme
149  ), array(
150  "setting_id='1'"
151  ))) {
152  return true;
153  }
154  }
155  return false;
156  }
157 
158  /**
159  * Delete theme
160  *
161  * @return boolean
162  */
163  public function deletetheme($theme) {
164  if(OssnFile::DeleteDir(ossn_route()->themes . "{$theme}/")) {
165  return true;
166  }
167  return false;
168  }
169  /**
170  * Check if theme is older than 3.x
171  *
172  * @param string $element Component xml string.
173  *
174  * @return boolean
175  */
176  public function isOld($element) {
177  if(empty($element)) {
178  return false;
179  }
180  $version = current($element->getNamespaces());
181  if(substr($version, -3) == '1.0') {
182  return true;
183  }
184  return false;
185  }
186  /**
187  * Check theme requirments
188  *
189  * @param string $element A valid theme xml file
190  *
191  * @return false|array
192  */
193  public static function checkRequirments($element) {
194  if(empty($element)) {
195  return false;
196  }
197  $types = array(
198  'ossn_version',
199  'php_extension',
200  'php_version',
201  'php_function'
202  );
203  if(isset($element->name)) {
204  if(isset($element->requires)) {
205  $result = array();
206  $requires = $element->requires;
207  foreach($requires as $item) {
208  if(!in_array($item->type, $types)) {
209  continue;
210  }
211  $requirments = array();
212  //version checks
213  if($item->type == 'ossn_version') {
214 
215  $requirments['type'] = ossn_print('ossn:version');
216  $requirments['value'] = (string) $item->version;
217  $requirments['availability'] = 0;
218  $site_version = (int) ossn_site_settings('site_version');
219 
220  if(($site_version <= $item->version) && ($site_version == (int) $item->version)) {
221  $requirments['availability'] = 1;
222  }
223 
224  }
225  //check php extension
226  if($item->type == 'php_extension') {
227 
228  $requirments['type'] = ossn_print('php:extension');
229  $requirments['value'] = (string) $item->name;
230  $requirments['availability'] = 0;
231 
232  if(extension_loaded($item->name)) {
233  $requirments['availability'] = 1;
234  }
235  }
236  //check php version
237  if($item->type == 'php_version') {
238 
239  $requirments['type'] = ossn_print('php:version');
240  $requirments['value'] = (string) $item->version;
241  $requirments['availability'] = 0;
242 
243  $phpversion = substr(PHP_VERSION, 0, 6);
244  if($phpversion >= $item->version) {
245  $requirments['availability'] = 1;
246  }
247  }
248  //check php function
249  if($item->type == 'php_function') {
250 
251  $requirments['type'] = ossn_print('php:function');
252  $requirments['value'] = (string) $item->name;
253  $requirments['availability'] = 0;
254 
255  if(function_exists($item->name)) {
256  $requirments['availability'] = 1;
257  }
258  }
259  $result[] = $requirments;
260  } //loop
261  return $result;
262  }
263  }
264  return false;
265  } //func
266 
267 } //class