<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Core_AdminIndexController extends Core_Controller_Action_Admin {

  public function indexAction() {

		Engine_Api::_()->getApi('settings', 'core')->setSetting('core.general.site.url', _ENGINE_SITE_URL);
		
    //Feed Default installation
    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('activity.pluginactivated')) {
      include_once APPLICATION_PATH . "/application/modules/Activity/controllers/defaultsettings.php";
      Engine_Api::_()->getApi('settings', 'core')->setSetting('activity.pluginactivated', 1);
    }

		//Avatar Default installation
		if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('user.avatar.imported')) {
    	$this->uploadAvatars();
		}

    $table = Engine_Api::_()->getDbtable('users', 'user');
    $select = $table->select()
                  ->from($table->info('name'))
                  ->where('import =?', 0);
    $this->view->paginator = Zend_Paginator::factory($select);
  }

	public function licenseKeyAction() {

		$settings = Engine_Api::_()->getApi('settings', 'core');
		$coreApi  = Engine_Api::_()->core();	

		$form = $this->view->form = new Core_Form_Admin_Index_LicenseKey();

		$domain_name = @base64_encode(str_replace(array('http://','https://','www.'),array('','',''),$_SERVER['HTTP_HOST']));
		
		if (engine_in_array($_SERVER['SERVER_NAME'], ['localhost', '127.0.0.1'])) {
			$form->domain_name->setValue($settings->getSetting('core.license.domain'));
		}
		

		if( !$this->getRequest()->isPost() ) {
      return;
    }

    if( !$form->isValid($this->getRequest()->getPost()) ) {
      return;
    }

		// Verify license
    $values = $form->getValues();

    try {
      $valid = Engine_Api::_()->getApi('verify', 'core')->verifyKey($values['key'], @$values['domain_name']);
    } catch( Exception $e ) {
      $valid = false;
      $error = $e->getMessage();
    }
		if( $valid['status'] != 'Active') {
      $form->addError("License key is not valid. Please enter a valid license key.");
      return;
    }

		$db = Engine_Api::_()->getDbtable('settings', 'core')->getAdapter();
		$db->beginTransaction();
		try {

			$settings->setSetting('core.license.key', $values['key']);

			$domain_name = @base64_encode(str_replace(array('http://','https://','www.'),array('','',''),$_SERVER['HTTP_HOST']));
			$licensekey = @base64_encode($values['key']);
			if (engine_in_array($_SERVER['SERVER_NAME'], ['localhost', '127.0.0.1'])) {
				$settings->setSetting('core.license.domain', $values['domain_name']);
				$settings->setSetting('core.license.domainauth', @base64_encode($values['domain_name']));
			} else {
				$settings->setSetting('core.license.domain', $domain_name);
				$settings->setSetting('core.license.domainauth', $domain_name);
			}

			// Update cache
			$cachedState = isset($valid['status']) ? $valid['status'] : 'Inactive';
			$settings->setSetting('core.license.cachedstatus', $cachedState);
			$settings->setSetting('core.license.lastcheck', time());
			
			$settings->setSetting('core.license.keyauth', $licensekey);
			$db->commit();
			return $this->_forward('success' ,'utility', 'core', array(
				'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'core', 'controller' => 'index', 'action' => 'index'), 'admin_default', true),
				'messages' => Array('License key updated successfully.')
			));
		} catch (Exception $e) {
			$db->rollback();
			$form->getElement('key')->addError('There was a problem saving the new license key; please try again.');
			return;
		}
	}

  public function changeEnvironmentModeAction()
  {
    if ($this->getRequest()->isPost() && $this->_getParam('environment_mode', false)) {
      $environmentMode = $this->_getParam('environment_mode', false);
      
      $global_settings_file = APPLICATION_PATH . '/application/settings/general.php';
      if (file_exists($global_settings_file)) {
          $g = include $global_settings_file;
          if (!is_array($g)) {
              $g = (array) $g;
          }
      } else {
          $g = array();
      }

      if (!is_writable($global_settings_file)) {
          // not writable; can we delete and re-create?
          if (is_writable(dirname($global_settings_file))) {
              @rename($global_settings_file, $global_settings_file.'_backup.php');
              @touch($global_settings_file);
              @chmod($global_settings_file, 0666);
              if (!file_exists($global_settings_file) || !is_writable($global_settings_file)) {
                  @rename($global_settings_file, $global_settings_file.'_delete.php');
                  @rename($global_settings_file.'_backup.php', $global_settings_file);
                  @unlink($global_settings_file.'_delete.php');
              }
          }
          if (!is_writable($global_settings_file)) {
              $this->view->success = false;
              $this->view->error   = 'Unable to write to settings file; please CHMOD 666 the file /application/settings/general.php, then try again.';
              return;
          } else {
              // it worked; continue.
          }
      }
      if($environmentMode == "development"){
        // create js and css file
        Engine_Api::_()->core()->generateJsCss();
      }
      if ($this->_getParam('environment_mode') != @$g['environment_mode']) {
          $g['environment_mode'] = $this->_getParam('environment_mode');
          $file_contents  = "<?php defined('_ENGINE') or die('Access Denied'); return ";
          $file_contents .= var_export($g, true);
          $file_contents .= "; ?>";
          $this->view->success = @file_put_contents($global_settings_file, $file_contents);

          // clear scaffold cache
          Core_Model_DbTable_Themes::clearScaffoldCache();

          // Increment site counter
          $settings = Engine_Api::_()->getApi('settings', 'core');
          $settings->core_site_counter = $settings->core_site_counter + 1;

          return;
      } else {
          $this->view->message = 'No change necessary';
          $this->view->success = true; // no change
      }

      

    }
    $this->view->success = false;
  }
  
  public function flushPhotoAction() {
  
    $this->view->form = $form = new Core_Form_Admin_FlushPhotos();
    
    $this->_helper->layout->setLayout('admin-simple');
    
    if( !$this->getRequest()->isPost())
      return;

    try {
      $flushData = Engine_Api::_()->getDbTable('files', 'storage')->getFlushPhotoData();
      foreach($flushData as $item) {
        Engine_Api::_()->storage()->deleteExternalsFiles($item->file_id);
        $item->delete();
      }
    } catch(Exception $e) {
      //throw $e;
    }
    $this->view->message = Zend_Registry::get('Zend_Translate')->_("Unmapped photos remove successfully.");
    return 
    $this->_forward('success' ,'utility', 'core', array(
      'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'core', 'controller' => 'index', 'action' => 'index'), 'admin_default', true),
      'messages' => Array($this->view->message)
    ));
  }

  public function notesAction() {
    if( !Engine_Api::_()->getApi('settings', 'core')->getSetting('coreadmin.notes')) {
      $notes = Engine_Api::_()->getApi('settings', 'core')->setSetting('coreadmin.notes', $_POST['coreadmin_notes']);
    } else {
      $notes = Engine_Api::_()->getApi('settings', 'core')->setSetting('coreadmin.notes', $_POST['coreadmin_notes']);
    }
    echo json_encode(array('status' => true));die;
  }

  public function userDataImportAction() {
  
    $this->view->form = $form = new Core_Form_Admin_UserDataImport();
    
    $this->_helper->layout->setLayout('admin-simple');
    
    if( !$this->getRequest()->isPost())
      return;

    try {
    
      $userApi = Engine_Api::_()->user();
      $usersTable = Engine_Api::_()->getDbTable('users', 'user');

      $results = $usersTable->fetchAll($usersTable->select()->from($usersTable->info('name'))->where('import =?', 0)->limit('500'));
      foreach($results as $result) {
        $fieldsByAlias = Engine_Api::_()->fields()->getFieldsObjectsByAlias($result);

        if (!empty($fieldsByAlias['profile_type'])) {
          $optionId = $fieldsByAlias['profile_type']->getValue($result);
          
          //Save profile type value in users table
          $profile_type = $optionId->value;
          $result->profile_type = $profile_type;
          $result->save();
          
          if(!empty($profile_type)) {
            //Display Name
            $firstNameFieldId = $userApi->getFieldId($profile_type, array('first_name'));
            $lastNameFieldId = $userApi->getFieldId($profile_type, array('last_name'));
            $genderFieldId = $userApi->getFieldId($profile_type, array('gender'));
            $birthdateFieldId = $userApi->getFieldId($profile_type, array('birthdate'));
            
            $user_id = $result->getIdentity();
            $first_name = $userApi->getprofileFieldValue(array('user_id' => $user_id, 'field_id' => $firstNameFieldId));
            if(!empty($first_name)) {
              $result->firstname = $first_name;
              $result->save();
              
              Engine_Api::_()->fields()->getTable('user', 'values')->delete(array('item_id = ?' => $user_id, 'field_id = ?' => $firstNameFieldId));
            }
            
            $last_name = $userApi->getprofileFieldValue(array('user_id' => $user_id, 'field_id' => $lastNameFieldId));
            if(!empty($last_name)) {
              $result->lastname = $last_name;
              $result->save();
              
              Engine_Api::_()->fields()->getTable('user', 'values')->delete(array('item_id = ?' => $user_id, 'field_id = ?' => $lastNameFieldId));
            }
            
            $gender = $userApi->getprofileFieldValue(array('user_id' => $user_id, 'field_id' => $genderFieldId));
            if(!empty($gender)) {
              $getOptionValue = $userApi->getOptionIdValue(array('option_id' => $gender));
              $result->gender = strtolower($getOptionValue);
              $result->save();
              
              Engine_Api::_()->fields()->getTable('user', 'values')->delete(array('item_id = ?' => $user_id, 'field_id = ?' => $genderFieldId));
            }
            
            $birthdate = $userApi->getprofileFieldValue(array('user_id' => $user_id, 'field_id' => $birthdateFieldId));
            if(!empty($birthdate)) {
              $result->dob = $birthdate;
              $result->save();
              
              Engine_Api::_()->fields()->getTable('user', 'values')->delete(array('item_id = ?' => $user_id, 'field_id = ?' => $birthdateFieldId));
            }
          }
          $result->import = 1;
          $result->save();
        }
      }
      
      //When all field import done, then delete related profile field
      // $table = Engine_Api::_()->getDbtable('users', 'user');
      // $select = $table->select()
      //               ->from($table->info('name'))
      //               ->where('import =?', 0);
      // $userData = Zend_Paginator::factory($select);
      // if($userData->getTotalItemCount() == 0) {
      
      //   $metaTable = Engine_Api::_()->fields()->getTable('user', 'meta');
      //   $metaTableName = $metaTable->info('name');
        
      //   $select = $metaTable->select()
      //             ->from($metaTableName)
      //             ->where($metaTableName . '.type IN (?)', array('first_name', 'last_name', 'gender', 'birthdate'));
      //   $datas = $metaTable->fetchAll($select);
      //   foreach($datas as $data) {
      //     $field = Engine_Api::_()->fields()->getField($data->field_id, 'user');
      //     Engine_Api::_()->fields()->deleteField('user', $field);
      //   }
      // }
    } catch(Exception $e) {
      //throw $e;
    }
    $this->view->message = Zend_Registry::get('Zend_Translate')->_("User data imported successfully.");
    return $this->_forward('success' ,'utility', 'core', array(
      'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'core', 'controller' => 'index', 'action' => 'index'), 'admin_default', true),
      'messages' => Array($this->view->message)
    ));
  }

  public function uploadAvatars() {

    $PathFile = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'User' . DIRECTORY_SEPARATOR . "externals" . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR . "avatars" . DIRECTORY_SEPARATOR . "male" . DIRECTORY_SEPARATOR;

		$this->uploadDefaultAvatars($PathFile, "male");

		$PathFile = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'User' . DIRECTORY_SEPARATOR . "externals" . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR . "avatars" . DIRECTORY_SEPARATOR . "female" . DIRECTORY_SEPARATOR;

		$this->uploadDefaultAvatars($PathFile, "female");

		Engine_Api::_()->getApi('settings', 'core')->setSetting('user.avatar.imported', 1);
  }

	protected function uploadDefaultAvatars($PathFile, $gender = '') {

		$avatarsTable = Engine_Api::_()->getDbtable('avatars', 'user');
		$file_display = array('jpg', 'jpeg', 'png', 'gif');
    if (file_exists($PathFile)) {
      $dir_contents = scandir( $PathFile );
      foreach ( $dir_contents as $file ) {
        $f = explode('.', $file );
        $g = end($f);
        $file_type = strtolower($g);
        if ( ($file !== '.') && ($file !== '..') && (engine_in_array( $file_type, $file_display)) ) {
          $images = explode('.', $file);
          $db = Engine_Db_Table::getDefaultAdapter();
          $db->beginTransaction();
          // If we're here, we're done
          try {
            $item = $avatarsTable->createRow();
            $values['enabled'] = 1;
						$values['gender'] = $gender;
            $item->setFromArray($values);
            $item->save();
            $item->order = $item->avatar_id;
            $item->save();
            if(!empty($file)) {
              $file_ext = pathinfo($file);
              $file_ext = $file_ext['extension'];
              $storage = Engine_Api::_()->getItemTable('storage_file');
              $pngFile = $PathFile . $file;
              $storageObject = $storage->createFile($pngFile, array(
                'parent_id' => $item->getIdentity(),
                'parent_type' => $item->getType(),
                'user_id' => Engine_Api::_()->user()->getViewer()->getIdentity(),
              ));
              // Remove temporary file
              ////@unlink($file['tmp_name']);
              $item->file_id = $storageObject->file_id;
              $item->save();
            }
            $db->commit();
          } catch(Exception $e) {
            $db->rollBack();
            throw $e;
          }
        }
      }
    }
	}
}
