PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ww.incs/basics.php

http://webworks-webme.googlecode.com/
PHP | 322 lines | 311 code | 0 blank | 11 comment | 51 complexity | eb98bf66276539615110d4d987a74dac MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. apache_setenv('PATH', '/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin');
  3. session_start();
  4. require 'Log.php';
  5. if(!defined('START_TIME'))define('START_TIME',microtime(true));
  6. function __() {
  7. $str = gettext(func_get_arg(0));
  8. for($i = func_num_args()-1 ; $i ; --$i){
  9. $s=func_get_arg($i);
  10. $str=str_replace('%'.$i,$s,$str);
  11. }
  12. return $str;
  13. }
  14. spl_autoload_register('WebME_autoload');
  15. function WebME_autoload($name) {
  16. require $name . '.php';
  17. }
  18. function cache_clear($type='') {
  19. if (!is_dir(USERBASE.'/ww.cache/'.$type)) {
  20. return;
  21. }
  22. $d=new DirectoryIterator(USERBASE.'/ww.cache/'.$type);
  23. foreach($d as $f){
  24. $f=$f->getFilename();
  25. if ($f=='.' || $f=='..') {
  26. continue;
  27. }
  28. if (is_dir(USERBASE.'/ww.cache/'.$type.'/'.$f)) {
  29. cache_clear($type.'/'.$f);
  30. rmdir(USERBASE.'/ww.cache/'.$type.'/'.$f);
  31. }
  32. else {
  33. unlink(USERBASE.'/ww.cache/'.$type.'/'.$f);
  34. }
  35. }
  36. }
  37. function cache_load($type,$md5){
  38. if(file_exists(USERBASE.'/ww.cache/'.$type.'/'.$md5)){
  39. return json_decode(file_get_contents(USERBASE.'/ww.cache/'.$type.'/'.$md5), true);
  40. }
  41. return false;
  42. }
  43. function cache_save($type,$md5,$vals){
  44. if (!is_dir(USERBASE.'/ww.cache/'.$type)) {
  45. mkdir(USERBASE.'/ww.cache/'.$type, 0777, true);
  46. }
  47. file_put_contents(USERBASE.'/ww.cache/'.$type.'/'.$md5, json_encode($vals));
  48. }
  49. function config_rewrite(){
  50. global $DBVARS;
  51. $tmparr=$DBVARS;
  52. $tmparr['plugins']=join(',',$DBVARS['plugins']);
  53. $tmparr2=array();
  54. foreach($tmparr as $name=>$val)$tmparr2[]='\''.addslashes($name).'\'=>\''.addslashes($val).'\'';
  55. $config="<?php\n\$DBVARS=array(\n ".join(",\n ",$tmparr2)."\n);";
  56. file_put_contents(CONFIG_FILE,$config);
  57. }
  58. function Core_addUserToGroup($uid, $gid) {
  59. $meta=json_decode(dbOne('select meta from groups where id='.$gid, 'meta'), true);
  60. $expires=(@$meta['paid-membership']=='yes')
  61. ?'expires=date_add(now(), interval '.$meta['paid-membership-subscription-period-num'].' '.$meta['paid-membership-subscription-period'].'),'
  62. :'';
  63. dbQuery('insert into users_groups set '.$expires.'user_accounts_id='.$uid.',groups_id='.$gid);
  64. if (@$meta['mailinglist'] == 'Mailchimp' && @$meta['mailinglist_apikey']) {
  65. $user=dbRow('select * from user_accounts where id='.$uid);
  66. $email=$user['email'];
  67. $first_name=preg_replace('/ .*/', '', $user['name']);
  68. $last_name=preg_replace('/^[^ ]* /', '', $user['name']);
  69. $apikey=$meta['mailinglist_apikey'];
  70. $listId=$meta['mailinglist_listid'];
  71. $merges = array(
  72. 'FNAME'=>$first_name,
  73. 'LNAME'=>$last_name
  74. );
  75. $double_optin=false;
  76. $update_existing=false;
  77. $replace_interests=true;
  78. $send_welcome=false;
  79. $email_type = 'html';
  80. $data = array(
  81. 'email_address'=>$email,
  82. 'apikey'=>$apikey,
  83. 'merge_vars' => $merges,
  84. 'id' => $listId,
  85. 'double_optin' => $double_optin,
  86. 'update_existing' => $update_existing,
  87. 'replace_interests' => $replace_interests,
  88. 'send_welcome' => $send_welcome,
  89. 'email_type' => $email_type
  90. );
  91. $payload = json_encode($data);
  92. $submit_url='http://'.preg_replace('/.*-/', '', $apikey).'.api.mailchimp.com/1.3/?method=listSubscribe';
  93. $ch = curl_init();
  94. curl_setopt($ch, CURLOPT_URL, $submit_url);
  95. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  96. curl_setopt($ch, CURLOPT_POST, true);
  97. curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));
  98. $result = curl_exec($ch);
  99. curl_close ($ch);
  100. $data=json_decode($result);
  101. }
  102. }
  103. function dbAll($query, $key='') {
  104. $q = dbQuery($query);
  105. if ($q === false) {
  106. return false;
  107. }
  108. $results=array();
  109. while ($r=$q->fetch(PDO::FETCH_ASSOC)) {
  110. $results[]=$r;
  111. }
  112. if (!$key) {
  113. return $results;
  114. }
  115. $arr=array();
  116. foreach ($results as $r) {
  117. $arr[$r[$key]]=$r;
  118. }
  119. return $arr;
  120. }
  121. function dbInit(){
  122. if(isset($GLOBALS['db']))return $GLOBALS['db'];
  123. global $DBVARS;
  124. $db=new PDO('mysql:host='.$DBVARS['hostname'].';dbname='.$DBVARS['db_name'],$DBVARS['username'],$DBVARS['password']);
  125. $db->query('SET NAMES utf8');
  126. $db->num_queries=0;
  127. $GLOBALS['db']=$db;
  128. return $db;
  129. }
  130. function dbLastInsertId(){
  131. return dbOne('select last_insert_id() as id','id');
  132. }
  133. function dbOne($query, $field='') {
  134. $r = dbRow($query);
  135. if ($r === false) {
  136. return false;
  137. }
  138. return $r[$field];
  139. }
  140. function dbQuery($query){
  141. $db=dbInit();
  142. $q=$db->query($query);
  143. if ($q === false) { // failed
  144. return false;
  145. }
  146. $db->num_queries++;
  147. return $q;
  148. }
  149. function dbRow($query) {
  150. $q = dbQuery($query);
  151. if ($q === false) {
  152. return false;
  153. }
  154. return $q->fetch(PDO::FETCH_ASSOC);
  155. }
  156. function ob_show_and_log($type,$header=''){
  157. $log = Log::singleton('file',USERBASE.'/log.txt',$type,array('locking'=>true,'timeFormat'=>'%Y-%m-%d %H:%M:%S'));
  158. $length=ob_get_length();
  159. $num_queries=isset($GLOBALS['db'])?$GLOBALS['db']->num_queries:0;
  160. switch($type){
  161. case 'design_file': // {
  162. $location=$_SERVER['REQUEST_URI'];
  163. break;
  164. // }
  165. case 'file': // {
  166. $location=$_SERVER['REQUEST_URI'];
  167. break;
  168. // }
  169. case 'menu': // {
  170. $location='menu';
  171. break;
  172. // }
  173. case 'page': // {
  174. $location=$GLOBALS['PAGEDATA']->id.'|'.$GLOBALS['PAGEDATA']->getRelativeUrl();
  175. break;
  176. // }
  177. default: // {
  178. $location='unknown_type_'.$type;
  179. //}
  180. }
  181. $log->log(
  182. $_SERVER['REMOTE_ADDR']
  183. .' '.$location
  184. .' '.(isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'')
  185. .' '.(isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'')
  186. .' '.memory_get_peak_usage()
  187. .' '.$length
  188. .' '.(microtime(true)-START_TIME)
  189. .' '.$num_queries
  190. );
  191. if($header)header($header);
  192. ob_flush();
  193. }
  194. function admin_can_create_top_pages(){
  195. return has_page_permissions(1024);
  196. }
  197. function is_admin(){
  198. return (isset($_SESSION['userdata']) && isset($_SESSION['userdata']['groups']['administrators']));
  199. }
  200. function is_logged_in(){
  201. return isset($_SESSION['userdata']);
  202. }
  203. function get_userid(){
  204. return $_SESSION['userdata']['id'];
  205. }
  206. function has_page_permissions($val){
  207. return true;
  208. }
  209. function has_access_permissions($val){
  210. return true;
  211. }
  212. function plugin_trigger($trigger_name){
  213. global $PLUGIN_TRIGGERS,$PAGEDATA;
  214. if(!isset($PLUGIN_TRIGGERS[$trigger_name]))return;
  215. $c='';
  216. foreach($PLUGIN_TRIGGERS[$trigger_name] as $fn) {
  217. $c.=$fn($PAGEDATA);
  218. }
  219. return $c;
  220. }
  221. function plugin_trigger_arr($trigger_name, $params=null) {
  222. global $PLUGIN_TRIGGERS;
  223. if (!isset($PLUGIN_TRIGGERS[$trigger_name])) {
  224. return;
  225. }
  226. foreach($PLUGIN_TRIGGERS[$trigger_name] as $fn) {
  227. $c=$fn($params);
  228. if ($c && is_array($c)) {
  229. return $c;
  230. }
  231. }
  232. return false;
  233. }
  234. define('SCRIPTBASE', $_SERVER['DOCUMENT_ROOT'] . '/');
  235. if (!file_exists(SCRIPTBASE . '.private/config.php')) {
  236. echo '<html><body><p>No configuration file found</p>';
  237. if(file_exists('install/index.php'))echo '<p><a href="/install/index.php">Click here to install</a></p>';
  238. else echo '<p><strong>Installation script also missing...</strong> please contact kae@webworks.ie if you think there\'s a problem.</p>';
  239. echo '</body></html>';
  240. exit;
  241. }
  242. require SCRIPTBASE . '.private/config.php';
  243. if(isset($DBVARS['userbase']))define('USERBASE', $DBVARS['userbase']);
  244. else define('USERBASE', SCRIPTBASE);
  245. // { built-in page types
  246. $pagetypes=array(
  247. array(0,'normal',0),
  248. array(4,'page summaries',0),
  249. array(5,'search results',0),
  250. array(9,'table of contents',0)
  251. );
  252. // }
  253. $admin_top_menu=array(
  254. array('id'=>'am_pages','name'=>'pages','link'=>'pages.php'),
  255. array('id'=>'am_siteoptions','name'=>_('site options'),'link'=>'siteoptions.php'),
  256. array('id'=>'am_stats','name'=>_('stats'),'link'=>'stats.php')
  257. );
  258. $DBVARS['plugins']=(isset($DBVARS['plugins']) && $DBVARS['plugins']!='')?explode(',',$DBVARS['plugins']):array();
  259. if(!defined('CONFIG_FILE'))define('CONFIG_FILE',SCRIPTBASE.'.private/config.php');
  260. define('WORKDIR_IMAGERESIZES', USERBASE.'/f/.files/image_resizes/');
  261. define('WORKURL_IMAGERESIZES', '/f/.files/image_resizes/');
  262. define('CKEDITOR','ckeditor');
  263. if(!defined('KFM_BASE_PATH'))define('KFM_BASE_PATH', SCRIPTBASE.'j/kfm/');
  264. set_include_path(SCRIPTBASE.'ww.php_classes'.PATH_SEPARATOR.KFM_BASE_PATH.'classes'.PATH_SEPARATOR.get_include_path());
  265. // { theme variables
  266. if (isset($DBVARS['theme_dir_personal']) && $DBVARS['theme_dir_personal']) {
  267. define('THEME_DIR',$DBVARS['theme_dir_personal']);
  268. }
  269. else if (isset($DBVARS['theme_dir']) && $DBVARS['theme_dir']) {
  270. define('THEME_DIR',$DBVARS['theme_dir']);
  271. }
  272. else {
  273. define('THEME_DIR',SCRIPTBASE.'ww.skins');
  274. }
  275. if (@$DBVARS['theme']) {
  276. define('THEME',$DBVARS['theme']);
  277. }
  278. else{
  279. if (!file_exists(THEME_DIR)) {
  280. die(
  281. 'error: theme directory '.THEME_DIR.' does not exist. please '
  282. .'create it and make sure it is writable by the web server.'
  283. );
  284. }
  285. $dir=new DirectoryIterator(THEME_DIR);
  286. $themes_found=0;
  287. $DBVARS['theme']='.default';
  288. foreach($dir as $file){
  289. if($file->isDot())continue;
  290. if(!$file->isDir())continue;
  291. $DBVARS['theme']=$file->getFileName();
  292. break;
  293. }
  294. define('THEME',$DBVARS['theme']);
  295. }
  296. // }
  297. // { plugin
  298. $PLUGINS=array();
  299. $PLUGIN_TRIGGERS=array();
  300. if(!isset($ignore_webme_plugins)){
  301. foreach($DBVARS['plugins'] as $pname){
  302. if(strpos('/',$pname)!==false)continue;
  303. require SCRIPTBASE . 'ww.plugins/'.$pname.'/plugin.php';
  304. if (isset($plugin['version']) && $plugin['version'] && (!isset($DBVARS[$pname.'|version']) || $DBVARS[$pname.'|version']!=$plugin['version'])){
  305. $version=isset($DBVARS[$pname.'|version'])
  306. ?(int)$DBVARS[$pname.'|version']
  307. :0;
  308. require SCRIPTBASE . 'ww.plugins/'.$pname.'/upgrade.php';
  309. header('Location: '.$_SERVER['REQUEST_URI']);
  310. exit;
  311. }
  312. $PLUGINS[$pname]=$plugin;
  313. if(isset($plugin['triggers'])){
  314. foreach($plugin['triggers'] as $name=>$fn){
  315. if(!isset($PLUGIN_TRIGGERS[$name]))$PLUGIN_TRIGGERS[$name]=array();
  316. $PLUGIN_TRIGGERS[$name][]=$fn;
  317. }
  318. }
  319. }
  320. }
  321. // }
  322. plugin_trigger('initialisation-completed');