ossn.lib.cache.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  */
11 
12 /**
13  * Generate css cache
14  *
15  * @return false|void
16  */
17 function ossn_trigger_css_cache($cache = '') {
18  if(empty($cache)) {
19  return false;
20  }
21  global $Ossn;
22 
23  require_once(ossn_route()->libs . 'minify/CSSmin.php');
24  $minify = new CSSmin;
25 
26  $dir = ossn_route()->cache;
27  if(!is_dir("{$dir}css/{$cache}/view/")) {
28  mkdir("{$dir}css/{$cache}/view/", 0755, true);
29  }
30  if(!isset($Ossn->css)) {
31  return false;
32  }
33  foreach($Ossn->css as $name => $file) {
34  $cache_file = "{$dir}css/{$cache}/view/{$name}.css";
35  $css = $minify->run(ossn_plugin_view($file));
36  $css .= $minify->run(ossn_fetch_extend_views("css/{$name}"));
37  file_put_contents($cache_file, $css);
38  }
39 }
40 
41 /**
42  * Generate js cache
43  *
44  * @return false|void
45  */
46 function ossn_trigger_js_cache($cache = '') {
47  if(empty($cache)) {
48  return false;
49  }
50  global $Ossn;
51  require_once(ossn_route()->libs . 'minify/JSMin.php');
52  $dir = ossn_route()->cache;
53  if(!is_dir("{$dir}js/{$cache}/view/")) {
54  mkdir("{$dir}js/{$cache}/view/", 0755, true);
55  }
56  if(!isset($Ossn->js)) {
57  return false;
58  }
59  foreach($Ossn->js as $name => $file) {
60  $cache_file = "{$dir}js/{$cache}/view/{$name}.js";
61  $js = JSMin::minify(ossn_plugin_view($file));
62  $js .= JSMin::minify(ossn_fetch_extend_views("js/{$name}"));
63  file_put_contents($cache_file, $js);
64  }
65 }
66 /**
67  * Create a cache for plugin paths
68  *
69  * @return void;
70  */
72  global $Ossn;
73  if(isset($Ossn->plugins)) {
74  //we have to also secure paths
75  $dir = ossn_get_userdata("system/");
76  if(!is_dir($dir)) {
77  mkdir($dir, 0755, true);
78  }
79  $encode = json_encode($Ossn->plugins);
80  file_put_contents($dir . 'plugins_paths', $encode);
81  }
82 }
83 /**
84  * Create languages cache
85  *
86  * @retrun false|void
87  */
88 function ossn_trigger_language_cache($cache) {
89  if(empty($cache)) {
90  return false;
91  }
92  global $Ossn;
94  $dir = ossn_route()->cache;
95 
96  $coms = new OssnComponents;
97  $comlist = $coms->getActive();
98  $comdir = ossn_route()->com;
99 
100  header('Content-Type: text/html; charset=utf-8');
101  foreach($available_languages as $lang) {
102  //load all laguages
103  foreach($Ossn->locale[$lang] as $item){
104  if(is_file($item)){
105  include_once($item);
106  }
107  }
108  //load components all languages
109  if($comlist){
110  foreach($comlist as $com){
111  if(is_file("{$comdir}{$com->com_id}/locale/ossn.{$lang}.php")) {
112  include_once("{$comdir}{$com->com_id}/locale/ossn.{$lang}.php");
113  }
114  }
115  }
116  if(isset($Ossn->localestr[$lang])) {
117  $json = json_encode($Ossn->localestr[$lang], JSON_UNESCAPED_UNICODE);
118  $json = "var OssnLocale = $json";
119  $cache_file = "{$dir}js/{$cache}/view/ossn.{$lang}.language.js";
120  file_put_contents($cache_file, $json);
121  }
122  }
123 }
124 
125 /**
126  * Create and Enable site cache
127  *
128  * @return bool
129  */
130 function ossn_create_cache() {
131  $database = new OssnDatabase;
132  $params['table'] = 'ossn_site_settings';
133  $params['names'] = array(
134  'value'
135  );
136  $params['values'] = array(
137  1
138  );
139  $params['wheres'] = array(
140  "setting_id='4'"
141  );
142  if($database->update($params)) {
143  $cache = ossn_update_last_cache();
144  if($cache) {
145  ossn_link_cache_files($cache);
146  }
147  return true;
148  }
149  return false;
150 }
151 /**
152  * Update last cache time
153  *
154  * @return integer;
155  */
156 function ossn_update_last_cache($type = true) {
157  if($type === true) {
158  $time = time();
159  } else {
160  $time = 0;
161  }
162  $database = new OssnDatabase;
163  $params['table'] = 'ossn_site_settings';
164  $params['names'] = array(
165  'value'
166  );
167  $params['values'] = array(
168  $time
169  );
170  $params['wheres'] = array(
171  "name='last_cache'"
172  );
173  if($database->update($params)) {
174  return $time;
175  }
176  return false;
177 }
178 /**
179  * Disable cache
180  *
181  * @return bool
182  */
183 function ossn_disable_cache() {
184  $database = new OssnDatabase;
185  $params['table'] = 'ossn_site_settings';
186  $params['names'] = array(
187  'value'
188  );
189  $params['values'] = array(
190  0
191  );
192  $params['wheres'] = array(
193  "setting_id='4'"
194  );
195  if($database->update($params)) {
196  ossn_update_last_cache(false);
198  return true;
199  }
200  return false;
201 }
202 /**
203  * Link cache files
204  *
205  * @return void;
206  */
207 function ossn_link_cache_files($cache) {
208  if(empty($cache)) {
209  return false;
210  }
211  ossn_trigger_css_cache($cache);
212  ossn_trigger_js_cache($cache);
215 }
216 /**
217  * Unlink cache files
218  *
219  * @return void;
220  */
224 }