1: <?php
2: /**
3: * A package management object meant for OpenBSD
4: *
5: * this object should allow; listing, verifying, updating, removing and installing
6: * OpenBSD port packages.
7: * Meant to virtualize the base command line tools so we can later port
8: * this object to other OSes like Linux, CentOS, etc..
9: *
10: */
11: class Package {
12: public $SUPPORTED_COMMANDS=array("list"=>"List_Packages","version"=>"Verify_Package","update"=>"Update_Package","delete","install"=>"install");
13: public $Repos = array();
14: public $Digest=array();
15:
16:
17: /**
18: * Package::__construct()
19: * Make sure to instantiate this object with an Array list of Repositories
20: * @param mixed $array_Repositories
21: */
22: function __construct($array_Repositories=null){
23: if (is_null($array_Repositories)) {
24: // emit an error/warning ?
25: print "Repositories array not set\n";
26: $this->Repos[0]="not set!";
27: } else {
28: if (is_array($array_Repositories)) {
29: // all fine
30: print "Got an array for Repos\n";
31: //print_r($array_Repositories);
32: $this->Repos = $array_Repositories;
33: //print "\nAnd stored it as:\n";
34: //print_r($this->Repos);
35: } else {
36: print "Got a string for Repos\n";
37: $this->Repos[0] = $array_Repositories;
38: }
39: foreach ($this->Repos as $x=>$folder ){
40: if (substr($folder,-1)!=="/") {
41: $this->Repos[$x].="/";
42: }
43: }
44: }
45: }
46:
47: /**
48: * Package::Version()
49: * Get the installed version of a specific package
50: * @param mixed $Pkg_Name
51: * @param boolean $use_REST boolean, true for REST, false for JSON
52: * @param boolean $use_SHORT
53: * @return string $Version
54: */
55: public function Version($Pkg_Name=null,$use_REST=false,$use_SHORT=false){
56: if (is_null($Pkg_Name)) {
57: return false;
58: } else {
59: $result=trim(`pkg_info | grep "^$Pkg_Name" | awk '{print ($1)}' | sed 's/^$Pkg_Name-//g'`);
60: }
61: if ($use_REST) {
62: $tmp_result=preg_split("/\\n/",$result);
63: return json_encode($tmp_result);
64: }
65: return $result;
66: }
67:
68: /**
69: * Package::isInstalled()
70: * Verify if a package is installed
71: * @param mixed $Pkg_Name
72: * @return boolean $result
73: */
74: public function isInstalled($Pkg_Name){
75:
76: $result=intval(trim(`pkg_info | grep "^$Pkg_Name" | wc -l`));
77:
78: if ($result > 0) {
79: return true;
80: } else {
81: return false;
82: }
83: }
84:
85: /**
86: * Package::Package_List()
87: * Obtain a list of installed packages.
88: * @param mixed $Pkg_Name
89: * @param boolean $use_REST
90: * @param boolean $use_SHORT
91: * @return boolean $result
92: */
93: public function Package_List($Pkg_Name=null,$use_REST=false,$use_SHORT=false){
94: if (is_null($Pkg_Name)) {
95: $result=trim(`pkg_info | awk '{print ($1)}'`);
96: } else {
97: $result=trim(`pkg_info | grep "^$Pkg_Name" | awk '{print ($1)}'`);
98: }
99: if ($use_REST) {
100: $tmp_result=preg_split("/\\n/",$result);
101: return json_encode($tmp_result);
102: }
103: return $result;
104: }
105:
106:
107: /**
108: * Package::Pick_A_Repos()
109: * Internal method which returns a random repository from the list.
110: * @return string $repository_line
111: */
112: private function Pick_A_Repos(){
113: //print_r($this->Repos);
114: $num_repos = count($this->Repos);
115: if ($num_repos > 1) {
116: // pick a random one
117: $x = array_rand($this->Repos,1);
118: // $x=rand(1,$num_repos)-1;
119: return $this->Repos[$x];
120: } else {
121: return $this->Repos[0];
122: }
123: }
124:
125: /**
126: * Package::Install()
127: * Install a particular package ( using the OpenBSD package methodology )
128: * @param mixed $Pkg_Name
129: * @param boolean $use_REST
130: * @param boolean $use_SHORT
131: * @param mixed $Repository
132: * @return boolean $result
133: */
134: public function Install($Pkg_Name,$use_REST=false,$use_SHORT=false,$Repository=null){
135: global $LOGLEVEL;
136: /**
137: * TODO: A global way of defining the Repository variable, which
138: * could be multiple, and thus, perhaps, a global function to retrieve ONE.
139: *
140: */
141:
142: if (is_null($Repository)) {
143: $Selected_Repos = $this->Pick_A_Repos();
144: } else {
145: $Selected_Repos = $Repository;
146: }
147: /**
148: * TODO: We might want to implement a version checking for possible updating
149: *
150: */
151:
152: if ($this->isInstalled($Pkg_Name)) {
153: print __CLASS__."->".__METHOD__." : $Pkg_Name from $Selected_Repos is already installed\n";
154: return true;
155: }
156:
157: if (isset($LOGLEVEL) AND $LOGLEVEL > 0) print __CLASS__."->".__METHOD__." : Will install $Pkg_Name from $Selected_Repos\n";
158: //return true;
159: $result=`env PKG_PATH="$Selected_Repos" pkg_add -I $Pkg_Name`;
160: if (preg_match("/^Can't find/",$result)===false) {
161: // all good it seems... ?
162:
163: /**
164: * On OpenBSD 5.3+, we now need to add the installed package
165: * starter option in /etc/rc.conf.local in the pkg_scripts="" variable.
166: * But we leave that to the Service object so we can actually treat it like a service daemon
167: */
168:
169:
170: if ($use_REST) {
171: return json_encode("0");
172: } else {
173: print __CLASS__."->".__METHOD__." : Installed $Pkg_Name from $Selected_Repos\n";
174: return true;
175: }
176: } else {
177: if ($use_REST) {
178: return json_encode("1");
179: } else {
180: print __CLASS__."->".__METHOD__." : ERROR INSTALLING $Pkg_Name from $Selected_Repos\n";
181:
182: return false;
183: }
184: }
185: }
186: }
187: ?>
188: