/** * Comando de consola para crear un modelo * * @param array $params parametros nombrados de la consola * @param string $model modelo * @throw KumbiaException */ public function create($params, $model) { // nombre de archivo $file = APP_PATH . 'models'; // obtiene el path $path = explode('/', trim($model, '/')); // obtiene el nombre de modelo $model_name = array_pop($path); if (count($path)) { $dir = implode('/', $path); $file .= "/{$dir}"; if (!is_dir($file) && !FileUtil::mkdir($file)) { throw new KumbiaException("No se ha logrado crear el directorio \"{$file}\""); } } $file .= "/{$model_name}.php"; // si no existe o se sobreescribe if (!is_file($file) || Console::input("El modelo existe, �desea sobrescribirlo? (s/n): ", array('s', 'n')) == 's') { // nombre de clase $class = Util::camelcase($model_name); // codigo de modelo ob_start(); include CORE_PATH . 'console/generators/model.php'; $code = '<?php' . PHP_EOL . ob_get_clean(); // genera el archivo if (file_put_contents($file, $code)) { echo "-> Creado modelo {$model_name} en: {$file}" . PHP_EOL; } else { throw new KumbiaException("No se ha logrado crear el archivo \"{$file}\""); } } }
/** * Comando de consola para crear un controlador * * @param array $params parametros nombrados de la consola * @param string $controller controlador * @throw KumbiaException */ public function create($params, $controller) { // nombre de archivo $file = APP_PATH . 'controllers'; // limpia el path de controller $clean_path = trim($controller, '/'); // obtiene el path $path = explode('/', $clean_path); // obtiene el nombre de controlador $controller_name = array_pop($path); // si se agrupa el controlador en un directorio if (count($path)) { $dir = implode('/', $path); $file .= "/{$dir}"; if (!is_dir($file) && !FileUtil::mkdir($file)) { throw new KumbiaException("No se ha logrado crear el directorio \"{$file}\""); } } $file .= "/{$controller_name}_controller.php"; // si no existe o se sobreescribe if (!is_file($file) || Console::input("El controlador existe, ¿desea sobrescribirlo? (s/n): ", array('s', 'n')) == 's') { // nombre de clase $class = Util::camelcase($controller_name); // codigo de controlador ob_start(); include __DIR__ . '/generators/controller.php'; $code = '<?php' . PHP_EOL . ob_get_clean(); // genera el archivo if (file_put_contents($file, $code)) { echo "-> Creado controlador {$controller_name} en: {$file}" . PHP_EOL; } else { throw new KumbiaException("No se ha logrado crear el archivo \"{$file}\""); } // directorio para vistas $views_dir = APP_PATH . "views/{$clean_path}"; //si el directorio no existe if (!is_dir($views_dir)) { if (FileUtil::mkdir($views_dir)) { echo "-> Creado directorio para vistas: {$views_dir}" . PHP_EOL; } else { throw new KumbiaException("No se ha logrado crear el directorio \"{$views_dir}\""); } } } }
/** * 新增专题 */ public function formAction() { //定义js匿名函数 $execJs = function ($msg) { $js = "<script type='text/javascript'>\r\n //禁止回退\r\n window.history.forward(1);\r\n alert('{$msg}');\r\n location.href = history.go(-1);\r\n </script>"; die($js); }; $validate = new \Validate(); $data['file_cat_id'] = $validate->getPost('file_cat_id', \Validate::int()); //专题类型 //验证参数 if ($validate->getMessage()) { $execJs('参数错误'); } //验证文件 if (!isset($_FILES['doc'])) { $execJs('非法操作'); } //验证zip压缩文件 $doc = new File($_FILES['doc']); if ($doc->getExtension() != 'doc' && $doc->getExtension() != 'docx' && $doc->getExtension() != 'ppt' && $doc->getExtension() != 'pptx') { $execJs('请上传doc、docx、ppt、pptx类型的文件'); } $file_name = $doc->getName(); //创建目录 $file_path = FILE_PATH . '/file/' . $data['file_cat_id']; $util = new \FileUtil(); $util->mkdir($file_path); $data['file_url'] = '/file/' . $data['file_cat_id'] . '/' . md5($file_name . time()) . '.' . $doc->getExtension(); $file_url = FILE_PATH . $data['file_url']; $data['file_name'] = $file_name; //移动资源文件,并重命名,与phtml文件相同 $doc->moveTo($file_url); $result = (new \File())->addFile($this->session->get('id'), $data); if ($result != 200) { //删除所有专题相关文件 $this->unlink($file_url); $execJs('上传失败'); } $execJs('专题添加成功'); }
/** * 新增专题 */ public function formAction() { //定义js匿名函数 $execJs = function ($msg) { $js = "<script type='text/javascript'>\r\n //禁止回退\r\n window.history.forward(1);\r\n alert('{$msg}');\r\n location.href = history.go(-1);\r\n </script>"; die($js); }; //定义随机数匿名函数 $rand = function ($len) { $base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $rand_str = ''; for ($i = 0; $i < $len; $i++) { $rand_str .= $base[mt_rand(0, 61)]; } return $rand_str; }; $validate = new \Validate(); $data['top_type'] = $validate->getPost('top_type', \Validate::int()); //专题类型 $data['top_name'] = $validate->getPost('top_name', ['length' => ['max' => 30, 'min' => 0]], ['slashes' => true, 'html' => true]); //专题标题 $data['top_summary'] = $validate->getPost('top_summary', ['length' => ['max' => 200, 'min' => 0]], ['slashes' => true, 'html' => true]); //专题备注 $data['top_remark'] = $validate->getPost('top_remark', ['length' => ['max' => 50, 'min' => 0]], ['slashes' => true, 'html' => true]); //专题备注 //验证参数 if ($validate->getMessage()) { $execJs('参数错误'); } //验证文件 if (!isset($_FILES['zip']) || !isset($_FILES['top_thumb'])) { $execJs('非法操作'); } //验证缩略图 $thumb = new File($_FILES['top_thumb']); $extensions = ['png', 'jpg', 'gif']; if (!in_array($thumb->getExtension(), $extensions)) { $execJs('请上传png、jpg、gif类型图片'); } //声明图片地址 $pic_name = md5(microtime(true)) . '.' . $thumb->getExtension(); $pic_add = $this->mkdir('topic', $pic_name); $data['top_thumb'] = $pic_add['data_file']; //移动图片 $thumb->moveTo($pic_add['host_file']); //验证zip压缩文件 $zip = new File($_FILES['zip']); if ($zip->getExtension() != 'zip') { $execJs('请上传zip压缩文件'); } //解压缩 $archive = new \ZipArchive(); if (!$archive->open($zip->getTempName())) { $execJs('zip压缩文件已损坏'); } //12位随机数专题序列号,并检查是否重复 $data['top_unique'] = ''; while (1) { $data['top_unique'] = $rand(12); if ((new \Topic())->isTopicExist($data['top_unique']) == 404) { break; } } //创建临时解压目录 $zip_path = TEMP_PATH . '/tmp' . $data['top_unique']; $util = new \FileUtil(); $util->mkdir($zip_path); //解压到临时目录 if ($archive->extractTo($zip_path)) { //判断上传文件结构 if (!is_file($zip_path . '/index.html') || !is_dir($zip_path . '/index')) { $this->unlink($pic_add['host_file']); $util->rm($zip_path); $execJs('zip压缩文件内容与要求不匹配'); } //定义html,以及phtml文件路径 $html_file = $zip_path . '/index.html'; $phtml_file = TOPIC_HTML_PATH . '/' . $data['top_unique'] . '.phtml'; //分别以只读,和只写方式打开html,phtml文件 $html_handle = fopen($html_file, 'r'); $phtml_handle = fopen($phtml_file, 'w'); //读取html文件,写入phtml while (!feof($html_handle)) { $code = fgets($html_handle); //逐行读取html $code = preg_replace('/([src|href]=[\'\\"])(index)/', '$1/extract/' . $data['top_unique'], $code); //替换链接 fwrite($phtml_handle, $code); //写入phtml } //关闭指针 fclose($html_handle); fclose($phtml_handle); //移动资源文件,并重命名,与phtml文件相同 $util->mv($zip_path . '/index', TOPIC_ROOT_PATH . '/' . $data['top_unique'], true); } //删除临时文件夹 $util->rm($zip_path); //关闭zip指针 $archive->close(); $result = (new \Topic())->addTopic($this->session->get('id'), $data); if ($result != 200) { //删除所有专题相关文件 $this->unlink($pic_add['host_file']); $this->unlink(TOPIC_HTML_PATH . '/' . $data['top_unique'] . '.phtml'); $util->rm(TOPIC_ROOT_PATH . '/' . $data['top_unique']); $execJs('专题添加失败'); } $execJs('专题添加成功'); }
/** * Crea un path. * @deprecated * @todo Mover este método a una lib para manejo de ficheros. * En salir la beta2 se eliminará del Util * * @param string $path ruta a crear * @return boolean */ public static function mkpath($path) { return FileUtil::mkdir($path); }