Include YII Application in a script - yii

I would like to create a PHP script which is called via CLI. And I would like that script to have access to the Yii application. Therefore I created a file called (script.php)
// file: script.php
ob_start();
include "index.php"
ob_clean();
echo "This is my script !";
I had to include the *ob_start* and *ob_clean* as if I didn't the layout of the application was being render.
Is this the right way to do this? Or should I be doing something else?

If you want to deal with php console, it is highly recommended to use CConsoleCommand. You can create your file which must act in console in protected/commands path. So your class would be something like below:
class TestConsoleCommand extends CConsoleCommand{}
You can implement your php codes into your functions, but note that the most important method in your class would be init() method. Once your consoleCommand runs, this method will be called.
another important note is that, THE CONFIG OF CONSOLE COMMAND IS DIFFERENT WITH NON-CONSOLE. It means that while your ordinary Yii application is running, you are using main.php config file, which is situated in protected/config/main.php. In order to access your models and database config ... in your Yii ConsoleApp, You must fulfill the console.php config file which is situated in protected/config/console.php.
You can configure your database connection in this file, which you want to access it, while your ConsoleApp is running. You can also import all your classes you need to interact.
Some useful links and documents:
CConsoleCommand - represents an executable console command
The Definitive Guide to Yii - Console Applications
I hope it helps :)

Related

Can a dotnet core library app use the appsettings.json file from the consuming application?

Hi I am trying to set up a dotnet core library app that requires certain information from a appsettings.json to run. I understand how to have an application use the appsettings.json file via the builder etc. However I also want my library to use this file for its own configuration. Obviously the consuming app would have to know the settings to have in the appsettings.json file but for my purposes that is not a problem. Does anyone have an example of this working? What I have found so far are not that great and involve loading the appsettings.json every time we instantiate the configuration class in the library. There must be a better way than this.
That's exactly Options are created for!
You should not read settings file - DI will give you strongly-typed class/struct with parameters you need. Loaded from all configured sources (appsettings.json, environment variables etc), and updated automatically (if required/configured).
Check this documentation - sample is pretty short and copy-pasting it here isn't wise.

Web2py & nginx - do I have to set up static folder

I'm running nginx/uWSGI and trying to lock down a web2py site using 'auth.requires_login()' (https://groups.google.com/forum/#!topic/web2py/0j92-sPp4bc) so that only logged in users can get to it, even the content under /static/. If I set up nginx config file with
location ~* /(\w+)/static/ {
root /home/www-data/web2py/applications/;
}
as recommended in the docs, won't that bypass the access control, and allow anyone to see the static content? If I leave this line out of the config file, will web2py still share the static content to logged-in users (although presumably a little slower)?
Yes, using that nginx rule will bypass web2py. Removing it and letting web2py handle /static/ won't change much either, as this is directly from the web2py manual:
http://127.0.0.1:8000/a/static/filename
There is no controller called "static". web2py interprets this as a request for the file called "filename" in the subfolder "static" of the application "a".
When static files are downloaded, web2py does not create a session, nor does it issue a cookie or execute the models.
So because there is no controller, you cannot directly use auth.requires_login() for static content. This is because files in /static/ are generally not meant to be access-controlled, or else browsers will not be able to get the css, js, etc. needed to even render the welcome or login page.
However, if you still want site-wide access control to static files (i.e. private pdf files) you can do it like so:
in your application directory, create a folder called private_static
then in your controller add the following:
#default.py
import os
auth.requires_login()
def private_static():
filename = os.path.join(*request.args)
allowed = ['private1.pdf', 'private2.pdf'] # have some kind of validation for security!
if not filename in allowed:
raise HTTP(404)
return response.stream(open(os.path.join(request.folder, 'private_static', filename)))
and in your view something like the following:
Private Document
Now accessing http://.../private_static/private1.pdf will force the user to login before getting the static file.

authentification of Ipython notebook using c.NotebookApp.login_handler_class parameter in config file

I've setup a Ipython 3.2.1 Notebook server. However, I'm looking for a way to add our own authentication rule, which means I want to add my own authentication security authentication mechanism similar to LDAP, OAuth to notebook.
I don't want to use JupyterHub, since it's too complex for me to use. However,
I know from this site two factor authentication with username and password for a Jupyter Notebook server that we can deal with " The login handler class to use.
c.NotebookApp.login_handler_class = 'notebook.auth.login.LoginHandler'
in notebook configure file, but I don't know how to do it, do I need to write another loginHandler class to overwrite it? if so, which directory should I put this class file in?
Yes, you can modify the behavior of the LoginHandler by extending it. Something like the code below where I am overwriting the method _render.
class MyLoginHandler(LoginHandler):
def _render(self, message=None):
# ... this is my custom code
Then you have to modify the Jupyter file to something like:
c.NotebookApp.login_handler_class = 'myModule.MyLoginHandler'

Output List of message routes

As part of my startup sequence, I am attempting to log the routes that are defined in the configuration. I see (in the debugger) that what I am looking for is exposed in "StaticMessageRouter.routes". Is there any way to get programmatic access to these routes? This would allow me to see (in the log file) which commands/events defined in this service as well as seeing what is in the UnicastBusConfig section in the config file.
Does this make sense? Is there any other way to accomplish the same goal?

Issues with intern-runner and proxyUrl that contains subfolders

I need to setup intern to test ajax calls from a different server. I set everything up sort of following the official wiki in this address
https://github.com/theintern/intern/wiki/Using-Intern-to-unit-test-Ajax-calls
My config file has proxyUrl set to http://localhost:8080/sub
and http://localhost:8080/sub is setup as a reverse proxy to inter-runner in http://localhost:9000
When I run ./node_modules/.bin/intern-runner -config=tests/config from the tests root folder, the browser opens up and is able to request several files, until it tries to request the config file. That's when it receives a 404, because it requests the wrong address - http://localhost:8080/tests/config.js - without the sub folder.
I'm wondering if I'm missing something inside the config file, or if intern is not able to use proxies with subfolders. I tried to set the baseUrl parameter, but it had no effect.
Any ideas?
Update:
It seems that sometimes intern-runner uses the path provided in the config param, and sometimes it uses the one in the proxyUrl parameter inside the config file. As a workaround, what I did was to place the config file and the tests on 2 folders (actually I made a symbolic link). The first on tests/ and the second on sub/tests/ and ran it using ./node_modules/.bin/intern-runner -config=sub/tests/config.
It works, but it's kind of stupid and I really wished there was a better way to do it.
This is indeed a limitation/bug of intern. It assumes that the proxy sits at the root of the absolute domain name, i.e. that it has a pathname of /.
An issue has been created on intern's github repository here and the corresponding pull request that fixes the problem is here. Hopefully this gets merged into the upcoming 2.1 release of intern.