Example #1
0
 static function fork($phpScript, $phpExec = null)
 {
     $cwd = dirname($phpScript);
     @putenv("PHP_FORCECLI=true");
     if (!is_string($phpExec) || !file_exists($phpExec)) {
         if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
             $phpExec = str_replace('/', '\\', dirname(ini_get('extension_dir'))) . '\\php.exe';
             if (@file_exists($phpExec)) {
                 BackgroundProcess::open(escapeshellarg($phpExec) . " " . escapeshellarg($phpScript), $cwd);
             }
         } else {
             $phpExec = exec("which php-cli");
             if ($phpExec[0] != '/') {
                 $phpExec = exec("which php");
             }
             if ($phpExec[0] == '/') {
                 BackgroundProcess::open(escapeshellarg($phpExec) . " " . escapeshellarg($phpScript), $cwd);
             }
         }
     } else {
         if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
             $phpExec = str_replace('/', '\\', $phpExec);
         }
         BackgroundProcess::open(escapeshellarg($phpExec) . " " . escapeshellarg($phpScript), $cwd);
     }
 }
 /**
  * Tests running a background process.
  *
  * @covers Cocur\BackgroundProcess\BackgroundProcess::__construct()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::run()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::isRunning()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::getPid()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::stop()
  */
 public function testRun()
 {
     $process = new BackgroundProcess('sleep 5');
     $this->assertFalse($process->isRunning(), 'process should not run');
     $process->run();
     $this->assertNotNull($process->getPid(), 'process should have a pid');
     $this->assertTrue($process->isRunning(), 'process should run');
     $this->assertTrue($process->stop(), 'stop process');
     $this->assertFalse($process->isRunning(), 'processes should not run anymore');
     $this->assertFalse($process->stop(), 'cannot stop process that is not running');
 }
 public function run()
 {
     $faker = Faker::create();
     $imgDir = public_path() . DS . 'assets' . DS . 'upload' . DS . 'images';
     if (!File::exists($imgDir)) {
         File::makeDirectory($imgDir, 0755);
     }
     File::cleanDirectory($imgDir, true);
     foreach (range(1, 500) as $index) {
         $dir = $imgDir . DS . $index;
         if (!File::exists($dir)) {
             File::makeDirectory($dir, 0755);
         }
         $width = $faker->numberBetween(800, 1024);
         $height = $faker->numberBetween(800, 1024);
         $orgImg = $faker->image($dir, $width, $height);
         chmod($orgImg, 0755);
         $img = str_replace($dir . DS, '', $orgImg);
         $image = new Imagick($orgImg);
         $dpi = $image->getImageResolution();
         $dpi = $dpi['x'] > $dpi['y'] ? $dpi['x'] : $dpi['y'];
         $size = $image->getImageLength();
         $extension = strtolower($image->getImageFormat());
         //get 5 most used colors from the image
         $color_extractor = new ColorExtractor();
         $myfile = $orgImg;
         $mime_type = $image->getImageMimeType();
         switch ($mime_type) {
             case 'image/jpeg':
                 $palette_obj = $color_extractor->loadJpeg($myfile);
                 break;
             case 'image/png':
                 $palette_obj = $color_extractor->loadPng($myfile);
                 break;
             case 'image/gif':
                 $palette_obj = $color_extractor->loadGif($myfile);
                 break;
         }
         $main_color = '';
         if (is_object($palette_obj)) {
             $arr_palette = $palette_obj->extract(5);
             if (!empty($arr_palette)) {
                 $main_color = strtolower($arr_palette[0]);
                 for ($i = 1; $i < count($arr_palette); $i++) {
                     $main_color .= ',' . strtolower($arr_palette[$i]);
                 }
             }
         }
         //update field main_color from images table
         $image_obj = VIImage::findorFail((int) $index);
         $image_obj->main_color = $main_color;
         $image_obj->save();
         $id = VIImageDetail::insertGetId(['path' => 'assets/upload/images/' . $index . '/' . $img, 'height' => $height, 'width' => $width, 'ratio' => $width / $height, 'dpi' => $dpi, 'size' => $size, 'extension' => $extension, 'type' => 'main', 'image_id' => $index]);
         BackgroundProcess::makeSize($id);
     }
 }
 function handle($params)
 {
     @session_write_close();
     header('Connection: close');
     require_once 'inc/BackgroundProcess.php';
     $out = array('running' => BackgroundProcess::getRunningProcessInfo(), 'recent' => BackgroundProcess::getRecentProcessInfo());
     header('Content-Type: text/json; charset="' . Dataface_Application::getInstance()->_conf['oe'] . '"');
     echo json_encode($out);
     exit;
 }
 function handle($params)
 {
     set_time_limit(0);
     @session_write_close();
     header('Connection: close');
     $out = '//OK';
     header('Content-Type: text/javascript; charset="' . Dataface_Application::getInstance()->_conf['oe'] . '"');
     header('Content-Length: ' . strlen($out));
     echo $out;
     flush();
     require_once 'inc/BackgroundProcess.php';
     BackgroundProcess::runProcesses();
 }
Example #6
0
 public static function upload($file, $path, $width = 110, $makeThumb = true, $fileName = '')
 {
     if (!File::exists($path)) {
         File::makeDirectory($path, 493, true);
     }
     if (!empty($fileName)) {
         $fileName .= '.' . $file->getClientOriginalExtension();
     } else {
         $fileName = Str::slug(str_replace('.' . $file->getClientOriginalExtension(), '', $file->getClientOriginalName())) . '.' . date('d-m-y') . '.' . $file->getClientOriginalExtension();
     }
     $path = str_replace(['\\', '/'], DS, $path);
     if ($file->move($path, $fileName)) {
         BackgroundProcess::resize($width, $path, $fileName);
         if ($makeThumb) {
             BackgroundProcess::makeThumb($path, $fileName);
         }
         return $path . DS . $fileName;
     }
     return false;
 }
 public function updateImage()
 {
     if (!Request::ajax()) {
         return App::abort(404);
     }
     $arrReturn = ['status' => 'error'];
     $id = Input::has('id') ? Input::get('id') : 0;
     if ($id) {
         $create = false;
         try {
             $image = VIImage::findorFail((int) Input::get('id'));
         } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
             return App::abort(404);
         }
         $message = 'has been updated successful';
     } else {
         $create = true;
         $image = new VIImage();
         $message = 'has been created successful';
     }
     if ($create && !Input::hasFile('image')) {
         return ['status' => 'error', 'message' => 'You need to upload at least 1 image.'];
     }
     $image->name = Input::get('name');
     $image->short_name = Str::slug($image->name);
     $image->description = Input::get('description');
     $image->keywords = Input::get('keywords');
     $image->keywords = rtrim(trim($image->keywords), ',');
     $image->model = Input::get('model');
     $image->model = rtrim(trim($image->model), ',');
     $image->artist = Input::get('artist');
     $image->age_from = Input::get('age_from');
     $image->age_to = Input::get('age_to');
     $image->gender = Input::get('gender');
     $image->number_people = Input::get('number_people');
     $pass = $image->valid();
     if ($pass->passes()) {
         if (Input::hasFile('image')) {
             $color_extractor = new ColorExtractor();
             $myfile = Input::file('image');
             $mime_type = $myfile->getClientMimeType();
             switch ($mime_type) {
                 case 'image/jpeg':
                     $palette_obj = $color_extractor->loadJpeg($myfile);
                     break;
                 case 'image/png':
                     $palette_obj = $color_extractor->loadPng($myfile);
                     break;
                 case 'image/gif':
                     $palette_obj = $color_extractor->loadGif($myfile);
                     break;
             }
             $main_color = '';
             if (is_object($palette_obj)) {
                 $arr_palette = $palette_obj->extract(5);
                 if (!empty($arr_palette)) {
                     $main_color = strtolower($arr_palette[0]);
                     for ($i = 1; $i < count($arr_palette); $i++) {
                         $main_color .= ',' . strtolower($arr_palette[$i]);
                     }
                 }
             }
             $image->main_color = $main_color;
         }
         $image->save();
         //insert into statistic_images table
         if ($create) {
             StatisticImage::create(['image_id' => $image->id, 'view' => '0', 'download' => '0']);
         }
         foreach (['category_id' => 'categories', 'collection_id' => 'collections'] as $key => $value) {
             $arrOld = $remove = $add = [];
             $old = $image->{$value};
             $data = Input::has($key) ? Input::get($key) : [];
             $data = (array) json_decode($data[0]);
             if (!empty($old)) {
                 foreach ($old as $val) {
                     if (!in_array($val->id, $data)) {
                         $remove[] = $val->id;
                     } else {
                         $arrOld[] = $val->id;
                     }
                 }
             }
             foreach ($data as $id) {
                 if (!$id) {
                     continue;
                 }
                 if (!in_array($id, $arrOld)) {
                     $add[] = $id;
                 }
             }
             if (!empty($remove)) {
                 $image->{$value}()->detach($remove);
             }
             if (!empty($add)) {
                 $image->{$value}()->attach($add);
             }
             foreach ($add as $v) {
                 $arrOld[] = $v;
             }
             $image->{'arr' . ucfirst($value)} = $arrOld;
         }
         $path = public_path('assets' . DS . 'upload' . DS . 'images' . DS . $image->id);
         if ($create) {
             $main = new VIImageDetail();
             $main->image_id = $image->id;
             $main->type = 'main';
             File::makeDirectory($path, 0755, true);
         } else {
             $main = VIImageDetail::where('type', 'main')->where('image_id', $image->id)->first();
             File::delete(public_path($main->path));
         }
         if (Input::hasFile('image')) {
             $file = Input::file('image');
             $name = $image->short_name . '.' . $file->getClientOriginalExtension();
             $file->move($path, $name);
             $imageChange = true;
         } else {
             if (Input::has('choose_image') && Input::has('choose_name')) {
                 $chooseImage = Input::get('choose_image');
                 $name = Input::get('choose_name');
                 file_put_contents($path . DS . $name, file_get_contents($chooseImage));
                 $imageChange = true;
             }
         }
         if (isset($imageChange)) {
             $main->path = 'assets/upload/images/' . $image->id . '/' . $name;
             $img = Image::make($path . DS . $name);
             $main->width = $img->width();
             $main->height = $img->height();
             $main->ratio = $main->width / $main->height;
             $img = new Imagick($path . DS . $name);
             $dpi = $img->getImageResolution();
             $main->dpi = $dpi['x'] > $dpi['y'] ? $dpi['x'] : $dpi['y'];
             $main->size = $img->getImageLength();
             $main->extension = strtolower($img->getImageFormat());
             $main->save();
             BackgroundProcess::makeSize($main->detail_id);
             $image->changeImg = true;
             if ($create) {
                 $image->dimension = $main->width . 'x' . $main->height;
                 $image->newImg = true;
                 $image->path = URL . '/pic/large-thumb/' . $image->short_name . '-' . $image->id . '.jpg';
             }
         }
         $arrReturn['status'] = 'ok';
         $arrReturn['message'] = "{$image->name} {$message}.";
         $arrReturn['data'] = $image;
         return $arrReturn;
     }
     $arrReturn['message'] = '';
     $arrErr = $pass->messages()->all();
     foreach ($arrErr as $value) {
         $arrReturn['message'] .= "{$value}\n";
     }
     return $arrReturn;
 }
 /**
  * Poorman launcher background process callback.
  *
  * @param string $lock_id
  *   The lock id used for this process.
  */
 public static function poormanLauncher($lock_id)
 {
     $class = _ultimate_cron_get_class('lock');
     // Bail out if someone stole our lock.
     if (!$class::reLock($lock_id, 60)) {
         return;
     }
     ultimate_cron_poorman_page_flush();
     // Wait until it's our turn (0 seconds at next minute).
     $cron_last = variable_get('cron_last', 0);
     $cron_next = floor(($cron_last + 60) / 60) * 60;
     $time = time();
     if ($time < $cron_next) {
         $sleep = $cron_next - $time;
         sleep($sleep);
     }
     // Get settings, so we can determine the poormans cron service group.
     $plugin = _ultimate_cron_plugin_load('launcher', 'background_process_legacy');
     if (!$plugin) {
         $class::unlock($lock_id);
         throw new Exception(t('Failed to load launcher plugin?!?!?!?!'));
     }
     $settings = $plugin->getDefaultSettings();
     // In case launchers fail, we don't want to relaunch this process
     // immediately.
     _ultimate_cron_variable_save('cron_last', time());
     // It's our turn!
     $launchers = array();
     foreach (_ultimate_cron_job_load_all() as $job) {
         $launcher = $job->getPlugin('launcher');
         $launchers[$launcher->name] = $launcher->name;
     }
     foreach ($launchers as $name) {
         $process = new BackgroundProcess('_ultimate_cron_poorman_' . $name);
         $process->uid = 0;
         $process->service_group = $settings['poorman_service_group'];
         $process->start('ultimate_cron_run_launchers', array(array($name)));
     }
     // Bail out if someone stole our lock.
     if (!$class::reLock($lock_id, 60)) {
         return;
     }
     // Wait until it's our turn (0 seconds at next minute).
     $cron_last = _ultimate_cron_variable_load('cron_last', 0);
     $cron_next = floor(($cron_last + 60) / 60) * 60;
     $time = time();
     if ($time < $cron_next) {
         $sleep = $cron_next - $time;
         sleep($sleep);
     }
     // Check poorman settings. If launcher has changed, we don't want
     // to keepalive.
     $poorman = _ultimate_cron_plugin_load('settings', 'poorman');
     if (!$poorman) {
         return;
     }
     $settings = $poorman->getDefaultSettings();
     if (!$settings['launcher'] || $settings['launcher'] !== 'background_process_legacy') {
         return;
     }
     background_process_keepalive();
 }
 public function index($image_id)
 {
     $arrReturn = array();
     $arrImage = array();
     $imageObj = VIImage::select('images.id', 'images.name', 'images.short_name', 'images.description', 'images.keywords', 'images.artist', 'images.author_id', 'image_details.path', 'image_details.width', 'image_details.height', 'image_details.ratio')->join('image_details', 'image_details.image_id', '=', 'images.id')->where('images.id', $image_id)->first();
     if (is_object($imageObj)) {
         if ($imageObj->ratio > 1) {
             $width = 450;
             $height = $width / $imageObj->ratio;
         } else {
             $height = 450;
             $width = $height * $imageObj->ratio;
         }
         $arrImage = ['image_id' => $imageObj->id, 'name' => $imageObj->name, 'short_name' => $imageObj->short_name, 'description' => $imageObj->description, 'keywords' => $imageObj->keywords, 'artist' => $imageObj->artist, 'author_id' => $imageObj->author_id, 'width' => $width, 'height' => $height, 'path' => '/pic/with-logo/' . $imageObj->short_name . '-' . $imageObj->id . '.jpg', 'path_thumb' => '/pic/thumb/' . $imageObj->short_name . '-' . $imageObj->id . '.jpg'];
         $htmlChooseDownload = $this->loadChooseDownload($imageObj->id);
         $product = Product::with(['type', 'sizeLists', 'optionGroups' => function ($query) {
             $query->select('option_groups.id', 'name', 'key');
         }, 'options' => function ($query) {
             $query->select('options.id', 'name', 'key', 'option_group_id');
         }]);
         $product = $product->where('active', 1)->get();
         $arrProduct = array();
         if (is_object($product)) {
             $arrProduct = $product->toArray();
         }
         // echo '<pre>';
         // print_r($arrProduct[0]['option_groups']);
         // echo '</pre>';
         $htmlOrder = $this->loadOrder($arrImage, $arrProduct);
         $htmlSignin = View::make('frontend.account.signin')->with([])->render();
         $arrKeywords = explode(',', $imageObj->keywords);
         $arrReturn['htmlKeywords'] = View::make('frontend.images.view-keywords')->with('arrKeywords', $arrKeywords)->render();
         $action = Input::has('a') ? Input::get('a') : '';
         $arrReturn['htmlMainImage'] = View::make('frontend.images.main-image')->with(['imageObj' => $arrImage, 'htmlChooseDownload' => $htmlChooseDownload, 'htmlOrder' => $htmlOrder, 'htmlSignin' => $htmlSignin, 'action' => $action, 'htmlKeywords' => $arrReturn['htmlKeywords']])->render();
         $arrReturn['htmlSameArtist'] = $this->loadSameArtist($imageObj->author_id);
         $arrReturn['htmlSimilarImages'] = $this->getSimilarImage(array($imageObj->id));
         $this->layout->metaTitle = $imageObj->name;
         //save to recently_view_images table
         //App::make('AccountController')->addToRecentlyViewImages($imageObj->id);
         if (Auth::user()->check()) {
             BackgroundProcess::actionSearch(['type' => 'recently-view', 'image_id' => $imageObj->id, 'user_id' => Auth::user()->get()->id]);
         }
     } else {
         return App::abort(404);
     }
     $arrReturn['imageObj'] = $arrImage;
     $categories = $this->layout->categories;
     $arrReturn['htmlCategories'] = View::make('frontend.account.view-categories')->with('categories', $categories)->render();
     $lightboxes = array();
     if (Auth::user()->check()) {
         $lightboxes = Lightbox::where('user_id', '=', Auth::user()->get()->id)->get()->toArray();
         foreach ($lightboxes as $key => $lightbox) {
             $lightboxes[$key]['total'] = LightboxImages::where('lightbox_id', '=', $lightbox['id'])->count();
         }
     }
     $arrReturn['lightboxes'] = $lightboxes;
     $arrReturn['mod_download'] = Configure::GetValueConfigByKey('mod_download');
     $arrReturn['mod_order'] = Configure::GetValueConfigByKey('mod_order');
     $arrReturn['arrProduct'] = $arrProduct;
     $this->layout->content = View::make('frontend.images.index')->with($arrReturn);
 }
Example #10
0
 function testProcesses()
 {
     SweteDb::q('delete from background_processes');
     require_once 'inc/BackgroundProcess.php';
     $process = new BackgroundProcess();
     $this->assertEquals(null, $process->getProcessId());
     $this->assertEquals(false, $process->isClean());
     $process->save();
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
     $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
     $this->assertEquals(0, intval($prec->val('running')));
     $this->assertEquals(0, intval($prec->val('complete')));
     $this->assertEquals(0, intval($prec->val('error')));
     $process->start();
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertTrue(isset($prec));
     if ($prec) {
         $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
         $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
         $this->assertEquals(0, intval($prec->val('running')));
         $this->assertEquals(1, intval($prec->val('complete')));
         $this->assertEquals(0, intval($prec->val('error')));
     }
     $process = new BackgroundProcess();
     $this->assertEquals(null, $process->getProcessId());
     $this->assertEquals(false, $process->isClean());
     $process->save();
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
     $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
     $this->assertEquals(0, intval($prec->val('running')));
     $this->assertEquals(0, intval($prec->val('complete')));
     $this->assertEquals(0, intval($prec->val('error')));
     BackgroundProcess::runProcess($process->getProcessId());
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertTrue(isset($prec));
     if ($prec) {
         $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
         $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
         $this->assertEquals(0, intval($prec->val('running')));
         $this->assertEquals(1, intval($prec->val('complete')));
         $this->assertEquals(0, intval($prec->val('error')));
     }
     $process = new BackgroundProcess();
     $this->assertEquals(null, $process->getProcessId());
     $this->assertEquals(false, $process->isClean());
     $process->save();
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
     $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
     $this->assertEquals(0, intval($prec->val('running')));
     $this->assertEquals(0, intval($prec->val('complete')));
     $this->assertEquals(0, intval($prec->val('error')));
     $res = BackgroundProcess::runProcesses();
     $this->assertTrue($res);
     if ($res) {
         $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
         $this->assertTrue($prec instanceof Dataface_Record);
         $this->assertTrue(isset($prec));
         if ($prec) {
             $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
             $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
             $this->assertEquals(0, intval($prec->val('running')));
             $this->assertEquals(1, intval($prec->val('complete')));
             $this->assertEquals(0, intval($prec->val('error')));
         }
     }
 }
 public function synchronize()
 {
     BackgroundProcess::sync();
     return Response::json(1);
 }
Example #12
0
 public function searchImage()
 {
     $keyword = Input::has('keyword') ? Input::get('keyword') : '';
     //for recently searched images
     $keyword_searched = $keyword;
     $short_name = Input::has('short_name') ? Input::get('short_name') : '';
     $sort_method = Input::has('sort_method') ? Input::get('sort_method') : 'new';
     $sort_style = Input::has('sort_style') ? Input::get('sort_style') : 'mosaic';
     if (trim($keyword) != '') {
         $keyword = '*' . trim($keyword) . '*';
     }
     $page = Input::has('page') ? Input::get('page') : 1;
     $take = Input::has('take') ? Input::get('take') : 30;
     $type = Input::has('type') ? intval(Input::get('type')) : 0;
     $orientation = Input::has('orientation') ? Input::get('orientation') : 'any';
     $skip = ($page - 1) * $take;
     $category = Input::has('category') ? intval(Input::get('category')) : 0;
     $search_type = Input::has('search_type') ? Input::get('search_type') : 'search';
     $exclude_keyword = Input::has('exclude_keyword') ? '*' . Input::get('exclude_keyword') . '*' : '';
     $exclude_people = Input::has('exclude_people');
     $include_people = Input::has('include_people');
     $gender = Input::has('gender') ? Input::get('gender') : 'any';
     $age = Input::has('age') ? Input::get('age') : 'any';
     $ethnicity = Input::has('ethnicity') ? Input::get('ethnicity') : 'any';
     $number_people = Input::has('number_people') ? Input::get('number_people') : 'any';
     $color = Input::has('color') ? Input::get('color') : '';
     $images = VIImage::select('images.short_name', 'images.id', 'images.name', 'images.main_color', DB::raw('count(lightbox_images.id) as count_favorite'))->leftJoin('lightbox_images', 'images.id', '=', 'lightbox_images.image_id')->groupBy('images.id')->with('downloads');
     switch ($sort_method) {
         case 'new':
             $images = $images->with('statistic')->orderBy('images.id', 'desc')->withType('main')->withOrientation($orientation);
             break;
         case 'relevant':
             $images = $images->with('statistic')->orderBy('images.id', 'desc')->withType('main')->withOrientation($orientation);
             break;
         case 'popular':
             $images = $images->join('statistic_image', 'statistic_image.image_id', '=', 'images.id')->with('statistic')->orderBy('view', 'desc')->orderBy('download', 'desc')->withType('main')->withOrientation($orientation);
             break;
         case 'undiscovered':
             $images = $images->join('statistic_image', 'statistic_image.image_id', '=', 'images.id')->with('statistic')->orderBy('view', 'asc')->orderBy('download', 'asc')->withType('main')->withOrientation($orientation);
             break;
         default:
             $images = $images->with('statistic')->orderBy('images.id', 'desc')->withType('main')->withOrientation($orientation);
             break;
     }
     if ($search_type == 'search' && $keyword != '') {
         $images = $images->search($keyword);
     }
     if ($exclude_keyword != '') {
         $images = $images->notSearchKeyWords($exclude_keyword);
     }
     if ($exclude_people) {
         $images = $images->where('number_people', '=', 0);
     }
     if ($include_people) {
         if ($gender != 'any') {
             $images = $images->where('gender', '=', $gender);
         }
         if ($age != 'any') {
             $age = explode('-', $age);
             $from = intval($age[0]);
             $to = intval($age[1]);
             $images = $images->where('age_from', '<=', $to)->where('age_to', '>=', $from);
         }
         if ($ethnicity != 'any') {
             $images = $images->where('ethnicity', '=', $ethnicity);
         }
         if ($number_people != 'any') {
             if ($number_people < 4) {
                 $images = $images->where('number_people', '=', $number_people);
             } else {
                 $images = $images->where('number_people', '>=', $number_people);
             }
         }
     }
     if (!(Input::has('editorial') && Input::has('non_editorial'))) {
         if (Input::has('editorial')) {
             $images = $images->where('editorial', '=', 1);
         }
         if (Input::has('non_editorial')) {
             $images = $images->where('editorial', '=', 0);
         }
     }
     if ($type != 0) {
         $images = $images->where('type_id', '=', $type);
     }
     if ($category != 0) {
         $images = $images->join('images_categories', 'images_categories.image_id', '=', 'images.id')->where('images_categories.category_id', '=', $category);
     }
     $total_image = $images->count();
     $images = $images->get()->toArray();
     $images = $this->searchColorFromArray($color, $images);
     $total_image = count($images);
     $images = array_slice($images, $skip, $take);
     //pr(DB::getQueryLog());die;
     $total_page = ceil($total_image / $take);
     //for recently searched images
     if ((trim($keyword_searched) != '' || $short_name != '') && count($images) > 0) {
         if ($keyword_searched == '') {
             $keyword_searched = str_replace('-', ' ', $short_name);
         }
         if (Auth::user()->check()) {
             BackgroundProcess::actionSearch(['type' => 'recently-search', 'keyword' => $keyword_searched, 'image_id' => $images[0]['id'], 'user_id' => Auth::user()->get()->id, 'query' => Request::server('REQUEST_URI')]);
         }
         BackgroundProcess::actionSearch(['type' => 'popular-search', 'keyword' => $keyword_searched, 'image_id' => $images[0]['id'], 'query' => Request::server('REQUEST_URI')]);
     }
     $lightboxes = array();
     if (Auth::user()->check()) {
         $lightboxes = Lightbox::where('user_id', '=', Auth::user()->get()->id)->get()->toArray();
         foreach ($lightboxes as $key => $lightbox) {
             $lightboxes[$key]['total'] = LightboxImages::where('lightbox_id', '=', $lightbox['id'])->count();
         }
     }
     $image_action_title = 'Like this item';
     if (Auth::user()->check()) {
         $image_action_title = 'Save to lightbox';
     }
     $arr_sort_method = array('new' => 'New', 'popular' => 'Popular', 'relevant' => 'Relevant', 'undiscovered' => 'Undiscovered');
     $arrReturn = array('images' => $images, 'total_page' => $total_page, "total_image" => $total_image, "sort_style" => $sort_style, "search_type" => $search_type, "categories" => $this->layout->categories, "lightboxes" => $lightboxes, "image_action_title" => $image_action_title, "arr_sort_method" => $arr_sort_method, "sort_method" => $sort_method, 'mod_download' => Configure::GetValueConfigByKey('mod_download'), 'mod_order' => Configure::GetValueConfigByKey('mod_download'));
     if (Request::ajax()) {
         return $arrReturn;
     }
     $this->layout->in_search = 1;
     $this->layout->content = View::make('frontend.search')->with($arrReturn);
 }
 /**
  * @test
  * @covers Cocur\BackgroundProcess\BackgroundProcess::stop()
  * @expectedException \RuntimeException
  */
 public function stopShouldThrowExceptionIfWindows()
 {
     if (!preg_match('/^WIN/', PHP_OS)) {
         $this->markTestSkipped('Cocur\\BackgroundProcess\\BackgroundProcess::stop() is supported on *nix systems ' . 'and does not need to throw an exception.');
         return;
     }
     $process = new BackgroundProcess('sleep 1');
     $process->stop();
 }
Example #14
0
 /**
  * Run the PDF Processor utility.
  * @return  mixed   Total pages or false if failed processing.
  */
 protected function runPdfProcessor($localPDF)
 {
     // Get Chaucer PDF converter
     $ChaucerPDF = Configure::read('processor.ChaucerPDF');
     if (strlen($ChaucerPDF) > 0) {
         if (!strlen($localPDF)) {
             throw new exception("[PdfProcessor::runPdfProcessor] PDF Processing executable not configured");
         }
         if (!file_exists($localPDF)) {
             throw new exception("[PdfProcessor::runPdfProcessor] PDF Processor {$ChaucerPDF} not found");
         }
         // Get book dimensions
         $bookSize = $this->getBookDimensions($localPDF);
         // Get book processing options
         $options = $this->getPdfProcessorSwitches($bookSize['width'], $bookSize['height']);
         // Assemble PDF converter command
         $cmd = "{$ChaucerPDF} {$options} {$localPDF}";
         CakeLog::debug('[PdfProcessor::runPdfProcessor] Executing: ' . $cmd);
         // Set progress and error log files for PDF converter
         $progressFile = Folder::addPathElement($this->workingDir, "progress.txt");
         $errorLogFile = Folder::addPathElement($this->workingDir, "ErrorLog.txt");
         // Create and run new background process
         $pdfProc = new BackgroundProcess($cmd);
         $endTime = time() + PROCESS_TIMEOUT;
         $pdfProc->run($cmd);
         // Preset progress vars
         $maxSteps = 0;
         $lastPage = 0;
         $totalPages = 0;
         // Get and check running process
         $bRunning = true;
         while ($bRunning && time() < $endTime) {
             // Get the progress
             if (file_exists($progressFile)) {
                 // Get contents of the progress file
                 $content = file_get_contents($progressFile);
                 // Check for the string 10/1020. <page>/<page_count>
                 if (strpos($content, '/') !== false) {
                     // Get page info
                     $pageInfo = explode('/', $content);
                     // Check to stat the progress
                     if (!$maxSteps) {
                         $totalPages = (int) $pageInfo[1];
                         if ($totalPages > 0) {
                             $maxSteps = $this->getMaxProgressSteps($totalPages);
                             $this->progress->startProgress($maxSteps);
                         }
                     }
                     // Update step
                     $this->progress->setCurrentStep($pageInfo[0]);
                     if ($lastPage != $pageInfo[0]) {
                         $endTime = time() + PROCESS_TIMEOUT;
                         // reset the timeout window for the pdf processor
                         $lastPage = $pageInfo[0];
                     }
                 } else {
                     // Check if process is running
                     $bRunning = $this->isPdfProcessorRunning($progressFile, $errorLogFile);
                 }
             }
             sleep(2);
         }
         clearstatcache();
         // Check if there were any errors
         if (file_exists($errorLogFile)) {
             throw new Exception('PDF Processor Failed: ' . file_get_contents($errorLogFile));
         }
         CakeLog::debug('[PdfProcessor::runPdfProcessor] Complete: ' . $cmd);
         // Return total pages
         return $totalPages;
     }
     // Return false if PDF Converter was not detected
     return FALSE;
 }