public function pjActionCreate()
 {
     $this->setLayout('pjAdminSignUpLayout');
     if (isset($_POST['user_singup'])) {
         $data = array();
         $data['is_active'] = 'T';
         $data['ip'] = $_SERVER['REMOTE_ADDR'];
         $id = pjUserModel::factory(array_merge($_POST, $data))->insert()->getInsertId();
         if ($id !== false && (int) $id > 0) {
             $err = 'AU03';
         } else {
             $err = 'AU04';
         }
         //Let's check when or not coming from clover.
         if (empty($_SESSION['cloverData'])) {
             pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionLogin");
         } else {
             $_SESSION['user_id'] = $id;
             pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminSignUp&action=pjActionMain&case=signup");
         }
     } else {
         $this->set('role_arr', pjRoleModel::factory()->orderBy('t1.id ASC')->findAll()->getData());
         $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
         $this->appendJs('pjAdminUsers.js');
     }
 }
 public function pjActionUpdate()
 {
     $this->checkLogin();
     if ($this->isAdmin()) {
         if (isset($_POST['user_update'])) {
             pjUserModel::factory()->where('id', $_POST['id'])->limit(1)->modifyAll($_POST);
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminUsers&action=pjActionIndex&err=AU01");
         } else {
             $arr = pjUserModel::factory()->find($_GET['id'])->getData();
             if (count($arr) === 0) {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminUsers&action=pjActionIndex&err=AU08");
             }
             $this->set('arr', $arr);
             $this->set('role_arr', pjRoleModel::factory()->orderBy('t1.id ASC')->findAll()->getData());
             $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
             $this->appendJs('pjAdminUsers.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function getAdminPhone()
 {
     $arr = pjUserModel::factory()->findAll()->orderBy("t1.id ASC")->limit(1)->getData();
     return !empty($arr) ? !empty($arr[0]['phone']) ? $arr[0]['phone'] : null : null;
 }
 public function pjActionProfile()
 {
     $this->checkLogin();
     if (!$this->isAdmin()) {
         if (isset($_POST['profile_update'])) {
             $pjUserModel = pjUserModel::factory();
             $arr = $pjUserModel->find($this->getUserId())->getData();
             $data = array();
             $data['role_id'] = $arr['role_id'];
             $data['status'] = $arr['status'];
             $post = array_merge($_POST, $data);
             if (!$pjUserModel->validates($post)) {
                 pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionProfile&err=AA14");
             }
             $pjUserModel->set('id', $this->getUserId())->modify($post);
             pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionProfile&err=AA13");
         } else {
             $this->set('arr', pjUserModel::factory()->find($this->getUserId())->getData());
             $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
             $this->appendJs('pjAdmin.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionSetDb()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         if (!self::pjActionCheckVars()) {
             pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 108, 'text' => 'Missing, empty or invalid parameters.'));
         }
         @set_time_limit(300);
         //5 minutes
         $resp = array();
         $driver = function_exists('mysqli_connect') ? 'pjMysqliDriver' : 'pjMysqlDriver';
         $params = array('hostname' => $_SESSION[$this->defaultInstaller]['hostname'], 'username' => $_SESSION[$this->defaultInstaller]['username'], 'password' => $_SESSION[$this->defaultInstaller]['password'], 'database' => $_SESSION[$this->defaultInstaller]['database']);
         if (strpos($params['hostname'], ":") !== FALSE) {
             list($hostname, $value) = explode(":", $params['hostname'], 2);
             if (preg_match('/\\D/', $value)) {
                 $params['socket'] = $value;
             } else {
                 $params['port'] = $value;
             }
             $params['hostname'] = $hostname;
         }
         $dbo = pjSingleton::getInstance($driver, $params);
         if (!$dbo->init()) {
             $err = $dbo->connectError();
             if (!empty($err)) {
                 $resp['code'] = 100;
                 $resp['text'] = 'Could not connect: ' . $err;
                 self::pjActionDbError($resp);
             } else {
                 $resp['code'] = 101;
                 $resp['text'] = $dbo->error();
                 self::pjActionDbError($resp);
             }
         } else {
             $idb = self::pjActionImportSQL($dbo, 'app/config/database.sql', $_SESSION[$this->defaultInstaller]['prefix']);
             if ($idb === true) {
                 $_GET['install'] = 2;
                 require 'app/config/options.inc.php';
                 $result = $this->requestAction(array('controller' => 'pjAppController', 'action' => 'pjActionBeforeInstall'), array('return'));
                 if ($result !== NULL && isset($result['code']) && $result['code'] != 200 && isset($result['info'])) {
                     $resp['text'] = join("<br>", $result['info']);
                     $resp['code'] = 104;
                     self::pjActionDbError($resp);
                 }
                 if (isset($CONFIG['plugins'])) {
                     if (!is_array($CONFIG['plugins'])) {
                         $CONFIG['plugins'] = array($CONFIG['plugins']);
                     }
                     foreach ($CONFIG['plugins'] as $plugin) {
                         $file = PJ_PLUGINS_PATH . $plugin . '/config/database.sql';
                         if (is_file($file)) {
                             $response = self::pjActionExecuteSQL($dbo, $file, $_SESSION[$this->defaultInstaller]['prefix'], PJ_SCRIPT_PREFIX);
                             if ($response['status'] == "ERR") {
                                 self::pjActionDbError($response);
                             }
                             $update_folder = PJ_PLUGINS_PATH . $plugin . '/config/updates';
                             if (is_dir($update_folder)) {
                                 $files = array();
                                 pjToolkit::readDir($files, $update_folder);
                                 foreach ($files as $path) {
                                     if (preg_match('/\\.sql$/', basename($path)) && is_file($path)) {
                                         $response = self::pjActionExecuteSQL($dbo, $path, $_SESSION[$this->defaultInstaller]['prefix'], PJ_SCRIPT_PREFIX);
                                         if ($response['status'] == "ERR") {
                                             self::pjActionDbError($response);
                                         }
                                     }
                                 }
                             }
                         }
                         $modelName = pjObject::getConstant($plugin, 'PLUGIN_MODEL');
                         if (class_exists($modelName) && method_exists($modelName, 'pjActionSetup')) {
                             $pluginModel = new $modelName();
                             $pluginModel->begin();
                             $pluginModel->pjActionSetup();
                             $pluginModel->commit();
                         }
                         $result = $this->requestAction(array('controller' => $plugin, 'action' => 'pjActionBeforeInstall'), array('return'));
                         if ($result !== NULL && isset($result['code']) && $result['code'] != 200 && isset($result['info'])) {
                             $resp['text'] = join("<br>", $result['info']);
                             $resp['code'] = 104;
                             self::pjActionDbError($resp);
                         }
                     }
                 }
                 $updates = self::pjActionGetUpdates();
                 foreach ($updates as $record) {
                     $file_path = $record['path'];
                     $response = self::pjActionExecuteSQL($dbo, $file_path, $_SESSION[$this->defaultInstaller]['prefix'], PJ_SCRIPT_PREFIX);
                     if ($response['status'] == "ERR") {
                         self::pjActionDbError($response);
                     }
                 }
                 $result = $this->requestAction(array('controller' => 'pjAppController', 'action' => 'pjActionAfterInstall'), array('return'));
                 if ($result !== NULL && isset($result['code']) && $result['code'] != 200 && isset($result['info'])) {
                     $resp['text'] = join("<br>", $result['info']);
                     $resp['code'] = 105;
                     self::pjActionDbError($resp);
                 }
                 pjUserModel::factory()->setPrefix($_SESSION[$this->defaultInstaller]['prefix'])->setAttributes(array('email' => $_SESSION[$this->defaultInstaller]['admin_email'], 'password' => $_SESSION[$this->defaultInstaller]['admin_password'], 'role_id' => 1, 'name' => "Administrator", 'ip' => $_SERVER['REMOTE_ADDR']))->insert();
                 pjOptionModel::factory()->setPrefix($_SESSION[$this->defaultInstaller]['prefix'])->setAttributes(array('foreign_id' => $this->getForeignId(), 'key' => 'private_key', 'tab_id' => 99, 'value' => $_SESSION[$this->defaultInstaller]['private_key'], 'type' => 'string'))->insert();
                 if (!isset($resp['code'])) {
                     $resp['code'] = 200;
                 }
             } elseif ($idb === false) {
                 $resp['code'] = 102;
                 //File not found (can't be open/read)
                 $resp['text'] = "File not found (or can't be read)";
                 self::pjActionDbError($resp);
             } else {
                 $resp['code'] = 103;
                 //MySQL error
                 $resp['text'] = $idb;
                 self::pjActionDbError($resp);
             }
         }
         if (isset($resp['code']) && $resp['code'] != 200) {
             self::pjActionDbError($resp);
         }
         pjAppController::jsonResponse($resp);
     }
     exit;
 }