PageRenderTime 70ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/admin/settings.php

https://github.com/fayazv/Taarifa_Web
PHP | 1269 lines | 831 code | 202 blank | 236 comment | 63 complexity | b10510fbc8d9a6fd84572a25998ed5ef MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * This controller is used to manage user settings
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @subpackage Admin
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  14. */
  15. class Settings_Controller extends Admin_Controller
  16. {
  17. protected $cache;
  18. function __construct()
  19. {
  20. parent::__construct();
  21. $this->template->this_page = 'settings';
  22. // If user doesn't have access, redirect to dashboard
  23. if ( ! admin::permissions($this->user, "settings"))
  24. {
  25. url::redirect(url::site().'admin/dashboard');
  26. }
  27. $this->cache = Cache::instance();
  28. }
  29. /**
  30. * Site Settings
  31. */
  32. function site()
  33. {
  34. $this->template->content = new View('admin/site');
  35. $this->template->content->title = Kohana::lang('ui_admin.settings');
  36. $this->template->js = new View('admin/site_js');
  37. // setup and initialize form field names
  38. $form = array
  39. (
  40. 'site_name' => '',
  41. 'site_tagline' => '',
  42. 'banner_image' => '',
  43. 'delete_banner_image' => '',
  44. 'site_email' => '',
  45. 'alerts_email' => '',
  46. 'site_language' => '',
  47. 'site_timezone' => '',
  48. 'site_message' => '',
  49. 'site_copyright_statement' => '',
  50. 'site_submit_report_message' => '',
  51. 'site_contact_page' => '',
  52. 'items_per_page' => '',
  53. 'items_per_page_admin' => '',
  54. 'blocks_per_row' => '',
  55. 'allow_alerts' => '',
  56. 'allow_reports' => '',
  57. 'allow_comments' => '',
  58. 'allow_feed' => '',
  59. 'allow_stat_sharing' => '',
  60. 'allow_clustering' => '',
  61. 'cache_pages' => '',
  62. 'cache_pages_lifetime' => '',
  63. 'private_deployment' => '',
  64. 'checkins' => '',
  65. 'default_map_all' => '',
  66. 'google_analytics' => '',
  67. 'twitter_hashtags' => '',
  68. 'api_akismet' => ''
  69. );
  70. // Copy the form as errors, so the errors will be stored with keys
  71. // corresponding to the form field names
  72. $errors = $form;
  73. $form_error = FALSE;
  74. $form_saved = FALSE;
  75. // Retrieve Current Settings
  76. $settings = ORM::factory('settings', 1);
  77. // check, has the form been submitted, if so, setup validation
  78. if ($_POST)
  79. {
  80. //print_r($_POST);exit;
  81. // Instantiate Validation, use $post, so we don't overwrite $_POST
  82. // fields with our own things
  83. $post = new Validation($_POST);
  84. // Add some filters
  85. $post->pre_filter('trim', TRUE);
  86. // Add some rules, the input field, followed by a list of checks, carried out in order
  87. $post->add_rules('site_name', 'required', 'length[3,250]');
  88. $post->add_rules('site_tagline', 'length[3,250]');
  89. $post->add_rules('site_email', 'email', 'length[4,100]');
  90. //$post->add_rules('alerts_email','required', 'email', 'length[4,100]');
  91. //$post->add_rules('site_message', 'standard_text');
  92. $post->add_rules('site_copyright_statement', 'length[4,600]');
  93. $post->add_rules('site_language','required', 'length[5, 5]');
  94. //$post->add_rules('site_timezone','required', 'between[10,50]');
  95. $post->add_rules('site_contact_page','required','between[0,1]');
  96. $post->add_rules('items_per_page','required','between[10,50]');
  97. $post->add_rules('items_per_page_admin','required','between[10,50]');
  98. $post->add_rules('blocks_per_row','required','numeric');
  99. $post->add_rules('allow_alerts','required','between[0,1]');
  100. $post->add_rules('allow_reports','required','between[0,1]');
  101. $post->add_rules('allow_comments','required','between[0,2]');
  102. $post->add_rules('allow_feed','required','between[0,1]');
  103. $post->add_rules('allow_stat_sharing','required','between[0,1]');
  104. $post->add_rules('allow_clustering','required','between[0,1]');
  105. $post->add_rules('cache_pages','required','between[0,1]');
  106. $post->add_rules('cache_pages_lifetime','required','in_array[60,300,600,900,1800]');
  107. $post->add_rules('private_deployment','required','between[0,1]');
  108. $post->add_rules('checkins','required','between[0,1]');
  109. $post->add_rules('default_map_all','required', 'alpha_numeric', 'length[6,6]');
  110. $post->add_rules('google_analytics','length[0,20]');
  111. $post->add_rules('twitter_hashtags','length[0,500]');
  112. $post->add_rules('api_akismet','length[0,100]', 'alpha_numeric');
  113. // Add rules for file upload
  114. $files = Validation::factory($_FILES);
  115. $files->add_rules('banner_image', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[250K]');
  116. // Test to see if things passed the rule checks
  117. if ($post->validate() AND $files->validate())
  118. {
  119. // Yes! everything is valid
  120. $settings = new Settings_Model(1);
  121. $settings->site_name = $post->site_name;
  122. $settings->site_tagline = $post->site_tagline;
  123. $settings->site_email = $post->site_email;
  124. $settings->alerts_email = $post->alerts_email;
  125. $settings->site_message = $post->site_message;
  126. $settings->site_copyright_statement = $post->site_copyright_statement;
  127. $settings->site_submit_report_message = $post->site_submit_report_message;
  128. $settings->site_language = $post->site_language;
  129. $settings->site_timezone = $post->site_timezone;
  130. if($settings->site_timezone == "0")
  131. {
  132. // "0" is the "Server Timezone" setting and it needs to be null in the db
  133. $settings->site_timezone = NULL;
  134. }
  135. $settings->site_contact_page = $post->site_contact_page;
  136. $settings->items_per_page = $post->items_per_page;
  137. $settings->items_per_page_admin = $post->items_per_page_admin;
  138. $settings->blocks_per_row = $post->blocks_per_row;
  139. $settings->allow_alerts = $post->allow_alerts;
  140. $settings->allow_reports = $post->allow_reports;
  141. $settings->allow_comments = $post->allow_comments;
  142. $settings->allow_feed = $post->allow_feed;
  143. $settings->allow_stat_sharing = $post->allow_stat_sharing;
  144. $settings->allow_clustering = $post->allow_clustering;
  145. $settings->cache_pages = $post->cache_pages;
  146. $settings->cache_pages_lifetime = $post->cache_pages_lifetime;
  147. $settings->private_deployment = $post->private_deployment;
  148. $settings->checkins = $post->checkins;
  149. $settings->default_map_all = $post->default_map_all;
  150. $settings->google_analytics = $post->google_analytics;
  151. $settings->twitter_hashtags = $post->twitter_hashtags;
  152. $settings->api_akismet = $post->api_akismet;
  153. $settings->date_modify = date("Y-m-d H:i:s",time());
  154. $settings->save();
  155. // Deal with banner image now
  156. // Check if deleting or updating a new image (or doing nothing)
  157. if( isset($post->delete_banner_image) AND $post->delete_banner_image == 1)
  158. {
  159. // Delete old badge image
  160. ORM::factory('media')->delete($settings->site_banner_id);
  161. // Remove from DB table
  162. $settings = new Settings_Model(1);
  163. $settings->site_banner_id = NULL;
  164. $settings->save();
  165. }else{
  166. // We aren't deleting, so try to upload if we are uploading an image
  167. $filename = upload::save('banner_image');
  168. if ($filename)
  169. {
  170. $new_filename = "banner";
  171. $file_type = strrev(substr(strrev($filename),0,4));
  172. // Large size
  173. $l_name = $new_filename.$file_type;
  174. Image::factory($filename)->save(Kohana::config('upload.directory', TRUE).$l_name);
  175. // Medium size
  176. $m_name = $new_filename."_m".$file_type;
  177. Image::factory($filename)->resize(80,80,Image::HEIGHT)
  178. ->save(Kohana::config('upload.directory', TRUE).$m_name);
  179. // Thumbnail
  180. $t_name = $new_filename."_t".$file_type;
  181. Image::factory($filename)->resize(60,60,Image::HEIGHT)
  182. ->save(Kohana::config('upload.directory', TRUE).$t_name);
  183. // Name the files for the DB
  184. $media_link = $l_name;
  185. $media_medium = $m_name;
  186. $media_thumb = $t_name;
  187. // Okay, now we have these three different files on the server, now check to see
  188. // if we should be dropping them on the CDN
  189. if (Kohana::config("cdn.cdn_store_dynamic_content"))
  190. {
  191. $media_link = cdn::upload($media_link);
  192. $media_medium = cdn::upload($media_medium);
  193. $media_thumb = cdn::upload($media_thumb);
  194. // We no longer need the files we created on the server. Remove them.
  195. $local_directory = rtrim(Kohana::config('upload.directory', TRUE), '/').'/';
  196. unlink($local_directory.$l_name);
  197. unlink($local_directory.$m_name);
  198. unlink($local_directory.$t_name);
  199. }
  200. // Remove the temporary file
  201. unlink($filename);
  202. // Save banner image in the media table
  203. $media = new Media_Model();
  204. $media->media_type = 1; // Image
  205. $media->media_link = $media_link;
  206. $media->media_medium = $media_medium;
  207. $media->media_thumb = $media_thumb;
  208. $media->media_date = date("Y-m-d H:i:s",time());
  209. $media->save();
  210. // Save new banner image in settings
  211. $settings = new Settings_Model(1);
  212. $settings->site_banner_id = $media->id;
  213. $settings->save();
  214. }
  215. }
  216. // Delete Settings Cache
  217. $this->cache->delete('settings');
  218. $this->cache->delete_tag('settings');
  219. // Everything is A-Okay!
  220. $form_saved = TRUE;
  221. // repopulate the form fields
  222. $form = arr::overwrite($form, $post->as_array());
  223. }
  224. // No! We have validation errors, we need to show the form again,
  225. // with the errors
  226. else
  227. {
  228. // repopulate the form fields
  229. $form = arr::overwrite($form, $post->as_array());
  230. // populate the error fields, if any
  231. if(is_array($files->errors()) AND count($files->errors()) > 0){
  232. // Error with file upload
  233. $errors = arr::overwrite($errors, $files->errors('settings'));
  234. }else{
  235. // Error with other form filed
  236. $errors = arr::overwrite($errors, $post->errors('settings'));
  237. }
  238. $form_error = TRUE;
  239. }
  240. }
  241. else
  242. {
  243. $form = array
  244. (
  245. 'site_name' => $settings->site_name,
  246. 'site_tagline' => $settings->site_tagline,
  247. 'site_banner_id' => $settings->site_banner_id,
  248. 'site_email' => $settings->site_email,
  249. 'alerts_email' => $settings->alerts_email,
  250. 'site_message' => $settings->site_message,
  251. 'site_copyright_statement' => $settings->site_copyright_statement,
  252. 'site_submit_report_message' => $settings->site_submit_report_message,
  253. 'site_language' => $settings->site_language,
  254. 'site_timezone' => $settings->site_timezone,
  255. 'site_contact_page' => $settings->site_contact_page,
  256. 'items_per_page' => $settings->items_per_page,
  257. 'items_per_page_admin' => $settings->items_per_page_admin,
  258. 'blocks_per_row' => $settings->blocks_per_row,
  259. 'allow_alerts' => $settings->allow_alerts,
  260. 'allow_reports' => $settings->allow_reports,
  261. 'allow_comments' => $settings->allow_comments,
  262. 'allow_feed' => $settings->allow_feed,
  263. 'allow_stat_sharing' => $settings->allow_stat_sharing,
  264. 'allow_clustering' => $settings->allow_clustering,
  265. 'cache_pages' => $settings->cache_pages,
  266. 'cache_pages_lifetime' => $settings->cache_pages_lifetime,
  267. 'private_deployment' => $settings->private_deployment,
  268. 'checkins' => $settings->checkins,
  269. 'default_map_all' => $settings->default_map_all,
  270. 'google_analytics' => $settings->google_analytics,
  271. 'twitter_hashtags' => $settings->twitter_hashtags,
  272. 'api_akismet' => $settings->api_akismet
  273. );
  274. }
  275. // Get banner image
  276. if($settings->site_banner_id != NULL){
  277. $banner = ORM::factory('media')->find($settings->site_banner_id);
  278. $this->template->content->banner = $banner->media_link;
  279. $this->template->content->banner_m = $banner->media_medium;
  280. $this->template->content->banner_t = $banner->media_thumb;
  281. }else{
  282. $this->template->content->banner = NULL;
  283. $this->template->content->banner_m = NULL;
  284. $this->template->content->banner_t = NULL;
  285. }
  286. $this->template->colorpicker_enabled = TRUE;
  287. $this->template->content->form = $form;
  288. $this->template->content->errors = $errors;
  289. $this->template->content->form_error = $form_error;
  290. $this->template->content->form_saved = $form_saved;
  291. $this->template->content->items_per_page_array = array('10'=>'10 Items','20'=>'20 Items','30'=>'30 Items','50'=>'50 Items');
  292. $blocks_per_row_array = array();
  293. for ($i=1; $i <= 21; $i++)
  294. {
  295. $blocks_per_row_array[$i] = $i;
  296. }
  297. $this->template->content->blocks_per_row_array = $blocks_per_row_array;
  298. $this->template->content->yesno_array = array(
  299. '1'=>strtoupper(Kohana::lang('ui_main.yes')),
  300. '0'=>strtoupper(Kohana::lang('ui_main.no')));
  301. $this->template->content->comments_array = array(
  302. '1'=>strtoupper(Kohana::lang('ui_main.yes')." - ".Kohana::lang('ui_admin.approve_auto')),
  303. '2'=>strtoupper(Kohana::lang('ui_main.yes')." - ".Kohana::lang('ui_admin.approve_manual')),
  304. '0'=>strtoupper(Kohana::lang('ui_main.no')));
  305. $this->template->content->cache_pages_lifetime_array = array(
  306. '60'=>'1 '.Kohana::lang('ui_admin.minute'),
  307. '300'=>'5 '.Kohana::lang('ui_admin.minutes'),
  308. '600'=>'10 '.Kohana::lang('ui_admin.minutes'),
  309. '900'=>'15 '.Kohana::lang('ui_admin.minutes'),
  310. '1800'=>'30 '.Kohana::lang('ui_admin.minutes'));
  311. //Generate all timezones
  312. $site_timezone_array = array();
  313. $site_timezone_array[0] = Kohana::lang('ui_admin.server_time');
  314. foreach (timezone_identifiers_list() as $timezone)
  315. {
  316. $site_timezone_array[$timezone] = $timezone;
  317. }
  318. $this->template->content->site_timezone_array = $site_timezone_array;
  319. // Generate Available Locales
  320. $locales = ush_locale::get_i18n();
  321. $this->template->content->locales_array = $locales;
  322. $this->cache->set('locales', $locales, array('locales'), 604800);
  323. }
  324. /**
  325. * Map Settings
  326. */
  327. function index($saved = false)
  328. {
  329. // Display all maps
  330. $this->template->api_url = Kohana::config('settings.api_url_all');
  331. // Current Default Country
  332. $current_country = Kohana::config('settings.default_country');
  333. $this->template->content = new View('admin/settings');
  334. $this->template->content->title = Kohana::lang('ui_admin.settings');
  335. // setup and initialize form field names
  336. $form = array
  337. (
  338. 'default_map' => '',
  339. 'api_google' => '',
  340. 'api_yahoo' => '',
  341. 'default_country' => '',
  342. 'multi_country' => '',
  343. 'default_lat' => '',
  344. 'default_lon' => '',
  345. 'default_zoom' => ''
  346. );
  347. // Copy the form as errors, so the errors will be stored with keys
  348. // corresponding to the form field names
  349. $errors = $form;
  350. $form_error = FALSE;
  351. if ($saved == 'saved')
  352. {
  353. $form_saved = TRUE;
  354. }
  355. else
  356. {
  357. $form_saved = FALSE;
  358. }
  359. // check, has the form been submitted, if so, setup validation
  360. if ($_POST)
  361. {
  362. // Instantiate Validation, use $post, so we don't overwrite $_POST
  363. // fields with our own things
  364. $post = new Validation($_POST);
  365. // Add some filters
  366. $post->pre_filter('trim', TRUE);
  367. // Add some rules, the input field, followed by a list of checks, carried out in order
  368. $post->add_rules('default_country', 'required', 'numeric', 'length[1,4]');
  369. $post->add_rules('multi_country', 'numeric', 'length[1,1]');
  370. $post->add_rules('default_map', 'required', 'length[0,100]');
  371. $post->add_rules('api_google','required', 'length[0,200]');
  372. $post->add_rules('api_yahoo','required', 'length[0,200]');
  373. $post->add_rules('default_zoom','required','between[0,21]'); // Validate for maximum and minimum zoom values
  374. $post->add_rules('default_lat','required','between[-85,85]'); // Validate for maximum and minimum latitude values
  375. $post->add_rules('default_lon','required','between[-180,180]'); // Validate for maximum and minimum longitude values
  376. // Test to see if things passed the rule checks
  377. if ($post->validate())
  378. {
  379. // Yes! everything is valid
  380. $settings = new Settings_Model(1);
  381. $settings->default_country = $post->default_country;
  382. $settings->multi_country = $post->multi_country;
  383. $settings->default_map = $post->default_map;
  384. $settings->api_google = $post->api_google;
  385. $settings->api_yahoo = $post->api_yahoo;
  386. $settings->default_zoom = $post->default_zoom;
  387. $settings->default_lat = $post->default_lat;
  388. $settings->default_lon = $post->default_lon;
  389. $settings->date_modify = date("Y-m-d H:i:s",time());
  390. $settings->save();
  391. // Delete Settings Cache
  392. $this->cache->delete('settings');
  393. $this->cache->delete_tag('settings');
  394. // Everything is A-Okay!
  395. $form_saved = TRUE;
  396. // Redirect to reload everything over again
  397. url::redirect('admin/settings/index/saved');
  398. }
  399. // No! We have validation errors, we need to show the form again,
  400. // with the errors
  401. else
  402. {
  403. // repopulate the form fields
  404. $form = arr::overwrite($form, $post->as_array());
  405. // populate the error fields, if any
  406. $errors = arr::overwrite($errors, $post->errors('settings'));
  407. $form_error = TRUE;
  408. }
  409. }
  410. else
  411. {
  412. // Retrieve Current Settings
  413. $settings = ORM::factory('settings', 1);
  414. $form = array
  415. (
  416. 'default_map' => $settings->default_map,
  417. 'api_google' => $settings->api_google,
  418. 'api_yahoo' => $settings->api_yahoo,
  419. 'default_country' => $settings->default_country,
  420. 'multi_country' => $settings->multi_country,
  421. 'default_lat' => $settings->default_lat,
  422. 'default_lon' => $settings->default_lon,
  423. 'default_zoom' => $settings->default_zoom
  424. );
  425. }
  426. $this->template->content->form = $form;
  427. $this->template->content->errors = $errors;
  428. $this->template->content->form_error = $form_error;
  429. $this->template->content->form_saved = $form_saved;
  430. // Get Countries
  431. $countries = array();
  432. foreach (ORM::factory('country')->orderby('country')->find_all() as $country)
  433. {
  434. // Create a list of all categories
  435. $this_country = $country->country;
  436. if (strlen($this_country) > 35)
  437. {
  438. $this_country = substr($this_country, 0, 30) . "...";
  439. }
  440. $countries[$country->id] = $this_country;
  441. }
  442. $this->template->content->countries = $countries;
  443. // Zoom Array for Slider
  444. $default_zoom_array = array();
  445. for ($i=Kohana::config('map.minZoomLevel'); $i<Kohana::config('map.minZoomLevel')+Kohana::config('map.numZoomLevels') ; $i++)
  446. {
  447. $default_zoom_array[$i] = $i;
  448. }
  449. $this->template->content->default_zoom_array = $default_zoom_array;
  450. // Get Map API Providers
  451. $layers = map::base();
  452. $map_array = array();
  453. foreach ($layers as $layer)
  454. {
  455. $map_array[$layer->name] = $layer->title;
  456. }
  457. $this->template->content->map_array = $map_array;
  458. // Javascript Header
  459. $this->template->map_enabled = TRUE;
  460. $this->template->js = new View('admin/settings_js');
  461. $this->template->js->default_map = $form['default_map'];
  462. $this->template->js->default_zoom = $form['default_zoom'];
  463. $this->template->js->default_lat = $form['default_lat'];
  464. $this->template->js->default_lon = $form['default_lon'];
  465. $this->template->js->all_maps_json = $this->_generate_settings_map_js();
  466. }
  467. /**
  468. * Handles SMS Settings
  469. */
  470. function sms()
  471. {
  472. $this->template->content = new View('admin/sms');
  473. $this->template->content->title = Kohana::lang('ui_admin.settings');
  474. // setup and initialize form field names
  475. $form = array
  476. (
  477. 'sms_provider' => '',
  478. 'sms_no1' => '',
  479. 'sms_no2' => '',
  480. 'sms_no3' => ''
  481. );
  482. // Copy the form as errors, so the errors will be stored with keys
  483. // corresponding to the form field names
  484. $errors = $form;
  485. $form_error = FALSE;
  486. $form_saved = FALSE;
  487. // check, has the form been submitted, if so, setup validation
  488. if ($_POST)
  489. {
  490. // Instantiate Validation, use $post, so we don't overwrite $_POST
  491. // fields with our own things
  492. $post = new Validation($_POST);
  493. // Add some filters
  494. $post->pre_filter('trim', TRUE);
  495. // Add some rules, the input field, followed by a list of checks, carried out in order
  496. $post->add_rules('sms_provider', 'length[1,100]');
  497. $post->add_rules('sms_no1', 'numeric', 'length[1,30]');
  498. $post->add_rules('sms_no2', 'numeric', 'length[1,30]');
  499. $post->add_rules('sms_no3', 'numeric', 'length[1,30]');
  500. // Test to see if things passed the rule checks
  501. if ($post->validate())
  502. {
  503. // Yes! everything is valid
  504. $settings = new Settings_Model(1);
  505. $settings->sms_provider = $post->sms_provider;
  506. $settings->sms_no1 = $post->sms_no1;
  507. $settings->sms_no2 = $post->sms_no2;
  508. $settings->sms_no3 = $post->sms_no3;
  509. $settings->date_modify = date("Y-m-d H:i:s",time());
  510. $settings->save();
  511. // Delete Settings Cache
  512. $this->cache->delete('settings');
  513. $this->cache->delete_tag('settings');
  514. // Everything is A-Okay!
  515. $form_saved = TRUE;
  516. // repopulate the form fields
  517. $form = arr::overwrite($form, $post->as_array());
  518. }
  519. // No! We have validation errors, we need to show the form again,
  520. // with the errors
  521. else
  522. {
  523. // repopulate the form fields
  524. $form = arr::overwrite($form, $post->as_array());
  525. // populate the error fields, if any
  526. $errors = arr::overwrite($errors, $post->errors('settings'));
  527. $form_error = TRUE;
  528. }
  529. }
  530. else
  531. {
  532. // Retrieve Current Settings
  533. $settings = ORM::factory('settings', 1);
  534. $form = array
  535. (
  536. 'sms_provider' => $settings->sms_provider,
  537. 'sms_no1' => $settings->sms_no1,
  538. 'sms_no2' => $settings->sms_no2,
  539. 'sms_no3' => $settings->sms_no3
  540. );
  541. }
  542. $this->template->content->form = $form;
  543. $this->template->content->errors = $errors;
  544. $this->template->content->form_error = $form_error;
  545. $this->template->content->form_saved = $form_saved;
  546. $this->template->content->sms_provider_array = array_merge(
  547. array("" => "-- Select One --"),
  548. plugin::get_sms_providers()
  549. );
  550. }
  551. /**
  552. * Email Settings
  553. */
  554. function email()
  555. {
  556. $this->template->content = new View('admin/email');
  557. $this->template->content->title = Kohana::lang('ui_admin.settings');
  558. // setup and initialize form field names
  559. $form = array
  560. (
  561. 'email_username' => '',
  562. 'email_password' => '',
  563. 'email_port' => '',
  564. 'email_host' => '',
  565. 'email_servertype' => '',
  566. 'email_ssl' => ''
  567. );
  568. // Copy the form as errors, so the errors will be stored with keys
  569. // corresponding to the form field names
  570. $errors = $form;
  571. $form_error = FALSE;
  572. $form_saved = FALSE;
  573. // check, has the form been submitted, if so, setup validation
  574. if ($_POST)
  575. {
  576. // Instantiate Validation, use $post, so we don't overwrite $_POST
  577. // fields with our own things
  578. $post = new Validation($_POST);
  579. // Add some filters
  580. $post->pre_filter('trim', TRUE);
  581. // Add some rules, the input field, followed by a list of checks, carried out in order
  582. $post->add_rules('email_username', 'required', 'length[3,50]');
  583. $post->add_rules('email_password', 'length[3,100]');
  584. $post->add_rules('email_port', 'numeric[1,100]','length[1,20]');
  585. $post->add_rules('email_host','required', 'length[3,100]');
  586. $post->add_rules('email_servertype','required','length[3,100]');
  587. // Test to see if things passed the rule checks
  588. if ($post->validate())
  589. {
  590. // Yes! everything is valid
  591. $settings = new Settings_Model(1);
  592. $settings->email_username = $post->email_username;
  593. $settings->email_password = $post->email_password;
  594. $settings->email_port = $post->email_port;
  595. $settings->email_host = $post->email_host;
  596. $settings->email_servertype = $post->email_servertype;
  597. $settings->email_ssl = $post->email_ssl;
  598. $settings->save();
  599. //add details to application/config/email.php
  600. //$this->_add_email_settings($settings);
  601. // Delete Settings Cache
  602. $this->cache->delete('settings');
  603. $this->cache->delete_tag('settings');
  604. // Everything is A-Okay!
  605. $form_saved = TRUE;
  606. // repopulate the form fields
  607. $form = arr::overwrite($form, $post->as_array());
  608. }
  609. // No! We have validation errors, we need to show the form again,
  610. // with the errors
  611. else
  612. {
  613. // repopulate the form fields
  614. $form = arr::overwrite($form, $post->as_array());
  615. // populate the error fields, if any
  616. $errors = arr::overwrite($errors, $post->errors('settings'));
  617. $form_error = TRUE;
  618. }
  619. }
  620. else
  621. {
  622. // Retrieve Current Settings
  623. $settings = ORM::factory('settings', 1);
  624. $form = array
  625. (
  626. 'email_username' => $settings->email_username,
  627. 'email_password' => $settings->email_password,
  628. 'email_port' => $settings->email_port,
  629. 'email_host' => $settings->email_host,
  630. 'email_servertype' => $settings->email_servertype,
  631. 'email_ssl' => $settings->email_ssl
  632. );
  633. }
  634. $this->template->colorpicker_enabled = TRUE;
  635. $this->template->content->form = $form;
  636. $this->template->content->errors = $errors;
  637. $this->template->content->form_error = $form_error;
  638. $this->template->content->form_saved = $form_saved;
  639. $this->template->content->email_ssl_array = array('1'=>Kohana::lang('ui_admin.yes'),'0'=>Kohana::lang('ui_admin.no'));
  640. // Javascript Header
  641. $this->template->js = new View('admin/email_js');
  642. }
  643. /**
  644. * Clean URLs settings
  645. */
  646. function cleanurl() {
  647. // We cannot allow cleanurl settings to be changed if MHI is enabled since it modifies a file in the config folder
  648. if (Kohana::config('config.enable_mhi') == TRUE)
  649. {
  650. throw new Kohana_User_Exception('Access Error', "Please contact the administrator in order to use this feature.");
  651. }
  652. $this->template->content = new View('admin/cleanurl');
  653. $this->template->content->title = Kohana::lang('ui_admin.settings');
  654. // setup and initialize form field names
  655. $form = array
  656. (
  657. 'enable_clean_url' => '',
  658. );
  659. // Copy the form as errors, so the errors will be stored with keys
  660. // corresponding to the form field names
  661. $errors = $form;
  662. $form_error = FALSE;
  663. $form_saved = FALSE;
  664. // check, has the form been submitted, if so, setup validation
  665. if ($_POST)
  666. {
  667. // Instantiate Validation, use $post, so we don't overwrite $_POST
  668. // fields with our own things
  669. $post = new Validation($_POST);
  670. // Add some filters
  671. $post->pre_filter('trim', TRUE);
  672. // Add some rules, the input field, followed by a list of checks, carried out in order
  673. $post->add_rules('enable_clean_url','required','between[0,1]');
  674. // Test to see if things passed the rule checks
  675. if ($post->validate())
  676. {
  677. // Yes! everything is valid
  678. // Delete Settings Cache
  679. $this->cache->delete('settings');
  680. $this->cache->delete_tag('settings');
  681. $this->_configure_index_page($post->enable_clean_url);
  682. // Everything is A-Okay!
  683. $form_saved = TRUE;
  684. // repopulate the form fields
  685. $form = arr::overwrite($form, $post->as_array());
  686. }
  687. // No! We have validation errors, we need to show the form again,
  688. // with the errors
  689. else
  690. {
  691. // repopulate the form fields
  692. $form = arr::overwrite($form, $post->as_array());
  693. // populate the error fields, if any
  694. $errors = arr::overwrite($errors, $post->errors('settings'));
  695. $form_error = TRUE;
  696. }
  697. }
  698. else
  699. {
  700. $yes_or_no = $this->_check_clean_url_on_ushahidi() == TRUE ? 1 : 0;
  701. // initialize form
  702. $form = array
  703. (
  704. 'enable_clean_url' => $yes_or_no,
  705. );
  706. }
  707. $this->template->content->form = $form;
  708. $this->template->content->errors = $errors;
  709. $this->template->content->form_error = $form_error;
  710. $this->template->content->form_saved = $form_saved;
  711. $this->template->content->yesno_array = array('1'=>strtoupper(Kohana::lang('ui_main.yes')),'0'=>strtoupper(Kohana::lang('ui_main.no')));
  712. $this->template->content->is_clean_url_enabled = $this->_check_for_clean_url();
  713. }
  714. /**
  715. * HTTPS settings
  716. */
  717. public function https()
  718. {
  719. // We cannot allow cleanurl settings to be changed if MHI is enabled since it modifies a file in the config folder
  720. if (Kohana::config('config.enable_mhi') == TRUE)
  721. {
  722. throw new Kohana_User_Exception('Access Error', "Please contact the administrator in order to use this feature.");
  723. }
  724. $this->template->content = new View('admin/https');
  725. $this->template->content->title = Kohana::lang('ui_admin.settings');
  726. // setup and initialize form field names
  727. $form = array
  728. (
  729. 'enable_https' => '',
  730. );
  731. // Copy the form as errors, so the errors will be stored with keys
  732. // corresponding to the form field names
  733. $errors = $form;
  734. $form_error = FALSE;
  735. $form_saved = FALSE;
  736. // check, has the form been submitted, if so, setup validation
  737. if ($_POST)
  738. {
  739. // Instantiate Validation, use $post, so we don't overwrite $_POST
  740. // fields with our own things
  741. $post = new Validation($_POST);
  742. // Add some filters
  743. $post->pre_filter('trim', TRUE);
  744. // Add some rules, the input field, followed by a list of checks, carried out in order
  745. $post->add_rules('enable_https','required','between[0,1]');
  746. // Test to see if things passed the rule checks
  747. if ($post->validate())
  748. {
  749. // Yes! everything is valid
  750. // Delete Settings Cache
  751. $this->cache->delete('settings');
  752. $this->cache->delete_tag('settings');
  753. $this->_configure_https_mode($post->enable_https);
  754. // Everything is A-Okay!
  755. $form_saved = TRUE;
  756. // repopulate the form fields
  757. $form = arr::overwrite($form, $post->as_array());
  758. }
  759. // No! We have validation errors, we need to show the form again,
  760. // with the errors
  761. else
  762. {
  763. // repopulate the form fields
  764. $form = arr::overwrite($form, $post->as_array());
  765. // populate the error fields, if any
  766. $errors = arr::overwrite($errors, $post->errors('settings'));
  767. $form_error = TRUE;
  768. }
  769. }
  770. else
  771. {
  772. $yes_or_no = $this->_is_https_enabled() == TRUE ? 1 : 0;
  773. // initialize form
  774. $form = array
  775. (
  776. 'enable_https' => $yes_or_no,
  777. );
  778. }
  779. $this->template->content->form = $form;
  780. $this->template->content->errors = $errors;
  781. $this->template->content->form_error = $form_error;
  782. $this->template->content->form_saved = $form_saved;
  783. $this->template->content->yesno_array = array('1'=>strtoupper(Kohana::lang('ui_main.yes')),'0'=>strtoupper(Kohana::lang('ui_main.no')));
  784. $this->template->content->is_https_capable = $this->_is_https_capable();
  785. }
  786. /**
  787. * Retrieves cities listing using GeoNames Service
  788. * @param int $cid The id of the country to retrieve cities for
  789. * Returns a JSON response
  790. */
  791. function updateCities($cid = 0)
  792. {
  793. $this->template = "";
  794. $this->auto_render = FALSE;
  795. $cities = 0;
  796. // Get country ISO code from DB
  797. $country = ORM::factory('country', (int)$cid);
  798. if ($country->loaded==true)
  799. {
  800. $iso = $country->iso;
  801. // GeoNames WebService URL + Country ISO Code
  802. $geonames_url = "http://ws.geonames.org/search?country="
  803. .$iso."&featureCode=PPL&featureCode=PPLA&featureCode=PPLC&maxRows=1000";
  804. // Grabbing GeoNames requires cURL so we will check for that here.
  805. if (!function_exists('curl_exec'))
  806. {
  807. throw new Kohana_Exception('settings.updateCities.cURL_not_installed');
  808. return false;
  809. }
  810. // Use Curl
  811. $ch = curl_init();
  812. $timeout = 20;
  813. curl_setopt ($ch, CURLOPT_URL, $geonames_url);
  814. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  815. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  816. $xmlstr = curl_exec($ch);
  817. $err = curl_errno( $ch );
  818. curl_close($ch);
  819. // $xmlstr = file_get_contents($geonames_url);
  820. // No Timeout Error, so proceed
  821. if ($err == 0) {
  822. // Reset All Countries City Counts to Zero
  823. $countries = ORM::factory('country')->find_all();
  824. foreach ($countries as $country)
  825. {
  826. $country->cities = 0;
  827. $country->save();
  828. }
  829. // Delete currently loaded cities
  830. ORM::factory('city')->delete_all();
  831. $sitemap = new SimpleXMLElement($xmlstr);
  832. foreach($sitemap as $city)
  833. {
  834. if ($city->name && $city->lng && $city->lat)
  835. {
  836. $newcity = new City_Model();
  837. $newcity->country_id = $cid;
  838. $newcity->city = mysql_real_escape_string($city->name);
  839. $newcity->city_lat = mysql_real_escape_string($city->lat);
  840. $newcity->city_lon = mysql_real_escape_string($city->lng);
  841. $newcity->save();
  842. $cities++;
  843. }
  844. }
  845. // Update Country With City Count
  846. $country = ORM::factory('country', $cid);
  847. $country->cities = $cities;
  848. $country->save();
  849. echo json_encode(array("status"=>"success", "response"=>"$cities ".Kohana::lang('ui_admin.cities_loaded')));
  850. }
  851. else {
  852. echo json_encode(array("status"=>"error", "response"=>"0 ".Kohana::lang('ui_admin.cities_loaded').". ".Kohana::lang('ui_admin.geonames_timeout')));
  853. }
  854. }
  855. else
  856. {
  857. echo json_encode(array("status"=>"error", "response"=>"0 ".Kohana::lang('ui_admin.cities_loaded').". ".Kohana::lang('ui_admin.country_not_found')));
  858. }
  859. }
  860. /**
  861. * adds the email settings to the application/config/email.php file
  862. */
  863. private function _add_email_settings( $settings )
  864. {
  865. $email_file = @file('application/config/email.template.php');
  866. $handle = @fopen('application/config/email.php', 'w');
  867. if(is_array($email_file) ) {
  868. foreach( $email_file as $number_line => $line )
  869. {
  870. switch( $line ) {
  871. case strpos($line,"\$config['username']"):
  872. fwrite($handle, str_replace("\$config['username'] = \"\"","\$config['username'] = ".'"'.$settings->email_username.'"',$line ));
  873. break;
  874. case strpos($line,"\$config['password']"):
  875. fwrite($handle, str_replace("\$config['password'] = \"\"","\$config['password'] = ".'"'.$settings->email_password.'"',$line ));
  876. break;
  877. case strpos($line,"\$config['port']"):
  878. fwrite($handle, str_replace("\$config['port'] = 25","\$config['port'] = ".'"'.$settings->email_port.'"',$line ));
  879. break;
  880. case strpos($line,"\$config['server']"):
  881. fwrite($handle, str_replace("\$config['server'] = \"\"","\$config['server'] = ".'"'.$settings->email_host.'"',$line ));
  882. break;
  883. case strpos($line,"\$config['servertype']"):
  884. fwrite($handle, str_replace("\$config['servertype'] = \"pop3\"","\$config['servertype'] = ".'"'.$settings->email_servertype.'"',$line ));
  885. break;
  886. case strpos($line,"\$config['ssl']"):
  887. $enable = $settings->email_ssl == 0? 'false':'true';
  888. fwrite($handle, str_replace("\$config['ssl'] = false","\$config['ssl'] = ".$enable,$line ));
  889. break;
  890. default:
  891. fwrite($handle, $line );
  892. }
  893. }
  894. }
  895. }
  896. /**
  897. * Check if clean url can be enabled on the server so
  898. * Ushahidi can cough it.
  899. *
  900. * @return boolean
  901. */
  902. private function _check_for_clean_url() {
  903. $url = url::base()."help";
  904. $curl_handle = curl_init();
  905. curl_setopt($curl_handle, CURLOPT_URL, $url);
  906. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true );
  907. curl_exec($curl_handle);
  908. $return_code = curl_getinfo($curl_handle,CURLINFO_HTTP_CODE);
  909. curl_close($curl_handle);
  910. return ($return_code == 404)? FALSE : TRUE;
  911. }
  912. /**
  913. * Removes / Adds index.php from / to index page variable in application/config.config.php file
  914. *
  915. * @param $yes_or_no
  916. */
  917. private function _configure_index_page( $yes_or_no ) {
  918. $config_file = @file('application/config/config.php');
  919. $handle = @fopen('application/config/config.php', 'w');
  920. if(is_array($config_file) )
  921. {
  922. foreach ($config_file as $line_number => $line)
  923. {
  924. if ($yes_or_no == 1)
  925. {
  926. if( strpos(" ".$line,"\$config['index_page'] = 'index.php';") != 0 )
  927. {
  928. fwrite($handle, str_replace("index.php","",$line ));
  929. // Set the 'index_page' property in the configuration
  930. Kohana::config_set('core.index_page', '');
  931. }
  932. else
  933. {
  934. fwrite($handle, $line);
  935. }
  936. }
  937. else
  938. {
  939. if( strpos(" ".$line,"\$config['index_page'] = '';") != 0 )
  940. {
  941. fwrite($handle, str_replace("''","'index.php'",$line ));
  942. // Set the 'index_page' property in the configuration
  943. Kohana::config_set('core.index_page', 'index.php');
  944. }
  945. else
  946. {
  947. fwrite($handle, $line);
  948. }
  949. }
  950. }
  951. }
  952. }
  953. /**
  954. * Check if clean URL is enabled on Ushahidi
  955. */
  956. private function _check_clean_url_on_ushahidi() {
  957. $config_file = @file_get_contents('application/config/config.php');
  958. return (strpos( $config_file,"\$config['index_page'] = 'index.php';") != 0 )
  959. ? FALSE
  960. : TRUE;
  961. }
  962. private function _generate_settings_map_js()
  963. {
  964. $map_layers = array();
  965. $layers = map::base();
  966. foreach ($layers as $layer)
  967. {
  968. $map_layers[$layer->name] = array();
  969. $map_layers[$layer->name]['title'] = $layer->title;
  970. $map_layers[$layer->name]['openlayers'] = $layer->openlayers;
  971. if (isset($layer->api_signup))
  972. {
  973. $map_layers[$layer->name]['api_signup'] = $layer->api_signup;
  974. }
  975. else
  976. {
  977. $map_layers[$layer->name]['api_signup'] = "";
  978. }
  979. }
  980. return json_encode($map_layers);
  981. }
  982. /**
  983. * Check if SSL is currently enabled on the instance
  984. */
  985. private function _is_https_enabled()
  986. {
  987. $config_file = @file_get_contents('application/config/config.php');
  988. return (strpos( $config_file,"\$config['site_protocol'] = 'http';") != 0 )
  989. ? FALSE
  990. : TRUE;
  991. }
  992. /**
  993. * Check if the Webserver is HTTPS capable
  994. */
  995. private function _is_https_capable()
  996. {
  997. // Get the current site protocol
  998. $protocol = Kohana::config('core.site_protocol');
  999. // Build an SSL URL
  1000. $url = ($protocol == 'https')? url::base() : str_replace('http://', 'https://', url::base());
  1001. $url .= 'index.php';
  1002. // Initialize cURL
  1003. $ch = curl_init();
  1004. // Set cURL options
  1005. curl_setopt($ch, CURLOPT_URL, $url);
  1006. // Disable following any "Location:" sent as part of the HTTP header
  1007. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  1008. // Return the output of curl_exec() as a string instead of outputting it directly
  1009. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  1010. // Suppress header information from the output
  1011. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  1012. // Perform cURL session
  1013. curl_exec($ch);
  1014. // Get the cURL error number
  1015. $error_no = curl_errno($ch);
  1016. // Get the return code
  1017. $http_return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  1018. // Close the cURL handle
  1019. curl_close($ch);
  1020. // Check if the cURL session succeeded
  1021. return (($error_no > 0 AND $error_no != 60) OR $http_return_code == 404)
  1022. ? FALSE
  1023. : TRUE;
  1024. }
  1025. /**
  1026. * Configures the HTTPS mode for the Ushahidi instance
  1027. *
  1028. * @param int $yes_or_no
  1029. */
  1030. private function _configure_https_mode($yes_or_no)
  1031. {
  1032. $config_file = @file('application/config/config.php');
  1033. $handle = @fopen('application/config/config.php', 'w');
  1034. if(is_array($config_file) AND $handle)
  1035. {
  1036. foreach ($config_file as $line_number => $line)
  1037. {
  1038. if ($yes_or_no == 1)
  1039. {
  1040. if( strpos(" ".$line,"\$config['site_protocol'] = 'http';") != 0 )
  1041. {
  1042. fwrite($handle, str_replace("http", "https", $line ));
  1043. // Enable HTTPS on the config
  1044. Kohana::config_set('core.site_protocol', 'https');
  1045. }
  1046. else
  1047. {
  1048. fwrite($handle, $line);
  1049. }
  1050. }
  1051. else
  1052. {
  1053. if( strpos(" ".$line,"\$config['site_protocol'] = 'https';") != 0 )
  1054. {
  1055. fwrite($handle, str_replace("https", "http", $line ));
  1056. // Enable HTTP on the config
  1057. Kohana::config_set('core.site_protocol', 'http');
  1058. }
  1059. else
  1060. {
  1061. fwrite($handle, $line);
  1062. }
  1063. }
  1064. }
  1065. }
  1066. }
  1067. }