PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/halogy/libraries/Core.php

https://gitlab.com/intelij/Halogy
PHP | 1369 lines | 1023 code | 189 blank | 157 comment | 149 complexity | 0784aca283f6e1d58c16bdd531636619 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Halogy
  4. *
  5. * A user friendly, modular content management system for PHP 5.0
  6. * Built on CodeIgniter - http://codeigniter.com
  7. *
  8. * @package Halogy
  9. * @author Haloweb Ltd
  10. * @copyright Copyright (c) 2012, Haloweb Ltd
  11. * @license http://halogy.com/license
  12. * @link http://halogy.com/
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. class Core {
  18. var $CI; // CI instance
  19. var $table ; // default table
  20. var $siteID; // id of the site
  21. var $uri_assoc_segment = 4; // segment where the magic happens
  22. var $adminOverRide = FALSE; // allows for override of siteID
  23. var $currentPage;
  24. var $where = array();
  25. var $set = array();
  26. var $required = array();
  27. function Core()
  28. {
  29. // init vars
  30. $this->CI =& get_instance();
  31. // get siteID, if available
  32. if (defined('SITEID'))
  33. {
  34. $this->siteID = SITEID;
  35. }
  36. // set groupID from session (if set)
  37. $this->groupID = ($this->CI->session->userdata('groupID')) ? $this->CI->session->userdata('groupID') : 0;
  38. }
  39. function get_page($pageID = FALSE, $uri = FALSE)
  40. {
  41. // get page data
  42. $this->CI->db->where('siteID', $this->siteID);
  43. // if getting by uri check its not deleted
  44. if ($uri)
  45. {
  46. $this->CI->db->where('deleted', 0);
  47. }
  48. if (intval($pageID))
  49. {
  50. $this->CI->db->where('pageID', $pageID);
  51. }
  52. elseif ($uri !== FALSE && $pageID === FALSE)
  53. {
  54. $this->CI->db->where('uri', $uri);
  55. }
  56. else
  57. {
  58. return FALSE;
  59. }
  60. // get the latest one, not a deleted one from the past
  61. $this->CI->db->order_by('dateCreated', 'desc');
  62. $query = $this->CI->db->get('pages', 1);
  63. if ($query->num_rows() == 1)
  64. {
  65. return $query->row_array();
  66. }
  67. else
  68. {
  69. return FALSE;
  70. }
  71. }
  72. function get_active_page($uri = '')
  73. {
  74. // get published page
  75. if (!$uri)
  76. {
  77. return FALSE;
  78. }
  79. $this->CI->db->where('siteID', $this->siteID);
  80. $this->CI->db->where('active', 1);
  81. $this->CI->db->where('deleted', 0);
  82. $this->CI->db->where('uri', $uri);
  83. $query = $this->CI->db->get('pages', 1);
  84. if ($query->num_rows() == 1)
  85. {
  86. return $query->row_array();
  87. }
  88. else
  89. {
  90. return FALSE;
  91. }
  92. }
  93. function generate_page($pageID, $admin = FALSE, $templateID = '')
  94. {
  95. // get page data
  96. $pagedata = $this->get_page($pageID);
  97. // load template, either from override or from page data
  98. if ($templateID)
  99. {
  100. $page = $this->CI->template->generate_template(array('templateID' => $templateID));
  101. }
  102. else
  103. {
  104. $page = $this->CI->template->generate_template($pagedata);
  105. }
  106. // set default parse variable to nothing
  107. $page['error'] = '';
  108. $page['message'] = '';
  109. // tell the parser some important info like versionID
  110. $page['pageID'] = $pagedata['pageID'];
  111. $page['templateID'] = $pagedata['templateID'];
  112. $page['versionID'] = $pagedata['versionID'];
  113. // if logged in as admin, then get the blocks from draft, otherwise get them from the published version
  114. $versionID = ($admin === TRUE) ? $pagedata['draftID'] : $pagedata['versionID'];
  115. // populate blocks from db (if they exist)
  116. if ($blocksResult = $this->get_blocks($versionID))
  117. {
  118. foreach($blocksResult as $blockRow)
  119. {
  120. // set bodies and get images for mkdn view
  121. $body[$blockRow['blockRef']] = form_prep($blockRow['body']);
  122. $mkdnBody[$blockRow['blockRef']] = $this->CI->template->parse_body($blockRow['body']);
  123. }
  124. }
  125. // parse for blocks
  126. preg_match_all('/block([a-z0-9\-_]+)/i', $page['body'], $blocks);
  127. if ($blocks)
  128. {
  129. foreach($blocks[1] as $block => $value)
  130. {
  131. $blockRef = 'block'.$value;
  132. if ($admin)
  133. {
  134. $page[$blockRef] = '
  135. <div class="halogycms_container">
  136. <div id="'.$blockRef.'" class="halogycms_edit">
  137. <div class="halogycms_buttons">
  138. <a href="#" class="halogycms_boldbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_bold.png" alt="Bold" title="Bold" class="halogycms_helper" /></a>
  139. <a href="#" class="halogycms_italicbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_italic.png" alt="Italic" title="Italic" class="halogycms_helper" /></a>
  140. <a href="#" class="halogycms_h1button"><img src="'.$this->CI->config->item('staticPath').'/images/btn_h1.png" alt="Heading 1" title="Heading 1" class="halogycms_helper" /></a>
  141. <a href="#" class="halogycms_h2button"><img src="'.$this->CI->config->item('staticPath').'/images/btn_h2.png" alt="Heading 2" title="Heading 2" class="halogycms_helper" /></a>
  142. <a href="#" class="halogycms_h3button"><img src="'.$this->CI->config->item('staticPath').'/images/btn_h3.png" alt="Heading 3" title="Heading 3" class="halogycms_helper" /></a>
  143. <a href="#" class="halogycms_urlbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_url.png" alt="Insert Link" title="Insert Link" class="halogycms_helper" /></a>
  144. <a href="'.site_url('/admin/images/browser').'" class="halogycms_imagebutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_image.png" alt="Insert Image" title="Insert Image" class="halogycms_helper" /></a>
  145. <a href="'.site_url('/admin/files/browser').'" class="halogycms_filebutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_file.png" alt="Insert File" title="Insert File" class="halogycms_helper" /></a>
  146. <a href="#" class="halogycms_cancelbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_cancel.png" alt="Cancel" title="Cancel Changes" class="halogycms_helper" /></a>
  147. <a href="'.site_url('/admin/pages/add_block/'.$versionID.'/'.$blockRef).'" class="halogycms_savebutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_save.png" alt="Save" title="Save Changes" class="halogycms_helper" /></a>
  148. <a href="#" class="halogycms_editbutton">Edit</a>
  149. </div>
  150. <div class="halogycms_blockelement">'.@$mkdnBody[$blockRef].'</div>
  151. <div class="halogycms_editblock"><textarea rows="8" cols="10" class="code">'.@$body[$blockRef].'</textarea></div>
  152. </div>
  153. </div>
  154. ';
  155. }
  156. else
  157. {
  158. $page[$blockRef] = @$mkdnBody[$blockRef];
  159. }
  160. }
  161. }
  162. return $page;
  163. }
  164. function get_versions($pageID)
  165. {
  166. $this->CI->db->where('pageID', $pageID);
  167. $this->CI->db->where('published', 1);
  168. $this->CI->db->order_by('dateCreated', 'desc');
  169. $query = $this->CI->db->get('page_versions', 30);
  170. // get data
  171. if ($query->num_rows())
  172. {
  173. return $query->result_array();
  174. }
  175. else
  176. {
  177. return false;
  178. }
  179. }
  180. function get_drafts($pageID)
  181. {
  182. $this->CI->db->where('pageID', $pageID);
  183. $this->CI->db->where('published', 0);
  184. $this->CI->db->order_by('dateCreated', 'desc');
  185. $query = $this->CI->db->get('page_versions');
  186. // get data
  187. if ($query->num_rows())
  188. {
  189. return $query->result_array();
  190. }
  191. else
  192. {
  193. return false;
  194. }
  195. }
  196. function get_blocks($versionID)
  197. {
  198. $this->CI->db->where('siteID', $this->siteID);
  199. $this->CI->db->select('MAX(blockID) as blockID');
  200. $this->CI->db->where('versionID', $versionID);
  201. $this->CI->db->group_by('blockRef');
  202. $this->CI->db->order_by('dateCreated','DESC');
  203. $query = $this->CI->db->get('page_blocks');
  204. $result = $query->result_array();
  205. $numBlocks = $query->num_rows();
  206. // get data
  207. if ($numBlocks > 0)
  208. {
  209. foreach($result as $row)
  210. {
  211. $blockIDs[] = $row['blockID'];
  212. }
  213. $this->CI->db->where('siteID', $this->siteID);
  214. $this->CI->db->where_in('blockID', $blockIDs);
  215. $this->CI->db->where('versionID', $versionID);
  216. $this->CI->db->order_by('blockID');
  217. $query = $this->CI->db->get('page_blocks', $numBlocks);
  218. return $query->result_array();
  219. }
  220. else
  221. {
  222. return false;
  223. }
  224. }
  225. function get_template($templateID = '')
  226. {
  227. // default where
  228. $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
  229. $this->CI->db->where('t1.deleted', 0, FALSE);
  230. $this->CI->db->where('templateID', $templateID);
  231. // select
  232. $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
  233. // join revisions
  234. $this->CI->db->join('template_versions t2', 't2.versionID = t1 . versionID', 'left');
  235. // get em
  236. $query = $this->CI->db->get('templates t1', 1);
  237. if ($query->num_rows())
  238. {
  239. return $query->row_array();
  240. }
  241. else
  242. {
  243. return FALSE;
  244. }
  245. }
  246. function get_module_template($modulePath = '')
  247. {
  248. // default where
  249. $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
  250. $this->CI->db->where('t1.deleted', 0, FALSE);
  251. $this->CI->db->where('modulePath', $modulePath);
  252. // select
  253. $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
  254. // join revisions
  255. $this->CI->db->join('template_versions t2', 't2.versionID = t1 . versionID', 'left');
  256. // get em
  257. $query = $this->CI->db->get('templates t1', 1);
  258. if ($query->num_rows())
  259. {
  260. return $query->row_array();
  261. }
  262. else
  263. {
  264. return FALSE;
  265. }
  266. }
  267. function get_include($includeRef = '', $includeID = '')
  268. {
  269. // default where
  270. $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
  271. $this->CI->db->where('t1.deleted', 0, FALSE);
  272. // get by reference
  273. if ($includeRef)
  274. {
  275. $this->CI->db->where('includeRef', $includeRef);
  276. }
  277. // get by ID
  278. elseif ($includeID)
  279. {
  280. $this->CI->db->where('includeID', $includeID);
  281. }
  282. // or fail
  283. else
  284. {
  285. return FALSE;
  286. }
  287. // select
  288. $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
  289. // join revisions
  290. $this->CI->db->join('include_versions t2', 't2.versionID = t1 . versionID', 'left');
  291. // get em
  292. $query = $this->CI->db->get('includes t1', 1);
  293. if ($query->num_rows())
  294. {
  295. return $query->row_array();
  296. }
  297. else
  298. {
  299. return FALSE;
  300. }
  301. }
  302. function lookup_user($userID, $display = FALSE)
  303. {
  304. // default wheres
  305. $this->CI->db->where('userID', $userID);
  306. // grab
  307. $query = $this->CI->db->get('users', 1);
  308. if ($query->num_rows())
  309. {
  310. $row = $query->row_array();
  311. if ($display !== FALSE)
  312. {
  313. return ($row['displayName']) ? $row['displayName'] : trim($row['firstName'].' '.$row['lastName']);
  314. }
  315. else
  316. {
  317. return $row;
  318. }
  319. }
  320. else
  321. {
  322. return FALSE;
  323. }
  324. }
  325. function add_draft($pageID)
  326. {
  327. if ($pagedata = $this->get_page($pageID))
  328. {
  329. // add new version
  330. $this->CI->db->set('pageID', $pageID);
  331. $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
  332. $this->CI->db->set('userID', $this->CI->session->userdata('userID'));
  333. $this->CI->db->set('siteID', $this->siteID);
  334. $this->CI->db->insert('page_versions');
  335. // get version ID
  336. $draftID = $this->CI->db->insert_id();
  337. // update page draft
  338. $this->CI->db->set('draftID', $draftID);
  339. $this->CI->db->where('siteID', $this->siteID);
  340. $this->CI->db->where('pageID', $pageID);
  341. $this->CI->db->update('pages');
  342. // add the old blocks in to this new version
  343. if ($blocks = $this->get_blocks($pagedata['draftID']))
  344. {
  345. foreach($blocks as $block)
  346. {
  347. $body = $block['body'];
  348. $this->add_block($body, $draftID, $block['blockRef']);
  349. }
  350. }
  351. return $draftID;
  352. }
  353. else
  354. {
  355. return FALSE;
  356. }
  357. }
  358. function get_latest_block($versionID)
  359. {
  360. $this->CI->db->where('versionID', $versionID);
  361. $this->CI->db->where('siteID', $this->siteID);
  362. $this->CI->db->order_by('dateCreated', 'desc');
  363. // grab
  364. $query = $this->CI->db->get('page_blocks', 1);
  365. if ($query->num_rows())
  366. {
  367. $row = $query->row_array();
  368. return $row;
  369. }
  370. else
  371. {
  372. return FALSE;
  373. }
  374. }
  375. function publish_page($pageID, $draftID)
  376. {
  377. $this->CI->db->set('dateModified', date("Y-m-d H:i:s"));
  378. $this->CI->db->set('datePublished', date("Y-m-d H:i:s"));
  379. $this->CI->db->set('versionID', $draftID);
  380. $this->CI->db->set('active', 1);
  381. $this->CI->db->where('pageID', $pageID);
  382. $this->CI->db->where('siteID', $this->siteID);
  383. $this->CI->db->update('pages');
  384. return TRUE;
  385. }
  386. function publish_draft($draftID)
  387. {
  388. // publish version
  389. $this->CI->db->set('published', 1);
  390. $this->CI->db->where('siteID', $this->siteID);
  391. $this->CI->db->where('versionID', $draftID);
  392. $this->CI->db->update('page_versions');
  393. return TRUE;
  394. }
  395. function revert_version($pageID, $versionID)
  396. {
  397. // update the template with version
  398. $this->CI->db->set('versionID', $versionID);
  399. $this->CI->db->where('pageID', $pageID);
  400. $this->CI->db->where('siteID', $this->siteID);
  401. $this->CI->db->update('pages');
  402. return TRUE;
  403. }
  404. function revert_draft($pageID, $draftID)
  405. {
  406. // update the template with version
  407. $this->CI->db->set('draftID', $draftID);
  408. $this->CI->db->where('pageID', $pageID);
  409. $this->CI->db->where('siteID', $this->siteID);
  410. $this->CI->db->update('pages');
  411. return TRUE;
  412. }
  413. function add_block($body, $versionID, $blockRef = 'block')
  414. {
  415. // delete blocks for this version
  416. $this->CI->db->where('page_blocks.siteID', $this->siteID);
  417. $this->CI->db->where('page_blocks.versionID', $versionID);
  418. $this->CI->db->where('page_blocks.blockRef', $blockRef);
  419. $this->CI->db->delete('page_blocks');
  420. // add block
  421. $this->CI->db->query("SET NAMES 'utf8'");
  422. $this->CI->db->set('versionID', $versionID);
  423. $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
  424. $this->CI->db->set('siteID', $this->siteID);
  425. $this->CI->db->set('blockRef', $blockRef);
  426. $this->CI->db->set('body', $body);
  427. $this->CI->db->insert('page_blocks');
  428. return TRUE;
  429. }
  430. function add_view($pageID)
  431. {
  432. $this->CI->db->set('views', 'views+1', false);
  433. $this->CI->db->where('pageID', $pageID);
  434. $this->CI->db->where('siteID', $this->siteID);
  435. $this->CI->db->update('pages');
  436. }
  437. function get_web_form_by_ref($formRef)
  438. {
  439. $this->CI->db->where('formRef', $formRef);
  440. $this->CI->db->where('deleted', 0);
  441. $this->CI->db->where('siteID', $this->siteID);
  442. $query = $this->CI->db->get('web_forms', 1);
  443. if ($query->num_rows())
  444. {
  445. return $query->row_array();
  446. }
  447. else
  448. {
  449. return FALSE;
  450. }
  451. }
  452. function web_form()
  453. {
  454. // get web form
  455. if (!$webform = $this->CI->core->get_web_form_by_ref($this->CI->core->decode($this->CI->input->post('formID'))))
  456. {
  457. return FALSE;
  458. }
  459. // set main required field
  460. $this->CI->form_validation->set_rules('email', 'Email', 'required|valid_email');
  461. // find out if a user account needs to be created
  462. $account = ($webform['account']) ? TRUE : FALSE;
  463. // get required fields
  464. $required = $this->CI->input->post('required', TRUE);
  465. // get optional required fields
  466. if ($required)
  467. {
  468. $requiredArray = explode('|', $required);
  469. foreach($requiredArray as $field)
  470. {
  471. $this->CI->form_validation->set_rules($field, ucfirst($field), 'required');
  472. }
  473. }
  474. // optional captcha (deprecated - use javascript for captcha)
  475. (@in_array('captcha', $requiredArray)) ? $this->CI->form_validation->set_rules('captcha', 'Captcha', 'required|callback__captcha_check') : '';
  476. // get first and last name
  477. if ($this->CI->input->post('firstName', TRUE))
  478. {
  479. $firstName = $this->CI->input->post('firstName', TRUE);
  480. $lastName = $this->CI->input->post('lastName', TRUE);
  481. }
  482. elseif ($fullName = $this->CI->input->post('fullName', TRUE))
  483. {
  484. $fullNameArray = @explode(' ', $fullName);
  485. $lastName = (sizeof($fullNameArray) > 0) ? ucfirst(trim(end($fullNameArray))) : '';
  486. $firstName = (sizeof($fullNameArray) > 0) ? ucfirst(trim($fullNameArray[0])) : $fullName;
  487. }
  488. else
  489. {
  490. $firstName = '';
  491. $lastName = '';
  492. }
  493. // at least set the name and email in to a session
  494. if (!$this->CI->session->userdata('session_user'))
  495. {
  496. $this->CI->session->set_userdata('email', $this->CI->input->post('email', TRUE));
  497. $this->CI->session->set_userdata('firstName', $firstName);
  498. $this->CI->session->set_userdata('lastName', $lastName);
  499. }
  500. // if capturing check user is unique and a password matches
  501. if ($account)
  502. {
  503. // email and message are always required
  504. $this->CI->form_validation->set_rules('email', 'Email', 'required|valid_email|unique[users.email]|trim');
  505. // check if password was submitted, make it required if so
  506. if (array_key_exists('password', $_POST))
  507. {
  508. // require password confirm?
  509. if (isset($_POST['confirmPassword']))
  510. {
  511. $this->form_validation->set_rules('password', 'Password', 'required|matches[confirmPassword]');
  512. }
  513. else
  514. {
  515. $this->form_validation->set_rules('password', 'Password', 'required');
  516. }
  517. }
  518. }
  519. // look for files
  520. $files = FALSE;
  521. if ($webform['fileTypes'] && count($_FILES))
  522. {
  523. foreach($_FILES as $name => $file)
  524. {
  525. $this->CI->uploads->maxSize = '2000';
  526. $this->CI->uploads->allowedTypes = $webform['fileTypes'];
  527. // check a file has actually been uploaded
  528. if ($file['name'] != '')
  529. {
  530. if ($fileData = $this->CI->uploads->upload_file($name))
  531. {
  532. $files[$name] = $fileData;
  533. }
  534. else
  535. {
  536. $this->CI->form_validation->set_error($this->CI->uploads->errors);
  537. }
  538. }
  539. }
  540. }
  541. // add ticket
  542. if ($this->CI->form_validation->run())
  543. {
  544. if ($account)
  545. {
  546. // create user
  547. $this->create_user();
  548. // set admin session name, if given
  549. if (!$this->CI->site->config['activation'])
  550. {
  551. $this->CI->load->library('auth');
  552. $username = array('field' => 'email', 'label' => 'Email address', 'value' => $this->CI->input->post('email'));
  553. $password = ($this->CI->input->post('password')) ? $this->CI->input->post('password', TRUE) : substr(md5(time()),0,6);
  554. // login or get error message
  555. if (!$this->CI->auth->login($username, $password, 'session_user', FALSE))
  556. {
  557. $this->CI->form_validation->set_error($this->CI->auth->error);
  558. }
  559. }
  560. }
  561. // add ticket
  562. $this->add_ticket($webform, $files);
  563. // redirect if set
  564. if ($redirect = $webform['outcomeRedirect'])
  565. {
  566. redirect($redirect);
  567. }
  568. // get message if set
  569. if ($message = $webform['outcomeMessage'])
  570. {
  571. return $message;
  572. }
  573. else
  574. {
  575. return 'Thank you, your message was sent successfully.';
  576. }
  577. }
  578. else
  579. {
  580. return FALSE;
  581. }
  582. }
  583. function add_ticket($webform, $files = '')
  584. {
  585. // get web form
  586. if (!$webform)
  587. {
  588. return FALSE;
  589. }
  590. if ($this->CI->input->post('email'))
  591. {
  592. // set system fields
  593. $fields = array('required', 'formID', 'fieldSet', 'fileTypes', 'account', 'formName', 'outcomeEmails', 'outcomeRedirect', 'outcomeMessage', 'fullName', 'email', 'subject', 'message', 'toEmail', 'captcha', 'firstName', 'lastName', 'password', 'confirmPassword', 'groupID');
  594. // set default message
  595. $message = '';
  596. $filepaths = '';
  597. // get extra posted info and prepend to message
  598. if (count($_POST))
  599. {
  600. foreach($_POST as $post => $value)
  601. {
  602. if (!in_array($post, $fields) && !preg_match('/^submit$|^submit\_x$|^submit\_y|^x|^y/i', $post))
  603. {
  604. $postValue = $this->CI->input->post($post, TRUE);
  605. $message .= "\t".ucfirst($post) . ": ".$value."\n\n";
  606. }
  607. }
  608. }
  609. // get files and prepend to message
  610. if ($files)
  611. {
  612. $message .= "\tFiles: ".count($files).((count($files) != 1) ? ' files' : ' file')." uploaded\n\n";
  613. $filepaths .= '<br />';
  614. foreach($files as $name => $fileData)
  615. {
  616. $filepaths .= '<br /><a href="'.site_url($this->CI->uploads->uploadsPath.'/'.$fileData['file_name']).'">'.$fileData['client_name'].'</a>';
  617. }
  618. }
  619. // get posted message
  620. $message .= (strlen($message) > 1) ? "\n" : '';
  621. $message .= $this->CI->input->post('message', TRUE);
  622. // set defaults
  623. $fullName = ($this->CI->input->post('fullName')) ? $this->CI->input->post('fullName', TRUE) : 'N/A';
  624. $subject = ($this->CI->input->post('subject')) ? $this->CI->input->post('subject', TRUE) : (($webform['formName']) ? $webform['formName'] : 'No Subject');
  625. $outcomeEmails = ($webform['outcomeEmails']) ? explode(',', $webform['outcomeEmails']) : $this->CI->site->config['siteEmail'];
  626. // get first name and last name
  627. $names = explode(' ', $fullName);
  628. $firstName = (sizeof($names) > 1 && $names[0]) ? ucfirst(trim($names[0])) : '';
  629. $lastName = (sizeof($names) > 1) ? ucfirst(end($names)) : '';
  630. // add ticket
  631. $this->CI->db->set('siteID', $this->siteID);
  632. $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
  633. ($webform['formName']) ? $this->CI->db->set('formName', $webform['formName']) : '';
  634. $this->CI->db->set('fullName', $fullName);
  635. $this->CI->db->set('email', $this->CI->input->post('email', TRUE));
  636. $this->CI->db->set('subject', $subject);
  637. $this->CI->db->set('body', $message.$filepaths);
  638. $this->CI->db->insert('tickets');
  639. $ticketID = $this->CI->db->insert_id();
  640. // set header and footer
  641. $emailHeader = str_replace('{name}', $fullName, $this->CI->site->config['emailHeader']);
  642. $emailHeader = str_replace('{first-name}', $firstName, $emailHeader);
  643. $emailHeader = str_replace('{last-name}', $lastName, $emailHeader);
  644. $emailHeader = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailHeader);
  645. $emailFooter = str_replace('{name}', $fullName, $this->CI->site->config['emailFooter']);
  646. $emailFooter = str_replace('{first-name}', $firstName, $emailFooter);
  647. $emailFooter = str_replace('{last-name}', $lastName, $emailFooter);
  648. $emailFooter = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailFooter);
  649. $emailTicket = str_replace('{name}', $fullName, $this->CI->site->config['emailTicket']);
  650. $emailTicket = str_replace('{first-name}', $firstName, $emailTicket);
  651. $emailTicket = str_replace('{last-name}', $lastName, $emailTicket);
  652. $emailTicket = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailTicket);
  653. // send despatch email to customer
  654. $body = $emailHeader."\n\n";
  655. $body .= $emailTicket."\n\n";
  656. $body .= "\tTicket ID: ".$ticketID."\n";
  657. $body .= "\tSubject: ".$subject."\n";
  658. $body .= "\tName: ".$fullName."\n";
  659. $body .= "\tEmail: ".$this->CI->input->post('email')."\n\n";
  660. // attach message
  661. if ($message)
  662. {
  663. $body .= "Message:\n";
  664. $body .= "---------------------------------------------\n\n";
  665. $body .= $message."\n\n";
  666. $body .= "---------------------------------------------\n\n";
  667. }
  668. // send username and password
  669. if ($webform['account'])
  670. {
  671. $body .= "Your login details are below:\n";
  672. $body .= "---------------------------------------------\n\n";
  673. $body .= "Your email: \t".$this->CI->input->post('email')."\n";
  674. $body .= "Your password: \t".(($this->CI->input->post('password', TRUE)) ? $this->CI->input->post('password', TRUE) : substr(md5(time()),0,6))."\n\n";
  675. $body .= "---------------------------------------------\n\n";
  676. }
  677. $footerBody = $emailFooter;
  678. // load email lib and email user and admin
  679. $this->CI->load->library('email');
  680. // attach files
  681. if ($files)
  682. {
  683. foreach ($files as $file)
  684. {
  685. $this->CI->email->attach($file['full_path']);
  686. }
  687. }
  688. // send to recipient
  689. $this->CI->email->to($this->CI->input->post('email', TRUE));
  690. $this->CI->email->from($this->CI->site->config['siteEmail'], $this->CI->site->config['siteName']);
  691. $this->CI->email->subject('[#'.$ticketID.']: ' . $subject);
  692. $this->CI->email->message($body.$footerBody);
  693. $this->CI->email->send();
  694. $this->CI->email->clear();
  695. // send to CC or admin
  696. $this->CI->email->to($outcomeEmails);
  697. $this->CI->email->from($this->CI->input->post('email', TRUE));
  698. $this->CI->email->subject('FW: [#'.$ticketID.']: ' . $this->CI->input->post('subject', TRUE));
  699. $this->CI->email->message("A web form was submitted on ".$this->CI->site->config['siteName'].".\n\n---------------------------------------------\n\n".$body.$footerBody);
  700. $this->CI->email->send();
  701. return $ticketID;
  702. }
  703. else
  704. {
  705. return FALSE;
  706. }
  707. }
  708. function create_user()
  709. {
  710. // get values
  711. $this->CI->core->get_values('users');
  712. // security check
  713. if ($this->CI->input->post('username')) $this->CI->core->set['username'] = '';
  714. if ($this->CI->input->post('subscribed')) $this->CI->core->set['subscribed'] = '';
  715. if ($this->CI->input->post('plan')) $this->CI->core->set['plan'] = '';
  716. if ($this->CI->input->post('siteID')) $this->CI->core->set['siteID'] = $this->siteID;
  717. if ($this->CI->input->post('userID')) $this->CI->core->set['userID'] = '';
  718. if ($this->CI->input->post('kudos')) $this->CI->core->set['kudos'] = '';
  719. if ($this->CI->input->post('posts')) $this->CI->core->set['posts'] = '';
  720. // set folder (making sure it's not an admin folder)
  721. $permissionGroupsArray = $this->CI->permission->get_groups('admin');
  722. foreach((array)$permissionGroupsArray as $group)
  723. {
  724. $permissionGroups[$group['groupID']] = $group['groupName'];
  725. }
  726. if ($this->CI->input->post('groupID') > 0 && !@in_array($this->CI->input->post('groupID'), $permissionGroups))
  727. {
  728. $this->CI->core->set['groupID'] = $this->CI->input->post('groupID');
  729. }
  730. // set date
  731. $this->CI->core->set['dateCreated'] = date("Y-m-d H:i:s");
  732. // init null name
  733. $firstName = '';
  734. $lastName = '';
  735. // set name if only fullName is posted
  736. if ($this->CI->input->post('fullName') && (!$this->CI->input->post('firstName') && !$this->CI->input->post('lastName')))
  737. {
  738. $fullName = $this->CI->input->post('fullName', TRUE);
  739. $fullNameArray = @explode(' ', $fullName);
  740. $lastName = (sizeof($fullNameArray) > 0) ? ucfirst(trim(end($fullNameArray))) : '';
  741. $firstName = (sizeof($fullNameArray) > 0) ? ucfirst(trim($fullNameArray[0])) : $fullName;
  742. $this->CI->core->set['firstName'] = $firstName;
  743. $this->CI->core->set['lastName'] = $lastName;
  744. }
  745. // set first name
  746. if ($this->CI->input->post('firstName'))
  747. {
  748. $firstName = ucfirst($this->CI->input->post('firstName', TRUE));
  749. $this->CI->core->set['firstName'] = $firstName;
  750. }
  751. // set last name
  752. if ($this->CI->input->post('lastName'))
  753. {
  754. $lastName = ucfirst($this->CI->input->post('lastName', TRUE));
  755. $this->CI->core->set['lastName'] = $lastName;
  756. }
  757. // generate password
  758. if (!$this->CI->input->post('password'))
  759. {
  760. $password = md5(substr(md5(time()),0,6));
  761. $this->CI->core->set['password'] = $password;
  762. }
  763. // set manual activation
  764. if ($this->CI->site->config['activation'])
  765. {
  766. $this->CI->core->set['active'] = 0;
  767. }
  768. // set email on flash data
  769. $flashEmail = $this->CI->session->flashdata('email');
  770. // update table
  771. if ($this->CI->input->post('email') && ($this->CI->input->post('password') || $password))
  772. {
  773. if ($this->CI->core->update('users'))
  774. {
  775. $result = array(
  776. 'userID' => $this->CI->db->insert_id(),
  777. 'email' => $this->CI->input->post('email', TRUE),
  778. 'password' => ($this->CI->input->post('password')) ? $this->CI->input->post('password', TRUE) : $password,
  779. 'firstName' => $firstName,
  780. 'lastName' => $lastName
  781. );
  782. return $result;
  783. }
  784. else
  785. {
  786. return FALSE;
  787. }
  788. }
  789. else
  790. {
  791. return FALSE;
  792. }
  793. }
  794. function captcha_check()
  795. {
  796. // Then see if a captcha exists:
  797. $exp=time()-600;
  798. $sql = "SELECT COUNT(*) AS count FROM ha_captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
  799. $binds = array($this->CI->input->post('captcha'), $this->CI->input->ip_address(), $exp);
  800. $query = $this->CI->db->query($sql, $binds);
  801. $row = $query->row();
  802. if ($row->count == 0)
  803. {
  804. $this->CI->form_validation->set_message('_captcha_check', 'The Captcha word was not correct.');
  805. return FALSE;
  806. }
  807. else
  808. {
  809. return TRUE;
  810. }
  811. }
  812. /* UTILITIES */
  813. // gets posted values
  814. function get_post()
  815. {
  816. if (count($_POST))
  817. {
  818. $post = array();
  819. foreach($_POST as $key => $value)
  820. {
  821. $post[$key] = $this->CI->input->post($key);
  822. }
  823. return $post;
  824. }
  825. else
  826. {
  827. return FALSE;
  828. }
  829. }
  830. // gets values from post and/or the row
  831. function get_values($data = '', $id = '')
  832. {
  833. // init array
  834. $values = array();
  835. // populate by row if set
  836. if (@is_array($data))
  837. {
  838. $row = $data;
  839. $values = $data;
  840. }
  841. // get data from database
  842. else
  843. {
  844. $table = $data;
  845. if ($id)
  846. {
  847. $query = $this->CI->db->get_where($table, $id);
  848. if ($query->num_rows())
  849. {
  850. $row = $query->row_array();
  851. $values = $row;
  852. }
  853. }
  854. }
  855. // get post if there is any
  856. if ($post = $this->get_post())
  857. {
  858. // check posted data is in fields
  859. foreach ($post as $field => $value)
  860. {
  861. // make sure the value is just a normal value and not an array
  862. if (!is_array($value))
  863. {
  864. if (isset($row) && isset($row[$field]) && $value == $row[$field])
  865. {
  866. unset($this->required[$field]);
  867. }
  868. else
  869. {
  870. // prep password
  871. if ($field == 'password')
  872. {
  873. if ($value != '')
  874. {
  875. $values[$field] = md5($value);
  876. }
  877. }
  878. // overwrite value with posted value
  879. else
  880. {
  881. $values[$field] = $value;
  882. }
  883. }
  884. if (array_key_exists($field, $this->set))
  885. {
  886. unset($values[$field]);
  887. }
  888. }
  889. }
  890. }
  891. return $values;
  892. }
  893. // is ajax?
  894. function is_ajax()
  895. {
  896. return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
  897. }
  898. // check for errors
  899. function check_errors()
  900. {
  901. // set rules for validation
  902. if (isset($this->required))
  903. {
  904. $config = array();
  905. foreach ($this->required as $field => $name)
  906. {
  907. if (is_array($name))
  908. {
  909. $config[$field] = array('field' => $field, 'label' => $name['label'], 'rules' => $name['rules']);
  910. }
  911. else
  912. {
  913. if ($field == 'email')
  914. {
  915. $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required|valid_email');
  916. }
  917. elseif ($field == 'password')
  918. {
  919. $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required|matches[confirmPassword]');
  920. }
  921. else
  922. {
  923. $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required');
  924. }
  925. }
  926. }
  927. // set rules and fields for validation
  928. $this->CI->form_validation->set_rules($config);
  929. if (!$this->CI->form_validation->run() && isset($this->required) && count($this->required))
  930. {
  931. return FALSE;
  932. }
  933. else
  934. {
  935. return TRUE;
  936. }
  937. }
  938. else
  939. {
  940. return TRUE;
  941. }
  942. }
  943. // get all rows from a table
  944. function viewall($table, $where = '', $order = '', $limit = '')
  945. {
  946. // get table fields
  947. $fields = $this->CI->db->list_fields($table);
  948. // set limit from uri if set
  949. $limit = (!$limit) ? $this->CI->site->config['paging'] : $limit;
  950. // get uri array for ordering
  951. $uriArray = $this->CI->uri->uri_to_assoc($this->uri_assoc_segment);
  952. // set order on order array
  953. if (count($uriArray))
  954. {
  955. foreach($uriArray as $key => $value)
  956. {
  957. if ($key)
  958. {
  959. if ($key == 'orderasc')
  960. {
  961. $this->CI->db->order_by($value,'asc');
  962. }
  963. elseif ($key == 'orderdesc')
  964. {
  965. $this->CI->db->order_by($value,'desc');
  966. }
  967. }
  968. }
  969. }
  970. // order override
  971. elseif ($order && !is_array($order))
  972. {
  973. $this->CI->db->order_by($order, 'asc');
  974. }
  975. elseif ($order && is_array($order))
  976. {
  977. $this->CI->db->order_by($order[0], $order[1]);
  978. }
  979. if (!(isset($uriArray['orderasc']) || isset($uriArray['orderdesc'])) && in_array('dateCreated', $fields))
  980. {
  981. $this->CI->db->order_by('dateCreated', 'desc');
  982. }
  983. // wheres
  984. if ($where)
  985. {
  986. $this->CI->db->where($where);
  987. }
  988. if (!$this->adminOverRide && $this->siteID)
  989. {
  990. $this->CI->db->where('siteID', $this->siteID);
  991. }
  992. if (in_array('deleted', $fields))
  993. {
  994. $this->CI->db->where('deleted', 0);
  995. }
  996. // get and return results
  997. $query = $this->CI->db->get($table, $limit, $this->CI->pagination->offset);
  998. $output[$table] = $query->result_array();
  999. // do same thing again but get count
  1000. if ($where)
  1001. {
  1002. $this->CI->db->where($where);
  1003. }
  1004. if (!$this->adminOverRide && $this->siteID)
  1005. {
  1006. $this->CI->db->where('siteID', $this->siteID);
  1007. }
  1008. if (in_array('deleted', $fields))
  1009. {
  1010. $this->CI->db->where('deleted', 0);
  1011. }
  1012. $query_total = $this->CI->db->get($table);
  1013. $totalRows = $query_total->num_rows();
  1014. // set pagination config
  1015. $this->set_paging($totalRows, $limit);
  1016. return $output;
  1017. }
  1018. // update table
  1019. function update($table, $id = '')
  1020. {
  1021. if (count($_POST) || count($_FILES))
  1022. {
  1023. // get fields of this table
  1024. $fields = $this->CI->db->list_fields($table);
  1025. // get data from database
  1026. if ($id)
  1027. {
  1028. $query = $this->CI->db->get_where($table, $id);
  1029. if ($query->num_rows())
  1030. {
  1031. $row = $query->row_array();
  1032. }
  1033. }
  1034. // get values
  1035. $values = @$this->get_values($row);
  1036. // check posted data is in fields
  1037. foreach ($values as $field => $value)
  1038. {
  1039. if (@!in_array($field, $fields))
  1040. {
  1041. unset($values[$field]);
  1042. }
  1043. if (array_key_exists($field, $this->set))
  1044. {
  1045. unset($values[$field]);
  1046. }
  1047. }
  1048. // if validate is unsuccessful show errors (return false) else insert and redirect
  1049. if ($this->check_errors())
  1050. {
  1051. // set siteID
  1052. if (!$this->adminOverRide && $this->siteID)
  1053. {
  1054. $this->set['siteID'] = SITEID;
  1055. }
  1056. // set fields
  1057. if ($this->set && sizeof($this->set) > 0)
  1058. {
  1059. $this->CI->db->set($this->set);
  1060. unset($this->set);
  1061. }
  1062. // add row
  1063. if (@!$row && !$id)
  1064. {
  1065. $this->CI->db->insert($table, $values);
  1066. }
  1067. // edit row
  1068. else
  1069. {
  1070. if ($this->where && sizeof($this->where) > 0)
  1071. {
  1072. $this->CI->db->where($this->where);
  1073. }
  1074. $this->CI->db->where($id);
  1075. $this->CI->db->update($table, $values);
  1076. }
  1077. unset($this->required);
  1078. return TRUE;
  1079. }
  1080. else
  1081. {
  1082. return FALSE;
  1083. }
  1084. }
  1085. else
  1086. {
  1087. return FALSE;
  1088. }
  1089. }
  1090. // set paging
  1091. function set_paging($totalRows, $limit = '')
  1092. {
  1093. // get default limit
  1094. $limit = ($limit) ? $limit : $this->CI->site->config['paging'];
  1095. // set pagination config
  1096. $config['total_rows'] = $totalRows;
  1097. $config['per_page'] = $limit;
  1098. $config['full_tag_open'] = '<div class="pagination"><p>';
  1099. $config['full_tag_close'] = '</p></div>';
  1100. $config['num_links'] = 6;
  1101. $this->CI->pagination->initialize($config);
  1102. }
  1103. // delete permanently
  1104. function delete($table, $id)
  1105. {
  1106. // delete item from db
  1107. if (!$this->adminOverRide && $this->siteID)
  1108. {
  1109. $this->CI->db->where('siteID', $this->siteID);
  1110. }
  1111. if ($this->where && sizeof($this->where) > 0)
  1112. {
  1113. $this->CI->db->where($this->where);
  1114. }
  1115. $this->CI->db->delete($table, $id);
  1116. if ($this->CI->db->affected_rows())
  1117. {
  1118. return true;
  1119. }
  1120. else
  1121. {
  1122. return false;
  1123. }
  1124. }
  1125. // delete from site but keep in database
  1126. function soft_delete($table, $id)
  1127. {
  1128. // soft delete item from db
  1129. if (!$this->adminOverRide && $this->siteID)
  1130. {
  1131. $this->CI->db->where('siteID', $this->siteID);
  1132. }
  1133. $this->CI->db->set('deleted', 1);
  1134. if ($this->where && sizeof($this->where) > 0)
  1135. {
  1136. $this->CI->db->where($this->where);
  1137. }
  1138. $this->CI->db->where($id);
  1139. $this->CI->db->update($table);
  1140. if ($this->CI->db->affected_rows())
  1141. {
  1142. return true;
  1143. }
  1144. else
  1145. {
  1146. return false;
  1147. }
  1148. }
  1149. // order rows
  1150. function order($table = '', $field = '')
  1151. {
  1152. // for each posted item, order it with new row id
  1153. if ($table && $field)
  1154. {
  1155. foreach ($_POST[$table] as $key => $value)
  1156. {
  1157. if ($this->siteID)
  1158. {
  1159. $this->CI->db->where('siteID', $this->siteID);
  1160. }
  1161. $this->CI->db->where($field.'ID', $value);
  1162. $this->CI->db->update($table, array($field.'Order' => ($key + 1)));
  1163. }
  1164. }
  1165. else
  1166. {
  1167. return false;
  1168. }
  1169. }
  1170. // encode url
  1171. function encode($data)
  1172. {
  1173. return strtr(rtrim(base64_encode($data), '='), '+/', '-_');
  1174. }
  1175. // decode url
  1176. function decode($base64)
  1177. {
  1178. return base64_decode(strtr($base64, '-_', '+/'));
  1179. }
  1180. }